| 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 | #include <errno.h>
|
|---|
| 30 | #include <fmgt.h>
|
|---|
| 31 | #include <pcut/pcut.h>
|
|---|
| 32 | #include <stdbool.h>
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <str.h>
|
|---|
| 35 | #include <vfs/vfs.h>
|
|---|
| 36 |
|
|---|
| 37 | PCUT_INIT;
|
|---|
| 38 |
|
|---|
| 39 | PCUT_TEST_SUITE(walk);
|
|---|
| 40 |
|
|---|
| 41 | static errno_t test_walk_dir_enter(void *, const char *);
|
|---|
| 42 | static errno_t test_walk_dir_leave(void *, const char *);
|
|---|
| 43 | static errno_t test_walk_file(void *, const char *);
|
|---|
| 44 |
|
|---|
| 45 | static fmgt_walk_cb_t test_walk_cb = {
|
|---|
| 46 | .dir_enter = test_walk_dir_enter,
|
|---|
| 47 | .dir_leave = test_walk_dir_leave,
|
|---|
| 48 | .file = test_walk_file
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | typedef struct {
|
|---|
| 52 | bool dir_enter;
|
|---|
| 53 | bool dir_leave;
|
|---|
| 54 | bool file_proc;
|
|---|
| 55 | char *dirname;
|
|---|
| 56 | char *fname;
|
|---|
| 57 | errno_t rc;
|
|---|
| 58 | } test_resp_t;
|
|---|
| 59 |
|
|---|
| 60 | /** Walk file system tree. */
|
|---|
| 61 | PCUT_TEST(walk_success)
|
|---|
| 62 | {
|
|---|
| 63 | char buf[L_tmpnam];
|
|---|
| 64 | char *fname;
|
|---|
| 65 | FILE *f;
|
|---|
| 66 | char *p;
|
|---|
| 67 | int rv;
|
|---|
| 68 | fmgt_flist_t *flist;
|
|---|
| 69 | fmgt_walk_params_t params;
|
|---|
| 70 | test_resp_t resp;
|
|---|
| 71 | errno_t rc;
|
|---|
| 72 |
|
|---|
| 73 | /* Create name for temporary directory */
|
|---|
| 74 | p = tmpnam(buf);
|
|---|
| 75 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 76 |
|
|---|
| 77 | /* Create temporary directory */
|
|---|
| 78 | rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
|
|---|
| 79 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 80 |
|
|---|
| 81 | rv = asprintf(&fname, "%s/%s", p, "a");
|
|---|
| 82 | PCUT_ASSERT_TRUE(rv >= 0);
|
|---|
| 83 |
|
|---|
| 84 | f = fopen(fname, "wb");
|
|---|
| 85 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 86 |
|
|---|
| 87 | rv = fprintf(f, "X");
|
|---|
| 88 | PCUT_ASSERT_TRUE(rv >= 0);
|
|---|
| 89 |
|
|---|
| 90 | rv = fclose(f);
|
|---|
| 91 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 92 |
|
|---|
| 93 | rc = fmgt_flist_create(&flist);
|
|---|
| 94 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 95 |
|
|---|
| 96 | rc = fmgt_flist_append(flist, p);
|
|---|
| 97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 98 |
|
|---|
| 99 | fmgt_walk_params_init(¶ms);
|
|---|
| 100 | params.flist = flist;
|
|---|
| 101 | params.cb = &test_walk_cb;
|
|---|
| 102 | params.arg = &resp;
|
|---|
| 103 |
|
|---|
| 104 | resp.dir_enter = false;
|
|---|
| 105 | resp.dir_leave = false;
|
|---|
| 106 | resp.file_proc = false;
|
|---|
| 107 | resp.rc = EOK;
|
|---|
| 108 |
|
|---|
| 109 | rc = fmgt_walk(¶ms);
|
|---|
| 110 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 111 |
|
|---|
| 112 | PCUT_ASSERT_TRUE(resp.dir_enter);
|
|---|
| 113 | PCUT_ASSERT_TRUE(resp.dir_leave);
|
|---|
| 114 | PCUT_ASSERT_TRUE(resp.file_proc);
|
|---|
| 115 | PCUT_ASSERT_STR_EQUALS(p, resp.dirname);
|
|---|
| 116 | PCUT_ASSERT_STR_EQUALS(fname, resp.fname);
|
|---|
| 117 |
|
|---|
| 118 | free(resp.dirname);
|
|---|
| 119 | free(resp.fname);
|
|---|
| 120 | fmgt_flist_destroy(flist);
|
|---|
| 121 |
|
|---|
| 122 | rv = remove(fname);
|
|---|
| 123 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 124 |
|
|---|
| 125 | rv = remove(p);
|
|---|
| 126 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 127 |
|
|---|
| 128 | free(fname);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | errno_t test_walk_dir_enter(void *arg, const char *fname)
|
|---|
| 132 | {
|
|---|
| 133 | test_resp_t *resp = (test_resp_t *)arg;
|
|---|
| 134 | resp->dir_enter = true;
|
|---|
| 135 | resp->dirname = str_dup(fname);
|
|---|
| 136 | return resp->rc;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | errno_t test_walk_dir_leave(void *arg, const char *fname)
|
|---|
| 140 | {
|
|---|
| 141 | test_resp_t *resp = (test_resp_t *)arg;
|
|---|
| 142 | resp->dir_leave = true;
|
|---|
| 143 | return resp->rc;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | errno_t test_walk_file(void *arg, const char *fname)
|
|---|
| 147 | {
|
|---|
| 148 | test_resp_t *resp = (test_resp_t *)arg;
|
|---|
| 149 | resp->file_proc = true;
|
|---|
| 150 | resp->fname = str_dup(fname);
|
|---|
| 151 | return resp->rc;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | PCUT_EXPORT(walk);
|
|---|