]> wimlib.net Git - wimlib/blobdiff - src/lookup_table.h
Add more comments
[wimlib] / src / lookup_table.h
index 8de9f075683aa0934bb3271e1d7170f9c4f9035b..83796aae2d8bca57cf4566de9467755444da9cfa 100644 (file)
 #define LOOKUP_FLAG_ADS_OK             0x00000001
 #define LOOKUP_FLAG_DIRECTORY_OK       0x00000002
 
+#ifdef __WIN32__
+#include <windef.h>
+#endif
+
 
 /* The lookup table of a WIM file maps SHA1 message digests to streams of data.
  * Here, the in-memory structure is implemented as a hash table.
@@ -28,14 +32,15 @@ struct wim_lookup_table {
        struct hlist_head *array;
        u64 num_entries;
        u64 capacity;
+       struct list_head *unhashed_streams;
 };
 
 #ifdef WITH_NTFS_3G
 struct ntfs_location {
-       char *path_utf8;
-       char *stream_name_utf16;
-       u16 stream_name_utf16_num_chars;
-       struct _ntfs_volume **ntfs_vol_p;
+       tchar *path;
+       utf16lechar *stream_name;
+       u16 stream_name_nchars;
+       struct _ntfs_volume *ntfs_vol;
        bool is_reparse_point;
 };
 #endif
@@ -64,19 +69,35 @@ enum resource_location {
         * */
        RESOURCE_IN_FILE_ON_DISK,
 
-       /* The stream resource is located in an external file in the staging
-        * directory for a read-write mount.  */
-       RESOURCE_IN_STAGING_FILE,
-
        /* The stream resource is directly attached in an in-memory buffer
         * pointed to by @attached_buffer. */
        RESOURCE_IN_ATTACHED_BUFFER,
 
+#ifdef WITH_FUSE
+       /* The stream resource is located in an external file in the staging
+        * directory for a read-write mount.  */
+       RESOURCE_IN_STAGING_FILE,
+#endif
+
+#ifdef WITH_NTFS_3G
        /* The stream resource is located in an NTFS volume.  It is identified
         * by volume, filename, data stream name, and by whether it is a reparse
         * point or not. @ntfs_loc points to a structure containing this
         * information. */
        RESOURCE_IN_NTFS_VOLUME,
+#endif
+
+#ifdef __WIN32__
+       /* Resource must be accessed using Win32 API (may be a named data
+        * stream) */
+       RESOURCE_WIN32,
+
+       /* Windows only: the file is on disk in the file named @file_on_disk,
+        * but the file is encrypted and must be read using special functions.
+        * */
+       RESOURCE_WIN32_ENCRYPTED,
+#endif
+
 };
 
 /*
@@ -114,8 +135,14 @@ struct wim_lookup_table_entry {
         */
        u16 part_number;
 
-       /* See enum resource_location above */
-       u16 resource_location;
+       /* One of the `enum resource_location' values documented above. */
+       u16 resource_location : 5;
+
+       /* 1 if this stream is a unique size (only set while writing streams). */
+       u8 unique_size : 1;
+
+       /* 1 if this stream had a SHA1-message digest calculated for it yet? */
+       u8 unhashed : 1;
 
        /* (On-disk field)
         * Number of times this lookup table entry is referenced by dentries.
@@ -135,57 +162,57 @@ struct wim_lookup_table_entry {
                 * the full 20 byte hash just to insert the entry in a hash
                 * table. */
                size_t hash_short;
-       };
 
-       union {
-               #ifdef WITH_FUSE
-               u16 num_opened_fds;
-               #endif
-
-               /* This field is used for the special hardlink or symlink image
-                * extraction mode.   In these mode, all identical files are linked
-                * together, and @extracted_file will be set to the filename of the
-                * first extracted file containing this stream.  */
-               char *extracted_file;
+               /* Unhashed entries only (unhashed == 1): these variables make
+                * it possible to find the pointer to this 'struct
+                * wim_lookup_table_entry' contained in either 'struct
+                * wim_ads_entry' or 'struct wim_inode'.  There can be at most 1
+                * such pointer, as we can only join duplicate streams after
+                * they have been hashed.  */
+               struct {
+                       struct wim_inode *back_inode;
+                       u32 back_stream_id;
+               };
        };
 
