dumpRIOobj.cpp File Reference

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

Go to the source code of this file.

Defines

#define DEFAULT_BLOCK_SIZE   131072
#define MAX_INPUT_BUFFER   60000

Functions

int main (int argc, char *argv[])

Define Documentation

#define DEFAULT_BLOCK_SIZE   131072

Definition at line 31 of file dumpRIOobj.cpp.

#define MAX_INPUT_BUFFER   60000

Definition at line 32 of file dumpRIOobj.cpp.


Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 36 of file dumpRIOobj.cpp.

00037 {
00038     int           file_in, i, n_rep, c, a1, a2, a3, a4, a5;
00039     // TODO: Fazer o disk e o fileOut nao ter tamanhos fixos
00040     int           disk[ 30 ], file_out[ 10 ];
00041     FILE *arquivo_discos;
00042     int           vetor_discos1[ 400 ];
00043     int           vetor_blocos1[ 400 ];
00044     int           vetor_discos2[ 400 ];
00045     int           vetor_blocos2[ 400 ];
00046     int           vetor_discos3[ 400 ];
00047     int           vetor_blocos3[ 400 ];
00048     int           vetor_discos4[ 400 ];
00049     int           vetor_blocos4[ 400 ];
00050     int           vetor_discos5[ 400 ];
00051     int           vetor_blocos5[ 400 ];
00052 
00053     char          header[ 64 ];
00054     unsigned char buf[ DEFAULT_BLOCK_SIZE ];
00055     unsigned int  source, block;
00056     char *linha;
00057     int           lidos, escritos;
00058 
00059 // Le os parametros de entrada e inicializa as devidas variaveis.
00060 // Lembre-se que o nome do programa e' o argv[0]!
00061 
00062     fprintf( stdout, "AVISO: ESTA VERSAO DO dumpRIOObj SOMENTE FUNCIONA COM 1 DISCO POR STORAGE!\n" );
00063     fprintf( stdout, "E LEMBRE-SE DE SEMPRE APAGAR OS ARQUIVOS DE SAIDA,\n" );
00064     fprintf( stdout, "NAO SEI O QUE ACONTECE SE NAO FAZE-LO! SIM, EXISTE UM TODO PRA ISSO TB!\n\n" );
00065 
00066     switch (argc)
00067     {
00068         case 1: fprintf( stdout, "\nPara utilizar o dumpRIOObj, use a sintaxe ./dumpRIOObj [OP��ES] [CAMINHO DO METADADO DO ARQUIVO]\n" );
00069                 fprintf( stdout, "\nAs op��es v�lidas implementadas at� agora s�o:\n" );
00070                 fprintf( stdout, "\n-r: N�mero de replica��es do arquivo (default = 1)\n" );
00071                 return(0);
00072 
00073         case 2: n_rep = 1;
00074                 file_in = open( argv[ 1 ], O_RDONLY );
00075 //                fprintf(stdout, "\nargv =%s\n", argv[1] );
00076                 break;
00077 
00078         case 4: n_rep = atoi( argv[2] );
00079                 file_in = open( argv[ 3 ], O_RDONLY );
00080 //                fprintf( stdout, "\nargv =%s\n", argv[2] );
00081                 break;
00082 
00083         default:fprintf( stdout, "\nN�mero de par�metros n�o suportado por esta vers�o do dumpRIOObj, contate o seu revendedor autorizado\n");
00084                 return ( 0 );
00085     }
00086 
00087 
00088 //------------------------------------------------------------------------------
00089 
00090 // TODO: Fazer um vetor dinamico para os discos
00091 
00092     arquivo_discos = fopen( "discos.conf", "r" );
00093 
00094     if( arquivo_discos == NULL )
00095     {
00096         fprintf( stdout, "\nArquivo de configuracao dos discos nao encontrado!" );
00097         fprintf( stdout, "\nCrie um arquivo \"discos.conf\" no diretorio corrente" );
00098         fprintf( stdout, "\n  com o caminho completo de um disco por linha\n\n" );
00099         return ( 0 );
00100     }
00101 
00102     c = 0;
00103 
00104     // Lendo o disco do arquivo e carregando-o no vetor disk
00105 
00106     linha = (char *) malloc( sizeof(char) * MAX_INPUT_BUFFER );
00107     if( fgets( linha, MAX_INPUT_BUFFER, arquivo_discos ) == NULL )
00108     {
00109         fprintf( stderr, "main erro ao obter dados do arquivo discos.conf\n");
00110     }
00111     if( strlen( linha ) > 0 && linha[ strlen( linha ) - 1 ] == '\n' )
00112         linha[ strlen( linha ) - 1 ] = 0;
00113 
00114     while( 1 )
00115     {
00116 
00117         disk[ c ] = open( linha, O_RDONLY );
00118 
00119         if( disk[ c ] < 0 )
00120         {
00121             fprintf( stdout, "\nERRO NO ARQUIVO! Disco nao existe: %s !\n", linha );
00122             return( 1 );
00123         }
00124 
00125         if( fgets( linha, MAX_INPUT_BUFFER, arquivo_discos ) == NULL )
00126         {
00127             fprintf( stderr, "main erro ao obter dados do arquivo %s\n",
00128                      linha );
00129         }
00130 
00131         if( strlen( linha ) > 0 && linha[ strlen( linha ) - 1 ] == '\n' )
00132             linha[ strlen( linha ) - 1 ] = 0;
00133 
00134         if( feof( arquivo_discos ) )
00135         {
00136             break;
00137         }
00138         // TODO: numerar os discos corretamente e quando o fizer, tirar aviso do inicio do prog.
00139         c += 4;
00140     }
00141 
00142     fclose ( arquivo_discos );
00143 
00144   // Cria os videos de saida
00145     for( i = 0; i<n_rep; i++ )
00146     {
00147   // TODO: apagar arquivos se eles ja existirem
00148         char str[ 100 ];
00149         sprintf( str,"%d",i );
00150         char str2[] = "RIOObj";
00151         char str3[] = ".mpg";
00152         strcat( str2, str );
00153         strcat( str2, str3 );
00154         file_out[ i ] = creat( str2, S_IRUSR );
00155 
00156     }
00157 //------------------------------------------------------------------------------
00158 
00159     // Efetivamente le e escreve os blocos no arquivo de saida
00160     lidos = read( file_in, (char *)&header, 64 );
00161 
00162     if( lidos != 64 )
00163     {
00164         fprintf( stdout, "main erro ao ler o valor de header: lidos %u", lidos);
00165         fprintf( stdout, " bytes de um total de 64 bytes.\n" );
00166     }
00167     else if( lidos == -1 )
00168     {
00169         fprintf( stdout, "main erro %u (%s) ao ler o valor de header\n",
00170                  errno, strerror( errno ) );
00171     }
00172 
00173     i = 0;
00174     a1 = 0;
00175     a2 = 0;
00176     a3 = 0;
00177     a4 = 0;
00178     a5 = 0;
00179 
00180     while( read( file_in, (char *)&source, 4 ) == 4 &&
00181            read( file_in, (char *)&block,  4 ) == 4    )
00182     {
00183         lseek( disk[ source - 1 ], DEFAULT_BLOCK_SIZE * block, SEEK_SET );
00184 
00185         // teste de corretude do dump e serve para imprimir a tabela de metadado no final
00186         if(i == 0) { vetor_discos1[a1] = source; vetor_blocos1[a1] = block; a1++; }
00187         if(i == 1) { vetor_discos2[a2] = source; vetor_blocos2[a2] = block; a2++; }
00188         if(i == 2) { vetor_discos3[a3] = source; vetor_blocos3[a3] = block; a3++; }
00189         if(i == 3) { vetor_discos4[a4] = source; vetor_blocos4[a4] = block; a4++; }
00190         if(i == 4) { vetor_discos5[a5] = source; vetor_blocos5[a5] = block; a5++; }
00191 
00192 
00193         if(read(  disk[ source - 1 ], buf, DEFAULT_BLOCK_SIZE ) != DEFAULT_BLOCK_SIZE )
00194         {
00195             fprintf(stdout, "leu menos bytes! disco %6d bloco %6d\n", source, block);
00196         }
00197         else
00198         {
00199             escritos = write( file_out[ i ], buf, DEFAULT_BLOCK_SIZE );
00200             if( escritos != DEFAULT_BLOCK_SIZE )
00201             {
00202                 fprintf( stdout, "main erro ao escrever o valor de buf: escritos %u",
00203                          escritos);
00204                 fprintf( stdout, " bytes de um total de %u bytes.\n", 
00205                          DEFAULT_BLOCK_SIZE); 
00206             }
00207             else if( escritos == -1 )
00208             {  
00209                 fprintf( stdout, "main erro %u (%s) ao escrever o valor de buf\n",
00210                          errno, strerror( errno ) );
00211             }
00212         }
00213 
00214         // solucao elegante para sempre escrever no arquivo certo
00215         // se o metadado possuir os blocos em sequencia e o local das replicas
00216         // sequencialmente, ie, com n_rep = 2 (1 copia redundante)
00217         // na primeira linha a ser lida temos o primeiro bloco,
00218         // na segunda temos a copia
00219         // na terceira temos o segundo bloco, na quarta temos a copia do segundo bloco
00220         // e assim por diante
00221         i++;
00222         i %= n_rep;
00223     }
00224 
00225     close( file_in );
00226 
00227     for( i = 0; i < n_rep; i++ )
00228     {
00229         close( file_out[ i ] );
00230     }
00231 
00232     fprintf( stdout, "\nCOPIA1 \t-\t COPIA 2 \t-\t COPIA 3 \t-\t COPIA 4 \t-\t COPIA 5\t-\tno.bloco\n\n" );
00233     fprintf( stdout, "___________________________________________________________________________________________________________________\n" );
00234     for( i = 0; i < a1; i++ )
00235     {
00236         fprintf( stdout, "|disco %4d bloco %4d |disco %4d bloco %4d |disco %4d bloco %4d |disco %4d bloco %4d |disco %4d bloco %4d |#%4d\n", vetor_discos1[i], vetor_blocos1[i], vetor_discos2[i], vetor_blocos2[i], vetor_discos3[i], vetor_blocos3[i], vetor_discos4[i], vetor_blocos4[i], vetor_discos5[i], vetor_blocos5[i], i );
00237     }
00238     fprintf( stdout, "___________________________________________________________________________________________________________________\n" );
00239 
00240 
00241     return( 0 );
00242 }

Generated on Wed Jul 4 16:03:29 2012 for RIO by  doxygen 1.6.3