/
emu_msvcrt.cpp
2206 lines (2002 loc) · 54.9 KB
/
emu_msvcrt.cpp
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifndef _LINUX
#include <io.h>
#include <direct.h>
#include <process.h>
#else
#ifndef __APPLE__
#include <mntent.h>
#endif
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include "system.h" // for HAS_DVD_DRIVE
#ifdef HAS_DVD_DRIVE
#ifdef _LINUX
#include <sys/ioctl.h>
#ifndef __APPLE__
#include <linux/cdrom.h>
#else
#include <IOKit/storage/IODVDMediaBSDClient.h>
#endif
#endif
#endif
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#ifdef _LINUX
#include "PlatformDefs.h" // for __stat64
#endif
#include "Util.h"
#include "filesystem/SpecialProtocol.h"
#include "URL.h"
#include "filesystem/File.h"
#include "settings/GUISettings.h"
#include "FileItem.h"
#include "filesystem/Directory.h"
#include "emu_msvcrt.h"
#include "emu_dummy.h"
#include "emu_kernel32.h"
#include "util/EmuFileWrapper.h"
#include "utils/log.h"
#include "threads/SingleLock.h"
#ifndef _LINUX
#include "utils/CharsetConverter.h"
#include "utils/URIUtils.h"
#endif
using namespace std;
using namespace XFILE;
#if defined(_MSC_VER) && _MSC_VER < 1500
extern "C" {
__int64 __cdecl _ftelli64(FILE *);
int __cdecl _fseeki64(FILE *, __int64, int);
}
#endif
struct SDirData
{
CFileItemList items;
int curr_index;
struct dirent *last_entry;
SDirData()
{
curr_index = -1;
last_entry = NULL;
}
};
#define MAX_OPEN_DIRS 10
static SDirData vecDirsOpen[MAX_OPEN_DIRS];
bool bVecDirsInited = false;
#ifdef HAS_VIDEO_PLAYBACK
extern void update_cache_dialog(const char* tmp);
#endif
struct _env
{
const char* name;
char* value;
};
#define EMU_MAX_ENVIRONMENT_ITEMS 100
static char *dll__environ_imp[EMU_MAX_ENVIRONMENT_ITEMS + 1];
extern "C" char **dll__environ;
char **dll__environ = dll__environ_imp;
CCriticalSection dll_cs_environ;
#define dll_environ (*dll___p__environ()) /* pointer to environment table */
extern "C" void __stdcall init_emu_environ()
{
memset(dll__environ, 0, EMU_MAX_ENVIRONMENT_ITEMS + 1);
// python
#if defined(_WIN32)
// fill our array with the windows system vars
LPTSTR lpszVariable;
LPTCH lpvEnv;
lpvEnv = GetEnvironmentStrings();
if (lpvEnv != NULL)
{
lpszVariable = (LPTSTR) lpvEnv;
while (*lpszVariable)
{
dll_putenv(lpszVariable);
lpszVariable += lstrlen(lpszVariable) + 1;
}
FreeEnvironmentStrings(lpvEnv);
}
dll_putenv("OS=win32");
#elif defined(__APPLE__)
dll_putenv("OS=darwin");
#elif defined(_LINUX)
dll_putenv("OS=linux");
#else
dll_putenv("OS=unknown");
#endif
// check if we are running as real xbmc.app or just binary
if (!CUtil::GetFrameworksPath(true).IsEmpty())
{
// using external python, it's build looking for xxx/lib/python2.6
// so point it to frameworks which is where python2.6 is located
dll_putenv(string("PYTHONPATH=" + _P("special://frameworks")).c_str());
dll_putenv(string("PYTHONHOME=" + _P("special://frameworks")).c_str());
dll_putenv(string("PATH=.;" + _P("special://xbmc") + ";" + _P("special://frameworks")).c_str());
}
else
{
dll_putenv(string("PYTHONPATH=" + _P("special://xbmc/system/python/DLLs") + ";" + _P("special://xbmc/system/python/Lib")).c_str());
dll_putenv(string("PYTHONHOME=" + _P("special://xbmc/system/python")).c_str());
dll_putenv(string("PATH=.;" + _P("special://xbmc") + ";" + _P("special://xbmc/system/python")).c_str());
}
//dll_putenv("PYTHONCASEOK=1");
//dll_putenv("PYTHONDEBUG=1");
//dll_putenv("PYTHONVERBOSE=2"); // "1" for normal verbose, "2" for more verbose ?
dll_putenv("PYTHONOPTIMIZE=1");
//dll_putenv("PYTHONDUMPREFS=1");
//dll_putenv("THREADDEBUG=1");
//dll_putenv("PYTHONMALLOCSTATS=1");
//dll_putenv("PYTHONY2K=1");
dll_putenv("TEMP=special://temp/temp"); // for python tempdir
// libdvdnav
dll_putenv("DVDREAD_NOKEYS=1");
//dll_putenv("DVDREAD_VERBOSE=1");
//dll_putenv("DVDREAD_USE_DIRECT=1");
// libdvdcss
dll_putenv("DVDCSS_METHOD=key");
dll_putenv("DVDCSS_VERBOSE=3");
dll_putenv("DVDCSS_CACHE=special://masterprofile/cache");
}
extern "C" void __stdcall update_emu_environ()
{
// Use a proxy, if the GUI was configured as such
if (g_guiSettings.GetBool("network.usehttpproxy") &&
g_guiSettings.GetString("network.httpproxyserver") &&
g_guiSettings.GetString("network.httpproxyport"))
{
CStdString strProxy;
if (g_guiSettings.GetString("network.httpproxyusername") &&
g_guiSettings.GetString("network.httpproxypassword"))
{
strProxy.Format("%s:%s@", g_guiSettings.GetString("network.httpproxyusername").c_str(),
g_guiSettings.GetString("network.httpproxypassword").c_str());
}
strProxy += g_guiSettings.GetString("network.httpproxyserver");
strProxy += ":" + g_guiSettings.GetString("network.httpproxyport");
#ifdef _WIN32
pgwin32_putenv("HTTP_PROXY=http://" +strProxy.c_str());
pgwin32_putenv("HTTPS_PROXY=http://" +strProxy.c_str());
#else
setenv( "HTTP_PROXY", "http://" + strProxy, true );
setenv( "HTTPS_PROXY", "http://" + strProxy, true );
#endif
}
else
{
// is there a better way to delete an environment variable?
// this works but leaves the variable
dll_putenv( "HTTP_PROXY=" );
dll_putenv( "HTTPS_PROXY=" );
}
}
static int convert_fmode(const char* mode)
{
int iMode = O_BINARY;
if (strstr(mode, "r+"))
iMode |= O_RDWR;
else if (strchr(mode, 'r'))
iMode |= _O_RDONLY;
if (strstr(mode, "w+"))
iMode |= O_RDWR | _O_TRUNC;
else if (strchr(mode, 'w'))
iMode |= _O_WRONLY | O_CREAT;
return iMode;
}
#ifdef _WIN32
static void to_finddata64i32(_wfinddata64i32_t *wdata, _finddata64i32_t *data)
{
CStdString strname;
g_charsetConverter.wToUTF8(wdata->name, strname);
size_t size = sizeof(data->name) / sizeof(char);
strncpy(data->name, strname.c_str(), size);
if (size)
data->name[size - 1] = '\0';
data->attrib = wdata->attrib;
data->time_create = wdata->time_create;
data->time_access = wdata->time_access;
data->time_write = wdata->time_write;
data->size = wdata->size;
}
static void to_wfinddata64i32(_finddata64i32_t *data, _wfinddata64i32_t *wdata)
{
CStdStringW strwname;
g_charsetConverter.utf8ToW(data->name, strwname, false);
size_t size = sizeof(wdata->name) / sizeof(wchar_t);
wcsncpy(wdata->name, strwname.c_str(), size);
if (size)
wdata->name[size - 1] = '\0';
wdata->attrib = data->attrib;
wdata->time_create = data->time_create;
wdata->time_access = data->time_access;
wdata->time_write = data->time_write;
wdata->size = data->size;
}
#endif
extern "C"
{
void dll_sleep(unsigned long imSec)
{
Sleep(imSec);
}
// FIXME, XXX, !!!!!!
void dllReleaseAll( )
{
// close all open dirs...
if (bVecDirsInited)
{
for (int i=0;i < MAX_OPEN_DIRS; ++i)
{
vecDirsOpen[i].items.Clear();
}
bVecDirsInited = false;
}
}
void* dllmalloc(size_t size)
{
void* pBlock = malloc(size);
if (!pBlock)
{
CLog::Log(LOGSEVERE, "malloc %"PRIdS" bytes failed, crash imminent", size);
}
return pBlock;
}
void dllfree( void* pPtr )
{
free(pPtr);
}
void* dllcalloc(size_t num, size_t size)
{
void* pBlock = calloc(num, size);
if (!pBlock)
{
CLog::Log(LOGSEVERE, "calloc %"PRIdS" bytes failed, crash imminent", size);
}
return pBlock;
}
void* dllrealloc( void *memblock, size_t size )
{
void* pBlock = realloc(memblock, size);
if (!pBlock)
{
CLog::Log(LOGSEVERE, "realloc %"PRIdS" bytes failed, crash imminent", size);
}
return pBlock;
}
void dllexit(int iCode)
{
not_implement("msvcrt.dll fake function exit() called\n"); //warning
}
void dllabort()
{
not_implement("msvcrt.dll fake function abort() called\n"); //warning
}
void* dll__dllonexit(PFV input, PFV ** start, PFV ** end)
{
//ported from WINE code
PFV *tmp;
int len;
if (!start || !*start || !end || !*end)
{
//FIXME("bad table\n");
return NULL;
}
len = (*end - *start);
if (++len <= 0)
return NULL;
tmp = (PFV*) realloc (*start, len * sizeof(tmp) );
if (!tmp)
return NULL;
*start = tmp;
*end = tmp + len;
tmp[len - 1] = input;
return (void *)input;
//wrong handling, this function is used for register functions
//that called before exit use _initterm functions.
//dllReleaseAll( );
//return TRUE;
}
_onexit_t dll_onexit(_onexit_t func)
{
not_implement("msvcrt.dll fake function dll_onexit() called\n");
// register to dll unload list
// return func if succsesfully added to the dll unload list
return NULL;
}
int dllputs(const char* szLine)
{
if (!szLine[0]) return EOF;
if (szLine[strlen(szLine) - 1] != '\n')
CLog::Log(LOGDEBUG," msg: %s", szLine);
else
CLog::Log(LOGDEBUG," msg: %s\n", szLine);
// return a non negative value
return 0;
}
int dllprintf(const char *format, ...)
{
va_list va;
static char tmp[2048];
va_start(va, format);
_vsnprintf(tmp, 2048, format, va);
va_end(va);
tmp[2048 - 1] = 0;
CLog::Log(LOGDEBUG, " msg: %s", tmp);
return strlen(tmp);
}
char *dll_fullpath(char *absPath, const char *relPath, size_t maxLength)
{
unsigned int len = strlen(relPath);
if (len > maxLength && absPath != NULL) return NULL;
// dll has to make sure it uses the correct path for now
if (len > 1 && relPath[1] == ':')
{
if (absPath == NULL) absPath = dll_strdup(relPath);
else
{
strncpy(absPath, relPath, maxLength);
if (maxLength != 0)
absPath[maxLength-1] = '\0';
}
return absPath;
}
if (!strncmp(relPath, "\\Device\\Cdrom0", 14))
{
// needed?
if (absPath == NULL) absPath = strdup(relPath);
else
{
strncpy(absPath, relPath, maxLength);
if (maxLength != 0)
absPath[maxLength-1] = '\0';
}
return absPath;
}
not_implement("msvcrt.dll incomplete function _fullpath(...) called\n"); //warning
return NULL;
}
FILE* dll_popen(const char *command, const char *mode)
{
not_implement("msvcrt.dll fake function _popen(...) called\n"); //warning
return NULL;
}
int dll_pclose(FILE *stream)
{
not_implement("msvcrt.dll fake function _pclose(...) called\n"); //warning
return 0;
}
FILE* dll_fdopen(int fd, const char* mode)
{
EmuFileObject* o = g_emuFileWrapper.GetFileObjectByDescriptor(fd);
if (o)
{
if(!o->used)
return NULL;
int nmode = convert_fmode(mode);
if( (o->mode & nmode) != nmode)
CLog::Log(LOGWARNING, "dll_fdopen - mode 0x%x differs from fd mode 0x%x", nmode, o->mode);
return &o->file_emu;
}
else if (!IS_STD_DESCRIPTOR(fd))
{
// it might be something else than a file, or the file is not emulated
// let the operating system handle it
return _fdopen(fd, mode);
}
not_implement("msvcrt.dll incomplete function _fdopen(...) called\n");
return NULL;
}
int dll_open(const char* szFileName, int iMode)
{
char str[1024];
int size = sizeof(str);
// move to CFile classes
if (strncmp(szFileName, "\\Device\\Cdrom0", 14) == 0)
{
// replace "\\Device\\Cdrom0" with "D:"
strncpy(str, "D:", size);
if (size)
{
str[size-1] = '\0';
strncat(str, szFileName + 14, size - strlen(str));
}
}
else
{
strncpy(str, szFileName, size);
if (size)
str[size-1] = '\0';
}
CFile* pFile = new CFile();
bool bWrite = false;
if ((iMode & O_RDWR) || (iMode & O_WRONLY))
bWrite = true;
bool bOverwrite=false;
if ((iMode & _O_TRUNC) || (iMode & O_CREAT))
bOverwrite = true;
// currently always overwrites
bool bResult;
// We need to validate the path here as some calls from ie. libdvdnav
// or the python DLLs have malformed slashes on Win32 & Xbox
// (-> E:\test\VIDEO_TS/VIDEO_TS.BUP))
if (bWrite)
bResult = pFile->OpenForWrite(CUtil::ValidatePath(str), bOverwrite);
else
bResult = pFile->Open(CUtil::ValidatePath(str));
if (bResult)
{
EmuFileObject* object = g_emuFileWrapper.RegisterFileObject(pFile);
if (object == NULL)
{
pFile->Close();
delete pFile;
return -1;
}
object->mode = iMode;
return g_emuFileWrapper.GetDescriptorByStream(&object->file_emu);
}
delete pFile;
return -1;
}
FILE* dll_freopen(const char *path, const char *mode, FILE *stream)
{
if (g_emuFileWrapper.StreamIsEmulatedFile(stream))
{
dll_fclose(stream);
return dll_fopen(path, mode);
}
else if (!IS_STD_STREAM(stream))
{
// Translate the path
return freopen(_P(path).c_str(), mode, stream);
}
// error
// close stream and return NULL
dll_fclose(stream);
return NULL;
}
int dll_read(int fd, void* buffer, unsigned int uiSize)
{
CFile* pFile = g_emuFileWrapper.GetFileXbmcByDescriptor(fd);
if (pFile != NULL)
{
return pFile->Read(buffer, uiSize);
}
else if (!IS_STD_DESCRIPTOR(fd))
{
// it might be something else than a file, or the file is not emulated
// let the operating system handle it
return read(fd, buffer, uiSize);
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return -1;
}
int dll_write(int fd, const void* buffer, unsigned int uiSize)
{
CFile* pFile = g_emuFileWrapper.GetFileXbmcByDescriptor(fd);
if (pFile != NULL)
{
return pFile->Write(buffer, uiSize);
}
else if (!IS_STD_DESCRIPTOR(fd))
{
// it might be something else than a file, or the file is not emulated
// let the operating system handle it
return write(fd, buffer, uiSize);
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return -1;
}
int dll_fstat64(int fd, struct __stat64 *buf)
{
CFile* pFile = g_emuFileWrapper.GetFileXbmcByDescriptor(fd);
if (pFile != NULL)
return pFile->Stat(buf);
else if (IS_STD_DESCRIPTOR(fd))
return _fstat64(fd, buf);
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return -1;
}
int dll_fstatvfs64(int fd, struct statvfs64 *buf)
{
not_implement("msvcrt.dll incomplete function fstatvfs64(...) called\n");
return -1;
}
int dll_close(int fd)
{
CFile* pFile = g_emuFileWrapper.GetFileXbmcByDescriptor(fd);
if (pFile != NULL)
{
g_emuFileWrapper.UnRegisterFileObjectByDescriptor(fd);
pFile->Close();
delete pFile;
return 0;
}
else if (!IS_STD_DESCRIPTOR(fd))
{
// it might be something else than a file, or the file is not emulated
// let the operating system handle it
return close(fd);
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return -1;
}
__off64_t dll_lseeki64(int fd, __off64_t lPos, int iWhence)
{
CFile* pFile = g_emuFileWrapper.GetFileXbmcByDescriptor(fd);
if (pFile != NULL)
{
lPos = pFile->Seek(lPos, iWhence);
return lPos;
}
else if (!IS_STD_DESCRIPTOR(fd))
{
// it might be something else than a file, or the file is not emulated
// let the operating system handle it
// not supported: return lseeki64(fd, lPos, iWhence);
CLog::Log(LOGWARNING, "msvcrt.dll: dll_lseeki64 called, TODO: add 'int64 -> long' type checking"); //warning
return (__int64)lseek(fd, (long)lPos, iWhence);
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return (__int64)-1;
}
__off_t dll_lseek(int fd, __off_t lPos, int iWhence)
{
if (g_emuFileWrapper.DescriptorIsEmulatedFile(fd))
{
return (__off_t)dll_lseeki64(fd, (__off_t)lPos, iWhence);
}
else if (!IS_STD_DESCRIPTOR(fd))
{
// it might be something else than a file, or the file is not emulated
// let the operating system handle it
return lseek(fd, lPos, iWhence);
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return -1;
}
void dll_rewind(FILE* stream)
{
int fd = g_emuFileWrapper.GetDescriptorByStream(stream);
if (fd >= 0)
{
dll_lseeki64(fd, 0, SEEK_SET);
}
else if (!IS_STD_STREAM(stream))
{
// it might be something else than a file, let the operating system handle it
rewind(stream);
}
else
{
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
}
}
//---------------------------------------------------------------------------------------------------------
void dll_flockfile(FILE *stream)
{
int fd = g_emuFileWrapper.GetDescriptorByStream(stream);
if (fd >= 0)
{
g_emuFileWrapper.LockFileObjectByDescriptor(fd);
return;
}
else if (!IS_STD_STREAM(stream))
{
// it might be something else than a file, let the operating system handle it
#ifdef _LINUX
flockfile(stream);
return;
#else
CLog::Log(LOGERROR, "%s: flockfile not available on non-linux platforms", __FUNCTION__);
#endif
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
}
int dll_ftrylockfile(FILE *stream)
{
int fd = g_emuFileWrapper.GetDescriptorByStream(stream);
if (fd >= 0)
{
if (g_emuFileWrapper.TryLockFileObjectByDescriptor(fd))
return 0;
return -1;
}
else if (!IS_STD_STREAM(stream))
{
// it might be something else than a file, let the operating system handle it
#ifdef _LINUX
return ftrylockfile(stream);
#else
CLog::Log(LOGERROR, "%s: ftrylockfile not available on non-linux platforms", __FUNCTION__);
#endif
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return -1;
}
void dll_funlockfile(FILE *stream)
{
int fd = g_emuFileWrapper.GetDescriptorByStream(stream);
if (fd >= 0)
{
g_emuFileWrapper.UnlockFileObjectByDescriptor(fd);
return;
}
else if (!IS_STD_STREAM(stream))
{
// it might be something else than a file, let the operating system handle it
#ifdef _LINUX
funlockfile(stream);
return;
#else
CLog::Log(LOGERROR, "%s: funlockfile not available on non-linux platforms", __FUNCTION__);
#endif
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
}
int dll_fclose(FILE * stream)
{
int fd = g_emuFileWrapper.GetDescriptorByStream(stream);
if (fd >= 0)
{
return dll_close(fd);
}
else if (!IS_STD_STREAM(stream))
{
// it might be something else than a file, let the operating system handle it
return fclose(stream);
}
CLog::Log(LOGERROR, "%s emulated function failed", __FUNCTION__);
return EOF;
}
#ifndef _LINUX
// should be moved to CFile classes
intptr_t dll_findfirst(const char *file, struct _finddata_t *data)
{
struct _finddata64i32_t data64i32;
intptr_t ret = dll_findfirst64i32(file, &data64i32);
if (ret != -1)
{
int size = sizeof(data->name);
strncpy(data->name, data64i32.name, size);
if (size)
data->name[size - 1] = '\0';
data->size = (_fsize_t)data64i32.size;
data->time_write = (time_t)data64i32.time_write;
data->time_access = (time_t)data64i32.time_access;
}
return ret;
}
intptr_t dll_findfirst64i32(const char *file, struct _finddata64i32_t *data)
{
char str[1024];
int size = sizeof(str);
CURL url(_P(file));
if (url.IsLocal())
{
// move to CFile classes
if (strncmp(file, "\\Device\\Cdrom0", 14) == 0)
{
// replace "\\Device\\Cdrom0" with "D:"
strncpy(str, "D:", size);
if (size)
{
str[size - 1] = '\0';
strncat(str, file + 14, size - strlen(str));
}
}
else
{
strncpy(str, file, size);
if (size)
str[size - 1] = '\0';
}
// Make sure the slashes are correct & translate the path
struct _wfinddata64i32_t wdata;
CStdStringW strwfile;
g_charsetConverter.utf8ToW(CUtil::ValidatePath(_P(str)), strwfile, false);
intptr_t ret = _wfindfirst64i32(strwfile.c_str(), &wdata);
if (ret != -1)
to_finddata64i32(&wdata, data);
return ret;
}
// non-local files. handle through IDirectory-class - only supports '*.bah' or '*.*'
CStdString strURL(file);
CStdString strMask;
if (url.GetFileName().Find("*.*") != string::npos)
{
CStdString strReplaced = url.GetFileName();
strReplaced.Replace("*.*","");
url.SetFileName(strReplaced);
}
else if (url.GetFileName().Find("*.") != string::npos)
{
URIUtils::GetExtension(url.GetFileName(),strMask);
url.SetFileName(url.GetFileName().Left(url.GetFileName().Find("*.")));
}
int iDirSlot=0; // locate next free directory
while ((vecDirsOpen[iDirSlot].curr_index != -1) && (iDirSlot<MAX_OPEN_DIRS)) iDirSlot++;
if (iDirSlot >= MAX_OPEN_DIRS)
return -1; // no free slots
if (url.GetProtocol().Equals("filereader"))
{
CURL url2(url.GetFileName());
url = url2;
}
CStdString fName = url.GetFileName();
url.SetFileName("");
strURL = url.Get();
bVecDirsInited = true;
vecDirsOpen[iDirSlot].items.Clear();
XFILE::CDirectory::GetDirectory(strURL, vecDirsOpen[iDirSlot].items, strMask);
if (vecDirsOpen[iDirSlot].items.Size())
{
int size = sizeof(data->name);
strncpy(data->name,vecDirsOpen[iDirSlot].items[0]->GetLabel().c_str(), size);
if (size)
data->name[size - 1] = '\0';
data->size = static_cast<_fsize_t>(vecDirsOpen[iDirSlot].items[0]->m_dwSize);
data->time_write = 0;
data->time_access = 0;
vecDirsOpen[iDirSlot].curr_index = 0;
return (intptr_t)&vecDirsOpen[iDirSlot];
}
vecDirsOpen[iDirSlot].curr_index = -1;
return -1; // whatever != NULL
}
// should be moved to CFile classes
int dll_findnext(intptr_t f, _finddata_t* data)
{
struct _finddata64i32_t data64i32;
int ret = dll_findnext64i32(f, &data64i32);
if (ret == 0)
{
int size = sizeof(data->name);
strncpy(data->name, data64i32.name, size);
if (size)
data->name[size - 1] = '\0';
data->size = (_fsize_t)data64i32.size;
data->time_write = (time_t)data64i32.time_write;
data->time_access = (time_t)data64i32.time_access;
}
return ret;
}
int dll_findnext64i32(intptr_t f, _finddata64i32_t* data)
{
int found = MAX_OPEN_DIRS;
for (int i = 0; i < MAX_OPEN_DIRS; i++)
{
if (f == (intptr_t)&vecDirsOpen[i] && vecDirsOpen[i].curr_index != -1)
{
found = i;
break;
}
}
if (found >= MAX_OPEN_DIRS)
{
struct _wfinddata64i32_t wdata;
to_wfinddata64i32(data, &wdata);
intptr_t ret = _wfindnext64i32(f, &wdata); // local dir
if (ret != -1)
to_finddata64i32(&wdata, data);
return ret;
}
// we have a valid data struture. get next item!
int iItem = vecDirsOpen[found].curr_index;
if (iItem+1 < vecDirsOpen[found].items.Size()) // we have a winner!
{
int size = sizeof(data->name);
strncpy(data->name,vecDirsOpen[found].items[iItem+1]->GetLabel().c_str(), size);
if (size)
data->name[size - 1] = '\0';
data->size = static_cast<_fsize_t>(vecDirsOpen[found].items[iItem+1]->m_dwSize);
vecDirsOpen[found].curr_index++;
return 0;
}
vecDirsOpen[found].items.Clear();
return -1;
}
int dll_findclose(intptr_t handle)
{
int found = MAX_OPEN_DIRS;
for (int i = 0; i < MAX_OPEN_DIRS; i++)
{
if (handle == (intptr_t)&vecDirsOpen[i] && vecDirsOpen[i].curr_index != -1)
{
found = i;
break;
}
}
if (found >= MAX_OPEN_DIRS)
return _findclose(handle);
vecDirsOpen[found].items.Clear();
vecDirsOpen[found].curr_index = -1;
return 0;
}
void dll__security_error_handler(int code, void *data)
{
//NOTE: __security_error_handler has been removed in VS2005 and up
CLog::Log(LOGERROR, "security_error, code %i", code);
}
#endif
DIR *dll_opendir(const char *file)
{
CURL url(_P(file));
if (url.IsLocal())
{ // Make sure the slashes are correct & translate the path
return opendir(CUtil::ValidatePath(url.Get().c_str()));
}
// locate next free directory
int iDirSlot=0;
while ((vecDirsOpen[iDirSlot].curr_index != -1) && (iDirSlot<MAX_OPEN_DIRS)) iDirSlot++;
if (iDirSlot >= MAX_OPEN_DIRS)
{
CLog::Log(LOGDEBUG, "Dll: Max open dirs reached");
return NULL; // no free slots
}
if (url.GetProtocol().Equals("filereader"))
{
CURL url2(url.GetFileName());
url = url2;
}
bVecDirsInited = true;
vecDirsOpen[iDirSlot].items.Clear();
if (XFILE::CDirectory::GetDirectory(url.Get(), vecDirsOpen[iDirSlot].items))
{
vecDirsOpen[iDirSlot].curr_index = 0;
return (DIR *)&vecDirsOpen[iDirSlot];
}
else
return NULL;
}
struct dirent *dll_readdir(DIR *dirp)
{
if (!dirp)
return NULL;
bool emulated(false);
for (int i = 0; i < MAX_OPEN_DIRS; i++)
{
if (dirp == (DIR*)&vecDirsOpen[i])
{
emulated = true;
break;
}
}
if (!emulated)
return readdir(dirp); // local dir
// dirp is actually a SDirData*
SDirData* dirData = (SDirData*)dirp;
if (dirData->last_entry)
free(dirData->last_entry);
struct dirent *entry = NULL;
entry = (dirent*) malloc(sizeof(*entry));
if (dirData->curr_index < dirData->items.Size() + 2)
{ // simulate the '.' and '..' dir entries
if (dirData->curr_index == 0)
strncpy(entry->d_name, ".\0", 2);
else if (dirData->curr_index == 1)
strncpy(entry->d_name, "..\0", 3);
else
{
strncpy(entry->d_name, dirData->items[dirData->curr_index - 2]->GetLabel().c_str(), sizeof(entry->d_name));
entry->d_name[sizeof(entry->d_name)-1] = '\0'; // null-terminate any truncated paths
}
dirData->last_entry = entry;
dirData->curr_index++;
return entry;