sdl3-src 3.4.4

Source code of the SDL 3 library
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2026 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"

#if defined(SDL_FSOPS_POSIX)

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// System dependent filesystem routines

#include "../SDL_sysfilesystem.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef SDL_PLATFORM_ANDROID
#include "../../core/android/SDL_android.h"
#endif


bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback cb, void *userdata)
{
    char *apath = NULL;  // absolute path (for Android, iOS, etc). Overrides `path`.

#if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_IOS)
    if (*path != '/') {
        #ifdef SDL_PLATFORM_ANDROID
        SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
        #elif defined(SDL_PLATFORM_IOS)
        char *base = SDL_GetPrefPath("", "");
        if (!base) {
            return false;
        }

        SDL_asprintf(&apath, "%s%s", base, path);
        SDL_free(base);
        #endif

        if (!apath) {
            return false;
        }
    }
#elif 0  // this is just for testing that `apath` works when you aren't on iOS or Android.
    if (*path != '/') {
        char *c = SDL_SYS_GetCurrentDirectory();
        SDL_asprintf(&apath, "%s%s", c, path);
        SDL_free(c);
        if (!apath) {
            return false;
        }
    }
#endif

    char *pathwithsep = NULL;
    int pathwithseplen = SDL_asprintf(&pathwithsep, "%s/", apath ? apath : path);
    const size_t extralen = apath ? (SDL_strlen(apath) - SDL_strlen(path)) : 0;
    SDL_free(apath);
    if ((pathwithseplen == -1) || (!pathwithsep)) {
        return false;
    }

    // trim down to a single path separator at the end, in case the caller added one or more.
    pathwithseplen--;
    while ((pathwithseplen > 0) && (pathwithsep[pathwithseplen - 1] == '/')) {
        pathwithsep[pathwithseplen--] = '\0';
    }

    DIR *dir = opendir(pathwithsep);
    if (!dir) {
#ifdef SDL_PLATFORM_ANDROID  // Maybe it's an asset...?
        const bool retval = Android_JNI_EnumerateAssetDirectory(pathwithsep + extralen, cb, userdata);
        SDL_free(pathwithsep);
        return retval;
#else
        SDL_free(pathwithsep);
        return SDL_SetError("Can't open directory: %s", strerror(errno));
#endif
    }

    SDL_EnumerationResult result = SDL_ENUM_CONTINUE;
    struct dirent *ent;
    while ((result == SDL_ENUM_CONTINUE) && ((ent = readdir(dir)) != NULL)) {
        const char *name = ent->d_name;
        if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
            continue;
        }
        result = cb(userdata, pathwithsep + extralen, name);
    }

    closedir(dir);

    SDL_free(pathwithsep);

    return (result != SDL_ENUM_FAILURE);
}

bool SDL_SYS_RemovePath(const char *path)
{
    int rc;

#ifdef SDL_PLATFORM_ANDROID
    if (*path == '/') {
        rc = remove(path);
    } else {
        char *apath = NULL;
        SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
        if (!apath) {
            return false;
        }
        rc = remove(apath);
        SDL_free(apath);
    }
#elif defined(SDL_PLATFORM_IOS)
    if (*path == '/') {
        rc = remove(path);
    } else {
        char *base = SDL_GetPrefPath("", "");
        if (!base) {
            return false;
        }

        char *apath = NULL;
        SDL_asprintf(&apath, "%s%s", base, path);
        SDL_free(base);
        if (!apath) {
            return false;
        }
        rc = remove(apath);
        SDL_free(apath);
    }
#else
    rc = remove(path);
#endif
    if (rc < 0) {
        if (errno == ENOENT) {
            // It's already gone, this is a success
            return true;
        }
        return SDL_SetError("Can't remove path: %s", strerror(errno));
    }
    return true;
}

bool SDL_SYS_RenamePath(const char *oldpath, const char *newpath)
{
    int rc;

#ifdef SDL_PLATFORM_ANDROID
    char *aoldpath = NULL;
    char *anewpath = NULL;
    if (*oldpath != '/') {
        SDL_asprintf(&aoldpath, "%s/%s", SDL_GetAndroidInternalStoragePath(), oldpath);
        if (!aoldpath) {
            return false;
        }
        oldpath = aoldpath;
    }
    if (*newpath != '/') {
        SDL_asprintf(&anewpath, "%s/%s", SDL_GetAndroidInternalStoragePath(), newpath);
        if (!anewpath) {
            SDL_free(aoldpath);
            return false;
        }
        newpath = anewpath;
    }
    rc = rename(oldpath, newpath);
    SDL_free(aoldpath);
    SDL_free(anewpath);
#elif defined(SDL_PLATFORM_IOS)
    char *base = NULL;
    if (*oldpath != '/' || *newpath != '/') {
        base = SDL_GetPrefPath("", "");
        if (!base) {
            return false;
        }
    }

    char *aoldpath = NULL;
    char *anewpath = NULL;
    if (*oldpath != '/') {
        SDL_asprintf(&aoldpath, "%s%s", base, oldpath);
        if (!aoldpath) {
            SDL_free(base);
            return false;
        }
        oldpath = aoldpath;
    }
    if (*newpath != '/') {
        SDL_asprintf(&anewpath, "%s%s", base, newpath);
        if (!anewpath) {
            SDL_free(base);
            SDL_free(aoldpath);
            return false;
        }
        newpath = anewpath;
    }
    rc = rename(oldpath, newpath);
    SDL_free(base);
    SDL_free(aoldpath);
    SDL_free(anewpath);
#else
    rc = rename(oldpath, newpath);
#endif
    if (rc < 0) {
        return SDL_SetError("Can't rename path: %s", strerror(errno));
    }
    return true;
}

