| 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 futil
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file File manipulation utility functions
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <dirent.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <stdbool.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <stddef.h>
|
|---|
| 41 | #include <vfs/vfs.h>
|
|---|
| 42 | #include <dirent.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "futil.h"
|
|---|
| 45 |
|
|---|
| 46 | #define BUF_SIZE 16384
|
|---|
| 47 | static char buf[BUF_SIZE];
|
|---|
| 48 |
|
|---|
| 49 | /** Create file utility instance.
|
|---|
| 50 | *
|
|---|
| 51 | * @param cb Callback functions
|
|---|
| 52 | * @param arg Argument to callback functions
|
|---|
| 53 | * @param rfutil Place to store pointer to new file utility instance
|
|---|
| 54 | * @return EOK on succcess, ENOMEM if out of memory.
|
|---|
| 55 | */
|
|---|
| 56 | errno_t futil_create(futil_cb_t *cb, void *arg, futil_t **rfutil)
|
|---|
| 57 | {
|
|---|
| 58 | futil_t *futil;
|
|---|
| 59 |
|
|---|
| 60 | futil = calloc(1, sizeof(futil_t));
|
|---|
| 61 | if (futil == NULL)
|
|---|
| 62 | return ENOMEM;
|
|---|
| 63 |
|
|---|
| 64 | futil->cb = cb;
|
|---|
| 65 | futil->cb_arg = arg;
|
|---|
| 66 | *rfutil = futil;
|
|---|
| 67 | return EOK;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /** Destroy file utility instance.
|
|---|
| 71 | *
|
|---|
| 72 | * @param futil File utility instance
|
|---|
| 73 | */
|
|---|
| 74 | void futil_destroy(futil_t *futil)
|
|---|
| 75 | {
|
|---|
| 76 | free(futil);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Copy file.
|
|---|
| 80 | *
|
|---|
| 81 | * @param futil File utility instance
|
|---|
| 82 | * @param srcp Source path
|
|---|
| 83 | * @param dstp Destination path
|
|---|
| 84 | *
|
|---|
| 85 | * @return EOK on success, EIO on I/O error
|
|---|
| 86 | */
|
|---|
| 87 | errno_t futil_copy_file(futil_t *futil, const char *srcp, const char *destp)
|
|---|
| 88 | {
|
|---|
| 89 | int sf, df;
|
|---|
| 90 | size_t nr, nw;
|
|---|
| 91 | errno_t rc;
|
|---|
| 92 | aoff64_t posr = 0, posw = 0;
|
|---|
| 93 |
|
|---|
| 94 | if (futil->cb != NULL && futil->cb->copy_file != NULL)
|
|---|
| 95 | futil->cb->copy_file(futil->cb_arg, srcp, destp);
|
|---|
| 96 |
|
|---|
| 97 | rc = vfs_lookup_open(srcp, WALK_REGULAR, MODE_READ, &sf);
|
|---|
| 98 | if (rc != EOK)
|
|---|
| 99 | return EIO;
|
|---|
| 100 |
|
|---|
| 101 | rc = vfs_lookup_open(destp, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE, &df);
|
|---|
| 102 | if (rc != EOK)
|
|---|
| 103 | return EIO;
|
|---|
| 104 |
|
|---|
| 105 | do {
|
|---|
| 106 | rc = vfs_read(sf, &posr, buf, BUF_SIZE, &nr);
|
|---|
| 107 | if (rc != EOK)
|
|---|
| 108 | goto error;
|
|---|
| 109 | if (nr == 0)
|
|---|
| 110 | break;
|
|---|
| 111 |
|
|---|
| 112 | rc = vfs_write(df, &posw, buf, nr, &nw);
|
|---|
| 113 | if (rc != EOK)
|
|---|
| 114 | goto error;
|
|---|
| 115 |
|
|---|
| 116 | } while (nr == BUF_SIZE);
|
|---|
| 117 |
|
|---|
| 118 | (void) vfs_put(sf);
|
|---|
| 119 |
|
|---|
| 120 | rc = vfs_put(df);
|
|---|
| 121 | if (rc != EOK)
|
|---|
| 122 | return EIO;
|
|---|
| 123 |
|
|---|
| 124 | return EOK;
|
|---|
| 125 | error:
|
|---|
| 126 | vfs_put(sf);
|
|---|
| 127 | vfs_put(df);
|
|---|
| 128 | return rc;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** Copy contents of srcdir (recursively) into destdir.
|
|---|
| 132 | *
|
|---|
| 133 | * @param futil File utility instance
|
|---|
| 134 | * @param srcdir Source directory
|
|---|
| 135 | * @param destdir Destination directory
|
|---|
| 136 | *
|
|---|
| 137 | * @return EOK on success, ENOMEM if out of memory, EIO on I/O error
|
|---|
| 138 | */
|
|---|
| 139 | errno_t futil_rcopy_contents(futil_t *futil, const char *srcdir,
|
|---|
| 140 | const char *destdir)
|
|---|
| 141 | {
|
|---|
| 142 | DIR *dir;
|
|---|
| 143 | struct dirent *de;
|
|---|
| 144 | vfs_stat_t s;
|
|---|
| 145 | char *srcp, *destp;
|
|---|
| 146 | errno_t rc;
|
|---|
| 147 |
|
|---|
| 148 | dir = opendir(srcdir);
|
|---|
| 149 | if (dir == NULL)
|
|---|
| 150 | return EIO;
|
|---|
| 151 |
|
|---|
| 152 | de = readdir(dir);
|
|---|
| 153 | while (de != NULL) {
|
|---|
| 154 | if (asprintf(&srcp, "%s/%s", srcdir, de->d_name) < 0)
|
|---|
| 155 | return ENOMEM;
|
|---|
| 156 | if (asprintf(&destp, "%s/%s", destdir, de->d_name) < 0)
|
|---|
| 157 | return ENOMEM;
|
|---|
| 158 |
|
|---|
| 159 | rc = vfs_stat_path(srcp, &s);
|
|---|
| 160 | if (rc != EOK)
|
|---|
| 161 | return EIO;
|
|---|
| 162 |
|
|---|
| 163 | if (s.is_file) {
|
|---|
| 164 | rc = futil_copy_file(futil, srcp, destp);
|
|---|
| 165 | if (rc != EOK)
|
|---|
| 166 | return EIO;
|
|---|
| 167 | } else if (s.is_directory) {
|
|---|
| 168 | if (futil->cb != NULL && futil->cb->create_dir != NULL)
|
|---|
| 169 | futil->cb->create_dir(futil->cb_arg, destp);
|
|---|
| 170 | rc = vfs_link_path(destp, KIND_DIRECTORY, NULL);
|
|---|
| 171 | if (rc != EOK)
|
|---|
| 172 | return EIO;
|
|---|
| 173 | rc = futil_rcopy_contents(futil, srcp, destp);
|
|---|
| 174 | if (rc != EOK)
|
|---|
| 175 | return EIO;
|
|---|
| 176 | } else {
|
|---|
| 177 | return EIO;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | de = readdir(dir);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | return EOK;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** Return file contents as a heap-allocated block of bytes.
|
|---|
| 187 | *
|
|---|
| 188 | * @param futil File utility instance
|
|---|
| 189 | * @param srcp File path
|
|---|
| 190 | * @param rdata Place to store pointer to data
|
|---|
| 191 | * @param rsize Place to store size of data
|
|---|
| 192 | *
|
|---|
| 193 | * @return EOK on success, ENOENT if failed to open file, EIO on other
|
|---|
| 194 | * I/O error, ENOMEM if out of memory
|
|---|
| 195 | */
|
|---|
| 196 | errno_t futil_get_file(futil_t *futil, const char *srcp, void **rdata,
|
|---|
| 197 | size_t *rsize)
|
|---|
| 198 | {
|
|---|
| 199 | int sf;
|
|---|
| 200 | size_t nr;
|
|---|
| 201 | errno_t rc;
|
|---|
| 202 | size_t fsize;
|
|---|
| 203 | char *data;
|
|---|
| 204 | vfs_stat_t st;
|
|---|
| 205 |
|
|---|
| 206 | rc = vfs_lookup_open(srcp, WALK_REGULAR, MODE_READ, &sf);
|
|---|
| 207 | if (rc != EOK)
|
|---|
| 208 | return ENOENT;
|
|---|
| 209 |
|
|---|
| 210 | if (vfs_stat(sf, &st) != EOK) {
|
|---|
| 211 | vfs_put(sf);
|
|---|
| 212 | return EIO;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | fsize = st.size;
|
|---|
| 216 |
|
|---|
| 217 | data = calloc(fsize, 1);
|
|---|
| 218 | if (data == NULL) {
|
|---|
| 219 | vfs_put(sf);
|
|---|
| 220 | return ENOMEM;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | rc = vfs_read(sf, (aoff64_t []) { 0 }, data, fsize, &nr);
|
|---|
| 224 | if (rc != EOK || nr != fsize) {
|
|---|
| 225 | vfs_put(sf);
|
|---|
| 226 | free(data);
|
|---|
| 227 | return EIO;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | (void) vfs_put(sf);
|
|---|
| 231 | *rdata = data;
|
|---|
| 232 | *rsize = fsize;
|
|---|
| 233 |
|
|---|
| 234 | return EOK;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /** @}
|
|---|
| 238 | */
|
|---|