static void printStats(offset_t filesize,unsigned start,unsigned startu) { StringBuffer tmp; unsigned elapsed = msTick()-start; unsigned elapsedu = usTick()-startu; if (!elapsedu) elapsedu = 1; if (elapsed<1000) printf("%"I64F"d bytes copied, at %.2f MB/s in %s\n",filesize,((((double)filesize)/(1024*1024))/elapsedu)*1000000,formatTimeU(elapsedu,tmp)); else printf("%"I64F"d bytes copied, at %.2f MB/s in %s\n",filesize,((((double)filesize)/(1024*1024))/elapsed)*1000,formatTime(elapsed*1000,tmp)); }
void print(const char *str) { if (nl) { char timeStamp[32]; time_t tNow; time(&tNow); #ifdef _WIN32 unsigned tpid = GetCurrentThreadId(); #else unsigned tpid = getpid(); #endif #ifdef _WIN32 struct tm ltNow = *localtime(&tNow); #else struct tm ltNow; localtime_r(&tNow, <Now); #endif #ifdef MINIMALTRACE if (((detail & _LOG_TID) && tpid!=lasttpid)||((detail & _LOG_TIME) && memcmp(&tNow,&lastt,sizeof(tNow))!=0)) { lasttpid = tpid; lastt = tNow; FPRINTF("["); if (detail & _LOG_TIME) { strftime(timeStamp, 32, "%H:%M:%S ", <Now); FPRINTF(timeStamp); } if (detail & _LOG_TID) { fprintf(stdlog, "TID=%d ", tpid); if (logFile) fprintf(logFile, "TID=%d ", tpid); } FPRINTF("]\n"); } #else if (detail & _LOG_TIME) { strftime(timeStamp, 32, "%m/%d/%y %H:%M:%S ", <Now); FPRINTF(timeStamp); } if (detail & _LOG_CLOCK) { unsigned t=msTick(); fprintf(stdlog, "%u ", t); if (logFile) fprintf(logFile, "%u ", t); } if (detail & _LOG_HIRESCLOCK) { unsigned clock = usTick(); fprintf(stdlog, " TICK=%u ", clock); if (logFile) fprintf(logFile, " TICK=%u ", clock); } #if defined(_WIN32) if (detail & _LOG_TID) { fprintf(stdlog, "TID=%d ", GetCurrentThreadId()); if (logFile) fprintf(logFile, "TID=%d ", GetCurrentThreadId()); } if (detail & _LOG_PID) { fprintf(stdlog, "PID=%d ", GetCurrentProcessId()); if (logFile) fprintf(logFile, "PID=%d ", GetCurrentProcessId()); } #else if (detail & _LOG_PID) { fprintf(stdlog, "PID=%d ", getpid()); if (logFile) fprintf(logFile, "PID=%d ", getpid()); } #endif if (detail & (_LOG_PID|_LOG_TID|_LOG_TIME)) { fprintf(stdlog, "- "); if (logFile) fprintf(logFile, "- "); } #endif } if (str && *str) { nl = str[strlen(str)-1]=='\n'; FPRINTF(str); } else nl = false; fflush(stdlog); if (logFile) { fflush(logFile); } }
void copyCompress(const char *from, const char *to, size32_t rowsize, bool fast, bool flzstrm, bool stats) { Owned<IFile> srcfile = createIFile(from); Owned<IFileIO> baseio = srcfile->open(IFOread); if (!baseio) { printf("ERROR: could not open '%s' for read\n",from); doexit(3); } Owned<ICompressedFileIO> cmpio = createCompressedFileReader(baseio); Owned<IFileIOStream> flzstrmsrc = cmpio?NULL:createFastLZStreamRead(baseio); bool plaincopy = false; IFileIO *srcio = NULL; if (cmpio) { srcio = cmpio; if (rowsize&&(cmpio->recordSize()==rowsize)) plaincopy = true; else if (!rowsize) { if (fast&&(cmpio->method()==COMPRESS_METHOD_FASTLZ)) plaincopy = true; else if (!fast&&(cmpio->method()==COMPRESS_METHOD_LZW)) plaincopy = true; } } else if (flzstrmsrc) { if (flzstrm) plaincopy = true; } else srcio = baseio; if (plaincopy) { cmpio.clear(); srcio = baseio.get(); } Owned<IFile> dstfile = createIFile(to); StringBuffer fulldst; if (dstfile->isDirectory()==foundYes) { dstfile.clear(); addPathSepChar(fulldst.append(to)).append(pathTail(from)); to = fulldst.str(); dstfile.setown(createIFile(to)); } if (dstfile->exists()) { printf("ERROR: file '%s' already exists\n",to); doexit(4); } unsigned start; unsigned startu; if (stats) { start = msTick(); startu = usTick(); } Owned<IFileIO> dstio; Owned<IFileIOStream> flzstrmdst; if (plaincopy||flzstrm) { dstio.setown(dstfile->open(IFOcreate)); if (dstio&&!plaincopy) flzstrmdst.setown(createFastLZStreamWrite(dstio)); } else dstio.setown(createCompressedFileWriter(dstfile,rowsize,false,true,NULL,fast)); if (!dstio) { printf("ERROR: could not open '%s' for write\n",to); doexit(5); } #ifdef __linux__ // this is not really needed in windows - if it is we will have to // test the file extension - .exe, .bat struct stat info; if (stat(from, &info) == 0) // cannot fail - exception would have been thrown above dstfile->setCreateFlags(info.st_mode&(S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH|S_IXUSR|S_IXGRP|S_IXOTH)); #endif MemoryAttr mb; void * buffer = mb.allocate(BUFFERSIZE); offset_t offset = 0; try { loop { size32_t got = cmpio.get()?cmpio->read(offset, BUFFERSIZE, buffer):srcio->read(offset, BUFFERSIZE, buffer); if (got == 0) break; if (flzstrmdst) flzstrmdst->write(got,buffer); else dstio->write(offset, got, buffer); offset += got; } } catch (IException *e) { // try to delete partial copy dstio.clear(); try { dstfile->remove(); } catch (IException *e2) { StringBuffer s; pexception(s.clear().append("Removing partial copy file: ").append(to).str(),e2); e2->Release(); } throw e; } flzstrmdst.clear(); dstio.clear(); if (stats) printStats(offset,start,startu); CDateTime createTime, modifiedTime; if (srcfile->getTime(&createTime, &modifiedTime, NULL)) dstfile->setTime(&createTime, &modifiedTime, NULL); printf("copied %s to %s%s\n",from,to,plaincopy?"":" compressing"); { // print details dstio.setown(dstfile->open(IFOread)); if (dstio) { Owned<ICompressedFileIO> cmpio = createCompressedFileReader(dstio); Owned<IFileIOStream> flzstrm = cmpio?NULL:createFastLZStreamRead(dstio); if (cmpio||flzstrm) printCompDetails(to,dstio,cmpio,flzstrm); else printf("destination %s not compressed\n",to); } else printf("destination %s could not be read\n",to); } }
int copyExpanded(const char *from, const char *to, bool stats) { Owned<IFile> srcfile = createIFile(from); Owned<IFileIO> srcio = srcfile->open(IFOread); if (!srcio) { printf("ERROR: could not open '%s' for read\n",from); doexit(3); } Owned<ICompressedFileIO> cmpio = createCompressedFileReader(srcio); Owned<IFileIOStream> flzstrm = cmpio?NULL:createFastLZStreamRead(srcio); int ret = 0; if (cmpio||flzstrm) printCompDetails(from,srcio,cmpio,flzstrm); else { ret = 1; printf("%s is not compressed, size= %"I64F"d\n",from,srcio->size()); } if (!to||!*to) return ret; Owned<IFile> dstfile = createIFile(to); StringBuffer fulldst; if (dstfile->isDirectory()==foundYes) { dstfile.clear(); addPathSepChar(fulldst.append(to)).append(pathTail(from)); to = fulldst.str(); dstfile.setown(createIFile(to)); } if (dstfile->exists()) { printf("ERROR: file '%s' already exists\n",to); doexit(4); } unsigned start; unsigned startu; if (stats) { start = msTick(); startu = usTick(); } Owned<IFileIO> dstio = dstfile->open(IFOcreate); if (!dstio) { printf("ERROR: could not open '%s' for write\n",to); doexit(5); } #ifdef __linux__ // this is not really needed in windows - if it is we will have to // test the file extension - .exe, .bat struct stat info; if (stat(from, &info) == 0) // cannot fail - exception would have been thrown above dstfile->setCreateFlags(info.st_mode&(S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH|S_IXUSR|S_IXGRP|S_IXOTH)); #endif MemoryAttr mb; void * buffer = mb.allocate(BUFFERSIZE); offset_t offset = 0; try { loop { size32_t got = cmpio.get()?cmpio->read(offset,BUFFERSIZE, buffer): (flzstrm?flzstrm->read(BUFFERSIZE, buffer): srcio->read(offset, BUFFERSIZE, buffer)); if (got == 0) break; dstio->write(offset, got, buffer); offset += got; } } catch (IException *e) { // try to delete partial copy dstio.clear(); try { dstfile->remove(); } catch (IException *e2) { StringBuffer s; pexception(s.clear().append("Removing partial copy file: ").append(to).str(),e2); e2->Release(); } throw e; } dstio.clear(); if (stats) printStats(offset,start,startu); CDateTime createTime, modifiedTime; if (srcfile->getTime(&createTime, &modifiedTime, NULL)) dstfile->setTime(&createTime, &modifiedTime, NULL); printf("copied %s to %s%s\n",from,to,cmpio.get()?" expanding":""); return 0; }