Ejemplo n.º 1
0
/* Opens the file with the given NAME.
   Returns the new file if successful or a null pointer
   otherwise.
   Fails if no file named NAME exists,
   or if an internal memory allocation fails. */
struct file *
filesys_open (const char *name)
{
  char s[100];
  char *save_ptr;
//  printf("%s \n",name);
  memcpy(s,name,strlen(name)+1);
  struct inode *inode = NULL;
  char *file_name = get_file_name(name);
//  printf("file name : %s\n",file_name);
  if( strtok_r(s,"/",&save_ptr) ==NULL&&strlen(name)>0 )
  {
//    printf("aaa\n");  
    free(file_name);
    return file_open(inode_open(1));
  }
  if( strcmp(file_name,"")==0 )
    return NULL;
  struct dir *dir = get_directory(name);
//  printf("sector of dir :  %d\n", inode_get_inumber(dir_get_inode(dir)));
//  printf("lookup start\n");
  if (dir != NULL)
  {
//    printf("here?\n");
    if(strcmp(file_name,".")==0)
    {
      //      printf("here\n");
      inode = dir_get_inode(dir);
      //      printf("ohhhhhh\n");
      //  dir_close (dir);
      free(file_name);
      if(inode_is_removed(inode))
        return NULL;
      else
        return file_open (inode);
    }
    else if( strcmp(file_name,"..")==0)
    {
      inode = dir_get_inode(inode);
      //      dir_close (dir);
      free(file_name);
      return file_open (inode);
    }
    else
      dir_lookup (dir, file_name, &inode);
    dir_close (dir);
    if(inode==NULL)
      return NULL;
    free(file_name);
    return file_open (inode);
  }
  else
  {
//    printf("here\n");
    return NULL;
  }
//  if(inode_is_removed(inode))
//    return NULL;
//  return file_open (inode);
}
Ejemplo n.º 2
0
/* Adds a file named NAME to DIR, which must not already contain a
   file by that name.  The file's inode is in sector
   INODE_SECTOR.
   Returns true if successful, false on failure.
   Fails if NAME is invalid (i.e. too long) or a disk or memory
   error occurs. */
bool
dir_add (struct dir *dir, const char *name, block_sector_t inode_sector)
{
  struct dir_entry e;
  off_t ofs;
  bool success = false;

  ASSERT (dir != NULL);
  ASSERT (name != NULL);
  inode_lock(dir_get_inode (dir));
  /* Check NAME for validity. */
  if (*name == '\0' || strlen (name) > NAME_MAX)
  {  
      inode_unlock (dir_get_inode(dir));
      return false;
  }

  /* Check that NAME is not in use. */
  if (lookup (dir, name, NULL, NULL))
    goto done;

  if (!inode_add_parent (inode_get_inumber (dir_get_inode(dir)),inode_sector))
  {
      goto done;
  }
  
  /* Set OFS to offset of free slot.
     If there are no free slots, then it will be set to the
     current end-of-file.
     
     inode_read_at() will only return a short read at end of file.
     Otherwise, we'd need to verify that we didn't get a short
     read due to something intermittent such as low memory. */
  for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
       ofs += sizeof e) 
    if (!e.in_use)
      break;

  /* Write slot. */
  e.in_use = true;
  strlcpy (e.name, name, sizeof e.name);
  e.inode_sector = inode_sector;
  success = inode_write_at (dir->inode, &e, sizeof e, ofs) == sizeof e;

 done:
  inode_unlock (dir_get_inode(dir));
  return success;
}
Ejemplo n.º 3
0
struct inode *
filesys_open_inode (const char *path, bool *is_directory)
{
  struct dir *dir = NULL;
  struct inode *inode = NULL;
  bool success = false;
  char *name = malloc (NAME_MAX + 1);
  if (name == NULL)
    goto done;
  if (!dir_follow_path (thread_current ()->cwd, path, &dir, name))
    goto done;
  if (dir == NULL)
    goto done;

  /* open directory in this case */
  if (*path != '\0' && strlen (name) == 0)
  {
    inode = inode_reopen (dir_get_inode (dir));
    if (inode == NULL)
      goto done;
    *is_directory = true;
  }
  else if (!dir_lookup (dir, name, &inode, is_directory))
    goto done;
  success = true;
done:
  dir_close (dir);
  free (name);
  return success ? inode : NULL;
}
Ejemplo n.º 4
0
/* Initializes an inode with LENGTH bytes of data and
   writes the new inode to sector SECTOR on the file system
   disk.
   Returns true if successful.
   Returns false if memory or disk allocation fails. */
