udftools

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
wrudf-desc.c
Go to the documentation of this file.
1 /* wrudf-desc.c
2 *
3 * PURPOSE
4 * Routines to create/find/insert/delete FileIdentDesc's and FileEntries.
5 *
6 * COPYRIGHT
7 * This file is distributed under the terms of the GNU General Public
8 * License (GPL). Copies of the GPL can be obtained from:
9 * ftp://prep.ai.mit.edu/pub/gnu/GPL
10 *
11 * (C) 2001 Enno Fennema <e.fennema@dataweb.nl>
12 *
13 * HISTORY
14 * 16 Aug 01 ef Created.
15 */
16
17 #include "config.h"
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23
24 #include "wrudf.h"
25
26 /* removeFID()
27 *
28 * Physically remove a FileIdentDesc from the directory
29 */
30 int
31 removeFID(Directory *dir, struct fileIdentDesc *fid)
32 {
33 uint32_t lenFID = (sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3;
34 uint32_t lenMove;
35
36 dir->fe.informationLength -= lenFID;
37 lenMove = dir->fe.informationLength - ((char *)fid - dir->data);
38 memcpy(fid, (char *)fid + lenFID, lenMove);
39 dir->dirDirty = 1;
40 return 0;
41 }
42
43 /* deleteFID()
44 *
45 * Remove an FID from the directory
46 * If fileLinkCount now zero, deallocate FileEntry and any data extents
47 */
48 int
49 deleteFID(Directory * dir, struct fileIdentDesc * fid)
50 {
51 int rv;
52 struct fileEntry *fe;
53
54 rv = CMND_OK;
55
56 // if( fid->icb.extLocation.partitionReferenceNum != pd->partitionNumber )
57 // return NOT_IN_RW_PARTITION;
58
59 fe = readTaggedBlock(fid->icb.extLocation.logicalBlockNum, fid->icb.extLocation.partitionReferenceNum);
60
61 /* check permission */
62
63 freeBlock(fid->icb.extLocation.logicalBlockNum, fid->icb.extLocation.partitionReferenceNum);
64
65 if( fe->fileLinkCount > 1 ) {
66 fe->fileLinkCount--;
67 setChecksum(fe);
68 if( medium == CDRW )
69 dirtyBlock(fid->icb.extLocation.logicalBlockNum, fid->icb.extLocation.partitionReferenceNum);
70 else
71 vat[fid->icb.extLocation.logicalBlockNum] = writeCDR(fe);
72 } else {
73 if( medium == CDR ) {
74 if( fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY )
75 dir->fe.fileLinkCount--;
76 vat[fid->icb.extLocation.logicalBlockNum] = 0xFFFFFFFF;
77 } else {
78 if( fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY ) {
79 dir->fe.fileLinkCount--;
80 ((struct logicalVolIntegrityDescImpUse*)
81 (lvid->impUse + 2 * sizeof(uint32_t) * lvid->numOfPartitions))->numDirs--;
82 } else {
83 ((struct logicalVolIntegrityDescImpUse*)
84 (lvid->impUse + 2 * sizeof(uint32_t) * lvid->numOfPartitions))->numFiles--;
85 }
86
87 /* free file data */
88 switch( fe->icbTag.flags & ICBTAG_FLAG_AD_MASK ) {
89 case ICBTAG_FLAG_AD_IN_ICB:
90 break;
91 case ICBTAG_FLAG_AD_SHORT:
92 freeShortExtents((short_ad*)(fe->allocDescs + fe->lengthExtendedAttr));
93 break;
94 case ICBTAG_FLAG_AD_LONG:
95 freeLongExtents((long_ad*)(fe->allocDescs + fe->lengthExtendedAttr));
96 break;
97 default:
98 printf("UDF does not use Extended Alloc Descs\n");
99 rv = CMND_FAILED;
100 }
101 /* free the File Entry itself */
102 markBlock(FREE, fid->icb.extLocation.logicalBlockNum);
103 }
104 }
105
106 removeFID(dir, fid);
107 dir->dirDirty = 1;
108
109 return rv;
110 }
111
112 struct fileIdentDesc*
113 findFileIdentDesc(Directory *dir, char* name)
114 {
115 uint64_t i;
116 char *data;
117 struct fileIdentDesc *fid;
118 dstring uName[256];
119 size_t uLen;
120
121 uLen = encode_utf8(uName, "", name, 256);
122
123 data = dir->data;
124
125 for( i = 0; i < dir->fe.informationLength;
126 i += (sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3 ) {
127
128 fid = (struct fileIdentDesc*)(data + i);
129
130 if( fid->descTag.tagIdent == TAG_IDENT_TE )
131 return NULL;
132
133 if( fid->descTag.tagIdent == TAG_IDENT_IE ) {
134 printf("Indirect Entry not yet implemented\n");
135 continue;
136 }
137 if( fid->descTag.tagIdent != TAG_IDENT_FID )
138 fail("Unknown tag id %08X in directory\n", fid->descTag.tagIdent);
139
140 // check CRC
141
142 if( fid->fileCharacteristics == FID_FILE_CHAR_PARENT)
143 continue;
144
145 if( fid->lengthFileIdent == uLen
146 && memcmp( fid->fileIdent, uName, fid->lengthFileIdent) == 0 )
147 return fid;
148 }
149 return NULL;
150 }
151
152
153 /*
154 * Return pointer to new FileEntry in a 2k allocated block to be free'd when no longer required.
155 * Only basic data is filled in.
156 */
157 struct fileEntry*
158 makeFileEntry()
159 {
160 struct fileEntry *fe = (struct fileEntry*) calloc(2048, 1);
161
162 updateTimestamp(0,0);
163 fe->descTag.tagIdent = TAG_IDENT_FE;
164 fe->descTag.descVersion = 2;
165 fe->descTag.tagSerialNum = lvd->descTag.tagSerialNum;
166 fe->icbTag.strategyType = 4;
167 fe->icbTag.numEntries = 1;
168 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
169 /* User has all permission; Group/Others Read/Exec */
170 fe->permissions = FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_DELETE | FE_PERM_U_CHATTR | FE_PERM_U_EXEC |
171 FE_PERM_G_READ | FE_PERM_G_EXEC | FE_PERM_O_READ | FE_PERM_O_EXEC;
172 fe->fileLinkCount = 1;
173 fe->accessTime = timeStamp;
174 fe->modificationTime = timeStamp;
175 fe->attrTime = timeStamp;
176 fe->impIdent = entityWRUDF;
177 fe->uniqueID = ((struct logicalVolHeaderDesc*)lvid->logicalVolContentsUse)->uniqueID++;
178 return fe;
179 }
180
181
182 /*
183 * Return pointer to new FileIdentDesc in an allocated block to be free'd when no longer required.
184 * Does NOT fill: descTag.checksum, descTag.tagLocation, icbTag.characteristics, icb
185 */
186 struct fileIdentDesc*
187 makeFileIdentDesc(char* name)
188 {
189 dstring uName[256];
190 uint8_t uLen;
191 struct fileIdentDesc *fid;
192
193 fid = (struct fileIdentDesc*) calloc(512,1);
194 fid->descTag.tagIdent = TAG_IDENT_FID;
195 fid->descTag.descVersion = 2;
196 fid->descTag.tagSerialNum = lvd->descTag.tagSerialNum;
197 fid->fileVersionNum = 1;
198 fid->icb.extLength = 2048;
199
200 if( name[0] != 0 ) { /* FID to parent directory has no name */
201 memset(&uName, 0, 256);
202 uLen = encode_utf8(uName, "", name, 256);
203 fid->lengthFileIdent = uLen;
204 memcpy(fid->fileIdent + fid->lengthOfImpUse, uName, fid->lengthFileIdent);
205 }
206 return fid;
207 }
208
209
210 int
211 insertFileIdentDesc(Directory *dir, struct fileIdentDesc* fid)
212 {
213 uint32_t lenFid;
214
215 lenFid = (sizeof(struct fileIdentDesc) + fid->lengthOfImpUse + fid->lengthFileIdent + 3) & ~3;
216 fid->descTag.descCRCLength = lenFid - sizeof(tag);
217 setChecksum(fid);
218
219 if( dir->fe.informationLength + lenFid > dir->dataSize ) {
220 if( !(dir->data = realloc(dir->data, dir->dataSize + 2048)) ) {
221 fail("Realloc directory data failed\n");
222 }
223 dir->dataSize += 2048;
224 }
225
226 memcpy(dir->data + dir->fe.informationLength, fid, lenFid);
227 dir->fe.informationLength += lenFid;
228 dir->dirDirty = 1;
229 return CMND_OK;
230 }
timeStamp
timestamp timeStamp
Definition: wrudf.c:49
setChecksum
void setChecksum(void *descriptor)
Definition: wrudf-cdrw.c:229
fileEntry::lengthExtendedAttr
uint32_t lengthExtendedAttr
Definition: ecma_167.h:578
readTaggedBlock
void * readTaggedBlock(uint32_t lbn, uint16_t partition)
Definition: wrudf-cdrw.c:639
_dir_::dataSize
uint32_t dataSize
Definition: wrudf.h:89
TAG_IDENT_IE
#define TAG_IDENT_IE
Definition: ecma_167.h:415
freeLongExtents
int freeLongExtents(long_ad *extents)
Definition: wrudf-cdrw.c:176
fileEntry::icbTag
icbtag icbTag
Definition: ecma_167.h:561
logicalVolIntegrityDesc::logicalVolContentsUse
uint8_t logicalVolContentsUse[32]
Definition: ecma_167.h:366
FID_FILE_CHAR_PARENT
#define FID_FILE_CHAR_PARENT
Definition: ecma_167.h:477
medium
enum MEDIUM medium
Definition: wrudf.c:20
markBlock
void markBlock(enum markAction action, uint32_t blkno)
Definition: wrudf-cdrw.c:74
ICBTAG_FLAG_AD_MASK
#define ICBTAG_FLAG_AD_MASK
Definition: ecma_167.h:525
makeFileIdentDesc
struct fileIdentDesc * makeFileIdentDesc(char *name)
Definition: wrudf-desc.c:187
FE_PERM_G_EXEC
#define FE_PERM_G_EXEC
Definition: ecma_167.h:590
fileEntry::permissions
uint32_t permissions
Definition: ecma_167.h:564
FE_PERM_U_DELETE
#define FE_PERM_U_DELETE
Definition: ecma_167.h:599
dstring
uint8_t dstring
Definition: ecma_167.h:63
logicalVolDesc::descTag
tag descTag
Definition: ecma_167.h:298
CDRW
Definition: wrudf.h:40
FE_PERM_U_READ
#define FE_PERM_U_READ
Definition: ecma_167.h:597
fileEntry::fileLinkCount
uint16_t fileLinkCount
Definition: ecma_167.h:565
insertFileIdentDesc
int insertFileIdentDesc(Directory *dir, struct fileIdentDesc *fid)
Definition: wrudf-desc.c:211
fileEntry::accessTime
timestamp accessTime
Definition: ecma_167.h:571
freeBlock
void freeBlock(uint32_t lbn, uint16_t part)
Definition: wrudf-cdrw.c:592
TAG_IDENT_FE
#define TAG_IDENT_FE
Definition: ecma_167.h:417
deleteFID
int deleteFID(Directory *dir, struct fileIdentDesc *fid)
Definition: wrudf-desc.c:49
ICBTAG_FLAG_AD_SHORT
#define ICBTAG_FLAG_AD_SHORT
Definition: ecma_167.h:526
lvid
struct logicalVolIntegrityDesc * lvid
Definition: wrudf.c:71
FE_PERM_U_CHATTR
#define FE_PERM_U_CHATTR
Definition: ecma_167.h:598
ICBTAG_FLAG_AD_LONG
#define ICBTAG_FLAG_AD_LONG
Definition: ecma_167.h:527
fileIdentDesc::fileVersionNum
uint16_t fileVersionNum
Definition: ecma_167.h:463
FE_PERM_O_READ
#define FE_PERM_O_READ
Definition: ecma_167.h:587
CMND_FAILED
Definition: wrudf.h:60
dirtyBlock
void dirtyBlock(uint32_t lbn, uint16_t part)
Definition: wrudf-cdrw.c:610
FE_PERM_G_READ
#define FE_PERM_G_READ
Definition: ecma_167.h:592
logicalVolIntegrityDesc::impUse
uint8_t impUse[0]
Definition: ecma_167.h:371
fileIdentDesc::fileCharacteristics
uint8_t fileCharacteristics
Definition: ecma_167.h:464
_dir_::fe
struct fileEntry fe
Definition: wrudf.h:94
CDR
Definition: wrudf.h:40
fail
void fail(char *fmt,...)
Definition: ide-pc.c:43
fileIdentDesc::lengthOfImpUse
uint16_t lengthOfImpUse
Definition: ecma_167.h:467
ICBTAG_FILE_TYPE_REGULAR
#define ICBTAG_FILE_TYPE_REGULAR
Definition: ecma_167.h:514
fileEntry
Definition: ecma_167.h:558
removeFID
int removeFID(Directory *dir, struct fileIdentDesc *fid)
Definition: wrudf-desc.c:31
makeFileEntry
struct fileEntry * makeFileEntry()
Definition: wrudf-desc.c:158
rv
static int rv
Definition: ide-pc.c:39
fileEntry::informationLength
uint64_t informationLength
Definition: ecma_167.h:569
_dir_::dirDirty
uint32_t dirDirty
Definition: wrudf.h:93
logicalVolHeaderDesc
Definition: ecma_167.h:782
_dir_
Definition: wrudf.h:87
CMND_OK
Definition: wrudf.h:60
entityWRUDF
regid entityWRUDF
Definition: wrudf.c:50
fileEntry::descTag
tag descTag
Definition: ecma_167.h:560
TAG_IDENT_TE
#define TAG_IDENT_TE
Definition: ecma_167.h:416
fileIdentDesc::lengthFileIdent
uint8_t lengthFileIdent
Definition: ecma_167.h:465
encode_utf8
size_t encode_utf8(dstring *, char *, char *, size_t)
Definition: unicode.c:68
FE_PERM_U_WRITE
#define FE_PERM_U_WRITE
Definition: ecma_167.h:596
fileEntry::modificationTime
timestamp modificationTime
Definition: ecma_167.h:572
FE_PERM_U_EXEC
#define FE_PERM_U_EXEC
Definition: ecma_167.h:595
fileEntry::uniqueID
uint64_t uniqueID
Definition: ecma_167.h:577
logicalVolIntegrityDesc::numOfPartitions
uint32_t numOfPartitions
Definition: ecma_167.h:367
TAG_IDENT_FID
#define TAG_IDENT_FID
Definition: ecma_167.h:413
fileEntry::impIdent
regid impIdent
Definition: ecma_167.h:576
updateTimestamp
void updateTimestamp(time_t t, uint32_t ut)
Definition: wrudf-cdrw.c:243
fileIdentDesc::descTag
tag descTag
Definition: ecma_167.h:462
ICBTAG_FLAG_AD_IN_ICB
#define ICBTAG_FLAG_AD_IN_ICB
Definition: ecma_167.h:529
vat
uint32_t * vat
Definition: wrudf.c:64
wrudf.h
findFileIdentDesc
struct fileIdentDesc * findFileIdentDesc(Directory *dir, char *name)
Definition: wrudf-desc.c:113
FREE
Definition: wrudf.h:127
ICBTAG_FILE_TYPE_DIRECTORY
#define ICBTAG_FILE_TYPE_DIRECTORY
Definition: ecma_167.h:513
logicalVolIntegrityDescImpUse
Definition: osta_udf.h:94
_dir_::data
char * data
Definition: wrudf.h:90
lvd
struct logicalVolDesc * lvd
Definition: wrudf.c:68
fileEntry::attrTime
timestamp attrTime
Definition: ecma_167.h:573
freeShortExtents
int freeShortExtents(short_ad *extents)
Definition: wrudf-cdrw.c:159
fileIdentDesc::fileIdent
uint8_t fileIdent[0]
Definition: ecma_167.h:469
fileIdentDesc
Definition: ecma_167.h:460
fileEntry::allocDescs
uint8_t allocDescs[0]
Definition: ecma_167.h:581
fileIdentDesc::icb
long_ad icb
Definition: ecma_167.h:466
FE_PERM_O_EXEC
#define FE_PERM_O_EXEC
Definition: ecma_167.h:585
writeCDR
uint32_t writeCDR(void *src)
Definition: wrudf-cdr.c:121