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

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

UNIX-like I/O functions should use errno to return error code for many reasons.

  • 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 <fcntl.h>
37#include <macros.h>
38#include <getopt.h>
39#include <stdarg.h>
40#include <str.h>
41#include <ctype.h>
42
43#include "config.h"
44#include "errors.h"
45#include "util.h"
46#include "entry.h"
47#include "mkfile.h"
48#include "cmds.h"
49
50/** Number of bytes to write at a time */
51#define BUFFER_SIZE 16384
52
53static const char *cmdname = "mkfile";
54
55static struct option const long_options[] = {
56 {"size", required_argument, 0, 's'},
57 {"sparse", no_argument, 0, 'p'},
58 {"help", no_argument, 0, 'h'},
59 {0, 0, 0, 0}
60};
61
62void help_cmd_mkfile(unsigned int level)
63{
64 if (level == HELP_SHORT) {
65 printf("`%s' creates a new zero-filled file\n", cmdname);
66 } else {
67 help_cmd_mkfile(HELP_SHORT);
68 printf(
69 "Usage: %s [options] <path>\n"
70 "Options:\n"
71 " -h, --help A short option summary\n"
72 " -s, --size sz Size of the file\n"
73 " -p, --sparse Create a sparse file\n"
74 "\n"
75 "Size is a number followed by 'k', 'm' or 'g' for kB, MB, GB.\n"
76 "E.g. 100k, 2m, 1g.\n",
77 cmdname);
78 }
79
80 return;
81}
82
83/** Parse size specification.
84 *
85 * Size specification is in the form <decimal_number><unit> where
86 * <unit> is 'k', 'm' or 'g' for kB, MB, GB.
87 *
88 * @param str String containing the size specification.
89 * @return Non-negative size in bytes on success, -1 on failure.
90 */
91static ssize_t read_size(const char *str)
92{
93 ssize_t number, unit;
94 char *ep;
95
96 number = strtol(str, &ep, 10);
97 if (ep[0] == '\0')
98 return number;
99
100 if (ep[1] != '\0')
101 return -1;
102
103 switch (tolower(ep[0])) {
104 case 'k': unit = 1024; break;
105 case 'm': unit = 1024*1024; break;
106 case 'g': unit = 1024*1024*1024; break;
107 default: return -1;
108 }
109
110 return number * unit;
111}
112
113int cmd_mkfile(char **argv)
114{
115 unsigned int argc;
116 int c, opt_ind;
117 int fd;
118 ssize_t file_size;
119 ssize_t total_written;
120 ssize_t to_write, rc, rc2 = 0;
121 char *file_name;
122 void *buffer;
123 bool create_sparse = false;
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 if ((rc2 = lseek(fd, file_size - 1, SEEK_SET)) < 0)
169 goto error;
170
171 rc2 = write(fd, &byte, sizeof(char));
172 if (rc2 < 0)
173 goto error;
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, buffer, to_write);
187 if (rc <= 0) {
188 printf("%s: Error writing file (%d).\n", cmdname, errno);
189 close(fd);
190 return CMD_FAILURE;
191 }
192 total_written += rc;
193 }
194
195 free(buffer);
196
197 if (close(fd) < 0)
198 goto error;
199
200 return CMD_SUCCESS;
201error:
202 printf("%s: Error writing file (%d).\n", cmdname, errno);
203 return CMD_FAILURE;
204}
Note: See TracBrowser for help on using the repository browser.