link.c revision d1154eb460efe588eaed3d439c1caaca149fa362
1/* 2 * link.c --- create links in a ext2fs directory 3 * 4 * Copyright (C) 1993, 1994 Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Library 8 * General Public License, version 2. 9 * %End-Header% 10 */ 11 12#include "config.h" 13#include <stdio.h> 14#include <string.h> 15#if HAVE_UNISTD_H 16#include <unistd.h> 17#endif 18 19#include "ext2_fs.h" 20#include "ext2fs.h" 21 22struct link_struct { 23 ext2_filsys fs; 24 const char *name; 25 int namelen; 26 ext2_ino_t inode; 27 int flags; 28 int done; 29 unsigned int blocksize; 30 errcode_t err; 31 struct ext2_super_block *sb; 32}; 33 34static int link_proc(struct ext2_dir_entry *dirent, 35 int offset, 36 int blocksize, 37 char *buf, 38 void *priv_data) 39{ 40 struct link_struct *ls = (struct link_struct *) priv_data; 41 struct ext2_dir_entry *next; 42 unsigned int rec_len, min_rec_len, curr_rec_len; 43 int ret = 0; 44 45 rec_len = EXT2_DIR_REC_LEN(ls->namelen); 46 47 ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len); 48 if (ls->err) 49 return DIRENT_ABORT; 50 51 /* 52 * See if the following directory entry (if any) is unused; 53 * if so, absorb it into this one. 54 */ 55 next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len); 56 if ((offset + (int) curr_rec_len < blocksize - 8) && 57 (next->inode == 0) && 58 (offset + (int) curr_rec_len + (int) next->rec_len <= blocksize)) { 59 curr_rec_len += next->rec_len; 60 ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent); 61 if (ls->err) 62 return DIRENT_ABORT; 63 ret = DIRENT_CHANGED; 64 } 65 66 /* 67 * If the directory entry is used, see if we can split the 68 * directory entry to make room for the new name. If so, 69 * truncate it and return. 70 */ 71 if (dirent->inode) { 72 min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF); 73 if (curr_rec_len < (min_rec_len + rec_len)) 74 return ret; 75 rec_len = curr_rec_len - min_rec_len; 76 ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent); 77 if (ls->err) 78 return DIRENT_ABORT; 79 next = (struct ext2_dir_entry *) (buf + offset + 80 dirent->rec_len); 81 next->inode = 0; 82 next->name_len = 0; 83 ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next); 84 if (ls->err) 85 return DIRENT_ABORT; 86 return DIRENT_CHANGED; 87 } 88 89 /* 90 * If we get this far, then the directory entry is not used. 91 * See if we can fit the request entry in. If so, do it. 92 */ 93 if (curr_rec_len < rec_len) 94 return ret; 95 dirent->inode = ls->inode; 96 dirent->name_len = ls->namelen; 97 strncpy(dirent->name, ls->name, ls->namelen); 98 if (ls->sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE) 99 dirent->name_len |= (ls->flags & 0x7) << 8; 100 101 ls->done++; 102 return DIRENT_ABORT|DIRENT_CHANGED; 103} 104 105/* 106 * Note: the low 3 bits of the flags field are used as the directory 107 * entry filetype. 108 */ 109#ifdef __TURBOC__ 110 #pragma argsused 111#endif 112errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name, 113 ext2_ino_t ino, int flags) 114{ 115 errcode_t retval; 116 struct link_struct ls; 117 struct ext2_inode inode; 118 119 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 120 121 if (!(fs->flags & EXT2_FLAG_RW)) 122 return EXT2_ET_RO_FILSYS; 123 124 ls.fs = fs; 125 ls.name = name; 126 ls.namelen = name ? strlen(name) : 0; 127 ls.inode = ino; 128 ls.flags = flags; 129 ls.done = 0; 130 ls.sb = fs->super; 131 ls.blocksize = fs->blocksize; 132 ls.err = 0; 133 134 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY, 135 0, link_proc, &ls); 136 if (retval) 137 return retval; 138 if (ls.err) 139 return ls.err; 140 141 if (!ls.done) 142 return EXT2_ET_DIR_NO_SPACE; 143 144 if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0) 145 return retval; 146 147 if (inode.i_flags & EXT2_INDEX_FL) { 148 inode.i_flags &= ~EXT2_INDEX_FL; 149 if ((retval = ext2fs_write_inode(fs, dir, &inode)) != 0) 150 return retval; 151 } 152 153 return 0; 154} 155