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