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