ws_fsService.cpp 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning (disable : 4786)
  14. #pragma warning (disable : 4129)
  15. #include <math.h>
  16. #include "jsocket.hpp"
  17. #include "dasds.hpp"
  18. #include "dadfs.hpp"
  19. #include "dautils.hpp"
  20. #include "daclient.hpp"
  21. #include "wshelpers.hpp"
  22. #include "dfuwu.hpp"
  23. #include "ws_fsService.hpp"
  24. #ifdef _WIN32
  25. #include "windows.h"
  26. #endif
  27. #include "TpWrapper.hpp"
  28. #include "LogicFileWrapper.hpp"
  29. #include "dfuutil.hpp"
  30. #include "portlist.h"
  31. #include "sacmd.hpp"
  32. #include "exception_util.hpp"
  33. #define DFU_WU_URL "DfuWorkunitsAccess"
  34. #define DFU_EX_URL "DfuExceptionsAccess"
  35. #define FILE_SPRAY_URL "FileSprayAccess"
  36. #define FILE_DESPRAY_URL "FileDesprayAccess"
  37. #define WUDETAILS_REFRESH_MINS 1
  38. void SetResp(StringBuffer &resp, IConstDFUWorkUnit * wu, bool array);
  39. int Schedule::run()
  40. {
  41. try
  42. {
  43. while(true)
  44. {
  45. {
  46. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  47. Owned<IConstDFUWorkUnitIterator> itr = factory->getWorkUnitsByState(DFUstate_scheduled);
  48. itr->first();
  49. while(itr->isValid())
  50. {
  51. Owned<IConstDFUWorkUnit> wu = itr->get();
  52. CDateTime dt, now;
  53. now.setNow();
  54. try
  55. {
  56. wu->getTimeScheduled(dt);
  57. if (now.compare(dt) > 0)
  58. {
  59. StringAttr wuid(wu->queryId());
  60. wu.clear();
  61. submitDFUWorkUnit(wuid.get());
  62. }
  63. }
  64. catch(IException *e)
  65. {
  66. StringBuffer msg;
  67. ERRLOG("Exception %d:%s in WsWorkunits Schedule::run", e->errorCode(), e->errorMessage(msg).str());
  68. e->Release();
  69. }
  70. itr->next();
  71. }
  72. }
  73. sleep(60);
  74. }
  75. }
  76. catch(IException *e)
  77. {
  78. StringBuffer msg;
  79. ERRLOG("Exception %d:%s in WS_FS Schedule::run", e->errorCode(), e->errorMessage(msg).str());
  80. e->Release();
  81. }
  82. catch(...)
  83. {
  84. ERRLOG("Unknown exception in WS_FS Schedule::run");
  85. }
  86. return 0;
  87. }
  88. void CFileSprayEx::init(IPropertyTree *cfg, const char *process, const char *service)
  89. {
  90. StringBuffer xpath;
  91. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/QueueLabel", process, service);
  92. cfg->getProp(xpath.str(), m_QueueLabel);
  93. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/MonitorQueueLabel", process, service);
  94. cfg->getProp(xpath.str(), m_MonitorQueueLabel);
  95. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/RootFolder", process, service);
  96. cfg->getProp(xpath.str(), m_RootFolder);
  97. directories.set(cfg->queryPropTree("Software/Directories"));
  98. StringBuffer prop;
  99. prop.appendf("queueLabel=%s", m_QueueLabel.str());
  100. PrintLog(prop.str());
  101. prop.clear();
  102. prop.appendf("monitorQueueLabel=%s", m_MonitorQueueLabel.str());
  103. PrintLog(prop.str());
  104. prop.clear();
  105. prop.appendf("rootFolder=%s", m_RootFolder.str());
  106. PrintLog(prop.str());
  107. if (!daliClientActive())
  108. {
  109. ERRLOG("No Dali Connection Active.");
  110. throw MakeStringException(-1, "No Dali Connection Active. Please Specify a Dali to connect to in you configuration file");
  111. }
  112. m_sched.start();
  113. }
  114. void ParsePath(const char * fullPath, StringBuffer &ip, StringBuffer &filePath, StringBuffer &title)
  115. {
  116. ip.clear();
  117. filePath.clear();
  118. title.clear();
  119. if(fullPath == NULL || *fullPath == '\0')
  120. return;
  121. const char* ptr = fullPath;
  122. if(*ptr == '\\' && *(ptr+1) == '\\')
  123. {
  124. ptr += 2;
  125. while(*ptr != '\0' && *ptr != '\\')
  126. ptr++;
  127. ip.append(ptr - fullPath - 2, fullPath + 2);
  128. }
  129. filePath.append(ptr);
  130. ptr = fullPath + strlen(fullPath) - 1;
  131. while(ptr > fullPath && *ptr != '\\')
  132. ptr--;
  133. title.append(ptr + 1);
  134. }
  135. const char * const NODATETIME="1970-01-01T00:00:00Z";
  136. // Assign from a dfuwu workunit structure to an esp request workunit structure.
  137. static void DeepAssign(IEspContext &context, IConstDFUWorkUnit *src, IEspDFUWorkunit &dest)
  138. {
  139. if(src == NULL)
  140. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "'Source DFU workunit' doesn't exist.");
  141. if(&dest == NULL)
  142. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "'Destination DFU workunit' not valid.");
  143. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory();
  144. Owned<IConstEnvironment> constEnv = envFactory->openEnvironmentByFile();
  145. Owned<IPropertyTree> root = &constEnv->getPTree();
  146. if (!root)
  147. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  148. double version = context.getClientVersion();
  149. StringBuffer tmp, encoded;
  150. dest.setID(src->queryId());
  151. if (src->getClusterName(tmp.clear()).length()!=0)
  152. {
  153. char *clusterName = (char *)tmp.str();
  154. if (clusterName && *clusterName)
  155. {
  156. StringBuffer clusterNameForDisplay(clusterName);
  157. Owned<IPropertyTreeIterator> clusters= root->getElements("Software/Topology/Cluster");
  158. if (clusters->first())
  159. {
  160. do {
  161. IPropertyTree &cluster = clusters->query();
  162. const char* name = cluster.queryProp("@name");
  163. if (!name || !*name)
  164. continue;
  165. Owned<IPropertyTreeIterator> thorClusters= cluster.getElements(eqThorCluster);
  166. Owned<IPropertyTreeIterator> roxieClusters= cluster.getElements(eqRoxieCluster);
  167. if (thorClusters->first() || roxieClusters->first())
  168. {
  169. if (thorClusters->first())
  170. {
  171. IPropertyTree &thorCluster = thorClusters->query();
  172. const char* process = thorCluster.queryProp("@process");
  173. if (process && *process)
  174. {
  175. if (clusterName && !stricmp(clusterName, process))
  176. {
  177. clusterNameForDisplay.clear().append(name);
  178. break;
  179. }
  180. }
  181. }
  182. if (roxieClusters->first())
  183. {
  184. IPropertyTree &roxieCluster = roxieClusters->query();
  185. const char* process = roxieCluster.queryProp("@process");
  186. if (process && *process)
  187. {
  188. if (clusterName && !stricmp(clusterName, name))
  189. {
  190. clusterNameForDisplay.clear().append(name);
  191. break;
  192. }
  193. }
  194. }
  195. }
  196. } while (clusters->next());
  197. }
  198. dest.setClusterName(clusterNameForDisplay.str());
  199. }
  200. }
  201. if ((version > 1.05) && src->getDFUServerName(tmp.clear()).length())
  202. dest.setDFUServerName(tmp.str());
  203. if (src->getJobName(tmp.clear()).length()!=0)
  204. dest.setJobName(tmp.str());
  205. else
  206. dest.setJobName("");
  207. if (src->getQueue(tmp.clear()).length()!=0)
  208. dest.setQueue(tmp.str());
  209. if (src->getUser(tmp.clear()).length()!=0)
  210. dest.setUser(tmp.str());
  211. dest.setIsProtected(src->isProtected());
  212. dest.setCommand(src->getCommand());
  213. IConstDFUprogress *prog = src->queryProgress();
  214. if (prog != NULL)
  215. {
  216. DFUstate state = prog->getState();
  217. dest.setState(state);
  218. StringBuffer statemsg;
  219. encodeDFUstate(state,statemsg);
  220. dest.setStateMessage(statemsg.str());
  221. CDateTime startAt;
  222. CDateTime stoppAt;
  223. prog->getTimeStarted(startAt);
  224. prog->getTimeStopped(stoppAt);
  225. StringBuffer tmpstr;
  226. startAt.getDateString(tmpstr);
  227. tmpstr.append(" ");
  228. startAt.getTimeString(tmpstr);
  229. dest.setTimeStarted(tmpstr.str());
  230. tmpstr.clear();
  231. stoppAt.getDateString(tmpstr);
  232. tmpstr.append(" ");
  233. stoppAt.getTimeString(tmpstr);
  234. dest.setTimeStopped(tmpstr.str());
  235. StringBuffer prgmsg;
  236. prog->formatProgressMessage(prgmsg);
  237. dest.setProgressMessage(prgmsg.str());
  238. prog->formatSummaryMessage(prgmsg.clear());
  239. dest.setSummaryMessage(prgmsg.str());
  240. unsigned secs = prog->getSecsLeft();
  241. if(secs > 0)
  242. dest.setSecsLeft(secs);
  243. dest.setPercentDone(prog->getPercentDone());
  244. }
  245. IConstDFUoptions *options = src->queryOptions();
  246. if(options)
  247. {
  248. dest.setReplicate(options->getReplicate());
  249. dest.setOverwrite(options->getOverwrite());
  250. }
  251. IConstDFUfileSpec * file = src->querySource();
  252. if (file != NULL)
  253. {
  254. //if (file->getTitle(tmp.clear()).length()!=0)
  255. // dest.setSourceTitle(tmp.str());
  256. StringBuffer lfn;
  257. file->getLogicalName(lfn);
  258. if (lfn.length() != 0)
  259. dest.setSourceLogicalName(lfn.str());
  260. else
  261. dest.setSourceFormat(file->getFormat());
  262. if (file->getRawDirectory(tmp.clear()).length()!=0)
  263. dest.setSourceDirectory(tmp.str());
  264. SocketEndpoint srcdali;
  265. StringBuffer srcdaliip;
  266. file->getForeignDali(srcdali);
  267. srcdali.getIpText(srcdaliip);
  268. if(srcdaliip.length() > 0 && strcmp(srcdaliip.str(), "0.0.0.0") != 0)
  269. dest.setSourceDali(srcdaliip.str());
  270. StringBuffer diffkeyname;
  271. file->getDiffKey(diffkeyname);
  272. if(diffkeyname.length() > 0)
  273. dest.setSourceDiffKeyName(diffkeyname.str());
  274. StringBuffer socket, dir, title;
  275. unsigned np = file->getNumParts(0); // should handle multiple clusters?
  276. if (lfn.length() == 0) { // no logical name
  277. if (np == 1)
  278. {
  279. Owned<IFileDescriptor> info;
  280. try
  281. {
  282. info.setown(file->getFileDescriptor());
  283. if(info)
  284. {
  285. Owned<INode> node = info->getNode(0);
  286. if (node)
  287. {
  288. node->endpoint().getIpText(socket);
  289. dest.setSourceIP(socket.str());
  290. }
  291. const char *defaultdir = info->queryDefaultDir();
  292. if (defaultdir&&*defaultdir)
  293. addPathSepChar(dir.append(defaultdir));
  294. file->getRawFileMask(dir);
  295. dest.setSourceFilePath(dir.str());
  296. }
  297. }
  298. catch(IException *e)
  299. {
  300. EXCLOG(e,"DeepAssign getFileDescriptor");
  301. e->Release();
  302. }
  303. }
  304. }
  305. if (np)
  306. dest.setSourceNumParts(np);
  307. unsigned rs = file->getRecordSize();
  308. if (rs)
  309. dest.setSourceRecordSize(rs);
  310. StringBuffer rowtag;
  311. file->getRowTag(rowtag);
  312. if(rowtag.length() > 0)
  313. dest.setRowTag(rowtag.str());
  314. if (version >= 1.04 && (file->getFormat() == DFUff_csv))
  315. {
  316. StringBuffer separate, terminate, quote, escape;
  317. file->getCsvOptions(separate,terminate,quote, escape);
  318. if(separate.length() > 0)
  319. dest.setSourceCsvSeparate(separate.str());
  320. if(terminate.length() > 0)
  321. dest.setSourceCsvTerminate(terminate.str());
  322. if(quote.length() > 0)
  323. dest.setSourceCsvQuote(quote.str());
  324. if((version >= 1.05) && (escape.length() > 0))
  325. dest.setSourceCsvEscape(escape.str());
  326. }
  327. }
  328. file = src->queryDestination();
  329. if (file != NULL)
  330. {
  331. StringBuffer lfn;
  332. file->getLogicalName(lfn);
  333. if (lfn.length() != 0)
  334. dest.setDestLogicalName(lfn.str());
  335. else
  336. dest.setDestFormat(file->getFormat());
  337. if (file->getRawDirectory(tmp.clear()).length()!=0)
  338. dest.setDestDirectory(tmp.str());
  339. if (file->getGroupName(0,tmp.clear()).length()!=0) // should handle multiple clusters?
  340. {
  341. char *clusterName = (char *)tmp.str();
  342. if (clusterName)
  343. dest.setDestGroupName(clusterName);
  344. }
  345. StringBuffer socket, dir, title;
  346. unsigned np = file->getNumParts(0); // should handle multiple clusters?
  347. if (lfn.length() == 0) { // no logical name
  348. if (np == 1)
  349. {
  350. Owned<IFileDescriptor> info;
  351. try
  352. {
  353. info.setown(file->getFileDescriptor());
  354. if(info)
  355. {
  356. Owned<INode> node = info->getNode(0);
  357. if (node)
  358. {
  359. node->endpoint().getIpText(socket);
  360. dest.setDestIP(socket.str());
  361. }
  362. const char *defaultdir = info->queryDefaultDir();
  363. if (defaultdir&&*defaultdir)
  364. addPathSepChar(dir.append(defaultdir));
  365. file->getRawFileMask(dir);
  366. dest.setDestFilePath(dir.str());
  367. }
  368. }
  369. catch(IException *e)
  370. {
  371. EXCLOG(e,"DeepAssign getFileDescriptor dest");
  372. e->Release();
  373. }
  374. }
  375. }
  376. if (np)
  377. dest.setDestNumParts(np);
  378. unsigned rs = file->getRecordSize();
  379. if (rs)
  380. dest.setDestRecordSize(rs);
  381. dest.setCompress(file->isCompressed());
  382. }
  383. // monitor stuff
  384. IConstDFUmonitor *monitor = src->queryMonitor();
  385. if (monitor) {
  386. monitor->getEventName(tmp.clear());
  387. if (tmp.length())
  388. dest.setMonitorEventName(tmp.str());
  389. bool sub = monitor->getSub();
  390. dest.setMonitorSub(sub);
  391. unsigned sl = monitor->getShotLimit();
  392. if (sl)
  393. dest.setMonitorShotLimit(sl);
  394. }
  395. }
  396. bool CFileSprayEx::ParseLogicalPath(const char * pLogicalPath, const char* cluster, StringBuffer &folder, StringBuffer &title, StringBuffer &defaultFolder, StringBuffer &defaultReplicateFolder)
  397. {
  398. if(!pLogicalPath || !*pLogicalPath)
  399. return false;
  400. folder.clear();
  401. title.clear();
  402. defaultFolder.clear();
  403. defaultReplicateFolder.clear();
  404. DFD_OS os = DFD_OSdefault;
  405. if(cluster != NULL && *cluster != '\0')
  406. {
  407. Owned<IGroup> group = queryNamedGroupStore().lookup(cluster);
  408. if (group) {
  409. switch (queryOS(group->queryNode(0).endpoint())) {
  410. case MachineOsW2K:
  411. os = DFD_OSwindows; break;
  412. case MachineOsSolaris:
  413. case MachineOsLinux:
  414. os = DFD_OSunix; break;
  415. }
  416. if (directories.get()) {
  417. getConfigurationDirectory(directories, "data", "thor", cluster, defaultFolder);
  418. getConfigurationDirectory(directories, "mirror", "thor", cluster, defaultReplicateFolder);
  419. }
  420. }
  421. else
  422. {
  423. // Error here?
  424. }
  425. }
  426. makePhysicalPartName(pLogicalPath,0,0,folder,false,os,defaultFolder.str());
  427. const char *n = pLogicalPath;
  428. const char* p;
  429. do {
  430. p = strstr(n,"::");
  431. if(p)
  432. n = p+2;
  433. } while(p);
  434. title.append(n);
  435. return true;
  436. }
  437. bool CFileSprayEx::ParseLogicalPath(const char * pLogicalPath, StringBuffer &title)
  438. {
  439. if(!pLogicalPath || !*pLogicalPath)
  440. return false;
  441. title.clear();
  442. const char *n = pLogicalPath;
  443. const char* p;
  444. do {
  445. p = strstr(n,"::");
  446. if(p)
  447. n = p+2;
  448. } while(p);
  449. title.append(n);
  450. return true;
  451. }
  452. DFUclusterPartDiskMapping readClusterMappingSettings(const char *cluster, StringBuffer &dir, int& offset)
  453. {
  454. DFUclusterPartDiskMapping mapping = DFUcpdm_c_only;
  455. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory();
  456. envFactory->validateCache();
  457. StringBuffer dirxpath;
  458. dirxpath.appendf("Software/RoxieCluster[@name=\"%s\"]",cluster);
  459. Owned<IConstEnvironment> constEnv = envFactory->openEnvironmentByFile();
  460. Owned<IPropertyTree> pEnvRoot = &constEnv->getPTree();
  461. Owned<IPropertyTreeIterator> processes = pEnvRoot->getElements(dirxpath);
  462. if (processes->first())
  463. {
  464. IPropertyTree &processe = processes->query();
  465. const char *slaveConfig = processe.queryProp("@slaveConfig");
  466. if (slaveConfig && *slaveConfig)
  467. {
  468. if (!stricmp(slaveConfig, "simple"))
  469. {
  470. mapping = DFUcpdm_c_only;
  471. }
  472. else if (!stricmp(slaveConfig, "overloaded"))
  473. {
  474. mapping = DFUcpdm_c_then_d;
  475. }
  476. else if (!stricmp(slaveConfig, "full_redundancy"))
  477. {
  478. ;
  479. }
  480. else //circular redundancy
  481. {
  482. mapping = DFUcpdm_c_replicated_by_d;
  483. offset = processe.getPropInt("@cyclicOffset");
  484. }
  485. dir = processe.queryProp("@slaveDataDir");
  486. }
  487. else
  488. {
  489. DBGLOG("Failed to get RoxieCluster settings");
  490. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_INFO, "Failed to get RoxieCluster settings. The workunit will not be created.");
  491. }
  492. }
  493. else
  494. {
  495. DBGLOG("Failed to get RoxieCluster settings");
  496. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_INFO, "Failed to get RoxieCluster settings. The workunit will not be created.");
  497. }
  498. return mapping;
  499. }
  500. void getClusterFromLFN(const char* lfn, StringBuffer& cluster, const char* username, const char* passwd)
  501. {
  502. Owned<IUserDescriptor> udesc;
  503. if(username != NULL && *username != '\0')
  504. {
  505. udesc.setown(createUserDescriptor());
  506. udesc->set(username, passwd);
  507. }
  508. LogicFileWrapper lfw;
  509. lfw.FindClusterName(lfn, cluster, udesc);
  510. }
  511. StringBuffer& constructFileMask(const char* filename, StringBuffer& filemask)
  512. {
  513. filemask.clear().append(filename).toLowerCase().append("._$P$_of_$N$");
  514. return filemask;
  515. }
  516. bool CFileSprayEx::onDFUWUSearch(IEspContext &context, IEspDFUWUSearchRequest & req, IEspDFUWUSearchResponse & resp)
  517. {
  518. try
  519. {
  520. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Read, false))
  521. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  522. StringArray dfuclusters;
  523. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  524. Owned<IConstEnvironment> environment = factory->openEnvironmentByFile();
  525. Owned<IPropertyTree> root = &environment->getPTree();
  526. if (!root)
  527. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  528. Owned<IPropertyTreeIterator> clusterIterator = root->getElements("Software/Topology/Cluster");
  529. if (clusterIterator->first())
  530. {
  531. do {
  532. IPropertyTree &cluster = clusterIterator->query();
  533. const char *clusterName = cluster.queryProp("@name");
  534. if (!clusterName || !*clusterName)
  535. continue;
  536. dfuclusters.append(clusterName);
  537. } while (clusterIterator->next());
  538. }
  539. resp.setClusterNames(dfuclusters);
  540. }
  541. catch(IException* e)
  542. {
  543. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  544. }
  545. return true;
  546. }
  547. int readFromCommaSeparatedString(const char *commaSeparatedString, StringBuffer* output)
  548. {
  549. int numOfItems = 0;
  550. if (commaSeparatedString && *commaSeparatedString)
  551. {
  552. char *pStr = (char *) commaSeparatedString;
  553. while (pStr)
  554. {
  555. char item[1024];
  556. bool bFoundComma = false;
  557. int len = strlen(pStr);
  558. for (int i = 0; i < len; i++)
  559. {
  560. char *pStr1 = pStr + i;
  561. if (pStr1[0] != ',')
  562. continue;
  563. strncpy(item, pStr, pStr1 - pStr);
  564. item[pStr1 - pStr] = 0;
  565. bFoundComma = true;
  566. if (i < len - 1)
  567. pStr = pStr1 + 1;
  568. else
  569. pStr = NULL;
  570. break;
  571. }
  572. if (!bFoundComma && len > 0)
  573. {
  574. strcpy(item, pStr);
  575. pStr = NULL;
  576. }
  577. output[numOfItems] = item;
  578. numOfItems++;
  579. }
  580. }
  581. return numOfItems;
  582. }
  583. bool CFileSprayEx::GetArchivedDFUWorkunits(IEspContext &context, IEspGetDFUWorkunits &req, IEspGetDFUWorkunitsResponse &resp)
  584. {
  585. StringBuffer user;
  586. context.getUserID(user);
  587. StringBuffer sashaAddress;
  588. IArrayOf<IConstTpSashaServer> sashaservers;
  589. CTpWrapper dummy;
  590. dummy.getTpSashaServers(sashaservers);
  591. ForEachItemIn(i, sashaservers)
  592. {
  593. IConstTpSashaServer& sashaserver = sashaservers.item(i);
  594. IArrayOf<IConstTpMachine> &sashaservermachine = sashaserver.getTpMachines();
  595. sashaAddress.append(sashaservermachine.item(0).getNetaddress());
  596. }
  597. SocketEndpoint ep;
  598. ep.set(sashaAddress,DEFAULT_SASHA_PORT);
  599. Owned<INode> sashaserver = createINode(ep);
  600. __int64 count=req.getPageSize();
  601. if(count < 1)
  602. count=100;
  603. __int64 begin=req.getPageStartFrom();
  604. if (begin < 0)
  605. begin = 0;
  606. Owned<ISashaCommand> cmd = createSashaCommand();
  607. cmd->setAction(SCA_LIST);
  608. cmd->setOnline(false);
  609. cmd->setArchived(true);
  610. cmd->setDFU(true);
  611. cmd->setLimit((int) count+1);
  612. cmd->setStart((int)begin);
  613. if(req.getCluster() && *req.getCluster())
  614. cmd->setCluster(req.getCluster());
  615. if(req.getOwner() && *req.getOwner())
  616. cmd->setOwner(req.getOwner());
  617. if(req.getJobname() && *req.getJobname())
  618. cmd->setJobName(req.getJobname());
  619. if(req.getStateReq() && *req.getStateReq())
  620. cmd->setState(req.getStateReq());
  621. cmd->setOutputFormat("owner,jobname,cluster,state,command");//date range/owner/jobname/state*/
  622. if (!cmd->send(sashaserver))
  623. {
  624. StringBuffer msg;
  625. msg.appendf("Cannot connect to archive server at %s",sashaAddress.str());
  626. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER, "%s", msg.str());
  627. }
  628. IArrayOf<IEspDFUWorkunit> results;
  629. __int64 actualCount = cmd->numIds();
  630. StringBuffer s;
  631. for (unsigned j=0;j<actualCount;j++)
  632. {
  633. const char *wuidStr = cmd->queryId(j);
  634. if (!wuidStr)
  635. continue;
  636. StringBuffer strArray[6];
  637. readFromCommaSeparatedString(wuidStr, strArray);
  638. //skip any workunits without access
  639. Owned<IEspDFUWorkunit> resultWU = createDFUWorkunit("", "");
  640. resultWU->setArchived(true);
  641. if (strArray[0].length() > 0)
  642. resultWU->setID(strArray[0].str());
  643. if (strArray[1].length() > 0)
  644. resultWU->setUser(strArray[1].str());
  645. if (strArray[2].length() > 0)
  646. resultWU->setJobName(strArray[2].str());
  647. if (strArray[3].length() > 0)
  648. resultWU->setClusterName(strArray[3].str());
  649. if (strArray[4].length() > 0)
  650. resultWU->setStateMessage(strArray[4].str());
  651. if (strArray[5].length() > 0)
  652. resultWU->setCommand(atoi(strArray[5].str()));
  653. results.append(*resultWU.getLink());
  654. }
  655. resp.setPageStartFrom(begin+1);
  656. resp.setNextPage(-1);
  657. if(count < actualCount)
  658. {
  659. if (results.length() > count)
  660. {
  661. results.pop();
  662. }
  663. resp.setNextPage(begin + count);
  664. resp.setPageEndAt(begin + count);
  665. }
  666. else
  667. {
  668. resp.setPageEndAt(begin + actualCount);
  669. }
  670. if(begin > 0)
  671. {
  672. resp.setFirst(false);
  673. if (begin - count > 0)
  674. resp.setPrevPage(begin - count);
  675. else
  676. resp.setPrevPage(0);
  677. }
  678. resp.setPageSize(count);
  679. resp.setResults(results);
  680. StringBuffer basicQuery;
  681. if (req.getStateReq() && *req.getStateReq())
  682. {
  683. resp.setStateReq(req.getStateReq());
  684. addToQueryString(basicQuery, "StateReq", req.getStateReq());
  685. }
  686. if (req.getCluster() && *req.getCluster())
  687. {
  688. resp.setCluster(req.getCluster());
  689. addToQueryString(basicQuery, "Cluster", req.getCluster());
  690. }
  691. if (req.getOwner() && *req.getOwner())
  692. {
  693. resp.setOwner(req.getOwner());
  694. addToQueryString(basicQuery, "Owner", req.getOwner());
  695. }
  696. if (req.getType() && *req.getType())
  697. {
  698. resp.setType(req.getType());
  699. addToQueryString(basicQuery, "Type", req.getType());
  700. }
  701. resp.setFilters(basicQuery.str());
  702. resp.setBasicQuery(basicQuery.str());
  703. return true;
  704. }
  705. bool CFileSprayEx::onGetDFUWorkunits(IEspContext &context, IEspGetDFUWorkunits &req, IEspGetDFUWorkunitsResponse &resp)
  706. {
  707. try
  708. {
  709. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Read, false))
  710. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  711. double version = context.getClientVersion();
  712. if (version > 1.02)
  713. {
  714. const char *type = req.getType();
  715. if (type && *type && !stricmp(type, "archived workunits"))
  716. {
  717. return GetArchivedDFUWorkunits(context, req, resp);
  718. }
  719. }
  720. StringBuffer clusterReq;
  721. const char *clusterName = req.getCluster();
  722. if(clusterName && *clusterName)
  723. {
  724. clusterReq.append(clusterName);
  725. }
  726. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory();
  727. Owned<IConstEnvironment> constEnv = envFactory->openEnvironmentByFile();
  728. Owned<IPropertyTree> root = &constEnv->getPTree();
  729. if (!root)
  730. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  731. StringArray targetClusters, clusterProcesses;
  732. Owned<IPropertyTreeIterator> clusters= root->getElements("Software/Topology/Cluster");
  733. if (clusters->first())
  734. {
  735. do {
  736. IPropertyTree &cluster = clusters->query();
  737. const char* name = cluster.queryProp("@name");
  738. if (!name || !*name)
  739. continue;
  740. Owned<IPropertyTreeIterator> thorClusters= cluster.getElements(eqThorCluster);
  741. Owned<IPropertyTreeIterator> roxieClusters= cluster.getElements(eqRoxieCluster);
  742. if (thorClusters->first() || roxieClusters->first())
  743. {
  744. bool bFound = false;
  745. if (thorClusters->first())
  746. {
  747. IPropertyTree &thorCluster = thorClusters->query();
  748. const char* process = thorCluster.queryProp("@process");
  749. if (process && *process)
  750. {
  751. targetClusters.append(name);
  752. clusterProcesses.append(process);
  753. if (clusterName && !stricmp(clusterName, name))
  754. {
  755. clusterReq.clear().append(process);
  756. }
  757. }
  758. }
  759. if (!bFound && roxieClusters->first())
  760. {
  761. IPropertyTree &roxieCluster = roxieClusters->query();
  762. const char* process = roxieCluster.queryProp("@process");
  763. if (process && *process)
  764. {
  765. targetClusters.append(name);
  766. clusterProcesses.append(process);
  767. if (clusterName && !stricmp(clusterName, name))
  768. {
  769. clusterReq.clear().append(process);
  770. }
  771. }
  772. }
  773. }
  774. } while (clusters->next());
  775. }
  776. __int64 pagesize = req.getPageSize();
  777. __int64 pagefrom = req.getPageStartFrom();
  778. __int64 displayFrom = 0;
  779. if (pagesize < 1)
  780. {
  781. pagesize = 100;
  782. }
  783. if (pagefrom > 0)
  784. {
  785. displayFrom = pagefrom;
  786. }
  787. DFUsortfield sortorder[2] = {DFUsf_wuid, DFUsf_term};
  788. sortorder[0] = (DFUsortfield) (DFUsf_wuid + DFUsf_reverse);
  789. if(req.getSortby() && *req.getSortby())
  790. {
  791. const char *sortby = req.getSortby();
  792. if (!stricmp(sortby, "Owner"))
  793. sortorder[0] = DFUsf_user;
  794. else if (!stricmp(sortby, "JobName"))
  795. sortorder[0] = DFUsf_job;
  796. else if (!stricmp(sortby, "Cluster"))
  797. sortorder[0] = DFUsf_cluster;
  798. else if (!stricmp(sortby, "State"))
  799. sortorder[0] = DFUsf_state;
  800. else if (!stricmp(sortby, "Type"))
  801. sortorder[0] = DFUsf_command;
  802. else if (!stricmp(sortby, "Protected"))
  803. sortorder[0] = DFUsf_protected;
  804. else if (!stricmp(sortby, "PCTDone"))
  805. sortorder[0] = (DFUsortfield) (DFUsf_pcdone | DFUsf_numeric);
  806. else
  807. sortorder[0] = DFUsf_wuid;
  808. bool descending = req.getDescending();
  809. if (descending)
  810. sortorder[0] = (DFUsortfield) (sortorder[0] | DFUsf_reverse);
  811. }
  812. DFUsortfield filters[10];
  813. unsigned short filterCount = 0;
  814. MemoryBuffer filterbuf;
  815. if(req.getStateReq() && *req.getStateReq())
  816. {
  817. filters[filterCount] = DFUsf_state;
  818. filterCount++;
  819. if (stricmp(req.getStateReq(), "unknown") != 0)
  820. filterbuf.append(req.getStateReq());
  821. else
  822. filterbuf.append("");
  823. }
  824. if(clusterName && *clusterName)
  825. {
  826. filters[filterCount] = DFUsf_cluster;
  827. filterCount++;
  828. filterbuf.append(clusterReq.str());
  829. }
  830. if(req.getOwner() && *req.getOwner())
  831. {
  832. filters[filterCount] = DFUsortfield (DFUsf_user | DFUsf_nocase);
  833. filterCount++;
  834. filterbuf.append(req.getOwner());
  835. }
  836. if(req.getJobname() && *req.getJobname())
  837. {
  838. filters[filterCount] = DFUsortfield (DFUsf_job | DFUsf_nocase);
  839. filterCount++;
  840. filterbuf.append(req.getJobname());
  841. }
  842. filters[filterCount] = DFUsf_term;
  843. __int64 cacheHint = req.getCacheHint();
  844. if (cacheHint < 0) //Not set yet
  845. cacheHint = 0;
  846. IArrayOf<IEspDFUWorkunit> result;
  847. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  848. unsigned numWUs;
  849. Owned<IConstDFUWorkUnitIterator> itr = factory->getWorkUnitsSorted(sortorder, filters, filterbuf.bufferBase(), (int) displayFrom, (int) pagesize+1, req.getOwner(), &cacheHint, &numWUs);
  850. if (version >= 1.07)
  851. resp.setCacheHint(cacheHint);
  852. //unsigned actualCount = 0;
  853. itr->first();
  854. while(itr->isValid())
  855. {
  856. Owned<IConstDFUWorkUnit> wu = itr->get();
  857. //actualCount++;
  858. Owned<IEspDFUWorkunit> resultWU = createDFUWorkunit("", "");
  859. resultWU->setID(wu->queryId());
  860. StringBuffer jobname, user, cluster;
  861. resultWU->setJobName(wu->getJobName(jobname).str());
  862. resultWU->setCommand(wu->getCommand());
  863. resultWU->setUser(wu->getUser(user).str());
  864. const char* clusterName = wu->getClusterName(cluster).str();
  865. if (clusterName)
  866. {
  867. StringBuffer clusterForDisplay(clusterName);
  868. if (clusterProcesses.ordinality())
  869. {
  870. for (unsigned i = 0; i < clusterProcesses.length(); i++)
  871. {
  872. const char* clusterProcessName = clusterProcesses.item(i);
  873. if (!stricmp(clusterProcessName, clusterName))
  874. {
  875. clusterForDisplay.clear().append(targetClusters.item(i));
  876. break;
  877. }
  878. }
  879. }
  880. resultWU->setClusterName(clusterForDisplay.str());
  881. }
  882. resultWU->setIsProtected(wu->isProtected());
  883. IConstDFUprogress *prog = wu->queryProgress();
  884. if (prog != NULL)
  885. {
  886. DFUstate state = prog->getState();
  887. resultWU->setState(state);
  888. StringBuffer statemsg;
  889. encodeDFUstate(state,statemsg);
  890. resultWU->setStateMessage(statemsg.str());
  891. resultWU->setPercentDone(prog->getPercentDone());
  892. }
  893. result.append(*resultWU.getLink());
  894. itr->next();
  895. }
  896. if (result.length() > pagesize)
  897. result.pop();
  898. resp.setPageSize(pagesize);
  899. resp.setNumWUs(numWUs);
  900. resp.setPageStartFrom(displayFrom + 1);
  901. if(displayFrom + pagesize < numWUs)
  902. {
  903. resp.setNextPage(displayFrom + pagesize);
  904. resp.setPageEndAt(pagefrom + pagesize);
  905. __int64 last = displayFrom + pagesize;
  906. while (last + pagesize < numWUs)
  907. {
  908. last += pagesize;
  909. }
  910. resp.setLastPage(last);
  911. }
  912. else
  913. {
  914. resp.setNextPage(-1);
  915. resp.setPageEndAt(numWUs);
  916. }
  917. if(displayFrom > 0)
  918. {
  919. resp.setFirst(false);
  920. if (displayFrom - pagesize > 0)
  921. resp.setPrevPage(displayFrom - pagesize);
  922. else
  923. resp.setPrevPage(0);
  924. }
  925. StringBuffer basicQuery;
  926. if (req.getStateReq() && *req.getStateReq())
  927. {
  928. resp.setStateReq(req.getStateReq());
  929. addToQueryString(basicQuery, "StateReq", req.getStateReq());
  930. }
  931. if (req.getCluster() && *req.getCluster())
  932. {
  933. resp.setCluster(req.getCluster());
  934. addToQueryString(basicQuery, "Cluster", req.getCluster());
  935. }
  936. if (req.getOwner() && *req.getOwner())
  937. {
  938. resp.setOwner(req.getOwner());
  939. addToQueryString(basicQuery, "Owner", req.getOwner());
  940. }
  941. resp.setFilters(basicQuery.str());
  942. if (req.getSortby() && *req.getSortby())
  943. {
  944. resp.setSortby(req.getSortby());
  945. if (req.getDescending())
  946. resp.setDescending(req.getDescending());
  947. StringBuffer strbuf = req.getSortby();
  948. strbuf.append("=");
  949. String str1(strbuf.str());
  950. String str(basicQuery.str());
  951. if (str.indexOf(str1) < 0)
  952. {
  953. addToQueryString(basicQuery, "Sortby", req.getSortby());
  954. if (req.getDescending())
  955. addToQueryString(basicQuery, "Descending", "1");
  956. }
  957. }
  958. resp.setBasicQuery(basicQuery.str());
  959. resp.setResults(result);
  960. }
  961. catch(IException* e)
  962. {
  963. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  964. }
  965. return true;
  966. }
  967. void CFileSprayEx::addToQueryString(StringBuffer &queryString, const char *name, const char *value)
  968. {
  969. if (queryString.length() > 0)
  970. {
  971. queryString.append("&amp;");
  972. }
  973. queryString.append(name);
  974. queryString.append("=");
  975. queryString.append(value);
  976. }
  977. void CFileSprayEx::getInfoFromSasha(IEspContext &context, const char *sashaServer, const char* wuid, IEspDFUWorkunit *info)
  978. {
  979. Owned<ISashaCommand> cmd = createSashaCommand();
  980. cmd->addId(wuid);
  981. cmd->setAction(SCA_GET);
  982. cmd->setArchived(true);
  983. cmd->setDFU(true);
  984. SocketEndpoint ep(sashaServer, DEFAULT_SASHA_PORT);
  985. Owned<INode> node = createINode(ep);
  986. if (!cmd->send(node,1*60*1000))
  987. {
  988. DBGLOG("Cannot connect to Sasha server at %s",sashaServer);
  989. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER,"Cannot connect to archive server at %s.",sashaServer);
  990. }
  991. if (cmd->numIds()==0)
  992. {
  993. DBGLOG("Could not read archived %s",wuid);
  994. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT,"Cannot read workunit %s.",wuid);
  995. }
  996. unsigned num = cmd->numResults();
  997. if (num < 1)
  998. return;
  999. StringBuffer res;
  1000. cmd->getResult(0,res);
  1001. if(res.length() < 1)
  1002. return;
  1003. Owned<IPropertyTree> wu = createPTreeFromXMLString(res.str());
  1004. if (!wu)
  1005. return;
  1006. const char * command = wu->queryProp("@command");
  1007. const char * submitID = wu->queryProp("@submitID");
  1008. const char * cluster = wu->queryProp("@clusterName");
  1009. const char * queue = wu->queryProp("@queue");
  1010. const char * jobName = wu->queryProp("@jobName");
  1011. const char * protectedWU = wu->queryProp("@protected");
  1012. info->setID(wuid);
  1013. info->setArchived(true);
  1014. if (command && *command)
  1015. info->setCommandMessage(command);
  1016. if (cluster && *cluster)
  1017. info->setClusterName(cluster);
  1018. if (submitID && *submitID)
  1019. info->setUser(submitID);
  1020. if (queue && *queue)
  1021. info->setQueue(queue);
  1022. if (jobName && *jobName)
  1023. info->setJobName(jobName);
  1024. if (protectedWU && stricmp(protectedWU, "0"))
  1025. info->setIsProtected(true);
  1026. else
  1027. info->setIsProtected(false);
  1028. IPropertyTree *source = wu->queryPropTree("Source");
  1029. if(source)
  1030. {
  1031. const char * directory = source->queryProp("@directory");
  1032. const char * name = source->queryProp("@name");
  1033. if (directory && *directory)
  1034. info->setSourceDirectory(directory);
  1035. if (name && *name)
  1036. info->setSourceLogicalName(name);
  1037. }
  1038. IPropertyTree *dest = wu->queryPropTree("Destination");
  1039. if(dest)
  1040. {
  1041. const char * directory = dest->queryProp("@directory");
  1042. int numParts = dest->getPropInt("@numparts", -1);
  1043. if (directory && *directory)
  1044. info->setDestDirectory(directory);
  1045. if (numParts > 0)
  1046. info->setDestNumParts(numParts);
  1047. }
  1048. IPropertyTree *progress = wu->queryPropTree("Progress");
  1049. if(progress)
  1050. {
  1051. const char * state = progress->queryProp("@state");
  1052. const char * timeStarted = progress->queryProp("@timestarted");
  1053. const char * timeStopped = progress->queryProp("@timestopped");
  1054. if (state && *state)
  1055. info->setStateMessage(state);
  1056. if (timeStarted && *timeStarted)
  1057. {
  1058. StringBuffer startStr = timeStarted;
  1059. startStr.replace('T', ' ');
  1060. info->setTimeStarted(startStr.str());
  1061. }
  1062. if (timeStopped && *timeStopped)
  1063. {
  1064. StringBuffer stopStr = timeStopped;
  1065. stopStr.replace('T', ' ');
  1066. info->setTimeStopped(stopStr.str());
  1067. }
  1068. }
  1069. return;
  1070. }
  1071. bool CFileSprayEx::getArchivedWUInfo(IEspContext &context, IEspGetDFUWorkunit &req, IEspGetDFUWorkunitResponse &resp)
  1072. {
  1073. const char *wuid = req.getWuid();
  1074. if (wuid && *wuid)
  1075. {
  1076. StringBuffer sashaAddress;
  1077. IArrayOf<IConstTpSashaServer> sashaservers;
  1078. CTpWrapper dummy;
  1079. dummy.getTpSashaServers(sashaservers);
  1080. ForEachItemIn(i, sashaservers)
  1081. {
  1082. IConstTpSashaServer& sashaserver = sashaservers.item(i);
  1083. IArrayOf<IConstTpMachine> &sashaservermachine = sashaserver.getTpMachines();
  1084. sashaAddress.append(sashaservermachine.item(0).getNetaddress());
  1085. }
  1086. if (sashaAddress.length() < 1)
  1087. {
  1088. throw MakeStringException(ECLWATCH_ARCHIVE_SERVER_NOT_FOUND,"Archive server not found.");
  1089. }
  1090. getInfoFromSasha(context, sashaAddress.str(), wuid, &resp.updateResult());
  1091. resp.setAutoRefresh(WUDETAILS_REFRESH_MINS);
  1092. return true;
  1093. }
  1094. return false;
  1095. }
  1096. bool CFileSprayEx::onGetDFUWorkunit(IEspContext &context, IEspGetDFUWorkunit &req, IEspGetDFUWorkunitResponse &resp)
  1097. {
  1098. try
  1099. {
  1100. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Read, false))
  1101. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  1102. const char* wuid = req.getWuid();
  1103. if (!wuid || !*wuid)
  1104. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Dfu workunit ID not specified.");
  1105. bool found = false;
  1106. double version = context.getClientVersion();
  1107. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1108. Owned<IConstDFUWorkUnit> wu = factory->openWorkUnit(wuid, false);
  1109. if(wu)
  1110. {
  1111. IEspDFUWorkunit &result = resp.updateResult();
  1112. DeepAssign(context, wu, result);
  1113. int n = resp.getResult().getState();
  1114. if (n == DFUstate_scheduled || n == DFUstate_queued || n == DFUstate_started)
  1115. {
  1116. resp.setAutoRefresh(WUDETAILS_REFRESH_MINS);
  1117. }
  1118. found = true;
  1119. }
  1120. else if ((version > 1.02) && getArchivedWUInfo(context, req, resp))
  1121. {
  1122. found = true;
  1123. }
  1124. if (!found)
  1125. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1126. }
  1127. catch(IException* e)
  1128. {
  1129. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1130. }
  1131. return true;
  1132. }
  1133. bool CFileSprayEx::onGetDFUProgress(IEspContext &context, IEspProgressRequest &req, IEspProgressResponse &resp)
  1134. {
  1135. try
  1136. {
  1137. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Read, false))
  1138. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  1139. const char* wuid = req.getWuid();
  1140. if(!wuid || !*wuid)
  1141. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Workunit ID not specified.");
  1142. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1143. Owned<IConstDFUWorkUnit> wu = factory->openWorkUnit(req.getWuid(), false);
  1144. if(!wu)
  1145. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1146. resp.setWuid(req.getWuid());
  1147. IConstDFUprogress *prog = wu->queryProgress();
  1148. if (prog)
  1149. {
  1150. resp.setPercentDone(prog->getPercentDone());
  1151. resp.setKbPerSec(prog->getKbPerSec());
  1152. resp.setKbPerSecAve(prog->getKbPerSecAve());
  1153. resp.setSecsLeft(prog->getSecsLeft());
  1154. StringBuffer statestr;
  1155. encodeDFUstate(prog->getState(), statestr);
  1156. resp.setState(statestr.str());
  1157. resp.setSlavesDone(prog->getSlavesDone());
  1158. StringBuffer msg;
  1159. prog->formatProgressMessage(msg);
  1160. resp.setProgressMessage(msg.str());
  1161. prog->formatSummaryMessage(msg.clear());
  1162. resp.setSummaryMessage(msg.str());
  1163. prog->getTimeTaken(msg.clear());
  1164. resp.setTimeTaken(msg.str());
  1165. }
  1166. }
  1167. catch(IException* e)
  1168. {
  1169. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1170. }
  1171. return true;
  1172. }
  1173. bool CFileSprayEx::onCreateDFUWorkunit(IEspContext &context, IEspCreateDFUWorkunit &req, IEspCreateDFUWorkunitResponse &resp)
  1174. {
  1175. try
  1176. {
  1177. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Write, false))
  1178. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to create DFU workunit. Permission denied.");
  1179. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1180. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1181. wu->setQueue(m_QueueLabel.str());
  1182. StringBuffer user, passwd;
  1183. wu->setUser(context.getUserID(user).str());
  1184. wu->setPassword(context.getPassword(passwd).str());
  1185. wu->commit();
  1186. const char * d = wu->queryId();
  1187. IEspDFUWorkunit &result = resp.updateResult();
  1188. DeepAssign(context, wu, result);
  1189. result.setOverwrite(false);
  1190. result.setReplicate(true);
  1191. }
  1192. catch(IException* e)
  1193. {
  1194. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1195. }
  1196. return true;
  1197. }
  1198. bool CFileSprayEx::onUpdateDFUWorkunit(IEspContext &context, IEspUpdateDFUWorkunit &req, IEspUpdateDFUWorkunitResponse &resp)
  1199. {
  1200. try
  1201. {
  1202. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Write, false))
  1203. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to update DFU workunit. Permission denied.");
  1204. IConstDFUWorkunit & reqWU = req.getWu();
  1205. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1206. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(reqWU.getID());
  1207. if(!wu)
  1208. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Dfu workunit %s not found.", reqWU.getID());
  1209. IDFUprogress *prog = wu->queryUpdateProgress();
  1210. if (prog && req.getStateOrig() != reqWU.getState())
  1211. {
  1212. if (prog->getState() != req.getStateOrig())
  1213. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot update DFU workunit %s because its state has been changed internally. Please refresh the page and try again.",reqWU.getID());
  1214. prog->setState((enum DFUstate)reqWU.getState());
  1215. }
  1216. const char* clusterOrig = req.getClusterOrig();
  1217. const char* cluster = reqWU.getClusterName();
  1218. if(cluster && (!clusterOrig || stricmp(clusterOrig, cluster)))
  1219. {
  1220. wu->setClusterName(reqWU.getClusterName());
  1221. }
  1222. const char* jobNameOrig = req.getJobNameOrig();
  1223. const char* jobName = reqWU.getJobName();
  1224. if(jobName && (!jobNameOrig || stricmp(jobNameOrig, jobName)))
  1225. {
  1226. wu->setJobName(jobName);
  1227. }
  1228. if (reqWU.getIsProtected() != req.getIsProtectedOrig())
  1229. wu->protect(reqWU.getIsProtected());
  1230. wu->commit();
  1231. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(reqWU.getID()).str());
  1232. }
  1233. catch(IException* e)
  1234. {
  1235. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1236. }
  1237. return true;
  1238. }
  1239. bool markWUFailed(IDFUWorkUnitFactory *f, const char *wuid)
  1240. {
  1241. Owned<IDFUWorkUnit> wu = f->updateWorkUnit(wuid);
  1242. if(!wu)
  1243. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Dfu workunit %s not found.", wuid);
  1244. IDFUprogress *prog = wu->queryUpdateProgress();
  1245. if(!prog)
  1246. throw MakeStringException(ECLWATCH_PROGRESS_INFO_NOT_FOUND, "progress information not found for workunit %s.", wuid);
  1247. else if(prog->getState() == DFUstate_started)
  1248. throw MakeStringException(ECLWATCH_CANNOT_DELETE_WORKUNIT, "Cannot delete workunit %s because its state is Started.", wuid);
  1249. else
  1250. {
  1251. prog->setState(DFUstate_failed);
  1252. return true;
  1253. }
  1254. return false;
  1255. }
  1256. bool CFileSprayEx::onDFUWorkunitsAction(IEspContext &context, IEspDFUWorkunitsActionRequest &req, IEspDFUWorkunitsActionResponse &resp)
  1257. {
  1258. try
  1259. {
  1260. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Write, false))
  1261. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to update DFU workunit. Permission denied.");
  1262. bool bAllSuccess = true;
  1263. IArrayOf<IEspDFUActionResult> results;
  1264. const char* action = req.getType();
  1265. if(!action || !*action || !strcmp(action, "Delete"))
  1266. {
  1267. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1268. StringArray & wuids = req.getWuids();
  1269. for(unsigned i = 0; i < wuids.ordinality(); ++i)
  1270. {
  1271. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  1272. res->setID(wuids.item(i));
  1273. res->setAction("Delete");
  1274. res->setResult("Success");
  1275. try
  1276. {
  1277. if (markWUFailed(factory, wuids.item(i)))
  1278. {
  1279. if (!factory->deleteWorkUnit(wuids.item(i)))
  1280. throw MakeStringException(ECLWATCH_CANNOT_DELETE_WORKUNIT, "Failed in deleting workunit %s.", wuids.item(i));
  1281. }
  1282. }
  1283. catch (IException *e)
  1284. {
  1285. bAllSuccess = false;
  1286. StringBuffer eMsg;
  1287. eMsg = e->errorMessage(eMsg);
  1288. e->Release();
  1289. StringBuffer failedMsg = "Failed: ";
  1290. failedMsg.append(eMsg);
  1291. res->setResult(failedMsg.str());
  1292. }
  1293. results.append(*res.getLink());
  1294. }
  1295. }
  1296. else if (!strcmp(action, "Restore"))
  1297. {
  1298. StringBuffer sashaAddress;
  1299. IArrayOf<IConstTpSashaServer> sashaservers;
  1300. CTpWrapper dummy;
  1301. dummy.getTpSashaServers(sashaservers);
  1302. ForEachItemIn(i, sashaservers)
  1303. {
  1304. IConstTpSashaServer& sashaserver = sashaservers.item(i);
  1305. IArrayOf<IConstTpMachine> &sashaservermachine = sashaserver.getTpMachines();
  1306. sashaAddress.append(sashaservermachine.item(0).getNetaddress());
  1307. }
  1308. if (sashaAddress.length() < 1)
  1309. {
  1310. throw MakeStringException(ECLWATCH_ARCHIVE_SERVER_NOT_FOUND,"Archive server not found.");
  1311. }
  1312. SocketEndpoint ep(sashaAddress.str(), DEFAULT_SASHA_PORT);
  1313. Owned<INode> node = createINode(ep);
  1314. Owned<ISashaCommand> cmd = createSashaCommand();
  1315. cmd->setAction(SCA_RESTORE);
  1316. cmd->setDFU(true);
  1317. StringArray & wuids = req.getWuids();
  1318. for(unsigned ii = 0; ii < wuids.ordinality(); ++ii)
  1319. {
  1320. StringBuffer msg;
  1321. const char *wuid = wuids.item(ii);
  1322. cmd->addId(wuid);
  1323. if (!cmd->send(node,1*60*1000))
  1324. {
  1325. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER,"Cannot connect to archive server at %s.",sashaAddress.str());
  1326. }
  1327. if (cmd->numIds()==0)
  1328. {
  1329. bAllSuccess = false;
  1330. msg.appendf("Restore failed for %s", wuid);
  1331. }
  1332. else
  1333. {
  1334. StringBuffer reply;
  1335. cmd->getId(0,reply);
  1336. msg.appendf("Restore: %s, reply: %s", wuid, reply.str());
  1337. }
  1338. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  1339. res->setID(wuid);
  1340. res->setAction("Restore");
  1341. res->setResult(msg.str());
  1342. results.append(*res.getLink());
  1343. }
  1344. }
  1345. else if(!strcmp(action, "Protect"))
  1346. {
  1347. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1348. StringArray & wuids = req.getWuids();
  1349. for(unsigned i = 0; i < wuids.ordinality(); ++i)
  1350. {
  1351. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  1352. res->setID(wuids.item(i));
  1353. res->setAction("Protect");
  1354. res->setResult("Success");
  1355. try
  1356. {
  1357. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1358. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(wuids.item(i));
  1359. if(!wu.get())
  1360. continue;
  1361. wu->protect(true);
  1362. wu->commit();
  1363. }
  1364. catch (IException *e)
  1365. {
  1366. bAllSuccess = false;
  1367. StringBuffer eMsg;
  1368. eMsg = e->errorMessage(eMsg);
  1369. e->Release();
  1370. StringBuffer failedMsg = "Failed: ";
  1371. failedMsg.append(eMsg);
  1372. res->setResult(failedMsg.str());
  1373. }
  1374. results.append(*res.getLink());
  1375. }
  1376. }
  1377. else if(!strcmp(action, "Unprotect"))
  1378. {
  1379. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1380. StringArray & wuids = req.getWuids();
  1381. for(unsigned i = 0; i < wuids.ordinality(); ++i)
  1382. {
  1383. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  1384. res->setID(wuids.item(i));
  1385. res->setAction("Unprotect");
  1386. res->setResult("Success");
  1387. try
  1388. {
  1389. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1390. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(wuids.item(i));
  1391. if(!wu.get())
  1392. continue;
  1393. wu->protect(false);
  1394. wu->commit();
  1395. }
  1396. catch (IException *e)
  1397. {
  1398. bAllSuccess = false;
  1399. StringBuffer eMsg;
  1400. eMsg = e->errorMessage(eMsg);
  1401. e->Release();
  1402. StringBuffer failedMsg = "Failed: ";
  1403. failedMsg.append(eMsg);
  1404. res->setResult(failedMsg.str());
  1405. }
  1406. results.append(*res.getLink());
  1407. }
  1408. }
  1409. else if(!strcmp(action, "SetToFailed"))
  1410. {
  1411. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1412. StringArray & wuids = req.getWuids();
  1413. for(unsigned i = 0; i < wuids.ordinality(); ++i)
  1414. {
  1415. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  1416. res->setID(wuids.item(i));
  1417. res->setAction("SetToFailed");
  1418. res->setResult("Success");
  1419. try
  1420. {
  1421. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(wuids.item(i));
  1422. if(wu)
  1423. {
  1424. IDFUprogress *prog = wu->queryUpdateProgress();
  1425. if (prog)
  1426. {
  1427. prog->setState(DFUstate_failed);
  1428. wu->commit();
  1429. }
  1430. }
  1431. }
  1432. catch (IException *e)
  1433. {
  1434. bAllSuccess = false;
  1435. StringBuffer eMsg;
  1436. eMsg = e->errorMessage(eMsg);
  1437. e->Release();
  1438. StringBuffer failedMsg = "Failed: ";
  1439. failedMsg.append(eMsg);
  1440. res->setResult(failedMsg.str());
  1441. }
  1442. results.append(*res.getLink());
  1443. }
  1444. }
  1445. else
  1446. throw MakeStringException(ECLWATCH_INVALID_ACTION, "Unknown action type %s", action);
  1447. if (bAllSuccess && strcmp(action, "Delete"))
  1448. {
  1449. if (!strcmp(action, "Restore"))
  1450. resp.setRedirectUrl("/FileSpray/GetDFUWorkunits?Type=archived workunits");
  1451. else
  1452. resp.setRedirectUrl("/FileSpray/GetDFUWorkunits");
  1453. }
  1454. else
  1455. {
  1456. resp.setDFUActionResults(results);
  1457. }
  1458. }
  1459. catch(IException* e)
  1460. {
  1461. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1462. }
  1463. return true;
  1464. }
  1465. bool CFileSprayEx::onDeleteDFUWorkunits(IEspContext &context, IEspDeleteDFUWorkunits &req, IEspDeleteDFUWorkunitsResponse &resp)
  1466. {
  1467. try
  1468. {
  1469. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Write, false))
  1470. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to delete DFU workunit. Permission denied.");
  1471. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1472. StringArray & wuids = req.getWuids();
  1473. for(unsigned i = 0; i < wuids.ordinality(); ++i)
  1474. {
  1475. if (markWUFailed(factory, wuids.item(i)))
  1476. factory->deleteWorkUnit(wuids.item(i));
  1477. }
  1478. resp.setRedirectUrl("/FileSpray/GetDFUWorkunits");
  1479. }
  1480. catch(IException* e)
  1481. {
  1482. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1483. }
  1484. return true;
  1485. }
  1486. bool CFileSprayEx::onDeleteDFUWorkunit(IEspContext &context, IEspDeleteDFUWorkunit &req, IEspDeleteDFUWorkunitResponse &resp)
  1487. {
  1488. try
  1489. {
  1490. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Write, false))
  1491. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to delete DFU workunit. Permission denied.");
  1492. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1493. if (markWUFailed(factory, req.getWuid()))
  1494. resp.setResult(factory->deleteWorkUnit(req.getWuid()));
  1495. else
  1496. resp.setResult(false);
  1497. resp.setRedirectUrl("/FileSpray/GetDFUWorkunits");
  1498. }
  1499. catch(IException* e)
  1500. {
  1501. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1502. }
  1503. return true;
  1504. }
  1505. bool CFileSprayEx::onSubmitDFUWorkunit(IEspContext &context, IEspSubmitDFUWorkunit &req, IEspSubmitDFUWorkunitResponse &resp)
  1506. {
  1507. try
  1508. {
  1509. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Write, false))
  1510. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to submit DFU workunit. Permission denied.");
  1511. submitDFUWorkUnit(req.getWuid());
  1512. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(req.getWuid()).str());
  1513. }
  1514. catch(IException* e)
  1515. {
  1516. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1517. }
  1518. return true;
  1519. }
  1520. bool CFileSprayEx::onAbortDFUWorkunit(IEspContext &context, IEspAbortDFUWorkunit &req, IEspAbortDFUWorkunitResponse &resp)
  1521. {
  1522. try
  1523. {
  1524. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Write, false))
  1525. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to abort DFU workunit. Permission denied.");
  1526. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1527. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(req.getWuid());
  1528. if(!wu)
  1529. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1530. wu->requestAbort();
  1531. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(req.getWuid()).str());
  1532. }
  1533. catch(IException* e)
  1534. {
  1535. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1536. }
  1537. return true;
  1538. }
  1539. bool CFileSprayEx::onGetDFUExceptions(IEspContext &context, IEspGetDFUExceptions &req, IEspGetDFUExceptionsResponse &resp)
  1540. {
  1541. try
  1542. {
  1543. if (!context.validateFeatureAccess(DFU_EX_URL, SecAccess_Read, false))
  1544. throw MakeStringException(ECLWATCH_DFU_EX_ACCESS_DENIED, "Failed to get DFU Exceptions. Permission denied.");
  1545. IArrayOf<IEspDFUException> result;
  1546. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1547. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(req.getWuid());
  1548. if(!wu)
  1549. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1550. Owned<IExceptionIterator> itr = wu->getExceptionIterator();
  1551. itr->first();
  1552. while(itr->isValid())
  1553. {
  1554. Owned<IEspDFUException> resultE = createDFUException("", "");
  1555. IException &e = itr->query();
  1556. resultE->setCode(e.errorCode());
  1557. StringBuffer msg;
  1558. resultE->setMessage(e.errorMessage(msg).str());
  1559. result.append(*resultE.getLink());
  1560. itr->next();
  1561. }
  1562. resp.setResult(result);
  1563. }
  1564. catch(IException* e)
  1565. {
  1566. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1567. }
  1568. return true;
  1569. }
  1570. bool CFileSprayEx::onSprayFixed(IEspContext &context, IEspSprayFixed &req, IEspSprayFixedResponse &resp)
  1571. {
  1572. try
  1573. {
  1574. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, false))
  1575. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Spray. Permission denied.");
  1576. StringBuffer destFolder, destTitle, defaultFolder, defaultReplicateFolder;
  1577. const char* destCluster = req.getDestGroup();
  1578. if(destCluster == NULL || *destCluster == '\0')
  1579. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination cluster/group not specified.");
  1580. MemoryBuffer& srcxml = (MemoryBuffer&)req.getSrcxml();
  1581. const char* srcip = req.getSourceIP();
  1582. const char* srcfile = req.getSourcePath();
  1583. if(srcxml.length() == 0)
  1584. {
  1585. if(!srcip || !*srcip)
  1586. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source network IP not specified.");
  1587. if(!srcfile || !*srcfile)
  1588. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source file not specified.");
  1589. }
  1590. bool nosplit = req.getNosplit();
  1591. int recordsize = req.getSourceRecordSize();
  1592. if(recordsize == 0 && !nosplit) // -ve record sizes for blocked
  1593. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid record size");
  1594. const char* destname = req.getDestLogicalName();
  1595. if(!destname || !*destname)
  1596. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination file not specified.");
  1597. CDfsLogicalFileName lfn;
  1598. if (!lfn.setValidate(destname))
  1599. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid destination filename");
  1600. destname = lfn.get();
  1601. StringBuffer gName, ipAddr;
  1602. const char *pTr = strchr(destCluster, ' ');
  1603. if (pTr)
  1604. {
  1605. gName.append(pTr - destCluster, destCluster);
  1606. ipAddr.append(pTr+1);
  1607. }
  1608. else
  1609. gName.append(destCluster);
  1610. if (ipAddr.length() > 0)
  1611. ParseLogicalPath(destname, ipAddr.str(), destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1612. else
  1613. ParseLogicalPath(destname, destCluster, destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1614. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1615. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1616. wu->setClusterName(gName.str());
  1617. wu->setJobName(destTitle.str());
  1618. wu->setQueue(m_QueueLabel.str());
  1619. StringBuffer user, passwd;
  1620. wu->setUser(context.getUserID(user).str());
  1621. wu->setPassword(context.getPassword(passwd).str());
  1622. wu->setCommand(DFUcmd_import);
  1623. IDFUfileSpec *source = wu->queryUpdateSource();
  1624. if(srcxml.length() == 0)
  1625. {
  1626. RemoteMultiFilename rmfn;
  1627. SocketEndpoint ep(srcip);
  1628. rmfn.setEp(ep);
  1629. StringBuffer fnamebuf(srcfile);
  1630. fnamebuf.trim();
  1631. rmfn.append(fnamebuf.str()); // handles comma separated files
  1632. source->setMultiFilename(rmfn);
  1633. }
  1634. else
  1635. {
  1636. srcxml.append('\0');
  1637. source->setFromXML((const char*)srcxml.toByteArray());
  1638. }
  1639. IDFUfileSpec *destination = wu->queryUpdateDestination();
  1640. if(recordsize > 0)
  1641. source->setRecordSize(recordsize);
  1642. else if (recordsize == RECFMVB_RECSIZE_ESCAPE) {
  1643. source->setFormat(DFUff_recfmvb);
  1644. destination->setFormat(DFUff_variable);
  1645. }
  1646. else if (recordsize == RECFMV_RECSIZE_ESCAPE) {
  1647. source->setFormat(DFUff_recfmv);
  1648. destination->setFormat(DFUff_variable);
  1649. }
  1650. else if (recordsize == PREFIX_VARIABLE_RECSIZE_ESCAPE) {
  1651. source->setFormat(DFUff_variable);
  1652. destination->setFormat(DFUff_variable);
  1653. }
  1654. else if (recordsize == PREFIX_VARIABLE_BIGENDIAN_RECSIZE_ESCAPE) {
  1655. source->setFormat(DFUff_variablebigendian);
  1656. destination->setFormat(DFUff_variable);
  1657. }
  1658. destination->setLogicalName(destname);
  1659. destination->setDirectory(destFolder.str());
  1660. StringBuffer fileMask;
  1661. constructFileMask(destTitle.str(), fileMask);
  1662. destination->setFileMask(fileMask.str());
  1663. destination->setGroupName(gName.str());
  1664. const char * encryptkey = req.getEncrypt();
  1665. if(req.getCompress()||(encryptkey&&*encryptkey))
  1666. destination->setCompressed(true);
  1667. ClusterPartDiskMapSpec mspec;
  1668. destination->getClusterPartDiskMapSpec(gName.str(), mspec);
  1669. mspec.setDefaultBaseDir(defaultFolder.str());
  1670. mspec.setDefaultReplicateDir(defaultReplicateFolder.str());
  1671. destination->setClusterPartDiskMapSpec(gName.str(), mspec);
  1672. int repo = req.getReplicateOffset();
  1673. bool isNull = req.getReplicateOffset_isNull();
  1674. if (!isNull && (repo!=1))
  1675. destination->setReplicateOffset(repo);
  1676. if (req.getWrap())
  1677. destination->setWrap(true);
  1678. IDFUoptions *options = wu->queryUpdateOptions();
  1679. const char * decryptkey = req.getDecrypt();
  1680. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  1681. options->setEncDec(encryptkey,decryptkey);
  1682. options->setReplicate(req.getReplicate());
  1683. options->setOverwrite(req.getOverwrite()); // needed if target already exists
  1684. const char* prefix = req.getPrefix();
  1685. if(prefix && *prefix)
  1686. options->setLengthPrefix(prefix);
  1687. if(req.getNosplit())
  1688. options->setNoSplit(true);
  1689. if(req.getNorecover())
  1690. options->setNoRecover(true);
  1691. if(req.getMaxConnections() > 0)
  1692. options->setmaxConnections(req.getMaxConnections());
  1693. if(req.getThrottle() > 0)
  1694. options->setThrottle(req.getThrottle());
  1695. if(req.getTransferBufferSize() > 0)
  1696. options->setTransferBufferSize(req.getTransferBufferSize());
  1697. if (req.getPull())
  1698. options->setPull(true);
  1699. if (req.getPush())
  1700. options->setPush(true);
  1701. resp.setWuid(wu->queryId());
  1702. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  1703. submitDFUWorkUnit(wu.getClear());
  1704. }
  1705. catch(IException* e)
  1706. {
  1707. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1708. }
  1709. return true;
  1710. }
  1711. bool CFileSprayEx::onSprayVariable(IEspContext &context, IEspSprayVariable &req, IEspSprayResponse &resp)
  1712. {
  1713. try
  1714. {
  1715. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, false))
  1716. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Spray. Permission denied.");
  1717. StringBuffer destFolder, destTitle, defaultFolder, defaultReplicateFolder;
  1718. const char* destCluster = req.getDestGroup();
  1719. if(destCluster == NULL || *destCluster == '\0')
  1720. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination cluster/group not specified.");
  1721. StringBuffer gName, ipAddr;
  1722. const char *pTr = strchr(destCluster, ' ');
  1723. if (pTr)
  1724. {
  1725. gName.append(pTr - destCluster, destCluster);
  1726. ipAddr.append(pTr+1);
  1727. }
  1728. else
  1729. gName.append(destCluster);
  1730. MemoryBuffer& srcxml = (MemoryBuffer&)req.getSrcxml();
  1731. const char* srcip = req.getSourceIP();
  1732. const char* srcfile = req.getSourcePath();
  1733. if(srcxml.length() == 0)
  1734. {
  1735. if(!srcip || !*srcip)
  1736. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source network IP not specified.");
  1737. if(!srcfile || !*srcfile)
  1738. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source file not specified.");
  1739. }
  1740. const char* destname = req.getDestLogicalName();
  1741. if(!destname || !*destname)
  1742. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination file not specified.");
  1743. CDfsLogicalFileName lfn;
  1744. if (!lfn.setValidate(destname))
  1745. throw MakeStringException(ECLWATCH_INVALID_INPUT, "invalid destination filename");
  1746. destname = lfn.get();
  1747. if (ipAddr.length() > 0)
  1748. ParseLogicalPath(destname, ipAddr.str(), destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1749. else
  1750. ParseLogicalPath(destname, destCluster, destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1751. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1752. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1753. wu->setClusterName(gName.str());
  1754. wu->setJobName(destTitle.str());
  1755. wu->setQueue(m_QueueLabel.str());
  1756. StringBuffer user, passwd;
  1757. wu->setUser(context.getUserID(user).str());
  1758. wu->setPassword(context.getPassword(passwd).str());
  1759. wu->setCommand(DFUcmd_import);
  1760. IDFUfileSpec *source = wu->queryUpdateSource();
  1761. IDFUfileSpec *destination = wu->queryUpdateDestination();
  1762. IDFUoptions *options = wu->queryUpdateOptions();
  1763. if(srcxml.length() == 0)
  1764. {
  1765. RemoteMultiFilename rmfn;
  1766. SocketEndpoint ep(srcip);
  1767. rmfn.setEp(ep);
  1768. StringBuffer fnamebuf(srcfile);
  1769. fnamebuf.trim();
  1770. rmfn.append(fnamebuf.str()); // handles comma separated files
  1771. source->setMultiFilename(rmfn);
  1772. }
  1773. else
  1774. {
  1775. srcxml.append('\0');
  1776. source->setFromXML((const char*)srcxml.toByteArray());
  1777. }
  1778. source->setMaxRecordSize(req.getSourceMaxRecordSize());
  1779. source->setFormat((DFUfileformat)req.getSourceFormat());
  1780. // if rowTag specified, it means it's xml format, otherwise it's csv
  1781. const char* rowtag = req.getSourceRowTag();
  1782. if(rowtag != NULL && *rowtag != '\0')
  1783. {
  1784. source->setRowTag(rowtag);
  1785. options->setKeepHeader(true);
  1786. }
  1787. else
  1788. {
  1789. const char* cs = req.getSourceCsvSeparate();
  1790. if (req.getNoSourceCsvSeparator())
  1791. {
  1792. cs = "";
  1793. }
  1794. else if(cs == NULL || *cs == '\0')
  1795. cs = "\\,";
  1796. const char* ct = req.getSourceCsvTerminate();
  1797. if(ct == NULL || *ct == '\0')
  1798. ct = "\\n,\\r\\n";
  1799. const char* cq = req.getSourceCsvQuote();
  1800. if(cq== NULL)
  1801. cq = "'";
  1802. source->setCsvOptions(cs, ct, cq, req.getSourceCsvEscape());
  1803. }
  1804. destination->setLogicalName(destname);
  1805. destination->setDirectory(destFolder.str());
  1806. StringBuffer fileMask;
  1807. constructFileMask(destTitle.str(), fileMask);
  1808. destination->setFileMask(fileMask.str());
  1809. destination->setGroupName(gName.str());
  1810. ClusterPartDiskMapSpec mspec;
  1811. destination->getClusterPartDiskMapSpec(gName.str(), mspec);
  1812. mspec.setDefaultBaseDir(defaultFolder.str());
  1813. mspec.setDefaultReplicateDir(defaultReplicateFolder.str());
  1814. destination->setClusterPartDiskMapSpec(gName.str(), mspec);
  1815. const char * encryptkey = req.getEncrypt();
  1816. if(req.getCompress()||(encryptkey&&*encryptkey))
  1817. destination->setCompressed(true);
  1818. const char * decryptkey = req.getDecrypt();
  1819. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  1820. options->setEncDec(encryptkey,decryptkey);
  1821. int repo = req.getReplicateOffset();
  1822. bool isNull = req.getReplicateOffset_isNull();
  1823. if (!isNull && (repo!=1))
  1824. destination->setReplicateOffset(repo);
  1825. options->setReplicate(req.getReplicate());
  1826. options->setOverwrite(req.getOverwrite()); // needed if target already exists
  1827. const char* prefix = req.getPrefix();
  1828. if(prefix && *prefix)
  1829. options->setLengthPrefix(prefix);
  1830. if(req.getNosplit())
  1831. options->setNoSplit(true);
  1832. if(req.getNorecover())
  1833. options->setNoRecover(true);
  1834. if(req.getMaxConnections() > 0)
  1835. options->setmaxConnections(req.getMaxConnections());
  1836. if(req.getThrottle() > 0)
  1837. options->setThrottle(req.getThrottle());
  1838. if(req.getTransferBufferSize() > 0)
  1839. options->setTransferBufferSize(req.getTransferBufferSize());
  1840. if (req.getPull())
  1841. options->setPull(true);
  1842. if (req.getPush())
  1843. options->setPush(true);
  1844. resp.setWuid(wu->queryId());
  1845. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  1846. submitDFUWorkUnit(wu.getClear());
  1847. }
  1848. catch(IException* e)
  1849. {
  1850. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1851. }
  1852. return true;
  1853. }
  1854. bool CFileSprayEx::onReplicate(IEspContext &context, IEspReplicate &req, IEspReplicateResponse &resp)
  1855. {
  1856. try
  1857. {
  1858. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, false))
  1859. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Replicate. Permission denied.");
  1860. const char* srcname = req.getSourceLogicalName();
  1861. if(!srcname || !*srcname)
  1862. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  1863. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1864. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1865. StringBuffer jobname = "Replicate: ";
  1866. jobname.append(srcname);
  1867. wu->setJobName(jobname.str());
  1868. wu->setQueue(m_QueueLabel.str());
  1869. StringBuffer user, passwd;
  1870. wu->setUser(context.getUserID(user).str());
  1871. wu->setPassword(context.getPassword(passwd).str());
  1872. wu->setCommand(DFUcmd_replicate);
  1873. IDFUfileSpec *source = wu->queryUpdateSource();
  1874. if (source)
  1875. {
  1876. source->setLogicalName(srcname);
  1877. int repo = req.getReplicateOffset();
  1878. if (repo!=1)
  1879. source->setReplicateOffset(repo);
  1880. }
  1881. const char* cluster = req.getCluster();
  1882. if(cluster && *cluster)
  1883. {
  1884. IDFUoptions *opt = wu->queryUpdateOptions();
  1885. opt->setReplicateMode(DFURMmissing,cluster,req.getRepeatLast(),req.getOnlyRepeated());
  1886. }
  1887. resp.setWuid(wu->queryId());
  1888. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  1889. submitDFUWorkUnit(wu.getClear());
  1890. }
  1891. catch(IException* e)
  1892. {
  1893. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1894. }
  1895. return true;
  1896. }
  1897. bool CFileSprayEx::onDespray(IEspContext &context, IEspDespray &req, IEspDesprayResponse &resp)
  1898. {
  1899. try
  1900. {
  1901. if (!context.validateFeatureAccess(FILE_DESPRAY_URL, SecAccess_Write, false))
  1902. throw MakeStringException(ECLWATCH_FILE_DESPRAY_ACCESS_DENIED, "Failed to do Despray. Permission denied.");
  1903. const char* srcname = req.getSourceLogicalName();
  1904. if(!srcname || !*srcname)
  1905. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  1906. const char* destip = req.getDestIP();
  1907. StringBuffer fnamebuf(req.getDestPath());
  1908. const char* destfile = fnamebuf.trim().str();
  1909. MemoryBuffer& dstxml = (MemoryBuffer&)req.getDstxml();
  1910. if(dstxml.length() == 0)
  1911. {
  1912. if(!destip || !*destip)
  1913. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination network IP not specified.");
  1914. if(!destfile || !*destfile)
  1915. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination file not specified.");
  1916. }
  1917. StringBuffer srcTitle;
  1918. ParseLogicalPath(srcname, srcTitle);
  1919. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1920. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1921. wu->setJobName(srcTitle.str());
  1922. wu->setQueue(m_QueueLabel.str());
  1923. StringBuffer user, passwd;
  1924. wu->setUser(context.getUserID(user).str());
  1925. wu->setPassword(context.getPassword(passwd).str());
  1926. wu->setCommand(DFUcmd_export);
  1927. IDFUfileSpec *source = wu->queryUpdateSource();
  1928. IDFUfileSpec *destination = wu->queryUpdateDestination();
  1929. IDFUoptions *options = wu->queryUpdateOptions();
  1930. source->setLogicalName(srcname);
  1931. if(dstxml.length() == 0)
  1932. {
  1933. RemoteFilename rfn;
  1934. SocketEndpoint ep(destip);
  1935. rfn.setPath(ep, destfile);
  1936. destination->setSingleFilename(rfn);
  1937. }
  1938. else
  1939. {
  1940. dstxml.append('\0');
  1941. destination->setFromXML((const char*)dstxml.toByteArray());
  1942. }
  1943. destination->setTitle(srcTitle.str());
  1944. options->setKeepHeader(true);
  1945. options->setOverwrite(req.getOverwrite()); // needed if target already exists
  1946. const char* splitprefix = req.getSplitprefix();
  1947. if(splitprefix && *splitprefix)
  1948. options->setSplitPrefix(splitprefix);
  1949. double version = context.getClientVersion();
  1950. if (version > 1.01)
  1951. {
  1952. if(req.getMaxConnections() > 0)
  1953. options->setmaxConnections(req.getMaxConnections());
  1954. else if(req.getSingleConnection())
  1955. options->setmaxConnections(1);
  1956. }
  1957. else
  1958. {
  1959. if(req.getMaxConnections() > 0)
  1960. options->setmaxConnections(req.getMaxConnections());
  1961. }
  1962. if(req.getThrottle() > 0)
  1963. options->setThrottle(req.getThrottle());
  1964. if(req.getTransferBufferSize() > 0)
  1965. options->setTransferBufferSize(req.getTransferBufferSize());
  1966. if(req.getNorecover())
  1967. options->setNoRecover(true);
  1968. if (req.getWrap()) {
  1969. options->setPush(); // I think needed for a despray
  1970. destination->setWrap(true);
  1971. }
  1972. if (req.getMultiCopy())
  1973. destination->setMultiCopy(true);
  1974. const char * encryptkey = req.getEncrypt();
  1975. if(req.getCompress()||(encryptkey&&*encryptkey))
  1976. destination->setCompressed(true);
  1977. const char * decryptkey = req.getDecrypt();
  1978. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  1979. options->setEncDec(encryptkey,decryptkey);
  1980. resp.setWuid(wu->queryId());
  1981. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  1982. submitDFUWorkUnit(wu.getClear());
  1983. }
  1984. catch(IException* e)
  1985. {
  1986. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1987. }
  1988. return true;
  1989. }
  1990. bool CFileSprayEx::doCopyForRoxie(IEspContext &context, const char * srcName, const char * srcDali, const char * srcUser, const char * srcPassword,
  1991. const char * dstName, const char * destCluster, bool compressed, bool overwrite, bool supercopy,
  1992. DFUclusterPartDiskMapping val, StringBuffer baseDir, StringBuffer fileMask, IEspCopyResponse &resp)
  1993. {
  1994. StringBuffer user, passwd;
  1995. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1996. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1997. if (supercopy)
  1998. {
  1999. wu->setJobName(dstName);
  2000. wu->setQueue(m_QueueLabel.str());
  2001. wu->setUser(context.getUserID(user).str());
  2002. wu->setPassword(context.getPassword(passwd).str());
  2003. wu->setClusterName(destCluster);
  2004. IDFUfileSpec *source = wu->queryUpdateSource();
  2005. wu->setCommand(DFUcmd_supercopy); // **** super copy
  2006. source->setLogicalName(srcName);
  2007. if (srcDali) // remote copy
  2008. {
  2009. SocketEndpoint ep(srcDali);
  2010. source->setForeignDali(ep);
  2011. source->setForeignUser(srcUser, srcPassword);
  2012. }
  2013. IDFUfileSpec *destination = wu->queryUpdateDestination();
  2014. destination->setLogicalName(dstName);
  2015. destination->setFileMask(fileMask);
  2016. destination->setClusterPartDiskMapping(val, baseDir, destCluster); // roxie
  2017. if(compressed)
  2018. destination->setCompressed(true);
  2019. destination->setWrap(true); // roxie always wraps
  2020. IDFUoptions *options = wu->queryUpdateOptions();
  2021. options->setOverwrite(overwrite);
  2022. options->setReplicate(val==DFUcpdm_c_replicated_by_d); // roxie
  2023. }
  2024. else
  2025. {
  2026. wu->setJobName(dstName);
  2027. wu->setQueue(m_QueueLabel.str());
  2028. wu->setUser(context.getUserID(user).str());
  2029. wu->setPassword(context.getPassword(passwd).str());
  2030. wu->setClusterName(destCluster);
  2031. wu->setCommand(DFUcmd_copy);
  2032. IDFUfileSpec *source = wu->queryUpdateSource();
  2033. source->setLogicalName(srcName);
  2034. if (srcDali) // remote copy
  2035. {
  2036. SocketEndpoint ep(srcDali);
  2037. source->setForeignDali(ep);
  2038. source->setForeignUser(srcUser, srcPassword);
  2039. }
  2040. IDFUfileSpec *destination = wu->queryUpdateDestination();
  2041. destination->setLogicalName(dstName);
  2042. destination->setFileMask(fileMask);
  2043. destination->setClusterPartDiskMapping(val, baseDir, destCluster, true); // **** repeat last part
  2044. if(compressed)
  2045. destination->setCompressed(true);
  2046. destination->setWrap(true); // roxie always wraps
  2047. IDFUoptions *options = wu->queryUpdateOptions();
  2048. options->setOverwrite(overwrite);
  2049. options->setReplicate(val==DFUcpdm_c_replicated_by_d); // roxie
  2050. options->setSuppressNonKeyRepeats(true); // **** only repeat last part when src kind = key
  2051. }
  2052. resp.setResult(wu->queryId());
  2053. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2054. submitDFUWorkUnit(wu.getClear());
  2055. return true;
  2056. }
  2057. bool CFileSprayEx::onCopy(IEspContext &context, IEspCopy &req, IEspCopyResponse &resp)
  2058. {
  2059. try
  2060. {
  2061. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, false))
  2062. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Copy. Permission denied.");
  2063. const char* srcname = req.getSourceLogicalName();
  2064. const char* dstname = req.getDestLogicalName();
  2065. if(!srcname || !*srcname)
  2066. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  2067. if(!dstname || !*dstname)
  2068. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination logical file not specified.");
  2069. StringBuffer destFolder, destTitle, defaultFolder, defaultReplicateFolder;
  2070. StringBuffer srcCluster, destCluster, destClusterName;
  2071. bool bRoxie = false;
  2072. const char* destCluster0 = req.getDestGroup();
  2073. if(destCluster0 == NULL || *destCluster0 == '\0')
  2074. {
  2075. getClusterFromLFN(srcname, srcCluster, context.queryUserId(), context.queryPassword());
  2076. DBGLOG("Destination cluster/group not specified, using source cluster %s", srcCluster.str());
  2077. destCluster = srcCluster.str();
  2078. destClusterName = srcCluster.str();
  2079. }
  2080. else
  2081. {
  2082. destCluster = destCluster0;
  2083. destClusterName = destCluster0;
  2084. const char* destClusterRoxie = req.getDestGroupRoxie();
  2085. if (destClusterRoxie && !stricmp(destClusterRoxie, "Yes"))
  2086. {
  2087. bRoxie = true;
  2088. }
  2089. }
  2090. int offset;
  2091. StringBuffer sbf, baseDir;
  2092. DFUclusterPartDiskMapping val;
  2093. CDfsLogicalFileName lfn;
  2094. if (!bRoxie)
  2095. {
  2096. if (!lfn.setValidate(dstname))
  2097. throw MakeStringException(ECLWATCH_INVALID_INPUT, "invalid destination filename");
  2098. dstname = lfn.get();
  2099. }
  2100. else
  2101. {
  2102. val = readClusterMappingSettings(destCluster.str(), baseDir, offset);
  2103. }
  2104. ParseLogicalPath(dstname, destCluster.str(), destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  2105. StringBuffer fileMask;
  2106. constructFileMask(destTitle.str(), fileMask);
  2107. const char* srcDali = req.getSourceDali();
  2108. bool supercopy = req.getSuperCopy();
  2109. if (supercopy)
  2110. {
  2111. StringBuffer user, passwd;
  2112. context.getUserID(user);
  2113. context.getPassword(passwd);
  2114. StringBuffer u(user);
  2115. StringBuffer p(passwd);
  2116. Owned<INode> foreigndali;
  2117. if (srcDali)
  2118. {
  2119. SocketEndpoint ep(srcDali);
  2120. foreigndali.setown(createINode(ep));
  2121. const char* srcu = req.getSrcusername();
  2122. if(srcu && *srcu)
  2123. {
  2124. u.clear().append(srcu);
  2125. p.clear().append(req.getSrcpassword());
  2126. }
  2127. }
  2128. Owned<IUserDescriptor> udesc=createUserDescriptor();
  2129. udesc->set(u.str(),p.str());
  2130. if (!queryDistributedFileDirectory().isSuperFile(srcname,udesc,foreigndali))
  2131. supercopy = false;
  2132. }
  2133. if (bRoxie)
  2134. {
  2135. bool compressRoxieCopy = false;
  2136. bool overwriteRoxieCopy = false;
  2137. if(req.getCompress())
  2138. compressRoxieCopy = true;
  2139. if(req.getOverwrite())
  2140. overwriteRoxieCopy = true;
  2141. return doCopyForRoxie(context, srcname, req.getSourceDali(), req.getSrcusername(), req.getSrcpassword(),
  2142. dstname, destCluster, req.getCompress(), req.getOverwrite(), supercopy, val, baseDir, fileMask, resp);
  2143. }
  2144. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2145. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2146. wu->setJobName(dstname);
  2147. wu->setQueue(m_QueueLabel.str());
  2148. StringBuffer user, passwd;
  2149. wu->setUser(context.getUserID(user).str());
  2150. wu->setPassword(context.getPassword(passwd).str());
  2151. if(destCluster.length() > 0)
  2152. {
  2153. wu->setClusterName(destCluster.str());
  2154. }
  2155. const char* srcDiffKeyName = req.getSourceDiffKeyName();
  2156. const char* destDiffKeyName = req.getDestDiffKeyName();
  2157. IDFUfileSpec *source = wu->queryUpdateSource();
  2158. IDFUfileSpec *destination = wu->queryUpdateDestination();
  2159. IDFUoptions *options = wu->queryUpdateOptions();
  2160. if (supercopy)
  2161. wu->setCommand(DFUcmd_supercopy);
  2162. else
  2163. wu->setCommand(DFUcmd_copy);
  2164. source->setLogicalName(srcname);
  2165. if(srcDali && *srcDali)
  2166. {
  2167. SocketEndpoint ep(srcDali);
  2168. source->setForeignDali(ep);
  2169. const char* srcusername = req.getSrcusername();
  2170. if(srcusername && *srcusername)
  2171. {
  2172. const char* srcpasswd = req.getSrcpassword();
  2173. source->setForeignUser(srcusername, srcpasswd);
  2174. }
  2175. }
  2176. if (bRoxie)
  2177. {
  2178. destination->setClusterPartDiskMapping(val, baseDir.str(), destCluster.str());
  2179. if (val != DFUcpdm_c_replicated_by_d)
  2180. {
  2181. options->setReplicate(false);
  2182. }
  2183. else
  2184. {
  2185. options->setReplicate(true);
  2186. destination->setReplicateOffset(offset);
  2187. }
  2188. }
  2189. if (srcDiffKeyName&&*srcDiffKeyName)
  2190. source->setDiffKey(srcDiffKeyName);
  2191. if (destDiffKeyName&&*destDiffKeyName)
  2192. destination->setDiffKey(destDiffKeyName);
  2193. if (!bRoxie)
  2194. {
  2195. destination->setDirectory(destFolder.str());
  2196. ClusterPartDiskMapSpec mspec;
  2197. destination->getClusterPartDiskMapSpec(destCluster.str(), mspec);
  2198. mspec.setDefaultBaseDir(defaultFolder.str());
  2199. mspec.setDefaultReplicateDir(defaultReplicateFolder.str());
  2200. destination->setClusterPartDiskMapSpec(destCluster.str(), mspec);
  2201. }
  2202. destination->setFileMask(fileMask.str());
  2203. destination->setGroupName(destCluster.str());
  2204. destination->setLogicalName(dstname);
  2205. const char * encryptkey = req.getEncrypt();
  2206. if(req.getCompress()||(encryptkey&&*encryptkey))
  2207. destination->setCompressed(true);
  2208. if (!bRoxie)
  2209. {
  2210. options->setReplicate(req.getReplicate());
  2211. destination->setWrap(req.getWrap());
  2212. }
  2213. else
  2214. {
  2215. destination->setWrap(true);
  2216. }
  2217. const char * decryptkey = req.getDecrypt();
  2218. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  2219. options->setEncDec(encryptkey,decryptkey);
  2220. options->setOverwrite(req.getOverwrite());
  2221. if(req.getNorecover())
  2222. options->setNoRecover(true);
  2223. if(!req.getNosplit_isNull())
  2224. options->setNoSplit(req.getNosplit());
  2225. if(req.getMaxConnections() > 0)
  2226. options->setmaxConnections(req.getMaxConnections());
  2227. if(req.getThrottle() > 0)
  2228. options->setThrottle(req.getThrottle());
  2229. if(req.getTransferBufferSize() > 0)
  2230. options->setTransferBufferSize(req.getTransferBufferSize());
  2231. if (req.getPull())
  2232. options->setPull(true);
  2233. if (req.getPush())
  2234. options->setPush(true);
  2235. if (req.getIfnewer())
  2236. options->setIfNewer(true);
  2237. resp.setResult(wu->queryId());
  2238. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2239. submitDFUWorkUnit(wu.getClear());
  2240. }
  2241. catch(IException* e)
  2242. {
  2243. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2244. }
  2245. return true;
  2246. }
  2247. bool CFileSprayEx::onRename(IEspContext &context, IEspRename &req, IEspRenameResponse &resp)
  2248. {
  2249. try
  2250. {
  2251. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, false))
  2252. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Rename. Permission denied.");
  2253. const char* srcname = req.getSrcname();
  2254. const char* dstname = req.getDstname();
  2255. if(!srcname || !*srcname)
  2256. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  2257. if(!dstname || !*dstname)
  2258. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination logical file not specified.");
  2259. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2260. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2261. StringBuffer destTitle;
  2262. ParseLogicalPath(req.getDstname(), destTitle);
  2263. wu->setJobName(destTitle.str());
  2264. wu->setQueue(m_QueueLabel.str());
  2265. StringBuffer user, passwd;
  2266. wu->setUser(context.getUserID(user).str());
  2267. wu->setPassword(context.getPassword(passwd).str());
  2268. wu->setCommand(DFUcmd_rename);
  2269. #if 0 // TBD - Handling for multiple clusters? the cluster should be specified by user if needed
  2270. Owned<IUserDescriptor> udesc;
  2271. if(user.length() > 0)
  2272. {
  2273. const char* passwd = context.queryPassword();
  2274. udesc.setown(createUserDescriptor());
  2275. udesc->set(user.str(), passwd);
  2276. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(srcname, udesc);
  2277. if(df)
  2278. {
  2279. StringBuffer cluster0;
  2280. df->getClusterName(0,cluster0); // TBD - Handling for multiple clusters?
  2281. if (cluster0.length()!=0)
  2282. {
  2283. wu->setClusterName(cluster0.str());
  2284. }
  2285. else
  2286. {
  2287. const char *cluster = df->queryAttributes().queryProp("@group");
  2288. if (cluster && *cluster)
  2289. {
  2290. wu->setClusterName(cluster);
  2291. }
  2292. }
  2293. }
  2294. }
  2295. #endif
  2296. IDFUfileSpec *source = wu->queryUpdateSource();
  2297. source->setLogicalName(srcname);
  2298. IDFUfileSpec *destination = wu->queryUpdateDestination();
  2299. destination->setLogicalName(dstname);
  2300. IDFUoptions *options = wu->queryUpdateOptions();
  2301. options->setOverwrite(req.getOverwrite());
  2302. resp.setWuid(wu->queryId());
  2303. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2304. submitDFUWorkUnit(wu.getClear());
  2305. }
  2306. catch(IException* e)
  2307. {
  2308. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2309. }
  2310. return true;
  2311. }
  2312. bool CFileSprayEx::onDFUWUFile(IEspContext &context, IEspDFUWUFileRequest &req, IEspDFUWUFileResponse &resp)
  2313. {
  2314. try
  2315. {
  2316. if (!context.validateFeatureAccess(DFU_WU_URL, SecAccess_Read, false))
  2317. throw MakeStringException(ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  2318. if (*req.getWuid())
  2319. {
  2320. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2321. Owned<IConstDFUWorkUnit> wu = factory->openWorkUnit(req.getWuid(), false);
  2322. if(!wu)
  2323. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  2324. StringBuffer xmlbuf;
  2325. xmlbuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  2326. const char* plainText = req.getPlainText();
  2327. if (plainText && (!stricmp(plainText, "yes")))
  2328. {
  2329. wu->toXML(xmlbuf);
  2330. resp.setFile(xmlbuf.str());
  2331. resp.setFile_mimetype(HTTP_TYPE_TEXT_PLAIN);
  2332. }
  2333. else
  2334. {
  2335. xmlbuf.append("<?xml-stylesheet href=\"../esp/xslt/xmlformatter.xsl\" type=\"text/xsl\"?>");
  2336. wu->toXML(xmlbuf);
  2337. resp.setFile(xmlbuf.str());
  2338. resp.setFile_mimetype(HTTP_TYPE_APPLICATION_XML);
  2339. }
  2340. }
  2341. }
  2342. catch(IException* e)
  2343. {
  2344. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2345. }
  2346. return true;
  2347. }
  2348. int CFileSprayEx::doFileCheck(const char* mask, const char* netaddr, const char* osStr, const char* path)
  2349. {
  2350. int iRet = 1;
  2351. if (mask && *mask)
  2352. {
  2353. char *str = (char *) mask + strlen(mask) - 4;
  2354. if (!stricmp(str, ".cfg") || !stricmp(str, ".log"))
  2355. iRet = 0;
  2356. }
  2357. else if (netaddr && *netaddr && path && *path)
  2358. {
  2359. iRet = 2;
  2360. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  2361. factory->validateCache();
  2362. Owned<IConstEnvironment> env = factory->openEnvironmentByFile();
  2363. Owned<IPropertyTree> pEnvRoot = &env->getPTree();
  2364. IPropertyTree* pEnvSoftware = pEnvRoot->queryPropTree("Software");
  2365. Owned<IPropertyTree> pRoot = createPTreeFromXMLString("<Environment/>");
  2366. IPropertyTree* pSoftware = pRoot->addPropTree("Software", createPTree("Software"));
  2367. if (pEnvSoftware && pSoftware)
  2368. {
  2369. Owned<IPropertyTreeIterator> it = pEnvSoftware->getElements("DropZone");
  2370. ForEach(*it)
  2371. {
  2372. const char* pszComputer = it->query().queryProp("@computer");
  2373. if (!strcmp(pszComputer, "."))
  2374. pszComputer = "localhost";
  2375. StringBuffer xpath, sNetAddr;
  2376. xpath.appendf("Hardware/Computer[@name='%s']/@netAddress", pszComputer);
  2377. const char* pszNetAddr = pEnvRoot->queryProp(xpath.str());
  2378. if (strcmp(pszNetAddr, "."))
  2379. {
  2380. sNetAddr.append(pszNetAddr);
  2381. }
  2382. else
  2383. {
  2384. StringBuffer ipStr;
  2385. IpAddress ipaddr = queryHostIP();
  2386. ipaddr.getIpText(ipStr);
  2387. if (ipStr.length() > 0)
  2388. {
  2389. #ifdef MACHINE_IP
  2390. sNetAddr.append(MACHINE_IP);
  2391. #else
  2392. sNetAddr.append(ipStr.str());
  2393. #endif
  2394. }
  2395. }
  2396. #ifdef MACHINE_IP
  2397. if ((sNetAddr.length() > 0) && !stricmp(sNetAddr.str(), MACHINE_IP))
  2398. #else
  2399. if ((sNetAddr.length() > 0) && !stricmp(sNetAddr.str(), netaddr))
  2400. #endif
  2401. {
  2402. StringBuffer dir;
  2403. IPropertyTree* pDropZone = pSoftware->addPropTree("DropZone", &it->get());
  2404. pDropZone->getProp("@directory", dir);
  2405. if (osStr && *osStr)
  2406. {
  2407. int os = atoi(osStr);
  2408. const char pathSep = (os == OS_WINDOWS) ? '\\' : '/';
  2409. dir.replace(pathSep=='\\'?'/':'\\', pathSep);
  2410. }
  2411. if ((dir.length() > 0) && !strnicmp(path, dir.str(), dir.length()))
  2412. {
  2413. iRet = 0;
  2414. break;
  2415. }
  2416. }
  2417. }
  2418. }
  2419. }
  2420. return iRet;
  2421. }
  2422. bool CFileSprayEx::onFileList(IEspContext &context, IEspFileListRequest &req, IEspFileListResponse &resp)
  2423. {
  2424. try
  2425. {
  2426. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, false))
  2427. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do FileList. Permission denied.");
  2428. const char* path = req.getPath();
  2429. if (!path || !*path)
  2430. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Path not specified.");
  2431. const char* netaddr = req.getNetaddr();
  2432. const char* mask = req.getMask();
  2433. bool directoryOnly = req.getDirectoryOnly();
  2434. StringBuffer sPath(path);
  2435. const char* osStr = req.getOS();
  2436. if (osStr && *osStr)
  2437. {
  2438. int os = atoi(osStr);
  2439. const char pathSep = (os == OS_WINDOWS) ? '\\' : '/';
  2440. sPath.replace(pathSep=='\\'?'/':'\\', pathSep);
  2441. if (*(sPath.str() + sPath.length() -1) != pathSep)
  2442. sPath.append( pathSep );
  2443. }
  2444. int checkReturn = doFileCheck(mask, netaddr, osStr, sPath.str());
  2445. if (checkReturn > 1)
  2446. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "Dropzone is not found in the environment settings.");
  2447. else if (checkReturn > 0)
  2448. throw MakeStringException(ECLWATCH_ACCESS_TO_FILE_DENIED, "Access to the file path denied.");
  2449. RemoteFilename rfn;
  2450. SocketEndpoint ep;
  2451. #ifdef MACHINE_IP
  2452. ep.set(MACHINE_IP);
  2453. #else
  2454. ep.set(netaddr);
  2455. #endif
  2456. rfn.setPath(ep, sPath.str());
  2457. Owned<IFile> f = createIFile(rfn);
  2458. if(!f->isDirectory())
  2459. throw MakeStringException(ECLWATCH_INVALID_DIRECTORY, "%s is not a directory.", path);
  2460. IArrayOf<IEspPhysicalFileStruct> files;
  2461. if (mask && !*mask)
  2462. mask = NULL;
  2463. Owned<IDirectoryIterator> di = f->directoryFiles(NULL, false, true);
  2464. if(di.get() != NULL)
  2465. {
  2466. ForEach(*di)
  2467. {
  2468. StringBuffer fname;
  2469. di->getName(fname);
  2470. if (fname.length() == 0 || (directoryOnly && !di->isDir()) || (!di->isDir() && mask && !WildMatch(fname.str(), mask, true)))
  2471. continue;
  2472. Owned<IEspPhysicalFileStruct> onefile = createPhysicalFileStruct();
  2473. onefile->setName(fname.str());
  2474. onefile->setIsDir(di->isDir());
  2475. onefile->setFilesize(di->getFileSize());
  2476. CDateTime modtime;
  2477. StringBuffer timestr;
  2478. di->getModifiedTime(modtime);
  2479. unsigned y,m,d,h,min,sec,nsec;
  2480. modtime.getDate(y,m,d,true);
  2481. modtime.getTime(h,min,sec,nsec,true);
  2482. timestr.appendf("%04d-%02d-%02d %02d:%02d:%02d", y,m,d,h,min,sec);
  2483. onefile->setModifiedtime(timestr.str());
  2484. files.append(*onefile.getLink());
  2485. }
  2486. }
  2487. sPath.replace('\\', '/');//XSLT cannot handle backslashes
  2488. resp.setPath(sPath);
  2489. resp.setFiles(files);
  2490. resp.setNetaddr(netaddr);
  2491. if (osStr && *osStr)
  2492. {
  2493. int os = atoi(osStr);
  2494. resp.setOS(os);
  2495. }
  2496. if (mask && *mask)
  2497. resp.setMask(mask);
  2498. resp.setDirectoryOnly(directoryOnly);
  2499. }
  2500. catch(IException* e)
  2501. {
  2502. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2503. }
  2504. return true;
  2505. }
  2506. bool CFileSprayEx::onDfuMonitor(IEspContext &context, IEspDfuMonitorRequest &req, IEspDfuMonitorResponse &resp)
  2507. {
  2508. try
  2509. {
  2510. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, false))
  2511. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do DfuMonitor. Permission denied.");
  2512. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2513. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2514. wu->setQueue(m_MonitorQueueLabel.str());
  2515. StringBuffer user, passwd;
  2516. wu->setUser(context.getUserID(user).str());
  2517. wu->setPassword(context.getPassword(passwd).str());
  2518. wu->setCommand(DFUcmd_monitor);
  2519. IDFUmonitor *monitor = wu->queryUpdateMonitor();
  2520. IDFUfileSpec *source = wu->queryUpdateSource();
  2521. const char *eventname = req.getEventName();
  2522. const char *lname = req.getLogicalName();
  2523. if (lname&&*lname)
  2524. source->setLogicalName(lname);
  2525. else {
  2526. const char *ip = req.getIp();
  2527. const char *filename = req.getFilename();
  2528. if (filename&&*filename) {
  2529. RemoteFilename rfn;
  2530. if (ip&&*ip) {
  2531. SocketEndpoint ep;
  2532. ep.set(ip);
  2533. rfn.setPath(ep,filename);
  2534. }
  2535. else
  2536. rfn.setRemotePath(filename);
  2537. source->setSingleFilename(rfn);
  2538. }
  2539. else
  2540. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Neither logical name nor network ip/file specified for monitor.");
  2541. }
  2542. if (eventname)
  2543. monitor->setEventName(eventname);
  2544. monitor->setShotLimit(req.getShotLimit());
  2545. monitor->setSub(req.getSub());
  2546. resp.setWuid(wu->queryId());
  2547. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2548. submitDFUWorkUnit(wu.getClear());
  2549. }
  2550. catch(IException* e)
  2551. {
  2552. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2553. }
  2554. return true;
  2555. }
  2556. bool CFileSprayEx::onOpenSave(IEspContext &context, IEspOpenSaveRequest &req, IEspOpenSaveResponse &resp)
  2557. {
  2558. try
  2559. {
  2560. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, false))
  2561. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  2562. const char* location = req.getLocation();
  2563. const char* path = req.getPath();
  2564. const char* name = req.getName();
  2565. const char* type = req.getType();
  2566. const char* dateTime = req.getDateTime();
  2567. if (location && *location)
  2568. resp.setLocation(location);
  2569. if (path && *path)
  2570. resp.setPath(path);
  2571. if (name && *name)
  2572. resp.setName(name);
  2573. if (type && *type)
  2574. resp.setType(type);
  2575. if (dateTime && *dateTime)
  2576. resp.setDateTime(dateTime);
  2577. if (req.getBinaryFile())
  2578. resp.setViewable(false);
  2579. }
  2580. catch(IException* e)
  2581. {
  2582. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2583. }
  2584. return true;
  2585. }
  2586. bool CFileSprayEx::getDropZoneFiles(IEspContext &context, const char* netaddr, const char* osStr, const char* path,
  2587. IEspDropZoneFilesRequest &req, IEspDropZoneFilesResponse &resp)
  2588. {
  2589. bool directoryOnly = req.getDirectoryOnly();
  2590. int checkReturn = doFileCheck(NULL, netaddr, osStr, path);
  2591. if (checkReturn > 1)
  2592. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "Dropzone is not found in the environment settings.");
  2593. else if (checkReturn > 0)
  2594. throw MakeStringException(ECLWATCH_ACCESS_TO_FILE_DENIED, "Access to the file path denied.");
  2595. RemoteFilename rfn;
  2596. SocketEndpoint ep;
  2597. #ifdef MACHINE_IP
  2598. ep.set(MACHINE_IP);
  2599. #else
  2600. ep.set(netaddr);
  2601. #endif
  2602. rfn.setPath(ep, path);
  2603. Owned<IFile> f = createIFile(rfn);
  2604. if(!f->isDirectory())
  2605. throw MakeStringException(ECLWATCH_INVALID_DIRECTORY, "%s is not a directory.", path);
  2606. IArrayOf<IEspPhysicalFileStruct> files;
  2607. Owned<IDirectoryIterator> di = f->directoryFiles(NULL, false, true);
  2608. if(di.get() != NULL)
  2609. {
  2610. ForEach(*di)
  2611. {
  2612. StringBuffer fname;
  2613. di->getName(fname);
  2614. if (fname.length() == 0 || (directoryOnly && !di->isDir()))
  2615. continue;
  2616. Owned<IEspPhysicalFileStruct> onefile = createPhysicalFileStruct();
  2617. onefile->setName(fname.str());
  2618. onefile->setIsDir(di->isDir());
  2619. onefile->setFilesize(di->getFileSize());
  2620. CDateTime modtime;
  2621. StringBuffer timestr;
  2622. di->getModifiedTime(modtime);
  2623. unsigned y,m,d,h,min,sec,nsec;
  2624. modtime.getDate(y,m,d,true);
  2625. modtime.getTime(h,min,sec,nsec,true);
  2626. timestr.appendf("%04d-%02d-%02d %02d:%02d:%02d", y,m,d,h,min,sec);
  2627. onefile->setModifiedtime(timestr.str());
  2628. files.append(*onefile.getLink());
  2629. }
  2630. }
  2631. resp.setFiles(files);
  2632. return true;
  2633. }
  2634. bool CFileSprayEx::onDropZoneFiles(IEspContext &context, IEspDropZoneFilesRequest &req, IEspDropZoneFilesResponse &resp)
  2635. {
  2636. try
  2637. {
  2638. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, false))
  2639. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  2640. const char* netAddress = req.getNetAddress();
  2641. const char* directory = req.getPath();
  2642. const char* subfolder = req.getSubfolder();
  2643. StringBuffer netAddressStr, directoryStr, osStr;
  2644. if (netAddress && *netAddress && directory && *directory)
  2645. {
  2646. netAddressStr.append(netAddress);
  2647. directoryStr.append(directory);
  2648. }
  2649. IArrayOf<IEspDropZone> dropZoneList;
  2650. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  2651. Owned<IConstEnvironment> m_constEnv = factory->openEnvironmentByFile();
  2652. Owned<IPropertyTree> pEnvRoot = &m_constEnv->getPTree();
  2653. IPropertyTree* pEnvSoftware = pEnvRoot->queryPropTree("Software");
  2654. if (pEnvSoftware)
  2655. {
  2656. Owned<IPropertyTreeIterator> it = pEnvSoftware->getElements("DropZone");
  2657. ForEach(*it)
  2658. {
  2659. IPropertyTree& pDropZone = it->query();
  2660. //get IP Address of the computer associated with this drop zone
  2661. const char* pszName = pDropZone.queryProp("@name");
  2662. const char* pszComputer = pDropZone.queryProp("@computer");
  2663. if (!strcmp(pszComputer, "."))
  2664. pszComputer = "localhost";
  2665. StringBuffer xpath;
  2666. xpath.appendf("Hardware/Computer[@name='%s']/@netAddress", pszComputer);
  2667. StringBuffer sNetAddr;
  2668. const char* pszNetAddr = pEnvRoot->queryProp(xpath.str());
  2669. if (strcmp(pszNetAddr, "."))
  2670. {
  2671. sNetAddr.append(pszNetAddr);
  2672. }
  2673. else
  2674. {
  2675. StringBuffer ipStr;
  2676. IpAddress ipaddr = queryHostIP();
  2677. ipaddr.getIpText(ipStr);
  2678. if (ipStr.length() > 0)
  2679. {
  2680. #ifdef MACHINE_IP
  2681. sNetAddr.append(MACHINE_IP);
  2682. #else
  2683. sNetAddr.append(ipStr.str());
  2684. #endif
  2685. }
  2686. }
  2687. Owned<IConstMachineInfo> machine;
  2688. if (strcmp(pszNetAddr, "."))
  2689. machine.setown(m_constEnv->getMachineByAddress(sNetAddr.str()));
  2690. else
  2691. {
  2692. machine.setown(m_constEnv->getMachineByAddress(pszNetAddr));
  2693. if (!machine)
  2694. machine.setown(m_constEnv->getMachineByAddress(sNetAddr.str()));
  2695. }
  2696. StringBuffer dir;
  2697. pDropZone.getProp("@directory", dir);
  2698. Owned<IEspDropZone> aDropZone= createDropZone("","");
  2699. if (machine)
  2700. {
  2701. if (machine->getOS() == MachineOsLinux || machine->getOS() == MachineOsSolaris)
  2702. {
  2703. dir.replace('\\', '/');//replace all '\\' by '/'
  2704. aDropZone->setLinux("true");
  2705. osStr = "1";
  2706. }
  2707. else
  2708. {
  2709. dir.replace('/', '\\');
  2710. dir.replace('$', ':');
  2711. osStr = "0";
  2712. }
  2713. }
  2714. aDropZone->setComputer(pszComputer);
  2715. aDropZone->setPath(dir.str());
  2716. aDropZone->setName(pszName);
  2717. aDropZone->setNetAddress(sNetAddr.str());
  2718. if (netAddressStr.length() < 1)
  2719. {
  2720. netAddressStr = sNetAddr;
  2721. directoryStr = dir;
  2722. }
  2723. dropZoneList.append(*aDropZone.getClear());
  2724. }
  2725. }
  2726. if (dropZoneList.ordinality())
  2727. resp.setDropZones(dropZoneList);
  2728. char pathSep = '/';
  2729. if (osStr && *osStr)
  2730. {
  2731. int os = atoi(osStr);
  2732. if (os == OS_WINDOWS)
  2733. pathSep = '\\';
  2734. }
  2735. directoryStr.replace(pathSep=='\\'?'/':'\\', pathSep);
  2736. if (subfolder && *subfolder)
  2737. {
  2738. if (*(directoryStr.str() + directoryStr.length() -1) != pathSep)
  2739. directoryStr.append( pathSep );
  2740. directoryStr.append(subfolder);
  2741. }
  2742. if (*(directoryStr.str() + directoryStr.length() -1) != pathSep)
  2743. directoryStr.append( pathSep );
  2744. getDropZoneFiles(context, netAddressStr.str(), osStr.str(), directoryStr.str(), req, resp);
  2745. if (pathSep=='\\')
  2746. directoryStr.replaceString("\\", "\\\\");
  2747. resp.setNetAddress(netAddressStr.str());
  2748. resp.setPath(directoryStr.str());
  2749. resp.setOS(atoi(osStr.str()));
  2750. }
  2751. catch(IException* e)
  2752. {
  2753. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2754. }
  2755. return true;
  2756. }
  2757. bool CFileSprayEx::onDeleteDropZoneFiles(IEspContext &context, IEspDeleteDropZoneFilesRequest &req, IEspDFUWorkunitsActionResponse &resp)
  2758. {
  2759. try
  2760. {
  2761. if (!context.validateFeatureAccess(FILE_SPRAY_URL, SecAccess_Full, false))
  2762. throw MakeStringException(ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  2763. const char* netAddress = req.getNetAddress();
  2764. const char* directory = req.getPath();
  2765. const char* osStr = req.getOS();
  2766. StringArray & files = req.getNames();
  2767. if (!netAddress || !*netAddress || !directory || !*directory)
  2768. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "Dropzone not specified.");
  2769. if (!files.ordinality())
  2770. throw MakeStringException(ECLWATCH_INVALID_INPUT, "File not specified.");
  2771. char pathSep = '/';
  2772. StringBuffer sPath(directory);
  2773. if (osStr && *osStr)
  2774. {
  2775. int os = atoi(osStr);
  2776. pathSep = (os == OS_WINDOWS) ? '\\' : '/';
  2777. sPath.replace(pathSep=='\\'?'/':'\\', pathSep);
  2778. if (*(sPath.str() + sPath.length() -1) != pathSep)
  2779. sPath.append( pathSep );
  2780. }
  2781. int checkReturn = doFileCheck(NULL, netAddress, osStr, sPath.str());
  2782. if (checkReturn > 1)
  2783. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "Dropzone is not found in the environment settings.");
  2784. else if (checkReturn > 0)
  2785. throw MakeStringException(ECLWATCH_ACCESS_TO_FILE_DENIED, "Access to the file path denied.");
  2786. RemoteFilename rfn;
  2787. SocketEndpoint ep;
  2788. #ifdef MACHINE_IP
  2789. ep.set(MACHINE_IP);
  2790. #else
  2791. ep.set(netAddress);
  2792. #endif
  2793. rfn.setPath(ep, sPath.str());
  2794. Owned<IFile> f = createIFile(rfn);
  2795. if(!f->isDirectory())
  2796. throw MakeStringException(ECLWATCH_INVALID_DIRECTORY, "%s is not a directory.", directory);
  2797. bool bAllSuccess = true;
  2798. IArrayOf<IEspDFUActionResult> results;
  2799. for(unsigned i = 0; i < files.ordinality(); ++i)
  2800. {
  2801. const char* file = files.item(i);
  2802. if (!file || !*file)
  2803. continue;
  2804. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  2805. res->setID(files.item(i));
  2806. res->setAction("Delete");
  2807. res->setResult("Success");
  2808. try
  2809. {
  2810. StringBuffer fileToDelete = sPath;
  2811. if (*(fileToDelete.str() + fileToDelete.length() -1) != pathSep)
  2812. fileToDelete.append( pathSep );
  2813. fileToDelete.append(file);
  2814. rfn.setPath(ep, fileToDelete.str());
  2815. Owned<IFile> rFile = createIFile(rfn);
  2816. if (!rFile->exists())
  2817. res->setResult("Warning: this file does not exist.");
  2818. else
  2819. rFile->remove();
  2820. }
  2821. catch (IException *e)
  2822. {
  2823. bAllSuccess = false;
  2824. StringBuffer eMsg;
  2825. eMsg = e->errorMessage(eMsg);
  2826. e->Release();
  2827. StringBuffer failedMsg = "Failed: ";
  2828. failedMsg.append(eMsg);
  2829. res->setResult(failedMsg.str());
  2830. }
  2831. results.append(*res.getLink());
  2832. }
  2833. resp.setFirstColumn("File");
  2834. resp.setDFUActionResults(results);
  2835. }
  2836. catch(IException* e)
  2837. {
  2838. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2839. }
  2840. return true;
  2841. }