]> wimlib.net Git - wimlib/blobdiff - src/dentry.c
wimfs_write(), dentry_to_stbuf()
[wimlib] / src / dentry.c
index 68f9c57afa9df926b1856ccb79b825b1ccd36f21..ea21e07b57e90540adfccd65fb0485adeab3e39e 100644 (file)
@@ -80,8 +80,8 @@ void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
 }
 
 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
-void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf, 
-                    const struct lookup_table *table)
+int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf, 
+                   const struct lookup_table *table)
 {
        struct lookup_table_entry *lte;
 
@@ -92,20 +92,31 @@ void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
        else
                stbuf->st_mode = S_IFREG | 0644;
 
+       stbuf->st_ino = (ino_t)dentry->hard_link;
+
+       stbuf->st_nlink = dentry_link_group_size(dentry);
+       stbuf->st_uid   = getuid();
+       stbuf->st_gid   = getgid();
+
        /* 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
+       if (table && (lte = __lookup_resource(table, dentry_hash(dentry)))) {
+               if (lte->staging_file_name) {
+                       struct stat native_stat;
+                       if (stat(lte->staging_file_name, &native_stat) != 0)
+                               return -errno;
+                       stbuf->st_size = native_stat.st_size;
+               } else {
+                       stbuf->st_size = lte->resource_entry.original_size;
+               }
+       } 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);
        stbuf->st_mtime   = ms_timestamp_to_unix(dentry->last_write_time);
        stbuf->st_ctime   = ms_timestamp_to_unix(dentry->creation_time);
        stbuf->st_blocks  = (stbuf->st_size + 511) / 512;
+       return 0;
 }
 
 /* Makes all timestamp fields for the dentry be the current time. */
@@ -161,7 +172,7 @@ 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))
+              (dentry->num_ads - (sentry - dentry->ads_entries + 1))
                 * sizeof(struct ads_entry));
        dentry->num_ads--;
 }
@@ -474,6 +485,7 @@ static inline void dentry_common_init(struct dentry *dentry)
        memset(dentry, 0, sizeof(struct dentry));
        dentry->refcnt = 1;
        dentry->security_id = -1;
+       dentry->link_group_master_status = GROUP_SLAVE;
 }
 
 /* 
@@ -498,6 +510,7 @@ struct dentry *new_dentry(const char *name)
        dentry->next   = dentry;
        dentry->prev   = dentry;
        dentry->parent = dentry;
+       INIT_LIST_HEAD(&dentry->link_group_list);
        return dentry;
 err:
        FREE(dentry);
@@ -514,17 +527,99 @@ void dentry_free_ads_entries(struct dentry *dentry)
        dentry->num_ads = 0;
 }
 
-
-void free_dentry(struct dentry *dentry)
+static void __destroy_dentry(struct dentry *dentry)
 {
        FREE(dentry->file_name);
        FREE(dentry->file_name_utf8);
        FREE(dentry->short_name);
        FREE(dentry->full_path_utf8);
-       dentry_free_ads_entries(dentry);
+}
+
+void free_dentry(struct dentry *dentry)
+{
+       __destroy_dentry(dentry);
+       if (dentry->link_group_master_status != GROUP_SLAVE)
+               dentry_free_ads_entries(dentry);
        FREE(dentry);
 }
 
+void put_dentry(struct dentry *dentry)
+{
+       if (dentry->link_group_master_status == GROUP_MASTER) {
+               struct dentry *new_master;
+               list_for_each_entry(new_master, &dentry->link_group_list,
+                                   link_group_list)
+               {
+                       if (new_master->link_group_master_status == GROUP_SLAVE) {
+                               new_master->link_group_master_status = GROUP_MASTER;
+                               dentry->link_group_master_status = GROUP_SLAVE;
+                               break;
+                       }
+               }
+       }
+       struct list_head *next;
+       next = dentry->link_group_list.next;
+       list_del(&dentry->link_group_list);
+       /*if (next->next == next)*/
+               /*container_of(next, struct dentry, link_group_list)->hard_link = 0;*/
+       free_dentry(dentry);
+}
+
+static bool dentries_have_same_ads(const struct dentry *d1,
+                                  const struct dentry *d2)
+{
+       /* Verify stream names and hashes are the same */
+       for (u16 i = 0; i < d1->num_ads; i++) {
+               if (strcmp(d1->ads_entries[i].stream_name_utf8,
+                          d2->ads_entries[i].stream_name_utf8) != 0)
+                       return false;
+               if (memcmp(d1->ads_entries[i].hash,
+                          d2->ads_entries[i].hash,
+                          WIM_HASH_SIZE) != 0)
+                       return false;
+       }
+       return true;
+}
+
+/* Share the alternate stream entries between hard-linked dentries. */
+int share_dentry_ads(struct dentry *master, struct dentry *slave)
+{
+       const char *mismatch_type;
+       wimlib_assert(master->num_ads == 0 ||
+                     master->ads_entries != slave->ads_entries);
+       if (master->attributes != slave->attributes) {
+               mismatch_type = "attributes";
+               goto mismatch;
+       }
+       if (master->attributes & FILE_ATTRIBUTE_DIRECTORY) {
+               ERROR("`%s' is hard-linked to `%s', which is a directory ",
+                     slave->full_path_utf8, master->full_path_utf8);
+               return WIMLIB_ERR_INVALID_DENTRY;
+       }
+       if (master->security_id != slave->security_id) {
+               mismatch_type = "security ID";
+               goto mismatch;
+       }
+       if (memcmp(master->hash, slave->hash, WIM_HASH_SIZE) != 0) {
+               mismatch_type = "main file resource";
+               goto mismatch;
+       }
+       if (!dentries_have_same_ads(master, slave)) {
+               mismatch_type = "Alternate Stream Entries";
+               goto mismatch;
+       }
+       dentry_free_ads_entries(slave);
+       slave->ads_entries = master->ads_entries;
+       slave->link_group_master_status = GROUP_SLAVE;
+       return 0;
+mismatch:
+       ERROR("Dentries `%s' and `%s' in the same hard-link group but "
+             "do not share the same %s",
+             master->full_path_utf8, slave->full_path_utf8,
+             mismatch_type);
+       return WIMLIB_ERR_INVALID_DENTRY;
+}
+
 /* clones a dentry.
  *
  * Beware:
@@ -621,7 +716,13 @@ void link_dentry(struct dentry *dentry, struct dentry *parent)
        }
 }
 
-/* Unlink a dentry from the directory tree. */
+
+/* Unlink a dentry from the directory tree. 
+ *
+ * Note: This merely removes it from the in-memory tree structure.  See
+ * remove_dentry() in mount.c for a function implemented on top of this one that
+ * frees the dentry and implements reference counting for the lookup table
+ * entries. */
 void unlink_dentry(struct dentry *dentry)
 {
        if (dentry_is_root(dentry))
@@ -647,36 +748,33 @@ static inline void recalculate_dentry_size(struct dentry *dentry)
        dentry->length = (dentry->length + 7) & ~7;
 }
 
