Subversion Repositories Kolibri OS

Rev

Rev 7213 | Rev 7225 | 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 WriteFile(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 WriteFileWithOffset(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. :int GetFile(dword buf, filesize, read_path)
  207. {
  208.         int return_val = 0;
  209.         BDVK ReadFile_atr;
  210.         dword rBuf;
  211.         if (! GetFileInfo(read_path, #ReadFile_atr))
  212.         {
  213.                 rBuf = malloc(ReadFile_atr.sizelo);    
  214.                 if (! ReadFile(0, ReadFile_atr.sizelo, rBuf, read_path))
  215.                 {
  216.                         ESDWORD[buf] = rBuf;
  217.                         ESDWORD[filesize] = ReadFile_atr.sizelo;
  218.                         return_val = 1;
  219.                 }
  220.         }
  221.         free(rBuf);
  222.         return return_val;
  223. }
  224.  
  225. enum
  226. {
  227.         DIRS_ALL,
  228.         DIRS_NOROOT,
  229.         DIRS_ONLYREAL
  230. };
  231. :int GetDir(dword dir_buf, file_count, path, doptions)
  232. {
  233.         dword buf, fcount, error;
  234.         buf = malloc(32);
  235.         error = ReadDir(0, buf, path);
  236.         if (!error)
  237.         {
  238.                 fcount = ESDWORD[buf+8];
  239.                 buf = realloc(buf, fcount+1*304+32);
  240.                 ReadDir(fcount, buf, path);
  241.                 //fcount=EBX;
  242.  
  243.                 if (doptions == DIRS_ONLYREAL)
  244.                 {
  245.                         if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);}
  246.                         if (!strcmp("..",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);}
  247.                 }
  248.                 if (doptions == DIRS_NOROOT)
  249.                 {
  250.                         if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);}
  251.                 }
  252.  
  253.                 ESDWORD[dir_buf] = buf;
  254.                 ESDWORD[file_count] = fcount;
  255.         }
  256.         else
  257.         {
  258.                 ESDWORD[file_count] = 0;
  259.                 ESDWORD[dir_buf] = free(buf);
  260.         }
  261.         return error;
  262. }
  263.  
  264. :dword abspath(dword relative_path) //GetAbsolutePathFromRelative()
  265. {
  266.         char absolute_path[4096];
  267.         if (ESBYTE[relative_path]=='/')
  268.         {
  269.                 strcpy(#absolute_path, relative_path);
  270.         }
  271.         else
  272.         {
  273.                 strcpy(#absolute_path, I_Path);
  274.                 absolute_path[strrchr(#absolute_path, '/')] = '\0';
  275.                 strcat(#absolute_path, relative_path);
  276.         }
  277.         return #absolute_path;
  278. }
  279.  
  280. :dword GetIni(dword ini_path, ini_name) //search it on /kolibrios/ then on /sys/
  281. {
  282.         strcpy(ini_path, "/kolibrios/settings/");
  283.         strcat(ini_path, ini_name);
  284.         if (!file_exists(ini_path)) {
  285.                 strcpy(ini_path, "/sys/SETTINGS/");
  286.                 strcat(ini_path, ini_name);
  287.         }
  288.         return ini_path;
  289. }
  290.  
  291. :byte ConvertSize_size_prefix[8];
  292. :dword ConvertSize(dword bytes)
  293. {
  294.   byte size_nm[4];
  295.   if (bytes>=1073741824) strlcpy(#size_nm, "Gb",2);
  296.   else if (bytes>=1048576) strlcpy(#size_nm, "Mb",2);
  297.   else if (bytes>=1024) strlcpy(#size_nm, "Kb",2);
  298.   else strlcpy(#size_nm, "b ",2);
  299.   while (bytes>1023) bytes >>= 10;
  300.   sprintf(#ConvertSize_size_prefix,"%d %s",bytes,#size_nm);
  301.   return #ConvertSize_size_prefix;
  302. }
  303.  
  304. :dword ConvertSize64(dword bytes_lo, bytes_hi)
  305. {
  306.   if (bytes_hi > 0) {
  307.         if (bytes_lo>=1073741824) bytes_lo >>= 30; else bytes_lo = 0;
  308.         sprintf(#ConvertSize_size_prefix,"%d Gb",bytes_hi<<2 + bytes_lo);
  309.         return #ConvertSize_size_prefix;
  310.   }
  311.   else return ConvertSize(bytes_lo);
  312. }
  313.  
  314. :dword notify(dword notify_param)
  315. {
  316.         return RunProgram("/sys/@notify", notify_param);
  317. }
  318.  
  319. :void die(dword _last_msg)
  320. {
  321.         notify(_last_msg);
  322.         ExitProcess();
  323. }
  324.  
  325. :unsigned char size[25]=0;
  326. :dword ConvertSizeToKb(unsigned int bytes)
  327. {
  328.         unsigned int kb;
  329.         dword kb_line;
  330.  
  331.         if (bytes >= 1024)
  332.         {
  333.                 kb_line = itoa(bytes / 1024);
  334.                 strcpy(#size, kb_line);
  335.                 strcat(#size, " Kb");          
  336.         }
  337.         else {
  338.                 kb_line = itoa(bytes);
  339.                 strcpy(#size, kb_line);
  340.                 strcat(#size, " b");
  341.         }
  342.  
  343.         return #size;
  344. }
  345.  
  346. :int CopyFileAtOnce(dword size, copyFrom, copyTo)
  347. dword cbuf;
  348. int error;
  349. {
  350.         cbuf = malloc(size);
  351.         if (error = ReadFile(0, size, cbuf, copyFrom))
  352.         {
  353.                 debugln("Error: CopyFileAtOnce->ReadFile");
  354.         }
  355.         else
  356.         {
  357.                 if (error = WriteFile(size, cbuf, copyTo)) debugln("Error: CopyFileAtOnce->WriteFile");
  358.         }
  359.         free(cbuf);
  360.         return error;
  361. }
  362.  
  363. :int CopyFileByBlocks(dword size, copyFrom, copyTo)
  364. dword cbuf;
  365. int error=-1;
  366. dword offpos=0;
  367. int block_size=1024*4024; //copy by 4 MiBs
  368. {
  369.         cbuf = malloc(block_size);
  370.         WriteFile(0, 0, copyTo); //create file
  371.         while(offpos < size)
  372.         {
  373.                 error = ReadFile(offpos, block_size, cbuf, copyFrom);
  374.                 if (error = 6) { //File ended before last byte was readed
  375.                         block_size = EBX;
  376.                         error=0;
  377.                 }
  378.                 else
  379.                         if (error!=0) break;
  380.                 if (error = WriteFileWithOffset(offpos, block_size, cbuf, copyTo)) break;
  381.                 offpos += block_size;
  382.         }
  383.         free(cbuf);
  384.         return error;
  385. }
  386.  
  387.  
  388. #endif