1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
|
---|
2 | * All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions are met:
|
---|
6 | *
|
---|
7 | * Redistributions of source code must retain the above copyright notice, this
|
---|
8 | * list of conditions and the following disclaimer.
|
---|
9 | *
|
---|
10 | * Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | * this list of conditions and the following disclaimer in the documentation
|
---|
12 | * and/or other materials provided with the distribution.
|
---|
13 | *
|
---|
14 | * Neither the name of the original program's authors nor the names of its
|
---|
15 | * contributors may be used to endorse or promote products derived from this
|
---|
16 | * software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
---|
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
28 | * POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <unistd.h>
|
---|
34 | #include <getopt.h>
|
---|
35 | #include <str.h>
|
---|
36 | #include <fcntl.h>
|
---|
37 | #include "config.h"
|
---|
38 | #include "util.h"
|
---|
39 | #include "errors.h"
|
---|
40 | #include "entry.h"
|
---|
41 | #include "cp.h"
|
---|
42 | #include "cmds.h"
|
---|
43 |
|
---|
44 | #define CP_VERSION "0.0.1"
|
---|
45 | #define CP_DEFAULT_BUFLEN 1024
|
---|
46 |
|
---|
47 | static const char *cmdname = "cp";
|
---|
48 |
|
---|
49 | static struct option const long_options[] = {
|
---|
50 | { "buffer", required_argument, 0, 'b' },
|
---|
51 | { "force", no_argument, 0, 'f' },
|
---|
52 | { "recursive", no_argument, 0, 'r' },
|
---|
53 | { "help", no_argument, 0, 'h' },
|
---|
54 | { "version", no_argument, 0, 'v' },
|
---|
55 | { "verbose", no_argument, 0, 'V' },
|
---|
56 | { 0, 0, 0, 0 }
|
---|
57 | };
|
---|
58 |
|
---|
59 | static int strtoint(const char *s1)
|
---|
60 | {
|
---|
61 | long t1;
|
---|
62 |
|
---|
63 | if (-1 == (t1 = strtol(s1, (char **) NULL, 10)))
|
---|
64 | return -1;
|
---|
65 |
|
---|
66 | if (t1 <= 0)
|
---|
67 | return -1;
|
---|
68 |
|
---|
69 | return (int) t1;
|
---|
70 | }
|
---|
71 |
|
---|
72 | static int64_t copy_file(const char *src, const char *dest,
|
---|
73 | size_t blen, int vb)
|
---|
74 | {
|
---|
75 | int fd1, fd2, bytes = 0;
|
---|
76 | off64_t total = 0;
|
---|
77 | int64_t copied = 0;
|
---|
78 | char *buff = NULL;
|
---|
79 |
|
---|
80 | if (vb)
|
---|
81 | printf("Copying %s to %s\n", src, dest);
|
---|
82 |
|
---|
83 | if (-1 == (fd1 = open(src, O_RDONLY))) {
|
---|
84 | printf("Unable to open source file %s\n", src);
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (-1 == (fd2 = open(dest, O_CREAT))) {
|
---|
89 | printf("Unable to open destination file %s\n", dest);
|
---|
90 | close(fd1);
|
---|
91 | return -1;
|
---|
92 | }
|
---|
93 |
|
---|
94 | total = lseek(fd1, 0, SEEK_END);
|
---|
95 |
|
---|
96 | if (vb)
|
---|
97 | printf("%" PRIu64 " bytes to copy\n", total);
|
---|
98 |
|
---|
99 | lseek(fd1, 0, SEEK_SET);
|
---|
100 |
|
---|
101 | if (NULL == (buff = (char *) malloc(blen))) {
|
---|
102 | printf("Unable to allocate enough memory to read %s\n",
|
---|
103 | src);
|
---|
104 | copied = -1;
|
---|
105 | goto out;
|
---|
106 | }
|
---|
107 |
|
---|
108 | for (;;) {
|
---|
109 | ssize_t res;
|
---|
110 | size_t written = 0;
|
---|
111 |
|
---|
112 | bytes = read(fd1, buff, blen);
|
---|
113 | if (bytes <= 0)
|
---|
114 | break;
|
---|
115 | copied += bytes;
|
---|
116 | res = bytes;
|
---|
117 | do {
|
---|
118 | /*
|
---|
119 | * Theoretically, it may not be enough to call write()
|
---|
120 | * only once. Also the previous read() may have
|
---|
121 | * returned less data than requested.
|
---|
122 | */
|
---|
123 | bytes = write(fd2, buff + written, res);
|
---|
124 | if (bytes < 0)
|
---|
125 | goto err;
|
---|
126 | written += bytes;
|
---|
127 | res -= bytes;
|
---|
128 | } while (res > 0);
|
---|
129 |
|
---|
130 | /* TODO: re-insert assert() once this is stand alone,
|
---|
131 | * removed as abort() exits the entire shell
|
---|
132 | */
|
---|
133 | if (res != 0) {
|
---|
134 | printf("\n%zd more bytes than actually exist were copied\n", res);
|
---|
135 | goto err;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (bytes < 0) {
|
---|
140 | err:
|
---|
141 | printf("\nError copying %s, (%d)\n", src, bytes);
|
---|
142 | copied = bytes;
|
---|
143 | }
|
---|
144 |
|
---|
145 | out:
|
---|
146 | close(fd1);
|
---|
147 | close(fd2);
|
---|
148 | if (buff)
|
---|
149 | free(buff);
|
---|
150 | return copied;
|
---|
151 | }
|
---|
152 |
|
---|
153 | void help_cmd_cp(unsigned int level)
|
---|
154 | {
|
---|
155 | static char helpfmt[] =
|
---|
156 | "Usage: %s [options] <source> <dest>\n"
|
---|
157 | "Options: (* indicates not yet implemented)\n"
|
---|
158 | " -h, --help A short option summary\n"
|
---|
159 | " -v, --version Print version information and exit\n"
|
---|
160 | "* -V, --verbose Be annoyingly noisy about what's being done\n"
|
---|
161 | "* -f, --force Do not complain when <dest> exists\n"
|
---|
162 | "* -r, --recursive Copy entire directories\n"
|
---|
163 | " -b, --buffer ## Set the read buffer size to ##\n"
|
---|
164 | "Currently, %s is under development, some options may not work.\n";
|
---|
165 | if (level == HELP_SHORT) {
|
---|
166 | printf("`%s' copies files and directories\n", cmdname);
|
---|
167 | } else {
|
---|
168 | help_cmd_cp(HELP_SHORT);
|
---|
169 | printf(helpfmt, cmdname, cmdname);
|
---|
170 | }
|
---|
171 |
|
---|
172 | return;
|
---|
173 | }
|
---|
174 |
|
---|
175 | int cmd_cp(char **argv)
|
---|
176 | {
|
---|
177 | unsigned int argc, verbose = 0;
|
---|
178 | int buffer = 0;
|
---|
179 | int c, opt_ind;
|
---|
180 | int64_t ret;
|
---|
181 |
|
---|
182 | argc = cli_count_args(argv);
|
---|
183 |
|
---|
184 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
---|
185 | c = getopt_long(argc, argv, "hvVfrb:", long_options, &opt_ind);
|
---|
186 | switch (c) {
|
---|
187 | case 'h':
|
---|
188 | help_cmd_cp(1);
|
---|
189 | return CMD_SUCCESS;
|
---|
190 | case 'v':
|
---|
191 | printf("%s\n", CP_VERSION);
|
---|
192 | return CMD_SUCCESS;
|
---|
193 | case 'V':
|
---|
194 | verbose = 1;
|
---|
195 | break;
|
---|
196 | case 'f':
|
---|
197 | break;
|
---|
198 | case 'r':
|
---|
199 | break;
|
---|
200 | case 'b':
|
---|
201 | if (-1 == (buffer = strtoint(optarg))) {
|
---|
202 | printf("%s: Invalid buffer specification, "
|
---|
203 | "(should be a number greater than zero)\n",
|
---|
204 | cmdname);
|
---|
205 | return CMD_FAILURE;
|
---|
206 | }
|
---|
207 | if (verbose)
|
---|
208 | printf("Buffer = %d\n", buffer);
|
---|
209 | break;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (buffer == 0)
|
---|
214 | buffer = CP_DEFAULT_BUFLEN;
|
---|
215 |
|
---|
216 | argc -= optind;
|
---|
217 |
|
---|
218 | if (argc != 2) {
|
---|
219 | printf("%s: invalid number of arguments. Try %s --help\n",
|
---|
220 | cmdname, cmdname);
|
---|
221 | return CMD_FAILURE;
|
---|
222 | }
|
---|
223 |
|
---|
224 | ret = copy_file(argv[optind], argv[optind + 1], buffer, verbose);
|
---|
225 |
|
---|
226 | if (verbose)
|
---|
227 | printf("%" PRId64 " bytes copied\n", ret);
|
---|
228 |
|
---|
229 | if (ret >= 0)
|
---|
230 | return CMD_SUCCESS;
|
---|
231 | else
|
---|
232 | return CMD_FAILURE;
|
---|
233 | }
|
---|
234 |
|
---|