]> wimlib.net Git - wimlib/blobdiff - src/dentry.c
clone_dentry(): Set name fields to NULL
[wimlib] / src / dentry.c
index d2ce4e8c3b437ecae7b6a888f91c56a11335cda5..68f9c57afa9df926b1856ccb79b825b1ccd36f21 100644 (file)
 #include <unistd.h>
 #include <sys/stat.h>
 
+/*
+ * Returns true if @dentry has the UTF-8 file name @name that has length
+ * @name_len.
+ */
+static bool dentry_has_name(const struct dentry *dentry, const char *name, 
+                           size_t name_len)
+{
+       if (dentry->file_name_utf8_len != name_len)
+               return false;
+       return memcmp(dentry->file_name_utf8, name, name_len) == 0;
+}
+
 /* Real length of a dentry, including the alternate data stream entries, which
  * are not included in the dentry->length field... */
 u64 dentry_total_length(const struct dentry *dentry)
 {
-       u64 length = dentry->length;
+       u64 length = (dentry->length + 7) & ~7;
        for (u16 i = 0 ; i < dentry->num_ads; i++)
                length += ads_entry_length(&dentry->ads_entries[i]);
-
-       /* Round to 8 byte boundary. */
-       return (length + 7) & ~7;
+       return length;
 }
 
 /* Transfers file attributes from a `stat' buffer to a struct dentry. */
 void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
 {
-       if (S_ISDIR(stbuf->st_mode))
+       if (S_ISLNK(stbuf->st_mode)) {
+               dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
+               dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
+       } else if (S_ISDIR(stbuf->st_mode)) {
                dentry->attributes = FILE_ATTRIBUTE_DIRECTORY;
-       else
+       } else {
                dentry->attributes = FILE_ATTRIBUTE_NORMAL;
+       }
+       if (sizeof(ino_t) >= 8)
+               dentry->hard_link = (u64)stbuf->st_ino;
+       else
+               dentry->hard_link = (u64)stbuf->st_ino |
+                                  ((u64)stbuf->st_dev << (sizeof(ino_t) * 8));
 }
 
 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
@@ -66,23 +85,21 @@ void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
 {
        struct lookup_table_entry *lte;
 
-       if (dentry_is_directory(dentry))
+       if (dentry_is_symlink(dentry))
+               stbuf->st_mode = S_IFLNK | 0777;
+       else if (dentry_is_directory(dentry))
                stbuf->st_mode = S_IFDIR | 0755;
        else
                stbuf->st_mode = S_IFREG | 0644;
 
-       if (table)
-               lte = lookup_resource(table, dentry->hash);
-       else
-               lte = NULL;
-
-       if (lte) {
-               stbuf->st_nlink = lte->refcnt;
+       /* Use the size of the unnamed (default) file stream. */
+       if (table && (lte = __lookup_resource(table, dentry_hash(dentry))))
                stbuf->st_size = lte->resource_entry.original_size;
-       } else {
-               stbuf->st_nlink = 1;
+       else
                stbuf->st_size = 0;
-       }
+
+       stbuf->st_nlink   = dentry_link_group_size(dentry);
+       stbuf->st_ino     = dentry->hard_link;
        stbuf->st_uid     = getuid();
        stbuf->st_gid     = getgid();
        stbuf->st_atime   = ms_timestamp_to_unix(dentry->last_access_time);
@@ -100,6 +117,55 @@ void dentry_update_all_timestamps(struct dentry *dentry)
        dentry->last_write_time  = now;
 }
 
