#endif
#ifdef __WIN32__
-# include <win32.h>
+# include "win32.h"
#endif
#include "list.h"
#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)
{
#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
/* 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
#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
}
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'",