| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup copy
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Copy files and directories.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <fmgt.h>
|
|---|
| 37 | #include <io/console.h>
|
|---|
| 38 | #include <io/cons_event.h>
|
|---|
| 39 | #include <io/kbd_event.h>
|
|---|
| 40 | #include <stdbool.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <str.h>
|
|---|
| 44 | #include <str_error.h>
|
|---|
| 45 |
|
|---|
| 46 | #define NAME "copy"
|
|---|
| 47 |
|
|---|
| 48 | static bool copy_abort_query(void *);
|
|---|
| 49 | static void copy_progress(void *, fmgt_progress_t *);
|
|---|
| 50 | static fmgt_error_action_t copy_io_error_query(void *, fmgt_io_error_t *);
|
|---|
| 51 |
|
|---|
| 52 | static bool prog_upd = false;
|
|---|
| 53 | static bool nonint = false;
|
|---|
| 54 | static bool quiet = false;
|
|---|
| 55 |
|
|---|
| 56 | static console_ctrl_t *con;
|
|---|
| 57 |
|
|---|
| 58 | static fmgt_cb_t copy_fmgt_cb = {
|
|---|
| 59 | .abort_query = copy_abort_query,
|
|---|
| 60 | .io_error_query = copy_io_error_query,
|
|---|
| 61 | .progress = copy_progress,
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | static void print_syntax(void)
|
|---|
| 65 | {
|
|---|
| 66 | printf("Copy files and directories.\n");
|
|---|
| 67 | printf("Syntax: %s [<options] <source>... <dest>\n", NAME);
|
|---|
| 68 | printf("\t-h help\n");
|
|---|
| 69 | printf("\t-n non-interactive\n");
|
|---|
| 70 | printf("\t-q quiet\n");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Called by fmgt to query for user abort.
|
|---|
| 74 | *
|
|---|
| 75 | * @param arg Argument (not used)
|
|---|
| 76 | * @return @c true iff user requested abort
|
|---|
| 77 | */
|
|---|
| 78 | static bool copy_abort_query(void *arg)
|
|---|
| 79 | {
|
|---|
| 80 | cons_event_t event;
|
|---|
| 81 | kbd_event_t *ev;
|
|---|
| 82 | errno_t rc;
|
|---|
| 83 | usec_t timeout;
|
|---|
| 84 |
|
|---|
| 85 | if (con == NULL)
|
|---|
| 86 | return false;
|
|---|
| 87 |
|
|---|
| 88 | timeout = 0;
|
|---|
| 89 | rc = console_get_event_timeout(con, &event, &timeout);
|
|---|
| 90 | if (rc != EOK)
|
|---|
| 91 | return false;
|
|---|
| 92 |
|
|---|
| 93 | if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
|
|---|
| 94 | ev = &event.ev.key;
|
|---|
| 95 | if ((ev->mods & KM_ALT) == 0 &&
|
|---|
| 96 | (ev->mods & KM_SHIFT) == 0 &&
|
|---|
| 97 | (ev->mods & KM_CTRL) != 0) {
|
|---|
| 98 | if (ev->key == KC_C)
|
|---|
| 99 | return true;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return false;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Called by fmgt to give the user progress update.
|
|---|
| 107 | *
|
|---|
| 108 | * @param arg Argument (not used)
|
|---|
| 109 | * @param progress Progress report
|
|---|
| 110 | */
|
|---|
| 111 | static void copy_progress(void *arg, fmgt_progress_t *progress)
|
|---|
| 112 | {
|
|---|
| 113 | (void)arg;
|
|---|
| 114 |
|
|---|
| 115 | if (!quiet) {
|
|---|
| 116 | printf("\rCopied %s files, %s; current file: %s done. "
|
|---|
| 117 | "\b\b", progress->total_procf, progress->total_procb,
|
|---|
| 118 | progress->curf_percent);
|
|---|
| 119 | fflush(stdout);
|
|---|
| 120 | prog_upd = true;
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** Called by fmgt to let user choose I/O error recovery action.
|
|---|
| 125 | *
|
|---|
| 126 | * @param arg Argument (not used)
|
|---|
| 127 | * @param err I/O error report
|
|---|
| 128 | * @return Error recovery action.
|
|---|
| 129 | */
|
|---|
| 130 | static fmgt_error_action_t copy_io_error_query(void *arg,
|
|---|
| 131 | fmgt_io_error_t *err)
|
|---|
| 132 | {
|
|---|
| 133 | cons_event_t event;
|
|---|
| 134 | kbd_event_t *ev;
|
|---|
| 135 | errno_t rc;
|
|---|
| 136 |
|
|---|
| 137 | (void)arg;
|
|---|
| 138 |
|
|---|
| 139 | if (nonint)
|
|---|
| 140 | return fmgt_er_abort;
|
|---|
| 141 |
|
|---|
| 142 | if (prog_upd)
|
|---|
| 143 | putchar('\n');
|
|---|
| 144 |
|
|---|
| 145 | fprintf(stderr, "I/O error %s file '%s' (%s).\n",
|
|---|
| 146 | err->optype == fmgt_io_write ? "writing" : "reading",
|
|---|
| 147 | err->fname, str_error(err->rc));
|
|---|
| 148 | fprintf(stderr, "[A]bort or [R]etry?\n");
|
|---|
| 149 |
|
|---|
| 150 | if (con == NULL)
|
|---|
| 151 | return fmgt_er_abort;
|
|---|
| 152 |
|
|---|
| 153 | while (true) {
|
|---|
| 154 | rc = console_get_event(con, &event);
|
|---|
| 155 | if (rc != EOK)
|
|---|
| 156 | return fmgt_er_abort;
|
|---|
| 157 |
|
|---|
| 158 | if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
|
|---|
| 159 | ev = &event.ev.key;
|
|---|
| 160 | if ((ev->mods & KM_ALT) == 0 &&
|
|---|
| 161 | (ev->mods & KM_CTRL) == 0) {
|
|---|
| 162 | if (ev->c == 'r' || ev->c == 'R')
|
|---|
| 163 | return fmgt_er_retry;
|
|---|
| 164 | if (ev->c == 'a' || ev->c == 'A')
|
|---|
| 165 | return fmgt_er_abort;
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
|
|---|
| 170 | ev = &event.ev.key;
|
|---|
| 171 | if ((ev->mods & KM_ALT) == 0 &&
|
|---|
| 172 | (ev->mods & KM_SHIFT) == 0 &&
|
|---|
| 173 | (ev->mods & KM_CTRL) != 0) {
|
|---|
| 174 | if (ev->key == KC_C)
|
|---|
| 175 | return fmgt_er_abort;
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | int main(int argc, char *argv[])
|
|---|
| 182 | {
|
|---|
| 183 | fmgt_t *fmgt = NULL;
|
|---|
| 184 | errno_t rc;
|
|---|
| 185 | int i;
|
|---|
| 186 | fmgt_flist_t *flist = NULL;
|
|---|
| 187 | const char *dest;
|
|---|
| 188 |
|
|---|
| 189 | rc = fmgt_flist_create(&flist);
|
|---|
| 190 | if (rc != EOK) {
|
|---|
| 191 | printf("Out of memory.\n");
|
|---|
| 192 | goto error;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | con = console_init(stdin, stdout);
|
|---|
| 196 |
|
|---|
| 197 | i = 1;
|
|---|
| 198 | while (i < argc && argv[i][0] == '-') {
|
|---|
| 199 | if (str_cmp(argv[i], "-h") == 0) {
|
|---|
| 200 | print_syntax();
|
|---|
| 201 | return 0;
|
|---|
| 202 | } else if (str_cmp(argv[i], "-n") == 0) {
|
|---|
| 203 | ++i;
|
|---|
| 204 | nonint = true;
|
|---|
| 205 | } else if (str_cmp(argv[i], "-q") == 0) {
|
|---|
| 206 | ++i;
|
|---|
| 207 | quiet = true;
|
|---|
| 208 | } else {
|
|---|
| 209 | printf("Invalid option '%s'.\n", argv[i]);
|
|---|
| 210 | print_syntax();
|
|---|
| 211 | goto error;
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /* Need at least one source and one destination. */
|
|---|
| 216 | if (i + 1 >= argc) {
|
|---|
| 217 | print_syntax();
|
|---|
| 218 | goto error;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /* All arguments except the last one are sources. */
|
|---|
| 222 | do {
|
|---|
| 223 | rc = fmgt_flist_append(flist, argv[i++]);
|
|---|
| 224 | if (rc != EOK) {
|
|---|
| 225 | printf("Out of memory.\n");
|
|---|
| 226 | goto error;
|
|---|
| 227 | }
|
|---|
| 228 | } while (i + 1 < argc);
|
|---|
| 229 |
|
|---|
| 230 | dest = argv[i];
|
|---|
| 231 |
|
|---|
| 232 | rc = fmgt_create(&fmgt);
|
|---|
| 233 | if (rc != EOK) {
|
|---|
| 234 | printf("Out of memory.\n");
|
|---|
| 235 | goto error;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | fmgt_set_cb(fmgt, ©_fmgt_cb, NULL);
|
|---|
| 239 |
|
|---|
| 240 | rc = fmgt_copy(fmgt, flist, dest);
|
|---|
| 241 | if (prog_upd)
|
|---|
| 242 | putchar('\n');
|
|---|
| 243 | if (rc != EOK) {
|
|---|
| 244 | printf("Error creating file: %s.\n", str_error(rc));
|
|---|
| 245 | goto error;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | fmgt_flist_destroy(flist);
|
|---|
| 249 | fmgt_destroy(fmgt);
|
|---|
| 250 | return 0;
|
|---|
| 251 | error:
|
|---|
| 252 | if (flist != NULL)
|
|---|
| 253 | fmgt_flist_destroy(flist);
|
|---|
| 254 | if (fmgt != NULL)
|
|---|
| 255 | fmgt_destroy(fmgt);
|
|---|
| 256 | return 1;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /** @}
|
|---|
| 260 | */
|
|---|