]>
git.saurik.com Git - apple/hfs.git/blob - CopyHFSMeta/SparseBundle.c
10 #include <removefile.h>
12 #include <CoreFoundation/CoreFoundation.h>
18 * Routines to maniupulate a sparse bundle.
19 * N.B.: The sparse bundle format it uses is a subset of
20 * the real sparse bundle format: no partition map, and
25 ({ __typeof(a) __a = (a); __typeof(b) __b = (b); \
26 __a < __b ? __a : __b; })
29 * Context for the sparse bundle routines. The path name,
30 * size of the band files, and cached file descriptor and
31 * band numbers, to reduce the amount of pathname lookups
34 struct SparseBundleContext
{
37 int cfd
; // Cached file descriptor
38 int cBandNum
; // cached bandfile number
41 static const int kBandSize
= 8388608;
43 // Prototype bundle Info.plist file
44 static const char *bundlePrototype
=
45 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
46 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
47 "<plist version=\"1.0\">\n"
49 "\t<key>CFBundleInfoDictionaryVersion</key>\n"
50 "\t<string>6.0</string>\n"
51 "\t<key>band-size</key>\n"
52 "\t<integer>%d</integer>\n"
53 "\t<key>bundle-backingstore-version</key>\n"
54 "\t<integer>1</integer>\n"
55 "\t<key>diskimage-bundle-type</key>\n"
56 "\t<string>com.apple.diskimage.sparsebundle</string>\n"
58 "\t<integer>%llu</integer>\n"
63 * Read from a sparse bundle. If the band file doesn't exist, or is shorter than
64 * what we need to get from it, we pad out with 0's.
67 doSparseRead(struct IOWrapper
*context
, off_t offset
, void *buffer
, off_t len
)
69 struct SparseBundleContext
*ctx
= context
->context
;
70 off_t blockSize
= ctx
->bandSize
;
75 off_t bandNum
= (offset
+ nread
) / blockSize
; // Which band file to use
76 off_t bandOffset
= (offset
+ nread
) % blockSize
; // how far to go into the file
77 size_t amount
= MIN(len
- nread
, blockSize
- bandOffset
); // How many bytes to write in this band file
83 asprintf(&bandName
, "%s/bands/%x", ctx
->pathname
, bandNum
);
84 fd
= open(bandName
, O_RDONLY
);
86 if (errno
== ENOENT
) {
87 // Doesn't exist, so we just write zeroes
89 memset(buffer
+ nread
, 0, amount
);
93 warn("Cannot open band file %s for offset %llu", bandName
, offset
+ nread
);
98 n
= pread(fd
, (char*)buffer
+ nread
, amount
, bandOffset
);
100 warn("Cannot write to band file %s/band/%x for offset %llu for amount %zu", ctx
->pathname
, bandNum
, offset
+nread
, amount
);
104 if (n
< amount
) { // hit EOF, pad out with zeroes
105 memset(buffer
+ nread
+ amount
, 0, amount
- n
);
116 * Write a chunk of data to a bundle.
119 doSparseWrite(IOWrapper_t
*context
, off_t offset
, void *buffer
, size_t len
)
121 struct SparseBundleContext
*ctx
= context
->context
;
122 off_t blockSize
= ctx
->bandSize
;
126 while (written
< len
) {
127 off_t bandNum
= (offset
+ written
) / blockSize
; // Which band file to use
128 off_t bandOffset
= (offset
+ written
) % blockSize
; // how far to go into the file
129 size_t amount
= MIN(len
- written
, blockSize
- bandOffset
); // How many bytes to write in this band file
134 if (ctx
->cfd
== -1 || ctx
->cBandNum
!= bandNum
) {
137 asprintf(&bandName
, "%s/bands/%x", ctx
->pathname
, bandNum
);
138 fd
= open(bandName
, O_WRONLY
| O_CREAT
, 0666);
140 warn("Cannot open band file %s for offset %llu", bandName
, offset
+ written
);
147 ctx
->cBandNum
= bandNum
;
151 nwritten
= pwrite(fd
, (char*)buffer
+ written
, amount
, bandOffset
);
152 if (nwritten
== -1) {
153 warn("Cannot write to band file %s/band/%x for offset %llu for amount %zu", ctx
->pathname
, bandNum
, offset
+written
, amount
);
159 (void)fcntl(fd
, F_FULLFSYNC
, 0);
169 * Write a given extent (<start, length> pair) from an input device to the
170 * sparse bundle. We also use a block to update progress.
173 WriteExtentToSparse(struct IOWrapper
* context
, DeviceInfo_t
*devp
, off_t start
, off_t len
, void (^bp
)(off_t
))
175 const size_t bufSize
= 1024 * 1024;
176 uint8_t buffer
[bufSize
];
179 if (debug
) printf("Writing extent <%lld, %lld>\n", start
, len
);
180 while (total
< len
) {
183 size_t amt
= MIN(bufSize
, len
- total
);
184 nread
= pread(devp
->fd
, buffer
, amt
, start
+ total
);
186 warn("Cannot read from device at offset %lld", start
+ total
);
190 warnx("Short read from source device -- got %zd, expected %zd", nread
, amt
);
192 nwritten
= doSparseWrite(context
, start
+ total
, buffer
, nread
);
198 if (debug
) printf("\twrote %lld\n", total
);
202 static const CFStringRef kBandSizeKey
= CFSTR("band-size");
203 static const CFStringRef kDevSizeKey
= CFSTR("size");
206 * We need to be able to get the size of the "device" from a sparse bundle;
207 * we do this by using CF routines to parse the Info.plist file, and then
208 * get the two keys we care about: band-size (size of the band files), and
209 * size (size -- in bytes -- of the "disk").
212 GetSizesFromPlist(const char *path
, size_t *bandSize
, off_t
*devSize
)
215 CFReadStreamRef inFile
= NULL
;
216 CFURLRef inFileURL
= NULL
;
217 CFStringRef cfPath
= NULL
;
218 CFPropertyListRef cfDict
= NULL
;
219 CFNumberRef cfVal
= NULL
;
224 inFileURL
= CFURLCreateFromFileSystemRepresentation(NULL
, path
, strlen(path
), FALSE
);
225 if (inFileURL
== NULL
) {
226 if (debug
) warn("Cannot create url from pathname %s", path
);
230 inFile
= CFReadStreamCreateWithFile(NULL
, inFileURL
);
231 if (inFile
== NULL
) {
232 if (debug
) warn("cannot create read stream from path %s", path
);
236 if (CFReadStreamOpen(inFile
) == FALSE
) {
237 if (debug
) warn("cannot open read stream");
241 cfDict
= CFPropertyListCreateWithStream(NULL
, inFile
, 0, 0, NULL
, NULL
);
242 if (cfDict
== NULL
) {
243 if (debug
) warnx("cannot create propertly list from stream for path %s", path
);
247 cfVal
= CFDictionaryGetValue(cfDict
, kBandSizeKey
);
249 if (debug
) warnx("cannot get bandsize key from plist");
253 if (CFNumberGetValue(cfVal
, kCFNumberIntType
, &tmpInt
) == false) {
254 if (debug
) warnx("cannot get value from band size number");
260 cfVal
= CFDictionaryGetValue(cfDict
, kDevSizeKey
);
262 if (debug
) warnx("cannot get dev size key from plist");
265 if (CFNumberGetValue(cfVal
, kCFNumberLongLongType
, &tmpLL
) == false) {
277 CFRelease(inFileURL
);
285 #define kProgressName "HC.progress.txt"
288 * Get the progress state from a sparse bundle. If it's not there, then
292 GetProgress(struct IOWrapper
*context
)
294 struct SparseBundleContext
*ctx
= context
->context
;
297 char progFile
[strlen(ctx
->pathname
) + sizeof(kProgressName
) + 2]; // '/' and NUL
299 sprintf(progFile
, "%s/%s", ctx
->pathname
, kProgressName
);
300 fp
= fopen(progFile
, "r");
304 if (fscanf(fp
, "%llu", &retval
) != 1) {
313 * Write the progress information out. This involves writing a file in
314 * the sparse bundle with the amount -- in bytes -- we've written so far.
317 SetProgress(struct IOWrapper
*context
, off_t prog
)
319 struct SparseBundleContext
*ctx
= context
->context
;
321 char progFile
[strlen(ctx
->pathname
) + sizeof(kProgressName
) + 2]; // '/' and NUL
323 sprintf(progFile
, "%s/%s", ctx
->pathname
, kProgressName
);
327 fp
= fopen(progFile
, "w");
329 (void)fprintf(fp
, "%llu\n", prog
);
337 * Clean up. This is used when we have to initialize the bundle, but don't
338 * have any progress information -- in that case, we don't want to have any
339 * of the old band files laying around. We use removefile() to recursively
340 * remove them, but keep the bands directory.
343 doCleanup(struct IOWrapper
*ctx
)
345 struct SparseBundleContext
*context
= ctx
->context
;
347 char bandsDir
[strlen(context
->pathname
) + sizeof("/bands") + 1]; // 1 for NUL
349 sprintf(bandsDir
, "%s/bands", context
->pathname
);
352 fprintf(stderr
, "Cleaning up, about to call removefile\n");
353 rv
= removefile(bandsDir
, NULL
, REMOVEFILE_RECURSIVE
| REMOVEFILE_KEEP_PARENT
);
355 fprintf(stderr
, "removefile returned %d\n", rv
);
357 return (rv
== 0) ? 0 : -1;
361 * Initialize the IOWrapper structure for a sparse bundle. This will
362 * create the bundle directory (but not its parents!) if needed, and
363 * will populate it out. It checks to see if there is an existing bundle
364 * of the same name, and, if so, ensures that the izes are correct. Then
365 * it sets up all the function pointers.
368 InitSparseBundle(const char *path
, DeviceInfo_t
*devp
)
370 struct SparseBundleContext ctx
= { 0 };
371 struct SparseBundleContext
*retctx
= NULL
;
372 IOWrapper_t
*retval
= NULL
;
374 char tmpname
[strlen(path
) + sizeof("Info.plist") + 2]; // '/' + NUL
376 if (strstr(path
, ".sparsebundle") == NULL
) {
377 asprintf(&ctx
.pathname
, "%s.sparsebundle", path
);
379 ctx
.pathname
= strdup(path
);
382 if (lstat(ctx
.pathname
, &sb
) == -1) {
383 if (errno
!= ENOENT
) {
384 warn("cannot check sparse bundle %s", ctx
.pathname
);
387 if (mkdir(ctx
.pathname
, 0777) == -1) {
388 warn("cannot create sparse bundle %s", ctx
.pathname
);
391 } else if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
) {
392 warnx("sparse bundle object %s is not a directory", ctx
.pathname
);
395 sprintf(tmpname
, "%s/Info.plist", ctx
.pathname
);
396 if (stat(tmpname
, &sb
) != -1) {
399 if (GetSizesFromPlist(tmpname
, &bandSize
, &devSize
) == -1) {
400 warnx("Existing sparse bundle can't be parsed");
404 printf("Existing sparse bundle size = %lld, bandsize = %zu\n", devSize
, bandSize
);
406 if (devSize
!= devp
->size
) {
407 warnx("Existing sparse bundle size (%lld) != dev size (%lld)", devSize
, devp
->size
);
410 ctx
.bandSize
= bandSize
;
412 FILE *fp
= fopen(tmpname
, "w");
414 warn("cannot create sparse bundle info plist %s", tmpname
);
417 ctx
.bandSize
= kBandSize
;
418 fprintf(fp
, bundlePrototype
, kBandSize
, devp
->size
);
420 sprintf(tmpname
, "%s/Info.bckup", ctx
.pathname
);
421 fp
= fopen(tmpname
, "w");
423 fprintf(fp
, bundlePrototype
, kBandSize
, devp
->size
);
426 sprintf(tmpname
, "%s/bands", ctx
.pathname
);
427 if (mkdir(tmpname
, 0777) == -1) {
428 warn("cannot create bands directory in sparse bundle %s", ctx
.pathname
);
431 sprintf(tmpname
, "%s/token", ctx
.pathname
);
432 close(open(tmpname
, O_CREAT
| O_TRUNC
, 0666));
435 retval
= malloc(sizeof(*retval
));
436 if (retval
== NULL
) {
441 retctx
= malloc(sizeof(*retctx
));
447 retval
->writer
= &WriteExtentToSparse
;
448 retval
->reader
= &doSparseRead
;
449 retval
->getprog
= &GetProgress
;
450 retval
->setprog
= &SetProgress
;
451 retval
->cleanup
= &doCleanup
;
453 retval
->context
= retctx
;
455 if (retval
== NULL
) {