Whamcloud - gitweb
AOSP: mke2fs, libext2fs: fix bugs on windows
[tools/e2fsprogs.git] / lib / ext2fs / symlink.c
index 279f48b..607396d 100644 (file)
@@ -29,7 +29,7 @@
 #include "ext2fs.h"
 
 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
-                        const char *name, char *target)
+                        const char *name, const char *target)
 {
        errcode_t               retval;
        struct ext2_inode       inode;
@@ -42,7 +42,7 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
        EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
 
        /* The Linux kernel doesn't allow for links longer than a block */
-       target_len = strlen(target);
+       target_len = strnlen(target, fs->blocksize + 1);
        if (target_len > fs->blocksize) {
                retval = EXT2_ET_INVALID_ARGUMENT;
                goto cleanup;
@@ -51,6 +51,12 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
        /*
         * Allocate a data block for slow links
         */
+       retval = ext2fs_get_mem(fs->blocksize+1, &block_buf);
+       if (retval)
+               goto cleanup;
+       memset(block_buf, 0, fs->blocksize+1);
+       strncpy(block_buf, target, fs->blocksize);
+
        memset(&inode, 0, sizeof(struct ext2_inode));
        fastlink = (target_len < sizeof(inode.i_block));
        if (!fastlink) {
@@ -60,9 +66,6 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
                                           NULL, &blk);
                if (retval)
                        goto cleanup;
-               retval = ext2fs_get_mem(fs->blocksize, &block_buf);
-               if (retval)
-                       goto cleanup;
        }
 
        /*
@@ -85,8 +88,7 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
        /* The time fields are set by ext2fs_write_new_inode() */
 
        inlinelink = !fastlink &&
-                    (fs->super->s_feature_incompat &
-                                       EXT4_FEATURE_INCOMPAT_INLINE_DATA) &&
+                    ext2fs_has_feature_inline_data(fs->super) &&
                     (target_len < fs->blocksize);
        if (fastlink) {
                /* Fast symlinks, target stored in inode */
@@ -97,7 +99,7 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
                retval = ext2fs_write_new_inode(fs, ino, &inode);
                if (retval)
                        goto cleanup;
-               retval = ext2fs_inline_data_set(fs, ino, &inode, target,
+               retval = ext2fs_inline_data_set(fs, ino, &inode, block_buf,
                                                target_len);
                if (retval) {
                        inode.i_flags &= ~EXT4_INLINE_DATA_FL;
@@ -109,12 +111,9 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
                        goto cleanup;
        } else {
 need_block:
-               ext2fs_iblk_set(fs, &inode, 1);
                /* Slow symlinks, target stored in the first block */
-               memset(block_buf, 0, fs->blocksize);
-               strncpy(block_buf, target, fs->blocksize);
-               if (fs->super->s_feature_incompat &
-                   EXT3_FEATURE_INCOMPAT_EXTENTS) {
+               ext2fs_iblk_set(fs, &inode, 1);
+               if (ext2fs_has_feature_extents(fs->super)) {
                        /*
                         * The extent bmap is setup after the inode and block
                         * have been written out below.
@@ -175,3 +174,14 @@ cleanup:
                ext2fs_free_mem(&block_buf);
        return retval;
 }
+
+/*
+ * Test whether an inode is a fast symlink.
+ *
+ * A fast symlink has its symlink data stored in inode->i_block.
+ */
+int ext2fs_is_fast_symlink(struct ext2_inode *inode)
+{
+       return LINUX_S_ISLNK(inode->i_mode) && EXT2_I_SIZE(inode) &&
+              EXT2_I_SIZE(inode) < sizeof(inode->i_block);
+}