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

@@ -55,7 +55,9 @@ SDL3_0.0.0 {
SDL_ConvertPixelsAndColorspace;
SDL_ConvertSurface;
SDL_ConvertSurfaceAndColorspace;
SDL_CopyFile;
SDL_CopyProperties;
SDL_CopyStorageFile;
SDL_CreateAudioStream;
SDL_CreateColorCursor;
SDL_CreateCondition;

View File

@@ -80,7 +80,9 @@
#define SDL_ConvertPixelsAndColorspace SDL_ConvertPixelsAndColorspace_REAL
#define SDL_ConvertSurface SDL_ConvertSurface_REAL
#define SDL_ConvertSurfaceAndColorspace SDL_ConvertSurfaceAndColorspace_REAL
#define SDL_CopyFile SDL_CopyFile_REAL
#define SDL_CopyProperties SDL_CopyProperties_REAL
#define SDL_CopyStorageFile SDL_CopyStorageFile_REAL
#define SDL_CreateAudioStream SDL_CreateAudioStream_REAL
#define SDL_CreateColorCursor SDL_CreateColorCursor_REAL
#define SDL_CreateCondition SDL_CreateCondition_REAL

View File

@@ -100,7 +100,9 @@ SDL_DYNAPI_PROC(int,SDL_ConvertPixels,(int a, int b, SDL_PixelFormat c, const vo
SDL_DYNAPI_PROC(int,SDL_ConvertPixelsAndColorspace,(int a, int b, SDL_PixelFormat c, SDL_Colorspace d, SDL_PropertiesID e, const void *f, int g, SDL_PixelFormat h, SDL_Colorspace i, SDL_PropertiesID j, void *k, int l),(a,b,c,d,e,f,g,h,i,j,k,l),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_ConvertSurface,(SDL_Surface *a, SDL_PixelFormat b),(a,b),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_ConvertSurfaceAndColorspace,(SDL_Surface *a, SDL_PixelFormat b, SDL_Palette *c, SDL_Colorspace d, SDL_PropertiesID e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(int,SDL_CopyFile,(const char *a, const char *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_CopyProperties,(SDL_PropertiesID a, SDL_PropertiesID b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_CopyStorageFile,(SDL_Storage *a, const char *b, const char *c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_AudioStream*,SDL_CreateAudioStream,(const SDL_AudioSpec *a, const SDL_AudioSpec *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_Cursor*,SDL_CreateColorCursor,(SDL_Surface *a, int b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_Condition*,SDL_CreateCondition,(void),(),return)

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);

View File

@@ -291,6 +291,24 @@ int SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char
return storage->iface.rename(storage->userdata, oldpath, newpath);
}
int SDL_CopyStorageFile(SDL_Storage *storage, const char *oldpath, const char *newpath)
{
CHECK_STORAGE_MAGIC()
if (!oldpath) {
return SDL_InvalidParamError("oldpath");
}
if (!newpath) {
return SDL_InvalidParamError("newpath");
}
if (!storage->iface.copy) {
return SDL_Unsupported();
}
return storage->iface.copy(storage->userdata, oldpath, newpath);
}
int SDL_GetStoragePathInfo(SDL_Storage *storage, const char *path, SDL_PathInfo *info)
{
SDL_PathInfo dummy;

View File

@@ -160,6 +160,21 @@ static int GENERIC_RenameStoragePath(void *userdata, const char *oldpath, const
return result;
}
static int GENERIC_CopyStorageFile(void *userdata, const char *oldpath, const char *newpath)
{
int result = -1;
char *fulloldpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, oldpath);
char *fullnewpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, newpath);
if (fulloldpath && fullnewpath) {
result = SDL_CopyFile(fulloldpath, fullnewpath);
}
SDL_free(fulloldpath);
SDL_free(fullnewpath);
return result;
}
static Uint64 GENERIC_GetStorageSpaceRemaining(void *userdata)
{
/* TODO: There's totally a way to query a folder root's quota... */
@@ -176,6 +191,7 @@ static const SDL_StorageInterface GENERIC_title_iface = {
NULL, /* mkdir */
NULL, /* remove */
NULL, /* rename */
NULL, /* copy */
NULL /* space_remaining */
};
@@ -219,6 +235,7 @@ static const SDL_StorageInterface GENERIC_user_iface = {
GENERIC_CreateStorageDirectory,
GENERIC_RemoveStoragePath,
GENERIC_RenameStoragePath,
GENERIC_CopyStorageFile,
GENERIC_GetStorageSpaceRemaining
};
@@ -257,6 +274,7 @@ static const SDL_StorageInterface GENERIC_file_iface = {
GENERIC_CreateStorageDirectory,
GENERIC_RemoveStoragePath,
GENERIC_RenameStoragePath,
GENERIC_CopyStorageFile,
GENERIC_GetStorageSpaceRemaining
};

View File

@@ -140,6 +140,7 @@ static const SDL_StorageInterface STEAM_user_iface = {
NULL, /* mkdir */
NULL, /* remove */
NULL, /* rename */
NULL, /* copy */
STEAM_GetStorageSpaceRemaining
};