Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * unlink.c --- delete links in a ext2fs directory |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994, 1997 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% |
| 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 | 19c78dc | 1997-04-29 16:17:09 +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 | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | |
| 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 unlink_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 | |
| 39 | if (ls->name && (dirent->name_len != ls->namelen)) |
| 40 | return 0; |
| 41 | if (ls->name && strncmp(ls->name, dirent->name, dirent->name_len)) |
| 42 | return 0; |
| 43 | if (ls->inode && (dirent->inode != ls->inode)) |
| 44 | return 0; |
| 45 | |
| 46 | dirent->inode = 0; |
| 47 | ls->done++; |
| 48 | return DIRENT_ABORT|DIRENT_CHANGED; |
| 49 | } |
| 50 | |
| 51 | errcode_t ext2fs_unlink(ext2_filsys fs, ino_t dir, const char *name, ino_t ino, |
| 52 | int flags) |
| 53 | { |
| 54 | errcode_t retval; |
| 55 | struct link_struct ls; |
| 56 | |
| 57 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 58 | |
| 59 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 60 | return EXT2_ET_RO_FILSYS; |
| 61 | |
| 62 | ls.name = name; |
| 63 | ls.namelen = name ? strlen(name) : 0; |
| 64 | ls.inode = ino; |
| 65 | ls.flags = 0; |
| 66 | ls.done = 0; |
| 67 | |
| 68 | retval = ext2fs_dir_iterate(fs, dir, 0, 0, unlink_proc, &ls); |
| 69 | if (retval) |
| 70 | return retval; |
| 71 | |
| 72 | return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE; |
| 73 | } |
| 74 | |