cvs: pear /File_Fstab Fstab.php
| From: | Ian Eure | Date: | Fri, 30 Apr 2004 21:44:36 +0000 |
| Subject: | cvs: pear /File_Fstab Fstab.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-19045@lists.php.net to get a copy of this message | ||
ieure Fri Apr 30 17:44:36 2004 EDT
Modified files:
/pear/File_Fstab Fstab.php
Log:
Define constants for errors & return them where needed.
Add file mtime checking.
Make sure we clear things out if we're re-loading the fstab.
getEntryFor*() returns PEAR_Error instead of false now if the requested entry doesn't exist.
http://cvs.php.net/diff.php/pear/File_Fstab/Fstab.php?r1=1.2&r2=1.3&ty=u
Index: pear/File_Fstab/Fstab.php
diff -u pear/File_Fstab/Fstab.php:1.2 pear/File_Fstab/Fstab.php:1.3
--- pear/File_Fstab/Fstab.php:1.2 Fri Apr 30 16:01:40 2004
+++ pear/File_Fstab/Fstab.php Fri Apr 30 17:44:35 2004
@@ -16,12 +16,16 @@
// | Author: Ian Eure <ieure@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: Fstab.php,v 1.2 2004/04/30 20:01:40 ieure Exp $
+// $Id: Fstab.php,v 1.3 2004/04/30 21:44:35 ieure Exp $
require_once 'PEAR.php';
//require_once 'File/Fstab/Entry.php';
require_once 'File/Fstab/Entry.php';
+define('FILE_FSTAB_ERROR_NOENT', -1);
+define('FILE_FSTAB_PERMISSION_DENIED', -2);
+define('FILE_FSTAB_WRONG_CLASS', -3);
+
/**
* Class to read, write, and manipulate fstab files
*
@@ -65,6 +69,18 @@
var $_isLoaded = false;
/**
+ * Last-modified time of the fstab file
+ *
+ * This is used by load() to determine if the fstab has been changed since
+ * it was last loaded.
+ *
+ * @type int
+ * @access protected
+ * @see load()
+ */
+ var $_mtime = 0;
+
+ /**
* Constructor
*
* @param $options array Associative array of options to set
@@ -101,11 +117,23 @@
/**
* Parse fstab file
*
+ *
+ *
+ * @param $force boolean Force re-loading fstab, even if it hasn't changed.
* @return void
* @since 1.0.1
*/
- function load()
+ function load($force = false)
{
+ // Dont re-load if everything is up-to-date
+ if (filemtime($this->options['file']) < $this->_mtime &&
+ !$force) {
+ return;
+ }
+
+ // Clear things out before we load.
+ $this->entries = array();
+
$fp = fopen($this->options['file'], 'r');
while ($line = fgets($fp, 1024)) {
@@ -123,6 +151,7 @@
}
$this->_isLoaded = true;
+ $this->_mtime = filemtime($this->options['file']);
}
/**
@@ -145,7 +174,7 @@
* Get a File_Fstab_Entry object for a path
*
* @param $path string Mount point
- * @return mixed File_Fstab_Entry instance or false
+ * @return mixed File_Fstab_Entry instance on success, PEAR_Error otherwise
*/
function &getEntryForPath($path)
{
@@ -155,15 +184,14 @@
return $this->entries[$key];
}
}
- //return PEAR::raiseError("No entry for \"$path\"");
- return false;
+ return PEAR::raiseError("No entry for path \"{$path}\"",
PEAR_ERROR_NOENT);
}
/**
* Get a File_Fstab_Entry object for a block device
*
* @param $blockdev string Block device
- * @return mixed File_Fstab_Entry instance or false
+ * @return mixed File_Fstab_Entry instance on success, PEAR_Error otherwise
*/
function &getEntryForDevice($blockdev)
{
@@ -174,14 +202,14 @@
return $this->entries[$key];
}
}
- return false;
+ return PEAR::raiseError("No entry for device \"{$blockdev}\"",
PEAR_ERROR_NOENT);
}
/**
* Get a File_Fstab_Entry object for a UUID
*
* @param $uuid string UUID device
- * @return mixed File_Fstab_Entry instance or false
+ * @return mixed File_Fstab_Entry instance on success, PEAR_Error otherwise
*/
function &getEntryForUUID($uuid)
{
@@ -192,14 +220,14 @@
return $this->entries[$key];
}
}
- return false;
+ return PEAR::raiseError("No entry for UUID \"{$uuid}\"",
PEAR_ERROR_NOENT);
}
/**
* Get a File_Fstab_Entry object for a label
*
* @param $label string Label
- * @return mixed File_Fstab_Entry instance or false
+ * @return mixed File_Fstab_Entry instance on success, PEAR_Error otherwise
*/
function &getEntryForLabel($label)
{
@@ -210,7 +238,7 @@
return $this->entries[$key];
}
}
- return false;
+ return PEAR::raiseError("No entry for label \"{$label}\"",
PEAR_ERROR_NOENT);
}
/**
@@ -222,7 +250,8 @@
function addEntry(&$entry)
{
if (!is_a($entry, 'File_Fstab_Entry')) {
- return PEAR::raiseError("Entry must be derived from File_Fstab_Entry.");
+ return PEAR::raiseError("Entry must be derived from File_Fstab_Entry.",
+ FILE_FSTAB_WRONG_CLASS);
}
$this->entries[] = $entry;
@@ -256,7 +285,8 @@
{
$output = $output ? $output : $this->options['file'];
if (file_exists($output) && !is_writable($output)) {
- return PEAR::raiseError("Can't write to {$output}");
+ return PEAR::raiseError("Can't write to {$output}",
+ FILE_FSTAB_PERMISSION_DENIED);
}
$fp = @fopen($output, 'w');