SDL_sysfsops.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_FSOPS_POSIX)
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. /* System dependent filesystem routines */
  22. #include "../SDL_sysfilesystem.h"
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <dirent.h>
  27. #include <sys/stat.h>
  28. int SDL_SYS_EnumerateDirectory(const char *path, const char *dirname, SDL_EnumerateDirectoryCallback cb, void *userdata)
  29. {
  30. int retval = 1;
  31. DIR *dir = opendir(path);
  32. if (!dir) {
  33. return SDL_SetError("Can't open directory: %s", strerror(errno));
  34. }
  35. struct dirent *ent;
  36. while ((retval == 1) && ((ent = readdir(dir)) != NULL))
  37. {
  38. const char *name = ent->d_name;
  39. if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
  40. continue;
  41. }
  42. retval = cb(userdata, dirname, name);
  43. }
  44. closedir(dir);
  45. return retval;
  46. }
  47. int SDL_SYS_RemovePath(const char *path)
  48. {
  49. int rc = remove(path);
  50. if (rc < 0) {
  51. if (errno == ENOENT) {
  52. // It's already gone, this is a success
  53. return 0;
  54. }
  55. return SDL_SetError("Can't remove path: %s", strerror(errno));
  56. }
  57. return 0;
  58. }
  59. int SDL_SYS_RenamePath(const char *oldpath, const char *newpath)
  60. {
  61. if (rename(oldpath, newpath) < 0) {
  62. return SDL_SetError("Can't remove path: %s", strerror(errno));
  63. }
  64. return 0;
  65. }
  66. int SDL_SYS_CreateDirectory(const char *path)
  67. {
  68. const int rc = mkdir(path, 0770);
  69. if (rc < 0) {
  70. const int origerrno = errno;
  71. if (origerrno == EEXIST) {
  72. struct stat statbuf;
  73. if ((stat(path, &statbuf) == 0) && (S_ISDIR(statbuf.st_mode))) {
  74. return 0; // it already exists and it's a directory, consider it success.
  75. }
  76. }
  77. return SDL_SetError("Can't create directory: %s", strerror(origerrno));
  78. }
  79. return 0;
  80. }
  81. int SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
  82. {
  83. struct stat statbuf;
  84. const int rc = stat(path, &statbuf);
  85. if (rc < 0) {
  86. return SDL_SetError("Can't stat: %s", strerror(errno));
  87. } else if (S_ISREG(statbuf.st_mode)) {
  88. info->type = SDL_PATHTYPE_FILE;
  89. info->size = (Uint64) statbuf.st_size;
  90. } else if (S_ISDIR(statbuf.st_mode)) {
  91. info->type = SDL_PATHTYPE_DIRECTORY;
  92. info->size = 0;
  93. } else {
  94. info->type = SDL_PATHTYPE_OTHER;
  95. info->size = (Uint64) statbuf.st_size;
  96. }
  97. #if defined(HAVE_ST_MTIM)
  98. /* POSIX.1-2008 standard */
  99. info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctim.tv_sec) + statbuf.st_ctim.tv_nsec;
  100. info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtim.tv_sec) + statbuf.st_mtim.tv_nsec;
  101. info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atim.tv_sec) + statbuf.st_atim.tv_nsec;
  102. #elif defined(SDL_PLATFORM_APPLE)
  103. /* Apple platform stat structs use 'st_*timespec' naming. */
  104. info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctimespec.tv_sec) + statbuf.st_ctimespec.tv_nsec;
  105. info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtimespec.tv_sec) + statbuf.st_mtimespec.tv_nsec;
  106. info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atimespec.tv_sec) + statbuf.st_atimespec.tv_nsec;
  107. #else
  108. info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctime);
  109. info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtime);
  110. info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atime);
  111. #endif
  112. return 0;
  113. }
  114. #endif // SDL_FSOPS_POSIX