bool
inode_create (disk_sector_t sector, off_t length, bool is_dir)
{
  struct inode_disk *disk_inode = NULL;
  struct inode *inode;
  bool success = false;

  ASSERT (length >= 0);

  /* If this assertion fails, the inode structure is not exactly
     one sector in size, and you should fix that. */
  ASSERT (sizeof *disk_inode == DISK_SECTOR_SIZE);

  disk_inode = calloc (1, sizeof *disk_inode);
  if (disk_inode != NULL)
    {
      disk_inode->length = 0;
      disk_inode->sector_count = 0;
      disk_inode->is_dir = is_dir;
      disk_inode->parent = dir_get_inode (thread_current ()->dir)->sector;
      disk_inode->magic = INODE_MAGIC;

      cache_write (sector, disk_inode, 0, DISK_SECTOR_SIZE);
      inode = inode_open (sector);
      success = inode_extend (inode, length);
      free (disk_inode);
    }
  return success;
}
Ejemplo n.º 5
0
/* Creates a file named NAME with the given INITIAL_SIZE.
   Returns true if successful, false otherwise.
   Fails if a file named NAME already exists,
   or if internal memory allocation fails. */
bool
filesys_create (const char *name, off_t initial_size, enum file_type type) 
{
  block_sector_t inode_sector = 0;

  char * parse = parse_filename (name); 
  //get the correct dir

  struct dir *dir = dir_lookup_rec (parse);

  bool success = (dir != NULL
                  && free_map_allocate (1, &inode_sector)
                  && inode_create (inode_sector, initial_size, type)
                  && dir_add (dir, parse, inode_sector));

  if (!success && inode_sector != 0) 
    free_map_release (inode_sector, 1);
  if( success == true && type == FILE_DIR ) {
	//we want to add . and .. as well if it is a dir
		//open the created directory
		struct file * created = filesys_open(parse);
		struct dir * mydir = dir_open(file_get_inode (created));
		//add . to it
		dir_add (mydir, ".", inode_sector);
		struct inode * parent = dir_get_inode (dir);
		block_sector_t inode_sector_parent = inode_id(parent);
		//add .. to it
		dir_add (mydir, "..", inode_sector_parent);
		dir_close(mydir);
		file_close(created);
  }
  dir_close (dir);

  return success;
}
Ejemplo n.º 6
0
bool
dir_get_parent (struct dir *dir, struct inode **inode)
{
    block_sector_t sector = inode_get_parent (dir_get_inode(dir));
    *inode  = inode_open(sector);
    return *inode != NULL;
}
Ejemplo n.º 7
0
/* Opens the file with the given NAME.
   Returns the new file if successful or a null pointer
   otherwise.
   Fails if no file named NAME exists,
   or if an internal memory allocation fails. */
