//Write final dummy header with FLAG_LASTFILE set. void finishArchive() { EspFsHeader h; h.magic=('E'<<0)+('S'<<8)+('f'<<16)+('s'<<24); h.flags=FLAG_LASTFILE; h.compression=COMPRESS_NONE; h.nameLen=htoxs(0); h.fileLenComp=htoxl(0); h.fileLenDecomp=htoxl(0); write(1, &h, sizeof(EspFsHeader)); }
int handleFile(int f, char *name, int compression, int level) { char *fdat, *cdat; off_t size, csize; EspFsHeader h; int nameLen; size=lseek(f, 0, SEEK_END); fdat=mmap(NULL, size, PROT_READ, MAP_SHARED, f, 0); if (fdat==MAP_FAILED) { perror("mmap"); return 0; } if (compression==COMPRESS_NONE) { csize=size; cdat=fdat; } else if (compression==COMPRESS_HEATSHRINK) { cdat=malloc(size*2); csize=compressHeatshrink(fdat, size, cdat, size*2, level); } else { fprintf(stderr, "Unknown compression - %d\n", compression); exit(1); } if (csize>size) { //Compressing enbiggened this file. Revert to uncompressed store. compression=COMPRESS_NONE; csize=size; cdat=fdat; } //Fill header data h.magic=('E'<<0)+('S'<<8)+('f'<<16)+('s'<<24); h.flags=0; h.compression=compression; nameLen=strlen(name)+1; if (nameLen&3) nameLen+=4-(nameLen&3); //Round to next 32bit boundary h.nameLen=htoxs(nameLen); h.fileLenComp=htoxl(csize); h.fileLenDecomp=htoxl(size); write(1, &h, sizeof(EspFsHeader)); write(1, name, nameLen); //ToDo: this can eat up a few bytes after the buffer. write(1, cdat, csize); //Pad out to 32bit boundary while (csize&3) { write(1, "\000", 1); csize++; } munmap(fdat, size); return (csize*100)/size; }
int handleFile(int f, char *name, int compression, int level, char **compName) { char *fdat, *cdat; off_t size, csize; EspFsHeader h; int nameLen; int8_t flags = 0; size=lseek(f, 0, SEEK_END); fdat=mmap(NULL, size, PROT_READ, MAP_SHARED, f, 0); if (fdat==MAP_FAILED) { perror("mmap"); return 0; } #ifdef ESPFS_GZIP if (shouldCompressGzip(name)) { csize = size*3; if (csize<100) // gzip has some headers that do not fit when trying to compress small files csize = 100; // enlarge buffer if this is the case cdat=malloc(csize); csize=compressGzip(fdat, size, cdat, csize, level); compression = COMPRESS_NONE; flags = FLAG_GZIP; } else #endif if (compression==COMPRESS_NONE) { csize=size; cdat=fdat; #ifdef ESPFS_HEATSHRINK } else if (compression==COMPRESS_HEATSHRINK) { cdat=malloc(size*2); csize=compressHeatshrink(fdat, size, cdat, size*2, level); #endif } else { fprintf(stderr, "Unknown compression - %d\n", compression); exit(1); } if (csize>size) { //Compressing enbiggened this file. Revert to uncompressed store. compression=COMPRESS_NONE; csize=size; cdat=fdat; flags=0; } //Fill header data h.magic=('E'<<0)+('S'<<8)+('f'<<16)+('s'<<24); h.flags=flags; h.compression=compression; h.nameLen=nameLen=strlen(name)+1; if (h.nameLen&3) h.nameLen+=4-(h.nameLen&3); //Round to next 32bit boundary h.nameLen=htoxs(h.nameLen); h.fileLenComp=htoxl(csize); h.fileLenDecomp=htoxl(size); write(1, &h, sizeof(EspFsHeader)); write(1, name, nameLen); while (nameLen&3) { write(1, "\000", 1); nameLen++; } write(1, cdat, csize); //Pad out to 32bit boundary while (csize&3) { write(1, "\000", 1); csize++; } munmap(fdat, size); if (compName != NULL) { if (h.compression==COMPRESS_HEATSHRINK) { *compName = "heatshrink"; } else if (h.compression==COMPRESS_NONE) { if (h.flags & FLAG_GZIP) { *compName = "gzip"; } else { *compName = "none"; } } else { *compName = "unknown"; } } return (csize*100)/size; }