source: mainline/uspace/app/copy/copy.c@ 81805e0

Last change on this file since 81805e0 was 81805e0, checked in by Jiri Svoboda <jiri@…>, 3 months ago

Display correct error message in Navigator, too.

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