* along with wimlib; if not, see http://www.gnu.org/licenses/.
*/
+#include <errno.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
#include "wimlib_internal.h"
#include "dentry.h"
#include "io.h"
#include "timestamp.h"
#include "lookup_table.h"
#include "sha1.h"
-#include <unistd.h>
-#include <sys/stat.h>
-
-/* Transfers file attributes from a `stat' buffer to a struct dentry. */
-void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
+/*
+ * 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 (S_ISDIR(stbuf->st_mode))
- dentry->attributes = WIM_FILE_ATTRIBUTE_DIRECTORY;
- else
- dentry->attributes = WIM_FILE_ATTRIBUTE_NORMAL;
+ if (dentry->file_name_utf8_len != name_len)
+ return false;
+ return memcmp(dentry->file_name_utf8, name, name_len) == 0;
}
-/* 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)
+/* 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)
{
- struct lookup_table_entry *lte;
-
- 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;
+ u64 length = (dentry->length + 7) & ~7;
+ for (u16 i = 0 ; i < dentry->num_ads; i++)
+ length += ads_entry_length(&dentry->ads_entries[i]);
+ return length;
+}
- if (lte) {
- stbuf->st_nlink = lte->refcnt;
- stbuf->st_size = lte->resource_entry.original_size;
+/* Transfers file attributes from a `stat' buffer to a struct dentry. */
+void stbuf_to_dentry(const struct stat *stbuf, struct dentry *dentry)
+{
+ 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 {
- stbuf->st_nlink = 1;
- stbuf->st_size = 0;
+ dentry->attributes = FILE_ATTRIBUTE_NORMAL;
}
- 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;
+ 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));
+ /* Set timestamps */
+ dentry->creation_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
+ dentry->last_write_time = timespec_to_wim_timestamp(&stbuf->st_mtim);
+ dentry->last_access_time = timespec_to_wim_timestamp(&stbuf->st_atim);
}
+
/* Makes all timestamp fields for the dentry be the current time. */
void dentry_update_all_timestamps(struct dentry *dentry)
{
- u64 now = get_timestamp();
+ u64 now = get_wim_timestamp();
dentry->creation_time = now;
dentry->last_access_time = now;
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;
+}
+
+static void ads_entry_init(struct ads_entry *ads_entry)
+{
+ memset(ads_entry, 0, sizeof(struct ads_entry));
+ INIT_LIST_HEAD(&ads_entry->lte_group_list.list);
+ ads_entry->lte_group_list.type = STREAM_TYPE_ADS;
+}
+
+/* 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;
+ struct ads_entry *ads_entries;
+ struct ads_entry *new_entry;
+
+ if (dentry->num_ads == 0xffff)
+ return NULL;
+ num_ads = dentry->num_ads + 1;
+ ads_entries = REALLOC(dentry->ads_entries,
+ num_ads * sizeof(struct ads_entry));
+ if (!ads_entries)
+ return NULL;
+ if (ads_entries != dentry->ads_entries) {
+ /* We moved the ADS entries. Adjust the stream lists. */
+ for (u16 i = 0; i < dentry->num_ads; i++) {
+ struct list_head *cur = &ads_entries[i].lte_group_list.list;
+ cur->prev->next = cur;
+ cur->next->prev = cur;
+ }
+ }
+ dentry->ads_entries = ads_entries;
+
+ new_entry = &ads_entries[num_ads - 1];
+ if (change_ads_name(new_entry, stream_name) != 0)
+ return NULL;
+ dentry->num_ads = num_ads;
+ ads_entry_init(new_entry);
+ return new_entry;
+}
+
+void dentry_remove_ads(struct dentry *dentry, struct ads_entry *ads_entry)
+{
+ u16 idx;
+ u16 following;
+
+ wimlib_assert(dentry->num_ads);
+ idx = ads_entry - dentry->ads_entries;
+ wimlib_assert(idx < dentry->num_ads);
+ following = dentry->num_ads - idx - 1;
+
+ destroy_ads_entry(ads_entry);
+ memcpy(ads_entry, ads_entry + 1, following * sizeof(struct ads_entry));
+
+ /* We moved the ADS entries. Adjust the stream lists. */
+ for (u16 i = 0; i < following; i++) {
+ struct list_head *cur = &ads_entry[i].lte_group_list.list;
+ cur->prev->next = cur;
+ cur->next->prev = cur;
+ }
+
+ dentry->num_ads--;
+}
+
/*
* Calls a function on all directory entries in a directory tree. It is called
* on a parent before its children.
/* Advance the subdir offset by the amount of space the children
* of this dentry take up. */
do {
- *subdir_offset_p += child->length;
+ *subdir_offset_p += dentry_total_length(child);
child = child->next;
} while (child != dentry->children);
const char *name;
};
struct file_attr_flag file_attr_flags[] = {
- {WIM_FILE_ATTRIBUTE_READONLY, "READONLY"},
- {WIM_FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
- {WIM_FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
- {WIM_FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
- {WIM_FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
- {WIM_FILE_ATTRIBUTE_DEVICE, "DEVICE"},
- {WIM_FILE_ATTRIBUTE_NORMAL, "NORMAL"},
- {WIM_FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
- {WIM_FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
- {WIM_FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
- {WIM_FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
- {WIM_FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
- {WIM_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
- {WIM_FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
- {WIM_FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
+ {FILE_ATTRIBUTE_READONLY, "READONLY"},
+ {FILE_ATTRIBUTE_HIDDEN, "HIDDEN"},
+ {FILE_ATTRIBUTE_SYSTEM, "SYSTEM"},
+ {FILE_ATTRIBUTE_DIRECTORY, "DIRECTORY"},
+ {FILE_ATTRIBUTE_ARCHIVE, "ARCHIVE"},
+ {FILE_ATTRIBUTE_DEVICE, "DEVICE"},
+ {FILE_ATTRIBUTE_NORMAL, "NORMAL"},
+ {FILE_ATTRIBUTE_TEMPORARY, "TEMPORARY"},
+ {FILE_ATTRIBUTE_SPARSE_FILE, "SPARSE_FILE"},
+ {FILE_ATTRIBUTE_REPARSE_POINT, "REPARSE_POINT"},
+ {FILE_ATTRIBUTE_COMPRESSED, "COMPRESSED"},
+ {FILE_ATTRIBUTE_OFFLINE, "OFFLINE"},
+ {FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,"NOT_CONTENT_INDEXED"},
+ {FILE_ATTRIBUTE_ENCRYPTED, "ENCRYPTED"},
+ {FILE_ATTRIBUTE_VIRTUAL, "VIRTUAL"},
};
/* Prints a directory entry. @lookup_table is a pointer to the lookup table, or
* NULL if the resource entry for the dentry is not to be printed. */
int print_dentry(struct dentry *dentry, void *lookup_table)
{
+ const u8 *hash;
struct lookup_table_entry *lte;
- unsigned i;
printf("[DENTRY]\n");
printf("Length = %"PRIu64"\n", dentry->length);
printf("Attributes = 0x%x\n", dentry->attributes);
- for (i = 0; i < ARRAY_LEN(file_attr_flags); i++)
+ for (unsigned i = 0; i < ARRAY_LEN(file_attr_flags); i++)
if (file_attr_flags[i].flag & dentry->attributes)
- printf(" WIM_FILE_ATTRIBUTE_%s is set\n",
+ printf(" FILE_ATTRIBUTE_%s is set\n",
file_attr_flags[i].name);
printf("Security ID = %d\n", dentry->security_id);
printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
- /*printf("Unused1 = %"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 = ");
- print_hash(dentry->hash);
- putchar('\n');
- printf("Reparse Tag = %u\n", dentry->reparse_tag);
- printf("Hard Link Group = %"PRIu64"\n", dentry->hard_link);
+#if 0
+ printf("Unused1 = 0x%"PRIu64"\n", dentry->unused1);
+ printf("Unused2 = %"PRIu64"\n", dentry->unused2);
+#endif
+#if 0
+ printf("Creation Time = 0x%"PRIx64"\n");
+ printf("Last Access Time = 0x%"PRIx64"\n");
+ printf("Last Write Time = 0x%"PRIx64"\n");
+#endif
+
+ time_t creat_time = wim_timestamp_to_unix(dentry->creation_time);
+ time_t access_time = wim_timestamp_to_unix(dentry->last_access_time);
+ time_t mod_time = wim_timestamp_to_unix(dentry->last_write_time);
+ printf("Creation Time = %s", asctime(localtime(&creat_time)));
+ printf("Last Access Time = %s", asctime(localtime(&access_time)));
+ printf("Last Write Time = %s", asctime(localtime(&mod_time)));
+
+ printf("Reparse Tag = 0x%"PRIx32"\n", dentry->reparse_tag);
+ printf("Hard Link Group = 0x%"PRIx64"\n", dentry->hard_link);
printf("Number of Alternate Data Streams = %hu\n", dentry->num_ads);
printf("Filename = \"");
print_string(dentry->file_name, dentry->file_name_len);
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');
+ lte = dentry_stream_lte(dentry, 0, lookup_table);
+ if (lte) {
+ print_lookup_table_entry(lte);
} else {
- putchar('\n');
+ hash = dentry_stream_hash(dentry, 0);
+ if (hash) {
+ printf("Hash = 0x");
+ print_hash(hash);
+ 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);
+ printf("Name Length (UTF-16) = %u\n",
+ dentry->ads_entries[i].stream_name_len);
+ hash = dentry_stream_hash(dentry, i + 1);
+ if (hash) {
+ printf("Hash = 0x");
+ print_hash(hash);
+ putchar('\n');
+ }
+ print_lookup_table_entry(dentry_stream_lte(dentry, i + 1,
+ lookup_table));
}
return 0;
}
memset(dentry, 0, sizeof(struct dentry));
dentry->refcnt = 1;
dentry->security_id = -1;
+ dentry->ads_entries_status = ADS_ENTRIES_DEFAULT;
+ dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
}
/*
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;
+ INIT_LIST_HEAD(&dentry->link_group_list);
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;
+}
-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);
+ FREE(dentry->extracted_file);
+}
+
+void free_dentry(struct dentry *dentry)
+{
+ wimlib_assert(dentry);
+ __destroy_dentry(dentry);
+ if (dentry->ads_entries_status != ADS_ENTRIES_USER)
+ dentry_free_ads_entries(dentry);
FREE(dentry);
}
-/* Arguments for do_free_dentry(). */
-struct free_dentry_args {
- struct lookup_table *lookup_table;
- bool lt_decrement_refcnt;
-};
+/* Like free_dentry(), but assigns a new ADS entries owner if this dentry was
+ * the previous owner, and also deletes the dentry from its link_group_list */
+void put_dentry(struct dentry *dentry)
+{
+ if (dentry->ads_entries_status == ADS_ENTRIES_OWNER) {
+ struct dentry *new_owner;
+ list_for_each_entry(new_owner, &dentry->link_group_list,
+ link_group_list)
+ {
+ if (new_owner->ads_entries_status == ADS_ENTRIES_USER) {
+ new_owner->ads_entries_status = ADS_ENTRIES_OWNER;
+ break;
+ }
+ }
+ dentry->ads_entries_status = ADS_ENTRIES_USER;
+ }
+ struct list_head *next;
+ list_del(&dentry->link_group_list);
+ free_dentry(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;
+}
/*
* This function is passed as an argument to for_dentry_in_tree_depth() in order
* to free a directory tree. __args is a pointer to a `struct free_dentry_args'.
*/
-static int do_free_dentry(struct dentry *dentry, void *__args)
+static int do_free_dentry(struct dentry *dentry, void *__lookup_table)
{
- struct free_dentry_args *args = (struct free_dentry_args*)__args;
-
- if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) {
- lookup_table_decrement_refcnt(args->lookup_table,
- dentry->hash);
+ struct lookup_table *lookup_table = __lookup_table;
+ if (lookup_table) {
+ struct lookup_table_entry *lte;
+ if (dentry->resolved)
+ lte = dentry->lte;
+ else
+ lte = __lookup_resource(lookup_table, dentry->hash);
+ lte_decrement_refcnt(lte, lookup_table);
}
- wimlib_assert(dentry->refcnt >= 1);
+ wimlib_assert(dentry->refcnt != 0);
if (--dentry->refcnt == 0)
free_dentry(dentry);
return 0;
* Unlinks and frees a dentry tree.
*
* @root: The root of the tree.
- * @lookup_table: The lookup table for dentries.
- * @decrement_refcnt: True if the dentries in the tree are to have their
- * reference counts in the lookup table decremented.
+ * @lookup_table: The lookup table for dentries. If non-NULL, the
+ * reference counts in the lookup table for the lookup
+ * table entries corresponding to the dentries will be
+ * decremented.
*/
-void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table,
- bool lt_decrement_refcnt)
+void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table)
{
if (!root || !root->parent)
return;
-
- struct free_dentry_args args;
- args.lookup_table = lookup_table;
- args.lt_decrement_refcnt = lt_decrement_refcnt;
- for_dentry_in_tree_depth(root, do_free_dentry, &args);
+ for_dentry_in_tree_depth(root, do_free_dentry, lookup_table);
}
int increment_dentry_refcnt(struct dentry *dentry, void *ignore)
*/
void link_dentry(struct dentry *dentry, struct dentry *parent)
{
+ wimlib_assert(dentry_is_directory(parent));
dentry->parent = parent;
if (parent->children) {
/* Not an only child; link to siblings. */
}
}
-/* 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))
{
dentry->length = WIM_DENTRY_DISK_SIZE + dentry->file_name_len +
2 + dentry->short_name_len;
- for (u16 i = 0; i < dentry->num_ads; i++)
- dentry->length += ads_entry_length(&dentry->ads_entries[i]);
/* 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)
+/* Duplicates a UTF-8 name into UTF-8 and UTF-16 strings and returns the strings
+ * and their lengths in the pointer arguments */
+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 *name_utf16, *name_utf8;
- FREE(dentry->file_name);
+ utf8_len = strlen(name);
- utf8_len = strlen(new_name);
+ name_utf16 = utf8_to_utf16(name, utf8_len, &utf16_len);
- dentry->file_name = utf8_to_utf16(new_name, utf8_len, &utf16_len);
-
- if (!dentry->file_name)
+ if (!name_utf16)
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;
+ name_utf8 = MALLOC(utf8_len + 1);
+ if (!name_utf8) {
+ FREE(name_utf8);
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(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;
}
+/* 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 = 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;
+}
+
+int change_ads_name(struct ads_entry *entry, const char *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(). */
struct image_statistics {
struct lookup_table *lookup_table;
{
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;
+ if (dentry->resolved)
+ lte = dentry->lte;
+ else
+ 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;
}
}
static int read_ads_entries(const u8 *p, struct dentry *dentry,
- unsigned remaining_size)
+ u64 remaining_size)
{
u16 num_ads = dentry->num_ads;
struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
int ret;
if (!ads_entries) {
- ERROR("Could not allocate memory for %u alternate data stream "
- "entries", num_ads);
+ ERROR("Could not allocate memory for %"PRIu16" "
+ "alternate data stream entries", num_ads);
return WIMLIB_ERR_NOMEM;
}
+ DEBUG2("Reading %"PRIu16" alternate data streams "
+ "(remaining size = %"PRIu64")", num_ads, remaining_size);
+
for (u16 i = 0; i < num_ads; i++) {
struct ads_entry *cur_entry = &ads_entries[i];
u64 length;
size_t utf8_len;
+ const char *p_save = p;
/* Read the base stream entry, excluding the stream name. */
if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
- ERROR("Stream entries go past end of directory entry");
+ ERROR("Stream entries go past end of metadata resource");
+ ERROR("(remaining_size = %"PRIu64")", remaining_size);
ret = WIMLIB_ERR_INVALID_DENTRY;
goto out_free_ads_entries;
}
remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
p = get_u64(p, &length); /* ADS entry length */
+
+ DEBUG2("ADS length = %"PRIu64, length);
+
p += 8; /* Unused */
- p = get_bytes(p, WIM_HASH_SIZE, (u8*)cur_entry->hash);
+ p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
p = get_u16(p, &cur_entry->stream_name_len);
+
+ DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
+
cur_entry->stream_name = NULL;
cur_entry->stream_name_utf8 = NULL;
- if (remaining_size < cur_entry->stream_name_len) {
- ERROR("Stream entries go past end of directory entry");
+ if (remaining_size < cur_entry->stream_name_len + 2) {
+ ERROR("Stream entries go past end of metadata resource");
+ ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
+ "= %"PRIu16" bytes", remaining_size,
+ cur_entry->stream_name_len);
ret = WIMLIB_ERR_INVALID_DENTRY;
goto out_free_ads_entries;
}
+ remaining_size -= cur_entry->stream_name_len + 2;
cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
if (!cur_entry->stream_name) {
ret = WIMLIB_ERR_NOMEM;
goto out_free_ads_entries;
}
- p = get_bytes(p, cur_entry->stream_name_len,
- (u8*)cur_entry->stream_name);
+ get_bytes(p, cur_entry->stream_name_len,
+ (u8*)cur_entry->stream_name);
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;
goto out_free_ads_entries;
}
+ p = p_save + ads_entry_length(cur_entry);
}
dentry->ads_entries = ads_entries;
return 0;
/* 2 unused fields */
p += 2 * sizeof(u64);
+ /*p = get_u64(p, &dentry->unused1);*/
+ /*p = get_u64(p, &dentry->unused2);*/
p = get_u64(p, &dentry->creation_time);
p = get_u64(p, &dentry->last_access_time);
p = get_u64(p, &dentry->last_write_time);
- p = get_bytes(p, WIM_HASH_SIZE, dentry->hash);
+ p = get_bytes(p, SHA1_HASH_SIZE, dentry->hash);
- p = get_u32(p, &dentry->reparse_tag);
+ /*
+ * I don't know what's going on here. It seems like M$ screwed up the
+ * reparse points, then put the fields in the same place and didn't
+ * document it. The WIM_HDR_FLAG_RP_FIX flag in the WIM header might
+ * have something to do with this, but it's not documented.
+ */
+ if (dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
+ /* ??? */
+ p += 4;
+ p = get_u32(p, &dentry->reparse_tag);
+ p += 4;
+ } else {
+ p = get_u32(p, &dentry->reparse_tag);
+ p = get_u64(p, &dentry->hard_link);
+ }
- /* The reparse_reserved field does not actually exist. */
+ /* By the way, the reparse_reserved field does not actually exist (at
+ * least when the file is not a reparse point) */
- p = get_u64(p, &dentry->hard_link);
p = get_u16(p, &dentry->num_ads);
goto out_free_file_name_utf8;
}
- get_bytes(p, short_name_len, short_name);
+ p = get_bytes(p, short_name_len, short_name);
+
+ /* Some directory entries inexplicibly have a little over 70 bytes of
+ * extra data. The exact amount of data seems to be 72 bytes, but it is
+ * aligned on the next 8-byte boundary. Here's an example of the
+ * aligned data:
+ *
+ * 01000000400000006c786bbac58ede11b0bb00261870892ab6adb76fe63a3
+ * e468fca86530d2effa16c786bbac58ede11b0bb00261870892a0000000000
+ * 0000000000000000000000
+ *
+ * Here's one interpretation of how the data is laid out.
+ *
+ * struct unknown {
+ * u32 field1; (always 0x00000001)
+ * u32 field2; (always 0x40000000)
+ * u16 field3;
+ * u32 field4;
+ * u32 field5;
+ * u32 field6;
+ * u8 data[48]; (???)
+ * u64 reserved1; (always 0)
+ * u64 reserved2; (always 0)
+ * };*/
+#if 0
+ if (dentry->length - calculated_size >= WIM_ADS_ENTRY_DISK_SIZE) {
+ printf("%s: %lu / %lu (", file_name_utf8,
+ calculated_size, dentry->length);
+ print_string(p + WIM_ADS_ENTRY_DISK_SIZE, dentry->length - calculated_size - WIM_ADS_ENTRY_DISK_SIZE);
+ puts(")");
+ print_byte_field(p, dentry->length - calculated_size);
+ putchar('\n');
+ }
+#endif
if (dentry->num_ads != 0) {
- ret = read_ads_entries(p, dentry,
- dentry->length - calculated_size);
+ 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;
}
static u8 *write_dentry(const struct dentry *dentry, u8 *p)
{
u8 *orig_p = p;
- memset(p, 0, dentry->length);
+ unsigned padding;
+ const u8 *hash;
+
p = put_u64(p, dentry->length);
p = put_u32(p, dentry->attributes);
p = put_u32(p, dentry->security_id);
p = put_u64(p, dentry->creation_time);
p = put_u64(p, dentry->last_access_time);
p = put_u64(p, dentry->last_write_time);
- if (!is_empty_file_hash(dentry->hash))
- memcpy(p, dentry->hash, WIM_HASH_SIZE);
+ if (dentry->resolved && dentry->lte)
+ hash = dentry->lte->hash;
else
- DEBUG("zero hash for %s\n", dentry->file_name_utf8);
- 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 */
+ hash = dentry->hash;
+ p = put_bytes(p, SHA1_HASH_SIZE, 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 {
+ u64 hard_link;
+ p = put_u32(p, dentry->reparse_tag);
+ 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);
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 */
- p = put_bytes(p, WIM_HASH_SIZE, dentry->ads_entries[i].hash);
+ if (dentry->resolved && dentry->ads_entries[i].lte)
+ hash = dentry->ads_entries[i].lte->hash;
+ else
+ hash = dentry->ads_entries[i].hash;
+ p = put_bytes(p, SHA1_HASH_SIZE, hash);
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
/* 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;
}
}
/* Advance to the offset of the next child. */
- cur_offset += child->length;
+ cur_offset += dentry_total_length(child);
}
/* Link last child to first one, and set parent's