bool SDL_SYS_CopyFile(const char *oldpath, const char *newpath)
{
    char *buffer = NULL;
    SDL_IOStream *input = NULL;
    SDL_IOStream *output = NULL;
    const size_t maxlen = 4096;
    size_t len;
    bool result = false;

    input = SDL_IOFromFile(oldpath, "rb");
    if (!input) {
        goto done;
    }

    output = SDL_IOFromFile(newpath, "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_FlushIO(output)) {
        goto done;
    }

    result = SDL_CloseIO(output);
    output = NULL;  // it's gone, even if it failed.

done:
    if (output) {
        SDL_CloseIO(output);
    }
    if (input) {
        SDL_CloseIO(input);
    }
    SDL_free(buffer);

    return result;
}

bool SDL_SYS_CreateDirectory(const char *path)
{
    int rc;

#ifdef SDL_PLATFORM_ANDROID
    if (*path == '/') {
        rc = mkdir(path, 0770);
    } else {
        char *apath = NULL;
        SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
        if (!apath) {
            return false;
        }
        rc = mkdir(apath, 0770);
        SDL_free(apath);
    }
#elif defined(SDL_PLATFORM_IOS)
    if (*path == '/') {
        rc = mkdir(path, 0770);
    } else {
        char *base = SDL_GetPrefPath("", "");
        if (!base) {
            return false;
        }

        char *apath = NULL;
        SDL_asprintf(&apath, "%s%s", base, path);
        SDL_free(base);
        if (!apath) {
            return false;
        }
        rc = mkdir(apath, 0770);
        SDL_free(apath);
    }
#else
    rc = mkdir(path, 0770);
#endif
    if (rc < 0) {
        const int origerrno = errno;
        if (origerrno == EEXIST) {
            struct stat statbuf;
            if ((stat(path, &statbuf) == 0) && (S_ISDIR(statbuf.st_mode))) {
                return true;  // it already exists and it's a directory, consider it success.
            }
        }
        return SDL_SetError("Can't create directory: %s", strerror(origerrno));
    }
    return true;
}

bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
{
    struct stat statbuf;
    int rc;

#ifdef SDL_PLATFORM_ANDROID
    if (*path == '/') {
        rc = stat(path, &statbuf);
    } else {
        char *apath = NULL;
        SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
        if (!apath) {
            return false;
        }
        rc = stat(apath, &statbuf);
        SDL_free(apath);
    }
    if (rc < 0) {
        return Android_JNI_GetAssetPathInfo(path, info);
    }
#elif defined(SDL_PLATFORM_IOS)
    if (*path == '/') {
        rc = stat(path, &statbuf);
    } else {
        char *base = SDL_GetPrefPath("", "");
        if (!base) {
            return false;
        }

        char *apath = NULL;
        SDL_asprintf(&apath, "%s%s", base, path);
        SDL_free(base);
        if (!apath) {
            return false;
        }
        rc = stat(apath, &statbuf);
        SDL_free(apath);

        if (rc < 0) {
            rc = stat(path, &statbuf);
        }
    }
#else
    rc = stat(path, &statbuf);
#endif
    if (rc < 0) {
        return SDL_SetError("Can't stat: %s", strerror(errno));
    } else if (S_ISREG(statbuf.st_mode)) {
        info->type = SDL_PATHTYPE_FILE;
        info->size = (Uint64) statbuf.st_size;
    } else if (S_ISDIR(statbuf.st_mode)) {
        info->type = SDL_PATHTYPE_DIRECTORY;
        info->size = 0;
    } else {
        info->type = SDL_PATHTYPE_OTHER;
        info->size = (Uint64) statbuf.st_size;
    }

#if defined(HAVE_ST_MTIM)
    // POSIX.1-2008 standard
    info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctim.tv_sec) + statbuf.st_ctim.tv_nsec;
    info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtim.tv_sec) + statbuf.st_mtim.tv_nsec;
    info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atim.tv_sec) + statbuf.st_atim.tv_nsec;
#elif defined(SDL_PLATFORM_APPLE)
    /* Apple platform stat structs use 'st_*timespec' naming. */
    info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctimespec.tv_sec) + statbuf.st_ctimespec.tv_nsec;
    info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtimespec.tv_sec) + statbuf.st_mtimespec.tv_nsec;
    info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atimespec.tv_sec) + statbuf.st_atimespec.tv_nsec;
#else
    info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctime);
    info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtime);
    info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atime);
#endif
    return true;
}

// Note that this is actually part of filesystem, not fsops, but everything that uses posix fsops uses this implementation, even with separate filesystem code.
char *SDL_SYS_GetCurrentDirectory(void)
{
    size_t buflen = 64;
    char *buf = NULL;

    while (true) {
        void *ptr = SDL_realloc(buf, buflen);
        if (!ptr) {
            SDL_free(buf);
            return NULL;
        }
        buf = (char *) ptr;

        if (getcwd(buf, buflen-1) != NULL) {
            break;  // we got it!
        }

        if (errno == ERANGE) {
            buflen *= 2;  // try again with a bigger buffer.
            continue;
        }

        SDL_free(buf);
        SDL_SetError("getcwd failed: %s", strerror(errno));
        return NULL;
    }

    // make sure there's a path separator at the end.
    SDL_assert(SDL_strlen(buf) < (buflen + 2));
    buflen = SDL_strlen(buf);
    if ((buflen == 0) || (buf[buflen-1] != '/')) {
        buf[buflen] = '/';
        buf[buflen + 1] = '\0';
    }

    return buf;
}

#endif // SDL_FSOPS_POSIX