summaryrefslogtreecommitdiff
path: root/wrudf/wrudf-cmnd.c
blob: e861d82e6bcf8a16f50753c8df292bf089803347 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
/* 	wrudf-cmnd.c
 *
 * PURPOSE
 *	High level wrudf command functions
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *
 *	(C) 2001 Enno Fennema <e.fennema@dataweb.nl>
 *
 * HISTORY
 *	16 Aug 01  ef  Created.
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>

#include "wrudf.h"


int	deleteDirectory(Directory *dir, struct fileIdentDesc *fid);
Directory *makeDir(Directory *dir, char* name);
int	questionOverwrite(Directory *dir, struct fileIdentDesc *fid, char* name);
int	directoryIsEmpty(Directory *dir);

char	*hdWorkingDir;

/*	copyFile()
 *	Write File Entry immediately followed by data
 *	A verify error on CDR causes a further packet with
 *	the new FE followed by copies of the defective blocks only
 */
int
copyFile(Directory *dir, char* inName, char*newName, struct stat *fileStat) 
{
    uint32_t	i=0;
    int		fd, blkno;
    uint32_t	nBytes, blkInPkt;
    uint32_t	maxVarPktSize;		// in bytes
    struct fileIdentDesc *fid;
    struct fileEntry *fe;
    struct allocDescImpUse *adiu;
    struct logicalVolIntegrityDescImpUse *lvidiu;
    uint8_t	p[2048];

    fd = open(inName, O_RDONLY);
    if( fd == 0 ) {
	printf("'%s' does not exist\n", inName);
	return CMND_FAILED;
    }

    printf("Copy file %s\n", inName);
    fid = findFileIdentDesc(dir, newName);

    if( fid  && questionOverwrite(dir, fid, newName) ) {
	close(fd);
	return CMND_OK;
    }

    fid = makeFileIdentDesc(newName);				// could reuse FID if overwrite allowed
	    
    fe = makeFileEntry();
    fe->uid = fileStat->st_uid;
    fe->gid = fileStat->st_gid;
    if( fileStat->st_mode & S_IRUSR ) fe->permissions |= FE_PERM_U_READ;
    if( fileStat->st_mode & S_IWUSR ) fe->permissions |= FE_PERM_U_WRITE | FE_PERM_U_DELETE | FE_PERM_U_CHATTR;;
    if( fileStat->st_mode & S_IXUSR ) fe->permissions |= FE_PERM_U_EXEC;
    if( fileStat->st_mode & S_IRGRP ) fe->permissions |= FE_PERM_G_READ;
    if( fileStat->st_mode & S_IWGRP ) fe->permissions |= FE_PERM_G_WRITE | FE_PERM_G_DELETE | FE_PERM_G_CHATTR;
    if( fileStat->st_mode & S_IXGRP ) fe->permissions |= FE_PERM_G_EXEC;
    if( fileStat->st_mode & S_IROTH ) fe->permissions |= FE_PERM_O_READ;
    if( fileStat->st_mode & S_IWOTH ) fe->permissions |= FE_PERM_O_WRITE | FE_PERM_O_DELETE | FE_PERM_O_CHATTR;;
    if( fileStat->st_mode & S_IXOTH ) fe->permissions |= FE_PERM_O_EXEC;
    if( fileStat->st_mode & S_ISUID ) fe->icbTag.flags |= ICBTAG_FLAG_SETUID;
    if( fileStat->st_mode & S_ISGID ) fe->icbTag.flags |= ICBTAG_FLAG_SETGID;

    fe->informationLength = fileStat->st_size;
    updateTimestamp(fileStat->st_atime, 0);
    fe->accessTime = timeStamp;
    updateTimestamp(fileStat->st_mtime, 0);
    fe->modificationTime = timeStamp;
    updateTimestamp(fileStat->st_ctime, 0);
    fe->attrTime = timeStamp;

    /* check whether embedding of data is possible  */
    fe->logicalBlocksRecorded = ((fileStat->st_size + 2047) & ~2047) >> 11;

    if( medium == CDR ) {
	/*	File data written in physical space but fileEntry in virtual space
	 *	so must use long allocation descriptors
	 *
	 *	Variable packet length restricted by drive buffer size.
	 *	Must break up long file over several var packets.
	 *	Only 116 long_ad's fit in fileEntry without going to extended alloc descs
	 */
	long_ad	*ad;
	uint32_t	loc;

	maxVarPktSize = getMaxVarPktSize();
	
	if( fe->informationLength / maxVarPktSize > 116 ) {
	    printf("Cannot handle files longer than %d\n", 116 * maxVarPktSize);
	    close(fd);
	    free(fe);
	    free(fid);
	    return CMND_FAILED;
	}

	fe->icbTag.flags |= ICBTAG_FLAG_AD_LONG;
	/* +1 as the fileEntry itself occupies block NWA */
	loc = getNWA() + 1 - pd->partitionStartingLocation;
	nBytes = (uint32_t) fe->informationLength ;

	for( ad = (long_ad*)(fe->allocDescs + fe->lengthExtendedAttr); nBytes > 0; ad++ ) {
	    if( nBytes > maxVarPktSize ) {
		ad->extLength = maxVarPktSize;
		nBytes -= maxVarPktSize;
	    } else {
		ad->extLength = nBytes;
		nBytes = 0;
	    }
	    ad->extLocation.logicalBlockNum = loc;
	    ad->extLocation.partitionReferenceNum = pd->partitionNumber;
	    adiu = (struct allocDescImpUse*)(ad->impUse);
	    memcpy(&adiu->impUse, &fe->uniqueID, sizeof(uint32_t));
	    loc += ((ad->extLength + 2047) >> 11) + 7;
	}

	if( ((ad-1)->extLength & 2047) == 0 ) {
	    memset(ad, 0, sizeof(long_ad));
	    adiu = (struct allocDescImpUse*)(ad->impUse);
	    memcpy(&adiu->impUse, &fe->uniqueID, sizeof(uint32_t));
	    ad++;
	}

	fe->lengthAllocDescs = (uint8_t*)ad - (fe->allocDescs + fe->lengthExtendedAttr);
	fe->descTag.descCRCLength = sizeof(struct fileEntry) + 
	    fe->lengthExtendedAttr + fe->lengthAllocDescs - sizeof(tag);
	fe->descTag.tagLocation = newVATentry();

	for( ;; ) {	    					/* retry loop for verify failure */
	    int		blknoFE, skipped, retries;
	    long_ad	*extent;

	    setChecksum(fe);
	    skipped = 0;
	    retries = 0;
	    lseek(fd, 0, SEEK_SET);

	    /* write FE */
	    blknoFE = vat[fe->descTag.tagLocation] = writeCDR(fe) - pd->partitionStartingLocation;
	    fid->icb.extLocation.logicalBlockNum = fe->descTag.tagLocation; 
	    fid->icb.extLocation.partitionReferenceNum = virtualPartitionNum;

	    /* write file data */
	    extent = (long_ad*)(fe->allocDescs + fe->lengthExtendedAttr);

	    for( blkInPkt = nBytes = 0; nBytes < fe->informationLength; extent++ ) {
		blkno = extent->extLocation.logicalBlockNum;
		if( blkno < blknoFE ) {
		    nBytes += extent->extLength;
		    skipped = 1;
		    continue;
		}
		for( i = 0; i < extent->extLength; blkno++, i += 2048 ) {
		    memset(p, 0, 2048);
		    if( skipped ) {
			lseek(fd, nBytes, SEEK_SET);
			skipped = 0;
		    }
		    nBytes += read(fd, p, 2048);
		    writeCDR(p);
		    if( ++blkInPkt == (maxVarPktSize >> 11) ) {
			syncCDR();
			blkInPkt = 0;
		    }
		}	
	    }

	    syncCDR();
	    if( verifyCDR(fe) == 0 ) 			// verify the file data
		break;

	    if( retries++ > 3 ) {
		printf("Retry count 3 exceeded\n");
		break;
	    }
	}
    } else {
	short_ad	*extent, extentFE[2];

	if( getExtents(2048, extentFE ) != 16 ) {		/* extent for File Entry */
	    printf("No space for File Entry\n");
	    close(fd);
	    free(fe);
	    free(fid);
	    return CMND_FAILED;
	}
	fe->descTag.tagLocation = extentFE[0].extPosition;
	// must mark allocated; otherwise allocated for a second time before being written
	markBlock(ALLOC, fe->descTag.tagLocation);

	fe->lengthAllocDescs = 					/* extents for the file data */
	    getExtents(fe->informationLength, (short_ad*)(fe->allocDescs + fe->lengthExtendedAttr));
	if( !fe->lengthAllocDescs ) {
	    printf("No space for file\n");
	    close(fd);
	    free(fe);
	    free(fid);
	    return CMND_FAILED;
	}
	/* write FE */
	fe->descTag.descCRCLength = sizeof(struct fileEntry) + 
	    fe->lengthExtendedAttr + fe->lengthAllocDescs - sizeof(tag);
	setChecksum(fe);
	writeBlock(fe->descTag.tagLocation, pd->partitionNumber, fe);
	fid->icb.extLocation.logicalBlockNum = fe->descTag.tagLocation;
	fid->icb.extLocation.partitionReferenceNum = pd->partitionNumber;

	/* write file data */
	extent = (short_ad*)(fe->allocDescs + fe->lengthExtendedAttr);
	for( nBytes = 0; nBytes < fe->informationLength; extent++ ) {
	    blkno = extent->extPosition;
	    for( i = 0; i < extent->extLength; blkno++, i += 2048 ) {
		memset(p, 0, 2048);
		nBytes += read(fd, p, 2048);
		writeBlock(blkno, pd->partitionNumber, p);
	    }	
	}
    }

    if( devicetype == DISK_IMAGE && medium == CDR )
	writeHDlink();

    adiu = (struct allocDescImpUse*)(fid->icb.impUse);
    memcpy(&adiu->impUse, &fe->uniqueID, sizeof(uint32_t));
    insertFileIdentDesc(dir, fid);

    lvidiu = (struct logicalVolIntegrityDescImpUse*)
	(lvid->impUse + 2 * sizeof(uint32_t) * lvid->numOfPartitions);
    lvidiu->numFiles++;

    close(fd);
    free(fe);
    free(fid);
    return CMND_OK;
}


