Subversion Repositories HelenOS

Rev

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

Rev 3815 Rev 3816
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 "config.h"
37
#include "config.h"
38
#include "util.h"
38
#include "util.h"
39
#include "errors.h"
39
#include "errors.h"
40
#include "entry.h"
40
#include "entry.h"
41
#include "cp.h"
41
#include "cp.h"
42
#include "cmds.h"
42
#include "cmds.h"
43
 
43
 
44
#define CP_VERSION "0.0.1"
44
#define CP_VERSION "0.0.1"
45
#define CP_DEFAULT_BUFLEN  1024
45
#define CP_DEFAULT_BUFLEN  1024
46
 
46
 
47
static const char *cmdname = "cp";
47
static const char *cmdname = "cp";
48
 
48
 
49
static struct option const long_options[] = {
49
static struct option const long_options[] = {
50
    { "buffer", required_argument, 0, 'b' },
50
    { "buffer", required_argument, 0, 'b' },
51
    { "force", no_argument, 0, 'f' },
51
    { "force", no_argument, 0, 'f' },
52
    { "recursive", no_argument, 0, 'r' },
52
    { "recursive", no_argument, 0, 'r' },
53
    { "help", no_argument, 0, 'h' },
53
    { "help", no_argument, 0, 'h' },
54
    { "version", no_argument, 0, 'v' },
54
    { "version", no_argument, 0, 'v' },
55
    { "verbose", no_argument, 0, 'V' },
55
    { "verbose", no_argument, 0, 'V' },
56
    { 0, 0, 0, 0 }
56
    { 0, 0, 0, 0 }
57
};
57
};
58
 
58
 
59
static int strtoint(const char *s1)
59
static int strtoint(const char *s1)
60
{
60
{
61
    long t1;
61
    long t1;
62
 
62
 
63
    if (-1 == (t1 = strtol(s1, (char **) NULL, 10)))
63
    if (-1 == (t1 = strtol(s1, (char **) NULL, 10)))
64
        return -1;
64
        return -1;
65
 
65
 
66
    if (t1 <= 0)
66
    if (t1 <= 0)
67
        return -1;
67
        return -1;
68
 
68
 
69
    return (int) t1;
69
    return (int) t1;
70
}
70
}
71
 
71
 
72
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,
-
 
73
    size_t blen, int vb)
