]> wimlib.net Git - wimlib/blobdiff - src/wim.c
wimlib_get_wim_info(): Fix setting of is_readonly
[wimlib] / src / wim.c
index cfe100d69b516fcb0633c1ad784496b9c2f39880..ba0702f7949a4260650182b25de720409046fac6 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -125,6 +125,8 @@ wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
        struct wim_lookup_table *table;
        int ret;
 
+       wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
+
        DEBUG("Creating new WIM with %"TS" compression.",
              wimlib_get_compression_type_string(ctype));
 
@@ -152,12 +154,6 @@ out_free:
        return ret;
 }
 
-WIMLIBAPI int
-wimlib_get_num_images(const WIMStruct *w)
-{
-       return w->hdr.image_count;
-}
-
 int
 select_wim_image(WIMStruct *w, int image)
 {
@@ -208,13 +204,6 @@ select_wim_image(WIMStruct *w, int image)
 }
 
 
-/* Returns the compression type of the WIM file. */
-WIMLIBAPI int
-wimlib_get_compression_type(const WIMStruct *w)
-{
-       return w->compression_type;
-}
-
 WIMLIBAPI const tchar *
 wimlib_get_compression_type_string(int ctype)
 {
@@ -265,37 +254,31 @@ wimlib_resolve_image(WIMStruct *w, const tchar *image_name_or_num)
 
 /* Prints some basic information about a WIM file. */
 WIMLIBAPI void
-wimlib_print_wim_information(const WIMStruct *w)
+wimlib_print_wim_information(const WIMStruct *wim)
 {
-       const struct wim_header *hdr;
+       struct wimlib_wim_info info;
+
+       wimlib_get_wim_info((WIMStruct*)wim, &info);
 
-       hdr = &w->hdr;
        tputs(T("WIM Information:"));
        tputs(T("----------------"));
-       tprintf(T("Path:           %"TS"\n"), w->filename);
+       tprintf(T("Path:           %"TS"\n"), wim->filename);
        tfputs(T("GUID:           0x"), stdout);
-       print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
+       print_byte_field(info.guid, WIM_GID_LEN, stdout);
        tputchar(T('\n'));
-       tprintf(T("Image Count:    %d\n"), hdr->image_count);
+       tprintf(T("Image Count:    %d\n"), info.image_count);
        tprintf(T("Compression:    %"TS"\n"),
-               wimlib_get_compression_type_string(w->compression_type));
-       tprintf(T("Part Number:    %d/%d\n"), hdr->part_number, hdr->total_parts);
-       tprintf(T("Boot Index:     %d\n"), hdr->boot_idx);
-       tprintf(T("Size:           %"PRIu64" bytes\n"),
-               wim_info_get_total_bytes(w->wim_info));
+               wimlib_get_compression_type_string(info.compression_type));
+       tprintf(T("Part Number:    %d/%d\n"), info.part_number, info.total_parts);
+       tprintf(T("Boot Index:     %d\n"), info.boot_index);
+       tprintf(T("Size:           %"PRIu64" bytes\n"), info.total_bytes);
        tprintf(T("Integrity Info: %"TS"\n"),
-               (w->hdr.integrity.offset != 0) ? T("yes") : T("no"));
+               info.has_integrity_table ? T("yes") : T("no"));
        tprintf(T("Relative path junction: %"TS"\n"),
-               (hdr->flags & WIM_HDR_FLAG_RP_FIX) ? T("yes") : T("no"));
+               info.has_rpfix ? T("yes") : T("no"));
        tputchar(T('\n'));
 }
 
-WIMLIBAPI bool
-wimlib_has_integrity_table(const WIMStruct *w)
-{
-       return w->hdr.integrity.size != 0;
-}
-
 WIMLIBAPI void
 wimlib_print_available_images(const WIMStruct *w, int image)
 {
@@ -350,34 +333,138 @@ wimlib_print_files(WIMStruct *w, int image)
        return for_image(w, image, image_print_files);
 }
 
-/* Sets the index of the bootable image. */
 WIMLIBAPI int
