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