1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#include "../SDL_sysstorage.h"
static char *GENERIC_INTERNAL_CreateFullPath(const char *base, const char *relative)
{
char *result = NULL;
SDL_asprintf(&result, "%s%s", base ? base : "", relative);
return result;
}
static bool GENERIC_CloseStorage(void *userdata)
{
SDL_free(userdata);
return true;
}
typedef struct GenericEnumerateData
{
size_t base_len;
SDL_EnumerateDirectoryCallback real_callback;
void *real_userdata;
} GenericEnumerateData;
static SDL_EnumerationResult SDLCALL GENERIC_EnumerateDirectory(void *userdata, const char *dirname, const char *fname)
{
// SDL_EnumerateDirectory will return the full path, so for Storage we
// can take the base directory and add its length to the dirname string,
// effectively trimming the root without having to strdup anything.
const GenericEnumerateData *wrap_data = (GenericEnumerateData *)userdata;
dirname += wrap_data->base_len; // skip the base, just return the part inside of the Storage.
#ifdef SDL_PLATFORM_WINDOWS
char *dirnamecpy = NULL;
const size_t slen = SDL_strlen(dirname);
if (slen && (dirname[slen - 1] == '\\')) {
dirnamecpy = SDL_strdup(dirname);
if (!dirnamecpy) {
return SDL_ENUM_FAILURE;
}
dirnamecpy[slen - 1] = '/'; // storage layer always uses '/' path separators.
dirname = dirnamecpy;
}
const SDL_EnumerationResult retval = wrap_data->real_callback(wrap_data->real_userdata, dirname, fname);
SDL_free(dirnamecpy);
return retval;
#else
return wrap_data->real_callback(wrap_data->real_userdata, dirname, fname);
#endif
}
static bool GENERIC_EnumerateStorageDirectory(void *userdata, const char *path, SDL_EnumerateDirectoryCallback callback, void *callback_userdata)
{
bool result = false;
GenericEnumerateData wrap_data;
char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
if (fullpath) {
wrap_data.base_len = SDL_strlen((char *)userdata);
wrap_data.real_callback = callback;
wrap_data.real_userdata = callback_userdata;
result = SDL_EnumerateDirectory(fullpath, GENERIC_EnumerateDirectory, &wrap_data);
SDL_free(fullpath);
}
return result;
}
static bool GENERIC_GetStoragePathInfo(void *userdata, const char *path, SDL_PathInfo *info)
{
bool result = false;
char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
if (fullpath) {
result = SDL_GetPathInfo(fullpath, info);
SDL_free(fullpath);
}
return result;
}
static bool GENERIC_ReadStorageFile(void *userdata, const char *path, void *destination, Uint64 length)
{
bool result = false;
if (length > SDL_SIZE_MAX) {
return SDL_SetError("Read size exceeds SDL_SIZE_MAX");
}
char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
if (fullpath) {
SDL_IOStream *stream = SDL_IOFromFile(fullpath, "rb");
if (stream) {
// FIXME: Should SDL_ReadIO use u64 now...?
if (SDL_ReadIO(stream, destination, (size_t)length) == length) {
result = true;
} else {
SDL_SetError("File length did not exactly match the destination length");
}
SDL_CloseIO(stream);
}
SDL_free(fullpath);
}
return result;
}
static bool GENERIC_WriteStorageFile(void *userdata, const char *path, const void *source, Uint64 length)
{
// TODO: Recursively create subdirectories with SDL_CreateDirectory
bool result = false;
if (length > SDL_SIZE_MAX) {
return SDL_SetError("Write size exceeds SDL_SIZE_MAX");
}
char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
if (fullpath) {
SDL_IOStream *stream = SDL_IOFromFile(fullpath, "wb");
if (stream) {
// FIXME: Should SDL_WriteIO use u64 now...?
if (SDL_WriteIO(stream, source, (size_t)length) == length) {
result = true;
} else {
SDL_SetError("Resulting file length did not exactly match the source length");
}
SDL_CloseIO(stream);
}
SDL_free(fullpath);
}
return result;
}
static bool GENERIC_CreateStorageDirectory(void *userdata, const char *path)
{
// TODO: Recursively create subdirectories with SDL_CreateDirectory
bool result = false;
char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
if (fullpath) {
result = SDL_CreateDirectory(fullpath);
SDL_free(fullpath);
}
return result;
}
static bool GENERIC_RemoveStoragePath(void *userdata, const char *path)
{
bool result = false;
char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
if (fullpath) {
result = SDL_RemovePath(fullpath);
SDL_free(fullpath);
}
return result;
}
static bool GENERIC_RenameStoragePath(void *userdata, const char *oldpath, const char *newpath)
{
bool result = false;
char *fulloldpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, oldpath);
char *fullnewpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, newpath);
if (fulloldpath && fullnewpath) {
result = SDL_RenamePath(fulloldpath, fullnewpath);
}
SDL_free(fulloldpath);
SDL_free(fullnewpath);
return result;
}
static bool GENERIC_CopyStorageFile(void *userdata, const char *oldpath, const char *newpath)
{
bool result = false;
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...
return SDL_MAX_UINT64;
}
static const SDL_StorageInterface GENERIC_title_iface = {
sizeof(SDL_StorageInterface),
GENERIC_CloseStorage,
NULL, // ready
GENERIC_EnumerateStorageDirectory,
GENERIC_GetStoragePathInfo,
GENERIC_ReadStorageFile,
NULL, // write_file
NULL, // mkdir
NULL, // remove
NULL, // rename
NULL, // copy
NULL // space_remaining
};
static SDL_Storage *GENERIC_Title_Create(const char *override, SDL_PropertiesID props)
{
SDL_Storage *result = NULL;
char *basepath = NULL;
if (override != NULL) {
// make sure override has a path separator at the end. If you're not on Windows and used '\\', that's on you.
const size_t slen = SDL_strlen(override);
const bool need_sep = (!slen || ((override[slen-1] != '/') && (override[slen-1] != '\\')));
if (SDL_asprintf(&basepath, "%s%s", override, need_sep ? "/" : "") == -1) {
return NULL;
}
} else {
const char *base = SDL_GetBasePath();
basepath = base ? SDL_strdup(base) : NULL;
}
if (basepath != NULL) {
result = SDL_OpenStorage(&GENERIC_title_iface, basepath);
if (result == NULL) {
SDL_free(basepath); // otherwise CloseStorage will free it.
}
}
return result;
}
TitleStorageBootStrap GENERIC_titlebootstrap = {
"generic",
"SDL generic title storage driver",
GENERIC_Title_Create
};
static const SDL_StorageInterface GENERIC_user_iface = {
sizeof(SDL_StorageInterface),
GENERIC_CloseStorage,
NULL, // ready
GENERIC_EnumerateStorageDirectory,
GENERIC_GetStoragePathInfo,
GENERIC_ReadStorageFile,
GENERIC_WriteStorageFile,
GENERIC_CreateStorageDirectory,
GENERIC_RemoveStoragePath,
GENERIC_RenameStoragePath,
GENERIC_CopyStorageFile,
GENERIC_GetStorageSpaceRemaining
};
static SDL_Storage *GENERIC_User_Create(const char *org, const char *app, SDL_PropertiesID props)
{
SDL_Storage *result;
char *prefpath = SDL_GetPrefPath(org, app);
if (prefpath == NULL) {
return NULL;
}
result = SDL_OpenStorage(&GENERIC_user_iface, prefpath);
if (result == NULL) {
SDL_free(prefpath); // otherwise CloseStorage will free it.
}
return result;
}
UserStorageBootStrap GENERIC_userbootstrap = {
"generic",
"SDL generic user storage driver",
GENERIC_User_Create
};
static const SDL_StorageInterface GENERIC_file_iface = {
sizeof(SDL_StorageInterface),
GENERIC_CloseStorage,
NULL, // ready
GENERIC_EnumerateStorageDirectory,
GENERIC_GetStoragePathInfo,
GENERIC_ReadStorageFile,
GENERIC_WriteStorageFile,
GENERIC_CreateStorageDirectory,
GENERIC_RemoveStoragePath,
GENERIC_RenameStoragePath,
GENERIC_CopyStorageFile,
GENERIC_GetStorageSpaceRemaining
};
SDL_Storage *GENERIC_OpenFileStorage(const char *path)
{
SDL_Storage *result;
size_t len = 0;
char *basepath = NULL;
if (path) {
len += SDL_strlen(path);
}
if (len > 0) {
#ifdef SDL_PLATFORM_WINDOWS
const bool appended_separator = (path[len-1] == '/') || (path[len-1] == '\\');
#else
const bool appended_separator = (path[len-1] == '/');
#endif
if (appended_separator) {
basepath = SDL_strdup(path);
if (!basepath) {
return NULL;
}
} else {
if (SDL_asprintf(&basepath, "%s/", path) < 0) {
return NULL;
}
}
}
result = SDL_OpenStorage(&GENERIC_file_iface, basepath);
if (result == NULL) {
SDL_free(basepath);
}
return result;
}
|