Subversion Repositories HelenOS

Rev

Rev 3651 | Rev 3816 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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