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