-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathakaiutil_tar.c
1145 lines (1049 loc) · 30.8 KB
/
akaiutil_tar.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
/*
* Copyright (C) 2008,2010,2012,2018,2019 Klaus Michael Indlekofer. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "akaiutil_io.h"
#include "akaiutil.h"
#include "akaiutil_file.h"
#include "akaiutil_take.h"
#include "akaiutil_tar.h"
u_int
tar_checksum(u_char *hp)
{
u_int sum;
u_int i;
int zeroflag;
if (hp==NULL){
return 0;
}
sum=0;
zeroflag=1; /* still all zeroes */
for (i=0;i<sizeof(struct tar_head_s);i++){
if (hp[i]!=0){
zeroflag=0;
}
if ((i>=148)&&(i<156)){ /* treat chksum field */
sum+=(u_int)' '; /* space */
}else{
sum+=(u_int)hp[i];
}
}
/* Note: sum is always >0 !!! */
if (zeroflag){
sum=0; /* unique value, see comment above */
}
return sum;
}
int
tar_export(int fd,struct part_s *pp,struct vol_s *vp,struct file_s *fp,u_int ti,u_int flags,int verbose,u_char *filtertagp)
{
struct tar_head_s tarhd;
u_int chksum;
u_int size;
u_int n;
u_char buf[TAR_BLOCKSIZE];
int wavret;
char *wavname;
/* DD take */
u_int ddcstarts,ddcstarte;
u_int ddcsizes,ddcsizee;
struct akai_ddtake_s ddt;
if (fd<0){
return -1;
}
if ((flags&(TAR_EXPORT_PART|TAR_EXPORT_VOL|TAR_EXPORT_ANYFILE))==0){
return -1;
}
ddcstarts=0;
ddcstarte=0;
ddcsizes=0;
ddcsizee=0;
/* create tar header */
bzero(&tarhd,sizeof(struct tar_head_s));
/* name */
/* XXX should always fit */
if (flags&TAR_EXPORT_PART){
/* partition */
if (pp==NULL){
return -1;
}
if (pp->type==PART_TYPE_DD){
/* harddisk DD partition */
if (pp->letter==0){
sprintf(tarhd.name,"DD/");
}else{
sprintf(tarhd.name,"DD%u/",(u_int)pp->letter);
}
}else{
/* sampler partition */
sprintf(tarhd.name,"%c/",pp->letter);
}
size=0;
}
if (flags&TAR_EXPORT_VOL){
/* sampler volume */
if ((vp==NULL)||(vp->type==AKAI_VOL_TYPE_INACT)){
return -1;
}
sprintf(tarhd.name+strlen(tarhd.name),"%s/",vp->name);
size=0;
if ((flags&TAR_EXPORT_ANYFILE)==0){ /* not a file? */
/* XXX use devmajor field for type */
sprintf(tarhd.devmajor,"%07o",vp->type);
/* XXX use devminor field for load number */
sprintf(tarhd.devminor,"%07o",vp->lnum);
#ifndef TAR_NOVOLPARAMTARHD
if (vp->param!=NULL){
/* XXX use linkname for volume parameters */
tarhd.linkname[0]='\0'; /* XXX magic, also empty linkname string */
tarhd.linkname[1]='P'; /* XXX magic */
bcopy(vp->param,
&tarhd.linkname[0]+2, /* XXX +2: linkname starts with magic */
sizeof(struct akai_volparam_s)); /* XXX should fit */
}
#endif
}
}
if (flags&TAR_EXPORT_DDFILE){
if (flags&TAR_EXPORT_WAV){
wavret=akai_take2wav(pp,ti,fd,&size,&wavname,TAKE2WAV_CHECK);
if (wavret<0){ /* error? */
#if 1
return 0; /* skip this file w/o error */
#else
return -1;
#endif
}
if (wavret>0){ /* not for WAV? */
return 0; /* skip this file w/o error */
}
strcpy(tarhd.name+strlen(tarhd.name),wavname);
}else{
char fnamebuf[AKAI_NAME_LEN+4+1]; /* name (ASCII), +4 for ".<type>", +1 for '\0' */
/* index */
if (ti>=AKAI_DDTAKE_MAXNUM){
return 0; /* skip this file w/o error */
}
/* get directory entry */
bcopy(&pp->head.dd.take[ti],&ddt,sizeof(struct akai_ddtake_s));
/* name */
akai2ascii_name(ddt.name,fnamebuf,0); /* 0: not S900 */
strcpy(fnamebuf+strlen(fnamebuf),AKAI_DDTAKE_FNAMEEND);
strcpy(tarhd.name+strlen(tarhd.name),fnamebuf);
/* clusters */
ddcstarts=(ddt.cstarts[1]<<8)
+ddt.cstarts[0];
if (ddcstarts==0){ /* empty? */
return 0; /* skip this file w/o error */
}
ddcstarte=(ddt.cstarte[1]<<8)
+ddt.cstarte[0];
/* determine csizes (sample clusters) and csizee (envelope clusters) */
ddcsizes=akai_count_ddfatchain(pp,ddcstarts);
ddcsizee=akai_count_ddfatchain(pp,ddcstarte);
/* XXX use cstarts/cstarte fields in DD take header for csizes/csizee */
ddt.cstarts[1]=0xff&(ddcsizes>>8);
ddt.cstarts[0]=0xff&ddcsizes;
ddt.cstarte[1]=0xff&(ddcsizee>>8);
ddt.cstarte[0]=0xff&ddcsizee;
/* size in bytes */
size=sizeof(struct akai_ddtake_s)+(ddcsizes+ddcsizee)*AKAI_DDPART_CBLKS*AKAI_HD_BLOCKSIZE;
}
}
if (flags&TAR_EXPORT_FILE){
/* sampler file */
if ((fp==NULL)||(fp->type==AKAI_FTYPE_FREE)){
return -1;
}
/* test if tag-filter matches */
if (akai_match_filetags(filtertagp,fp->tag)<0){ /* no match? */
return 0; /* skip this file w/o error */
}
if (flags&TAR_EXPORT_WAV){
wavret=akai_sample2wav(fp,fd,&size,&wavname,SAMPLE2WAV_CHECK);
if (wavret<0){ /* error? */
#if 1
return 0; /* skip this file w/o error */
#else
return -1;
#endif
}
if (wavret>0){ /* not for WAV? */
return 0; /* skip this file w/o error */
}
strcpy(tarhd.name+strlen(tarhd.name),wavname);
}else{
strcpy(tarhd.name+strlen(tarhd.name),fp->name);
size=fp->size;
}
/* XXX use devmajor field for osver */
if ((flags&TAR_EXPORT_WAV)&&(fp->type==AKAI_SAMPLE900_FTYPE)){
sprintf(tarhd.devmajor,"%07o",AKAI_OSVER_S900VOL); /* XXX */
}else{
sprintf(tarhd.devmajor,"%07o",fp->osver); /* XXX */
}
if ((fp->volp!=NULL)
&&((fp->volp->type==AKAI_VOL_TYPE_S3000)||(fp->volp->type==AKAI_VOL_TYPE_CD3000))){
/* XXX use linkname for file tags */
tarhd.linkname[0]='\0'; /* XXX magic, also empty linkname string */
tarhd.linkname[1]='T'; /* XXX magic */
bcopy(fp->tag,
&tarhd.linkname[0]+2, /* XXX +2: linkname starts with magic */
AKAI_FILE_TAGNUM); /* XXX should fit */
}
}
#ifndef TAR_NOTAGSFILE
#ifndef TAR_TAGSFILENAME
#define TAR_TAGSFILENAME "TAGS.DAT"
#endif
if (flags&TAR_EXPORT_TAGSFILE){
/* tags-file */
if ((pp==NULL)||(pp->type!=PART_TYPE_HD) /* not in S1000/S3000 harddisk partition on partition level? */
||(strncmp(AKAI_PARTHEAD_TAGSMAGIC,(char *)pp->head.hd.tagsmagic,4)!=0)
||(flags&TAR_EXPORT_WAV)){
#if 1
return 0; /* skip this file w/o error */
#else
return -1;
#endif
}
strcpy(tarhd.name+strlen(tarhd.name),TAR_TAGSFILENAME);
size=4+AKAI_PARTHEAD_TAGNUM*AKAI_NAME_LEN; /* tags magic and tag names */;
}
#endif
#ifndef TAR_NOVOLPARAMFILE
#ifndef TAR_VOLPARAMFILENAME
#define TAR_VOLPARAMFILENAME "VOLPARAM.DAT"
#endif
if (flags&TAR_EXPORT_VOLPARAMFILE){
/* volparam-file */
if ((vp==NULL)||(vp->type==AKAI_VOL_TYPE_INACT)||(vp->param==NULL)
||(flags&TAR_EXPORT_WAV)){
#if 1
return 0; /* skip this file w/o error */
#else
return -1;
#endif
}
strcpy(tarhd.name+strlen(tarhd.name),TAR_VOLPARAMFILENAME);
size=sizeof(struct akai_volparam_s);
}
#endif
if (verbose){
printf("%s\n",tarhd.name);
fflush(NULL);
}
/* mode, uid, gid */
sprintf(tarhd.mode,"%07o",0600); /* XXX -rw------- */
sprintf(tarhd.uid,"%07o",0); /* XXX */
sprintf(tarhd.gid,"%07o",0); /* XXX */
/* size */
sprintf(tarhd.size,"%011o",size);
/* mtime */
sprintf(tarhd.mtime,"%011o",(u_int)time(NULL)); /* XXX */
/* space */
tarhd.space=' ';
/* type */
if (flags&TAR_EXPORT_ANYFILE){ /* file? */
tarhd.type=TAR_TYPE_REG;
}else{
/* directory */
tarhd.type=TAR_TYPE_DIR;
}
/* magic */
bcopy(TAR_USTAR,tarhd.ustar,8);
/* user name */
#ifndef TAR_UNAME
#define TAR_UNAME "AKAI"
#endif
strcpy(tarhd.uname,TAR_UNAME);
/* group name */
#ifndef TAR_GNAME
#define TAR_GNAME "AKAI"
#endif
strcpy(tarhd.gname,TAR_GNAME);
/* checksum */
chksum=tar_checksum((u_char *)&tarhd);
sprintf(tarhd.chksum,"%06o",chksum);
/* write tar header */
if (WRITE(fd,(void *)&tarhd,sizeof(struct tar_head_s))!=sizeof(struct tar_head_s)){
perror("write");
return -1;
}
if ((flags&TAR_EXPORT_ANYFILE)==0){ /* not a file? */
return 0; /* done */
}
if (flags&TAR_EXPORT_DDFILE){
if (flags&TAR_EXPORT_WAV){
if (akai_take2wav(pp,ti,fd,NULL,NULL,SAMPLE2WAV_EXPORT)<0){
return -1;
}
}else{
if (akai_export_take(fd,pp,&ddt,ddcsizes,ddcsizee,ddcstarts,ddcstarte)<0){
fprintf(stderr,"cannot export take\n");
return -1;
}
}
}
if (flags&TAR_EXPORT_FILE){
/* export sampler file */
if (flags&TAR_EXPORT_WAV){
if (akai_sample2wav(fp,fd,NULL,NULL,SAMPLE2WAV_EXPORT)<0){
return -1;
}
}else{
if (akai_read_file(fd,NULL,fp,0,fp->size)<0){
return -1;
}
}
}
#ifndef TAR_NOTAGSFILE
if (flags&TAR_EXPORT_TAGSFILE){
/* export tags-file */
if (WRITE(fd,(void *)pp->head.hd.tagsmagic,size)!=(int)size){
perror("write");
return -1;
}
}
#endif
#ifndef TAR_NOVOLPARAMFILE
if (flags&TAR_EXPORT_VOLPARAMFILE){
/* export volparam-file */
if (WRITE(fd,(void *)vp->param,size)!=(int)size){
perror("write");
return -1;
}
}
#endif
/* fill rest of TAR_BLOCKSIZE bytes */
n=size%TAR_BLOCKSIZE;
n=(TAR_BLOCKSIZE-n)%TAR_BLOCKSIZE;
if (n>0){
bzero(buf,n);
/* write */
if (WRITE(fd,(void *)buf,n)!=(int)n){
perror("write");
return -1;
}
}
return 0;
}
int
tar_export_vol(int fd,struct vol_s *vp,u_int flags,int verbose,u_char *filtertagp)
{
u_int fi;
struct file_s tmpfile; /* current file */
if ((fd<0)||(vp==NULL)||(vp->type==AKAI_VOL_TYPE_INACT)||(vp->partp==NULL)||(!vp->partp->valid)||(vp->partp->fd<0)){
return -1;
}
#ifndef TAR_NOVOLPARAMFILE
if ((vp->param!=NULL)
&&((flags&TAR_EXPORT_WAV)==0)){
/* export volparam-file */
if (tar_export(fd,vp->partp,vp,NULL,0,flags|TAR_EXPORT_VOLPARAMFILE,verbose,NULL)){
return -1;
}
}
#endif
/* files in volume directory */
for (fi=0;fi<vp->fimax;fi++){
/* get file */
if (akai_get_file(vp,&tmpfile,fi)<0){
continue; /* XXX ignore error, next file */
}
/* export [part/volume/]file */
if (tar_export(fd,vp->partp,vp,&tmpfile,0,flags|TAR_EXPORT_FILE,verbose,filtertagp)){
return -1;
}
}
return 0;
}
int
tar_export_part(int fd,struct part_s *pp,u_int flags,int verbose,u_char *filtertagp)
{
u_int vi;
struct vol_s tmpvol; /* current volume */
if ((fd<0)||(pp==NULL)||(!pp->valid)||(pp->fd<0)){
return -1;
}
if (pp->type==PART_TYPE_DD){
/* harddisk DD partition */
/* export all DD takes */
u_int ti;
u_int cstarts;
for (ti=0;ti<AKAI_DDTAKE_MAXNUM;ti++){
cstarts=(pp->head.dd.take[ti].cstarts[1]<<8)
+pp->head.dd.take[ti].cstarts[0];
if (cstarts==0){ /* empty? */
continue; /* next */
}
if (tar_export(fd,pp,NULL,NULL,ti,flags|TAR_EXPORT_DDFILE,verbose,filtertagp)){
return -1;
}
}
}
/* now, sampler partition */
#ifndef TAR_NOTAGSFILE
if ((pp->type==PART_TYPE_HD) /* in S1000/S3000 harddisk partition? */
&&((flags&TAR_EXPORT_WAV)==0)){
/* export tags-file */
/* Note: tar_export() will check for valid tags magic */
if (tar_export(fd,pp,NULL,NULL,0,flags|TAR_EXPORT_TAGSFILE,verbose,NULL)){
return -1;
}
}
#endif
/* volumes in root directory of partition */
for (vi=0;vi<pp->volnummax;vi++){
/* get volume */
if (akai_get_vol(pp,&tmpvol,vi)<0){
continue; /* XXX ignore error, next volume */
}
if ((tmpvol.type!=AKAI_VOL_TYPE_S900)
&&(tmpvol.type!=AKAI_VOL_TYPE_S1000)
&&(tmpvol.type!=AKAI_VOL_TYPE_S3000)
&&(tmpvol.type!=AKAI_VOL_TYPE_CD3000)){
continue; /* next volume */
}
/* create vol/ */
if (tar_export(fd,pp,&tmpvol,NULL,0,flags|TAR_EXPORT_VOL,verbose,NULL)){
return -1;
}
/* export volume */
if (tar_export_vol(fd,&tmpvol,flags|TAR_EXPORT_VOL,verbose,filtertagp)<0){
return -1;
}
}
return 0;
}
int
tar_export_disk(int fd,struct disk_s *dp,u_int flags,int verbose,u_char *filtertagp)
{
u_int pi;
if ((fd<0)||(dp==NULL)){
return -1;
}
/* partitions on disk */
for (pi=0;pi<part_num;pi++){
/* partition not on disk */
if (part[pi].diskp!=dp){
continue; /* next partition */
}
/* create part/ */
if (tar_export(fd,&part[pi],NULL,NULL,0,flags|TAR_EXPORT_PART,verbose,NULL)){
return -1;
}
/* export partition (sampler or DD) */
if (tar_export_part(fd,&part[pi],flags|TAR_EXPORT_PART,verbose,filtertagp)<0){
return -1;
}
}
return 0;
}
int
tar_export_curdir(int fd,int verbose,u_int flags)
{
if (curdiskp==NULL){ /* no disk? */
printf("must be on disk\n");
return -1;
}
if (curpartp==NULL){ /* no partition? */
/* no, on disk */
return tar_export_disk(fd,curdiskp,flags,verbose,curfiltertag);
}
if (curvolp==NULL){ /* no sampler volume? */
/* now, in partition (sampler or DD) */
return tar_export_part(fd,curpartp,flags,verbose,curfiltertag);
}
/* now, in sampler volume */
return tar_export_vol(fd,curvolp,flags,verbose,curfiltertag);
}
int
tar_export_tailzero(int fd)
{
u_char buf[TAR_TAILZERO_BLOCKS*TAR_BLOCKSIZE];
/* zero blocks */
bzero(buf,TAR_TAILZERO_BLOCKS*TAR_BLOCKSIZE);
/* write blocks */
if (WRITE(fd,buf,TAR_TAILZERO_BLOCKS*TAR_BLOCKSIZE)!=TAR_TAILZERO_BLOCKS*TAR_BLOCKSIZE){
perror("write");
return -1;
}
return 0;
}
int
tar_import_curdir(int fd,u_int vtype0,int verbose,u_int flags)
{
struct tar_head_s tarhd;
u_int byteend,skip;
u_int chksum,chksum0;
u_int l;
int ret;
struct vol_s tmpvol;
struct file_s tmpfile;
u_int vtype,lnum;
u_int ftype;
u_int osver;
u_char *tagp;
struct akai_volparam_s *parp;
u_int wavbcount;
if (curdiskp==NULL){
printf("must be on disk\n");
return -1;
}
save_curdir(1); /* 1: could be modifications */
/* interpret tar file */
skip=0;
ret=-1; /* no success so far */
for (;;){
if (verbose){
fflush(NULL);
}
#ifdef DEBUG
print_blk_cache();
#endif
restore_curdir();
/* skip if necessary */
if (skip>0){
/* skip file */
if (LSEEK(fd,(long)skip,SEEK_CUR)<0){
perror("lseek");
ret=-1;
goto tar_import_done;
}
}
skip=0;
/* read header */
/* XXX size must not exceed SSIZE_MAX! */
ret=(int)READ(fd,(void *)&tarhd,sizeof(struct tar_head_s));
if (ret==0){
/* end of file */
ret=0; /* success */
goto tar_import_done;
}else if (ret!=sizeof(struct tar_head_s)){
perror("read");
ret=-1;
goto tar_import_done;
}
/* check header */
chksum0=tar_checksum((u_char *)&tarhd);
if (chksum0==0){ /* all zero block? */
/* XXX ignore */
continue; /* next */
}
/* checksum */
if ((sscanf(tarhd.chksum,"%o",&chksum)!=1)||(chksum!=chksum0)){
printf("checksum error in next header\n");
ret=-1;
goto tar_import_done;
}
/* get size */
byteend=0; /* default */
sscanf(tarhd.size,"%o",&byteend);
/* Note: if sscanf finds nothing, vtype remains! */
/* skip rest of full TAR_BLOCKSIZE */
skip=byteend%TAR_BLOCKSIZE;
skip=(TAR_BLOCKSIZE-skip)%TAR_BLOCKSIZE;
/* check type */
if ((tarhd.type!=TAR_TYPE_DIR)
&&(tarhd.type!=TAR_TYPE_REG)
&&(tarhd.type!=TAR_TYPE_REG0)){
skip+=byteend; /* skip file */
continue; /* next */
}
/* now, directory or regular file */
/* get name */
if (verbose){
printf("%s\n",tarhd.name);
}
l=(u_int)strlen(tarhd.name);
/* correct name if directory */
if ((tarhd.type==TAR_TYPE_DIR)&&(l>0)&&(tarhd.name[l-1]=='/')){
tarhd.name[l-1]='\0'; /* terminate */
l--;
}
if (l==0){
if (verbose){
printf("empty name, skipping file\n");
}
skip+=byteend; /* skip file */
continue; /* next */
}
/* XXX ignore other tarhd elements */
/* change to upper level directory */
/* Note: will be undone upon exit or next for-loop iteration */
save_curdir(1); /* 1: could be modifications */
if (change_curdir(tarhd.name,0,dirnamebuf,0)<0){
fprintf(stderr,"directory not found\n");
ret=-1;
goto tar_import_done;
}
/* parse type further */
if (tarhd.type==TAR_TYPE_DIR){
/* directory */
/* check if on disk level */
if (curpartp==NULL){
/* on disk level */
/* Note: no need to create partition, but must check if exists!!! */
if (akai_find_part(curdiskp,dirnamebuf)==NULL){
fprintf(stderr,"partition does not exist\n");
ret=-1;
goto tar_import_done;
}
}else{
if (curpartp->type==PART_TYPE_DD){
/* harddisk DD partition */
fprintf(stderr,"no directory allowed in DD-partition\n");
ret=-1;
goto tar_import_done;
}
/* now, sampler partition */
if (curvolp!=NULL){ /* already in volume? */
fprintf(stderr,"no directory allowed in volume\n");
ret=-1;
goto tar_import_done;
}
/* load number */
lnum=AKAI_VOL_LNUM_OFF; /* default */
/* XXX use devminor field for load number */
sscanf(tarhd.devminor,"%o",&lnum);
/* Note: if sscanf finds nothing, lnum remains! */
/* correct lnum */
if ((lnum!=AKAI_VOL_LNUM_OFF)
&&((lnum<AKAI_VOL_LNUM_MIN)||(lnum>AKAI_VOL_LNUM_MAX))){
lnum=AKAI_VOL_LNUM_OFF; /* XXX default */
}
/* volume parameters */
parp=NULL; /* default volume parameters or keep old ones */
#ifndef TAR_NOVOLPARAMTARHD
/* XXX use linkname for volume parameters */
if ((tarhd.linkname[0]=='\0')&&(tarhd.linkname[1]=='P')){ /* XXX correct magic? */
parp=(struct akai_volparam_s *)(&tarhd.linkname[0]+2); /* XXX +2: linkname starts with magic */
}
#endif
/* on partition level */
/* check if destination volume already exists */
if (akai_find_vol(curpartp,&tmpvol,dirnamebuf)<0){
/* does not exist (or error in finding it, don't care) */
/* user-supplied volume type, Note: will be corrected below if bad or inactive */
vtype=vtype0;
if (vtype0==AKAI_VOL_TYPE_INACT){ /* no user-supplied type? */
/* XXX use devmajor field for type */
sscanf(tarhd.devmajor,"%o",&vtype);
/* Note: if sscanf finds nothing, vtype remains! */
/* correct vtype */
if (curpartp->type==PART_TYPE_HD9){
vtype=AKAI_VOL_TYPE_S900;
}else if ((vtype!=AKAI_VOL_TYPE_S1000)
&&(vtype!=AKAI_VOL_TYPE_S3000)
&&(vtype!=AKAI_VOL_TYPE_CD3000)){
vtype=AKAI_VOL_TYPE_S1000; /* XXX default */
}
}
/* create volume */
if (akai_create_vol(curpartp,&tmpvol,vtype,AKAI_CREATE_VOL_NOINDEX,dirnamebuf,lnum,parp)<0){
fprintf(stderr,"cannot create volume\n");
ret=-1;
goto tar_import_done;
}
}else{
/* volume already exists */
/* Note: keep volume type (even if user-supplied type) */
/* XXX use devminor field for load number */
if (tarhd.devminor[0]==0x00){ /* no load number? */
lnum=tmpvol.lnum; /* keep old value */
}
osver=tmpvol.osver; /* keep old value */
/* update load number and optional volume parameters */
if (akai_rename_vol(&tmpvol,NULL,lnum,osver,parp)<0){
fprintf(stderr,"cannot update volume\n");
ret=-1;
goto tar_import_done;
}
}
if ((tmpvol.param!=NULL)&&(parp!=NULL)&&verbose){
printf("volume parameters imported\n");
}
}
skip+=byteend; /* skip file */
continue; /* next */
}
/* now, regular file */
#ifndef TAR_NOTAGSFILE
/* handle tags-file */
if (strcmp(dirnamebuf,TAR_TAGSFILENAME)==0){
if ((curpartp!=NULL)&&(curpartp->type==PART_TYPE_HD)&&(curvolp==NULL)){ /* in S1000/S3000 harddisk partition on partition level? */
if (byteend==(4+AKAI_PARTHEAD_TAGNUM*AKAI_NAME_LEN)){ /* valid file size? (Note: tags magic and tag names) */
/* read tags-file into partition header */
if (READ(fd,(void *)curpartp->head.hd.tagsmagic,byteend)!=(int)byteend){
perror("read");
ret=-1;
goto tar_import_done;
}
/* XXX no check if valid tags magic */
/* write partition header */
if (akai_io_blks(curpartp,(u_char *)&curpartp->head.hd,
0,
AKAI_PARTHEAD_BLKS,
1,IO_BLKS_WRITE)<0){ /* 1: allocate cache if possible */
fprintf(stderr,"cannot write partition header\n");
ret=-1;
goto tar_import_done;
}
if (verbose){
printf("tags imported\n");
}
}else{
if (verbose){
printf("invalid file size of tags-file, skipping file\n");
}
skip+=byteend; /* skip file */
continue; /* next */
}
}else{
if (verbose){
printf("tags-file must be on partition level of S1000/S3000 harddisk, skipping file\n");
}
skip+=byteend; /* skip file */
continue; /* next */
}
continue; /* next */
}
#endif
#ifndef TAR_NOVOLPARAMFILE
/* handle volparam-file */
if (strcmp(dirnamebuf,TAR_VOLPARAMFILENAME)==0){
if ((curvolp!=NULL)&&(curvolp->param!=NULL)){ /* on volume level and volume has parameters? */
if (byteend==sizeof(struct akai_volparam_s)){ /* valid file size? */
/* read volparam-file into volume */
if (READ(fd,(void *)curvolp->param,byteend)!=(int)byteend){
perror("read");
ret=-1;
goto tar_import_done;
}
/* update volume */
if (akai_rename_vol(curvolp,NULL,curvolp->lnum,curvolp->osver,curvolp->param)<0){
fprintf(stderr,"cannot update volume\n");
ret=-1;
goto tar_import_done;
}
if ((curvolp->param!=NULL)&&verbose){
printf("volume parameters imported\n");
}
}else{
if (verbose){
printf("invalid file size of volparam-file, skipping file\n");
}
skip+=byteend; /* skip file */
continue; /* next */
}
}else{
if (verbose){
if (curvolp==NULL){
printf("volparam-file must be on volume level, skipping file\n");
}else{
printf("volume has no parameters, skipping file\n");
}
}
skip+=byteend; /* skip file */
continue; /* next */
}
continue; /* next */
}
#endif
#ifndef TAR_NOVOLINFO1FILE
#ifndef TAR_VOLINFO1FILENAME
#define TAR_VOLINFO1FILENAME "VOLINFO1.DAT"
#endif
#ifndef TAR_VOLINFO1FILE_SKIPB
#define TAR_VOLINFO1FILE_SKIPB 0x0010 /* number of bytes to skip in volinfo1-file */
#endif
/* handle volinfo1-file */
if (strcmp(dirnamebuf,TAR_VOLINFO1FILENAME)==0){
if ((curvolp!=NULL)&&(curvolp->param!=NULL)){ /* on volume level and volume has parameters? */
if (byteend==(TAR_VOLINFO1FILE_SKIPB+sizeof(struct akai_volparam_s))){ /* valid file size? */
if (LSEEK(fd,TAR_VOLINFO1FILE_SKIPB,SEEK_CUR)<0){
perror("lssek");
ret=-1;
goto tar_import_done;
}
/* read volume parameters from volinfo1-file into volume */
l=sizeof(struct akai_volparam_s);
if (READ(fd,(void *)curvolp->param,l)!=(int)l){
perror("read");
ret=-1;
goto tar_import_done;
}
/* update volume */
if (akai_rename_vol(curvolp,NULL,curvolp->lnum,curvolp->osver,curvolp->param)<0){
fprintf(stderr,"cannot update volume\n");
ret=-1;
goto tar_import_done;
}
if (verbose){
printf("volume parameters imported\n");
}
}else{
if (verbose){
printf("invalid file size of volinfo1-file, skipping file\n");
}
skip+=byteend; /* skip file */
continue; /* next */
}
}else{
if (verbose){
if (curvolp==NULL){
printf("volinfo1-file must be on volume level, skipping file\n");
}else{
printf("volume has no parameters, skipping file\n");
}
}
skip+=byteend; /* skip file */
continue; /* next */
}
continue; /* next */
}
#endif
#ifndef TAR_NOSKIPDATFILE
/* skip dat-files */
l=(u_int)strlen(dirnamebuf);
if ((l>=4)&&((strcmp(dirnamebuf+l-4,".dat")==0)||(strcmp(dirnamebuf+l-4,".DAT")==0))){
if (verbose){
printf("skipping dat-file\n");
}
skip+=byteend; /* skip file */
continue; /* next */
}
#endif
if (curpartp==NULL){
fprintf(stderr,"no files allowed on disk level\n");
ret=-1;
goto tar_import_done;
}
if (curpartp->type==PART_TYPE_DD){
/* harddisk DD partition */
if (flags&TAR_IMPORT_WAV){
u_int ti;
u_char cstarts;
/* find free index */
for (ti=0;ti<AKAI_DDTAKE_MAXNUM;ti++){
/* take */
cstarts=(curpartp->head.dd.take[ti].cstarts[1]<<8)
+curpartp->head.dd.take[ti].cstarts[0];
if (cstarts==0){ /* free? */
break; /* done */
}
}
if (ti==AKAI_DDTAKE_MAXNUM){ /* no free index? */
fprintf(stderr,"no free take index\n");
ret=-1;
goto tar_import_done;
}
/* Note: don't check if name already used, create new DD take */
if (akai_wav2take(fd,dirnamebuf,
curpartp,
ti,
&wavbcount,
0)<0){
return -1;
}
if (byteend<wavbcount){
return -1;
}
skip+=byteend-wavbcount; /* skip unread rest */
}else{
/* import DD take */
u_int ti;
u_int cstarts;
u_int csizes,csizee;
u_int tsize;
struct akai_ddtake_s t;
/* check file name */
if ((strlen(dirnamebuf)<3)
||(strcmp(dirnamebuf+strlen(dirnamebuf)-3,AKAI_DDTAKE_FNAMEEND)!=0)){
fprintf(stderr,"invalid file name for DD take, skipping file\n");
skip+=byteend; /* skip file */
continue; /* next */
}
/* find free index */
for (ti=0;ti<AKAI_DDTAKE_MAXNUM;ti++){
/* take */
cstarts=(curpartp->head.dd.take[ti].cstarts[1]<<8)
+curpartp->head.dd.take[ti].cstarts[0];
if (cstarts==0){ /* free? */
break; /* done */
}
}
if (ti==AKAI_DDTAKE_MAXNUM){ /* no free index? */
fprintf(stderr,"no free take index\n");
ret=-1;
goto tar_import_done;
}
/* get directory entry */
if (READ(fd,&t,sizeof(struct akai_ddtake_s))!=(int)sizeof(struct akai_ddtake_s)){
perror("read");
ret=-1;
goto tar_import_done;
}
/* get csizes (sample clusters) and csizee (envelope clusters) */
/* XXX use cstarts/cstarte fields in DD take header for csizes/csizee */
csizes=(t.cstarts[1]<<8)
+t.cstarts[0];
csizee=(t.cstarte[1]<<8)
+t.cstarte[0];
/* Note: akai_import_take() will increase csizee if required */
/* XXX preliminary check of csizes+csizee */
if ((csizes+csizee)*AKAI_DDPART_CBLKS>curpartp->bfree){
fprintf(stderr,"not enough space left\n");
ret=-1;
goto tar_import_done;