| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 18 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/io/Dir.h> |
| 24 |
#include <lsp-plug.in/io/File.h> |
| 25 |
|
| 26 |
using namespace lsp; |
| 27 |
|
| 28 |
UTEST_BEGIN("runtime.io", dir) |
| 29 |
|
| 30 |
void testReadDir(const char *dir, bool full) |
| 31 |
{ |
| 32 |
io::Dir dh; |
| 33 |
io::Path path; |
| 34 |
LSPString xdir; |
| 35 |
size_t n_read1 = 0, n_read2 = 0; |
| 36 |
|
| 37 |
UTEST_ASSERT(xdir.set_utf8(dir)); |
| 38 |
|
| 39 |
// Try to read non-empty directory |
| 40 |
UTEST_ASSERT(dh.open(&xdir) == STATUS_OK); |
| 41 |
printf("Reading directory %s...\n", xdir.get_native()); |
| 42 |
while (dh.read(&path, full) == STATUS_OK) |
| 43 |
{ |
| 44 |
if (path.is_dot() || path.is_dotdot()) |
| 45 |
continue; |
| 46 |
|
| 47 |
printf(" found file: %s\n", path.as_native()); |
| 48 |
++n_read1; |
| 49 |
} |
| 50 |
UTEST_ASSERT(dh.last_error() == STATUS_EOF); |
| 51 |
UTEST_ASSERT(n_read1 >= 7); |
| 52 |
|
| 53 |
// Try to re-read non-empty directory |
| 54 |
UTEST_ASSERT(dh.rewind() == STATUS_OK); |
| 55 |
printf("Re-reading directory %s...\n", xdir.get_native()); |
| 56 |
while (dh.read(&path, full) == STATUS_OK) |
| 57 |
{ |
| 58 |
if (path.is_dot() || path.is_dotdot()) |
| 59 |
continue; |
| 60 |
|
| 61 |
printf(" found file: %s\n", path.as_native()); |
| 62 |
++n_read2; |
| 63 |
} |
| 64 |
UTEST_ASSERT(dh.last_error() == STATUS_EOF); |
| 65 |
UTEST_ASSERT(n_read1 == n_read2); |
| 66 |
|
| 67 |
// Try to re-read non-empty directory |
| 68 |
n_read2 = 0; |
| 69 |
io::fattr_t attr; |
| 70 |
UTEST_ASSERT(dh.rewind() == STATUS_OK); |
| 71 |
printf("Re-reading directory with stat %s...\n", xdir.get_native()); |
| 72 |
while (dh.reads(&path, &attr, full) == STATUS_OK) |
| 73 |
{ |
| 74 |
if (path.is_dot() || path.is_dotdot()) |
| 75 |
continue; |
| 76 |
const char *type = "unknown"; |
| 77 |
|
| 78 |
switch (attr.type) |
| 79 |
{ |
| 80 |
case io::fattr_t::FT_BLOCK: type = "block"; break; |
| 81 |
case io::fattr_t::FT_CHARACTER: type = "char"; break; |
| 82 |
case io::fattr_t::FT_DIRECTORY: type = "dir"; break; |
| 83 |
case io::fattr_t::FT_FIFO: type = "fifo"; break; |
| 84 |
case io::fattr_t::FT_SYMLINK: type = "symlink"; break; |
| 85 |
case io::fattr_t::FT_REGULAR: type = "regular"; break; |
| 86 |
case io::fattr_t::FT_SOCKET: type = "socket"; break; |
| 87 |
default: type = "unknown"; break; |
| 88 |
} |
| 89 |
|
| 90 |
printf(" found file: %s, type=%s, size=%ld\n", path.as_native(), type, long(attr.size)); |
| 91 |
++n_read2; |
| 92 |
} |
| 93 |
UTEST_ASSERT(dh.last_error() == STATUS_EOF); |
| 94 |
|
| 95 |
UTEST_ASSERT(dh.close() == STATUS_OK); |
| 96 |
} |
| 97 |
|
| 98 |
void testCreateDir(const char *path, bool cwd) |
| 99 |
{ |
| 100 |
io::Path bp, p, xp; |
| 101 |
if (cwd) |
| 102 |
{ |
| 103 |
UTEST_ASSERT(p.current() == STATUS_OK); |
| 104 |
printf("Current path is: %s\n", p.as_native()); |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
UTEST_ASSERT(p.set(tempdir()) == STATUS_OK); |
| 109 |
printf("Temporary path is: %s\n", p.as_native()); |
| 110 |
} |
| 111 |
|
| 112 |
UTEST_ASSERT(bp.set(&p) == STATUS_OK); |
| 113 |
UTEST_ASSERT(p.append_child(full_name()) == STATUS_OK); |
| 114 |
|
| 115 |
printf("Creating directory: %s\n", p.as_native()); |
| 116 |
UTEST_ASSERT(p.mkdir() == STATUS_OK); |
| 117 |
UTEST_ASSERT(p.mkdir() == STATUS_OK); |
| 118 |
|
| 119 |
UTEST_ASSERT(xp.set(path) == STATUS_OK); |
| 120 |
UTEST_ASSERT(p.append_child(&xp) == STATUS_OK); |
| 121 |
printf("Will try to create subpath: %s\n", p.as_native()); |
| 122 |
UTEST_ASSERT(p.mkdir(true) == STATUS_OK); |
| 123 |
|
| 124 |
printf("Removing: %s\n", p.as_native()); |
| 125 |
UTEST_ASSERT(io::File::remove(&p) == STATUS_IS_DIRECTORY); |
| 126 |
UTEST_ASSERT(io::Dir::remove(&p) == STATUS_OK); |
| 127 |
UTEST_ASSERT(io::Dir::remove(&bp) != STATUS_OK); |
| 128 |
|
| 129 |
// Recursively remove directory until we reach the base path |
| 130 |
UTEST_ASSERT(p.parent() == STATUS_OK); |
| 131 |
do |
| 132 |
{ |
| 133 |
printf("Removing: %s\n", p.as_native()); |
| 134 |
UTEST_ASSERT(io::Dir::remove(&p) == STATUS_OK); |
| 135 |
UTEST_ASSERT(p.parent() == STATUS_OK); |
| 136 |
} while (!p.equals(&bp)); |
| 137 |
} |
| 138 |
|
| 139 |
UTEST_MAIN |
| 140 |
{ |
| 141 |
char path[PATH_MAX]; |
| 142 |
char c = FILE_SEPARATOR_C; |
| 143 |
|
| 144 |
::snprintf(path, sizeof(path), "%s" "%c" "io" "%c" "iconv", |
| 145 |
resources(), c, c); |
| 146 |
|
| 147 |
testReadDir(path, false); |
| 148 |
testReadDir(path, true); |
| 149 |
|
| 150 |
::snprintf(path, sizeof(path), "some" "%c" "long" "%c" "path", c, c); |
| 151 |
testCreateDir(path, true); |
| 152 |
|
| 153 |
::snprintf(path, sizeof(path), "another" "%c" "long" "%c" "path", c, c); |
| 154 |
testCreateDir(path, false); |
| 155 |
} |
| 156 |
|
| 157 |
UTEST_END |
| 158 |
|
| 159 |
|
| 160 |
|