]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 | |
5 | * Stelian Pop <pop@cybercable.fr>, 1999-2000 | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Copyright (c) 1983, 1993 | |
10 | * The Regents of the University of California. All rights reserved. | |
11 | * (c) UNIX System Laboratories, Inc. | |
12 | * All or some portions of this file are derived from material licensed | |
13 | * to the University of California by American Telephone and Telegraph | |
14 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
15 | * the permission of UNIX System Laboratories, Inc. | |
16 | * | |
17 | * Redistribution and use in source and binary forms, with or without | |
18 | * modification, are permitted provided that the following conditions | |
19 | * are met: | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * 2. Redistributions in binary form must reproduce the above copyright | |
23 | * notice, this list of conditions and the following disclaimer in the | |
24 | * documentation and/or other materials provided with the distribution. | |
25 | * 3. All advertising materials mentioning features or use of this software | |
26 | * must display the following acknowledgement: | |
27 | * This product includes software developed by the University of | |
28 | * California, Berkeley and its contributors. | |
29 | * 4. Neither the name of the University nor the names of its contributors | |
30 | * may be used to endorse or promote products derived from this software | |
31 | * without specific prior written permission. | |
32 | * | |
33 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
34 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
35 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
37 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
39 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
40 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
41 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
42 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
43 | * SUCH DAMAGE. | |
44 | */ | |
45 | ||
46 | #ifndef lint | |
47 | static const char rcsid[] = | |
48 | "$Id: dirs.c,v 1.7 2000/01/21 10:17:41 stelian Exp $"; | |
49 | #endif /* not lint */ | |
50 | ||
51 | #include <sys/param.h> | |
52 | #include <sys/file.h> | |
53 | #include <sys/stat.h> | |
54 | ||
55 | #ifdef __linux__ | |
56 | #include <linux/ext2_fs.h> | |
57 | #include <bsdcompat.h> | |
58 | #else /* __linux__ */ | |
59 | #include <ufs/ufs/dinode.h> | |
60 | #include <ufs/ufs/dir.h> | |
61 | #endif /* __linux__ */ | |
62 | #include <protocols/dumprestore.h> | |
63 | ||
64 | #include <compaterr.h> | |
65 | #include <errno.h> | |
66 | #include <stdio.h> | |
67 | #include <stdlib.h> | |
68 | #include <string.h> | |
69 | #include <unistd.h> | |
70 | ||
71 | #ifdef __linux__ | |
72 | #include <endian.h> | |
73 | #else | |
74 | #include <machine/endian.h> | |
75 | #endif | |
76 | ||
77 | #include "pathnames.h" | |
78 | #include "restore.h" | |
79 | #include "extern.h" | |
80 | ||
81 | /* | |
82 | * Symbol table of directories read from tape. | |
83 | */ | |
84 | #define HASHSIZE 1000 | |
85 | #define INOHASH(val) (val % HASHSIZE) | |
86 | struct inotab { | |
87 | struct inotab *t_next; | |
88 | ino_t t_ino; | |
89 | int32_t t_seekpt; | |
90 | int32_t t_size; | |
91 | }; | |
92 | static struct inotab *inotab[HASHSIZE]; | |
93 | ||
94 | /* | |
95 | * Information retained about directories. | |
96 | */ | |
97 | struct modeinfo { | |
98 | ino_t ino; | |
99 | struct timeval timep[2]; | |
100 | mode_t mode; | |
101 | uid_t uid; | |
102 | gid_t gid; | |
103 | unsigned int flags; | |
104 | }; | |
105 | ||
106 | /* | |
107 | * Definitions for library routines operating on directories. | |
108 | */ | |
109 | #undef DIRBLKSIZ | |
110 | #define DIRBLKSIZ 1024 | |
111 | struct rstdirdesc { | |
112 | int dd_fd; | |
113 | int32_t dd_loc; | |
114 | int32_t dd_size; | |
115 | char dd_buf[DIRBLKSIZ]; | |
116 | }; | |
117 | ||
118 | /* | |
119 | * Global variables for this file. | |
120 | */ | |
121 | static long seekpt; | |
122 | static FILE *df, *mf; | |
123 | static RST_DIR *dirp; | |
124 | static char dirfile[MAXPATHLEN] = "#"; /* No file */ | |
125 | static char modefile[MAXPATHLEN] = "#"; /* No file */ | |
126 | static char dot[2] = "."; /* So it can be modified */ | |
127 | ||
128 | /* | |
129 | * Format of old style directories. | |
130 | */ | |
131 | #define ODIRSIZ 14 | |
132 | struct odirect { | |
133 | u_short d_ino; | |
134 | char d_name[ODIRSIZ]; | |
135 | }; | |
136 | ||
137 | #ifdef __linux__ | |
138 | static struct inotab *allocinotab __P((ino_t, struct new_bsd_inode *, long)); | |
139 | #else | |
140 | static struct inotab *allocinotab __P((ino_t, struct dinode *, long)); | |
141 | #endif | |
142 | static void dcvt __P((struct odirect *, struct direct *)); | |
143 | static void flushent __P((void)); | |
144 | static struct inotab *inotablookup __P((ino_t)); | |
145 | static RST_DIR *opendirfile __P((const char *)); | |
146 | static void putdir __P((char *, size_t)); | |
147 | static void putent __P((struct direct *)); | |
148 | static void rst_seekdir __P((RST_DIR *, long, long)); | |
149 | static long rst_telldir __P((RST_DIR *)); | |
150 | static struct direct *searchdir __P((ino_t, char *)); | |
151 | ||
152 | /* | |
153 | * Extract directory contents, building up a directory structure | |
154 | * on disk for extraction by name. | |
155 | * If genmode is requested, save mode, owner, and times for all | |
156 | * directories on the tape. | |
157 | */ | |
158 | void | |
159 | extractdirs(int genmode) | |
160 | { | |
161 | register int i; | |
162 | #ifdef __linux__ | |
163 | register struct new_bsd_inode *ip; | |
164 | #else | |
165 | register struct dinode *ip; | |
166 | #endif | |
167 | struct inotab *itp; | |
168 | struct direct nulldir; | |
169 | int fd; | |
170 | ||
171 | Vprintf(stdout, "Extract directories from tape\n"); | |
172 | (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir, | |
173 | (long)dumpdate); | |
174 | if (command != 'r' && command != 'R') { | |
175 | (void) strncat(dirfile, "-XXXXXX", | |
176 | sizeof(dirfile) - strlen(dirfile)); | |
177 | fd = mkstemp(dirfile); | |
178 | } else | |
179 | fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666); | |
180 | if (fd == -1 || (df = fdopen(fd, "w")) == NULL) { | |
181 | if (fd != -1) | |
182 | close(fd); | |
183 | err(1, "cannot create directory temporary %s", dirfile); | |
184 | } | |
185 | if (genmode != 0) { | |
186 | (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ld", tmpdir, (long)dumpdate); | |
187 | if (command != 'r' && command != 'R') { | |
188 | (void) strncat(modefile, "-XXXXXX", | |
189 | sizeof(modefile) - strlen(modefile)); | |
190 | fd = mkstemp(modefile); | |
191 | } else | |
192 | fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666); | |
193 | if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) { | |
194 | if (fd != -1) | |
195 | close(fd); | |
196 | err(1, "cannot create modefile %s", modefile); | |
197 | } | |
198 | } | |
199 | nulldir.d_ino = 0; | |
200 | nulldir.d_type = DT_DIR; | |
201 | nulldir.d_namlen = 1; | |
202 | nulldir.d_name[0] = '/'; | |
203 | nulldir.d_name[1] = '\0'; | |
204 | nulldir.d_reclen = DIRSIZ(0, &nulldir); | |
205 | for (;;) { | |
206 | curfile.name = "<directory file - name unknown>"; | |
207 | curfile.action = USING; | |
208 | ip = curfile.dip; | |
209 | if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) { | |
210 | (void) fclose(df); | |
211 | dirp = opendirfile(dirfile); | |
212 | if (dirp == NULL) | |
213 | warn("opendirfile"); | |
214 | if (mf != NULL) | |
215 | (void) fclose(mf); | |
216 | i = dirlookup(dot); | |
217 | if (i == 0) | |
218 | panic("Root directory is not on tape\n"); | |
219 | return; | |
220 | } | |
221 | itp = allocinotab(curfile.ino, ip, seekpt); | |
222 | getfile(putdir, xtrnull); | |
223 | putent(&nulldir); | |
224 | flushent(); | |
225 | itp->t_size = seekpt - itp->t_seekpt; | |
226 | } | |
227 | } | |
228 | ||
229 | /* | |
230 | * skip over all the directories on the tape | |
231 | */ | |
232 | void | |
233 | skipdirs(void) | |
234 | { | |
235 | ||
236 | while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) { | |
237 | skipfile(); | |
238 | } | |
239 | } | |
240 | ||
241 | /* | |
242 | * Recursively find names and inumbers of all files in subtree | |
243 | * pname and pass them off to be processed. | |
244 | */ | |
245 | void | |
246 | treescan(char *pname, ino_t ino, long (*todo) __P((char *, ino_t, int))) | |
247 | { | |
248 | register struct inotab *itp; | |
249 | register struct direct *dp; | |
250 | int namelen; | |
251 | long bpt; | |
252 | char locname[MAXPATHLEN + 1]; | |
253 | ||
254 | itp = inotablookup(ino); | |
255 | if (itp == NULL) { | |
256 | /* | |
257 | * Pname is name of a simple file or an unchanged directory. | |
258 | */ | |
259 | (void) (*todo)(pname, ino, LEAF); | |
260 | return; | |
261 | } | |
262 | /* | |
263 | * Pname is a dumped directory name. | |
264 | */ | |
265 | if ((*todo)(pname, ino, NODE) == FAIL) | |
266 | return; | |
267 | /* | |
268 | * begin search through the directory | |
269 | * skipping over "." and ".." | |
270 | */ | |
271 | namelen = snprintf(locname, sizeof(locname), "%s/", pname); | |
272 | if (namelen >= sizeof(locname)) | |
273 | namelen = sizeof(locname) - 1; | |
274 | rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); | |
275 | dp = rst_readdir(dirp); /* "." */ | |
276 | if (dp != NULL && strcmp(dp->d_name, ".") == 0) | |
277 | dp = rst_readdir(dirp); /* ".." */ | |
278 | else | |
279 | fprintf(stderr, "Warning: `.' missing from directory %s\n", | |
280 | pname); | |
281 | if (dp != NULL && strcmp(dp->d_name, "..") == 0) | |
282 | dp = rst_readdir(dirp); /* first real entry */ | |
283 | else | |
284 | fprintf(stderr, "Warning: `..' missing from directory %s\n", | |
285 | pname); | |
286 | bpt = rst_telldir(dirp); | |
287 | /* | |
288 | * a zero inode signals end of directory | |
289 | */ | |
290 | while (dp != NULL) { | |
291 | locname[namelen] = '\0'; | |
292 | if (namelen + dp->d_namlen >= sizeof(locname)) { | |
293 | fprintf(stderr, "%s%s: name exceeds %ld char\n", | |
294 | locname, dp->d_name, (long)sizeof(locname) - 1); | |
295 | } else { | |
296 | (void) strncat(locname, dp->d_name, (int)dp->d_namlen); | |
297 | treescan(locname, dp->d_ino, todo); | |
298 | rst_seekdir(dirp, bpt, itp->t_seekpt); | |
299 | } | |
300 | dp = rst_readdir(dirp); | |
301 | bpt = rst_telldir(dirp); | |
302 | } | |
303 | } | |
304 | ||
305 | /* | |
306 | * Lookup a pathname which is always assumed to start from the ROOTINO. | |
307 | */ | |
308 | struct direct * | |
309 | pathsearch(const char *pathname) | |
310 | { | |
311 | ino_t ino; | |
312 | struct direct *dp; | |
313 | char *path, *name, buffer[MAXPATHLEN]; | |
314 | ||
315 | strcpy(buffer, pathname); | |
316 | path = buffer; | |
317 | ino = ROOTINO; | |
318 | while (*path == '/') | |
319 | path++; | |
320 | dp = NULL; | |
321 | while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) { | |
322 | if ((dp = searchdir(ino, name)) == NULL) | |
323 | return (NULL); | |
324 | ino = dp->d_ino; | |
325 | } | |
326 | return (dp); | |
327 | } | |
328 | ||
329 | /* | |
330 | * Lookup the requested name in directory inum. | |
331 | * Return its inode number if found, zero if it does not exist. | |
332 | */ | |
333 | static struct direct * | |
334 | searchdir(ino_t inum, char *name) | |
335 | { | |
336 | register struct direct *dp; | |
337 | register struct inotab *itp; | |
338 | int len; | |
339 | ||
340 | itp = inotablookup(inum); | |
341 | if (itp == NULL) | |
342 | return (NULL); | |
343 | rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); | |
344 | len = strlen(name); | |
345 | do { | |
346 | dp = rst_readdir(dirp); | |
347 | if (dp == NULL) | |
348 | return (NULL); | |
349 | } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0); | |
350 | return (dp); | |
351 | } | |
352 | ||
353 | /* | |
354 | * Put the directory entries in the directory file | |
355 | */ | |
356 | static void | |
357 | putdir(char *buf, size_t size) | |
358 | { | |
359 | struct direct cvtbuf; | |
360 | register struct odirect *odp; | |
361 | struct odirect *eodp; | |
362 | register struct direct *dp; | |
363 | long loc, i; | |
364 | ||
365 | if (cvtflag) { | |
366 | eodp = (struct odirect *)&buf[size]; | |
367 | for (odp = (struct odirect *)buf; odp < eodp; odp++) | |
368 | if (odp->d_ino != 0) { | |
369 | dcvt(odp, &cvtbuf); | |
370 | putent(&cvtbuf); | |
371 | } | |
372 | } else { | |
373 | for (loc = 0; loc < size; ) { | |
374 | dp = (struct direct *)(buf + loc); | |
375 | #ifdef DIRDEBUG | |
376 | printf ("reclen = %d, namlen = %d, type = %d\n", | |
377 | dp->d_reclen, dp->d_namlen, dp->d_type); | |
378 | #endif | |
379 | if (Bcvt) | |
380 | swabst((u_char *)"is", (u_char *) dp); | |
381 | if (oldinofmt && dp->d_ino != 0) { | |
382 | #ifdef __linux__ | |
383 | if (Bcvt) | |
384 | swabst((u_char *)"s", (u_char *)&dp->d_namlen); | |
385 | #else | |
386 | # if BYTE_ORDER == BIG_ENDIAN | |
387 | if (Bcvt) | |
388 | dp->d_namlen = dp->d_type; | |
389 | # else | |
390 | if (!Bcvt) | |
391 | dp->d_namlen = dp->d_type; | |
392 | # endif | |
393 | #endif /* __linux__ */ | |
394 | dp->d_type = DT_UNKNOWN; | |
395 | } | |
396 | #ifdef __linux__ | |
397 | /* | |
398 | * Horrible hack to read FreeBSD 2.0 dumps | |
399 | */ | |
400 | if (!oldinofmt) | |
401 | swabst((u_char *)"6bs", (u_char *) dp); | |
402 | #endif /* __linux__ */ | |
403 | #ifdef DIRDEBUG | |
404 | printf ("reclen = %d, namlen = %d, type = %d\n", | |
405 | dp->d_reclen, dp->d_namlen, dp->d_type); | |
406 | #endif | |
407 | i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1)); | |
408 | if ((dp->d_reclen & 0x3) != 0 || | |
409 | dp->d_reclen > i || | |
410 | dp->d_reclen < DIRSIZ(0, dp) || | |
411 | dp->d_namlen > NAME_MAX) { | |
412 | Vprintf(stdout, "Mangled directory: "); | |
413 | if ((dp->d_reclen & 0x3) != 0) | |
414 | Vprintf(stdout, | |
415 | "reclen not multiple of 4 "); | |
416 | if (dp->d_reclen < DIRSIZ(0, dp)) | |
417 | Vprintf(stdout, | |
418 | "reclen less than DIRSIZ (%d < %d) ", | |
419 | dp->d_reclen, DIRSIZ(0, dp)); | |
420 | if (dp->d_namlen > NAME_MAX) | |
421 | Vprintf(stdout, | |
422 | "reclen name too big (%d > %d) ", | |
423 | dp->d_namlen, NAME_MAX); | |
424 | Vprintf(stdout, "\n"); | |
425 | loc += i; | |
426 | continue; | |
427 | } | |
428 | loc += dp->d_reclen; | |
429 | if (dp->d_ino != 0) { | |
430 | putent(dp); | |
431 | } | |
432 | } | |
433 | } | |
434 | } | |
435 | ||
436 | /* | |
437 | * These variables are "local" to the following two functions. | |
438 | */ | |
439 | char dirbuf[DIRBLKSIZ]; | |
440 | long dirloc = 0; | |
441 | long prev = 0; | |
442 | ||
443 | /* | |
444 | * add a new directory entry to a file. | |
445 | */ | |
446 | static void | |
447 | putent(struct direct *dp) | |
448 | { | |
449 | dp->d_reclen = DIRSIZ(0, dp); | |
450 | if (dirloc + dp->d_reclen > DIRBLKSIZ) { | |
451 | ((struct direct *)(dirbuf + prev))->d_reclen = | |
452 | DIRBLKSIZ - prev; | |
453 | (void) fwrite(dirbuf, 1, DIRBLKSIZ, df); | |
454 | dirloc = 0; | |
455 | } | |
456 | memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen); | |
457 | prev = dirloc; | |
458 | dirloc += dp->d_reclen; | |
459 | } | |
460 | ||
461 | /* | |
462 | * flush out a directory that is finished. | |
463 | */ | |
464 | static void | |
465 | flushent(void) | |
466 | { | |
467 | ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev; | |
468 | (void) fwrite(dirbuf, (int)dirloc, 1, df); | |
469 | seekpt = ftell(df); | |
470 | dirloc = 0; | |
471 | } | |
472 | ||
473 | static void | |
474 | dcvt(struct odirect *odp, struct direct *ndp) | |
475 | { | |
476 | ||
477 | memset(ndp, 0, (size_t)(sizeof *ndp)); | |
478 | ndp->d_ino = odp->d_ino; | |
479 | ndp->d_type = DT_UNKNOWN; | |
480 | (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ); | |
481 | ndp->d_namlen = strlen(ndp->d_name); | |
482 | ndp->d_reclen = DIRSIZ(0, ndp); | |
483 | } | |
484 | ||
485 | /* | |
486 | * Seek to an entry in a directory. | |
487 | * Only values returned by rst_telldir should be passed to rst_seekdir. | |
488 | * This routine handles many directories in a single file. | |
489 | * It takes the base of the directory in the file, plus | |
490 | * the desired seek offset into it. | |
491 | */ | |
492 | static void | |
493 | rst_seekdir(RST_DIR *dirp, long loc, long base) | |
494 | { | |
495 | ||
496 | if (loc == rst_telldir(dirp)) | |
497 | return; | |
498 | loc -= base; | |
499 | if (loc < 0) | |
500 | fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc); | |
501 | (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET); | |
502 | dirp->dd_loc = loc & (DIRBLKSIZ - 1); | |
503 | if (dirp->dd_loc != 0) | |
504 | dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ); | |
505 | } | |
506 | ||
507 | /* | |
508 | * get next entry in a directory. | |
509 | */ | |
510 | struct direct * | |
511 | rst_readdir(RST_DIR *dirp) | |
512 | { | |
513 | register struct direct *dp; | |
514 | ||
515 | for (;;) { | |
516 | if (dirp->dd_loc == 0) { | |
517 | dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, | |
518 | DIRBLKSIZ); | |
519 | if (dirp->dd_size <= 0) { | |
520 | Dprintf(stderr, "error reading directory\n"); | |
521 | return (NULL); | |
522 | } | |
523 | } | |
524 | if (dirp->dd_loc >= dirp->dd_size) { | |
525 | dirp->dd_loc = 0; | |
526 | continue; | |
527 | } | |
528 | dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc); | |
529 | if (dp->d_reclen == 0 || | |
530 | dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) { | |
531 | Dprintf(stderr, "corrupted directory: bad reclen %d\n", | |
532 | dp->d_reclen); | |
533 | return (NULL); | |
534 | } | |
535 | dirp->dd_loc += dp->d_reclen; | |
536 | if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0) | |
537 | return (NULL); | |
538 | if (dp->d_ino >= maxino) { | |
539 | Dprintf(stderr, "corrupted directory: bad inum %d\n", | |
540 | dp->d_ino); | |
541 | continue; | |
542 | } | |
543 | return (dp); | |
544 | } | |
545 | } | |
546 | ||
547 | /* | |
548 | * Simulate the opening of a directory | |
549 | */ | |
550 | RST_DIR * | |
551 | rst_opendir(const char *name) | |
552 | { | |
553 | struct inotab *itp; | |
554 | RST_DIR *dirp; | |
555 | ino_t ino; | |
556 | ||
557 | if ((ino = dirlookup(name)) > 0 && | |
558 | (itp = inotablookup(ino)) != NULL) { | |
559 | dirp = opendirfile(dirfile); | |
560 | rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); | |
561 | return (dirp); | |
562 | } | |
563 | return (NULL); | |
564 | } | |
565 | ||
566 | /* | |
567 | * In our case, there is nothing to do when closing a directory. | |
568 | */ | |
569 | void | |
570 | rst_closedir(RST_DIR *dirp) | |
571 | { | |
572 | ||
573 | (void)close(dirp->dd_fd); | |
574 | free(dirp); | |
575 | return; | |
576 | } | |
577 | ||
578 | /* | |
579 | * Simulate finding the current offset in the directory. | |
580 | */ | |
581 | static long | |
582 | rst_telldir(RST_DIR *dirp) | |
583 | { | |
584 | return ((long)lseek(dirp->dd_fd, | |
585 | (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc); | |
586 | } | |
587 | ||
588 | /* | |
589 | * Open a directory file. | |
590 | */ | |
591 | static RST_DIR * | |
592 | opendirfile(const char *name) | |
593 | { | |
594 | register RST_DIR *dirp; | |
595 | register int fd; | |
596 | ||
597 | if ((fd = open(name, O_RDONLY)) == -1) | |
598 | return (NULL); | |
599 | if ((dirp = malloc(sizeof(RST_DIR))) == NULL) { | |
600 | (void)close(fd); | |
601 | return (NULL); | |
602 | } | |
603 | dirp->dd_fd = fd; | |
604 | dirp->dd_loc = 0; | |
605 | return (dirp); | |
606 | } | |
607 | ||
608 | /* | |
609 | * Set the mode, owner, and times for all new or changed directories | |
610 | */ | |
611 | void | |
612 | setdirmodes(int flags) | |
613 | { | |
614 | FILE *mf; | |
615 | struct modeinfo node; | |
616 | struct entry *ep; | |
617 | char *cp; | |
618 | ||
619 | Vprintf(stdout, "Set directory mode, owner, and times.\n"); | |
620 | if (command == 'r' || command == 'R') | |
621 | (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%lu", tmpdir, (long)dumpdate); | |
622 | if (modefile[0] == '#') { | |
623 | panic("modefile not defined\n"); | |
624 | fprintf(stderr, "directory mode, owner, and times not set\n"); | |
625 | return; | |
626 | } | |
627 | mf = fopen(modefile, "r"); | |
628 | if (mf == NULL) { | |
629 | warn("fopen"); | |
630 | fprintf(stderr, "cannot open mode file %s\n", modefile); | |
631 | fprintf(stderr, "directory mode, owner, and times not set\n"); | |
632 | return; | |
633 | } | |
634 | clearerr(mf); | |
635 | for (;;) { | |
636 | (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf); | |
637 | if (feof(mf)) | |
638 | break; | |
639 | ep = lookupino(node.ino); | |
640 | if (command == 'i' || command == 'x') { | |
641 | if (ep == NULL) | |
642 | continue; | |
643 | if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) { | |
644 | ep->e_flags &= ~NEW; | |
645 | continue; | |
646 | } | |
647 | if (node.ino == ROOTINO && | |
648 | reply("set owner/mode for '.'") == FAIL) | |
649 | continue; | |
650 | } | |
651 | if (ep == NULL) { | |
652 | panic("cannot find directory inode %d\n", node.ino); | |
653 | } else { | |
654 | cp = myname(ep); | |
655 | (void) chown(cp, node.uid, node.gid); | |
656 | (void) chmod(cp, node.mode); | |
657 | if (node.flags) | |
658 | #ifdef __linux__ | |
659 | (void) fsetflags(cp, node.flags); | |
660 | #else | |
661 | (void) chflags(cp, node.flags); | |
662 | #endif | |
663 | utimes(cp, node.timep); | |
664 | ep->e_flags &= ~NEW; | |
665 | } | |
666 | } | |
667 | if (ferror(mf)) | |
668 | panic("error setting directory modes\n"); | |
669 | (void) fclose(mf); | |
670 | } | |
671 | ||
672 | /* | |
673 | * Generate a literal copy of a directory. | |
674 | */ | |
675 | int | |
676 | genliteraldir(char *name, ino_t ino) | |
677 | { | |
678 | register struct inotab *itp; | |
679 | int ofile, dp, i, size; | |
680 | char buf[BUFSIZ]; | |
681 | ||
682 | itp = inotablookup(ino); | |
683 | if (itp == NULL) | |
684 | panic("Cannot find directory inode %d named %s\n", ino, name); | |
685 | if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { | |
686 | warn("%s: cannot create file\n", name); | |
687 | return (FAIL); | |
688 | } | |
689 | rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); | |
690 | dp = dup(dirp->dd_fd); | |
691 | for (i = itp->t_size; i > 0; i -= BUFSIZ) { | |
692 | size = i < BUFSIZ ? i : BUFSIZ; | |
693 | if (read(dp, buf, (int) size) == -1) { | |
694 | warnx("write error extracting inode %lu, name %s\n", | |
695 | (unsigned long)curfile.ino, curfile.name); | |
696 | err(1, "read"); | |
697 | } | |
698 | if (!Nflag && write(ofile, buf, (int) size) == -1) { | |
699 | warnx("write error extracting inode %lu, name %s\n", | |
700 | (unsigned long)curfile.ino, curfile.name); | |
701 | err(1, "write"); | |
702 | } | |
703 | } | |
704 | (void) close(dp); | |
705 | (void) close(ofile); | |
706 | return (GOOD); | |
707 | } | |
708 | ||
709 | /* | |
710 | * Determine the type of an inode | |
711 | */ | |
712 | int | |
713 | inodetype(ino_t ino) | |
714 | { | |
715 | struct inotab *itp; | |
716 | ||
717 | itp = inotablookup(ino); | |
718 | if (itp == NULL) | |
719 | return (LEAF); | |
720 | return (NODE); | |
721 | } | |
722 | ||
723 | /* | |
724 | * Allocate and initialize a directory inode entry. | |
725 | * If requested, save its pertinent mode, owner, and time info. | |
726 | */ | |
727 | static struct inotab * | |
728 | #ifdef __linux__ | |
729 | allocinotab(ino_t ino, struct new_bsd_inode *dip, long seekpt) | |
730 | #else | |
731 | allocinotab(ino_t ino, struct dinode *dip, long seekpt) | |
732 | #endif | |
733 | { | |
734 | register struct inotab *itp; | |
735 | struct modeinfo node; | |
736 | ||
737 | itp = calloc(1, sizeof(struct inotab)); | |
738 | if (itp == NULL) | |
739 | panic("no memory directory table\n"); | |
740 | itp->t_next = inotab[INOHASH(ino)]; | |
741 | inotab[INOHASH(ino)] = itp; | |
742 | itp->t_ino = ino; | |
743 | itp->t_seekpt = seekpt; | |
744 | if (mf == NULL) | |
745 | return (itp); | |
746 | node.ino = ino; | |
747 | #ifdef __linux__ | |
748 | node.timep[0].tv_sec = dip->di_atime.tv_sec; | |
749 | node.timep[0].tv_usec = dip->di_atime.tv_usec; | |
750 | node.timep[1].tv_sec = dip->di_mtime.tv_sec; | |
751 | node.timep[1].tv_usec = dip->di_mtime.tv_usec; | |
752 | #else /* __linux__ */ | |
753 | node.timep[0].tv_sec = dip->di_atime; | |
754 | node.timep[0].tv_usec = dip->di_atimensec / 1000; | |
755 | node.timep[1].tv_sec = dip->di_mtime; | |
756 | node.timep[1].tv_usec = dip->di_mtimensec / 1000; | |
757 | #endif /* __linux__ */ | |
758 | node.mode = dip->di_mode; | |
759 | node.flags = dip->di_flags; | |
760 | node.uid = dip->di_uid; | |
761 | node.gid = dip->di_gid; | |
762 | (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf); | |
763 | return (itp); | |
764 | } | |
765 | ||
766 | /* | |
767 | * Look up an inode in the table of directories | |
768 | */ | |
769 | static struct inotab * | |
770 | inotablookup(ino_t ino) | |
771 | { | |
772 | register struct inotab *itp; | |
773 | ||
774 | for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next) | |
775 | if (itp->t_ino == ino) | |
776 | return (itp); | |
777 | return (NULL); | |
778 | } | |
779 | ||
780 | /* | |
781 | * Clean up and exit | |
782 | */ | |
783 | void | |
784 | cleanup(void) | |
785 | { | |
786 | closemt(); | |
787 | if (modefile[0] != '#') | |
788 | (void) unlink(modefile); | |
789 | if (dirfile[0] != '#') | |
790 | (void) unlink(dirfile); | |
791 | } |