Subversion Repositories Kolibri OS

Rev

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