PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/phoronix-test-suite/pts-core/objects/pts_file_io.php

#
PHP | 125 lines | 90 code | 10 blank | 25 comment | 25 complexity | ea52e57ff038ae6d8ce667f2ff5ca950 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 - 2011, Phoronix Media
  6. Copyright (C) 2008 - 2011, 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 = false)
  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))
  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. function copy($source, $dest)
  81. {
  82. $success = false;
  83. if(is_file($source))
  84. {
  85. $success = copy($source, $dest);
  86. }
  87. else if(is_link($source))
  88. {
  89. $success = copy(readlink($source), $dest);
  90. }
  91. else if(is_dir($source))
  92. {
  93. if(!is_dir($dest))
  94. {
  95. mkdir($dest);
  96. }
  97. $dir = dir($source);
  98. while(($entry = $dir->read()) !== false)
  99. {
  100. if($entry == '.' || $entry == '..')
  101. {
  102. continue;
  103. }
  104. self::copy($source . '/' . $entry, $dest . '/' . $entry);
  105. }
  106. $dir->close();
  107. $success = true;
  108. }
  109. return $success;
  110. }
  111. }
  112. ?>