| 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 fmgt
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Verify files.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <stdbool.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <vfs/vfs.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "fmgt.h"
|
|---|
| 41 | #include "fmgt/walk.h"
|
|---|
| 42 | #include "../private/fmgt.h"
|
|---|
| 43 |
|
|---|
| 44 | static errno_t fmgt_verify_file(void *, const char *, const char *);
|
|---|
| 45 |
|
|---|
| 46 | static fmgt_walk_cb_t fmgt_verify_cb = {
|
|---|
| 47 | .file = fmgt_verify_file
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | /** Verify a single file.
|
|---|
| 51 | *
|
|---|
| 52 | * @param arg Argument (fmgt_t *)
|
|---|
| 53 | * @param fname File name
|
|---|
| 54 | * @param unused Unused
|
|---|
| 55 | * @return EOK on success or an error code
|
|---|
| 56 | */
|
|---|
| 57 | static errno_t fmgt_verify_file(void *arg, const char *fname,
|
|---|
| 58 | const char *unused)
|
|---|
| 59 | {
|
|---|
| 60 | fmgt_t *fmgt = (fmgt_t *)arg;
|
|---|
| 61 | int fd;
|
|---|
| 62 | size_t nr;
|
|---|
| 63 | aoff64_t pos = 0;
|
|---|
| 64 | char *buffer;
|
|---|
| 65 | fmgt_io_error_t err;
|
|---|
| 66 | fmgt_error_action_t action;
|
|---|
| 67 | errno_t rc;
|
|---|
| 68 |
|
|---|
| 69 | (void)unused;
|
|---|
| 70 |
|
|---|
| 71 | buffer = calloc(BUFFER_SIZE, 1);
|
|---|
| 72 | if (buffer == NULL)
|
|---|
| 73 | return ENOMEM;
|
|---|
| 74 |
|
|---|
| 75 | rc = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ, &fd);
|
|---|
| 76 | if (rc != EOK) {
|
|---|
| 77 | free(buffer);
|
|---|
| 78 | return rc;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | fmgt_progress_init_file(fmgt, fname);
|
|---|
| 82 |
|
|---|
| 83 | do {
|
|---|
| 84 | do {
|
|---|
| 85 | rc = vfs_read(fd, &pos, buffer, BUFFER_SIZE, &nr);
|
|---|
| 86 | if (rc == EOK)
|
|---|
| 87 | break;
|
|---|
| 88 |
|
|---|
| 89 | /* I/O error */
|
|---|
| 90 | err.fname = fname;
|
|---|
| 91 | err.optype = fmgt_io_read;
|
|---|
| 92 | err.rc = rc;
|
|---|
| 93 | fmgt_timer_stop(fmgt);
|
|---|
| 94 | action = fmgt_io_error_query(fmgt, &err);
|
|---|
| 95 | fmgt_timer_start(fmgt);
|
|---|
| 96 | } while (action == fmgt_er_retry);
|
|---|
| 97 |
|
|---|
| 98 | /* Not recovered? */
|
|---|
| 99 | if (rc != EOK) {
|
|---|
| 100 | free(buffer);
|
|---|
| 101 | vfs_put(fd);
|
|---|
| 102 | fmgt_final_progress_update(fmgt);
|
|---|
| 103 | return rc;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | fmgt_progress_incr_bytes(fmgt, nr);
|
|---|
| 107 |
|
|---|
| 108 | /* User requested abort? */
|
|---|
| 109 | if (fmgt_abort_query(fmgt)) {
|
|---|
| 110 | free(buffer);
|
|---|
| 111 | vfs_put(fd);
|
|---|
| 112 | fmgt_final_progress_update(fmgt);
|
|---|
| 113 | return EINTR;
|
|---|
| 114 | }
|
|---|
| 115 | } while (nr > 0);
|
|---|
| 116 |
|
|---|
| 117 | free(buffer);
|
|---|
| 118 | vfs_put(fd);
|
|---|
| 119 | fmgt_progress_incr_files(fmgt);
|
|---|
| 120 | return EOK;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** Verify files.
|
|---|
| 124 | *
|
|---|
| 125 | * @param fmgt File management object
|
|---|
| 126 | * @param flist File list
|
|---|
| 127 | * @return EOK on success or an error code
|
|---|
| 128 | */
|
|---|
| 129 | errno_t fmgt_verify(fmgt_t *fmgt, fmgt_flist_t *flist)
|
|---|
| 130 | {
|
|---|
| 131 | fmgt_walk_params_t params;
|
|---|
| 132 | errno_t rc;
|
|---|
| 133 |
|
|---|
| 134 | fmgt_walk_params_init(¶ms);
|
|---|
| 135 |
|
|---|
| 136 | params.flist = flist;
|
|---|
| 137 | params.cb = &fmgt_verify_cb;
|
|---|
| 138 | params.arg = (void *)fmgt;
|
|---|
| 139 |
|
|---|
| 140 | fmgt_progress_init(fmgt);
|
|---|
| 141 |
|
|---|
| 142 | fmgt_timer_start(fmgt);
|
|---|
| 143 | fmgt_initial_progress_update(fmgt);
|
|---|
| 144 | rc = fmgt_walk(¶ms);
|
|---|
| 145 | fmgt_final_progress_update(fmgt);
|
|---|
| 146 | return rc;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /** @}
|
|---|
| 150 | */
|
|---|