struct file *
filesys_open (const char *name)
{
    if (strnlen (name, FULLPATH_MAX_LEN) == 0)
        return NULL;

    if (strcmp (".", name) == 0)
    {
        block_sector_t cwd = thread_current ()->cwd_sector;
        struct inode *curr = NULL;
        curr = inode_open (cwd);

        struct dir *p;
        p = dir_open (inode_open (curr->data.parent_dir_sector));

        struct dir_entry e;
        size_t ofs;

        ASSERT (p != NULL);

        for (ofs = 0;
                inode_read_at (p->inode, &e, sizeof e, ofs) == sizeof e;
                ofs += sizeof e)
        {
            if (e.inode_sector == cwd && e.in_use)
            {
                return filesys_open (e.name);
            }
        }
        return NULL;
    }
    struct inode *crr = NULL;
    crr = inode_open (thread_current ()->cwd_sector);

    struct dir *parent_dir = dir_reopen(dir_get_parent_dir (name));
    //if (crr->data.is_dir)
    //parent_dir = dir_open (inode_open (crr->data.parent_dir_sector));
    //else
    if (parent_dir == NULL)
        return NULL;

    struct inode *inode = NULL;

    char leaf_name[NAME_MAX + 1];
    if (!dir_get_leaf_name (name, leaf_name) &&
            strnlen(leaf_name, NAME_MAX) == 0)
    {
        inode = inode_reopen (dir_get_inode (parent_dir));
        dir_close (parent_dir);
        return file_open (inode);
    }

    if (parent_dir != NULL)
        dir_lookup (parent_dir, leaf_name, &inode);
    dir_close (parent_dir);

    return file_open (inode);
}
Ejemplo n.º 8
0
//returns the inode of the last dir in name before the last /
//the user has to check for existing file after the /
struct dir *
dir_lookup_rec (const char *name) {

	//printf("lookup path is %s\n",name);

	//if the string starts with / it is an absolut path
	//root dir
	if ( strcmp( name, "/") == 0 ) return dir_open_root();

	//for the return value
	int success = 0;
	char *token ,*save_ptr;
	char * temp = (char *)malloc(strlen(name) + 1 );
	strlcpy (temp, name, strlen(name) + 1); 

	//open root and start 
	struct dir * current;

	//if it is relative make it absolute 
	if ( name[0] != '/' ) {
		current = dir_reopen(thread_current()->pwd);
	} else {
		current = dir_open_root();
	}
	
	struct inode * nextdir = dir_get_inode(current);

	//go through and check that the previous direcrtories exist
	for (token = strtok_r (temp, "/", &save_ptr); token != NULL; token = strtok_r (NULL, "/", &save_ptr)) {

		//somethings wrong if this hapens	
		if (current == NULL ) break;
		//last round has to be not existing
		if (strlen(save_ptr) == 0) {
			success = 1;
			break;
				
		}

		//goto next if token is empty in case of //a/
		if(strlen(token) !=  0) {
			//check if this directory exists true if exists
			if ( dir_lookup (current, token,&nextdir) ) {
				//check if it is a directory and then open it
				enum file_type type =  inode_type (nextdir);
				//is it a dir
				if(type == FILE_DIR) {
					dir_close(current);
					current = dir_open(nextdir);
				}
				else break;
			}
			else break;
		}
	}

  if( success == 1) return current; else return NULL;
}
Ejemplo n.º 9
0
bool filesys_chdir(const char *name)
{
  bool success=false;
  struct inode *inode = NULL;
  struct thread *t = thread_current();
  char *file_name = get_file_name(name);
  struct dir *dir = get_directory(name);
//  printf("lookup start\n");
  if (dir != NULL)
  {
    if( strcmp(file_name,".")==0)
    {
      t->curr_dir = dir_reopen(dir);
      free(file_name);
      return true;
    }
    else if(strcmp(file_name,"..")==0)
    {
      if( inode_get_inumber(dir_get_inode(dir))==1 )
      {
        t->curr_dir = dir_open(inode_open(1));
        free(file_name);
        dir_close(dir);
        return true;
      }
      else
      {
        t->curr_dir = dir_open( inode_open(inode_get_parent(dir_get_inode(dir))));
        free(file_name);
        dir_close(dir);
        return true;
      }
    }
    success=dir_lookup (dir, file_name, &inode);
  }
  dir_close (dir);
  if(success)
  {
    t->curr_dir = dir_open(inode);
  }
  free(file_name);
/*  printf("changed dir sector: %d\n",inode_get_inumber(dir_get_inode(t->curr_dir)));
  printf("value %d\n",inode_get_data(dir_get_inode(t->curr_dir),0));*/
  return success;
}
Ejemplo n.º 10
0
struct dir *get_directory(const char *path, bool flag){
  if(strlen(path)==0)
    return NULL;
  struct dir *curr;
  char *word, *brkt, *buffer = malloc(strlen(path)+1), *save, *last;
  struct inode *inode;
  memcpy(buffer, path, strlen(path)+1);
  save = buffer;
  if(buffer[0]=='/'){
    curr = dir_open_root();
    last = strtok_r(buffer+1, "/", &brkt);
  }
  else{
    if(thread_current()->dir)
      curr = dir_reopen(thread_current()->dir);
    else
      curr = dir_open_root();
    last = strtok_r(buffer, "/", &brkt);
  }
  
  while(1){
    word = last;
    if(word == NULL) break;
    last = strtok_r(NULL, "/", &brkt);
    if(last == NULL && flag) break;
    if(strcmp(word,"")==0);
    else if(strcmp(word,".")==0);
    else if(strcmp(word,"..")==0){
      inode = inode_open(inode_parent_number(dir_get_inode(curr)));
      dir_close(curr);
      curr = dir_open(inode);
    }
    else{
      inode = NULL;
      if(dir_lookup(curr, word, &inode)){
        dir_close(curr);
        if(inode_is_dir(inode))
          curr = dir_open(inode);
        else{
          inode_close(inode);
          free(save);
          return NULL;
        }
      }
      else{
        dir_close(curr);
        free(save);
        return NULL;
      }
    }
  }
  free(save);
  if(inode_removed(curr->inode))
    return NULL;
  return curr;
}
Ejemplo n.º 11
0
/*! Returns the sector number of the inode associated with fd */
int _inumber(uint32_t fd) {
    /* First get the f_info*/
    struct f_info* f = findfile(fd);
    
    /* Get the sector number*/
    if (f->isdir)
        return dir_get_inode(f->d)->sector;
    else
        return f->f->inode->sector;
}
Ejemplo n.º 12
0
static int inumber_handler (int fd) {
  struct file_struct * f = get_file(fd);
  if (f != NULL) {
    if (f->sys_dir != NULL) {
      return inode_get_inumber(dir_get_inode(f->sys_dir));
    } else {
      return inode_get_inumber(file_get_inode(f->sys_file));
    }
  }
  return -1;
}
Ejemplo n.º 13
0
/* Removes any entry for NAME in DIR.
   Returns true if successful, false on failure,
   which occurs only if there is no file with the given NAME. */
