FilesystemPosix.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "anki/util/Filesystem.h"
  2. #include "anki/util/Exception.h"
  3. #include "anki/util/Assert.h"
  4. #include <fstream>
  5. #include <cstring>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <ftw.h>
  9. #include <cerrno>
  10. namespace anki {
  11. //==============================================================================
  12. bool fileExists(const char* filename)
  13. {
  14. ANKI_ASSERT(filename);
  15. struct stat s;
  16. if(stat(filename, &s) == 0)
  17. {
  18. return S_ISREG(s.st_mode);
  19. }
  20. else
  21. {
  22. return false;
  23. }
  24. }
  25. //==============================================================================
  26. bool directoryExists(const char* filename)
  27. {
  28. ANKI_ASSERT(filename);
  29. struct stat s;
  30. if(stat(filename, &s) == 0)
  31. {
  32. return S_ISDIR(s.st_mode);
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. //==============================================================================
  40. static int rmDir(const char* fpath, const struct stat* sb, int /*typeflag*/,
  41. struct FTW* /*ftwbuf*/)
  42. {
  43. (void)sb;
  44. int rv = remove(fpath);
  45. if(rv)
  46. {
  47. throw ANKI_EXCEPTION(strerror(errno) + ": " + fpath);
  48. }
  49. return rv;
  50. }
  51. void removeDirectory(const char* dir)
  52. {
  53. if(nftw(dir, rmDir, 64, FTW_DEPTH | FTW_PHYS))
  54. {
  55. throw ANKI_EXCEPTION(strerror(errno) + ": " + dir);
  56. }
  57. }
  58. //==============================================================================
  59. void createDirectory(const char* dir)
  60. {
  61. if(directoryExists(dir))
  62. {
  63. return;
  64. }
  65. if(mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
  66. {
  67. throw ANKI_EXCEPTION(strerror(errno) + ": " + dir);
  68. }
  69. }
  70. //==============================================================================
  71. void toNativePath(const char* path)
  72. {
  73. ANKI_ASSERT(path);
  74. (void)path;
  75. }
  76. } // end namespace anki