PageRenderTime 87ms CodeModel.GetById 34ms RepoModel.GetById 5ms app.codeStats 0ms

/pts-core/objects/pts_file_io.php

https://github.com/atlassianhlechner/phoronix-test-suite
PHP | 139 lines | 102 code | 12 blank | 25 comment | 27 complexity | ddcbc7f0ef860452d1b9724259c30786 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Phoronix Test Suite
  4. URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
  5. Copyright (C) 2008 - 2013, Phoronix Media
  6. Copyright (C) 2008 - 2013, Michael Larabel
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class pts_file_io
  19. {
  20. public static function mkdir($dir, $mode = 0777, $recursive = true)
  21. {
  22. // Compared to the normal PHP mkdir, don't emit a warning/notice when the directory already exists
  23. return !is_dir($dir) && mkdir($dir, $mode, $recursive);
  24. }
  25. public static function symlink($target, $link)
  26. {
  27. // Compared to the normal PHP symlink, don't emit a warning when the symlink already exists
  28. return is_file($target) && !is_file($link) ? symlink($target, $link) : false;
  29. }
  30. public static function unlink($file)
  31. {
  32. // Compared to the normal PHP mkdir, don't emit a warning/notice when the file doesn't exist
  33. return is_file($file) && unlink($file);
  34. }
  35. public static function glob($pattern, $flags = 0)
  36. {
  37. // Compared to the normal PHP glob, don't return false when no files are there, but return an empty array
  38. $r = glob($pattern, $flags);
  39. return is_array($r) ? $r : array();
  40. }
  41. public static function file_get_contents($filename, $flags = 0, $context = null)
  42. {
  43. // Compared to the normal PHP file_get_contents, trim the file as a string when acquired
  44. return trim(file_get_contents($filename, $flags, $context));
  45. }
  46. public static function delete($object, $ignore_files = null, $remove_root_directory = false)
  47. {
  48. // Delete files and/or directories
  49. if($object == false)
  50. {
  51. return false;
  52. }
  53. if(is_dir($object))
  54. {
  55. $object = pts_strings::add_trailing_slash($object);
  56. }
  57. foreach(pts_file_io::glob($object . '*') as $to_remove)
  58. {
  59. if(is_file($to_remove) || is_link($to_remove))
  60. {
  61. if(is_array($ignore_files) && in_array(basename($to_remove), $ignore_files))
  62. {
  63. continue; // Don't remove the file
  64. }
  65. else
  66. {
  67. unlink($to_remove);
  68. }
  69. }
  70. else if(is_dir($to_remove))
  71. {
  72. self::delete($to_remove, $ignore_files, true);
  73. }
  74. }
  75. if($remove_root_directory && is_dir($object) && count(pts_file_io::glob($object . '/*')) == 0)
  76. {
  77. rmdir($object);
  78. }
  79. }
  80. public static function array_filesize($r)
  81. {
  82. $filesize = 0;
  83. foreach($r as $file)
  84. {
  85. if(is_file($file))
  86. {
  87. $filesize += filesize($file);
  88. }
  89. }
  90. return $filesize;
  91. }
  92. public static function copy($source, $dest)
  93. {
  94. $success = false;
  95. if(is_file($source))
  96. {
  97. $success = copy($source, $dest);
  98. }
  99. else if(is_link($source))
  100. {
  101. $success = copy(readlink($source), $dest);
  102. }
  103. else if(is_dir($source))
  104. {
  105. if(!is_dir($dest))
  106. {
  107. mkdir($dest);
  108. }
  109. $dir = dir($source);
  110. while(($entry = $dir->read()) !== false)
  111. {
  112. if($entry == '.' || $entry == '..')
  113. {
  114. continue;
  115. }
  116. self::copy($source . '/' . $entry, $dest . '/' . $entry);
  117. }
  118. $dir->close();
  119. $success = true;
  120. }
  121. return $success;
  122. }
  123. }
  124. ?>