testfilesystem.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple test of filesystem functions. */
  11. #include <SDL3/SDL.h>
  12. #include <SDL3/SDL_main.h>
  13. #include <SDL3/SDL_test.h>
  14. static SDL_EnumerationResult SDLCALL enum_callback(void *userdata, const char *origdir, const char *fname)
  15. {
  16. SDL_PathInfo info;
  17. char *fullpath = NULL;
  18. /* you can use '/' for a path separator on Windows, but to make the log output look correct, we'll #ifdef this... */
  19. #ifdef SDL_PLATFORM_WINDOWS
  20. const char *pathsep = "\\";
  21. #else
  22. const char *pathsep = "/";
  23. #endif
  24. if (SDL_asprintf(&fullpath, "%s%s%s", origdir, *origdir ? pathsep : "", fname) < 0) {
  25. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  26. return SDL_ENUM_FAILURE;
  27. }
  28. if (!SDL_GetPathInfo(fullpath, &info)) {
  29. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't stat '%s': %s", fullpath, SDL_GetError());
  30. } else {
  31. const char *type;
  32. if (info.type == SDL_PATHTYPE_FILE) {
  33. type = "FILE";
  34. } else if (info.type == SDL_PATHTYPE_DIRECTORY) {
  35. type = "DIRECTORY";
  36. } else {
  37. type = "OTHER";
  38. }
  39. SDL_Log("%s (type=%s, size=%" SDL_PRIu64 ", create=%" SDL_PRIu64 ", mod=%" SDL_PRIu64 ", access=%" SDL_PRIu64 ")",
  40. fullpath, type, info.size, info.modify_time, info.create_time, info.access_time);
  41. if (info.type == SDL_PATHTYPE_DIRECTORY) {
  42. if (!SDL_EnumerateDirectory(fullpath, enum_callback, userdata)) {
  43. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Enumeration failed!");
  44. }
  45. }
  46. }
  47. SDL_free(fullpath);
  48. return SDL_ENUM_CONTINUE; /* keep going */
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. SDLTest_CommonState *state;
  53. char *pref_path;
  54. const char *base_path;
  55. /* Initialize test framework */
  56. state = SDLTest_CommonCreateState(argv, 0);
  57. if (!state) {
  58. return 1;
  59. }
  60. /* Parse commandline */
  61. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  62. return 1;
  63. }
  64. if (!SDL_Init(0)) {
  65. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
  66. return 1;
  67. }
  68. base_path = SDL_GetBasePath();
  69. if (!base_path) {
  70. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find base path: %s\n",
  71. SDL_GetError());
  72. } else {
  73. SDL_Log("base path: '%s'\n", base_path);
  74. }
  75. pref_path = SDL_GetPrefPath("libsdl", "test_filesystem");
  76. if (!pref_path) {
  77. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path: %s\n",
  78. SDL_GetError());
  79. } else {
  80. SDL_Log("pref path: '%s'\n", pref_path);
  81. }
  82. SDL_free(pref_path);
  83. pref_path = SDL_GetPrefPath(NULL, "test_filesystem");
  84. if (!pref_path) {
  85. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path without organization: %s\n",
  86. SDL_GetError());
  87. } else {
  88. SDL_Log("pref path: '%s'\n", pref_path);
  89. }
  90. SDL_free(pref_path);
  91. if (base_path) {
  92. char **globlist;
  93. SDL_IOStream *stream;
  94. const char *text = "foo\n";
  95. if (!SDL_EnumerateDirectory(base_path, enum_callback, NULL)) {
  96. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Base path enumeration failed!");
  97. }
  98. globlist = SDL_GlobDirectory(base_path, "*/test*/T?st*", SDL_GLOB_CASEINSENSITIVE, NULL);
  99. if (!globlist) {
  100. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Base path globbing failed!");
  101. } else {
  102. int i;
  103. for (i = 0; globlist[i]; i++) {
  104. SDL_Log("GLOB[%d]: '%s'", i, globlist[i]);
  105. }
  106. SDL_free(globlist);
  107. }
  108. /* !!! FIXME: put this in a subroutine and make it test more thoroughly (and put it in testautomation). */
  109. if (!SDL_CreateDirectory("testfilesystem-test")) {
  110. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test') failed: %s", SDL_GetError());
  111. } else if (!SDL_CreateDirectory("testfilesystem-test/1")) {
  112. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
  113. } else if (!SDL_CreateDirectory("testfilesystem-test/1")) { /* THIS SHOULD NOT FAIL! Making a directory that already exists should succeed here. */
  114. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
  115. } else if (!SDL_CreateDirectory("testfilesystem-test/3/4/5/6")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  116. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/3/4/5/6') failed: %s", SDL_GetError());
  117. } else if (!SDL_RemovePath("testfilesystem-test/3/4/5/6")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  118. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3/4/5/6') failed: %s", SDL_GetError());
  119. } else if (!SDL_RemovePath("testfilesystem-test/3/4/5")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  120. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3/4/5') failed: %s", SDL_GetError());
  121. } else if (!SDL_RemovePath("testfilesystem-test/3/4")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  122. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3/4') failed: %s", SDL_GetError());
  123. } else if (!SDL_RemovePath("testfilesystem-test/3")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  124. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3') failed: %s", SDL_GetError());
  125. } else if (!SDL_RenamePath("testfilesystem-test/1", "testfilesystem-test/2")) {
  126. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RenamePath('testfilesystem-test/1', 'testfilesystem-test/2') failed: %s", SDL_GetError());
  127. } else if (!SDL_RemovePath("testfilesystem-test/2")) {
  128. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/2') failed: %s", SDL_GetError());
  129. } else if (!SDL_RemovePath("testfilesystem-test")) {
  130. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test') failed: %s", SDL_GetError());
  131. } else if (!SDL_RemovePath("testfilesystem-test")) { /* THIS SHOULD NOT FAIL! Removing a directory that is already gone should succeed here. */
  132. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test') failed: %s", SDL_GetError());
  133. }
  134. stream = SDL_IOFromFile("testfilesystem-A", "wb");
  135. if (stream) {
  136. SDL_WriteIO(stream, text, SDL_strlen(text));
  137. SDL_CloseIO(stream);
  138. if (!SDL_RenamePath("testfilesystem-A", "testfilesystem-B")) {
  139. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RenamePath('testfilesystem-A', 'testfilesystem-B') failed: %s", SDL_GetError());
  140. } else if (!SDL_CopyFile("testfilesystem-B", "testfilesystem-A")) {
  141. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CopyFile('testfilesystem-B', 'testfilesystem-A') failed: %s", SDL_GetError());
  142. } else {
  143. size_t sizeA, sizeB;
  144. char *textA, *textB;
  145. textA = (char *)SDL_LoadFile("testfilesystem-A", &sizeA);
  146. if (!textA || sizeA != SDL_strlen(text) || SDL_strcmp(textA, text) != 0) {
  147. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Contents of testfilesystem-A didn't match, expected %s, got %s\n", text, textA);
  148. }
  149. SDL_free(textA);
  150. textB = (char *)SDL_LoadFile("testfilesystem-B", &sizeB);
  151. if (!textB || sizeB != SDL_strlen(text) || SDL_strcmp(textB, text) != 0) {
  152. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Contents of testfilesystem-B didn't match, expected %s, got %s\n", text, textB);
  153. }
  154. SDL_free(textB);
  155. }
  156. if (!SDL_RemovePath("testfilesystem-A")) {
  157. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-A') failed: %s", SDL_GetError());
  158. }
  159. if (!SDL_RemovePath("testfilesystem-B")) {
  160. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-B') failed: %s", SDL_GetError());
  161. }
  162. } else {
  163. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_IOFromFile('testfilesystem-A', 'w') failed: %s", SDL_GetError());
  164. }
  165. }
  166. SDL_Quit();
  167. SDLTest_CommonDestroyState(state);
  168. return 0;
  169. }