]> wimlib.net Git - wimlib/blob - src/unix_capture.c
ntfs-3g_apply.c: note bugs fixed in NTFS-3G version 2017.3.23
[wimlib] / src / unix_capture.c
1 /*
2  * unix_capture.c:  Capture a directory tree on UNIX.
3  */
4
5 /*
6  * Copyright (C) 2012-2016 Eric Biggers
7  *
8  * This file is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifndef __WIN32__
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h> /* for PATH_MAX */
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_XATTR_H
35 #  include <sys/xattr.h>
36 #endif
37 #include <unistd.h>
38
39 #include "wimlib/blob_table.h"
40 #include "wimlib/dentry.h"
41 #include "wimlib/error.h"
42 #include "wimlib/reparse.h"
43 #include "wimlib/scan.h"
44 #include "wimlib/timestamp.h"
45 #include "wimlib/unix_data.h"
46 #include "wimlib/xattr.h"
47
48 #ifdef HAVE_FDOPENDIR
49 #  define my_fdopendir(dirfd_p) fdopendir(*(dirfd_p))
50 #else
51 static DIR *
52 my_fdopendir(int *dirfd_p)
53 {
54         DIR *dir = NULL;
55         int old_pwd;
56
57         old_pwd = open(".", O_RDONLY);
58         if (old_pwd >= 0) {
59                 if (!fchdir(*dirfd_p)) {
60                         dir = opendir(".");
61                         if (dir) {
62                                 close(*dirfd_p);
63                                 *dirfd_p = dirfd(dir);
64                         }
65                         fchdir(old_pwd);
66                 }
67                 close(old_pwd);
68         }
69         return dir;
70 }
71 #endif
72
73 #ifdef HAVE_OPENAT
74 #  define my_openat(full_path, dirfd, relpath, flags) \
75                 openat((dirfd), (relpath), (flags))
76 #else
77 #  define my_openat(full_path, dirfd, relpath, flags) \
78                 open((full_path), (flags))
79 #endif
80
81 #ifdef HAVE_READLINKAT
82 #  define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
83                 readlinkat((dirfd), (relpath), (buf), (bufsize))
84 #else
85 #  define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
86                 readlink((full_path), (buf), (bufsize))
87 #endif
88
89 #ifdef HAVE_FSTATAT
90 #  define my_fstatat(full_path, dirfd, relpath, stbuf, flags)   \
91         fstatat((dirfd), (relpath), (stbuf), (flags))
92 #else
93 #  define my_fstatat(full_path, dirfd, relpath, stbuf, flags)   \
94         ((flags) & AT_SYMLINK_NOFOLLOW) ? \
95                 lstat((full_path), (stbuf)) : \
96                 stat((full_path), (stbuf))
97 #endif
98
99 #ifndef AT_FDCWD
100 #  define AT_FDCWD      -100
101 #endif
102
103 #ifndef AT_SYMLINK_NOFOLLOW
104 #  define AT_SYMLINK_NOFOLLOW   0x100
105 #endif
106
107 #ifdef HAVE_XATTR_SUPPORT
108 /*
109  * Retrieves the values of the xattrs named by the null-terminated @names of the
110  * file at @path and serializes the xattr names and values into @entries.  If
111  * successful, returns the number of bytes used in @entries.  If unsuccessful,
112  * returns -1 and sets errno (ERANGE if @entries was too small).
113  */
114 static ssize_t
115 gather_xattr_entries(const char *path, const char *names, size_t names_size,
116                      void *entries, size_t entries_size)
117 {
118         const char * const names_end = names + names_size;
119         void * const entries_end = entries + entries_size;
120         const char *name = names;
121         struct wimlib_xattr_entry *entry = entries;
122
123         wimlib_assert((uintptr_t)entries % 4 == 0 &&
124                       entries_size % 4 == 0 && names_size != 0);
125         do {
126                 size_t name_len = strnlen(name, names_end - name);
127                 void *value;
128                 ssize_t value_len;
129
130                 if (name_len == 0 || name_len >= names_end - name ||
131                     (u16)name_len != name_len) {
132                         ERROR("\"%s\": malformed extended attribute names list",
133                               path);
134                         errno = EINVAL;
135                         return -1;
136                 }
137
138                 /*
139                  * Note: we take care to always call lgetxattr() with a nonzero
140                  * size, since zero size means to return the value length only.
141                  */
142                 if (entries_end - (void *)entry <= sizeof(*entry) + name_len) {
143                         errno = ERANGE;
144                         return -1;
145                 }
146
147                 entry->name_len = cpu_to_le16(name_len);
148                 entry->reserved = 0;
149                 value = mempcpy(entry->name, name, name_len);
150
151                 value_len = lgetxattr(path, name, value, entries_end - value);
152                 if (value_len < 0) {
153                         if (errno != ERANGE) {
154                                 ERROR_WITH_ERRNO("\"%s\": unable to read extended attribute \"%s\"",
155                                                  path, name);
156                         }
157                         return -1;
158                 }
159                 if ((u32)value_len != value_len) {
160                         ERROR("\"%s\": value of extended attribute \"%s\" is too large",
161                               path, name);
162                         errno = EINVAL;
163                         return -1;
164                 }
165                 entry->value_len = cpu_to_le32(value_len);
166
167                 /*
168                  * Zero-pad the entry to the next 4-byte boundary.
169                  * Note: because we've guaranteed that @entries_size is a
170                  * multiple of 4, this cannot overflow the @entries buffer.
171                  */
172                 value += value_len;
173                 while ((uintptr_t)value & 3) {
174                         *(u8 *)value = 0;
175                         value++;
176                 }
177
178                 entry = value;
179                 name += name_len + 1;
180         } while (name < names_end);
181
182         return (void *)entry - entries;
183 }
184
185 static int
186 create_xattr_item(const char *path, struct wim_inode *inode,
187                   const char *names, size_t names_size)
188 {
189         char _entries[1024] _aligned_attribute(4);
190         char *entries = _entries;
191         size_t entries_avail = ARRAY_LEN(_entries);
192         ssize_t entries_size;
193         int ret;
194
195 retry:
196         /* Serialize the xattrs into @entries */
197         entries_size = gather_xattr_entries(path, names, names_size,
198                                             entries, entries_avail);
199         if (entries_size < 0) {
200                 ret = WIMLIB_ERR_STAT;
201                 if (errno != ERANGE)
202                         goto out;
203                 /* Not enough space in @entries.  Reallocate it. */
204                 if (entries != _entries)
205                         FREE(entries);
206                 ret = WIMLIB_ERR_NOMEM;
207                 entries_avail *= 2;
208                 entries = MALLOC(entries_avail);
209                 if (!entries)
210                         goto out;
211                 goto retry;
212         }
213
214         /* Copy @entries into an xattr item associated with @inode */
215         if ((u32)entries_size != entries_size) {
216                 ERROR("\"%s\": too much xattr data!", path);
217                 ret = WIMLIB_ERR_STAT;
218                 goto out;
219         }
220         ret = WIMLIB_ERR_NOMEM;
221         if (!inode_set_linux_xattrs(inode, entries, entries_size))
222                 goto out;
223
224         ret = 0;
225 out:
226         if (entries != _entries)
227                 FREE(entries);
228         return ret;
229 }
230
231 /*
232  * If the file at @path has Linux-style extended attributes, read them into
233  * memory and add them to @inode as a tagged item.
234  */
235 static noinline_for_stack int
236 scan_linux_xattrs(const char *path, struct wim_inode *inode)
237 {
238         char _names[256];
239         char *names = _names;
240         ssize_t names_size = ARRAY_LEN(_names);
241         int ret = 0;
242
243 retry:
244         /* Gather the names of the xattrs of the file at @path */
245         names_size = llistxattr(path, names, names_size);
246         if (names_size == 0) /* No xattrs? */
247                 goto out;
248         if (names_size < 0) {
249                 /* xattrs unsupported or disabled? */
250                 if (errno == ENOTSUP || errno == ENOSYS)
251                         goto out;
252                 if (errno == ERANGE) {
253                         /*
254                          * Not enough space in @names.  Ask for how much space
255                          * we need, then try again.
256                          */
257                         names_size = llistxattr(path, NULL, 0);
258                         if (names_size == 0)
259                                 goto out;
260                         if (names_size > 0) {
261                                 if (names != _names)
262                                         FREE(names);
263                                 names = MALLOC(names_size);
264                                 if (!names) {
265                                         ret = WIMLIB_ERR_NOMEM;
266                                         goto out;
267                                 }
268                                 goto retry;
269                         }
270                 }
271                 /* Some other error occurred. */
272                 ERROR_WITH_ERRNO("\"%s\": unable to list extended attributes",
273                                  path);
274                 ret = WIMLIB_ERR_STAT;
275                 goto out;
276         }
277
278         /*
279          * We have a nonempty list of xattr names.  Gather the xattr values and
280          * add them as a tagged item.
281          */
282         ret = create_xattr_item(path, inode, names, names_size);
283 out:
284         if (names != _names)
285                 FREE(names);
286         return ret;
287 }
288 #endif /* HAVE_XATTR_SUPPORT */
289
290 static int
291 unix_scan_regular_file(const char *path, u64 blocks, u64 size,
292                        struct wim_inode *inode,
293                        struct list_head *unhashed_blobs)
294 {
295         struct blob_descriptor *blob = NULL;
296         struct wim_inode_stream *strm;
297
298         /*
299          * Set FILE_ATTRIBUTE_SPARSE_FILE if the file uses less disk space than
300          * expected given its size.
301          */
302         if (blocks < DIV_ROUND_UP(size, 512))
303                 inode->i_attributes = FILE_ATTRIBUTE_SPARSE_FILE;
304         else
305                 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
306
307         if (size) {
308                 blob = new_blob_descriptor();
309                 if (unlikely(!blob))
310                         goto err_nomem;
311                 blob->file_on_disk = STRDUP(path);
312                 if (unlikely(!blob->file_on_disk))
313                         goto err_nomem;
314                 blob->blob_location = BLOB_IN_FILE_ON_DISK;
315                 blob->size = size;
316                 blob->file_inode = inode;
317         }
318
319         strm = inode_add_stream(inode, STREAM_TYPE_DATA, NO_STREAM_NAME, blob);
320         if (unlikely(!strm))
321                 goto err_nomem;
322
323         prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
324         return 0;
325
326 err_nomem:
327         free_blob_descriptor(blob);
328         return WIMLIB_ERR_NOMEM;
329 }
330
331 static int
332 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
333                                  char *path, size_t path_len,
334                                  int dirfd, const char *relpath,
335                                  struct scan_params *params);
336
337 static int
338 unix_scan_directory(struct wim_dentry *dir_dentry,
339                     char *full_path, size_t full_path_len,
340                     int parent_dirfd, const char *dir_relpath,
341                     struct scan_params *params)
342 {
343
344         int dirfd;
345         DIR *dir;
346         int ret;
347
348         dirfd = my_openat(full_path, parent_dirfd, dir_relpath, O_RDONLY);
349         if (dirfd < 0) {
350                 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
351                 return WIMLIB_ERR_OPENDIR;
352         }
353
354         dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
355         dir = my_fdopendir(&dirfd);
356         if (!dir) {
357                 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
358                 close(dirfd);
359                 return WIMLIB_ERR_OPENDIR;
360         }
361
362         ret = 0;
363         for (;;) {
364                 struct dirent *entry;
365                 struct wim_dentry *child;
366                 size_t name_len;
367
368                 errno = 0;
369                 entry = readdir(dir);
370                 if (!entry) {
371                         if (errno) {
372                                 ret = WIMLIB_ERR_READ;
373                                 ERROR_WITH_ERRNO("\"%s\": Error reading directory",
374                                                  full_path);
375                         }
376                         break;
377                 }
378
379                 name_len = strlen(entry->d_name);
380
381                 if (should_ignore_filename(entry->d_name, name_len))
382                         continue;
383
384                 full_path[full_path_len] = '/';
385                 memcpy(&full_path[full_path_len + 1], entry->d_name, name_len + 1);
386                 ret = unix_build_dentry_tree_recursive(&child,
387                                                        full_path,
388                                                        full_path_len + 1 + name_len,
389                                                        dirfd,
390                                                        &full_path[full_path_len + 1],
391                                                        params);
392                 full_path[full_path_len] = '\0';
393                 if (ret)
394                         break;
395                 attach_scanned_tree(dir_dentry, child, params->blob_table);
396         }
397         closedir(dir);
398         return ret;
399 }
400
401 /*
402  * Given an absolute symbolic link target (UNIX-style, beginning with '/'),
403  * determine whether it points into the directory identified by @ino and @dev.
404  * If yes, return the suffix of @target which is relative to this directory, but
405  * retaining leading slashes.  If no, return @target.
406  *
407  * Here are some examples, assuming that the @ino/@dev directory is "/home/e":
408  *
409  *      Original target         New target
410  *      ---------------         ----------
411  *      /home/e/test            /test
412  *      /home/e/test/           /test/
413  *      //home//e//test//       //test//
414  *      /home/e                                         (empty string)
415  *      /home/e/                /
416  *      /usr/lib                /usr/lib                (external link)
417  *
418  * Because of the possibility of other links into the @ino/@dev directory and/or
419  * multiple path separators, we can't simply do a string comparison; instead we
420  * need to stat() each ancestor directory.
421  *
422  * If the link points directly to the @ino/@dev directory with no trailing
423  * slashes, then the new target will be an empty string.  This is not a valid
424  * UNIX symlink target, but we store this in the archive anyway since the target
425  * is intended to be de-relativized when the link is extracted.
426  */
427 static char *
428 unix_relativize_link_target(char *target, u64 ino, u64 dev)
429 {
430         char *p = target;
431
432         do {
433                 char save;
434                 struct stat stbuf;
435                 int ret;
436
437                 /* Skip slashes (guaranteed to be at least one here)  */
438                 do {
439                         p++;
440                 } while (*p == '/');
441
442                 /* End of string?  */
443                 if (!*p)
444                         break;
445
446                 /* Skip non-slashes (guaranteed to be at least one here)  */
447                 do {
448                         p++;
449                 } while (*p && *p != '/');
450
451                 /* Get the inode and device numbers for this prefix.  */
452                 save = *p;
453                 *p = '\0';
454                 ret = stat(target, &stbuf);
455                 *p = save;
456
457                 if (ret) {
458                         /* stat() failed.  Assume the link points outside the
459                          * directory tree being captured.  */
460                         break;
461                 }
462
463                 if (stbuf.st_ino == ino && stbuf.st_dev == dev) {
464                         /* Link points inside directory tree being captured.
465                          * Return abbreviated path.  */
466                         return p;
467                 }
468         } while (*p);
469
470         /* Link does not point inside directory tree being captured.  */
471         return target;
472 }
473
474 static noinline_for_stack int
475 unix_scan_symlink(const char *full_path, int dirfd, const char *relpath,
476                   struct wim_inode *inode, struct scan_params *params)
477 {
478         char orig_target[REPARSE_POINT_MAX_SIZE];
479         char *target = orig_target;
480         int ret;
481
482         /* Read the UNIX symbolic link target.  */
483         ret = my_readlinkat(full_path, dirfd, relpath, target,
484                             sizeof(orig_target));
485         if (unlikely(ret < 0)) {
486                 ERROR_WITH_ERRNO("\"%s\": Can't read target of symbolic link",
487                                  full_path);
488                 return WIMLIB_ERR_READLINK;
489         }
490         if (unlikely(ret >= sizeof(orig_target))) {
491                 ERROR("\"%s\": target of symbolic link is too long", full_path);
492                 return WIMLIB_ERR_READLINK;
493         }
494         target[ret] = '\0';
495
496         /* If the link is absolute and reparse point fixups are enabled, then
497          * change it to be "absolute" relative to the tree being captured.  */
498         if (target[0] == '/' && (params->add_flags & WIMLIB_ADD_FLAG_RPFIX)) {
499                 int status = WIMLIB_SCAN_DENTRY_NOT_FIXED_SYMLINK;
500
501                 params->progress.scan.cur_path = full_path;
502                 params->progress.scan.symlink_target = target;
503
504                 target = unix_relativize_link_target(target,
505                                                      params->capture_root_ino,
506                                                      params->capture_root_dev);
507                 if (target != orig_target) {
508                         /* Link target was fixed.  */
509                         inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
510                         status = WIMLIB_SCAN_DENTRY_FIXED_SYMLINK;
511                 }
512                 ret = do_scan_progress(params, status, NULL);
513                 if (ret)
514                         return ret;
515         }
516
517         /* Translate the UNIX symlink target into a Windows reparse point.  */
518         ret = wim_inode_set_symlink(inode, target, params->blob_table);
519         if (unlikely(ret)) {
520                 if (ret == WIMLIB_ERR_INVALID_UTF8_STRING) {
521                         ERROR("\"%s\": target of symbolic link is not valid "
522                               "UTF-8.  This is not supported.", full_path);
523                 }
524                 return ret;
525         }
526
527         /* On Windows, a reparse point can be set on both directory and
528          * non-directory files.  Usually, a link that is intended to point to a
529          * (non-)directory is stored as a reparse point on a (non-)directory
530          * file.  Replicate this behavior by examining the target file.  */
531         struct stat stbuf;
532         if (my_fstatat(full_path, dirfd, relpath, &stbuf, 0) == 0 &&
533             S_ISDIR(stbuf.st_mode))
534                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
535         return 0;
536 }
537
538 static int
539 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
540                                  char *full_path, size_t full_path_len,
541                                  int dirfd, const char *relpath,
542                                  struct scan_params *params)
543 {
544         struct wim_dentry *tree = NULL;
545         struct wim_inode *inode = NULL;
546         int ret;
547         struct stat stbuf;
548         int stat_flags;
549
550         ret = try_exclude(full_path, params);
551         if (unlikely(ret < 0)) /* Excluded? */
552                 goto out_progress;
553         if (unlikely(ret > 0)) /* Error? */
554                 goto out;
555
556         if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
557                                  WIMLIB_ADD_FLAG_ROOT))
558                 stat_flags = 0;
559         else
560                 stat_flags = AT_SYMLINK_NOFOLLOW;
561
562         ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
563
564         if (ret) {
565                 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
566                 ret = WIMLIB_ERR_STAT;
567                 goto out;
568         }
569
570         if (!(params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA)) {
571                 if (unlikely(!S_ISREG(stbuf.st_mode) &&
572                              !S_ISDIR(stbuf.st_mode) &&
573                              !S_ISLNK(stbuf.st_mode)))
574                 {
575                         if (params->add_flags &
576                             WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
577                         {
578                                 ERROR("\"%s\": File type is unsupported",
579                                       full_path);
580                                 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
581                                 goto out;
582                         }
583                         params->progress.scan.cur_path = full_path;
584                         ret = do_scan_progress(params,
585                                                WIMLIB_SCAN_DENTRY_UNSUPPORTED,
586                                                NULL);
587                         goto out;
588                 }
589         }
590
591         ret = inode_table_new_dentry(params->inode_table, relpath,
592                                      stbuf.st_ino, stbuf.st_dev, false, &tree);
593         if (unlikely(ret)) {
594                 if (ret == WIMLIB_ERR_INVALID_UTF8_STRING) {
595                         ERROR("\"%s\": filename is not valid UTF-8.  "
596                               "This is not supported.", full_path);
597                 }
598                 goto out;
599         }
600
601         inode = tree->d_inode;
602
603         /* Already seen this inode?  */
604         if (inode->i_nlink > 1)
605                 goto out_progress;
606
607 #ifdef HAVE_STAT_NANOSECOND_PRECISION
608         inode->i_creation_time = timespec_to_wim_timestamp(&stbuf.st_mtim);
609         inode->i_last_write_time = timespec_to_wim_timestamp(&stbuf.st_mtim);
610         inode->i_last_access_time = timespec_to_wim_timestamp(&stbuf.st_atim);
611 #else
612         inode->i_creation_time = time_t_to_wim_timestamp(stbuf.st_mtime);
613         inode->i_last_write_time = time_t_to_wim_timestamp(stbuf.st_mtime);
614         inode->i_last_access_time = time_t_to_wim_timestamp(stbuf.st_atime);
615 #endif
616         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
617                 struct wimlib_unix_data unix_data;
618
619                 unix_data.uid = stbuf.st_uid;
620                 unix_data.gid = stbuf.st_gid;
621                 unix_data.mode = stbuf.st_mode;
622                 unix_data.rdev = stbuf.st_rdev;
623                 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_ALL)) {
624                         ret = WIMLIB_ERR_NOMEM;
625                         goto out;
626                 }
627 #ifdef HAVE_XATTR_SUPPORT
628                 ret = scan_linux_xattrs(full_path, inode);
629                 if (ret)
630                         goto out;
631 #endif
632         }
633
634         if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
635                 params->capture_root_ino = stbuf.st_ino;
636                 params->capture_root_dev = stbuf.st_dev;
637                 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
638         }
639
640         if (S_ISREG(stbuf.st_mode)) {
641                 ret = unix_scan_regular_file(full_path, stbuf.st_blocks,
642                                              stbuf.st_size, inode,
643                                              params->unhashed_blobs);
644         } else if (S_ISDIR(stbuf.st_mode)) {
645                 ret = unix_scan_directory(tree, full_path, full_path_len,
646                                           dirfd, relpath, params);
647         } else if (S_ISLNK(stbuf.st_mode)) {
648                 ret = unix_scan_symlink(full_path, dirfd, relpath,
649                                         inode, params);
650         }
651
652         if (ret)
653                 goto out;
654
655 out_progress:
656         params->progress.scan.cur_path = full_path;
657         if (likely(tree))
658                 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
659         else
660                 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
661 out:
662         if (unlikely(ret)) {
663                 free_dentry_tree(tree, params->blob_table);
664                 tree = NULL;
665                 ret = report_scan_error(params, ret, full_path);
666         }
667         *tree_ret = tree;
668         return ret;
669 }
670
671 /*
672  * unix_build_dentry_tree():
673  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
674  *      version; no NTFS-specific data is captured).
675  *
676  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Set
677  *              to NULL if the file or directory was excluded from capture.
678  *
679  * @root_disk_path:  The path to the root of the directory tree on disk.
680  *
681  * @params:     See doc for `struct scan_params'.
682  *
683  * @return:     0 on success, nonzero on failure.  It is a failure if any of
684  *              the files cannot be `stat'ed, or if any of the needed
685  *              directories cannot be opened or read.  Failure to add the files
686  *              to the WIM may still occur later when trying to actually read
687  *              the on-disk files during a call to wimlib_write() or
688  *              wimlib_overwrite().
689  */
690 int
691 unix_build_dentry_tree(struct wim_dentry **root_ret,
692                        const char *root_disk_path, struct scan_params *params)
693 {
694         size_t path_len;
695         size_t path_bufsz;
696         char *path_buf;
697         int ret;
698
699         path_len = strlen(root_disk_path);
700         path_bufsz = min(32790, PATH_MAX + 1);
701
702         if (path_len >= path_bufsz)
703                 return WIMLIB_ERR_INVALID_PARAM;
704
705         path_buf = MALLOC(path_bufsz);
706         if (!path_buf)
707                 return WIMLIB_ERR_NOMEM;
708         memcpy(path_buf, root_disk_path, path_len + 1);
709
710         params->capture_root_nchars = path_len;
711
712         ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
713                                                AT_FDCWD, path_buf, params);
714         FREE(path_buf);
715         return ret;
716 }
717
718 #endif /* !__WIN32__ */