1 | /*
|
---|
2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <errno.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <dirent.h>
|
---|
33 | #include <sys/types.h>
|
---|
34 | #include <macros.h>
|
---|
35 | #include <getopt.h>
|
---|
36 | #include <stdarg.h>
|
---|
37 | #include <str.h>
|
---|
38 | #include <ctype.h>
|
---|
39 | #include <vfs/vfs.h>
|
---|
40 |
|
---|
41 | #include "config.h"
|
---|
42 | #include "errors.h"
|
---|
43 | #include "util.h"
|
---|
44 | #include "entry.h"
|
---|
45 | #include "mkfile.h"
|
---|
46 | #include "cmds.h"
|
---|
47 |
|
---|
48 | /** Number of bytes to write at a time */
|
---|
49 | #define BUFFER_SIZE 16384
|
---|
50 |
|
---|
51 | static const char *cmdname = "mkfile";
|
---|
52 |
|
---|
53 | static struct option const long_options[] = {
|
---|
54 | {"size", required_argument, 0, 's'},
|
---|
55 | {"sparse", no_argument, 0, 'p'},
|
---|
56 | {"help", no_argument, 0, 'h'},
|
---|
57 | {0, 0, 0, 0}
|
---|
58 | };
|
---|
59 |
|
---|
60 | void help_cmd_mkfile(unsigned int level)
|
---|
61 | {
|
---|
62 | if (level == HELP_SHORT) {
|
---|
63 | printf("`%s' creates a new zero-filled file\n", cmdname);
|
---|
64 | } else {
|
---|
65 | help_cmd_mkfile(HELP_SHORT);
|
---|
66 | printf(
|
---|
67 | "Usage: %s [options] <path>\n"
|
---|
68 | "Options:\n"
|
---|
69 | " -h, --help A short option summary\n"
|
---|
70 | " -s, --size sz Size of the file\n"
|
---|
71 | " -p, --sparse Create a sparse file\n"
|
---|
72 | "\n"
|
---|
73 | "Size is a number followed by 'k', 'm' or 'g' for kB, MB, GB.\n"
|
---|
74 | "E.g. 100k, 2m, 1g.\n",
|
---|
75 | cmdname);
|
---|
76 | }
|
---|
77 |
|
---|
78 | return;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Parse size specification.
|
---|
82 | *
|
---|
83 | * Size specification is in the form <decimal_number><unit> where
|
---|
84 | * <unit> is 'k', 'm' or 'g' for kB, MB, GB.
|
---|
85 | *
|
---|
86 | * @param str String containing the size specification.
|
---|
87 | * @return Non-negative size in bytes on success, -1 on failure.
|
---|
88 | */
|
---|
89 | static ssize_t read_size(const char *str)
|
---|
90 | {
|
---|
91 | ssize_t number, unit;
|
---|
92 | char *ep;
|
---|
93 |
|
---|
94 | number = strtol(str, &ep, 10);
|
---|
95 | if (ep[0] == '\0')
|
---|
96 | return number;
|
---|
97 |
|
---|
98 | if (ep[1] != '\0')
|
---|
99 | return -1;
|
---|
100 |
|
---|
101 | switch (tolower(ep[0])) {
|
---|
102 | case 'k': unit = 1024; break;
|
---|
103 | case 'm': unit = 1024*1024; break;
|
---|
104 | case 'g': unit = 1024*1024*1024; break;
|
---|
105 | default: return -1;
|
---|
106 | }
|
---|
107 |
|
---|
108 | return number * unit;
|
---|
109 | }
|
---|
110 |
|
---|
111 | int cmd_mkfile(char **argv)
|
---|
112 | {
|
---|
113 | unsigned int argc;
|
---|
114 | int c, opt_ind;
|
---|
115 | int fd;
|
---|
116 | ssize_t file_size;
|
---|
117 | ssize_t total_written;
|
---|
118 | ssize_t to_write, rc, rc2 = 0;
|
---|
119 | char *file_name;
|
---|
120 | void *buffer;
|
---|
121 | bool create_sparse = false;
|
---|
122 | aoff64_t pos = 0;
|
---|
123 |
|
---|
124 | file_size = 0;
|
---|
125 |
|
---|
126 | argc = cli_count_args(argv);
|
---|
127 |
|
---|
128 | for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
|
---|
129 | c = getopt_long(argc, argv, "ps:h", long_options, &opt_ind);
|
---|
130 | switch (c) {
|
---|
131 | case 'h':
|
---|
132 | help_cmd_mkfile(HELP_LONG);
|
---|
133 | return CMD_SUCCESS;
|
---|
134 | case 'p':
|
---|
135 | create_sparse = true;
|
---|
136 | break;
|
---|
137 | case 's':
|
---|
138 | file_size = read_size(optarg);
|
---|
139 | if (file_size < 0) {
|
---|
140 | printf("%s: Invalid file size specification.\n",
|
---|
141 | cmdname);
|
---|
142 | return CMD_FAILURE;
|
---|
143 | }
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | argc -= optind;
|
---|
149 |
|
---|
150 | if (argc != 1) {
|
---|
151 | printf("%s: incorrect number of arguments. Try `%s --help'\n",
|
---|
152 | cmdname, cmdname);
|
---|
153 | return CMD_FAILURE;
|
---|
154 | }
|
---|
155 |
|
---|
156 | file_name = argv[optind];
|
---|
157 |
|
---|
158 | fd = vfs_lookup_open(file_name, WALK_REGULAR | WALK_MUST_CREATE, MODE_WRITE);
|
---|
159 | if (fd < 0) {
|
---|
160 | printf("%s: failed to create file %s.\n", cmdname, file_name);
|
---|
161 | return CMD_FAILURE;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (create_sparse && file_size > 0) {
|
---|
165 | const char byte = 0x00;
|
---|
166 |
|
---|
167 | pos = file_size - 1;
|
---|
168 | rc2 = write(fd, &pos, &byte, sizeof(char));
|
---|
169 | if (rc2 < 0) {
|
---|
170 | vfs_put(fd);
|
---|
171 | goto error;
|
---|
172 | }
|
---|
173 | return CMD_SUCCESS;
|
---|
174 | }
|
---|
175 |
|
---|
176 | buffer = calloc(BUFFER_SIZE, 1);
|
---|
177 | if (buffer == NULL) {
|
---|
178 | printf("%s: Error, out of memory.\n", cmdname);
|
---|
179 | return CMD_FAILURE;
|
---|
180 | }
|
---|
181 |
|
---|
182 | total_written = 0;
|
---|
183 | while (total_written < file_size) {
|
---|
184 | to_write = min(file_size - total_written, BUFFER_SIZE);
|
---|
185 | rc = write(fd, &pos, buffer, to_write);
|
---|
186 | if (rc <= 0) {
|
---|
187 | printf("%s: Error writing file (%d).\n", cmdname, errno);
|
---|
188 | vfs_put(fd);
|
---|
189 | free(buffer);
|
---|
190 | return CMD_FAILURE;
|
---|
191 | }
|
---|
192 | total_written += rc;
|
---|
193 | }
|
---|
194 |
|
---|
195 | free(buffer);
|
---|
196 |
|
---|
197 | if (vfs_put(fd) < 0)
|
---|
198 | goto error;
|
---|
199 |
|
---|
200 | return CMD_SUCCESS;
|
---|
201 | error:
|
---|
202 | printf("%s: Error writing file (%d).\n", cmdname, errno);
|
---|
203 | return CMD_FAILURE;
|
---|
204 | }
|
---|