Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. int cmd_mv(char param[])
  3. {
  4.     char* argv[100];
  5.     int argc;
  6.     char *filename_in = NULL;
  7.     char *filename_out = NULL;
  8.     char *buffer = NULL;
  9.  
  10.     kol_struct70 k70_in;
  11.     kol_struct70 k70_out;
  12.  
  13.     kol_struct_BDVK     bdvk;
  14.  
  15.     unsigned long long filesize;
  16.     unsigned result, buf_size;
  17.  
  18.     argc = parameters_prepare(param, argv);
  19.  
  20.     /*
  21.         argv[0] - path (abs or rel) to file
  22.         argv[1] - new location: path (abs or rel) to dir or file
  23.     */
  24.  
  25.     if (argc != 2)
  26.     {
  27.         #if LANG_ENG
  28.             printf("  mv <file_in> <file_out>\n\r");
  29.         #elif LANG_RUS
  30.             printf("  mv <¨áâ®ç­¨ª> <१ã«ìâ â>\n\r");
  31.         #endif
  32.         parameters_free(argc, argv);
  33.         return TRUE;
  34.     }
  35.  
  36.     filename_in  = (char*) malloc(FILENAME_MAX);
  37.     filename_out = (char*) malloc(FILENAME_MAX);
  38.  
  39.     if (argv[0][0] != '/')
  40.     {
  41.         strcpy(filename_in, cur_dir);
  42.         if (filename_in[strlen(filename_in)-1] != '/')
  43.         {
  44.             strcat(filename_in, "/"); // add slash
  45.         }
  46.         strcat(filename_in, argv[0]);
  47.     } else
  48.     {
  49.         strcpy(filename_in, argv[0]);
  50.     }
  51.     // -----
  52.     if (argv[1][0] != '/')
  53.     {
  54.         strcpy(filename_out, cur_dir);
  55.         if (filename_out[strlen(filename_out)-1] != '/')
  56.         {
  57.             strcat(filename_out, "/"); // add slash
  58.         }
  59.         strcat(filename_out, argv[1]);
  60.     } else
  61.     {
  62.         strcpy(filename_out, argv[1]);
  63.     }
  64.        
  65.     // add ability to use directory as destination
  66.     if ( dir_check(filename_out) )
  67.     {
  68.         char *fname = strrchr(filename_in, '/') + 1;  // always exist, as we add curdir
  69.         if (filename_out[strlen(filename_out)-1] != '/')
  70.         {
  71.             strcat(filename_out, "/"); // add slash
  72.         }
  73.         strcat(filename_out, fname);
  74.     }
  75.  
  76.     if (strcmp(filename_in, filename_out) == 0) // if source file and destination file are same then exist with success
  77.     {
  78.         result = 0;
  79.         goto lbl_exit;
  80.     }
  81.  
  82.  
  83.     k70_in.p00 = 5;
  84.     k70_in.p04 = 0LL;
  85.     k70_in.p12 = 0;
  86.     k70_in.p16 = (unsigned) &bdvk;
  87.     k70_in.p20 = 0;
  88.     k70_in.p21 = filename_in;
  89.  
  90.     result = kol_file_70(&k70_in); // get information about file
  91.     if ( 0 != result )
  92.         goto lbl_exit;
  93.  
  94.     // count buffer size up to 1Mb, but no more than 1/2 of free memory
  95.     buf_size = 1 << 20; // 1Mb
  96.     while( ((buf_size >> 10) > kol_system_memfree()) && (buf_size > 4096) )
  97.         buf_size /= 2;
  98.  
  99.     filesize = bdvk.p32; // getting file size (restriction - 4 GB only for FAT)
  100.     if (buf_size > filesize)
  101.         buf_size = (unsigned)filesize;  // may be zero!
  102.     if (buf_size == 0) buf_size = 4096; // ...
  103.  
  104.     buffer = (char*) malloc(buf_size);
  105.     if (!buffer)
  106.     {
  107.         result = E_NOMEM;
  108.         goto lbl_exit;
  109.     }
  110.  
  111.     k70_in.p00 = 0;
  112.     //k70_in.p08 = 0;
  113.     k70_in.p12 = buf_size;
  114.     k70_in.p16 = (unsigned) buffer;
  115.     k70_in.p20 = 0;
  116.     k70_in.p21 = filename_in;
  117.  
  118.     k70_out.p00 = 2;
  119.     //k70_out.p08 = 0;
  120.     k70_out.p12 = buf_size;
  121.     k70_out.p16 = (unsigned) buffer;
  122.     k70_out.p20 = 0;
  123.     k70_out.p21 = filename_out;
  124.  
  125.     unsigned long long offset = 0;
  126.     do
  127.     {
  128.         k70_in.p04 = offset;
  129.         if (offset + buf_size > filesize)  // last chunk
  130.         {
  131.             k70_in.p12 = k70_out.p12 = (unsigned)(filesize - offset); // filesize % buf_size;
  132.         }
  133.        
  134.         result = kol_file_70(&k70_in); // read
  135.         if (result != 0)
  136.         {
  137.             goto lbl_exit;
  138.         }
  139.  
  140.         k70_out.p04 = offset;
  141.         result = kol_file_70(&k70_out); // write
  142.         if (result != 0)
  143.         {
  144.            goto lbl_exit;
  145.         }
  146.  
  147.         if (k70_out.p00 == 2)
  148.         {
  149.             k70_out.p00 = 3; // changing function from create (2) to append (3)
  150.         }
  151.         offset += buf_size;
  152.     } while (offset < filesize);
  153.  
  154.     cmd_rm(filename_in); // remove source file
  155.  
  156.     lbl_exit:
  157.  
  158.     parameters_free(argc, argv);
  159.     free(filename_in);
  160.     free(filename_out);
  161.     free(buffer);
  162.  
  163.     return (result == 0);
  164. }