forked from Detanup01/gbe_fork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap.cpp
More file actions
672 lines (564 loc) · 21 KB
/
wrap.cpp
File metadata and controls
672 lines (564 loc) · 21 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/* Copyright (C) 2019 Mr Goldberg
This file is part of the Goldberg Emulator
The Goldberg Emulator is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
The Goldberg Emulator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the Goldberg Emulator; if not, see
<http://www.gnu.org/licenses/>. */
#include "common_helpers/os_detector.h"
#if defined(__LINUX__)
#define STEAM_API_FUNCTIONS_IMPL
#include "dll/base.h"
#include "dll/dll.h"
#define STEAM_PATH_CACHE_SIZE 4096
const char *STEAM_PATH = nullptr;
size_t STEAM_PATH_SIZE{};
// Returns a '/' terminated absolute path to the steam folder in user's home,
// root is returned if env home is not set
const char *get_steam_path()
{
char *home_path = getenv("HOME");
char steam_path[STEAM_PATH_CACHE_SIZE];
char *steam_realpath = nullptr;
// Build steam_path from home
int required_size = snprintf(steam_path, STEAM_PATH_CACHE_SIZE, "%s/.steam/steam", home_path);
// Allocate more space for steam_path if needed (required_size does not count terminator)
if (required_size > 0 && required_size >= STEAM_PATH_CACHE_SIZE) {
char *large_steam_path = (char *)malloc(sizeof(char) * (required_size + 1));
int check_size = snprintf(large_steam_path, required_size + 1, "%s/.steam/steam", home_path);
// Check that path fits this time
if (check_size == required_size) {
steam_realpath = realpath(large_steam_path, nullptr);
}
free(large_steam_path);
} else {
steam_realpath = realpath(steam_path, nullptr);
}
// Terminate path with a file separator
if (steam_realpath && *steam_realpath) {
size_t path_size = strlen(steam_realpath);
if (steam_realpath[path_size - 1] != *PATH_SEPARATOR) {
steam_realpath = (char *)realloc(steam_realpath, path_size + 2);
steam_realpath[path_size] = *PATH_SEPARATOR;
steam_realpath[path_size + 1] = 0;
}
} else {
// Failsafe to root
steam_realpath = strdup("/");
}
return steam_realpath;
}
// Fixes given path by navigating filesystem and lowering case to match
// existing entries on disk
bool match_path(char *path, int start, bool accept_same_case)
{
if (!path[start + 1]) {
return true;
}
// Snap to the next separator in path
int separator = start + 1;
while (path[separator] != *PATH_SEPARATOR && path[separator]) {
separator++;
}
bool is_last_component = path[separator] != *PATH_SEPARATOR;
char stored_char = path[separator];
path[separator] = 0;
bool path_accessible = access(path, F_OK) == 0;
path[separator] = stored_char;
if (!path_accessible || (!is_last_component && !match_path(path, separator, accept_same_case))) {
DIR *current_directory = nullptr;
int component = start + 1;
if (start) {
stored_char = path[start];
path[start] = 0;
current_directory = opendir(path);
path[start] = stored_char;
component = start + 1;
} else {
if (*path == *PATH_SEPARATOR) {
component = start + 1;
current_directory = opendir("/");
} else {
component = start;
current_directory = opendir(".");
}
}
// 0123456789012345678901234567890123456789
// path = /this/is/a/sample/path/to/file.txt
// ^^ ^
// ab c
// a. start = 10
// b. component = 11
// c. separator = 17
// current_directory = /this/is/a/
if (current_directory) {
dirent64 *entry = (dirent64 *)readdir64(current_directory);
while (entry) {
const char *entry_name = entry->d_name;
stored_char = path[separator];
path[separator] = 0;
// Fix current component if entry with similar name exists
if (!strcasecmp(&path[component], entry_name)) {
bool case_differs = strcmp(&path[component], entry_name) != 0;
path[separator] = stored_char;
if (case_differs) {
char *iterator = &path[component];
// Replace with entry name
while (*entry_name != *PATH_SEPARATOR && *entry_name) {
*(iterator++) = *(entry_name++);
}
// Fix next component
if (is_last_component || match_path(path, separator, accept_same_case)) {
closedir(current_directory);
return true;
}
}
} else {
path[separator] = stored_char;
}
entry = (dirent64 *)readdir64(current_directory);
}
}
if (current_directory) {
closedir(current_directory);
}
return accept_same_case && is_last_component;
}
return true;
}
// Tries to convert the given path to the preferred lower-cased version
const char *lowercase_path(const char *path, bool accept_same_case, bool stop_at_separator)
{
std::locale loc{};
char *path_lowercased = nullptr;
if (path && *path) {
// If file does not exist
if (access(path, F_OK)) {
// Make a copy of the path on which to work on
path_lowercased = strdup(path);
if (!path_lowercased) {
return nullptr;
}
// Load steam path if not done already
if (!STEAM_PATH) {
STEAM_PATH = get_steam_path();
STEAM_PATH_SIZE = strlen(STEAM_PATH);
}
char *lowercase_iterator = path_lowercased;
// Lowercase whole steam path if possible
bool has_steam_root = false;
if (!strncasecmp(path_lowercased, STEAM_PATH, STEAM_PATH_SIZE)) {
memcpy(path_lowercased, STEAM_PATH, STEAM_PATH_SIZE);
lowercase_iterator = &path_lowercased[STEAM_PATH_SIZE - 1];
has_steam_root = true;
}
// Lowercase rest of the path
char *iterator = lowercase_iterator;
while ((!stop_at_separator || *iterator != *PATH_SEPARATOR) && *iterator) {
*iterator = std::tolower(*iterator, loc);
iterator++;
}
// Check if we can access the lowered-case path
int error = access(path_lowercased, F_OK);
if (!error) {
// The new path is valid
return path_lowercased;
} else {
if (accept_same_case) {
const char *name_iterator = &path[lowercase_iterator - path_lowercased];
while (*lowercase_iterator) {
*(lowercase_iterator++) = *(name_iterator++);
}
}
// Retry accesing the file again and tweak the path if needed
if (match_path(path_lowercased, has_steam_root ? STEAM_PATH_SIZE - 1 : 0, accept_same_case)) {
return path_lowercased;
}
}
}
}
return path;
}
STEAMAPI_API FILE *__wrap_freopen(const char *path, const char *modes, FILE *stream)
{
PRINT_DEBUG("'%s' '%s' %p", path, modes, stream);
bool is_writable = strpbrk(modes, "wa+") != 0;
const char *path_lowercased = lowercase_path(path, is_writable, true);
FILE *result = freopen(path_lowercased, modes, stream);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API FILE *__wrap_fopen(const char *path, const char *modes)
{
PRINT_DEBUG("'%s' '%s'", path, modes);
bool is_writable = strpbrk(modes, "wa+") != 0;
const char *path_lowercased = lowercase_path(path, is_writable, true);
FILE *result = fopen(path_lowercased, modes);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API FILE *__wrap_fopen64(const char *path, const char *modes)
{
PRINT_DEBUG("'%s' '%s'", path, modes);
bool is_writable = strpbrk(modes, "wa+") != 0;
const char *path_lowercased = lowercase_path(path, is_writable, true);
FILE *result = fopen64(path_lowercased, modes);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_open(const char *path, int flags, mode_t mode)
{
PRINT_DEBUG("'%s' 0x%x 0x%x", path, flags, mode);
bool is_writable = flags & (X_OK | W_OK);
const char *path_lowercased = lowercase_path(path, is_writable, true);
int result = open(path_lowercased, flags, mode);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_open64(const char *path, int flags, mode_t mode)
{
PRINT_DEBUG("'%s' 0x%x 0x%x", path, flags, mode);
bool is_writable = flags & (X_OK | W_OK);
const char *path_lowercased = lowercase_path(path, is_writable, true);
int result = open64(path_lowercased, flags, mode);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_access(const char *path, int mode)
{
PRINT_DEBUG("'%s' 0x%x", path, mode);
const char *path_lowercased = lowercase_path(path, false, false);
int result = access(path_lowercased, mode);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap___xstat(int ver, const char * path, struct stat * stat_buf)
{
PRINT_DEBUG("ver: %i | '%s' %p", ver, path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = stat(path_lowercased, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap___xstat64(int ver, const char *path, struct stat64 *stat_buf)
{
PRINT_DEBUG("ver: %i | '%s' %p", ver, path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = stat64(path_lowercased, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap___lxstat(int ver, const char * path, struct stat * stat_buf)
{
PRINT_DEBUG("ver: %i | '%s' %p", ver, path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = lstat(path_lowercased, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap___lxstat64(int ver, const char *path, struct stat64 *stat_buf)
{
PRINT_DEBUG("ver: %i | '%s' %p", ver, path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = lstat64(path_lowercased, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_stat(const char * path, struct stat * stat_buf)
{
PRINT_DEBUG("'%s' %p", path, stat_buf);
return __wrap___xstat(3, path, stat_buf);
}
STEAMAPI_API int __wrap_stat64(const char *path, struct stat64 *stat_buf)
{
PRINT_DEBUG("'%s' %p", path, stat_buf);
return __wrap___xstat64(3, path, stat_buf);
}
STEAMAPI_API int __wrap_lstat(const char * path, struct stat * stat_buf)
{
PRINT_DEBUG("'%s' %p", path, stat_buf);
return __wrap___lxstat(3, path, stat_buf);
}
STEAMAPI_API int __wrap_statfs(const char *path, struct statfs *stat_buf)
{
PRINT_DEBUG("'%s' %p", path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = statfs(path, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_statfs64(const char *path, struct statfs64 *stat_buf)
{
PRINT_DEBUG("'%s' %p", path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = statfs64(path, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_statvfs(const char *path, struct statvfs *stat_buf)
{
PRINT_DEBUG("'%s' %p", path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = statvfs(path_lowercased, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_statvfs64(const char *path, struct statvfs64 *stat_buf)
{
PRINT_DEBUG("'%s' %p", path, stat_buf);
const char *path_lowercased = lowercase_path(path, false, false);
int result = statvfs64(path_lowercased, stat_buf);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_scandir(const char *path, struct dirent ***namelist, int (*sel)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **))
{
PRINT_DEBUG("'%s' %p %p %p", path, namelist, sel, compar);
const char *path_lowercased = lowercase_path(path, false, false);
int result = scandir(path_lowercased, namelist, sel, compar);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_scandir64(const char *path, struct dirent64 ***namelist, int (*sel)(const struct dirent64 *), int (*compar)(const struct dirent64 **, const struct dirent64 **))
{
PRINT_DEBUG("'%s' %p %p %p", path, namelist, sel, compar);
const char *path_lowercased = lowercase_path(path, false, false);
int result = scandir64(path_lowercased, namelist, sel, compar);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API DIR *__wrap_opendir(const char *path)
{
PRINT_DEBUG("'%s'", path);
const char *path_lowercased = lowercase_path(path, false, false);
DIR *result = opendir(path_lowercased);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_chmod(const char *path, mode_t mode)
{
PRINT_DEBUG("'%s' 0x%x", path, mode);
const char *path_lowercased = lowercase_path(path, false, false);
int result = chmod(path_lowercased, mode);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_chown(const char *path, uid_t owner, gid_t group)
{
PRINT_DEBUG("'%s' 0x%x 0x%x", path, owner, group);
const char *path_lowercased = lowercase_path(path, false, false);
int result = chown(path_lowercased, owner, group);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_lchown(const char *path, uid_t owner, gid_t group)
{
PRINT_DEBUG("'%s' 0x%x 0x%x", path, owner, group);
const char *path_lowercased = lowercase_path(path, false, false);
int result = lchown(path_lowercased, owner, group);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_symlink(const char *path1, const char *path2)
{
PRINT_DEBUG("'%s' '%s'", path1, path2);
const char *path_lowercased1 = lowercase_path(path1, true, true);
const char *path_lowercased2 = lowercase_path(path2, false, false);
int result = symlink(path_lowercased1, path_lowercased2);
if (path_lowercased1 != path1) {
free((void *)path_lowercased1);
}
if (path_lowercased2 != path2) {
free((void *)path_lowercased2);
}
return result;
}
STEAMAPI_API int __wrap_link(const char *path1, const char *path2)
{
PRINT_DEBUG("'%s' '%s'", path1, path2);
const char *path_lowercased1 = lowercase_path(path1, true, true);
const char *path_lowercased2 = lowercase_path(path2, false, false);
int result = link(path_lowercased1, path_lowercased2);
if (path_lowercased1 != path1) {
free((void *)path_lowercased1);
}
if (path_lowercased2 != path2) {
free((void *)path_lowercased2);
}
return result;
}
STEAMAPI_API int __wrap_mknod(const char *path, mode_t mode, dev_t dev)
{
PRINT_DEBUG("'%s' 0x%x %lu", path, mode, dev);
const char *path_lowercased = lowercase_path(path, true, true);
int result = mknod(path_lowercased, mode, dev);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_mount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data)
{
PRINT_DEBUG("'%s' -> '%s' | <%s> | 0x%x %p", source, target, filesystemtype, mountflags, data);
const char *source_lowercased = lowercase_path(source, false, false);
const char *target_lowercased = lowercase_path(target, false, false);
int result = mount(source_lowercased, target_lowercased, filesystemtype, mountflags, data);
if (source_lowercased != source) {
free((void *)source_lowercased);
}
if (target_lowercased != target) {
free((void *)target_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_unlink(const char *path)
{
PRINT_DEBUG("'%s'", path);
const char *path_lowercased = lowercase_path(path, false, false);
int result = unlink(path);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_mkfifo(const char *path, mode_t mode)
{
PRINT_DEBUG("'%s' 0x%x", path, mode);
const char *path_lowercased = lowercase_path(path, true, true);
int result = mkfifo(path, mode);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_rename(const char *old_name, const char *new_name)
{
PRINT_DEBUG("'%s' -> '%s'", old_name, new_name);
const char *old_name_lowercased = lowercase_path(old_name, true, true);
const char *new_name_lowercased = lowercase_path(new_name, false, false);
int result = rename(old_name_lowercased, new_name_lowercased);
if (old_name_lowercased != old_name) {
free((void *)old_name_lowercased);
}
if (new_name_lowercased != new_name) {
free((void *)new_name_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_utime(const char *path, const struct utimbuf *times)
{
PRINT_DEBUG("'%s'", path);
const char *path_lowercased = lowercase_path(path, false, false);
int result = utime(path_lowercased, times);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_utimes(const char *path, const struct timeval times[2])
{
PRINT_DEBUG("'%s'", path);
const char *path_lowercased = lowercase_path(path, false, false);
int result = utimes(path_lowercased, times);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_mkdir(const char *path, mode_t mode)
{
PRINT_DEBUG("'%s' 0x%x", path, mode);
const char *path_lowercased = lowercase_path(path, true, true);
int result = mkdir(path_lowercased, mode);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_rmdir(const char *path)
{
PRINT_DEBUG("'%s'", path);
const char *path_lowercased = lowercase_path(path, false, false);
int result = rmdir(path_lowercased);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API void *__wrap_dlopen(const char *path, int mode)
{
PRINT_DEBUG("'%s' 0x%x", path, mode);
const char *path_lowercased = lowercase_path(path, false, false);
void * result = dlopen(path_lowercased, mode);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API void *__wrap_dlmopen(Lmid_t lmid, const char *path, int flags)
{
PRINT_DEBUG("%lu '%s' 0x%x", lmid, path, flags);
const char *path_lowercased = lowercase_path(path, false, false);
void * result = dlmopen(lmid, path_lowercased, flags);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
STEAMAPI_API int __wrap_chdir(const char *path)
{
PRINT_DEBUG("'%s'", path);
const char *path_lowercased = lowercase_path(path, false, false);
int result = chdir(path_lowercased);
if (path_lowercased != path) {
free((void *)path_lowercased);
}
return result;
}
#endif