|
4 | 4 | #include <stdlib.h>
|
5 | 5 |
|
6 | 6 | #include "test_util.h"
|
7 |
| - |
| 7 | +#include "adf_file_util.h" |
8 | 8 |
|
9 | 9 | unsigned verify_file_data ( struct AdfVolume * const vol,
|
10 | 10 | const char * const filename,
|
@@ -64,6 +64,89 @@ unsigned verify_file_data ( struct AdfVolume * const vol,
|
64 | 64 | return nerrors;
|
65 | 65 | }
|
66 | 66 |
|
| 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 | + |
67 | 150 | // bufsize must be divisible by 4
|
68 | 151 | void pattern_AMIGAMIG ( unsigned char * buf,
|
69 | 152 | const unsigned bufsize )
|
|
0 commit comments