/* * 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 #include #include #include #include #include #include #include #define MAX_FILENAME_SIZE 255 #define MAX_BLOCK_SIZE 8192 static char buffer[ MAX_BLOCK_SIZE ]; extern int errno; char ** cred_1_svc(void *argp, struct svc_req *rqstp) { static char * result = "Gustavo Sverzut Barbieri "; return &result; } int * exist_1_svc(char **file, struct svc_req *rqstp) { static int result; struct stat buf; result = 0; assert( file || *file ); if ( ( stat( *file, &buf ) == 0 ) && S_ISREG( buf.st_mode ) ) result = 1; return &result; } fs_get_ret_t * get_1_svc(fs_get_param_t *file, struct svc_req *rqstp) { static fs_get_ret_t result; FILE *fd = NULL; errno = 0; assert( file && file->name && file->count ); result.status = 0; result.buffer.buffer_len = 0; result.buffer.buffer_val = buffer; if ( file->count >= MAX_BLOCK_SIZE ) file->count = MAX_BLOCK_SIZE; if ( ( fd = fopen( file->name, "r" ) ) == NULL ) { result.status = - errno; return &result; } if ( file->offset ) fseek( fd, file->offset, SEEK_SET ); result.buffer.buffer_len = fread( buffer, 1, file->count, fd ); if ( result.buffer.buffer_len != file->count ) { if ( feof( fd ) ) result.status = 0; else if ( ferror( fd ) ) { result.status = - errno; result.buffer.buffer_len = 0; } } else result.status = 1; fclose( fd ); return &result; } fs_list_t * list_1_svc(fs_list_t *dir, struct svc_req *rqstp) { static fs_list_t result; DIR *d; struct dirent *f; struct stat fstat; int dlen, len; errno = 0; assert( dir && dir->name ); result.status = 0; result.offset = 0; result.name = buffer; buffer[ 0 ] = '\0'; if ( ( d = opendir( dir->name ) ) == NULL ) { result.status = -errno; return &result; } if ( dir->offset ) seekdir( d, dir->offset ); dlen = strlen( dir->name ); while ( ( f = readdir( d ) ) != NULL ) { len = strlen( f->d_name ); if ( len + dlen + 2 >= MAX_BLOCK_SIZE ) { result.status = - ENOMEM; buffer[ 0 ] = '\0'; return &result; } strcpy( buffer, dir->name ); buffer[ dlen ] = '/'; buffer[ dlen + 1 ] = '\0'; strcat( buffer, f->d_name ); stat( buffer, &fstat ); if ( S_ISREG( fstat.st_mode ) ) { result.offset = telldir( d ); result.status = 1; break; } } closedir( d ); return &result; } /* author: Gustavo Sverzut Barbieri (http://www.gustavobarbieri.com.br) */