int
copyDirectory(Directory *dir, char* name) 
{
    DIR		*srcDir;
    Directory	*workDir;
    struct dirent *dirEnt;
    struct stat dirEntStat;
    struct fileIdentDesc *fid;

    if( !(srcDir = opendir(name)) ) {
	printf("Open dir '%s': %m\n", name);
	return CMND_FAILED;
    }

    if( chdir(name) != 0 ) {
	printf("Change dir '%s': %m\n", name);
	closedir(srcDir);
	return CMND_FAILED;
    }

    printf("Now in %s\n", getcwd(NULL, 0));
    workDir = dir;

    while( (dirEnt = readdir(srcDir)) ) {
	if( !strcmp(dirEnt->d_name, ".") || !strcmp(dirEnt->d_name, "..") )
	    continue;
	
	if( lstat(dirEnt->d_name, &dirEntStat) != 0 ) {		// do not follow links
	    printf("Stat dirEnt '%s' failed: %s\n", dirEnt->d_name, strerror(errno));
	    continue;
	}

	if( S_ISDIR(dirEntStat.st_mode) ) {
	    if( !(options & OPT_RECURSIVE) ) {
		printf("Not recursive. Ignoring '%s' directory\n", dirEnt->d_name);
		continue;
	    }
	    fid = findFileIdentDesc(dir, dirEnt->d_name);

	    if( fid  && !(fid->fileCharacteristics & FID_FILE_CHAR_DIRECTORY ) ) {
		printf("'%s' exists but is not a directory\n", dirEnt->d_name);
		continue;
	    }
	    if( fid && (fid->fileCharacteristics & FID_FILE_CHAR_DELETED) ) {
		removeFID(dir, fid);
		fid = NULL;
	    }
	    if( !fid )
		workDir = makeDir(dir, dirEnt->d_name);
	    else
		workDir = readDirectory(dir, &fid->icb, dirEnt->d_name);
	    copyDirectory(workDir, dirEnt->d_name);
	} else {
	    if( S_ISREG(dirEntStat.st_mode) )
		copyFile(workDir, dirEnt->d_name, dirEnt->d_name, &dirEntStat);
	}
    }

    if( chdir("..") != 0 )
	printf("Change dir '..': %m\n");

    closedir(srcDir);
    return CMND_OK;
}


