testfilesystem.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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 int SDLCALL enum_callback(void *userdata, void *reserved, 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 -1;
  27. }
  28. if (SDL_GetPathInfo(fullpath, &info) < 0) {
  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) < 0) {
  43. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Enumeration failed!");
  44. }
  45. }
  46. }
  47. SDL_free(fullpath);
  48. return 1; /* keep going */
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. SDLTest_CommonState *state;
  53. char *pref_path;
  54. char *base_path;
  55. /* Initialize test framework */
  56. state = SDLTest_CommonCreateState(argv, 0);
  57. if (!state) {
  58. return 1;
  59. }
  60. /* Enable standard application logging */
  61. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  62. /* Parse commandline */
  63. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  64. return 1;
  65. }
  66. if (SDL_Init(0) == -1) {
  67. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
  68. return 1;
  69. }
  70. base_path = SDL_GetBasePath();
  71. if (!base_path) {
  72. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find base path: %s\n",
  73. SDL_GetError());
  74. } else {
  75. SDL_Log("base path: '%s'\n", base_path);
  76. }
  77. pref_path = SDL_GetPrefPath("libsdl", "test_filesystem");
  78. if (!pref_path) {
  79. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path: %s\n",
  80. SDL_GetError());
  81. } else {
  82. SDL_Log("pref path: '%s'\n", pref_path);
  83. SDL_free(pref_path);
  84. }
  85. pref_path = SDL_GetPrefPath(NULL, "test_filesystem");
  86. if (!pref_path) {
  87. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path without organization: %s\n",
  88. SDL_GetError());
  89. } else {
  90. SDL_Log("pref path: '%s'\n", pref_path);
  91. SDL_free(pref_path);
  92. }
  93. if (base_path) {
  94. if (SDL_EnumerateDirectory(base_path, enum_callback, NULL) < 0) {
  95. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Base path enumeration failed!");
  96. }
  97. /* !!! FIXME: put this in a subroutine and make it test more thoroughly (and put it in testautomation). */
  98. if (SDL_CreateDirectory("testfilesystem-test") == -1) {
  99. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test') failed: %s", SDL_GetError());
  100. } else if (SDL_CreateDirectory("testfilesystem-test/1") == -1) {
  101. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
  102. } else if (SDL_CreateDirectory("testfilesystem-test/1") == -1) { /* THIS SHOULD NOT FAIL! Making a directory that already exists should succeed here. */
  103. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
  104. } else if (SDL_RenamePath("testfilesystem-test/1", "testfilesystem-test/2") == -1) {
  105. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RenamePath('testfilesystem-test/1', 'testfilesystem-test/2') failed: %s", SDL_GetError());
  106. } else if (SDL_RemovePath("testfilesystem-test/2") == -1) {
  107. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/2') failed: %s", SDL_GetError());
  108. } else if (SDL_RemovePath("testfilesystem-test") == -1) {
  109. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test') failed: %s", SDL_GetError());
  110. } else if (SDL_RemovePath("testfilesystem-test") == -1) { /* THIS SHOULD NOT FAIL! Removing a directory that is already gone should succeed here. */
  111. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test') failed: %s", SDL_GetError());
  112. }
  113. }
  114. SDL_free(base_path);
  115. SDL_Quit();
  116. SDLTest_CommonDestroyState(state);
  117. return 0;
  118. }