1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/fsaccess/op_make_dirs.c Fri Mar 18 00:14:07 2022 +0100
1.3 @@ -0,0 +1,108 @@
1.4 +/*
1.5 + * Make a directory in the filesystem using the client library.
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 <stdlib.h>
1.27 +#include <string.h>
1.28 +#include <unistd.h>
1.29 +
1.30 +#include <sys/stat.h>
1.31 +#include <sys/types.h>
1.32 +
1.33 +#include <e2access/path.h>
1.34 +#include <fsclient/client.h>
1.35 +
1.36 +#include "ops.h"
1.37 +#include "session.h"
1.38 +
1.39 +
1.40 +
1.41 +/* Alternative metadata set by options. */
1.42 +
1.43 +extern struct metadata md;
1.44 +
1.45 +
1.46 +
1.47 +static int _make_dir(const char *path)
1.48 +{
1.49 + char parent[strlen(path) + 1];
1.50 +
1.51 + /* Attempt to create the directory and return if successful. */
1.52 +
1.53 + long err = client_mkdir(path, 0777 & ~md.mask);
1.54 +
1.55 + if (!err)
1.56 + return 0;
1.57 +
1.58 + /* Without a usable parent directory, attempt to create the parent, and then
1.59 + attempt to create the directory again. */
1.60 +
1.61 + if (err == -L4_ENOENT)
1.62 + {
1.63 + strcpy(parent, path);
1.64 + path_split(parent);
1.65 +
1.66 + if (_make_dir(parent))
1.67 + return 1;
1.68 +
1.69 + return _make_dir(path);
1.70 + }
1.71 +
1.72 + fprintf(stderr, "Could not create directory: %s\n", path);
1.73 + return 1;
1.74 +}
1.75 +
1.76 +static int _make_dirs(const char *path)
1.77 +{
1.78 + struct stat st;
1.79 +
1.80 + /* Search for the remaining components. */
1.81 +
1.82 + long err = client_stat(path, &st);
1.83 +
1.84 + if (!err)
1.85 + {
1.86 + fprintf(stderr, "Path exists: %s\n", path);
1.87 + return 1;
1.88 + }
1.89 +
1.90 + /* Try and make the directory. */
1.91 +
1.92 + return _make_dir(path);
1.93 +}
1.94 +
1.95 +/* Make directories in the filesystem. */
1.96 +
1.97 +int make_dirs(int argc, char *argv[])
1.98 +{
1.99 + int i;
1.100 +
1.101 + /* Make each directory component in the given pathname. */
1.102 +
1.103 + for (i = 0; i < argc; i++)
1.104 + if (_make_dirs(argv[i]))
1.105 + return 1;
1.106 +
1.107 + return 0;
1.108 +}
1.109 +
1.110 +/* vim: tabstop=4 expandtab shiftwidth=4
1.111 +*/