10 #include "d64copy_int.h"
20 static FILE *the_file;
21 static char *error_map;
22 static int block_count;
25 #define ERROR_MAP_LENGTH D71_BLOCKS
27 static int block_offset(
int tr,
int se)
30 for(i = 1; i < tr; i++)
32 sectors += d64copy_sector_count(fs_settings->two_sided, i);
34 return (sectors + se) * BLOCKSIZE;
37 static int read_block(
unsigned char tr,
unsigned char se,
unsigned char *block)
39 if(fseek(the_file, block_offset(tr, se), SEEK_SET) == 0)
41 return fread(block, BLOCKSIZE, 1, the_file) != 1;
49 static int atom_execute = 0;
50 static unsigned char atom_tr;
51 static unsigned char atom_se;
52 static const unsigned char *atom_blk;
54 static int atom_read_status;
56 static int write_block(
unsigned char tr,
unsigned char se,
const unsigned char *blk,
int size,
int read_status)
65 atom_read_status = read_status;
69 ofs = block_offset(tr, se);
70 if(fseek(the_file, ofs, SEEK_SET) == 0)
72 error_map[ofs / BLOCKSIZE] = (char) ((read_status == 0) ? 1 : read_status);
73 ret = fwrite(blk, size, 1, the_file) != 1;
86 const void *arg,
int for_writing,
87 turbo_start start, d64copy_message_cb message_cb)
90 int stat_ok, is_image, error_info;
92 char *name = (
char*)arg;
95 fs_settings = settings;
99 is_image = error_info = 0;
103 if(filesize == D71_BLOCKS * BLOCKSIZE)
106 block_count = D71_BLOCKS;
109 else if(filesize == D71_BLOCKS * (BLOCKSIZE + 1))
113 block_count = D71_BLOCKS;
118 block_count = STD_BLOCKS;
119 for( tr = STD_TRACKS; !is_image && tr <= TOT_TRACKS; )
121 is_image = filesize == block_count * BLOCKSIZE;
124 error_info = is_image =
125 filesize == block_count * (BLOCKSIZE + 1);
129 block_count += d64copy_sector_count( 0, tr++ );
132 if( is_image && tr != STD_TRACKS )
134 message_cb(1,
"non-standard number or tracks: %d", tr);
145 the_file = fopen(name,
"rb");
148 message_cb(0,
"could not open %s", name);
152 message_cb(1,
"image contains error information");
154 if(settings->end_track == -1)
156 settings->end_track = tr;
158 else if(settings->end_track > tr)
160 message_cb(1,
"resetting end track to %d", tr);
161 settings->end_track = tr;
167 message_cb(0,
"neither a .d64 not .d71 file: %s", name);
172 message_cb(0,
"could not stat %s", name);
177 the_file = fopen(name, is_image ?
"r+b" :
"wb");
182 if(settings->two_sided)
186 else if(settings->end_track <= STD_TRACKS)
190 else if(settings->end_track <= EXT_TRACKS)
200 error_map = calloc(ERROR_MAP_LENGTH, 1);
203 message_cb(0,
"no memory for error map");
216 if(fseek(the_file, block_count * BLOCKSIZE, SEEK_SET) != 0 ||
217 fread(error_map, block_count, 1, the_file) != 1)
219 message_cb(0,
"%s: could not read error map", name);
224 if(fseek(the_file, block_count * BLOCKSIZE, SEEK_SET) != 0)
226 message_cb(0,
"%s: could not seek to end of file", name);
237 block_count += d64copy_sector_count(settings->two_sided, ++tr);
240 message_cb(1,
"growing image file to %d blocks", block_count);
242 if (arch_ftruncate(arch_fileno(the_file), block_count * BLOCKSIZE) != 0)
244 message_cb(0,
"%s: could not extend image file", name);
254 message_cb(0,
"could not open %s", name);
257 return the_file == NULL;
260 static void close_disk(
void)
262 int i, has_errors = 0;
268 if (the_file && atom_execute)
271 write_block(atom_tr, atom_se, atom_blk, atom_size, atom_read_status);
276 switch(fs_settings->error_mode)
287 for(i = 0; !has_errors && i < block_count; i++)
289 has_errors = error_map[i] != 1;
300 if(fseek(the_file, block_count * BLOCKSIZE, SEEK_SET) == 0)
302 fwrite(error_map, block_count, 1, the_file);
307 arch_ftruncate(arch_fileno(the_file), block_count * BLOCKSIZE);
323 DECLARE_TRANSFER_FUNCS(fs_transfer, 0, 0);
int arch_filesize(const char *Filename, off_t *Filesize)
Obtain the size of a given file.
Define makros and functions which account for differences between the different architectures.