Subversion Repositories Kolibri OS

Rev

Rev 7369 | Rev 7533 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef INCLUDE_FILESYSTEM_H
  2. #define INCLUDE_FILESYSTEM_H
  3. #print "[include <fs.h>]\n"
  4.  
  5. #ifndef INCLUDE_DATE_H
  6. #include "../lib/date.h"
  7. #endif
  8.  
  9. //===================================================//
  10. //                                                   //
  11. //              Basic System Functions               //
  12. //                                                   //
  13. //===================================================//
  14.  
  15. :struct f70{
  16.         dword   func;
  17.         dword   param1;
  18.         dword   param2;
  19.         dword   param3;
  20.         dword   param4;
  21.         char    rezerv;
  22.         dword   name;
  23. };
  24.  
  25. :struct BDVK {
  26.         dword   readonly:1, hidden:1, system:1, volume_label:1, isfolder:1, notarchived:1, :0;
  27.         byte    type_name;
  28.         byte    rez1, rez2, selected;
  29.         dword   timecreate;
  30.         date    datecreate;
  31.         dword   timelastaccess;
  32.         date    datelastaccess;
  33.         dword   timelastedit;
  34.         date    datelastedit;
  35.         dword   sizelo;
  36.         dword   sizehi;
  37.         char    name[518];
  38. };
  39.  
  40.  
  41.  
  42. :f70 getinfo_file_70;
  43. :dword GetFileInfo(dword file_path, bdvk_struct)
  44. {    
  45.     getinfo_file_70.func = 5;
  46.     getinfo_file_70.param1 =
  47.     getinfo_file_70.param2 =
  48.     getinfo_file_70.param3 = 0;
  49.     getinfo_file_70.param4 = bdvk_struct;
  50.     getinfo_file_70.rezerv = 0;
  51.     getinfo_file_70.name = file_path;
  52.     $mov eax,70
  53.     $mov ebx,#getinfo_file_70.func
  54.     $int 0x40
  55. }
  56.  
  57. :f70 setinfo_file_70;
  58. :dword SetFileInfo(dword file_path, bdvk_struct)
  59. {    
  60.     setinfo_file_70.func = 6;
  61.     setinfo_file_70.param1 =
  62.     setinfo_file_70.param2 =
  63.     setinfo_file_70.param3 = 0;
  64.     setinfo_file_70.param4 = bdvk_struct;
  65.     setinfo_file_70.rezerv = 0;
  66.     setinfo_file_70.name = file_path;
  67.     $mov eax,70
  68.     $mov ebx,#setinfo_file_70.func
  69.     $int 0x40
  70. }
  71.  
  72. :f70 run_file_70;
  73. :signed int RunProgram(dword run_path, run_param)
  74. {      
  75.     run_file_70.func = 7;
  76.     run_file_70.param1 =
  77.     run_file_70.param3 =
  78.     run_file_70.param4 =
  79.     run_file_70.rezerv = 0;
  80.     run_file_70.param2 = run_param;
  81.     run_file_70.name = run_path;
  82.     $mov eax,70
  83.     $mov ebx,#run_file_70.func
  84.     $int 0x40
  85. }
  86.  
  87. :f70 create_dir_70;
  88. :int CreateDir(dword new_folder_path)
  89. {
  90.         create_dir_70.func = 9;
  91.         create_dir_70.param1 =
  92.         create_dir_70.param2 =
  93.         create_dir_70.param3 =
  94.         create_dir_70.param4 =
  95.         create_dir_70.rezerv = 0;
  96.         create_dir_70.name = new_folder_path;
  97.         $mov eax,70
  98.         $mov ebx,#create_dir_70.func
  99.         $int 0x40
  100. }
  101.  
  102. :f70 del_file_70;      
  103. :int DeleteFile(dword del_file_path)
  104. {    
  105.         del_file_70.func = 8;
  106.         del_file_70.param1 =
  107.         del_file_70.param2 =
  108.         del_file_70.param3 =
  109.         del_file_70.param4 =
  110.         del_file_70.rezerv = 0;
  111.         del_file_70.name = del_file_path;
  112.         $mov eax,70
  113.         $mov ebx,#del_file_70.func
  114.         $int 0x40
  115. }
  116.  
  117. :f70 read_file_70;
  118. :int ReadFile(dword offset, data_size, buffer, file_path)
  119. {
  120.         read_file_70.func = 0;
  121.         read_file_70.param1 = offset;
  122.         read_file_70.param2 = 0;
  123.         read_file_70.param3 = data_size;
  124.         read_file_70.param4 = buffer;
  125.         read_file_70.rezerv = 0;
  126.         read_file_70.name = file_path;
  127.         $mov eax,70
  128.         $mov ebx,#read_file_70.func
  129.         $int 0x40
  130. }
  131.  
  132. :f70 write_file_70;
  133. :int CreateFile(dword data_size, buffer, file_path)
  134. {
  135.         write_file_70.func = 2;
  136.         write_file_70.param1 = 0;
  137.         write_file_70.param2 = 0;
  138.         write_file_70.param3 = data_size;
  139.         write_file_70.param4 = buffer;
  140.         write_file_70.rezerv = 0;
  141.         write_file_70.name = file_path;
  142.         $mov eax,70
  143.         $mov ebx,#write_file_70.func
  144.         $int 0x40
  145. }
  146.  
  147.   ////////////////////////////////////////
  148.  //     WriteInFileThatAlredyExists    //
  149. ////////////////////////////////////////
  150. :f70 write_file_offset_70;
  151. :int WriteFile(dword offset, data_size, buffer, file_path)
  152. {
  153.         write_file_offset_70.func = 3;
  154.         write_file_offset_70.param1 = offset;
  155.         write_file_offset_70.param2 = 0;
  156.         write_file_offset_70.param3 = data_size;
  157.         write_file_offset_70.param4 = buffer;
  158.         write_file_offset_70.rezerv = 0;
  159.         write_file_offset_70.name = file_path;
  160.         $mov eax,70
  161.         $mov ebx,#write_file_offset_70.func
  162.         $int 0x40
  163. }
  164.  
  165. :f70 read_dir_70;
  166. :int ReadDir(dword file_count, read_buffer, dir_path)
  167. {
  168.         read_dir_70.func = 1;
  169.         read_dir_70.param1 =
  170.         read_dir_70.param2 =
  171.         read_dir_70.rezerv = 0;
  172.         read_dir_70.param3 = file_count;
  173.         read_dir_70.param4 = read_buffer;
  174.         read_dir_70.name = dir_path;
  175.         $mov eax,70
  176.         $mov ebx,#read_dir_70.func
  177.         $int 0x40
  178. }
  179.  
  180. inline fastcall void SetCurDir( ECX)
  181. {
  182.         EAX=30;
  183.         EBX=1;
  184.         $int 0x40
  185. }
  186.  
  187. //===================================================//
  188. //                                                   //
  189. //                        Misc                       //
  190. //                                                   //
  191. //===================================================//
  192.  
  193. :bool dir_exists(dword fpath)
  194. {
  195.         char buf[32];
  196.         if (!ReadDir(0, #buf, fpath)) return true;
  197.         return false;
  198. }
  199.  
  200. /*
  201. // This implementation of dir_exists() is faster than
  202. // previous but here virtual folders like
  203. // '/' and '/tmp' are not recognised as FOLDERS
  204. // by GetFileInfo() => BDVK.isfolder attribute :(
  205.  
  206. :bool dir_exists(dword fpath)
  207. {
  208.         BDVK fpath_atr;
  209.         if (GetFileInfo(fpath, #fpath_atr) != 0) return false;
  210.         return fpath_atr.isfolder;
  211. }
  212. */
  213.  
  214. :bool file_exists(dword fpath)
  215. {
  216.         BDVK ReadFile_atr;
  217.         if (! GetFileInfo(fpath, #ReadFile_atr)) return true;
  218.         return false;
  219. }
  220.  
  221. enum
  222. {
  223.         DIRS_ALL,
  224.         DIRS_NOROOT,
  225.         DIRS_ONLYREAL
  226. };
  227. :int GetDir(dword dir_buf, file_count, path, doptions)
  228. {
  229.         dword buf, fcount, error;
  230.         buf = malloc(32);
  231.         error = ReadDir(0, buf, path);
  232.         if (!error)
  233.         {
  234.                 fcount = ESDWORD[buf+8];
  235.                 buf = realloc(buf, fcount+1*304+32);
  236.                 ReadDir(fcount, buf, path);
  237.                 //fcount=EBX;
  238.  
  239.                 if (doptions == DIRS_ONLYREAL)
  240.                 {
  241.                         if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);}
  242.                         if (!strcmp("..",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);}
  243.                 }
  244.                 if (doptions == DIRS_NOROOT)
  245.                 {
  246.                         if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);}
  247.                 }
  248.  
  249.                 ESDWORD[dir_buf] = buf;
  250.                 ESDWORD[file_count] = fcount;
  251.         }
  252.         else
  253.         {
  254.                 ESDWORD[dir_buf] = free(buf);
  255.                 ESDWORD[file_count] = 0;
  256.         }
  257.         return error;
  258. }
  259.  
  260. :dword abspath(dword relative_path) //GetAbsolutePathFromRelative()
  261. {
  262.         char absolute_path[4096];
  263.         if (ESBYTE[relative_path]=='/')
  264.         {
  265.                 strcpy(#absolute_path, relative_path);
  266.         }
  267.         else
  268.         {
  269.                 strcpy(#absolute_path, I_Path);
  270.                 absolute_path[strrchr(#absolute_path, '/')] = '\0';
  271.                 strcat(#absolute_path, relative_path);
  272.         }
  273.         return #absolute_path;
  274. }
  275.  
  276. :dword GetIni(dword ini_path, ini_name) //search it on /kolibrios/ then on /sys/
  277. {
  278.         strcpy(ini_path, "/kolibrios/settings/");
  279.         strcat(ini_path, ini_name);
  280.         if (!file_exists(ini_path)) {
  281.                 strcpy(ini_path, "/sys/SETTINGS/");
  282.                 strcat(ini_path, ini_name);
  283.         }
  284.         return ini_path;
  285. }
  286.  
  287. :dword notify(dword notify_param)
  288. {
  289.         return RunProgram("/sys/@notify", notify_param);
  290. }
  291.  
  292. :void die(dword _last_msg)
  293. {
  294.         notify(_last_msg);
  295.         ExitProcess();
  296. }
  297.  
  298. //===================================================//
  299. //                                                   //
  300. //                   Convert Size                    //
  301. //                                                   //
  302. //===================================================//
  303.  
  304. :byte ConvertSize_size_prefix[8];
  305. :dword ConvertSize(dword bytes)
  306. {
  307.   byte size_nm[4];
  308.   if (bytes>=1073741824) strlcpy(#size_nm, "Gb",2);
  309.   else if (bytes>=1048576) strlcpy(#size_nm, "Mb",2);
  310.   else if (bytes>=1024) strlcpy(#size_nm, "Kb",2);
  311.   else strlcpy(#size_nm, "b ",2);
  312.   while (bytes>1023) bytes >>= 10;
  313.   sprintf(#ConvertSize_size_prefix,"%d %s",bytes,#size_nm);
  314.   return #ConvertSize_size_prefix;
  315. }
  316.  
  317. :dword ConvertSize64(dword bytes_lo, bytes_hi)
  318. {
  319.   if (bytes_hi > 0) {
  320.         if (bytes_lo>=1073741824) bytes_lo >>= 30; else bytes_lo = 0;
  321.         sprintf(#ConvertSize_size_prefix,"%d Gb",bytes_hi<<2 + bytes_lo);
  322.         return #ConvertSize_size_prefix;
  323.   }
  324.   else return ConvertSize(bytes_lo);
  325. }
  326.  
  327. :unsigned char size[25];
  328. :dword ConvertSizeToKb(unsigned int bytes)
  329. {
  330.         unsigned int kb;
  331.         dword kb_line;
  332.  
  333.         if (bytes >= 1024)
  334.         {
  335.                 kb_line = itoa(bytes / 1024);
  336.                 strcpy(#size, kb_line);
  337.                 strcat(#size, " Kb");          
  338.         }
  339.         else {
  340.                 kb_line = itoa(bytes);
  341.                 strcpy(#size, kb_line);
  342.                 strcat(#size, " b");
  343.         }
  344.  
  345.         return #size;
  346. }
  347.  
  348. //===================================================//
  349. //                                                   //
  350. //                      Copy                         //
  351. //                                                   //
  352. //===================================================//
  353.  
  354. :int CopyFileAtOnce(dword size, copyFrom, copyTo)
  355. dword cbuf;
  356. int error;
  357. {
  358.         cbuf = malloc(size);
  359.         if (error = ReadFile(0, size, cbuf, copyFrom))
  360.         {
  361.                 debugln("Error: CopyFileAtOnce->ReadFile");
  362.         }
  363.         else
  364.         {
  365.                 if (error = CreateFile(size, cbuf, copyTo)) debugln("Error: CopyFileAtOnce->CreateFile");
  366.         }
  367.         free(cbuf);
  368.         return error;
  369. }
  370.  
  371. :int CopyFileByBlocks(dword size, copyFrom, copyTo)
  372. dword cbuf;
  373. int error=-1;
  374. dword offpos=0;
  375. int block_size=1024*1024*4; //copy by 4 MiB
  376. {
  377.         if (GetFreeRAM()>1024*78) {
  378.                 //Set block size 32 MiB
  379.                 block_size <<= 3;
  380.         }
  381.         cbuf = malloc(block_size);
  382.         if (error = CreateFile(0, 0, copyTo))
  383.         {
  384.                 debugln("Error: CopyFileByBlocks->CreateFile");
  385.                 size = -1;     
  386.         }
  387.         while(offpos < size)
  388.         {
  389.                 error = ReadFile(offpos, block_size, cbuf, copyFrom);
  390.                 if (error = 6) { //File ended before last byte was readed
  391.                         block_size = EBX;
  392.                         if (block_size+offpos>=size) error=0;
  393.                 }
  394.                 else
  395.                         if (error!=0) {
  396.                                 debugln("Error: CopyFileByBlocks->ReadFile");
  397.                                 break;
  398.                         }
  399.                 if (error = WriteFile(offpos, block_size, cbuf, copyTo)) {
  400.                         debugln("Error: CopyFileByBlocks->WriteFile");
  401.                         break;
  402.                 }
  403.                 offpos += block_size;
  404.         }
  405.         free(cbuf);
  406.         return error;
  407. }
  408.  
  409. //===================================================//
  410. //                                                   //
  411. //                  Directory Size                   //
  412. //                                                   //
  413. //===================================================//
  414.  
  415. :struct _dir_size
  416. {
  417.         BDVK dir_info;
  418.         dword folders;
  419.         dword files;
  420.         dword bytes;
  421.         void get();    
  422.         void calculate_loop(); 
  423. } dir_size;
  424.  
  425. :void _dir_size::get(dword way)
  426. {
  427.         folders = files = bytes = 0;
  428.         if (way) calculate_loop(way);
  429. }
  430.  
  431. :void _dir_size::calculate_loop(dword way)
  432. {
  433.         dword dirbuf, fcount, i, filename;
  434.         dword cur_file;
  435.         if (dir_exists(way))
  436.         {
  437.                 cur_file = malloc(4096);
  438.                 // In the process of recursive descent, memory must be allocated dynamically,
  439.                 // because the static memory -> was a bug !!! But unfortunately pass away to sacrifice speed.
  440.                 GetDir(#dirbuf, #fcount, way, DIRS_ONLYREAL);
  441.                 for (i=0; i<fcount; i++)
  442.                 {
  443.                         filename = i*304+dirbuf+72;
  444.                         sprintf(cur_file,"%s/%s",way,filename);
  445.                        
  446.                         if (TestBit(ESDWORD[filename-40], 4) )
  447.                         {
  448.                                 folders++;
  449.                                 calculate_loop(cur_file);
  450.                         }
  451.                         else
  452.                         {
  453.                                 GetFileInfo(cur_file, #dir_info);
  454.                                 bytes += dir_info.sizelo;
  455.                                 files++;
  456.                         }
  457.                 }
  458.                 free(cur_file);
  459.                 free(dirbuf);
  460.         }
  461. }
  462.  
  463. #endif