]> wimlib.net Git - wimlib/blobdiff - src/write.c
Update wimlib.h docs
[wimlib] / src / write.c
index bd5b05ef1f33e6f34e3325d9b787319d0f26d24d..8355a28357e14cc46774785039b7b59e12e151de 100644 (file)
@@ -33,7 +33,7 @@
 #endif
 
 #ifdef __WIN32__
-#  include <win32.h>
+#  include "win32.h"
 #endif
 
 #include "list.h"
@@ -45,6 +45,7 @@
 #include "lzx.h"
 #include "xpress.h"
 
+
 #ifdef ENABLE_MULTITHREADED_COMPRESSION
 #  include <pthread.h>
 #endif
 
 #include <limits.h>
 
+#if defined(__WIN32__) && !defined(INVALID_HANDLE_VALUE)
+#  define INVALID_HANDLE_VALUE ((HANDLE)(-1))
+#endif
+
 static int
 fflush_and_ftruncate(FILE *fp, off_t size)
 {
@@ -306,10 +311,14 @@ prepare_resource_for_read(struct wim_lookup_table_entry *lte
 #endif
 #ifdef __WIN32__
        case RESOURCE_WIN32:
-               if (!lte->file_on_disk_fp) {
-                       lte->file_on_disk_fp = win32_open_file_readonly(lte->file_on_disk);
-                       if (!lte->file_on_disk_fp)
+               if (lte->win32_file_on_disk_fp == INVALID_HANDLE_VALUE) {
+                       lte->win32_file_on_disk_fp =
+                               win32_open_file_readonly(lte->win32_file_on_disk, true);
+                       if (lte->win32_file_on_disk_fp == INVALID_HANDLE_VALUE) {
+                               ERROR("Win32 API: Can't open %ls", lte->win32_file_on_disk);
+                               win32_error_last();
                                return WIMLIB_ERR_OPEN;
+                       }
                }
                break;
 #endif
@@ -321,7 +330,7 @@ prepare_resource_for_read(struct wim_lookup_table_entry *lte
 
 /* Undo prepare_resource_for_read() by closing the cached FILE * or NTFS
  * attribute. */
-static void 
+static void
 end_wim_resource_read(struct wim_lookup_table_entry *lte
                        #ifdef WITH_NTFS_3G
                                , ntfs_inode *ni
@@ -346,10 +355,10 @@ end_wim_resource_read(struct wim_lookup_table_entry *lte
 #endif
 #ifdef __WIN32__
        else if (lte->resource_location == RESOURCE_WIN32
-                && lte->file_on_disk_fp)
+                && lte->win32_file_on_disk_fp != INVALID_HANDLE_VALUE)
        {
-               win32_close_file(lte->file_on_disk_fp);
-               lte->file_on_disk_fp = NULL;
+               win32_close_file(lte->win32_file_on_disk_fp);
+               lte->win32_file_on_disk_fp = INVALID_HANDLE_VALUE;
        }
 #endif
 }
@@ -1973,6 +1982,17 @@ overwrite_wim_via_tmpfile(WIMStruct *w, int write_flags,
 
        DEBUG("Renaming `%s' to `%s'", tmpfile, w->filename);
 
+#ifdef __WIN32__
+       /* Windows won't let you delete open files unless FILE_SHARE_DELETE was
+        * specified to CreateFile().  The WIM was opened with fopen(), which
+        * didn't provided this flag to CreateFile, so the handle must be closed
+        * before executing the rename(). */
+       if (w->fp != NULL) {
+               fclose(w->fp);
+               w->fp = NULL;
+       }
+#endif
+
        /* Rename the new file to the old file .*/
        if (rename(tmpfile, w->filename) != 0) {
                ERROR_WITH_ERRNO("Failed to rename `%s' to `%s'",