17c478bd9Sstevel@tonic-gate /*
2fe0e7ec4Smaheshvs * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include "restore.h"
167c478bd9Sstevel@tonic-gate #include <ctype.h>
177c478bd9Sstevel@tonic-gate #include <errno.h>
187c478bd9Sstevel@tonic-gate #include <syslog.h>
197c478bd9Sstevel@tonic-gate #include <limits.h>
207c478bd9Sstevel@tonic-gate /* LINTED: this file really is necessary */
217c478bd9Sstevel@tonic-gate #include <euc.h>
227c478bd9Sstevel@tonic-gate #include <widec.h>
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate * Insure that all the components of a pathname exist. Note that
267c478bd9Sstevel@tonic-gate * lookupname() and addentry() both expect complex names as
277c478bd9Sstevel@tonic-gate * input arguments, so a double NULL needs to be added to each name.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate void
pathcheck(char * name)30fe0e7ec4Smaheshvs pathcheck(char *name)
317c478bd9Sstevel@tonic-gate {
327c478bd9Sstevel@tonic-gate char *cp, save;
337c478bd9Sstevel@tonic-gate struct entry *ep;
347c478bd9Sstevel@tonic-gate char *start;
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate start = strchr(name, '/');
377c478bd9Sstevel@tonic-gate if (start == 0)
387c478bd9Sstevel@tonic-gate return;
397c478bd9Sstevel@tonic-gate for (cp = start; *cp != '\0'; cp++) {
407c478bd9Sstevel@tonic-gate if (*cp != '/')
417c478bd9Sstevel@tonic-gate continue;
427c478bd9Sstevel@tonic-gate *cp = '\0';
437c478bd9Sstevel@tonic-gate save = *(cp+1);
447c478bd9Sstevel@tonic-gate *(cp+1) = '\0';
457c478bd9Sstevel@tonic-gate ep = lookupname(name);
467c478bd9Sstevel@tonic-gate if (ep == NIL) {
477c478bd9Sstevel@tonic-gate ep = addentry(name, psearch(name), NODE);
487c478bd9Sstevel@tonic-gate newnode(ep);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
517c478bd9Sstevel@tonic-gate ep->e_flags |= NEW|KEEP;
527c478bd9Sstevel@tonic-gate *cp = '/';
537c478bd9Sstevel@tonic-gate *(cp+1) = save;
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate * Change a name to a unique temporary name.
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate void
mktempname(struct entry * ep)61fe0e7ec4Smaheshvs mktempname(struct entry *ep)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate char *newname;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate if (ep->e_flags & TMPNAME)
667c478bd9Sstevel@tonic-gate badentry(ep, gettext("mktempname: called with TMPNAME"));
677c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
687c478bd9Sstevel@tonic-gate ep->e_flags |= TMPNAME;
697c478bd9Sstevel@tonic-gate newname = savename(gentempname(ep));
707c478bd9Sstevel@tonic-gate renameit(myname(ep), newname);
717c478bd9Sstevel@tonic-gate freename(ep->e_name);
727c478bd9Sstevel@tonic-gate ep->e_name = newname;
737c478bd9Sstevel@tonic-gate /* LINTED: savename guarantees strlen will fit */
747c478bd9Sstevel@tonic-gate ep->e_namlen = strlen(ep->e_name);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * Generate a temporary name for an entry.
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate char *
gentempname(struct entry * ep)81fe0e7ec4Smaheshvs gentempname(struct entry *ep)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate static char name[MAXPATHLEN];
847c478bd9Sstevel@tonic-gate struct entry *np;
857c478bd9Sstevel@tonic-gate long i = 0;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links)
887c478bd9Sstevel@tonic-gate i++;
897c478bd9Sstevel@tonic-gate if (np == NIL)
907c478bd9Sstevel@tonic-gate badentry(ep, gettext("not on ino list"));
917c478bd9Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "%s%ld%lu", TMPHDR, i, ep->e_ino);
927c478bd9Sstevel@tonic-gate return (name);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * Rename a file or directory.
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate void
renameit(char * fp,char * tp)99fe0e7ec4Smaheshvs renameit(char *fp, char *tp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate int fromfd, tofd;
1027c478bd9Sstevel@tonic-gate char *from, *to;
1037c478bd9Sstevel@tonic-gate char tobuf[MAXPATHLEN];
1047c478bd9Sstevel@tonic-gate char *pathend;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate resolve(fp, &fromfd, &from);
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * The to pointer argument is assumed to be either a fully
1097c478bd9Sstevel@tonic-gate * specified path (starting with "./") or a simple temporary
1107c478bd9Sstevel@tonic-gate * file name (starting with TMPHDR). If passed a simple temp
1117c478bd9Sstevel@tonic-gate * file name, we need to set up the descriptors explicitly.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate if (strncmp(tp, TMPHDR, sizeof (TMPHDR) - 1) == 0) {
1147c478bd9Sstevel@tonic-gate tofd = fromfd;
1157c478bd9Sstevel@tonic-gate if ((pathend = strrchr(from, '/')) != NULL) {
1167c478bd9Sstevel@tonic-gate strncpy(tobuf, from, pathend - from + 1);
117*82be58ffSToomas Soome tobuf[pathend - from + 1] = '\0';
1187c478bd9Sstevel@tonic-gate strlcat(tobuf, tp, sizeof (tobuf));
1197c478bd9Sstevel@tonic-gate to = tobuf;
1207c478bd9Sstevel@tonic-gate } else {
1217c478bd9Sstevel@tonic-gate to = tp;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate } else
1247c478bd9Sstevel@tonic-gate resolve(tp, &tofd, &to);
1257c478bd9Sstevel@tonic-gate if (renameat(fromfd, from, tofd, to) < 0) {
1267c478bd9Sstevel@tonic-gate int saverr = errno;
1277c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1287c478bd9Sstevel@tonic-gate gettext("Warning: cannot rename %s to %s: %s\n"),
1297c478bd9Sstevel@tonic-gate from, to, strerror(saverr));
1307c478bd9Sstevel@tonic-gate (void) fflush(stderr);
1317c478bd9Sstevel@tonic-gate } else {
1327c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("rename %s to %s\n"), from, to);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate if (fromfd != AT_FDCWD) (void) close(fromfd);
1357c478bd9Sstevel@tonic-gate if (tofd != AT_FDCWD) (void) close(tofd);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Create a new node (directory). Note that, because we have no
1407c478bd9Sstevel@tonic-gate * mkdirat() function, fchdir() must be used set up the appropriate
1417c478bd9Sstevel@tonic-gate * name space context prior to the call to mkdir() if we are
1427c478bd9Sstevel@tonic-gate * operating in attribute space.
1437c478bd9Sstevel@tonic-gate */
1447c478bd9Sstevel@tonic-gate void
newnode(struct entry * np)145fe0e7ec4Smaheshvs newnode(struct entry *np)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate char *cp;
1487c478bd9Sstevel@tonic-gate int dfd;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if (np->e_type != NODE)
1517c478bd9Sstevel@tonic-gate badentry(np, gettext("newnode: not a node"));
1527c478bd9Sstevel@tonic-gate resolve(myname(np), &dfd, &cp);
1537c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
1547c478bd9Sstevel@tonic-gate if (fchdir(dfd) < 0) {
1557c478bd9Sstevel@tonic-gate int saverr = errno;
1567c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1577c478bd9Sstevel@tonic-gate gettext("Warning: cannot create %s: %s"),
1587c478bd9Sstevel@tonic-gate cp, strerror(saverr));
1597c478bd9Sstevel@tonic-gate (void) fflush(stderr);
1607c478bd9Sstevel@tonic-gate (void) close(dfd);
1617c478bd9Sstevel@tonic-gate return;
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate if (mkdir(cp, 0777) < 0) {
1657c478bd9Sstevel@tonic-gate int saverr = errno;
1667c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
1677c478bd9Sstevel@tonic-gate np->e_flags |= EXISTED;
1687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning: "));
1697c478bd9Sstevel@tonic-gate (void) fflush(stderr);
1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", cp, strerror(saverr));
1717c478bd9Sstevel@tonic-gate } else {
1727c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Make node %s\n"), cp);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
1757c478bd9Sstevel@tonic-gate fchdir(savepwd);
1767c478bd9Sstevel@tonic-gate (void) close(dfd);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate * Remove an old node (directory). See comment above on newnode()
1827c478bd9Sstevel@tonic-gate * for explanation of fchdir() use below.
1837c478bd9Sstevel@tonic-gate */
1847c478bd9Sstevel@tonic-gate void
removenode(struct entry * ep)185fe0e7ec4Smaheshvs removenode(struct entry *ep)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate char *cp;
1887c478bd9Sstevel@tonic-gate int dfd;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate if (ep->e_type != NODE)
1917c478bd9Sstevel@tonic-gate badentry(ep, gettext("removenode: not a node"));
1927c478bd9Sstevel@tonic-gate if (ep->e_entries != NIL)
1937c478bd9Sstevel@tonic-gate badentry(ep, gettext("removenode: non-empty directory"));
1947c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
1957c478bd9Sstevel@tonic-gate ep->e_flags |= REMOVED;
1967c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
1977c478bd9Sstevel@tonic-gate ep->e_flags &= ~TMPNAME;
1987c478bd9Sstevel@tonic-gate resolve(myname(ep), &dfd, &cp);
1997c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
2007c478bd9Sstevel@tonic-gate if (fchdir(dfd) < 0) {
2017c478bd9Sstevel@tonic-gate int saverr = errno;
2027c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2037c478bd9Sstevel@tonic-gate gettext("Warning: cannot remove %s: %s"),
2047c478bd9Sstevel@tonic-gate cp, strerror(saverr));
2057c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2067c478bd9Sstevel@tonic-gate (void) close(dfd);
2077c478bd9Sstevel@tonic-gate return;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate if (rmdir(cp) < 0) { /* NOTE: could use unlinkat (..,REMOVEDIR) */
2117c478bd9Sstevel@tonic-gate int saverr = errno;
2127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning: %s: %s\n"),
2137c478bd9Sstevel@tonic-gate cp, strerror(saverr));
2147c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2157c478bd9Sstevel@tonic-gate } else {
2167c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Remove node %s\n"), cp);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
2197c478bd9Sstevel@tonic-gate (void) fchdir(savepwd);
2207c478bd9Sstevel@tonic-gate (void) close(dfd);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * Remove a leaf.
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate void
removeleaf(struct entry * ep)228fe0e7ec4Smaheshvs removeleaf(struct entry *ep)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate char *cp;
2317c478bd9Sstevel@tonic-gate int dfd;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate if (ep->e_type != LEAF)
2347c478bd9Sstevel@tonic-gate badentry(ep, gettext("removeleaf: not a leaf"));
2357c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
2367c478bd9Sstevel@tonic-gate ep->e_flags |= REMOVED;
2377c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
2387c478bd9Sstevel@tonic-gate ep->e_flags &= ~TMPNAME;
2397c478bd9Sstevel@tonic-gate resolve(myname(ep), &dfd, &cp);
2407c478bd9Sstevel@tonic-gate if (unlinkat(dfd, cp, 0) < 0) {
2417c478bd9Sstevel@tonic-gate int saverr = errno;
2427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning: %s: %s\n"),
2437c478bd9Sstevel@tonic-gate cp, strerror(saverr));
2447c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2457c478bd9Sstevel@tonic-gate } else {
2467c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Remove leaf %s\n"), cp);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD)
2497c478bd9Sstevel@tonic-gate (void) close(dfd);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * Create a link.
2547c478bd9Sstevel@tonic-gate * This function assumes that the context has already been set
2557c478bd9Sstevel@tonic-gate * for the link file to be created (i.e., we have "fchdir-ed"
2567c478bd9Sstevel@tonic-gate * into attribute space already if this is an attribute link).
2577c478bd9Sstevel@tonic-gate */
258fe0e7ec4Smaheshvs int
lf_linkit(char * existing,char * new,int type)259fe0e7ec4Smaheshvs lf_linkit(char *existing, char *new, int type)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate char linkbuf[MAXPATHLEN];
2627c478bd9Sstevel@tonic-gate struct stat64 s1[1], s2[1];
2637c478bd9Sstevel@tonic-gate char *name;
2647c478bd9Sstevel@tonic-gate int dfd, l, result;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate resolve(existing, &dfd, &name);
2677c478bd9Sstevel@tonic-gate if (dfd == -1) {
2687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2697c478bd9Sstevel@tonic-gate "Warning: cannot restore %s link %s->%s\n"),
2707c478bd9Sstevel@tonic-gate (type == SYMLINK ? "symbolic" : "hard"), new, existing);
2717c478bd9Sstevel@tonic-gate result = FAIL;
2727c478bd9Sstevel@tonic-gate goto out;
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate if (type == SYMLINK) {
2757c478bd9Sstevel@tonic-gate if (symlink(name, new) < 0) {
2767c478bd9Sstevel@tonic-gate /* No trailing \0 from readlink(2) */
2777c478bd9Sstevel@tonic-gate if (((l = readlink(new, linkbuf, sizeof (linkbuf)))
2787c478bd9Sstevel@tonic-gate > 0) &&
2797c478bd9Sstevel@tonic-gate (l == strlen(name)) &&
2807c478bd9Sstevel@tonic-gate (strncmp(linkbuf, name, l) == 0)) {
2817c478bd9Sstevel@tonic-gate vprintf(stdout,
2827c478bd9Sstevel@tonic-gate gettext("Symbolic link %s->%s ok\n"),
2837c478bd9Sstevel@tonic-gate new, name);
2847c478bd9Sstevel@tonic-gate result = GOOD;
2857c478bd9Sstevel@tonic-gate goto out;
2867c478bd9Sstevel@tonic-gate } else {
2877c478bd9Sstevel@tonic-gate int saverr = errno;
2887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2897c478bd9Sstevel@tonic-gate "Warning: cannot create symbolic link %s->%s: %s"),
2907c478bd9Sstevel@tonic-gate new, name, strerror(saverr));
2917c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2927c478bd9Sstevel@tonic-gate result = FAIL;
2937c478bd9Sstevel@tonic-gate goto out;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate } else if (type == HARDLINK) {
2977c478bd9Sstevel@tonic-gate if (link(name, new) < 0) {
2987c478bd9Sstevel@tonic-gate int saverr = errno;
2997c478bd9Sstevel@tonic-gate if ((stat64(name, s1) == 0) &&
3007c478bd9Sstevel@tonic-gate (stat64(new, s2) == 0) &&
3017c478bd9Sstevel@tonic-gate (s1->st_dev == s2->st_dev) &&
3027c478bd9Sstevel@tonic-gate (s1->st_ino == s2->st_ino)) {
3037c478bd9Sstevel@tonic-gate vprintf(stdout,
3047c478bd9Sstevel@tonic-gate gettext("Hard link %s->%s ok\n"),
3057c478bd9Sstevel@tonic-gate new, name);
3067c478bd9Sstevel@tonic-gate result = GOOD;
3077c478bd9Sstevel@tonic-gate goto out;
3087c478bd9Sstevel@tonic-gate } else {
3097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3107c478bd9Sstevel@tonic-gate "Warning: cannot create hard link %s->%s: %s\n"),
3117c478bd9Sstevel@tonic-gate new, name, strerror(saverr));
3127c478bd9Sstevel@tonic-gate (void) fflush(stderr);
3137c478bd9Sstevel@tonic-gate result = FAIL;
3147c478bd9Sstevel@tonic-gate goto out;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate } else {
3187c478bd9Sstevel@tonic-gate panic(gettext("%s: unknown type %d\n"), "linkit", type);
3197c478bd9Sstevel@tonic-gate result = FAIL;
3207c478bd9Sstevel@tonic-gate goto out;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate result = GOOD;
3237c478bd9Sstevel@tonic-gate if (type == SYMLINK)
3247c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Create symbolic link %s->%s\n"),
3257c478bd9Sstevel@tonic-gate new, name);
3267c478bd9Sstevel@tonic-gate else
3277c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Create hard link %s->%s\n"),
3287c478bd9Sstevel@tonic-gate new, name);
3297c478bd9Sstevel@tonic-gate out:
3307c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
3317c478bd9Sstevel@tonic-gate (void) close(dfd);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate return (result);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate * Find lowest-numbered inode (above "start") that needs to be extracted.
3387c478bd9Sstevel@tonic-gate * Caller knows that a return value of maxino means there's nothing left.
3397c478bd9Sstevel@tonic-gate */
3407c478bd9Sstevel@tonic-gate ino_t
lowerbnd(ino_t start)341fe0e7ec4Smaheshvs lowerbnd(ino_t start)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate struct entry *ep;
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate for (; start < maxino; start++) {
3467c478bd9Sstevel@tonic-gate ep = lookupino(start);
3477c478bd9Sstevel@tonic-gate if (ep == NIL || ep->e_type == NODE)
3487c478bd9Sstevel@tonic-gate continue;
3497c478bd9Sstevel@tonic-gate if (ep->e_flags & (NEW|EXTRACT))
3507c478bd9Sstevel@tonic-gate return (start);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate return (start);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate * Find highest-numbered inode (below "start") that needs to be extracted.
3577c478bd9Sstevel@tonic-gate */
3587c478bd9Sstevel@tonic-gate ino_t
upperbnd(ino_t start)359fe0e7ec4Smaheshvs upperbnd(ino_t start)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate struct entry *ep;
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate for (; start > ROOTINO; start--) {
3647c478bd9Sstevel@tonic-gate ep = lookupino(start);
3657c478bd9Sstevel@tonic-gate if (ep == NIL || ep->e_type == NODE)
3667c478bd9Sstevel@tonic-gate continue;
3677c478bd9Sstevel@tonic-gate if (ep->e_flags & (NEW|EXTRACT))
3687c478bd9Sstevel@tonic-gate return (start);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate return (start);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate * report on a badly formed entry
3757c478bd9Sstevel@tonic-gate */
3767c478bd9Sstevel@tonic-gate void
badentry(struct entry * ep,char * msg)377fe0e7ec4Smaheshvs badentry(struct entry *ep, char *msg)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("bad entry: %s\n"), msg);
3817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("name: %s\n"), myname(ep));
3827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("parent name %s\n"),
3837c478bd9Sstevel@tonic-gate myname(ep->e_parent));
3847c478bd9Sstevel@tonic-gate if (ep->e_sibling != NIL)
3857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("sibling name: %s\n"),
3867c478bd9Sstevel@tonic-gate myname(ep->e_sibling));
3877c478bd9Sstevel@tonic-gate if (ep->e_entries != NIL)
3887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("next entry name: %s\n"),
3897c478bd9Sstevel@tonic-gate myname(ep->e_entries));
3907c478bd9Sstevel@tonic-gate if (ep->e_links != NIL)
3917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("next link name: %s\n"),
3927c478bd9Sstevel@tonic-gate myname(ep->e_links));
3937c478bd9Sstevel@tonic-gate if (ep->e_xattrs != NIL)
3947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("attribute root name: %s\n"),
3957c478bd9Sstevel@tonic-gate myname(ep->e_xattrs));
3967c478bd9Sstevel@tonic-gate if (ep->e_next != NIL)
3977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("next hashchain name: %s\n"),
3987c478bd9Sstevel@tonic-gate myname(ep->e_next));
3997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("entry type: %s\n"),
4007c478bd9Sstevel@tonic-gate ep->e_type == NODE ? gettext("NODE") : gettext("LEAF"));
4017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("inode number: %lu\n"), ep->e_ino);
4027c478bd9Sstevel@tonic-gate panic(gettext("flags: %s\n"), flagvalues(ep));
4037c478bd9Sstevel@tonic-gate /* Our callers are expected to handle our returning. */
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate * Construct a string indicating the active flag bits of an entry.
4087c478bd9Sstevel@tonic-gate */
4097c478bd9Sstevel@tonic-gate char *
flagvalues(struct entry * ep)410fe0e7ec4Smaheshvs flagvalues(struct entry *ep)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate static char flagbuf[BUFSIZ];
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate (void) strlcpy(flagbuf, gettext("|NIL"), sizeof (flagbuf));
4157c478bd9Sstevel@tonic-gate flagbuf[0] = '\0';
4167c478bd9Sstevel@tonic-gate if (ep->e_flags & REMOVED)
4177c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|REMOVED"), sizeof (flagbuf));
4187c478bd9Sstevel@tonic-gate if (ep->e_flags & TMPNAME)
4197c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|TMPNAME"), sizeof (flagbuf));
4207c478bd9Sstevel@tonic-gate if (ep->e_flags & EXTRACT)
4217c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|EXTRACT"), sizeof (flagbuf));
4227c478bd9Sstevel@tonic-gate if (ep->e_flags & NEW)
4237c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|NEW"), sizeof (flagbuf));
4247c478bd9Sstevel@tonic-gate if (ep->e_flags & KEEP)
4257c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|KEEP"), sizeof (flagbuf));
4267c478bd9Sstevel@tonic-gate if (ep->e_flags & EXISTED)
4277c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|EXISTED"), sizeof (flagbuf));
4287c478bd9Sstevel@tonic-gate if (ep->e_flags & XATTR)
4297c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|XATTR"), sizeof (flagbuf));
4307c478bd9Sstevel@tonic-gate if (ep->e_flags & XATTRROOT)
4317c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|XATTRROOT"),
4327c478bd9Sstevel@tonic-gate sizeof (flagbuf));
4337c478bd9Sstevel@tonic-gate return (&flagbuf[1]);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * Check to see if a name is on a dump tape.
4387c478bd9Sstevel@tonic-gate */
4397c478bd9Sstevel@tonic-gate ino_t
dirlookup(char * name)440fe0e7ec4Smaheshvs dirlookup(char *name)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate ino_t ino;
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate ino = psearch(name);
4457c478bd9Sstevel@tonic-gate if (ino == 0 || BIT(ino, dumpmap) == 0)
4467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not on volume\n"), name);
4477c478bd9Sstevel@tonic-gate return (ino);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate * Elicit a reply.
4527c478bd9Sstevel@tonic-gate */
453fe0e7ec4Smaheshvs int
reply(char * question)454fe0e7ec4Smaheshvs reply(char *question)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate char *yesorno = gettext("yn"); /* must be two characters, "yes" first */
4577c478bd9Sstevel@tonic-gate int c;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate do {
4607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s? [%s] ", question, yesorno);
4617c478bd9Sstevel@tonic-gate (void) fflush(stderr);
4627c478bd9Sstevel@tonic-gate c = getc(terminal);
4637c478bd9Sstevel@tonic-gate while (c != '\n' && getc(terminal) != '\n') {
4647c478bd9Sstevel@tonic-gate if (ferror(terminal)) {
4657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4667c478bd9Sstevel@tonic-gate "Error reading response\n"));
4677c478bd9Sstevel@tonic-gate (void) fflush(stderr);
4687c478bd9Sstevel@tonic-gate return (FAIL);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate if (feof(terminal))
4717c478bd9Sstevel@tonic-gate return (FAIL);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate if (isupper(c))
4747c478bd9Sstevel@tonic-gate c = tolower(c);
4757c478bd9Sstevel@tonic-gate } while (c != yesorno[0] && c != yesorno[1]);
4767c478bd9Sstevel@tonic-gate if (c == yesorno[0])
4777c478bd9Sstevel@tonic-gate return (GOOD);
4787c478bd9Sstevel@tonic-gate return (FAIL);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate /*
4827c478bd9Sstevel@tonic-gate * handle unexpected inconsistencies
4837c478bd9Sstevel@tonic-gate */
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate * Note that a panic w/ EOF on the tty means all panics will return...
4867c478bd9Sstevel@tonic-gate */
4877c478bd9Sstevel@tonic-gate #include <stdarg.h>
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate /* VARARGS1 */
4907c478bd9Sstevel@tonic-gate void
panic(const char * msg,...)4917c478bd9Sstevel@tonic-gate panic(const char *msg, ...)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate va_list args;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate va_start(args, msg);
4967c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, msg, args);
4977c478bd9Sstevel@tonic-gate va_end(args);
4987c478bd9Sstevel@tonic-gate if (reply(gettext("abort")) == GOOD) {
4997c478bd9Sstevel@tonic-gate if (reply(gettext("dump core")) == GOOD)
5007c478bd9Sstevel@tonic-gate abort();
5017c478bd9Sstevel@tonic-gate done(1);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate * Locale-specific version of ctime
5077c478bd9Sstevel@tonic-gate */
5087c478bd9Sstevel@tonic-gate char *
lctime(time_t * tp)509fe0e7ec4Smaheshvs lctime(time_t *tp)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate static char buf[256];
5127c478bd9Sstevel@tonic-gate struct tm *tm;
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate tm = localtime(tp);
5157c478bd9Sstevel@tonic-gate (void) strftime(buf, sizeof (buf), "%c\n", tm);
5167c478bd9Sstevel@tonic-gate return (buf);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate static int
statcmp(const struct stat * left,const struct stat * right)5207c478bd9Sstevel@tonic-gate statcmp(const struct stat *left, const struct stat *right)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate int result = 1;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate if ((left->st_dev == right->st_dev) &&
5257c478bd9Sstevel@tonic-gate (left->st_ino == right->st_ino) &&
5267c478bd9Sstevel@tonic-gate (left->st_mode == right->st_mode) &&
5277c478bd9Sstevel@tonic-gate (left->st_nlink == right->st_nlink) &&
5287c478bd9Sstevel@tonic-gate (left->st_uid == right->st_uid) &&
5297c478bd9Sstevel@tonic-gate (left->st_gid == right->st_gid) &&
5307c478bd9Sstevel@tonic-gate (left->st_rdev == right->st_rdev) &&
5317c478bd9Sstevel@tonic-gate (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
5327c478bd9Sstevel@tonic-gate (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
5337c478bd9Sstevel@tonic-gate (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
5347c478bd9Sstevel@tonic-gate (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec) &&
5357c478bd9Sstevel@tonic-gate (left->st_blksize == right->st_blksize) &&
5367c478bd9Sstevel@tonic-gate (left->st_blocks == right->st_blocks)) {
5377c478bd9Sstevel@tonic-gate result = 0;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate return (result);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate * Safely open a file.
5457c478bd9Sstevel@tonic-gate */
5467c478bd9Sstevel@tonic-gate int
safe_open(int dfd,const char * filename,int mode,int perms)5477c478bd9Sstevel@tonic-gate safe_open(int dfd, const char *filename, int mode, int perms)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate static int init_syslog = 1;
5507c478bd9Sstevel@tonic-gate int fd;
5517c478bd9Sstevel@tonic-gate int working_mode;
5527c478bd9Sstevel@tonic-gate int saverr;
5537c478bd9Sstevel@tonic-gate char *errtext;
5547c478bd9Sstevel@tonic-gate struct stat pre_stat, pre_lstat;
5557c478bd9Sstevel@tonic-gate struct stat post_stat, post_lstat;
5567c478bd9Sstevel@tonic-gate
5577c478bd9Sstevel@tonic-gate if (init_syslog) {
5587c478bd9Sstevel@tonic-gate openlog(progname, LOG_CONS, LOG_DAEMON);
5597c478bd9Sstevel@tonic-gate init_syslog = 0;
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate * Don't want to be spoofed into trashing something we
5647c478bd9Sstevel@tonic-gate * shouldn't, thus the following rigamarole. If it doesn't
5657c478bd9Sstevel@tonic-gate * exist, we create it and proceed. Otherwise, require that
5667c478bd9Sstevel@tonic-gate * what's there be a real file with no extraneous links and
5677c478bd9Sstevel@tonic-gate * owned by whoever ran us.
5687c478bd9Sstevel@tonic-gate *
5697c478bd9Sstevel@tonic-gate * The silliness with using both lstat() and fstat() is to avoid
5707c478bd9Sstevel@tonic-gate * race-condition games with someone replacing the file with a
5717c478bd9Sstevel@tonic-gate * symlink after we've opened it. If there was an flstat(),
5727c478bd9Sstevel@tonic-gate * we wouldn't need the fstat().
5737c478bd9Sstevel@tonic-gate *
5747c478bd9Sstevel@tonic-gate * The initial open with the hard-coded flags is ok even if we
5757c478bd9Sstevel@tonic-gate * are intending to open only for reading. If it succeeds,
5767c478bd9Sstevel@tonic-gate * then the file did not exist, and we'll synthesize an appropriate
5777c478bd9Sstevel@tonic-gate * complaint below. Otherwise, it does exist, so we won't be
5787c478bd9Sstevel@tonic-gate * truncating it with the open.
5797c478bd9Sstevel@tonic-gate */
5807c478bd9Sstevel@tonic-gate if ((fd = openat(dfd, filename,
5817c478bd9Sstevel@tonic-gate O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE, perms)) < 0) {
5827c478bd9Sstevel@tonic-gate if (errno == EEXIST) {
5837c478bd9Sstevel@tonic-gate if (fstatat(dfd, filename, &pre_lstat,
5847c478bd9Sstevel@tonic-gate AT_SYMLINK_NOFOLLOW) < 0) {
5857c478bd9Sstevel@tonic-gate saverr = errno;
5867c478bd9Sstevel@tonic-gate (void) close(fd);
5877c478bd9Sstevel@tonic-gate errno = saverr;
5887c478bd9Sstevel@tonic-gate return (-1);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate if (fstatat(dfd, filename, &pre_stat, 0) < 0) {
5927c478bd9Sstevel@tonic-gate saverr = errno;
5937c478bd9Sstevel@tonic-gate (void) close(fd);
5947c478bd9Sstevel@tonic-gate errno = saverr;
5957c478bd9Sstevel@tonic-gate return (-1);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
5997c478bd9Sstevel@tonic-gate working_mode |= O_LARGEFILE;
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate if ((fd = openat(dfd, filename, working_mode)) < 0) {
6027c478bd9Sstevel@tonic-gate if (errno == ENOENT) {
6037c478bd9Sstevel@tonic-gate errtext = gettext(
6047c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s used to exist, but doesn't any longer\n");
6057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext,
6067c478bd9Sstevel@tonic-gate filename);
6077c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6087c478bd9Sstevel@tonic-gate errno = ENOENT;
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate return (-1);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate if (fstatat(fd, NULL, &post_lstat,
6147c478bd9Sstevel@tonic-gate AT_SYMLINK_NOFOLLOW) < 0) {
6157c478bd9Sstevel@tonic-gate saverr = errno;
6167c478bd9Sstevel@tonic-gate (void) close(fd);
6177c478bd9Sstevel@tonic-gate errno = saverr;
6187c478bd9Sstevel@tonic-gate return (-1);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate if (fstatat(fd, NULL, &post_stat, 0) < 0) {
6227c478bd9Sstevel@tonic-gate saverr = errno;
6237c478bd9Sstevel@tonic-gate (void) close(fd);
6247c478bd9Sstevel@tonic-gate errno = saverr;
6257c478bd9Sstevel@tonic-gate return (-1);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate
6287c478bd9Sstevel@tonic-gate if (statcmp(&pre_lstat, &post_lstat) != 0) {
6297c478bd9Sstevel@tonic-gate errtext = gettext(
6307c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s's lstat(2) information changed\n");
6317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6327c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6337c478bd9Sstevel@tonic-gate errno = EPERM;
6347c478bd9Sstevel@tonic-gate return (-1);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate if (statcmp(&pre_stat, &post_stat) != 0) {
6387c478bd9Sstevel@tonic-gate errtext = gettext(
6397c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s's stat(2) information changed\n");
6407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6417c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6427c478bd9Sstevel@tonic-gate errno = EPERM;
6437c478bd9Sstevel@tonic-gate return (-1);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate * If inode, device, or type are wrong, bail out.
6487c478bd9Sstevel@tonic-gate */
6497c478bd9Sstevel@tonic-gate if ((!S_ISREG(post_lstat.st_mode) ||
6507c478bd9Sstevel@tonic-gate (post_stat.st_ino != post_lstat.st_ino) ||
6517c478bd9Sstevel@tonic-gate (post_stat.st_dev != post_lstat.st_dev))) {
6527c478bd9Sstevel@tonic-gate errtext = gettext(
6537c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s is not a regular file\n");
6547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6557c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6567c478bd9Sstevel@tonic-gate (void) close(fd);
6577c478bd9Sstevel@tonic-gate errno = EPERM;
6587c478bd9Sstevel@tonic-gate return (-1);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Bad link count implies someone's linked our
6637c478bd9Sstevel@tonic-gate * target to something else, which we probably
6647c478bd9Sstevel@tonic-gate * shouldn't step on.
6657c478bd9Sstevel@tonic-gate */
6667c478bd9Sstevel@tonic-gate if (post_lstat.st_nlink != 1) {
6677c478bd9Sstevel@tonic-gate errtext = gettext(
6687c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s must have exactly one link\n");
6697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6707c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6717c478bd9Sstevel@tonic-gate (void) close(fd);
6727c478bd9Sstevel@tonic-gate errno = EPERM;
6737c478bd9Sstevel@tonic-gate return (-1);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate * Root might make a file, but non-root might
6777c478bd9Sstevel@tonic-gate * need to open it. If the permissions let us
6787c478bd9Sstevel@tonic-gate * get this far, then let it through.
6797c478bd9Sstevel@tonic-gate */
6807c478bd9Sstevel@tonic-gate if (post_lstat.st_uid != getuid() &&
6817c478bd9Sstevel@tonic-gate post_lstat.st_uid != 0) {
6827c478bd9Sstevel@tonic-gate errtext = gettext(
6837c478bd9Sstevel@tonic-gate "Unsupported condition detected: %s must be owned by uid %ld or 0\n");
6847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename,
6857c478bd9Sstevel@tonic-gate (long)getuid());
6867c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename,
6877c478bd9Sstevel@tonic-gate (long)getuid());
6887c478bd9Sstevel@tonic-gate (void) close(fd);
6897c478bd9Sstevel@tonic-gate errno = EPERM;
6907c478bd9Sstevel@tonic-gate return (-1);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate if (mode & (O_WRONLY|O_TRUNC)) {
6937c478bd9Sstevel@tonic-gate if (ftruncate(fd, (off_t)0) < 0) {
6947c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6957c478bd9Sstevel@tonic-gate "ftruncate(%s): %s\n",
6967c478bd9Sstevel@tonic-gate filename, strerror(errno));
6977c478bd9Sstevel@tonic-gate (void) close(fd);
6987c478bd9Sstevel@tonic-gate return (-1);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate } else {
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate * Didn't exist, but couldn't open it.
7047c478bd9Sstevel@tonic-gate */
7057c478bd9Sstevel@tonic-gate return (-1);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate } else {
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate * If truncating open succeeded for a read-only open,
7107c478bd9Sstevel@tonic-gate * bail out, as we really shouldn't have succeeded.
7117c478bd9Sstevel@tonic-gate */
7127c478bd9Sstevel@tonic-gate if (mode & O_RDONLY) {
7137c478bd9Sstevel@tonic-gate /* Undo the O_CREAT */
7147c478bd9Sstevel@tonic-gate (void) unlinkat(dfd, filename, 0);
7157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "open(%s): %s\n",
7167c478bd9Sstevel@tonic-gate filename, strerror(ENOENT));
7177c478bd9Sstevel@tonic-gate (void) close(fd);
7187c478bd9Sstevel@tonic-gate errno = ENOENT;
7197c478bd9Sstevel@tonic-gate return (-1);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate return (fd);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate * STDIO version of safe_open. Equivalent to fopen64(...).
7287c478bd9Sstevel@tonic-gate */
7297c478bd9Sstevel@tonic-gate FILE *
safe_fopen(const char * filename,const char * smode,int perms)7307c478bd9Sstevel@tonic-gate safe_fopen(const char *filename, const char *smode, int perms)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate int fd;
7337c478bd9Sstevel@tonic-gate int bmode;
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate * accepts only modes "r", "r+", and "w"
7377c478bd9Sstevel@tonic-gate */
7387c478bd9Sstevel@tonic-gate if (smode[0] == 'r') {
7397c478bd9Sstevel@tonic-gate if (smode[1] == '\0') {
7407c478bd9Sstevel@tonic-gate bmode = O_RDONLY;
7417c478bd9Sstevel@tonic-gate } else if ((smode[1] == '+') && (smode[2] == '\0')) {
7427c478bd9Sstevel@tonic-gate bmode = O_RDWR;
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate } else if ((smode[0] == 'w') && (smode[1] == '\0')) {
7457c478bd9Sstevel@tonic-gate bmode = O_WRONLY;
7467c478bd9Sstevel@tonic-gate } else {
7477c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7487c478bd9Sstevel@tonic-gate gettext("internal error: safe_fopen: invalid mode `%s'\n"),
7497c478bd9Sstevel@tonic-gate smode);
7507c478bd9Sstevel@tonic-gate return (NULL);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate fd = safe_open(AT_FDCWD, filename, bmode, perms);
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate * caller is expected to report error.
7577c478bd9Sstevel@tonic-gate */
7587c478bd9Sstevel@tonic-gate if (fd >= 0)
7597c478bd9Sstevel@tonic-gate return (fdopen(fd, smode));
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate return ((FILE *)NULL);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate * Read the contents of a directory.
7667c478bd9Sstevel@tonic-gate */
7677c478bd9Sstevel@tonic-gate int
mkentry(char * name,ino_t ino,struct arglist * ap)768fe0e7ec4Smaheshvs mkentry(char *name, ino_t ino, struct arglist *ap)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate struct afile *fp;
7717c478bd9Sstevel@tonic-gate
7727c478bd9Sstevel@tonic-gate if (ap->base == NULL) {
7737c478bd9Sstevel@tonic-gate ap->nent = 20;
7747c478bd9Sstevel@tonic-gate ap->base = (struct afile *)calloc((unsigned)ap->nent,
7757c478bd9Sstevel@tonic-gate sizeof (*(ap->base)));
7767c478bd9Sstevel@tonic-gate if (ap->base == NULL) {
7777c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7787c478bd9Sstevel@tonic-gate gettext("%s: out of memory\n"), ap->cmd);
7797c478bd9Sstevel@tonic-gate return (FAIL);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate if (ap->head == NULL)
7837c478bd9Sstevel@tonic-gate ap->head = ap->last = ap->base;
7847c478bd9Sstevel@tonic-gate fp = ap->last;
7857c478bd9Sstevel@tonic-gate fp->fnum = ino;
7867c478bd9Sstevel@tonic-gate fp->fname = savename(name);
7877c478bd9Sstevel@tonic-gate fp++;
7887c478bd9Sstevel@tonic-gate if (fp == ap->head + ap->nent) {
7897c478bd9Sstevel@tonic-gate ap->base = (struct afile *)realloc((char *)ap->base,
7907c478bd9Sstevel@tonic-gate (size_t)(2 * ap->nent * (size_t)sizeof (*(ap->base))));
7917c478bd9Sstevel@tonic-gate if (ap->base == NULL) {
7927c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7937c478bd9Sstevel@tonic-gate gettext("%s: out of memory\n"), ap->cmd);
7947c478bd9Sstevel@tonic-gate return (FAIL);
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate ap->head = ap->base;
7977c478bd9Sstevel@tonic-gate fp = ap->head + ap->nent;
7987c478bd9Sstevel@tonic-gate ap->nent *= 2;
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate ap->last = fp;
8017c478bd9Sstevel@tonic-gate return (GOOD);
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate static int gmatch(wchar_t *, wchar_t *);
8057c478bd9Sstevel@tonic-gate static int addg(struct direct *, char *, char *, struct arglist *);
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate * XXX This value is ASCII (but not language) dependent. In
8097c478bd9Sstevel@tonic-gate * ASCII, it is the DEL character (unlikely to appear in paths).
8107c478bd9Sstevel@tonic-gate * If you are compiling on an EBCDIC-based machine, re-define
8117c478bd9Sstevel@tonic-gate * this (0x7f is '"') to be something like 0x7 (DEL). It's
8127c478bd9Sstevel@tonic-gate * either this hack or re-write the expand() algorithm...
8137c478bd9Sstevel@tonic-gate */
8147c478bd9Sstevel@tonic-gate #define DELIMCHAR ((char)0x7f)
8157c478bd9Sstevel@tonic-gate
8167c478bd9Sstevel@tonic-gate /*
8177c478bd9Sstevel@tonic-gate * Expand a file name.
8187c478bd9Sstevel@tonic-gate * "as" is the pattern to expand.
8197c478bd9Sstevel@tonic-gate * "rflg" non-zero indicates that we're recursing.
8207c478bd9Sstevel@tonic-gate * "ap" is where to put the results of the expansion.
8217c478bd9Sstevel@tonic-gate *
8227c478bd9Sstevel@tonic-gate * Our caller guarantees that "as" is at least the string ".".
8237c478bd9Sstevel@tonic-gate */
8247c478bd9Sstevel@tonic-gate int
expand(char * as,int rflg,struct arglist * ap)825fe0e7ec4Smaheshvs expand(char *as, int rflg, struct arglist *ap)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate int count, size;
8287c478bd9Sstevel@tonic-gate char dir = 0;
8297c478bd9Sstevel@tonic-gate char *rescan = 0;
8307c478bd9Sstevel@tonic-gate RST_DIR *dirp;
8317c478bd9Sstevel@tonic-gate char *s, *cs;
8327c478bd9Sstevel@tonic-gate int sindex, rindexa, lindex;
8337c478bd9Sstevel@tonic-gate struct direct *dp;
8347c478bd9Sstevel@tonic-gate char slash;
8357c478bd9Sstevel@tonic-gate char *rs;
8367c478bd9Sstevel@tonic-gate char c;
8377c478bd9Sstevel@tonic-gate wchar_t w_fname[PATH_MAX+1];
8387c478bd9Sstevel@tonic-gate wchar_t w_pname[PATH_MAX+1];
8397c478bd9Sstevel@tonic-gate
8407c478bd9Sstevel@tonic-gate /*
8417c478bd9Sstevel@tonic-gate * check for meta chars
8427c478bd9Sstevel@tonic-gate */
8437c478bd9Sstevel@tonic-gate s = cs = as;
8447c478bd9Sstevel@tonic-gate slash = 0;
8457c478bd9Sstevel@tonic-gate while (*cs != '*' && *cs != '?' && *cs != '[') {
8467c478bd9Sstevel@tonic-gate if (*cs++ == 0) {
8477c478bd9Sstevel@tonic-gate if (rflg && slash)
8487c478bd9Sstevel@tonic-gate break;
8497c478bd9Sstevel@tonic-gate else
8507c478bd9Sstevel@tonic-gate return (0);
8517c478bd9Sstevel@tonic-gate } else if (*cs == '/') {
8527c478bd9Sstevel@tonic-gate slash++;
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate for (;;) {
8567c478bd9Sstevel@tonic-gate if (cs == s) {
8577c478bd9Sstevel@tonic-gate s = "";
8587c478bd9Sstevel@tonic-gate break;
8597c478bd9Sstevel@tonic-gate } else if (*--cs == '/') {
8607c478bd9Sstevel@tonic-gate *cs = 0;
8617c478bd9Sstevel@tonic-gate if (s == cs)
8627c478bd9Sstevel@tonic-gate s = "/";
8637c478bd9Sstevel@tonic-gate break;
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate if ((dirp = rst_opendir(s)) != NULL)
8677c478bd9Sstevel@tonic-gate dir++;
8687c478bd9Sstevel@tonic-gate count = 0;
8697c478bd9Sstevel@tonic-gate if (*cs == 0)
8707c478bd9Sstevel@tonic-gate *cs++ = DELIMCHAR;
8717c478bd9Sstevel@tonic-gate if (dir) {
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate * check for rescan
8747c478bd9Sstevel@tonic-gate */
8757c478bd9Sstevel@tonic-gate rs = cs;
8767c478bd9Sstevel@tonic-gate do {
8777c478bd9Sstevel@tonic-gate if (*rs == '/') {
8787c478bd9Sstevel@tonic-gate rescan = rs;
8797c478bd9Sstevel@tonic-gate *rs = 0;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate } while (*rs++);
8827c478bd9Sstevel@tonic-gate /* LINTED: result fits into an int */
8837c478bd9Sstevel@tonic-gate sindex = (int)(ap->last - ap->head);
8847c478bd9Sstevel@tonic-gate (void) mbstowcs(w_pname, cs, PATH_MAX);
8857c478bd9Sstevel@tonic-gate w_pname[PATH_MAX - 1] = 0;
8867c478bd9Sstevel@tonic-gate while ((dp = rst_readdir(dirp)) != NULL && dp->d_ino != 0) {
8877c478bd9Sstevel@tonic-gate if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
8887c478bd9Sstevel@tonic-gate continue;
8897c478bd9Sstevel@tonic-gate if ((*dp->d_name == '.' && *cs != '.'))
8907c478bd9Sstevel@tonic-gate continue;
8917c478bd9Sstevel@tonic-gate (void) mbstowcs(w_fname, dp->d_name, PATH_MAX);
8927c478bd9Sstevel@tonic-gate w_fname[PATH_MAX - 1] = 0;
8937c478bd9Sstevel@tonic-gate if (gmatch(w_fname, w_pname)) {
8947c478bd9Sstevel@tonic-gate if (addg(dp, s, rescan, ap) < 0) {
8957c478bd9Sstevel@tonic-gate rst_closedir(dirp);
8967c478bd9Sstevel@tonic-gate return (-1);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate count++;
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate if (rescan) {
9027c478bd9Sstevel@tonic-gate rindexa = sindex;
9037c478bd9Sstevel@tonic-gate /* LINTED: result fits into an int */
9047c478bd9Sstevel@tonic-gate lindex = (int)(ap->last - ap->head);
9057c478bd9Sstevel@tonic-gate if (count) {
9067c478bd9Sstevel@tonic-gate count = 0;
9077c478bd9Sstevel@tonic-gate while (rindexa < lindex) {
9087c478bd9Sstevel@tonic-gate size = expand(ap->head[rindexa].fname,
9097c478bd9Sstevel@tonic-gate 1, ap);
9107c478bd9Sstevel@tonic-gate if (size < 0) {
9117c478bd9Sstevel@tonic-gate rst_closedir(dirp);
9127c478bd9Sstevel@tonic-gate return (size);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate count += size;
9157c478bd9Sstevel@tonic-gate rindexa++;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate /* LINTED: lint is confused about pointer size/type */
9197c478bd9Sstevel@tonic-gate bcopy((void *)(&ap->head[lindex]),
9207c478bd9Sstevel@tonic-gate (void *)(&ap->head[sindex]),
9217c478bd9Sstevel@tonic-gate (size_t)((ap->last - &ap->head[rindexa])) *
9227c478bd9Sstevel@tonic-gate sizeof (*ap->head));
9237c478bd9Sstevel@tonic-gate ap->last -= lindex - sindex;
9247c478bd9Sstevel@tonic-gate *rescan = '/';
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate rst_closedir(dirp);
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate s = as;
9297c478bd9Sstevel@tonic-gate while ((c = *s) != '\0')
9307c478bd9Sstevel@tonic-gate *s++ = (c != DELIMCHAR ? c : '/');
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate return (count);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate /*
9367c478bd9Sstevel@tonic-gate * Check for a name match
9377c478bd9Sstevel@tonic-gate */
9387c478bd9Sstevel@tonic-gate static int
gmatch(wchar_t * s,wchar_t * p)939fe0e7ec4Smaheshvs gmatch(wchar_t *s, wchar_t *p)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate long scc; /* source character to text */
9427c478bd9Sstevel@tonic-gate wchar_t c; /* pattern character to match */
9437c478bd9Sstevel@tonic-gate char ok; /* [x-y] range match status */
9447c478bd9Sstevel@tonic-gate long lc; /* left character of [x-y] range */
9457c478bd9Sstevel@tonic-gate
9467c478bd9Sstevel@tonic-gate scc = *s++;
9477c478bd9Sstevel@tonic-gate switch (c = *p++) {
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate case '[':
9507c478bd9Sstevel@tonic-gate ok = 0;
9517c478bd9Sstevel@tonic-gate lc = -1;
9527c478bd9Sstevel@tonic-gate while (c = *p++) {
9537c478bd9Sstevel@tonic-gate if (c == ']') {
9547c478bd9Sstevel@tonic-gate return (ok ? gmatch(s, p) : 0);
9557c478bd9Sstevel@tonic-gate } else if (c == '-') {
9567c478bd9Sstevel@tonic-gate wchar_t rc = *p++;
9577c478bd9Sstevel@tonic-gate /*
9587c478bd9Sstevel@tonic-gate * Check both ends must belong to
9597c478bd9Sstevel@tonic-gate * the same codeset.
9607c478bd9Sstevel@tonic-gate */
9617c478bd9Sstevel@tonic-gate if (wcsetno(lc) != wcsetno(rc)) {
9627c478bd9Sstevel@tonic-gate /*
9637c478bd9Sstevel@tonic-gate * If not, ignore the '-'
9647c478bd9Sstevel@tonic-gate * operator and [x-y] is
9657c478bd9Sstevel@tonic-gate * treated as if it were
9667c478bd9Sstevel@tonic-gate * [xy].
9677c478bd9Sstevel@tonic-gate */
9687c478bd9Sstevel@tonic-gate if (scc == lc)
9697c478bd9Sstevel@tonic-gate ok++;
9707c478bd9Sstevel@tonic-gate if (scc == (lc = rc))
9717c478bd9Sstevel@tonic-gate ok++;
9727c478bd9Sstevel@tonic-gate } else if (lc <= scc && scc <= rc)
9737c478bd9Sstevel@tonic-gate ok++;
9747c478bd9Sstevel@tonic-gate } else {
9757c478bd9Sstevel@tonic-gate lc = c;
9767c478bd9Sstevel@tonic-gate if (scc == lc)
9777c478bd9Sstevel@tonic-gate ok++;
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate /* No closing bracket => failure */
9817c478bd9Sstevel@tonic-gate return (0);
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate default:
9847c478bd9Sstevel@tonic-gate if (c != scc)
9857c478bd9Sstevel@tonic-gate return (0);
9867c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate case '?':
9897c478bd9Sstevel@tonic-gate return (scc ? gmatch(s, p) : 0);
9907c478bd9Sstevel@tonic-gate
9917c478bd9Sstevel@tonic-gate case '*':
9927c478bd9Sstevel@tonic-gate if (*p == 0)
9937c478bd9Sstevel@tonic-gate return (1);
9947c478bd9Sstevel@tonic-gate s--;
9957c478bd9Sstevel@tonic-gate while (*s) {
9967c478bd9Sstevel@tonic-gate if (gmatch(s++, p))
9977c478bd9Sstevel@tonic-gate return (1);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate return (0);
10007c478bd9Sstevel@tonic-gate
10017c478bd9Sstevel@tonic-gate case 0:
10027c478bd9Sstevel@tonic-gate return (scc == 0);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate
10067c478bd9Sstevel@tonic-gate /*
10077c478bd9Sstevel@tonic-gate * Construct a matched name.
10087c478bd9Sstevel@tonic-gate */
10097c478bd9Sstevel@tonic-gate static int
addg(struct direct * dp,char * as1,char * as3,struct arglist * ap)1010fe0e7ec4Smaheshvs addg(struct direct *dp, char *as1, char *as3, struct arglist *ap)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate char *s1, *s2, *limit;
10137c478bd9Sstevel@tonic-gate int c;
10147c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN + 1];
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate s2 = buf;
10177c478bd9Sstevel@tonic-gate limit = buf + sizeof (buf) - 1;
10187c478bd9Sstevel@tonic-gate s1 = as1;
10197c478bd9Sstevel@tonic-gate while ((c = *s1++) != '\0' && s2 < limit) {
10207c478bd9Sstevel@tonic-gate if (c == DELIMCHAR) {
10217c478bd9Sstevel@tonic-gate *s2++ = '/';
10227c478bd9Sstevel@tonic-gate break;
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate /* LINTED narrowing cast */
10257c478bd9Sstevel@tonic-gate *s2++ = (char)c;
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate s1 = dp->d_name;
10287c478bd9Sstevel@tonic-gate while ((*s2 = *s1++) != '\0' && s2 < limit)
10297c478bd9Sstevel@tonic-gate s2++;
10307c478bd9Sstevel@tonic-gate s1 = as3;
10317c478bd9Sstevel@tonic-gate if (s1 != NULL && s2 < limit) {
10327c478bd9Sstevel@tonic-gate *s2++ = '/';
10337c478bd9Sstevel@tonic-gate
10347c478bd9Sstevel@tonic-gate while ((*s2++ = *++s1) != '\0' && s2 < limit) {
10357c478bd9Sstevel@tonic-gate continue;
10367c478bd9Sstevel@tonic-gate /*LINTED [empty loop body]*/
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate *s2 = '\0';
10407c478bd9Sstevel@tonic-gate if (mkentry(buf, dp->d_ino, ap) == FAIL)
10417c478bd9Sstevel@tonic-gate return (-1);
10427c478bd9Sstevel@tonic-gate return (0);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate * Resolve a "complex" pathname (as generated by myname()) into
10487c478bd9Sstevel@tonic-gate * a file descriptor and a relative path. The file descriptor
10497c478bd9Sstevel@tonic-gate * will reference the hidden directory containing the attribute
10507c478bd9Sstevel@tonic-gate * named by the relative path. If the provided path is not
10517c478bd9Sstevel@tonic-gate * complex, the returned file descriptor will be AT_FDCWD and rpath
10527c478bd9Sstevel@tonic-gate * will equal path.
10537c478bd9Sstevel@tonic-gate *
10547c478bd9Sstevel@tonic-gate * This function is intended to be used to transform a complex
10557c478bd9Sstevel@tonic-gate * pathname into a pair of handles that can be used to actually
10567c478bd9Sstevel@tonic-gate * manipulate the named file. Since extended attributes have
10577c478bd9Sstevel@tonic-gate * an independant name space, a file descriptor for a directory
10587c478bd9Sstevel@tonic-gate * in the attribute name space is necessary to actually manipulate
10597c478bd9Sstevel@tonic-gate * the attribute file (via the path-relative xxxat() system calls
10607c478bd9Sstevel@tonic-gate * or a call to fchdir()).
10617c478bd9Sstevel@tonic-gate *
10627c478bd9Sstevel@tonic-gate * In the event of an error, the returned file descriptor will be
10637c478bd9Sstevel@tonic-gate * -1. It is expected that callers will either check for this
10647c478bd9Sstevel@tonic-gate * condition directly, or attempt to use the descriptor, fail, and
10657c478bd9Sstevel@tonic-gate * generate an appropriate context-specific error message.
10667c478bd9Sstevel@tonic-gate *
10677c478bd9Sstevel@tonic-gate * This function is pretty much a no-op for "simple" (non-attribute)
10687c478bd9Sstevel@tonic-gate * paths.
10697c478bd9Sstevel@tonic-gate */
10707c478bd9Sstevel@tonic-gate void
resolve(char * path,int * fd,char ** rpath)1071fe0e7ec4Smaheshvs resolve(char *path, int *fd, char **rpath)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate int tfd;
10747c478bd9Sstevel@tonic-gate
10757c478bd9Sstevel@tonic-gate *fd = tfd = AT_FDCWD;
10767c478bd9Sstevel@tonic-gate *rpath = path;
10777c478bd9Sstevel@tonic-gate path = *rpath + strlen(*rpath) +1;
10787c478bd9Sstevel@tonic-gate while (*path != '\0' &&
10797c478bd9Sstevel@tonic-gate (*fd = openat64(tfd, *rpath, O_RDONLY)) > 0) {
1080*82be58ffSToomas Soome if (tfd != AT_FDCWD)
1081*82be58ffSToomas Soome (void) close(tfd);
10827c478bd9Sstevel@tonic-gate tfd = *fd;
10837c478bd9Sstevel@tonic-gate *rpath = path;
10847c478bd9Sstevel@tonic-gate path = *rpath + strlen(*rpath) +1;
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate if (*fd == AT_FDCWD)
10877c478bd9Sstevel@tonic-gate return;
10887c478bd9Sstevel@tonic-gate if (*fd < 0 || (*fd = openat64(tfd, ".", O_RDONLY|O_XATTR)) < 0) {
10897c478bd9Sstevel@tonic-gate int saverr = errno;
10907c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10917c478bd9Sstevel@tonic-gate gettext("Warning: cannot fully resolve %s: %s"),
10927c478bd9Sstevel@tonic-gate path, strerror(saverr));
10937c478bd9Sstevel@tonic-gate (void) fflush(stderr);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate if (tfd != AT_FDCWD) (void) close(tfd);
10967c478bd9Sstevel@tonic-gate }
10977c478bd9Sstevel@tonic-gate
10987c478bd9Sstevel@tonic-gate /*
10997c478bd9Sstevel@tonic-gate * Copy a complex pathname to another string. Note that the
11007c478bd9Sstevel@tonic-gate * length returned by this function is the number of characters
11017c478bd9Sstevel@tonic-gate * up to (but not including) the final NULL.
11027c478bd9Sstevel@tonic-gate */
11037c478bd9Sstevel@tonic-gate int
complexcpy(char * s1,char * s2,int max)1104fe0e7ec4Smaheshvs complexcpy(char *s1, char *s2, int max)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate int nullseen = 0;
11077c478bd9Sstevel@tonic-gate int len = 0;
11087c478bd9Sstevel@tonic-gate
11097c478bd9Sstevel@tonic-gate while (len++ < max) {
11107c478bd9Sstevel@tonic-gate *s1++ = *s2;
11117c478bd9Sstevel@tonic-gate if (*s2++ == '\0') {
11127c478bd9Sstevel@tonic-gate if (nullseen)
11137c478bd9Sstevel@tonic-gate return (len-1);
11147c478bd9Sstevel@tonic-gate else
11157c478bd9Sstevel@tonic-gate nullseen = 1;
11167c478bd9Sstevel@tonic-gate } else {
11177c478bd9Sstevel@tonic-gate nullseen = 0;
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate *s1 = '\0';
11217c478bd9Sstevel@tonic-gate if (nullseen == 0)
11227c478bd9Sstevel@tonic-gate *--s1 = '\0';
11237c478bd9Sstevel@tonic-gate fprintf(stderr,
11247c478bd9Sstevel@tonic-gate gettext("Warning: unterminated source string in complexcpy\n"));
11257c478bd9Sstevel@tonic-gate return (max-1);
11267c478bd9Sstevel@tonic-gate }
1127