-
Notifications
You must be signed in to change notification settings - Fork 3
/
bkAdd.c
451 lines (354 loc) · 12.5 KB
/
bkAdd.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
/******************************* 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:
*
******************************************************************************/
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "bk.h"
#include "bkPath.h"
#include "bkAdd.h"
#include "bkError.h"
#include "bkGet.h"
#include "bkMangle.h"
#include "bkLink.h"
#include "bkMisc.h"
#include "bkSet.h"
#include "bkIoWrappers.h"
int add(VolInfo* volInfo, const char* srcPathAndName, BkDir* destDir,
const char* nameToUse)
{
int rc;
char lastName[NCHARS_FILE_ID_MAX_STORE];
BkFileBase* oldHead; /* of the children list */
BkStatStruct statStruct;
if(volInfo->stopOperation)
return BKERROR_OPER_CANCELED_BY_USER;
maybeUpdateProgress(volInfo);
if(nameToUse == NULL)
{
rc = getLastNameFromPath(srcPathAndName, lastName);
if(rc <= 0)
return rc;
}
else
{
if(strlen(nameToUse) > NCHARS_FILE_ID_MAX_STORE - 1)
return BKERROR_MAX_NAME_LENGTH_EXCEEDED;
strcpy(lastName, nameToUse);
}
if(strcmp(lastName, ".") == 0 || strcmp(lastName, "..") == 0)
return BKERROR_NAME_INVALID;
if( !nameIsValid(lastName) )
return BKERROR_NAME_INVALID_CHAR;
oldHead = destDir->children;
/* windows doesn't have symbolic links */
if(volInfo->followSymLinks)
rc = bkStat(srcPathAndName, &statStruct);
else
rc = lstat(srcPathAndName, &statStruct);
if(rc == -1)
return BKERROR_STAT_FAILED;
if( IS_DIR(statStruct.st_mode) )
{
BkDir* newDir;
newDir = malloc(sizeof(BkDir));
if(newDir == NULL)
return BKERROR_OUT_OF_MEMORY;
memset(newDir, 0, sizeof(BkDir));
strcpy(BK_BASE_PTR(newDir)->name, lastName);
BK_BASE_PTR(newDir)->posixFileMode = statStruct.st_mode;
BK_BASE_PTR(newDir)->next = oldHead;
newDir->children = NULL;
/* ADD dir contents */
rc = addDirContents(volInfo, srcPathAndName, newDir);
if(rc < 0)
{
free(newDir);
return rc;
}
/* END ADD dir contents */
destDir->children = BK_BASE_PTR(newDir);
}
else if( IS_REG_FILE(statStruct.st_mode) )
{
BkFile* newFile;
if(statStruct.st_size > 0xFFFFFFFF)
/* size won't fit in a 32bit variable on the iso */
return BKERROR_ADD_FILE_TOO_BIG;
newFile = malloc(sizeof(BkFile));
if(newFile == NULL)
return BKERROR_OUT_OF_MEMORY;
memset(newFile, 0, sizeof(BkFile));
strcpy(BK_BASE_PTR(newFile)->name, lastName);
BK_BASE_PTR(newFile)->posixFileMode = statStruct.st_mode;
BK_BASE_PTR(newFile)->next = oldHead;
newFile->size = statStruct.st_size;
newFile->onImage = false;
newFile->position = 0;
newFile->pathAndName = malloc(strlen(srcPathAndName) + 1);
strcpy(newFile->pathAndName, srcPathAndName);
if( volInfo->scanForDuplicateFiles)
{
BkHardLink* newLink;
rc = findInHardLinkTable(volInfo, 0, newFile->pathAndName,
statStruct.st_size, false, &newLink);
if(rc < 0)
{
free(newFile);
return rc;
}
if(newLink == NULL)
/* not found */
{
rc = addToHardLinkTable(volInfo, 0, newFile->pathAndName,
statStruct.st_size, false, &newLink);
if(rc < 0)
{
free(newFile);
return rc;
}
}
newFile->location = newLink;
}
destDir->children = BK_BASE_PTR(newFile);
}
else if( IS_SYMLINK(statStruct.st_mode) )
{
BkSymLink* newSymLink;
ssize_t numChars;
newSymLink = malloc(sizeof(BkSymLink));
if(newSymLink == NULL)
return BKERROR_OUT_OF_MEMORY;
memset(newSymLink, 0, sizeof(BkSymLink));
strcpy(BK_BASE_PTR(newSymLink)->name, lastName);
BK_BASE_PTR(newSymLink)->posixFileMode = statStruct.st_mode;
BK_BASE_PTR(newSymLink)->next = oldHead;
numChars = readlink(srcPathAndName, newSymLink->target,
NCHARS_SYMLINK_TARGET_MAX - 1);
if(numChars == -1)
{
free(newSymLink);
return BKERROR_OPEN_READ_FAILED;
}
newSymLink->target[numChars] = '\0';
destDir->children = BK_BASE_PTR(newSymLink);
}
else
return BKERROR_NO_SPECIAL_FILES;
return 1;
}
int addDirContents(VolInfo* volInfo, const char* srcPath, BkDir* destDir)
{
int rc;
int srcPathLen;
char* newSrcPathAndName;
/* vars to read contents of a dir on fs */
DIR* srcDir;
struct dirent* dirEnt;
srcPathLen = strlen(srcPath);
/* including the new name and the possibly needed trailing '/' */
newSrcPathAndName = malloc(srcPathLen + NCHARS_FILE_ID_MAX_STORE + 2);
if(newSrcPathAndName == NULL)
return BKERROR_OUT_OF_MEMORY;
strcpy(newSrcPathAndName, srcPath);
if(srcPath[srcPathLen - 1] != '/')
{
strcat(newSrcPathAndName, "/");
srcPathLen++;
}
srcDir = opendir(srcPath);
if(srcDir == NULL)
{
free(newSrcPathAndName);
return BKERROR_OPENDIR_FAILED;
}
/* it may be possible but in any case very unlikely that readdir() will fail
* if it does, it returns NULL (same as end of dir) */
while( (dirEnt = readdir(srcDir)) != NULL )
{
if( strcmp(dirEnt->d_name, ".") == 0 || strcmp(dirEnt->d_name, "..") == 0 )
/* ignore "." and ".." */
continue;
if(strlen(dirEnt->d_name) > NCHARS_FILE_ID_MAX_STORE - 1)
{
closedir(srcDir);
free(newSrcPathAndName);
return BKERROR_MAX_NAME_LENGTH_EXCEEDED;
}
/* append file/dir name */
strcpy(newSrcPathAndName + srcPathLen, dirEnt->d_name);
rc = add(volInfo, newSrcPathAndName, destDir, NULL);
if(rc <= 0 && rc != BKWARNING_OPER_PARTLY_FAILED)
{
bool goOn;
if(volInfo->warningCbk != NULL && !volInfo->stopOperation)
/* perhaps the user wants to ignore this failure */
{
snprintf(volInfo->warningMessage, BK_WARNING_MAX_LEN,
"Failed to add item '%s': '%s'",
dirEnt->d_name,
bk_get_error_string(rc));
goOn = volInfo->warningCbk(volInfo->warningMessage);
rc = BKWARNING_OPER_PARTLY_FAILED;
}
else
goOn = false;
if(goOn)
continue;
else
{
volInfo->stopOperation = true;
closedir(srcDir);
free(newSrcPathAndName);
return rc;
}
}
}
free(newSrcPathAndName);
rc = closedir(srcDir);
if(rc != 0)
/* exotic error */
return BKERROR_EXOTIC;
return 1;
}
int bk_add(VolInfo* volInfo, const char* srcPathAndName,
const char* destPathStr, void(*progressFunction)(VolInfo*))
{
return bk_add_as(volInfo, srcPathAndName, destPathStr, NULL,
progressFunction);
}
int bk_add_as(VolInfo* volInfo, const char* srcPathAndName,
const char* destPathStr, const char* nameToUse,
void(*progressFunction)(VolInfo*))
{
int rc;
NewPath destPath;
char lastName[NCHARS_FILE_ID_MAX_STORE];
/* vars to find the dir in the tree */
BkDir* destDirInTree;
bool dirFound;
volInfo->progressFunction = progressFunction;
rc = makeNewPathFromString(destPathStr, &destPath);
if(rc <= 0)
{
freePathContents(&destPath);
return rc;
}
rc = getLastNameFromPath(srcPathAndName, lastName);
if(rc <= 0)
{
freePathContents(&destPath);
return rc;
}
dirFound = findDirByNewPath(&destPath, &(volInfo->dirTree), &destDirInTree);
if(!dirFound)
{
freePathContents(&destPath);
return BKERROR_DIR_NOT_FOUND_ON_IMAGE;
}
freePathContents(&destPath);
if(itemIsInDir(lastName, destDirInTree))
return BKERROR_DUPLICATE_ADD;
volInfo->stopOperation = false;
rc = add(volInfo, srcPathAndName, destDirInTree, nameToUse);
if(rc <= 0)
return rc;
return 1;
}
/*******************************************************************************
* bk_add_boot_record()
* Source boot file must be exactly the right size if floppy emulation requested.
* */
int bk_add_boot_record(VolInfo* volInfo, const char* srcPathAndName,
int bootMediaType)
{
BkStatStruct statStruct;
int rc;
if(bootMediaType != BOOT_MEDIA_NO_EMULATION &&
bootMediaType != BOOT_MEDIA_1_2_FLOPPY &&
bootMediaType != BOOT_MEDIA_1_44_FLOPPY &&
bootMediaType != BOOT_MEDIA_2_88_FLOPPY)
{
return BKERROR_ADD_UNKNOWN_BOOT_MEDIA;
}
rc = bkStat(srcPathAndName, &statStruct);
if(rc == -1)
return BKERROR_STAT_FAILED;
if(statStruct.st_size > 0xFFFFFFFF)
/* size won't fit in a 32bit variable on the iso */
return BKERROR_ADD_FILE_TOO_BIG;
if( (bootMediaType == BOOT_MEDIA_1_2_FLOPPY &&
statStruct.st_size != 1228800) ||
(bootMediaType == BOOT_MEDIA_1_44_FLOPPY &&
statStruct.st_size != 1474560) ||
(bootMediaType == BOOT_MEDIA_2_88_FLOPPY &&
statStruct.st_size != 2949120) )
{
return BKERROR_ADD_BOOT_RECORD_WRONG_SIZE;
}
volInfo->bootMediaType = bootMediaType;
volInfo->bootRecordSize = statStruct.st_size;
volInfo->bootRecordIsOnImage = false;
/* make copy of the source path and name */
if(volInfo->bootRecordPathAndName != NULL)
free(volInfo->bootRecordPathAndName);
volInfo->bootRecordPathAndName = malloc(strlen(srcPathAndName) + 1);
if(volInfo->bootRecordPathAndName == NULL)
{
volInfo->bootMediaType = BOOT_MEDIA_NONE;
return BKERROR_OUT_OF_MEMORY;
}
strcpy(volInfo->bootRecordPathAndName, srcPathAndName);
/* this is the wrong function to use if you want a visible one */
volInfo->bootRecordIsVisible = false;
return 1;
}
/*******************************************************************************
* bk_create_dir()
*
* */
int bk_create_dir(VolInfo* volInfo, const char* destPathStr,
const char* newDirName)
{
size_t nameLen;
BkDir* destDir;
int rc;
BkFileBase* oldHead;
BkDir* newDir;
nameLen = strlen(newDirName);
if(nameLen > NCHARS_FILE_ID_MAX_STORE - 1)
return BKERROR_MAX_NAME_LENGTH_EXCEEDED;
if(nameLen == 0)
return BKERROR_BLANK_NAME;
if(strcmp(newDirName, ".") == 0 || strcmp(newDirName, "..") == 0)
return BKERROR_NAME_INVALID;
if( !nameIsValid(newDirName) )
return BKERROR_NAME_INVALID_CHAR;
rc = getDirFromString(&(volInfo->dirTree), destPathStr, &destDir);
if(rc <= 0)
return rc;
if(itemIsInDir(newDirName, destDir))
return BKERROR_DUPLICATE_CREATE_DIR;
oldHead = destDir->children;
newDir = malloc(sizeof(BkDir));
if(newDir == NULL)
return BKERROR_OUT_OF_MEMORY;
strcpy(BK_BASE_PTR(newDir)->name, newDirName);
BK_BASE_PTR(newDir)->posixFileMode = volInfo->posixDirDefaults;
BK_BASE_PTR(newDir)->next = oldHead;
newDir->children = NULL;
destDir->children = BK_BASE_PTR(newDir);
return 1;
}