-wimlib_set_boot_idx(WIMStruct *wim, int boot_idx)
+wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info)
+{
+       memset(info, 0, sizeof(struct wimlib_wim_info));
+       memcpy(info->guid, wim->hdr.guid, WIMLIB_GUID_LEN);
+       info->image_count = wim->hdr.image_count;
+       info->boot_index = wim->hdr.boot_idx;
+       info->wim_version = WIM_VERSION;
+       info->chunk_size = WIM_CHUNK_SIZE;
+       info->part_number = wim->hdr.part_number;
+       info->total_parts = wim->hdr.total_parts;
+       info->compression_type = wim->compression_type;
+       if (wim->wim_info)
+               info->total_bytes = wim->wim_info->total_bytes;
+       else
+               info->total_bytes = 0;
+       info->has_integrity_table = wim->hdr.integrity.offset != 0;
+       info->opened_from_file = (wim->filename != NULL);
+       info->is_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) ||
+                            (wim->hdr.total_parts != 1) ||
+                            (wim->filename && taccess(wim->filename, W_OK));
+       info->has_rpfix = (wim->hdr.flags & WIM_HDR_FLAG_RP_FIX) != 0;
+       info->is_marked_readonly = (wim->hdr.flags & WIM_HDR_FLAG_READONLY) != 0;
+       info->write_in_progress = (wim->hdr.flags & WIM_HDR_FLAG_WRITE_IN_PROGRESS) != 0;
+       info->metadata_only = (wim->hdr.flags & WIM_HDR_FLAG_METADATA_ONLY) != 0;
+       info->resource_only = (wim->hdr.flags & WIM_HDR_FLAG_RESOURCE_ONLY) != 0;
+       info->spanned = (wim->hdr.flags & WIM_HDR_FLAG_SPANNED) != 0;
+       return 0;
+}
+
+
+/* Deprecated */
+WIMLIBAPI int
+wimlib_get_boot_idx(const WIMStruct *wim)
 {
-       int ret;
+       struct wimlib_wim_info info;
 
-       ret = can_modify_wim(wim);
-       if (ret)
-               return ret;
-       if (boot_idx < 0 || boot_idx > wim->hdr.image_count)
-               return WIMLIB_ERR_INVALID_IMAGE;
-       wim->hdr.boot_idx = boot_idx;
-       return 0;
+       wimlib_get_wim_info((WIMStruct*)wim, &info);
+       return info.boot_index;
+}
+
+/* Deprecated */
+WIMLIBAPI int
+wimlib_get_compression_type(const WIMStruct *wim)
+{
+       struct wimlib_wim_info info;
+
+       wimlib_get_wim_info((WIMStruct*)wim, &info);
+       return info.compression_type;
+}
+
+/* Deprecated */
+WIMLIBAPI int
+wimlib_get_num_images(const WIMStruct *wim)
+{
+       struct wimlib_wim_info info;
+
+       wimlib_get_wim_info((WIMStruct*)wim, &info);
+       return info.image_count;
 }
 
+/* Deprecated */
 WIMLIBAPI int
-wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
+wimlib_get_part_number(const WIMStruct *wim, int *total_parts_ret)
 {
+       struct wimlib_wim_info info;
+
+       wimlib_get_wim_info((WIMStruct*)wim, &info);
        if (total_parts_ret)
-               *total_parts_ret = w->hdr.total_parts;
-       return w->hdr.part_number;
+               *total_parts_ret = info.total_parts;
+       return info.part_number;
 }
 
+/* Deprecated */
+WIMLIBAPI bool
+wimlib_has_integrity_table(const WIMStruct *wim)
+{
+       struct wimlib_wim_info info;
+
+       wimlib_get_wim_info((WIMStruct*)wim, &info);
+       return info.has_integrity_table;
+}
+
+WIMLIBAPI int
+wimlib_set_wim_info(WIMStruct *wim, const struct wimlib_wim_info *info, int which)
+{
+       int ret;
+
+       if (which & WIMLIB_CHANGE_READONLY_FLAG) {
+               if (info->is_marked_readonly)
+                       wim->hdr.flags |= WIM_HDR_FLAG_READONLY;
+               else
+                       wim->hdr.flags &= ~WIM_HDR_FLAG_READONLY;
+       }
+
+       if ((which & ~WIMLIB_CHANGE_READONLY_FLAG) == 0)
+               return 0;
+
+       ret = can_modify_wim(wim);
+       if (ret)
+               return ret;
 
+       if (which & WIMLIB_CHANGE_GUID)
+               memcpy(wim->hdr.guid, info->guid, WIM_GID_LEN);
+
+       if (which & WIMLIB_CHANGE_BOOT_INDEX) {
+               if (info->boot_index < 0 || info->boot_index > wim->hdr.image_count)
+               {
+                       ERROR("%u is not 0 or a valid image in the WIM to mark as bootable",
+                             info->boot_index);
+                       return WIMLIB_ERR_INVALID_IMAGE;
+               }
+               wim->hdr.boot_idx = info->boot_index;
+       }
+
+       if (which & WIMLIB_CHANGE_RPFIX_FLAG) {
+               if (info->has_rpfix)
+                       wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
+               else
+                       wim->hdr.flags &= ~WIM_HDR_FLAG_RP_FIX;
+       }
+       return 0;
+}
+
+/* Deprecated */
 WIMLIBAPI int
-wimlib_get_boot_idx(const WIMStruct *w)
+wimlib_set_boot_idx(WIMStruct *wim, int boot_idx)
 {
-       return w->hdr.boot_idx;
+       struct wimlib_wim_info info;
+
+       info.boot_index = boot_idx;
+       return wimlib_set_wim_info(wim, &info, WIMLIB_CHANGE_BOOT_INDEX);
 }
 
 static int
@@ -553,6 +640,8 @@ wimlib_open_wim(const tchar *wim_file, int open_flags,
        WIMStruct *wim;
        int ret;
 
+       wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
+
        ret = WIMLIB_ERR_INVALID_PARAM;
        if (!wim_file || !wim_ret)
                goto out;
@@ -799,6 +888,10 @@ test_locale_ctype_utf8(void)
 WIMLIBAPI int
 wimlib_global_init(int init_flags)
 {
+       static bool already_inited = false;
+
+       if (already_inited)
+               return 0;
        libxml_global_init();
        if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
                wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
@@ -810,6 +903,7 @@ wimlib_global_init(int init_flags)
 #ifdef __WIN32__
        win32_global_init();
 #endif
+       already_inited = true;
        return 0;
 }