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@226 | 44 | |
paul@226 | 45 | |
paul@241 | 46 | /* Alternative metadata set by options. */ |
paul@241 | 47 | |
paul@241 | 48 | struct metadata |
paul@241 | 49 | { |
paul@241 | 50 | uid_t uid; |
paul@241 | 51 | gid_t gid; |
paul@241 | 52 | mode_t mask; |
paul@241 | 53 | int have_uid, have_gid; |
paul@241 | 54 | }; |
paul@241 | 55 | |
paul@241 | 56 | struct metadata md; |
paul@241 | 57 | |
paul@246 | 58 | /* Parse program options. */ |
paul@246 | 59 | |
paul@241 | 60 | static int parse_options(int argc, char *argv[]) |
paul@241 | 61 | { |
paul@241 | 62 | int opt; |
paul@241 | 63 | |
paul@241 | 64 | md.have_uid = 0; |
paul@241 | 65 | md.have_gid = 0; |
paul@241 | 66 | md.mask = 0000; |
paul@241 | 67 | |
paul@241 | 68 | while ((opt = getopt(argc, argv, "g:m:u:")) != -1) |
paul@241 | 69 | { |
paul@241 | 70 | switch (opt) |
paul@241 | 71 | { |
paul@241 | 72 | case 'g': |
paul@241 | 73 | md.gid = atoi(optarg); |
paul@241 | 74 | md.have_gid = 1; |
paul@241 | 75 | break; |
paul@241 | 76 | |
paul@241 | 77 | case 'm': |
paul@241 | 78 | md.mask = strtol(optarg, NULL, 0); |
paul@241 | 79 | break; |
paul@241 | 80 | |
paul@241 | 81 | case 'u': |
paul@241 | 82 | md.uid = atoi(optarg); |
paul@241 | 83 | md.have_uid = 1; |
paul@241 | 84 | break; |
paul@241 | 85 | |
paul@241 | 86 | default: |
paul@246 | 87 | fprintf(stderr, "Option not recognised: %s\n", argv[optind]); |
paul@241 | 88 | return -1; |
paul@241 | 89 | } |
paul@241 | 90 | } |
paul@241 | 91 | |
paul@241 | 92 | return 0; |
paul@241 | 93 | } |
paul@241 | 94 | |
paul@241 | 95 | |
paul@241 | 96 | |
paul@226 | 97 | /* Copy a file into the filesystem image. */ |
paul@226 | 98 | |
paul@226 | 99 | int copy_file_in(const char *filename, ext2_filsys fs, ext2_ino_t ino_file, int flags) |
paul@226 | 100 | { |
paul@226 | 101 | int retval = 0; |
paul@226 | 102 | ext2_file_t file; |
paul@226 | 103 | |
paul@226 | 104 | /* Copying details. */ |
paul@226 | 105 | |
paul@226 | 106 | FILE *fp; |
paul@226 | 107 | char buf[BUFSIZE]; |
paul@226 | 108 | size_t got; |
paul@226 | 109 | unsigned int written; |
paul@226 | 110 | |
paul@226 | 111 | /* Open a file in the target directory. */ |
paul@226 | 112 | |
paul@226 | 113 | if (ext2fs_file_open(fs, ino_file, flags, &file)) |
paul@226 | 114 | return 1; |
paul@226 | 115 | |
paul@226 | 116 | /* Open the file in the source directory. */ |
paul@226 | 117 | |
paul@226 | 118 | fp = fopen(filename, "r"); |
paul@226 | 119 | |
paul@226 | 120 | /* Copy the file content. */ |
paul@226 | 121 | |
paul@226 | 122 | if (fp != NULL) |
paul@226 | 123 | { |
paul@226 | 124 | while (got = fread(buf, sizeof(char), BUFSIZE, fp)) |
paul@226 | 125 | { |
paul@226 | 126 | while (got) |
paul@226 | 127 | { |
paul@226 | 128 | if (ext2fs_file_write(file, buf, got, &written)) |
paul@226 | 129 | { |
paul@226 | 130 | retval = 1; |
paul@226 | 131 | goto close_files; |
paul@226 | 132 | } |
paul@226 | 133 | got -= written; |
paul@226 | 134 | } |
paul@226 | 135 | } |
paul@226 | 136 | } |
paul@226 | 137 | |
paul@226 | 138 | close_files: |
paul@226 | 139 | fclose(fp); |
paul@226 | 140 | ext2fs_file_flush(file); |
paul@226 | 141 | ext2fs_file_close(file); |
paul@226 | 142 | |
paul@226 | 143 | return retval; |
paul@226 | 144 | } |
paul@226 | 145 | |
paul@226 | 146 | /* Copy a file out of the filesystem image. */ |
paul@226 | 147 | |
paul@226 | 148 | int copy_file_out(const char *target, const char *filename, int target_is_file, |
paul@226 | 149 | ext2_filsys fs, ext2_ino_t ino_file) |
paul@226 | 150 | { |
paul@226 | 151 | int retval = 0; |
paul@226 | 152 | ext2_file_t file; |
paul@226 | 153 | |
paul@226 | 154 | /* Copying details. */ |
paul@226 | 155 | |
paul@226 | 156 | FILE *fp; |
paul@226 | 157 | char buf[BUFSIZE]; |
paul@226 | 158 | unsigned int got; |
paul@226 | 159 | size_t written; |
paul@226 | 160 | |
paul@226 | 161 | /* Open the file in the source directory. */ |
paul@226 | 162 | |
paul@226 | 163 | if (ext2fs_file_open(fs, ino_file, 0, &file)) |
paul@226 | 164 | return 1; |
paul@226 | 165 | |
paul@226 | 166 | /* Open a file in the target directory. */ |
paul@226 | 167 | |
paul@226 | 168 | if (target_is_file) |
paul@226 | 169 | fp = fopen(target, "w"); |
paul@226 | 170 | else |
paul@226 | 171 | fp = open_file_in_dir(target, path_basename(filename), "w"); |
paul@226 | 172 | |
paul@226 | 173 | /* Copy the file content. */ |
paul@226 | 174 | |
paul@226 | 175 | if (fp != NULL) |
paul@226 | 176 | { |
paul@226 | 177 | do |
paul@226 | 178 | { |
paul@226 | 179 | if (ext2fs_file_read(file, buf, BUFSIZE, &got)) |
paul@226 | 180 | { |
paul@226 | 181 | retval = 1; |
paul@226 | 182 | goto close_files; |
paul@226 | 183 | } |
paul@226 | 184 | |
paul@226 | 185 | while (got) |
paul@226 | 186 | { |
paul@226 | 187 | written = fwrite(buf, sizeof(char), got, fp); |
paul@226 | 188 | got -= written; |
paul@226 | 189 | } |
paul@226 | 190 | |
paul@226 | 191 | } while (got); |
paul@226 | 192 | } |
paul@226 | 193 | |
paul@226 | 194 | close_files: |
paul@226 | 195 | fclose(fp); |
paul@226 | 196 | ext2fs_file_close(file); |
paul@226 | 197 | |
paul@226 | 198 | return retval; |
paul@226 | 199 | } |
paul@226 | 200 | |
paul@226 | 201 | |
paul@226 | 202 | |
paul@226 | 203 | /* Copy source files from the external environment into the filesystem image. */ |
paul@226 | 204 | |
paul@226 | 205 | int copy_in(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 206 | { |
paul@226 | 207 | errcode_t retval; |
paul@226 | 208 | |
paul@226 | 209 | /* Target filename details. */ |
paul@226 | 210 | |
paul@226 | 211 | const char *target = argv[argc - 1]; |
paul@226 | 212 | const char *target_remaining = argv[argc - 1]; |
paul@226 | 213 | const char *basename; |
paul@226 | 214 | int target_is_file; |
paul@226 | 215 | |
paul@226 | 216 | /* Target file and directory details. */ |
paul@226 | 217 | |
paul@226 | 218 | int target_is_new; |
paul@226 | 219 | ext2_ino_t ino_file, ino_target; |
paul@226 | 220 | int flags; |
paul@226 | 221 | |
paul@226 | 222 | /* Source file details. */ |
paul@226 | 223 | |
paul@226 | 224 | struct stat st; |
paul@226 | 225 | int i; |
paul@226 | 226 | |
paul@226 | 227 | /* Locate the target and test whether it is a file or a directory. */ |
paul@226 | 228 | |
paul@257 | 229 | if (image_resolve_by_path(fs, &target_remaining, &ino_target)) |
paul@226 | 230 | { |
paul@226 | 231 | /* Only a non-existent file in an existing directory is permitted. */ |
paul@226 | 232 | |
paul@226 | 233 | if (!_image_isdir(fs, ino_target) || !path_is_leafname(target_remaining)) |
paul@226 | 234 | { |
paul@246 | 235 | fprintf(stderr, "Target not found: %s\n", target); |
paul@226 | 236 | return 1; |
paul@226 | 237 | } |
paul@226 | 238 | |
paul@226 | 239 | target_is_file = 1; |
paul@226 | 240 | target_is_new = 1; |
paul@226 | 241 | } |
paul@226 | 242 | else |
paul@226 | 243 | { |
paul@226 | 244 | target_is_file = _image_isfile(fs, ino_target); |
paul@226 | 245 | target_is_new = 0; |
paul@226 | 246 | } |
paul@226 | 247 | |
paul@226 | 248 | /* Only permit a target file when one source file is given. */ |
paul@226 | 249 | |
paul@226 | 250 | if (target_is_file) |
paul@226 | 251 | { |
paul@226 | 252 | if (argc > 2) |
paul@226 | 253 | { |
paul@246 | 254 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@226 | 255 | return 1; |
paul@226 | 256 | } |
paul@226 | 257 | } |
paul@226 | 258 | else if (!_image_isdir(fs, ino_target)) |
paul@226 | 259 | { |
paul@246 | 260 | fprintf(stderr, "Target is not a directory: %s\n", target); |
paul@226 | 261 | return 1; |
paul@226 | 262 | } |
paul@226 | 263 | |
paul@226 | 264 | /* Copy each source object to the target directory. */ |
paul@226 | 265 | |
paul@226 | 266 | for (i = 0; i < argc - 1; i++) |
paul@226 | 267 | { |
paul@226 | 268 | if (target_is_file) |
paul@226 | 269 | basename = target_remaining; |
paul@226 | 270 | else |
paul@226 | 271 | { |
paul@226 | 272 | basename = path_basename(argv[i]); |
paul@226 | 273 | target_is_new = image_find_file(fs, target, basename, &ino_file); |
paul@226 | 274 | } |
paul@226 | 275 | |
paul@226 | 276 | /* Directories are created with the same metadata. */ |
paul@226 | 277 | |
paul@226 | 278 | if (isdir(argv[i])) |
paul@226 | 279 | { |
paul@226 | 280 | if (!target_is_new) |
paul@226 | 281 | { |
paul@246 | 282 | fprintf(stderr, "Target cannot be created since it already exists: %s\n", target); |
paul@226 | 283 | return 1; |
paul@226 | 284 | } |
paul@226 | 285 | |
paul@226 | 286 | /* Obtain the metadata. */ |
paul@226 | 287 | |
paul@226 | 288 | if (lstat(argv[i], &st)) |
paul@246 | 289 | { |
paul@246 | 290 | fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]); |
paul@226 | 291 | return 1; |
paul@246 | 292 | } |
paul@226 | 293 | |
paul@241 | 294 | retval = image_make_dir(fs, ino_target, basename, |
paul@241 | 295 | st.st_mode & ~md.mask, |
paul@241 | 296 | md.have_uid ? md.uid : st.st_uid, |
paul@241 | 297 | md.have_gid ? md.gid : st.st_gid, |
paul@241 | 298 | &ino_file); |
paul@226 | 299 | |
paul@226 | 300 | if (retval) |
paul@226 | 301 | { |
paul@246 | 302 | fprintf(stderr, "Failed to create directory: %s\n", argv[i]); |
paul@226 | 303 | return 1; |
paul@226 | 304 | } |
paul@226 | 305 | } |
paul@226 | 306 | |
paul@226 | 307 | /* Files are copied. */ |
paul@226 | 308 | |
paul@226 | 309 | else if (isfile(argv[i])) |
paul@226 | 310 | { |
paul@226 | 311 | flags = EXT2_FILE_WRITE; |
paul@226 | 312 | |
paul@226 | 313 | /* Obtain the inode for the target file. */ |
paul@226 | 314 | |
paul@226 | 315 | if (target_is_new) |
paul@226 | 316 | { |
paul@226 | 317 | /* Populate the inode details. */ |
paul@226 | 318 | |
paul@226 | 319 | if (lstat(argv[i], &st)) |
paul@246 | 320 | { |
paul@246 | 321 | fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]); |
paul@226 | 322 | return 1; |
paul@246 | 323 | } |
paul@226 | 324 | |
paul@241 | 325 | retval = image_create_file(fs, ino_target, basename, |
paul@241 | 326 | st.st_mode & ~md.mask, |
paul@241 | 327 | md.have_uid ? md.uid : st.st_uid, |
paul@241 | 328 | md.have_gid ? md.gid : st.st_gid, |
paul@241 | 329 | &ino_file); |
paul@226 | 330 | if (retval) |
paul@226 | 331 | { |
paul@246 | 332 | fprintf(stderr, "Failed to create file: %s\n", argv[i]); |
paul@226 | 333 | return 1; |
paul@226 | 334 | } |
paul@226 | 335 | |
paul@226 | 336 | flags |= EXT2_FILE_CREATE; |
paul@226 | 337 | } |
paul@226 | 338 | |
paul@226 | 339 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@226 | 340 | |
paul@226 | 341 | if (copy_file_in(argv[i], fs, ino_file, flags)) |
paul@226 | 342 | { |
paul@246 | 343 | fprintf(stderr, "Failed to write to file: %s\n", argv[i]); |
paul@226 | 344 | return 1; |
paul@226 | 345 | } |
paul@226 | 346 | } |
paul@226 | 347 | } |
paul@226 | 348 | |
paul@226 | 349 | return 0; |
paul@226 | 350 | } |
paul@226 | 351 | |
paul@226 | 352 | /* Copy source files out of the filesystem image into the external environment. */ |
paul@226 | 353 | |
paul@226 | 354 | int copy_out(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 355 | { |
paul@226 | 356 | /* Target filename details. */ |
paul@226 | 357 | |
paul@226 | 358 | char *target = argv[argc - 1]; |
paul@226 | 359 | int target_is_file; |
paul@226 | 360 | |
paul@226 | 361 | /* Target file and directory details. */ |
paul@226 | 362 | |
paul@226 | 363 | ext2_file_t file; |
paul@226 | 364 | ext2_ino_t ino_file; |
paul@226 | 365 | |
paul@226 | 366 | /* Source file details. */ |
paul@226 | 367 | |
paul@226 | 368 | const char *path; |
paul@226 | 369 | int i; |
paul@226 | 370 | |
paul@226 | 371 | /* Locate the target and test whether it is a directory. */ |
paul@226 | 372 | |
paul@226 | 373 | if (!isdir(target)) |
paul@226 | 374 | { |
paul@226 | 375 | /* Only a new or existing file in an existing directory is permitted. */ |
paul@226 | 376 | |
paul@226 | 377 | if (isfile(target) || isdir_dirname(target)) |
paul@226 | 378 | target_is_file = 1; |
paul@226 | 379 | else |
paul@226 | 380 | { |
paul@246 | 381 | fprintf(stderr, "Target is not a directory: %s\n", target); |
paul@226 | 382 | return 1; |
paul@226 | 383 | } |
paul@226 | 384 | |
paul@226 | 385 | /* Only permit a target file when one source file is given. */ |
paul@226 | 386 | |
paul@226 | 387 | if (argc > 2) |
paul@226 | 388 | { |
paul@246 | 389 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@226 | 390 | return 1; |
paul@226 | 391 | } |
paul@226 | 392 | } |
paul@226 | 393 | else |
paul@226 | 394 | target_is_file = 0; |
paul@226 | 395 | |
paul@226 | 396 | /* For each source filename, test whether it references a file. */ |
paul@226 | 397 | |
paul@226 | 398 | for (i = 0; i < argc - 1; i++) |
paul@226 | 399 | { |
paul@226 | 400 | if (!image_isfile(fs, argv[i])) |
paul@226 | 401 | { |
paul@246 | 402 | fprintf(stderr, "Source is not a file: %s\n", argv[i]); |
paul@226 | 403 | return 1; |
paul@226 | 404 | } |
paul@226 | 405 | } |
paul@226 | 406 | |
paul@226 | 407 | /* Copy each source file to the target directory. */ |
paul@226 | 408 | |
paul@226 | 409 | for (i = 0; i < argc - 1; i++) |
paul@226 | 410 | { |
paul@226 | 411 | path = argv[i]; |
paul@226 | 412 | |
paul@257 | 413 | if (image_find_by_path(fs, path, &ino_file)) |
paul@246 | 414 | { |
paul@246 | 415 | fprintf(stderr, "Failed to find file: %s\n", argv[i]); |
paul@226 | 416 | return 1; |
paul@246 | 417 | } |
paul@226 | 418 | |
paul@226 | 419 | if (copy_file_out(target, argv[i], target_is_file, fs, ino_file)) |
paul@226 | 420 | { |
paul@246 | 421 | fprintf(stderr, "Failed to read from file: %s\n", argv[i]); |
paul@226 | 422 | return 1; |
paul@226 | 423 | } |
paul@226 | 424 | |
paul@226 | 425 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@226 | 426 | } |
paul@226 | 427 | |
paul@226 | 428 | return 0; |
paul@226 | 429 | } |
paul@226 | 430 | |
paul@244 | 431 | /* List objects in the filesystem image. */ |
paul@226 | 432 | |
paul@244 | 433 | int list(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 434 | { |
paul@226 | 435 | int i; |
paul@244 | 436 | char *path; |
paul@226 | 437 | |
paul@226 | 438 | for (i = 0; i < argc; i++) |
paul@226 | 439 | { |
paul@226 | 440 | path = argv[i]; |
paul@226 | 441 | |
paul@244 | 442 | /* Emit each object. */ |
paul@226 | 443 | |
paul@226 | 444 | puts(path); |
paul@226 | 445 | |
paul@244 | 446 | /* List individual files or directories. */ |
paul@244 | 447 | |
paul@250 | 448 | if (utils_list_dir(fs, path)) |
paul@246 | 449 | { |
paul@246 | 450 | fprintf(stderr, "Failed to list directory: %s\n", path); |
paul@226 | 451 | return 1; |
paul@246 | 452 | } |
paul@226 | 453 | } |
paul@226 | 454 | |
paul@226 | 455 | return 0; |
paul@226 | 456 | } |
paul@226 | 457 | |
paul@226 | 458 | /* Make directories in the filesystem image. */ |
paul@226 | 459 | |
paul@226 | 460 | int make_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 461 | { |
paul@226 | 462 | int i; |
paul@226 | 463 | const char *path; |
paul@226 | 464 | ext2_ino_t ino; |
paul@226 | 465 | |
paul@226 | 466 | /* Make each directory component in the given pathname. */ |
paul@226 | 467 | |
paul@226 | 468 | for (i = 0; i < argc; i++) |
paul@226 | 469 | { |
paul@226 | 470 | path = argv[i]; |
paul@226 | 471 | |
paul@226 | 472 | /* Search for the remaining components. */ |
paul@226 | 473 | |
paul@257 | 474 | if ((!*path) || !image_resolve_by_path(fs, &path, &ino)) |
paul@246 | 475 | { |
paul@246 | 476 | fprintf(stderr, "Path exists: %s\n", argv[i]); |
paul@246 | 477 | return 1; |
paul@246 | 478 | } |
paul@226 | 479 | |
paul@226 | 480 | /* From the first unrecognised component, make the remaining |
paul@226 | 481 | directories. */ |
paul@226 | 482 | |
paul@244 | 483 | if (image_make_dirs(fs, &path, ino, |
paul@244 | 484 | 0777 & ~md.mask, |
paul@244 | 485 | md.have_uid ? md.uid : 0, |
paul@244 | 486 | md.have_gid ? md.gid : 0)) |
paul@246 | 487 | { |
paul@246 | 488 | fprintf(stderr, "Failed to make directory: %s\n", argv[i]); |
paul@226 | 489 | return 1; |
paul@246 | 490 | } |
paul@226 | 491 | } |
paul@226 | 492 | |
paul@226 | 493 | return 0; |
paul@226 | 494 | } |
paul@226 | 495 | |
paul@255 | 496 | /* Remove objects from the filesystem image. */ |
paul@244 | 497 | |
paul@255 | 498 | int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only) |
paul@244 | 499 | { |
paul@244 | 500 | int i; |
paul@244 | 501 | const char *path; |
paul@244 | 502 | ext2_ino_t ino; |
paul@244 | 503 | |
paul@244 | 504 | /* Remove each directory with the given pathname. */ |
paul@244 | 505 | |
paul@244 | 506 | for (i = 0; i < argc; i++) |
paul@244 | 507 | { |
paul@244 | 508 | path = argv[i]; |
paul@246 | 509 | |
paul@246 | 510 | /* Detect missing objects. */ |
paul@244 | 511 | |
paul@246 | 512 | if ((!*path) || !image_exists(fs, path)) |
paul@246 | 513 | { |
paul@246 | 514 | fprintf(stderr, "Not found: %s\n", path); |
paul@246 | 515 | return 1; |
paul@246 | 516 | } |
paul@244 | 517 | |
paul@255 | 518 | /* Insist on a directory if specified. */ |
paul@255 | 519 | |
paul@255 | 520 | if (dir_only) |
paul@255 | 521 | { |
paul@255 | 522 | if (!image_isdir(fs, path)) |
paul@255 | 523 | { |
paul@255 | 524 | fprintf(stderr, "Not a directory: %s\n", path); |
paul@255 | 525 | return 1; |
paul@255 | 526 | } |
paul@255 | 527 | |
paul@255 | 528 | /* Test for an empty directory. */ |
paul@244 | 529 | |
paul@255 | 530 | if (image_dir_empty_by_path(fs, path, &ino)) |
paul@255 | 531 | { |
paul@255 | 532 | fprintf(stderr, "Directory not empty: %s\n", path); |
paul@255 | 533 | return 1; |
paul@255 | 534 | } |
paul@255 | 535 | } |
paul@255 | 536 | |
paul@255 | 537 | /* Otherwise, insist on a non-directory. */ |
paul@255 | 538 | |
paul@255 | 539 | else if (image_isdir_by_path(fs, path, &ino)) |
paul@246 | 540 | { |
paul@255 | 541 | fprintf(stderr, "Cannot remove a directory: %s\n", path); |
paul@244 | 542 | return 1; |
paul@246 | 543 | } |
paul@244 | 544 | |
paul@255 | 545 | /* Unlink the object. */ |
paul@244 | 546 | |
paul@255 | 547 | if (image_unlink_by_path(fs, path)) |
paul@246 | 548 | { |
paul@255 | 549 | fprintf(stderr, "Could not unlink object: %s\n", path); |
paul@244 | 550 | return 1; |
paul@246 | 551 | } |
paul@244 | 552 | |
paul@255 | 553 | /* Remove the object. */ |
paul@249 | 554 | |
paul@249 | 555 | if (image_remove_by_inode(fs, ino)) |
paul@249 | 556 | { |
paul@255 | 557 | fprintf(stderr, "Could not remove object: %s\n", path); |
paul@249 | 558 | return 1; |
paul@249 | 559 | } |
paul@244 | 560 | } |
paul@244 | 561 | |
paul@244 | 562 | return 0; |
paul@244 | 563 | } |
paul@244 | 564 | |
paul@255 | 565 | /* Remove directories from the filesystem image. */ |
paul@255 | 566 | |
paul@255 | 567 | int remove_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@255 | 568 | { |
paul@255 | 569 | return _remove(fs, argc, argv, 1); |
paul@255 | 570 | } |
paul@255 | 571 | |
paul@255 | 572 | /* Remove non-directories from the filesystem image. */ |
paul@255 | 573 | |
paul@255 | 574 | int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@255 | 575 | { |
paul@255 | 576 | return _remove(fs, argc, argv, 0); |
paul@255 | 577 | } |
paul@255 | 578 | |
paul@244 | 579 | |
paul@244 | 580 | |
paul@244 | 581 | /* Help message. */ |
paul@241 | 582 | |
paul@241 | 583 | char help_text[] = "\ |
paul@241 | 584 | Usage: %s [ <options> ] <image file> <operation> <filename>...\n\ |
paul@241 | 585 | \n\ |
paul@241 | 586 | File ownership options:\n\ |
paul@241 | 587 | \n\ |
paul@241 | 588 | -g GID Set group identifier for new files\n\ |
paul@241 | 589 | -u UID Set user identifier for new files\n\ |
paul@241 | 590 | \n\ |
paul@241 | 591 | File permission options:\n\ |
paul@241 | 592 | \n\ |
paul@241 | 593 | -m MASK Set mode/permissions mask for new directories\n\ |
paul@244 | 594 | \n\ |
paul@244 | 595 | Operations:\n\ |
paul@244 | 596 | \n\ |
paul@244 | 597 | copy-in Copy files into a directory within the image\n\ |
paul@244 | 598 | copy-out Copy files from the image into a directory\n\ |
paul@244 | 599 | ls List files and directories within the image\n\ |
paul@244 | 600 | mkdir Make directories within the image\n\ |
paul@255 | 601 | rm Remove non-directory objects from the image\n\ |
paul@244 | 602 | rmdir Remove directories from the image\n\ |
paul@241 | 603 | "; |
paul@241 | 604 | |
paul@244 | 605 | /* Operations exposed by the program. */ |
paul@244 | 606 | |
paul@244 | 607 | struct operation |
paul@244 | 608 | { |
paul@244 | 609 | const char *name; |
paul@244 | 610 | int (*fn)(ext2_filsys, int, char *[]); |
paul@244 | 611 | }; |
paul@244 | 612 | |
paul@244 | 613 | static struct operation operations[] = { |
paul@244 | 614 | {"copy-in", copy_in}, |
paul@244 | 615 | {"copy-out", copy_out}, |
paul@244 | 616 | {"ls", list}, |
paul@244 | 617 | {"mkdir", make_dirs}, |
paul@255 | 618 | {"rm", remove_non_dirs}, |
paul@244 | 619 | {"rmdir", remove_dirs}, |
paul@244 | 620 | {NULL, NULL}, |
paul@244 | 621 | }; |
paul@244 | 622 | |
paul@244 | 623 | /* Main program. */ |
paul@244 | 624 | |
paul@226 | 625 | int main(int argc, char *argv[]) |
paul@226 | 626 | { |
paul@226 | 627 | int flags = EXT2_FLAG_RW; // | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
paul@226 | 628 | ext2_filsys fs = NULL; |
paul@226 | 629 | errcode_t retval; |
paul@226 | 630 | int exitcode = 0; |
paul@226 | 631 | |
paul@241 | 632 | /* Program argument details. */ |
paul@241 | 633 | |
paul@241 | 634 | char **args; |
paul@241 | 635 | char *fsname, *operation, *filename; |
paul@241 | 636 | int num_args; |
paul@244 | 637 | struct operation *op; |
paul@241 | 638 | |
paul@241 | 639 | /* Parse program options and initialise the argument details. */ |
paul@241 | 640 | |
paul@241 | 641 | if (parse_options(argc, argv)) |
paul@241 | 642 | return 1; |
paul@241 | 643 | |
paul@241 | 644 | args = &argv[optind]; |
paul@241 | 645 | num_args = argc - optind; |
paul@241 | 646 | |
paul@241 | 647 | if (num_args < 3) |
paul@226 | 648 | { |
paul@246 | 649 | fprintf(stderr, help_text, argv[0]); |
paul@226 | 650 | return 1; |
paul@226 | 651 | } |
paul@226 | 652 | |
paul@241 | 653 | /* Open the filesystem image using the POSIX file access mechanism. */ |
paul@226 | 654 | |
paul@241 | 655 | fsname = args[0]; |
paul@226 | 656 | |
paul@226 | 657 | retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); |
paul@226 | 658 | if (retval) |
paul@226 | 659 | { |
paul@246 | 660 | fprintf(stderr, "Could not open filesystem: %s\n", fsname); |
paul@226 | 661 | return 1; |
paul@226 | 662 | } |
paul@226 | 663 | |
paul@226 | 664 | //fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
paul@226 | 665 | |
paul@226 | 666 | retval = ext2fs_read_bitmaps(fs); |
paul@226 | 667 | if (retval) |
paul@226 | 668 | { |
paul@246 | 669 | fprintf(stderr, "Could not read bitmaps: %s\n", fsname); |
paul@226 | 670 | return 1; |
paul@226 | 671 | } |
paul@226 | 672 | |
paul@226 | 673 | /* Perform the requested operation. */ |
paul@226 | 674 | |
paul@241 | 675 | operation = args[1]; |
paul@241 | 676 | args = &args[2]; |
paul@241 | 677 | num_args -= 2; |
paul@226 | 678 | |
paul@244 | 679 | for (op = &operations[0]; op->name != NULL; op++) |
paul@226 | 680 | { |
paul@244 | 681 | if (!strcmp(operation, op->name)) |
paul@244 | 682 | { |
paul@244 | 683 | exitcode = op->fn(fs, num_args, args); |
paul@246 | 684 | if (exitcode) |
paul@246 | 685 | fprintf(stderr, "Operation failed: %s\n", operation); |
paul@244 | 686 | break; |
paul@244 | 687 | } |
paul@226 | 688 | } |
paul@244 | 689 | |
paul@244 | 690 | if (op->name == NULL) |
paul@226 | 691 | { |
paul@246 | 692 | fprintf(stderr, "Operation not recognised: %s\n", operation); |
paul@226 | 693 | exitcode = 1; |
paul@226 | 694 | } |
paul@226 | 695 | |
paul@226 | 696 | /* Close the filesystem image. */ |
paul@226 | 697 | |
paul@226 | 698 | retval = ext2fs_flush(fs); |
paul@226 | 699 | if (retval) |
paul@226 | 700 | { |
paul@246 | 701 | fprintf(stderr, "Error flushing filesystem: %s\n", fsname); |
paul@226 | 702 | exitcode = 1; |
paul@226 | 703 | } |
paul@226 | 704 | |
paul@226 | 705 | retval = ext2fs_close(fs); |
paul@226 | 706 | if (retval) |
paul@226 | 707 | { |
paul@246 | 708 | fprintf(stderr, "Error closing filesystem: %s\n", fsname); |
paul@226 | 709 | exitcode = 1; |
paul@226 | 710 | } |
paul@226 | 711 | |
paul@226 | 712 | ext2fs_free(fs); |
paul@226 | 713 | return exitcode; |
paul@226 | 714 | } |
paul@241 | 715 | |
paul@241 | 716 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@241 | 717 | */ |