Skip to content

Commit

Permalink
added find parent indode helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
sara committed Apr 13, 2018
1 parent 64330e5 commit 96b1788
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
46 changes: 35 additions & 11 deletions Asst3/src/sfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,6 @@ int sfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
int retstat = 0;
log_msg("\nsfs_create(path=\"%s\", mode=0%03o, fi=0x%08x)\n",
path, mode, fi);
char * temp = malloc(strlen(path));
strcpy(temp,path);
//why do we need this?
/* if(getFilePath(temp) == 0)
{
return retstat;
}*/
Inode * file = findFreeInode();
if(file == NULL)
{
Expand All @@ -587,21 +580,52 @@ int sfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
char* buf = malloc(strlen(path));
strcpy(buf,path);
char * last = strrchr(buf, '/');;
//problem for future Sam and SaraAnn - what if last is null?
last = last+1;
if(last !=NULL){
if(last !=NULL)
{
strcpy(file->fileName,last);

}

//memcpy(file->fileName,path,strlen(path));
file->file_mode = mode;
file->timestamp = time(NULL);
file->parent = FT->files[0]->fd;
//right now this just hardcodes to set root to be the parent
//TODO: use SaraAnn's string parsing method to get the actual parent
file->parent = get_parent(path);
writeFS(0);

return retstat;
}

int get_parent (const char * full_path)
{
char * buffer = malloc(128);
memcpy (buffer, full_path, 128);
//if you've fucked up royally and somehow got passed the root directory as a param
if (full_path == NULL || strlen(full_path) == 1)
return -5;
int i;
char curr;
for (i = strlen(full_path)-1; curr!= '/'; i--)
{
curr = full_path[i];
}
//When crr == '/' we go back i again, so we need to bring it back
i++;
//return root if the param was directly below root directory
if (i == 0)
{
return 0;
}
//truncate the path so that it just leads to the parent directory
else
{
buffer[i] = '\0';
return getFilePath(buffer)->fd;
}
}


/** Remove a file */
int sfs_unlink(const char *path)
{
Expand Down
2 changes: 2 additions & 0 deletions Asst3/src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,5 @@ int fileSize(Inode *);
int loadFS();
int ceil_bytes(int);
int writeFS(int);
int get_parent(const char *);

0 comments on commit 96b1788

Please sign in to comment.