1a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar/* 25254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * link.c --- create links in a ext2fs directory 35254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * 45254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * Copyright (C) 1993, 1994 Theodore Ts'o. 55254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * 65254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * %Begin-Header% 75254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * This file may be redistributed under the terms of the GNU Library 85254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * General Public License, version 2. 95254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * %End-Header% 105254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu */ 115254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 125254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include <stdio.h> 135254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include <string.h> 145254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#if HAVE_UNISTD_H 155254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include <unistd.h> 165254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#endif 175254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 185254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "ext2_fs.h" 195254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "ext2fs.h" 205254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 215254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieustruct link_struct { 225254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu ext2_filsys fs; 235254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu const char *name; 245254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu int namelen; 255254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu ext2_ino_t inode; 265254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu int flags; 275254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu int done; 28651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines unsigned int blocksize; 29651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines errcode_t err; 30651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines struct ext2_super_block *sb; 31651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}; 32651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 33651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstatic int link_proc(struct ext2_dir_entry *dirent, 34651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines int offset, 35651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines int blocksize, 365254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu char *buf, 37651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void *priv_data) 38651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines{ 39651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines struct link_struct *ls = (struct link_struct *) priv_data; 40651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines struct ext2_dir_entry *next; 41651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines unsigned int rec_len, min_rec_len, curr_rec_len; 42651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines int ret = 0; 43651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 44651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (ls->done) 455254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu return DIRENT_ABORT; 465254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 475254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu rec_len = EXT2_DIR_REC_LEN(ls->namelen); 485254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 495254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len); 505254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if (ls->err) 515254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu return DIRENT_ABORT; 525254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 535254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu /* 545254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * See if the following directory entry (if any) is unused; 555254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * if so, absorb it into this one. 565254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu */ 575254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len); 585254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if ((offset + (int) curr_rec_len < blocksize - 8) && 595254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu (next->inode == 0) && 605254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu (offset + (int) curr_rec_len + (int) next->rec_len <= blocksize)) { 615254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu curr_rec_len += next->rec_len; 625254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent); 635254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if (ls->err) 645254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu return DIRENT_ABORT; 655254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu ret = DIRENT_CHANGED; 665254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu } 675254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 685254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu /* 695254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * If the directory entry is used, see if we can split the 705254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * directory entry to make room for the new name. If so, 715254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu * truncate it and return. 725254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu */ 735254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if (dirent->inode) { 745254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF); 755254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if (curr_rec_len < (min_rec_len + rec_len)) 765254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu return ret; 775254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu rec_len = curr_rec_len - min_rec_len; 785254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent); 795254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if (ls->err) 805254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu return DIRENT_ABORT; 81 next = (struct ext2_dir_entry *) (buf + offset + 82 dirent->rec_len); 83 next->inode = 0; 84 next->name_len = 0; 85 ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next); 86 if (ls->err) 87 return DIRENT_ABORT; 88 return DIRENT_CHANGED; 89 } 90 91 /* 92 * If we get this far, then the directory entry is not used. 93 * See if we can fit the request entry in. If so, do it. 94 */ 95 if (curr_rec_len < rec_len) 96 return ret; 97 dirent->inode = ls->inode; 98 dirent->name_len = ls->namelen; 99 strncpy(dirent->name, ls->name, ls->namelen); 100 if (ls->sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE) 101 dirent->name_len |= (ls->flags & 0x7) << 8; 102 103 ls->done++; 104 return DIRENT_ABORT|DIRENT_CHANGED; 105} 106 107/* 108 * Note: the low 3 bits of the flags field are used as the directory 109 * entry filetype. 110 */ 111#ifdef __TURBOC__ 112 #pragma argsused 113#endif 114errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name, 115 ext2_ino_t ino, int flags) 116{ 117 errcode_t retval; 118 struct link_struct ls; 119 struct ext2_inode inode; 120 121 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 122 123 if (!(fs->flags & EXT2_FLAG_RW)) 124 return EXT2_ET_RO_FILSYS; 125 126 ls.fs = fs; 127 ls.name = name; 128 ls.namelen = name ? strlen(name) : 0; 129 ls.inode = ino; 130 ls.flags = flags; 131 ls.done = 0; 132 ls.sb = fs->super; 133 ls.blocksize = fs->blocksize; 134 ls.err = 0; 135 136 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY, 137 0, link_proc, &ls); 138 if (retval) 139 return retval; 140 if (ls.err) 141 return ls.err; 142 143 if (!ls.done) 144 return EXT2_ET_DIR_NO_SPACE; 145 146 if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0) 147 return retval; 148 149 if (inode.i_flags & EXT2_INDEX_FL) { 150 inode.i_flags &= ~EXT2_INDEX_FL; 151 if ((retval = ext2fs_write_inode(fs, dir, &inode)) != 0) 152 return retval; 153 } 154 155 return 0; 156} 157