| 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 | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | #include <stdlib.h> |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 18 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 19 | #include <linux/ext2_fs.h> |
| 20 | |
| 21 | #include "ext2fs.h" |
| 22 | |
| 23 | struct link_struct { |
| 24 | const char *name; |
| 25 | int namelen; |
| 26 | ino_t inode; |
| 27 | int flags; |
| 28 | int done; |
| 29 | }; |
| 30 | |
| 31 | static int link_proc(struct ext2_dir_entry *dirent, |
| 32 | int offset, |
| 33 | int blocksize, |
| 34 | char *buf, |
| 35 | void *private) |
| 36 | { |
| 37 | struct link_struct *ls = (struct link_struct *) private; |
| 38 | struct ext2_dir_entry *next; |
| 39 | int rec_len; |
| 40 | int ret = 0; |
| 41 | |
| 42 | rec_len = EXT2_DIR_REC_LEN(ls->namelen); |
| 43 | |
| 44 | /* |
| 45 | * See if the following directory entry (if any) is unused; |
| 46 | * if so, absorb it into this one. |
| 47 | */ |
| 48 | next = (struct ext2_dir_entry *) (buf + offset + dirent->rec_len); |
| 49 | if ((offset + dirent->rec_len < blocksize - 8) && |
| 50 | (next->inode == 0) && |
| 51 | (offset + dirent->rec_len + next->rec_len <= blocksize)) { |
| 52 | dirent->rec_len += next->rec_len; |
| 53 | ret = DIRENT_CHANGED; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * If the directory entry is used, see if we can split the |
| 58 | * directory entry to make room for the new name. If so, |
| 59 | * truncate it and return. |
| 60 | */ |
| 61 | if (dirent->inode) { |
| 62 | if (dirent->rec_len < (EXT2_DIR_REC_LEN(dirent->name_len) + |
| 63 | rec_len)) |
| 64 | return ret; |
| 65 | rec_len = dirent->rec_len - EXT2_DIR_REC_LEN(dirent->name_len); |
| 66 | dirent->rec_len = EXT2_DIR_REC_LEN(dirent->name_len); |
| 67 | next = (struct ext2_dir_entry *) (buf + offset + |
| 68 | dirent->rec_len); |
| 69 | next->inode = 0; |
| 70 | next->name_len = 0; |
| 71 | next->rec_len = rec_len; |
| 72 | return DIRENT_CHANGED; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * If we get this far, then the directory entry is not used. |
| 77 | * See if we can fit the request entry in. If so, do it. |
| 78 | */ |
| 79 | if (dirent->rec_len < rec_len) |
| 80 | return ret; |
| 81 | dirent->inode = ls->inode; |
| 82 | dirent->name_len = ls->namelen; |
| 83 | strncpy(dirent->name, ls->name, ls->namelen); |
| 84 | |
| 85 | ls->done++; |
| 86 | return DIRENT_ABORT|DIRENT_CHANGED; |
| 87 | } |
| 88 | |
| 89 | errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name, ino_t ino, |
| 90 | int flags) |
| 91 | { |
| 92 | errcode_t retval; |
| 93 | struct link_struct ls; |
| 94 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 95 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 96 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 97 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 98 | return EXT2_ET_RO_FILSYS; |
| 99 | |
| 100 | ls.name = name; |
| 101 | ls.namelen = name ? strlen(name) : 0; |
| 102 | ls.inode = ino; |
| 103 | ls.flags = 0; |
| 104 | ls.done = 0; |
| 105 | |
| 106 | retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY, |
| 107 | 0, link_proc, &ls); |
| 108 | if (retval) |
| 109 | return retval; |
| 110 | |
| 111 | return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE; |
| 112 | } |