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 Public
27 struct ext2_super_block *sb;
30 static int link_proc(struct ext2_dir_entry *dirent,
36 struct link_struct *ls = (struct link_struct *) priv_data;
37 struct ext2_dir_entry *next;
38 int rec_len, min_rec_len;
41 rec_len = EXT2_DIR_REC_LEN(ls->namelen);
44 * See if the following directory entry (if any) is unused;
45 * if so, absorb it into this one.
47 next = (struct ext2_dir_entry *) (buf + offset + dirent->rec_len);
48 if ((offset + dirent->rec_len < blocksize - 8) &&
50 (offset + dirent->rec_len + next->rec_len <= blocksize)) {
51 dirent->rec_len += next->rec_len;
56 * If the directory entry is used, see if we can split the
57 * directory entry to make room for the new name. If so,
58 * truncate it and return.
61 min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
62 if (dirent->rec_len < (min_rec_len + rec_len))
64 rec_len = dirent->rec_len - min_rec_len;
65 dirent->rec_len = min_rec_len;
66 next = (struct ext2_dir_entry *) (buf + offset +
70 next->rec_len = rec_len;
71 return DIRENT_CHANGED;
75 * If we get this far, then the directory entry is not used.
76 * See if we can fit the request entry in. If so, do it.
78 if (dirent->rec_len < rec_len)
80 dirent->inode = ls->inode;
81 dirent->name_len = ls->namelen;
82 strncpy(dirent->name, ls->name, ls->namelen);
83 if (ls->sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
84 dirent->name_len |= (ls->flags & 0x7) << 8;
87 return DIRENT_ABORT|DIRENT_CHANGED;
91 * Note: the low 3 bits of the flags field are used as the directory
97 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
98 ext2_ino_t ino, int flags)
101 struct link_struct ls;
102 struct ext2_inode inode;
104 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
106 if (!(fs->flags & EXT2_FLAG_RW))
107 return EXT2_ET_RO_FILSYS;
110 ls.namelen = name ? strlen(name) : 0;
116 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
122 return EXT2_ET_DIR_NO_SPACE;
124 if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0)
127 if (inode.i_flags & EXT2_INDEX_FL) {
128 inode.i_flags &= ~EXT2_INDEX_FL;
129 if ((retval = ext2fs_write_inode(fs, dir, &inode)) != 0)