1 /*
2 * symlink.c --- make a symlink in the filesystem, based on mkdir.c
3 *
4 * Copyright (c) 2012, Intel Corporation.
5 * All Rights Reserved.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
10 * %End-Header%
11 */
12
13 #include "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <fcntl.h>
20 #include <time.h>
21 #if HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #if HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27
28 #include "ext2_fs.h"
29 #include "ext2fs.h"
30
ext2fs_symlink(ext2_filsys fs,ext2_ino_t parent,ext2_ino_t ino,const char * name,const char * target)31 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
32 const char *name, const char *target)
33 {
34 errcode_t retval;
35 struct ext2_inode inode;
36 ext2_ino_t scratch_ino;
37 blk64_t blk;
38 int fastlink, inlinelink;
39 unsigned int target_len;
40 char *block_buf = 0;
41
42 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
43
44 /* The Linux kernel doesn't allow for links longer than a block */
45 target_len = strnlen(target, fs->blocksize + 1);
46 if (target_len > fs->blocksize) {
47 retval = EXT2_ET_INVALID_ARGUMENT;
48 goto cleanup;
49 }
50
51 /*
52 * Allocate a data block for slow links
53 */
54 retval = ext2fs_get_mem(fs->blocksize+1, &block_buf);
55 if (retval)
56 goto cleanup;
57 memset(block_buf, 0, fs->blocksize+1);
58 strncpy(block_buf, target, fs->blocksize);
59
60 memset(&inode, 0, sizeof(struct ext2_inode));
61 fastlink = (target_len < sizeof(inode.i_block));
62 if (!fastlink) {
63 retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
64 &inode,
65 0),
66 NULL, &blk);
67 if (retval)
68 goto cleanup;
69 }
70
71 /*
72 * Allocate an inode, if necessary
73 */
74 if (!ino) {
75 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFLNK | 0755,
76 0, &ino);
77 if (retval)
78 goto cleanup;
79 }
80
81 /*
82 * Create the inode structure....
83 */
84 inode.i_mode = LINUX_S_IFLNK | 0777;
85 inode.i_uid = inode.i_gid = 0;
86 inode.i_links_count = 1;
87 ext2fs_inode_size_set(fs, &inode, target_len);
88 /* The time fields are set by ext2fs_write_new_inode() */
89
90 inlinelink = !fastlink &&
91 ext2fs_has_feature_inline_data(fs->super) &&
92 (target_len < fs->blocksize);
93 if (fastlink) {
94 /* Fast symlinks, target stored in inode */
95 strcpy((char *)&inode.i_block, target);
96 } else if (inlinelink) {
97 /* Try inserting an inline data symlink */
98 inode.i_flags |= EXT4_INLINE_DATA_FL;
99 retval = ext2fs_write_new_inode(fs, ino, &inode);
100 if (retval)
101 goto cleanup;
102 retval = ext2fs_inline_data_set(fs, ino, &inode, block_buf,
103 target_len);
104 if (retval) {
105 inode.i_flags &= ~EXT4_INLINE_DATA_FL;
106 inlinelink = 0;
107 goto need_block;
108 }
109 retval = ext2fs_read_inode(fs, ino, &inode);
110 if (retval)
111 goto cleanup;
112 } else {
113 need_block:
114 /* Slow symlinks, target stored in the first block */
115 ext2fs_iblk_set(fs, &inode, 1);
116 if (ext2fs_has_feature_extents(fs->super)) {
117 /*
118 * The extent bmap is setup after the inode and block
119 * have been written out below.
120 */
121 inode.i_flags |= EXT4_EXTENTS_FL;
122 }
123 }
124
125 /*
126 * Write out the inode and inode data block. The inode generation
127 * number is assigned by write_new_inode, which means that the
128 * operations using ino must come after it.
129 */
130 if (inlinelink)
131 retval = ext2fs_write_inode(fs, ino, &inode);
132 else
133 retval = ext2fs_write_new_inode(fs, ino, &inode);
134 if (retval)
135 goto cleanup;
136
137 if (!fastlink && !inlinelink) {
138 retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
139 &blk);
140 if (retval)
141 goto cleanup;
142
143 retval = io_channel_write_blk64(fs->io, blk, 1, block_buf);
144 if (retval)
145 goto cleanup;
146 }
147
148 /*
149 * Link the symlink into the filesystem hierarchy
150 */
151 if (name) {
152 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
153 &scratch_ino);
154 if (!retval) {
155 retval = EXT2_ET_FILE_EXISTS;
156 goto cleanup;
157 }
158 if (retval != EXT2_ET_FILE_NOT_FOUND)
159 goto cleanup;
160 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_SYMLINK);
161 if (retval)
162 goto cleanup;
163 }
164
165 /*
166 * Update accounting....
167 */
168 if (!fastlink && !inlinelink)
169 ext2fs_block_alloc_stats2(fs, blk, +1);
170 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
171
172 cleanup:
173 if (block_buf)
174 ext2fs_free_mem(&block_buf);
175 return retval;
176 }
177