]> wimlib.net Git - wimlib/blobdiff - src/win32.c
WIM capture: Share inodes immediately
[wimlib] / src / win32.c
index 3a914943ef70d4be1e2e8e7a163e2642aca7b447..638380908b62091fe7b91dbb164adbecb152271d 100644 (file)
@@ -23,9 +23,7 @@
  * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
-#ifndef __WIN32__
-#  error "This file contains Windows code"
-#endif
+#ifdef __WIN32__
 
 #include "config.h"
 #include <windows.h>
@@ -41,6 +39,7 @@
 #include "lookup_table.h"
 #include "security.h"
 #include "endianness.h"
+#include <pthread.h>
 
 #include <errno.h>
 
@@ -220,6 +219,10 @@ win32_get_short_name(struct wim_dentry *dentry, const wchar_t *path)
                memcpy(dentry->short_name, dat.cAlternateFileName, n);
                dentry->short_name_nbytes = short_name_nbytes;
        }
+       /* If we can't read the short filename for some reason, we just ignore
+        * the error and assume the file has no short name.  I don't think this
+        * should be an issue, since the short names are essentially obsolete
+        * anyway. */
        return 0;
 }
 
@@ -307,8 +310,9 @@ win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
                                  wchar_t *path,
                                  size_t path_num_chars,
                                  struct wim_lookup_table *lookup_table,
+                                 struct wim_inode_table *inode_table,
                                  struct sd_set *sd_set,
-                                 const struct capture_config *config,
+                                 const struct wimlib_capture_config *config,
                                  int add_image_flags,
                                  wimlib_progress_func_t progress_func,
                                  struct win32_capture_state *state);
@@ -320,8 +324,9 @@ win32_recurse_directory(struct wim_dentry *root,
                        wchar_t *dir_path,
                        size_t dir_path_num_chars,
                        struct wim_lookup_table *lookup_table,
+                       struct wim_inode_table *inode_table,
                        struct sd_set *sd_set,
-                       const struct capture_config *config,
+                       const struct wimlib_capture_config *config,
                        int add_image_flags,
                        wimlib_progress_func_t progress_func,
                        struct win32_capture_state *state)
