symlink.c revision 18a1444b4f1e6a0948fd38fa0de382d86cfe04de
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 <stdio.h>
14#include <string.h>
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#include <fcntl.h>
19#include <time.h>
20#if HAVE_SYS_STAT_H
21#include <sys/stat.h>
22#endif
23#if HAVE_SYS_TYPES_H
24#include <sys/types.h>
25#endif
26
27#include "ext2_fs.h"
28#include "ext2fs.h"
29
30errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
31			 const char *name, char *target)
32{
33	ext2_extent_handle_t	handle;
34	errcode_t		retval;
35	struct ext2_inode	inode;
36	ext2_ino_t		scratch_ino;
37	blk64_t			blk;
38	int			fastlink;
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 = strlen(target);
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	fastlink = (target_len < sizeof(inode.i_block));
55	if (!fastlink) {
56		retval = ext2fs_new_block2(fs, 0, 0, &blk);
57		if (retval)
58			goto cleanup;
59		retval = ext2fs_get_mem(fs->blocksize, &block_buf);
60		if (retval)
61			goto cleanup;
62	}
63
64	/*
65	 * Allocate an inode, if necessary
66	 */
67	if (!ino) {
68		retval = ext2fs_new_inode(fs, parent, LINUX_S_IFLNK | 0755,
69					  0, &ino);
70		if (retval)
71			goto cleanup;
72	}
73
74	/*
75	 * Create the inode structure....
76	 */
77	memset(&inode, 0, sizeof(struct ext2_inode));
78	inode.i_mode = LINUX_S_IFLNK | 0777;
79	inode.i_uid = inode.i_gid = 0;
80	ext2fs_iblk_set(fs, &inode, fastlink ? 0 : 1);
81	inode.i_links_count = 1;
82	inode.i_size = target_len;
83	/* The time fields are set by ext2fs_write_new_inode() */
84
85	if (fastlink) {
86		/* Fast symlinks, target stored in inode */
87		strcpy((char *)&inode.i_block, target);
88	} else {
89		/* Slow symlinks, target stored in the first block */
90		memset(block_buf, 0, fs->blocksize);
91		strcpy(block_buf, target);
92		if (fs->super->s_feature_incompat &
93		    EXT3_FEATURE_INCOMPAT_EXTENTS) {
94			/*
95			 * The extent bmap is setup after the inode and block
96			 * have been written out below.
97			 */
98			inode.i_flags |= EXT4_EXTENTS_FL;
99		}
100	}
101
102	/*
103	 * Write out the inode and inode data block.  The inode generation
104	 * number is assigned by write_new_inode, which means that the
105	 * operations using ino must come after it.
106	 */
107	retval = ext2fs_write_new_inode(fs, ino, &inode);
108	if (retval)
109		goto cleanup;
110
111	if (!fastlink) {
112		retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
113				      &blk);
114		if (retval)
115			goto cleanup;
116
117		retval = io_channel_write_blk64(fs->io, blk, 1, block_buf);
118		if (retval)
119			goto cleanup;
120	}
121
122	/*
123	 * Link the symlink into the filesystem hierarchy
124	 */
125	if (name) {
126		retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
127				       &scratch_ino);
128		if (!retval) {
129			retval = EXT2_ET_FILE_EXISTS;
130			goto cleanup;
131		}
132		if (retval != EXT2_ET_FILE_NOT_FOUND)
133			goto cleanup;
134		retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_SYMLINK);
135		if (retval)
136			goto cleanup;
137	}
138
139	/*
140	 * Update accounting....
141	 */
142	if (!fastlink)
143		ext2fs_block_alloc_stats2(fs, blk, +1);
144	ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
145
146cleanup:
147	if (block_buf)
148		ext2fs_free_mem(&block_buf);
149	return retval;
150}
151