Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mtian/PortOSnix
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaosky committed Dec 8, 2012
2 parents 2f0de41 + 39b69d2 commit d50e487
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
93 changes: 64 additions & 29 deletions P6/minifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ minifile_t minifile_creat(char *filename)
inode->type = MINIFILE;
inode->size = 0;
iunlock(inode);
iupdate(inode);
/*
* Put inode number and filename in directory
*/
} else {
iget(maindisk, inum, &inode);
}
Expand All @@ -47,55 +51,54 @@ minifile_t minifile_creat(char *filename)

minifile_t minifile_open(char *filename, char *mode)
{

minifile_t file = NULL;
inodenum_t inum;
mem_inode_t inode;

inum = namei(filename);
if (0 == inum) {
/* When file is not found, return NULL if 'r' or 'r+' */
if ('r' == mode[0]) {
return NULL;
} else {

if ((file = minifile_creat(filename)) != 0) {
inode = file->inode;
}
}
} else {
/* When file is found */
file = malloc(sizeof(struct minifile));
iget(maindisk, inum, &inode);
if (NULL == file || NULL == inode) {
free(file);
iput(inode);
return NULL;
}
ilock(inode);
file->inode = inode;
file->inode_num = inum;
/* Empty the content if 'w' or 'w+' */
if ('w' == mode[0]) {
iclear(inode);
}
}
file = malloc(sizeof(struct minifile));
if (NULL == file) {
return NULL;
}

iget(maindisk, inum, &inode);
if (NULL == inode) {
free(file);
return NULL;
}
ilock(inode);
if (MINIDIRECTORY == inode->type) {
free(file);
iunlock(inode);
iput(inode);
return NULL;
}

file->inode = inode;
file->inode_num = inum;

/* Set modes */
file->mode[0] = mode[0];
file->mode[1] = '\0';
file->mode[2] = '\0';
file->block_cursor = 0;
file->byte_cursor = 0;
file->byte_in_block = 0;
if ('a' == mode[0]) {
/* Position cursor to the end */
minifile_cursor_shift(file, inode->size);
}
if ('+' == mode[1]) {
file->mode[1] = '+';
}

iunlock(inode);

return file;
}

Expand All @@ -106,7 +109,8 @@ int minifile_read(minifile_t file, char *data, int maxlen)
int disk_block = 0;
buf_block_t buf;
int step = 0;
if ("w" == file->mode || "a" == file->mode) {
if (('w' == file->mode[0] || 'a' == file->mode[0])
&& ('\0' == file->mode[1])) {
return -1;
}

Expand All @@ -122,32 +126,63 @@ int minifile_read(minifile_t file, char *data, int maxlen)
} else {
step = maxlen;
}
/* Get disk block number from block cursor */
ilock(file->inode);
disk_block = blockmap(maindisk, file->inode, file->block_cursor);
iunlock(file->inode);

/* Copy disk block */
if (bread(maindisk, disk_block, &buf) != 0)
return count;
memcpy(data, buf->data + file->byte_in_block, step);
brelse(buf);
/* Update upon success */
minifile_cursor_shift(file, step);
count += step;
data += step;
maxlen -= step;
count += step;
}

return count;
}

int minifile_write(minifile_t file, char *data, int len)
{
int count = 0;
int left_byte = 0;
int disk_block = 0;
blocknum_t blocknum = 0;
buf_block_t buf;
int disk_block = 0;
int step = 0;
if ("r" == file->mode) {
if ('r' == file->mode[0] && '\0' == file->mode[1]) {
return -1;
}

while (len > 0) {
if (file->block_cursor * DISK_BLOCK_SIZE < file->byte_cursor + 1) {
blocknum = balloc(maindisk);
ilock(file->inode);
iadd_block(file->inode, blocknum);
iunlock(file->inode);
}
/* Get step size */
if (file->byte_in_block + len - 1 > DISK_BLOCK_SIZE) {
step = DISK_BLOCK_SIZE - file->byte_in_block;
} else {
step = len;
}
/* Get disk block number from block cursor */
ilock(file->inode);
disk_block = blockmap(maindisk, file->inode, file->block_cursor);
iunlock(file->inode);
/* Copy disk block */
if (bread(maindisk, disk_block, &buf) != 0)
return -1;
memcpy(buf->data + file->byte_in_block, data, step);
bwrite(buf);
/* Update upon success */
minifile_cursor_shift(file, step);
len -= step;
}

return 0;
}

Expand Down
3 changes: 3 additions & 0 deletions P6/minifile_inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ iget(disk_t* disk, inodenum_t n, mem_inode_t *inop)
void
iput(mem_inode_t ino)
{
if (NULL == ino)
return;

semaphore_P(itable_lock);
ino->ref_count--;
if (ino->ref_count == 0) {
Expand Down

0 comments on commit d50e487

Please sign in to comment.