@@ -372,6 +377,7 @@ win32_recurse_directory(struct wim_dentry *root,
                                                        dir_path,
                                                        path_len,
                                                        lookup_table,
+                                                       inode_table,
                                                        sd_set,
                                                        config,
                                                        add_image_flags,
@@ -675,6 +681,7 @@ win32_capture_streams(const wchar_t *path,
                        return 0;
                } else {
                        if (err == ERROR_ACCESS_DENIED) {
+                               /* XXX This maybe should be an error. */
                                WARNING("Failed to look up data streams "
                                        "of \"%ls\": Access denied!\n%ls",
                                        path, capture_access_denied_msg);
@@ -705,11 +712,16 @@ out_find_close:
        FindClose(hFind);
        return ret;
 unnamed_only:
+       /* FindFirstStreamW() API is not available.  Only capture the unnamed
+        * data stream. */
        if (inode->i_attributes &
             (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
        {
                ret = 0;
        } else {
+               /* Just create our own WIN32_FIND_STREAM_DATA for an unnamed
+                * stream to reduce the code to a call to the
+                * already-implemented win32_capture_stream() */
                wcscpy(dat.cStreamName, L"::$DATA");
                dat.StreamSize.QuadPart = file_size;
                ret = win32_capture_stream(path,
@@ -725,8 +737,9 @@ win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
                                  wchar_t *path,
                                  size_t path_num_chars,
                                  struct wim_lookup_table *lookup_table,
+                                 struct wim_inode_table *inode_table,
                                  struct sd_set *sd_set,
-                                 const struct capture_config *config,
+                                 const struct wimlib_capture_config *config,
                                  int add_image_flags,
                                  wimlib_progress_func_t progress_func,
                                  struct win32_capture_state *state)
@@ -737,13 +750,13 @@ win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
        u64 file_size;
        int ret = 0;
 
-       if (exclude_path(path, config, true)) {
+       if (exclude_path(path, path_num_chars, config, true)) {
                if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
                        ERROR("Cannot exclude the root directory from capture");
                        ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
                        goto out;
                }
-               if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
+               if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
                    && progress_func)
                {
                        union wimlib_progress_info info;
@@ -783,29 +796,32 @@ win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
                goto out_close_handle;
        }
 
-       /* Create a WIM dentry */
-       ret = new_dentry_with_timeless_inode(path_basename_with_len(path, path_num_chars),
-                                            &root);
+       /* Create a WIM dentry with an associated inode, which may be shared */
+       ret = inode_table_new_dentry(inode_table,
+                                    path_basename_with_len(path, path_num_chars),
+                                    ((u64)file_info.nFileIndexHigh << 32) |
+                                        (u64)file_info.nFileIndexLow,
+                                    file_info.dwVolumeSerialNumber,
+                                    &root);
+       if (ret)
+               goto out_close_handle;
+
+       ret = win32_get_short_name(root, path);
        if (ret)
                goto out_close_handle;
 
-       /* Start preparing the associated WIM inode */
        inode = root->d_inode;
 
+       if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
+               goto out_close_handle;
+
        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);
-       if (ret)
-               goto out_close_handle;
+       add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
 
        if (!(add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)) {
                ret = win32_get_security_descriptor(root, sd_set, path, state,
@@ -833,13 +849,16 @@ win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
                                              path,
                                              path_num_chars,
                                              lookup_table,
+                                             inode_table,
                                              sd_set,
                                              config,
                                              add_image_flags,
                                              progress_func,
                                              state);
        } else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
-               /* Reparse point: save the reparse tag and data */
+               /* Reparse point: save the reparse tag and data.  Alternate data
+                * streams are not captured, if it's even possible for a reparse
+                * point to have alternate data streams... */
                ret = win32_capture_reparse_point(hFile,
                                                  inode,
                                                  lookup_table,
@@ -900,8 +919,9 @@ int
 win32_build_dentry_tree(struct wim_dentry **root_ret,
                        const wchar_t *root_disk_path,
                        struct wim_lookup_table *lookup_table,
+                       struct wim_inode_table *inode_table,
                        struct sd_set *sd_set,
-                       const struct capture_config *config,
+                       const struct wimlib_capture_config *config,
                        int add_image_flags,
                        wimlib_progress_func_t progress_func,
                        void *extra_arg)
@@ -930,6 +950,7 @@ win32_build_dentry_tree(struct wim_dentry **root_ret,
                                                path,
                                                path_nchars,
                                                lookup_table,
+                                               inode_table,
                                                sd_set,
                                                config,
                                                add_image_flags,
@@ -1029,12 +1050,9 @@ win32_set_security_data(const struct wim_inode *inode,
 again:
        if (SetFileSecurityW(path, securityInformation, descriptor))
                return 0;
-
        err = GetLastError();
-
        if (args->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
                goto fail;
-
        switch (err) {
        case ERROR_PRIVILEGE_NOT_HELD:
                if (securityInformation & SACL_SECURITY_INFORMATION) {
@@ -1081,7 +1099,6 @@ fail:
                win32_error(err);
                return WIMLIB_ERR_WRITE;
        }
-       return 0;
 }
 
 
@@ -1404,16 +1421,21 @@ out:
 int
 fsync(int fd)
 {
-       HANDLE h = (HANDLE)_get_osfhandle(fd);
+       DWORD err;
+       HANDLE h;
+
+       h = (HANDLE)_get_osfhandle(fd);
        if (h == INVALID_HANDLE_VALUE) {
+               err = GetLastError();
                ERROR("Could not get Windows handle for file descriptor");
-               win32_error(GetLastError());
+               win32_error(err);
                errno = EBADF;
                return -1;
        }
        if (!FlushFileBuffers(h)) {
+               err = GetLastError();
                ERROR("Could not flush file buffers to disk");
-               win32_error(GetLastError());
+               win32_error(err);
                errno = EIO;
                return -1;
        }
@@ -1437,33 +1459,30 @@ realpath(const wchar_t *path, wchar_t *resolved_path)
 {
        DWORD ret;
        wimlib_assert(resolved_path == NULL);
+       DWORD err;
 
        ret = GetFullPathNameW(path, 0, NULL, NULL);
-       if (!ret)
+       if (!ret) {
+               err = GetLastError();
                goto fail_win32;
+       }
 
        resolved_path = TMALLOC(ret);
        if (!resolved_path)
-               goto fail;
+               goto out;
        ret = GetFullPathNameW(path, ret, resolved_path, NULL);
        if (!ret) {
+               err = GetLastError();
                free(resolved_path);
+               resolved_path = NULL;
                goto fail_win32;
        }
-       return resolved_path;
+       goto out;
 fail_win32:
-       win32_error(GetLastError());
-fail:
-       return NULL;
-}
-
-char *
-nl_langinfo(nl_item item)
-{
-       wimlib_assert(item == CODESET);
-       static char buf[64];
-       strcpy(buf, "Unknown");
-       return buf;
+       win32_error(err);
+       errno = -1;
+out:
+       return resolved_path;
 }
 
 /* rename() on Windows fails if the destination file exists.  And we need to
@@ -1479,7 +1498,7 @@ win32_rename_replacement(const wchar_t *oldpath, const wchar_t *newpath)
                ERROR("MoveFileEx(): Can't rename \"%ls\" to \"%ls\"",
                      oldpath, newpath);
                win32_error(err);
-               errno = 0;
+               errno = -1;
                return -1;
        }
 }
@@ -1520,8 +1539,26 @@ fail_close_handle:
 fail:
        if (err == NO_ERROR)
                err = GetLastError();
-       ERROR("Can't truncate %ls to %"PRIu64" bytes", path, size);
+       ERROR("Can't truncate \"%ls\" to %"PRIu64" bytes", path, size);
        win32_error(err);
        errno = -1;
        return -1;
 }
+
+
+/* This really could be replaced with _wcserror_s, but this doesn't seem to
+ * actually be available in MSVCRT.DLL on Windows XP (perhaps it's statically
+ * linked in by Visual Studio...?). */
+extern int
+win32_strerror_r_replacement(int errnum, wchar_t *buf, size_t buflen)
+{
+       static pthread_mutex_t strerror_lock = PTHREAD_MUTEX_INITIALIZER;
+
+       pthread_mutex_lock(&strerror_lock);
+       mbstowcs(buf, strerror(errnum), buflen);
+       buf[buflen - 1] = '\0';
+       pthread_mutex_unlock(&strerror_lock);
+       return 0;
+}
+
+#endif /* __WIN32__ */