int main(int argc, char* argv[]) { enableMemLeakChecking(true); InitModuleObjects(); EnableSEHtoExceptionMapping(); if (argc<2) { usage(argv[0]); return -1; } SocketEndpoint dalieps(argv[1],DALI_SERVER_PORT); // endpoint of dali server Owned<IGroup> group = createIGroup(1,&dalieps); try { initClientProcess(group, DCR_Other); // I will add a DCR_Orbit at some point try { doStuff(); } catch (IException *e) { pexception(argv[0],e); e->Release(); } closedownClientProcess(); } catch (IException *e) { pexception(argv[0],e); e->Release(); } releaseAtoms(); return 0; }
IHRPCtransport * TryMakeServerTransport(unsigned port, const char * errorMessage) { IHRPCtransport *transport = NULL; try { transport = MakeTcpTransport(NULL,port); } catch(IHRPC_Exception *e) { switch (e->errorCode()) { case HRPCERR_transport_port_in_use: default: if (errorMessage) pexception(errorMessage, e); e->Release(); break; } } return transport; }
int main(int argc, char* argv[]) { InitModuleObjects(); int exitCode = 1; try { if (argc<2) { printf("usage: dalistop <server_ip:port> [/nowait]\n"); printf("eg: dalistop . -- stop dali server running locally\n"); printf(" dalistop eq0001016 -- stop dali server running remotely\n"); } else { SocketEndpoint ep; ep.set(argv[1],DALI_SERVER_PORT); bool nowait = false; if (argc>=3) nowait = stricmp(argv[2],"/nowait")==0; printf("Stopping Dali Server on %s\n",argv[1]); startMPServer(0); Owned<IGroup> group = createIGroup(1,&ep); Owned<ICommunicator> comm = createCommunicator(group); CMessageBuffer mb; int fn=-1; mb.append(fn); if (comm->verifyConnection(0,2000)) { comm->send(mb,0,MPTAG_DALI_COVEN_REQUEST,MP_ASYNC_SEND); if (nowait) { Sleep(1000); exitCode = 0; } else { // verifyConnection() has a min conn timeout of 10s // use recv() instead to check for socket closed ... try { while (!comm->recv(mb,0,MPTAG_DALI_COVEN_REQUEST,nullptr,5000)) { printf("Waiting for Dali Server to stop....\n"); } exitCode = 0; } catch (IMP_Exception *e) { if (e->errorCode() == MPERR_link_closed) exitCode = 0; e->Release(); } } } else fprintf(stderr, "Dali not responding\n"); stopMPServer(); } } catch (IException *e) { pexception("Exception",e); stopMPServer(); } releaseAtoms(); return exitCode; }
int doSendQuery(const char * ip, unsigned port, const char * base) { Owned<ISocket> socket; Owned<ISecureSocketContext> secureContext; __int64 starttime, endtime; StringBuffer ipstr; try { if (strcmp(ip, ".")==0) ip = GetCachedHostName(); else { const char *dash = strchr(ip, '-'); if (dash && isdigit(dash[1]) && dash>ip && isdigit(dash[-1])) { if (persistConnections) UNIMPLEMENTED; const char *startrange = dash-1; while (isdigit(startrange[-1])) startrange--; char *endptr; unsigned firstnum = atoi(startrange); unsigned lastnum = strtol(dash+1, &endptr, 10); if (lastnum > firstnum) { static unsigned counter; static CriticalSection counterCrit; CriticalBlock b(counterCrit); ipstr.append(startrange - ip, ip).append((counter++ % (lastnum+1-firstnum)) + firstnum).append(endptr); ip = ipstr.str(); printf("Sending to %s\n", ip); } } } starttime= get_cycles_now(); if (persistConnections) { if (!persistSocket) { SocketEndpoint ep(ip,port); persistSocket.setown(ISocket::connect_timeout(ep, 1000)); if (useSSL) { #ifdef _USE_OPENSSL if (!persistSecureContext) persistSecureContext.setown(createSecureSocketContext(ClientSocket)); persistSSock.setown(persistSecureContext->createSecureSocket(persistSocket.getClear())); persistSSock->secure_connect(); persistSocket.setown(persistSSock.getClear()); #else throw MakeStringException(-1, "OpenSSL disabled in build"); #endif } } socket = persistSocket; } else { SocketEndpoint ep(ip,port); socket.setown(ISocket::connect_timeout(ep, 100000)); if (useSSL) { #ifdef _USE_OPENSSL secureContext.setown(createSecureSocketContext(ClientSocket)); Owned<ISecureSocket> ssock = secureContext->createSecureSocket(socket.getClear()); ssock->secure_connect(); socket.setown(ssock.getClear()); #else throw MakeStringException(1, "OpenSSL disabled in build"); #endif } } } catch(IException * e) { pexception("failed to connect to server", e); return 1; } StringBuffer fullQuery; bool useHTTP = forceHTTP || strstr(base, "<soap:Envelope") != NULL; if (useHTTP) { StringBuffer newQuery; Owned<IPTree> p = createPTreeFromXMLString(base, ipt_none, ptr_none); const char *queryName = p->queryName(); if ((stricmp(queryName, "envelope") != 0) && (stricmp(queryName, "envelope") != 0)) { if (queryNameOverride.length()) queryName = queryNameOverride; newQuery.appendf("<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><Body><%sRequest>", queryName); Owned<IPTreeIterator> elements = p->getElements("./*"); ForEach(*elements) { IPTree &elem = elements->query(); toXML(&elem, newQuery, 0, XML_SingleQuoteAttributeValues); } newQuery.appendf("</%sRequest></Body></Envelope>", queryName); base = newQuery.str(); } // note - don't support queryname override unless original query is xml fullQuery.appendf("POST /doc HTTP/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n", (int) strlen(base)).append(base); } else { if (sendToSocket)
int readResults(ISocket * socket, bool readBlocked, bool useHTTP, StringBuffer &result, const char *query, size32_t queryLen) { if (readBlocked) socket->set_block_mode(BF_SYNC_TRANSFER_PULL,0,60*1000); MemoryBuffer remoteReadCursorMb; unsigned len; bool is_status; bool isBlockedResult; for (;;) { if (delay) MilliSleep(delay); is_status = false; isBlockedResult = false; try { if (useHTTP) len = 0x10000; else if (readBlocked) len = socket->receive_block_size(); else { socket->read(&len, sizeof(len)); _WINREV(len); } } catch(IException * e) { if (manyResults) showMessage("End of result multiple set\n"); else pexception("failed to read len data", e); e->Release(); return 1; } if (len == 0) { if (manyResults) { showMessage("----End of result set----\n"); continue; } break; } bool isSpecial = false; bool pluginRequest = false; bool dataBlockRequest = false; bool remoteReadRequest = false; if (len & 0x80000000) { unsigned char flag; isSpecial = true; socket->read(&flag, sizeof(flag)); switch (flag) { case '-': if (echoResults) fputs("Error:", stdout); if (saveResults && trace != NULL) fputs("Error:", trace); break; case 'D': showMessage("request for datablock\n"); dataBlockRequest = true; break; case 'P': showMessage("request for plugin\n"); pluginRequest = true; break; case 'S': if (showStatus) showMessage("Status:"); is_status=true; break; case 'T': showMessage("Timing:\n"); break; case 'X': showMessage("---Compound query finished---\n"); return 1; case 'R': isBlockedResult = true; break; case 'J': remoteReadRequest = true; break; } len &= 0x7FFFFFFF; len--; // flag already read } MemoryBuffer mb; mb.setEndian(__BIG_ENDIAN); char *mem = (char *)mb.reserveTruncate(len+1); char * t = mem; size32_t sendlen = len; t[len]=0; try { if (useHTTP) { try { socket->read(t, 0, len, sendlen); } catch (IException *E) { if (E->errorCode()!= JSOCKERR_graceful_close) throw; E->Release(); break; } if (!sendlen) break; } else if (readBlocked) socket->receive_block(t, len); else socket->read(t, len); } catch(IException * e) { pexception("failed to read data", e); e->Release(); return 1; } if (pluginRequest) { //Not very robust! A poor man's implementation for testing... StringBuffer dllname, libname; const char * dot = strchr(t, '.'); dllname.append("\\edata\\bin\\debug\\").append(t); libname.append("\\edata\\bin\\debug\\").append(dot-t,t).append(".lib"); sendFile(dllname.str(), socket); sendFile(libname.str(), socket); } else if (dataBlockRequest) { //Not very robust! A poor man's implementation for testing... offset_t offset; mb.read(offset); sendFileChunk((const char *)mb.readDirect(offset), offset, socket); } else if (remoteReadRequest) { Owned<IPropertyTree> requestTree = createPTreeFromJSONString(queryLen, query); Owned<IPropertyTree> responseTree; // used if response is xml or json const char *outputFmtStr = requestTree->queryProp("format"); const char *response = nullptr; if (!outputFmtStr || strieq("xml", outputFmtStr)) { response = (const char *)mb.readDirect(len); responseTree.setown(createPTreeFromXMLString(len, response)); } else if (strieq("json", outputFmtStr)) { response = (const char *)mb.readDirect(len); responseTree.setown(createPTreeFromJSONString(len, response)); } unsigned cursorHandle; if (responseTree) cursorHandle = responseTree->getPropInt("cursor"); else mb.read(cursorHandle); bool retrySend = false; if (cursorHandle) { PROGLOG("Got handle back: %u; len=%u", cursorHandle, len); StringBuffer xml; if (responseTree) { if (echoResults && response) { fputs(response, stdout); fflush(stdout); } if (!responseTree->getPropBin("cursorBin", remoteReadCursorMb.clear())) break; } else { size32_t dataLen; mb.read(dataLen); if (!dataLen) break; const void *rowData = mb.readDirect(dataLen); // JCSMORE - output binary row data? // cursor size32_t cursorLen; mb.read(cursorLen); if (!cursorLen) break; const void *cursor = mb.readDirect(cursorLen); memcpy(remoteReadCursorMb.clear().reserveTruncate(cursorLen), cursor, cursorLen); } if (remoteStreamForceResend) cursorHandle = NotFound; // fake that it's a handle dafilesrv doesn't know about Owned<IPropertyTree> requestTree = createPTree(); requestTree->setPropInt("cursor", cursorHandle); // Only the handle is needed for continuation, but this tests the behaviour of some clients which may send cursor per request (e.g. to refresh) if (remoteStreamSendCursor) requestTree->setPropBin("cursorBin", remoteReadCursorMb.length(), remoteReadCursorMb.toByteArray()); requestTree->setProp("format", outputFmtStr); StringBuffer requestStr; toJSON(requestTree, requestStr); #ifdef _DEBUG fputs(requestStr, stdout); #endif sendlen = requestStr.length(); _WINREV(sendlen); try { if (!rawSend && !useHTTP) socket->write(&sendlen, sizeof(sendlen)); socket->write(requestStr.str(), requestStr.length()); } catch (IJSOCK_Exception *e) { retrySend = true; EXCLOG(e, nullptr); e->Release(); } } else // dafilesrv didn't know who I was, resent query + serialized cursor retrySend = true; if (retrySend) { PROGLOG("Retry send for handle: %u", cursorHandle); requestTree->setPropBin("cursorBin", remoteReadCursorMb.length(), remoteReadCursorMb.toByteArray()); StringBuffer requestStr; toJSON(requestTree, requestStr); PROGLOG("requestStr = %s", requestStr.str()); sendlen = requestStr.length(); _WINREV(sendlen); if (!rawSend && !useHTTP) socket->write(&sendlen, sizeof(sendlen)); socket->write(requestStr.str(), requestStr.length()); } } else { if (isBlockedResult) { t += 8; t += strlen(t)+1; sendlen -= (t - mem); } if (echoResults && (!is_status || showStatus)) { fwrite(t, sendlen, 1, stdout); fflush(stdout); } if (!is_status) result.append(sendlen, t); } if (abortAfterFirst) return 0; } return 0; }
void ppreprocessor::error(const pstring &err) { throw pexception("PREPRO ERROR: " + err); }
int readResults(ISocket * socket, bool readBlocked, bool useHTTP, StringBuffer &result) { if (readBlocked) socket->set_block_mode(BF_SYNC_TRANSFER_PULL,0,60*1000); unsigned len; bool is_status; bool isBlockedResult; for (;;) { if (delay) MilliSleep(delay); is_status = false; isBlockedResult = false; try { if (useHTTP) len = 0x10000; else if (readBlocked) len = socket->receive_block_size(); else { socket->read(&len, sizeof(len)); _WINREV(len); } } catch(IException * e) { if (manyResults) showMessage("End of result multiple set\n"); else pexception("failed to read len data", e); e->Release(); return 1; } if (len == 0) { if (manyResults) { showMessage("----End of result set----\n"); continue; } break; } bool isSpecial = false; bool pluginRequest = false; bool dataBlockRequest = false; if (len & 0x80000000) { unsigned char flag; isSpecial = true; socket->read(&flag, sizeof(flag)); switch (flag) { case '-': if (echoResults) fputs("Error:", stdout); if (saveResults && trace != NULL) fputs("Error:", trace); break; case 'D': showMessage("request for datablock\n"); dataBlockRequest = true; break; case 'P': showMessage("request for plugin\n"); pluginRequest = true; break; case 'S': if (showStatus) showMessage("Status:"); is_status=true; break; case 'T': showMessage("Timing:\n"); break; case 'X': showMessage("---Compound query finished---\n"); return 1; case 'R': isBlockedResult = true; break; } len &= 0x7FFFFFFF; len--; // flag already read } char * mem = (char*) malloc(len+1); char * t = mem; unsigned sendlen = len; t[len]=0; try { if (useHTTP) { try { socket->read(t, 0, len, sendlen); } catch (IException *E) { if (E->errorCode()!= JSOCKERR_graceful_close) throw; E->Release(); break; } if (!sendlen) break; } else if (readBlocked) socket->receive_block(t, len); else socket->read(t, len); } catch(IException * e) { pexception("failed to read data", e); return 1; } if (pluginRequest) { //Not very robust! A poor man's implementation for testing... StringBuffer dllname, libname; const char * dot = strchr(t, '.'); dllname.append("\\edata\\bin\\debug\\").append(t); libname.append("\\edata\\bin\\debug\\").append(dot-t,t).append(".lib"); sendFile(dllname.str(), socket); sendFile(libname.str(), socket); } else if (dataBlockRequest) { //Not very robust! A poor man's implementation for testing... offset_t offset; memcpy(&offset, t, sizeof(offset)); _WINREV(offset); sendFileChunk(t+sizeof(offset), offset, socket); } else { if (isBlockedResult) { t += 8; t += strlen(t)+1; sendlen -= (t - mem); } if (echoResults && (!is_status || showStatus)) { fwrite(t, sendlen, 1, stdout); fflush(stdout); } if (!is_status) result.append(sendlen, t); } free(mem); if (abortAfterFirst) return 0; } return 0; }
int main( int argc, char *argv[] ) { int res=0; if (argc < 3) { printf("frunagent <nodelist> start \"command\" [options] \n" " stop <commandid> [options]\n" " account <user> <password> <dir> [option]\n" " dir <dir> [option]\n" " alive <integer> [option]\n\n" "where <nodelist> is of the form h009100:h009119,h007010:h007020\n" "or 192.168.6.100:192.168.6.119,192.168.7.10:192.168.7.20\n" "or @filename where filename contains a file in the above format\n" "options: /n<number_of_thread> /t<call_time_out> /encrypt /o<replication_offset>\n" ); return 255; } InitModuleObjects(); StringBuffer tracepath; tracepath.append(".").append(PATHSEPCHAR).append("frunagent.txt"); settrace(tracepath.str(),false); ECHO_TO_CONSOLE=true; try { setoptions(argc,argv); split_node x(argc,argv); if (argv[1][0]=='@') { StringBuffer b; b.loadFile(argv[1]+1); char *finger = (char *) b.str(); while (*finger) { if (*finger == '\n') *finger++ = ';'; else if (*finger == '#') { while (*finger && *finger != '\n') *finger++ = ' '; } else finger++; } x.split_nodes_ex(b.str(),replicationoffset); } else x.split_nodes_ex(argv[1],replicationoffset); disp->all_done_ex(false); } catch(IException *e) { pexception("",e); e->Release(); res=255; } catch (...) { traceft("Caught unknown exception"); } #ifdef _TRACING traceflush(); #endif if (disp) delete disp; return res; }
int main(int argc, char* argv[]) { InitModuleObjects(); EnableSEHtoExceptionMapping(); if (argc<2) { usage(argv[0]); return -1; } bool forceGroupUpdate = false; StringBuffer filename; StringBuffer inst; StringBuffer dcat; StringBuffer dcomp; StringBuffer dinst; StringBuffer dip; for (int i=1;i<argc;i++) { if (argv[i][0]=='-') { if ((stricmp(argv[i],"-i")==0)&&(i+1<argc)) { inst.append(argv[++i]); } else if (0==stricmp(argv[i],"-f")) { forceGroupUpdate = true; } else if ((stricmp(argv[i],"-d")==0)&&(i+3<argc)) { dcat.append(argv[++i]); dcomp.append(argv[++i]); dinst.append(argv[++i]); } else if ((stricmp(argv[i],"-ip")==0)&&(i+1<argc)) { dip.append(argv[++i]); } else { usage(argv[0]); return -1; } } else { if (filename.length()) { usage(argv[0]); return -1; } filename.append(argv[i]); } } Owned<IPropertyTree> env; try { env.setown(createPTreeFromXMLFile(argv[1])); if (!env.get()) { fprintf(stderr,"Could not load Environment from %s\n",argv[1]); return 1; } const char *s = env->queryName(); if (!s||(strcmp(s,"Environment")!=0)) { fprintf(stderr,"File %s is invalid\n",argv[1]); return 1; } } catch (IException *e) { StringBuffer err; e->errorMessage(err); fprintf(stderr,"Could not load Environment from %s, %s\n",argv[1],err.str()); return 1; } int ret = 0; try { if (dcat.length()) { IPropertyTree* dirs = env->queryPropTree("Software/Directories"); StringBuffer dirout; if (getConfigurationDirectory(dirs,dcat.str(),dcomp.str(),dinst.str(),dirout)&&dirout.length()) { if (dip.length()) { SocketEndpoint ep(dip.str()); RemoteFilename rfn; rfn.setPath(ep,dirout.str()); rfn.getRemotePath(dirout.clear()); } printf("%s",dirout.str()); } else { ret = 1; } } else { if (!updateDaliEnv(env, forceGroupUpdate, inst.str())) ret = 1; } } catch (IException *e) { pexception("updtdalienv",e); e->Release(); ret = 1; } releaseAtoms(); return ret; }
int CMCastRecvServer::run() { SocketEndpoint ackEp(broadcastRoot); ackEp.port = ackPort; StringBuffer s; ackEp.getIpText(s); ackSock.setown(ISocket::udp_connect(ackEp.port, s.str())); ackSock->set_send_buffer_size(UDP_SEND_SIZE); StringBuffer ipStr; mcastEp.getIpText(ipStr); sock.setown(ISocket::multicast_create(mcastEp.port, ipStr.str())); sock->set_receive_buffer_size(UDP_RECV_SIZE); SocketEndpoint ep(ipStr.str()); sock->join_multicast_group(ep); MemoryBuffer mbAck; MCAckPacketHeader *ackPacket = (MCAckPacketHeader *)mbAck.reserveTruncate(MC_ACK_PACKET_SIZE); ackPacket->node = groupMember; LOG(MCdebugProgress(10), unknownJob, "Running as client %d connected to server %s", groupMember, broadcastRoot.get()); unsigned *nackList = (unsigned *)(((byte *)ackPacket)+sizeof(MCAckPacketHeader)); const unsigned *nackUpper = (unsigned *)((byte *)ackPacket)+MC_ACK_PACKET_SIZE-sizeof(unsigned); Owned<CDataPacket> dataPacket = new CDataPacket(); CTimeMon logTm(10000), logTmCons(5000), logTmPoll(5000), logTmOld(5000), logTmNoRecv(5000); loop { try { unsigned startTime = msTick(); loop { try { size32_t szRead; sock->read(dataPacket->header, sizeof(MCPacketHeader), MC_PACKET_SIZE, szRead, 5000); break; } catch (IException *e) { if (JSOCKERR_timeout_expired != e->errorCode()) throw; else e->Release(); LOG(MCdebugProgress(1), unknownJob, "Waiting on packet read socket (waited=%d)", msTick()-startTime); } } if (stopped) break; if (MCPacket_Stop == dataPacket->header->cmd) { stopped = true; break; } ackPacket->tag = dataPacket->header->tag; ackPacket->jobId = dataPacket->header->jobId; if (oldJobIds.find(dataPacket->header->jobId)) { if (MCPacket_Poll == dataPacket->header->cmd) { ackPacket->ackDone = true; MilliSleep(MAX_POLL_REPLY_DELAY/(groupMember+1)); ackSock->write(ackPacket, sizeof(MCAckPacketHeader)); } if (tracingPeriod && logTmOld.timedout()) { LOG(MCdebugProgress(1), unknownJob, "Old job polled=%s", MCPacket_Poll == dataPacket->header->cmd?"true":"false"); logTmOld.reset(tracingPeriod); } } else { CMCastReceiver *receiver = getReceiver(dataPacket->header->tag); if (receiver) { if (MCPacket_Poll == dataPacket->header->cmd) { size32_t sz; bool res = receiver->buildNack(ackPacket, sz, dataPacket->header->total); MilliSleep(MAX_POLL_REPLY_DELAY/(groupMember+1)); ackSock->write(ackPacket, sz); if (tracingPeriod && logTmPoll.timedout()) { LOG(MCdebugProgress(1), unknownJob, "Send nack back sz=%d, res=%s, done=%s", sz, res?"true":"false", ackPacket->ackDone?"true":"false"); logTmPoll.reset(tracingPeriod); } } else { unsigned total = dataPacket->header->total; bool done; if (receiver->packetReceived(*dataPacket, done)) // if true, packet consumed { unsigned level; if (tracingPeriod && logTmCons.timedout()) { level = 1; logTmCons.reset(5000); } else level = 110; LOG(MCdebugProgress(level), unknownJob, "Pkt %d taken by receiver", dataPacket->header->id); if (done) { LOG(MCdebugProgress(10), unknownJob, "Client (tag=%x, jobId=%d) received all %d packets", dataPacket->header->tag, dataPacket->header->jobId, dataPacket->header->total); oldJobIds.replace(* new CUIntValue(dataPacket->header->jobId)); } // JCSMORE should use packet pool. // init new packet dataPacket.setown(new CDataPacket()); } else if (tracingPeriod && logTm.timedout()) { LOG(MCdebugProgress(150), unknownJob, "throwing away packet %d", dataPacket->header->id); logTm.reset(tracingPeriod); } if (!done) { size32_t sz; if (receiver->buildNack(ackPacket, sz, total)) ackSock->write(ackPacket, sz); } } } else if (tracingPeriod && logTmNoRecv.timedout()) { LOG(MCdebugProgress(1), unknownJob, "No Receiver tag=%d", dataPacket->header->tag); logTmNoRecv.reset(tracingPeriod); } } } catch (IException *e) { pexception("Client Exception",e); break; } } PROGLOG("Receive server stopping, aborting receivers"); { CriticalBlock b(receiversCrit); SuperHashIteratorOf<CMCastReceiver> iter(receivers); ForEach (iter) iter.query().stop(); } return 0; }
int main(int argc, char * const * argv) { InitModuleObjects(); int ret = 0; try { bool test=false; unsigned arg = 1; StringBuffer fname1; StringBuffer fname2; bool lzw = false; bool fast = false; bool flzstrm = false; bool stats = false; size32_t rowsz = 0; for (int a = 1; a<argc; a++) { const char *arg = argv[a]; if (arg[0]=='-') { if(strcmp(arg, "-t") == 0) test = true; else if(strcmp(arg, "-?") == 0) usage(true); else if(strcmp(arg, "-h") == 0) usage(true); else if(strcmp(arg, "-z") == 0) { lzw = true; continue; } else if(strcmp(arg, "-s") == 0) { stats = true; continue; } else if(strcmp(arg, "-f") == 0) { fast = true; continue; } else if(strcmp(arg, "-fs") == 0) { flzstrm = true; continue; } else if(strcmp(arg, "-r") == 0) { if (a+1<argc) { rowsz = atoi(argv[a+1]); if (rowsz) { a++; continue; } } usage(false); } else { printf("ERROR unexpected parameter '%s'",arg); usage(false); } } if (fname1.length()) { if (test||fname2.length()) { printf("ERROR unexpected parameter '%s'",arg); usage(false); } fname2.append(arg); } else fname1.append(arg); } if (!fname1.length()) usage(true); if (!fast&&!lzw&&!rowsz&&!flzstrm) copyExpanded(fname1.str(),fname2.str(),stats); else copyCompress(fname1.str(),fname2.str(),rowsz,fast,flzstrm,stats); } catch(IException * e) { pexception("copyexp: ",e); e->Release(); ret = 99; } releaseAtoms(); return ret; }
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; }