1 | /* |
---|
2 | * $Id: files.c,v 4.12 1999-01-22 23:15:59 ghudson Exp $ |
---|
3 | */ |
---|
4 | |
---|
5 | #ifndef lint |
---|
6 | static char *rcsid_header_h = "$Id: files.c,v 4.12 1999-01-22 23:15:59 ghudson Exp $"; |
---|
7 | #endif lint |
---|
8 | |
---|
9 | #include "mit-copyright.h" |
---|
10 | #include "bellcore-copyright.h" |
---|
11 | |
---|
12 | #include "track.h" |
---|
13 | |
---|
14 | /* |
---|
15 | * Check to see if a file is older than a certain amount of time. |
---|
16 | */ |
---|
17 | |
---|
18 | int |
---|
19 | too_old( name, maxtime) |
---|
20 | char *name; |
---|
21 | long maxtime; |
---|
22 | { |
---|
23 | struct stat sbuf; |
---|
24 | long now; |
---|
25 | int retval; |
---|
26 | |
---|
27 | time(&now); |
---|
28 | if(stat(name,&sbuf) == -1){ |
---|
29 | sprintf(errmsg, |
---|
30 | "can't find file %s in routine too_old()", |
---|
31 | name); |
---|
32 | do_panic(); |
---|
33 | } |
---|
34 | |
---|
35 | retval = ((sbuf.st_mtime + maxtime) <= now); |
---|
36 | |
---|
37 | if (debug) |
---|
38 | printf("too_old(%s,%ld): %d\n",name,maxtime,retval); |
---|
39 | |
---|
40 | return (retval); |
---|
41 | } |
---|
42 | |
---|
43 | follow_link( name, retval) |
---|
44 | char *name, *retval; |
---|
45 | { |
---|
46 | int cc; |
---|
47 | if (0 >= ( cc = readlink( name, retval, LINELEN))) { |
---|
48 | sprintf( errmsg, "can't read link: %s\n", name); |
---|
49 | do_gripe(); |
---|
50 | return( -1); |
---|
51 | } |
---|
52 | retval[ cc] = '\0'; |
---|
53 | |
---|
54 | /* |
---|
55 | if ( verboseflag) |
---|
56 | fprintf( stderr, "following link: %s -> %s\n", name, retval); |
---|
57 | */ |
---|
58 | |
---|
59 | return( 0); |
---|
60 | } |
---|
61 | |
---|
62 | /* |
---|
63 | * if the parent-dir exists, returns 0. |
---|
64 | * returns the length of the parent's pathname, otherwise. |
---|
65 | * this weird returned-value allows makepath |
---|
66 | * to recurse without lots of strcpy's. |
---|
67 | */ |
---|
68 | int |
---|
69 | findparent(path) |
---|
70 | char *path; |
---|
71 | { |
---|
72 | char *tmp; |
---|
73 | int retval; |
---|
74 | |
---|
75 | /* |
---|
76 | ** assume / exists |
---|
77 | */ |
---|
78 | if (! strcmp( path,"/")) |
---|
79 | return(-1); |
---|
80 | |
---|
81 | if (!( tmp = strrchr( path,'/'))) { |
---|
82 | sprintf(errmsg,"checkroot can't find / in %s", path); |
---|
83 | do_gripe(); |
---|
84 | return(-1); |
---|
85 | } |
---|
86 | if ( tmp == path) return( 0); /* root is the parent-dir */ |
---|
87 | |
---|
88 | *tmp = '\0'; |
---|
89 | retval = access( path, 0) ? tmp - path - 1 : 0; |
---|
90 | *tmp = '/'; |
---|
91 | return( retval); |
---|
92 | } |
---|
93 | |
---|
94 | int makepath( path,s) |
---|
95 | char *path; |
---|
96 | struct stat *s; |
---|
97 | { |
---|
98 | char parent[ LINELEN]; |
---|
99 | int n, usave; |
---|
100 | |
---|
101 | if ( 0 < ( n = findparent( path))) { |
---|
102 | parent[ n] = '\0'; |
---|
103 | if ( makepath( parent, s)) return( -1); |
---|
104 | parent[ n] = '/'; |
---|
105 | } |
---|
106 | else if ( 0 > n) return( -1); |
---|
107 | |
---|
108 | if ( verboseflag) |
---|
109 | fprintf( stderr,"making root directory %s mode %o\n", |
---|
110 | path, MODE( *s)); |
---|
111 | |
---|
112 | usave = umask( 022); |
---|
113 | |
---|
114 | if ( mkdir( path, MODE( *s))) { |
---|
115 | sprintf(errmsg, "can't create directory %s", path); |
---|
116 | do_gripe(); |
---|
117 | umask( usave); |
---|
118 | return(-1); |
---|
119 | } |
---|
120 | umask( usave); |
---|
121 | |
---|
122 | if ( set_prots( path, s)) return(-1); |
---|
123 | |
---|
124 | return (0); |
---|
125 | } |
---|
126 | |
---|
127 | int |
---|
128 | removeit(name, type) |
---|
129 | char *name; |
---|
130 | unsigned int type; |
---|
131 | { |
---|
132 | #ifdef POSIX |
---|
133 | struct dirent *next; |
---|
134 | #else |
---|
135 | struct direct *next; |
---|
136 | #endif |
---|
137 | DIR *dirp; |
---|
138 | char *leaf, *type_str = ""; |
---|
139 | struct stat sbuf; |
---|
140 | |
---|
141 | /* caller can pass us the file-type, if he's got it: |
---|
142 | */ |
---|
143 | if ( type != 0); |
---|
144 | else if ( lstat( name, &sbuf)) { |
---|
145 | sprintf( errmsg, "(removeit) can't lstat %s\n", name); |
---|
146 | do_gripe(); |
---|
147 | return(-1); |
---|
148 | } |
---|
149 | else type = TYPE( sbuf); |
---|
150 | |
---|
151 | if (verboseflag) { |
---|
152 | switch( type) { |
---|
153 | case S_IFBLK: type_str = "block special device"; break; |
---|
154 | case S_IFCHR: type_str = "char special device"; break; |
---|
155 | case S_IFDIR: type_str = "directory"; break; |
---|
156 | case S_IFREG: type_str = "regular file"; break; |
---|
157 | case S_IFLNK: type_str = "symbolic link"; break; |
---|
158 | } |
---|
159 | fprintf(stderr,"removing %s %s\n",type_str,name); |
---|
160 | } |
---|
161 | if ( type == S_IFDIR); |
---|
162 | else if ( unlink( name)) { |
---|
163 | sprintf( errmsg, "can't remove %s %s", type_str, name); |
---|
164 | do_gripe(); |
---|
165 | return(-1); |
---|
166 | } |
---|
167 | else return(0); |
---|
168 | |
---|
169 | if (!(dirp = (DIR *) opendir( name))) { |
---|
170 | sprintf(errmsg, "removeit: error from opendir of %s", name); |
---|
171 | do_gripe(); |
---|
172 | return(-1); |
---|
173 | } |
---|
174 | |
---|
175 | strcat( name,"/"); /* XXX: don't copy for recursive call */ |
---|
176 | leaf = name + strlen( name); |
---|
177 | for( next = readdir(dirp); next != NULL; next = readdir(dirp)) { |
---|
178 | if ((! next->d_name[0]) || /* "" */ |
---|
179 | ((next->d_name[0] == '.') && |
---|
180 | ((! next->d_name[1]) || /* "." */ |
---|
181 | ((next->d_name[1] == '.') && |
---|
182 | (! next->d_name[2]))))) /* ".." */ |
---|
183 | continue; |
---|
184 | strcpy( leaf, next->d_name); /* changes name[] */ |
---|
185 | removeit( name, 0); |
---|
186 | } |
---|
187 | leaf[-1] = '\0'; /* XXX: see strcat, above */ |
---|
188 | |
---|
189 | (void) closedir(dirp); |
---|
190 | |
---|
191 | if ( rmdir( name) == -1) { |
---|
192 | sprintf(errmsg, "can't remove directory %s",name); |
---|
193 | do_gripe(); |
---|
194 | return(-1); |
---|
195 | } |
---|
196 | return(0); |
---|
197 | } |
---|