1 /* 2 * Make a directory in the filesystem using the client library. 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 <stdlib.h> 24 #include <string.h> 25 #include <unistd.h> 26 27 #include <sys/stat.h> 28 #include <sys/types.h> 29 30 #include <e2access/path.h> 31 #include <fsclient/client.h> 32 33 #include "ops.h" 34 #include "session.h" 35 36 37 38 /* Alternative metadata set by options. */ 39 40 extern struct metadata md; 41 42 43 44 static int _make_dir(const char *path) 45 { 46 char parent[strlen(path) + 1]; 47 48 /* Attempt to create the directory and return if successful. */ 49 50 long err = client_mkdir(path, 0777 & ~md.mask); 51 52 if (!err) 53 return 0; 54 55 /* Without a usable parent directory, attempt to create the parent, and then 56 attempt to create the directory again. */ 57 58 if (err == -L4_ENOENT) 59 { 60 strcpy(parent, path); 61 path_split(parent); 62 63 if (_make_dir(parent)) 64 return 1; 65 66 return _make_dir(path); 67 } 68 69 fprintf(stderr, "Could not create directory: %s\n", path); 70 return 1; 71 } 72 73 static int _make_dirs(const char *path) 74 { 75 struct stat st; 76 77 /* Search for the remaining components. */ 78 79 long err = client_stat(path, &st); 80 81 if (!err) 82 { 83 fprintf(stderr, "Path exists: %s\n", path); 84 return 1; 85 } 86 87 /* Try and make the directory. */ 88 89 return _make_dir(path); 90 } 91 92 /* Make directories in the filesystem. */ 93 94 int make_dirs(int argc, char *argv[]) 95 { 96 int i; 97 98 /* Make each directory component in the given pathname. */ 99 100 for (i = 0; i < argc; i++) 101 if (_make_dirs(argv[i])) 102 return 1; 103 104 return 0; 105 } 106 107 /* vim: tabstop=4 expandtab shiftwidth=4 108 */