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