Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1211 lines
31 KiB

  1. if (!$__IITPRINTLPM ) { use iit::printl; }
  2. if (!$__IITUTILPM ) { use iit::util; }
  3. use Win32::File;
  4. use File::Copy; #allows use of built in copy() and move() functions
  5. use File::Path; #allows use of built in mkpath() and rmtree() functions
  6. use File::Compare;
  7. use FileHandle; #allows use of activeperl filehandle layer
  8. use Cwd; #allows use of cwd() to get current working directory
  9. use English; #allows use of english names for $(*) variables
  10. ####################################################################################
  11. # DelAll()
  12. # deletes all files in a first argument directory name (recursively deletes if argument 2 is non-null)
  13. # report number of files in what directory deleted to screen and log
  14. # returns total number (including recursively) of deleted files
  15. # a-jbilas, 04/10/99 - created
  16. # a-jbilas, 06/11/99 - check if PushD() fails, don't remove directories after delete
  17. # a-jbilas, 07/20/99 - remove directories after deletion, don't delete files or dirs containing slm.ini
  18. ####################################################################################
  19. sub DelAll($;$$)
  20. {
  21. my($sDirectory, $bRecurse, $bIgnoreIni) = @_;
  22. my($fileNum) = 0;
  23. if ((-e $sDirectory) && PushD($sDirectory))
  24. {
  25. my($bNoRemove) = 0;
  26. my(@lFiles) = GetFiles();
  27. if ($bIgnoreIni || !IsMemberOf("slm.ini", @lFiles))
  28. {
  29. local(@lDeletedFiles) = ();
  30. foreach $file (@lFiles)
  31. {
  32. if (!unlink($file))
  33. {
  34. PrintL("Could not delete ".$file." ($!)\n", PL_ERROR);
  35. $rc = 0;
  36. }
  37. else
  38. {
  39. push(@lDeletedFiles, $file);
  40. }
  41. }
  42. PrintLTip(" - deleted ".scalar(@lDeletedFiles)." files in ".cwd()."\n", join(", ", @lDeletedFiles), PL_BLUE);
  43. $fileNum += scalar(@lDeletedFiles);
  44. }
  45. else
  46. {
  47. $bNoRemove = 1;
  48. }
  49. if (defined $bRecurse)
  50. {
  51. foreach $dir (GetSubdirs())
  52. {
  53. if (-d cwd()."\\$dir")
  54. {
  55. $fileNum += DelAll(cwd()."\\$dir", 1, $bIgnoreIni); #recurse each directory
  56. }
  57. }
  58. }
  59. PopD(); #$sDirectory
  60. if (!$bNoRemove)
  61. {
  62. if (!rmdir($sDirectory))
  63. {
  64. PrintL("Could not remove directory: ".$sDirectory." (".$!.")\n", PL_ERROR);
  65. }
  66. }
  67. }
  68. return($fileNum);
  69. }
  70. ####################################################################################
  71. # DelOld()
  72. # search recursively for directory names matching elements in @lBuilds and call DelOld
  73. # to delete their contents
  74. # a-jbilas, 04/10/99 - created
  75. # a-jbilas, 06/11/99 - check if PushD() fails
  76. ####################################################################################
  77. sub DelOld
  78. {
  79. carp("Usage: bool DelOld(directory, listBuildsPtr) ")
  80. unless(@_ == 2);
  81. local($sDirectory, *m_lBuilds) = @_;
  82. my($nDelFiles) = 0;
  83. if ((-d $sDirectory) && PushD($sDirectory))
  84. {
  85. opendir(SRC, $sDirectory); #must be directories as all files are deleted
  86. local(@lDirectories) = grep(!/^\.\.?$/, readdir(SRC)); #(ignore .. .)
  87. foreach $dir (@lDirectories)
  88. {
  89. if ((-d cwd()."\\$dir") && (IsMemberOf($dir, @m_lBuilds)))
  90. {
  91. $nDelFiles += DelAll(cwd()."\\$dir", 1); #recursively delete target and object dirs
  92. }
  93. elsif (-d cwd()."\\$dir")
  94. {
  95. $nDelFiles += DelOld(cwd()."\\$dir", *m_lBuilds); #recursively look for target and object dirs
  96. }
  97. }
  98. PopD(); #$sDirectory
  99. }
  100. closedir($sDirectory);
  101. return($nDelFiles);
  102. }
  103. ####################################################################################
  104. # EchoedCopy()
  105. # copy file1 to file2 and echo results to screen and log
  106. # returns 1 for success, 0 for failure
  107. # a-jbilas, 04/10/99 - created
  108. # a-jbilas, 08/04/99 - use wildcards
  109. ####################################################################################
  110. sub EchoedCopy($;$)
  111. {
  112. my($sFile1, $sFile2) = @_;
  113. my($rc) = 1;
  114. if ($bVerbose) { PrintL(" - Called EchoedCopy (".$_[0].", ".$_[1].")\n", PL_PURPLE); }
  115. if ($sFile2 eq "") #copy to current directory (no directory or filename given)
  116. {
  117. $sFile2 = cwd();
  118. }
  119. if (IsDirectory($sFile2) && ($sFile1 !~ /(\*|\?)/)) #copy to path (no filename given)
  120. {
  121. $sFile2 .= "\\".RemovePath($sFile1);
  122. }
  123. if ($sFile2 =~ /(\*|\?)/)
  124. {
  125. PrintL("EchoedCopy() Error: destination does not support wildcards\n",
  126. (IsCritical() ? PL_BIGERROR : PL_ERROR) | PL_SETERROR);
  127. return(0);
  128. }
  129. $sFile1 =~ s/\//\\/g;
  130. $sFile2 =~ s/\//\\/g;
  131. if ($sFile1 =~ /(\*|\?)/) # if file contains wildcards
  132. {
  133. if ($sFile1 =~ /\?/)
  134. {
  135. my($tmp) = $sFile1;
  136. $tmp =~ s/\?/ /g;
  137. if (($tmp =~ /\*/) || (-e $tmp))
  138. {
  139. $rc = EchoedCopy($tmp, $sFile2) && $rc;
  140. }
  141. }
  142. foreach $file (glob($sFile1))
  143. {
  144. $rc = EchoedCopy($file, $sFile2) && $rc;
  145. }
  146. }
  147. else
  148. {
  149. PrintL(" - Copying ".$sFile1." --> ".$sFile2."\n", PL_BLUE);
  150. if ($sFile2 =~ /\\/ && !IsDirectory(GetPath($sFile2)))
  151. {
  152. PrintL("EchoedCopy() Warning : Destination directory does not exist, creating ...\n",
  153. (IsCritical() ? PL_BIGWARNING : PL_WARNING) | PL_SETERROR);
  154. EchoedMkdir(GetPath($sFile2));
  155. }
  156. if (!copy($sFile1, $sFile2))
  157. {
  158. $rc = 0;
  159. my($err) = $!;
  160. PrintL("Copy of ".$sFile1." --> ".$sFile2." <b>FAILED</b>",
  161. (IsCritical() ? PL_BIGERROR : PL_ERROR) | PL_SETERROR);
  162. PrintL("\n$err\n\n", PL_RED | PL_BOLD | PL_SETERROR);
  163. if (IsCritical())
  164. {
  165. PrintMsgBlock(split(/\n/, $err));
  166. }
  167. }
  168. }
  169. if (!$rc && IsCritical())
  170. {
  171. $bCopyFailed = 1;
  172. $bcStatus |= BC_COPYFAILED
  173. }
  174. return($rc);
  175. }
  176. ####################################################################################
  177. # EchoedCompare()
  178. # compare file1 to file2 and echo results to screen and log
  179. # returns 1 for identical, 0 for differ (or not exist)
  180. # a-jbilas, 08/01/99 - created
  181. # a-jbilas, 09/17/99 - add file diff (third arg non-null to enable)
  182. # a-jbilas, 10/20/99 - if second arg is null, file will be tested for zerolength
  183. ####################################################################################
  184. sub EchoedCompare($$;$)
  185. {
  186. my($rc) = 1;
  187. my($f1, $f2) = @_;
  188. my($bRemoteDiff) = ((scalar(@_) == 3) ? 1 : 0);
  189. if ($f1 eq "")
  190. {
  191. PrintL(' - Comparing '.$f2.' against an empty file'."\n", PL_BLUE);
  192. if (!-e $f2)
  193. {
  194. PrintL($f2.' does not exist'."\n", PL_BLUE);
  195. return(1);
  196. }
  197. else
  198. {
  199. $f1 = "null";
  200. if (!-e "null")
  201. {
  202. PrintToFile($f1, "");
  203. }
  204. }
  205. }
  206. if ($f2 eq "")
  207. {
  208. PrintL(' - Comparing '.$f1.' against an empty file'."\n", PL_BLUE);
  209. if (!-e $f1)
  210. {
  211. PrintL($f1.' does not exist'."\n", PL_BLUE);
  212. return(1);
  213. }
  214. else
  215. {
  216. $f2 = "null";
  217. if (!-e "null")
  218. {
  219. PrintToFile($f2, "");
  220. }
  221. }
  222. # swap the files (better for first file to be null)
  223. my($temp) = $f1;
  224. $f1 = $f2;
  225. $f2 = $temp;
  226. }
  227. else
  228. {
  229. PrintL(' - Comparing '.$f1.' against '.$f2."\n", PL_BLUE);
  230. if (!-e $f1)
  231. {
  232. PrintL($f1." does not exist\n", PL_WARNING);
  233. $rc = 0;
  234. }
  235. if (!-e $f2)
  236. {
  237. PrintL($f2." does not exist\n", PL_WARNING);
  238. $rc = 0;
  239. }
  240. }
  241. if ($rc)
  242. {
  243. if (compare($f1, $f2) != 0)
  244. {
  245. $rc = 0;
  246. if ($bRemoteDiff && (($sDropDir && $bOfficialBuild) || (!$bOfficialBuild && ($TEMP ne ""))))
  247. {
  248. my($sDiffDir) = $sDropDir."\\dif";
  249. if (!$bOfficialBuild)
  250. {
  251. $sDiffDir = $TEMP."\\".$PROJ."dif";
  252. }
  253. my($sDiffFile1) = $sDiffDir."\\".time().".".RemovePath($f1);
  254. my($sDiffFile2) = $sDiffDir."\\".(time() + 1).".".RemovePath($f2);
  255. my($sDiffBat) = $sDiffDir."\\".(time() + 2).".ViewDiff.bat";
  256. if (EchoedMkdir($sDiffDir)
  257. && EchoedCopy($f1, $sDiffFile1)
  258. && EchoedCopy($f2, $sDiffFile2)
  259. && PrintToFile($sDiffBat, "start windiff.exe ".$sDiffFile1." ".$sDiffFile2."\n"))
  260. {
  261. PrintL(" - ".$f1." and ".$f2." differ (<a href=\"".TranslateToHTTP($sDiffBat)."\">"
  262. ."click and run to view diff<\/a>)\n", PL_BLUE | PL_SETERROR | PL_NOTAG);
  263. }
  264. else
  265. {
  266. PrintL(" - ".$f1." and ".$f2." differ\n", PL_BLUE | PL_SETERROR);
  267. }
  268. }
  269. else
  270. {
  271. PrintL(" - ".$f1." and ".$f2." differ\n", PL_BLUE | PL_SETERROR);
  272. }
  273. }
  274. else
  275. {
  276. PrintL(" - files are identical\n", PL_BLUE);
  277. }
  278. }
  279. return($rc);
  280. }
  281. ####################################################################################
  282. # EchoedMkdir()
  283. # make a directory from passed argument and echo results to screen and log
  284. # returns 1 for success, 0 for failure
  285. # a-jbilas, 04/20/99 - created
  286. ####################################################################################
  287. sub EchoedMkdir($)
  288. {
  289. my($sPath) = @_;
  290. my($rc) = 1;
  291. if (!-d $sPath)
  292. {
  293. PrintL(" - Creating path ".$sPath."\n", PL_BLUE);
  294. my($sMsg) = "";
  295. eval
  296. {
  297. PrintL("mkdir ".$sPath."\n", PL_VERBOSE);
  298. if ($bWin98)
  299. {
  300. open(FPIN, 'md '.$sPath.' |');
  301. }
  302. else
  303. {
  304. open(FPIN, 'mkdir '.$sPath.' 2>&1 |');
  305. }
  306. while (<FPIN>)
  307. {
  308. PrintL($_);
  309. $sMsg .= "<dd>".$_;
  310. }
  311. close (FPIN);
  312. };
  313. if ($CHILD_ERROR)
  314. {
  315. $rc = 0;
  316. PrintL("Creation of path ".$sPath." <b>FAILED</b>\n",
  317. (IsCritical() ? PL_BIGERROR : PL_ERROR) | PL_SETERROR);
  318. if ($sMsg ne "")
  319. {
  320. PrintMsgBlock(split(/\n/, $sMsg));
  321. }
  322. }
  323. }
  324. else
  325. {
  326. PrintL("EchoedMkdir($sPath): directory already exists\n", PL_VERBOSE);
  327. }
  328. return($rc);
  329. }
  330. ####################################################################################
  331. # EchoedUnlink()
  332. # delete multiple or single files passed by string, echo results
  333. # returns 1 on all deletions successful, 0 if any deletions fail
  334. # a-jbilas, 08/01/99 - created
  335. ####################################################################################
  336. sub EchoedUnlink
  337. {
  338. my($rc) = 1;
  339. local(@lDeletedFiles) = ();
  340. for ($index = 0 ; $index < scalar(@_) ; ++$index)
  341. {
  342. $! = "";
  343. if ($_[$index] =~ /(\*|\?)/) # if file contains wildcards
  344. {
  345. my($temp) = $_[$index];
  346. $temp =~ s/\//\\/g;
  347. $rc = EchoedUnlink(glob($temp)) && $rc;
  348. }
  349. elsif (!unlink($_[$index]))
  350. {
  351. if ($! eq "No such file or directory")
  352. {
  353. PrintL("Warning: Could not delete ".$_[$index]." ($!)\n", PL_WARNING | PL_VERBOSE);
  354. }
  355. else
  356. {
  357. PrintL("Could not delete ".$_[$index]." ($!)\n", PL_ERROR);
  358. }
  359. $rc = 0;
  360. }
  361. else
  362. {
  363. push(@lDeletedFiles, $_[$index]);
  364. }
  365. }
  366. if (@lDeletedFiles != ())
  367. {
  368. PrintL(" - Deleted ".join(", ", @lDeletedFiles)."\n", PL_BLUE);
  369. }
  370. return($rc);
  371. }
  372. ####################################################################################
  373. # EchoedMove()
  374. # rename a file and echo results
  375. # returns 1 on success, 0 on failure
  376. # a-jbilas, 08/01/99 - created
  377. ####################################################################################
  378. sub EchoedMove($$)
  379. {
  380. my($rc) = 1;
  381. my($file1, $file2) = @_;
  382. $file1 =~ s/\//\\/g;
  383. $file2 =~ s/\//\\/g;
  384. if (($file1 =~ /(\*|\?)/) || ($file2 =~ /(\*|\?)/)) # if files contain wildcards
  385. {
  386. $rc = Execute("move /Y ".$file1." ".$file2) && $rc; #REVIEW: win9x compatibility?
  387. }
  388. else
  389. {
  390. PrintL(" - Renaming ".$file1." --> ".$file2."\n", PL_BLUE);
  391. if (!-e $file1)
  392. {
  393. my($err) = $!;
  394. if (IsCritical())
  395. {
  396. PrintL("Rename of ".$file1." --> ".$file2." <b>FAILED</b>",
  397. PL_BIGERROR | PL_SETERROR);
  398. PrintMsgBlock($err);
  399. }
  400. else
  401. {
  402. PrintL("Rename of ".$file1." --> ".$file2." <b>FAILED</b>\n$err",
  403. PL_ERROR);
  404. }
  405. $rc = 0;
  406. }
  407. else
  408. {
  409. EchoedUnlink($file2);
  410. if (!move($file1, $file2))
  411. {
  412. my($err) = $!;
  413. if (IsCritical())
  414. {
  415. PrintL("Rename of ".$file1." --> ".$file2." <b>FAILED</b>",
  416. PL_BIGERROR | PL_SETERROR);
  417. PrintMsgBlock($err);
  418. }
  419. else
  420. {
  421. PrintL("Rename of ".$file1." --> ".$file2." <b>FAILED</b>\n$err",
  422. PL_ERROR);
  423. }
  424. $rc = 0;
  425. }
  426. }
  427. }
  428. if (!$rc && IsCritical())
  429. {
  430. $bCopyFailed = 1;
  431. }
  432. return($rc);
  433. }
  434. ####################################################################################
  435. # PopD()
  436. # perl version of DOS pushd
  437. # differences:
  438. # will warn user if empty directory stack popped instead of simply doing nothing
  439. # returns 1 on success, 0 on error
  440. # a-jbilas, 03/10/99 - created
  441. ####################################################################################
  442. sub PopD
  443. {
  444. $sNewDir = pop(@__sDirStack) || PrintL("Error: Trying to pop an empty directory stack!\n",
  445. PL_BIGERROR | PL_SETERROR); #TODO: break?
  446. # if (($_[0] ne "") && (lc($sNewDir) ne lc($_[0])))
  447. # {
  448. # PrintL("PopD() Warning : dir verification fails (expected: ".$_[0].", actual: ".$sNewDir.")\n", PL_BIGWARNING);
  449. # }
  450. if (!chdir($sNewDir))
  451. {
  452. PrintL("PopD() ERROR : $!\n", PL_BIGERROR);
  453. return 0;
  454. }
  455. PrintL("Popped to $sNewDir\n", PL_VERBOSE);
  456. return 1;
  457. }
  458. ####################################################################################
  459. # PushD()
  460. # perl version of DOS pushd
  461. # differences:
  462. # will create directory (and warn user) if pushed directory doesn't exist instead of simply doing nothing
  463. # returns 1 on success, 0 on error
  464. # a-jbilas, 03/10/99 - created
  465. ####################################################################################
  466. sub PushD($)
  467. {
  468. carp("Usage: PushD(directory) ")
  469. unless(@_ == 1);
  470. if (!defined @__sDirStack)
  471. {
  472. @__sDirStack = ();
  473. }
  474. my($sNewDir) = @_;
  475. $sCurDir = cwd();
  476. if (!-d $sNewDir)
  477. {
  478. EchoedMkdir($sNewDir);
  479. PrintL("PushD() Warning: creating new directory: ".$sNewDir."\n", PL_BIGWARNING | PL_SETERROR);
  480. }
  481. if (!chdir($sNewDir) && !chdir("$sCurDir\\$sNewDir"))
  482. {
  483. PrintL("PushD() Error: Cannot open directory $sNewDir (".$!.")\n", PL_BIGERROR | PL_SETERROR);
  484. return(0);
  485. }
  486. push(@__sDirStack, $sCurDir);
  487. PrintL("Pushed to $sNewDir\n", PL_VERBOSE);
  488. return(1);
  489. }
  490. ####################################################################################
  491. # OpenFile()
  492. # wrapper for filehandle->open
  493. # when passed a filename and a accesstype (read/write/append/full), function will
  494. # return a filehandle associated with the given filename (returns 0 for failure)
  495. # a-jbilas, 03/10/99 - created
  496. ####################################################################################
  497. sub OpenFile($$)
  498. {
  499. # TODO: add combined opens
  500. my($__OpenFileFH) = 0;
  501. local($sFileName, $sFileAccessType) = @_;
  502. if ($sFileAccessType =~ /^r(ead)?$/i)
  503. {
  504. $__OpenFileFH = new FileHandle;
  505. if ($__OpenFileFH->open("<".$sFileName))
  506. {
  507. PrintL("$sFileName successfully opened for input\n", PL_VERBOSE);
  508. }
  509. elsif (IsCritical())
  510. {
  511. PrintL("OpenFile() Error: could not open $sFileName for input\n", PL_BIGERROR | PL_SETERROR);
  512. PrintMsgBlock($!);
  513. $__OpenFileFH = 0;
  514. }
  515. else
  516. {
  517. PrintL("OpenFile() Error: could not open $sFileName for input\n", PL_ERROR | PL_SETERROR);
  518. $__OpenFileFH = 0;
  519. }
  520. }
  521. elsif ($sFileAccessType =~ /^w(rite)?$/i)
  522. {
  523. $__OpenFileFH = new FileHandle;
  524. if ($__OpenFileFH->open(">".$sFileName))
  525. {
  526. PrintL("$sFileName successfully opened for output\n", PL_VERBOSE);
  527. }
  528. elsif (IsCritical())
  529. {
  530. PrintL("OpenFile() Error: could not open $sFileName for output\n", PL_BIGERROR | PL_SETERROR);
  531. $__OpenFileFH = 0;
  532. }
  533. else
  534. {
  535. PrintL("OpenFile() Error: could not open $sFileName for output\n", PL_ERROR | PL_SETERROR);
  536. $__OpenFileFH = 0;
  537. }
  538. }
  539. elsif ($sFileAccessType =~ /^a(ppend)?$/i)
  540. {
  541. $__OpenFileFH = new FileHandle;
  542. if ($__OpenFileFH->open(">>".$sFileName))
  543. {
  544. PrintL("$sFileName successfully opened for output (appended)\n", PL_VERBOSE);
  545. }
  546. elsif (IsCritical())
  547. {
  548. PrintL("OpenFile() Error: could not open $sFileName for append\n", PL_BIGERROR | PL_SETERROR);
  549. $__OpenFileFH = 0;
  550. }
  551. else
  552. {
  553. PrintL("OpenFile() Error: could not open $sFileName for append\n", PL_ERROR | PL_SETERROR);
  554. $__OpenFileFH = 0;
  555. }
  556. }
  557. elsif ($sFileAccessType =~ /^f(ull)?$/i)
  558. {
  559. $__OpenFileFH = new FileHandle;
  560. if ($__OpenFileFH->open("+>".$sFileName))
  561. {
  562. PrintL("$sFileName successfully opened for input and output\n", PL_VERBOSE);
  563. }
  564. elsif (IsCritical())
  565. {
  566. PrintL("OpenFile() Error: could not open $sFileName for input and output\n", PL_BIGERROR | PL_SETERROR);
  567. $__OpenFileFH = 0;
  568. }
  569. else
  570. {
  571. PrintL("OpenFile() Error: could not open $sFileName for input and output\n", PL_ERROR | PL_SETERROR);
  572. $__OpenFileFH = 0;
  573. }
  574. }
  575. else
  576. {
  577. $__OpenFileFH = 0;
  578. }
  579. return($__OpenFileFH);
  580. }
  581. ####################################################################################
  582. # CloseFile()
  583. # wrapper for $filehandle->close (just closes the file)
  584. # return 1 for success, 0 for failure
  585. # a-jbilas, 03/10/99 - created
  586. ####################################################################################
  587. sub CloseFile
  588. {
  589. local($fh) = @_;
  590. $rc = 0;
  591. if ($fh)
  592. {
  593. if($fh->close)
  594. {
  595. PrintL($fh." successully closed\n", PL_VERBOSE);
  596. $rc = 1;
  597. }
  598. else
  599. {
  600. PrintL("CloseFile() Error: could not close filehandle\n", PL_BIGWARNING | PL_SETERROR);
  601. PrintMsgBlock($!);
  602. $rc = 0;
  603. }
  604. }
  605. else
  606. {
  607. PrintL("CloseFile() Error: could not close filehandle\n", PL_BIGWARNING | PL_SETERROR);
  608. PrintMsgBlock($!);
  609. $rc = 0;
  610. }
  611. return($rc);
  612. }
  613. ####################################################################################
  614. # Delnode()
  615. # quietly delete a directory and all subdirectories of passed directory name
  616. # dougp, 03/10/99 - created
  617. # a-jbilas, 06/09/99 - calls rmdir instead of delnode (name doesn't make too much
  618. # sense anymore, oh well)
  619. # a-jbilas, 07/21/99 - calls DelAll() instead of rmdir (note that it no longer
  620. # deletes files in directories containing slm.ini)
  621. ####################################################################################
  622. sub Delnode($)
  623. {
  624. my ($fname) = $_[0];
  625. if (-d $fname)
  626. {
  627. DelAll($fname, 1);
  628. }
  629. }
  630. ####################################################################################
  631. # Append()
  632. # append file1 with file2
  633. # a-jbilas, 03/20/99 - created
  634. # a-jbilas, 06/29/99 - echo event to log
  635. ####################################################################################
  636. sub Append($$)
  637. {
  638. local($file1, $file2) = @_;
  639. PrintL(" - Appending $file1 with $file2\n", PL_BLUE);
  640. if (!-e $file1)
  641. {
  642. PrintL(" - Append Warning: $file1 does not exist, just copying file to be appended\n", PL_WARNING);
  643. return(EchoedCopy($file2, $file1));
  644. }
  645. elsif (!-e $file2)
  646. {
  647. PrintL(" - Append Error: $file2 does not exist ($file1 can not be appended)\n", PL_BIGERROR | PL_SETERROR);
  648. return(0);
  649. }
  650. $f1h = OpenFile($file1, "append");
  651. $f2h = OpenFile($file2, "read");
  652. if (!$f1h)
  653. {
  654. my($oldErr) = $ERROR;
  655. PrintL(" - Append Error: $file1 failed to open ($file1 can not be appended)\n", PL_BIGERROR | PL_SETERROR);
  656. PrintMsgBlock($oldErr);
  657. return(0);
  658. }
  659. if (!$f2h)
  660. {
  661. my($oldErr) = $ERROR;
  662. PrintL(" - Append Error: $file2 failed to open ($file1 can not be appended)\n", PL_BIGERROR | PL_SETERROR);
  663. PrintMsgBlock($oldErr);
  664. return(0);
  665. }
  666. @lFile2Buffer = $f2h->getlines();
  667. foreach $i (@lFile2Buffer) { print($f1h $i); }
  668. close($f1h);
  669. close($f2h);
  670. return(1);
  671. }
  672. ####################################################################################
  673. # GetFiles()
  674. # When passed a directory, it will return a list of all absolute path filenames contained
  675. # within. Returns an empty list upon failure (either to open dir or find subdirs)
  676. # if no dir passed as argument, will assume current directory and do relative path filenames
  677. # adding a non-null second argument will recurse subdirectories (to recurse current
  678. # directory subdirectories, pass either "" (relative paths) or cwd() (absolute paths)
  679. # as first argument). subdirs .. and . are ignored
  680. # a-jbilas, 07/08/99 - created
  681. # a-jbilas, 07/16/99 - added recurse option
  682. ####################################################################################
  683. sub GetFiles
  684. {
  685. my(@lFiles) = ();
  686. my($sRelDir) = (($_[0] eq "") ? "" : $_[0]."\\");
  687. opendir(SRCDIR, (($_[0] eq "") ? cwd() : $_[0]));
  688. foreach $file (readdir(SRCDIR))
  689. {
  690. if (!-d $sRelDir.$file)
  691. {
  692. push(@lFiles, $sRelDir.$file);
  693. }
  694. elsif ((-d $sRelDir.$file) && ($_[1] ne "") && ($file !~ /^\.\.?$/))
  695. {
  696. push(@lFiles, GetFiles($sRelDir.$file, 1));
  697. }
  698. }
  699. closedir(SRCDIR);
  700. if ($bVerbose && (@lFiles == ()) && ($_[1] eq ""))
  701. {
  702. PrintToLogErr("GetFiles() Warning: no files found in ".(($_[0] eq "") ? cwd() : $_[0])."\n");
  703. }
  704. return(@lFiles);
  705. }
  706. ####################################################################################
  707. # GetSubdirs()
  708. # When passed a directory, it will return a list of all absolute path subdirs contained
  709. # within. Returns an empty list upon failure (either to open dir or find subdirs)
  710. # if no dir passed as argument, will assume current directory and do relative paths
  711. # adding a non-null second argument will recurse subdirectories (to recurse current
  712. # directory subdirectories, pass either "" for relative paths or cwd() for absolute paths
  713. # as first argument). subdirs .. and . are ignored
  714. # a-jbilas, 07/08/99 - created
  715. # a-jbilas, 07/16/99 - added recurse option
  716. ####################################################################################
  717. sub GetSubdirs
  718. {
  719. my(@lDirs) = ();
  720. my($sRelDir) = (($_[0] eq "") ? "" : $_[0]."\\");
  721. opendir(SRCDIR, (($_[0] eq "") ? cwd() : $_[0]));
  722. foreach $dir (readdir(SRCDIR))
  723. {
  724. if ((-d $sRelDir.$dir) && ($dir !~ /^\.\.?$/))
  725. {
  726. push(@lDirs, $sRelDir.$dir);
  727. if ($_[1] ne "")
  728. {
  729. push(@lDirs, GetSubdirs($sRelDir.$dir, 1));
  730. }
  731. }
  732. }
  733. closedir(SRCDIR);
  734. if ($bVerbose && (@lDirs == ()) && ($_[1] eq ""))
  735. {
  736. PrintToLogErr("GetSubdirs() Warning: no subdirs found in ".(($_[0] eq "") ? cwd() : $_[0])."\n");
  737. }
  738. return(@lDirs);
  739. }
  740. #### DougP 7/19/99
  741. #### return full path of a program found on the path.
  742. sub FindOnPath($)
  743. {
  744. my ($strProgram) = $_[0];
  745. foreach $dir (split (';', $ENV{"PATH"}))
  746. {
  747. my $strFullPath = $dir."\\".$strProgram;
  748. if (-e $strFullPath)
  749. {
  750. return($strFullPath);
  751. }
  752. }
  753. PrintL("couldn't find path for ".$strProgram."\n", PL_WARNING);
  754. return(0);
  755. }
  756. ####################################################################################
  757. # GlobalReplaceInFile()
  758. # Performs a global string replacement in file specified
  759. # a-jbilas, 07/26/99 - created
  760. ####################################################################################
  761. sub GlobalReplaceInFile($$$)
  762. {
  763. # NOTE: entire file buffered in memory, not for use w/ extremely large files
  764. my($sFileName, $sSrc, $sTgt) = @_;
  765. my($buf) = "";
  766. my($acc) = "";
  767. my($bFound) = 0;
  768. my($fhIn) = OpenFile($sFileName, "read");
  769. if (!$fhIn)
  770. {
  771. return(0);
  772. }
  773. else
  774. {
  775. while (!$fhIn->eof())
  776. {
  777. $buf = $fhIn->getline();
  778. if (!$bFound && ($buf =~ /$sSrc/))
  779. {
  780. $bFound = 1;
  781. }
  782. $buf =~ s/$sSrc/$sTgt/g;
  783. $acc .= $buf;
  784. }
  785. CloseFile($fhIn);
  786. if ($bFound)
  787. {
  788. unlink($fhIn);
  789. my($fhOut) = OpenFile($sFileName, "write");
  790. $fhOut->print($acc);
  791. CloseFile($fhOut);
  792. return(1);
  793. }
  794. }
  795. }
  796. # two routines to track disk space
  797. # return the space left on a directory (in Mb)
  798. # DougP 7/6/99
  799. sub SpaceLeft($)
  800. {
  801. my ($strDir) = $_[0];
  802. open (FPIN, "dir /-C $strDir |");
  803. my $iSpace = -1;
  804. while (<FPIN>)
  805. {
  806. if (/(\d+) bytes free/)
  807. {
  808. $iSpace = $1;
  809. }
  810. }
  811. close (FPIN);
  812. $iSpace /= (1 << 20); # convert to Mb
  813. return int $iSpace;
  814. }
  815. # return an html message if disk space available is below the set limit (in Mb)
  816. # warning if below 5 times set limit
  817. # DougP 7/6/99
  818. sub SpaceLeftAlarm($$)
  819. {
  820. my ($strDir, $iAlarmLevel) = @_;
  821. my $iSpaceLeft = SpaceLeft $strDir;
  822. print "Space left on $strDir is ${iSpaceLeft}M\n";
  823. if ($iSpaceLeft < $iAlarmLevel)
  824. {
  825. return "<strong><font color=red>Space left on $strDir is ${iSpaceLeft}M</font></strong><br>\n";
  826. }
  827. if ($iSpaceLeft < 5*$iAlarmLevel)
  828. {
  829. return "<font color=orange>Space left on $strDir is ${iSpaceLeft}M</font><br>\n";
  830. }
  831. return "";
  832. }
  833. sub GetDLLVersion($)
  834. {
  835. local($_Execute) = 1;
  836. my($version) = "";
  837. if (Execute($cmdShowVer." $_[0]"))
  838. {
  839. $_Execute =~ s/.*Version: \"([^\"]*)\".*\n.*/$1/;
  840. $version = $_Execute;
  841. }
  842. undef $_Execute;
  843. return($version);
  844. }
  845. sub IsDLLVersionHigher($$)
  846. {
  847. my($rc) = 0;
  848. local(@file1ver) = split(/\./, GetDLLVersion($_[0]));
  849. local(@file2ver) = split(/\./, GetDLLVersion($_[1]));
  850. if (@file1ver != 4)
  851. {
  852. PrintL("WARNING: ".$_[0]." DLL does not contain version info, cannot get latest DLL\n",
  853. PL_BIGWARNING | PL_SETERROR);
  854. }
  855. elsif (@file2ver != 4)
  856. {
  857. PrintL("WARNING: ".$_[1]." DLL does not contain version info, cannot get latest DLL\n",
  858. PL_BIGWARNING | PL_SETERROR);
  859. }
  860. else
  861. {
  862. my($latestFound) = 0;
  863. for ($index = 0 ; !$latestFound && ($index < 4) ; ++$index)
  864. {
  865. if ($file1ver[$index] > $file2ver[$index])
  866. {
  867. ++$latestFound;
  868. $rc = 1;
  869. }
  870. elsif ($file1ver[$index] < $file2ver[$index])
  871. {
  872. ++$latestFound;
  873. }
  874. }
  875. }
  876. return($rc);
  877. }
  878. sub GetLatestDLL($$)
  879. {
  880. if (IsDLLVersionHigher($_[1], $_[0]))
  881. {
  882. return($_[1]);
  883. }
  884. elsif (IsDLLVersionHigher($_[0], $_[1]))
  885. {
  886. return($_[0]);
  887. }
  888. else
  889. {
  890. return("");
  891. }
  892. }
  893. sub IsDirectory($)
  894. {
  895. local($rc) = 0;
  896. if (Win32::File::GetAttributes($_[0], $rc))
  897. {
  898. return($rc & DIRECTORY);
  899. }
  900. else
  901. {
  902. return(0);
  903. }
  904. }
  905. sub IsReadOnly($)
  906. {
  907. local($rc) = 0;
  908. if (Win32::File::GetAttributes($_[0], $rc))
  909. {
  910. return($rc & READONLY);
  911. }
  912. else
  913. {
  914. return(0);
  915. }
  916. }
  917. sub SetReadOnly($$)
  918. {
  919. local($attr) = 0;
  920. if (Win32::File::GetAttributes($_[0], $attr))
  921. {
  922. if ($_[1] && !($attr & READONLY))
  923. {
  924. PrintL(" - Adding read only flag to ".$_[0]."\n", PL_BLUE);
  925. $attr = $attr | READONLY;
  926. return(Win32::File::SetAttributes($_[0], $attr));
  927. }
  928. elsif (!$_[1] && ($attr & READONLY))
  929. {
  930. PrintL(" - Removing read only flag from ".$_[0]."\n", PL_BLUE);
  931. $attr = $attr - READONLY;
  932. return(Win32::File::SetAttributes($_[0], $attr));
  933. }
  934. else
  935. {
  936. return(1);
  937. }
  938. }
  939. else
  940. {
  941. return(0);
  942. }
  943. }
  944. sub PrintToFile
  945. {
  946. my($fileName) = $_[0];
  947. shift(@_);
  948. my($rc) = 1;
  949. my($fhOut) = OpenFile($fileName, "append");
  950. if ($fhOut)
  951. {
  952. foreach $elem (@_)
  953. {
  954. $fhOut->print($elem);
  955. }
  956. CloseFile($fhOut);
  957. }
  958. else
  959. {
  960. $rc = 0;
  961. }
  962. return($rc);
  963. }
  964. sub GetAllTextFromFile($)
  965. {
  966. my($fileName) = $_[0];
  967. my($data) = "";
  968. my($fhIn) = OpenFile($fileName, "read");
  969. if ($fhIn)
  970. {
  971. while (!$fhIn->eof())
  972. {
  973. $data .= $fhIn->getline();
  974. }
  975. CloseFile($fhIn);
  976. }
  977. return($data);
  978. }
  979. # given a remote UNC filename, will return the network server name
  980. # (if given a local filename, will return the local computer name)
  981. sub GetServerName($)
  982. {
  983. my($file) = $_[0];
  984. if ($file !~ /^\\\\/)
  985. {
  986. return(uc($COMPUTERNAME));
  987. }
  988. $file =~ s/^\\\\([^\\]+).*/$1/;
  989. return(uc($file));
  990. }
  991. sub KillOpenFiles
  992. {
  993. my($sServer) = $_[0];
  994. shift(@_);
  995. local(@lFiles) = @_;
  996. local($_Execute) = 1;
  997. Execute($cmdKillOpen." \\\\".$sServer);
  998. my(@lOpenFiles) = ();
  999. my($bFilesReached) = 0;
  1000. foreach $line (split("\n", $_Execute))
  1001. {
  1002. if ($bFilesReached)
  1003. {
  1004. push(@lOpenFiles, join(" ", split(/ +/, $line)));
  1005. }
  1006. elsif ($line =~ /ID User Name File Name/)
  1007. {
  1008. $bFilesReached = 1;
  1009. }
  1010. }
  1011. undef($_Execute);
  1012. foreach $openfile (@lOpenFiles)
  1013. {
  1014. my($id, $user, $file) = split(" ", $openfile);
  1015. print($id." : ".$user." : ".$file."\n");
  1016. }
  1017. }
  1018. $__IITFILEPM = 1;
  1019. 1;