From d3cdae71d893eebaf9ab485fbcee33c0d6e280da Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 30 Aug 2015 17:37:17 +0200 Subject: creating a basedir per host in recursive scp --- src/cssh.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/cssh.c b/src/cssh.c index bd53804..244d90e 100644 --- a/src/cssh.c +++ b/src/cssh.c @@ -971,7 +971,17 @@ int main( int argc, char *argv[] ) rc = push_dir_stack( &dir_stack[i], pwd ); - // TODO: directory should be a base per hostname[i] + size_t len = strlen( pwd ) + 2 + strlen( host[i] ); + char *full_path = (char *)malloc( len ); + if( full_path == NULL ) { + fprintf( stderr, "ERROR: Memory allocation failed for full path of basse directory for host '%s'\n", + host[i] ); + free( pwd ); + cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + exit( EXIT_FAILURE ); + } + snprintf( full_path, len, "%s/%s", pwd, host[i] ); + rc = push_dir_stack( &dir_stack[i], full_path ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed remember initial base directory '%s' in directory stack\n", @@ -981,6 +991,14 @@ int main( int argc, char *argv[] ) exit( EXIT_FAILURE ); } free( pwd ); + + rc = mkdir( full_path, 0750 ); + if( rc < 0 ) { + fprintf( stderr, "ERROR: failed to create base directory '%s' for host '%s': %s\n", + full_path, host[i], strerror( errno ) ); + cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + exit( EXIT_FAILURE ); + } } while( !all_eof ) { @@ -999,9 +1017,8 @@ int main( int argc, char *argv[] ) rc = ssh_scp_pull_request( scp[i] ); switch( rc ) { case SSH_SCP_REQUEST_NEWDIR: { - // TODO: mkdir in host[i] workspace - char *dir = top_dir_stack( &dir_stack[i] ); + rc = chdir( dir ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed to change to directory '%s': %s\n", @@ -1021,7 +1038,7 @@ int main( int argc, char *argv[] ) cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } - // TODO: change dir to workspace of scp[i] + rc = mkdir( filename, mode ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed to create directory '%s': %s\n", @@ -1046,6 +1063,7 @@ int main( int argc, char *argv[] ) exit( EXIT_FAILURE ); } snprintf( full_path, len, "%s/%s", pwd, filename ); + rc = push_dir_stack( &dir_stack[i], full_path ); free( pwd ); if( rc < 0 ) { -- cgit v1.2.3-54-g00ecf