paul@294 | 1 | /* |
paul@294 | 2 | * Make a directory in the filesystem using the client library. |
paul@294 | 3 | * |
paul@294 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@294 | 5 | * |
paul@294 | 6 | * This program is free software; you can redistribute it and/or |
paul@294 | 7 | * modify it under the terms of the GNU General Public License as |
paul@294 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@294 | 9 | * the License, or (at your option) any later version. |
paul@294 | 10 | * |
paul@294 | 11 | * This program is distributed in the hope that it will be useful, |
paul@294 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@294 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@294 | 14 | * GNU General Public License for more details. |
paul@294 | 15 | * |
paul@294 | 16 | * You should have received a copy of the GNU General Public License |
paul@294 | 17 | * along with this program; if not, write to the Free Software |
paul@294 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@294 | 19 | * Boston, MA 02110-1301, USA |
paul@294 | 20 | */ |
paul@294 | 21 | |
paul@294 | 22 | #include <stdio.h> |
paul@294 | 23 | #include <stdlib.h> |
paul@294 | 24 | #include <string.h> |
paul@294 | 25 | #include <unistd.h> |
paul@294 | 26 | |
paul@294 | 27 | #include <sys/stat.h> |
paul@294 | 28 | #include <sys/types.h> |
paul@294 | 29 | |
paul@294 | 30 | #include <e2access/path.h> |
paul@294 | 31 | #include <fsclient/client.h> |
paul@294 | 32 | |
paul@294 | 33 | #include "ops.h" |
paul@294 | 34 | #include "session.h" |
paul@294 | 35 | |
paul@294 | 36 | |
paul@294 | 37 | |
paul@294 | 38 | /* Alternative metadata set by options. */ |
paul@294 | 39 | |
paul@294 | 40 | extern struct metadata md; |
paul@294 | 41 | |
paul@294 | 42 | |
paul@294 | 43 | |
paul@294 | 44 | static int _make_dir(const char *path) |
paul@294 | 45 | { |
paul@294 | 46 | char parent[strlen(path) + 1]; |
paul@294 | 47 | |
paul@294 | 48 | /* Attempt to create the directory and return if successful. */ |
paul@294 | 49 | |
paul@294 | 50 | long err = client_mkdir(path, 0777 & ~md.mask); |
paul@294 | 51 | |
paul@294 | 52 | if (!err) |
paul@294 | 53 | return 0; |
paul@294 | 54 | |
paul@294 | 55 | /* Without a usable parent directory, attempt to create the parent, and then |
paul@294 | 56 | attempt to create the directory again. */ |
paul@294 | 57 | |
paul@294 | 58 | if (err == -L4_ENOENT) |
paul@294 | 59 | { |
paul@294 | 60 | strcpy(parent, path); |
paul@294 | 61 | path_split(parent); |
paul@294 | 62 | |
paul@294 | 63 | if (_make_dir(parent)) |
paul@294 | 64 | return 1; |
paul@294 | 65 | |
paul@294 | 66 | return _make_dir(path); |
paul@294 | 67 | } |
paul@294 | 68 | |
paul@294 | 69 | fprintf(stderr, "Could not create directory: %s\n", path); |
paul@294 | 70 | return 1; |
paul@294 | 71 | } |
paul@294 | 72 | |
paul@294 | 73 | static int _make_dirs(const char *path) |
paul@294 | 74 | { |
paul@294 | 75 | struct stat st; |
paul@294 | 76 | |
paul@294 | 77 | /* Search for the remaining components. */ |
paul@294 | 78 | |
paul@294 | 79 | long err = client_stat(path, &st); |
paul@294 | 80 | |
paul@294 | 81 | if (!err) |
paul@294 | 82 | { |
paul@294 | 83 | fprintf(stderr, "Path exists: %s\n", path); |
paul@294 | 84 | return 1; |
paul@294 | 85 | } |
paul@294 | 86 | |
paul@294 | 87 | /* Try and make the directory. */ |
paul@294 | 88 | |
paul@294 | 89 | return _make_dir(path); |
paul@294 | 90 | } |
paul@294 | 91 | |
paul@294 | 92 | /* Make directories in the filesystem. */ |
paul@294 | 93 | |
paul@294 | 94 | int make_dirs(int argc, char *argv[]) |
paul@294 | 95 | { |
paul@294 | 96 | int i; |
paul@294 | 97 | |
paul@294 | 98 | /* Make each directory component in the given pathname. */ |
paul@294 | 99 | |
paul@294 | 100 | for (i = 0; i < argc; i++) |
paul@294 | 101 | if (_make_dirs(argv[i])) |
paul@294 | 102 | return 1; |
paul@294 | 103 | |
paul@294 | 104 | return 0; |
paul@294 | 105 | } |
paul@294 | 106 | |
paul@294 | 107 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@294 | 108 | */ |