paul@283 | 1 | /* |
paul@283 | 2 | * Access a filesystem. |
paul@283 | 3 | * |
paul@591 | 4 | * Copyright (C) 2019, 2022, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@283 | 5 | * |
paul@283 | 6 | * This program is free software; you can redistribute it and/or |
paul@283 | 7 | * modify it under the terms of the GNU General Public License as |
paul@283 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@283 | 9 | * the License, or (at your option) any later version. |
paul@283 | 10 | * |
paul@283 | 11 | * This program is distributed in the hope that it will be useful, |
paul@283 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@283 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@283 | 14 | * GNU General Public License for more details. |
paul@283 | 15 | * |
paul@283 | 16 | * You should have received a copy of the GNU General Public License |
paul@283 | 17 | * along with this program; if not, write to the Free Software |
paul@283 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@283 | 19 | * Boston, MA 02110-1301, USA |
paul@283 | 20 | */ |
paul@283 | 21 | |
paul@283 | 22 | #include <stdio.h> |
paul@294 | 23 | #include <unistd.h> |
paul@283 | 24 | |
paul@283 | 25 | #include "ops.h" |
paul@294 | 26 | #include "session.h" |
paul@283 | 27 | |
paul@283 | 28 | |
paul@283 | 29 | |
paul@283 | 30 | /* Help message. */ |
paul@283 | 31 | |
paul@283 | 32 | char help_text[] = "\ |
paul@283 | 33 | Usage: %s [ <options> ] <operation> <filename>...\n\ |
paul@283 | 34 | \n\ |
paul@283 | 35 | File permission options:\n\ |
paul@283 | 36 | \n\ |
paul@283 | 37 | -m MASK Set mode/permissions mask for new directories\n\ |
paul@283 | 38 | \n\ |
paul@283 | 39 | Transfer operations:\n\ |
paul@283 | 40 | \n\ |
paul@283 | 41 | copy-in Copy files into a directory within the image\n\ |
paul@283 | 42 | \n\ |
paul@283 | 43 | Image operations:\n\ |
paul@283 | 44 | \n\ |
paul@283 | 45 | ls List files and directories within the image\n\ |
paul@283 | 46 | mkdir Make directories within the image\n\ |
paul@283 | 47 | rm Remove non-directory objects from the image\n\ |
paul@283 | 48 | rmdir Remove directories from the image\n\ |
paul@283 | 49 | stat Show statistics for files and directories\n\ |
paul@283 | 50 | \n\ |
paul@283 | 51 | Script operations:\n\ |
paul@283 | 52 | \n\ |
paul@591 | 53 | script Read operations from a script file (or stdin)\n\ |
paul@600 | 54 | Execution operations:\n\ |
paul@600 | 55 | \n\ |
paul@606 | 56 | jobs Show initiated programs\n\ |
paul@600 | 57 | run Run a program from the filesystem\n\ |
paul@606 | 58 | wait Wait for a program to finish\n\ |
paul@283 | 59 | "; |
paul@283 | 60 | |
paul@283 | 61 | /* Operations exposed by the program. */ |
paul@283 | 62 | |
paul@283 | 63 | struct operation operations[] = { |
paul@299 | 64 | {"copy-in", copy_in}, |
paul@606 | 65 | {"jobs", show_programs}, |
paul@283 | 66 | {"ls", list_objects}, |
paul@294 | 67 | {"mkdir", make_dirs}, |
paul@283 | 68 | {"rm", remove_non_dirs}, |
paul@283 | 69 | {"rmdir", remove_dirs}, |
paul@600 | 70 | {"run", run_program}, |
paul@283 | 71 | {"script", run_script}, |
paul@283 | 72 | {"stat", stat_objects}, |
paul@606 | 73 | {"wait", wait_program}, |
paul@283 | 74 | {NULL, NULL}, |
paul@283 | 75 | }; |
paul@283 | 76 | |
paul@283 | 77 | /* Main program. */ |
paul@283 | 78 | |
paul@283 | 79 | int main(int argc, char *argv[]) |
paul@283 | 80 | { |
paul@283 | 81 | /* Program argument details. */ |
paul@283 | 82 | |
paul@283 | 83 | char **args; |
paul@283 | 84 | int num_args; |
paul@602 | 85 | int exitcode; |
paul@283 | 86 | enum op_results op_result; |
paul@283 | 87 | |
paul@294 | 88 | /* Parse program options and initialise the argument details. */ |
paul@294 | 89 | |
paul@294 | 90 | if (parse_options(argc, argv)) |
paul@294 | 91 | return 1; |
paul@294 | 92 | |
paul@294 | 93 | args = &argv[optind]; |
paul@294 | 94 | num_args = argc - optind; |
paul@283 | 95 | |
paul@283 | 96 | if (num_args < 2) |
paul@283 | 97 | { |
paul@283 | 98 | fprintf(stderr, help_text, argv[0]); |
paul@283 | 99 | return 1; |
paul@283 | 100 | } |
paul@283 | 101 | |
paul@283 | 102 | /* Perform the requested operation. */ |
paul@283 | 103 | |
paul@602 | 104 | op_result = run_operation(args[0], num_args - 1, &args[1], &exitcode); |
paul@283 | 105 | return handle_op_result(args[0], op_result); |
paul@283 | 106 | } |
paul@283 | 107 | |
paul@283 | 108 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@283 | 109 | */ |