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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02c6dcc was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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