1
0

fs.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "src/fs.h"
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <ftw.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define PATH_BUF_LEN 256
  10. void test_mkdirs(const char *temp_dir, const char *rel_path) {
  11. char abs_path[PATH_BUF_LEN];
  12. strcpy(abs_path, temp_dir);
  13. strcat(abs_path, "/");
  14. strcat(abs_path, rel_path);
  15. if (rgpgfs_fs_mkdirs(abs_path)) {
  16. fprintf(stderr, "rel_path = '%s'\n", rel_path);
  17. perror("rgpgfs_fs_mkdirs failed");
  18. exit(1);
  19. }
  20. FILE *f = fopen(abs_path, "w");
  21. assert(f);
  22. fclose(f);
  23. };
  24. int cleanup_rm_cb(const char *fpath, const struct stat *sb, int typeflag,
  25. struct FTW *ftwbuf) {
  26. if (remove(fpath)) {
  27. perror(fpath);
  28. }
  29. return 0;
  30. }
  31. int main() {
  32. char temp_dir[] = "/tmp/rgpgfs-tests-fs-XXXXXX";
  33. assert(mkdtemp(temp_dir));
  34. // printf("temp dir: %s\n", temp_dir);
  35. test_mkdirs(temp_dir, "file");
  36. test_mkdirs(temp_dir, "a/file");
  37. test_mkdirs(temp_dir, "a/b/file");
  38. test_mkdirs(temp_dir, "c/d/file");
  39. nftw(temp_dir, cleanup_rm_cb, 8, FTW_DEPTH);
  40. return 0;
  41. }