2 * symlink.c --- make a symlink in the filesystem, based on mkdir.c
4 * Copyright (c) 2012, Intel Corporation.
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
25 #include <sys/types.h>
31 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
32 const char *name, char *target)
34 ext2_extent_handle_t handle;
36 struct ext2_inode inode;
37 ext2_ino_t scratch_ino;
40 unsigned int target_len;
43 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
45 /* The Linux kernel doesn't allow for links longer than a block */
46 target_len = strlen(target);
47 if (target_len > fs->blocksize) {
48 retval = EXT2_ET_INVALID_ARGUMENT;
53 * Allocate a data block for slow links
55 fastlink = (target_len < sizeof(inode.i_block));
57 retval = ext2fs_new_block2(fs, 0, 0, &blk);
60 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
66 * Allocate an inode, if necessary
69 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFLNK | 0755,
76 * Create the inode structure....
78 memset(&inode, 0, sizeof(struct ext2_inode));
79 inode.i_mode = LINUX_S_IFLNK | 0777;
80 inode.i_uid = inode.i_gid = 0;
81 ext2fs_iblk_set(fs, &inode, fastlink ? 0 : 1);
82 inode.i_links_count = 1;
83 inode.i_size = target_len;
84 /* The time fields are set by ext2fs_write_new_inode() */
87 /* Fast symlinks, target stored in inode */
88 strcpy((char *)&inode.i_block, target);
90 /* Slow symlinks, target stored in the first block */
91 memset(block_buf, 0, fs->blocksize);
92 strcpy(block_buf, target);
93 if (fs->super->s_feature_incompat &
94 EXT3_FEATURE_INCOMPAT_EXTENTS) {
96 * The extent bmap is setup after the inode and block
97 * have been written out below.
99 inode.i_flags |= EXT4_EXTENTS_FL;
104 * Write out the inode and inode data block. The inode generation
105 * number is assigned by write_new_inode, which means that the
106 * operations using ino must come after it.
108 retval = ext2fs_write_new_inode(fs, ino, &inode);
113 retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
118 retval = io_channel_write_blk64(fs->io, blk, 1, block_buf);
124 * Link the symlink into the filesystem hierarchy
127 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
130 retval = EXT2_ET_FILE_EXISTS;
133 if (retval != EXT2_ET_FILE_NOT_FOUND)
135 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_SYMLINK);
141 * Update accounting....
144 ext2fs_block_alloc_stats2(fs, blk, +1);
145 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
149 ext2fs_free_mem(&block_buf);