Subversion Repositories Kolibri OS

Rev

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