+struct ads_entry *dentry_get_ads_entry(struct dentry *dentry,
+                                      const char *stream_name)
+{
+       size_t stream_name_len = strlen(stream_name);
+       if (!stream_name)
+               return NULL;
+       for (u16 i = 0; i < dentry->num_ads; i++)
+               if (ads_entry_has_name(&dentry->ads_entries[i],
+                                      stream_name, stream_name_len))
+                       return &dentry->ads_entries[i];
+       return NULL;
+}
+
+/* Add an alternate stream entry to a dentry and return a pointer to it, or NULL
+ * on failure. */
+struct ads_entry *dentry_add_ads(struct dentry *dentry, const char *stream_name)
+{
+       u16 num_ads = dentry->num_ads + 1;
+       struct ads_entry *ads_entries;
+       struct ads_entry *new_entry;
+       if (num_ads == 0xffff)
+               return NULL;
+       ads_entries = MALLOC(num_ads * sizeof(struct ads_entry));
+       if (!ads_entries)
+               return NULL;
+
+       new_entry = &ads_entries[num_ads - 1];
+       if (change_ads_name(new_entry, stream_name) != 0) {
+               FREE(ads_entries);
+               return NULL;
+       }
+
+       memcpy(ads_entries, dentry->ads_entries,
+              (num_ads - 1) * sizeof(struct ads_entry));
+       FREE(dentry->ads_entries);
+       dentry->ads_entries = ads_entries;
+       dentry->num_ads = num_ads;
+       return memset(new_entry, 0, sizeof(struct ads_entry));
+}
+
+void dentry_remove_ads(struct dentry *dentry, struct ads_entry *sentry)
+{
+       destroy_ads_entry(sentry);
+       memcpy(sentry, sentry + 1,
+              (dentry->num_ads - (sentry - dentry->ads_entries))
+                * sizeof(struct ads_entry));
+       dentry->num_ads--;
+}
+
 /* 
  * Calls a function on all directory entries in a directory tree.  It is called
  * on a parent before its children.
@@ -360,11 +426,10 @@ int print_dentry(struct dentry *dentry, void *lookup_table)
        printf("Subdir offset     = %"PRIu64"\n", dentry->subdir_offset);
        /*printf("Unused1           = 0x%"PRIu64"\n", dentry->unused1);*/
        /*printf("Unused2           = %"PRIu64"\n", dentry->unused2);*/
-       printf("Creation Time     = %"PRIu64"\n", dentry->creation_time);
-       printf("Last Access Time  = %"PRIu64"\n", dentry->last_access_time);
-       printf("Last Write Time   = %"PRIu64"\n", dentry->last_write_time);
        printf("Creation Time     = 0x%"PRIx64"\n", dentry->creation_time);
-       printf("Hash              = "); 
+       printf("Last Access Time  = 0x%"PRIx64"\n", dentry->last_access_time);
+       printf("Last Write Time   = 0x%"PRIx64"\n", dentry->last_write_time);
+       printf("Hash              = 0x"); 
        print_hash(dentry->hash); 
        putchar('\n');
        printf("Reparse Tag       = 0x%"PRIx32"\n", dentry->reparse_tag);
@@ -381,23 +446,25 @@ int print_dentry(struct dentry *dentry, void *lookup_table)
        puts("\"");
        printf("Short Name Length = %hu\n", dentry->short_name_len);
        printf("Full Path (UTF-8) = \"%s\"\n", dentry->full_path_utf8);
-       if (lookup_table) {
-               lte = lookup_resource(lookup_table, dentry->hash);
-               if (lte)
-                       print_lookup_table_entry(lte, NULL);
-               else
-                       putchar('\n');
-       } else {
+       if (lookup_table && (lte = __lookup_resource(lookup_table, dentry->hash)))
+               print_lookup_table_entry(lte, NULL);
+       else
                putchar('\n');
-       }
        for (u16 i = 0; i < dentry->num_ads; i++) {
                printf("[Alternate Stream Entry %u]\n", i);
                printf("Name = \"%s\"\n", dentry->ads_entries[i].stream_name_utf8);
-               lte = lookup_resource(lookup_table, dentry->ads_entries[i].hash);
-               if (lte)
+               printf("Name Length (UTF-16) = %u\n",
+                               dentry->ads_entries[i].stream_name_len);
+               printf("Hash              = 0x"); 
+               print_hash(dentry->ads_entries[i].hash); 
+               if (lookup_table &&
+                    (lte = __lookup_resource(lookup_table,
+                                             dentry->ads_entries[i].hash)))
+               {
                        print_lookup_table_entry(lte, NULL);
-               else
+               } else {
                        putchar('\n');
+               }
        }
        return 0;
 }