bool
dir_remove (struct dir *dir, const char *name) 
{
  struct dir_entry e;
  struct inode *inode = NULL;
  bool success = false;
  off_t ofs;

  ASSERT (dir != NULL);
  ASSERT (name != NULL);

  inode_lock (dir_get_inode(dir));
  /* Find directory entry. */
  if (!lookup (dir, name, &e, &ofs))
    goto done;

  /* Open inode. */
  inode = inode_open (e.inode_sector);
  if (inode == NULL)
    goto done;

  if (inode_is_dir(inode) && inode_get_open_cnt(inode) > 1 )
    goto done;

  if (inode_is_dir(inode) && !dir_is_empty(inode))
    goto done;

  /* Erase directory entry. */
  e.in_use = false;
  if (inode_write_at (dir->inode, &e, sizeof e, ofs) != sizeof e) 
    goto done;

  /* Remove inode. */
  inode_remove (inode);
  success = true;

 done:
  inode_close (inode);
  inode_unlock (dir_get_inode(dir));
  return success;
}
Ejemplo n.º 14
0
bool
dir_is_root (struct dir* dir)
{
   if (!dir)
   {
 	return false;
   }
   if (inode_get_inumber (dir_get_inode(dir)) == ROOT_DIR_SECTOR)
   {
	return true;
   }
   return false;
}
Ejemplo n.º 15
0
/* Opens the file with the given NAME.
   Returns the new file if successful or a null pointer
   otherwise.
   Fails if no file named NAME exists,
   or if an internal memory allocation fails. */
struct inode *
filesys_open (const char *name)
{
  struct dir *search_dir = get_cwd(name);

  if (search_dir == NULL) {
    return NULL;
  }

  struct inode *inode = NULL;

  if (name[0] == '/') {
    inode = dir_get_inode(search_dir);
  }

  char *part = malloc(NAME_MAX + 1);
  if (part == NULL) {
    return false;
  }
  memset(part, 0, NAME_MAX + 1);
  int retrieved_next_part;
  for (retrieved_next_part = get_next_part(part, &name); retrieved_next_part > 0;
       retrieved_next_part = get_next_part(part, &name)) {
    if (dir_lookup (search_dir, part, &inode)) {
      if (!inode_is_dir(inode)) {
        break;
      } else {
        dir_close(search_dir);
        search_dir = dir_open(inode);
        if (search_dir == NULL) {
          free(part);
          return false;
        }
      }
    } else {
      inode = NULL;
      break;
    }
  }
  if (inode != NULL && inode_is_dir(inode) && get_next_part(part, &name) == 0) {
    inode = inode_reopen(inode);
  }
  dir_close(search_dir);
  
  if (inode != NULL && get_next_part(part, &name) != 0) {
    inode_close(inode);
    inode = NULL;
  }
  free(part);
  return inode;
}
Ejemplo n.º 16
0
/* Formats the file system. */
static void
do_format (void)
{
  struct dir *root_dir = dir_open_root();
  struct inode *root_inode = dir_get_inode(root_dir); 
  printf ("Formatting file system...");
  free_map_create ();
  if (!dir_create (ROOT_DIR_SECTOR, 16))
    PANIC ("root directory creation failed");
  /* add . and .. entry to root directory */
  dir_add(root_dir, ".", inode_get_inumber(root_inode));
  dir_add(root_dir,"..", inode_get_inumber(root_inode));
  free_map_close ();
  printf ("done.\n");
}
Ejemplo n.º 17
0
void print_sector_info(struct dir * dir)
{
    block_sector_t bounce[128];
    int i,j;
    block_sector_t sector = byte_to_sector(dir_get_inode(dir),0);
    printf("Sector num : %d\n",sector);
    block_read(fs_device,sector,bounce);
    for(i=0; i<16; i++)
    {
        printf("%d : ",i);
        for(j=0; j<8; j++)
        {
            printf("%#010x  ",bounce[i*8 + j]);
        }
        printf("\n");
    }
}
Ejemplo n.º 18
0
/* Creates a file named NAME with the given INITIAL_SIZE.*/
bool
filesys_create (const char *name, off_t initial_size, bool isdir) 
{
  if (strlen(name) == 0) return false;
  block_sector_t inode_sector = 0;

  struct dir *dir_ = filesys_get_dir(name);
  char *name_ = filesys_get_name(name);
  bool success = false;

  if (strcmp (name_, "") == 0) goto done;
  success = (dir_ && free_map_allocate (1, &inode_sector)
    && inode_create (inode_sector, initial_size) 
    && dir_add (dir_, name_, inode_sector, isdir));

  struct inode *ninode = NULL;
  struct dir *ndir = NULL;
  bool success1 = true;
  if (success && isdir){
    success1 = ((ninode = inode_open (inode_sector))
      && (ndir = dir_open (ninode))
      && dir_add (ndir, ".", inode_sector, true)
      && dir_add (ndir, "..",inode_get_inumber (dir_get_inode (dir_)), true));
  }

  if (inode_sector != 0 && !success) 
    free_map_release (inode_sector, 1);

  if (success && !success1) {
    success = false;
    printf("Failure: create dir: %s\n", name);
    dir_remove (dir_, name_);
  }

  done:
    dir_close (dir_);

  free(name_);
  if (!ndir && ninode){
    inode_close(ninode);
  } else if (ndir) {
    dir_close(ndir);
  }

  return success;
}
Ejemplo n.º 19
0
bool filesys_chdir (const char *full_path)
{
    char leaf_name[NAME_MAX + 1];
    if (!dir_get_leaf_name (full_path, leaf_name))
        return false;

    struct dir *parent_dir = dir_get_parent_dir (full_path);
    if (parent_dir == NULL)
        return false;
    struct inode *tmp;
    if (!dir_lookup (parent_dir, leaf_name, &tmp))
        return false;
    if (!inode_is_dir (tmp))
        return false;
    struct dir *actual_dir = dir_open (tmp);
    thread_current()->cwd_sector = inode_get_inumber(dir_get_inode (actual_dir));
    dir_close (actual_dir);
    return true;
}
Ejemplo n.º 20
0
/*! Creates the directory named dir. Return true if successful, but false
 * if dir already exists or if any directory name in dir, besides the last
 * one does not exist. */
