}
static Opaque
-writeNode(WorkPlain *wp, int fd, SFSNode *node) {
+writeNode(WorkPlain *wp, int fd, SFSNode *node, u_int32_t extrasize) {
int size = getNodeSize(wp->info, node);
SFSNode *wnode = (SFSNode*)tmalloc(size);
off_t myoffset = wp->offset;
if ( node->isskip ) {
if (node->haschild)
*(Opaque*)( ((char*)wnode) + SFSNHRDSZ) =
- writeNode(wp, fd, getSkipChildPointer(wp->info, node));
+ writeNode(wp, fd, getSkipChildPointer(wp->info, node), extrasize);
} else {
SFSNodeData *nd = node->data;
u_int32_t i;
for(i=0;i<node->nchar;i++) {
if (nd->haschild)
*(Opaque*)(((char*)wnode) + ( ((char*)nd) - ((char*)node) ) + nd->child) =
- writeNode(wp, fd, getChildPointer( wp->info, nd ) );
+ writeNode(wp, fd, getChildPointer( wp->info, nd ), extrasize );
nd++;
}
}
- if ( lseek(fd, myoffset + SFSTDHSZ, SEEK_SET) < 0 )
+ if ( lseek(fd, myoffset + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
if ( write(fd, wnode, size) != size )
tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
}
void
-SFSWriteDump(SFSTree *info, char *filename) {
+SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize) {
int fd;
off_t size = info->totalen + SFSTDHSZ;
SFSTreeDumpHeader dh;
if ( flock(fd, LOCK_EX) < 0 )
tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
- if ( lseek(fd, size, SEEK_SET) < 0 )
+
+ if ( extrasize == 0 )
+ extradata = NULL;
+ else if ( extradata == NULL )
+ extrasize = 0;
+
+ if ( lseek(fd, size + MAXALIGN(extrasize) , SEEK_SET) < 0 )
tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
dh.version = SFSTREE_VERSION;
dh.datasize = info->datasize;
dh.totalen = info->totalen;
dh.nnodes = info->nnodes;
+ dh.extrasize = extrasize;
if ( lseek(fd, 0, SEEK_SET) < 0 )
tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
if ( write(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
+ if ( extrasize ) {
+ if ( write(fd, extradata, extrasize) != extrasize )
+ tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
+ if ( extrasize != MAXALIGN(extrasize) ) {
+ char dummy[8] = {0,0,0,0,0,0,0,0};
+ if ( write(fd, dummy, MAXALIGN(extrasize) - extrasize ) != MAXALIGN(extrasize) - extrasize )
+ tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
+
+ extrasize = MAXALIGN(extrasize);
+ }
+ }
+
if ( info->plainmemory ) {
if ( write(fd, info->node, info->totalen) != info->totalen )
tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
wp.info = info;
wp.offset = 0;
- writeNode(&wp, fd, info->node);
+ writeNode(&wp, fd, info->node, extrasize);
}
flock(fd, LOCK_UN);
}
static SFSNode*
-readNode(SFSTree *info, int fd, char *buf, int bufsize) {
+readNode(SFSTree *info, int fd, char *buf, int bufsize, int extrasize) {
SFSNode *node;
int size;
if ( node->isskip ) {
if (node->haschild) {
- if ( lseek(fd, *(Opaque*)node->data + SFSTDHSZ, SEEK_SET) < 0 )
+ if ( lseek(fd, *(Opaque*)node->data + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
*(SFSNode**)( (char*)(node->data) ) =
- readNode(info, fd, buf, bufsize);
+ readNode(info, fd, buf, bufsize, extrasize);
}
} else {
SFSNodeData *nd = node->data;
for(i=0;i<node->nchar;i++) {
if (nd->haschild) {
- if ( lseek(fd, *(Opaque*)(((char*)nd) + nd->child ) + SFSTDHSZ, SEEK_SET) < 0 )
+ if ( lseek(fd, *(Opaque*)(((char*)nd) + nd->child ) + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
*(SFSNode**) (((char*)nd) + nd->child ) =
- readNode(info, fd, buf, bufsize);
+ readNode(info, fd, buf, bufsize, extrasize);
}
nd++;
}
}
void
-SFSReadDump(SFSTree *info, char *filename) {
+SFSReadDump(SFSTree *info, char *filename, void **extradata, u_int32_t *extrasize) {
int fd;
SFSTreeDumpHeader dh;
char *buf;
- int bufsize;
-
+ int bufsize;
+
+ if ( extradata )
+ *extradata = NULL;
+ if (extrasize )
+ *extrasize = 0;
+
memset(info,0,sizeof(SFSTree));
if ( (fd = open(filename, O_RDONLY)) < 0 )
info->nnodes = dh.nnodes;
info->datasize = dh.datasize;
+ if ( dh.extrasize ) {
+ void *pointer = tmalloc( MAXALIGN(dh.extrasize) );
+
+ if ( read(fd, pointer, MAXALIGN(dh.extrasize)) != MAXALIGN(dh.extrasize) )
+ tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
+
+ if ( extradata )
+ *extradata = pointer;
+ else
+ tfree(pointer);
+
+ if ( extrasize )
+ *extrasize = dh.extrasize;
+ }
+
/* allocate buffer with max allowed size */
bufsize = SFSNHRDSZ + 256*(sizeof(SFSNodeData) + sizeof(void*) + info->datasize);
buf = tmalloc( bufsize );
- info->node = readNode(info, fd, buf, bufsize);
+ info->node = readNode(info, fd, buf, bufsize, MAXALIGN(dh.extrasize));
tfree(buf);
flock(fd, LOCK_UN);
}
void
-SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size) {
+SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size, void **extradata, u_int32_t *extrasize) {
SFSTreeDumpHeader *dh;
+ if ( extradata )
+ *extradata = NULL;
+ if (extrasize )
+ *extrasize = 0;
+
memset(info,0,sizeof(SFSTree));
if ( size && size < SFSTDHSZ )
info->datasize = dh->datasize;
info->plainmemory = 1;
+ if ( dh->extrasize ) {
+ if ( extradata )
+ *extradata = ((char*)pointer) + SFSTDHSZ;
+
+ if ( extrasize )
+ *extrasize = dh->extrasize;
+ }
+
if ( info->totalen && info->nnodes )
- info->node = (SFSNode*)( ((char*)pointer) + SFSTDHSZ );
+ info->node = (SFSNode*)( ((char*)pointer) + SFSTDHSZ + MAXALIGN(dh->extrasize) );
}