73
{
74
{
74
    int fd1, fd2, bytes = 0;
75
    int fd1, fd2, bytes = 0;
75
    off_t total = 0;
76
    off_t total = 0;
76
    int64_t copied = 0;
77
    int64_t copied = 0;
77
    char *buff = NULL;
78
    char *buff = NULL;
78
 
79
 
79
    if (vb)
80
    if (vb)
80
        printf("Copying %s to %s\n", src, dest);
81
        printf("Copying %s to %s\n", src, dest);
81
 
82
 
82
    if (-1 == (fd1 = open(src, O_RDONLY))) {
83
    if (-1 == (fd1 = open(src, O_RDONLY))) {
83
        printf("Unable to open source file %s\n", src);
84
        printf("Unable to open source file %s\n", src);
84
        return -1;
85
        return -1;
85
    }
86
    }
86
 
87
 
87
    if (-1 == (fd2 = open(dest, O_CREAT))) {
88
    if (-1 == (fd2 = open(dest, O_CREAT))) {
88
        printf("Unable to open destination file %s\n", dest);
89
        printf("Unable to open destination file %s\n", dest);
89
        close(fd1);
90
        close(fd1);
90
        return -1;
91
        return -1;
91
    }
92
    }
92
 
93
 
93
    total = lseek(fd1, 0, SEEK_END);
94
    total = lseek(fd1, 0, SEEK_END);
94
 
95
 
95
    if (vb)
96
    if (vb)
96
        printf("%d bytes to copy\n", total);
97
        printf("%d bytes to copy\n", total);
97
 
98
 
98
    lseek(fd1, 0, SEEK_SET);
99
    lseek(fd1, 0, SEEK_SET);
99
 
100
 
100
    if (NULL == (buff = (char *) malloc(blen))) {
101
    if (NULL == (buff = (char *) malloc(blen))) {
101
        printf("Unable to allocate enough memory to read %s\n",
102
        printf("Unable to allocate enough memory to read %s\n",
102
            src);
103
            src);
103
        copied = -1;
104
        copied = -1;
104
        goto out;
105
        goto out;
105
    }
106
    }
106
 
107
 
107
    for (;;) {
108
    for (;;) {
108
        ssize_t res;
109
        ssize_t res;
109
 
110
 
110
        bytes = read(fd1, buff, blen);
111
        bytes = read(fd1, buff, blen);
111
        if (bytes <= 0)
112
        if (bytes <= 0)
112
            break;
113
            break;
113
        copied += bytes;
114
        copied += bytes;
114
        res = bytes;
115
        res = bytes;
115
        do {
116
        do {
116
            /*
117
            /*
117
             * Theoretically, it may not be enough to call write()
118
             * Theoretically, it may not be enough to call write()
118
             * only once. Also the previous read() may have
119
             * only once. Also the previous read() may have
119
             * returned less data than requested.
120
             * returned less data than requested.
120
             */
121
             */
121
            bytes = write(fd2, buff, res);
122
            bytes = write(fd2, buff, res);
122
            if (bytes < 0)
123
            if (bytes < 0)
123
                goto err;
124
                goto err;
124
            res -= bytes;
125
            res -= bytes;
125
        } while (res > 0);
126
        } while (res > 0);
126
 
127
 
127
        /* TODO: re-insert assert() once this is stand alone,
128
        /* TODO: re-insert assert() once this is stand alone,
128
         * removed as abort() exits the entire shell
129
         * removed as abort() exits the entire shell
129
         */
130
         */
130
        if (res != 0) {
131
        if (res != 0) {
131
            printf("\n%d more bytes than actually exist were copied\n", res);
132
            printf("\n%d more bytes than actually exist were copied\n", res);
132
            goto err;
133
            goto err;
133
        }
134
        }
134
    }
135
    }
135
 
136
 
136
    if (bytes < 0) {
137
    if (bytes < 0) {
137
err:
138
err:
138
        printf("\nError copying %s, (%d)\n", src, bytes);
139
        printf("\nError copying %s, (%d)\n", src, bytes);
139
        copied = bytes;
140
        copied = bytes;
140
    }
141
    }
141
 
142
 
142
out:
143
out:
143
    close(fd1);
144
    close(fd1);
144
    close(fd2);
145
    close(fd2);
145
    if (buff)
146
    if (buff)
146
        free(buff);
147
        free(buff);
147
    return copied;
148
    return copied;
148
}
149
}
149
 
150
 
150
void help_cmd_cp(unsigned int level)
151
void help_cmd_cp(unsigned int level)
151
{
152
{
152
    static char helpfmt[] =
153
    static char helpfmt[] =
153
        "Usage:  %s [options] <source> <dest>\n"
154
        "Usage:  %s [options] <source> <dest>\n"
154
        "Options: (* indicates not yet implemented)\n"
155
        "Options: (* indicates not yet implemented)\n"
155
        "  -h, --help       A short option summary\n"
156
        "  -h, --help       A short option summary\n"
156
        "  -v, --version    Print version information and exit\n"
157
        "  -v, --version    Print version information and exit\n"
157
        "* -V, --verbose    Be annoyingly noisy about what's being done\n"
158
        "* -V, --verbose    Be annoyingly noisy about what's being done\n"
158
        "* -f, --force      Do not complain when <dest> exists\n"
159
        "* -f, --force      Do not complain when <dest> exists\n"
159
        "* -r, --recursive  Copy entire directories\n"
160
        "* -r, --recursive  Copy entire directories\n"
160
        "  -b, --buffer ## Set the read buffer size to ##\n"
161
        "  -b, --buffer ## Set the read buffer size to ##\n"
161
        "Currently, %s is under development, some options may not work.\n";
162
        "Currently, %s is under development, some options may not work.\n";
162
    if (level == HELP_SHORT) {
163
    if (level == HELP_SHORT) {
163
        printf("`%s' copies files and directories\n", cmdname);
164
        printf("`%s' copies files and directories\n", cmdname);
164
    } else {
165
    } else {
165
        help_cmd_cp(HELP_SHORT);
166
        help_cmd_cp(HELP_SHORT);
166
        printf(helpfmt, cmdname, cmdname);
167
        printf(helpfmt, cmdname, cmdname);
167
    }
168
    }
168
 
169
 
169
    return;
170
    return;
170
}
171
}
171
 
