source: mainline/uspace/app/verify/verify.c

Last change on this file was 1ec732a, checked in by Jiri Svoboda <jiri@…>, 7 weeks ago

Verify file - navigator operation and command-line utility.

  • Property mode set to 100644
File size: 5.9 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 verify
30 * @{
31 */
32/** @file Verify that file can be read.
33 */
34
35#include <errno.h>
36#include <fmgt.h>
37#include <io/console.h>
38#include <io/cons_event.h>
39#include <io/kbd_event.h>
40#include <stdbool.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <str.h>
44#include <str_error.h>
45
46#define NAME "verify"
47
48static bool verify_abort_query(void *);
49static void verify_progress(void *, fmgt_progress_t *);
50static fmgt_error_action_t verify_io_error_query(void *, fmgt_io_error_t *);
51
52static bool prog_upd = false;
53static bool nonint = false;
54static bool quiet = false;
55
56static console_ctrl_t *con;
57
58static fmgt_cb_t verify_fmgt_cb = {
59 .abort_query = verify_abort_query,
60 .io_error_query = verify_io_error_query,
61 .progress = verify_progress,
62};
63
64static void print_syntax(void)
65{
66 printf("Verify that file can be read.\n");
67 printf("Syntax: %s [<options] <file-name>...\n", NAME);
68 printf("\t-h help\n");
69 printf("\t-n non-interactive\n");
70 printf("\t-q quiet\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 verify_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 verify_progress(void *arg, fmgt_progress_t *progress)
112{
113 (void)arg;
114
115 if (!quiet) {
116 printf("\rVerified %s files, %s; current file: %s done. "
117 "\b\b", progress->total_procf, progress->total_procb,
118 progress->curf_percent);
119 fflush(stdout);
120 prog_upd = true;
121 }
122}
123
124/** Called by fmgt to let user choose I/O error recovery action.
125 *
126 * @param arg Argument (not used)
127 * @param err I/O error report
128 * @return Error recovery action.
129 */
130static fmgt_error_action_t verify_io_error_query(void *arg,
131 fmgt_io_error_t *err)
132{
133 cons_event_t event;
134 kbd_event_t *ev;
135 errno_t rc;
136
137 (void)arg;
138
139 if (nonint)
140 return fmgt_er_abort;
141
142 if (prog_upd)
143 putchar('\n');
144
145 fprintf(stderr, "I/O error %s file '%s' (%s).\n",
146 err->optype == fmgt_io_write ? "writing" : "reading",
147 err->fname, str_error(err->rc));
148 fprintf(stderr, "[A]bort or [R]etry?\n");
149
150 if (con == NULL)
151 return fmgt_er_abort;
152
153 while (true) {
154 rc = console_get_event(con, &event);
155 if (rc != EOK)
156 return fmgt_er_abort;
157
158 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
159 ev = &event.ev.key;
160 if ((ev->mods & KM_ALT) == 0 &&
161 (ev->mods & KM_CTRL) == 0) {
162 if (ev->c == 'r' || ev->c == 'R')
163 return fmgt_er_retry;
164 if (ev->c == 'a' || ev->c == 'A')
165 return fmgt_er_abort;
166 }
167 }
168
169 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) {
170 ev = &event.ev.key;
171 if ((ev->mods & KM_ALT) == 0 &&
172 (ev->mods & KM_SHIFT) == 0 &&
173 (ev->mods & KM_CTRL) != 0) {
174 if (ev->key == KC_C)
175 return fmgt_er_abort;
176 }
177 }
178 }
179}
180
181int main(int argc, char *argv[])
182{
183 fmgt_t *fmgt = NULL;
184 errno_t rc;
185 int i;
186 fmgt_flist_t *flist = NULL;
187
188 rc = fmgt_flist_create(&flist);
189 if (rc != EOK) {
190 printf("Out of memory.\n");
191 goto error;
192 }
193
194 con = console_init(stdin, stdout);
195
196 i = 1;
197 while (i < argc && argv[i][0] == '-') {
198 if (str_cmp(argv[i], "-h") == 0) {
199 print_syntax();
200 return 0;
201 } else if (str_cmp(argv[i], "-n") == 0) {
202 ++i;
203 nonint = true;
204 } else if (str_cmp(argv[i], "-q") == 0) {
205 ++i;
206 quiet = true;
207 } else {
208 printf("Invalid option '%s'.\n", argv[i]);
209 print_syntax();
210 goto error;
211 }
212 }
213
214 if (i >= argc) {
215 print_syntax();
216 goto error;
217 }
218
219 do {
220 rc = fmgt_flist_append(flist, argv[i++]);
221 if (rc != EOK) {
222 printf("Out of memory.\n");
223 goto error;
224 }
225 } while (i < argc);
226
227 rc = fmgt_create(&fmgt);
228 if (rc != EOK) {
229 printf("Out of memory.\n");
230 goto error;
231 }
232
233 fmgt_set_cb(fmgt, &verify_fmgt_cb, NULL);
234
235 rc = fmgt_verify(fmgt, flist);
236 if (prog_upd)
237 putchar('\n');
238 if (rc != EOK) {
239 printf("Error creating file: %s.\n", str_error(rc));
240 goto error;
241 }
242
243 fmgt_flist_destroy(flist);
244 fmgt_destroy(fmgt);
245 return 0;
246error:
247 if (flist != NULL)
248 fmgt_flist_destroy(flist);
249 if (fmgt != NULL)
250 fmgt_destroy(fmgt);
251 return 1;
252}
253
254/** @}
255 */
Note: See TracBrowser for help on using the repository browser.