-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)
+int get_names(char **name_utf16_ret, char **name_utf8_ret,
+             u16 *name_utf16_len_ret, u16 *name_utf8_len_ret,
+             const char *name)
 {
        size_t utf8_len;
        size_t utf16_len;
-       char *file_name, *file_name_utf8;
+       char *name_utf16, *name_utf8;
 
-       utf8_len = strlen(new_name);
+       utf8_len = strlen(name);
 
-       file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
+       name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
 
-       if (!file_name)
+       if (!name_utf16)
                return WIMLIB_ERR_NOMEM;
 
-       file_name_utf8 = MALLOC(utf8_len + 1);
-       if (!file_name_utf8) {
-               FREE(file_name);
+       name_utf8 = MALLOC(utf8_len + 1);
+       if (!name_utf8) {
+               FREE(name_utf8);
                return WIMLIB_ERR_NOMEM;
        }
-       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;
+       memcpy(name_utf8, name, utf8_len + 1);
+       FREE(*name_utf8_ret);
+       FREE(*name_utf16_ret);
+       *name_utf8_ret      = name_utf8;
+       *name_utf16_ret     = name_utf16;
+       *name_utf8_len_ret  = utf8_len;
+       *name_utf16_len_ret = utf16_len;
        return 0;
 }
 
@@ -687,9 +785,9 @@ 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);
+       ret = get_names(&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;
@@ -697,10 +795,10 @@ int change_dentry_name(struct dentry *dentry, const char *new_name)
 
 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);
+       return get_names(&entry->stream_name, &entry->stream_name_utf8,
+                        &entry->stream_name_len,
+                        &entry->stream_name_utf8_len,
+                         new_name);
 }
 
 /* Parameters for calculate_dentry_statistics(). */
@@ -1078,8 +1176,13 @@ static u8 *write_dentry(const struct dentry *dentry, u8 *p)
                p = put_u32(p, dentry->reparse_tag);
                p = put_zeroes(p, 4);
        } else {
+               u64 hard_link;
                p = put_u32(p, dentry->reparse_tag);
-               p = put_u64(p, dentry->hard_link);
+               if (dentry->link_group_list.next == &dentry->link_group_list)
+                       hard_link = 0;
+               else
+                       hard_link = dentry->hard_link;
+               p = put_u64(p, hard_link);
        }
        p = put_u16(p, dentry->num_ads);
        p = put_u16(p, dentry->short_name_len);