reindent-20030715
[openafs.git] / src / package / fileops.c
1 /*
2   *     (C) Copyright 10/12/86 by Carnegie Mellon University
3   */
4
5 /*
6  * Revision 1.2  89/09/13  11:36:48
7  * Various fixes so it can compile under AIX.
8  * 
9  * Revision 1.1  89/06/14  11:06:03
10  * Initial revision
11  * 
12  * Revision 1.3  88/07/28  13:41:05
13  * working simulation moved over to the beta cell
14  * 
15  * Revision 1.2  88/02/26  05:07:56
16  * simulation of package with a yacc parser
17  * 
18  * Revision 1.1  88/02/23  02:17:14
19  * Initial revision
20  * 
21  */
22
23 #include <afs/param.h>
24 #include <sys/param.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/file.h>
29 #include <dirent.h>
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #else
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #endif
37 #include <errno.h>
38 #ifdef  AFS_SUN5_ENV
39 #include <sys/fcntl.h>
40 #endif
41 #include "globals.h"
42 #include "package.h"
43
44
45 int
46 mv(from, to)
47      register char *from;
48      register char *to;
49 {
50     loudonly_message("mv %s %s", from, to);
51     if (!opt_lazy && rename(from, to) < 0) {
52 #if defined(AFS_HPUX_ENV)
53         char pnameBusy[512];
54
55         if (errno == ETXTBSY) {
56             (void)strcpy(pnameBusy, to);
57             (void)strcat(pnameBusy, ".BUSY");
58             if (rename(to, pnameBusy) == 0) {
59                 if (rename(from, to) < 0) {
60                     unlink(pnameBusy);
61                     if (errno == ETXTBSY) {
62                         message("rename %s %s; %m (ignored)", from, to);
63                         return 0;
64                     }
65                     message("rename %s %s; %m", from, to);
66                     return -1;
67                 }
68                 unlink(pnameBusy);
69                 return 0;
70             } else if (errno == ETXTBSY) {
71                 message("rename %s %s; %m (ignored)", to, pnameBusy);
72                 return 0;
73             }
74         }
75 #endif /* AFS_HPUX_ENV */
76         message("rename %s %s; %m", from, to);
77         return -1;
78     }
79     return 0;
80 }
81
82 int
83 rm(path)
84      register char *path;
85 {
86     register char *endp;
87     register struct dirent *de;
88     register DIR *dp;
89     struct stat stb;
90
91     if (lstat(path, &stb) < 0) {
92         /* message("lstat %s; %m",path); */
93         return;
94     }
95 #ifdef  KFLAG
96     if (opt_kflag && (stb.st_mode & 0222) == 0) {
97         loudonly_message("INHIBIT %s removal", path);
98         return;
99     }
100 #endif /* KFLAG */
101     if ((stb.st_mode & S_IFMT) != S_IFDIR) {
102         loudonly_message("rm %s", path);
103         if (!opt_lazy && unlink(path) < 0) {
104             message("unlink %s; %m", path);
105             return;
106         }
107         return;
108     }
109     endp = path + strlen(path);
110     if ((dp = opendir(path)) == 0) {
111         message("opendir %s; %m", path);
112         return;
113     }
114     *endp++ = '/';
115     while ((de = readdir(dp)) != 0) {
116         if (de->d_name[0] == '.') {
117             if (de->d_name[1] == 0)
118                 continue;
119             if (de->d_name[1] == '.' && de->d_name[2] == 0)
120                 continue;
121         }
122         (void)strcpy(endp, de->d_name);
123         (void)rm(path);
124     }
125     *--endp = 0;
126     (void)closedir(dp);
127     loudonly_message("rmdir %s", path);
128     if (!opt_lazy && rmdir(path) < 0) {
129         message("rmdir %s; %m", path);
130         return;
131     }
132     return;
133 }
134
135
136
137 int
138 cp(from, to)
139      register char *from;
140      register char *to;
141 {
142     register int ffd, tfd, cc;
143     char buffer[8192];
144
145     loudonly_message("cp %s %s", from, to);
146     if (opt_lazy)
147         return 0;
148     if ((ffd = open(from, O_RDONLY)) < 0) {
149         message("open %s; %m", from);
150         return -1;
151     }
152     if ((tfd = open(to, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
153         message("open %s; %m", to);
154         (void)close(ffd);
155         return -1;
156     }
157     for (;;) {
158         if ((cc = read(ffd, buffer, sizeof(buffer))) < 0) {
159             message("read %s; %m", from);
160             (void)close(ffd);
161             (void)close(tfd);
162             return -1;
163         }
164         if (cc == 0)
165             break;
166         if (cc != write(tfd, buffer, cc)) {
167             message("write %s; %m", to);
168             (void)close(ffd);
169             (void)close(tfd);
170             return -1;
171         }
172     }
173     if (close(ffd) < 0) {
174         message("close %s; %m", from);
175         (void)close(tfd);
176         return -1;
177     }
178     if (close(tfd) < 0) {
179         message("close %s; %m", to);
180         return -1;
181     }
182     return 0;
183 }
184
185
186 int
187 ln(from, to)
188      register char *from;
189      register char *to;
190 {
191     loudonly_message("ln %s %s", from, to);
192     if (!opt_lazy && link(from, to) < 0) {
193         message("ln %s %s; %m", from, to);
194         return -1;
195     }
196     return 0;
197 }
198
199
200
201
202 int
203 mklostfound(path)
204      register char *path;
205 {
206     register char *u, *l, *endp;
207     register int f;
208     struct stat stb;
209
210     loudonly_message("mklost+found %s", path);
211     if (opt_lazy)
212         return 0;
213     endp = path + strlen(path);
214     *endp++ = '/';
215     endp[2] = 0;
216     for (u = "0123456789abcdef"; *u; u++) {
217         for (l = "0123456789abcdef"; *l; l++) {
218             endp[0] = *u;
219             endp[1] = *l;
220             f = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
221             if (f < 0) {
222                 message("open %s; %m", path);
223                 continue;
224             }
225             (void)close(f);
226         }
227     }
228     for (u = "0123456789abcdef"; *u; u++) {
229         for (l = "0123456789abcdef"; *l; l++) {
230             endp[0] = *u;
231             endp[1] = *l;
232             if (lstat(path, &stb) >= 0)
233                 if ((stb.st_mode & S_IFMT) != S_IFDIR)
234                     if (unlink(path) < 0)
235                         message("unlink %s; %m", path);
236         }
237     }
238     *--endp = 0;
239     return 0;
240 }