X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fdentry.c;h=1c93792fd9ed0556f9d0914336c4b386dac2e795;hp=7f418539a0e932ebfa9b4d96b652d6a87b4030df;hb=bd25d812eaf391fea72f4a7970bc67fddf80ac6f;hpb=751a849fe04a9230a661e38429ff4d971f74bdbf diff --git a/src/dentry.c b/src/dentry.c index 7f418539..1c93792f 100644 --- a/src/dentry.c +++ b/src/dentry.c @@ -7,24 +7,27 @@ * resource with its own security data and dentry tree. Dentries in different * images may share file resources by referring to the same lookup table * entries. + */ + +/* * * Copyright (C) 2010 Carl Thijssen * Copyright (C) 2012 Eric Biggers * - * wimlib - Library for working with WIM files + * This file is part of wimlib, a library for working with WIM files. * - * This library is free software; you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by the Free - * Software Foundation; either version 2.1 of the License, or (at your option) any - * later version. + * wimlib is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. * - * This library is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A - * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. * - * You should have received a copy of the GNU Lesser General Public License along - * with this library; if not, write to the Free Software Foundation, Inc., 59 - * Temple Place, Suite 330, Boston, MA 02111-1307 USA + * You should have received a copy of the GNU Lesser General Public License + * along with wimlib; if not, see http://www.gnu.org/licenses/. */ #include "wimlib_internal.h" @@ -81,9 +84,9 @@ void dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf, void dentry_update_all_timestamps(struct dentry *dentry) { u64 now = get_timestamp(); - dentry->creation_time = now; - dentry->last_access_time = now; - dentry->last_write_time = now; + dentry->creation_time = now; + dentry->last_access_time = now; + dentry->last_write_time = now; } /* @@ -145,49 +148,48 @@ int for_dentry_in_tree_depth(struct dentry *root, */ int calculate_dentry_full_path(struct dentry *dentry, void *ignore) { - int parent_len; - int len; - char *parent_full_path; char *full_path; - - FREE(dentry->full_path_utf8); - + u32 full_path_len; if (dentry_is_root(dentry)) { - dentry->full_path_utf8 = MALLOC(2); - if (!dentry->full_path_utf8) { - ERROR("Out of memory!\n"); - return WIMLIB_ERR_NOMEM; - } - - dentry->full_path_utf8[0] = '/'; - dentry->full_path_utf8[1] = '\0'; - dentry->full_path_utf8_len = 1; - return 0; - } - - if (dentry_is_root(dentry->parent)) { - parent_len = 0; - parent_full_path = ""; + full_path = MALLOC(2); + if (!full_path) + goto oom; + full_path[0] = '/'; + full_path[1] = '\0'; + full_path_len = 1; } else { - parent_len = dentry->parent->full_path_utf8_len; - parent_full_path = dentry->parent->full_path_utf8; - } + char *parent_full_path; + u32 parent_full_path_len; + const struct dentry *parent = dentry->parent; - len = parent_len + 1 + dentry->file_name_utf8_len; - full_path = MALLOC(len + 1); - if (!full_path) { - ERROR("Out of memory!\n"); - return WIMLIB_ERR_NOMEM; - } + if (dentry_is_root(parent)) { + parent_full_path = ""; + parent_full_path_len = 0; + } else { + parent_full_path = parent->full_path_utf8; + parent_full_path_len = parent->full_path_utf8_len; + } - memcpy(full_path, parent_full_path, parent_len); - full_path[parent_len] = '/'; - memcpy(full_path + parent_len + 1, dentry->file_name_utf8, - dentry->file_name_utf8_len); - full_path[len] = '\0'; + full_path_len = parent_full_path_len + 1 + + dentry->file_name_utf8_len; + full_path = MALLOC(full_path_len + 1); + if (!full_path) + goto oom; + + memcpy(full_path, parent_full_path, parent_full_path_len); + full_path[parent_full_path_len] = '/'; + memcpy(full_path + parent_full_path_len + 1, + dentry->file_name_utf8, + dentry->file_name_utf8_len); + full_path[full_path_len] = '\0'; + } + FREE(dentry->full_path_utf8); dentry->full_path_utf8 = full_path; - dentry->full_path_utf8_len = len; + dentry->full_path_utf8_len = full_path_len; return 0; +oom: + ERROR("Out of memory while calculating dentry full path"); + return WIMLIB_ERR_NOMEM; } /* @@ -307,14 +309,42 @@ int print_dentry_full_path(struct dentry *dentry, void *ignore) return 0; } +struct file_attr_flag { + u32 flag; + 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"}, +}; + /* 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) { 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++) + if (file_attr_flags[i].flag & dentry->attributes) + printf(" WIM_FILE_ATTRIBUTE_%s is set\n", + file_attr_flags[i].name); #ifdef ENABLE_SECURITY_DATA printf("Security ID = %d\n", dentry->security_id); #endif @@ -358,9 +388,6 @@ static inline void dentry_common_init(struct dentry *dentry) { memset(dentry, 0, sizeof(struct dentry)); dentry->refcnt = 1; -#ifdef ENABLE_SECURITY_DATA - dentry->security_id = -1; -#endif } /* @@ -403,7 +430,7 @@ void free_dentry(struct dentry *dentry) /* Arguments for do_free_dentry(). */ struct free_dentry_args { struct lookup_table *lookup_table; - bool decrement_refcnt; + bool lt_decrement_refcnt; }; /* @@ -414,7 +441,7 @@ static int do_free_dentry(struct dentry *dentry, void *__args) { struct free_dentry_args *args = (struct free_dentry_args*)__args; - if (args->decrement_refcnt && !dentry_is_directory(dentry)) { + if (args->lt_decrement_refcnt && !dentry_is_directory(dentry)) { lookup_table_decrement_refcnt(args->lookup_table, dentry->hash); } @@ -434,14 +461,14 @@ static int do_free_dentry(struct dentry *dentry, void *__args) * reference counts in the lookup table decremented. */ void free_dentry_tree(struct dentry *root, struct lookup_table *lookup_table, - bool decrement_refcnt) + bool lt_decrement_refcnt) { if (!root || !root->parent) return; struct free_dentry_args args; args.lookup_table = lookup_table; - args.decrement_refcnt = decrement_refcnt; + args.lt_decrement_refcnt = lt_decrement_refcnt; for_dentry_in_tree_depth(root, do_free_dentry, &args); } @@ -586,7 +613,7 @@ void calculate_dir_tree_statistics(struct dentry *root, struct lookup_table *tab * Reads a directory entry from the metadata resource. */ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, - u64 offset, struct dentry *dentry) + u64 offset, struct dentry *dentry) { const u8 *p; u64 calculated_size; @@ -602,8 +629,8 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, /*Make sure the dentry really fits into the metadata resource.*/ if (offset + 8 > metadata_resource_len) { ERROR("Directory entry starting at %"PRIu64" ends past the " - "end of the metadata resource (size %"PRIu64")!\n", - offset, metadata_resource_len); + "end of the metadata resource (size %"PRIu64")", + offset, metadata_resource_len); return WIMLIB_ERR_INVALID_DENTRY; } @@ -622,9 +649,9 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, if (offset + dentry->length >= metadata_resource_len) { ERROR("Directory entry at offset %"PRIu64" and with size " - "%"PRIu64" ends past the end of the metadata resource " - "(size %"PRIu64")!\n", offset, dentry->length, - metadata_resource_len); + "%"PRIu64" ends past the end of the metadata resource " + "(size %"PRIu64")", + offset, dentry->length, metadata_resource_len); return WIMLIB_ERR_INVALID_DENTRY; } @@ -632,14 +659,14 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, * Note: The root directory entry has no name, and its length does not * include the short name length field. */ if (dentry->length < WIM_DENTRY_DISK_SIZE) { - ERROR("Directory entry has invalid length of " - "%"PRIu64" bytes\n", dentry->length); + ERROR("Directory entry has invalid length of %"PRIu64" bytes", + dentry->length); return WIMLIB_ERR_INVALID_DENTRY; } p = get_u32(p, &dentry->attributes); #ifdef ENABLE_SECURITY_DATA - p = get_u32(p, &dentry->security_id); + p = get_u32(p, (u32*)&dentry->security_id); #else p += sizeof(u32); #endif @@ -672,18 +699,18 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, if (dentry->length < calculated_size) { ERROR("Unexpected end of directory entry! (Expected " - "%"PRIu64" bytes, got %"PRIu64" bytes. " - "short_name_len = %hu, file_name_len = %hu)\n", - calculated_size, dentry->length, - short_name_len, file_name_len); + "%"PRIu64" bytes, got %"PRIu64" bytes. " + "short_name_len = %hu, file_name_len = %hu)", + calculated_size, dentry->length, + short_name_len, file_name_len); return WIMLIB_ERR_INVALID_DENTRY; } /* Read the filename. */ file_name = MALLOC(file_name_len); if (!file_name) { - ERROR("Failed to allocate %hu bytes for dentry file name!\n", - file_name_len); + ERROR("Failed to allocate %hu bytes for dentry file name", + file_name_len); return WIMLIB_ERR_NOMEM; } p = get_bytes(p, file_name_len, file_name); @@ -693,22 +720,21 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, &file_name_utf8_len); if (!file_name_utf8) { - ERROR("Failed to allocate memory to convert UTF16 " - "filename (%hu bytes) to UTF8\n", - file_name_len); - goto err_nomem2; + ERROR("Failed to allocate memory to convert UTF-16 " + "filename (%hu bytes) to UTF-8", file_name_len); + goto out_free_file_name; } /* Undocumented padding between file name and short name. This probably - * is supposed to be a terminating NULL character. */ + * is supposed to be a terminating null character. */ p += 2; /* Read the short filename. */ short_name = MALLOC(short_name_len); if (!short_name) { - ERROR("Failed to allocate %hu bytes for short filename\n", - short_name_len); - goto err_nomem1; + ERROR("Failed to allocate %hu bytes for short filename", + short_name_len); + goto out_free_file_name_utf8; } get_bytes(p, short_name_len, short_name); @@ -720,9 +746,9 @@ int read_dentry(const u8 metadata_resource[], u64 metadata_resource_len, dentry->file_name_utf8_len = file_name_utf8_len; dentry->short_name_len = short_name_len; return 0; -err_nomem1: +out_free_file_name_utf8: FREE(dentry->file_name_utf8); -err_nomem2: +out_free_file_name: FREE(dentry->file_name); return WIMLIB_ERR_NOMEM; } @@ -754,7 +780,7 @@ static u8 *write_dentry(const struct dentry *dentry, u8 *p) if (!is_empty_file_hash(dentry->hash)) memcpy(p, dentry->hash, WIM_HASH_SIZE); else - printf("zero hash for %s\n", dentry->file_name_utf8); + DEBUG("zero hash for %s\n", dentry->file_name_utf8); p += WIM_HASH_SIZE; p = put_u32(p, 0); /* reparse_tag */ p = put_u64(p, dentry->hard_link); @@ -811,13 +837,14 @@ u8 *write_dentry_tree(const struct dentry *tree, u8 *p) * metadata resource and into the dentry tree. * * @metadata_resource: An array that contains the uncompressed metadata - * resource for the WIM file. + * resource for the WIM file. * @metadata_resource_len: The length of @metadata_resource. - * @dentry: A pointer to a struct dentry that is the root of the directory tree - * and has already been read from the metadata resource. It does not - * need to be the real root, because this procedure is called - * recursively. - * @return: True on success, false on failure. + * @dentry: A pointer to a struct dentry that is the root of the directory + * tree and has already been read from the metadata resource. It + * does not need to be the real root because this procedure is + * called recursively. + * + * @return: Zero on success, nonzero on failure. */ int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len, struct dentry *dentry) @@ -853,8 +880,8 @@ int read_dentry_tree(const u8 metadata_resource[], u64 metadata_resource_len, * link it to the parent and previous child. */ child = MALLOC(sizeof(struct dentry)); if (!child) { - ERROR("Failed to allocate %zu bytes for new dentry!\n", - sizeof(struct dentry)); + ERROR("Failed to allocate %zu bytes for new dentry", + sizeof(struct dentry)); ret = WIMLIB_ERR_NOMEM; break; }