2 * link.c --- create links in a ext2fs directory
4 * Copyright (C) 1993, 1994 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
28 unsigned int blocksize;
30 struct ext2_super_block *sb;
33 static int link_proc(struct ext2_dir_entry *dirent,
39 struct link_struct *ls = (struct link_struct *) priv_data;
40 struct ext2_dir_entry *next;
41 unsigned int rec_len, min_rec_len, curr_rec_len;
44 rec_len = EXT2_DIR_REC_LEN(ls->namelen);
46 ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len);
51 * See if the following directory entry (if any) is unused;
52 * if so, absorb it into this one.
54 next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len);
55 if ((offset + curr_rec_len < blocksize - 8) &&
57 (offset + curr_rec_len + next->rec_len <= blocksize)) {
58 curr_rec_len += next->rec_len;
59 ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
66 * If the directory entry is used, see if we can split the
67 * directory entry to make room for the new name. If so,
68 * truncate it and return.
71 min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
72 if (curr_rec_len < (min_rec_len + rec_len))
74 rec_len = curr_rec_len - min_rec_len;
75 ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent);
78 next = (struct ext2_dir_entry *) (buf + offset +
82 ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next);
85 return DIRENT_CHANGED;
89 * If we get this far, then the directory entry is not used.
90 * See if we can fit the request entry in. If so, do it.
92 if (curr_rec_len < rec_len)
94 dirent->inode = ls->inode;
95 dirent->name_len = ls->namelen;
96 strncpy(dirent->name, ls->name, ls->namelen);
97 if (ls->sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
98 dirent->name_len |= (ls->flags & 0x7) << 8;
101 return DIRENT_ABORT|DIRENT_CHANGED;
105 * Note: the low 3 bits of the flags field are used as the directory
111 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
112 ext2_ino_t ino, int flags)
115 struct link_struct ls;
116 struct ext2_inode inode;
118 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
120 if (!(fs->flags & EXT2_FLAG_RW))
121 return EXT2_ET_RO_FILSYS;
125 ls.namelen = name ? strlen(name) : 0;
130 ls.blocksize = fs->blocksize;
133 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
141 return EXT2_ET_DIR_NO_SPACE;
143 if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0)
146 if (inode.i_flags & EXT2_INDEX_FL) {
147 inode.i_flags &= ~EXT2_INDEX_FL;
148 if ((retval = ext2fs_write_inode(fs, dir, &inode)) != 0)