* 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"
#include "io.h"
#include "timestamp.h"
#include "lookup_table.h"
+#include "sha1.h"
#include <unistd.h>
#include <sys/stat.h>
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;
}
/*
*/
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;
}
/*
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);
- /*printf("Security ID = %d\n", dentry->security_id);*/
+ 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
printf("Subdir offset = %"PRIu64"\n", dentry->subdir_offset);
/*printf("Unused1 = %"PRIu64"\n", dentry->unused1);*/
/*printf("Unused2 = %"PRIu64"\n", dentry->unused2);*/
{
memset(dentry, 0, sizeof(struct dentry));
dentry->refcnt = 1;
- /* We are currently ignoring the security data. */
- /*dentry->security_id = -1;*/
}
/*
/* Arguments for do_free_dentry(). */
struct free_dentry_args {
struct lookup_table *lookup_table;
- bool decrement_refcnt;
+ bool lt_decrement_refcnt;
};
/*
{
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);
}
* 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);
}
* 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;
/*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;
}
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;
}
* 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);
- /* Currently ignoring security ID. */
+#ifdef ENABLE_SECURITY_DATA
+ p = get_u32(p, (u32*)&dentry->security_id);
+#else
p += sizeof(u32);
+#endif
p = get_u64(p, &dentry->subdir_offset);
/* 2 unused fields */
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);
&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);
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;
}
memset(p, 0, dentry->length);
p = put_u64(p, dentry->length);
p = put_u32(p, dentry->attributes);
- p = put_u32(p, (u32)(-1)); /* security id */
+#ifdef ENABLE_SECURITY_DATA
+ p = put_u32(p, dentry->security_id);
+#else
+ p = put_u32(p, (u32)(-1));
+#endif
p = put_u64(p, dentry->subdir_offset);
p = put_u64(p, 0); /* unused1 */
p = put_u64(p, 0); /* unused2 */
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 (!is_empty_file_hash(dentry->hash))
+ memcpy(p, dentry->hash, WIM_HASH_SIZE);
+ else
+ 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);
p = put_u16(p, 0); /*streams */
* 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)
* 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;
}