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@241 | 23 | #include <unistd.h> |
paul@226 | 24 | |
paul@226 | 25 | #include <ext2fs/ext2fs.h> |
paul@226 | 26 | |
paul@278 | 27 | #include "ops.h" |
paul@278 | 28 | #include "session.h" |
paul@226 | 29 | |
paul@226 | 30 | |
paul@226 | 31 | |
paul@244 | 32 | /* Help message. */ |
paul@241 | 33 | |
paul@241 | 34 | char help_text[] = "\ |
paul@241 | 35 | Usage: %s [ <options> ] <image file> <operation> <filename>...\n\ |
paul@241 | 36 | \n\ |
paul@241 | 37 | File ownership options:\n\ |
paul@241 | 38 | \n\ |
paul@241 | 39 | -g GID Set group identifier for new files\n\ |
paul@241 | 40 | -u UID Set user identifier for new files\n\ |
paul@241 | 41 | \n\ |
paul@241 | 42 | File permission options:\n\ |
paul@241 | 43 | \n\ |
paul@241 | 44 | -m MASK Set mode/permissions mask for new directories\n\ |
paul@244 | 45 | \n\ |
paul@262 | 46 | Transfer operations:\n\ |
paul@244 | 47 | \n\ |
paul@244 | 48 | copy-in Copy files into a directory within the image\n\ |
paul@244 | 49 | copy-out Copy files from the image into a directory\n\ |
paul@262 | 50 | \n\ |
paul@262 | 51 | Image operations:\n\ |
paul@262 | 52 | \n\ |
paul@244 | 53 | ls List files and directories within the image\n\ |
paul@244 | 54 | mkdir Make directories within the image\n\ |
paul@255 | 55 | rm Remove non-directory objects from the image\n\ |
paul@244 | 56 | rmdir Remove directories from the image\n\ |
paul@264 | 57 | stat Show statistics for files and directories\n\ |
paul@262 | 58 | \n\ |
paul@262 | 59 | Script operations:\n\ |
paul@262 | 60 | \n\ |
paul@262 | 61 | script Read operations from a script file\n\ |
paul@241 | 62 | "; |
paul@241 | 63 | |
paul@244 | 64 | /* Operations exposed by the program. */ |
paul@244 | 65 | |
paul@279 | 66 | struct operation operations[] = { |
paul@244 | 67 | {"copy-in", copy_in}, |
paul@244 | 68 | {"copy-out", copy_out}, |
paul@244 | 69 | {"ls", list}, |
paul@244 | 70 | {"mkdir", make_dirs}, |
paul@255 | 71 | {"rm", remove_non_dirs}, |
paul@244 | 72 | {"rmdir", remove_dirs}, |
paul@262 | 73 | {"script", run_script}, |
paul@264 | 74 | {"stat", stat_objects}, |
paul@244 | 75 | {NULL, NULL}, |
paul@244 | 76 | }; |
paul@244 | 77 | |
paul@244 | 78 | /* Main program. */ |
paul@244 | 79 | |
paul@226 | 80 | int main(int argc, char *argv[]) |
paul@226 | 81 | { |
paul@226 | 82 | int flags = EXT2_FLAG_RW; // | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
paul@226 | 83 | ext2_filsys fs = NULL; |
paul@226 | 84 | errcode_t retval; |
paul@226 | 85 | int exitcode = 0; |
paul@226 | 86 | |
paul@241 | 87 | /* Program argument details. */ |
paul@241 | 88 | |
paul@241 | 89 | char **args; |
paul@262 | 90 | char *fsname, *filename; |
paul@241 | 91 | int num_args; |
paul@262 | 92 | enum op_results op_result; |
paul@241 | 93 | |
paul@241 | 94 | /* Parse program options and initialise the argument details. */ |
paul@241 | 95 | |
paul@241 | 96 | if (parse_options(argc, argv)) |
paul@241 | 97 | return 1; |
paul@241 | 98 | |
paul@241 | 99 | args = &argv[optind]; |
paul@241 | 100 | num_args = argc - optind; |
paul@241 | 101 | |
paul@241 | 102 | if (num_args < 3) |
paul@226 | 103 | { |
paul@246 | 104 | fprintf(stderr, help_text, argv[0]); |
paul@226 | 105 | return 1; |
paul@226 | 106 | } |
paul@226 | 107 | |
paul@241 | 108 | /* Open the filesystem image using the POSIX file access mechanism. */ |
paul@226 | 109 | |
paul@241 | 110 | fsname = args[0]; |
paul@226 | 111 | |
paul@226 | 112 | retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); |
paul@226 | 113 | if (retval) |
paul@226 | 114 | { |
paul@246 | 115 | fprintf(stderr, "Could not open filesystem: %s\n", fsname); |
paul@226 | 116 | return 1; |
paul@226 | 117 | } |
paul@226 | 118 | |
paul@226 | 119 | //fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
paul@226 | 120 | |
paul@226 | 121 | retval = ext2fs_read_bitmaps(fs); |
paul@226 | 122 | if (retval) |
paul@226 | 123 | { |
paul@246 | 124 | fprintf(stderr, "Could not read bitmaps: %s\n", fsname); |
paul@226 | 125 | return 1; |
paul@226 | 126 | } |
paul@226 | 127 | |
paul@226 | 128 | /* Perform the requested operation. */ |
paul@226 | 129 | |
paul@262 | 130 | op_result = run_operation(fs, args[1], num_args - 2, &args[2]); |
paul@262 | 131 | exitcode = handle_op_result(args[1], op_result); |
paul@226 | 132 | |
paul@226 | 133 | /* Close the filesystem image. */ |
paul@226 | 134 | |
paul@226 | 135 | retval = ext2fs_flush(fs); |
paul@226 | 136 | if (retval) |
paul@226 | 137 | { |
paul@246 | 138 | fprintf(stderr, "Error flushing filesystem: %s\n", fsname); |
paul@226 | 139 | exitcode = 1; |
paul@226 | 140 | } |
paul@226 | 141 | |
paul@226 | 142 | retval = ext2fs_close(fs); |
paul@226 | 143 | if (retval) |
paul@226 | 144 | { |
paul@246 | 145 | fprintf(stderr, "Error closing filesystem: %s\n", fsname); |
paul@226 | 146 | exitcode = 1; |
paul@226 | 147 | } |
paul@226 | 148 | |
paul@226 | 149 | ext2fs_free(fs); |
paul@226 | 150 | return exitcode; |
paul@226 | 151 | } |
paul@241 | 152 | |
paul@241 | 153 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@241 | 154 | */ |