X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fdentry.c;h=68f9c57afa9df926b1856ccb79b825b1ccd36f21;hp=e5af01ba695a4d0963d2a46a894b5609d3fa1905;hb=2891001b0171b2a89084b20d8b522b94930cc90d;hpb=d977c5b4f348208e47fd2922f202f3eb60d5d5cb diff --git a/src/dentry.c b/src/dentry.c index e5af01ba..68f9c57a 100644 --- a/src/dentry.c +++ b/src/dentry.c @@ -51,14 +51,6 @@ static bool dentry_has_name(const struct dentry *dentry, const char *name, return memcmp(dentry->file_name_utf8, name, name_len) == 0; } -static bool ads_entry_has_name(const struct ads_entry *entry, - const char *name, size_t name_len) -{ - if (entry->stream_name_utf8_len != name_len) - return false; - return memcmp(entry->stream_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) @@ -80,6 +72,11 @@ void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry) } 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. */ @@ -95,18 +92,14 @@ void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf, else stbuf->st_mode = S_IFREG | 0644; - if (table) - lte = __lookup_resource(table, dentry_hash(dentry)); - 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); @@ -495,22 +488,24 @@ 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; } -static void dentry_free_ads_entries(struct dentry *dentry) +void dentry_free_ads_entries(struct dentry *dentry) { for (u16 i = 0; i < dentry->num_ads; i++) destroy_ads_entry(&dentry->ads_entries[i]); @@ -530,6 +525,28 @@ void free_dentry(struct dentry *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; @@ -660,6 +677,7 @@ static int do_name_change(char **file_name_ret, *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 @@ -904,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); @@ -1048,7 +1062,7 @@ static u8 *write_dentry(const struct dentry *dentry, u8 *p) { u8 *orig_p = p; unsigned padding; - memset(p, 0, dentry->length); + p = put_u64(p, dentry->length); p = put_u32(p, dentry->attributes); p = put_u32(p, dentry->security_id); @@ -1058,11 +1072,16 @@ 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); @@ -1100,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; } @@ -1213,17 +1232,3 @@ int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len, dentry->children = first_child; return ret; } - -int dentry_set_symlink_buf(struct dentry *dentry, const u8 symlink_buf_hash[]) -{ - struct ads_entry *ads_entries; - - ads_entries = CALLOC(2, sizeof(struct ads_entry)); - if (!ads_entries) - return WIMLIB_ERR_NOMEM; - memcpy(ads_entries[1].hash, symlink_buf_hash, WIM_HASH_SIZE); - dentry_free_ads_entries(dentry); - dentry->num_ads = 2; - dentry->ads_entries = ads_entries; - return 0; -}