| 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 | 3839e65 | 1997-04-26 13:21:57 +0000 | [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% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 17 | |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 18 | #if EXT2_FLAT_INCLUDES |
| 19 | #include "ext2_fs.h" |
| 20 | #else |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include <linux/ext2_fs.h> |
| Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 22 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | |
| 24 | #include "ext2fs.h" |
| 25 | |
| 26 | struct link_struct { |
| 27 | const char *name; |
| 28 | int namelen; |
| 29 | ino_t inode; |
| 30 | int flags; |
| 31 | int done; |
| 32 | }; |
| 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 | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame^] | 42 | int rec_len, min_rec_len; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | int ret = 0; |
| 44 | |
| 45 | rec_len = EXT2_DIR_REC_LEN(ls->namelen); |
| 46 | |
| 47 | /* |
| 48 | * See if the following directory entry (if any) is unused; |
| 49 | * if so, absorb it into this one. |
| 50 | */ |
| 51 | next = (struct ext2_dir_entry *) (buf + offset + dirent->rec_len); |
| 52 | if ((offset + dirent->rec_len < blocksize - 8) && |
| 53 | (next->inode == 0) && |
| 54 | (offset + dirent->rec_len + next->rec_len <= blocksize)) { |
| 55 | dirent->rec_len += next->rec_len; |
| 56 | ret = DIRENT_CHANGED; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * If the directory entry is used, see if we can split the |
| 61 | * directory entry to make room for the new name. If so, |
| 62 | * truncate it and return. |
| 63 | */ |
| 64 | if (dirent->inode) { |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame^] | 65 | min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF); |
| 66 | if (dirent->rec_len < (min_rec_len + rec_len)) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 67 | return ret; |
| Theodore Ts'o | 674a4ee | 1998-03-23 02:06:52 +0000 | [diff] [blame^] | 68 | rec_len = dirent->rec_len - min_rec_len; |
| 69 | dirent->rec_len = min_rec_len; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 70 | next = (struct ext2_dir_entry *) (buf + offset + |
| 71 | dirent->rec_len); |
| 72 | next->inode = 0; |
| 73 | next->name_len = 0; |
| 74 | next->rec_len = rec_len; |
| 75 | return DIRENT_CHANGED; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * If we get this far, then the directory entry is not used. |
| 80 | * See if we can fit the request entry in. If so, do it. |
| 81 | */ |
| 82 | if (dirent->rec_len < rec_len) |
| 83 | return ret; |
| 84 | dirent->inode = ls->inode; |
| 85 | dirent->name_len = ls->namelen; |
| 86 | strncpy(dirent->name, ls->name, ls->namelen); |
| 87 | |
| 88 | ls->done++; |
| 89 | return DIRENT_ABORT|DIRENT_CHANGED; |
| 90 | } |
| 91 | |
| Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 92 | #ifdef __TURBOC__ |
| 93 | #pragma argsused |
| 94 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 95 | errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name, ino_t ino, |
| 96 | int flags) |
| 97 | { |
| 98 | errcode_t retval; |
| 99 | struct link_struct ls; |
| 100 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 101 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 102 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 103 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 104 | return EXT2_ET_RO_FILSYS; |
| 105 | |
| 106 | ls.name = name; |
| 107 | ls.namelen = name ? strlen(name) : 0; |
| 108 | ls.inode = ino; |
| 109 | ls.flags = 0; |
| 110 | ls.done = 0; |
| 111 | |
| 112 | retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY, |
| 113 | 0, link_proc, &ls); |
| 114 | if (retval) |
| 115 | return retval; |
| 116 | |
| 117 | return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE; |
| 118 | } |