CC-2560: Make sure new watched dirs do not contain existing

watched dirs (and stor dir) and vice-versa.

-fixed
This commit is contained in:
martin 2011-07-18 15:27:22 -04:00
parent 7edacd89cc
commit 0e82f2a606
2 changed files with 68 additions and 5 deletions

View File

@ -1,5 +1,7 @@
<?php
class NestedDirectoryException extends Exception { }
class MusicDir {
/**
@ -58,6 +60,58 @@ class MusicDir {
RabbitMq::PushSchedule();
}
/**
* Checks if p_dir1 is the ancestor of p_dir2. Returns
* true if it is the ancestor, false otherwise. Note that
* /home/user is considered the ancestor of /home/user
*
* @param string $p_dir1
* The potential ancestor directory.
* @param string $p_dir2
* The potential descendent directory.
* @return boolean
* Returns true if it is the ancestor, false otherwise.
*/
private static function isAncestorDir($p_dir1, $p_dir2){
if (strlen($p_dir1) > strlen($p_dir2)){
return false;
}
return substr($p_dir2, 0, strlen($p_dir1)) == $p_dir1;
}
/**
* Checks whether the path provided is a valid path. A valid path
* is defined as not being nested within an existing watched directory,
* or vice-versa. Throws a NestedDirectoryException if invalid.
*
* @param string $p_path
* The path we want to validate
* @return void
*/
public static function isPathValid($p_path){
$dirs = self::getWatchedDirs();
$dirs[] = self::getStorDir();
foreach ($dirs as $dirObj){
$dir = $dirObj->getDirectory();
$diff = strlen($dir) - strlen($p_path);
if ($diff == 0){
if ($dir == $p_path){
throw new NestedDirectoryException("'$p_path' is already watched.");
}
} else if ($diff > 0){
if (self::isAncestorDir($p_path, $dir)){
throw new NestedDirectoryException("'$p_path' contains nested watched directory: '$dir'");
}
} else { /* diff < 0*/
if (self::isAncestorDir($dir, $p_path)){
throw new NestedDirectoryException("'$p_path' is nested within existing watched directory: '$dir'");
}
}
}
}
public static function addDir($p_path, $p_type)
{
@ -67,13 +121,20 @@ class MusicDir {
$dir = new CcMusicDirs();
$dir->setType($p_type);
$p_path = realpath($p_path)."/";
$temp = $dir->setDirectory($p_path);
try{
try {
/* isPathValid() checks if path is a substring or a superstring of an
* existing dir and if not, throws NestedDirectoryException */
self::isPathValid($p_path);
$dir->setDirectory($p_path);
$dir->save();
return array("code"=>0);
}
catch(Exception $e){
//echo $e->getMessage();
} catch (NestedDirectoryException $nde){
$msg = $nde->getMessage();
return array("code"=>1, "error"=>"$msg");
} catch(Exception $e){
return array("code"=>1, "error"=>"'$p_path' is already set as the current storage dir or in the watched folders list");
}

View File

@ -30,6 +30,8 @@ except Exception, e:
logger = logging.getLogger()
logger.info("\n\n*** Media Monitor bootup ***\n\n")
try:
config = AirtimeMediaConfig(logger)
api_client = api_client.api_client_factory(config.cfg)