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 <string.h> |
paul@241 | 24 | #include <unistd.h> |
paul@226 | 25 | |
paul@226 | 26 | #include <ext2fs/ext2fs.h> |
paul@226 | 27 | |
paul@263 | 28 | #include "input.h" |
paul@278 | 29 | #include "ops.h" |
paul@278 | 30 | #include "session.h" |
paul@226 | 31 | |
paul@226 | 32 | |
paul@226 | 33 | |
paul@226 | 34 | /* Copy buffer size. */ |
paul@226 | 35 | |
paul@278 | 36 | extern int BUFSIZE; |
paul@226 | 37 | |
paul@262 | 38 | /* Maximum number of arguments in scripts. */ |
paul@262 | 39 | |
paul@262 | 40 | const int MAX_ARGS = 32; |
paul@262 | 41 | |
paul@226 | 42 | |
paul@226 | 43 | |
paul@262 | 44 | /* Read operations from a script file. */ |
paul@262 | 45 | |
paul@262 | 46 | enum op_results |
paul@262 | 47 | { |
paul@262 | 48 | OP_SUCCESS = 0, |
paul@262 | 49 | OP_FAILED = 1, |
paul@262 | 50 | OP_UNKNOWN = 2, |
paul@262 | 51 | }; |
paul@262 | 52 | |
paul@262 | 53 | int handle_op_result(const char *operation, enum op_results op_result) |
paul@262 | 54 | { |
paul@262 | 55 | if (op_result == OP_UNKNOWN) |
paul@262 | 56 | { |
paul@262 | 57 | fprintf(stderr, "Operation not recognised: %s\n", operation); |
paul@262 | 58 | return 1; |
paul@262 | 59 | } |
paul@262 | 60 | else if (op_result == OP_FAILED) |
paul@262 | 61 | { |
paul@262 | 62 | fprintf(stderr, "Operation failed: %s\n", operation); |
paul@262 | 63 | return 1; |
paul@262 | 64 | } |
paul@262 | 65 | else |
paul@262 | 66 | return 0; |
paul@262 | 67 | } |
paul@262 | 68 | |
paul@262 | 69 | enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]); |
paul@262 | 70 | |
paul@262 | 71 | int run_script(ext2_filsys fs, int argc, char *argv[]) |
paul@262 | 72 | { |
paul@262 | 73 | FILE *fp; |
paul@262 | 74 | char buffer[BUFSIZE]; |
paul@262 | 75 | struct read_line_state state; |
paul@262 | 76 | enum op_results op_result; |
paul@262 | 77 | int num_args; |
paul@262 | 78 | char *args[MAX_ARGS]; |
paul@262 | 79 | int i; |
paul@262 | 80 | |
paul@262 | 81 | for (i = 0; i < argc; i++) |
paul@262 | 82 | { |
paul@262 | 83 | fp = fopen(argv[i], "r"); |
paul@262 | 84 | |
paul@262 | 85 | state.buffer = buffer; |
paul@262 | 86 | state.start = buffer; |
paul@262 | 87 | state.end = buffer; |
paul@263 | 88 | state.buffer_size = BUFSIZE; |
paul@262 | 89 | state.remaining = BUFSIZE - 1; |
paul@262 | 90 | |
paul@262 | 91 | while (read_line(fp, &state) != NULL) |
paul@262 | 92 | { |
paul@262 | 93 | parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS); |
paul@262 | 94 | |
paul@262 | 95 | if (num_args > 1) |
paul@262 | 96 | { |
paul@262 | 97 | op_result = run_operation(fs, args[0], num_args - 1, &args[1]); |
paul@262 | 98 | |
paul@262 | 99 | if (handle_op_result(args[0], op_result)) |
paul@262 | 100 | return 1; |
paul@262 | 101 | } |
paul@262 | 102 | |
paul@262 | 103 | state.start = state.eolp + 1; |
paul@262 | 104 | } |
paul@262 | 105 | |
paul@262 | 106 | fclose(fp); |
paul@262 | 107 | } |
paul@262 | 108 | |
paul@262 | 109 | return 0; |
paul@262 | 110 | } |
paul@262 | 111 | |
paul@244 | 112 | |
paul@244 | 113 | |
paul@244 | 114 | /* Help message. */ |
paul@241 | 115 | |
paul@241 | 116 | char help_text[] = "\ |
paul@241 | 117 | Usage: %s [ <options> ] <image file> <operation> <filename>...\n\ |
paul@241 | 118 | \n\ |
paul@241 | 119 | File ownership options:\n\ |
paul@241 | 120 | \n\ |
paul@241 | 121 | -g GID Set group identifier for new files\n\ |
paul@241 | 122 | -u UID Set user identifier for new files\n\ |
paul@241 | 123 | \n\ |
paul@241 | 124 | File permission options:\n\ |
paul@241 | 125 | \n\ |
paul@241 | 126 | -m MASK Set mode/permissions mask for new directories\n\ |
paul@244 | 127 | \n\ |
paul@262 | 128 | Transfer operations:\n\ |
paul@244 | 129 | \n\ |
paul@244 | 130 | copy-in Copy files into a directory within the image\n\ |
paul@244 | 131 | copy-out Copy files from the image into a directory\n\ |
paul@262 | 132 | \n\ |
paul@262 | 133 | Image operations:\n\ |
paul@262 | 134 | \n\ |
paul@244 | 135 | ls List files and directories within the image\n\ |
paul@244 | 136 | mkdir Make directories within the image\n\ |
paul@255 | 137 | rm Remove non-directory objects from the image\n\ |
paul@244 | 138 | rmdir Remove directories from the image\n\ |
paul@264 | 139 | stat Show statistics for files and directories\n\ |
paul@262 | 140 | \n\ |
paul@262 | 141 | Script operations:\n\ |
paul@262 | 142 | \n\ |
paul@262 | 143 | script Read operations from a script file\n\ |
paul@241 | 144 | "; |
paul@241 | 145 | |
paul@244 | 146 | /* Operations exposed by the program. */ |
paul@244 | 147 | |
paul@244 | 148 | struct operation |
paul@244 | 149 | { |
paul@244 | 150 | const char *name; |
paul@244 | 151 | int (*fn)(ext2_filsys, int, char *[]); |
paul@244 | 152 | }; |
paul@244 | 153 | |
paul@244 | 154 | static struct operation operations[] = { |
paul@244 | 155 | {"copy-in", copy_in}, |
paul@244 | 156 | {"copy-out", copy_out}, |
paul@244 | 157 | {"ls", list}, |
paul@244 | 158 | {"mkdir", make_dirs}, |
paul@255 | 159 | {"rm", remove_non_dirs}, |
paul@244 | 160 | {"rmdir", remove_dirs}, |
paul@262 | 161 | {"script", run_script}, |
paul@264 | 162 | {"stat", stat_objects}, |
paul@244 | 163 | {NULL, NULL}, |
paul@244 | 164 | }; |
paul@244 | 165 | |
paul@262 | 166 | /* Invocation of operations. */ |
paul@262 | 167 | |
paul@262 | 168 | enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]) |
paul@262 | 169 | { |
paul@262 | 170 | struct operation *op; |
paul@262 | 171 | int exitcode; |
paul@262 | 172 | |
paul@262 | 173 | for (op = &operations[0]; op->name != NULL; op++) |
paul@262 | 174 | { |
paul@262 | 175 | if (!strcmp(operation, op->name)) |
paul@262 | 176 | { |
paul@262 | 177 | exitcode = op->fn(fs, argc, argv); |
paul@262 | 178 | if (exitcode) |
paul@262 | 179 | return OP_FAILED; |
paul@262 | 180 | break; |
paul@262 | 181 | } |
paul@262 | 182 | } |
paul@262 | 183 | |
paul@262 | 184 | if (op->name == NULL) |
paul@262 | 185 | return OP_UNKNOWN; |
paul@262 | 186 | |
paul@262 | 187 | return OP_SUCCESS; |
paul@262 | 188 | } |
paul@262 | 189 | |
paul@244 | 190 | /* Main program. */ |
paul@244 | 191 | |
paul@226 | 192 | int main(int argc, char *argv[]) |
paul@226 | 193 | { |
paul@226 | 194 | int flags = EXT2_FLAG_RW; // | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
paul@226 | 195 | ext2_filsys fs = NULL; |
paul@226 | 196 | errcode_t retval; |
paul@226 | 197 | int exitcode = 0; |
paul@226 | 198 | |
paul@241 | 199 | /* Program argument details. */ |
paul@241 | 200 | |
paul@241 | 201 | char **args; |
paul@262 | 202 | char *fsname, *filename; |
paul@241 | 203 | int num_args; |
paul@262 | 204 | enum op_results op_result; |
paul@241 | 205 | |
paul@241 | 206 | /* Parse program options and initialise the argument details. */ |
paul@241 | 207 | |
paul@241 | 208 | if (parse_options(argc, argv)) |
paul@241 | 209 | return 1; |
paul@241 | 210 | |
paul@241 | 211 | args = &argv[optind]; |
paul@241 | 212 | num_args = argc - optind; |
paul@241 | 213 | |
paul@241 | 214 | if (num_args < 3) |
paul@226 | 215 | { |
paul@246 | 216 | fprintf(stderr, help_text, argv[0]); |
paul@226 | 217 | return 1; |
paul@226 | 218 | } |
paul@226 | 219 | |
paul@241 | 220 | /* Open the filesystem image using the POSIX file access mechanism. */ |
paul@226 | 221 | |
paul@241 | 222 | fsname = args[0]; |
paul@226 | 223 | |
paul@226 | 224 | retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); |
paul@226 | 225 | if (retval) |
paul@226 | 226 | { |
paul@246 | 227 | fprintf(stderr, "Could not open filesystem: %s\n", fsname); |
paul@226 | 228 | return 1; |
paul@226 | 229 | } |
paul@226 | 230 | |
paul@226 | 231 | //fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
paul@226 | 232 | |
paul@226 | 233 | retval = ext2fs_read_bitmaps(fs); |
paul@226 | 234 | if (retval) |
paul@226 | 235 | { |
paul@246 | 236 | fprintf(stderr, "Could not read bitmaps: %s\n", fsname); |
paul@226 | 237 | return 1; |
paul@226 | 238 | } |
paul@226 | 239 | |
paul@226 | 240 | /* Perform the requested operation. */ |
paul@226 | 241 | |
paul@262 | 242 | op_result = run_operation(fs, args[1], num_args - 2, &args[2]); |
paul@262 | 243 | exitcode = handle_op_result(args[1], op_result); |
paul@226 | 244 | |
paul@226 | 245 | /* Close the filesystem image. */ |
paul@226 | 246 | |
paul@226 | 247 | retval = ext2fs_flush(fs); |
paul@226 | 248 | if (retval) |
paul@226 | 249 | { |
paul@246 | 250 | fprintf(stderr, "Error flushing filesystem: %s\n", fsname); |
paul@226 | 251 | exitcode = 1; |
paul@226 | 252 | } |
paul@226 | 253 | |
paul@226 | 254 | retval = ext2fs_close(fs); |
paul@226 | 255 | if (retval) |
paul@226 | 256 | { |
paul@246 | 257 | fprintf(stderr, "Error closing filesystem: %s\n", fsname); |
paul@226 | 258 | exitcode = 1; |
paul@226 | 259 | } |
paul@226 | 260 | |
paul@226 | 261 | ext2fs_free(fs); |
paul@226 | 262 | return exitcode; |
paul@226 | 263 | } |
paul@241 | 264 | |
paul@241 | 265 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@241 | 266 | */ |