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
17 #include <linux/ext2_fs.h>
29 static int link_proc(struct ext2_dir_entry *dirent,
35 struct link_struct *ls = (struct link_struct *) private;
36 struct ext2_dir_entry *next;
40 rec_len = EXT2_DIR_REC_LEN(ls->namelen);
43 * See if the following directory entry (if any) is unused;
44 * if so, absorb it into this one.
46 next = (struct ext2_dir_entry *) (buf + offset + dirent->rec_len);
47 if ((offset + dirent->rec_len < blocksize - 8) &&
49 (offset + dirent->rec_len + next->rec_len <= blocksize)) {
50 dirent->rec_len += next->rec_len;
55 * If the directory entry is used, see if we can split the
56 * directory entry to make room for the new name. If so,
57 * truncate it and return.
60 if (dirent->rec_len < (EXT2_DIR_REC_LEN(dirent->name_len) +
63 rec_len = dirent->rec_len - EXT2_DIR_REC_LEN(dirent->name_len);
64 dirent->rec_len = EXT2_DIR_REC_LEN(dirent->name_len);
65 next = (struct ext2_dir_entry *) (buf + offset +
69 next->rec_len = rec_len;
70 return DIRENT_CHANGED;
74 * If we get this far, then the directory entry is not used.
75 * See if we can fit the request entry in. If so, do it.
77 if (dirent->rec_len < rec_len)
79 dirent->inode = ls->inode;
80 dirent->name_len = ls->namelen;
81 strncpy(dirent->name, ls->name, ls->namelen);
84 return DIRENT_ABORT|DIRENT_CHANGED;
87 errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name, ino_t ino,
91 struct link_struct ls;
93 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
95 if (!(fs->flags & EXT2_FLAG_RW))
96 return EXT2_ET_RO_FILSYS;
99 ls.namelen = name ? strlen(name) : 0;
104 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
109 return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;