| 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 <stdio.h>
|
|---|
| 30 | #include <stdlib.h>
|
|---|
| 31 | #include <dirent.h>
|
|---|
| 32 | #include <fcntl.h>
|
|---|
| 33 | #include <sys/types.h>
|
|---|
| 34 | #include <sys/stat.h>
|
|---|
| 35 | #include <fcntl.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 |
|
|---|
| 52 | static const char *cmdname = "mkfile";
|
|---|
| 53 |
|
|---|
| 54 | static 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 |
|
|---|
| 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(
|
|---|
| 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 | */
|
|---|
| 90 | static 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 |
|
|---|
| 112 | int 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 |
|
|---|
| 124 | file_size = 0;
|
|---|
| 125 |
|
|---|
| 126 | argc = cli_count_args(argv);
|
|---|
| 127 |
|
|---|
| 128 | for (c = 0, 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 = open(file_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
|
|---|
| 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 | if ((rc2 = lseek(fd, file_size - 1, SEEK_SET)) < 0)
|
|---|
| 168 | goto exit;
|
|---|
| 169 |
|
|---|
| 170 | rc2 = write(fd, &byte, sizeof(char));
|
|---|
| 171 | goto exit;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | buffer = calloc(BUFFER_SIZE, 1);
|
|---|
| 175 | if (buffer == NULL) {
|
|---|
| 176 | printf("%s: Error, out of memory.\n", cmdname);
|
|---|
| 177 | return CMD_FAILURE;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | total_written = 0;
|
|---|
| 181 | while (total_written < file_size) {
|
|---|
| 182 | to_write = min(file_size - total_written, BUFFER_SIZE);
|
|---|
| 183 | rc = write(fd, buffer, to_write);
|
|---|
| 184 | if (rc <= 0) {
|
|---|
| 185 | printf("%s: Error writing file (%zd).\n", cmdname, rc);
|
|---|
| 186 | close(fd);
|
|---|
| 187 | return CMD_FAILURE;
|
|---|
| 188 | }
|
|---|
| 189 | total_written += rc;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | free(buffer);
|
|---|
| 193 | exit:
|
|---|
| 194 | rc = close(fd);
|
|---|
| 195 |
|
|---|
| 196 | if (rc != 0 || rc2 < 0) {
|
|---|
| 197 | printf("%s: Error writing file (%zd).\n", cmdname, rc);
|
|---|
| 198 | return CMD_FAILURE;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | return CMD_SUCCESS;
|
|---|
| 202 | }
|
|---|