@@ -421,19 +488,30 @@ struct dentry *new_dentry(const char *name)
        
        dentry = MALLOC(sizeof(struct dentry));
        if (!dentry)
-               return NULL;
+               goto err;
 
        dentry_common_init(dentry);
-       if (change_dentry_name(dentry, name) != 0) {
-               FREE(dentry);
-               return NULL;
-       }
+       if (change_dentry_name(dentry, name) != 0)
+               goto err;
 
        dentry_update_all_timestamps(dentry);
        dentry->next   = dentry;
        dentry->prev   = dentry;
        dentry->parent = dentry;
        return dentry;
+err:
+       FREE(dentry);
+       ERROR("Failed to allocate new dentry");
+       return NULL;
+}
+
+void dentry_free_ads_entries(struct dentry *dentry)
+{
+       for (u16 i = 0; i < dentry->num_ads; i++)
+               destroy_ads_entry(&dentry->ads_entries[i]);
+       FREE(dentry->ads_entries);
+       dentry->ads_entries = NULL;
+       dentry->num_ads = 0;
 }
 
 
@@ -443,9 +521,32 @@ void free_dentry(struct dentry *dentry)
        FREE(dentry->file_name_utf8);
        FREE(dentry->short_name);
        FREE(dentry->full_path_utf8);
+       dentry_free_ads_entries(dentry);
        FREE(dentry);
 }
 
