X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fmisc.c;h=b8ef6eabf1ae16e43b1fece6d948b7bf0bc65b76;hb=e3d28d84bda24a45c239b398e7b42ccd9be2d0c2;hp=1b78b69ea199cdc09e29a1c769eef595a4165594;hpb=20dcc4d1226a4c170b56c1f2472a66bc67fd1f1d;p=chaz%2Ftar diff --git a/src/misc.c b/src/misc.c index 1b78b69..b8ef6ea 100644 --- a/src/misc.c +++ b/src/misc.c @@ -283,7 +283,7 @@ normalize_filename (const char *name) getcwd is slow, it might fail, and it does not necessarily return a canonical name even when it succeeds. Perhaps we can use dev+ino pairs instead of names? */ - copy = xgetcwd (); + copy = tar_getcwd (); if (copy) { size_t copylen = strlen (copy); @@ -631,7 +631,7 @@ remove_any_file (const char *file_name, enum remove_option option) case RECURSIVE_REMOVE_OPTION: { - char *directory = savedir (file_name); + char *directory = tar_savedir (file_name, 0); char const *entry; size_t entrylen; @@ -976,6 +976,21 @@ chdir_do (int i) } } +char * +tar_getcwd (void) +{ + static char *cwd; + namebuf_t nbuf; + int i; + + if (!cwd) + cwd = xgetcwd (); + nbuf = namebuf_create (cwd); + for (i = 1; i <= chdir_current; i++) + namebuf_add_dir (nbuf, wd[i].name); + return namebuf_finish (nbuf); +} + void close_diag (char const *name) { @@ -1144,3 +1159,55 @@ namebuf_name (namebuf_t buf, const char *name) strcpy (buf->buffer + buf->dir_length, name); return buf->buffer; } + +void +namebuf_add_dir (namebuf_t buf, const char *name) +{ + static char dirsep[] = { DIRECTORY_SEPARATOR, 0 }; + if (!ISSLASH (buf->buffer[buf->dir_length - 1])) + { + namebuf_name (buf, dirsep); + buf->dir_length++; + } + namebuf_name (buf, name); + buf->dir_length += strlen (name); +} + +char * +namebuf_finish (namebuf_t buf) +{ + char *res = buf->buffer; + + if (ISSLASH (buf->buffer[buf->dir_length - 1])) + buf->buffer[buf->dir_length] = 0; + free (buf); + return res; +} + +/* Return the filenames in directory NAME, relative to the chdir_fd. + If the directory does not exist, report error if MUST_EXIST is + true. + + Return NULL on errors. +*/ +char * +tar_savedir (const char *name, int must_exist) +{ + char *ret = NULL; + DIR *dir = NULL; + int fd = openat (chdir_fd, name, open_read_flags | O_DIRECTORY); + if (fd < 0) + { + if (!must_exist && errno == ENOENT) + return NULL; + open_error (name); + } + else if (! ((dir = fdopendir (fd)) + && (ret = streamsavedir (dir)))) + savedir_error (name); + + if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0) + savedir_error (name); + + return ret; +}