int TSystem::CopyFile(const char *f, const char *t, Bool_t overwrite) { //Method to copy any file from anywhere to anywhere //(if the necessary TFile and TSystem plugins are available). // Copy a file. If overwrite is true and file already exists the // file will be overwritten. Returns 0 when successful, -1 in case // of failure, -2 in case the file already exists and overwrite was false. if (!AccessPathName(t) && !overwrite) return -2; //save current directory TDirectory* dir_sav = gDirectory; TUrl path(f,kTRUE); path.SetOptions("filetype=raw"); TFile* from = TFile::Open(path.GetUrl(),"READ"); if (!from || from->IsZombie()){ dir_sav->cd(); return -1; } // from->ls(); path.SetUrl(t,kTRUE); path.SetOptions("filetype=raw"); TString open_option("CREATE"); if(overwrite) open_option.Prepend("RE"); TFile* to = TFile::Open(path.GetUrl(), open_option.Data()); if (!to || to->IsZombie()){ dir_sav->cd(); return -2; } // to->ls(); //set buffer size depending on size of file to copy //as a default use 1MB buffer (good for large files and rfio-hpss system); //use a 1KB buffer for files smaller than 1MB int bufsize = 1024*1024; Long64_t filesize = from->GetSize(); if( filesize >0 && filesize<1024*1024 ) bufsize=1024; char* buf = new char[bufsize]; int ret = 0; Long64_t bytes_read, bytes_wrote, bytes_left, last_bytes_read, last_bytes_wrote; bytes_read = bytes_wrote = bytes_left = last_bytes_read = last_bytes_wrote = 0; // printf("Filesize=%lld Buffersize=%d\n",filesize,bufsize); // int op = 1; while (bufsize && !ret && !from->ReadBuffer(buf, bufsize)) { bytes_read = from->GetBytesRead() - last_bytes_read; last_bytes_read = from->GetBytesRead(); ret = (int)to->WriteBuffer(buf, bytes_read); bytes_wrote = to->GetBytesWritten() - last_bytes_wrote; last_bytes_wrote = to->GetBytesWritten(); if(bytes_wrote != bytes_read) ret = -3; //adjust buffer size bytes_left = filesize - last_bytes_read; if(bytes_left < bufsize) bufsize = (int)bytes_left; // printf("----------------------------------%d-----------------------------------\n",op++); // printf("bytes_read=%lld last_bytes_read=%lld total_bytes_read=%lld\n", // bytes_read, last_bytes_read, from->GetBytesRead()); // printf("bytes_wrote=%lld last_bytes_wrote=%lld total_bytes_wrote=%lld\n", // bytes_wrote, last_bytes_wrote, to->GetBytesWritten()); // printf("bytes_left=%lld bufsize=%d\n", // bytes_left, bufsize); } // printf("----------------------------------END-----------------------------------\n"); // printf("bytes_read=%lld last_bytes_read=%lld total_bytes_read=%lld\n", // bytes_read, last_bytes_read, from->GetBytesRead()); // printf("bytes_wrote=%lld last_bytes_wrote=%lld total_bytes_wrote=%lld\n", // bytes_wrote, last_bytes_wrote, to->GetBytesWritten()); // printf("bytes_left=%lld bufsize=%d\n", // bytes_left, bufsize); delete from; delete to; delete [] buf; //reset working directory dir_sav->cd(); return ret; }