Skip to content
Permalink
Browse files

merge still

  • Loading branch information
joshspicer committed Dec 5, 2017
2 parents 1be1a66 + 2a60852 commit 2258e3d5ffb14936d0d607e979590d766a8e194d
Showing with 268 additions and 64 deletions.
  1. +1 −0 big.txt
  2. +15 −3 datablock.c
  3. +24 −9 node.c
  4. +5 −0 node.h
  5. BIN nufs
  6. +62 −12 nufs.c
  7. +40 −6 storage.c
  8. +12 −3 superblock.c
  9. +95 −24 test.log
  10. +4 −0 test.pl
  11. +10 −7 util.h

Large diffs are not rendered by default.

@@ -42,6 +42,18 @@ correctSizeForLinkedBlocks(int givenBlockID, int size) {

// ---------------------------------------------------------------------------- //



// TODO: implement find_empty_block_index() and replace my other stuff with it
// Finds the next empty data block, and returns that index.
// ALSO FLIPS THE BIT MAP
int
find_empty_block_index() {
for (int i = 0; i < GET_NUMBER_OF_DATABLOCKS(); i++) {

// check block bitmap. If value isn't one, then that block isn't active.
if (*((int *) (GET_ptr_start_dataBlock_bitMap() + sizeof(int) * i)) != 1) {
flip_data_block_bit(i, 1);
return i;
}
}
// If there's no empty slot, return -1
return -1;
}
33 node.c
@@ -24,19 +24,12 @@ add_node(const char *completePath, int mode, int xtra, int which_iNode) {
// TODO calculate how many blocks we can allot with our superblock (1MB limit)
// so that we don't go over.

//int MAX_BLOCKS = 10; //FIXME

// Keep searching for next available block according to bitmap
int firstAvailableBlockIdx = -1;
for (int i = 0; i < GET_NUMBER_OF_DATABLOCKS(); i++) {
if (*((int *) (GET_ptr_start_iNode_bitMap() + sizeof(int) * i)) == 0) {
firstAvailableBlockIdx = i;
break;
}
}
int firstAvailableBlockIdx = find_empty_block_index();

if (firstAvailableBlockIdx != -1) {
flip_data_block_bit(firstAvailableBlockIdx, 1);
//flip_data_block_bit(firstAvailableBlockIdx, 1); // ^^ find_empty_block_index does this.
newNode->blockID = firstAvailableBlockIdx;
printf("BLOCK %d FOUND FOR INODE\n", firstAvailableBlockIdx);

@@ -51,6 +44,15 @@ add_node(const char *completePath, int mode, int xtra, int which_iNode) {
// Let the iNode know which node it is in the bitmap.
// Useful for removing.
newNode->nodeID = which_iNode;


// Set all additional blocks to -1, meaning they aren't in use.
for (int i = 0; i < 9;i++) {
newNode->additionalBlocks[i] = -1;
}

printf("Addtional Blocks for <%s>\n",completePath);
print_additionalBlocks(newNode);
}

// ---------------------------------------------------------------------------- //
@@ -197,3 +199,16 @@ print_node(pnode *node) {
}

}

// ---------------------------------------------------------------------------- //

void print_additionalBlocks(pnode *node) {
if (node) {
printf("\nadditionalBlocks{\nA: %d, B: %d, C: %d,\n D: %d, E: %d, F: %d,\n G: %d, H: %d, I: %d}\n\n",
node->additionalBlocks[0],node->additionalBlocks[1],node->additionalBlocks[2],
node->additionalBlocks[3],node->additionalBlocks[4],node->additionalBlocks[5],
node->additionalBlocks[6],node->additionalBlocks[7], node->additionalBlocks[8]);
} else {
printf("node{null}\n");
}
}
5 node.h
@@ -24,6 +24,9 @@ typedef struct inode {
char name[32]; // File name (last part of path).
int blockID; // ID of data block this node is referencing to store data.
int nodeID; // The ID of THIS node (in the associated bitmap).
time_t timeStamp; //Timestamp

int additionalBlocks[9]; // Additional blocks for data > 4k

} pnode;

@@ -35,6 +38,8 @@ void flip_iNode_bit(int which_iNode, int state); // 0 == off, 1 == on

void print_node(pnode *node);

void print_additionalBlocks(pnode *node);

int find_empty_inode_index(); // Returns the index of the first free iNode, or -1 if no empty inode.

const char *findName(const char* path);
BIN -43.5 KB nufs
Binary file not shown.
74 nufs.c
@@ -237,6 +237,7 @@ nufs_rename(const char *from, const char *to) {

if (!node) {
printf("Cannot rename file or directory because it does not exist.\n");
return -1;
}

const char* fromDir = findPrecedingPath(from);
@@ -261,7 +262,10 @@ nufs_rename(const char *from, const char *to) {
int
nufs_chmod(const char *path, mode_t mode) {
printf("chmod(%s, %04o)\n", path, mode);
return -1;

pnode *node = get_file_data(path);
node->mode = mode;
return 0;
}

int
@@ -291,8 +295,10 @@ nufs_open(const char *path, struct fuse_file_info *fi) {
// Actually read data
int
nufs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {

printf("read(%s, %ld bytes, @%ld)\n", path, size, offset);
const char *data = get_data(path);

const char *data = get_data(path); // <----- Now supports >4k

int len = strlen(data) + 1; // strlen(data) + 1
if (size < len) {
@@ -308,13 +314,13 @@ int
nufs_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {

printf("write(%s, %ld bytes, %ld)\n", path, size, offset);
printf("write(%s, %ld bytes, offset %ld)\n", path, size, offset);

// Get the node associated with this path.
pnode *node = get_file_data(path);

if (!node) {
printf("%s\n", "YOU CAN'T WRITE TO A FILE THAT DOESN'T EXIST!");
printf("%s\n", "Can't write. File doesn't exist.");
return -1; //TODO return error code.
}

@@ -325,30 +331,74 @@ nufs_write(const char *path, const char *buf, size_t size, off_t offset,
void *ptr = data_block_ptr_at_index(node->blockID);

// Write to that memory location (using the given buffer/size/offset)
//ptr = buf;
memcpy(ptr, buf, size);
//int fd = fi->direct_io;

// TODO Change stat somehow??? (I don't get stat.)
int singleBlockSafeSize = size;
if (singleBlockSafeSize > 4096) {
singleBlockSafeSize = 4096;
}

memcpy(ptr, buf, singleBlockSafeSize); //TODO add offset.


// TODO ::: if you want to memcpy more than one page,
// recalculate ptr after copying 2048 bytes and then
// memcpy again!!!!!!!!!!
/// TODO :: size/4096 to find out which additionalBlock you need to fill!!!


// Set node's size to size of this file.
node->size = size;

// Fix all the references files inodes!
correctSizeForLinkedBlocks(node->blockID, size);


// ---------- Beneath this line supports writing file ----------------- //

int sizeRemaining = node->size - 4096;
int looped = 1;

while (sizeRemaining > 0) {
printf("Inside over 4K writing block for <%s> ", node->path); //REMOVE
printf("Size Remaining: %d\n", sizeRemaining); //REMOVE

void *additionalPtr = data_block_ptr_at_index(node->additionalBlocks[looped]);

// Mem copy the offset data in 4k increments.
int sizeToCopy = 4096;
if (sizeRemaining < 4096) {
sizeToCopy = sizeRemaining;
}

memcpy(additionalPtr, buf + (4096*looped),sizeToCopy);

sizeRemaining -= 4096;
looped += 1;

// TODO
printf("TODO: %s\n", "implement correctSizeForLinkedBlocks()");
}

return 0;
}

// Update the timestamps on a file or directory.
int
nufs_utimens(const char *path, const struct timespec ts[2]) {
//int rv = storage_set_time(path, ts); TODO TODO TODO
int rv = -1;

printf("utimens(%s, [%ld, %ld; %ld %ld]) -> %d\n",
path, ts[0].tv_sec, ts[0].tv_nsec, ts[1].tv_sec, ts[1].tv_nsec, rv);
path, ts[0].tv_sec, ts[0].tv_nsec, ts[1].tv_sec, ts[1].tv_nsec);

pnode *node = get_file_data(path);

if (!node) {
printf("%s\n", "Can't write. File doesn't exist.");
return -1; //TODO return error code.
}


node->timeStamp = ts->tv_sec;

// return rv;
return 0;
}

@@ -18,6 +18,18 @@ storage_init(const char *path) {
superBlock_init(path);
}

// ----------------------------------------------------------------------------- //
char*
concatStrings(const char *string1, const char *string2) {
char *newStr =
malloc(strlen(string1) + strlen(string2) + 1);

strcpy(newStr, string1);
strcat(newStr, string2);

return newStr;
}

// ---------------------------------------------------------------------------- //


@@ -71,15 +83,12 @@ get_stat(const char *path, struct stat *st) {
memset(st, 0, sizeof(struct stat));
st->st_uid = getuid();
st->st_gid = getgid();
st->st_nlink = 1;
st->st_mode = dat->mode;
st->st_atime = time(NULL); //TODO time
st->st_mtime = time(NULL); //TODO time
st->st_atime = time(NULL);
st->st_mtime = time(NULL);
st->st_size = dat->size;

//st_nlink was here was when it was working.
st->st_nlink = 1;

//TODO stat with something to do with data blocks.

return 0;
}
@@ -89,6 +98,7 @@ get_stat(const char *path, struct stat *st) {
// Get the data stored in the data block of the given Path.
const char *
get_data(const char *path) {

pnode *node = get_file_data(path);

if (!node) {
@@ -108,5 +118,29 @@ get_data(const char *path) {
return 0; // TODO error codes
}


// -- CODE BELOW HERE SUPPORTS DATA OVER 4K --
int sizeRemaining = node->size - 4096;

while (sizeRemaining > 0) {
printf("Inside over 4K get_data() block for <%s> ", node->path); //REMOVE
printf("Size Remaining: %d\n", sizeRemaining); //REMOVE

// Use the current Size to determine which additional block.
int idx = node->size - sizeRemaining;

// Fetch the data at the additional block.
void *tmp = data_block_ptr_at_index(node->additionalBlocks[idx]);
const char *tmpString = ((const char *) tmp);

// Append entire contents (4K) of tmpString to blockPtr.
blockPtr = concatStrings(((const char *) blockPtr), tmpString);

printf("Iteration for %d remaining COMPLETE\n", sizeRemaining); //REMOVE

sizeRemaining -= 4096;
}


return ((const char *) blockPtr);
}
@@ -10,7 +10,7 @@
#include "superblock.h"

const int NUFS_SIZE = 1024 * 1024; // 1MB
const int PAGE_COUNT = 256;


void
superBlock_init(const char *path)
@@ -28,8 +28,10 @@ superBlock_init(const char *path)
// --- CREATE SUPERBLOCK ASSIGNING OFFSETS ---
// Start location of iNode
SUPER_SIZE = 20;
NUMBER_OF_INODES = 8;
NUMBER_OF_DATABLOCKS = 10;
NUMBER_OF_INODES = 800;
NUMBER_OF_DATABLOCKS = 238;

//TODO calculate how much space we have for 4k data blocks, and make that many!

start_iNode_bitMap = SUPER_SIZE;
start_dataBlock_bitMap = start_iNode_bitMap
@@ -39,6 +41,13 @@ superBlock_init(const char *path)
start_dataBlocks = start_iNode_Table
+ NUMBER_OF_INODES * sizeof(pnode);

// Calculate how many Data blocks we have space for.
printf("Start of DataBlock: %d\n", start_dataBlocks);
int MaxSizeOfDataSection = NUFS_SIZE - start_dataBlocks;
printf("NUFS Size - start_dataBlocks: %d\n", MaxSizeOfDataSection);
int numBlocksFloor = MaxSizeOfDataSection / 4096;
printf("Num Blocks: %d\n",numBlocksFloor );


// Write offset to start of inode bitmap in the superblock
write_int_offset(0, start_iNode_bitMap);

0 comments on commit 2258e3d

Please sign in to comment.
You can’t perform that action at this time.