1 | /* |
---|
2 | * $Id: files.c,v 4.14 1999-12-16 01:58:10 danw Exp $ |
---|
3 | */ |
---|
4 | |
---|
5 | #ifndef lint |
---|
6 | static char *rcsid_header_h = "$Id: files.c,v 4.14 1999-12-16 01:58:10 danw Exp $"; |
---|
7 | #endif |
---|
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 | struct dirent *next; |
---|
133 | DIR *dirp; |
---|
134 | char *leaf, *type_str = ""; |
---|
135 | struct stat sbuf; |
---|
136 | |
---|
137 | /* caller can pass us the file-type, if he's got it: |
---|
138 | */ |
---|
139 | if ( type != 0); |
---|
140 | else if ( lstat( name, &sbuf)) { |
---|
141 | sprintf( errmsg, "(removeit) can't lstat %s\n", name); |
---|
142 | do_gripe(); |
---|
143 | return(-1); |
---|
144 | } |
---|
145 | else type = TYPE( sbuf); |
---|
146 | |
---|
147 | if (verboseflag) { |
---|
148 | switch( type) { |
---|
149 | case S_IFBLK: type_str = "block special device"; break; |
---|
150 | case S_IFCHR: type_str = "char special device"; break; |
---|
151 | case S_IFDIR: type_str = "directory"; break; |
---|
152 | case S_IFREG: type_str = "regular file"; break; |
---|
153 | case S_IFLNK: type_str = "symbolic link"; break; |
---|
154 | } |
---|
155 | fprintf(stderr,"removing %s %s\n",type_str,name); |
---|
156 | } |
---|
157 | if ( type == S_IFDIR); |
---|
158 | else if ( unlink( name)) { |
---|
159 | sprintf( errmsg, "can't remove %s %s", type_str, name); |
---|
160 | do_gripe(); |
---|
161 | return(-1); |
---|
162 | } |
---|
163 | else return(0); |
---|
164 | |
---|
165 | if (!(dirp = (DIR *) opendir( name))) { |
---|
166 | sprintf(errmsg, "removeit: error from opendir of %s", name); |
---|
167 | do_gripe(); |
---|
168 | return(-1); |
---|
169 | } |
---|
170 | |
---|
171 | strcat( name,"/"); /* XXX: don't copy for recursive call */ |
---|
172 | leaf = name + strlen( name); |
---|
173 | for( next = readdir(dirp); next != NULL; next = readdir(dirp)) { |
---|
174 | if ((! next->d_name[0]) || /* "" */ |
---|
175 | ((next->d_name[0] == '.') && |
---|
176 | ((! next->d_name[1]) || /* "." */ |
---|
177 | ((next->d_name[1] == '.') && |
---|
178 | (! next->d_name[2]))))) /* ".." */ |
---|
179 | continue; |
---|
180 | strcpy( leaf, next->d_name); /* changes name[] */ |
---|
181 | removeit( name, 0); |
---|
182 | } |
---|
183 | leaf[-1] = '\0'; /* XXX: see strcat, above */ |
---|
184 | |
---|
185 | (void) closedir(dirp); |
---|
186 | |
---|
187 | if ( rmdir( name) == -1) { |
---|
188 | sprintf(errmsg, "can't remove directory %s",name); |
---|
189 | do_gripe(); |
---|
190 | return(-1); |
---|
191 | } |
---|
192 | return(0); |
---|
193 | } |
---|