bool _mkdir(const char* dir) {
    /* Inode for the new directory created. */
    struct inode* next_inode;
    
    /* Parent directory. */
    struct dir* cur_dir;
    
    /* Name of the directory. */
    char name[15];
    
    /* Sector number allocated to the new directory. */
    block_sector_t sector;
    
    /* Check the validity of the pointer*/
    if (!checkva(dir))
       exit(-1);
    
    /* The compose the path to a final dir name and its parent directory. */
    if (!decompose_dir(dir, name, &cur_dir)){
        return false;
    }
    ASSERT(cur_dir != NULL);
    
    /* Allocate a new sector for this new directory;
     * Create a new directory */
    if (!free_map_allocate(1, &sector) || 
        !dir_create(sector, 0, dir_get_inode(cur_dir)->sector)){
        
        dir_close(cur_dir);
        return false;
    }
    
    /* Add the new directory to its parent directory. */
    if (!dir_add(cur_dir, name, sector)){
        free_map_release(sector, 1);
        dir_close(cur_dir);
        return false;
    }
    
    dir_close(cur_dir);
    return true;
}
Ejemplo n.º 21
0
/* create directory file */
bool filesys_create_dir(const char *name)
{
	struct dir *parent_dir;
	struct inode *parent_inode;
	struct inode *tmp;
	struct dir *new_dir;
	bool result = false;
	/* if dir name is NULL, return false*/
	if(name == NULL)
		return result;
	/* copy name to cp_name */
	char *cp_name = malloc( sizeof(char) * (strlen(name)+1) );
	strlcpy(cp_name, name, strlen(name)+1 );
	char *file_name;
	file_name = malloc( sizeof(char) * (strlen(name)+1) );
	if(file_name == NULL)
	{
		free(cp_name);
		return result;
	}
	parent_dir = parse_path(cp_name, file_name);
	/* if already same name file exist in directory, return false*/
	if(dir_lookup(parent_dir, file_name, &tmp) == true)
		return result;
	/* allocate bitmap */
	block_sector_t sector_idx;
	free_map_allocate(1, &sector_idx);
	/* create directory */
	dir_create(sector_idx, 16);
	/* add new entry to parent directory */
	dir_add(parent_dir, file_name, sector_idx);
	/* add entry '.' and '..' to directory */
	new_dir = dir_open( inode_open(sector_idx) ); 
	dir_add(new_dir,".",sector_idx);
	parent_inode = dir_get_inode(parent_dir);
	dir_add(new_dir,"..", inode_get_inumber(parent_inode));

	free(cp_name);
	free(file_name);
	result = true;
	return result;
}
Ejemplo n.º 22
0
/* Creates a file named NAME with the given INITIAL_SIZE.
   Returns true if successful, false otherwise.
   Fails if a file named NAME already exists,
   or if internal memory allocation fails. */
