* 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 <errno.h>
-#include <unistd.h>
-#include <sys/stat.h>
/*
* Returns true if @dentry has the UTF-8 file name @name that has length
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);
}
-/* Transfers file attributes from a struct dentry to a `stat' buffer. */
-int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf,
- const struct lookup_table *table)
-{
- struct lookup_table_entry *lte;
-
- 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;
-
- 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)))) {
- 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_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. */
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;
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 = dentry->num_ads + 1;
+ u16 num_ads;
struct ads_entry *ads_entries;
struct ads_entry *new_entry;
- if (num_ads == 0xffff)
+
+ if (dentry->num_ads == 0xffff)
return NULL;
- ads_entries = MALLOC(num_ads * sizeof(struct ads_entry));
+ 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) {
- FREE(ads_entries);
+ if (change_ads_name(new_entry, stream_name) != 0)
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));
+ ads_entry_init(new_entry);
+ return new_entry;
}
-void dentry_remove_ads(struct dentry *dentry, struct ads_entry *sentry)
+void dentry_remove_ads(struct dentry *dentry, struct ads_entry *ads_entry)
{
- destroy_ads_entry(sentry);
- memcpy(sentry, sentry + 1,
- (dentry->num_ads - (sentry - dentry->ads_entries + 1))
- * sizeof(struct 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--;
}
* 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(" 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 = 0x%"PRIu64"\n", dentry->unused1);*/
- /*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
- printf("Creation Time = 0x%"PRIx64"\n", dentry->creation_time);
- 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');
+#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);
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)))
- 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 {
+ 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);
- 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 {
+ 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->link_group_master_status = GROUP_SLAVE;
+ dentry->ads_entries_status = ADS_ENTRIES_DEFAULT;
+ dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
}
/*
void free_dentry(struct dentry *dentry)
{
+ wimlib_assert(dentry);
__destroy_dentry(dentry);
- if (dentry->link_group_master_status != GROUP_SLAVE)
+ if (dentry->ads_entries_status != ADS_ENTRIES_USER)
dentry_free_ads_entries(dentry);
FREE(dentry);
}
+/* 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->link_group_master_status == GROUP_MASTER) {
- struct dentry *new_master;
- list_for_each_entry(new_master, &dentry->link_group_list,
+ 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_master->link_group_master_status == GROUP_SLAVE) {
- new_master->link_group_master_status = GROUP_MASTER;
- dentry->link_group_master_status = GROUP_SLAVE;
+ 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;
- 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) {
- WARNING("`%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:
- WARNING("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.
*
return new;
}
-/* Arguments for do_free_dentry(). */
-struct free_dentry_args {
- struct lookup_table *lookup_table;
- bool lt_decrement_refcnt;
-};
-
/*
* 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. */
dentry->length = (dentry->length + 7) & ~7;
}
+/* 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)
else
++*stats->file_count;
- lte = __lookup_resource(stats->lookup_table, dentry->hash);
+ if (dentry->resolved)
+ lte = dentry->lte;
+ else
+ lte = __lookup_resource(stats->lookup_table, dentry->hash);
i = 0;
while (1) {
if (lte) {
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);
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);
/*
* I don't know what's going on here. It seems like M$ screwed up the
{
u8 *orig_p = p;
unsigned padding;
+ const u8 *hash;
p = put_u64(p, dentry->length);
p = put_u32(p, dentry->attributes);
p = put_u64(p, dentry->creation_time);
p = put_u64(p, dentry->last_access_time);
p = put_u64(p, dentry->last_write_time);
- p = put_bytes(p, WIM_HASH_SIZE, dentry->hash);
+ if (dentry->resolved && dentry->lte)
+ hash = dentry->lte->hash;
+ else
+ 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);
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);