172
 
172
int cmd_cp(char **argv)
173
int cmd_cp(char **argv)
173
{
174
{
174
    unsigned int argc, buffer = 0, verbose = 0;
175
    unsigned int argc, buffer = 0, verbose = 0;
175
    int c, opt_ind;
176
    int c, opt_ind;
176
    int64_t ret;
177
    int64_t ret;
177
 
178
 
178
    argc = cli_count_args(argv);
179
    argc = cli_count_args(argv);
179
 
180
 
180
    for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
181
    for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
181
        c = getopt_long(argc, argv, "hvVfrb:", long_options, &opt_ind);
182
        c = getopt_long(argc, argv, "hvVfrb:", long_options, &opt_ind);
182
        switch (c) {
183
        switch (c) {
183
        case 'h':
184
        case 'h':
184
            help_cmd_cp(1);
185
            help_cmd_cp(1);
185
            return CMD_SUCCESS;
186
            return CMD_SUCCESS;
186
        case 'v':
187
        case 'v':
187
            printf("%d\n", CP_VERSION);
188
            printf("%d\n", CP_VERSION);
188
            return CMD_SUCCESS;
189
            return CMD_SUCCESS;
189
        case 'V':
190
        case 'V':
190
            verbose = 1;
191
            verbose = 1;
191
            break;
192
            break;
192
        case 'f':
193
        case 'f':
193
            break;
194
            break;
194
        case 'r':
195
        case 'r':
195
            break;
196
            break;
196
        case 'b':
197
        case 'b':
197
            if (-1 == (buffer = strtoint(optarg))) {
198
            if (-1 == (buffer = strtoint(optarg))) {
198
                printf("%s: Invalid buffer specification, "
199
                printf("%s: Invalid buffer specification, "
199
                    "(should be a number greater than zero)\n",
200
                    "(should be a number greater than zero)\n",
200
                    cmdname);
201
                    cmdname);
201
                return CMD_FAILURE;
202
                return CMD_FAILURE;
202
            }
203
            }
203
            if (verbose)
204
            if (verbose)
204
                printf("Buffer = %d\n", buffer);
205
                printf("Buffer = %d\n", buffer);
205
            break;
206
            break;
206
        }
207
        }
207
    }
208
    }
208
 
209
 
209
    if (buffer == 0)
210
    if (buffer == 0)
210
        buffer = CP_DEFAULT_BUFLEN;
211
        buffer = CP_DEFAULT_BUFLEN;
211
 
212
 
212
    argc -= optind;
213
    argc -= optind;
213
 
214
 
214
    if (argc != 2) {
215
    if (argc != 2) {
215
        printf("%s: invalid number of arguments. Try %s --help\n",
216
        printf("%s: invalid number of arguments. Try %s --help\n",
216
            cmdname, cmdname);
217
            cmdname, cmdname);
217
        return CMD_FAILURE;
218
        return CMD_FAILURE;
218
    }
219
    }
219
 
220
 
220
    ret = copy_file(argv[optind], argv[optind + 1], buffer, verbose);
221
    ret = copy_file(argv[optind], argv[optind + 1], buffer, verbose);
221
 
222
 
222
    if (verbose)
223
    if (verbose)
223
        printf("%d bytes copied\n", ret);
224
        printf("%d bytes copied\n", ret);
224
 
225
 
225
    if (ret >= 0)
226
    if (ret >= 0)
226
        return CMD_SUCCESS;
227
        return CMD_SUCCESS;
227
    else
228
    else
228
        return CMD_FAILURE;
229
        return CMD_FAILURE;
229
}
230
}
230
 
231
 
231
 
232