diff --git a/src/sys/ubixfsv2/btree.cpp b/src/sys/ubixfsv2/btree.cpp index 7812af2..ee90d62 100644 --- a/src/sys/ubixfsv2/btree.cpp +++ b/src/sys/ubixfsv2/btree.cpp @@ -11,9 +11,9 @@ #define VERIFY(x, y, z, n) if ((x) != (y)) { cout << "verify " << z << " failed" << endl; PrintWholeTree(); } bTree::bTree(ubixfsInode * inode) : root(NULL) { - treeDepth = 1; - treeWidth = 0; - treeLeafCount = 0; + header.treeDepth = 1; + header.treeWidth = 0; + header.treeLeafCount = 0; if (inode == NULL) return; root = allocEmptyNode(); if (root == NULL) return; @@ -45,9 +45,9 @@ // note: this code is right out of the constructor if (root == NULL) { - treeDepth = 1; - treeWidth = 0; - treeLeafCount = 0; + header.treeDepth = 1; + header.treeWidth = 0; + header.treeLeafCount = 0; root = allocEmptyNode(); assert(root); if (root == NULL) return false; @@ -72,7 +72,7 @@ // PrintWholeTree(); // cout << "Insert(" << inode->name << ")" << endl; //Info(bnode); - ++treeLeafCount; + ++header.treeLeafCount; /* * Find the leaf node the inode goes into */ @@ -234,7 +234,7 @@ bnode->childCount[curSlot] = (B_MAX_CHILD_COUNT+1) >> 1; bnode->childCount[curSlot+1] -= bnode->childCount[curSlot]; bnode->present[curSlot] = true; - ++treeWidth; + ++header.treeWidth; if (++bnode->used == B_MAX_KEYS) splitNode(bnode); } // if leaf is full @@ -313,7 +313,7 @@ if (oldNode == root) { // allocate a new root node - ++treeDepth; + ++header.treeDepth; root = allocEmptyNode(); oldNode->parent = root; newNode->parent = root; @@ -609,10 +609,31 @@ return NULL; } // bTree::findLeafNode +off_t +bTree::saveNode(FILE * fd, bNode * node, void * tmpPtr, off_t parent) { + if (fd == NULL) return -1; + off_t myOffset = ftello(fd); + + for (int i = 0; i < node->used; i++) { + } // for i + +} // bTree::saveNode bool bTree::Save(const char * filename) { + ubixfsInode * uPtr = NULL; if (filename == NULL) return false; + FILE * fd = NULL; + if ((fd = fopen(filename, "wb+")) == NULL) return false; + + header.firstNodeOffset = 4096; + header.firstDeleted = -1; + void * tmpPtr = malloc(4096); + uPtr = (ubixfsInode *)tmpPtr; + memset(tmpPtr, 0, 4096); + memcpy(tmpPtr, &header, sizeof(header)); + fwrite(tmpPtr, 4096, 1, fd); + fclose(fd); return true; } // bTree::Save @@ -661,7 +682,7 @@ } // bTree::PrintWholeTree; bTree::~bTree(void) { - cout << "tree depth: " << treeDepth << endl; - cout << "tree width: " << treeWidth << endl; - cout << "tree leaf count: " << treeLeafCount << endl; + cout << "tree depth: " << header.treeDepth << endl; + cout << "tree width: " << header.treeWidth << endl; + cout << "tree leaf count: " << header.treeLeafCount << endl; } // bTree::~bTree diff --git a/src/sys/ubixfsv2/btree.h b/src/sys/ubixfsv2/btree.h index d20591b..3dfda94 100644 --- a/src/sys/ubixfsv2/btree.h +++ b/src/sys/ubixfsv2/btree.h @@ -3,6 +3,7 @@ #include "superblock.h" #include "inode.h" +#include "btreeheader.h" #define B_NODE_MAGIC_1 0xDEADBEEF #define B_NODE_MAGIC_2 0x1900BABE @@ -29,20 +30,10 @@ } bNode; // bNode -typedef struct bTreeHeader { - uInt32 treeDepth; - uInt32 treeWidth; - uInt32 treeLeafCount; - int32 firstDeleted; -} bTreeHeader; // bTreeHeader - class bTree { protected: bNode * root; - uInt32 treeDepth; - uInt32 treeWidth; - uInt32 treeLeafCount; - int32 firstDeleted; + bTreeHeader header; ubixfsInode * treeSearch(bNode *, const char *); ubixfsInode * inodeSearch(ubixfsInode *, const char *); void splitNode(bNode *); @@ -50,9 +41,10 @@ void insertNode(bNode *, const char *, void *, void *); bNode * findLeafNode(bNode *, const char *); void Print(bNode *); + off_t saveNode(FILE *, bNode *, void *, off_t); public: bTree(ubixfsInode *); - bTree(void) : root(NULL), treeDepth(0) { }; + bTree(void) : root(NULL) { }; ubixfsInode * Find(const char *); ubixfsInode * GetFirstNode(void); ubixfsInode * GetFirstNode(bNode *); diff --git a/src/sys/ubixfsv2/btreeheader.h b/src/sys/ubixfsv2/btreeheader.h new file mode 100644 index 0000000..2aa7267 --- /dev/null +++ b/src/sys/ubixfsv2/btreeheader.h @@ -0,0 +1,12 @@ +#ifndef BTREEHEADER_H +#define BTREEHEADER_H + +typedef struct bTreeHeader { + uInt32 treeDepth; + uInt32 treeWidth; + uInt32 treeLeafCount; + uInt32 firstNodeOffset; // used when tree is on disk + int32 firstDeleted; // used to point to an empty node +} bTreeHeader; // bTreeHeader + +#endif /* !BTREEHEADER_H */ diff --git a/src/sys/ubixfsv2/ubixfs.cpp b/src/sys/ubixfsv2/ubixfs.cpp index ef9c28c..00e0ef0 100644 --- a/src/sys/ubixfsv2/ubixfs.cpp +++ b/src/sys/ubixfsv2/ubixfs.cpp @@ -60,7 +60,7 @@ if ((*ptr & subCount) == 0) break; subCount >>= 1; ++count; - if (count == numBlocks) { + if (count == superBlock->numBlocks) { count = 0; ptr = freeBlockList; goto rescan; @@ -116,14 +116,21 @@ endPtr = freeBlockList + (superBlock->numBlocks >> 3); bool secondTime = false; - while (*ptr != 0 && ptr < endPtr) { + while (*ptr != 0) { ++ptr; count += 8; if (ptr == endPtr) { + if (secondTime) + return -1; + else + secondTime = true; + count = 0; - + ptr = freeBlockList; } // if } // while + *ptr = -1; + return count; } // UbixFS::get8FreeBlocks int UbixFS::format(dev_t * dev) {