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 28 29 30 /* Remove objects from the filesystem image. */ 31 32 int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only) 33 { 34 int i; 35 const char *path; 36 ext2_ino_t ino; 37 38 /* Remove each directory with the given pathname. */ 39 40 for (i = 0; i < argc; i++) 41 { 42 path = argv[i]; 43 44 /* Detect missing objects. */ 45 46 if ((!*path) || image_find_by_path(fs, path, &ino)) 47 { 48 fprintf(stderr, "Not found: %s\n", path); 49 return 1; 50 } 51 52 /* Insist on a directory if specified. */ 53 54 if (dir_only) 55 { 56 if (!image_isdir_by_inode(fs, ino)) 57 { 58 fprintf(stderr, "Not a directory: %s\n", path); 59 return 1; 60 } 61 62 /* Test for an empty directory. */ 63 64 if (image_dir_empty_by_inode(fs, ino)) 65 { 66 fprintf(stderr, "Directory not empty: %s\n", path); 67 return 1; 68 } 69 } 70 71 /* Otherwise, insist on a non-directory. */ 72 73 else if (image_isdir_by_inode(fs, ino)) 74 { 75 fprintf(stderr, "Cannot remove a directory: %s\n", path); 76 return 1; 77 } 78 79 /* Unlink the object. */ 80 81 if (image_unlink_by_path(fs, path)) 82 { 83 fprintf(stderr, "Could not unlink object: %s\n", path); 84 return 1; 85 } 86 87 /* Remove the object. */ 88 89 if (image_remove_by_inode(fs, ino)) 90 { 91 fprintf(stderr, "Could not remove object: %s\n", path); 92 return 1; 93 } 94 } 95 96 return 0; 97 } 98 99 /* Remove directories from the filesystem image. */ 100 101 int remove_dirs(ext2_filsys fs, int argc, char *argv[]) 102 { 103 return _remove(fs, argc, argv, 1); 104 } 105 106 /* Remove non-directories from the filesystem image. */ 107 108 int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]) 109 { 110 return _remove(fs, argc, argv, 0); 111 } 112 113 /* vim: tabstop=4 expandtab shiftwidth=4 114 */