filesystem.cpp 2.9 KB

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