X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fsymlink.c;h=3e89a88eead6cfed31980c86f9f4fd51a2e5ca4e;hb=868f9905359573d33f06c19106523535b309273b;hp=42a771e65978171a8f0fff35e8a2a52570fd16ec;hpb=815ad485b8ce48db48a3137d73e511f6c9af868c;p=wimlib diff --git a/src/symlink.c b/src/symlink.c index 42a771e6..3e89a88e 100644 --- a/src/symlink.c +++ b/src/symlink.c @@ -1,6 +1,33 @@ +/* + * symlink.c + * + * Code to read and set symbolic links in WIM files. + */ + +/* + * Copyright (C) 2012 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 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 General Public License for more + * details. + * + * 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 "lookup_table.h" +#include "sha1.h" +#include /* * Find the symlink target of a symbolic link or junction point in the WIM. @@ -12,7 +39,7 @@ */ static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, char *buf, size_t buf_len, - bool is_junction_point) + u32 reparse_tag) { const u8 *p = resource; u16 substitute_name_offset; @@ -25,6 +52,7 @@ static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, unsigned header_size; char *translated_target; bool is_absolute; + u32 flags; if (resource_len < 12) return -EIO; @@ -32,17 +60,27 @@ static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, p = get_u16(p, &substitute_name_len); p = get_u16(p, &print_name_offset); p = get_u16(p, &print_name_len); - if (is_junction_point) { + get_u32(p, &flags); + + 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 { - u32 flags; - p = get_u32(p, &flags); is_absolute = (flags & 1) ? false : true; header_size = 12; + p += 4; } if (header_size + substitute_name_offset + substitute_name_len > resource_len) return -EIO; - link_target = utf16_to_utf8(p + substitute_name_offset, + link_target = utf16_to_utf8((const char *)p + substitute_name_offset, substitute_name_len, &link_target_len); @@ -55,7 +93,7 @@ static ssize_t get_symlink_name(const u8 *resource, size_t resource_len, } translated_target = link_target; - if (is_junction_point || is_absolute) { + if (reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT || is_absolute) { if (link_target_len < 7 || memcmp(translated_target, "\\??\\", 4) != 0 || translated_target[4] == '\0' @@ -66,10 +104,18 @@ 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... + * + * 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] == '\\') + translated_target[i] = '/'; } - for (size_t i = 0; i < link_target_len; i++) - if (translated_target[i] == '\\') - translated_target[i] = '/'; memcpy(buf, translated_target, link_target_len + 1); ret = link_target_len; @@ -87,21 +133,24 @@ void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret) 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; + if (((u16*)name_utf16)[i] == cpu_to_le16('/')) + ((u16*)name_utf16)[i] = cpu_to_le16('\\'); + size_t len = 12 + utf16_len * 2 + 4; 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 + 2); /* 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, 0); /* Print name offset */ p = put_u16(p, utf16_len); /* Print name length */ - p = put_u32(p, (symlink_target[0] == '/') ? 0 : 1); - p = put_bytes(p, utf16_len, name_utf16); - p = put_bytes(p, utf16_len, name_utf16); + p = put_u32(p, 1); + p = put_bytes(p, utf16_len, (const u8*)name_utf16); + p = put_u16(p, 0); + p = put_bytes(p, utf16_len, (const u8*)name_utf16); + p = put_u16(p, 0); /*DEBUG("utf16_len = %zu, len = %zu", utf16_len, len);*/ *len_ret = len; out: @@ -109,54 +158,97 @@ out: 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) +/* Get the symlink target from a dentry. + * + * The dentry may be either "real" symlink or a junction point. + */ +ssize_t inode_readlink(const struct inode *inode, char *buf, size_t buf_len, + const WIMStruct *w) { - struct ads_entry *ads; - struct lookup_table_entry *entry; - struct resource_entry *res_entry; - bool is_junction_point; - - wimlib_assert(dentry_is_symlink(dentry)); - - if (dentry->reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK) { - is_junction_point = false; - /* - * This is of course not actually documented, but what I think is going - * on here is that the symlink dentries have 2 alternate data streams; - * one is the default data stream, which is not used and is empty, and - * one is the symlink buffer data stream, which is confusingly also - * unnamed, but isn't empty as it contains the symlink target within the - * resource. - */ - if (dentry->num_ads != 2) - return -EIO; - if ((entry = lookup_resource(w->lookup_table, dentry->ads_entries[0].hash))) - goto do_readlink; - if ((entry = lookup_resource(w->lookup_table, dentry->ads_entries[1].hash))) - goto do_readlink; - } else { - wimlib_assert(dentry->reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT); + const struct lookup_table_entry *lte; + int ret; - is_junction_point = true; + wimlib_assert(inode_is_symlink(inode)); - if ((entry = lookup_resource(w->lookup_table, dentry->hash))) - goto do_readlink; - } - return -EIO; -do_readlink: - res_entry = &entry->resource_entry; - if (res_entry->original_size > 10000) + lte = inode_unnamed_lte(inode, w->lookup_table); + if (!lte) return -EIO; - char res_buf[res_entry->original_size]; - 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) + + if (wim_resource_size(lte) > 10000) + return -EIO; + + u8 res_buf[wim_resource_size(lte)]; + ret = read_full_wim_resource(lte, res_buf); + if (ret != 0) return -EIO; - return get_symlink_name(res_buf, res_entry->original_size, buf, - buf_len, is_junction_point); + return get_symlink_name(res_buf, wim_resource_size(lte), buf, + buf_len, inode->reparse_tag); +} + +/* + * 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 + * for the exact same data, in which its reference count is incremented. + * + * The lookup table entry is returned in @lte_ret. + * + * On failure @dentry and @lookup_table are not modified. + */ +int inode_set_symlink(struct inode *inode, const char *target, + struct lookup_table *lookup_table, + struct lookup_table_entry **lte_ret) + +{ + int ret; + size_t symlink_buf_len; + struct 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; + + 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(); + if (!lte) { + ret = WIMLIB_ERR_NOMEM; + goto out_free_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); + } + + inode->lte = lte; + inode->resolved = true; + + DEBUG("Loaded symlink buf"); + + if (existing_lte) + lte->refcnt++; + else + lookup_table_insert(lookup_table, lte); + if (lte_ret) + *lte_ret = lte; + return 0; +out_free_symlink_buf: + FREE(symlink_buf); + return ret; }