Subversion Repositories Kolibri OS

Rev

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