1 /* 2 * Access a filesystem. 3 * 4 * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <stdio.h> 23 #include <unistd.h> 24 25 #include <ext2fs/ext2fs.h> 26 27 #include "ops.h" 28 #include "session.h" 29 30 31 32 /* Help message. */ 33 34 char help_text[] = "\ 35 Usage: %s [ <options> ] <image file> <operation> <filename>...\n\ 36 \n\ 37 File ownership options:\n\ 38 \n\ 39 -g GID Set group identifier for new files\n\ 40 -u UID Set user identifier for new files\n\ 41 \n\ 42 File permission options:\n\ 43 \n\ 44 -m MASK Set mode/permissions mask for new directories\n\ 45 \n\ 46 Transfer operations:\n\ 47 \n\ 48 copy-in Copy files into a directory within the image\n\ 49 copy-out Copy files from the image into a directory\n\ 50 \n\ 51 Image operations:\n\ 52 \n\ 53 ls List files and directories within the image\n\ 54 mkdir Make directories within the image\n\ 55 rm Remove non-directory objects from the image\n\ 56 rmdir Remove directories from the image\n\ 57 stat Show statistics for files and directories\n\ 58 \n\ 59 Script operations:\n\ 60 \n\ 61 script Read operations from a script file\n\ 62 "; 63 64 /* Operations exposed by the program. */ 65 66 struct operation operations[] = { 67 {"copy-in", copy_in}, 68 {"copy-out", copy_out}, 69 {"ls", list}, 70 {"mkdir", make_dirs}, 71 {"rm", remove_non_dirs}, 72 {"rmdir", remove_dirs}, 73 {"script", run_script}, 74 {"stat", stat_objects}, 75 {NULL, NULL}, 76 }; 77 78 /* Main program. */ 79 80 int main(int argc, char *argv[]) 81 { 82 int flags = EXT2_FLAG_RW; // | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; 83 ext2_filsys fs = NULL; 84 errcode_t retval; 85 int exitcode = 0; 86 87 /* Program argument details. */ 88 89 char **args; 90 char *fsname, *filename; 91 int num_args; 92 enum op_results op_result; 93 94 /* Parse program options and initialise the argument details. */ 95 96 if (parse_options(argc, argv)) 97 return 1; 98 99 args = &argv[optind]; 100 num_args = argc - optind; 101 102 if (num_args < 3) 103 { 104 fprintf(stderr, help_text, argv[0]); 105 return 1; 106 } 107 108 /* Open the filesystem image using the POSIX file access mechanism. */ 109 110 fsname = args[0]; 111 112 retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); 113 if (retval) 114 { 115 fprintf(stderr, "Could not open filesystem: %s\n", fsname); 116 return 1; 117 } 118 119 //fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; 120 121 retval = ext2fs_read_bitmaps(fs); 122 if (retval) 123 { 124 fprintf(stderr, "Could not read bitmaps: %s\n", fsname); 125 return 1; 126 } 127 128 /* Perform the requested operation. */ 129 130 op_result = run_operation(fs, args[1], num_args - 2, &args[2]); 131 exitcode = handle_op_result(args[1], op_result); 132 133 /* Close the filesystem image. */ 134 135 retval = ext2fs_flush(fs); 136 if (retval) 137 { 138 fprintf(stderr, "Error flushing filesystem: %s\n", fsname); 139 exitcode = 1; 140 } 141 142 retval = ext2fs_close(fs); 143 if (retval) 144 { 145 fprintf(stderr, "Error closing filesystem: %s\n", fsname); 146 exitcode = 1; 147 } 148 149 ext2fs_free(fs); 150 return exitcode; 151 } 152 153 /* vim: tabstop=4 expandtab shiftwidth=4 154 */