]> wimlib.net Git - wimlib/blobdiff - src/wimlib_internal.h
Implement multi-threaded compression
[wimlib] / src / wimlib_internal.h
index e9a10798b273a90f9e732dcf442bc14ef7376e85..9af0f3360255571a07aaf8de40712d0207733a5d 100644 (file)
@@ -2,34 +2,40 @@
  * wimlib_internal.h
  *
  * Internal header for wimlib.
- *
- * wimlib - Library for working with WIM files 
- *
+ */
+
+/*
  * Copyright (C) 2010 Carl Thijssen
  * Copyright (C) 2012 Eric Biggers
  *
- * This library is free software; you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option) any
- * later version.
+ * This file is part of wimlib, a library for working with WIM files.
  *
- * This library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * wimlib is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.
  *
- * You should have received a copy of the GNU Lesser General Public License along
- * with this library; if not, write to the Free Software Foundation, Inc., 59
- * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+ * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
 #ifndef _WIMLIB_INTERNAL_H
 #define _WIMLIB_INTERNAL_H
 
 #include "util.h"
+#include "list.h"
+
+#include <pthread.h>
 
 struct stat;
+struct dentry;
+struct inode;
 
-#define WIM_HASH_SIZE  20
 #define WIM_MAGIC_LEN  8
 #define WIM_GID_LEN    16
 #define WIM_UNUSED_LEN 60
@@ -42,8 +48,8 @@ struct stat;
  * of this size. */
 #define WIM_CHUNK_SIZE 32768
 
