Home | History | Annotate | Download | only in ext2fs
      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 <stdio.h>
     13 #include <string.h>
     14 #if HAVE_UNISTD_H
     15 #include <unistd.h>
     16 #endif
     17 
     18 #include "ext2_fs.h"
     19 #include "ext2fs.h"
     20 
     21 struct link_struct  {
     22 	ext2_filsys	fs;
     23 	const char	*name;
     24 	int		namelen;
     25 	ext2_ino_t	inode;
     26 	int		flags;
     27 	int		done;
     28 	unsigned int	blocksize;
     29 	errcode_t	err;
     30 	struct ext2_super_block *sb;
     31 };
     32 
     33 static int link_proc(struct ext2_dir_entry *dirent,
     34 		     int	offset,
     35 		     int	blocksize,
     36 		     char	*buf,
     37 		     void	*priv_data)
     38 {
     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;
     42 	int ret = 0;
     43 
     44 	if (ls->done)
     45 		return DIRENT_ABORT;
     46 
     47 	rec_len = EXT2_DIR_REC_LEN(ls->namelen);
     48 
     49 	ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len);
     50 	if (ls->err)
     51 		return DIRENT_ABORT;
     52 
     53 	/*
     54 	 * See if the following directory entry (if any) is unused;
     55 	 * if so, absorb it into this one.
     56 	 */
     57 	next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len);
     58 	if ((offset + (int) curr_rec_len < blocksize - 8) &&
     59 	    (next->inode == 0) &&
     60 	    (offset + (int) curr_rec_len + (int) next->rec_len <= blocksize)) {
     61 		curr_rec_len += next->rec_len;
     62 		ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
     63 		if (ls->err)
     64 			return DIRENT_ABORT;
     65 		ret = DIRENT_CHANGED;
     66 	}
     67 
     68 	/*
     69 	 * If the directory entry is used, see if we can split the
     70 	 * directory entry to make room for the new name.  If so,
     71 	 * truncate it and return.
     72 	 */
     73 	if (dirent->inode) {
     74 		min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
     75 		if (curr_rec_len < (min_rec_len + rec_len))
     76 			return ret;
     77 		rec_len = curr_rec_len - min_rec_len;
     78 		ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent);
     79 		if (ls->err)
     80 			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
    114 errcode_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