|
| 1 | +/* |
| 2 | + * We modified musl-fts not to use fchdir() and we made FTS_NOCHDIR |
| 3 | + * the default behavior. This test is to make sure that the modified |
| 4 | + * musl-fts works as expected. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <fts.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <sys/stat.h> |
| 13 | +#include <errno.h> |
| 14 | + |
| 15 | +void __expect_next_ftsent(FTSENT *p, const char *path, short level, int info, const char *file, int line) { |
| 16 | + if (p == NULL) { |
| 17 | + printf("Error: fts_read() returned NULL at %s:%d\n", file, line); |
| 18 | + exit(1); |
| 19 | + } |
| 20 | + |
| 21 | + if (strcmp(p->fts_path, path) != 0) { |
| 22 | + printf("Error: expected path \"%s\", got \"%s\" at %s:%d\n", path, p->fts_path, file, line); |
| 23 | + exit(1); |
| 24 | + } |
| 25 | + |
| 26 | + if (p->fts_level != level) { |
| 27 | + printf("Error: expected level %d, got %d at %s:%d\n", level, p->fts_level, file, line); |
| 28 | + exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + if (p->fts_info != info) { |
| 32 | + printf("Error: expected info %d, got %d at %s:%d\n", info, p->fts_info, file, line); |
| 33 | + exit(1); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#define expect_next_ftsent(p, path, level, info) __expect_next_ftsent(p, path, level, info, __FILE__, __LINE__) |
| 38 | + |
| 39 | +static int compare_ents (const FTSENT **a, const FTSENT **b) { |
| 40 | + return strcmp((*a)->fts_name, (*b)->fts_name); |
| 41 | +} |
| 42 | + |
| 43 | +void touch(const char *path) { |
| 44 | + FILE *f = fopen(path, "w"); |
| 45 | + if (f == NULL) { |
| 46 | + printf("Error: fopen(%s) failed: %s\n", path, strerror(errno)); |
| 47 | + exit(1); |
| 48 | + } |
| 49 | + fclose(f); |
| 50 | +} |
| 51 | + |
| 52 | +void test_fts_info(int base_options) { |
| 53 | + FTS *ftsp; |
| 54 | + |
| 55 | + mkdir("t1", 0755); |
| 56 | + touch("t1/file1"); |
| 57 | + mkdir("t1/dir1", 0755); |
| 58 | + touch("t1/dir1/file2"); |
| 59 | + mkdir("t1/dir1/dir2", 0755); |
| 60 | + touch("t1/dir1/file3"); |
| 61 | + |
| 62 | + char *paths[] = {"./t1", NULL}; |
| 63 | + ftsp = fts_open(paths, base_options, compare_ents); |
| 64 | + if (ftsp == NULL) { |
| 65 | + printf("Error: fts_open(%s, %d) failed: %s\n", paths[0], base_options, strerror(errno)); |
| 66 | + exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + expect_next_ftsent(fts_read(ftsp), "./t1", 0, FTS_D); |
| 70 | + expect_next_ftsent(fts_read(ftsp), "./t1/dir1", 1, FTS_D); |
| 71 | + expect_next_ftsent(fts_read(ftsp), "./t1/dir1/dir2", 2, FTS_D); |
| 72 | + expect_next_ftsent(fts_read(ftsp), "./t1/dir1/dir2", 2, FTS_DP); |
| 73 | + expect_next_ftsent(fts_read(ftsp), "./t1/dir1/file2", 2, FTS_F); |
| 74 | + expect_next_ftsent(fts_read(ftsp), "./t1/dir1/file3", 2, FTS_F); |
| 75 | + expect_next_ftsent(fts_read(ftsp), "./t1/dir1", 1, FTS_DP); |
| 76 | + expect_next_ftsent(fts_read(ftsp), "./t1/file1", 1, FTS_F); |
| 77 | + expect_next_ftsent(fts_read(ftsp), "./t1", 0, FTS_DP); |
| 78 | + |
| 79 | + fts_close(ftsp); |
| 80 | +} |
| 81 | + |
| 82 | +void test_symlink_fts_info(int base_options) { |
| 83 | + FTS *ftsp; |
| 84 | + |
| 85 | + mkdir("t2", 0755); |
| 86 | + touch("t2/file1"); |
| 87 | + symlink("file1", "t2/symlink1"); |
| 88 | + symlink("nonexistent", "t2/broken_symlink1"); |
| 89 | + |
| 90 | + char *paths[] = {"./t2", NULL}; |
| 91 | + ftsp = fts_open(paths, base_options, compare_ents); |
| 92 | + if (ftsp == NULL) { |
| 93 | + printf("Error: fts_open(%s, %d) failed: %s\n", paths[0], base_options, strerror(errno)); |
| 94 | + exit(1); |
| 95 | + } |
| 96 | + |
| 97 | + expect_next_ftsent(fts_read(ftsp), "./t2", 0, FTS_D); |
| 98 | + expect_next_ftsent(fts_read(ftsp), "./t2/broken_symlink1", 1, FTS_SL); |
| 99 | + expect_next_ftsent(fts_read(ftsp), "./t2/file1", 1, FTS_F); |
| 100 | + expect_next_ftsent(fts_read(ftsp), "./t2/symlink1", 1, FTS_SL); |
| 101 | + expect_next_ftsent(fts_read(ftsp), "./t2", 0, FTS_DP); |
| 102 | + |
| 103 | + fts_close(ftsp); |
| 104 | + |
| 105 | + ftsp = fts_open(paths, base_options | FTS_LOGICAL, compare_ents); |
| 106 | + if (ftsp == NULL) { |
| 107 | + printf("Error: fts_open(%s, %d | FTS_LOGICAL) failed: %s\n", paths[0], base_options, strerror(errno)); |
| 108 | + exit(1); |
| 109 | + } |
| 110 | + |
| 111 | + expect_next_ftsent(fts_read(ftsp), "./t2", 0, FTS_D); |
| 112 | + // FTS_SLNONE should be returned for broken symlinks in FTS_LOGICAL mode |
| 113 | + expect_next_ftsent(fts_read(ftsp), "./t2/broken_symlink1", 1, FTS_SLNONE); |
| 114 | + expect_next_ftsent(fts_read(ftsp), "./t2/file1", 1, FTS_F); |
| 115 | + expect_next_ftsent(fts_read(ftsp), "./t2/symlink1", 1, FTS_F); |
| 116 | + expect_next_ftsent(fts_read(ftsp), "./t2", 0, FTS_DP); |
| 117 | +} |
| 118 | + |
| 119 | +void __expect_child_ftsent(FTSENT *p, const char *name, short level, int info, const char *file, int line) { |
| 120 | + if (p == NULL) { |
| 121 | + printf("Error: fts_children() returned NULL at %s:%d\n", file, line); |
| 122 | + exit(1); |
| 123 | + } |
| 124 | + |
| 125 | + // Check fts_name instead of fts_path because fts_children() doesn't set fts_path |
| 126 | + if (strcmp(p->fts_name, name) != 0) { |
| 127 | + printf("Error: expected name \"%s\", got \"%s\" at %s:%d\n", name, p->fts_name, file, line); |
| 128 | + exit(1); |
| 129 | + } |
| 130 | + |
| 131 | + if (p->fts_level != level) { |
| 132 | + printf("Error: expected level %d, got %d at %s:%d\n", level, p->fts_level, file, line); |
| 133 | + exit(1); |
| 134 | + } |
| 135 | + |
| 136 | + if (p->fts_info != info) { |
| 137 | + printf("Error: expected info %d, got %d at %s:%d\n", info, p->fts_info, file, line); |
| 138 | + exit(1); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +#define expect_child_ftsent(p, name, level, info) __expect_child_ftsent(p, name, level, info, __FILE__, __LINE__) |
| 143 | + |
| 144 | +void test_fts_children(int base_options) { |
| 145 | + FTS *ftsp; |
| 146 | + FTSENT *p; |
| 147 | + |
| 148 | + mkdir("t3", 0755); |
| 149 | + touch("t3/file1"); |
| 150 | + mkdir("t3/dir1", 0755); |
| 151 | + touch("t3/dir1/file2"); |
| 152 | + mkdir("t3/dir1/dir2", 0755); |
| 153 | + touch("t3/dir1/file3"); |
| 154 | + |
| 155 | + char *paths[] = {"./t3", NULL}; |
| 156 | + ftsp = fts_open(paths, base_options, compare_ents); |
| 157 | + if (ftsp == NULL) { |
| 158 | + printf("Error: fts_open(%s, %d) failed: %s\n", paths[0], base_options, strerror(errno)); |
| 159 | + exit(1); |
| 160 | + } |
| 161 | + |
| 162 | + FTSENT *t3 = fts_read(ftsp); |
| 163 | + expect_next_ftsent(t3, "./t3", 0, FTS_D); |
| 164 | + |
| 165 | + FTSENT *ents = fts_children(ftsp, 0); |
| 166 | + expect_child_ftsent(ents, "dir1", 1, FTS_D); |
| 167 | + expect_child_ftsent(ents->fts_link, "file1", 1, FTS_F); |
| 168 | + |
| 169 | + fts_close(ftsp); |
| 170 | +} |
| 171 | + |
| 172 | +int main(void) { |
| 173 | + int base_options_set[] = {FTS_PHYSICAL, FTS_NOCHDIR}; |
| 174 | + for (int i = 0; i < sizeof(base_options_set) / sizeof(base_options_set[0]); i++) { |
| 175 | + test_fts_info(base_options_set[i]); |
| 176 | + test_symlink_fts_info(base_options_set[i]); |
| 177 | + test_fts_children(base_options_set[i]); |
| 178 | + } |
| 179 | + return 0; |
| 180 | +} |
0 commit comments