+/* clones a dentry.
+ *
+ * Beware:
+ *     - memory for file names is not cloned
+ *     - next, prev, and children pointers and not touched
+ *     - stream entries are not cloned.
+ */
+struct dentry *clone_dentry(struct dentry *old)
+{
+       struct dentry *new = MALLOC(sizeof(struct dentry));
+       if (!new)
+               return NULL;
+       memcpy(new, old, sizeof(struct dentry));
+       new->file_name          = NULL;
+       new->file_name_len      = 0;
+       new->file_name_utf8     = NULL;
+       new->file_name_utf8_len = 0;
+       new->short_name         = NULL;
+       new->short_name_len     = 0;
+       return new;
+}
+
 /* Arguments for do_free_dentry(). */
 struct free_dentry_args {
        struct lookup_table *lookup_table;
@@ -541,43 +642,67 @@ void unlink_dentry(struct dentry *dentry)
 static inline void recalculate_dentry_size(struct dentry *dentry)
 {
        dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len + 
-                        2 + dentry->short_name_len + 2;
+                        2 + dentry->short_name_len;
        /* Must be multiple of 8. */
        dentry->length = (dentry->length + 7) & ~7;
 }
 
-/* Changes the name of a dentry to @new_name.  Only changes the file_name and
- * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
- * full_path_utf8 fields.  Also recalculates its length. */
-int change_dentry_name(struct dentry *dentry, const char *new_name)
+static int do_name_change(char **file_name_ret,
+                         char **file_name_utf8_ret,
+                         u16 *file_name_len_ret,
+                         u16 *file_name_utf8_len_ret,
+                         const char *new_name)
 {
        size_t utf8_len;
        size_t utf16_len;
-
-       FREE(dentry->file_name);
+       char *file_name, *file_name_utf8;
 
        utf8_len = strlen(new_name);
 
-       dentry->file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
+       file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
 
-       if (!dentry->file_name)
+       if (!file_name)
                return WIMLIB_ERR_NOMEM;
 
-       FREE(dentry->file_name_utf8);
-       dentry->file_name_utf8 = MALLOC(utf8_len + 1);
-       if (!dentry->file_name_utf8) {
-               FREE(dentry->file_name);
-               dentry->file_name = NULL;
+       file_name_utf8 = MALLOC(utf8_len + 1);
+       if (!file_name_utf8) {
+               FREE(file_name);
                return WIMLIB_ERR_NOMEM;
        }
-
-       dentry->file_name_len = utf16_len;
-       dentry->file_name_utf8_len = utf8_len;
-       memcpy(dentry->file_name_utf8, new_name, utf8_len + 1);
-       recalculate_dentry_size(dentry);
+       memcpy(file_name_utf8, new_name, utf8_len + 1);
+
+       FREE(*file_name_ret);
+       FREE(*file_name_utf8_ret);
+       *file_name_ret          = file_name;
+       *file_name_utf8_ret     = file_name_utf8;
+       *file_name_len_ret      = utf16_len;
+       *file_name_utf8_len_ret = utf8_len;
        return 0;
 }
 
+/* Changes the name of a dentry to @new_name.  Only changes the file_name and
+ * file_name_utf8 fields; does not change the short_name, short_name_utf8, or
+ * full_path_utf8 fields.  Also recalculates its length. */
+int change_dentry_name(struct dentry *dentry, const char *new_name)
+{
+       int ret;
+
+       ret = do_name_change(&dentry->file_name, &dentry->file_name_utf8,
+                            &dentry->file_name_len, &dentry->file_name_utf8_len,
+                            new_name);
+       if (ret == 0)
+               recalculate_dentry_size(dentry);
+       return ret;
+}
+
+int change_ads_name(struct ads_entry *entry, const char *new_name)
+{
+       return do_name_change(&entry->stream_name, &entry->stream_name_utf8,
+                             &entry->stream_name_len,
+                             &entry->stream_name_utf8_len,
+                             new_name);
+}
+
 /* Parameters for calculate_dentry_statistics(). */
 struct image_statistics {
        struct lookup_table *lookup_table;
@@ -591,21 +716,31 @@ static int calculate_dentry_statistics(struct dentry *dentry, void *arg)
 {
        struct image_statistics *stats;
        struct lookup_table_entry *lte; 
+       u16 i;
        
        stats = arg;
-       lte = lookup_resource(stats->lookup_table, dentry->hash);
 
        if (dentry_is_directory(dentry) && !dentry_is_root(dentry))
                ++*stats->dir_count;
        else
                ++*stats->file_count;
 
-       if (lte) {
-               u64 size = lte->resource_entry.original_size;
-               *stats->total_bytes += size;
-               if (++lte->out_refcnt == 1)
-                       *stats->hard_link_bytes += size;
+       lte = __lookup_resource(stats->lookup_table, dentry->hash);
+       i = 0;
+       while (1) {
+               if (lte) {
+                       u64 size = lte->resource_entry.original_size;
+                       *stats->total_bytes += size;
+                       if (++lte->out_refcnt == 1)
+                               *stats->hard_link_bytes += size;
+               }
+               if (i == dentry->num_ads)
+                       break;
+               lte = __lookup_resource(stats->lookup_table,
+                                       dentry->ads_entries[i].hash);
+               i++;
        }
+
        return 0;
 }
 
@@ -655,8 +790,6 @@ static int read_ads_entries(const u8 *p, struct dentry *dentry,
                        goto out_free_ads_entries;
                }
                remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
-               /*print_string(p + 40, 10);*/
-               /*print_byte_field(p, 50);*/
 
                p = get_u64(p, &length); /* ADS entry length */
 
@@ -691,7 +824,7 @@ static int read_ads_entries(const u8 *p, struct dentry *dentry,
                cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
                                                            cur_entry->stream_name_len,
                                                            &utf8_len);
-               cur_entry->stream_name_len_utf8 = utf8_len;
+               cur_entry->stream_name_utf8_len = utf8_len;
 
                if (!cur_entry->stream_name_utf8) {
                        ret = WIMLIB_ERR_NOMEM;
@@ -789,13 +922,9 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
         */
        if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
                /* ??? */
-               u32 u1, u2;
-               p = get_u32(p, &u1);
-               /*p += 4;*/
+               p += 4;
                p = get_u32(p, &dentry->reparse_tag);
-               p = get_u32(p, &u2);
-               /*p += 4;*/
-               dentry->hard_link = (u64)(u1) | ((u64)(u2) << 32);
+               p += 4;
        } else {
                p = get_u32(p, &dentry->reparse_tag);
                p = get_u64(p, &dentry->hard_link);
@@ -891,7 +1020,15 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len,
 #endif
 
        if (dentry->num_ads != 0) {
-               ret = read_ads_entries(p, dentry,
+               calculated_size = (calculated_size + 7) & ~7;
+               if (calculated_size > metadata_resource_len - offset) {
+                       ERROR("Not enough space in metadata resource for "
+                             "alternate stream entries");
+                       ret = WIMLIB_ERR_INVALID_DENTRY;
+                       goto out_free_short_name;
+               }
+               ret = read_ads_entries(&metadata_resource[offset + calculated_size],
+                                      dentry,
                                       metadata_resource_len - offset - calculated_size);
                if (ret != 0)
                        goto out_free_short_name;
@@ -924,7 +1061,8 @@ out_free_file_name:
 static u8 *write_dentry(const struct dentry *dentry, u8 *p)
 {
        u8 *orig_p = p;
-       memset(p, 0, dentry->length);
+       unsigned padding;
+
        p = put_u64(p, dentry->length);
        p = put_u32(p, dentry->attributes);
        p = put_u32(p, dentry->security_id);
@@ -934,16 +1072,28 @@ static u8 *write_dentry(const struct dentry *dentry, u8 *p)
        p = put_u64(p, dentry->creation_time);
        p = put_u64(p, dentry->last_access_time);
        p = put_u64(p, dentry->last_write_time);
-       memcpy(p, dentry->hash, WIM_HASH_SIZE);
-       p += WIM_HASH_SIZE;
-       p = put_u32(p, dentry->reparse_tag);
-       p = put_u64(p, dentry->hard_link);
-       p = put_u16(p, dentry->num_ads); /*streams */
+       p = put_bytes(p, WIM_HASH_SIZE, dentry->hash);
+       if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
+               p = put_zeroes(p, 4);
+               p = put_u32(p, dentry->reparse_tag);
+               p = put_zeroes(p, 4);
+       } else {
+               p = put_u32(p, dentry->reparse_tag);
+               p = put_u64(p, dentry->hard_link);
+       }
+       p = put_u16(p, dentry->num_ads);
        p = put_u16(p, dentry->short_name_len);
        p = put_u16(p, dentry->file_name_len);
        p = put_bytes(p, dentry->file_name_len, (u8*)dentry->file_name);
        p = put_u16(p, 0); /* filename padding, 2 bytes. */
        p = put_bytes(p, dentry->short_name_len, (u8*)dentry->short_name);
+
+       wimlib_assert(p - orig_p <= dentry->length);
+       if (p - orig_p < dentry->length)
+               p = put_zeroes(p, dentry->length - (p - orig_p));
+
+       p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
+
        for (u16 i = 0; i < dentry->num_ads; i++) {
                p = put_u64(p, ads_entry_length(&dentry->ads_entries[i]));
                p = put_u64(p, 0); /* Unused */
@@ -951,8 +1101,9 @@ static u8 *write_dentry(const struct dentry *dentry, u8 *p)
                p = put_u16(p, dentry->ads_entries[i].stream_name_len);
                p = put_bytes(p, dentry->ads_entries[i].stream_name_len,
                                 (u8*)dentry->ads_entries[i].stream_name);
+               p = put_zeroes(p, (8 - (p - orig_p) % 8) % 8);
        }
-       return orig_p + dentry->length;
+       return p;
 }
 
 /* Recursive function that writes a dentry tree rooted at @tree, not including
@@ -968,8 +1119,8 @@ u8 *write_dentry_tree(const struct dentry *tree, u8 *p)
                /* write end of directory entry */
                p = put_u64(p, 0);
        } else {
-               /* Nothing to do for a regular file. */
-               if (dentry_is_regular_file(tree))
+               /* Nothing to do for non-directories */
+               if (!dentry_is_directory(tree))
                        return p;
        }