Subversion Repositories HelenOS

Rev

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

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