source: mainline/uspace/app/newfile/newfile.c@ c3db721

Last change on this file since c3db721 was c3db721, checked in by Jiri Svoboda <jiri@…>, 2 months ago

Allow aborting file management operation.

  • Property mode set to 100644
File size: 5.3 KB
Line 
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 <io/console.h>
39#include <io/cons_event.h>
40#include <io/kbd_event.h>
41#include <stdbool.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <str.h>
45#include <str_error.h>
46
47#define NAME "newfile"
48
49static bool newfile_abort_query(void *);
50static void newfile_progress(void *, fmgt_progress_t *);
51
52static bool prog_upd = false;
53static bool quiet = false;
54
55static console_ctrl_t *con;
56
57static fmgt_cb_t newfile_fmgt_cb = {
58 .abort_query = newfile_abort_query,
59 .progress = newfile_progress
60};
61
62static void print_syntax(void)
63{
64 printf("Create new file.\n");
65 printf("Syntax: %s [<options] [<file-name>]\n", NAME);
66 printf("\t-h help\n");
67 printf("\t-n non-interactive\n");
68 printf("\t-p create sparse file\n");
69 printf("\t-q quiet\n");
70 printf("\t-size=<cap> file size (<number>[<kB>|<MB>|...])\n");
71}
72
73/** Called by fmgt to query for user abort.
74 *
75 * @param arg Argument (not used)
76 * @return @c true iff user requested abort
77 */
78static bool newfile_abort_query(void *arg)
79{
80 cons_event_t event;
81 kbd_event_t *ev;
82 errno_t rc;
83 usec_t timeout;
84
85 if (con == NULL)
86 return false;
87
88 timeout = 0;
89 rc = console_get_event_timeout(con, &event, &timeout);
90 if (rc != EOK)
91 return false;
92
93 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
94 ev = &event.ev.key;
95 if ((ev->mods & KM_ALT) == 0 &&
96 (ev->mods & KM_SHIFT) == 0 &&
97 (ev->mods & KM_CTRL) != 0) {
98 if (ev->key == KC_C)
99 return true;
100 }
101 }
102
103 return false;
104}
105
106/** Called by fmgt to give the user progress update.
107 *
108 * @param arg Argument (not used)
109 * @param progress Progress report
110 */
111static void newfile_progress(void *arg, fmgt_progress_t *progress)
112{
113 (void)arg;
114
115 if (!quiet) {
116 printf("\rWritten %s of %s (%s done).", progress->curf_procb,
117 progress->curf_totalb, progress->curf_percent);
118 fflush(stdout);
119 prog_upd = true;
120 }
121}
122
123int main(int argc, char *argv[])
124{
125 fmgt_t *fmgt = NULL;
126 errno_t rc;
127 int i;
128 bool nonint = false;
129 bool sparse = false;
130 char *fsize = NULL;
131 char *fname = NULL;
132 capa_spec_t fcap;
133 uint64_t nbytes = 0;
134
135 con = console_init(stdin, stdout);
136
137 i = 1;
138 while (i < argc && argv[i][0] == '-') {
139 if (str_cmp(argv[i], "-h") == 0) {
140 print_syntax();
141 return 0;
142 } else if (str_cmp(argv[i], "-n") == 0) {
143 ++i;
144 nonint = true;
145 } else if (str_cmp(argv[i], "-p") == 0) {
146 ++i;
147 sparse = true;
148 } else if (str_cmp(argv[i], "-q") == 0) {
149 ++i;
150 quiet = true;
151 } else if (str_lcmp(argv[i], "-size=",
152 str_length("-size=")) == 0) {
153 fsize = argv[i] + str_length("-size=");
154 ++i;
155 } else {
156 printf("Invalid option '%s'.\n", argv[i]);
157 print_syntax();
158 goto error;
159 }
160 }
161
162 if (i < argc) {
163 fname = str_dup(argv[i++]);
164 if (fname == NULL) {
165 printf("Out of memory.\n");
166 goto error;
167 }
168 }
169
170 if (i < argc) {
171 printf("Unexpected argument.\n");
172 print_syntax();
173 goto error;
174 }
175
176 if (fname == NULL) {
177 rc = fmgt_new_file_suggest(&fname);
178 if (rc != EOK) {
179 printf("Out of memory.\n");
180 goto error;
181 }
182 }
183
184 (void)nonint;
185 (void)quiet;
186 (void)sparse;
187
188 if (fsize != NULL) {
189 rc = capa_parse(fsize, &fcap);
190 if (rc != EOK) {
191 printf("Invalid file size '%s'.\n", fsize);
192 goto error;
193 }
194
195 rc = capa_to_blocks(&fcap, cv_nom, 1, &nbytes);
196 if (rc != EOK) {
197 printf("File size too large '%s'.\n", fsize);
198 goto error;
199 }
200 }
201
202 rc = fmgt_create(&fmgt);
203 if (rc != EOK) {
204 printf("Out of memory.\n");
205 goto error;
206 }
207
208 fmgt_set_cb(fmgt, &newfile_fmgt_cb, NULL);
209
210 rc = fmgt_new_file(fmgt, fname, nbytes, sparse ? nf_sparse : nf_none);
211 if (prog_upd)
212 printf("\n");
213 if (rc != EOK) {
214 printf("Error creating file: %s.\n", str_error(rc));
215 goto error;
216 }
217
218 free(fname);
219 fmgt_destroy(fmgt);
220 return 0;
221error:
222 if (fname != NULL)
223 free(fname);
224 if (fmgt != NULL)
225 fmgt_destroy(fmgt);
226 return 1;
227}
228
229/** @}
230 */
Note: See TracBrowser for help on using the repository browser.