| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 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 | /** @addtogroup newfile
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Create new file.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <capa.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <fmgt.h>
|
|---|
| 38 | #include <stdbool.h>
|
|---|
| 39 | #include <stdio.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 | #include <str.h>
|
|---|
| 42 | #include <str_error.h>
|
|---|
| 43 |
|
|---|
| 44 | #define NAME "newfile"
|
|---|
| 45 |
|
|---|
| 46 | static void newfile_progress(void *, fmgt_progress_t *);
|
|---|
| 47 |
|
|---|
| 48 | static bool prog_upd = false;
|
|---|
| 49 | static bool quiet = false;
|
|---|
| 50 |
|
|---|
| 51 | static fmgt_cb_t newfile_fmgt_cb = {
|
|---|
| 52 | .progress = newfile_progress
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | static void print_syntax(void)
|
|---|
| 56 | {
|
|---|
| 57 | printf("Create new file.\n");
|
|---|
| 58 | printf("Syntax: %s [<options] [<file-name>]\n", NAME);
|
|---|
| 59 | printf("\t-h help\n");
|
|---|
| 60 | printf("\t-n non-interactive\n");
|
|---|
| 61 | printf("\t-p create sparse file\n");
|
|---|
| 62 | printf("\t-q quiet\n");
|
|---|
| 63 | printf("\t-size=<cap> file size (<number>[<kB>|<MB>|...])\n");
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /** Called by fmgt to give the user progress update.
|
|---|
| 67 | *
|
|---|
| 68 | * @param arg Argument (not used)
|
|---|
| 69 | * @param progress Progress report
|
|---|
| 70 | */
|
|---|
| 71 | static void newfile_progress(void *arg, fmgt_progress_t *progress)
|
|---|
| 72 | {
|
|---|
| 73 | (void)arg;
|
|---|
| 74 |
|
|---|
| 75 | if (!quiet) {
|
|---|
| 76 | printf("\rWritten %s of %s (%s done).", progress->curf_procb,
|
|---|
| 77 | progress->curf_totalb, progress->curf_percent);
|
|---|
| 78 | fflush(stdout);
|
|---|
| 79 | prog_upd = true;
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int main(int argc, char *argv[])
|
|---|
| 84 | {
|
|---|
| 85 | fmgt_t *fmgt = NULL;
|
|---|
| 86 | errno_t rc;
|
|---|
| 87 | int i;
|
|---|
| 88 | bool nonint = false;
|
|---|
| 89 | bool sparse = false;
|
|---|
| 90 | char *fsize = NULL;
|
|---|
| 91 | char *fname = NULL;
|
|---|
| 92 | capa_spec_t fcap;
|
|---|
| 93 | uint64_t nbytes = 0;
|
|---|
| 94 |
|
|---|
| 95 | i = 1;
|
|---|
| 96 | while (i < argc && argv[i][0] == '-') {
|
|---|
| 97 | if (str_cmp(argv[i], "-h") == 0) {
|
|---|
| 98 | print_syntax();
|
|---|
| 99 | return 0;
|
|---|
| 100 | } else if (str_cmp(argv[i], "-n") == 0) {
|
|---|
| 101 | ++i;
|
|---|
| 102 | nonint = true;
|
|---|
| 103 | } else if (str_cmp(argv[i], "-p") == 0) {
|
|---|
| 104 | ++i;
|
|---|
| 105 | sparse = true;
|
|---|
| 106 | } else if (str_cmp(argv[i], "-q") == 0) {
|
|---|
| 107 | ++i;
|
|---|
| 108 | quiet = true;
|
|---|
| 109 | } else if (str_lcmp(argv[i], "-size=",
|
|---|
| 110 | str_length("-size=")) == 0) {
|
|---|
| 111 | fsize = argv[i] + str_length("-size=");
|
|---|
| 112 | ++i;
|
|---|
| 113 | } else {
|
|---|
| 114 | printf("Invalid option '%s'.\n", argv[i]);
|
|---|
| 115 | print_syntax();
|
|---|
| 116 | goto error;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | if (i < argc) {
|
|---|
| 121 | fname = str_dup(argv[i++]);
|
|---|
| 122 | if (fname == NULL) {
|
|---|
| 123 | printf("Out of memory.\n");
|
|---|
| 124 | goto error;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | if (i < argc) {
|
|---|
| 129 | printf("Unexpected argument.\n");
|
|---|
| 130 | print_syntax();
|
|---|
| 131 | goto error;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (fname == NULL) {
|
|---|
| 135 | rc = fmgt_new_file_suggest(&fname);
|
|---|
| 136 | if (rc != EOK) {
|
|---|
| 137 | printf("Out of memory.\n");
|
|---|
| 138 | goto error;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | (void)nonint;
|
|---|
| 143 | (void)quiet;
|
|---|
| 144 | (void)sparse;
|
|---|
| 145 |
|
|---|
| 146 | if (fsize != NULL) {
|
|---|
| 147 | rc = capa_parse(fsize, &fcap);
|
|---|
| 148 | if (rc != EOK) {
|
|---|
| 149 | printf("Invalid file size '%s'.\n", fsize);
|
|---|
| 150 | goto error;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | rc = capa_to_blocks(&fcap, cv_nom, 1, &nbytes);
|
|---|
| 154 | if (rc != EOK) {
|
|---|
| 155 | printf("File size too large '%s'.\n", fsize);
|
|---|
| 156 | goto error;
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | rc = fmgt_create(&fmgt);
|
|---|
| 161 | if (rc != EOK) {
|
|---|
| 162 | printf("Out of memory.\n");
|
|---|
| 163 | goto error;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | fmgt_set_cb(fmgt, &newfile_fmgt_cb, NULL);
|
|---|
| 167 |
|
|---|
| 168 | rc = fmgt_new_file(fmgt, fname, nbytes);
|
|---|
| 169 | if (rc != EOK) {
|
|---|
| 170 | printf("Error creating file: %s.\n", str_error(rc));
|
|---|
| 171 | goto error;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | if (prog_upd)
|
|---|
| 175 | printf("\n");
|
|---|
| 176 |
|
|---|
| 177 | free(fname);
|
|---|
| 178 | fmgt_destroy(fmgt);
|
|---|
| 179 | return 0;
|
|---|
| 180 | error:
|
|---|
| 181 | if (fname != NULL)
|
|---|
| 182 | free(fname);
|
|---|
| 183 | if (fmgt != NULL)
|
|---|
| 184 | fmgt_destroy(fmgt);
|
|---|
| 185 | return 1;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /** @}
|
|---|
| 189 | */
|
|---|