ws_fsService.cpp 122 KB

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