- DIR *directory;
- struct dirent *entry, *result;
- char *link;
- int link_len;
- int len;
- int status;
- dev_t our_device = ff_pkt->statp.st_dev;
-
- if (access(fname, R_OK) == -1 && geteuid() != 0) {
- /* Could not access() directory */
- ff_pkt->type = FT_NOACCESS;
- ff_pkt->ff_errno = errno;
- return handle_file(ff_pkt, pkt);
- }
-
- /* Build a canonical directory name with a trailing slash. */
- len = strlen(fname);
- link_len = len + 200;
- link = (char *)bmalloc(link_len + 2);
- bstrncpy(link, fname, link_len);
- /* Strip all trailing slashes */
- while (len >= 1 && link[len - 1] == '/')
- len--;
- link[len++] = '/'; /* add back one */
- link[len] = 0;
-
- ff_pkt->link = link;
- if (ff_pkt->incremental &&
- (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
- ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
- /* Incremental option, directory entry not changed */
- ff_pkt->type = FT_DIRNOCHG;
- } else {
- ff_pkt->type = FT_DIR;
- }
- handle_file(ff_pkt, pkt); /* handle directory entry */
-
- ff_pkt->link = ff_pkt->fname; /* reset "link" */
-
- /*
- * Do not decend into subdirectories (recurse) if the
- * user has turned it off for this directory.
- */
- if (ff_pkt->flags & FO_NO_RECURSION) {
- free(link);
- /* No recursion into this directory */
- ff_pkt->type = FT_NORECURSE;
- return handle_file(ff_pkt, pkt);
- }
-
- /*
- * See if we are crossing file systems, and
- * avoid doing so if the user only wants to dump one file system.
- */
- if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
- parent_device != ff_pkt->statp.st_dev) {
- free(link);
- /* returning here means we do not handle this directory */
- ff_pkt->type = FT_NOFSCHG;
- return handle_file(ff_pkt, pkt);
- }
- /*
- * Now process the files in this directory.
- */
- errno = 0;
- if ((directory = opendir(fname)) == NULL) {
- free(link);
- ff_pkt->type = FT_NOOPEN;
- ff_pkt->ff_errno = errno;
- return handle_file(ff_pkt, pkt);
- }
-
- /*
- * This would possibly run faster if we chdir to the directory
- * before traversing it.
- */
- rtn_stat = 1;
- entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
- for ( ; !job_cancelled(jcr); ) {
- char *p, *q;
- int i;
-
- status = readdir_r(directory, entry, &result);
- Dmsg3(200, "readdir stat=%d result=%x name=%s\n", status, result,
- entry->d_name);
- if (status != 0 || result == NULL) {
- break;
- }
- ASSERT(name_max+1 > sizeof(struct dirent) + (int)NAMELEN(entry));
- p = entry->d_name;
- /* Skip `.', `..', and excluded file names. */
- if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
- (p[1] == '.' && p[2] == '\0')))) {
- continue;
- }
-
- if ((int)NAMELEN(entry) + len >= link_len) {
- link_len = len + NAMELEN(entry) + 1;
- link = (char *)brealloc(link, link_len + 1);
- }
- q = link + len;
- for (i=0; i < (int)NAMELEN(entry); i++) {
- *q++ = *p++;
- }
- *q = 0;
- if (!file_is_excluded(ff_pkt, link)) {
- rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, 0);
- }
- }
- closedir(directory);
- free(link);
- free(entry);
-
- if (ff_pkt->atime_preserve) {
- utime(fname, &restore_times);
- }
- return rtn_stat;
+ DIR *directory;
+ struct dirent *entry, *result;
+ char *link;
+ int link_len;
+ int len;
+ int status;
+ dev_t our_device = ff_pkt->statp.st_dev;
+
+ /*
+ * If we are using Win32 (non-portable) backup API, don't check
+ * access as everything is more complicated, and
+ * in principle, we should be able to access everything.
+ */
+ if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
+ if (access(fname, R_OK) == -1 && geteuid() != 0) {
+ /* Could not access() directory */
+ ff_pkt->type = FT_NOACCESS;
+ ff_pkt->ff_errno = errno;
+ rtn_stat = handle_file(ff_pkt, pkt);
+ if (ff_pkt->linked) {
+ ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
+ }
+ return rtn_stat;
+ }
+ }
+
+ /* Build a canonical directory name with a trailing slash in link var */
+ len = strlen(fname);
+ link_len = len + 200;
+ link = (char *)bmalloc(link_len + 2);
+ bstrncpy(link, fname, link_len);
+ /* Strip all trailing slashes */
+ while (len >= 1 && link[len - 1] == '/')
+ len--;
+ link[len++] = '/'; /* add back one */
+ link[len] = 0;
+
+ ff_pkt->link = link;
+ if (ff_pkt->incremental &&
+ (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
+ ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
+ /* Incremental option, directory entry not changed */
+ ff_pkt->type = FT_DIRNOCHG;
+ } else {
+ ff_pkt->type = FT_DIR;
+ }
+
+ /*
+ * Create a temporary ff packet for this directory
+ * entry, and defer handling the directory until
+ * we have recursed into it. This saves the
+ * directory after all files have been processed, and
+ * during the restore, the directory permissions will
+ * be reset after all the files have been restored.
+ */
+ Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
+ FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
+ memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
+ dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
+ dir_ff_pkt->link = bstrdup(ff_pkt->link);
+ dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
+ dir_ff_pkt->included_files_list = NULL;
+ dir_ff_pkt->excluded_files_list = NULL;
+ dir_ff_pkt->excluded_paths_list = NULL;
+ dir_ff_pkt->linklist = NULL;
+
+ ff_pkt->link = ff_pkt->fname; /* reset "link" */
+
+ /*
+ * Do not decend into subdirectories (recurse) if the
+ * user has turned it off for this directory.
+ */
+ if (ff_pkt->flags & FO_NO_RECURSION) {
+ /* No recursion into this directory */
+ ff_pkt->type = FT_NORECURSE;
+ rtn_stat = handle_file(ff_pkt, pkt);
+ if (ff_pkt->linked) {
+ ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
+ }
+ free(link);
+ free_dir_ff_pkt(dir_ff_pkt);
+ return rtn_stat;
+ }
+
+ /*
+ * See if we are crossing file systems, and
+ * avoid doing so if the user only wants to dump one file system.
+ */
+ if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
+ parent_device != ff_pkt->statp.st_dev) {
+ /* returning here means we do not handle this directory */
+ ff_pkt->type = FT_NOFSCHG;
+ rtn_stat = handle_file(ff_pkt, pkt);
+ if (ff_pkt->linked) {
+ ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
+ }
+ free(link);
+ free_dir_ff_pkt(dir_ff_pkt);
+ return rtn_stat;
+ }
+ /*
+ * Decend into or "recurse" into the directory to read
+ * all the files in it.
+ */
+ errno = 0;
+ if ((directory = opendir(fname)) == NULL) {
+ ff_pkt->type = FT_NOOPEN;
+ ff_pkt->ff_errno = errno;
+ rtn_stat = handle_file(ff_pkt, pkt);
+ if (ff_pkt->linked) {
+ ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
+ }
+ free(link);
+ free_dir_ff_pkt(dir_ff_pkt);
+ return rtn_stat;
+ }
+
+ /*
+ * Process all files in this directory entry (recursing).
+ * This would possibly run faster if we chdir to the directory
+ * before traversing it.
+ */
+ rtn_stat = 1;
+ entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
+ for ( ; !job_canceled(jcr); ) {
+ char *p, *q;
+ int i;
+
+ status = readdir_r(directory, entry, &result);
+ if (status != 0 || result == NULL) {
+// Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
+// status, (long)result);
+ break;
+ }
+ ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
+ p = entry->d_name;
+ /* Skip `.', `..', and excluded file names. */
+ if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
+ (p[1] == '.' && p[2] == '\0')))) {
+ continue;
+ }
+
+ if ((int)NAMELEN(entry) + len >= link_len) {
+ link_len = len + NAMELEN(entry) + 1;
+ link = (char *)brealloc(link, link_len + 1);
+ }
+ q = link + len;
+ for (i=0; i < (int)NAMELEN(entry); i++) {
+ *q++ = *p++;
+ }
+ *q = 0;
+ if (!file_is_excluded(ff_pkt, link)) {
+ rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, 0);
+ if (ff_pkt->linked) {
+ ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
+ }
+ }
+ }
+ closedir(directory);
+ free(link);
+ free(entry);
+
+ /*
+ * Now that we have recursed through all the files in the
+ * directory, we "save" the directory so that after all
+ * the files are restored, this entry will serve to reset
+ * the directory modes and dates. Temp directory values
+ * were used without this record.
+ */
+ handle_file(dir_ff_pkt, pkt); /* handle directory entry */
+ if (ff_pkt->linked) {
+ ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
+ }
+ free_dir_ff_pkt(dir_ff_pkt);
+
+ if (ff_pkt->flags & FO_KEEPATIME) {
+ utime(fname, &restore_times);
+ }
+ return rtn_stat;