source: mainline/uspace/app/copy/copy.c

Last change on this file was 79b77ce, checked in by Jiri Svoboda <jiri@…>, 9 hours ago

Allow retrying failed file/dir creation and file open.

  • Property mode set to 100644
File size: 6.3 KB
Line 
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
48static bool copy_abort_query(void *);
49static void copy_progress(void *, fmgt_progress_t *);
50static fmgt_error_action_t copy_io_error_query(void *, fmgt_io_error_t *);
51
52static bool prog_upd = false;
53static bool nonint = false;
54static bool quiet = false;
55
56static console_ctrl_t *con;
57
58static 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
64static 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 */
78static 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 */
111static 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 */
130static 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 const char *opstr = NULL;
136 errno_t rc;
137
138 (void)arg;
139
140 if (nonint)
141 return fmgt_er_abort;
142
143 if (prog_upd)
144 putchar('\n');
145
146 switch (err->optype) {
147 case fmgt_io_read:
148 opstr = "reading";
149 break;
150 case fmgt_io_write:
151 opstr = "writing";
152 break;
153 case fmgt_io_create:
154 opstr = "creating";
155 break;
156 case fmgt_io_open:
157 opstr = "opening";
158 break;
159 }
160
161 fprintf(stderr, "I/O error %s file '%s' (%s).\n", opstr, err->fname,
162 str_error(err->rc));
163 fprintf(stderr, "[A]bort or [R]etry?\n");
164
165 if (con == NULL)
166 return fmgt_er_abort;
167
168 while (true) {
169 rc = console_get_event(con, &event);
170 if (rc != EOK)
171 return fmgt_er_abort;
172
173 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
174 ev = &event.ev.key;
175 if ((ev->mods & KM_ALT) == 0 &&
176 (ev->mods & KM_CTRL) == 0) {
177 if (ev->c == 'r' || ev->c == 'R')
178 return fmgt_er_retry;
179 if (ev->c == 'a' || ev->c == 'A')
180 return fmgt_er_abort;
181 }
182 }
183
184 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
185 ev = &event.ev.key;
186 if ((ev->mods & KM_ALT) == 0 &&
187 (ev->mods & KM_SHIFT) == 0 &&
188 (ev->mods & KM_CTRL) != 0) {
189 if (ev->key == KC_C)
190 return fmgt_er_abort;
191 }
192 }
193 }
194}
195
196int main(int argc, char *argv[])
197{
198 fmgt_t *fmgt = NULL;
199 errno_t rc;
200 int i;
201 fmgt_flist_t *flist = NULL;
202 const char *dest;
203
204 rc = fmgt_flist_create(&flist);
205 if (rc != EOK) {
206 printf("Out of memory.\n");
207 goto error;
208 }
209
210 con = console_init(stdin, stdout);
211
212 i = 1;
213 while (i < argc && argv[i][0] == '-') {
214 if (str_cmp(argv[i], "-h") == 0) {
215 print_syntax();
216 return 0;
217 } else if (str_cmp(argv[i], "-n") == 0) {
218 ++i;
219 nonint = true;
220 } else if (str_cmp(argv[i], "-q") == 0) {
221 ++i;
222 quiet = true;
223 } else {
224 printf("Invalid option '%s'.\n", argv[i]);
225 print_syntax();
226 goto error;
227 }
228 }
229
230 /* Need at least one source and one destination. */
231 if (i + 1 >= argc) {
232 print_syntax();
233 goto error;
234 }
235
236 /* All arguments except the last one are sources. */
237 do {
238 rc = fmgt_flist_append(flist, argv[i++]);
239 if (rc != EOK) {
240 printf("Out of memory.\n");
241 goto error;
242 }
243 } while (i + 1 < argc);
244
245 dest = argv[i];
246
247 rc = fmgt_create(&fmgt);
248 if (rc != EOK) {
249 printf("Out of memory.\n");
250 goto error;
251 }
252
253 fmgt_set_cb(fmgt, &copy_fmgt_cb, NULL);
254
255 rc = fmgt_copy(fmgt, flist, dest);
256 if (prog_upd)
257 putchar('\n');
258 if (rc != EOK) {
259 printf("Error creating file: %s.\n", str_error(rc));
260 goto error;
261 }
262
263 fmgt_flist_destroy(flist);
264 fmgt_destroy(fmgt);
265 return 0;
266error:
267 if (flist != NULL)
268 fmgt_flist_destroy(flist);
269 if (fmgt != NULL)
270 fmgt_destroy(fmgt);
271 return 1;
272}
273
274/** @}
275 */
Note: See TracBrowser for help on using the repository browser.