]> wimlib.net Git - wimlib/blob - src/unix_capture.c
9a28d55a9a2fc6f552e3cafd91f626a3e9db7bd8
[wimlib] / src / unix_capture.c
1 /*
2  * unix_capture.c:  Capture a directory tree on UNIX.
3  */
4
5 /*
6  * Copyright (C) 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifndef __WIN32__
25
26 #include "wimlib_internal.h"
27 #include "dentry.h"
28 #include "lookup_table.h"
29 #include "timestamp.h"
30
31 #include <dirent.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35
36 static int
37 unix_capture_regular_file(const char *path,
38                           u64 size,
39                           struct wim_inode *inode,
40                           struct wim_lookup_table *lookup_table)
41 {
42         inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
43
44         /* Empty files do not have to have a lookup table entry. */
45         if (size != 0) {
46                 struct wim_lookup_table_entry *lte;
47                 char *file_on_disk;
48
49                 file_on_disk = STRDUP(path);
50                 if (!file_on_disk)
51                         return WIMLIB_ERR_NOMEM;
52                 lte = new_lookup_table_entry();
53                 if (!lte) {
54                         FREE(file_on_disk);
55                         return WIMLIB_ERR_NOMEM;
56                 }
57                 lte->file_on_disk = file_on_disk;
58                 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
59                 lte->resource_entry.original_size = size;
60                 lookup_table_insert_unhashed(lookup_table, lte, inode, 0);
61                 inode->i_lte = lte;
62         }
63         return 0;
64 }
65
66 static int
67 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
68                                  char *path,
69                                  size_t path_len,
70                                  struct add_image_params *params);
71
72 static int
73 unix_capture_directory(struct wim_dentry *dir_dentry,
74                        char *path,
75                        size_t path_len,
76                        struct add_image_params *params)
77 {
78
79         DIR *dir;
80         struct dirent *entry;
81         struct wim_dentry *child;
82         int ret;
83
84         dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
85         dir = opendir(path);
86         if (!dir) {
87                 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
88                                  path);
89                 return WIMLIB_ERR_OPENDIR;
90         }
91
92         /* Recurse on directory contents */
93         ret = 0;
94         for (;;) {
95                 errno = 0;
96                 entry = readdir(dir);
97                 if (!entry) {
98                         if (errno) {
99                                 ret = WIMLIB_ERR_READ;
100                                 ERROR_WITH_ERRNO("Error reading the "
101                                                  "directory `%s'", path);
102                         }
103                         break;
104                 }
105
106                 if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0'
107                       || (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
108                                 continue;
109
110                 size_t name_len = strlen(entry->d_name);
111
112                 path[path_len] = '/';
113                 memcpy(&path[path_len + 1], entry->d_name, name_len + 1);
114                 ret = unix_build_dentry_tree_recursive(&child,
115                                                        path,
116                                                        path_len + 1 + name_len,
117                                                        params);
118                 if (ret)
119                         break;
120                 if (child)
121                         dentry_add_child(dir_dentry, child);
122         }
123         closedir(dir);
124         return ret;
125 }
126
127 static int
128 unix_capture_symlink(struct wim_dentry **root_p,
129                      const char *path,
130                      struct wim_inode *inode,
131                      struct add_image_params *params)
132 {
133         char deref_name_buf[4096];
134         ssize_t deref_name_len;
135         int ret;
136
137         inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
138         inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
139
140         /* The idea here is to call readlink() to get the UNIX target of
141          * the symbolic link, then turn the target into a reparse point
142          * data buffer that contains a relative or absolute symbolic
143          * link (NOT a junction point or *full* path symbolic link with
144          * drive letter).
145          */
146         deref_name_len = readlink(path, deref_name_buf,
147                                   sizeof(deref_name_buf) - 1);
148         if (deref_name_len >= 0) {
149                 char *dest = deref_name_buf;
150
151                 dest[deref_name_len] = '\0';
152                 DEBUG("Read symlink `%s'", dest);
153
154                 if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
155                      dest[0] == '/')
156                 {
157                         dest = capture_fixup_absolute_symlink(dest,
158                                                               params->capture_root_ino,
159                                                               params->capture_root_dev);
160                         if (!dest) {
161                                 WARNING("Ignoring out of tree absolute symlink "
162                                         "\"%s\" -> \"%s\"\n"
163                                         "          (Use --norpfix to capture "
164                                         "absolute symlinks as-is)",
165                                         path, deref_name_buf);
166                                 free_dentry(*root_p);
167                                 *root_p = NULL;
168                                 return 0;
169                         }
170                         inode->i_not_rpfixed = 0;
171                 }
172                 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
173                 if (ret == 0) {
174                         /* Unfortunately, Windows seems to have the concept of
175                          * "file" symbolic links as being different from
176                          * "directory" symbolic links...  so
177                          * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
178                          * symbolic link if the *target* of the symbolic link is
179                          * a directory.  */
180                         struct stat stbuf;
181                         if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
182                                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
183                 }
184         } else {
185                 ERROR_WITH_ERRNO("Failed to read target of "
186                                  "symbolic link `%s'", path);
187                 ret = WIMLIB_ERR_READLINK;
188         }
189         return ret;
190 }
191
192 static int
193 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
194                                  char *path,
195                                  size_t path_len,
196                                  struct add_image_params *params)
197 {
198         struct wim_dentry *root = NULL;
199         int ret = 0;
200         struct wim_inode *inode;
201
202         if (exclude_path(path, path_len, params->config, true)) {
203                 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
204                     && params->progress_func)
205                 {
206                         union wimlib_progress_info info;
207                         info.scan.cur_path = path;
208                         info.scan.excluded = true;
209                         params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
210                 }
211                 goto out;
212         }
213
214         if ((params->add_flags & WIMLIB_ADD_FLAG_VERBOSE)
215             && params->progress_func)
216         {
217                 union wimlib_progress_info info;
218                 info.scan.cur_path = path;
219                 info.scan.excluded = false;
220                 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
221         }
222
223         struct stat stbuf;
224         int (*stat_fn)(const char *restrict, struct stat *restrict);
225         if ((params->add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) ||
226             (params->add_flags & WIMLIB_ADD_FLAG_ROOT))
227                 stat_fn = stat;
228         else
229                 stat_fn = lstat;
230
231         ret = (*stat_fn)(path, &stbuf);
232         if (ret) {
233                 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
234                 ret = WIMLIB_ERR_STAT;
235                 goto out;
236         }
237         if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
238             && !S_ISLNK(stbuf.st_mode)) {
239                 ERROR("`%s' is not a regular file, directory, or symbolic link.",
240                       path);
241                 ret = WIMLIB_ERR_SPECIAL_FILE;
242                 goto out;
243         }
244
245         ret = inode_table_new_dentry(params->inode_table,
246                                      path_basename_with_len(path, path_len),
247                                      stbuf.st_ino, stbuf.st_dev, false, &root);
248         if (ret)
249                 goto out;
250
251         inode = root->d_inode;
252
253         if (inode->i_nlink > 1) /* Already captured this inode? */
254                 goto out;
255
256 #ifdef HAVE_STAT_NANOSECOND_PRECISION
257         inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
258         inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
259         inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
260 #else
261         inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
262         inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
263         inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
264 #endif
265         inode->i_resolved = 1;
266         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
267                 ret = inode_set_unix_data(inode, stbuf.st_uid,
268                                           stbuf.st_gid,
269                                           stbuf.st_mode,
270                                           params->lookup_table,
271                                           UNIX_DATA_ALL | UNIX_DATA_CREATE);
272                 if (ret)
273                         goto out;
274         }
275         params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
276         if (S_ISREG(stbuf.st_mode))
277                 ret = unix_capture_regular_file(path, stbuf.st_size,
278                                                 inode, params->lookup_table);
279         else if (S_ISDIR(stbuf.st_mode))
280                 ret = unix_capture_directory(root, path, path_len, params);
281         else
282                 ret = unix_capture_symlink(&root, path, inode, params);
283 out:
284         if (ret == 0)
285                 *root_ret = root;
286         else
287                 free_dentry_tree(root, params->lookup_table);
288         return ret;
289 }
290
291 /*
292  * unix_build_dentry_tree():
293  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
294  *      version; no NTFS-specific data is captured).
295  *
296  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
297  *              modified if successful.  Set to NULL if the file or directory was
298  *              excluded from capture.
299  *
300  * @root_disk_path:  The path to the root of the directory tree on disk.
301  *
302  * @params:     See doc for `struct add_image_params'.
303  *
304  * @return:     0 on success, nonzero on failure.  It is a failure if any of
305  *              the files cannot be `stat'ed, or if any of the needed
306  *              directories cannot be opened or read.  Failure to add the files
307  *              to the WIM may still occur later when trying to actually read
308  *              the on-disk files during a call to wimlib_write() or
309  *              wimlib_overwrite().
310  */
311 int
312 unix_build_dentry_tree(struct wim_dentry **root_ret,
313                        const char *root_disk_path,
314                        struct add_image_params *params)
315 {
316         char *path_buf;
317         int ret;
318         size_t path_len;
319         size_t path_bufsz;
320
321         {
322                 struct stat root_stbuf;
323                 if (stat(root_disk_path, &root_stbuf)) {
324                         ERROR_WITH_ERRNO("Failed to stat \"%s\"", root_disk_path);
325                         return WIMLIB_ERR_STAT;
326                 }
327
328                 if ((params->add_flags & WIMLIB_ADD_FLAG_ROOT) &&
329                     !S_ISDIR(root_stbuf.st_mode))
330                 {
331                         ERROR("Root of capture \"%s\" is not a directory",
332                               root_disk_path);
333                         return WIMLIB_ERR_NOTDIR;
334                 }
335                 params->capture_root_ino = root_stbuf.st_ino;
336                 params->capture_root_dev = root_stbuf.st_dev;
337         }
338
339         path_bufsz = min(32790, PATH_MAX + 1);
340         path_len = strlen(root_disk_path);
341
342         if (path_len >= path_bufsz)
343                 return WIMLIB_ERR_INVALID_PARAM;
344
345         path_buf = MALLOC(path_bufsz);
346         if (!path_buf)
347                 return WIMLIB_ERR_NOMEM;
348         memcpy(path_buf, root_disk_path, path_len + 1);
349
350         ret = unix_build_dentry_tree_recursive(root_ret, path_buf,
351                                                path_len, params);
352         FREE(path_buf);
353         return ret;
354 }
355
356 #endif /* !__WIN32__ */