From a16e3daa119833728af12f31802c49e8bb3864f5 Mon Sep 17 00:00:00 2001 From: naomiaro Date: Tue, 31 Aug 2010 15:36:05 -0700 Subject: [PATCH] #CC-1573 CleanStor script added to storage server to clean broken links or purge all files. --- src/modules/storageServer/bin/CleanStor.sh | 29 +++++ src/modules/storageServer/var/CleanStor.php | 128 ++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100755 src/modules/storageServer/bin/CleanStor.sh create mode 100644 src/modules/storageServer/var/CleanStor.php diff --git a/src/modules/storageServer/bin/CleanStor.sh b/src/modules/storageServer/bin/CleanStor.sh new file mode 100755 index 000000000..3103b06ae --- /dev/null +++ b/src/modules/storageServer/bin/CleanStor.sh @@ -0,0 +1,29 @@ +#!/bin/bash +#------------------------------------------------------------------------------- +# Copyright (c) 2010 Sourcefabric O.P.S. +# +# This file is part of the Campcaster project. +# http://campcaster.sourcefabric.org/ +# +# Campcaster is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Campcaster is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Campcaster; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +#------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- +# This script cleans audio files in the Campcaster storageServer. + +reldir=`dirname $0`/.. +cd $reldir/var + +php -q CleanStor.php "$@" || exit 1 diff --git a/src/modules/storageServer/var/CleanStor.php b/src/modules/storageServer/var/CleanStor.php new file mode 100644 index 000000000..b0809f61b --- /dev/null +++ b/src/modules/storageServer/var/CleanStor.php @@ -0,0 +1,128 @@ + 4)) { + list($dirList,$fileList) = File_Find::maptree($p_path); + + $array_mus; + foreach ($fileList as $filepath) { + + if (@substr($filepath, strlen($filepath) - 3) != "xml") { + $array_mus[] = $filepath; + } + } + + foreach ($array_mus as $audio_file) { + + if (@is_link($audio_file) && !@stat($audio_file)) { + + //filesystem clean up. + @unlink($audio_file); + echo "unlinked $audio_file\n"; + @unlink($audio_file . ".xml"); + echo "unlinked " . $audio_file . ".xml\n"; + @rmdir(@dirname($audio_file)); + echo "removed dir " . @dirname($audio_file) . "\n"; + + //database clean up. + $stored_audio_file = StoredFile::RecallByGunid(@basename($audio_file)); + $stored_audio_file->delete(); + } + } + + } +} + +function camp_remove_files($p_path) { + + if (!empty($p_path) && (strlen($p_path) > 4)) { + list($dirList,$fileList) = File_Find::maptree($p_path); + + foreach ($fileList as $filepath) { + echo " * Removing $filepath\n"; + @unlink($filepath); + echo "done.\n"; + } + foreach ($dirList as $dirpath) { + echo " * Removing $dirpath\n"; + @rmdir($dirpath); + echo "done.\n"; + } + } +} + +function camp_empty_db($db) { + global $CC_CONFIG; + + if (!PEAR::isError($db)) { + if (camp_db_table_exists($CC_CONFIG['filesTable'])) { + echo " * Deleting from database table ".$CC_CONFIG['filesTable']."\n"; + $sql = "DELETE FROM ".$CC_CONFIG['filesTable']; + camp_install_query($sql, false); + } + else { + echo " * Skipping: database table ".$CC_CONFIG['prefTable']."\n"; + } + if (camp_db_table_exists($CC_CONFIG['treeTable'])) { + echo " * Deleting from database table ".$CC_CONFIG['treeTable']."\n"; + $sql = "DELETE FROM ".$CC_CONFIG['treeTable']; + camp_install_query($sql, false); + } + else { + echo " * Skipping: database table ".$CC_CONFIG['treeTable']."\n"; + } + if (camp_db_table_exists($CC_CONFIG['mdataTable'])) { + echo " * Deleting from database table ".$CC_CONFIG['mdataTable']."\n"; + $sql = "DELETE FROM ".$CC_CONFIG['mdataTable']; + camp_install_query($sql, false); + } + else { + echo " * Skipping: database table ".$CC_CONFIG['mdataTable']."\n"; + } + } +} + + +global $CC_CONFIG; + +$CC_DBC = DB::connect($CC_CONFIG['dsn'], TRUE); +$CC_DBC->setFetchMode(DB_FETCHMODE_ASSOC); + +switch($argv[1]){ + + case '-e': + case '--empty': + camp_empty_db($CC_DBC); + camp_remove_files($CC_CONFIG['storageDir']); + break; + case '-c': + case '--clean': + camp_clean_files($CC_CONFIG['storageDir']); + break; + default: + printUsage(); + +} + +?> \ No newline at end of file