filesystem.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "filesystem.h"
  2. #if _WIN32
  3. #include <windows.h>
  4. #include "../../../libc/native/libc.h"
  5. #elif __APPLE__
  6. #include <mach-o/dyld.h>
  7. #include <sys/syslimits.h>
  8. #include <limits.h>
  9. #include <dirent.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include <copyfile.h>
  14. #elif __linux
  15. #include <limits.h>
  16. #include <dirent.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #else
  21. #include <limits.h>
  22. #include <dirent.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #endif
  27. namespace bbFileSystem{
  28. bbString _appDir;
  29. bbString _appPath;
  30. bbArray<bbString> _appArgs;
  31. struct GCRoot : public bbGCRoot{
  32. void gcMark(){
  33. bbGCMark( _appArgs );
  34. }
  35. };
  36. GCRoot root;
  37. void init(){
  38. static bool done;
  39. if( done ) return;
  40. done=true;
  41. _appArgs=bbArray<bbString>( bb_argc );
  42. for( int i=0;i<bb_argc;++i ) _appArgs[i]=bbString( bb_argv[i] );
  43. #if _WIN32
  44. WCHAR buf[MAX_PATH];
  45. GetModuleFileNameW( GetModuleHandleW(0),buf,MAX_PATH );
  46. buf[MAX_PATH-1]=0;
  47. for( WCHAR *p=buf;*p;++p ) if( *p=='\\' ) *p='/';
  48. _appPath=bbString( buf );
  49. #elif __APPLE__
  50. char buf[PATH_MAX];
  51. uint32_t size=sizeof( buf );
  52. _NSGetExecutablePath( buf,&size );
  53. buf[PATH_MAX-1]=0;
  54. _appPath=bbString( buf );
  55. #elif __linux
  56. pid_t pid=getpid();
  57. char lnk[PATH_MAX];
  58. char buf[PATH_MAX];
  59. sprintf( lnk,"/proc/%i/exe",pid );
  60. int i=readlink( lnk,buf,PATH_MAX );
  61. if( i>0 && i<PATH_MAX ){
  62. buf[i]=0;
  63. _appPath=bbString( buf );
  64. }
  65. #else
  66. _appPath="/";
  67. #endif
  68. int e=_appPath.findLast( "/" );
  69. if( e!=-1 ){
  70. _appDir=_appPath.slice( 0,e+1 );
  71. }else{
  72. _appDir=_appPath;
  73. }
  74. }
  75. bbString appDir(){
  76. init();
  77. return _appDir;
  78. }
  79. bbString appPath(){
  80. init();
  81. return _appPath;
  82. }
  83. bbArray<bbString> appArgs(){
  84. init();
  85. return _appArgs;
  86. }
  87. bbBool copyFile( bbString srcPath,bbString dstPath ){
  88. #if _WIN32
  89. // return CopyFileW( bbWString( srcPath ),bbWString( dstPath ),FALSE );
  90. return CopyFileW( widen_utf8( bbCString( srcPath ) ),widen_utf8( bbCString( dstPath ) ),FALSE );
  91. #elif __APPLE__
  92. int ret=copyfile( bbCString( srcPath ),bbCString( dstPath ),0,COPYFILE_ALL );
  93. if( ret>=0 ) return true;
  94. // printf( "copyfile failed, ret=%i\n",ret );
  95. // printf( "src=%s\n",srcPath.c_str() );
  96. // printf( "dst=%s\n",dstPath.c_str() );
  97. return false;
  98. #else
  99. //TODO: use sendfile() here?
  100. //
  101. int err=-1;
  102. if( FILE *srcp=fopen( bbCString( srcPath ),"rb" ) ){
  103. err=-2;
  104. if( FILE *dstp=fopen( bbCString( dstPath ),"wb" ) ){
  105. err=0;
  106. char buf[1024];
  107. while( int n=fread( buf,1,1024,srcp ) ){
  108. if( fwrite( buf,1,n,dstp )!=n ){
  109. err=-3;
  110. break;
  111. }
  112. }
  113. fclose( dstp );
  114. }else{
  115. // printf( "FOPEN 'wb' for CopyFile(%s,%s) failed\n",C_STR(srcpath),C_STR(dstpath) );
  116. fflush( stdout );
  117. }
  118. fclose( srcp );
  119. }else{
  120. // printf( "FOPEN 'rb' for CopyFile(%s,%s) failed\n",C_STR(srcpath),C_STR(dstpath) );
  121. fflush( stdout );
  122. }
  123. return err==0;
  124. #endif
  125. }
  126. }