bool
filesys_create (const char *name, off_t initial_size, uint32_t is_dir) 
{
  block_sector_t inode_sector = (block_sector_t) -1;
  struct inode *inode = NULL;
  struct dir *search_dir = get_cwd(name);

  if (search_dir == NULL) {
    return false;
  }

  char *part = malloc(NAME_MAX + 1);
  if (part == NULL) {
    return false;
  }
  memset(part, 0, NAME_MAX + 1);
  int retrieved_next_part;
  for (retrieved_next_part = get_next_part(part, &name); retrieved_next_part > 0;
       retrieved_next_part = get_next_part(part, &name)) {
    if (dir_lookup (search_dir, part, &inode)) {
      if (!inode_is_dir(inode)) {
        break;
      } else {
        dir_close(search_dir);
        search_dir = dir_open(inode);
        if (search_dir == NULL) {
          free(part);
          return false;
        }
      }
    } else {
      inode = NULL;
      break;
    }
  }
  if (inode != NULL || get_next_part(part, &name) != 0) {
    if (inode != NULL && !inode_is_dir(inode)) {
      inode_close(inode);
    }
    dir_close(search_dir);
    free(part);
    return false;
  }

  
  bool success = false;
  if (is_dir) {
    block_sector_t parent_sector = inode_get_inumber(dir_get_inode(search_dir));
    success = (search_dir != NULL
               && free_map_allocate (1, &inode_sector)
               && dir_create (inode_sector, initial_size, parent_sector)
               && dir_add (search_dir, part, inode_sector));
  } else {
    success = (search_dir != NULL
               && free_map_allocate (1, &inode_sector)
               && inode_create (inode_sector, initial_size, is_dir)
               && dir_add (search_dir, part, inode_sector));
  }
  
  if (!success) 
    free_map_release (inode_sector, 1);
  dir_close (search_dir);
  free(part);
  return success;
}
Ejemplo n.º 23
0
/* Decomposes the given path (dir) to a final file or directory name and
 * its parent directory. Write the name to ret_name, and the parent directory
 * struct to par_dir. Returns true if the decomposition is successfull, i.e.
 * successfully decomposed the final name, and open all the directories in 
 * path successfully. */
bool decompose_dir(const char* dir, char* ret_name, struct dir** par_dir){
    /* parent directory*/
    struct dir* cur_dir;
    
    /* inode for iteration*/
    struct inode* next_inode;
    
    /* iterator for the path string*/
    unsigned i;
    
    /* Current process*/
    struct thread* t = thread_current();
    
    /* Final name of the file or the directory*/
    char name[15];
    
    /* Check that give path is not empty*/
    if (*dir == NULL || dir[0] == '\0')
        return false;

    if (dir[0] == '/') {
        /* If give an absolute path, then first set cur_dir as root. */
        cur_dir = dir_open_root();
    } else if (t->cur_dir == NULL) {
        /* Given path is relative. However, as the process's cur_dir is
         * NULL, which is equivelant to the root directory in our 
         * implemtation. Also set update proccess's cur_dir to root. */
        cur_dir = dir_open_root();
        t->cur_dir = dir_open_root();
    } else {
        /* Give path is also relative. And set cur_dir same as the process's 
         * working director. */
        if (dir_get_inode(t->cur_dir)->removed)
            /* If the working directory has been removed, then return false 
             * right away. */
            return false;
        cur_dir = dir_reopen(t->cur_dir);    
    }

    /* Iterate the path with dir as iterator until the null terminator. */
    while (*dir != '\0'){
        /* Reset the index iterator. for extract next subdirectory name. */
        i = 0;
        
        /*Skip the directory symbol*/
        while (*dir == '/')
            ++dir;
        
        /* Increment i until the directory symbol or null terminator*/
        while ((*(dir + i) != '/' && *(dir + i) != '\0') && (i <= 15)){
            ++i;
        }
        
        /* Enforces NAME_MAX length to all directories*/
        if (i > 15) {
            dir_close(cur_dir);
            return false;
        }

        /* Copy this directory name to name*/
        memcpy(name, dir, i);
        name[i] = '\0';

        /* If this is the last name, then we got our final dir / file name,
         * i.e. finished decomposition of path. */
        if (*(dir + i) == '\0')
            break;
        
        /* Look up the file / dir from the parent directory cur_dir. */
        if (!dir_lookup(cur_dir, name, &next_inode)) {
            dir_close(cur_dir);
            return false;
        }
        
        /* Close the current parent directory, as we have found the next 
         * one.*/
        dir_close(cur_dir);
        
        /* Makes sure that we are always opening a directory. */
        if  (next_inode->data.type != DIR_INODE_DISK) {
            inode_close(next_inode);
            return false;
        }
        
        /* Open up the next directory in path. */
        cur_dir = dir_open(next_inode);
        
        if (cur_dir == NULL) {
            inode_close(next_inode);
            return false;
        }
        
        /* Advance. */
        dir += i;
    }
    
    /* Set the parent directory as cur_dir*/
    *par_dir = cur_dir;
    
    /*Copy the final name ret_name*/
    strlcpy(ret_name, name, strlen(name) + 1);

    return true;
}
Ejemplo n.º 24
0
/* Deletes the file named NAME.
   Returns true if successful, false on failure.
   Fails if no file named NAME exists,
   or if an internal memory allocation fails. */
