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