Commit d0b9f55f authored by Leigh B. Stoller's avatar Leigh B. Stoller

Add several FRISBEE ifdefs to the user level unzip code. Rather than

duplicate this code in the frisbee tree, build a version suitable for
linking in with frisbee. I also modified the FrisbeeRead interface to
pass back pointers instead of copying the data. There is no real
performance benefit that I noticed, but it made me feel better not to
copy 350 MBs of data another time. There is new initialization
function that is called by the frisbee main program to set up a few
things.
parent 630b7764
...@@ -5,7 +5,7 @@ SUBDIR = os/imagezip ...@@ -5,7 +5,7 @@ SUBDIR = os/imagezip
include $(OBJDIR)/Makeconf include $(OBJDIR)/Makeconf
all: imagezip imageunzip all: imagezip imageunzip frisbee.o
include $(TESTBED_SRCDIR)/GNUmakerules include $(TESTBED_SRCDIR)/GNUmakerules
...@@ -18,6 +18,9 @@ imagezip: imagezip.o ...@@ -18,6 +18,9 @@ imagezip: imagezip.o
imageunzip: imageunzip.o imageunzip: imageunzip.o
$(CC) $(CFLAGS) imageunzip.o $(LIBS) -o imageunzip $(CC) $(CFLAGS) imageunzip.o $(LIBS) -o imageunzip
frisbee.o: imageunzip.c
$(CC) -c $(CFLAGS) -DFRISBEE -o frisbee.o $<
install: $(INSTALL_BINDIR)/imagezip $(INSTALL_BINDIR)/imageunzip install: $(INSTALL_BINDIR)/imagezip $(INSTALL_BINDIR)/imageunzip
client-install: client-install:
......
...@@ -25,10 +25,9 @@ ...@@ -25,10 +25,9 @@
* *
* These numbers are in sectors. * These numbers are in sectors.
*/ */
long outputminsec = 0; static long outputminsec = 0;
long outputmaxsec = 0; static long outputmaxsec = 0;
long long outputmaxsize = 0; /* Sanity check */ static long long outputmaxsize = 0; /* Sanity check */
int slice;
/* Why is this not defined in a public header file? */ /* Why is this not defined in a public header file? */
#define BOOT_MAGIC 0xAA55 #define BOOT_MAGIC 0xAA55
...@@ -42,13 +41,14 @@ int slice; ...@@ -42,13 +41,14 @@ int slice;
#define SECSIZE 512 #define SECSIZE 512
#define BSIZE (32 * 1024) #define BSIZE (32 * 1024)
#define OUTSIZE (1 * BSIZE) #define OUTSIZE (3 * BSIZE)
char inbuf[BSIZE], outbuf[OUTSIZE + SECSIZE], zeros[BSIZE]; char inbuf[BSIZE], outbuf[OUTSIZE + SECSIZE], zeros[BSIZE];
int infd, outfd; static int infd, outfd;
int doseek = 0; static int doseek = 0;
int debug = 0; static int debug = 0;
long long total = 0; static long long total = 0;
int inflate_subblock(void); int inflate_subblock(void);
void writezeros(off_t zcount); void writezeros(off_t zcount);
...@@ -90,10 +90,11 @@ usage(void) ...@@ -90,10 +90,11 @@ usage(void)
exit(1); exit(1);
} }
#ifndef FRISBEE
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int ch; int ch, slice = 0;
struct timeval stamp, estamp; struct timeval stamp, estamp;
while ((ch = getopt(argc, argv, "dhs:")) != -1) while ((ch = getopt(argc, argv, "dhs:")) != -1)
...@@ -186,6 +187,35 @@ main(int argc, char **argv) ...@@ -186,6 +187,35 @@ main(int argc, char **argv)
return 0; return 0;
} }
#else
/*
* When compiled for frisbee, act as a library.
*/
ImageUnzipInit(char *filename, int slice, int dbg)
{
if ((outfd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
perror("opening output file");
exit(1);
}
doseek = 1;
debug = dbg;
if (slice) {
off_t minseek;
if (readmbr(slice)) {
fprintf(stderr, "Failed to read MBR\n");
exit(1);
}
minseek = ((off_t) outputminsec) * SECSIZE;
if (lseek(outfd, minseek, SEEK_SET) < 0) {
perror("Setting seek pointer to slice");
exit(1);
}
}
}
#endif
int int
inflate_subblock(void) inflate_subblock(void)
...@@ -196,8 +226,8 @@ inflate_subblock(void) ...@@ -196,8 +226,8 @@ inflate_subblock(void)
struct blockhdr *blockhdr; struct blockhdr *blockhdr;
struct region *curregion; struct region *curregion;
off_t offset, size; off_t offset, size;
char buf[DEFAULTREGIONSIZE]; char *buf = inbuf;
d_stream.zalloc = (alloc_func)0; d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0; d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0; d_stream.opaque = (voidpf)0;
...@@ -212,7 +242,11 @@ inflate_subblock(void) ...@@ -212,7 +242,11 @@ inflate_subblock(void)
* Read the header. It is uncompressed, and holds the real * Read the header. It is uncompressed, and holds the real
* image size and the magic number. * image size and the magic number.
*/ */
#ifdef FRISBEE
if ((cc = FrisbeeRead(&buf, DEFAULTREGIONSIZE)) <= 0) {
#else
if ((cc = read(infd, buf, DEFAULTREGIONSIZE)) <= 0) { if ((cc = read(infd, buf, DEFAULTREGIONSIZE)) <= 0) {
#endif
if (cc == 0) if (cc == 0)
return 1; return 1;
perror("reading zipped image header goo"); perror("reading zipped image header goo");
...@@ -239,7 +273,14 @@ inflate_subblock(void) ...@@ -239,7 +273,14 @@ inflate_subblock(void)
/* /*
* Set the output pointer to the beginning of the region. * Set the output pointer to the beginning of the region.
*/ */
if (total != offset) { if (doseek) {
if (devlseek(outfd, offset, SEEK_SET) < 0) {
perror("Skipping to start of output region");
exit(1);
}
total += offset - total;
}
else {
assert(offset > total); assert(offset > total);
writezeros(offset - total); writezeros(offset - total);
} }
...@@ -256,7 +297,11 @@ inflate_subblock(void) ...@@ -256,7 +297,11 @@ inflate_subblock(void)
else else
count = blockhdr->size; count = blockhdr->size;
if ((cc = read(infd, inbuf, count)) <= 0) { #ifdef FRISBEE
if ((cc = FrisbeeRead(&buf, count)) <= 0) {
#else
if ((cc = read(infd, buf, count)) <= 0) {
#endif
if (cc == 0) { if (cc == 0) {
return 1; return 1;
} }
...@@ -266,7 +311,7 @@ inflate_subblock(void) ...@@ -266,7 +311,7 @@ inflate_subblock(void)
assert(cc == count); assert(cc == count);
blockhdr->size -= cc; blockhdr->size -= cc;
d_stream.next_in = inbuf; d_stream.next_in = buf;
d_stream.avail_in = cc; d_stream.avail_in = cc;
inflate_again: inflate_again:
/* /*
...@@ -321,8 +366,9 @@ inflate_subblock(void) ...@@ -321,8 +366,9 @@ inflate_subblock(void)
total += cc; total += cc;
assert(count >= 0); assert(count >= 0);
assert(size >= 0); assert(size >= 0);
#ifndef FRISBEE
assert(total == offset); assert(total == offset);
#endif
/* /*
* Hit the end of the region. Need to figure out * Hit the end of the region. Need to figure out
* where the next one starts. We write a block of * where the next one starts. We write a block of
...@@ -339,15 +385,20 @@ inflate_subblock(void) ...@@ -339,15 +385,20 @@ inflate_subblock(void)
if (!blockhdr->regioncount) if (!blockhdr->regioncount)
break; break;
offset = curregion->start * (off_t) SECSIZE; newoffset = curregion->start * (off_t) SECSIZE;
size = curregion->size * (off_t) SECSIZE; size = curregion->size * (off_t) SECSIZE;
assert(size); assert(size);
curregion++; curregion++;
blockhdr->regioncount--; blockhdr->regioncount--;
#ifdef FRISBEE
writezeros(newoffset - offset);
offset = newoffset;
#else
offset = newoffset;
assert(offset >= total); assert(offset >= total);
if (offset > total) if (offset > total)
writezeros(offset - total); writezeros(offset - total);
#endif
} }
} }
if (d_stream.avail_in) if (d_stream.avail_in)
...@@ -381,7 +432,9 @@ writezeros(off_t zcount) ...@@ -381,7 +432,9 @@ writezeros(off_t zcount)
exit(1); exit(1);
} }
total += zcount; total += zcount;
#ifndef FRISBEE
assert(offset == total + (((long long)outputminsec)*SECSIZE)); assert(offset == total + (((long long)outputminsec)*SECSIZE));
#endif
return; return;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment