dd79b658e0238cc9f15ed3d039ef1285e2dc9015
[wimlib] / src / unix_capture.c
1 /*
2  * unix_capture.c:  Capture a directory tree on UNIX.
3  */
4
5 /*
6  * Copyright (C) 2012, 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 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <dirent.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35
36 #include "wimlib/capture.h"
37 #include "wimlib/dentry.h"
38 #include "wimlib/error.h"
39 #include "wimlib/lookup_table.h"
40 #include "wimlib/paths.h"
41 #include "wimlib/reparse.h"
42 #include "wimlib/timestamp.h"
43
44 static int
45 unix_capture_regular_file(const char *path,
46                           u64 size,
47                           struct wim_inode *inode,
48                           struct list_head *unhashed_streams)
49 {
50         inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
51
52         /* Empty files do not have to have a lookup table entry. */
53         if (size != 0) {
54                 struct wim_lookup_table_entry *lte;
55                 char *file_on_disk;
56
57                 file_on_disk = STRDUP(path);
58                 if (!file_on_disk)
59                         return WIMLIB_ERR_NOMEM;
60                 lte = new_lookup_table_entry();
61                 if (!lte) {
62                         FREE(file_on_disk);
63                         return WIMLIB_ERR_NOMEM;
64                 }
65                 lte->file_on_disk = file_on_disk;
66                 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
67                 lte->size = size;
68                 add_unhashed_stream(lte, inode, 0, unhashed_streams);
69                 inode->i_lte = lte;
70         }
71         return 0;
72 }
73
74 static int
75 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
76                                  char *path,
77                                  size_t path_len,
78                                  struct add_image_params *params);
79
80 static int
81 unix_capture_directory(struct wim_dentry *dir_dentry,
82                        char *path,
83                        size_t path_len,
84                        struct add_image_params *params)
85 {
86
87         DIR *dir;
88         struct dirent *entry;
89         struct wim_dentry *child;
90         int ret;
91
92         dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
93         dir = opendir(path);
94         if (!dir) {
95                 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
96                                  path);
97                 return WIMLIB_ERR_OPENDIR;
98         }
99
100         /* Recurse on directory contents */
101         ret = 0;
102         for (;;) {
103                 errno = 0;
104                 entry = readdir(dir);
105                 if (!entry) {
106                         if (errno) {
107                                 ret = WIMLIB_ERR_READ;
108                                 ERROR_WITH_ERRNO("Error reading the "
109                                                  "directory `%s'", path);
110                         }
111                         break;
112                 }
113
114                 if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0'
115                       || (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
116                                 continue;
117
118                 size_t name_len = strlen(entry->d_name);
119
120                 path[path_len] = '/';
121                 memcpy(&path[path_len + 1], entry->d_name, name_len + 1);
122                 ret = unix_build_dentry_tree_recursive(&child,
123                                                        path,
124                                                        path_len + 1 + name_len,
125                                                        params);
126                 if (ret)
127                         break;
128                 if (child)
129                         dentry_add_child(dir_dentry, child);
130         }
131         path[path_len] = '\0';
132         closedir(dir);
133         return ret;
134 }
135
136 static int
137 unix_capture_symlink(struct wim_dentry **root_p,
138                      const char *path,
139                      struct wim_inode *inode,
140                      struct add_image_params *params)
141 {
142         char deref_name_buf[4096];
143         ssize_t deref_name_len;
144         int ret;
145
146         inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
147         inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
148
149         /* The idea here is to call readlink() to get the UNIX target of the
150          * symbolic link, then turn the target into a reparse point data buffer
151          * that contains a relative or absolute symbolic link. */
152         deref_name_len = readlink(path, deref_name_buf,
153                                   sizeof(deref_name_buf) - 1);
154         if (deref_name_len >= 0) {
155                 char *dest = deref_name_buf;
156
157                 dest[deref_name_len] = '\0';
158                 DEBUG("Read symlink `%s'", dest);
159
160                 if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
161                      dest[0] == '/')
162                 {
163                         dest = capture_fixup_absolute_symlink(dest,
164                                                               params->capture_root_ino,
165                                                               params->capture_root_dev);
166                         if (dest == NULL) {
167                                 /* RPFIX (reparse point fixup) mode:  Ignore
168                                  * absolute symbolic link that points out of the
169                                  * tree to be captured.  */
170                                 free_dentry(*root_p);
171                                 *root_p = NULL;
172                                 params->progress.scan.cur_path = path;
173                                 params->progress.scan.symlink_target = deref_name_buf;
174                                 do_capture_progress(params,
175                                                     WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
176                                                     NULL);
177                                 return 0;
178                         }
179                         inode->i_not_rpfixed = 0;
180                 }
181                 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
182                 if (ret == 0) {
183                         /* Unfortunately, Windows seems to have the concept of
184                          * "file" symbolic links as being different from
185                          * "directory" symbolic links...  so
186                          * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
187                          * symbolic link if the *target* of the symbolic link is
188                          * a directory.  */
189                         struct stat stbuf;
190                         if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
191                                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
192                 }
193         } else {
194                 ERROR_WITH_ERRNO("Failed to read target of "
195                                  "symbolic link `%s'", path);
196                 ret = WIMLIB_ERR_READLINK;
197         }
198         return ret;
199 }
200
201 static int
202 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
203                                  char *path,
204                                  size_t path_len,
205                                  struct add_image_params *params)
206 {
207         struct wim_dentry *root = NULL;
208         int ret;
209         struct wim_inode *inode = NULL;
210         struct stat stbuf;
211
212         if (exclude_path(path, path_len, params->config, true)) {
213                 ret = 0;
214                 goto out_progress;
215         }
216
217         if ((params->add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) ||
218             (params->add_flags & WIMLIB_ADD_FLAG_ROOT))
219                 ret = stat(path, &stbuf);
220         else
221                 ret = lstat(path, &stbuf);
222
223         if (ret) {
224                 ERROR_WITH_ERRNO("Failed to stat \"%s\"", path);
225                 ret = WIMLIB_ERR_STAT;
226                 goto out;
227         }
228
229         if (!S_ISREG(stbuf.st_mode) &&
230             !S_ISDIR(stbuf.st_mode) &&
231             !S_ISLNK(stbuf.st_mode))
232         {
233                 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
234                 {
235                         ERROR("Can't archive unsupported file \"%s\"", path);
236                         ret = WIMLIB_ERR_UNSUPPORTED_FILE;
237                         goto out;
238                 }
239                 params->progress.scan.cur_path = path;
240                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
241                 ret = 0;
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) {
254                 /* Already captured this inode? */
255                 ret = 0;
256                 goto out_progress;
257         }
258
259 #ifdef HAVE_STAT_NANOSECOND_PRECISION
260         inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
261         inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
262         inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
263 #else
264         inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
265         inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
266         inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
267 #endif
268         inode->i_resolved = 1;
269         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
270                 ret = inode_set_unix_data(inode, stbuf.st_uid,
271                                           stbuf.st_gid,
272                                           stbuf.st_mode,
273                                           params->lookup_table,
274                                           UNIX_DATA_ALL | UNIX_DATA_CREATE);
275                 if (ret)
276                         goto out;
277         }
278         params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
279         if (S_ISREG(stbuf.st_mode))
280                 ret = unix_capture_regular_file(path, stbuf.st_size,
281                                                 inode, params->unhashed_streams);
282         else if (S_ISDIR(stbuf.st_mode))
283                 ret = unix_capture_directory(root, path, path_len, params);
284         else {
285                 ret = unix_capture_symlink(&root, path, inode, params);
286                 if (root == NULL)
287                         goto out;
288         }
289
290         if (ret)
291                 goto out;
292
293 out_progress:
294         params->progress.scan.cur_path = path;
295         if (root == NULL)
296                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
297         else
298                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
299 out:
300         if (ret)
301                 free_dentry_tree(root, params->lookup_table);
302         else
303                 *root_ret = root;
304         return ret;
305 }
306
307 /*
308  * unix_build_dentry_tree():
309  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
310  *      version; no NTFS-specific data is captured).
311  *
312  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
313  *              modified if successful.  Set to NULL if the file or directory was
314  *              excluded from capture.
315  *
316  * @root_disk_path:  The path to the root of the directory tree on disk.
317  *
318  * @params:     See doc for `struct add_image_params'.
319  *
320  * @return:     0 on success, nonzero on failure.  It is a failure if any of
321  *              the files cannot be `stat'ed, or if any of the needed
322  *              directories cannot be opened or read.  Failure to add the files
323  *              to the WIM may still occur later when trying to actually read
324  *              the on-disk files during a call to wimlib_write() or
325  *              wimlib_overwrite().
326  */
327 int
328 unix_build_dentry_tree(struct wim_dentry **root_ret,
329                        const char *root_disk_path,
330                        struct add_image_params *params)
331 {
332         char *path_buf;
333         int ret;
334         size_t path_len;
335         size_t path_bufsz;
336
337         {
338                 struct stat root_stbuf;
339                 if (stat(root_disk_path, &root_stbuf)) {
340                         ERROR_WITH_ERRNO("Failed to stat \"%s\"", root_disk_path);
341                         return WIMLIB_ERR_STAT;
342                 }
343
344                 if ((params->add_flags & WIMLIB_ADD_FLAG_ROOT) &&
345                     !S_ISDIR(root_stbuf.st_mode))
346                 {
347                         ERROR("Root of capture \"%s\" is not a directory",
348                               root_disk_path);
349                         return WIMLIB_ERR_NOTDIR;
350                 }
351                 params->capture_root_ino = root_stbuf.st_ino;
352                 params->capture_root_dev = root_stbuf.st_dev;
353         }
354
355         path_bufsz = min(32790, PATH_MAX + 1);
356         path_len = strlen(root_disk_path);
357
358         if (path_len >= path_bufsz)
359                 return WIMLIB_ERR_INVALID_PARAM;
360
361         path_buf = MALLOC(path_bufsz);
362         if (!path_buf)
363                 return WIMLIB_ERR_NOMEM;
364         memcpy(path_buf, root_disk_path, path_len + 1);
365
366         ret = unix_build_dentry_tree_recursive(root_ret, path_buf,
367                                                path_len, params);
368         FREE(path_buf);
369         return ret;
370 }
371
372 #endif /* !__WIN32__ */