- if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
-
- struct wim_lookup_table_entry *lte;
- u8 hash[SHA1_HASH_SIZE];
-
- inode->i_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->i_lte = lte;
- } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
-
- inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
-
- DIR *dir;
- struct dirent entry, *result;
- struct wim_dentry *child;
-
- dir = opendir(root_disk_path);
- if (!dir) {
- ERROR_WITH_ERRNO("Failed to open the directory `%s'",
- root_disk_path);
- ret = WIMLIB_ERR_OPEN;
- goto out;
- }
-
- /* Buffer for names of files in directory. */
- size_t len = strlen(root_disk_path);
- char name[len + 1 + FILENAME_MAX + 1];
- memcpy(name, root_disk_path, len);
- name[len] = '/';
-
- /* Create a dentry for each entry in the directory on disk, and recurse
- * to any subdirectories. */
- while (1) {
- errno = 0;
- ret = readdir_r(dir, &entry, &result);
- if (ret != 0) {
- ret = WIMLIB_ERR_READ;
- ERROR_WITH_ERRNO("Error reading the "
- "directory `%s'",
- root_disk_path);
- break;
- }
- if (result == NULL)
- break;
- if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
- || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
- continue;
- strcpy(name + len + 1, result->d_name);
- ret = build_dentry_tree(&child, name, lookup_table,
- NULL, config, add_image_flags,
- progress_func, NULL);
- if (ret != 0)
- break;
- if (child)
- dentry_add_child(root, child);
- }
- closedir(dir);
- } else { /* Archiving a symbolic link */
- inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
- inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
-
- /* The idea here is to call readlink() to get the UNIX target of
- * the symbolic link, then turn the target into a reparse point
- * data buffer that contains a relative or absolute symbolic
- * link (NOT a junction point or *full* path symbolic link with
- * drive letter).
- */
-
- char deref_name_buf[4096];
- ssize_t deref_name_len;
-
- deref_name_len = readlink(root_disk_path, deref_name_buf,
- sizeof(deref_name_buf) - 1);
- if (deref_name_len >= 0) {
- deref_name_buf[deref_name_len] = '\0';
- DEBUG("Read symlink `%s'", deref_name_buf);
- ret = inode_set_symlink(root->d_inode, deref_name_buf,
- lookup_table, NULL);
- if (ret == 0) {
- /*
- * Unfortunately, Windows seems to have the
- * concept of "file" symbolic links as being
- * different from "directory" symbolic links...
- * so FILE_ATTRIBUTE_DIRECTORY needs to be set
- * on the symbolic link if the *target* of the
- * symbolic link is a directory.
- */
- struct stat stbuf;
- if (stat(root_disk_path, &stbuf) == 0 &&
- S_ISDIR(stbuf.st_mode))
- {
- inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
- }
- }
- } else {
- ERROR_WITH_ERRNO("Failed to read target of "
- "symbolic link `%s'", root_disk_path);
- ret = WIMLIB_ERR_READLINK;
- }
- }
-#else
- /* Win32 version of capturing a directory tree */
-
- wchar_t *path_utf16;
- size_t path_utf16_nchars;
- struct sd_set *sd_set;
- DWORD err;
-
- if (extra_arg == NULL) {
- sd_set = alloca(sizeof(struct sd_set));
- sd_set->rb_root.rb_node = NULL,
- sd_set->sd = sd;
- } else {
- sd_set = extra_arg;
- }
-
- ret = utf8_to_utf16(root_disk_path, strlen(root_disk_path),
- (char**)&path_utf16, &path_utf16_nchars);
- if (ret)
- goto out_destroy_sd_set;
- path_utf16_nchars /= sizeof(wchar_t);
-
- HANDLE hFile = win32_open_file_readonly(path_utf16);
- if (hFile == INVALID_HANDLE_VALUE) {
- err = GetLastError();
- ERROR("Win32 API: Failed to open \"%s\"", root_disk_path);
- win32_error(err);
- ret = WIMLIB_ERR_OPEN;
- goto out_free_path_utf16;
- }
-
- BY_HANDLE_FILE_INFORMATION file_info;
- if (!GetFileInformationByHandle(hFile, &file_info)) {
- err = GetLastError();
- ERROR("Win32 API: Failed to get file information for \"%s\"",
- root_disk_path);
- win32_error(err);
- ret = WIMLIB_ERR_STAT;
- goto out_close_handle;
- }
-
- /* Create a WIM dentry */
- root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
- if (!root) {
- if (errno == EILSEQ)
- ret = WIMLIB_ERR_INVALID_UTF8_STRING;
- else if (errno == ENOMEM)
- ret = WIMLIB_ERR_NOMEM;
- else
- ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
- goto out_close_handle;
- }
-
- /* Start preparing the associated WIM inode */
- inode = root->d_inode;
-
- inode->i_attributes = file_info.dwFileAttributes;
- inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
- inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
- inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
- inode->i_ino = ((u64)file_info.nFileIndexHigh << 32) |
- (u64)file_info.nFileIndexLow;
-
- inode->i_resolved = 1;
- add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
-
- /* Get DOS name and security descriptor (if any). */
- ret = win32_get_short_name(root, path_utf16);
- if (ret)
- goto out_close_handle;
- ret = win32_get_security_descriptor(root, sd_set, path_utf16);
- if (ret)
- goto out_close_handle;
-
- if (inode_is_directory(inode)) {
- /* Directory (not a reparse point) --- recurse to children */
-
- /* But first... directories may have alternate data streams that
- * need to be captured. */
- ret = win32_capture_streams(path_utf16,
- path_utf16_nchars,
- inode,
- lookup_table);
- if (ret)
- goto out_close_handle;
- ret = win32_recurse_directory(root,
- root_disk_path,
- lookup_table,
- sd,
- config,
- add_image_flags,
- progress_func,
- sd_set,
- path_utf16,
- path_utf16_nchars);
- } else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
- /* Reparse point: save the reparse tag and data */
- ret = win32_capture_reparse_point(hFile,
- inode,
- lookup_table,
- root_disk_path);
- } else {
- /* Not a directory, not a reparse point; capture the default
- * file contents and any alternate data streams. */
- ret = win32_capture_streams(path_utf16,
- path_utf16_nchars,
- inode,
- lookup_table);
- }
-out_close_handle:
- CloseHandle(hFile);
-out_free_path_utf16:
- FREE(path_utf16);
-out_destroy_sd_set:
- if (extra_arg == NULL)
- destroy_sd_set(sd_set);
-#endif
- /* The below lines of code are common to both UNIX and Win32 builds. It
- * simply returns the captured directory tree if the capture was
- * successful, or frees it if the capture was unsuccessful. */