diff --git a/include/fs.h b/include/fs.h index 04561af..19ba178 100644 --- a/include/fs.h +++ b/include/fs.h @@ -21,16 +21,16 @@ public: ~SuperBlock(){ } - static off_t getFreeListHead(RawDisk &disk){ + static u_int64_t getFreeListHead(RawDisk &disk){ char buffer[8] = {0}; disk.rawdisk_read(0, buffer, sizeof(buffer)); - off_t t = 0; + u_int64_t t = 0; for (int j = 0; j < 8; j++) - t = t | (((off_t)buffer[j])<<(8*j)); + t = t | (((u_int64_t)buffer[j])<<(8*j)); return t; } - static void writeFreeListHead(RawDisk &disk, off_t t){ + static void writeFreeListHead(RawDisk &disk, u_int64_t t){ char buffer[8] = {0}; for (int j = 0; j < 8; j++){ buffer[j] = (t >> (8 * j)) & 0xFF; @@ -38,16 +38,16 @@ public: disk.rawdisk_write(0, buffer, sizeof(buffer)); } - static off_t getFreeINodeHead(RawDisk &disk){ + static u_int64_t getFreeINodeHead(RawDisk &disk){ char buffer[8] = {0}; disk.rawdisk_read(8, buffer, sizeof(buffer)); - off_t t = 0; + u_int64_t t = 0; for (int j = 0; j < 8; j++) - t = t | (((off_t)buffer[j])<<(8*j)); + t = t | (((u_int64_t)buffer[j])<<(8*j)); return t; } - static void writeFreeINodeHead(RawDisk &disk, off_t t){ + static void writeFreeINodeHead(RawDisk &disk, u_int64_t t){ char buffer[8] = {0}; for (int j = 0; j < 8; j++){ buffer[j] = (t >> (8 * j)) & 0xFF; @@ -58,33 +58,33 @@ public: class INode{ // direct datablocks - off_t blocks[48]; + u_int64_t blocks[48]; // indirect address - off_t single_indirect, double_indirect, triple_indirect; + u_int64_t single_indirect, double_indirect, triple_indirect; // other - off_t uid; - off_t gid; - off_t permissions; - off_t size; - off_t block_number; + u_int64_t uid; + u_int64_t gid; + u_int64_t permissions; + u_int64_t size; + u_int64_t block_number; public: - void read_get_byte(off_t &t, int ¤t_pos, char *buffer){ + void read_get_byte(u_int64_t &t, int ¤t_pos, char *buffer){ t = 0; for (int j = 0; j < 8; j++) - t = t | (((off_t)buffer[j+current_pos])<<(8*j)); + t = t | (((u_int64_t)buffer[j+current_pos])<<(8*j)); current_pos += 8; } - static off_t read_byte_at(int current_pos, char *buffer){ - off_t t = 0; + static u_int64_t read_byte_at(int current_pos, char *buffer){ + u_int64_t t = 0; for (int j = 0; j < 8; j++) - t = t | (((off_t)buffer[j+current_pos])<<(8*j)); + t = t | (((u_int64_t)buffer[j+current_pos])<<(8*j)); return t; } - void inode_construct(off_t blockNumber, RawDisk &disk){ + void inode_construct(u_int64_t blockNumber, RawDisk &disk){ char buffer[SECTOR_SIZE] = {0}; disk.rawdisk_read(blockNumber*SECTOR_SIZE, buffer, sizeof(buffer)); block_number = blockNumber; @@ -102,17 +102,17 @@ public: read_get_byte(size, current_pos, buffer); } - void write_get_byte(off_t t, int ¤t_pos, char *buffer){ + void write_get_byte(u_int64_t t, int ¤t_pos, char *buffer){ for (int j = 0; j < 8; j++){ - buffer[j+current_pos] = t & (((off_t)1<<(8))-1); + buffer[j+current_pos] = t & (((u_int64_t)1<<(8))-1); t >>= 8; } current_pos += 8; } - static void write_byte_at(off_t t, int current_pos, char *buffer){ + static void write_byte_at(u_int64_t t, int current_pos, char *buffer){ for (int j = 0; j < 8; j++){ - buffer[j+current_pos] = t & (((off_t)1<<(8))-1); + buffer[j+current_pos] = t & (((u_int64_t)1<<(8))-1); t >>= 8; } } @@ -133,16 +133,16 @@ public: disk.rawdisk_write(block_number*SECTOR_SIZE, buffer, sizeof(buffer)); } - off_t datablock_allocate_in_list(RawDisk &disk){ + u_int64_t datablock_allocate_in_list(RawDisk &disk){ //find a free data block - off_t freeListHead = SuperBlock::getFreeListHead(disk); + u_int64_t freeListHead = SuperBlock::getFreeListHead(disk); /* 1. initialization 2. data block starting position 3. r/w between storage and rawdisk to maintain */ char buffer[IO_BLOCK_SIZE] = {0}; - off_t freeBlockNum = 0; + u_int64_t freeBlockNum = 0; disk.rawdisk_read(freeListHead, buffer, sizeof(buffer)); for (int i = 8; i < 264; i++){ if(buffer[i] != 255){ @@ -165,13 +165,13 @@ public: } } if (!notFull){ - off_t next_header = read_byte_at(0, buffer); + u_int64_t next_header = read_byte_at(0, buffer); SuperBlock::writeFreeListHead(disk, next_header); } return freeBlockNum; } - bool allo_single_indirect(RawDisk &disk, off_t &single_i, off_t freeBlockNum) { + bool allo_single_indirect(RawDisk &disk, u_int64_t &single_i, u_int64_t freeBlockNum) { if (single_i == 0){ single_i = datablock_allocate_in_list(disk); } @@ -179,7 +179,7 @@ public: char buffer[IO_BLOCK_SIZE] = {0}; disk.rawdisk_read(single_i, buffer, sizeof(buffer)); for (int i = 0; i < IO_BLOCK_SIZE; i+=8){ - off_t addr = read_byte_at(i, buffer); + u_int64_t addr = read_byte_at(i, buffer); if(addr == 0){ inSingle = true; write_byte_at(freeBlockNum, i, buffer); @@ -190,7 +190,7 @@ public: return inSingle; } - bool allo_double_indirect(RawDisk &disk, off_t &double_i, off_t freeBlockNum) { + bool allo_double_indirect(RawDisk &disk, u_int64_t &double_i, u_int64_t freeBlockNum) { if (double_i == 0){ double_i = datablock_allocate_in_list(disk); } @@ -198,7 +198,7 @@ public: char buffer[IO_BLOCK_SIZE] = {0}; disk.rawdisk_read(double_i, buffer, sizeof(buffer)); for (int i = 0; i < IO_BLOCK_SIZE; i+=8){ - off_t addr = read_byte_at(i, buffer); + u_int64_t addr = read_byte_at(i, buffer); bool flag = allo_single_indirect(disk, addr, freeBlockNum); if (flag){ write_byte_at(addr, i, buffer); @@ -210,7 +210,7 @@ public: return inDouble; } - bool allo_triple_indirect(RawDisk &disk, off_t &triple_i, off_t freeBlockNum) { + bool allo_triple_indirect(RawDisk &disk, u_int64_t &triple_i, u_int64_t freeBlockNum) { if (triple_i == 0){ triple_i = datablock_allocate_in_list(disk); } @@ -218,7 +218,7 @@ public: char buffer[IO_BLOCK_SIZE] = {0}; disk.rawdisk_read(triple_i, buffer, sizeof(buffer)); for (int i = 0; i < IO_BLOCK_SIZE; i+=8){ - off_t addr = read_byte_at(i, buffer); + u_int64_t addr = read_byte_at(i, buffer); bool flag = allo_double_indirect(disk, addr, freeBlockNum); if (flag){ write_byte_at(addr, i, buffer); @@ -232,11 +232,11 @@ public: // allowcate 1 datablock and add to the end of the file - off_t datablock_allocate(RawDisk &disk){ + u_int64_t datablock_allocate(RawDisk &disk){ //do we need to check dynamic? //add the data block to blocks, single, double, triple - off_t freeBlockNum = datablock_allocate_in_list(disk); + u_int64_t freeBlockNum = datablock_allocate_in_list(disk); bool inBlocks = false; for (int i = 0; i < 48; i++) if(blocks[i] == 0){ @@ -260,9 +260,9 @@ public: return freeBlockNum; } - void datablock_deallocate_in_list(off_t freeBlockNum, RawDisk &disk) { + void datablock_deallocate_in_list(u_int64_t freeBlockNum, RawDisk &disk) { // find the related 2048block head - off_t freeBlockHead = ((freeBlockNum/SECTOR_SIZE-MAX_INODE)/(8*2048)*(8*2048)+MAX_INODE)*SECTOR_SIZE; + u_int64_t freeBlockHead = ((freeBlockNum/SECTOR_SIZE-MAX_INODE)/(8*2048)*(8*2048)+MAX_INODE)*SECTOR_SIZE; // mark it alive in its bitmap char buffer[IO_BLOCK_SIZE] = {0}; @@ -273,28 +273,28 @@ public: notEmpty = true; } } - off_t inBlockPos = (freeBlockNum-freeBlockHead)/IO_BLOCK_SIZE-1; + u_int64_t inBlockPos = (freeBlockNum-freeBlockHead)/IO_BLOCK_SIZE-1; buffer[8+inBlockPos/8] |= (1<<(inBlockPos%8)); // if its bitmap was 0, add it back to the list head if(!notEmpty){ - off_t freeListHead = SuperBlock::getFreeListHead(disk); + u_int64_t freeListHead = SuperBlock::getFreeListHead(disk); write_byte_at(freeListHead, 0, buffer); SuperBlock::writeFreeListHead(disk, freeBlockHead); } disk.rawdisk_write(freeBlockHead, buffer, sizeof(buffer)); } - off_t deallo_single_indirect(RawDisk &disk, off_t &single_i){ + u_int64_t deallo_single_indirect(RawDisk &disk, u_int64_t &single_i){ if (single_i == 0){ return 0; } - off_t freeBlockNum = 0; + u_int64_t freeBlockNum = 0; char buffer[IO_BLOCK_SIZE] = {0}; int delpoint = -1; disk.rawdisk_read(single_i, buffer, sizeof(buffer)); for (int i=4088; i >= 0; i--){ - off_t addr = read_byte_at(i, buffer); + u_int64_t addr = read_byte_at(i, buffer); if(addr != 0){ freeBlockNum = addr; addr = 0; @@ -303,8 +303,8 @@ public: break; } } - disk.rawdisk_read(triple_i, buffer, sizeof(buffer)); - off_t addr = read_byte_at(0, buffer); + disk.rawdisk_write(single_i, buffer, sizeof(buffer)); + u_int64_t addr = read_byte_at(0, buffer); if (delpoint == 0 && addr == 0){ datablock_deallocate_in_list(single_i, disk); single_i = 0; @@ -312,17 +312,17 @@ public: return freeBlockNum; } - bool deallo_double_indirect(RawDisk &disk, off_t &double_i){ + bool deallo_double_indirect(RawDisk &disk, u_int64_t &double_i){ if (double_i == 0){ return false; } - off_t freeBlockNum = 0; + u_int64_t freeBlockNum = 0; char buffer[IO_BLOCK_SIZE] = {0}; int delpoint = -1; disk.rawdisk_read(double_i, buffer, sizeof(buffer)); for (int i=4088; i >= 0; i-=8){ - off_t addr = read_byte_at(i, buffer); - off_t inSingle = deallo_single_indirect(disk, addr); + u_int64_t addr = read_byte_at(i, buffer); + u_int64_t inSingle = deallo_single_indirect(disk, addr); if (inSingle){ freeBlockNum = inSingle; write_byte_at(addr, i, buffer); @@ -330,8 +330,8 @@ public: break; } } - disk.rawdisk_read(triple_i, buffer, sizeof(buffer)); - off_t addr = read_byte_at(0, buffer); + disk.rawdisk_write(double_i, buffer, sizeof(buffer)); + u_int64_t addr = read_byte_at(0, buffer); if (delpoint == 0 && addr == 0){ datablock_deallocate_in_list(double_i, disk); double_i = 0; @@ -339,17 +339,17 @@ public: return freeBlockNum; } - bool deallo_triple_indirect(RawDisk &disk, off_t &triple_i){ + bool deallo_triple_indirect(RawDisk &disk, u_int64_t &triple_i){ if (triple_i == 0){ return false; } - off_t freeBlockNum = 0; + u_int64_t freeBlockNum = 0; char buffer[IO_BLOCK_SIZE] = {0}; int delpoint = -1; disk.rawdisk_read(triple_i, buffer, sizeof(buffer)); for (int i=4088; i >= 0; i-=8){ - off_t addr = read_byte_at(i, buffer); - off_t inDouble = deallo_double_indirect(disk, addr); + u_int64_t addr = read_byte_at(i, buffer); + u_int64_t inDouble = deallo_double_indirect(disk, addr); if (inDouble){ freeBlockNum = inDouble; write_byte_at(addr, i, buffer); @@ -357,8 +357,8 @@ public: break; } } - disk.rawdisk_read(triple_i, buffer, sizeof(buffer)); - off_t addr = read_byte_at(0, buffer); + disk.rawdisk_write(triple_i, buffer, sizeof(buffer)); + u_int64_t addr = read_byte_at(0, buffer); if (delpoint == 0 && addr == 0){ datablock_deallocate_in_list(triple_i, disk); triple_i = 0; @@ -369,7 +369,7 @@ public: // deallocate 1 datablock from the end of the file void datablock_deallocate(RawDisk &disk){ // find the last datablock and remove it from inode (triple->direct) - off_t freeBlockNum = 0; + u_int64_t freeBlockNum = 0; freeBlockNum = deallo_triple_indirect(disk, triple_indirect); if(!freeBlockNum){ freeBlockNum = deallo_double_indirect(disk, double_indirect); @@ -401,9 +401,9 @@ public: void initialize(RawDisk &disk){ // initialize Inode list head SuperBlock::writeFreeINodeHead(disk, 1); - for (off_t i = 1; i < MAX_INODE; i++){ + for (u_int64_t i = 1; i < MAX_INODE; i++){ char buffer[SECTOR_SIZE] = {0}; - off_t t = i + 1; + u_int64_t t = i + 1; if (t < MAX_INODE){ for (int j = 0; j < 8; j++){ buffer[j] = (t >> (8 * j)) & 0xFF; @@ -413,9 +413,9 @@ public: } SuperBlock::writeFreeListHead(disk, MAX_INODE*SECTOR_SIZE); // maximum inode number 2^19 0x80000 //Have tested this initialize function but MAX_BLOCK too much, MAX_INODE*2 works - for (off_t i = MAX_INODE; i < MAX_BLOCKNUM-4096; i += 2048*8){ + for (u_int64_t i = MAX_INODE; i < MAX_BLOCKNUM-4096; i += 2048*8){ char buffer[IO_BLOCK_SIZE] = {0}; - off_t t = (i + 2048*8)*SECTOR_SIZE; + u_int64_t t = (i + 2048*8)*SECTOR_SIZE; //t is address, storing in to buffer for (int j = 0; j < 8; j++){ buffer[j] = (t >> (8 * j)) & 0xFF; @@ -426,11 +426,11 @@ public: // allocate an inode from free inode list head and return the number of the inode // the i-th inode is in the i-th block - off_t inode_allocate(RawDisk &disk){ - off_t freeINodeHead = SuperBlock::getFreeINodeHead(disk); + u_int64_t inode_allocate(RawDisk &disk){ + u_int64_t freeINodeHead = SuperBlock::getFreeINodeHead(disk); char buffer[SECTOR_SIZE] = {0}; disk.rawdisk_read(freeINodeHead*SECTOR_SIZE, buffer, sizeof(buffer)); - off_t newINodeHead = INode::read_byte_at(0, buffer); + u_int64_t newINodeHead = INode::read_byte_at(0, buffer); // deal with no more INode SuperBlock::writeFreeINodeHead(disk, newINodeHead); //to do: initialize the INode on disk at freeINodeHead @@ -440,8 +440,8 @@ public: } // free the inode and add it to the free inode list head - void inode_free(RawDisk &disk, off_t INodeNumber){ - off_t freeINodeHead = SuperBlock::getFreeINodeHead(disk); + void inode_free(RawDisk &disk, u_int64_t INodeNumber){ + u_int64_t freeINodeHead = SuperBlock::getFreeINodeHead(disk); char buffer[SECTOR_SIZE] = {0}; INode::write_byte_at(freeINodeHead, 0, buffer); disk.rawdisk_write(INodeNumber*SECTOR_SIZE, buffer, sizeof(buffer)); diff --git a/include/rawdisk.h b/include/rawdisk.h index 344777e..4cccf70 100644 --- a/include/rawdisk.h +++ b/include/rawdisk.h @@ -6,14 +6,12 @@ #include #include -typedef unsigned long long off_t; - class RawDisk{ int fd; const char* dir; - off_t numSectors; - off_t diskSize; + u_int64_t numSectors; + u_int64_t diskSize; public: RawDisk(const char *directory) : fd(-1), dir(nullptr), numSectors(0), diskSize(0) { @@ -50,8 +48,8 @@ public: } } - int rawdisk_read(off_t offset, char *buffer, size_t length) { - if (lseek(fd, offset, SEEK_SET) == (off_t)-1) { + int rawdisk_read(u_int64_t offset, char *buffer, size_t length) { + if (lseek(fd, offset, SEEK_SET) == (u_int64_t)-1) { perror("Error seeking to offset"); return -1; } @@ -66,8 +64,8 @@ public: } // Write a specified number of bytes at a given byte offset - int rawdisk_write(off_t offset, char *buffer, size_t length) { - if (lseek(fd, offset, SEEK_SET) == (off_t)-1) { + int rawdisk_write(u_int64_t offset, char *buffer, size_t length) { + if (lseek(fd, offset, SEEK_SET) == (u_int64_t)-1) { perror("Error seeking to offset"); return -1; } diff --git a/test/layer0.cpp b/test/layer0.cpp index be5d998..8cc358a 100644 --- a/test/layer0.cpp +++ b/test/layer0.cpp @@ -14,11 +14,11 @@ int main(int argc, char *argv[]) { //printf("dir %s, numSectors %lld, diskSize %lld \n", H->dir, H->numSectors, H->diskSize); //use number to substitute H->getnumSector(), getnumSectors() are not yest implemented - for(off_t i = 0; i < 10; i++) { + for(u_int64_t i = 0; i < 10; i++) { H->rawdisk_write(i*512, buf, strlen(buf));//Change write_API } //use number to substitute H->getnumSector(), getnumSectors() are not yest implemented - for(off_t i = 0; i < 10; i++) { + for(u_int64_t i = 0; i < 10; i++) { H->rawdisk_read(i*512, readBuffer, sizeof(readBuffer));//Change read_API assert(strncmp(readBuffer, buf, strlen(buf)) == 0); } diff --git a/test/layer1_API.cpp b/test/layer1_API.cpp index 94719aa..799a76f 100644 --- a/test/layer1_API.cpp +++ b/test/layer1_API.cpp @@ -15,37 +15,37 @@ int main(int argc, char *argv[]) { char buffer[8] = {0}; //test the begining of inode 1~524287 H->rawdisk_read((1) * SECTOR_SIZE, buffer, sizeof(buffer)); - off_t t = 0; + u_int64_t t = 0; for (int j = 0; j < 8; j++) - t |= ((off_t)(unsigned char)buffer[j]) << (8 * j); + t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); assert(t == 2); //test the number before end of inode 524286 H->rawdisk_read((MAX_INODE - 2) * SECTOR_SIZE, buffer, sizeof(buffer)); t = 0; for (int j = 0; j < 8; j++) - t |= ((off_t)(unsigned char)buffer[j]) << (8 * j); + t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); assert(t == MAX_INODE - 1); //test the end of inode 1~524287 H->rawdisk_read((MAX_INODE - 1) * SECTOR_SIZE, buffer, sizeof(buffer)); t = 0; for (int j = 0; j < 8; j++) - t |= ((off_t)(unsigned char)buffer[j]) << (8 * j); + t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); assert(t == 0); //test the begining of datablock H->rawdisk_read((MAX_INODE) * SECTOR_SIZE, buffer, sizeof(buffer)); t = 0; for (int j = 0; j < 8; j++) - t |= ((off_t)(unsigned char)buffer[j]) << (8 * j); + t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); assert(t == (MAX_INODE+2048*8)*SECTOR_SIZE); //test the end of the datablock H->rawdisk_read((MAX_BLOCKNUM - 2048*8) * SECTOR_SIZE, buffer, sizeof(buffer)); t = 0; for (int j = 0; j < 8; j++) - t |= ((off_t)(unsigned char)buffer[j]) << (8 * j); + t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); assert(t == (MAX_BLOCKNUM)*SECTOR_SIZE);