]>
git.saurik.com Git - apple/hfs.git/blob - CopyHFSMeta/SparseBundle.c
10 #include <removefile.h>
12 #include <CoreFoundation/CoreFoundation.h>
13 #include <System/sys/fsctl.h>
19 * Routines to maniupulate a sparse bundle.
20 * N.B.: The sparse bundle format it uses is a subset of
21 * the real sparse bundle format: no partition map, and
26 ({ __typeof(a) __a = (a); __typeof(b) __b = (b); \
27 __a < __b ? __a : __b; })
30 * Context for the sparse bundle routines. The path name,
31 * size of the band files, and cached file descriptor and
32 * band numbers, to reduce the amount of pathname lookups
35 struct SparseBundleContext
{
38 int cfd
; // Cached file descriptor
39 int cBandNum
; // cached bandfile number
42 static const int kBandSize
= 8388608;
44 // Prototype bundle Info.plist file
45 static const char *bundlePrototype
=
46 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
47 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
48 "<plist version=\"1.0\">\n"
50 "\t<key>CFBundleInfoDictionaryVersion</key>\n"
51 "\t<string>6.0</string>\n"
52 "\t<key>band-size</key>\n"
53 "\t<integer>%d</integer>\n"
54 "\t<key>bundle-backingstore-version</key>\n"
55 "\t<integer>1</integer>\n"
56 "\t<key>diskimage-bundle-type</key>\n"
57 "\t<string>com.apple.diskimage.sparsebundle</string>\n"
59 "\t<integer>%llu</integer>\n"
64 * Do a per-volume sync. We use this just before updating the progress file, so
65 * that any changes -- data and metadata -- will have made it to disk, without
66 * causing a sync of every mounted volume.
71 sync_volume(const char *path
) {
72 int full_sync
= FSCTL_SYNC_FULLSYNC
| FSCTL_SYNC_WAIT
;
74 (void)fsctl(path
, FSCTL_SYNC_VOLUME
, &full_sync
, 0);
79 * Read from a sparse bundle. If the band file doesn't exist, or is shorter than
80 * what we need to get from it, we pad out with 0's.
83 doSparseRead(struct IOWrapper
*context
, off_t offset
, void *buffer
, off_t len
)
85 struct SparseBundleContext
*ctx
= context
->context
;
86 off_t blockSize
= ctx
->bandSize
;
91 off_t bandNum
= (offset
+ nread
) / blockSize
; // Which band file to use
92 off_t bandOffset
= (offset
+ nread
) % blockSize
; // how far to go into the file
93 size_t amount
= MIN(len
- nread
, blockSize
- bandOffset
); // How many bytes to write in this band file
99 asprintf(&bandName
, "%s/bands/%x", ctx
->pathname
, bandNum
);
100 fd
= open(bandName
, O_RDONLY
);
102 if (errno
== ENOENT
) {
103 // Doesn't exist, so we just write zeroes
105 memset(buffer
+ nread
, 0, amount
);
109 warn("Cannot open band file %s for offset %llu", bandName
, offset
+ nread
);
114 n
= pread(fd
, (char*)buffer
+ nread
, amount
, bandOffset
);
116 warn("Cannot write to band file %s/band/%x for offset %llu for amount %zu", ctx
->pathname
, bandNum
, offset
+nread
, amount
);
120 if (n
< amount
) { // hit EOF, pad out with zeroes
121 memset(buffer
+ nread
+ amount
, 0, amount
- n
);
132 * Write a chunk of data to a bundle.
135 doSparseWrite(IOWrapper_t
*context
, off_t offset
, void *buffer
, size_t len
)
137 struct SparseBundleContext
*ctx
= context
->context
;
138 off_t blockSize
= ctx
->bandSize
;
142 while (written
< len
) {
143 off_t bandNum
= (offset
+ written
) / blockSize
; // Which band file to use
144 off_t bandOffset
= (offset
+ written
) % blockSize
; // how far to go into the file
145 size_t amount
= MIN(len
- written
, blockSize
- bandOffset
); // How many bytes to write in this band file
150 if (ctx
->cfd
== -1 || ctx
->cBandNum
!= bandNum
) {
153 asprintf(&bandName
, "%s/bands/%x", ctx
->pathname
, bandNum
);
154 fd
= open(bandName
, O_WRONLY
| O_CREAT
, 0666);
156 warn("Cannot open band file %s for offset %llu", bandName
, offset
+ written
);
163 ctx
->cBandNum
= bandNum
;
167 nwritten
= pwrite(fd
, (char*)buffer
+ written
, amount
, bandOffset
);
168 if (nwritten
== -1) {
169 warn("Cannot write to band file %s/band/%x for offset %llu for amount %zu", ctx
->pathname
, bandNum
, offset
+written
, amount
);
184 * Write a given extent (<start, length> pair) from an input device to the
185 * sparse bundle. We also use a block to update progress.
188 WriteExtentToSparse(struct IOWrapper
* context
, DeviceInfo_t
*devp
, off_t start
, off_t len
, void (^bp
)(off_t
))
190 const size_t bufSize
= 1024 * 1024;
191 uint8_t buffer
[bufSize
];
194 if (debug
) printf("Writing extent <%lld, %lld>\n", start
, len
);
195 while (total
< len
) {
198 size_t amt
= MIN(bufSize
, len
- total
);
199 nread
= pread(devp
->fd
, buffer
, amt
, start
+ total
);
201 warn("Cannot read from device at offset %lld", start
+ total
);
205 warnx("Short read from source device -- got %zd, expected %zd", nread
, amt
);
207 nwritten
= doSparseWrite(context
, start
+ total
, buffer
, nread
);
213 if (debug
) printf("\twrote %lld\n", total
);
217 static const CFStringRef kBandSizeKey
= CFSTR("band-size");
218 static const CFStringRef kDevSizeKey
= CFSTR("size");
221 * We need to be able to get the size of the "device" from a sparse bundle;
222 * we do this by using CF routines to parse the Info.plist file, and then
223 * get the two keys we care about: band-size (size of the band files), and
224 * size (size -- in bytes -- of the "disk").
227 GetSizesFromPlist(const char *path
, size_t *bandSize
, off_t
*devSize
)
230 CFReadStreamRef inFile
= NULL
;
231 CFURLRef inFileURL
= NULL
;
232 CFStringRef cfPath
= NULL
;
233 CFPropertyListRef cfDict
= NULL
;
234 CFNumberRef cfVal
= NULL
;
239 inFileURL
= CFURLCreateFromFileSystemRepresentation(NULL
, path
, strlen(path
), FALSE
);
240 if (inFileURL
== NULL
) {
241 if (debug
) warn("Cannot create url from pathname %s", path
);
245 inFile
= CFReadStreamCreateWithFile(NULL
, inFileURL
);
246 if (inFile
== NULL
) {
247 if (debug
) warn("cannot create read stream from path %s", path
);
251 if (CFReadStreamOpen(inFile
) == FALSE
) {
252 if (debug
) warn("cannot open read stream");
256 cfDict
= CFPropertyListCreateWithStream(NULL
, inFile
, 0, 0, NULL
, NULL
);
257 if (cfDict
== NULL
) {
258 if (debug
) warnx("cannot create propertly list from stream for path %s", path
);
262 cfVal
= CFDictionaryGetValue(cfDict
, kBandSizeKey
);
264 if (debug
) warnx("cannot get bandsize key from plist");
268 if (CFNumberGetValue(cfVal
, kCFNumberIntType
, &tmpInt
) == false) {
269 if (debug
) warnx("cannot get value from band size number");
275 cfVal
= CFDictionaryGetValue(cfDict
, kDevSizeKey
);
277 if (debug
) warnx("cannot get dev size key from plist");
280 if (CFNumberGetValue(cfVal
, kCFNumberLongLongType
, &tmpLL
) == false) {
292 CFRelease(inFileURL
);
300 #define kProgressName "HC.progress.txt"
303 * Get the progress state from a sparse bundle. If it's not there, then
307 GetProgress(struct IOWrapper
*context
)
309 struct SparseBundleContext
*ctx
= context
->context
;
312 char progFile
[strlen(ctx
->pathname
) + sizeof(kProgressName
) + 2]; // '/' and NUL
314 sprintf(progFile
, "%s/%s", ctx
->pathname
, kProgressName
);
315 fp
= fopen(progFile
, "r");
319 if (fscanf(fp
, "%llu", &retval
) != 1) {
328 * Write the progress information out. This involves writing a file in
329 * the sparse bundle with the amount -- in bytes -- we've written so far.
332 SetProgress(struct IOWrapper
*context
, off_t prog
)
334 struct SparseBundleContext
*ctx
= context
->context
;
336 char progFile
[strlen(ctx
->pathname
) + sizeof(kProgressName
) + 2]; // '/' and NUL
338 sprintf(progFile
, "%s/%s", ctx
->pathname
, kProgressName
);
342 fp
= fopen(progFile
, "w");
344 sync_volume(ctx
->pathname
);
345 (void)fprintf(fp
, "%llu\n", prog
);
353 * Clean up. This is used when we have to initialize the bundle, but don't
354 * have any progress information -- in that case, we don't want to have any
355 * of the old band files laying around. We use removefile() to recursively
356 * remove them, but keep the bands directory.
359 doCleanup(struct IOWrapper
*ctx
)
361 struct SparseBundleContext
*context
= ctx
->context
;
363 char bandsDir
[strlen(context
->pathname
) + sizeof("/bands") + 1]; // 1 for NUL
365 sprintf(bandsDir
, "%s/bands", context
->pathname
);
368 fprintf(stderr
, "Cleaning up, about to call removefile\n");
369 rv
= removefile(bandsDir
, NULL
, REMOVEFILE_RECURSIVE
| REMOVEFILE_KEEP_PARENT
);
371 fprintf(stderr
, "removefile returned %d\n", rv
);
373 return (rv
== 0) ? 0 : -1;
377 * Initialize the IOWrapper structure for a sparse bundle. This will
378 * create the bundle directory (but not its parents!) if needed, and
379 * will populate it out. It checks to see if there is an existing bundle
380 * of the same name, and, if so, ensures that the izes are correct. Then
381 * it sets up all the function pointers.
384 InitSparseBundle(const char *path
, DeviceInfo_t
*devp
)
386 struct SparseBundleContext ctx
= { 0 };
387 struct SparseBundleContext
*retctx
= NULL
;
388 IOWrapper_t
*retval
= NULL
;
390 char tmpname
[strlen(path
) + sizeof("Info.plist") + 2]; // '/' + NUL
392 if (strstr(path
, ".sparsebundle") == NULL
) {
393 asprintf(&ctx
.pathname
, "%s.sparsebundle", path
);
395 ctx
.pathname
= strdup(path
);
398 if (lstat(ctx
.pathname
, &sb
) == -1) {
399 if (errno
!= ENOENT
) {
400 warn("cannot check sparse bundle %s", ctx
.pathname
);
403 if (mkdir(ctx
.pathname
, 0777) == -1) {
404 warn("cannot create sparse bundle %s", ctx
.pathname
);
407 } else if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
) {
408 warnx("sparse bundle object %s is not a directory", ctx
.pathname
);
411 sprintf(tmpname
, "%s/Info.plist", ctx
.pathname
);
412 if (stat(tmpname
, &sb
) != -1) {
415 if (GetSizesFromPlist(tmpname
, &bandSize
, &devSize
) == -1) {
416 warnx("Existing sparse bundle can't be parsed");
420 printf("Existing sparse bundle size = %lld, bandsize = %zu\n", devSize
, bandSize
);
422 if (devSize
!= devp
->size
) {
423 warnx("Existing sparse bundle size (%lld) != dev size (%lld)", devSize
, devp
->size
);
426 ctx
.bandSize
= bandSize
;
428 FILE *fp
= fopen(tmpname
, "w");
430 warn("cannot create sparse bundle info plist %s", tmpname
);
433 ctx
.bandSize
= kBandSize
;
434 fprintf(fp
, bundlePrototype
, kBandSize
, devp
->size
);
436 sprintf(tmpname
, "%s/Info.bckup", ctx
.pathname
);
437 fp
= fopen(tmpname
, "w");
439 fprintf(fp
, bundlePrototype
, kBandSize
, devp
->size
);
442 sprintf(tmpname
, "%s/bands", ctx
.pathname
);
443 if (mkdir(tmpname
, 0777) == -1) {
444 warn("cannot create bands directory in sparse bundle %s", ctx
.pathname
);
447 sprintf(tmpname
, "%s/token", ctx
.pathname
);
448 close(open(tmpname
, O_CREAT
| O_TRUNC
, 0666));
451 retval
= malloc(sizeof(*retval
));
452 if (retval
== NULL
) {
457 retctx
= malloc(sizeof(*retctx
));
463 retval
->writer
= &WriteExtentToSparse
;
464 retval
->reader
= &doSparseRead
;
465 retval
->getprog
= &GetProgress
;
466 retval
->setprog
= &SetProgress
;
467 retval
->cleanup
= &doCleanup
;
469 retval
->context
= retctx
;
471 if (retval
== NULL
) {