X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fsymlink.c;h=538b3e8548b1b5ec11fd3762349089a360d997f4;hp=03f78370bb7442bb56f95c20cf79813eb25996a7;hb=cf373e59a7f6ff7d1fd007c1f22defe508aa67d4;hpb=b1c4e6a269ae4c969060e33685db12f76a204a58 diff --git a/src/symlink.c b/src/symlink.c index 03f78370..538b3e85 100644 --- a/src/symlink.c +++ b/src/symlink.c @@ -1,41 +1,54 @@ /* - * Copyright (C) 2012 Eric Biggers + * symlink.c + * + * Code to read and set symbolic links in WIM files. + */ + +/* + * Copyright (C) 2012, 2013 Eric Biggers * * This file is part of wimlib, a library for working with WIM files. * * 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) + * terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) * any later version. * * 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 + * A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * - * You should have received a copy of the GNU Lesser General Public License + * You should have received a copy of the GNU General Public License * along with wimlib; if not, see http://www.gnu.org/licenses/. */ #include "dentry.h" -#include "io.h" +#include "buffer_io.h" #include "lookup_table.h" #include "sha1.h" #include +/* None of this file is ever needed in Win32 builds because the reparse point + * buffers are not parsed. */ +#if !defined(__WIN32__) + /* * Find the symlink target of a symbolic link or junction point in the WIM. * - * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx - * Except the first 8 bytes aren't included in the resource (presumably because - * we already know the reparse tag from the dentry, and we already know the - * reparse tag len from the lookup table entry resource length). + * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx for a + * description of the format of the so-called "reparse point data buffers". + * + * But, in the WIM format, the first 8 bytes of the reparse point data buffer + * are omitted, presumably because we already know the reparse tag from the + * dentry, and we already know the reparse tag length from the lookup table + * entry resource length. */ -static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, - char *buf, size_t buf_len, - u32 reparse_tag) +static ssize_t +get_symlink_name(const void *resource, size_t resource_len, char *buf, + size_t buf_len, u32 reparse_tag) { - const u8 *p = resource; + const void *p = resource; u16 substitute_name_offset; u16 substitute_name_len; u16 print_name_offset; @@ -59,12 +72,6 @@ static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, wimlib_assert(reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK || reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT); - /* I think that some junction points incorrectly get marked as symbolic - * links. So, parse the link buffer as a symlink if the flags seem - * plausible. */ - if (flags <= 1) - reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK; - if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT) { header_size = 8; } else { @@ -74,12 +81,12 @@ static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, } if (header_size + substitute_name_offset + substitute_name_len > resource_len) return -EIO; - link_target = utf16_to_utf8(p + substitute_name_offset, - substitute_name_len, - &link_target_len); - if (!link_target) - return -EIO; + ret = utf16le_to_tstr((const utf16lechar*)(p + substitute_name_offset), + substitute_name_len, + &link_target, &link_target_len); + if (ret) + return -errno; if (link_target_len + 1 > buf_len) { ret = -ENAMETOOLONG; @@ -99,7 +106,12 @@ static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, translated_target += 4; link_target_len -= 4; /* There's a drive letter, so just leave the backslashes since - * it won't go anyhwere on UNIX anyway... */ + * it won't go anyhwere on UNIX anyway... + * + * XXX + * NTFS-3g tries to re-map these links to actually point to + * something, so maybe we could do something like that here + * XXX*/ } else { for (size_t i = 0; i < link_target_len; i++) if (translated_target[i] == '\\') @@ -113,95 +125,76 @@ out: return ret; } -void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret) -{ - size_t utf8_len = strlen(symlink_target); - size_t utf16_len; - char *name_utf16 = utf8_to_utf16(symlink_target, utf8_len, &utf16_len); - if (!name_utf16) - return NULL; - /*DEBUG("utf16_len = %zu", utf16_len);*/ - for (size_t i = 0; i < utf16_len / 2; i++) - if (((u16*)name_utf16)[i] == to_le16('/')) - ((u16*)name_utf16)[i] = to_le16('\\'); - size_t len = 12 + utf16_len * 2; - void *buf = MALLOC(len); - if (!buf) - goto out; - /* XXX Fix absolute paths */ - - u8 *p = buf; - p = put_u16(p, 0); /* Substitute name offset */ - p = put_u16(p, utf16_len); /* Substitute name length */ - p = put_u16(p, utf16_len); /* Print name offset */ - p = put_u16(p, utf16_len); /* Print name length */ - p = put_u32(p, 1); - p = put_bytes(p, utf16_len, name_utf16); - p = put_bytes(p, utf16_len, name_utf16); - /*DEBUG("utf16_len = %zu, len = %zu", utf16_len, len);*/ - *len_ret = len; -out: - FREE(name_utf16); - return buf; -} - -/* Get the symlink target from a dentry that's already checked to be either a - * "real" symlink or a junction point. */ -ssize_t dentry_readlink(const struct dentry *dentry, char *buf, size_t buf_len, - const WIMStruct *w) +static int +make_symlink_reparse_data_buf(const char *symlink_target, + size_t *len_ret, void **buf_ret) { - const struct resource_entry *res_entry; - const struct lookup_table_entry *lte; + utf16lechar *name_utf16le; + size_t name_utf16le_nbytes; + int ret; - wimlib_assert(dentry_is_symlink(dentry)); + ret = tstr_to_utf16le(symlink_target, strlen(symlink_target), + &name_utf16le, &name_utf16le_nbytes); + if (ret != 0) + return ret; - lte = dentry_first_lte(dentry, w->lookup_table); - if (!lte) - return -EIO; + for (size_t i = 0; i < name_utf16le_nbytes / 2; i++) + if (name_utf16le[i] == cpu_to_le16('/')) + name_utf16le[i] = cpu_to_le16('\\'); - res_entry = <e->resource_entry; - if (res_entry->original_size > 10000) - return -EIO; - - char __res_buf[res_entry->original_size]; - const char *res_buf; - if (lte->is_symlink && lte->symlink_buf) { - res_buf = lte->symlink_buf; + size_t len = 12 + name_utf16le_nbytes * 2; + void *buf = MALLOC(len); + if (buf) { + void *p = buf; + p = put_u16(p, name_utf16le_nbytes); /* Substitute name offset */ + p = put_u16(p, name_utf16le_nbytes); /* Substitute name length */ + p = put_u16(p, 0); /* Print name offset */ + p = put_u16(p, name_utf16le_nbytes); /* Print name length */ + p = put_u32(p, 1); /* flags: 0 iff *full* target, including drive letter??? */ + p = put_bytes(p, name_utf16le_nbytes, name_utf16le); + p = put_bytes(p, name_utf16le_nbytes, name_utf16le); + *len_ret = len; + *buf_ret = buf; + ret = 0; } else { - if (read_full_resource(w->fp, res_entry->size, - res_entry->original_size, - res_entry->offset, - wim_resource_compression_type(w, res_entry), - __res_buf) != 0) - return -EIO; - res_buf = __res_buf; + ret = WIMLIB_ERR_NOMEM; } - return get_symlink_name(res_buf, res_entry->original_size, buf, - buf_len, dentry->reparse_tag); + FREE(name_utf16le); + return ret; } -static int dentry_set_symlink_buf(struct dentry *dentry, - struct lookup_table_entry *lte) +/* Get the symlink target from a WIM inode. + * + * The inode may be either a "real" symlink (reparse tag + * WIM_IO_REPARSE_TAG_SYMLINK), or it may be a junction point (reparse tag + * WIM_IO_REPARSE_TAG_MOUNT_POINT). + */ +ssize_t +inode_readlink(const struct wim_inode *inode, char *buf, size_t buf_len, + const WIMStruct *w, bool threadsafe) { - struct ads_entry *ads_entries; + const struct wim_lookup_table_entry *lte; + int ret; - ads_entries = CALLOC(2, sizeof(struct ads_entry)); - if (!ads_entries) - return WIMLIB_ERR_NOMEM; + wimlib_assert(inode_is_symlink(inode)); - wimlib_assert(dentry->num_ads == 0); - wimlib_assert(dentry->ads_entries == NULL); + lte = inode_unnamed_lte(inode, w->lookup_table); + if (!lte) + return -EIO; - ads_entries[1].lte = lte; + if (wim_resource_size(lte) > 10000) + return -EIO; - /*dentry_free_ads_entries(dentry);*/ - dentry->num_ads = 2; - dentry->ads_entries = ads_entries; - return 0; + u8 res_buf[wim_resource_size(lte)]; + ret = read_full_resource_into_buf(lte, res_buf, threadsafe); + if (ret != 0) + return -EIO; + return get_symlink_name(res_buf, wim_resource_size(lte), buf, + buf_len, inode->i_reparse_tag); } -/* - * Sets @dentry to be a symbolic link pointing to @target. +/* + * Sets @inode to be a symbolic link pointing to @target. * * A lookup table entry for the symbolic link data buffer is created and * inserted into @lookup_table, unless there is an existing lookup table entry @@ -211,30 +204,35 @@ static int dentry_set_symlink_buf(struct dentry *dentry, * * On failure @dentry and @lookup_table are not modified. */ -int dentry_set_symlink(struct dentry *dentry, const char *target, - struct lookup_table *lookup_table, - struct lookup_table_entry **lte_ret) +int +inode_set_symlink(struct wim_inode *inode, + const char *target, + struct wim_lookup_table *lookup_table, + struct wim_lookup_table_entry **lte_ret) { int ret; size_t symlink_buf_len; - struct lookup_table_entry *lte = NULL, *existing_lte; + struct wim_lookup_table_entry *lte = NULL, *existing_lte; u8 symlink_buf_hash[SHA1_HASH_SIZE]; void *symlink_buf; - - symlink_buf = make_symlink_reparse_data_buf(target, &symlink_buf_len); - if (!symlink_buf) - return WIMLIB_ERR_NOMEM; + + ret = make_symlink_reparse_data_buf(target, &symlink_buf_len, + &symlink_buf); + if (ret) + return ret; DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)", symlink_buf_len, symlink_buf_len); - + sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash); existing_lte = __lookup_resource(lookup_table, symlink_buf_hash); if (existing_lte) { lte = existing_lte; + FREE(symlink_buf); + symlink_buf = NULL; } else { DEBUG("Creating new lookup table entry for symlink buf"); lte = new_lookup_table_entry(); @@ -242,19 +240,14 @@ int dentry_set_symlink(struct dentry *dentry, const char *target, ret = WIMLIB_ERR_NOMEM; goto out_free_symlink_buf; } - lte->is_symlink = true; - lte->symlink_buf = symlink_buf; + lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER; + lte->attached_buffer = symlink_buf; lte->resource_entry.original_size = symlink_buf_len; - lte->resource_entry.size = symlink_buf_len; copy_hash(lte->hash, symlink_buf_hash); } - ret = dentry_set_symlink_buf(dentry, lte); - - if (ret != 0) - goto out_free_lte; - - dentry->resolved = true; + inode->i_lte = lte; + inode->i_resolved = 1; DEBUG("Loaded symlink buf"); @@ -265,10 +258,9 @@ int dentry_set_symlink(struct dentry *dentry, const char *target, if (lte_ret) *lte_ret = lte; return 0; -out_free_lte: - if (lte != existing_lte) - FREE(lte); out_free_symlink_buf: FREE(symlink_buf); return ret; } + +#endif /* !defined(__WIN32__) */