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