Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL_CreateDirectory(): Clear error message when it succeeds after first failing #11345

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Sackzement
Copy link
Contributor

@Sackzement Sackzement commented Oct 27, 2024

I noticed that sometimes SDL_CreateDirectory() sets an error message even if it returned true and directories were created.

This happens when creating a not yet existing directory tree.

Here is a small test case for ./testfilesystem:

diff

diff --git a/test/testfilesystem.c b/test/testfilesystem.c
index 3a068a0f2..281699b75 100644
--- a/test/testfilesystem.c
+++ b/test/testfilesystem.c
@@ -133,8 +133,16 @@ int main(int argc, char *argv[])
             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
         } else if (!SDL_CreateDirectory("testfilesystem-test/1")) {  /* THIS SHOULD NOT FAIL! Making a directory that already exists should succeed here. */
             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
-        } else if (!SDL_CreateDirectory("testfilesystem-test/3/4/5/6")) {  /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
+        } else if (SDL_ClearError(),
+                   SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "INFO : SDL_GetError(): %s", SDL_GetError()),
+                   false) {
+            /**/
+        } else if (SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "INFO : Calling SDL_CreateDirectory(\"testfilesystem-test/3/4/5/6\");"),
+                   !SDL_CreateDirectory("testfilesystem-test/3/4/5/6")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/3/4/5/6') failed: %s", SDL_GetError());
+        } else if (SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "INFO : SDL_GetError(): %s", SDL_GetError()),
+                  false) {
+            /**/
         } else if (!SDL_RemovePath("testfilesystem-test/3/4/5/6")) {  /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3/4/5/6') failed: %s", SDL_GetError());
         } else if (!SDL_RemovePath("testfilesystem-test/3/4/5")) {  /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */

Which then outputs this at the end:

GLOB[218]: 'CMakeFiles/testaudiostreamdynamicresample.dir/testaudiostreamdynamicresample.c.o'
GLOB[219]: 'CMakeFiles/testaudiostreamdynamicresample.dir/testaudiostreamdynamicresample.c.o.d'
INFO : SDL_GetError(): 
INFO : Calling SDL_CreateDirectory("testfilesystem-test/3/4/5/6");
INFO : SDL_GetError(): Can't create directory: No such file or directory

True is returned with a set error message when SDL_CreateDirectory() internally first fails, but then eventually succeeds after getting everything sorted with all directory parents.

This commit checks at the end of the function if it succeeded after first failing and clears the error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant