blob: 2f4f54a3514bb0ba323757da66b216376758c3cf [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002 * link.c --- create links in a ext2fs directory
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include "ext2fs.h"
21
22struct link_struct {
Theodore Ts'o8a480352009-06-21 21:07:38 -040023 ext2_filsys fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000024 const char *name;
25 int namelen;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000026 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +000027 int flags;
28 int done;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040029 unsigned int blocksize;
Theodore Ts'o8a480352009-06-21 21:07:38 -040030 errcode_t err;
Theodore Ts'oe5b38a52001-01-01 16:17:12 +000031 struct ext2_super_block *sb;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040032};
Theodore Ts'o3839e651997-04-26 13:21:57 +000033
34static int link_proc(struct ext2_dir_entry *dirent,
35 int offset,
36 int blocksize,
37 char *buf,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000038 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +000039{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000040 struct link_struct *ls = (struct link_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +000041 struct ext2_dir_entry *next;
Theodore Ts'o8a480352009-06-21 21:07:38 -040042 unsigned int rec_len, min_rec_len, curr_rec_len;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043 int ret = 0;
44
Darrick J. Wong7320cb72013-09-30 18:26:55 -070045 if (ls->done)
46 return 0;
47
Theodore Ts'o3839e651997-04-26 13:21:57 +000048 rec_len = EXT2_DIR_REC_LEN(ls->namelen);
49
Theodore Ts'o8a480352009-06-21 21:07:38 -040050 ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len);
51 if (ls->err)
52 return DIRENT_ABORT;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040053
Theodore Ts'o3839e651997-04-26 13:21:57 +000054 /*
55 * See if the following directory entry (if any) is unused;
56 * if so, absorb it into this one.
57 */
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040058 next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len);
Theodore Ts'od32c9152011-07-07 13:50:22 -040059 if ((offset + (int) curr_rec_len < blocksize - 8) &&
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 (next->inode == 0) &&
Theodore Ts'od32c9152011-07-07 13:50:22 -040061 (offset + (int) curr_rec_len + (int) next->rec_len <= blocksize)) {
Theodore Ts'o8a480352009-06-21 21:07:38 -040062 curr_rec_len += next->rec_len;
63 ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
64 if (ls->err)
65 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +000066 ret = DIRENT_CHANGED;
67 }
68
69 /*
70 * If the directory entry is used, see if we can split the
71 * directory entry to make room for the new name. If so,
72 * truncate it and return.
73 */
74 if (dirent->inode) {
Theodore Ts'o674a4ee1998-03-23 02:06:52 +000075 min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040076 if (curr_rec_len < (min_rec_len + rec_len))
Theodore Ts'o3839e651997-04-26 13:21:57 +000077 return ret;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040078 rec_len = curr_rec_len - min_rec_len;
Theodore Ts'o8a480352009-06-21 21:07:38 -040079 ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent);
80 if (ls->err)
81 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 next = (struct ext2_dir_entry *) (buf + offset +
83 dirent->rec_len);
84 next->inode = 0;
85 next->name_len = 0;
Theodore Ts'o8a480352009-06-21 21:07:38 -040086 ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next);
87 if (ls->err)
88 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +000089 return DIRENT_CHANGED;
90 }
91
92 /*
93 * If we get this far, then the directory entry is not used.
94 * See if we can fit the request entry in. If so, do it.
95 */
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040096 if (curr_rec_len < rec_len)
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 return ret;
98 dirent->inode = ls->inode;
99 dirent->name_len = ls->namelen;
100 strncpy(dirent->name, ls->name, ls->namelen);
Theodore Ts'oe6198e51999-10-23 00:58:54 +0000101 if (ls->sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
102 dirent->name_len |= (ls->flags & 0x7) << 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103
104 ls->done++;
105 return DIRENT_ABORT|DIRENT_CHANGED;
106}
107
Theodore Ts'oe6198e51999-10-23 00:58:54 +0000108/*
109 * Note: the low 3 bits of the flags field are used as the directory
110 * entry filetype.
111 */
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000112#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000113 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000114#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400115errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000116 ext2_ino_t ino, int flags)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117{
Theodore Ts'ofe4dd422002-07-29 19:26:33 -0400118 errcode_t retval;
119 struct link_struct ls;
120 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121
Theodore Ts'of3db3561997-04-26 13:34:30 +0000122 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
123
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124 if (!(fs->flags & EXT2_FLAG_RW))
125 return EXT2_ET_RO_FILSYS;
126
Theodore Ts'o8a480352009-06-21 21:07:38 -0400127 ls.fs = fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 ls.name = name;
129 ls.namelen = name ? strlen(name) : 0;
130 ls.inode = ino;
Theodore Ts'oe6198e51999-10-23 00:58:54 +0000131 ls.flags = flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132 ls.done = 0;
Theodore Ts'oe5b38a52001-01-01 16:17:12 +0000133 ls.sb = fs->super;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400134 ls.blocksize = fs->blocksize;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400135 ls.err = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136
137 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
138 0, link_proc, &ls);
139 if (retval)
140 return retval;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400141 if (ls.err)
142 return ls.err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143
Theodore Ts'ofe4dd422002-07-29 19:26:33 -0400144 if (!ls.done)
145 return EXT2_ET_DIR_NO_SPACE;
146
147 if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0)
148 return retval;
149
150 if (inode.i_flags & EXT2_INDEX_FL) {
151 inode.i_flags &= ~EXT2_INDEX_FL;
152 if ((retval = ext2fs_write_inode(fs, dir, &inode)) != 0)
153 return retval;
154 }
155
156 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157}