source: mainline/uspace/app/bdsh/cmds/modules/mkfile/mkfile.c@ 23a0368

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 23a0368 was 58898d1d, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove VFS_IN_SEEK from VFS

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