file : add folding

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5425 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2019-08-20 10:13:23 +00:00
parent f3386a4dfc
commit 98116d9550

View File

@@ -30,6 +30,7 @@ class file
* @throws If directory not exists, or the directory is not executable
*/
public function chdir ($directory)
// {{{
{
$this->debug (2, "chdir ($directory)");
$tmpdirectory = $this->realpath ($directory);
@@ -41,6 +42,7 @@ class file
$this->debug (1, "chdir $directory -> $this->cwd");
return true;
}
// }}}
/** Change the group for a file/dir...
* @param string $filename The file/directory to change
@@ -48,6 +50,7 @@ class file
* @throws If filename not exists, or the directory is not RW
*/
public function chgrp ($filename, $group)
// {{{
{
$this->debug (2, "chgrp ($filename, $group)");
$filename = $this->realpath ($filename);
@@ -64,6 +67,7 @@ class file
$this->debug (1, "chgrp ($filename, $group) => $rc");
return $rc;
}
// }}}
/** Change the rights mode for a file/dir...
* @param string $filename The file/directory to change
@@ -71,6 +75,7 @@ class file
* @throws If filename not exists, or the directory is not RW
*/
public function chmod ($filename, $mode)
// {{{
{
$this->debug (2, "chmod ($filename, $mode)");
$filename = $this->realpath ($filename);
@@ -87,6 +92,7 @@ class file
$this->debug (1, "chmod ($filename, $mode) => $rc");
return $rc;
}
// }}}
/** Change the owner for a file/dir...
* @param string $filename The file/directory to change
@@ -94,6 +100,7 @@ class file
* @throws If filename not exists, or the directory is not RW
*/
public function chown ($filename, $user)
// {{{
{
$this->debug (2, "chown ($filename, $user)");
if (posix_getuid () !== 0 &&
@@ -116,6 +123,7 @@ class file
$this->debug (1, "chown ($filename, $user) => $rc");
return $rc;
}
// }}}
/** Chroot in the provided directory
* @param string $directory The directory to chroot
@@ -123,6 +131,7 @@ class file
* @throws If directory not exists, or the directory is not executable
*/
public function chroot ($directory)
// {{{
{
// Use the checkPathRO (using the $this->baseDir) to not allow to go away of
// the chroot.
@@ -134,6 +143,7 @@ class file
$this->debug (1, "chroot $directory -> $this->baseDir");
return true;
}
// }}}
/** Checks whether a file or directory exists
* @param string $filename The file or directory to verify
@@ -141,6 +151,7 @@ class file
* @throws If parent directory not exists, or is not executable
*/
public function file_exists ($filename)
// {{{
{
$this->debug (2, "file_exists ($filename)");
$filename = $this->realpath ($filename);
@@ -157,6 +168,7 @@ class file
return true;
return false;
}
// }}}
/** Get the file contents
* @param string $filename Name of the file to read
@@ -165,6 +177,7 @@ class file
* exists or is not readable
*/
public function file_get_contents ($filename)
// {{{
{
$this->debug (2, "file_get_contents ($filename)");
$filename = $this->realpath ($filename);
@@ -182,6 +195,7 @@ class file
" bytes");
return $contents;
}
// }}}
/** Write a string to a file
* @param string $filename Path to the file where to write the data
@@ -192,6 +206,7 @@ class file
* exists and is not writeable
*/
public function file_put_contents ($filename, $data, $flags=0)
// {{{
{
$this->debug (2, "file_put_contents ($filename)");
$filename = $this->realpath ($filename);
@@ -210,6 +225,7 @@ class file
return $contents;
}
// }}}
/** Get the file modification time of a file
* @param string $filename Path to the file
@@ -219,12 +235,14 @@ class file
* @throws If parent directory not exists, is not writeable
*/
public function filemtime ($filename)
// {{{
{
$this->debug (2, "filemtime ($filename)");
$filename = $this->realpath ($filename);
$this->checkPathRO (dirname ($filename));
return filemtime ($filename);
}
// }}}
/** Get the file size
* @param string $filename Path to the file
@@ -232,12 +250,14 @@ class file
* @throws If parent directory not exists, is not writeable
*/
public function filesize ($filename)
// {{{
{
$this->debug (2, "filesize ($filename)");
$filename = $this->realpath ($filename);
$this->checkPathRO (dirname ($filename));
return filesize ($filename);
}
// }}}
/** Get the file info of the provided filename
* @param string $filename Path to the file
@@ -245,6 +265,7 @@ class file
* @throws If parent directory not exists, is not writeable
*/
public function fileinfoMimeType ($filename)
// {{{
{
$this->debug (2, "filesize ($filename)");
$filename = $this->realpath ($filename);
@@ -252,14 +273,17 @@ class file
$finfo = new \finfo (FILEINFO_MIME_TYPE);
return $finfo->file ($filename);
}
// }}}
/** Return the current working directory
* @return string the current working directory */
public function getcwd ()
// {{{
{
$this->debug (1, "getcwd $this->cwd");
return $this->cwd;
}
// }}}
/** Find pathnames matching a pattern
* If there is some unreadable files, skip them quietly
@@ -270,6 +294,7 @@ class file
* or if there is one file unreadable
*/
public function glob ($pattern, $flags=0)
// {{{
{
$this->debug (2, "glob ($pattern, $flags)");
$this->checkPathRO ($this->baseDir);
@@ -299,6 +324,7 @@ class file
}
return $files;
}
// }}}
/** Tells whether the given filename is a directory
* @param string $filename The filename to test
@@ -307,6 +333,7 @@ class file
* @throws If parent directory not exists, or is not executable
*/
public function is_dir ($filename)
// {{{
{
$this->debug (2, "is_dir ($filename)");
$filename = $this->realpath ($filename);
@@ -315,6 +342,7 @@ class file
return true;
return false;
}
// }}}
/** Tells whether the given filename is a valid file
* @param string $filename The filename to test
@@ -322,6 +350,7 @@ class file
* @throws If parent directory not exists, or is not executable
*/
public function is_file ($filename)
// {{{
{
$this->debug (2, "is_file ($filename)");
$filename = $this->realpath ($filename);
@@ -330,6 +359,7 @@ class file
return true;
return false;
}
// }}}
/** Tells whether a file exists and is executable
* @param string $filename The filename to test
@@ -337,6 +367,7 @@ class file
* @throws If parent directory not exists, or is not executable
*/
public function is_executable ($filename)
// {{{
{
$this->debug (2, "is_executable ($filename)");
$filename = $this->realpath ($filename);
@@ -345,6 +376,7 @@ class file
return true;
return false;
}
// }}}
/** Tells whether a file exists and is readable
* @param string $filename The filename to test
@@ -352,6 +384,7 @@ class file
* @throws If parent directory not exists, or is not executable
*/
public function is_readable ($filename)
// {{{
{
$this->debug (2, "is_readable ($filename)");
$filename = $this->realpath ($filename);
@@ -360,6 +393,7 @@ class file
return true;
return false;
}
// }}}
/** Tells whether a file exists and is writeable
* @param string $filename The filename to test
@@ -367,6 +401,7 @@ class file
* @throws If parent directory not exists, or is not executable
*/
public function is_writeable ($filename)
// {{{
{
$this->debug (2, "is_writeable ($filename)");
$filename = $this->realpath ($filename);
@@ -375,6 +410,7 @@ class file
return true;
return false;
}
// }}}
/** Lock a file exclusively
* @param string $filename The file to lock
@@ -382,6 +418,7 @@ class file
* @throws If parent directory not exists, or is not writeable
*/
public function lockEX ($filename)
// {{{
{
$this->debug (2, "lockEX ($filename)");
$filename = $this->realpath ($filename);
@@ -397,6 +434,7 @@ class file
$this->locks[$filename] = fopen ($filename, "rt");
return flock ($this->locks[$filename], LOCK_EX);
}
// }}}
/** Lock a file shared (allow multiple read)
* @param string $filename The file to lock
@@ -404,6 +442,7 @@ class file
* @throws If parent directory not exists, or is not writeable
*/
public function lockSH ($filename)
// {{{
{
$this->debug (2, "lockSH ($filename)");
$filename = $this->realpath ($filename);
@@ -419,6 +458,7 @@ class file
$this->locks[$filename] = fopen ($filename, "rt");
return flock ($this->locks[$filename], LOCK_SH);
}
// }}}
/** Unlock a file previously locked
* @param string $filename The file to lock
@@ -426,6 +466,7 @@ class file
* @throws If parent directory not exists, or is not writeable
*/
public function lockUN ($filename)
// {{{
{
$this->debug (2, "lockUN ($filename)");
$filename = $this->realpath ($filename);
@@ -438,6 +479,7 @@ class file
}
return $res;
}
// }}}
/** Calculate the md5 sum of a file
* @param string $filename The file to hash
@@ -445,6 +487,7 @@ class file
* @throws If the file doesn't exists
*/
public function md5_file ($filename)
// {{{
{
$this->debug (2, "md5_file ($filename)");
$filename = $this->realpath ($filename);
@@ -459,6 +502,7 @@ class file
$filename), 500);
return md5_file ($filename);
}
// }}}
/** Create a new directory
* @param string $pathname The directory to create
@@ -469,6 +513,7 @@ class file
* @throws If parent directory not exists, is not writeable
*/
public function mkdir ($pathname, $mode = 0777, $recursive = false)
// {{{
{
$this->debug (2, "mkdir ($pathname, $mode, $recursive)");
$pathname = $this->realpath ($pathname);
@@ -506,6 +551,7 @@ class file
$this->debug (1, "mkdir ($pathname, $mode, $recursive) => $rc");
return $rc;
}
// }}}
/** Copy a file or a directory
* @param string $oldname The file to copy
@@ -514,6 +560,7 @@ class file
* @return bool
*/
public function copy ($oldname, $newname)
// {{{
{
$this->debug (2, "copy ($oldname, $newname)");
$oldname = $this->realpath ($oldname);
@@ -536,6 +583,7 @@ class file
$this->debug (1, "copy ($oldname, $newname) => $rc");
return $rc;
}
// }}}
/** Renames a file or directory
* @param string $oldname The file or directory to rename
@@ -544,6 +592,7 @@ class file
* @return bool
*/
public function rename ($oldname, $newname)
// {{{
{
$this->debug (2, "rename ($oldname, $newname)");
$oldname = $this->realpath ($oldname);
@@ -554,6 +603,7 @@ class file
$this->debug (1, "rename ($oldname, $newname) => $rc");
return $rc;
}
// }}}
/** Return a ini file converted to an array
* @param string $filename The filename of the ini file being parsed.
@@ -561,6 +611,7 @@ class file
* @return array
*/
public function parse_ini_file ($filename, $process_sections = false)
// {{{
{
$this->debug (2, "parse_ini_file ($filename, $process_sections)");
$filename = $this->realpath ($filename);
@@ -575,6 +626,7 @@ class file
$filename), 500);
return parse_ini_file ($filename, $process_sections);
}
// }}}
/** Return the canonical absolute path. Do not check if the directory exists,
* if there is links. Just calculate the realpath based on the chroot value
@@ -582,6 +634,7 @@ class file
* @return string the canonical absolute path
*/
public function realpath ($path)
// {{{
{
$oriPath = $path;
$this->debug (2, "realpath ($oriPath)");
@@ -620,6 +673,7 @@ class file
$this->debug (1, "realpath ($oriPath) => $path");
return $path;
}
// }}}
/** Remove the provided directory
* If the recurse flag is true, remove the content too (files and
@@ -631,6 +685,7 @@ class file
* dir is not writeable
*/
public function rmdir ($dirname, $recursive=false)
// {{{
{
$this->debug (2, "rmdir ($dirname, $recursive)");
$tmpdirname = $this->realpath ($dirname);
@@ -652,6 +707,7 @@ class file
}
return rmdir ($tmpdirname);
}
// }}}
/** Return the list of files and directories in the directory.
* Do not return the . and .. virtual dirs.
@@ -661,6 +717,7 @@ class file
* @throws If directory not exists, or is not executable
*/
public function scandir ($directory)
// {{{
{
$this->debug (2, "scandir ($directory)");
$directory = $this->realpath ($directory);
@@ -669,6 +726,7 @@ class file
natsort ($res);
return $res;
}
// }}}
/** Return the list of files and directories in the directory.
* Do not return the . and .. virtual dirs.
@@ -678,6 +736,7 @@ class file
* @throws If directory not exists, or is not executable
*/
public function scandirNotSorted ($directory)
// {{{
{
$this->debug (2, "scandirNotSorted ($directory)");
$directory = $this->realpath ($directory);
@@ -686,6 +745,7 @@ class file
array('..', '.')));
return $res;
}
// }}}
/** Calculate the sha1 sum of a file
* @param string $filename The file to hash
@@ -693,6 +753,7 @@ class file
* @throws If the file doesn't exists
*/
public function sha1_file ($filename)
// {{{
{
$this->debug (2, "sha1_file ($filename)");
$filename = $this->realpath ($filename);
@@ -707,6 +768,7 @@ class file
$filename), 500);
return sha1_file ($filename);
}
// }}}
/** Create a new file or update the timestamp if the file exists
* @param string $filename the filename
@@ -718,6 +780,7 @@ class file
* @throws If parent directory not exists, is not writeable
*/
public function touch ($filename, $time = null, $atime = null)
// {{{
{
$this->debug (2, "touch ($filename, $time, $atime)");
$filename = $this->realpath ($filename);
@@ -730,6 +793,7 @@ class file
$this->debug (1, "touch ($filename, $time, $atime) => $rc");
return $rc;
}
// }}}
/** Delete an existing file.
* @param string $filename The filename to remove
@@ -737,6 +801,7 @@ class file
* @throws If parent directory not exists, or is not executable
*/
public function unlink ($filename)
// {{{
{
$this->debug (2, "unlink ($filename)");
$filename = $this->realpath ($filename);
@@ -745,6 +810,7 @@ class file
return false;
return unlink ($filename);
}
// }}}
/** Check all the parents of the $directory if they are available, and
* executable. The path must exists.
@@ -756,6 +822,7 @@ class file
* @throws if there is a missing part, or a parent is not executable
*/
private function checkPathRO ($path)
// {{{
{
$this->debug (2, "checkPathRO ($path)");
$path = preg_replace ("#//+#", "/", $path);
@@ -826,6 +893,7 @@ class file
}
return $this->checkExternalPathRO ($path);
}
// }}}
/** Check all the parents of the $directory if they are available, and
* executable. The path must exists.
@@ -837,6 +905,7 @@ class file
* @throws if there is a missing part, or a parent is not executable
*/
private function checkPathRW ($path)
// {{{
{
$this->debug (2, "checkPathRW ($path)");
$this->checkPathRO ($path);
@@ -860,6 +929,7 @@ class file
}
return true;
}
// }}}
/** Save a debug log
* @param integer $prio The message priority. Should be higher than
@@ -868,6 +938,7 @@ class file
* @return null;
*/
private function debug ($prio, $message)
// {{{
{
if ($this->debug === false || $this->debug === 0)
return;
@@ -879,6 +950,7 @@ class file
// FILE_APPEND);
}
}
// }}}
/** External function allowed to be overloaded to test the RO access to a
* resource
@@ -886,9 +958,11 @@ class file
* @return boolean true if RO access, false if not
*/
public function checkExternalPathRO ($path)
// {{{
{
return true;
}
// }}}
/** External function allowed to be overloaded to test the RW access to a
* resource
@@ -896,7 +970,9 @@ class file
* @return boolean true if RW access, false if not
*/
public function checkExternalPathRW ($path)
// {{{
{
return true;
}
// }}}
}