source: mainline/uspace/app/bdsh/cmds/modules/mkfile/mkfile.c@ 948222e4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 948222e4 was 948222e4, checked in by Jiri Svoboda <jiri@…>, 8 years ago

A few more cases of for loops without iteration expression (which isn't really a for loop).

  • Property mode set to 100644
File size: 5.2 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 <stdint.h>
33#include <str_error.h>
34#include <dirent.h>
35#include <macros.h>
36#include <getopt.h>
37#include <stdarg.h>
38#include <str.h>
39#include <ctype.h>
40#include <vfs/vfs.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 * @param rsize Place to store size in bytes
89 * @return EOK on success or an error code
90 */
91static errno_t read_size(const char *str, size_t *rsize)
92{
93 size_t number, unit;
94 char *ep;
95
96 number = strtol(str, &ep, 10);
97 if (ep[0] == '\0') {
98 *rsize = number;
99 return EOK;
100 }
101
102 if (ep[1] != '\0')
103 return EINVAL;
104
105 switch (tolower(ep[0])) {
106 case 'k': unit = 1024; break;
107 case 'm': unit = 1024*1024; break;
108 case 'g': unit = 1024*1024*1024; break;
109 default: return EINVAL;
110 }
111
112 *rsize = number * unit;
113 return EOK;
114}
115
116int cmd_mkfile(char **argv)
117{
118 unsigned int argc;
119 int c, opt_ind;
120 int fd;
121 size_t file_size;
122 size_t total_written;
123 size_t to_write;
124 size_t nwritten;
125 errno_t rc;
126 char *file_name;
127 void *buffer;
128 bool create_sparse = false;
129 aoff64_t pos = 0;
130
131 file_size = 0;
132
133 argc = cli_count_args(argv);
134
135 c = 0;
136 optreset = 1;
137 optind = 0;
138 opt_ind = 0;
139
140 while (c != -1) {
141 c = getopt_long(argc, argv, "ps:h", long_options, &opt_ind);
142 switch (c) {
143 case 'h':
144 help_cmd_mkfile(HELP_LONG);
145 return CMD_SUCCESS;
146 case 'p':
147 create_sparse = true;
148 break;
149 case 's':
150 rc = read_size(optarg, &file_size);
151 if (rc != EOK) {
152 printf("%s: Invalid file size specification.\n",
153 cmdname);
154 return CMD_FAILURE;
155 }
156 break;
157 }
158 }
159
160 argc -= optind;
161
162 if (argc != 1) {
163 printf("%s: incorrect number of arguments. Try `%s --help'\n",
164 cmdname, cmdname);
165 return CMD_FAILURE;
166 }
167
168 file_name = argv[optind];
169
170 rc = vfs_lookup_open(file_name, WALK_REGULAR | WALK_MUST_CREATE, MODE_WRITE, &fd);
171 if (rc != EOK) {
172 printf("%s: failed to create file %s.\n", cmdname, file_name);
173 return CMD_FAILURE;
174 }
175
176 if (create_sparse && file_size > 0) {
177 const char byte = 0x00;
178
179 pos = file_size - 1;
180 rc = vfs_write(fd, &pos, &byte, sizeof(char), &nwritten);
181 if (rc != EOK) {
182 vfs_put(fd);
183 goto error;
184 }
185 return CMD_SUCCESS;
186 }
187
188 buffer = calloc(BUFFER_SIZE, 1);
189 if (buffer == NULL) {
190 printf("%s: Error, out of memory.\n", cmdname);
191 return CMD_FAILURE;
192 }
193
194 total_written = 0;
195 while (total_written < file_size) {
196 to_write = min(file_size - total_written, BUFFER_SIZE);
197 rc = vfs_write(fd, &pos, buffer, to_write, &nwritten);
198 if (rc != EOK) {
199 printf("%s: Error writing file (%s).\n", cmdname, str_error(rc));
200 vfs_put(fd);
201 free(buffer);
202 return CMD_FAILURE;
203 }
204 total_written += nwritten;
205 }
206
207 free(buffer);
208
209 rc = vfs_put(fd);
210 if (rc != EOK)
211 goto error;
212
213 return CMD_SUCCESS;
214error:
215 printf("%s: Error writing file (%s).\n", cmdname, str_error(rc));
216 return CMD_FAILURE;
217}
Note: See TracBrowser for help on using the repository browser.