source: mainline/uspace/app/bdsh/cmds/modules/cp/cp.c

Last change on this file was 87822ce, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Avoid infinite loop when console communication is broken

Need to make sure callers of console_get_event_timeout() can distinguish
between timeout and I/O error. Fix all callers of console_get_event()
and console_get_event_timeout() not to enter infinite loop when console
connection is broken. Also avoid setting of errno variable.

  • Property mode set to 100644
File size: 12.7 KB
Line 
1/*
2 * Copyright (c) 2008 Tim Post
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#include <errno.h>
30#include <str_error.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <io/console.h>
34#include <io/keycode.h>
35#include <getopt.h>
36#include <str.h>
37#include <vfs/vfs.h>
38#include <dirent.h>
39#include "config.h"
40#include "util.h"
41#include "errors.h"
42#include "entry.h"
43#include "cp.h"
44#include "cmds.h"
45
46#define CP_VERSION "0.0.1"
47#define CP_DEFAULT_BUFLEN 1024
48
49static const char *cmdname = "cp";
50static console_ctrl_t *con;
51
52static struct option const long_options[] = {
53 { "buffer", required_argument, 0, 'b' },
54 { "force", no_argument, 0, 'f' },
55 { "interactive", no_argument, 0, 'i' },
56 { "recursive", no_argument, 0, 'r' },
57 { "help", no_argument, 0, 'h' },
58 { "version", no_argument, 0, 'v' },
59 { "verbose", no_argument, 0, 'V' },
60 { 0, 0, 0, 0 }
61};
62
63typedef enum {
64 TYPE_NONE,
65 TYPE_FILE,
66 TYPE_DIR
67} dentry_type_t;
68
69static int copy_file(const char *src, const char *dest,
70 size_t blen, int vb);
71
72/** Get the type of a directory entry.
73 *
74 * @param path Path of the directory entry.
75 *
76 * @return TYPE_DIR if the dentry is a directory.
77 * @return TYPE_FILE if the dentry is a file.
78 * @return TYPE_NONE if the dentry does not exists.
79 */
80static dentry_type_t get_type(const char *path)
81{
82 vfs_stat_t s;
83
84 errno_t r = vfs_stat_path(path, &s);
85
86 if (r != EOK)
87 return TYPE_NONE;
88 else if (s.is_directory)
89 return TYPE_DIR;
90 else if (s.is_file)
91 return TYPE_FILE;
92
93 return TYPE_NONE;
94}
95
96static int strtoint(const char *s1)
97{
98 long t1;
99
100 if (-1 == (t1 = strtol(s1, (char **) NULL, 10)))
101 return -1;
102
103 if (t1 <= 0)
104 return -1;
105
106 return (int) t1;
107}
108
109/** Get the last component of a path.
110 *
111 * e.g. /data/a ---> a
112 *
113 * @param path Pointer to the path.
114 *
115 * @return Pointer to the last component or to the path itself.
116 */
117static char *get_last_path_component(char *path)
118{
119 char *ptr;
120
121 ptr = str_rchr(path, '/');
122 if (!ptr)
123 return path;
124 else
125 return ptr + 1;
126}
127
128/** Merge two paths together.
129 *
130 * e.g. (path1 = /data/dir, path2 = a/b) --> /data/dir/a/b
131 *
132 * @param path1 Path to which path2 will be appended.
133 * @param path1_size Size of the path1 buffer.
134 * @param path2 Path that will be appended to path1.
135 */
136static void merge_paths(char *path1, size_t path1_size, char *path2)
137{
138 const char *delim = "/";
139
140 str_rtrim(path1, '/');
141 str_append(path1, path1_size, delim);
142 str_append(path1, path1_size, path2);
143}
144
145static bool get_user_decision(bool bdefault, const char *message, ...)
146{
147 va_list args;
148 errno_t rc;
149
150 va_start(args, message);
151 vprintf(message, args);
152 va_end(args);
153
154 while (true) {
155 cons_event_t ev;
156 console_flush(con);
157 rc = console_get_event(con, &ev);
158 if (rc != EOK)
159 exit(1);
160 if (ev.type != CEV_KEY || ev.ev.key.type != KEY_PRESS ||
161 (ev.ev.key.mods & (KM_CTRL | KM_ALT)) != 0) {
162 continue;
163 }
164
165 switch (ev.ev.key.key) {
166 case KC_Y:
167 printf("y\n");
168 return true;
169 case KC_N:
170 printf("n\n");
171 return false;
172 case KC_ENTER:
173 printf("%c\n", bdefault ? 'Y' : 'N');
174 return bdefault;
175 default:
176 break;
177 }
178 }
179}
180
181static errno_t do_copy(const char *src, const char *dest,
182 size_t blen, int vb, int recursive, int force, int interactive)
183{
184 errno_t rc = EOK;
185 char dest_path[PATH_MAX];
186 char src_path[PATH_MAX];
187 DIR *dir = NULL;
188 struct dirent *dp;
189
190 dentry_type_t src_type = get_type(src);
191 dentry_type_t dest_type = get_type(dest);
192
193 const size_t src_len = str_size(src);
194
195 if (src_type == TYPE_FILE) {
196 char *src_fname;
197
198 /* Initialize the src_path with the src argument */
199 str_cpy(src_path, src_len + 1, src);
200 str_rtrim(src_path, '/');
201
202 /* Get the last component name from the src path */
203 src_fname = get_last_path_component(src_path);
204
205 /* Initialize dest_path with the dest argument */
206 str_cpy(dest_path, PATH_MAX, dest);
207
208 if (dest_type == TYPE_DIR) {
209 /* e.g. cp file_name /data */
210 /* e.g. cp file_name /data/ */
211
212 /*
213 * dest is a directory,
214 * append the src filename to it.
215 */
216 merge_paths(dest_path, PATH_MAX, src_fname);
217 dest_type = get_type(dest_path);
218 } else if (dest_type == TYPE_NONE) {
219 if (dest_path[str_size(dest_path) - 1] == '/') {
220 /* e.g. cp /textdemo /data/dirnotexists/ */
221
222 printf("The dest directory %s does not exists",
223 dest_path);
224 rc = ENOENT;
225 goto exit;
226 }
227 }
228
229 if (dest_type == TYPE_DIR) {
230 printf("Cannot overwrite existing directory %s\n",
231 dest_path);
232 rc = EEXIST;
233 goto exit;
234 } else if (dest_type == TYPE_FILE) {
235 /* e.g. cp file_name existing_file */
236
237 /*
238 * dest already exists,
239 * if force is set we will try to remove it.
240 * if interactive is set user input is required.
241 */
242 if (force && !interactive) {
243 rc = vfs_unlink_path(dest_path);
244 if (rc != EOK) {
245 printf("Unable to remove %s\n",
246 dest_path);
247 goto exit;
248 }
249 } else if (!force && interactive) {
250 bool overwrite = get_user_decision(false,
251 "File already exists: %s. Overwrite? [y/N]: ",
252 dest_path);
253 if (overwrite) {
254 printf("Overwriting file: %s\n", dest_path);
255 rc = vfs_unlink_path(dest_path);
256 if (rc != EOK) {
257 printf("Unable to remove %s\n", dest_path);
258 goto exit;
259 }
260 } else {
261 printf("Not overwriting file: %s\n", dest_path);
262 rc = EOK;
263 goto exit;
264 }
265 } else {
266 printf("File already exists: %s\n", dest_path);
267 rc = EEXIST;
268 goto exit;
269 }
270 }
271
272 /* call copy_file and exit */
273 if (copy_file(src, dest_path, blen, vb) < 0) {
274 rc = EIO;
275 }
276
277 } else if (src_type == TYPE_DIR) {
278 /* e.g. cp -r /x/srcdir /y/destdir/ */
279
280 if (!recursive) {
281 printf("Cannot copy the %s directory without the "
282 "-r option\n", src);
283 rc = EINVAL;
284 goto exit;
285 } else if (dest_type == TYPE_FILE) {
286 printf("Cannot overwrite a file with a directory\n");
287 rc = EEXIST;
288 goto exit;
289 }
290
291 char *src_dirname;
292
293 /* Initialize src_path with the content of src */
294 str_cpy(src_path, src_len + 1, src);
295 str_rtrim(src_path, '/');
296
297 src_dirname = get_last_path_component(src_path);
298
299 str_cpy(dest_path, PATH_MAX, dest);
300
301 switch (dest_type) {
302 case TYPE_DIR:
303 if (str_cmp(src_dirname, "..") &&
304 str_cmp(src_dirname, ".")) {
305 /*
306 * The last component of src_path is
307 * not '.' or '..'
308 */
309 merge_paths(dest_path, PATH_MAX, src_dirname);
310
311 rc = vfs_link_path(dest_path, KIND_DIRECTORY,
312 NULL);
313 if (rc != EOK) {
314 printf("Unable to create "
315 "dest directory %s\n", dest_path);
316 goto exit;
317 }
318 }
319 break;
320 default:
321 case TYPE_NONE:
322 /*
323 * dest does not exists, this means the user wants
324 * to specify the name of the destination directory
325 *
326 * e.g. cp -r /src /data/new_dir_src
327 */
328 rc = vfs_link_path(dest_path, KIND_DIRECTORY, NULL);
329 if (rc != EOK) {
330 printf("Unable to create "
331 "dest directory %s\n", dest_path);
332 goto exit;
333 }
334 break;
335 }
336
337 dir = opendir(src);
338 if (!dir) {
339 /* Something strange is happening... */
340 printf("Unable to open src %s directory\n", src);
341 rc = ENOENT;
342 goto exit;
343 }
344
345 /*
346 * Copy every single directory entry of src into the
347 * destination directory.
348 */
349 while ((dp = readdir(dir))) {
350 vfs_stat_t src_s;
351 vfs_stat_t dest_s;
352
353 char src_dent[PATH_MAX];
354 char dest_dent[PATH_MAX];
355
356 str_cpy(src_dent, PATH_MAX, src);
357 merge_paths(src_dent, PATH_MAX, dp->d_name);
358
359 str_cpy(dest_dent, PATH_MAX, dest_path);
360 merge_paths(dest_dent, PATH_MAX, dp->d_name);
361
362 /* Check if we are copying a directory into itself */
363 vfs_stat_path(src_dent, &src_s);
364 vfs_stat_path(dest_path, &dest_s);
365
366 if (dest_s.index == src_s.index &&
367 dest_s.fs_handle == src_s.fs_handle) {
368 printf("Cannot copy a directory "
369 "into itself\n");
370 rc = EEXIST;
371 goto exit;
372 }
373
374 if (vb)
375 printf("copy %s %s\n", src_dent, dest_dent);
376
377 /* Recursively call do_copy() */
378 rc = do_copy(src_dent, dest_dent, blen, vb, recursive,
379 force, interactive);
380 if (rc != EOK)
381 goto exit;
382
383 }
384 } else
385 printf("Unable to open source file %s\n", src);
386
387exit:
388 if (dir)
389 closedir(dir);
390 return rc;
391}
392
393static int copy_file(const char *src, const char *dest,
394 size_t blen, int vb)
395{
396 int fd1, fd2;
397 size_t rbytes, wbytes;
398 errno_t rc;
399 off64_t total;
400 char *buff = NULL;
401 aoff64_t posr = 0, posw = 0;
402 vfs_stat_t st;
403
404 if (vb)
405 printf("Copying %s to %s\n", src, dest);
406
407 rc = vfs_lookup_open(src, WALK_REGULAR, MODE_READ, &fd1);
408 if (rc != EOK) {
409 printf("Unable to open source file %s\n", src);
410 return -1;
411 }
412
413 rc = vfs_lookup_open(dest, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE, &fd2);
414 if (rc != EOK) {
415 printf("Unable to open destination file %s\n", dest);
416 vfs_put(fd1);
417 return -1;
418 }
419
420 if (vfs_stat(fd1, &st) != EOK) {
421 printf("Unable to fstat %d\n", fd1);
422 vfs_put(fd1);
423 vfs_put(fd2);
424 return -1;
425 }
426
427 total = st.size;
428 if (vb)
429 printf("%" PRIu64 " bytes to copy\n", total);
430
431 if (NULL == (buff = (char *) malloc(blen))) {
432 printf("Unable to allocate enough memory to read %s\n",
433 src);
434 rc = ENOMEM;
435 goto out;
436 }
437
438 while ((rc = vfs_read(fd1, &posr, buff, blen, &rbytes)) == EOK &&
439 rbytes > 0) {
440 if ((rc = vfs_write(fd2, &posw, buff, rbytes, &wbytes)) != EOK)
441 break;
442 }
443
444 if (rc != EOK) {
445 printf("\nError copying %s: %s\n", src, str_error(rc));
446 return -1;
447 }
448
449out:
450 vfs_put(fd1);
451 vfs_put(fd2);
452 if (buff)
453 free(buff);
454 if (rc != EOK) {
455 return -1;
456 } else {
457 return 0;
458 }
459}
460
461void help_cmd_cp(unsigned int level)
462{
463 static char helpfmt[] =
464 "Usage: %s [options] <source> <dest>\n"
465 "Options:\n"
466 " -h, --help A short option summary\n"
467 " -v, --version Print version information and exit\n"
468 " -V, --verbose Be annoyingly noisy about what's being done\n"
469 " -f, --force Do not complain when <dest> exists (overrides a previous -i)\n"
470 " -i, --interactive Ask what to do when <dest> exists (overrides a previous -f)\n"
471 " -r, --recursive Copy entire directories\n"
472 " -b, --buffer ## Set the read buffer size to ##\n";
473 if (level == HELP_SHORT) {
474 printf("`%s' copies files and directories\n", cmdname);
475 } else {
476 help_cmd_cp(HELP_SHORT);
477 printf(helpfmt, cmdname);
478 }
479
480 return;
481}
482
483int cmd_cp(char **argv)
484{
485 unsigned int argc, verbose = 0;
486 int buffer = 0, recursive = 0;
487 int force = 0, interactive = 0;
488 int c, opt_ind;
489 errno_t ret;
490
491 con = console_init(stdin, stdout);
492 argc = cli_count_args(argv);
493
494 c = 0;
495 optreset = 1;
496 optind = 0;
497 opt_ind = 0;
498
499 while (c != -1) {
500 c = getopt_long(argc, argv, "hvVfirb:", long_options, &opt_ind);
501 switch (c) {
502 case 'h':
503 help_cmd_cp(1);
504 return CMD_SUCCESS;
505 case 'v':
506 printf("%s\n", CP_VERSION);
507 return CMD_SUCCESS;
508 case 'V':
509 verbose = 1;
510 break;
511 case 'f':
512 interactive = 0;
513 force = 1;
514 break;
515 case 'i':
516 force = 0;
517 interactive = 1;
518 break;
519 case 'r':
520 recursive = 1;
521 break;
522 case 'b':
523 if (-1 == (buffer = strtoint(optarg))) {
524 printf("%s: Invalid buffer specification, "
525 "(should be a number greater than zero)\n",
526 cmdname);
527 console_done(con);
528 return CMD_FAILURE;
529 }
530 if (verbose)
531 printf("Buffer = %d\n", buffer);
532 break;
533 }
534 }
535
536 if (buffer == 0)
537 buffer = CP_DEFAULT_BUFLEN;
538
539 argc -= optind;
540
541 if (argc != 2) {
542 printf("%s: invalid number of arguments. Try %s --help\n",
543 cmdname, cmdname);
544 console_done(con);
545 return CMD_FAILURE;
546 }
547
548 ret = do_copy(argv[optind], argv[optind + 1], buffer, verbose,
549 recursive, force, interactive);
550
551 console_done(con);
552
553 if (ret == 0)
554 return CMD_SUCCESS;
555 else
556 return CMD_FAILURE;
557}
Note: See TracBrowser for help on using the repository browser.