-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsk.c
1056 lines (856 loc) · 25.8 KB
/
dsk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef _WIN32
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "dsk.h"
#ifdef _WIN32
# define DIR_SEPARATOR '\\'
#else
# define DIR_SEPARATOR '/'
#endif
static void dsk_default_output(const char *s);
DSK_Print dsk_puts = dsk_default_output;
//----------------------------------------
// return pointer to the base filename without path
//----------------------------------------
static const char *dsk_basename(const char *s)
{
if (!strchr(s, DIR_SEPARATOR))
return s;
const char *p = s + strlen(s);
for (; p != s; p--)
if (*p == DIR_SEPARATOR)
{
p++;
break;
}
return p;
}
//----------------------------------------
// default output function
//----------------------------------------
static void dsk_default_output(const char *s)
{
fputs(s, stdout);
}
//----------------------------------------
// DSK formatted output function
//----------------------------------------
static void dsk_printf(char *format, ...)
{
char buf[DSK_PRINTF_BUF_SIZE];
va_list valist;
va_start(valist, format);
vsprintf(buf, format, valist);
va_end(valist);
dsk_puts(buf);
}
//----------------------------------------
// return the size of a file
//----------------------------------------
static long dsk_get_file_size(DSK_Drive* drv)
{
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return 0;
}
long current_file_ptr = ftell(drv->fp); // get current file pointer
fseek(drv->fp, 0L, SEEK_END); // seek to the end of the file
long size = ftell(drv->fp); // get the current file pointer position (size)
fseek(drv->fp, current_file_ptr, SEEK_SET); // seek back to the original file pointer
return size;
}
//----------------------------------------
// returns TRUE if headerless JVC file
//----------------------------------------
static int dsk_is_simple_file(DSK_Drive* drv)
{
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return FALSE;
}
// get file size
long size = dsk_get_file_size(drv);
// if exact multiple of 256 then file is headerless
if (0 == (size % 256))
return TRUE;
return FALSE;
}
//----------------------------------------
// count granules used by file
//----------------------------------------
static int count_granules(DSK_Drive *drv, int gran, int *tail_sectors)
{
int count = 0;
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return E_FAIL;
}
while (!DSK_IS_LAST_GRANULE(gran))
{
count++;
gran = drv->fat.granule_map[gran];
}
// return number of sectors in last granule
if (tail_sectors)
*tail_sectors = gran & DSK_SECTOR_COUNT_MASK;
return count;
}
//---------------------------------------------
// walk and print the granule chain in the FAT
//---------------------------------------------
static int granule_chain(DSK_Drive *drv, DSK_DirEntry *dirent)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return E_FAIL;
}
int gran = dirent->first_granule;
while (!DSK_IS_LAST_GRANULE(gran))
{
dsk_printf("%02X->", gran);
gran = drv->fat.granule_map[gran];
}
dsk_printf("%02X\n", gran);
return E_OK;
}
//----------------------------------------
// return size of file
//----------------------------------------
static int file_size(DSK_Drive *drv, DSK_DirEntry *dirent)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid.\n");
return E_FAIL;
}
// get granule count and count of sectors in last granule
int sectors = 0;
int grans = count_granules(drv, dirent->first_granule, §ors);
// add in size of full granules
int size = (grans - 1) * DSK_BYTES_PER_GRANULE;
// add in size of full sectors
if (sectors)
{
if (dirent->bytes_in_last_sector)
size += (sectors - 1) * DSK_BYTES_DATA_PER_SECTOR;
else
size += (sectors) * DSK_BYTES_DATA_PER_SECTOR;
}
// add in bytes in last sector
size += ntohs(dirent->bytes_in_last_sector);
DSK_TRACE("%d total grans, %d sectors in last gran, %d bytes in last sector\n", grans, sectors, ntohs(dirent->bytes_in_last_sector));
return size;
}
//------------------------------------
//
//------------------------------------
static char *file_ncopy(char *dst, char *src, int n)
{
while (n--)
{
if (*src == ' ' || *src == 0)
break;
*dst++ = *src++;
}
*dst =0;
return dst;
}
//----------------------------------------------
// look for the given file in the DSK directory
//----------------------------------------------
static DSK_DirEntry *find_file_in_dir(DSK_Drive *drv, const char *filename)
{
char dirfile[DSK_MAX_FILENAME + DSK_MAX_EXT + 2];
// find dir entry
for (int i = 0; i < DSK_MAX_DIR_ENTRIES; i++)
{
DSK_DirEntry *dirent = &drv->dirs[i];
if (dirent->filename[0] == DSK_DIRENT_DELETED || DSK_DIRENT_FREE == (uint8_t)dirent->filename[0])
continue;
file_ncopy(dirfile, dirent->filename, DSK_MAX_FILENAME);
if (dirent->ext[0] != ' ')
{
strcat(dirfile, ".");
strncat(dirfile, dirent->ext, DSK_MAX_EXT);
}
for (int j = 0; j < strlen(dirfile); j++)
if (dirfile[j] == ' ')
dirfile[j] = 0;
if (!strcasecmp(filename, dirfile))
return dirent;
}
return NULL;
}
//----------------------------------------
// override the default output function
//----------------------------------------
void dsk_set_output_function(DSK_Print f)
{
dsk_puts = f;
}
//----------------------------------------
// count free granules on drive
//----------------------------------------
int dsk_free_granules(DSK_Drive *drv)
{
int count = 0;
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return E_FAIL;
}
for (int i = 0; i < DSK_TOTAL_GRANULES; i++)
{
if (drv->fat.granule_map[i] == DSK_GRANULE_FREE)
count++;
}
return count;
}
//----------------------------------------
// calc free bytes on DSK
//----------------------------------------
int dsk_free_bytes(DSK_Drive *drv)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return E_FAIL;
}
int grans = dsk_free_granules(drv);
int count = grans * DSK_BYTES_PER_GRANULE;
return count;
}
//----------------------------------------
// print a directory of the mounted drive
//----------------------------------------
int dsk_dir(DSK_Drive *drv)
{
char file[DSK_MAX_FILENAME + 1], ext[DSK_MAX_EXT + 1];
if (!drv || !drv->fp)
{
dsk_printf("no disk mounted.\n");
return E_FAIL;
}
memset(file, 0, sizeof(file));
memset(ext, 0, sizeof(ext));
dsk_printf("Directory of '%s'\n\n", drv->filename);
for (int i = 0; i < DSK_MAX_DIR_ENTRIES; i++)
{
DSK_DirEntry *dirent = &drv->dirs[i];
if (dirent->filename[0] != DSK_DIRENT_DELETED && DSK_DIRENT_FREE != (uint8_t)dirent->filename[0])
{
strncpy(file, dirent->filename, DSK_MAX_FILENAME);
strncpy(ext, dirent->ext, DSK_MAX_EXT);
int grans = count_granules(drv, dirent->first_granule, NULL);
#if 1
dsk_printf("%8s %3s\t%d %c %d\n", file, ext, dirent->type, dirent->binary_ascii == 0 ? 'B' : 'A', grans);
#else
dsk_printf("%8s %3s\t%d %c %d (%d bytes)\n", file, ext, dirent->type, dirent->binary_ascii == 0 ? 'B' : 'A', grans, file_size(drv, dirent));
granule_chain(drv, dirent);
#endif
}
}
dsk_printf("\n%d bytes (%d granules) free.\n", dsk_free_bytes(drv), dsk_free_granules(drv));
return E_OK;
}
//------------------------------------
// seek to track/sector in DSK file
//------------------------------------
int dsk_seek_drive(DSK_Drive *drv, int track, int sector)
{
assert(drv && drv->fp);
assert(track >= 0 && track < drv->num_tracks);
assert(sector >= 1 && sector <= DSK_SECTORS_PER_TRACK);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return E_FAIL;
}
long offset = DSK_OFFSET(track, sector);
DSK_TRACE("seeking to track %d, sector %d, offset is %lX\n", track, sector, offset);
fseek(drv->fp, offset, SEEK_SET);
return E_OK;
}
//------------------------------------
// seek to track/sector for granule
//------------------------------------
int dsk_seek_to_granule(DSK_Drive *drv, int granule)
{
assert(drv && drv->fp);
assert(granule >= 0 && granule <= DSK_LAST_GRANULE);
int track = granule / DSK_GRANULES_PER_TRACK;
// skip FAT/DIR track
if (granule >= DSK_DIR_START_GRANULE )
track++;
assert(track != DSK_DIR_TRACK);
int sector = 1 + (granule % DSK_GRANULES_PER_TRACK) * DSK_SECTORS_PER_GRANULE;
return dsk_seek_drive(drv, track, sector);
}
//------------------------------------
// print granule map for mounted drive
//------------------------------------
int dsk_granule_map(DSK_Drive *drv)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return E_FAIL;
}
// print FAT
for (int i = 1; i <= DSK_TOTAL_GRANULES; i++)
{
dsk_printf("%02X ", drv->fat.granule_map[i-1]);
if (0 == (i%23))
dsk_printf("\n");
}
dsk_printf("\n");
return E_OK;
}
//------------------------------------
// mount a DSK file
//------------------------------------
DSK_Drive *dsk_mount_drive(const char *filename)
{
DSK_Drive *drv;
drv = malloc(sizeof(DSK_Drive));
if (!drv)
return NULL;
memset(drv, 0, sizeof(DSK_Drive));
drv->fp = fopen(filename, "r+b");
if (!drv->fp)
{
dsk_printf("Disk (%s) not found.\n", filename);
free(drv);
return NULL;
}
// check for headerless JVC files
if (!dsk_is_simple_file(drv))
{
dsk_printf("Disk (%s) invalid. Must be headerless.", filename);
fclose(drv->fp);
free(drv);
return NULL;
}
// deduce tracks/sides from disk size
long file_size = dsk_get_file_size(drv);
int sectors = file_size / DSK_BYTES_DATA_PER_SECTOR;
drv->num_tracks = sectors / DSK_SECTORS_PER_TRACK; // 35
drv->num_sides = 1;
// read in the FAT
dsk_seek_drive(drv, DSK_DIR_TRACK, DSK_FAT_SECTOR);
fread(&drv->fat, sizeof(DSK_FAT), 1, drv->fp);
// read in the directory
dsk_seek_drive(drv, DSK_DIR_TRACK, DSK_DIRECTORY_SECTOR);
fread(&drv->dirs, sizeof(DSK_DirEntry), DSK_MAX_DIR_ENTRIES, drv->fp);
// save the DSK filename
strcpy(drv->filename, filename);
drv->drv_status = DSK_MOUNTED;
return drv;
}
//------------------------------------
// unmount a DSK file
//------------------------------------
int dsk_unmount_drive(DSK_Drive *drv)
{
if (!drv || !drv->fp)
{
dsk_printf("no disk mounted.\n");
return E_FAIL;
}
// ensure any changes are written!
dsk_flush(drv);
// fflush(drv->fp);
fclose(drv->fp);
drv->fp = NULL;
drv->drv_status = DSK_UNMOUNTED;
free(drv);
return E_OK;
}
//------------------------------------
// find and return a free dir entry
//------------------------------------
static DSK_DirEntry *find_free_dir_entry(DSK_Drive *drv)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid\n");
return NULL;
}
// find dir entry
for (int i = 0; i < DSK_MAX_DIR_ENTRIES; i++)
{
DSK_DirEntry *dirent = &drv->dirs[i];
if (dirent->filename[0] == DSK_DIRENT_DELETED || DSK_DIRENT_FREE == (uint8_t)dirent->filename[0])
return dirent;
}
return NULL;
}
//------------------------------------
// uppercase the string
//------------------------------------
static char *string_upper(char *s)
{
char *pstr = s;
for (; *s; s++)
{
*s = toupper(*s);
}
return pstr;
}
//------------------------------------
// return the first free granule
//------------------------------------
static int find_first_free_granule(DSK_Drive *drv)
{
for (int i = 0; i < DSK_TOTAL_GRANULES; i++)
{
if (drv->fat.granule_map[i] == DSK_GRANULE_FREE)
{
DSK_TRACE("returning free granule: %d\n", i);
return i;
}
}
return -1;
}
//------------------------------------
//
//------------------------------------
void translate_crlf_read(char *blk, size_t size)
{
for (int i = 0; i < size; i++)
{
if (blk[i] == 0x0a)
blk[i] = 0x0d;
}
}
//------------------------------------
//
//------------------------------------
void translate_crlf_write(char *blk, size_t size)
{
for (int i = 0; i < size; i++)
{
if (blk[i] == 0x0d)
blk[i] = 0x0a;
}
}
//------------------------------------
// add file to a mounted DSK file
//------------------------------------
int dsk_add_file(DSK_Drive *drv, const char *filename, DSK_OPEN_MODE mode, DSK_FILE_TYPE type)
{
char *pmode = "rb";
char sector_data[DSK_BYTES_DATA_PER_SECTOR];
char dest_filename[DSK_MAX_FILENAME + DSK_MAX_EXT + 2];
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid.\n");
return E_FAIL;
}
// get dest filename and ensure upper case
strcpy(dest_filename, dsk_basename(filename));
string_upper(dest_filename);
DSK_TRACE("adding file '%s'\n", dest_filename);
// check filename.ext length
if (strlen(filename) > DSK_MAX_FILENAME + DSK_MAX_EXT + 1)
{
dsk_printf("filename '%s' is too long.\n", dest_filename);
return E_FAIL;
}
// open the input file
#ifndef _WIN32
if (mode == DSK_MODE_ASCII)
pmode = "rt";
#endif
FILE *fin = fopen(filename, pmode);
if (!fin)
{
dsk_printf("file not found.\n");
return E_FAIL;
}
// get the file size
fseek(fin, 0, SEEK_END);
long fin_size = ftell(fin);
fseek(fin, 0, SEEK_SET);
// check that disk has space for file
if (fin_size > dsk_free_bytes(drv))
{
dsk_printf("out of space.\n");
fclose(fin);
return E_FAIL;
}
// see if file already exists on DSK
DSK_DirEntry *dirent = find_file_in_dir(drv, dest_filename);
if (dirent)
{
dsk_printf("file already exists.\n");
fclose(fin);
return E_FAIL;
}
// find first free directory entry
dirent = find_free_dir_entry(drv);
if (!dirent)
{
dsk_printf("drive is full.\n");
fclose(fin);
return E_FAIL;
}
// update directory entry
char *basefile = strtok(dest_filename, ".");
char *ext = strtok(NULL, "");
// copy in the filename, left justified, padded with spaces
for (int i = 0; i < DSK_MAX_FILENAME; i++)
{
if (i < strlen(basefile))
dirent->filename[i]= basefile[i];
else
dirent->filename[i] = ' ';
}
// copy in the extension, left justified, padded with spaces
for (int i = 0; i < DSK_MAX_EXT; i++)
{
if (i < strlen(ext))
dirent->ext[i] = ext[i];
else
dirent->ext[i] = ' ';
}
DSK_TRACE("%s -> file: %s, ext: %s\n", dest_filename, basefile, ext);
dirent->binary_ascii = (mode == DSK_MODE_ASCII) ? DSK_ENCODING_ASCII : DSK_ENCODING_BINARY;
dirent->type = type;
// find first free granule
int next_gran, gran = find_first_free_granule(drv);
dirent->first_granule = gran;
// copy full granule data
for (; fin_size >= DSK_BYTES_PER_GRANULE; fin_size -= DSK_BYTES_PER_GRANULE)
{
dsk_seek_to_granule(drv, gran);
for (int sector = 0; sector < DSK_SECTORS_PER_GRANULE; sector++)
{
fread(sector_data, sizeof(sector_data), 1, fin);
if (dirent->binary_ascii == DSK_ENCODING_ASCII)
translate_crlf_read(sector_data, sizeof(sector_data));
fwrite(sector_data, sizeof(sector_data), 1, drv->fp);
}
// temporarily mark this granule as used find chooses next avail
drv->fat.granule_map[gran] = 0xC0;
// find next available granule
next_gran = find_first_free_granule(drv);
// patch current granule point to next granule
drv->fat.granule_map[gran] = next_gran;
gran = next_gran;
}
// find number of sectors used
int tail_sectors = fin_size / DSK_BYTES_DATA_PER_SECTOR;
int extra_bytes = fin_size % DSK_BYTES_DATA_PER_SECTOR;
DSK_TRACE("fin_size: %d, tail sectors: %d, extra bytes: %d\n", fin_size, tail_sectors, extra_bytes);
dsk_seek_to_granule(drv, gran);
for (int sector = 0; sector < tail_sectors; sector++)
{
fread(sector_data, sizeof(sector_data), 1, fin);
if (dirent->binary_ascii == DSK_ENCODING_ASCII)
translate_crlf_read(sector_data, sizeof(sector_data));
fwrite(sector_data, sizeof(sector_data), 1, drv->fp);
}
fread(sector_data, extra_bytes, 1, fin);
if (dirent->binary_ascii == DSK_ENCODING_ASCII)
translate_crlf_read(sector_data, extra_bytes);
fwrite(sector_data, extra_bytes, 1, drv->fp);
// mark last granule
drv->fat.granule_map[gran] = 0xC0 + tail_sectors + (extra_bytes > 0);
// update bytes in last sector, respecting endianess
dirent->bytes_in_last_sector = htons(extra_bytes);
fclose(fin);
// update DSK image
drv->dirty_flag = 1;
dsk_flush(drv);
return E_OK;
}
//------------------------------------
// extract a file from the DSK
//------------------------------------
int dsk_extract_file(DSK_Drive *drv, const char *filename)
{
char sector_data[DSK_BYTES_DATA_PER_SECTOR];
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid.\n");
return E_FAIL;
}
// TODO - ensure upper case?
// string_upper(filename);
DSK_DirEntry *dirent = find_file_in_dir(drv, filename);
if (!dirent)
{
dsk_printf("file not found.\n");
return E_FAIL;
}
// open the output file
// open as text if ascii is set?
char* pmode = "wb";
#ifndef _WIN32
if (dirent->binary_ascii == DSK_ENCODING_ASCII)
pmode = "wt";
#endif
FILE *fout = fopen(filename, pmode);
if (!fout)
{
dsk_printf("file not found.\n");
return E_FAIL;
}
// walk the granules
int gran = dirent->first_granule;
// write out full granules
while(!DSK_IS_LAST_GRANULE(drv->fat.granule_map[gran]))
{
int track = gran / DSK_GRANULES_PER_TRACK;
int sector = 1 + (gran % DSK_GRANULES_PER_TRACK) * DSK_SECTORS_PER_GRANULE;
DSK_TRACE("t: %d, s: %d\n", track, sector);
dsk_seek_drive(drv, track, sector);
DSK_TRACE("extracting granule %2X\n", gran);
for (int i = 0; i < DSK_SECTORS_PER_GRANULE; i++)
{
fread(sector_data, DSK_BYTES_DATA_PER_SECTOR, 1, drv->fp);
if (dirent->binary_ascii == DSK_ENCODING_ASCII)
translate_crlf_write(sector_data, DSK_BYTES_DATA_PER_SECTOR);
fwrite(sector_data, DSK_BYTES_DATA_PER_SECTOR, 1, fout);
}
// get next granule
gran = drv->fat.granule_map[gran];
}
// write out partial granule
int next_gran = drv->fat.granule_map[gran];
int tail_sectors = (next_gran & DSK_SECTOR_COUNT_MASK);
int bytes_in_last_sector = ntohs(dirent->bytes_in_last_sector);
DSK_TRACE("tail sectors in granule %02X: %d\n", gran, tail_sectors);
// extract full sectors
if (bytes_in_last_sector)
tail_sectors--;
dsk_seek_to_granule(drv, gran);
for (int i = 0; i < tail_sectors; i++)
{
fread(sector_data, DSK_BYTES_DATA_PER_SECTOR, 1, drv->fp);
if (dirent->binary_ascii == DSK_ENCODING_ASCII)
translate_crlf_write(sector_data, DSK_BYTES_DATA_PER_SECTOR);
fwrite(sector_data, DSK_BYTES_DATA_PER_SECTOR, 1, fout);
}
// extract partial sector
DSK_TRACE("bytes in last sector: %d\n", bytes_in_last_sector);
if (bytes_in_last_sector)
{
fread(sector_data, bytes_in_last_sector, 1, drv->fp);
if (dirent->binary_ascii == DSK_ENCODING_ASCII)
translate_crlf_write(sector_data, bytes_in_last_sector);
fwrite(sector_data, bytes_in_last_sector, 1, fout);
}
fclose(fout);
return E_OK;
}
//------------------------------------
// delete file from mounted DSK
//------------------------------------
int dsk_del(DSK_Drive *drv, const char *filename)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid.\n");
return E_FAIL;
}
DSK_DirEntry *dirent = find_file_in_dir(drv, filename);
if (!dirent)
{
dsk_printf("file '%s' not found.\n", filename);
return E_FAIL;
}
// mark all file granules as free
int gran = dirent->first_granule;
while (!DSK_IS_LAST_GRANULE(gran))
{
int next_gran = drv->fat.granule_map[gran];
DSK_TRACE("marking granule %2X as free.\n", gran);
drv->fat.granule_map[gran] = DSK_GRANULE_FREE;
gran = next_gran;
}
// mark the dir entry as freed
dirent->filename[0] = DSK_DIRENT_DELETED;
drv->dirty_flag = 1;
dsk_flush(drv);
return E_OK;
}
//------------------------------------
// create a new DSK file
//------------------------------------
DSK_Drive *dsk_new(char *filename, int tracks, int sides)
{
char sector_data[DSK_BYTES_DATA_PER_SECTOR];
assert(filename);
// TODO - check filename and ext length
// ensure upper case
string_upper(filename);
FILE *fout = fopen(filename, "wb");
if (!fout)
{
dsk_printf("file not found.\n");
return NULL;
}
// write out empty DSK
memset(sector_data, 0, DSK_BYTES_DATA_PER_SECTOR);
for (int track = 0; track < tracks * sides; track++)
{
for (int sector = 0; sector < DSK_SECTORS_PER_TRACK; sector++)
fwrite(sector_data, sizeof(sector_data), 1, fout);
}
fclose(fout);
// mount it
DSK_Drive *drv = dsk_mount_drive(filename);
if (!drv)
{
dsk_printf("disk not found.\n");
return NULL;
}
// format it
dsk_format(drv);
return drv;
}
//------------------------------------
// write the FAT and DIR to the DSK
//------------------------------------
int dsk_flush(DSK_Drive *drv)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid.\n");
return E_FAIL;
}
// don't flush if nothing has changed
if (!drv->dirty_flag)
{
DSK_TRACE("flush called with no changes.\n");
return E_OK;
} else
{
DSK_TRACE("flushing dirty file.\n");
}
// write out the FAT
dsk_seek_drive(drv, DSK_DIR_TRACK, DSK_FAT_SECTOR);
fwrite(drv->fat.granule_map, 1, DSK_TOTAL_GRANULES, drv->fp);
// write out the Directory
dsk_seek_drive(drv, DSK_DIR_TRACK, DSK_DIRECTORY_SECTOR);
fwrite(drv->dirs, sizeof(DSK_DirEntry), DSK_MAX_DIR_ENTRIES, drv->fp);
// clear dirty flag
drv->dirty_flag = 0;
return E_OK;
}
//------------------------------------
// format a mounted drive
//------------------------------------
int dsk_format(DSK_Drive *drv)
{
assert(drv && drv->fp);
if (!drv || !drv->fp)
{
dsk_printf("disk invalid.\n");
return E_FAIL;
}
// clear FAT granule entries
for (int i = 0; i < DSK_TOTAL_GRANULES; i++)
drv->fat.granule_map[i] = DSK_GRANULE_FREE;
// zero other FAT entries
for (int i = DSK_TOTAL_GRANULES; i < DSK_BYTES_DATA_PER_SECTOR; i++)
drv->fat.granule_map[i] = 0;
// clear Directory entries
for (int i = 0; i < DSK_MAX_DIR_ENTRIES; i++)
drv->dirs[i].filename[0] = DSK_DIRENT_FREE;
// flush changes to DSK file
drv->dirty_flag = 1;
dsk_flush(drv);
return E_OK;
}
//------------------------------------
// rename file1 to file2
//------------------------------------
int dsk_rename(DSK_Drive *drv, char *current_file, char *new_file)
{
string_upper(current_file);
string_upper(new_file);
if (strlen(new_file) > DSK_MAX_FILENAME + DSK_MAX_EXT + 1)
{
dsk_printf("filename too long.\n");
return E_FAIL;
}
// ensure current file exists
DSK_DirEntry *dirent1 = find_file_in_dir(drv, current_file);
if (!dirent1)
{
dsk_printf("file '%s' not found.\n", current_file);
return E_FAIL;
}