Subversion Repositories Kolibri OS

Rev

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