+       /* When a WIM file is written, out_refcnt starts at 0 and is incremented
+        * whenever the stream pointed to by this lookup table entry needs to be
+        * written.  The stream only need to be written when out_refcnt is
+        * nonzero, since otherwise it is not referenced by any dentries. */
+       u32 out_refcnt;
+
        /* Pointers to somewhere where the stream is actually located.  See the
         * comments for the @resource_location field above. */
        union {
                WIMStruct *wim;
-               char *file_on_disk;
-               char *staging_file_name;
-               u8 *attached_buffer;
+               tchar *file_on_disk;
+               void *attached_buffer;
+       #ifdef WITH_FUSE
+               tchar *staging_file_name;
+       #endif
        #ifdef WITH_NTFS_3G
                struct ntfs_location *ntfs_loc;
        #endif
        };
-       union {
-               /* @file_on_disk_fp and @attr are both used to cache file/stream
-                * handles so we don't have re-open them on every read */
 
-               /* Valid iff resource_location == RESOURCE_IN_FILE_ON_DISK */
-               FILE *file_on_disk_fp;
-       #ifdef WITH_NTFS_3G
-               /* Valid iff resource_location == RESOURCE_IN_NTFS_VOLUME */
-               struct _ntfs_attr *attr;
+       /* Actual reference count to this stream (only used while
+        * verifying an image). */
+       u32 real_refcnt;
+
+       union {
+       #ifdef WITH_FUSE
+               /* Number of times this stream has been opened (used only during
+                * mounting) */
+               u16 num_opened_fds;
        #endif
 
-               /* Pointer to inode that contains the opened file descriptors to
-                * this stream (valid iff resource_location ==
-                * RESOURCE_IN_STAGING_FILE) */
-               struct wim_inode *lte_inode;
+               /* This field is used for the special hardlink or symlink image
+                * extraction mode.   In these mode, all identical files are linked
+                * together, and @extracted_file will be set to the filename of the
+                * first extracted file containing this stream.  */
+               tchar *extracted_file;
        };
 
-       /* When a WIM file is written, out_refcnt starts at 0 and is incremented
-        * whenever the file resource pointed to by this lookup table entry
-        * needs to be written.  The file resource only need to be written when
-        * out_refcnt is nonzero, since otherwise it is not referenced by any
-        * dentries. */
-       u32 out_refcnt;
-
-       u32 real_refcnt;
-
        union {
                /* When a WIM file is written, @output_resource_entry is filled
                 * in with the resource entry for the output WIM.  This will not
@@ -198,15 +225,21 @@ struct wim_lookup_table_entry {
 
                struct list_head msg_list;
                struct list_head inode_list;
+
+               struct {
+                       struct hlist_node hash_list_2;
+
+                       struct list_head write_streams_list;
+               };
        };
 
-       /* List of lookup table entries that correspond to streams that have
-        * been extracted to the staging directory when modifying a read-write
-        * mounted WIM.
-        *
-        * This field is also used to make other lists of lookup table entries.
-        * */
-       struct list_head staging_list;
+       /* Temporary list fields */
+       union {
+               struct list_head unhashed_list;
+               struct list_head swm_stream_list;
+               struct list_head extraction_list;
+               struct list_head export_stream_list;
+       };
 };
 
 static inline u64
@@ -240,6 +273,19 @@ wim_resource_compression_type(const struct wim_lookup_table_entry *lte)
        return wimlib_get_compression_type(lte->wim);
 }
 
+static inline bool
+lte_filename_valid(const struct wim_lookup_table_entry *lte)
+{
+       return lte->resource_location == RESOURCE_IN_FILE_ON_DISK
+       #ifdef __WIN32__
+               || lte->resource_location == RESOURCE_WIN32
+               || lte->resource_location == RESOURCE_WIN32_ENCRYPTED
+       #endif
+       #ifdef WITH_FUSE
+               || lte->resource_location == RESOURCE_IN_STAGING_FILE
+       #endif
+               ;
+}
 
 extern struct wim_lookup_table *
 new_lookup_table(size_t capacity);
@@ -248,8 +294,8 @@ extern int
 read_lookup_table(WIMStruct *w);
 
 extern int
-write_lookup_table(struct wim_lookup_table *table, FILE *out,
-                  struct resource_entry *out_res_entry);
+write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry);
+
 extern void
 free_lookup_table(struct wim_lookup_table *table);
 
