]> wimlib.net Git - wimlib/blobdiff - src/write.c
Win32 capture
[wimlib] / src / write.c
index 1f471d6fead32e046b969efa428b7abf1094b04e..c346e61c65c57e51b629e5d306799724255d18f4 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 /*
- * Copyright (C) 2012 Eric Biggers
+ * Copyright (C) 2012, 2013 Eric Biggers
  *
  * This file is part of wimlib, a library for working with WIM files.
  *
@@ -257,43 +257,55 @@ static int prepare_resource_for_read(struct wim_lookup_table_entry *lte
                                        #endif
                )
 {
-       if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK
-            && !lte->file_on_disk_fp)
-       {
-               wimlib_assert(lte->file_on_disk);
-               lte->file_on_disk_fp = fopen(lte->file_on_disk, "rb");
+       switch (lte->resource_location) {
+       case RESOURCE_IN_FILE_ON_DISK:
                if (!lte->file_on_disk_fp) {
-                       ERROR_WITH_ERRNO("Failed to open the file `%s' for "
-                                        "reading", lte->file_on_disk);
-                       return WIMLIB_ERR_OPEN;
+                       lte->file_on_disk_fp = fopen(lte->file_on_disk, "rb");
+                       if (!lte->file_on_disk_fp) {
+                               ERROR_WITH_ERRNO("Failed to open the file "
+                                                "`%s'", lte->file_on_disk);
+                               return WIMLIB_ERR_OPEN;
+                       }
                }
-       }
+               break;
 #ifdef WITH_NTFS_3G
-       else if (lte->resource_location == RESOURCE_IN_NTFS_VOLUME
-                 && !lte->attr)
-       {
-               struct ntfs_location *loc = lte->ntfs_loc;
-               ntfs_inode *ni;
-               wimlib_assert(loc);
-               ni = ntfs_pathname_to_inode(*loc->ntfs_vol_p, NULL, loc->path_utf8);
-               if (!ni) {
-                       ERROR_WITH_ERRNO("Failed to open inode `%s' in NTFS "
-                                        "volume", loc->path_utf8);
-                       return WIMLIB_ERR_NTFS_3G;
-               }
-               lte->attr = ntfs_attr_open(ni,
-                                          loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
-                                          (ntfschar*)loc->stream_name_utf16,
-                                          loc->stream_name_utf16_num_chars);
+       case RESOURCE_IN_NTFS_VOLUME:
                if (!lte->attr) {
-                       ERROR_WITH_ERRNO("Failed to open attribute of `%s' in "
-                                        "NTFS volume", loc->path_utf8);
-                       ntfs_inode_close(ni);
-                       return WIMLIB_ERR_NTFS_3G;
+                       struct ntfs_location *loc = lte->ntfs_loc;
+                       ntfs_inode *ni;
+                       wimlib_assert(loc);
+                       ni = ntfs_pathname_to_inode(*loc->ntfs_vol_p, NULL, loc->path_utf8);
+                       if (!ni) {
+                               ERROR_WITH_ERRNO("Failed to open inode `%s' in NTFS "
+                                                "volume", loc->path_utf8);
+                               return WIMLIB_ERR_NTFS_3G;
+                       }
+                       lte->attr = ntfs_attr_open(ni,
+                                                  loc->is_reparse_point ? AT_REPARSE_POINT : AT_DATA,
+                                                  (ntfschar*)loc->stream_name_utf16,
+                                                  loc->stream_name_utf16_num_chars);
+                       if (!lte->attr) {
+                               ERROR_WITH_ERRNO("Failed to open attribute of `%s' in "
+                                                "NTFS volume", loc->path_utf8);
+                               ntfs_inode_close(ni);
+                               return WIMLIB_ERR_NTFS_3G;
+                       }
+                       *ni_ret = ni;
                }
-               *ni_ret = ni;
-       }
+               break;
 #endif
+#if defined(__CYGWIN__) || defined(__WIN32__)
+       case RESOURCE_WIN32:
+               if (!lte->file_on_disk_fp) {
+                       lte->file_on_disk_fp = win32_open_handle(lte->file_on_disk);
+                       if (!lte->file_on_disk_fp)
+                               return WIMLIB_ERR_OPEN;
+               }
+               break;
+#endif
+       default:
+               break;
+       }
        return 0;
 }
 
@@ -306,7 +318,8 @@ static void end_wim_resource_read(struct wim_lookup_table_entry *lte
                                        )
 {
        if (lte->resource_location == RESOURCE_IN_FILE_ON_DISK
-           && lte->file_on_disk_fp) {
+           && lte->file_on_disk_fp)
+       {
                fclose(lte->file_on_disk_fp);
                lte->file_on_disk_fp = NULL;
        }
@@ -320,6 +333,14 @@ static void end_wim_resource_read(struct wim_lookup_table_entry *lte
                        ntfs_inode_close(ni);
        }
 #endif
+#if defined(__CYGWIN__) || defined(__WIN32__)
+       else if (lte->resource_location == RESOURCE_WIN32
+                && lte->file_on_disk_fp)
+       {
+               win32_close_handle(lte->file_on_disk_fp);
+               lte->file_on_disk_fp = NULL;
+       }
+#endif
 }
 
 static int
@@ -835,10 +856,11 @@ static int main_writer_thread_proc(struct list_head *stream_list,
                for (size_t j = 0; j < MAX_CHUNKS_PER_MSG; j++) {
                        msgs[i].compressed_chunks[j] = MALLOC(WIM_CHUNK_SIZE);
 
-                       // The extra 8 bytes is because longest_match() in lz.c
-                       // may read a little bit off the end of the uncompressed
-                       // data.  It doesn't need to be initialized--- we really
-                       // just need to avoid accessing an unmapped page.
+                       // The extra 8 bytes is because longest_match() in
+                       // lz77.c may read a little bit off the end of the
+                       // uncompressed data.  It doesn't need to be
+                       // initialized--- we really just need to avoid accessing
+                       // an unmapped page.
                        msgs[i].uncompressed_chunks[j] = MALLOC(WIM_CHUNK_SIZE + 8);
                        if (msgs[i].compressed_chunks[j] == NULL ||
                            msgs[i].uncompressed_chunks[j] == NULL)
@@ -1265,8 +1287,8 @@ out_join:
 
        for (unsigned i = 0; i < num_threads; i++) {
                if (pthread_join(compressor_threads[i], NULL)) {
-                       WARNING("Failed to join compressor thread %u: %s",
-                               i, strerror(errno));
+                       WARNING_WITH_ERRNO("Failed to join compressor "
+                                          "thread %u", i);
                }
        }
        FREE(compressor_threads);
@@ -1603,8 +1625,8 @@ int lock_wim(WIMStruct *w, FILE *fp)
                                      "        by another process!", w->filename);
                                ret = WIMLIB_ERR_ALREADY_LOCKED;
                        } else {
-                               WARNING("Failed to lock `%s': %s",
-                                       w->filename, strerror(errno));
+                               WARNING_WITH_ERRNO("Failed to lock `%s'",
+                                                  w->filename);
                                ret = 0;
                        }
                } else {
@@ -1642,8 +1664,7 @@ void close_wim_writable(WIMStruct *w)
 {
        if (w->out_fp) {
                if (fclose(w->out_fp) != 0) {
-                       WARNING("Failed to close output WIM: %s",
-                               strerror(errno));
+                       WARNING_WITH_ERRNO("Failed to close output WIM");
                }
                w->out_fp = NULL;
        }
@@ -1704,6 +1725,7 @@ WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path,
        ret = finish_write(w, image, write_flags, progress_func);
 out:
        close_wim_writable(w);
+       DEBUG("wimlib_write(path=%s) = %d", path, ret);
        return ret;
 }
 
@@ -1929,8 +1951,8 @@ static int overwrite_wim_via_tmpfile(WIMStruct *w, int write_flags,
        w->fp = fopen(w->filename, "rb");
        if (w->fp == NULL) {
                ret = WIMLIB_ERR_REOPEN;
-               WARNING("Failed to re-open `%s' read-only: %s",
-                       w->filename, strerror(errno));
+               WARNING_WITH_ERRNO("Failed to re-open `%s' read-only",
+                                  w->filename);
                FREE(w->filename);
                w->filename = NULL;
        }
@@ -1938,7 +1960,7 @@ static int overwrite_wim_via_tmpfile(WIMStruct *w, int write_flags,
 err:
        /* Remove temporary file. */
        if (unlink(tmpfile) != 0)
-               WARNING("Failed to remove `%s': %s", tmpfile, strerror(errno));
+               WARNING_WITH_ERRNO("Failed to remove `%s'", tmpfile);
        return ret;
 }