paul@278 | 1 | /* |
paul@278 | 2 | * Remove objects from a filesystem. |
paul@278 | 3 | * |
paul@278 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@278 | 5 | * |
paul@278 | 6 | * This program is free software; you can redistribute it and/or |
paul@278 | 7 | * modify it under the terms of the GNU General Public License as |
paul@278 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@278 | 9 | * the License, or (at your option) any later version. |
paul@278 | 10 | * |
paul@278 | 11 | * This program is distributed in the hope that it will be useful, |
paul@278 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@278 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@278 | 14 | * GNU General Public License for more details. |
paul@278 | 15 | * |
paul@278 | 16 | * You should have received a copy of the GNU General Public License |
paul@278 | 17 | * along with this program; if not, write to the Free Software |
paul@278 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@278 | 19 | * Boston, MA 02110-1301, USA |
paul@278 | 20 | */ |
paul@278 | 21 | |
paul@278 | 22 | #include <stdio.h> |
paul@278 | 23 | |
paul@278 | 24 | #include <ext2fs/ext2fs.h> |
paul@278 | 25 | |
paul@278 | 26 | #include "image.h" |
paul@278 | 27 | #include "session.h" |
paul@278 | 28 | |
paul@278 | 29 | |
paul@278 | 30 | |
paul@278 | 31 | /* Alternative metadata set by options. */ |
paul@278 | 32 | |
paul@278 | 33 | extern struct metadata md; |
paul@278 | 34 | |
paul@278 | 35 | |
paul@278 | 36 | |
paul@278 | 37 | /* Remove objects from the filesystem image. */ |
paul@278 | 38 | |
paul@278 | 39 | int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only) |
paul@278 | 40 | { |
paul@278 | 41 | int i; |
paul@278 | 42 | const char *path; |
paul@278 | 43 | ext2_ino_t ino; |
paul@278 | 44 | |
paul@278 | 45 | /* Remove each directory with the given pathname. */ |
paul@278 | 46 | |
paul@278 | 47 | for (i = 0; i < argc; i++) |
paul@278 | 48 | { |
paul@278 | 49 | path = argv[i]; |
paul@278 | 50 | |
paul@278 | 51 | /* Detect missing objects. */ |
paul@278 | 52 | |
paul@278 | 53 | if ((!*path) || image_find_by_path(fs, path, &ino)) |
paul@278 | 54 | { |
paul@278 | 55 | fprintf(stderr, "Not found: %s\n", path); |
paul@278 | 56 | return 1; |
paul@278 | 57 | } |
paul@278 | 58 | |
paul@278 | 59 | /* Insist on a directory if specified. */ |
paul@278 | 60 | |
paul@278 | 61 | if (dir_only) |
paul@278 | 62 | { |
paul@278 | 63 | if (!image_isdir_by_inode(fs, ino)) |
paul@278 | 64 | { |
paul@278 | 65 | fprintf(stderr, "Not a directory: %s\n", path); |
paul@278 | 66 | return 1; |
paul@278 | 67 | } |
paul@278 | 68 | |
paul@278 | 69 | /* Test for an empty directory. */ |
paul@278 | 70 | |
paul@278 | 71 | if (image_dir_empty_by_inode(fs, ino)) |
paul@278 | 72 | { |
paul@278 | 73 | fprintf(stderr, "Directory not empty: %s\n", path); |
paul@278 | 74 | return 1; |
paul@278 | 75 | } |
paul@278 | 76 | } |
paul@278 | 77 | |
paul@278 | 78 | /* Otherwise, insist on a non-directory. */ |
paul@278 | 79 | |
paul@278 | 80 | else if (image_isdir_by_inode(fs, ino)) |
paul@278 | 81 | { |
paul@278 | 82 | fprintf(stderr, "Cannot remove a directory: %s\n", path); |
paul@278 | 83 | return 1; |
paul@278 | 84 | } |
paul@278 | 85 | |
paul@278 | 86 | /* Unlink the object. */ |
paul@278 | 87 | |
paul@278 | 88 | if (image_unlink_by_path(fs, path)) |
paul@278 | 89 | { |
paul@278 | 90 | fprintf(stderr, "Could not unlink object: %s\n", path); |
paul@278 | 91 | return 1; |
paul@278 | 92 | } |
paul@278 | 93 | |
paul@278 | 94 | /* Remove the object. */ |
paul@278 | 95 | |
paul@278 | 96 | if (image_remove_by_inode(fs, ino)) |
paul@278 | 97 | { |
paul@278 | 98 | fprintf(stderr, "Could not remove object: %s\n", path); |
paul@278 | 99 | return 1; |
paul@278 | 100 | } |
paul@278 | 101 | } |
paul@278 | 102 | |
paul@278 | 103 | return 0; |
paul@278 | 104 | } |
paul@278 | 105 | |
paul@278 | 106 | /* Remove directories from the filesystem image. */ |
paul@278 | 107 | |
paul@278 | 108 | int remove_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@278 | 109 | { |
paul@278 | 110 | return _remove(fs, argc, argv, 1); |
paul@278 | 111 | } |
paul@278 | 112 | |
paul@278 | 113 | /* Remove non-directories from the filesystem image. */ |
paul@278 | 114 | |
paul@278 | 115 | int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@278 | 116 | { |
paul@278 | 117 | return _remove(fs, argc, argv, 0); |
paul@278 | 118 | } |
paul@278 | 119 | |
paul@278 | 120 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@278 | 121 | */ |