bool
filesys_remove (const char *name)
{
    char name_copy[MAX_FULL_PATH];
    char *file_name = NULL;
    char *token = NULL, *save_ptr = NULL;
    struct dir *dir = NULL;

    /* Null name not allowed. */
    if (name[0] == '\0')
    {
        return false;
    }

    /* Open parent directory. */
    dir = recursive_dir_open (name);

    /* extract only file name from full path. */
    strlcpy (name_copy, name, strlen (name) + 1);
    for (token = strtok_r (name_copy, "/", &save_ptr); token != NULL;
            token = strtok_r (NULL, "/", &save_ptr))
    {
        file_name = token;
    }
    if (!file_name)
    {
        dir_close (dir);
        return false;
    }

    /* Check for and prevent simultaneous accesses. */
    struct list_elem *e;
    block_sector_t parent_dir_sector = inode_get_inumber (dir_get_inode (dir));
    lock_acquire (&cur_name_list_lock);
    for (e = list_begin (&cur_name_list);
            e != list_end (&cur_name_list);
            e = list_next (e))
    {
        struct cur_name_list_entry *cur_name_list_entry = NULL;
        cur_name_list_entry = list_entry (e, struct cur_name_list_entry, elem);
        if ((cur_name_list_entry->parent_dir_sector == parent_dir_sector)
                && (!strcmp (file_name, cur_name_list_entry->file_name)))
        {
            dir_close (dir);
            return false;
        }
    }
    struct cur_name_list_entry *name_entry = NULL;
    name_entry = malloc (sizeof (struct cur_name_list_entry));
    if (name_entry == NULL)
    {
        dir_close (dir);
        lock_release (&cur_name_list_lock);
        return false;
    }
    strlcpy (name_entry->file_name, file_name, strlen (file_name) + 1);
    name_entry->parent_dir_sector = parent_dir_sector;
    list_push_back (&cur_name_list, &name_entry->elem);
    lock_release (&cur_name_list_lock);

    /* Remove dirctory entry of file. */
    bool success = dir != NULL && dir_remove (dir, file_name);
    dir_close (dir);
    lock_acquire (&cur_name_list_lock);
    list_remove (&name_entry->elem);
    lock_release (&cur_name_list_lock);
    free (name_entry);
    return success;
}
Ejemplo n.º 25
0
/* Creates a file named NAME with the given INITIAL_SIZE.
   Returns true if successful, false otherwise.
   Fails if a file named NAME already exists,
   or if internal memory allocation fails. */
