fe95b9a6b6c7173bca3e225a657b7d30136fc343
[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 wim_lookup_table *lookup_table)
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->resource_entry.original_size = size;
68                 lookup_table_insert_unhashed(lookup_table, lte, inode, 0);
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         closedir(dir);
132         return ret;
133 }
134
135 static int
136 unix_capture_symlink(struct wim_dentry **root_p,
137                      const char *path,
138                      struct wim_inode *inode,
139                      struct add_image_params *params)
140 {
141         char deref_name_buf[4096];
142         ssize_t deref_name_len;
143         int ret;
144
145         inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
146         inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
147
148         /* The idea here is to call readlink() to get the UNIX target of the
149          * symbolic link, then turn the target into a reparse point data buffer
150          * that contains a relative or absolute symbolic link. */
151         deref_name_len = readlink(path, deref_name_buf,
152                                   sizeof(deref_name_buf) - 1);
153         if (deref_name_len >= 0) {
154                 char *dest = deref_name_buf;
155
156                 dest[deref_name_len] = '\0';
157                 DEBUG("Read symlink `%s'", dest);
158
159                 if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
160                      dest[0] == '/')
161                 {
162                         dest = capture_fixup_absolute_symlink(dest,
163                                                               params->capture_root_ino,
164                                                               params->capture_root_dev);
165                         if (!dest) {
166                                 WARNING("Ignoring out of tree absolute symlink "
167                                         "\"%s\" -> \"%s\"\n"
168                                         "          (Use --norpfix to capture "
169                                         "absolute symlinks as-is)",
170                                         path, deref_name_buf);
171                                 free_dentry(*root_p);
172                                 *root_p = NULL;
173                                 return 0;
174                         }
175                         inode->i_not_rpfixed = 0;
176                 }
177                 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
178                 if (ret == 0) {
179                         /* Unfortunately, Windows seems to have the concept of
180                          * "file" symbolic links as being different from
181                          * "directory" symbolic links...  so
182                          * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
183                          * symbolic link if the *target* of the symbolic link is
184                          * a directory.  */
185                         struct stat stbuf;
186                         if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
187                                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
188                 }
189         } else {
190                 ERROR_WITH_ERRNO("Failed to read target of "
191                                  "symbolic link `%s'", path);
192                 ret = WIMLIB_ERR_READLINK;
193         }
194         return ret;
195 }
196
197 static int
198 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
199                                  char *path,
200                                  size_t path_len,
201                                  struct add_image_params *params)
202 {
203         struct wim_dentry *root = NULL;
204         int ret = 0;
205         struct wim_inode *inode;
206
207         if (exclude_path(path, path_len, params->config, true)) {
208                 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
209                     && params->progress_func)
210                 {
211                         union wimlib_progress_info info;
212                         info.scan.cur_path = path;
213                         info.scan.status = WIMLIB_SCAN_DENTRY_EXCLUDED;
214                         params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
215                 }
216                 goto out;
217         }
218
219         if ((params->add_flags & WIMLIB_ADD_FLAG_VERBOSE)
220             && params->progress_func)
221         {
222                 union wimlib_progress_info info;
223                 info.scan.cur_path = path;
224                 info.scan.status = WIMLIB_SCAN_DENTRY_OK;
225                 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
226         }
227
228         struct stat stbuf;
229         int (*stat_fn)(const char *restrict, struct stat *restrict);
230         if ((params->add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) ||
231             (params->add_flags & WIMLIB_ADD_FLAG_ROOT))
232                 stat_fn = stat;
233         else
234                 stat_fn = lstat;
235
236         ret = (*stat_fn)(path, &stbuf);
237         if (ret) {
238                 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
239                 ret = WIMLIB_ERR_STAT;
240                 goto out;
241         }
242         if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
243             && !S_ISLNK(stbuf.st_mode)) {
244                 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
245                 {
246                         ERROR("Can't archive unsupported file \"%s\"", path);
247                         return WIMLIB_ERR_UNSUPPORTED_FILE;
248                 }
249                 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
250                     && params->progress_func)
251                 {
252                         union wimlib_progress_info info;
253                         info.scan.cur_path = path;
254                         info.scan.status = WIMLIB_SCAN_DENTRY_UNSUPPORTED;
255                         params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
256                 }
257                 goto out;
258         }
259
260         ret = inode_table_new_dentry(&params->inode_table,
261                                      path_basename_with_len(path, path_len),
262                                      stbuf.st_ino, stbuf.st_dev, false, &root);
263         if (ret)
264                 goto out;
265
266         inode = root->d_inode;
267
268         if (inode->i_nlink > 1) /* Already captured this inode? */
269                 goto out;
270
271 #ifdef HAVE_STAT_NANOSECOND_PRECISION
272         inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
273         inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
274         inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
275 #else
276         inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
277         inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
278         inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
279 #endif
280         inode->i_resolved = 1;
281         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
282                 ret = inode_set_unix_data(inode, stbuf.st_uid,
283                                           stbuf.st_gid,
284                                           stbuf.st_mode,
285                                           params->lookup_table,
286                                           UNIX_DATA_ALL | UNIX_DATA_CREATE);
287                 if (ret)
288                         goto out;
289         }
290         params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
291         if (S_ISREG(stbuf.st_mode))
292                 ret = unix_capture_regular_file(path, stbuf.st_size,
293                                                 inode, params->lookup_table);
294         else if (S_ISDIR(stbuf.st_mode))
295                 ret = unix_capture_directory(root, path, path_len, params);
296         else
297                 ret = unix_capture_symlink(&root, path, inode, params);
298 out:
299         if (ret == 0)
300                 *root_ret = root;
301         else
302                 free_dentry_tree(root, params->lookup_table);
303         return ret;
304 }
305
306 /*
307  * unix_build_dentry_tree():
308  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
309  *      version; no NTFS-specific data is captured).
310  *
311  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
312  *              modified if successful.  Set to NULL if the file or directory was
313  *              excluded from capture.
314  *
315  * @root_disk_path:  The path to the root of the directory tree on disk.
316  *
317  * @params:     See doc for `struct add_image_params'.
318  *
319  * @return:     0 on success, nonzero on failure.  It is a failure if any of
320  *              the files cannot be `stat'ed, or if any of the needed
321  *              directories cannot be opened or read.  Failure to add the files
322  *              to the WIM may still occur later when trying to actually read
323  *              the on-disk files during a call to wimlib_write() or
324  *              wimlib_overwrite().
325  */
326 int
327 unix_build_dentry_tree(struct wim_dentry **root_ret,
328                        const char *root_disk_path,
329                        struct add_image_params *params)
330 {
331         char *path_buf;
332         int ret;
333         size_t path_len;
334         size_t path_bufsz;
335
336         {
337                 struct stat root_stbuf;
338                 if (stat(root_disk_path, &root_stbuf)) {
339                         ERROR_WITH_ERRNO("Failed to stat \"%s\"", root_disk_path);
340                         return WIMLIB_ERR_STAT;
341                 }
342
343                 if ((params->add_flags & WIMLIB_ADD_FLAG_ROOT) &&
344                     !S_ISDIR(root_stbuf.st_mode))
345                 {
346                         ERROR("Root of capture \"%s\" is not a directory",
347                               root_disk_path);
348                         return WIMLIB_ERR_NOTDIR;
349                 }
350                 params->capture_root_ino = root_stbuf.st_ino;
351                 params->capture_root_dev = root_stbuf.st_dev;
352         }
353
354         path_bufsz = min(32790, PATH_MAX + 1);
355         path_len = strlen(root_disk_path);
356
357         if (path_len >= path_bufsz)
358                 return WIMLIB_ERR_INVALID_PARAM;
359
360         path_buf = MALLOC(path_bufsz);
361         if (!path_buf)
362                 return WIMLIB_ERR_NOMEM;
363         memcpy(path_buf, root_disk_path, path_len + 1);
364
365         ret = unix_build_dentry_tree_recursive(root_ret, path_buf,
366                                                path_len, params);
367         FREE(path_buf);
368         return ret;
369 }
370
371 #endif /* !__WIN32__ */