]> wimlib.net Git - wimlib/blobdiff - src/symlink.c
License and documentation
[wimlib] / src / symlink.c
index 03f78370bb7442bb56f95c20cf79813eb25996a7..3b8ed4351fc282c45b78262c1c2c59f14198fd5f 100644 (file)
@@ -1,19 +1,25 @@
+/*
+ * 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 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/.
  */
 
@@ -74,7 +80,7 @@ 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,
+       link_target = utf16_to_utf8((const char *)p + substitute_name_offset,
                                    substitute_name_len,
                                    &link_target_len);
 
@@ -99,7 +105,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] == '\\')
@@ -124,20 +135,22 @@ void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret)
        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;
+       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, 1);
-       p = put_bytes(p, utf16_len, name_utf16);
-       p = put_bytes(p, utf16_len, name_utf16);
+       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:
@@ -145,38 +158,30 @@ out:
        return buf;
 }
 
-/* Get the symlink target from a dentry that's already checked to be either a
- * "real" symlink or a junction point. */
+/* Get the symlink target from a dentry.
+ *
+ * The dentry may be either "real" symlink or a junction point.
+ */
 ssize_t dentry_readlink(const struct dentry *dentry, char *buf, size_t buf_len,
                        const WIMStruct *w)
 {
-       const struct resource_entry *res_entry;
        const struct lookup_table_entry *lte;
+       int ret;
 
        wimlib_assert(dentry_is_symlink(dentry));
 
-       lte = dentry_first_lte(dentry, w->lookup_table);
+       lte = dentry_unnamed_lte(dentry, w->lookup_table);
        if (!lte)
                return -EIO;
 
-       res_entry = &lte->resource_entry;
-       if (res_entry->original_size > 10000)
+       if (wim_resource_size(lte) > 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;
-       } 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;
-       }
-       return get_symlink_name(res_buf, res_entry->original_size, buf,
+       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, wim_resource_size(lte), buf,
                                buf_len, dentry->reparse_tag);
 }
 
@@ -192,7 +197,7 @@ static int dentry_set_symlink_buf(struct dentry *dentry,
        wimlib_assert(dentry->num_ads == 0);
        wimlib_assert(dentry->ads_entries == NULL);
 
-       ads_entries[1].lte = lte;
+       ads_entries[0].lte = lte;
 
        /*dentry_free_ads_entries(dentry);*/
        dentry->num_ads = 2;
@@ -235,6 +240,8 @@ int dentry_set_symlink(struct dentry *dentry, const char *target,
 
        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,10 +249,11 @@ 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;
+               lte->resource_entry.size          = symlink_buf_len;
+               lte->resource_entry.flags         = 0;
                copy_hash(lte->hash, symlink_buf_hash);
        }