PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/baseProject/src/db/phing-2.4.13/system/io/Win32FileSystem.php

https://github.com/hikirsch/JenkinsPython
PHP | 477 lines | 430 code | 16 blank | 31 comment | 8 complexity | 63f9e49b69db01918993f8f46ac8cf07 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: fdd5ec4d3e9954009c7b6076408f03dad4975096 $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/system/io/FileSystem.php';
  22. /**
  23. * @package phing.system.io
  24. */
  25. class Win32FileSystem extends FileSystem {
  26. protected $slash;
  27. protected $altSlash;
  28. protected $semicolon;
  29. private static $driveDirCache = array();
  30. function __construct() {
  31. $this->slash = self::getSeparator();
  32. $this->semicolon = self::getPathSeparator();
  33. $this->altSlash = ($this->slash === '\\') ? '/' : '\\';
  34. }
  35. function isSlash($c) {
  36. return ($c == '\\') || ($c == '/');
  37. }
  38. function isLetter($c) {
  39. return ((ord($c) >= ord('a')) && (ord($c) <= ord('z')))
  40. || ((ord($c) >= ord('A')) && (ord($c) <= ord('Z')));
  41. }
  42. function slashify($p) {
  43. if ((strlen($p) > 0) && ($p{0} != $this->slash)) {
  44. return $this->slash.$p;
  45. }
  46. else {
  47. return $p;
  48. }
  49. }
  50. /* -- Normalization and construction -- */
  51. function getSeparator() {
  52. // the ascii value of is the \
  53. return chr(92);
  54. }
  55. function getPathSeparator() {
  56. return ';';
  57. }
  58. /**
  59. * A normal Win32 pathname contains no duplicate slashes, except possibly
  60. * for a UNC prefix, and does not end with a slash. It may be the empty
  61. * string. Normalized Win32 pathnames have the convenient property that
  62. * the length of the prefix almost uniquely identifies the type of the path
  63. * and whether it is absolute or relative:
  64. *
  65. * 0 relative to both drive and directory
  66. * 1 drive-relative (begins with '\\')
  67. * 2 absolute UNC (if first char is '\\'), else directory-relative (has form "z:foo")
  68. * 3 absolute local pathname (begins with "z:\\")
  69. */
  70. function normalizePrefix($strPath, $len, &$sb) {
  71. $src = 0;
  72. while (($src < $len) && $this->isSlash($strPath{$src})) {
  73. $src++;
  74. }
  75. $c = "";
  76. if (($len - $src >= 2)
  77. && $this->isLetter($c = $strPath{$src})
  78. && $strPath{$src + 1} === ':') {
  79. /* Remove leading slashes if followed by drive specifier.
  80. * This hack is necessary to support file URLs containing drive
  81. * specifiers (e.g., "file://c:/path"). As a side effect,
  82. * "/c:/path" can be used as an alternative to "c:/path". */
  83. $sb .= $c;
  84. $sb .= ':';
  85. $src += 2;
  86. }
  87. else {
  88. $src = 0;
  89. if (($len >= 2)
  90. && $this->isSlash($strPath{0})
  91. && $this->isSlash($strPath{1})) {
  92. /* UNC pathname: Retain first slash; leave src pointed at
  93. * second slash so that further slashes will be collapsed
  94. * into the second slash. The result will be a pathname
  95. * beginning with "\\\\" followed (most likely) by a host
  96. * name. */
  97. $src = 1;
  98. $sb.=$this->slash;
  99. }
  100. }
  101. return $src;
  102. }
  103. /** Normalize the given pathname, whose length is len, starting at the given
  104. offset; everything before this offset is already normal. */
  105. protected function normalizer($strPath, $len, $offset) {
  106. if ($len == 0) {
  107. return $strPath;
  108. }
  109. if ($offset < 3) {
  110. $offset = 0; //Avoid fencepost cases with UNC pathnames
  111. }
  112. $src = 0;
  113. $slash = $this->slash;
  114. $sb = "";
  115. if ($offset == 0) {
  116. // Complete normalization, including prefix
  117. $src = $this->normalizePrefix($strPath, $len, $sb);
  118. } else {
  119. // Partial normalization
  120. $src = $offset;
  121. $sb .= substr($strPath, 0, $offset);
  122. }
  123. // Remove redundant slashes from the remainder of the path, forcing all
  124. // slashes into the preferred slash
  125. while ($src < $len) {
  126. $c = $strPath{$src++};
  127. if ($this->isSlash($c)) {
  128. while (($src < $len) && $this->isSlash($strPath{$src})) {
  129. $src++;
  130. }
  131. if ($src === $len) {
  132. /* Check for trailing separator */
  133. $sn = (int) strlen($sb);
  134. if (($sn == 2) && ($sb{1} === ':')) {
  135. // "z:\\"
  136. $sb .= $slash;
  137. break;
  138. }
  139. if ($sn === 0) {
  140. // "\\"
  141. $sb .= $slash;
  142. break;
  143. }
  144. if (($sn === 1) && ($this->isSlash($sb{0}))) {
  145. /* "\\\\" is not collapsed to "\\" because "\\\\" marks
  146. the beginning of a UNC pathname. Even though it is
  147. not, by itself, a valid UNC pathname, we leave it as
  148. is in order to be consistent with the win32 APIs,
  149. which treat this case as an invalid UNC pathname
  150. rather than as an alias for the root directory of
  151. the current drive. */
  152. $sb .= $slash;
  153. break;
  154. }
  155. // Path does not denote a root directory, so do not append
  156. // trailing slash
  157. break;
  158. } else {
  159. $sb .= $slash;
  160. }
  161. } else {
  162. $sb.=$c;
  163. }
  164. }
  165. $rv = (string) $sb;
  166. return $rv;
  167. }
  168. /**
  169. * Check that the given pathname is normal. If not, invoke the real
  170. * normalizer on the part of the pathname that requires normalization.
  171. * This way we iterate through the whole pathname string only once.
  172. * @param string $strPath
  173. * @return string
  174. */
  175. function normalize($strPath) {
  176. $n = strlen($strPath);
  177. $slash = $this->slash;
  178. $altSlash = $this->altSlash;
  179. $prev = 0;
  180. for ($i = 0; $i < $n; $i++) {
  181. $c = $strPath{$i};
  182. if ($c === $altSlash) {
  183. return $this->normalizer($strPath, $n, ($prev === $slash) ? $i - 1 : $i);
  184. }
  185. if (($c === $slash) && ($prev === $slash) && ($i > 1)) {
  186. return $this->normalizer($strPath, $n, $i - 1);
  187. }
  188. if (($c === ':') && ($i > 1)) {
  189. return $this->normalizer($strPath, $n, 0);
  190. }
  191. $prev = $c;
  192. }
  193. if ($prev === $slash) {
  194. return $this->normalizer($strPath, $n, $n - 1);
  195. }
  196. return $strPath;
  197. }
  198. function prefixLength($strPath) {
  199. $path = (string) $strPath;
  200. $slash = (string) $this->slash;
  201. $n = (int) strlen($path);
  202. if ($n === 0) {
  203. return 0;
  204. }
  205. $c0 = $path{0};
  206. $c1 = ($n > 1) ? $path{1} :
  207. 0;
  208. if ($c0 === $slash) {
  209. if ($c1 === $slash) {
  210. return 2; // absolute UNC pathname "\\\\foo"
  211. }
  212. return 1; // drive-relative "\\foo"
  213. }
  214. if ($this->isLetter($c0) && ($c1 === ':')) {
  215. if (($n > 2) && ($path{2}) === $slash) {
  216. return 3; // Absolute local pathname "z:\\foo" */
  217. }
  218. return 2; // Directory-relative "z:foo"
  219. }
  220. return 0; // Completely relative
  221. }
  222. function resolve($parent, $child) {
  223. $parent = (string) $parent;
  224. $child = (string) $child;
  225. $slash = (string) $this->slash;
  226. $pn = (int) strlen($parent);
  227. if ($pn === 0) {
  228. return $child;
  229. }
  230. $cn = (int) strlen($child);
  231. if ($cn === 0) {
  232. return $parent;
  233. }
  234. $c = $child;
  235. if (($cn > 1) && ($c{0} === $slash)) {
  236. if ($c{1} === $slash) {
  237. // drop prefix when child is a UNC pathname
  238. $c = substr($c, 2);
  239. }
  240. else {
  241. //Drop prefix when child is drive-relative */
  242. $c = substr($c, 1);
  243. }
  244. }
  245. $p = $parent;
  246. if ($p{$pn - 1} === $slash) {
  247. $p = substr($p, 0, $pn - 1);
  248. }
  249. return $p.$this->slashify($c);
  250. }
  251. function getDefaultParent() {
  252. return (string) ("".$this->slash);
  253. }
  254. function fromURIPath($strPath) {
  255. $p = (string) $strPath;
  256. if ((strlen($p) > 2) && ($p{2} === ':')) {
  257. // "/c:/foo" --> "c:/foo"
  258. $p = substr($p,1);
  259. // "c:/foo/" --> "c:/foo", but "c:/" --> "c:/"
  260. if ((strlen($p) > 3) && StringHelper::endsWith('/', $p)) {
  261. $p = substr($p, 0, strlen($p) - 1);
  262. }
  263. } elseif ((strlen($p) > 1) && StringHelper::endsWith('/', $p)) {
  264. // "/foo/" --> "/foo"
  265. $p = substr($p, 0, strlen($p) - 1);
  266. }
  267. return (string) $p;
  268. }
  269. /* -- Path operations -- */
  270. function isAbsolute(PhingFile $f) {
  271. $pl = (int) $f->getPrefixLength();
  272. $p = (string) $f->getPath();
  273. return ((($pl === 2) && ($p{0} === $this->slash)) || ($pl === 3) || ($pl === 1 && $p{0} === $this->slash));
  274. }
  275. /** private */
  276. function _driveIndex($d) {
  277. $d = (string) $d{0};
  278. if ((ord($d) >= ord('a')) && (ord($d) <= ord('z'))) {
  279. return ord($d) - ord('a');
  280. }
  281. if ((ord($d) >= ord('A')) && (ord($d) <= ord('Z'))) {
  282. return ord($d) - ord('A');
  283. }
  284. return -1;
  285. }
  286. /** private */
  287. function _getDriveDirectory($drive) {
  288. $drive = (string) $drive{0};
  289. $i = (int) $this->_driveIndex($drive);
  290. if ($i < 0) {
  291. return null;
  292. }
  293. $s = (isset(self::$driveDirCache[$i]) ? self::$driveDirCache[$i] : null);
  294. if ($s !== null) {
  295. return $s;
  296. }
  297. $s = $this->_getDriveDirectory($i + 1);
  298. self::$driveDirCache[$i] = $s;
  299. return $s;
  300. }
  301. function _getUserPath() {
  302. //For both compatibility and security, we must look this up every time
  303. return (string) $this->normalize(Phing::getProperty("user.dir"));
  304. }
  305. function _getDrive($path) {
  306. $path = (string) $path;
  307. $pl = $this->prefixLength($path);
  308. return ($pl === 3) ? substr($path, 0, 2) : null;
  309. }
  310. function resolveFile(PhingFile $f) {
  311. $path = $f->getPath();
  312. $pl = (int) $f->getPrefixLength();
  313. if (($pl === 2) && ($path{0} === $this->slash)) {
  314. return $path; // UNC
  315. }
  316. if ($pl === 3) {
  317. return $path; // Absolute local
  318. }
  319. if ($pl === 0) {
  320. return (string) ($this->_getUserPath().$this->slashify($path)); //Completely relative
  321. }
  322. if ($pl === 1) { // Drive-relative
  323. $up = (string) $this->_getUserPath();
  324. $ud = (string) $this->_getDrive($up);
  325. if ($ud !== null) {
  326. return (string) $ud.$path;
  327. }
  328. return (string) $up.$path; //User dir is a UNC path
  329. }
  330. if ($pl === 2) { // Directory-relative
  331. $up = (string) $this->_getUserPath();
  332. $ud = (string) $this->_getDrive($up);
  333. if (($ud !== null) && StringHelper::startsWith($ud, $path)) {
  334. return (string) ($up . $this->slashify(substr($path,2)));
  335. }
  336. $drive = (string) $path{0};
  337. $dir = (string) $this->_getDriveDirectory($drive);
  338. $np = (string) "";
  339. if ($dir !== null) {
  340. /* When resolving a directory-relative path that refers to a
  341. drive other than the current drive, insist that the caller
  342. have read permission on the result */
  343. $p = (string) $drive . (':'.$dir.$this->slashify(substr($path,2)));
  344. if (!$this->checkAccess($p, false)) {
  345. // FIXME
  346. // throw security error
  347. die("Can't resolve path $p");
  348. }
  349. return $p;
  350. }
  351. return (string) $drive.':'.$this->slashify(substr($path,2)); //fake it
  352. }
  353. throw new InvalidArgumentException("Unresolvable path: " . $path);
  354. }
  355. /* -- most of the following is mapped to the functions mapped th php natives in FileSystem */
  356. /* -- Attribute accessors -- */
  357. function setReadOnly($f) {
  358. // dunno how to do this on win
  359. throw new Exception("WIN32FileSystem doesn't support read-only yet.");
  360. }
  361. /* -- Filesystem interface -- */
  362. protected function _access($path) {
  363. if (!$this->checkAccess($path, false)) {
  364. throw new Exception("Can't resolve path $p");
  365. }
  366. return true;
  367. }
  368. function _nativeListRoots() {
  369. // FIXME
  370. }
  371. function listRoots() {
  372. $ds = _nativeListRoots();
  373. $n = 0;
  374. for ($i = 0; $i < 26; $i++) {
  375. if ((($ds >> $i) & 1) !== 0) {
  376. if (!$this->access((string)( chr(ord('A') + $i) . ':' . $this->slash))) {
  377. $ds &= ~(1 << $i);
  378. } else {
  379. $n++;
  380. }
  381. }
  382. }
  383. $fs = array();
  384. $j = (int) 0;
  385. $slash = (string) $this->slash;
  386. for ($i = 0; $i < 26; $i++) {
  387. if ((($ds >> $i) & 1) !== 0) {
  388. $fs[$j++] = new PhingFile(chr(ord('A') + $i) . ':' . $this->slash);
  389. }
  390. }
  391. return $fs;
  392. }
  393. /* -- Basic infrastructure -- */
  394. /** compares file paths lexicographically */
  395. function compare(PhingFile $f1, PhingFile $f2) {
  396. $f1Path = $f1->getPath();
  397. $f2Path = $f2->getPath();
  398. return (boolean) strcasecmp((string) $f1Path, (string) $f2Path);
  399. }
  400. /**
  401. * returns the contents of a directory in an array
  402. */
  403. function lister($f) {
  404. $dir = @opendir($f->getAbsolutePath());
  405. if (!$dir) {
  406. throw new Exception("Can't open directory " . $f->__toString());
  407. }
  408. $vv = array();
  409. while (($file = @readdir($dir)) !== false) {
  410. if ($file == "." || $file == "..") {
  411. continue;
  412. }
  413. $vv[] = (string) $file;
  414. }
  415. @closedir($dir);
  416. return $vv;
  417. }
  418. }