/*	deleteDirectory()
 *
 *	Recursing into subdirectories.
 *	If anywhere no permission, query deletion but proceed if 'yes'
 *	Returns 0 if fid freed, 1 if fid not empty
 */
int 
deleteDirectory(Directory *dir, struct fileIdentDesc* fid) 
{
    uint64_t		i;
    int			rv, notEmpty;
    char		*name;
    struct fileIdentDesc *childFid;
    Directory		*childDir;
    
    rv = 0;

    if( fid->fileCharacteristics & FID_FILE_CHAR_DELETED ) 
	return CMND_OK;

    if( (fid->fileCharacteristics & FID_FILE_CHAR_DIRECTORY) == 0 ) 
	return deleteFID(dir, fid);			/* delete regular files */

    name = malloc(fid->lengthFileIdent + 1);
    strncpy(name, (char *)(fid->fileIdent + fid->lengthOfImpUse), fid->lengthFileIdent);
    name[fid->lengthFileIdent] = 0;
    readDirectory(dir, &fid->icb, name);
    childDir = dir->child;

    /* check permission */

    notEmpty = 0;

    for( i = 0; i < childDir->fe.informationLength;
	 i += ((sizeof(struct fileIdentDesc) + childFid->lengthOfImpUse + childFid->lengthFileIdent + 3) & ~3) ) 
    {
	childFid = (struct fileIdentDesc*)(childDir->data + i);
	if( childFid->fileCharacteristics & (FID_FILE_CHAR_DELETED | FID_FILE_CHAR_PARENT) ) 
	    continue;
	if( childFid->fileCharacteristics & FID_FILE_CHAR_DIRECTORY )
	    deleteDirectory( childDir, childFid);
	else
	    deleteFID(childDir, childFid);
    }

    if( directoryIsEmpty(childDir) ) {
	rv |= deleteFID(dir, fid);
    } else {
	childDir->dirDirty = 1;
	updateDirectory(childDir);
	rv = notEmpty;
    }
    free(name);
    return rv;
}


/*	readDirectory()
 *	All fileIdentDesc's are put into the data area of the Directory structure.
 *	irrespective whether they were embedded or separately in allocated extents.
 *	updateDirectory() will embed or write in separate extents as appropriate.
 */
