Commit 22440680 authored by Gerald Salin's avatar Gerald Salin
Browse files

test if directory exist before trying to delete its content to avoid

errors
parent d25986df
......@@ -137,7 +137,6 @@ class tx_nG6_utils {
static function purge_directory($user_login, $user_pwd, $directory) {
if ($directory != 'undefined' && $directory != '') {
// And process the directories structure, remove all files except .html ans .png
$connection = ssh2_connect('127.0.0.1', 22);
if (!$connection) {
return 3;
......@@ -146,6 +145,20 @@ class tx_nG6_utils {
if (!ssh2_auth_password($connection, $user_login,$user_pwd )){
return 2;
}
$stream = ssh2_exec($connection, 'ls '.$directory.'/*' );
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
//if error is raised, $directory does not exist>no problem (in case of an analysis which do not have associated files
if(stream_get_contents($errorStream)) {
// Close the streams
fclose($errorStream);
fclose($stream);
return 0;
}
$stream = ssh2_exec($connection, 'ls '.$directory.'/* | grep -v -e ".html$" -e ".png$" | xargs rm -f' );
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
......@@ -153,8 +166,7 @@ class tx_nG6_utils {
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
// Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothing
//if error is raised, connected user does not have the right to delete the files
if(stream_get_contents($errorStream)) {
// Close the streams
fclose($errorStream);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment