Added SDL_CopyFile() and SDL_CopyStorageFile()

Fixes https://github.com/libsdl-org/SDL/issues/9553
This commit is contained in:
Sam Lantinga
2024-07-21 19:32:16 -07:00
parent 128df75e05
commit 033c9c5951
14 changed files with 213 additions and 0 deletions

View File

@@ -43,6 +43,16 @@ int SDL_RenamePath(const char *oldpath, const char *newpath)
return SDL_SYS_RenamePath(oldpath, newpath);
}
int SDL_CopyFile(const char *oldpath, const char *newpath)
{
if (!oldpath) {
return SDL_InvalidParamError("oldpath");
} else if (!newpath) {
return SDL_InvalidParamError("newpath");
}
return SDL_SYS_CopyFile(oldpath, newpath);
}
int SDL_CreateDirectory(const char *path)
{
/* TODO: Recursively create subdirectories */

View File

@@ -30,6 +30,7 @@ extern char *SDL_SYS_GetUserFolder(SDL_Folder folder);
int SDL_SYS_EnumerateDirectory(const char *path, const char *dirname, SDL_EnumerateDirectoryCallback cb, void *userdata);
int SDL_SYS_RemovePath(const char *path);
int SDL_SYS_RenamePath(const char *oldpath, const char *newpath);
int SDL_SYS_CopyFile(const char *oldpath, const char *newpath);
int SDL_SYS_CreateDirectory(const char *path);
int SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info);

View File

@@ -43,6 +43,11 @@ int SDL_SYS_RenamePath(const char *oldpath, const char *newpath)
return SDL_Unsupported();
}
int SDL_SYS_CopyFile(const char *oldpath, const char *newpath)
{
return SDL_Unsupported();
}
int SDL_SYS_CreateDirectory(const char *path)
{
return SDL_Unsupported();

View File

@@ -79,6 +79,73 @@ int SDL_SYS_RenamePath(const char *oldpath, const char *newpath)
return 0;
}
int SDL_SYS_CopyFile(const char *oldpath, const char *newpath)
{
char *buffer = NULL;
char *tmppath = NULL;
SDL_IOStream *input = NULL;
SDL_IOStream *output = NULL;
const size_t maxlen = 4096;
size_t len;
int retval = -1;
if (SDL_asprintf(&tmppath, "%s.tmp", newpath) < 0) {
goto done;
}
input = SDL_IOFromFile(oldpath, "rb");
if (!input) {
goto done;
}
output = SDL_IOFromFile(tmppath, "wb");
if (!output) {
goto done;
}
buffer = (char *)SDL_malloc(maxlen);
if (!buffer) {
goto done;
}
while ((len = SDL_ReadIO(input, buffer, maxlen)) > 0) {
if (SDL_WriteIO(output, buffer, len) < len) {
goto done;
}
}
if (SDL_GetIOStatus(input) != SDL_IO_STATUS_EOF) {
goto done;
}
SDL_CloseIO(input);
input = NULL;
if (SDL_CloseIO(output) < 0) {
goto done;
}
output = NULL;
if (SDL_RenamePath(tmppath, newpath) < 0) {
SDL_RemovePath(tmppath);
goto done;
}
retval = 0;
done:
if (output) {
SDL_CloseIO(output);
SDL_RemovePath(tmppath);
}
if (input) {
SDL_CloseIO(input);
}
SDL_free(tmppath);
SDL_free(buffer);
return retval;
}
int SDL_SYS_CreateDirectory(const char *path)
{
const int rc = mkdir(path, 0770);

View File

@@ -131,6 +131,25 @@ int SDL_SYS_RenamePath(const char *oldpath, const char *newpath)
return !rc ? WIN_SetError("Couldn't rename path") : 0;
}
int SDL_SYS_CopyFile(const char *oldpath, const char *newpath)
{
WCHAR *woldpath = WIN_UTF8ToStringW(oldpath);
if (!woldpath) {
return -1;
}
WCHAR *wnewpath = WIN_UTF8ToStringW(newpath);
if (!wnewpath) {
SDL_free(woldpath);
return -1;
}
const BOOL rc = CopyFileW(woldpath, wnewpath, TRUE);
SDL_free(wnewpath);
SDL_free(woldpath);
return !rc ? WIN_SetError("Couldn't copy path") : 0;
}
int SDL_SYS_CreateDirectory(const char *path)
{
WCHAR *wpath = WIN_UTF8ToStringW(path);