Subversion Repositories Kolibri OS

Rev

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