From fcc49648fd74a07c50a9b0f8eab0b4aa64bab6ed Mon Sep 17 00:00:00 2001 From: ewt Date: Tue, 19 Dec 1995 16:17:39 +0000 Subject: [PATCH] Added routines for traversal CVS patchset: 60 CVS date: 1995/12/19 16:17:39 --- lib/falloc.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- lib/falloc.h | 7 +++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/falloc.c b/lib/falloc.c index 3f6ac14..cda0d77 100644 --- a/lib/falloc.c +++ b/lib/falloc.c @@ -51,8 +51,10 @@ faFile faOpen(char * path, int flags, int perms) { return NULL; } fas.firstFree = 0; + fas.fileSize = sizeof(newHdr); } else { + lseek(fas.fd, 0, SEEK_SET); if (read(fas.fd, &newHdr, sizeof(newHdr)) != sizeof(newHdr)) { close(fas.fd); return NULL; @@ -62,6 +64,13 @@ faFile faOpen(char * path, int flags, int perms) { return NULL; } fas.firstFree = newHdr.firstFree; + + if (lseek(fas.fd, 0, SEEK_END) < 0) { + close(fas.fd); + return NULL; + } + + fas.fileSize = lseek(fas.fd, 0, SEEK_CUR); } fa = malloc(sizeof(*fa)); @@ -170,8 +179,7 @@ unsigned int faAlloc(faFile fa, unsigned int size) { /* returns 0 on failure */ char * space; /* make a new block */ - if (lseek(fa->fd, 0, SEEK_END) < 0) return 0; - newBlock = lseek(fa->fd, 0, SEEK_CUR); + newBlock = fa->fileSize; space = calloc(1, size); if (!space) return 0; @@ -189,6 +197,8 @@ unsigned int faAlloc(faFile fa, unsigned int size) { /* returns 0 on failure */ return 0; } free(space); + + fa->fileSize += sizeof(block) + size; } return newBlock + sizeof(block); @@ -200,3 +210,39 @@ void faClose(faFile fa) { close(fa->fd); free(fa); } + +unsigned int faFirstOffset(faFile fa) { + return faNextOffset(fa, 0); +} + +unsigned int faNextOffset(faFile fa, unsigned int lastOffset) { + struct faBlock block; + int offset; + int useBlock; + + if (lastOffset) { + offset = lastOffset - sizeof(block); + useBlock = 0; + } else { + offset = sizeof(struct faFileHeader); + useBlock = 1; + } + + block.isFree = 1; + while (offset < fa->fileSize && block.isFree) { + lseek(fa->fd, offset, SEEK_SET); + if (read(fa->fd, &block, sizeof(block)) != sizeof(block)) { + return 0; + } + + if (useBlock && !block.isFree) break; + useBlock = 1; + + offset += sizeof(block) + block.size; + } + + if (offset < fa->fileSize) + return (offset + sizeof(block)); + else + return 0; +} diff --git a/lib/falloc.h b/lib/falloc.h index c633999..49c2add 100644 --- a/lib/falloc.h +++ b/lib/falloc.h @@ -9,12 +9,19 @@ typedef struct faFile_s { int fd; int readOnly; unsigned int firstFree; + unsigned long fileSize; } * faFile; +struct FaPlace_s; +typedef struct FaPlace * faPlace; + /* flags here is the same as for open(2) - NULL returned on error */ faFile faOpen(char * path, int flags, int perms); unsigned int faAlloc(faFile fa, unsigned int size); /* returns 0 on failure */ void faFree(faFile fa, unsigned int offset); void faClose(faFile fa); +unsigned int faFirstOffset(faFile fa); +unsigned int faNextOffset(faFile fa, unsigned int lastOffset); /* 0 at end */ + #endif H_FALLOC -- 2.7.4