From 0e82f2a6062faa73055c2bec6610933d25cdd29e Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 18 Jul 2011 15:27:22 -0400 Subject: [PATCH] CC-2560: Make sure new watched dirs do not contain existing watched dirs (and stor dir) and vice-versa. -fixed --- airtime_mvc/application/models/MusicDir.php | 71 +++++++++++++++++++-- python_apps/media-monitor/MediaMonitor.py | 2 + 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/airtime_mvc/application/models/MusicDir.php b/airtime_mvc/application/models/MusicDir.php index 1ae298b16..2af522169 100644 --- a/airtime_mvc/application/models/MusicDir.php +++ b/airtime_mvc/application/models/MusicDir.php @@ -1,5 +1,7 @@ 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"); } diff --git a/python_apps/media-monitor/MediaMonitor.py b/python_apps/media-monitor/MediaMonitor.py index a4e89c725..f7f67516b 100644 --- a/python_apps/media-monitor/MediaMonitor.py +++ b/python_apps/media-monitor/MediaMonitor.py @@ -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)