-/* Version of the WIM file.  I don't know if there has ever been a different
- * version or not. */
+/* Version of the WIM file.  There is an older version, but we don't support it
+ * yet.  The differences between the versions are undocumented. */
 #define WIM_VERSION 0x10d00
 
 enum wim_integrity_status {
@@ -52,13 +58,6 @@ enum wim_integrity_status {
        WIM_INTEGRITY_NONEXISTENT,
 };
 
-
-#define RINOK(expr) do { \
-       int __ret = (expr); \
-       if (__ret != 0) \
-               return __ret; \
-} while (0) 
-
 /* Metadata for a resource in a WIM file. */
 struct resource_entry {
        /* Size, in bytes, of the resource in the WIM file. */
@@ -77,24 +76,31 @@ struct resource_entry {
 
 /* Flags for the `flags' field of the struct resource_entry structure. */
 
-/* ??? */
+/* I haven't seen this flag used in any of the WIMs I have examined.  I assume
+ * it means that there are no references to the stream, so the space is free.
+ * However, even after deleting files from a WIM mounted with `imagex.exe
+ * /mountrw', I could not see this flag being used.  Either way, we don't
+ * actually use this flag for anything. */
 #define WIM_RESHDR_FLAG_FREE            0x01
 
-/* Indicates that a file resource is a metadata resource. */
+/* Indicates that the stream is a metadata resource for a WIM image. */
 #define WIM_RESHDR_FLAG_METADATA        0x02
 
-/* Indicates that a file resource is compressed. */
+/* Indicates that the stream is compressed. */
 #define WIM_RESHDR_FLAG_COMPRESSED     0x04
 
-/* ??? */
+/* I haven't seen this flag used in any of the WIMs I have examined.  Perhaps it
+ * means that a stream could possibly be split among multiple split WIM parts.
+ * However, `imagex.exe /split' does not seem to create any WIMs like this.
+ * Either way, we don't actually use this flag for anything.  */
 #define WIM_RESHDR_FLAG_SPANNED         0x08
 
 
 /* Header at the very beginning of the WIM file. */
-struct wim_header { 
+struct wim_header {
        /* Identifies the file as WIM file. Must be exactly
         * {'M', 'S', 'W', 'I', 'M', 0, 0, 0}  */
-       //u8  magic[WIM_MAGIC_LEN];     
+       //u8  magic[WIM_MAGIC_LEN];
 
        /* size of WIM header in bytes. */
        //u32 hdr_size;
@@ -112,15 +118,15 @@ struct wim_header {
         * WIM_CHUNK_SIZE.  M$ incorrectly documents this as "the size of the
         * compressed .wim file in bytes".*/
        //u32 chunk_size;
-       
+
        /* A unique identifier for the WIM file. */
        u8  guid[WIM_GID_LEN];
 
        /* Part number of the WIM file in a spanned set. */
-       //u16 part_number;
+       u16 part_number;
 
        /* Total number of parts in a spanned set. */
-       //u16 total_parts;
+       u16 total_parts;
 
        /* Number of images in the WIM file. */
        u32 image_count;
@@ -138,7 +144,7 @@ struct wim_header {
 
        /* The index of the bootable image in the WIM file. If 0, there are no
         * bootable images available. */
-       u32 boot_idx; 
+       u32 boot_idx;
 
        /* The location of the optional integrity table used to verify the
         * integrity WIM.  Zeroed out if there is no integrity table.*/
@@ -148,8 +154,7 @@ struct wim_header {
        //u8 unused[WIM_UNUSED_LEN];
 };
 
-/* Flags for the `flags' field of the struct wim_header. */
-
+/* Flags for the `flags' field of the struct wim_header: */
 
 /* Reserved for future use by M$ */
 #define WIM_HDR_FLAG_RESERVED           0x00000001
@@ -157,26 +162,31 @@ struct wim_header {
 /* Files and metadata in the WIM are compressed. */
 #define WIM_HDR_FLAG_COMPRESSION        0x00000002
 
-/* WIM is read-only. */
+/* WIM is read-only (we ignore this). */
 #define WIM_HDR_FLAG_READONLY           0x00000004
 
 /* Resource data specified by images in this WIM may be contained in a different
- * WIM */
+ * WIM.  Or in other words, this WIM is part of a split WIM.  */
 #define WIM_HDR_FLAG_SPANNED            0x00000008
 
-/* The WIM contains resources only; no filesystem metadata. */
+/* The WIM contains resources only; no filesystem metadata.  We ignore this
+ * flag, as we look for file resources in all the WIMs anyway. */
 #define WIM_HDR_FLAG_RESOURCE_ONLY      0x00000010
 
-/* The WIM contains metadata only. */
+/* The WIM contains metadata only.  We ignore this flag.  Note that all the
+ * metadata resources for a split WIM should be in the first part. */
 #define WIM_HDR_FLAG_METADATA_ONLY      0x00000020
 
-/* Lock field to prevent multiple writers from writing the WIM concurrently. */
-#define WIM_HDR_FLAG_WRITE_IN_PROGRESS  0x00000040 
+/* Lock field to prevent multiple writers from writing the WIM concurrently.  We
+ * ignore this flag. */
+#define WIM_HDR_FLAG_WRITE_IN_PROGRESS  0x00000040
 
-/* Reparse point fixup ??? */
+/* Reparse point fixup ???
+ * This has something to do with absolute targets of reparse points / symbolic
+ * links but I don't know what.  We ignore this flag.  */
 #define WIM_HDR_FLAG_RP_FIX             0x00000080
 
-/* Unknown compression type */
+/* Unused, reserved flag for another compression type */
 #define WIM_HDR_FLAG_COMPRESS_RESERVED  0x00010000
 
 /* Resources within the WIM are compressed using "XPRESS" compression, which is
@@ -187,69 +197,83 @@ struct wim_header {
  * a LZ77-based algorithm. */
 #define WIM_HDR_FLAG_COMPRESS_LZX       0x00040000
 
+#ifdef WITH_NTFS_3G
+struct _ntfs_volume;
+#endif
 
-#if 0
 /* Structure for security data.  Each image in the WIM file has its own security
  * data. */
-typedef struct WIMSecurityData {
+struct wim_security_data {
        /* The total length of the security data, in bytes.  A typical size is
         * 2048 bytes.  If there is no security data, though (as in the WIMs
         * that wimlib writes, currently), it will be 8 bytes. */
        u32 total_length;
 
-       /* The number of security descriptors in the array @descriptors, below. */
-       u32 num_entries;
+       /* The number of security descriptors in the array @descriptors, below.
+        * It is really an unsigned int, but it must fit into an int because the
+        * security ID's are signed.  (Not like you would ever have more than a
+        * few hundred security descriptors anyway). */
+       int32_t num_entries;
 
        /* Array of sizes of the descriptors in the array @descriptors. */
        u64 *sizes;
 
        /* Array of descriptors. */
        u8 **descriptors;
-} WIMSecurityData;
-#endif
+
+       /* keep track of how many WIMs reference this security data (used when
+        * exporting images between WIMs) */
+       u32 refcnt;
+};
+
+struct inode_table;
+
 
 /* Metadata resource for an image. */
 struct image_metadata {
+       /* Pointer to the root dentry for the image. */
+       struct dentry    *root_dentry;
 
-#if 0
-       /* The security data for the image. */
-       WIMSecurityData      security_data;
-#endif
+       /* Pointer to the security data for the image. */
+       struct wim_security_data *security_data;
 
-       /* The root dentry for the image. */
-       struct dentry    *root_dentry;
+       /* A pointer to the lookup table entry for this image's metadata
+        * resource. */
+       struct lookup_table_entry *metadata_lte;
+
+       struct hlist_head inode_list;
 
        /* True if the filesystem of the image has been modified.  If this is
         * the case, the memory for the filesystem is not freed when switching
         * to a different WIM image. */
        bool modified;
 
-       /* A pointer to the lookup table entry for this image's metadata
-        * resource. */
-       struct lookup_table_entry *lookup_table_entry;
 };
 
+#define WIMLIB_RESOURCE_FLAG_RAW               0x1
+#define WIMLIB_RESOURCE_FLAG_MULTITHREADED     0x2
+
 /* The opaque structure exposed to the wimlib API. */
 typedef struct WIMStruct {
-       /* The name of the WIM file that has been opened. */
-       char                *filename;
 
        /* A pointer to the file indicated by @filename, opened for reading. */
-       FILE                *fp;
+       FILE  *fp;
 
-       /* The currently selected image, indexed starting at 1.  If not 0,
-        * subtract 1 from this to get the index of the current image in the
-        * image_metadata array. */
-       int                  current_image;
+       FILE **fp_tab;
+       size_t num_allocated_fps;
+       pthread_mutex_t fp_tab_mutex;
 
-       /* The lookup table for the WIM file. */ 
-       struct lookup_table *lookup_table;
+       /* FILE pointer for the WIM file that is being written. */
+       FILE  *out_fp;
 
-       /* The header of the WIM file. */
-       struct wim_header    hdr;
+       /* The name of the WIM file that has been opened. */
+       char  *filename;
+
+       /* The lookup table for the WIM file. */
+       struct lookup_table *lookup_table;
 
        /* Pointer to the XML data read from the WIM file. */
-       u8                  *xml_data;
+       u8    *xml_data;
 
        /* Information retrieved from the XML data, arranged
         * in an orderly manner. */
@@ -259,22 +283,26 @@ typedef struct WIMStruct {
         * WIM has a image metadata associated with it. */
        struct image_metadata     *image_metadata;
 
-       /* True if files names are to be printed when doing extraction. 
-        * May be used for other things later. */
-       bool   verbose;
-
-       /* The type of links to create when extracting files (hard, symbolic, or
-        * none.) */
-       int    link_type;
-
-       /* Name of the output directory for extraction. */
-       char  *output_dir;
+       /* The header of the WIM file. */
+       struct wim_header    hdr;
 
-       /* Set to true when extracting multiple images */
-       bool   is_multi_image_extraction;
+       /* Temporary flags to use when extracting a WIM image or adding a WIM
+        * image. */
+       union {
+               int extract_flags;
+               int add_flags;
+               int write_flags;
+               bool write_metadata;
+               void *private;
+       };
+#ifdef WITH_NTFS_3G
+       struct _ntfs_volume *ntfs_vol;
+#endif
 
-       /* FILE pointer for the WIM file that is being written. */
-       FILE  *out_fp;
+       /* The currently selected image, indexed starting at 1.  If not 0,
+        * subtract 1 from this to get the index of the current image in the
+        * image_metadata array. */
+       int current_image;
 } WIMStruct;
 
 
@@ -285,22 +313,21 @@ static inline struct dentry *wim_root_dentry(WIMStruct *w)
        return w->image_metadata[w->current_image - 1].root_dentry;
 }
 
-static inline struct dentry **wim_root_dentry_p(WIMStruct *w)
+static inline struct wim_security_data *
+wim_security_data(WIMStruct *w)
 {
-       return &w->image_metadata[w->current_image - 1].root_dentry;
+       return w->image_metadata[w->current_image - 1].security_data;
 }
-
-#if 0
-static inline WIMSecurityData *wim_security_data(WIMStruct *w)
+static inline const struct wim_security_data *
+wim_const_security_data(const WIMStruct *w)
 {
-       return &w->image_metadata[w->current_image - 1].security_data;
+       return w->image_metadata[w->current_image - 1].security_data;
 }
-#endif
 
 static inline struct lookup_table_entry*
 wim_metadata_lookup_table_entry(WIMStruct *w)
 {
-       return w->image_metadata[w->current_image - 1].lookup_table_entry;
+       return w->image_metadata[w->current_image - 1].metadata_lte;
 }
 
 /* Nonzero if a struct resource_entry indicates a compressed resource. */
@@ -309,96 +336,162 @@ static inline int resource_is_compressed(const struct resource_entry *entry)
        return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
 }
 
-static inline struct image_metadata *wim_get_current_image_metadata(WIMStruct *w)
+static inline struct image_metadata *
+wim_get_current_image_metadata(WIMStruct *w)
 {
        return &w->image_metadata[w->current_image - 1];
 }
 
-static inline bool wim_current_image_is_modified(const WIMStruct *w)
-{
-       return w->image_metadata[w->current_image - 1].modified;
-}
+struct pattern_list {
+       const char **pats;
+       size_t num_pats;
+       size_t num_allocated_pats;
+};
+
+struct capture_config {
+       struct pattern_list exclusion_list;
+       struct pattern_list exclusion_exception;
+       struct pattern_list compression_exclusion_list;
+       struct pattern_list alignment_list;
+       char *config_str;
+       char *prefix;
+       size_t prefix_len;
+};
+
+/* hardlink.c */
+
+/* Hash table to find inodes, identified by their inode ID.
+ * */
+struct inode_table {
+       /* Fields for the hash table */
+       struct hlist_head *array;
+       u64 num_entries;
+       u64 capacity;
+
+       /*
+        * Linked list of "extra" inodes.  These may be:
+        *
+        * - inodes with link count 1, which are all allowed to have 0 for their
+        *   inode number, meaning we cannot insert them into the hash table
+        *   before calling assign_inode_numbers().
+         *
+        * - Groups we create ourselves by splitting a nominal inode due to
+        *   inconsistencies in the dentries.  These inodes will share a inode
+        *   ID with some other inode until assign_inode_numbers() is called.
+        */
+       struct hlist_head extra_inodes;
+};
 
-/* Prints a hash code field. */
-static inline void print_hash(const u8 hash[])
+int init_inode_table(struct inode_table *table, size_t capacity);
+static inline void destroy_inode_table(struct inode_table *table)
 {
-       print_byte_field(hash, WIM_HASH_SIZE);
+       FREE(table->array);
 }
+int inode_table_insert(struct dentry *dentry, void *__table);
+u64 assign_inode_numbers(struct hlist_head *inode_list);
+int fix_inodes(struct inode_table *table, struct hlist_head *inode_list);
 
 
 /* header.c */
-extern int read_header(FILE *fp, struct wim_header *hdr);
+extern int read_header(FILE *fp, struct wim_header *hdr, int split_ok);
 extern int write_header(const struct wim_header *hdr, FILE *out);
 extern int init_header(struct wim_header *hdr, int ctype);
 
 /* integrity.c */
-extern int write_integrity_table(FILE *out, u64 end_header_offset, 
+extern int write_integrity_table(FILE *out, u64 end_header_offset,
                                 u64 end_lookup_table_offset,
                                 int show_progress);
 extern int check_wim_integrity(WIMStruct *w, int show_progress, int *status);
 
+/* join.c */
+
+extern int new_joined_lookup_table(WIMStruct *w,
+                                  WIMStruct **additional_swms,
+                                  unsigned num_additional_swms,
+                                  struct lookup_table **table_ret);
+
+extern int verify_swm_set(WIMStruct *w,
+                         WIMStruct **additional_swms,
+                         unsigned num_additional_swms);
+/* modify.c */
+extern void destroy_image_metadata(struct image_metadata *imd,
+                                  struct lookup_table *lt);
+extern bool exclude_path(const char *path,
+                        const struct capture_config *config,
+                        bool exclude_prefix);
+extern int do_add_image(WIMStruct *w, const char *dir, const char *name,
+                       const char *config_str, size_t config_len,
+                       int flags,
+                       int (*capture_tree)(struct dentry **, const char *,
+                                    struct lookup_table *,
+                                    struct wim_security_data *,
+                                    const struct capture_config *,
+                                    int, void *),
+                       void *extra_arg);
 
 /* resource.c */
 extern const u8 *get_resource_entry(const u8 *p, struct resource_entry *entry);
 extern u8 *put_resource_entry(u8 *p, const struct resource_entry *entry);
 
-extern int read_resource(FILE *fp, u64 resource_size, 
-                        u64 resource_original_size,
-                        u64 resource_offset, int resource_ctype, u64 len, 
-                        u64 offset, void *contents_ret);
+extern int read_uncompressed_resource(FILE *fp, u64 offset, u64 size, u8 buf[]);
 
-extern int read_uncompressed_resource(FILE *fp, u64 offset, u64 len, 
-                                       u8 contents_ret[]);
+extern int read_wim_resource(const struct lookup_table_entry *lte, u8 buf[],
+                            size_t size, u64 offset, int flags);
 
+extern int read_full_wim_resource(const struct lookup_table_entry *lte,
+                                 u8 buf[], int flags);
 
-extern int extract_resource_to_fd(WIMStruct *w, 
-                                 const struct resource_entry *entry, 
-                                 int fd, 
-                                 u64 size);
+extern int write_wim_resource(struct lookup_table_entry *lte,
+                             FILE *out_fp, int out_ctype,
+                             struct resource_entry *out_res_entry,
+                             int flags);
 
-extern int extract_full_resource_to_fd(WIMStruct *w, 
-                                      const struct resource_entry *entry, 
-                                      int fd);
+extern int extract_wim_resource_to_fd(const struct lookup_table_entry *lte,
+                                     int fd, u64 size);
 
-extern int read_metadata_resource(FILE *fp, const struct resource_entry *metadata,
-                             int wim_ctype, /* WIMSecurityData *sd, */
-                             struct dentry **root_dentry_p);
 
-extern int resource_compression_type(int wim_ctype, int reshdr_flags);
+extern int extract_full_wim_resource_to_fd(const struct lookup_table_entry *lte,
+                                          int fd);
 
-static inline int read_full_resource(FILE *fp, u64 resource_size, 
-                               u64 resource_original_size,
-                               u64 resource_offset, 
-                               int resource_ctype, void *contents_ret)
-{
-       return read_resource(fp, resource_size, resource_original_size, 
-                               resource_offset, resource_ctype,
-                               resource_original_size, 0, contents_ret);
-}
+extern int read_metadata_resource(WIMStruct *w,
+                                 struct image_metadata *image_metadata);
 
-#if 0
-/* security.c */
-bool read_security_data(const u8 metadata_resource[], 
-               u64 metadata_resource_len, WIMSecurityData *sd);
 
-void print_security_data(const WIMSecurityData *sd);
-u8 *write_security_data(const WIMSecurityData *sd, u8 *p);
-void init_security_data(WIMSecurityData *sd);
-void destroy_security_data(WIMSecurityData *sd);
-#endif
+extern int write_dentry_resources(struct dentry *dentry, void *wim_p);
+extern int copy_resource(struct lookup_table_entry *lte, void *w);
+extern int write_metadata_resource(WIMStruct *w);
+
+
+/* security.c */
+int read_security_data(const u8 metadata_resource[],
+               u64 metadata_resource_len, struct wim_security_data **sd_p);
+
+void print_security_data(const struct wim_security_data *sd);
+u8 *write_security_data(const struct wim_security_data *sd, u8 *p);
+void free_security_data(struct wim_security_data *sd);
+
+/* symlink.c */
+ssize_t inode_readlink(const struct inode *inode, char *buf, size_t buf_len,
+                       const WIMStruct *w, int read_resource_flags);
+extern void *make_symlink_reparse_data_buf(const char *symlink_target,
+                                          size_t *len_ret);
+extern int inode_set_symlink(struct inode *inode,
+                            const char *target,
+                            struct lookup_table *lookup_table,
+                            struct lookup_table_entry **lte_ret);
 
 /* wim.c */
-extern int wimlib_select_image(WIMStruct *w, int image);
-extern void wimlib_destroy(WIMStruct *w);
+extern WIMStruct *new_wim_struct();
+extern int select_wim_image(WIMStruct *w, int image);
 extern int wim_hdr_flags_compression_type(int wim_hdr_flags);
-extern int wim_resource_compression_type(const WIMStruct *w, 
-                                        const struct resource_entry *entry);
 extern int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *));
 
+/* Internal use only */
+#define WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE 0x80000000
+
 /* write.c */
-extern int write_resource_from_memory(const u8 resource[], int out_ctype,
-                                     u64 resource_original_size, FILE *out,
-                                     u64 *resource_size_ret);
+extern int begin_write(WIMStruct *w, const char *path, int write_flags);
+extern int finish_write(WIMStruct *w, int image, int write_flags);
 
 
 #include "wimlib.h"