paul@299 | 1 | /* |
paul@299 | 2 | * Copy a file into a filesystem. |
paul@299 | 3 | * |
paul@299 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@299 | 5 | * |
paul@299 | 6 | * This program is free software; you can redistribute it and/or |
paul@299 | 7 | * modify it under the terms of the GNU General Public License as |
paul@299 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@299 | 9 | * the License, or (at your option) any later version. |
paul@299 | 10 | * |
paul@299 | 11 | * This program is distributed in the hope that it will be useful, |
paul@299 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@299 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@299 | 14 | * GNU General Public License for more details. |
paul@299 | 15 | * |
paul@299 | 16 | * You should have received a copy of the GNU General Public License |
paul@299 | 17 | * along with this program; if not, write to the Free Software |
paul@299 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@299 | 19 | * Boston, MA 02110-1301, USA |
paul@299 | 20 | */ |
paul@299 | 21 | |
paul@299 | 22 | #include <stdio.h> |
paul@299 | 23 | #include <string.h> |
paul@299 | 24 | |
paul@299 | 25 | #include <sys/stat.h> |
paul@299 | 26 | |
paul@299 | 27 | #include <e2access/path.h> |
paul@299 | 28 | #include <fsclient/client.h> |
paul@299 | 29 | #include <systypes/fcntl.h> |
paul@299 | 30 | |
paul@299 | 31 | #include "file.h" |
paul@299 | 32 | #include "ops.h" |
paul@299 | 33 | #include "session.h" |
paul@299 | 34 | |
paul@299 | 35 | |
paul@299 | 36 | |
paul@299 | 37 | /* Copy buffer size. */ |
paul@299 | 38 | |
paul@299 | 39 | static int BUFSIZE = 4096; |
paul@299 | 40 | |
paul@299 | 41 | |
paul@299 | 42 | |
paul@299 | 43 | /* Alternative metadata set by options. */ |
paul@299 | 44 | |
paul@299 | 45 | extern struct metadata md; |
paul@299 | 46 | |
paul@299 | 47 | |
paul@299 | 48 | |
paul@299 | 49 | /* Copy a file into the filesystem image. */ |
paul@299 | 50 | |
paul@299 | 51 | static int _copy_file_in(const char *source, const char *target, flags_t flags) |
paul@299 | 52 | { |
paul@299 | 53 | int retval = 0; |
paul@299 | 54 | |
paul@299 | 55 | /* Copying details. */ |
paul@299 | 56 | |
paul@299 | 57 | FILE *fp; |
paul@299 | 58 | file_t *target_fp; |
paul@299 | 59 | char buf[BUFSIZE]; |
paul@299 | 60 | size_t got; |
paul@299 | 61 | offset_t written; |
paul@299 | 62 | |
paul@299 | 63 | /* Open a file in the target directory. */ |
paul@299 | 64 | |
paul@299 | 65 | target_fp = client_open(target, flags); |
paul@299 | 66 | |
paul@299 | 67 | if (target_fp == NULL) |
paul@299 | 68 | return 1; |
paul@299 | 69 | |
paul@299 | 70 | /* Open the file in the source directory. */ |
paul@299 | 71 | |
paul@299 | 72 | fp = fopen(source, "r"); |
paul@299 | 73 | |
paul@299 | 74 | /* Copy the file content. */ |
paul@299 | 75 | |
paul@299 | 76 | if (fp != NULL) |
paul@299 | 77 | { |
paul@299 | 78 | while ((got = fread(buf, sizeof(char), BUFSIZE, fp))) |
paul@299 | 79 | { |
paul@299 | 80 | while (got) |
paul@299 | 81 | { |
paul@299 | 82 | written = client_write(target_fp, buf, got); |
paul@299 | 83 | |
paul@299 | 84 | if (!written) |
paul@299 | 85 | { |
paul@299 | 86 | retval = 1; |
paul@299 | 87 | goto close_files; |
paul@299 | 88 | } |
paul@299 | 89 | got -= written; |
paul@299 | 90 | } |
paul@299 | 91 | } |
paul@299 | 92 | } |
paul@299 | 93 | |
paul@299 | 94 | close_files: |
paul@299 | 95 | fclose(fp); |
paul@299 | 96 | client_close(target_fp); |
paul@299 | 97 | |
paul@299 | 98 | return retval; |
paul@299 | 99 | } |
paul@299 | 100 | |
paul@299 | 101 | /* Copy a source file from the external environment into the filesystem. */ |
paul@299 | 102 | |
paul@299 | 103 | static int _copy_in(const char *source, const char *target, |
paul@299 | 104 | const char *basename) |
paul@299 | 105 | { |
paul@299 | 106 | /* Obtain basename and eventual target path. */ |
paul@299 | 107 | |
paul@299 | 108 | const char *source_basename = path_basename(source); |
paul@299 | 109 | char target_plus_basename[strlen(target) + 1 + |
paul@299 | 110 | (basename != NULL ? strlen(basename) |
paul@299 | 111 | : strlen(source_basename)) + 1]; |
paul@299 | 112 | const char *target_path; |
paul@299 | 113 | |
paul@299 | 114 | /* Target file properties. */ |
paul@299 | 115 | |
paul@299 | 116 | flags_t flags; |
paul@299 | 117 | struct stat st; |
paul@299 | 118 | long err; |
paul@299 | 119 | |
paul@299 | 120 | /* By default, treat the target as a new object. */ |
paul@299 | 121 | |
paul@299 | 122 | int target_is_new = 1; |
paul@299 | 123 | |
paul@299 | 124 | /* Without a basename, the target exists and is either a directory, into |
paul@299 | 125 | which the source file shall be copied, or it is a file that shall be |
paul@299 | 126 | overwritten. */ |
paul@299 | 127 | |
paul@299 | 128 | if (basename == NULL) |
paul@299 | 129 | { |
paul@299 | 130 | err = client_stat(target, &st); |
paul@299 | 131 | |
paul@299 | 132 | if (err) |
paul@299 | 133 | { |
paul@299 | 134 | fprintf(stderr, "Could not stat target: %s\n", target); |
paul@299 | 135 | return 1; |
paul@299 | 136 | } |
paul@299 | 137 | |
paul@299 | 138 | target_is_new = S_ISDIR(st.st_mode); |
paul@299 | 139 | |
paul@299 | 140 | if (target_is_new) |
paul@299 | 141 | basename = path_basename(source); |
paul@299 | 142 | } |
paul@299 | 143 | |
paul@299 | 144 | /* Obtain the target path. */ |
paul@299 | 145 | |
paul@299 | 146 | if (basename != NULL) |
paul@299 | 147 | { |
paul@299 | 148 | sprintf(target_plus_basename, "%s/%s", target, basename); |
paul@299 | 149 | target_path = target_plus_basename; |
paul@299 | 150 | } |
paul@299 | 151 | else |
paul@299 | 152 | target_path = target; |
paul@299 | 153 | |
paul@299 | 154 | /* Directories are created with the same metadata. */ |
paul@299 | 155 | |
paul@299 | 156 | if (isdir(source)) |
paul@299 | 157 | { |
paul@299 | 158 | if (!target_is_new) |
paul@299 | 159 | { |
paul@299 | 160 | fprintf(stderr, "Directory cannot be copied as it already exists: %s\n", target_path); |
paul@299 | 161 | return 1; |
paul@299 | 162 | } |
paul@299 | 163 | |
paul@299 | 164 | err = client_stat(source, &st); |
paul@299 | 165 | |
paul@299 | 166 | if (err) |
paul@299 | 167 | { |
paul@299 | 168 | fprintf(stderr, "Could not stat source: %s\n", source); |
paul@299 | 169 | return 1; |
paul@299 | 170 | } |
paul@299 | 171 | |
paul@299 | 172 | if (client_mkdir(target_path, st.st_mode & ~md.mask)) |
paul@299 | 173 | return 1; |
paul@299 | 174 | } |
paul@299 | 175 | |
paul@299 | 176 | /* Files are copied. */ |
paul@299 | 177 | |
paul@299 | 178 | else if (isfile(source)) |
paul@299 | 179 | { |
paul@299 | 180 | flags = O_WRONLY; |
paul@299 | 181 | |
paul@299 | 182 | /* Obtain the inode for the target file. */ |
paul@299 | 183 | |
paul@299 | 184 | if (target_is_new) |
paul@299 | 185 | flags |= O_CREAT; |
paul@299 | 186 | |
paul@299 | 187 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@299 | 188 | |
paul@299 | 189 | if (_copy_file_in(source, target_path, flags)) |
paul@299 | 190 | { |
paul@299 | 191 | fprintf(stderr, "Failed to copy file: %s\n", source); |
paul@299 | 192 | return 1; |
paul@299 | 193 | } |
paul@299 | 194 | } |
paul@299 | 195 | |
paul@299 | 196 | return 0; |
paul@299 | 197 | } |
paul@299 | 198 | |
paul@299 | 199 | /* Copy source files from the external environment into the filesystem image. */ |
paul@299 | 200 | |
paul@299 | 201 | int copy_in(int argc, char *argv[]) |
paul@299 | 202 | { |
paul@299 | 203 | int i; |
paul@299 | 204 | |
paul@299 | 205 | /* Target filename details. */ |
paul@299 | 206 | |
paul@299 | 207 | char *target = argv[argc - 1]; |
paul@299 | 208 | const char *basename; |
paul@299 | 209 | struct stat st; |
paul@299 | 210 | |
paul@299 | 211 | /* Locate the target and test whether it is a file or a directory. */ |
paul@299 | 212 | |
paul@299 | 213 | long err = client_stat(target, &st); |
paul@299 | 214 | |
paul@299 | 215 | /* Only a non-existent file in an existing directory is permitted. */ |
paul@299 | 216 | |
paul@299 | 217 | if (err == -L4_ENOENT) |
paul@299 | 218 | { |
paul@299 | 219 | /* Split the path, making target the parent directory. */ |
paul@299 | 220 | |
paul@299 | 221 | basename = path_split(target); |
paul@299 | 222 | err = client_stat(target, &st); |
paul@299 | 223 | |
paul@299 | 224 | if (err) |
paul@299 | 225 | { |
paul@299 | 226 | fprintf(stderr, "Could not stat target parent: %s\n", target); |
paul@299 | 227 | return 1; |
paul@299 | 228 | } |
paul@299 | 229 | |
paul@299 | 230 | if (!S_ISDIR(st.st_mode)) |
paul@299 | 231 | { |
paul@299 | 232 | fprintf(stderr, "Target parent is not directory: %s\n", target); |
paul@299 | 233 | return 1; |
paul@299 | 234 | } |
paul@299 | 235 | } |
paul@299 | 236 | else if (err) |
paul@299 | 237 | { |
paul@299 | 238 | fprintf(stderr, "Could not stat target: %s\n", target); |
paul@299 | 239 | return 1; |
paul@299 | 240 | } |
paul@299 | 241 | |
paul@299 | 242 | /* An existing object can be a file or directory. */ |
paul@299 | 243 | |
paul@299 | 244 | else |
paul@299 | 245 | { |
paul@299 | 246 | basename = NULL; |
paul@299 | 247 | |
paul@299 | 248 | /* Only permit a target file when one source file is given. */ |
paul@299 | 249 | |
paul@299 | 250 | if (!S_ISDIR(st.st_mode) && (argc > 2)) |
paul@299 | 251 | { |
paul@299 | 252 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@299 | 253 | return 1; |
paul@299 | 254 | } |
paul@299 | 255 | } |
paul@299 | 256 | |
paul@299 | 257 | /* Copy each source object to the target directory. */ |
paul@299 | 258 | |
paul@299 | 259 | for (i = 0; i < argc - 1; i++) |
paul@299 | 260 | if (_copy_in(argv[i], target, basename)) |
paul@299 | 261 | return 1; |
paul@299 | 262 | |
paul@299 | 263 | return 0; |
paul@299 | 264 | } |
paul@299 | 265 | |
paul@299 | 266 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@299 | 267 | */ |