Directory * 
readDirectory(Directory *parentDir, long_ad* icb, char *name) 
{
    char	*p;
    uint32_t	len;
    Directory	*dir;

    if( parentDir == NULL ) 
	dir = rootDir;
    else {
	dir = parentDir->child;
	if( dir == NULL ) {
	    parentDir->child = dir = (Directory*)malloc(sizeof(Directory));
	    memset(dir, 0, sizeof(Directory));
	    dir->parent = parentDir;
	    dir->dataSize = 4096;
	    dir->data = malloc(4096);
	    if( dir->name ) free(dir->name);
	    dir->name = malloc(strlen(name) + 1);
	    strcpy(dir->name, name);
	} else 
	    if( dir->icb.extLocation.logicalBlockNum == icb->extLocation.logicalBlockNum
		&& dir->icb.extLocation.partitionReferenceNum == icb->extLocation.partitionReferenceNum )
		return dir;		// already the one requested
    }

    if( dir->dirDirty )
	updateDirectory(dir);

    dir->icb = *icb;
    if( name[0] ) {
	if( dir->name ) free(dir->name);
	dir->name = malloc(strlen(name) + 1);
	strcpy(dir->name, name);
    }
    p = readTaggedBlock(icb->extLocation.logicalBlockNum, icb->extLocation.partitionReferenceNum);
    memcpy(&dir->fe, p, 2048);

    if( (dir->fe.icbTag.flags & ICBTAG_FLAG_AD_MASK) == ICBTAG_FLAG_AD_IN_ICB ) {
	memcpy(dir->data, dir->fe.allocDescs + dir->fe.lengthExtendedAttr, dir->fe.lengthAllocDescs);
	memset(dir->fe.allocDescs + dir->fe.lengthExtendedAttr, 0, dir->fe.lengthAllocDescs);
    } else {
	if( dir->fe.informationLength > dir->dataSize ) {
	    len = (dir->fe.informationLength + 2047) & ~2047;
	    dir->data = realloc(dir->data, len);
	    if( !dir->data ) {
		printf("Realloc directory data failed\n");
		return NULL;
	    }
	    dir->dataSize = len;
	}
	readExtents(dir->data, (dir->fe.icbTag.flags & ICBTAG_FLAG_AD_MASK) == ICBTAG_FLAG_AD_SHORT,
	    dir->fe.allocDescs + dir->fe.lengthExtendedAttr);
	if( medium == CDRW ) {
	    if( (dir->fe.icbTag.flags & ICBTAG_FLAG_AD_MASK) == ICBTAG_FLAG_AD_SHORT )
		freeShortExtents((short_ad*)(dir->fe.allocDescs + dir->fe.lengthExtendedAttr));
	    else if( (dir->fe.icbTag.flags & ICBTAG_FLAG_AD_MASK) == ICBTAG_FLAG_AD_LONG )
		freeLongExtents((long_ad*)(dir->fe.allocDescs + dir->fe.lengthExtendedAttr));
	}
    }
    printf("Read dir %s\n", dir->name);
    return dir;
}


/*	updateDirectory()
 *	Extents and ICB AllocMask are unchanged since reading the directory from CD.
 *	Based on current informationLength will have to decide embedding or on output extents required
 *	and free any superfluous extents
 */
