]>
git.saurik.com Git - apple/hfs.git/blob - CopyHFSMeta/SparseBundle.c
455903eebec2fbf7b7c2e7003a393de4145f0e59
10 #include <sys/fcntl.h>
11 #include <removefile.h>
13 #include <CoreFoundation/CoreFoundation.h>
14 #include <System/sys/fsctl.h>
20 * Routines to maniupulate a sparse bundle.
21 * N.B.: The sparse bundle format it uses is a subset of
22 * the real sparse bundle format: no partition map, and
28 ({ __typeof(a) __a = (a); __typeof(b) __b = (b); \
29 __a < __b ? __a : __b; })
33 * Context for the sparse bundle routines. The path name,
34 * size of the band files, and cached file descriptor and
35 * band numbers, to reduce the amount of pathname lookups
38 struct SparseBundleContext
{
41 int cfd
; // Cached file descriptor
42 int cBandNum
; // cached bandfile number
45 static const int kBandSize
= 8388608;
47 // Prototype bundle Info.plist file
48 static const char *bundlePrototype
=
49 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
50 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
51 "<plist version=\"1.0\">\n"
53 "\t<key>CFBundleInfoDictionaryVersion</key>\n"
54 "\t<string>6.0</string>\n"
55 "\t<key>band-size</key>\n"
56 "\t<integer>%d</integer>\n"
57 "\t<key>bundle-backingstore-version</key>\n"
58 "\t<integer>1</integer>\n"
59 "\t<key>diskimage-bundle-type</key>\n"
60 "\t<string>com.apple.diskimage.sparsebundle</string>\n"
62 "\t<integer>%llu</integer>\n"
68 * Read from a sparse bundle. If the band file doesn't exist, or is shorter than
69 * what we need to get from it, we pad out with 0's.
72 doSparseRead(struct IOWrapper
*context
, off_t offset
, void *buffer
, off_t len
)
74 struct SparseBundleContext
*ctx
= context
->context
;
75 off_t blockSize
= ctx
->bandSize
;
80 off_t bandNum
= (offset
+ nread
) / blockSize
; // Which band file to use
81 off_t bandOffset
= (offset
+ nread
) % blockSize
; // how far to go into the file
82 size_t amount
= MIN(len
- nread
, blockSize
- bandOffset
); // How many bytes to write in this band file
88 asprintf(&bandName
, "%s/bands/%x", ctx
->pathname
, bandNum
);
89 fd
= open(bandName
, O_RDONLY
);
91 if (errno
== ENOENT
) {
92 // Doesn't exist, so we just write zeroes
94 memset(buffer
+ nread
, 0, amount
);
98 warn("Cannot open band file %s for offset %llu", bandName
, offset
+ nread
);
103 n
= pread(fd
, (char*)buffer
+ nread
, amount
, bandOffset
);
105 warn("Cannot write to band file %s/band/%x for offset %llu for amount %zu", ctx
->pathname
, bandNum
, offset
+nread
, amount
);
109 if (n
< amount
) { // hit EOF, pad out with zeroes
110 memset(buffer
+ nread
+ amount
, 0, amount
- n
);
121 * Write a chunk of data to a bundle.
124 doSparseWrite(IOWrapper_t
*context
, off_t offset
, void *buffer
, size_t len
)
126 struct SparseBundleContext
*ctx
= context
->context
;
127 off_t blockSize
= ctx
->bandSize
;
131 while (written
< len
) {
132 off_t bandNum
= (offset
+ written
) / blockSize
; // Which band file to use
133 off_t bandOffset
= (offset
+ written
) % blockSize
; // how far to go into the file
134 size_t amount
= MIN(len
- written
, blockSize
- bandOffset
); // How many bytes to write in this band file
139 if (ctx
->cfd
== -1 || ctx
->cBandNum
!= bandNum
) {
140 if (ctx
->cfd
!= -1) {
143 asprintf(&bandName
, "%s/bands/%x", ctx
->pathname
, bandNum
);
144 fd
= open(bandName
, O_WRONLY
| O_CREAT
, 0666);
146 warn("Cannot open band file %s for offset %llu", bandName
, offset
+ written
);
151 * When we create a new band file, we sync the volume
152 * it's on, so that we can ensure that the band file is present
153 * on disk. (Otherwise, with a crash, we can end up with the
154 * data not where we expected.) In this case, however, we probably
155 * don't need to wait for it -- just start the sync.
157 fsync_volume_np(fd
, 0);
158 fcntl(fd
, F_NOCACHE
, 1);
162 ctx
->cBandNum
= bandNum
;
166 nwritten
= pwrite(fd
, (char*)buffer
+ written
, amount
, bandOffset
);
167 if (nwritten
== -1) {
168 warn("Cannot write to band file %s/band/%x for offset %llu for amount %zu", ctx
->pathname
, bandNum
, offset
+written
, amount
);
174 // Sync the data out.
185 * Write a given extent (<start, length> pair) from an input device to the
186 * sparse bundle. We also use a block to update progress.
189 WriteExtentToSparse(struct IOWrapper
* context
, DeviceInfo_t
*devp
, off_t start
, off_t len
, void (^bp
)(off_t
))
191 const size_t bufSize
= 1024 * 1024;
192 uint8_t *buffer
= NULL
;
196 if (debug
) printf("Writing extent <%lld, %lld>\n", start
, len
);
197 buffer
= malloc(bufSize
);
198 if (buffer
== NULL
) {
199 warn("%s(%s): Could not allocate %zu bytes for buffer", __FILE__
, __FUNCTION__
, bufSize
);
204 while (total
< len
) {
207 size_t amt
= MIN(bufSize
, len
- total
);
208 nread
= UnalignedRead(devp
, buffer
, amt
, start
+ total
);
210 warn("Cannot read from device at offset %lld", start
+ total
);
215 warnx("Short read from source device -- got %zd, expected %zd", nread
, amt
);
217 nwritten
= doSparseWrite(context
, start
+ total
, buffer
, nread
);
218 if (nwritten
== -1) {
225 if (debug
) printf("\twrote %lld\n", total
);
232 static const CFStringRef kBandSizeKey
= CFSTR("band-size");
233 static const CFStringRef kDevSizeKey
= CFSTR("size");
236 * We need to be able to get the size of the "device" from a sparse bundle;
237 * we do this by using CF routines to parse the Info.plist file, and then
238 * get the two keys we care about: band-size (size of the band files), and
239 * size (size -- in bytes -- of the "disk").
242 GetSizesFromPlist(const char *path
, size_t *bandSize
, off_t
*devSize
)
245 CFReadStreamRef inFile
= NULL
;
246 CFURLRef inFileURL
= NULL
;
247 CFStringRef cfPath
= NULL
;
248 CFPropertyListRef cfDict
= NULL
;
249 CFNumberRef cfVal
= NULL
;
254 inFileURL
= CFURLCreateFromFileSystemRepresentation(NULL
, path
, strlen(path
), FALSE
);
255 if (inFileURL
== NULL
) {
256 if (debug
) warn("Cannot create url from pathname %s", path
);
260 inFile
= CFReadStreamCreateWithFile(NULL
, inFileURL
);
261 if (inFile
== NULL
) {
262 if (debug
) warn("cannot create read stream from path %s", path
);
266 if (CFReadStreamOpen(inFile
) == FALSE
) {
267 if (debug
) warn("cannot open read stream");
271 cfDict
= CFPropertyListCreateWithStream(NULL
, inFile
, 0, 0, NULL
, NULL
);
272 if (cfDict
== NULL
) {
273 if (debug
) warnx("cannot create propertly list from stream for path %s", path
);
277 cfVal
= CFDictionaryGetValue(cfDict
, kBandSizeKey
);
279 if (debug
) warnx("cannot get bandsize key from plist");
283 if (CFNumberGetValue(cfVal
, kCFNumberIntType
, &tmpInt
) == false) {
284 if (debug
) warnx("cannot get value from band size number");
290 cfVal
= CFDictionaryGetValue(cfDict
, kDevSizeKey
);
292 if (debug
) warnx("cannot get dev size key from plist");
295 if (CFNumberGetValue(cfVal
, kCFNumberLongLongType
, &tmpLL
) == false) {
307 CFRelease(inFileURL
);
315 #define kProgressName "HC.progress.txt"
318 * Get the progress state from a sparse bundle. If it's not there, then
322 GetProgress(struct IOWrapper
*context
)
324 struct SparseBundleContext
*ctx
= context
->context
;
327 char progFile
[strlen(ctx
->pathname
) + sizeof(kProgressName
) + 2]; // '/' and NUL
329 sprintf(progFile
, "%s/%s", ctx
->pathname
, kProgressName
);
330 fp
= fopen(progFile
, "r");
334 if (fscanf(fp
, "%llu", &retval
) != 1) {
343 * Write the progress information out. This involves writing a file in
344 * the sparse bundle with the amount -- in bytes -- we've written so far.
347 SetProgress(struct IOWrapper
*context
, off_t prog
)
349 struct SparseBundleContext
*ctx
= context
->context
;
351 char progFile
[strlen(ctx
->pathname
) + sizeof(kProgressName
) + 2]; // '/' and NUL
353 sprintf(progFile
, "%s/%s", ctx
->pathname
, kProgressName
);
357 fp
= fopen(progFile
, "w");
359 (void)fprintf(fp
, "%llu\n", prog
);
367 * Clean up. This is used when we have to initialize the bundle, but don't
368 * have any progress information -- in that case, we don't want to have any
369 * of the old band files laying around. We use removefile() to recursively
370 * remove them, but keep the bands directory.
373 doCleanup(struct IOWrapper
*ctx
)
375 struct SparseBundleContext
*context
= ctx
->context
;
377 char bandsDir
[strlen(context
->pathname
) + sizeof("/bands") + 1]; // 1 for NUL
379 sprintf(bandsDir
, "%s/bands", context
->pathname
);
382 fprintf(stderr
, "Cleaning up, about to call removefile\n");
383 rv
= removefile(bandsDir
, NULL
, REMOVEFILE_RECURSIVE
| REMOVEFILE_KEEP_PARENT
);
385 fprintf(stderr
, "removefile returned %d\n", rv
);
387 return (rv
== 0) ? 0 : -1;
391 * Initialize the IOWrapper structure for a sparse bundle. This will
392 * create the bundle directory (but not its parents!) if needed, and
393 * will populate it out. It checks to see if there is an existing bundle
394 * of the same name, and, if so, ensures that the izes are correct. Then
395 * it sets up all the function pointers.
398 InitSparseBundle(const char *path
, DeviceInfo_t
*devp
)
400 struct SparseBundleContext ctx
= { 0 };
401 struct SparseBundleContext
*retctx
= NULL
;
402 IOWrapper_t
*retval
= NULL
;
404 char tmpname
[strlen(path
) + sizeof("Info.plist") + 2]; // '/' + NUL
406 if (strstr(path
, ".sparsebundle") == NULL
) {
407 asprintf(&ctx
.pathname
, "%s.sparsebundle", path
);
409 ctx
.pathname
= strdup(path
);
412 if (lstat(ctx
.pathname
, &sb
) == -1) {
413 if (errno
!= ENOENT
) {
414 warn("cannot check sparse bundle %s", ctx
.pathname
);
417 if (mkdir(ctx
.pathname
, 0777) == -1) {
418 warn("cannot create sparse bundle %s", ctx
.pathname
);
421 } else if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
) {
422 warnx("sparse bundle object %s is not a directory", ctx
.pathname
);
425 sprintf(tmpname
, "%s/Info.plist", ctx
.pathname
);
426 if (stat(tmpname
, &sb
) != -1) {
429 if (GetSizesFromPlist(tmpname
, &bandSize
, &devSize
) == -1) {
430 warnx("Existing sparse bundle can't be parsed");
434 printf("Existing sparse bundle size = %lld, bandsize = %zu\n", devSize
, bandSize
);
436 if (devSize
!= devp
->size
) {
437 warnx("Existing sparse bundle size (%lld) != dev size (%lld)", devSize
, devp
->size
);
440 ctx
.bandSize
= bandSize
;
442 FILE *fp
= fopen(tmpname
, "w");
444 warn("cannot create sparse bundle info plist %s", tmpname
);
447 ctx
.bandSize
= kBandSize
;
448 fprintf(fp
, bundlePrototype
, kBandSize
, devp
->size
);
450 sprintf(tmpname
, "%s/Info.bckup", ctx
.pathname
);
451 fp
= fopen(tmpname
, "w");
453 fprintf(fp
, bundlePrototype
, kBandSize
, devp
->size
);
456 sprintf(tmpname
, "%s/bands", ctx
.pathname
);
457 if (mkdir(tmpname
, 0777) == -1) {
458 warn("cannot create bands directory in sparse bundle %s", ctx
.pathname
);
461 sprintf(tmpname
, "%s/token", ctx
.pathname
);
462 close(open(tmpname
, O_CREAT
| O_TRUNC
, 0666));
465 retval
= malloc(sizeof(*retval
));
466 if (retval
== NULL
) {
471 retctx
= malloc(sizeof(*retctx
));
477 retval
->writer
= &WriteExtentToSparse
;
478 retval
->reader
= &doSparseRead
;
479 retval
->getprog
= &GetProgress
;
480 retval
->setprog
= &SetProgress
;
481 retval
->cleanup
= &doCleanup
;
483 retval
->context
= retctx
;
485 if (retval
== NULL
) {