| 1 | #include <fs/fat.h> |
| 2 | #include <mem/alloc.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | struct fat_lookup_ctx { |
| 8 | const char *fname; |
| 9 | struct fat_dirent *found_dir; // if this remains NULL, we never found it |
| 10 | // no need for 'found' field |
| 11 | uint32_t index; |
| 12 | }; |
| 13 | |
| 14 | static bool fat_lookup_cb(struct fat_dirent *d, uint32_t indx, void *c) { |
| 15 | struct fat_lookup_ctx *ctx = c; |
| 16 | if (memcmp(d->name, ctx->fname, 11) == 0) { |
| 17 | ctx->found_dir = d; |
| 18 | ctx->index = indx; |
| 19 | return true; |
| 20 | } |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | struct fat_dirent *fat_lookup(struct fat_fs *fs, uint32_t cluster, |
| 25 | const char *f, uint32_t *out_index) { |
| 26 | char fmtname[11]; |
| 27 | fat_format_filename_83(name: f, out: fmtname); |
| 28 | struct fat_lookup_ctx ctx = { |
| 29 | .found_dir = NULL, .fname = fmtname, .index = 0}; |
| 30 | |
| 31 | fat_walk_cluster(fs, cluster, cb: fat_lookup_cb, ctx: &ctx); |
| 32 | if (out_index && ctx.found_dir) { |
| 33 | *out_index = ctx.index; |
| 34 | } |
| 35 | return ctx.found_dir; |
| 36 | } |
| 37 | |
| 38 | bool fat_contains(struct fat_fs *fs, uint32_t cluster, const char *f) { |
| 39 | struct fat_dirent *d = fat_lookup(fs, cluster, f, NULL); |
| 40 | if (d) { |
| 41 | kfree(d); |
| 42 | return true; |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |