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 #include <sys/stat.h> 24 25 #include <fsclient/client.h> 26 27 #include "ops.h" 28 29 30 31 /* Remove objects from the filesystem image. */ 32 33 static int _remove(int argc, char *argv[], int dir_only) 34 { 35 int i; 36 const char *path; 37 long err; 38 struct stat st; 39 40 /* Remove each directory with the given pathname. */ 41 42 for (i = 0; i < argc; i++) 43 { 44 path = argv[i]; 45 46 /* Detect missing objects. */ 47 48 err = client_stat(path, &st); 49 50 if (err) 51 { 52 fprintf(stderr, "Could not stat object: %s\n", path); 53 return 1; 54 } 55 56 /* Insist on a directory if specified. */ 57 58 if (dir_only) 59 { 60 if (!S_ISDIR(st.st_mode)) 61 { 62 fprintf(stderr, "Not a directory: %s\n", path); 63 return 1; 64 } 65 } 66 67 /* Otherwise, insist on a non-directory. */ 68 69 else if (S_ISDIR(st.st_mode)) 70 { 71 fprintf(stderr, "Cannot remove a directory: %s\n", path); 72 return 1; 73 } 74 75 /* Remove the object. */ 76 77 if (client_remove(path)) 78 { 79 fprintf(stderr, "Could not remove object: %s\n", path); 80 return 1; 81 } 82 } 83 84 return 0; 85 } 86 87 /* Remove directories from the filesystem image. */ 88 89 int remove_dirs(int argc, char *argv[]) 90 { 91 return _remove(argc, argv, 1); 92 } 93 94 /* Remove non-directories from the filesystem image. */ 95 96 int remove_non_dirs(int argc, char *argv[]) 97 { 98 return _remove(argc, argv, 0); 99 } 100 101 /* vim: tabstop=4 expandtab shiftwidth=4 102 */