[336d2faa] | 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 |
|
---|
[6afc9d7] | 29 | #include <errno.h>
|
---|
[336d2faa] | 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 32 | #include <stdint.h>
|
---|
[9af1c61] | 33 | #include <str_error.h>
|
---|
[336d2faa] | 34 | #include <dirent.h>
|
---|
| 35 | #include <macros.h>
|
---|
| 36 | #include <getopt.h>
|
---|
| 37 | #include <stdarg.h>
|
---|
[19f857a] | 38 | #include <str.h>
|
---|
[336d2faa] | 39 | #include <ctype.h>
|
---|
[b19e892] | 40 | #include <vfs/vfs.h>
|
---|
[336d2faa] | 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 |
|
---|
[a000878c] | 52 | static const char *cmdname = "mkfile";
|
---|
[336d2faa] | 53 |
|
---|
| 54 | static struct option const long_options[] = {
|
---|
[1433ecda] | 55 | { "size", required_argument, 0, 's' },
|
---|
| 56 | { "sparse", no_argument, 0, 'p' },
|
---|
| 57 | { "help", no_argument, 0, 'h' },
|
---|
| 58 | { 0, 0, 0, 0 }
|
---|
[336d2faa] | 59 | };
|
---|
| 60 |
|
---|
| 61 | void 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(
|
---|
[1433ecda] | 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);
|
---|
[336d2faa] | 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.
|
---|
[8e3498b] | 88 | * @param rsize Place to store size in bytes
|
---|
[cde999a] | 89 | * @return EOK on success or an error code
|
---|
[336d2faa] | 90 | */
|
---|
[b7fd2a0] | 91 | static errno_t read_size(const char *str, size_t *rsize)
|
---|
[336d2faa] | 92 | {
|
---|
[8e3498b] | 93 | size_t number, unit;
|
---|
[336d2faa] | 94 | char *ep;
|
---|
| 95 |
|
---|
| 96 | number = strtol(str, &ep, 10);
|
---|
[8e3498b] | 97 | if (ep[0] == '\0') {
|
---|
| 98 | *rsize = number;
|
---|
| 99 | return EOK;
|
---|
| 100 | }
|
---|
[336d2faa] | 101 |
|
---|
| 102 | if (ep[1] != '\0')
|
---|
[1433ecda] | 103 | return EINVAL;
|
---|
[336d2faa] | 104 |
|
---|
| 105 | switch (tolower(ep[0])) {
|
---|
[1433ecda] | 106 | case 'k':
|
---|
| 107 | unit = 1024;
|
---|
| 108 | break;
|
---|
| 109 | case 'm':
|
---|
| 110 | unit = 1024 * 1024;
|
---|
| 111 | break;
|
---|
| 112 | case 'g':
|
---|
| 113 | unit = 1024 * 1024 * 1024;
|
---|
| 114 | break;
|
---|
| 115 | default:
|
---|
| 116 | return EINVAL;
|
---|
[336d2faa] | 117 | }
|
---|
| 118 |
|
---|
[8e3498b] | 119 | *rsize = number * unit;
|
---|
| 120 | return EOK;
|
---|
[336d2faa] | 121 | }
|
---|
| 122 |
|
---|
| 123 | int cmd_mkfile(char **argv)
|
---|
| 124 | {
|
---|
| 125 | unsigned int argc;
|
---|
| 126 | int c, opt_ind;
|
---|
| 127 | int fd;
|
---|
[8e3498b] | 128 | size_t file_size;
|
---|
| 129 | size_t total_written;
|
---|
| 130 | size_t to_write;
|
---|
| 131 | size_t nwritten;
|
---|
[b7fd2a0] | 132 | errno_t rc;
|
---|
[336d2faa] | 133 | char *file_name;
|
---|
| 134 | void *buffer;
|
---|
[f9d8c3a] | 135 | bool create_sparse = false;
|
---|
[58898d1d] | 136 | aoff64_t pos = 0;
|
---|
[336d2faa] | 137 |
|
---|
| 138 | file_size = 0;
|
---|
| 139 |
|
---|
| 140 | argc = cli_count_args(argv);
|
---|
| 141 |
|
---|
[948222e4] | 142 | c = 0;
|
---|
| 143 | optreset = 1;
|
---|
| 144 | optind = 0;
|
---|
| 145 | opt_ind = 0;
|
---|
| 146 |
|
---|
| 147 | while (c != -1) {
|
---|
[f9d8c3a] | 148 | c = getopt_long(argc, argv, "ps:h", long_options, &opt_ind);
|
---|
[336d2faa] | 149 | switch (c) {
|
---|
| 150 | case 'h':
|
---|
| 151 | help_cmd_mkfile(HELP_LONG);
|
---|
| 152 | return CMD_SUCCESS;
|
---|
[f9d8c3a] | 153 | case 'p':
|
---|
| 154 | create_sparse = true;
|
---|
| 155 | break;
|
---|
[336d2faa] | 156 | case 's':
|
---|
[8e3498b] | 157 | rc = read_size(optarg, &file_size);
|
---|
| 158 | if (rc != EOK) {
|
---|
[336d2faa] | 159 | printf("%s: Invalid file size specification.\n",
|
---|
| 160 | cmdname);
|
---|
| 161 | return CMD_FAILURE;
|
---|
| 162 | }
|
---|
| 163 | break;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | argc -= optind;
|
---|
| 168 |
|
---|
| 169 | if (argc != 1) {
|
---|
| 170 | printf("%s: incorrect number of arguments. Try `%s --help'\n",
|
---|
[1433ecda] | 171 | cmdname, cmdname);
|
---|
[336d2faa] | 172 | return CMD_FAILURE;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | file_name = argv[optind];
|
---|
| 176 |
|
---|
[f77c1c9] | 177 | rc = vfs_lookup_open(file_name, WALK_REGULAR | WALK_MUST_CREATE, MODE_WRITE, &fd);
|
---|
| 178 | if (rc != EOK) {
|
---|
[336d2faa] | 179 | printf("%s: failed to create file %s.\n", cmdname, file_name);
|
---|
| 180 | return CMD_FAILURE;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[f9d8c3a] | 183 | if (create_sparse && file_size > 0) {
|
---|
| 184 | const char byte = 0x00;
|
---|
[a35b458] | 185 |
|
---|
[58898d1d] | 186 | pos = file_size - 1;
|
---|
[8e3498b] | 187 | rc = vfs_write(fd, &pos, &byte, sizeof(char), &nwritten);
|
---|
| 188 | if (rc != EOK) {
|
---|
[9c4cf0d] | 189 | vfs_put(fd);
|
---|
[6afc9d7] | 190 | goto error;
|
---|
[bc41f3a3] | 191 | }
|
---|
[6afc9d7] | 192 | return CMD_SUCCESS;
|
---|
[f9d8c3a] | 193 | }
|
---|
| 194 |
|
---|
[336d2faa] | 195 | buffer = calloc(BUFFER_SIZE, 1);
|
---|
| 196 | if (buffer == NULL) {
|
---|
| 197 | printf("%s: Error, out of memory.\n", cmdname);
|
---|
| 198 | return CMD_FAILURE;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | total_written = 0;
|
---|
| 202 | while (total_written < file_size) {
|
---|
| 203 | to_write = min(file_size - total_written, BUFFER_SIZE);
|
---|
[8e3498b] | 204 | rc = vfs_write(fd, &pos, buffer, to_write, &nwritten);
|
---|
| 205 | if (rc != EOK) {
|
---|
[39b54fe] | 206 | printf("%s: Error writing file (%s).\n", cmdname, str_error(rc));
|
---|
[9c4cf0d] | 207 | vfs_put(fd);
|
---|
[bc41f3a3] | 208 | free(buffer);
|
---|
[336d2faa] | 209 | return CMD_FAILURE;
|
---|
| 210 | }
|
---|
[8e3498b] | 211 | total_written += nwritten;
|
---|
[336d2faa] | 212 | }
|
---|
| 213 |
|
---|
[f9d8c3a] | 214 | free(buffer);
|
---|
| 215 |
|
---|
[39b54fe] | 216 | rc = vfs_put(fd);
|
---|
| 217 | if (rc != EOK)
|
---|
[6afc9d7] | 218 | goto error;
|
---|
[336d2faa] | 219 |
|
---|
| 220 | return CMD_SUCCESS;
|
---|
[6afc9d7] | 221 | error:
|
---|
[39b54fe] | 222 | printf("%s: Error writing file (%s).\n", cmdname, str_error(rc));
|
---|
[6afc9d7] | 223 | return CMD_FAILURE;
|
---|
[336d2faa] | 224 | }
|
---|