1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/fsaccess/op_remove.c Thu Mar 24 22:11:47 2022 +0100
1.3 @@ -0,0 +1,102 @@
1.4 +/*
1.5 + * Remove objects from a filesystem.
1.6 + *
1.7 + * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <stdio.h>
1.26 +#include <sys/stat.h>
1.27 +
1.28 +#include <fsclient/client.h>
1.29 +
1.30 +#include "ops.h"
1.31 +
1.32 +
1.33 +
1.34 +/* Remove objects from the filesystem image. */
1.35 +
1.36 +static int _remove(int argc, char *argv[], int dir_only)
1.37 +{
1.38 + int i;
1.39 + const char *path;
1.40 + long err;
1.41 + struct stat st;
1.42 +
1.43 + /* Remove each directory with the given pathname. */
1.44 +
1.45 + for (i = 0; i < argc; i++)
1.46 + {
1.47 + path = argv[i];
1.48 +
1.49 + /* Detect missing objects. */
1.50 +
1.51 + err = client_stat(path, &st);
1.52 +
1.53 + if (err)
1.54 + {
1.55 + fprintf(stderr, "Could not stat object: %s\n", path);
1.56 + return 1;
1.57 + }
1.58 +
1.59 + /* Insist on a directory if specified. */
1.60 +
1.61 + if (dir_only)
1.62 + {
1.63 + if (!S_ISDIR(st.st_mode))
1.64 + {
1.65 + fprintf(stderr, "Not a directory: %s\n", path);
1.66 + return 1;
1.67 + }
1.68 + }
1.69 +
1.70 + /* Otherwise, insist on a non-directory. */
1.71 +
1.72 + else if (S_ISDIR(st.st_mode))
1.73 + {
1.74 + fprintf(stderr, "Cannot remove a directory: %s\n", path);
1.75 + return 1;
1.76 + }
1.77 +
1.78 + /* Remove the object. */
1.79 +
1.80 + if (client_remove(path))
1.81 + {
1.82 + fprintf(stderr, "Could not remove object: %s\n", path);
1.83 + return 1;
1.84 + }
1.85 + }
1.86 +
1.87 + return 0;
1.88 +}
1.89 +
1.90 +/* Remove directories from the filesystem image. */
1.91 +
1.92 +int remove_dirs(int argc, char *argv[])
1.93 +{
1.94 + return _remove(argc, argv, 1);
1.95 +}
1.96 +
1.97 +/* Remove non-directories from the filesystem image. */
1.98 +
1.99 +int remove_non_dirs(int argc, char *argv[])
1.100 +{
1.101 + return _remove(argc, argv, 0);
1.102 +}
1.103 +
1.104 +/* vim: tabstop=4 expandtab shiftwidth=4
1.105 +*/