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