@@ -260,7 +306,9 @@ lookup_table_insert(struct wim_lookup_table *table, struct wim_lookup_table_entr
 static inline void
 lookup_table_unlink(struct wim_lookup_table *table, struct wim_lookup_table_entry *lte)
 {
+       wimlib_assert(!lte->unhashed);
        hlist_del(&lte->hash_list);
+       wimlib_assert(table->num_entries != 0);
        table->num_entries--;
 }
 
@@ -271,7 +319,8 @@ extern struct wim_lookup_table_entry *
 clone_lookup_table_entry(const struct wim_lookup_table_entry *lte);
 
 extern void
-print_lookup_table_entry(const struct wim_lookup_table_entry *entry);
+print_lookup_table_entry(const struct wim_lookup_table_entry *entry,
+                        FILE *out);
 
 extern void
 free_lookup_table_entry(struct wim_lookup_table_entry *lte);
@@ -281,11 +330,20 @@ for_lookup_table_entry(struct wim_lookup_table *table,
                       int (*visitor)(struct wim_lookup_table_entry *, void *),
                       void *arg);
 
+extern int
+cmp_streams_by_wim_position(const void *p1, const void *p2);
+
+extern int
+for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
+                                 int (*visitor)(struct wim_lookup_table_entry *,
+                                                void *),
+                                 void *arg);
+
 extern struct wim_lookup_table_entry *
 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[]);
 
 extern int
-lookup_resource(WIMStruct *w, const char *path,
+lookup_resource(WIMStruct *w, const tchar *path,
                int lookup_flags, struct wim_dentry **dentry_ret,
                struct wim_lookup_table_entry **lte_ret, u16 *stream_idx_ret);
 
@@ -315,13 +373,6 @@ inode_unresolve_ltes(struct wim_inode *inode);
 extern int
 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *__out);
 
-static inline struct resource_entry*
-wim_metadata_resource_entry(WIMStruct *w)
-{
-       return &w->image_metadata[
-                       w->current_image - 1].metadata_lte->resource_entry;
-}
-
 static inline struct wim_lookup_table_entry *
 inode_stream_lte_resolved(const struct wim_inode *inode, unsigned stream_idx)
 {
@@ -393,13 +444,13 @@ inode_stream_hash(const struct wim_inode *inode, unsigned stream_idx)
 }
 
 static inline u16
-inode_stream_name_len(const struct wim_inode *inode, unsigned stream_idx)
+inode_stream_name_nbytes(const struct wim_inode *inode, unsigned stream_idx)
 {
        wimlib_assert(stream_idx <= inode->i_num_ads);
        if (stream_idx == 0)
                return 0;
        else
-               return inode->i_ads_entries[stream_idx - 1].stream_name_len;
+               return inode->i_ads_entries[stream_idx - 1].stream_name_nbytes;
 }
 
 static inline struct wim_lookup_table_entry *
@@ -407,7 +458,7 @@ inode_unnamed_lte_resolved(const struct wim_inode *inode)
 {
        wimlib_assert(inode->i_resolved);
        for (unsigned i = 0; i <= inode->i_num_ads; i++) {
-               if (inode_stream_name_len(inode, i) == 0 &&
+               if (inode_stream_name_nbytes(inode, i) == 0 &&
                    !is_zero_hash(inode_stream_hash_resolved(inode, i)))
                {
                        return inode_stream_lte_resolved(inode, i);
@@ -422,7 +473,7 @@ inode_unnamed_lte_unresolved(const struct wim_inode *inode,
 {
        wimlib_assert(!inode->i_resolved);
        for (unsigned i = 0; i <= inode->i_num_ads; i++) {
-               if (inode_stream_name_len(inode, i) == 0 &&
+               if (inode_stream_name_nbytes(inode, i) == 0 &&
                    !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
                {
                        return inode_stream_lte_unresolved(inode, i, table);
@@ -437,4 +488,25 @@ inode_unnamed_lte(const struct wim_inode *inode, const struct wim_lookup_table *
 extern u64
 lookup_table_total_stream_size(struct wim_lookup_table *table);
 
+
+static inline void
+lookup_table_insert_unhashed(struct wim_lookup_table *table,
+                            struct wim_lookup_table_entry *lte,
+                            struct wim_inode *back_inode,
+                            u32 back_stream_id)
+{
+       lte->unhashed = 1;
+       lte->back_inode = back_inode;
+       lte->back_stream_id = back_stream_id;
+       list_add_tail(&lte->unhashed_list, table->unhashed_streams);
+}
+
+extern int
+hash_unhashed_stream(struct wim_lookup_table_entry *lte,
+                    struct wim_lookup_table *lookup_table,
+                    struct wim_lookup_table_entry **lte_ret);
+
+extern struct wim_lookup_table_entry **
+retrieve_lte_pointer(struct wim_lookup_table_entry *lte);
+
 #endif