- struct stat root_stbuf;
- int ret = 0;
- int (*stat_fn)(const char *restrict, struct stat *restrict);
- struct dentry *root;
- const char *filename;
- struct inode *inode;
-
- if (exclude_path(root_disk_path, config, true)) {
- if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
- ERROR("Cannot exclude the root directory from capture");
- return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
- }
- if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
- && progress_func)
- {
- union wimlib_progress_info info;
- info.scan.cur_path = root_disk_path;
- info.scan.excluded = true;
- progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
- }
- *root_ret = NULL;
- return 0;
- }
-
- if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
- && progress_func)
- {
- union wimlib_progress_info info;
- info.scan.cur_path = root_disk_path;
- info.scan.excluded = false;
- progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
- }
-
- if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
- stat_fn = stat;
- else
- stat_fn = lstat;
-
- ret = (*stat_fn)(root_disk_path, &root_stbuf);
- if (ret != 0) {
- ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
- return WIMLIB_ERR_STAT;
- }
-
- if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
- !S_ISDIR(root_stbuf.st_mode))
- {
- /* Do a dereference-stat in case the root is a symbolic link.
- * This case is allowed, provided that the symbolic link points
- * to a directory. */
- ret = stat(root_disk_path, &root_stbuf);
- if (ret != 0) {
- ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
- return WIMLIB_ERR_STAT;
- }
- if (!S_ISDIR(root_stbuf.st_mode)) {
- ERROR("`%s' is not a directory", root_disk_path);
- return WIMLIB_ERR_NOTDIR;
- }
- }
- if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
- && !S_ISLNK(root_stbuf.st_mode)) {
- ERROR("`%s' is not a regular file, directory, or symbolic link.",
- root_disk_path);
- return WIMLIB_ERR_SPECIAL_FILE;
- }
-
- if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
- filename = "";
- else
- filename = path_basename(root_disk_path);
-
- root = new_dentry_with_timeless_inode(filename);
- if (!root) {
- if (errno == EILSEQ)
- return WIMLIB_ERR_INVALID_UTF8_STRING;
- else
- return WIMLIB_ERR_NOMEM;
- }
-
- inode = root->d_inode;
-
-#ifdef HAVE_STAT_NANOSECOND_PRECISION
- inode->creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
- inode->last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
- inode->last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
-#else
- inode->creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
- inode->last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
- inode->last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
-#endif
- if (sizeof(ino_t) >= 8)
- inode->ino = (u64)root_stbuf.st_ino;
- else
- inode->ino = (u64)root_stbuf.st_ino |
- ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
-
- add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
- inode->resolved = 1;
-
- if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
-
- struct lookup_table_entry *lte;
- u8 hash[SHA1_HASH_SIZE];
-
- inode->attributes = FILE_ATTRIBUTE_NORMAL;
-
- /* Empty files do not have to have a lookup table entry. */
- if (root_stbuf.st_size == 0)
- goto out;
-
- /* For each regular file, we must check to see if the file is in
- * the lookup table already; if it is, we increment its refcnt;
- * otherwise, we create a new lookup table entry and insert it.
- * */
-
- ret = sha1sum(root_disk_path, hash);
- if (ret != 0)
- goto out;
-
- lte = __lookup_resource(lookup_table, hash);
- if (lte) {
- lte->refcnt++;
- DEBUG("Add lte reference %u for `%s'", lte->refcnt,
- root_disk_path);
- } else {
- char *file_on_disk = STRDUP(root_disk_path);
- if (!file_on_disk) {
- ERROR("Failed to allocate memory for file path");
- ret = WIMLIB_ERR_NOMEM;
- goto out;
- }
- lte = new_lookup_table_entry();
- if (!lte) {
- FREE(file_on_disk);
- ret = WIMLIB_ERR_NOMEM;
- goto out;
- }
- lte->file_on_disk = file_on_disk;
- lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
- lte->resource_entry.original_size = root_stbuf.st_size;
- lte->resource_entry.size = root_stbuf.st_size;
- copy_hash(lte->hash, hash);
- lookup_table_insert(lookup_table, lte);
- }
- root->d_inode->lte = lte;
- } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
-
- inode->attributes = FILE_ATTRIBUTE_DIRECTORY;