Subversion Repositories Kolibri OS

Rev

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