+
+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_sort_order))))
+ savedir_error (name);
+
+ if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0)
+ savedir_error (name);
+
+ return ret;
+}