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>::create( bb_argc );
  41. for( int i=0;i<bb_argc;++i ) _appArgs->at(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. ;
  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 CopyFileA( bbTString( srcPath ),bbTString( dstPath ),FALSE );
  90. #elif __APPLE__
  91. int ret=copyfile( bbTString( srcPath ),bbTString( dstPath ),0,COPYFILE_ALL );
  92. if( ret>=0 ) return true;
  93. // printf( "copyfile failed, ret=%i\n",ret );
  94. // printf( "src=%s\n",srcPath.c_str() );
  95. // printf( "dst=%s\n",dstPath.c_str() );
  96. return false;
  97. #else
  98. //TODO: use sendfile() here?
  99. //
  100. int err=-1;
  101. if( FILE *srcp=fopen( bbTString( srcPath ),"rb" ) ){
  102. err=-2;
  103. if( FILE *dstp=fopen( bbTString( dstPath ),"wb" ) ){
  104. err=0;
  105. char buf[1024];
  106. while( int n=fread( buf,1,1024,srcp ) ){
  107. if( fwrite( buf,1,n,dstp )!=n ){
  108. err=-3;
  109. break;
  110. }
  111. }
  112. fclose( dstp );
  113. }else{
  114. // printf( "FOPEN 'wb' for CopyFile(%s,%s) failed\n",C_STR(srcpath),C_STR(dstpath) );
  115. fflush( stdout );
  116. }
  117. fclose( srcp );
  118. }else{
  119. // printf( "FOPEN 'rb' for CopyFile(%s,%s) failed\n",C_STR(srcpath),C_STR(dstpath) );
  120. fflush( stdout );
  121. }
  122. return err==0;
  123. #endif
  124. }
  125. }