int 
updateDirectory(Directory* dir) 
{
    uint64_t i;
    struct fileIdentDesc *fid;

    if( dir->child )
	updateDirectory(dir->child);

    if( !dir->dirDirty )
	return CMND_OK;

    if( sizeof(struct fileEntry) + dir->fe.lengthExtendedAttr + dir->fe.informationLength <= 2048 ) {			
        /* fileIdentDescs embedded in directory ICB */
	dir->fe.logicalBlocksRecorded = 0;
	dir->fe.icbTag.flags = (dir->fe.icbTag.flags & ~ICBTAG_FLAG_AD_MASK) | ICBTAG_FLAG_AD_IN_ICB;
	dir->fe.lengthAllocDescs = dir->fe.informationLength;
	memcpy(dir->fe.allocDescs + dir->fe.lengthExtendedAttr, dir->data, dir->fe.lengthAllocDescs);

	/* UDF2.00 2.2.1.3  - For a structure in virtual space, tagLocation is the virtual location */
	for( i = 0; i < dir->fe.informationLength;
	     i += (sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3 ) 
	{
	    fid = (struct fileIdentDesc*) (dir->fe.allocDescs + i);
	    fid->descTag.tagLocation = dir->icb.extLocation.logicalBlockNum;
	    setChecksum(&fid->descTag);
	}
    } else {
	/* get new extents for the directory data */
	dir->fe.logicalBlocksRecorded = ((dir->fe.informationLength + 2047) & ~2047) >> 11;

	if( medium == CDR ) {
	    long_ad *ad;
	    struct allocDescImpUse *adiu;
	    uint32_t	blkno;

	    dir->fe.icbTag.flags = (dir->fe.icbTag.flags & ~ ICBTAG_FLAG_AD_MASK) | ICBTAG_FLAG_AD_LONG;
	    ad = (long_ad*)(dir->fe.allocDescs + dir->fe.lengthExtendedAttr);
	    ad->extLength = dir->fe.informationLength;
	    ad->extLocation.logicalBlockNum  =  blkno = getNWA() + 1 - pd->partitionStartingLocation;
	    ad->extLocation.partitionReferenceNum = pd->partitionNumber;
	    adiu = (struct allocDescImpUse*)(ad->impUse);
	    memcpy(&adiu->impUse, &dir->fe.uniqueID, sizeof(uint32_t));
	    memset(ad + 1, 0, sizeof(long_ad));			/* necessary only if infolength multiple of 2048 */
	    dir->fe.lengthAllocDescs = 2 * sizeof(long_ad);

	    /* set tagLocation in all FIDs */
	    for( i = 0; i < dir->fe.informationLength;
		 i += (sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3 ) 
	    {
		fid = (struct fileIdentDesc*) (dir->data + i);
		fid->descTag.tagLocation = blkno + (i >> 11);
		fid->descTag.descCRCLength = 
		    ((sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3) - sizeof(tag);
		setChecksum(fid);
	    }

	} else {
	    uint32_t	*blocks, blkno, len;
	    short_ad	*extent;

	    extent =(short_ad*)(dir->fe.allocDescs + dir->fe.lengthExtendedAttr);
	    dir->fe.lengthAllocDescs = getExtents(dir->fe.informationLength, extent);
	    dir->fe.icbTag.flags = (dir->fe.icbTag.flags & ~ICBTAG_FLAG_AD_MASK) | ICBTAG_FLAG_AD_SHORT;

	    /* find which blocks are to going be used to set tagLocations */
	    blocks = (uint32_t*)malloc(dir->fe.logicalBlocksRecorded * sizeof(uint32_t));
	    blkno = extent->extPosition;
	    len = extent->extLength;
	    for( i = 0; i < dir->fe.logicalBlocksRecorded; i++ ) {
		blocks[i] = blkno;
		if( len <= 2048 ) {
		    extent++;
		    blkno = extent->extPosition;
		    len = extent->extLength;
		} else {
		    len -= 2048;
		    blkno++;
		}
	    }
	    /* set tagLocation */
	    for( i = 0; i < dir->fe.informationLength;
		 i += (sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3 ) 
	    {
		fid = (struct fileIdentDesc*) (dir->data + i);
		fid->descTag.tagLocation = blocks[i >> 11];
		fid->descTag.descCRCLength = 
		    ((sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3) - sizeof(tag);
		setChecksum(fid);
	    }
	    free(blocks);
	}
    }

    dir->fe.descTag.descCRCLength = 
	sizeof(struct fileEntry) + dir->fe.lengthExtendedAttr + dir->fe.lengthAllocDescs - sizeof(tag);
    setChecksum(&dir->fe);

    if( medium == CDRW ) {
	/* write the directory fileEntry */
	writeBlock(dir->icb.extLocation.logicalBlockNum, dir->icb.extLocation.partitionReferenceNum, &dir->fe);

	if( dir->fe.logicalBlocksRecorded  ) {
	    /* write any directory data */
	    writeExtents(dir->data, 1, (short_ad*)(dir->fe.allocDescs + dir->fe.lengthExtendedAttr));
	}
    } else {		/* medium == CDR */
	int retries;

	retries = 0;

	for( ;; ) {					/* loop only when verify failed */
	    uint32_t	pbn;

	    /* write the directory fileEntry */
	    pbn = writeCDR(&dir->fe);
	    vat[((long_ad*)&dir->icb)->extLocation.logicalBlockNum] = pbn - pd->partitionStartingLocation;

	    if( dir->fe.logicalBlocksRecorded )
		for( i = 0; i < dir->fe.informationLength; i += 2048 )
		    writeCDR(dir->data + i);

	    if( verifyCDR(&dir->fe) == 0 )
		break;

	    if( ++retries > 3 ) {
		printf("updateDirectory: '%s' failed\n", dir->name);
		return CMND_FAILED;
	    }
	}
    }
    dir->dirDirty = 0;
    printf("Wrote dir %s\n", dir->name);
    return CMND_OK;
}


/*	Create subdirectory called 'name' in 'dir' 
 */
Directory * 
makeDir(Directory *dir, char* name ) 
{
    Directory		*newDir;
    struct fileEntry	*fe;
    struct fileIdentDesc *backFid, *forwFid;
    struct allocDescImpUse *adiu;
    struct logicalVolIntegrityDescImpUse *lvidiu;
    short_ad		allocDescs[2];


    /* back reference to parent in new directory */
    backFid = makeFileIdentDesc("");
    backFid->icb = dir->icb;
    backFid->fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
    backFid->descTag.descCRCLength = ((sizeof(struct fileIdentDesc) + 3) & ~3) - sizeof(tag);

    fe = makeFileEntry();
    fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
    fe->icbTag.flags = ICBTAG_FLAG_AD_IN_ICB;
    fe->informationLength = (sizeof(struct fileIdentDesc) + 3) & ~3;

    /* forward reference to new directory in parent directory */
    forwFid = makeFileIdentDesc(name);
    forwFid->fileCharacteristics = FID_FILE_CHAR_DIRECTORY;
    adiu = (struct allocDescImpUse*)forwFid->icb.impUse;
    memcpy(&adiu->impUse, &fe->uniqueID, sizeof(uint32_t));

    if( medium == CDR ) {
	fe->descTag.tagLocation = newVATentry();
	forwFid->icb.extLocation.logicalBlockNum = fe->descTag.tagLocation;
	forwFid->icb.extLocation.partitionReferenceNum = virtualPartitionNum;
    } else {
	if(  getExtents( 2048, allocDescs) != 16 )
	    fail("makeDir: Could not get File Entry extent\n");

	markBlock(ALLOC, allocDescs[0].extPosition);
	fe->descTag.tagLocation = allocDescs[0].extPosition;
	backFid->descTag.tagLocation = allocDescs[0].extPosition;
	forwFid->icb.extLocation.logicalBlockNum = allocDescs[0].extPosition;
	forwFid->icb.extLocation.partitionReferenceNum = pd->partitionNumber;
	setChecksum(&backFid->descTag);
	fe->descTag.descCRCLength = sizeof(struct fileEntry) + fe->lengthAllocDescs - sizeof(tag);
	setChecksum(fe);
    }

    insertFileIdentDesc(dir, forwFid);
    dir->fe.fileLinkCount++;
    dir->dirDirty = 1;

    /* setup directory structure for new directory */
    newDir = dir->child;

    if( newDir != NULL ) 
	updateDirectory(newDir);
    else {
	newDir = (Directory*)malloc(sizeof(Directory));
	memset(newDir, 0, sizeof(Directory));
	newDir->parent = dir;
	newDir->dataSize = 4096;
	newDir->data = malloc(4096);
	dir->child = newDir;
    }
    
    memset(newDir->data, 0, newDir->dataSize);
    newDir->name = malloc(strlen(name)+1);
    strcpy(newDir->name, name);
    newDir->icb = forwFid->icb;
    memcpy(&newDir->fe, fe, 2048);
    memcpy(newDir->data, backFid, fe->informationLength);
    newDir->dirDirty = 1;

    lvidiu = (struct logicalVolIntegrityDescImpUse*)
	(lvid->impUse + 2 * sizeof(uint32_t) * lvid->numOfPartitions);
    lvidiu->numDirs++;

    free(backFid);
    free(forwFid);
    free(fe);
    return newDir;
}


/*	analyzeDest()
 *
 *	The last argument in a command is the destination for copying or the object
 *	to act on. The argument consists of / separated components 
 *	eg. /src/test/udf/wrudf.h or /src/test/iso/
 *	or just a single component eg. . or / or wrudf.c
 *
 *	All but the last component must be existing directories and curDir points to
 *	the last directopry in that chain, eg. udf, otherwise result DIR_INVALID.
 *
 *	When the last component does not exist, return result DOES_NOT_EXIST.
 *	name --> last component eg. wrudf.h or iso
 *
 *	If the last component identifies a directory return EXISTING_DIR or DELETED_DIR,
 *	*fid = NULL, *name = last component name eg. iso.
 *
 *	If the last component is a file  return EXISTING_FILE or DELETED_FILE.
 *	*fid points to FID entry in curDir with name equal the last component.
 *	*name --> last component name eg. wrudf.h
 */
enum RV 
analyzeDest(char* arg, struct fileIdentDesc** fid, char** name) 
{
    int		len;
    char 	*comp, *endComp;

    if( arg[0] == '/' ) {
	curDir = rootDir;
	comp = arg + 1;
    } else {
	comp = arg;
    }

    len = strlen(comp);
    if( len > 1 && comp[len-1] == '/' )
	comp[len-1] = 0;				/* remove any trailing slash */

    for(  ; ( endComp = strchr(comp, '/') ) != NULL; comp = ++endComp ) {
	*endComp = 0;

	if( strcmp(comp, ".") == 0 )
	    continue;

	if( strcmp(comp, "..") == 0 ) {
	    if( !curDir->parent )
		return DIR_INVALID;
	    curDir = curDir->parent;
	    continue;
	}
	
	*fid = findFileIdentDesc(curDir, comp);
	if( *fid == NULL )
	    return DIR_INVALID;
	if( ! ((*fid)->fileCharacteristics & FID_FILE_CHAR_DIRECTORY ))
	    return DIR_INVALID;
	if( (*fid)->fileCharacteristics & FID_FILE_CHAR_DELETED )
	    return DIR_INVALID;

//	curDir = readDirectory(curDir->child, &(*fid)->icb, comp); 
	curDir = readDirectory(curDir, &(*fid)->icb, comp); 
    }

    // final component
    *name = comp;

    if( comp[0] == 0 || strcmp(comp, ".") == 0 )
	return EXISTING_DIR;

    if( strcmp(comp, "..") == 0 ) {
	if( !curDir->parent )
	    return DIR_INVALID;
	curDir = curDir->parent;
	return EXISTING_DIR;
    }

    *fid = findFileIdentDesc(curDir, comp);

    if( *fid == NULL )
	return DOES_NOT_EXIST;

    if( (*fid)->fileCharacteristics & FID_FILE_CHAR_DIRECTORY ) {
	if( (*fid)->fileCharacteristics & FID_FILE_CHAR_DELETED )
	    return DELETED_DIR;
	else {
	    curDir = readDirectory(curDir, &(*fid)->icb, comp); 
	    return EXISTING_DIR;
	}
    } else {
	if( (*fid)->fileCharacteristics & FID_FILE_CHAR_DELETED )
	    return DELETED_FILE;
    }
    return EXISTING_FILE;    
}


int 
cpCommand(void) 
{
    int		i, rv;
    enum RV	state;
    char	*srcname, *name, *p;
    Directory	*cpyDir;
    struct fileIdentDesc *fid, *newFid;
    struct stat fileStat;

    if( cmndc < 2 )
	return WRONG_NO_ARGS;

    name = NULL;
    state = analyzeDest(cmndv[cmndc-1], &fid, &name);

    if( state == DIR_INVALID )
	return DIR_INVALID;

    if( state == DELETED_DIR ) {
	printf("Cannot reuse deleted directory name yet\n");
	return CMND_FAILED;
    }
    
    if( state == DELETED_FILE ) {
	removeFID(curDir, fid);
	state = DOES_NOT_EXIST;
    }
	
    if( state == EXISTING_FILE ) {
	if( questionOverwrite(curDir, fid, name) )
	    return CMND_FAILED;
	state = DOES_NOT_EXIST;
    }

    /* do I have write permission for the destination directory ? */

    /* process source arguments */
    for( i = 0; i < cmndc - 1; i++ ) {
	p = strrchr(cmndv[i], '/');

	if( p && p > cmndv[i] && *(p+1) == 0 ) *p = 0;	// remove any trailing slash (except in "/")

	p = strrchr(cmndv[i], '/');

	if( p ) {
	    srcname = p+1;
	} else
	    srcname = cmndv[i];

	if( (rv = lstat(cmndv[i], &fileStat)) < 0 ) {	// do not follow soft links
	    printf("stat failed on %s\n", cmndv[i]);
	    continue;
	}

	if( S_ISDIR(fileStat.st_mode) ) {
	    if( state == EXISTING_DIR ) {
		newFid = findFileIdentDesc(curDir, srcname);
		if( newFid == NULL ) {
		    cpyDir = makeDir(curDir, srcname);
		    newFid = findFileIdentDesc(curDir, srcname);
		} else {
		    if( newFid->fileCharacteristics != FID_FILE_CHAR_DIRECTORY ) {
			printf("Destination is not a directory\n");
			return CMND_FAILED;
		    }
		    cpyDir = readDirectory(curDir, &newFid->icb, srcname);
		}
		if( chdir(cmndv[i]) != 0 )
			printf("Change dir '%s': %m\n", cmndv[i]);
		else
			copyDirectory(cpyDir, ".");
	    } else 
		printf("Destination is not a directory\n");

	    if( chdir(hdWorkingDir) != 0 ) {
		printf("Change dir '%s': %m\n", hdWorkingDir);
		return CMND_FAILED;
	    }
	    continue;
	}

	if( !S_ISREG(fileStat.st_mode) ) {
	    printf("Can only copy regular files or directories\n");
	    return CMND_FAILED;
	}

	if( state == DOES_NOT_EXIST || state == EXISTING_DIR )
	    name = srcname;

	copyFile(curDir, cmndv[i], name, &fileStat);
    }
    return CMND_OK;
}


int 
rmCommand(void) 
{
    enum RV		state;
    int			i;
    struct fileIdentDesc *fid;
    char*	name;

    if( cmndc < 1 )
	return CMND_ARG_INVALID;

    for( i = 0; i < cmndc; i++ ) {
	state = analyzeDest(cmndv[i], &fid, &name);

	if( state == EXISTING_FILE ) {
	    deleteFID(curDir, fid);
	    continue;
	}

	if( state != EXISTING_DIR  )
	    return state;

	if( options & OPT_RECURSIVE ) {
	    deleteDirectory(curDir, fid);
	    continue;
	} else
	    printf("Cannot delete directory '%s' without -r option\n", cmndv[i]);
    }
    return CMND_OK;
}


int
mkdirCommand(void) 
{
    int		rv;
    char	*name;
    struct fileIdentDesc *fid;
    Directory 	*newDir;

    if( cmndc != 1 )
	return WRONG_NO_ARGS;

    rv = analyzeDest(cmndv[0], &fid, &name);

    if( rv == DIR_INVALID )
	return DIR_INVALID;

    if( rv == EXISTING_FILE || rv == EXISTING_DIR ) {
	if( fid->fileCharacteristics & FID_FILE_CHAR_DELETED ) {
	    removeFID(curDir, fid);
	} else 
	    return rv;
    }

    newDir = makeDir(curDir, name);

    if( !newDir )
	return CMND_FAILED;

    curDir = newDir;
    return CMND_OK;
}


int
rmdirCommand(void) 
{
    int		state;
    struct fileIdentDesc *fid;
    char*	name;

    if( cmndc != 1 )
	return WRONG_NO_ARGS;

    state = analyzeDest(cmndv[0], &fid, &name);

    if( state != EXISTING_DIR )
	return state;

    if( !directoryIsEmpty(curDir) )
	return DIR_NOT_EMPTY;

    if( !curDir->parent ) {
	printf("Cannot remove root directory\n");
	return CMND_FAILED;
    }
    return deleteFID(curDir, fid);
}

int cdcCommand() {
    int		state;
    struct fileIdentDesc *fid;
    char*	name;
    
    if( cmndc != 1 )
	return WRONG_NO_ARGS;

    state = analyzeDest(cmndv[0], &fid, &name);

    if( state != EXISTING_DIR )
	return state;

    return CMND_OK;
}

int lscCommand(void) {
    struct fileIdentDesc *fid;
    struct fileEntry *fe;
    char	*name, filename[512];
    uint64_t	i;
    int		state;

    if( cmndc > 1 )
	return WRONG_NO_ARGS;

    if( cmndc == 1 ) {
	state = analyzeDest(cmndv[0], &fid, &name);

	if( state != EXISTING_DIR )
	    return state;
    }

    for( i = 0; i < curDir->fe.informationLength;
	 i += ((sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3) ) 
    {
	fid = (struct fileIdentDesc*)(curDir->data + i);
	if( fid->fileCharacteristics & FID_FILE_CHAR_DELETED  ) 
	    continue;

	if( fid->fileCharacteristics & FID_FILE_CHAR_PARENT )
	    strcpy(filename, "..");
	else
	    decode_locale((dchars *)(fid->fileIdent + fid->lengthOfImpUse), filename, fid->lengthFileIdent, sizeof(filename));

	fe = readTaggedBlock( fid->icb.extLocation.logicalBlockNum, fid->icb.extLocation.partitionReferenceNum);

	printf("%s %6d:%c%c%c%c%c %6d:%c%c%c%c%c other:%c%c%c%c%c links:%2d info:%12d %s\n",
	    fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY ? "DIR" : "   ", 
	    fe->uid, 
	    fe->permissions & FE_PERM_U_READ ? 'r' : '-',
	    fe->permissions & FE_PERM_U_WRITE ? 'w' : '-',
	    fe->permissions & FE_PERM_U_EXEC ? 'x' : '-',
	    fe->permissions & FE_PERM_U_CHATTR ? 'a' : '-',
	    fe->permissions & FE_PERM_U_DELETE ? 'd' : '-',
	    fe->gid,
	    fe->permissions & FE_PERM_G_READ ? 'r' : '-',
	    fe->permissions & FE_PERM_G_WRITE ? 'w' : '-',
	    fe->permissions & FE_PERM_G_EXEC ? 'x' : '-',
	    fe->permissions & FE_PERM_G_CHATTR ? 'a' : '-',
	    fe->permissions & FE_PERM_G_DELETE ? 'd' : '-',
	    fe->permissions & FE_PERM_O_READ ? 'r' : '-',
	    fe->permissions & FE_PERM_O_WRITE ? 'w' : '-',
	    fe->permissions & FE_PERM_O_EXEC ? 'x' : '-',
	    fe->permissions & FE_PERM_O_CHATTR ? 'a' : '-',
	    fe->permissions & FE_PERM_O_DELETE ? 'd' : '-',
	    fe->fileLinkCount, (uint32_t)fe->informationLength, filename);
    }
    return CMND_OK;
}

int cdhCommand() 
{
    if( cmndc > 1 )
	return WRONG_NO_ARGS;

    if( chdir(cmndv[0]) != 0 ) {
	printf("Change dir '%s': %m\n", cmndv[0]);
	return CMND_FAILED;
    }

    if( hdWorkingDir ) free(hdWorkingDir);
    hdWorkingDir = getcwd(NULL, 0);
    printf("Harddisk working directory set to %s\n", cmndv[0]);
    return CMND_OK;
}


int lshCommand() {
    char	cmnd[128];

    if( cmndc > 1 )
	return WRONG_NO_ARGS;

    strcpy(cmnd, "ls -l ");

    if( cmndc == 1 )
	strncat(cmnd, cmndv[0], sizeof(cmnd)-strlen("ls -l ")-1);

    if( system(cmnd) != 0 )
	return CMND_FAILED;

    return CMND_OK;
}



/*	directoryIsEmpty()
 *	Return 1 if a directory contains only a parent dir entry and, possibly 0, deleted entries;
 *	Otherwise return 0;
 */
int
directoryIsEmpty(Directory *dir) 
{
    uint64_t i;
    struct fileIdentDesc *fid;

    for( i = 0; i < dir->fe.informationLength;
	 i += ((sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3) ) 
    {
	fid = (struct fileIdentDesc*)(dir->data + i);
	if( fid->fileCharacteristics & (FID_FILE_CHAR_DELETED | FID_FILE_CHAR_PARENT) ) 
	    continue;
	return 0;
    }
    return 1;
}


/*	questionOverwrite()
 *	Query whether to overwrite existing file.
 *	Remove the FID entry from the directory if reply was 'y' and return 0.
 *	Return 1 if don't overwrite.
 */
int
questionOverwrite(Directory *dir, struct fileIdentDesc *fid, char* name)
{
    printf("File %s already exists. Overwrite ? (y/N) : ", name);
#ifdef USE_READLINE
    readLine(NULL);
#else
    if (!fgets(line, 256, stdin))
	line[0] = 0;
#endif
#ifdef USE_READLINE
    if( !line )
	return 1;
#endif
    if( line[0] != 'y' )
	return 1;
    deleteFID(dir, fid);
    return 0;
}