Subversion Repositories Kolibri OS

Rev

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