Skip to content

Commit cef6001

Browse files
committed
test_util: adding a new function for validating file metadata; for now: checking only the last datablock list, from the file header or the last ext. block (if any).
1 parent 16a52cc commit cef6001

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

tests/test_util.c

+84-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdlib.h>
55

66
#include "test_util.h"
7-
7+
#include "adf_file_util.h"
88

99
unsigned verify_file_data ( struct AdfVolume * const vol,
1010
const char * const filename,
@@ -64,6 +64,89 @@ unsigned verify_file_data ( struct AdfVolume * const vol,
6464
return nerrors;
6565
}
6666

67+
68+
static unsigned validate_file_metadata_last_ext ( struct AdfFile * const file )
69+
{
70+
unsigned fsize = file->fileHdr->byteSize;
71+
72+
RETCODE rc = adfFileSeek ( file, fsize + 1 );
73+
if ( rc != RC_OK || adfEndOfFile ( file ) != TRUE) {
74+
return 1;
75+
}
76+
//unsigned nerrors = 0;
77+
unsigned nDataBlocks = adfFileSize2Datablocks ( file->fileHdr->byteSize,
78+
file->volume->datablockSize );
79+
unsigned nExtBlocks = adfFileDatablocks2Extblocks ( nDataBlocks );
80+
//printf ("nDataBlocks %u, nExtBlocks %u, bufsize %u, truncsize %u\n",
81+
// nDataBlocks, nExtBlocks, bufsize, truncsize );
82+
//fflush(stdout);
83+
84+
// make sure we have available current ext. block (if needed)
85+
//int32_t * const dataBlocks = ( nExtBlocks < 1 ) ? file->fileHdr->dataBlocks :
86+
// file->currentExt->dataBlocks;
87+
struct bFileExtBlock * fext = NULL;
88+
int32_t * dataBlocks = NULL;
89+
if ( nExtBlocks < 1 ) {
90+
dataBlocks = file->fileHdr->dataBlocks;
91+
} else { // ( nExtBlocks >= 1 )
92+
if ( file->volume->datablockSize != 488 ) {
93+
// FFS
94+
//ck_assert_ptr_nonnull ( file->currentExt );
95+
if ( file->currentExt == NULL )
96+
return 2;
97+
dataBlocks = file->currentExt;
98+
} else {
99+
// for OFS - we must read the current ext.(!)
100+
fext = malloc ( sizeof (struct bFileExtBlock) );
101+
if ( fext == NULL )
102+
return 3;
103+
if ( adfFileReadExtBlockN ( file, (int) nExtBlocks - 1, fext ) != RC_OK )
104+
return 4;
105+
dataBlocks = fext->dataBlocks;
106+
}
107+
}
108+
109+
// check the number of non-zero blocks in the array of the last metadata block (header or ext)
110+
unsigned nonZeroCount = 0;
111+
for ( unsigned i = 0 ; i < MAX_DATABLK ; ++i ) {
112+
if ( dataBlocks[i] != 0 )
113+
nonZeroCount++;
114+
}
115+
free(fext);
116+
117+
if ( file->fileHdr->byteSize == 0 )
118+
//ck_assert_uint_eq ( nonZeroCount, 0 );
119+
if ( nonZeroCount != 0 )
120+
return 5;
121+
else {
122+
unsigned nonZeroExpected = ( nDataBlocks % MAX_DATABLK != 0 ?
123+
nDataBlocks % MAX_DATABLK :
124+
MAX_DATABLK );
125+
//ck_assert_uint_eq ( nonZeroCount, nonZeroExpected );
126+
if ( nonZeroCount != nonZeroExpected ) {
127+
printf ("Incorrect number of non-zero blocks in the last metadata block:"
128+
"nonZeroCount %u != nonZeroExpected %u, filesize %u",
129+
nonZeroCount, nonZeroExpected, file->fileHdr->byteSize );
130+
return 6;
131+
}
132+
}
133+
return 0;
134+
}
135+
136+
137+
unsigned validate_file_metadata ( struct AdfVolume * const vol,
138+
const char * const filename,
139+
const unsigned errors_max )
140+
{
141+
struct AdfFile * const file = adfFileOpen ( vol, filename, "r" );
142+
if ( ! file )
143+
return 1;
144+
unsigned nerrors = validate_file_metadata_last_ext ( file );
145+
adfFileClose ( file );
146+
return nerrors;
147+
}
148+
149+
67150
// bufsize must be divisible by 4
68151
void pattern_AMIGAMIG ( unsigned char * buf,
69152
const unsigned bufsize )

tests/test_util.h

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ unsigned verify_file_data ( struct AdfVolume * const vol,
1111
const unsigned bytes_written,
1212
const unsigned errors_max );
1313

14+
unsigned validate_file_metadata ( struct AdfVolume * const vol,
15+
const char * const filename,
16+
const unsigned errors_max );
17+
1418
void pattern_AMIGAMIG ( unsigned char * buf,
1519
const unsigned BUFSIZE );
1620

0 commit comments

Comments
 (0)