-
Notifications
You must be signed in to change notification settings - Fork 3
/
bkPath.c
375 lines (316 loc) · 9.75 KB
/
bkPath.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
/******************************* LICENCE **************************************
* Any code in this file may be redistributed or modified under the terms of
* the GNU General Public Licence as published by the Free Software
* Foundation; version 2 of the licence.
****************************** END LICENCE ***********************************/
/******************************************************************************
* Author:
* Andrew Smith, http://littlesvr.ca/misc/contactandrew.php
*
* Contributors:
* Henrique Pinto
* - fixed bug that caused crash in makeNewPathFromString()
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "bk.h"
#include "bkInternal.h"
#include "bkError.h"
#include "bkPath.h"
#include "bkMangle.h"
/******************************************************************************
* nameIsValid9660()
* Checks each character in name to see whether it's allowed in the more strict
* ISO9660 fields.
* */
bool nameIsValid9660(const char* name)
{
size_t count;
size_t nameLen;
nameLen = strlen(name);
for(count = 0; count < nameLen; count++)
{
if(!charIsValid9660(name[count]))
return false;
}
return true;
}
bool findBaseByNewPath(NewPath* path, BkDir* tree, BkFileBase** base)
{
BkDir* parentDir;
bool dirFound;
BkFileBase* child;
/* parent directory */
path->numChildren--;
dirFound = findDirByNewPath(path, tree, &parentDir);
path->numChildren++;
if(!dirFound)
return false;
child = parentDir->children;
while(child != NULL)
{
if(strcmp(child->name, path->children[path->numChildren - 1]) == 0)
{
*base = child;
return true;
}
child = child->next;
}
return false;
}
bool findDirByNewPath(const NewPath* path, BkDir* tree, BkDir** dir)
{
bool dirFound;
unsigned count;
BkFileBase* child;
*dir = tree;
for(count = 0; count < path->numChildren; count++)
/* each directory in the path */
{
child = (*dir)->children;
dirFound = false;
while(child != NULL && !dirFound)
/* find the directory */
{
if(strcmp(child->name, path->children[count]) == 0)
{
if( !IS_DIR(child->posixFileMode) )
return false;
dirFound = true;
*dir = BK_DIR_PTR(child);
}
else
child = child->next;
}
if(!dirFound)
return false;
}
return true;
}
/******************************************************************************
* freeDirToWriteContents()
* Recursively deletes all the dynamically allocated contents of dir.
* */
void freeDirToWriteContents(DirToWrite* dir)
{
BaseToWrite* currentChild;
BaseToWrite* nextChild;
currentChild = dir->children;
while(currentChild != NULL)
{
nextChild = currentChild->next;
if( IS_DIR(currentChild->posixFileMode) )
{
freeDirToWriteContents(DIRTW_PTR(currentChild));
}
else if( IS_REG_FILE(currentChild->posixFileMode) )
{
if(!FILETW_PTR(currentChild)->onImage)
free(FILETW_PTR(currentChild)->pathAndName);
}
free(currentChild);
currentChild = nextChild;
}
}
void freePathContents(NewPath* path)
{
unsigned count;
for(count = 0; count < path->numChildren; count++)
{
/* if the path was not allocated properly (maybe ran out of memory)
* the first unallocated item is null */
if(path->children[count] == NULL)
break;
free(path->children[count]);
}
if(path->children != NULL)
free(path->children);
}
int getLastNameFromPath(const char* srcPathAndName, char* lastName)
{
size_t count;
size_t srcLen;
size_t lastCharIndex;
size_t firstCharIndex;
bool lastCharFound;
int count2;
srcLen = strlen(srcPathAndName);
/* FIND the last name */
lastCharIndex = srcLen;
lastCharFound = false;
for(count = srcLen; /* unsigned */; count--)
{
if(srcPathAndName[count] != '/')
{
if(!lastCharFound)
{
lastCharIndex = count;
lastCharFound = true;
firstCharIndex = lastCharIndex;
}
else
{
firstCharIndex = count;
}
}
else
{
if(lastCharFound)
break;
}
if(count == 0)
break;
}
if(!lastCharFound)
return BKERROR_MISFORMED_PATH;
/* END FIND the last name */
if(lastCharIndex - firstCharIndex > NCHARS_FILE_ID_MAX_STORE - 1)
return BKERROR_MAX_NAME_LENGTH_EXCEEDED;
/* copy the name */
for(count = firstCharIndex, count2 = 0; count <= lastCharIndex; count++, count2++)
{
lastName[count2] = srcPathAndName[count];
}
lastName[count2] = '\0';
return 1;
}
int makeNewPathFromString(const char* strPath, NewPath* pathPath)
{
size_t count;
size_t pathStrLen;
unsigned numChildrenDone;
int nextChildLen;
const char* nextChild;
pathStrLen = strlen(strPath);
pathPath->numChildren = 0;
pathPath->children = NULL;
if(strPath[0] != '/')
return BKERROR_MISFORMED_PATH;
/* count number of children */
for(count = 1; count < pathStrLen; count++)
{
if(strPath[count] != '/' && strPath[count - 1] == '/')
pathPath->numChildren++;
}
if(pathPath->numChildren == 0)
{
pathPath->children = NULL;
return 1;
}
pathPath->children = (char**)malloc(sizeof(char*) * pathPath->numChildren);
if(pathPath->children == NULL)
return BKERROR_OUT_OF_MEMORY;
numChildrenDone = 0;
nextChildLen = 0;
nextChild = &(strPath[1]);
for(count = 1; count <= pathStrLen; count++)
{
if(strPath[count] == '/' || (strPath[count] == '\0' && strPath[count - 1] != '/'))
{
if(strPath[count] == '/' && strPath[count - 1] == '/')
/* double slash */
{
nextChild = &(strPath[count + 1]);
continue;
}
else
/* this is the end of the string or the slash following a dir name */
{
pathPath->children[numChildrenDone] = (char*)malloc(nextChildLen + 1);
if(pathPath->children[numChildrenDone] == NULL)
return BKERROR_OUT_OF_MEMORY;
strncpy(pathPath->children[numChildrenDone], nextChild, nextChildLen);
pathPath->children[numChildrenDone][nextChildLen] = '\0';
numChildrenDone++;
nextChildLen = 0;
nextChild = &(strPath[count + 1]);
}
}
else
{
nextChildLen++;
}
}
if(numChildrenDone != pathPath->numChildren)
return BKERROR_SANITY;
return 1;
}
/******************************************************************************
* nameIsValid()
* Checks each character in name to see whether it's allowed in an identifier
* */
bool nameIsValid(const char* name)
{
size_t count;
size_t nameLen;
nameLen = strlen(name);
for(count = 0; count < nameLen; count++)
{
/* can be any ascii char between decimal 32 and 126 except '/' (47) */
if(name[count] < 32 || name[count] > 126 || name[count] == 47)
return false;
}
return true;
}
#ifdef DEBUG
void printDirToWrite(DirToWrite* dir, int level, int filenameTypes)
{
BaseToWrite* child;
int count;
child = dir->children;
while(child != NULL)
{
if(IS_DIR(child->posixFileMode))
{
if(filenameTypes & FNTYPE_9660)
{
for(count = 0; count < level; count ++)
printf(" ");
printf("dir9 '%s'\n", child->name9660);fflush(NULL);
}
if(filenameTypes & FNTYPE_JOLIET)
{
for(count = 0; count < level; count ++)
printf(" ");
printf("dirJ '%s'\n", child->nameJoliet);fflush(NULL);
}
if(filenameTypes & FNTYPE_ROCKRIDGE)
{
for(count = 0; count < level; count ++)
printf(" ");
printf("dirR '%s'\n", child->nameRock);fflush(NULL);
}
printDirToWrite(DIRTW_PTR(child), level + 1, filenameTypes);
}
child = child->next;
}
child = dir->children;
while(child != NULL)
{
if(!IS_DIR(child->posixFileMode))
{
if(filenameTypes & FNTYPE_9660)
{
for(count = 0; count < level; count ++)
printf(" ");
printf("file9 '%s'\n", child->name9660);fflush(NULL);
}
if(filenameTypes & FNTYPE_JOLIET)
{
for(count = 0; count < level; count ++)
printf(" ");
printf("fileJ '%s'\n", child->nameJoliet);fflush(NULL);
}
if(filenameTypes & FNTYPE_ROCKRIDGE)
{
for(count = 0; count < level; count ++)
printf(" ");
printf("fileR '%s'\n", child->nameRock);fflush(NULL);
}
}
child = child->next;
}
}
#endif /* DEBUG */