Subversion Repositories Kolibri OS

Rev

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

  1. //copyf - copy file or folder with content
  2. #ifndef INCLUDE_COPYF_H
  3. #define INCLUDE_COPYF_H
  4. #print "[include <copyf.h>]\n"
  5.  
  6. #ifndef INCLUDE_FILESYSTEM_H
  7. #include "../lib/fs.h"
  8. #endif
  9.  
  10. enum {
  11.         FILE_DEFAULT=0,
  12.         FILE_EXISTS,
  13.         FILE_REPLACE,
  14.         FILE_SKIP,
  15. };
  16.  
  17. int copy_state = FILE_DEFAULT;
  18.  
  19. :int copyf(dword from1, in1)
  20. {
  21.         dword error;
  22.         BDVK CopyFile_atr1;
  23.         copy_state = FILE_DEFAULT;
  24.  
  25.         if (!from1) || (!in1)
  26.         {
  27.                 notify("Error: too few copyf() params!");
  28.                 return -1;
  29.         }
  30.         if (error = GetFileInfo(from1, #CopyFile_atr1))
  31.         {
  32.                 debugln("Error: copyf->GetFileInfo");
  33.                 return error;
  34.         }
  35.         if (dir_exists(from1))
  36.                 return CopyFolder(from1, in1);
  37.         else
  38.         {              
  39.                 while(1)
  40.                 {
  41.                         Operation_Draw_Progress(from1+strrchr(from1, '/'));
  42.                         if (copy_state == FILE_DEFAULT) || (copy_state == FILE_REPLACE)
  43.                         {
  44.                                 error = CopyFile(from1, in1);
  45.                                 if (error != 222)
  46.                                 {
  47.                                         return error;
  48.                                 }
  49.                         }
  50.                         if (copy_state == FILE_SKIP)
  51.                         {
  52.                                 return 0;
  53.                         }
  54.                 }
  55.         }
  56. }
  57.  
  58. :int CopyFile(dword copy_from3, copy_in3)
  59. {
  60.         BDVK CopyFile_atr;
  61.         dword error;
  62.  
  63.         if (error = GetFileInfo(copy_from3, #CopyFile_atr))
  64.         {
  65.                 debugln("Error: CopyFile->GetFileInfo");
  66.         }
  67.         else
  68.         {
  69.                 if (file_exists(copy_in3)) && (copy_state != FILE_REPLACE)
  70.                 {
  71.                         copy_state = FILE_EXISTS;
  72.                         return 222;
  73.                 }
  74.                 if (GetFreeRAM()-1024*1024 < CopyFile_atr.sizelo) //GetFreeRam-1Mb and convert to bytes
  75.                 {
  76.                         if (error = CopyFileByBlocks(CopyFile_atr.sizelo, copy_from3, copy_in3))
  77.                                 debugln("Error: CopyFile->CopyFileByBlocks");
  78.                 }
  79.                 else {
  80.                         if (error = CopyFileAtOnce(CopyFile_atr.sizelo, copy_from3, copy_in3))
  81.                                 debugln("Error: CopyFile->CopyFileAtOnce");
  82.                 }              
  83.         }
  84.         if (error) debug_error(copy_from3, error);
  85.         return error;
  86. }
  87.  
  88. :int CopyFolder(dword from2, in2)
  89. {
  90.         dword dirbuf, fcount, i, filename;
  91.         char copy_from2[4096], copy_in2[4096], error;  
  92.  
  93.         if (error = GetDir(#dirbuf, #fcount, from2, DIRS_ONLYREAL))
  94.         {
  95.                 debugln("Error: CopyFolder->GetDir");
  96.                 debug_error(from2, error);
  97.                 free(dirbuf);
  98.                 return error;
  99.         }
  100.  
  101.         if (chrnum(in2, '/')>2) && (error = CreateDir(in2))
  102.         {
  103.                 debugln("Error: CopyFolder->CreateDir");
  104.                 debug_error(in2, error);
  105.                 free(dirbuf);
  106.                 return error;
  107.         }
  108.  
  109.         for (i=0; i<fcount; i++)
  110.         {
  111.                 copy_state = FILE_DEFAULT;
  112.                 filename = i*304+dirbuf+72;
  113.                 sprintf(#copy_from2,"%s/%s",from2,filename);
  114.                 sprintf(#copy_in2,"%s/%s",in2,filename);
  115.  
  116.                 if ( ESDWORD[filename-40] & ATR_FOLDER ) //dir_exists?
  117.                 {
  118.                         if ( (!strncmp(filename, ".",1)) || (!strncmp(filename, "..",2)) ) continue;
  119.                         CopyFolder(#copy_from2, #copy_in2);
  120.                 }
  121.                 else
  122.                 {
  123.                         while(1)
  124.                         {
  125.                                 Operation_Draw_Progress(filename+strrchr(filename, '/'));
  126.                                 if (copy_state == FILE_DEFAULT) || (copy_state == FILE_REPLACE)
  127.                                 {
  128.                                         error = CopyFile(#copy_from2, #copy_in2);
  129.                                         if (error != 222)
  130.                                         {
  131.                                                 if (error)
  132.                                                 {
  133.                                                         if (fabs(error)==8) { debugln("Stop copying."); break;} //TODO: may be need grobal var like stop_all
  134.                                                         error=CopyFile(#copy_from2, #copy_in2); // #2 :)
  135.                                                 }
  136.                                                 break;
  137.                                         }
  138.                                 }
  139.                                 if (copy_state == FILE_SKIP)
  140.                                 {
  141.                                         copy_state = FILE_DEFAULT;
  142.                                         break;
  143.                                 }
  144.                         }
  145.                 }
  146.         }
  147.         free(dirbuf);
  148.         return error;
  149. }
  150.  
  151. #ifdef LANG_RUS
  152.         unsigned char *ERROR_TEXT[]={
  153.         "Š®¤ #0: ãᯥ譮",
  154.         "Žè¨¡ª  #1: ­¥ ®¯à¥¤¥«¥­  ¡ §  ¨/¨«¨ à §¤¥« ¦ñá⪮£® ¤¨áª ",
  155.         "Žè¨¡ª  #2: äã­ªæ¨ï ­¥ ¯®¤¤¥à¦¨¢ ¥âáï ¤«ï í⮩ ä ©«®¢®© á¨á⥬ë",
  156.         "Žè¨¡ª  #3: ­¥¨§¢¥áâ­ ï ä ©«®¢ ï á¨á⥬ ",
  157.         0,
  158.         "Žè¨¡ª  #5: ä ©« ¨«¨ ¯ ¯ª  ­¥ ­ ©¤¥­ë",
  159.         "Žè¨¡ª  #6: ª®­¥æ ä ©« ",
  160.         "Žè¨¡ª  #7: 㪠§ â¥«ì ­ å®¤¨âáï ¢á¥ ¯ ¬ï⨠¯à¨«®¦¥­¨ï",
  161.         "Žè¨¡ª  #8: ­¥¤®áâ â®ç­® ¬¥áâ  ­  à §¤¥«¥",
  162.         "Žè¨¡ª  #9: ®è¨¡ª  ä ©«®¢®© á¨á⥬ë",
  163.         "Žè¨¡ª  #10: ¤®áâ㯠§ ¯à¥é¥­",
  164.         "Žè¨¡ª  #11: ®è¨¡ª  ãáâனá⢠",
  165.         0, 0, 0, 0, 0, 0, 0, 0, 0,
  166.         0, 0, 0, 0, 0, 0, 0, 0, 0,
  167.         "Žè¨¡ª  #30: ­¥¤®áâ â®ç­® ¯ ¬ïâ¨",
  168.         "Žè¨¡ª  #31: ä ©« ­¥ ï¥âáï ¨á¯®«­ï¥¬ë¬",
  169.         "Žè¨¡ª  #32: ᫨誮¬ ¬­®£® ¯à®æ¥áᮢ", 0};
  170. #else
  171.         unsigned char *ERROR_TEXT[]={
  172.         "Code #0 - No error",
  173.         "Error #1 - Base or partition of a hard disk is not defined",
  174.         "Error #2 - Function isn't supported for this file system",
  175.         "Error #3 - Unknown file system",
  176.         0,
  177.         "Error #5 - File or folder not found",
  178.         "Error #6 - End of file",
  179.         "Error #7 - Pointer lies outside of application memory",
  180.         "Error #8 - Not enough space on partition",
  181.         "Error #9 - File system error",
  182.         "Error #10 - Access denied",
  183.         "Error #11 - Device error",
  184.         0, 0, 0, 0, 0, 0, 0, 0, 0,
  185.         0, 0, 0, 0, 0, 0, 0, 0, 0,
  186.         "Error #30 - Not enough memory",
  187.         "Error #31 - File is not executable",
  188.         "Error #32 - Too many processes", 0};
  189. #endif
  190.  
  191. :dword get_error(int N)
  192. {
  193.         char error[256];
  194.         N = fabs(N);
  195.         if (N<=33) strcpy(#error, ERROR_TEXT[N]);
  196.         else sprintf(#error,"%d%s",N," - Unknown error number O_o");
  197.         return #error;
  198. }
  199.  
  200. :void debug_error(dword path, error_number)
  201. {
  202.         if (path) debugln(path);
  203.         debugln(get_error(error_number));
  204. }
  205.  
  206. #endif