/* * This is sample code generated by rpcgen. * These are only templates and you can use them * as a guideline for developing your own functions. */ #include "fs.h" #include #include "interface.c" #include #include #define MAX_FILENAME_SIZE 255 #define MAX_BLOCK_SIZE 8192 #define BLOCK_SIZE 8000 extern int errno; static CLIENT *client = NULL; void cred() { char **cred = NULL; char *arg; assert( client != NULL ); while ( 1 ) { cred = cred_1( (void*) &arg, client ); if ( cred == NULL ) clnt_perror( client, "Call failed!" ); else break; } printf( "CREDITS: %s\n", *cred ); if ( clnt_freeres( client, (xdrproc_t) xdr_wrapstring, (caddr_t) cred ) ==0 ) fprintf( stderr, "ERROR: Could not free RPC/XDR result 'cred' (%p)\n", cred ); } void exist( char *file ) { int *exist = NULL; while ( ! exist ) { exist = exist_1( &file, client ); if ( exist == NULL ) { clnt_perror( client, "Call failed!" ); continue; } printf( "%s, file '%s' %s.\n", (*exist) ? "Yes" : "No", file, (*exist) ? "does exist" : "doesn't exist" ); if ( clnt_freeres( client, (xdrproc_t) xdr_int, (caddr_t) exist ) == 0 ) fprintf( stderr, "ERROR: Could not free RPC/XDR result 'exist' (%p)\n", exist ); } } void list( char *dir ) { fs_list_t *file; fs_list_t arg; assert( client != NULL && dir != NULL ); arg.name = dir; arg.offset = 0; while ( 1 ) { file = list_1( &arg, client ); if ( file == (fs_list_t *) NULL ) { clnt_perror( client, "Call failed!" ); continue; } if ( file->status == 1 ) puts( file->name ); else if ( file->status < 0 ) printf( "ERROR: Could not read '%s': %s\n", arg.name, strerror( - file->status ) ); arg.offset = file->offset; if ( clnt_freeres( client, (xdrproc_t) xdr_fs_list_t, (caddr_t) file ) == 0 ) fprintf( stderr, "ERROR: Could not free RPC/XDR result 'file' (%p)\n", file ); if ( arg.offset == 0 ) break; } } void get( char *src, char *dst, int statistics ) { fs_get_ret_t *gr; fs_get_param_t gp; double size = 0.0f; char unity[3] = "b"; unsigned long long int ts, count=0; double rate; char ru[ 3 ] = ""; int status = 0; struct timeval t0, t1, t2, tr; assert( client != NULL && src != NULL && dst ); gp.offset = 0; gp.count = BLOCK_SIZE; gp.name = src; gettimeofday( &t0, NULL ); while ( 1 ) { if ( statistics ) gettimeofday( &t1, NULL ); gr = get_1( &gp, client ); if ( gr == (fs_get_ret_t *) NULL ) { clnt_perror( client, "Call failed!" ); continue; } if ( statistics ) { gettimeofday( &t2, NULL ); timersub( &t2, &t1, &tr ); ts = (unsigned long long) (tr.tv_sec * 1000000 + tr.tv_usec); if ( gr->buffer.buffer_len > ( 1024 * 1024 ) ) { strncpy( unity, "Mb", 3 ); size = (double)1.0f * gr->buffer.buffer_len / 1024.0f / 1024.0f; } if ( gr->buffer.buffer_len > 1024 ) { strncpy( unity, "Kb", 3 ); size = (double)1.0f * gr->buffer.buffer_len / 1024.0f; } else { strncpy( unity, "b", 3 ); size = (double)1.0f * gr->buffer.buffer_len; } rate = (double) gr->buffer.buffer_len / (double)ts; fprintf( stderr, "STAT: %0.3f %s in %lld us. [%.1f b/us]\n", size, unity, ts, rate ); } count += gr->buffer.buffer_len; status = gr->status; if ( status >=0 ) { gp.offset += gr->buffer.buffer_len; /* Free XDR 'gr' */ if ( clnt_freeres( client, (xdrproc_t) xdr_fs_get_ret_t, (caddr_t) gr ) ==0) fprintf( stderr, "ERROR: Could not free RPC/XDR result 'gr' (%p)\n", gr ); if ( status == 0 ) break; } else { printf( "ERROR: Could not get file '%s': %s\n", gp.name, strerror( - status ) ); /* Free XDR 'gr' */ if ( clnt_freeres( client, (xdrproc_t) xdr_fs_get_ret_t, (caddr_t) gr ) ==0) fprintf( stderr, "ERROR: Could not free RPC/XDR result 'gr' (%p)\n", gr ); break; } } gettimeofday( &t2, NULL ); timersub( &t2, &t0, &tr ); ts = (unsigned long long)((double)tr.tv_sec * 1000.0f + tr.tv_usec / 1000.0f); if ( count > ( 1024 * 1024 ) ) { strncpy( unity, "Mb", 3 ); size = 1.0f * count / 1024.0f / 1024.0f; } else if ( count > 1024 ) { strncpy( unity, "Kb", 3 ); size = 1.0f * count / 1024.0f; } else { strncpy( unity, "b", 3 ); size = 1.0f * count; } { double tv; char tu[ 3 ] = ""; if ( ts > 1000 * 60 * 60 ) { strncpy( tu, "H", 3 ); tv = (double) ts / 1000 * 60 * 60; } else if ( ts > 1000 * 60 ) { strncpy( tu, "M", 3 ); tv = (double) ts / 1000 * 60; } else if ( ts > 1000 ) { strncpy( tu, "s", 3 ); tv = (double) ts / 1000; } else { strncpy( tu, "ms", 3 ); tv = (double) ts; } rate = (double) count / ( ts / 1000.0f ); if ( rate > ( 1024.0f * 1024.0f ) ) { rate /= 1024.0f * 1024.0f; strncpy( ru, "Mb", 3 ); } else if ( rate > 1024.0f ) { rate /= 1024.0f; strncpy( ru, "Kb", 3 ); } else strncpy( ru, "b", 3 ); fprintf( stderr, "Got file '%s' (%0.1f %s) in %0.1f %s. [%0.1f%s/s]\n", gp.name, size, unity, tv, tu, rate, ru ); } gettimeofday( &t1, NULL ); } void fsprog_1( char *host ) { char *s, *line; #ifndef DEBUG if ( client ) clnt_destroy( client ); client = clnt_create( host, FSPROG, FSVERS, "udp" ); if ( client == NULL ) { clnt_pcreateerror( host ); exit( EXIT_FAILURE ); } #endif /* DEBUG */ ui_init(); while ( ui_stop == 0 ) { if ( ( line = readline( "> " ) ) == NULL ) { ui_comm_quit( "" ); break; } s = stripwhite( line ); if ( *s != '\0' ) { add_history( s ); ui_execute_line( s ); } free( line ); } #ifndef DEBUG clnt_destroy( client ); #endif /* DEBUG */ } int main( int argc, char *argv[] ) { char *host; if ( argc < 2 ) { printf( "usage: %s server_host\n", argv[ 0 ] ); exit( EXIT_FAILURE ); } host = argv[ 1 ]; fsprog_1( host ); exit( EXIT_SUCCESS ); } /* author: Gustavo Sverzut Barbieri (http://www.gustavobarbieri.com.br) */