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