Subversion Repositories Kolibri OS

Rev

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