|
| 1 | +/* |
| 2 | + * lws-api-test-dir |
| 3 | + * |
| 4 | + * Written in 2010-2019 by Andy Green <andy@warmcat.com> |
| 5 | + * |
| 6 | + * This file is made available under the Creative Commons CC0 1.0 |
| 7 | + * Universal Public Domain Dedication. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <libwebsockets.h> |
| 11 | +#include <sys/stat.h> |
| 12 | +#include <fcntl.h> |
| 13 | + |
| 14 | +#if defined(WIN32) |
| 15 | +#include <direct.h> |
| 16 | +#define mkdir(x,y) _mkdir(x) |
| 17 | +#define rmdir _rmdir |
| 18 | +#endif |
| 19 | + |
| 20 | +static int |
| 21 | +create_file(const char *path, size_t size) |
| 22 | +{ |
| 23 | + int fd = lws_open(path, O_CREAT | O_WRONLY | O_TRUNC, 0600); |
| 24 | + char buf[1024]; |
| 25 | + size_t s = size; |
| 26 | + |
| 27 | + if (fd < 0) |
| 28 | + return 1; |
| 29 | + |
| 30 | + memset(buf, 'A', sizeof(buf)); |
| 31 | + |
| 32 | + while (s) { |
| 33 | + size_t w = sizeof(buf); |
| 34 | + if (w > s) |
| 35 | + w = s; |
| 36 | + if (write(fd, buf, w) != (ssize_t)w) { |
| 37 | + close(fd); |
| 38 | + return 1; |
| 39 | + } |
| 40 | + s -= w; |
| 41 | + } |
| 42 | + |
| 43 | + close(fd); |
| 44 | + |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +int main(int argc, const char **argv) |
| 49 | +{ |
| 50 | + int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE; |
| 51 | + lws_dir_du_t du; |
| 52 | + int result = 0; |
| 53 | + |
| 54 | + lwsl_user("lws-api-test-dir\n"); |
| 55 | + lws_set_log_level(logs, NULL); |
| 56 | + lwsl_user("LWS API selftest: lws_dir du\n"); |
| 57 | + |
| 58 | + /* Create test directory structure */ |
| 59 | + mkdir("./test-dir", 0700); |
| 60 | + mkdir("./test-dir/subdir", 0700); |
| 61 | + |
| 62 | + if (create_file("./test-dir/file1", 10)) { |
| 63 | + lwsl_err("Failed to create file1\n"); |
| 64 | + result = 1; |
| 65 | + goto cleanup; |
| 66 | + } |
| 67 | + if (create_file("./test-dir/file2", 20)) { |
| 68 | + lwsl_err("Failed to create file2\n"); |
| 69 | + result = 1; |
| 70 | + goto cleanup; |
| 71 | + } |
| 72 | + if (create_file("./test-dir/subdir/file3", 30)) { |
| 73 | + lwsl_err("Failed to create file3\n"); |
| 74 | + result = 1; |
| 75 | + goto cleanup; |
| 76 | + } |
| 77 | + |
| 78 | + memset(&du, 0, sizeof(du)); |
| 79 | + if (!lws_dir("./test-dir", &du, lws_dir_du_cb)) { |
| 80 | + lwsl_err("lws_dir failed\n"); |
| 81 | + result = 1; |
| 82 | + goto cleanup; |
| 83 | + } |
| 84 | + |
| 85 | + lwsl_user("Total size: %llu, total files: %u\n", |
| 86 | + (unsigned long long)du.size_in_bytes, du.count_files); |
| 87 | + |
| 88 | + if (du.size_in_bytes != 60) { |
| 89 | + lwsl_err("size_in_bytes is %llu, expected 60\n", |
| 90 | + (unsigned long long)du.size_in_bytes); |
| 91 | + result = 1; |
| 92 | + } |
| 93 | + |
| 94 | + if (du.count_files != 3) { |
| 95 | + lwsl_err("count_files is %u, expected 3\n", du.count_files); |
| 96 | + result = 1; |
| 97 | + } |
| 98 | + |
| 99 | +cleanup: |
| 100 | + /* Clean up test directory structure */ |
| 101 | + lws_dir("./test-dir", NULL, lws_dir_rm_rf_cb); |
| 102 | + rmdir("./test-dir"); |
| 103 | + |
| 104 | + if (!result) |
| 105 | + lwsl_user("Completed successfully\n"); |
| 106 | + else |
| 107 | + lwsl_err("Failed\n"); |
| 108 | + |
| 109 | + return result; |
| 110 | +} |
0 commit comments