diff --git a/updateServer/src/situpdateInputFilter.c b/updateServer/src/situpdateInputFilter.cindex 41fb0ba..03d05d8 100644--- a/updateServer/src/situpdateInputFilter.c+++ b/updateServer/src/situpdateInputFilter.c@@ -28,12 +28,16 @@ int main(int argc,char **argv) int fd_partition=-1; static struct data_chunck_t data_chunk; static char all_chunkslist[MAX_CHUNKS_COUNT/8] = {0};- off64_t seekpos=0,lastseekpos=0;+ off64_t seekpos=0,lastseekpos=0, max_pos=0;+ int retcode=0;+ /* This variable tell to exit if we are arriving at the end of the device*/+ int exit_after_this_read=0;+ if (argc != 3) return -1; data_chunk.chunck_tag = SIGNATURE;-/* Get chunklist */+ /* Get chunklist */ fchunks=open(argv[2],O_RDONLY); if (fchunks < 0) { fprintf(stderr,"Cannot open chunck list file,error %s\n",strerror(errno));@@ -45,39 +49,70 @@ int main(int argc,char **argv) return 2; } close(fchunks);-/* Open partition for reading */++ /* Open partition for reading */ fd_partition=open(argv[1],O_RDONLY); if (fd_partition < 0) { fprintf(stderr,"Cannot open partition %s,error %s\n",argv[1],strerror(errno)); return 3; }- r=1;+ /* Let's find how big is our disk */+ max_pos=lseek64(fd_partition,0,SEEK_END);+ if (max_pos < 0) {+ fprintf(stderr, "Could not seek to the end of the device %s\n",strerror(errno));+ retcode=7; goto exit;+ }++ /* Let's get back to the begining */+ if (lseek64(fd_partition,0,SEEK_SET) < 0) {+ fprintf(stderr, "Could not seek to the begin of the device %s\n",strerror(errno));+ retcode=7; goto exit;+ }+ for (i=0;(i/8)<MAX_CHUNKS_COUNT;i++) { if (all_chunkslist[i/8] & (1 << (i%8))) { seekpos = i * CHUNK_SZ; if (seekpos != lastseekpos) { if (lseek64(fd_partition,seekpos,SEEK_SET)< 0) { fprintf(stderr,"Could not seek on partition,error %s\n",strerror(errno));- return 4;+ retcode=4; goto exit; } lastseekpos=seekpos; }- r=read(fd_partition,data_chunk.data,CHUNK_SZ);- if (r!=CHUNK_SZ)- {+ /* We want to read CHUNK_SZ bytes */+ int block_size=CHUNK_SZ;++ /* Will this get out of the device ? */+ if ((max_pos-seekpos) < CHUNK_SZ) {+ /*Let's grab the maximum amount of data we can */+ block_size=max_pos-seekpos;+ exit_after_this_read=1;+ }++ r=read(fd_partition,data_chunk.data,block_size);++ if (r != block_size) { fprintf(stderr,"Count not read on partition,error %s\n",strerror(errno));- return 5;+ retcode=5; goto exit; }- data_chunk.chunck_sz=r;++ data_chunk.chunck_sz=block_size; data_chunk.chunck_nb=i; if ( fwrite(&data_chunk,sizeof(data_chunk),1,stdout) != 1) { fprintf(stderr,"Count not write data on stdout,error %s\n",strerror(errno));- return 6;+ retcode=6; goto exit; } lastseekpos+=r;++ /* We reach the end of the device+ * Let's quit */+ if (exit_after_this_read == 1 )+ break; } }- return 0;+exit:+ close(fd_partition);+ return retcode; }