paul@226 | 1 | /* |
paul@226 | 2 | * Access a filesystem. |
paul@226 | 3 | * |
paul@246 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@226 | 5 | * |
paul@226 | 6 | * This program is free software; you can redistribute it and/or |
paul@226 | 7 | * modify it under the terms of the GNU General Public License as |
paul@226 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@226 | 9 | * the License, or (at your option) any later version. |
paul@226 | 10 | * |
paul@226 | 11 | * This program is distributed in the hope that it will be useful, |
paul@226 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@226 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@226 | 14 | * GNU General Public License for more details. |
paul@226 | 15 | * |
paul@226 | 16 | * You should have received a copy of the GNU General Public License |
paul@226 | 17 | * along with this program; if not, write to the Free Software |
paul@226 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@226 | 19 | * Boston, MA 02110-1301, USA |
paul@226 | 20 | */ |
paul@226 | 21 | |
paul@226 | 22 | #include <stdio.h> |
paul@226 | 23 | #include <stdlib.h> |
paul@226 | 24 | #include <string.h> |
paul@241 | 25 | #include <unistd.h> |
paul@226 | 26 | |
paul@226 | 27 | #include <sys/types.h> |
paul@226 | 28 | #include <sys/stat.h> |
paul@226 | 29 | |
paul@226 | 30 | #include <ext2fs/ext2fs.h> |
paul@226 | 31 | |
paul@226 | 32 | #include "file.h" |
paul@226 | 33 | #include "format.h" |
paul@226 | 34 | #include "image.h" |
paul@226 | 35 | #include "path.h" |
paul@250 | 36 | #include "utils.h" |
paul@226 | 37 | |
paul@226 | 38 | |
paul@226 | 39 | |
paul@226 | 40 | /* Copy buffer size. */ |
paul@226 | 41 | |
paul@226 | 42 | const int BUFSIZE = 4096; |
paul@226 | 43 | |
paul@262 | 44 | /* Maximum number of arguments in scripts. */ |
paul@262 | 45 | |
paul@262 | 46 | const int MAX_ARGS = 32; |
paul@262 | 47 | |
paul@226 | 48 | |
paul@226 | 49 | |
paul@241 | 50 | /* Alternative metadata set by options. */ |
paul@241 | 51 | |
paul@241 | 52 | struct metadata |
paul@241 | 53 | { |
paul@241 | 54 | uid_t uid; |
paul@241 | 55 | gid_t gid; |
paul@241 | 56 | mode_t mask; |
paul@241 | 57 | int have_uid, have_gid; |
paul@241 | 58 | }; |
paul@241 | 59 | |
paul@241 | 60 | struct metadata md; |
paul@241 | 61 | |
paul@246 | 62 | /* Parse program options. */ |
paul@246 | 63 | |
paul@241 | 64 | static int parse_options(int argc, char *argv[]) |
paul@241 | 65 | { |
paul@241 | 66 | int opt; |
paul@241 | 67 | |
paul@241 | 68 | md.have_uid = 0; |
paul@241 | 69 | md.have_gid = 0; |
paul@241 | 70 | md.mask = 0000; |
paul@241 | 71 | |
paul@241 | 72 | while ((opt = getopt(argc, argv, "g:m:u:")) != -1) |
paul@241 | 73 | { |
paul@241 | 74 | switch (opt) |
paul@241 | 75 | { |
paul@241 | 76 | case 'g': |
paul@241 | 77 | md.gid = atoi(optarg); |
paul@241 | 78 | md.have_gid = 1; |
paul@241 | 79 | break; |
paul@241 | 80 | |
paul@241 | 81 | case 'm': |
paul@241 | 82 | md.mask = strtol(optarg, NULL, 0); |
paul@241 | 83 | break; |
paul@241 | 84 | |
paul@241 | 85 | case 'u': |
paul@241 | 86 | md.uid = atoi(optarg); |
paul@241 | 87 | md.have_uid = 1; |
paul@241 | 88 | break; |
paul@241 | 89 | |
paul@241 | 90 | default: |
paul@246 | 91 | fprintf(stderr, "Option not recognised: %s\n", argv[optind]); |
paul@241 | 92 | return -1; |
paul@241 | 93 | } |
paul@241 | 94 | } |
paul@241 | 95 | |
paul@241 | 96 | return 0; |
paul@241 | 97 | } |
paul@241 | 98 | |
paul@241 | 99 | |
paul@241 | 100 | |
paul@226 | 101 | /* Copy a file into the filesystem image. */ |
paul@226 | 102 | |
paul@226 | 103 | int copy_file_in(const char *filename, ext2_filsys fs, ext2_ino_t ino_file, int flags) |
paul@226 | 104 | { |
paul@226 | 105 | int retval = 0; |
paul@226 | 106 | ext2_file_t file; |
paul@226 | 107 | |
paul@226 | 108 | /* Copying details. */ |
paul@226 | 109 | |
paul@226 | 110 | FILE *fp; |
paul@226 | 111 | char buf[BUFSIZE]; |
paul@226 | 112 | size_t got; |
paul@226 | 113 | unsigned int written; |
paul@226 | 114 | |
paul@226 | 115 | /* Open a file in the target directory. */ |
paul@226 | 116 | |
paul@226 | 117 | if (ext2fs_file_open(fs, ino_file, flags, &file)) |
paul@226 | 118 | return 1; |
paul@226 | 119 | |
paul@226 | 120 | /* Open the file in the source directory. */ |
paul@226 | 121 | |
paul@226 | 122 | fp = fopen(filename, "r"); |
paul@226 | 123 | |
paul@226 | 124 | /* Copy the file content. */ |
paul@226 | 125 | |
paul@226 | 126 | if (fp != NULL) |
paul@226 | 127 | { |
paul@226 | 128 | while (got = fread(buf, sizeof(char), BUFSIZE, fp)) |
paul@226 | 129 | { |
paul@226 | 130 | while (got) |
paul@226 | 131 | { |
paul@226 | 132 | if (ext2fs_file_write(file, buf, got, &written)) |
paul@226 | 133 | { |
paul@226 | 134 | retval = 1; |
paul@226 | 135 | goto close_files; |
paul@226 | 136 | } |
paul@226 | 137 | got -= written; |
paul@226 | 138 | } |
paul@226 | 139 | } |
paul@226 | 140 | } |
paul@226 | 141 | |
paul@226 | 142 | close_files: |
paul@226 | 143 | fclose(fp); |
paul@226 | 144 | ext2fs_file_flush(file); |
paul@226 | 145 | ext2fs_file_close(file); |
paul@226 | 146 | |
paul@226 | 147 | return retval; |
paul@226 | 148 | } |
paul@226 | 149 | |
paul@226 | 150 | /* Copy a file out of the filesystem image. */ |
paul@226 | 151 | |
paul@226 | 152 | int copy_file_out(const char *target, const char *filename, int target_is_file, |
paul@226 | 153 | ext2_filsys fs, ext2_ino_t ino_file) |
paul@226 | 154 | { |
paul@226 | 155 | int retval = 0; |
paul@226 | 156 | ext2_file_t file; |
paul@226 | 157 | |
paul@226 | 158 | /* Copying details. */ |
paul@226 | 159 | |
paul@226 | 160 | FILE *fp; |
paul@226 | 161 | char buf[BUFSIZE]; |
paul@226 | 162 | unsigned int got; |
paul@226 | 163 | size_t written; |
paul@226 | 164 | |
paul@226 | 165 | /* Open the file in the source directory. */ |
paul@226 | 166 | |
paul@226 | 167 | if (ext2fs_file_open(fs, ino_file, 0, &file)) |
paul@226 | 168 | return 1; |
paul@226 | 169 | |
paul@226 | 170 | /* Open a file in the target directory. */ |
paul@226 | 171 | |
paul@226 | 172 | if (target_is_file) |
paul@226 | 173 | fp = fopen(target, "w"); |
paul@226 | 174 | else |
paul@226 | 175 | fp = open_file_in_dir(target, path_basename(filename), "w"); |
paul@226 | 176 | |
paul@226 | 177 | /* Copy the file content. */ |
paul@226 | 178 | |
paul@226 | 179 | if (fp != NULL) |
paul@226 | 180 | { |
paul@226 | 181 | do |
paul@226 | 182 | { |
paul@226 | 183 | if (ext2fs_file_read(file, buf, BUFSIZE, &got)) |
paul@226 | 184 | { |
paul@226 | 185 | retval = 1; |
paul@226 | 186 | goto close_files; |
paul@226 | 187 | } |
paul@226 | 188 | |
paul@226 | 189 | while (got) |
paul@226 | 190 | { |
paul@226 | 191 | written = fwrite(buf, sizeof(char), got, fp); |
paul@226 | 192 | got -= written; |
paul@226 | 193 | } |
paul@226 | 194 | |
paul@226 | 195 | } while (got); |
paul@226 | 196 | } |
paul@226 | 197 | |
paul@226 | 198 | close_files: |
paul@226 | 199 | fclose(fp); |
paul@226 | 200 | ext2fs_file_close(file); |
paul@226 | 201 | |
paul@226 | 202 | return retval; |
paul@226 | 203 | } |
paul@226 | 204 | |
paul@226 | 205 | |
paul@226 | 206 | |
paul@226 | 207 | /* Copy source files from the external environment into the filesystem image. */ |
paul@226 | 208 | |
paul@226 | 209 | int copy_in(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 210 | { |
paul@226 | 211 | errcode_t retval; |
paul@226 | 212 | |
paul@226 | 213 | /* Target filename details. */ |
paul@226 | 214 | |
paul@226 | 215 | const char *target = argv[argc - 1]; |
paul@226 | 216 | const char *target_remaining = argv[argc - 1]; |
paul@226 | 217 | const char *basename; |
paul@226 | 218 | int target_is_file; |
paul@226 | 219 | |
paul@226 | 220 | /* Target file and directory details. */ |
paul@226 | 221 | |
paul@226 | 222 | int target_is_new; |
paul@226 | 223 | ext2_ino_t ino_file, ino_target; |
paul@226 | 224 | int flags; |
paul@226 | 225 | |
paul@226 | 226 | /* Source file details. */ |
paul@226 | 227 | |
paul@226 | 228 | struct stat st; |
paul@226 | 229 | int i; |
paul@226 | 230 | |
paul@226 | 231 | /* Locate the target and test whether it is a file or a directory. */ |
paul@226 | 232 | |
paul@257 | 233 | if (image_resolve_by_path(fs, &target_remaining, &ino_target)) |
paul@226 | 234 | { |
paul@226 | 235 | /* Only a non-existent file in an existing directory is permitted. */ |
paul@226 | 236 | |
paul@258 | 237 | if (!image_isdir_by_inode(fs, ino_target) || !path_is_leafname(target_remaining)) |
paul@226 | 238 | { |
paul@246 | 239 | fprintf(stderr, "Target not found: %s\n", target); |
paul@226 | 240 | return 1; |
paul@226 | 241 | } |
paul@226 | 242 | |
paul@226 | 243 | target_is_file = 1; |
paul@226 | 244 | target_is_new = 1; |
paul@226 | 245 | } |
paul@226 | 246 | else |
paul@226 | 247 | { |
paul@258 | 248 | target_is_file = image_isfile_by_inode(fs, ino_target); |
paul@226 | 249 | target_is_new = 0; |
paul@226 | 250 | } |
paul@226 | 251 | |
paul@226 | 252 | /* Only permit a target file when one source file is given. */ |
paul@226 | 253 | |
paul@226 | 254 | if (target_is_file) |
paul@226 | 255 | { |
paul@226 | 256 | if (argc > 2) |
paul@226 | 257 | { |
paul@246 | 258 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@226 | 259 | return 1; |
paul@226 | 260 | } |
paul@226 | 261 | } |
paul@258 | 262 | else if (!image_isdir_by_inode(fs, ino_target)) |
paul@226 | 263 | { |
paul@246 | 264 | fprintf(stderr, "Target is not a directory: %s\n", target); |
paul@226 | 265 | return 1; |
paul@226 | 266 | } |
paul@226 | 267 | |
paul@226 | 268 | /* Copy each source object to the target directory. */ |
paul@226 | 269 | |
paul@226 | 270 | for (i = 0; i < argc - 1; i++) |
paul@226 | 271 | { |
paul@226 | 272 | if (target_is_file) |
paul@226 | 273 | basename = target_remaining; |
paul@226 | 274 | else |
paul@226 | 275 | { |
paul@226 | 276 | basename = path_basename(argv[i]); |
paul@226 | 277 | target_is_new = image_find_file(fs, target, basename, &ino_file); |
paul@226 | 278 | } |
paul@226 | 279 | |
paul@226 | 280 | /* Directories are created with the same metadata. */ |
paul@226 | 281 | |
paul@226 | 282 | if (isdir(argv[i])) |
paul@226 | 283 | { |
paul@226 | 284 | if (!target_is_new) |
paul@226 | 285 | { |
paul@246 | 286 | fprintf(stderr, "Target cannot be created since it already exists: %s\n", target); |
paul@226 | 287 | return 1; |
paul@226 | 288 | } |
paul@226 | 289 | |
paul@226 | 290 | /* Obtain the metadata. */ |
paul@226 | 291 | |
paul@226 | 292 | if (lstat(argv[i], &st)) |
paul@246 | 293 | { |
paul@246 | 294 | fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]); |
paul@226 | 295 | return 1; |
paul@246 | 296 | } |
paul@226 | 297 | |
paul@241 | 298 | retval = image_make_dir(fs, ino_target, basename, |
paul@241 | 299 | st.st_mode & ~md.mask, |
paul@241 | 300 | md.have_uid ? md.uid : st.st_uid, |
paul@241 | 301 | md.have_gid ? md.gid : st.st_gid, |
paul@241 | 302 | &ino_file); |
paul@226 | 303 | |
paul@226 | 304 | if (retval) |
paul@226 | 305 | { |
paul@246 | 306 | fprintf(stderr, "Failed to create directory: %s\n", argv[i]); |
paul@226 | 307 | return 1; |
paul@226 | 308 | } |
paul@226 | 309 | } |
paul@226 | 310 | |
paul@226 | 311 | /* Files are copied. */ |
paul@226 | 312 | |
paul@226 | 313 | else if (isfile(argv[i])) |
paul@226 | 314 | { |
paul@226 | 315 | flags = EXT2_FILE_WRITE; |
paul@226 | 316 | |
paul@226 | 317 | /* Obtain the inode for the target file. */ |
paul@226 | 318 | |
paul@226 | 319 | if (target_is_new) |
paul@226 | 320 | { |
paul@226 | 321 | /* Populate the inode details. */ |
paul@226 | 322 | |
paul@226 | 323 | if (lstat(argv[i], &st)) |
paul@246 | 324 | { |
paul@246 | 325 | fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]); |
paul@226 | 326 | return 1; |
paul@246 | 327 | } |
paul@226 | 328 | |
paul@241 | 329 | retval = image_create_file(fs, ino_target, basename, |
paul@241 | 330 | st.st_mode & ~md.mask, |
paul@241 | 331 | md.have_uid ? md.uid : st.st_uid, |
paul@241 | 332 | md.have_gid ? md.gid : st.st_gid, |
paul@241 | 333 | &ino_file); |
paul@226 | 334 | if (retval) |
paul@226 | 335 | { |
paul@246 | 336 | fprintf(stderr, "Failed to create file: %s\n", argv[i]); |
paul@226 | 337 | return 1; |
paul@226 | 338 | } |
paul@226 | 339 | |
paul@226 | 340 | flags |= EXT2_FILE_CREATE; |
paul@226 | 341 | } |
paul@226 | 342 | |
paul@226 | 343 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@226 | 344 | |
paul@226 | 345 | if (copy_file_in(argv[i], fs, ino_file, flags)) |
paul@226 | 346 | { |
paul@246 | 347 | fprintf(stderr, "Failed to write to file: %s\n", argv[i]); |
paul@226 | 348 | return 1; |
paul@226 | 349 | } |
paul@226 | 350 | } |
paul@226 | 351 | } |
paul@226 | 352 | |
paul@226 | 353 | return 0; |
paul@226 | 354 | } |
paul@226 | 355 | |
paul@226 | 356 | /* Copy source files out of the filesystem image into the external environment. */ |
paul@226 | 357 | |
paul@226 | 358 | int copy_out(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 359 | { |
paul@226 | 360 | /* Target filename details. */ |
paul@226 | 361 | |
paul@226 | 362 | char *target = argv[argc - 1]; |
paul@226 | 363 | int target_is_file; |
paul@226 | 364 | |
paul@226 | 365 | /* Target file and directory details. */ |
paul@226 | 366 | |
paul@226 | 367 | ext2_file_t file; |
paul@226 | 368 | ext2_ino_t ino_file; |
paul@226 | 369 | |
paul@226 | 370 | /* Source file details. */ |
paul@226 | 371 | |
paul@226 | 372 | const char *path; |
paul@226 | 373 | int i; |
paul@226 | 374 | |
paul@226 | 375 | /* Locate the target and test whether it is a directory. */ |
paul@226 | 376 | |
paul@226 | 377 | if (!isdir(target)) |
paul@226 | 378 | { |
paul@226 | 379 | /* Only a new or existing file in an existing directory is permitted. */ |
paul@226 | 380 | |
paul@226 | 381 | if (isfile(target) || isdir_dirname(target)) |
paul@226 | 382 | target_is_file = 1; |
paul@226 | 383 | else |
paul@226 | 384 | { |
paul@246 | 385 | fprintf(stderr, "Target is not a directory: %s\n", target); |
paul@226 | 386 | return 1; |
paul@226 | 387 | } |
paul@226 | 388 | |
paul@226 | 389 | /* Only permit a target file when one source file is given. */ |
paul@226 | 390 | |
paul@226 | 391 | if (argc > 2) |
paul@226 | 392 | { |
paul@246 | 393 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@226 | 394 | return 1; |
paul@226 | 395 | } |
paul@226 | 396 | } |
paul@226 | 397 | else |
paul@226 | 398 | target_is_file = 0; |
paul@226 | 399 | |
paul@226 | 400 | /* For each source filename, test whether it references a file. */ |
paul@226 | 401 | |
paul@226 | 402 | for (i = 0; i < argc - 1; i++) |
paul@226 | 403 | { |
paul@258 | 404 | if (!image_isfile_by_path(fs, argv[i])) |
paul@226 | 405 | { |
paul@246 | 406 | fprintf(stderr, "Source is not a file: %s\n", argv[i]); |
paul@226 | 407 | return 1; |
paul@226 | 408 | } |
paul@226 | 409 | } |
paul@226 | 410 | |
paul@226 | 411 | /* Copy each source file to the target directory. */ |
paul@226 | 412 | |
paul@226 | 413 | for (i = 0; i < argc - 1; i++) |
paul@226 | 414 | { |
paul@226 | 415 | path = argv[i]; |
paul@226 | 416 | |
paul@257 | 417 | if (image_find_by_path(fs, path, &ino_file)) |
paul@246 | 418 | { |
paul@246 | 419 | fprintf(stderr, "Failed to find file: %s\n", argv[i]); |
paul@226 | 420 | return 1; |
paul@246 | 421 | } |
paul@226 | 422 | |
paul@226 | 423 | if (copy_file_out(target, argv[i], target_is_file, fs, ino_file)) |
paul@226 | 424 | { |
paul@246 | 425 | fprintf(stderr, "Failed to read from file: %s\n", argv[i]); |
paul@226 | 426 | return 1; |
paul@226 | 427 | } |
paul@226 | 428 | |
paul@226 | 429 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@226 | 430 | } |
paul@226 | 431 | |
paul@226 | 432 | return 0; |
paul@226 | 433 | } |
paul@226 | 434 | |
paul@244 | 435 | /* List objects in the filesystem image. */ |
paul@226 | 436 | |
paul@244 | 437 | int list(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 438 | { |
paul@226 | 439 | int i; |
paul@244 | 440 | char *path; |
paul@226 | 441 | |
paul@226 | 442 | for (i = 0; i < argc; i++) |
paul@226 | 443 | { |
paul@226 | 444 | path = argv[i]; |
paul@226 | 445 | |
paul@244 | 446 | /* Emit each object. */ |
paul@226 | 447 | |
paul@226 | 448 | puts(path); |
paul@226 | 449 | |
paul@244 | 450 | /* List individual files or directories. */ |
paul@244 | 451 | |
paul@250 | 452 | if (utils_list_dir(fs, path)) |
paul@246 | 453 | { |
paul@262 | 454 | fprintf(stderr, "Failed to list object: %s\n", path); |
paul@226 | 455 | return 1; |
paul@246 | 456 | } |
paul@226 | 457 | } |
paul@226 | 458 | |
paul@226 | 459 | return 0; |
paul@226 | 460 | } |
paul@226 | 461 | |
paul@226 | 462 | /* Make directories in the filesystem image. */ |
paul@226 | 463 | |
paul@226 | 464 | int make_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 465 | { |
paul@226 | 466 | int i; |
paul@226 | 467 | const char *path; |
paul@226 | 468 | ext2_ino_t ino; |
paul@226 | 469 | |
paul@226 | 470 | /* Make each directory component in the given pathname. */ |
paul@226 | 471 | |
paul@226 | 472 | for (i = 0; i < argc; i++) |
paul@226 | 473 | { |
paul@226 | 474 | path = argv[i]; |
paul@226 | 475 | |
paul@226 | 476 | /* Search for the remaining components. */ |
paul@226 | 477 | |
paul@257 | 478 | if ((!*path) || !image_resolve_by_path(fs, &path, &ino)) |
paul@246 | 479 | { |
paul@246 | 480 | fprintf(stderr, "Path exists: %s\n", argv[i]); |
paul@246 | 481 | return 1; |
paul@246 | 482 | } |
paul@226 | 483 | |
paul@226 | 484 | /* From the first unrecognised component, make the remaining |
paul@226 | 485 | directories. */ |
paul@226 | 486 | |
paul@244 | 487 | if (image_make_dirs(fs, &path, ino, |
paul@244 | 488 | 0777 & ~md.mask, |
paul@244 | 489 | md.have_uid ? md.uid : 0, |
paul@244 | 490 | md.have_gid ? md.gid : 0)) |
paul@246 | 491 | { |
paul@246 | 492 | fprintf(stderr, "Failed to make directory: %s\n", argv[i]); |
paul@226 | 493 | return 1; |
paul@246 | 494 | } |
paul@226 | 495 | } |
paul@226 | 496 | |
paul@226 | 497 | return 0; |
paul@226 | 498 | } |
paul@226 | 499 | |
paul@255 | 500 | /* Remove objects from the filesystem image. */ |
paul@244 | 501 | |
paul@255 | 502 | int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only) |
paul@244 | 503 | { |
paul@244 | 504 | int i; |
paul@244 | 505 | const char *path; |
paul@244 | 506 | ext2_ino_t ino; |
paul@244 | 507 | |
paul@244 | 508 | /* Remove each directory with the given pathname. */ |
paul@244 | 509 | |
paul@244 | 510 | for (i = 0; i < argc; i++) |
paul@244 | 511 | { |
paul@244 | 512 | path = argv[i]; |
paul@246 | 513 | |
paul@246 | 514 | /* Detect missing objects. */ |
paul@244 | 515 | |
paul@258 | 516 | if ((!*path) || image_find_by_path(fs, path, &ino)) |
paul@246 | 517 | { |
paul@246 | 518 | fprintf(stderr, "Not found: %s\n", path); |
paul@246 | 519 | return 1; |
paul@246 | 520 | } |
paul@244 | 521 | |
paul@255 | 522 | /* Insist on a directory if specified. */ |
paul@255 | 523 | |
paul@255 | 524 | if (dir_only) |
paul@255 | 525 | { |
paul@258 | 526 | if (!image_isdir_by_inode(fs, ino)) |
paul@255 | 527 | { |
paul@255 | 528 | fprintf(stderr, "Not a directory: %s\n", path); |
paul@255 | 529 | return 1; |
paul@255 | 530 | } |
paul@255 | 531 | |
paul@255 | 532 | /* Test for an empty directory. */ |
paul@244 | 533 | |
paul@258 | 534 | if (image_dir_empty_by_inode(fs, ino)) |
paul@255 | 535 | { |
paul@255 | 536 | fprintf(stderr, "Directory not empty: %s\n", path); |
paul@255 | 537 | return 1; |
paul@255 | 538 | } |
paul@255 | 539 | } |
paul@255 | 540 | |
paul@255 | 541 | /* Otherwise, insist on a non-directory. */ |
paul@255 | 542 | |
paul@258 | 543 | else if (image_isdir_by_inode(fs, ino)) |
paul@246 | 544 | { |
paul@255 | 545 | fprintf(stderr, "Cannot remove a directory: %s\n", path); |
paul@244 | 546 | return 1; |
paul@246 | 547 | } |
paul@244 | 548 | |
paul@255 | 549 | /* Unlink the object. */ |
paul@244 | 550 | |
paul@255 | 551 | if (image_unlink_by_path(fs, path)) |
paul@246 | 552 | { |
paul@255 | 553 | fprintf(stderr, "Could not unlink object: %s\n", path); |
paul@244 | 554 | return 1; |
paul@246 | 555 | } |
paul@244 | 556 | |
paul@255 | 557 | /* Remove the object. */ |
paul@249 | 558 | |
paul@249 | 559 | if (image_remove_by_inode(fs, ino)) |
paul@249 | 560 | { |
paul@255 | 561 | fprintf(stderr, "Could not remove object: %s\n", path); |
paul@249 | 562 | return 1; |
paul@249 | 563 | } |
paul@244 | 564 | } |
paul@244 | 565 | |
paul@244 | 566 | return 0; |
paul@244 | 567 | } |
paul@244 | 568 | |
paul@255 | 569 | /* Remove directories from the filesystem image. */ |
paul@255 | 570 | |
paul@255 | 571 | int remove_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@255 | 572 | { |
paul@255 | 573 | return _remove(fs, argc, argv, 1); |
paul@255 | 574 | } |
paul@255 | 575 | |
paul@255 | 576 | /* Remove non-directories from the filesystem image. */ |
paul@255 | 577 | |
paul@255 | 578 | int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@255 | 579 | { |
paul@255 | 580 | return _remove(fs, argc, argv, 0); |
paul@255 | 581 | } |
paul@255 | 582 | |
paul@262 | 583 | /* Read a line from a file into the given buffer. */ |
paul@262 | 584 | |
paul@262 | 585 | struct read_line_state |
paul@262 | 586 | { |
paul@262 | 587 | char *buffer, *start, *end, *eolp; |
paul@262 | 588 | size_t remaining; |
paul@262 | 589 | }; |
paul@262 | 590 | |
paul@262 | 591 | static char *read_line(FILE *fp, struct read_line_state *state) |
paul@262 | 592 | { |
paul@262 | 593 | size_t nread; |
paul@262 | 594 | |
paul@262 | 595 | do |
paul@262 | 596 | { |
paul@262 | 597 | do |
paul@262 | 598 | { |
paul@262 | 599 | /* Search for a newline character in any available text. */ |
paul@262 | 600 | |
paul@262 | 601 | if (state->end > state->start) |
paul@262 | 602 | { |
paul@262 | 603 | state->eolp = strchr(state->start, '\n'); |
paul@262 | 604 | |
paul@262 | 605 | if (state->eolp != NULL) |
paul@262 | 606 | { |
paul@262 | 607 | *(state->eolp) = '\0'; |
paul@262 | 608 | return state->eolp; |
paul@262 | 609 | } |
paul@262 | 610 | } |
paul@262 | 611 | |
paul@262 | 612 | /* Obtain more text if necessary. */ |
paul@262 | 613 | |
paul@262 | 614 | nread = fread(state->end, sizeof(char), state->remaining, fp); |
paul@262 | 615 | |
paul@262 | 616 | /* Handle end of file condition. */ |
paul@262 | 617 | |
paul@262 | 618 | if (!nread) |
paul@262 | 619 | { |
paul@262 | 620 | if (state->end > state->start) |
paul@262 | 621 | return state->end; |
paul@262 | 622 | else |
paul@262 | 623 | return NULL; |
paul@262 | 624 | } |
paul@262 | 625 | |
paul@262 | 626 | /* Zero-terminate the string for searching. */ |
paul@262 | 627 | |
paul@262 | 628 | *(state->end + nread) = '\0'; |
paul@262 | 629 | |
paul@262 | 630 | /* Advance the end of string and subtract remaining space. */ |
paul@262 | 631 | |
paul@262 | 632 | state->end += nread; |
paul@262 | 633 | state->remaining -= nread; |
paul@262 | 634 | } |
paul@262 | 635 | while (state->remaining); |
paul@262 | 636 | |
paul@262 | 637 | /* Copy the remaining text to the start of the buffer. */ |
paul@262 | 638 | |
paul@262 | 639 | if (state->start > state->buffer) |
paul@262 | 640 | { |
paul@262 | 641 | strcpy(state->buffer, state->start); |
paul@262 | 642 | |
paul@262 | 643 | state->end -= (state->start - state->buffer); |
paul@262 | 644 | state->start = state->buffer; |
paul@262 | 645 | state->remaining = BUFSIZE - 1 - (state->end - state->buffer); |
paul@262 | 646 | } |
paul@262 | 647 | } |
paul@262 | 648 | while (state->remaining); |
paul@262 | 649 | |
paul@262 | 650 | return NULL; |
paul@262 | 651 | } |
paul@262 | 652 | |
paul@262 | 653 | /* Parse the text in the given region, returning details of arguments. */ |
paul@262 | 654 | |
paul@262 | 655 | static void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args) |
paul@262 | 656 | { |
paul@262 | 657 | *num_args = 0; |
paul@262 | 658 | |
paul@262 | 659 | while ((start != NULL) && (start < end) && (*num_args < max_args)) |
paul@262 | 660 | { |
paul@262 | 661 | args[*num_args] = start; |
paul@262 | 662 | (*num_args)++; |
paul@262 | 663 | |
paul@262 | 664 | /* NOTE: Only handling spaces as delimiters. */ |
paul@262 | 665 | |
paul@262 | 666 | start = strchr(start, ' '); |
paul@262 | 667 | |
paul@262 | 668 | if (start != NULL) |
paul@262 | 669 | { |
paul@262 | 670 | *start = '\0'; |
paul@262 | 671 | |
paul@262 | 672 | if (start < end) |
paul@262 | 673 | start++; |
paul@262 | 674 | } |
paul@262 | 675 | } |
paul@262 | 676 | } |
paul@262 | 677 | |
paul@262 | 678 | /* Read operations from a script file. */ |
paul@262 | 679 | |
paul@262 | 680 | enum op_results |
paul@262 | 681 | { |
paul@262 | 682 | OP_SUCCESS = 0, |
paul@262 | 683 | OP_FAILED = 1, |
paul@262 | 684 | OP_UNKNOWN = 2, |
paul@262 | 685 | }; |
paul@262 | 686 | |
paul@262 | 687 | int handle_op_result(const char *operation, enum op_results op_result) |
paul@262 | 688 | { |
paul@262 | 689 | if (op_result == OP_UNKNOWN) |
paul@262 | 690 | { |
paul@262 | 691 | fprintf(stderr, "Operation not recognised: %s\n", operation); |
paul@262 | 692 | return 1; |
paul@262 | 693 | } |
paul@262 | 694 | else if (op_result == OP_FAILED) |
paul@262 | 695 | { |
paul@262 | 696 | fprintf(stderr, "Operation failed: %s\n", operation); |
paul@262 | 697 | return 1; |
paul@262 | 698 | } |
paul@262 | 699 | else |
paul@262 | 700 | return 0; |
paul@262 | 701 | } |
paul@262 | 702 | |
paul@262 | 703 | enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]); |
paul@262 | 704 | |
paul@262 | 705 | int run_script(ext2_filsys fs, int argc, char *argv[]) |
paul@262 | 706 | { |
paul@262 | 707 | FILE *fp; |
paul@262 | 708 | char buffer[BUFSIZE]; |
paul@262 | 709 | struct read_line_state state; |
paul@262 | 710 | enum op_results op_result; |
paul@262 | 711 | int num_args; |
paul@262 | 712 | char *args[MAX_ARGS]; |
paul@262 | 713 | int i; |
paul@262 | 714 | |
paul@262 | 715 | for (i = 0; i < argc; i++) |
paul@262 | 716 | { |
paul@262 | 717 | fp = fopen(argv[i], "r"); |
paul@262 | 718 | |
paul@262 | 719 | state.buffer = buffer; |
paul@262 | 720 | state.start = buffer; |
paul@262 | 721 | state.end = buffer; |
paul@262 | 722 | state.remaining = BUFSIZE - 1; |
paul@262 | 723 | |
paul@262 | 724 | while (read_line(fp, &state) != NULL) |
paul@262 | 725 | { |
paul@262 | 726 | parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS); |
paul@262 | 727 | |
paul@262 | 728 | if (num_args > 1) |
paul@262 | 729 | { |
paul@262 | 730 | op_result = run_operation(fs, args[0], num_args - 1, &args[1]); |
paul@262 | 731 | |
paul@262 | 732 | if (handle_op_result(args[0], op_result)) |
paul@262 | 733 | return 1; |
paul@262 | 734 | } |
paul@262 | 735 | |
paul@262 | 736 | state.start = state.eolp + 1; |
paul@262 | 737 | } |
paul@262 | 738 | |
paul@262 | 739 | fclose(fp); |
paul@262 | 740 | } |
paul@262 | 741 | |
paul@262 | 742 | return 0; |
paul@262 | 743 | } |
paul@262 | 744 | |
paul@262 | 745 | |
paul@244 | 746 | |
paul@244 | 747 | |
paul@244 | 748 | /* Help message. */ |
paul@241 | 749 | |
paul@241 | 750 | char help_text[] = "\ |
paul@241 | 751 | Usage: %s [ <options> ] <image file> <operation> <filename>...\n\ |
paul@241 | 752 | \n\ |
paul@241 | 753 | File ownership options:\n\ |
paul@241 | 754 | \n\ |
paul@241 | 755 | -g GID Set group identifier for new files\n\ |
paul@241 | 756 | -u UID Set user identifier for new files\n\ |
paul@241 | 757 | \n\ |
paul@241 | 758 | File permission options:\n\ |
paul@241 | 759 | \n\ |
paul@241 | 760 | -m MASK Set mode/permissions mask for new directories\n\ |
paul@244 | 761 | \n\ |
paul@262 | 762 | Transfer operations:\n\ |
paul@244 | 763 | \n\ |
paul@244 | 764 | copy-in Copy files into a directory within the image\n\ |
paul@244 | 765 | copy-out Copy files from the image into a directory\n\ |
paul@262 | 766 | \n\ |
paul@262 | 767 | Image operations:\n\ |
paul@262 | 768 | \n\ |
paul@244 | 769 | ls List files and directories within the image\n\ |
paul@244 | 770 | mkdir Make directories within the image\n\ |
paul@255 | 771 | rm Remove non-directory objects from the image\n\ |
paul@244 | 772 | rmdir Remove directories from the image\n\ |
paul@262 | 773 | \n\ |
paul@262 | 774 | Script operations:\n\ |
paul@262 | 775 | \n\ |
paul@262 | 776 | script Read operations from a script file\n\ |
paul@241 | 777 | "; |
paul@241 | 778 | |
paul@244 | 779 | /* Operations exposed by the program. */ |
paul@244 | 780 | |
paul@244 | 781 | struct operation |
paul@244 | 782 | { |
paul@244 | 783 | const char *name; |
paul@244 | 784 | int (*fn)(ext2_filsys, int, char *[]); |
paul@244 | 785 | }; |
paul@244 | 786 | |
paul@244 | 787 | static struct operation operations[] = { |
paul@244 | 788 | {"copy-in", copy_in}, |
paul@244 | 789 | {"copy-out", copy_out}, |
paul@244 | 790 | {"ls", list}, |
paul@244 | 791 | {"mkdir", make_dirs}, |
paul@255 | 792 | {"rm", remove_non_dirs}, |
paul@244 | 793 | {"rmdir", remove_dirs}, |
paul@262 | 794 | {"script", run_script}, |
paul@244 | 795 | {NULL, NULL}, |
paul@244 | 796 | }; |
paul@244 | 797 | |
paul@262 | 798 | /* Invocation of operations. */ |
paul@262 | 799 | |
paul@262 | 800 | enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]) |
paul@262 | 801 | { |
paul@262 | 802 | struct operation *op; |
paul@262 | 803 | int exitcode; |
paul@262 | 804 | |
paul@262 | 805 | for (op = &operations[0]; op->name != NULL; op++) |
paul@262 | 806 | { |
paul@262 | 807 | if (!strcmp(operation, op->name)) |
paul@262 | 808 | { |
paul@262 | 809 | exitcode = op->fn(fs, argc, argv); |
paul@262 | 810 | if (exitcode) |
paul@262 | 811 | return OP_FAILED; |
paul@262 | 812 | break; |
paul@262 | 813 | } |
paul@262 | 814 | } |
paul@262 | 815 | |
paul@262 | 816 | if (op->name == NULL) |
paul@262 | 817 | return OP_UNKNOWN; |
paul@262 | 818 | |
paul@262 | 819 | return OP_SUCCESS; |
paul@262 | 820 | } |
paul@262 | 821 | |
paul@244 | 822 | /* Main program. */ |
paul@244 | 823 | |
paul@226 | 824 | int main(int argc, char *argv[]) |
paul@226 | 825 | { |
paul@226 | 826 | int flags = EXT2_FLAG_RW; // | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
paul@226 | 827 | ext2_filsys fs = NULL; |
paul@226 | 828 | errcode_t retval; |
paul@226 | 829 | int exitcode = 0; |
paul@226 | 830 | |
paul@241 | 831 | /* Program argument details. */ |
paul@241 | 832 | |
paul@241 | 833 | char **args; |
paul@262 | 834 | char *fsname, *filename; |
paul@241 | 835 | int num_args; |
paul@262 | 836 | enum op_results op_result; |
paul@241 | 837 | |
paul@241 | 838 | /* Parse program options and initialise the argument details. */ |
paul@241 | 839 | |
paul@241 | 840 | if (parse_options(argc, argv)) |
paul@241 | 841 | return 1; |
paul@241 | 842 | |
paul@241 | 843 | args = &argv[optind]; |
paul@241 | 844 | num_args = argc - optind; |
paul@241 | 845 | |
paul@241 | 846 | if (num_args < 3) |
paul@226 | 847 | { |
paul@246 | 848 | fprintf(stderr, help_text, argv[0]); |
paul@226 | 849 | return 1; |
paul@226 | 850 | } |
paul@226 | 851 | |
paul@241 | 852 | /* Open the filesystem image using the POSIX file access mechanism. */ |
paul@226 | 853 | |
paul@241 | 854 | fsname = args[0]; |
paul@226 | 855 | |
paul@226 | 856 | retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); |
paul@226 | 857 | if (retval) |
paul@226 | 858 | { |
paul@246 | 859 | fprintf(stderr, "Could not open filesystem: %s\n", fsname); |
paul@226 | 860 | return 1; |
paul@226 | 861 | } |
paul@226 | 862 | |
paul@226 | 863 | //fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
paul@226 | 864 | |
paul@226 | 865 | retval = ext2fs_read_bitmaps(fs); |
paul@226 | 866 | if (retval) |
paul@226 | 867 | { |
paul@246 | 868 | fprintf(stderr, "Could not read bitmaps: %s\n", fsname); |
paul@226 | 869 | return 1; |
paul@226 | 870 | } |
paul@226 | 871 | |
paul@226 | 872 | /* Perform the requested operation. */ |
paul@226 | 873 | |
paul@262 | 874 | op_result = run_operation(fs, args[1], num_args - 2, &args[2]); |
paul@262 | 875 | exitcode = handle_op_result(args[1], op_result); |
paul@226 | 876 | |
paul@226 | 877 | /* Close the filesystem image. */ |
paul@226 | 878 | |
paul@226 | 879 | retval = ext2fs_flush(fs); |
paul@226 | 880 | if (retval) |
paul@226 | 881 | { |
paul@246 | 882 | fprintf(stderr, "Error flushing filesystem: %s\n", fsname); |
paul@226 | 883 | exitcode = 1; |
paul@226 | 884 | } |
paul@226 | 885 | |
paul@226 | 886 | retval = ext2fs_close(fs); |
paul@226 | 887 | if (retval) |
paul@226 | 888 | { |
paul@246 | 889 | fprintf(stderr, "Error closing filesystem: %s\n", fsname); |
paul@226 | 890 | exitcode = 1; |
paul@226 | 891 | } |
paul@226 | 892 | |
paul@226 | 893 | ext2fs_free(fs); |
paul@226 | 894 | return exitcode; |
paul@226 | 895 | } |
paul@241 | 896 | |
paul@241 | 897 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@241 | 898 | */ |