bool
filesys_create (const char *name, off_t initial_size)
{
    char name_copy[MAX_FULL_PATH];
    char *file_name = NULL;
    char *token, *save_ptr;
    block_sector_t inode_sector = 0;

    /* Null file name not allowed. */
    if (name[0] == '\0')
    {
        return false;
    }
    if (strlen (name) > MAX_FULL_PATH)
    {
        return false;
    }

    /* Open parent directory. */
    struct dir *dir = recursive_dir_open (name);
    if (!dir)
    {
        return false;
    }
    /* extract only file name from entire path. */
    strlcpy (name_copy, name, strlen (name) + 1);
    for (token = strtok_r (name_copy, "/", &save_ptr); token != NULL;
            token = strtok_r (NULL, "/", &save_ptr))
    {
        file_name = token;
    }
    if (file_name[0] == '\0')
    {
        dir_close (dir);
        return false;
    }
    if (strlen (file_name) > NAME_MAX)
    {
        dir_close (dir);
        return false;
    }

    /* Check for and prevent simultaneous accesses. */
    struct list_elem *e;
    block_sector_t parent_dir_sector = inode_get_inumber (dir_get_inode (dir));
    lock_acquire (&cur_name_list_lock);
    for (e = list_begin (&cur_name_list);
            e != list_end (&cur_name_list);
            e = list_next (e))
    {
        struct cur_name_list_entry *cur_name_list_entry = NULL;
        cur_name_list_entry = list_entry (e, struct cur_name_list_entry, elem);
        if ((cur_name_list_entry->parent_dir_sector == parent_dir_sector)
                && (!strcmp (file_name, cur_name_list_entry->file_name)))
        {
            dir_close (dir);
            return false;
        }
    }
    struct cur_name_list_entry *name_entry = NULL;
    name_entry = malloc (sizeof (struct cur_name_list_entry));
    if (name_entry == NULL)
    {
        dir_close (dir);
        lock_release (&cur_name_list_lock);
        return false;
    }
    strlcpy (name_entry->file_name, file_name, strlen (file_name) + 1);
    name_entry->parent_dir_sector = parent_dir_sector;
    list_push_back (&cur_name_list, &name_entry->elem);
    lock_release (&cur_name_list_lock);

    /* Create file. and add directory entry. */
    bool success = (dir != NULL
                    && free_map_allocate (1, &inode_sector)
                    && inode_create (inode_sector, initial_size)
                    && dir_add (dir, file_name, inode_sector, true));
    if (!success && inode_sector != 0)
        free_map_release (inode_sector, 1);
    dir_close (dir);

    lock_acquire (&cur_name_list_lock);
    list_remove (&name_entry->elem);
    lock_release (&cur_name_list_lock);
    free (name_entry);
    return success;
}
Ejemplo n.º 26
0
static struct dir *get_directory(char *path)
{
  char s[1024];
  memcpy(s,path,strlen(path)+1);
  char *token, *save_ptr,*prev_token="", *next_token="";//,*save,*real;
  token = strtok_r(s,"/",&save_ptr);
//  real = strtok_r(token,"/",&save);
  struct dir *start;
    struct inode *inode;
  if( s[0]=='/' || !thread_current()->curr_dir )
  {
//    printf("when create a/b\n");
    start = dir_open_root();
  }
  else
  {
//    printf("come on\n");
    if( inode_is_removed(dir_get_inode(thread_current()->curr_dir)))
      return NULL;
    else
      start = dir_reopen(thread_current()->curr_dir);
  }
/*  
  if( strcmp(token,"." )==0)
  {
  }
  else if( strcmp(token,"..") == 0)
  {
    if( thread_current()->curr_dir)
      start = dir_get_parent(thread_current()->curr_dir);
    else
      start = dir_open_root();
  }
  else
  {
    printf("here\n");
    if(thread_current()->curr_dir)
    {
//      printf("also here\n");
      start = dir_reopen(thread_current()->curr_dir);
    }
    else
    {      
//    printf("when create a/b\n");
      start = dir_open_root();
    }
  }*/
//  printf("first setting\n");
//  real = strtok_r(token,"/",&save);
//  printf("token %s  s %s\n",token,s);
  if(token)
  {
    next_token = strtok_r(NULL,"/",&save_ptr);
/*    if(next_token == NULL)
      printf("AAAA\n");
    else
      printf("%s\n",next_token);*/
//  printf("first %s second %s\n",token,next_token);
  }
  while( next_token!=NULL)
  {
//    printf("but not here\n");
    if( strcmp(token,"." )==0)
    {
      continue;
    }
    else if( strcmp(token,"..") == 0)
    {
      if(inode_get_inumber(dir_get_inode(start))!=1)
      {
        start=dir_get_parent(start);
      }
    }
    else
    {
      if(dir_lookup(start ,token, &inode))
      {
//    printf("when create a/b token:%s\n",token);
//        printf("aaaa\n");
        dir_close(start);
        start = dir_open(inode);
      }
      else
        return NULL;
//      dir_close(start);
/*      if( inode==NULL )
      {

        return NULL;
      }
      else if(inode_is_removed(inode))
        return NULL;*/
//      start = dir_open(inode);
    }
    prev_token = token;
    token = next_token;
    next_token = strtok_r(NULL,"/",&save_ptr);
//      printf("first %s second %s\n",token,next_token);
  }
//  printf("directory name : %s \n",token);
  return start;
}