Example #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)
{
  struct dir *dir;
  struct inode *inode = NULL;
  char parsed_name[NAME_MAX + 1];
  if (*name == NULL)
    {
      return NULL;
    }

  bool success = parse_path (name, &dir, parsed_name);
  if ((strlen (parsed_name) > NAME_MAX))
    {
      dir_close (dir);
      return NULL;
    }

  if (success)
  {
    if (dir != NULL)
       dir_lookup (dir, parsed_name, &inode);
    dir_close (dir);
  }
  struct file *result = file_open (inode);
  return result;
}
Example #2
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) 
{

  block_sector_t inode_sector = 0;
  struct dir *dir;
  char parsed_name[NAME_MAX + 1];
  
  if (*name == NULL || (strlen (name) > NAME_MAX))
    {
      return false;
    }
  bool success = parse_path (name, &dir, parsed_name);
  if (!success)
    {
      return success;
    }

  struct inode *inode;

  success = (dir != NULL
                  && free_map_allocate (1, &inode_sector)
                  && inode_create (inode_sector, initial_size, false)
                  && dir_add (dir, parsed_name, inode_sector)
                  && dir_lookup (dir, ".", &inode));
  if (!success && inode_sector != 0) 
    free_map_release (inode_sector, 1);
  dir_close (dir);


  return success;
}
Example #3
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);
}
Example #4
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;
}
Example #5
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;
}
Example #6
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) {
    struct dir *dir = dir_open_root();
    struct inode *inode = NULL;

    if (dir != NULL)
        dir_lookup(dir, name, &inode);
    dir_close(dir);

    return file_open(inode);
}
Example #7
0
struct dir*
get_containing_dir (const char* path)
{
   char s[strlen(path)+1];
   memcpy(s, path, strlen(path)+1);
   
   char *save_ptr, *next_token = NULL, *token = strtok_r(s, "/", &save_ptr);
   struct dir* dir;
   if (s[0] == 47 || !thread_current ()->cwd)
   {
	dir = dir_open_root ();
   } 
   else
   {
       dir = dir_reopen (thread_current()->cwd);
   }
  
   if (token)
   {
      next_token = strtok_r (NULL, "/",  &save_ptr);
   }
   while (next_token != NULL)
   {
        if (strcmp(token, ".") != 0)
        {
	    struct inode *inode;
	    if (strcmp(token, "..") == 0)
            {
                if (!dir_get_parent (dir, &inode))
	        {
		    return NULL;
	        }
	    }
	    else
	    {
		if (!dir_lookup (dir, token, &inode))
		 {
			return NULL;
		 }
	    }
	    if (inode_is_dir (inode))
	    {
	        dir_close (dir);
	        dir = dir_open (inode);
	    }
	    else
	    {
	        inode_close (inode);
            }
         }
	token = next_token;
        next_token = strtok_r (NULL, "/", &save_ptr);   
   }
   return dir;
}
Example #8
0
static void peer_lookup (void *msg)
{
	fs_msg_s	*m = msg;
	int		rc;
FN;
	m->dir_id = dir_lookup(m->dir_parent_id, m->dir_name);
	rc = send_tau(m->q.q_passed_key, m);//should use share key
	if (rc) {
		eprintf("peer_lookup send_tau failed %d", rc);
	}
}
Example #9
0
int
main ()
{
  mach_port_t root;
#if HURDISH_TESTS
  extern file_t *_hurd_init_dtable;
  char string[] = "Did this get into the file?\n";
  file_t filetowrite;
  retry_type retry;
  char pathbuf[1024];
  int written;
  error_t err;
#endif

  root = getcrdir ();

  printf ("fstests running...\n");

#if HURDISH_TESTS
  if ((err = dir_unlink (root, "CREATED")) && err != ENOENT)
    error (0, err, "Error on unlink");
  else if (err = dir_lookup (root, "CREATED", O_WRITE | O_CREAT, 0666,
			     &retry, pathbuf, &filetowrite))
    error (0, err, "Error on lookup");
  else if (err = io_write (filetowrite, string, strlen (string), -1, &written))
    error (0, err, "Error on write");
  else if (written != strlen (string))
    error (0, 0, "Short write: %d\n", written);
  else if (err = file_syncfs (filetowrite, 1, 0))
    error (0, err, "Error on sync");
#else

  if (unlink ("/newdir"))
    error (0, errno, "unlink");
  if (rmdir ("/newdir"))
    error (0, errno, "1st rmdir");
  if (mkdir ("/newdir", 0777))
    error (0, errno, "1st mkdir");
  if (rename ("/newdir", "/newdir2"))
    error (0, errno, "1st rename");
  if (rmdir ("/foo"))
    error (0, errno, "2nd rmdir");
  if (mkdir ("/foo", 0777))
    error (0, errno, "2nd mkdir");
  if (rename ("/newdir2", "/foo"))
    error (0, errno, "2nd rename");
  sync ();
#endif

  printf ("All done.\n");
  malloc (0);

  return 0;
}
Example #10
0
struct dir *
dest_dir_from_path (char *path)
{
  struct dir *directory = dir_from_path (path);
  struct inode *dir_inode;
  char *file_name = retrieve_file_name(path);

  if (!dir_lookup (directory, file_name, &dir_inode))
    return NULL;

  return dir_open (dir_inode);
}
Example #11
0
File: fs.c Project: amisharma/OSLAB
// Evaluate a path name, starting at the root.
// On success, set *pf to the file we found
// and set *pdir to the directory the file is in.
// If we cannot find the file but find the directory
// it should be in, set *pdir and copy the final path
// element into lastelem.
static int
walk_path(const char *path, struct File **pdir, struct File **pf, char *lastelem)
{
	const char *p;
	char name[MAXNAMELEN];
	struct File *dir, *f;
	int r;
//	cprintf("entering walk path\n");
//	if (*path != '/')
//		return -E_BAD_PATH;
	path = skip_slash(path);
	f = &super->s_root;
	dir = 0;
	name[0] = 0;

	if (pdir)
		*pdir = 0;
	*pf = 0;
	while (*path != '\0') {
//		cprintf("entering 2 walk path\n");
		dir = f;
		p = path;
		while (*path != '/' && *path != '\0')
			path++;
		if (path - p >= MAXNAMELEN)
			return -E_BAD_PATH;
		memmove(name, p, path - p);
		name[path - p] = '\0';
		path = skip_slash(path);

		if (dir->f_type != FTYPE_DIR)
		{
			cprintf("error 1\n");
			return -E_NOT_FOUND;
		}
		if ((r = dir_lookup(dir, name, &f)) < 0) {
			if (r == -E_NOT_FOUND && *path == '\0') {
				if (pdir)
					*pdir = dir;
				if (lastelem)
					strcpy(lastelem, name);
				*pf = 0;
			}
//			cprintf("error 2 \n");
			return r;
		}
	}

	if (pdir)
		*pdir = dir;
	*pf = f;
	return 0;
}
Example #12
0
/*
 *	Initialise.
 *	Try using the capabilities STDERR and STDOUT in turn
 *	otherwise lookup /tty:00
 */
static capability *
init()
{
    static capability *capp, tty;

    if ((capp = getcap("STDERR")) != NULL || (capp = getcap("STDOUT")) != NULL)
    	return capp;
    if ((capp = getcap("ROOT")) != NULL
    && dir_lookup(capp, "tty:00", &tty) == STD_OK) {
	return capp = &tty;
    }
    return (capability *) NULL;
} /* init */
Example #13
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;
}
Example #14
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)
{
  lock_acquire(&create_lock);
  struct dir *dir = dir_open_root ();
  struct inode *inode = NULL;

  if (dir != NULL)
    dir_lookup (dir, name, &inode);
  dir_close (dir);

  lock_release(&create_lock);
  return file_open (inode);
}
Example #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 file *
filesys_open (const char *path)
{
  char *name = get_filename (path);
  struct dir *dir = dir_get (path);
  struct inode *inode = NULL;

  if (dir != NULL && name != NULL && !is_dir (path))
    dir_lookup (dir, name, &inode);

  dir_close (dir);
  free (name);
  return file_open (inode);
}
Example #16
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 (strlen(name) == 0)
   {
  	return NULL;
   }
  struct dir *dir = get_containing_dir (name);
  char* file_name = get_filename (name);
  struct inode *inode = NULL;

  if (dir != NULL)
    {
	if (strcmp(file_name, "..") == 0)
	{
	   if (!dir_get_parent(dir, &inode))
	    {
	        free (file_name);
		return NULL;
	    }
	}
        else if ((dir_is_root(dir) && strlen(file_name) == 0) ||
		 strcmp(file_name, ".") == 0)
         {
		free (file_name);
		return (struct file*)dir;
         }
         else
         {
            dir_lookup(dir, file_name, &inode);
         }


    }
  dir_close (dir);
  free(file_name);
  if (!inode)
   {
	return NULL;
   }

   if (inode_is_dir(inode))
   {
	return (struct file *)dir_open(inode);
   }

  return file_open (inode);
}
Example #17
0
/* changes the current working directory to path */
bool filesys_chdir(const char *path)
{
  struct dir *dir = get_containing_dir(path);
  char *file_name = get_file_name(path);
  struct inode *inode = NULL;
  struct thread *cur = thread_current();

  if (dir != NULL)
  {
    if(strcmp(file_name, "..") == 0)
    {
      if(!dir_get_parent(dir, &inode))
      {
	free(file_name);
	return false;
      }
    }
    else if(strcmp(file_name, ".") == 0)
    {
      cur->cwd = dir;
      free(file_name);
      return true;
    }
    else if(dir_is_root(dir) && strlen(file_name) == 0)
    {
      cur->cwd = dir;
      free(file_name);
      return true;
    }
    else
    {
      dir_lookup (dir, file_name, &inode);
    }
  }

  dir_close(dir);
  free(file_name);

  dir = dir_open(inode);
  if(dir != NULL) 
  {
    dir_close(cur->cwd);
    cur->cwd = dir;
    return true;
  }

  return false;
}
Example #18
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)
{
  bool exists = false;
  struct dir *dir = dir_open_root ();
  struct inode *inode = NULL;
  if (dir != NULL)
    exists = dir_lookup (dir, name, &inode);

  dir_close (dir);
  
  if (!exists)
    return NULL;

  return file_open (inode);
}
Example #19
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 * parse = parse_filename (name); 
  //get the correct dir
  struct dir *dir = dir_lookup_rec (parse);

  struct inode *inode = NULL;

  if (dir != NULL)
    dir_lookup (dir, parse, &inode);
  dir_close (dir);

  return file_open (inode);
}
Example #20
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)
{
  struct dir *dir = dir_open_root ();
  struct inode *inode = NULL;

  if (dir != NULL)
    dir_lookup (dir, name, &inode);
  dir_close (dir);

/* SH proj4*/
 // 	if (inode == NULL)
  //  	PANIC ("sh: inode is NULL\n");
/* == SH proj4*/
  return file_open (inode);
}
Example #21
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)
{
  struct dir *dir = dir_open_root ();
  struct inode *inode = NULL;

  /*if(dir==NULL)
  {
	  printf("DIR NULL\n");
  }*/
  
  if (dir != NULL)
    dir_lookup (dir, name, &inode);
  dir_close (dir);

  return file_open (inode);
}
/* 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)
{
  struct thread *cur = thread_current();
  struct dir *dir;
  if(cur->current_directory == NULL)
    dir = dir_open_root ();
  else
    dir = dir_open_current();
  struct inode *inode = NULL;

  if (dir != NULL)
    dir_lookup (dir, name, &inode);
  dir_close (dir);

  return file_open (inode);
}
Example #23
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;
}
/* 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 file *dir, const char *name, block_sector_t inode_sector)
{
  ASSERT (file_is_dir (dir));
  struct dir_entry e;
  size_t ofs;
  bool success = false;

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

  /* Check NAME for validity. */
  if (*name == '\0' || strlen (name) > NAME_MAX)
    return false;

  bool locked = file_lock (dir);
  /* Check that NAME is not in use. */
  if (dir_lookup (dir, name) >= 0)
    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. */
  size_t dir_size = inode_length (dir->inode);
  for (ofs = 0; ofs < dir_size; ofs += sizeof e) 
    {
      inode_read_at (dir->inode, &e, sizeof e, ofs);
      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:
  if (locked) file_unlock (dir);
  return success;
}
Example #25
0
/* Start of Project 4 */
bool
change_directory (char *dir)
{
  struct inode *dir_node = NULL;
  struct dir *directory = dir_from_path (dir);
  struct thread *cur = thread_current ();
  char *file_name = retrieve_file_name(dir);
  bool success = false;

  if (directory != NULL)
  {
    if ((check_if_root_dir(directory) && strlen (file_name) == 0)
        || strcmp (file_name, ".") == 0)
    {
      cur->dir = directory;
      free(file_name);
      return true;
    }
    else if (strcmp (file_name, "..") == 0)
      success = retrieve_dir_parent (directory, &dir_node);
    else
    {
      dir_lookup (directory, file_name, &dir_node);
      success = true;
    }
  }

  free(file_name);
  if (!success)
    return success;
  dir_close (directory);
  directory = dir_open (dir_node);

  if (directory != NULL)
  {
    dir_close(cur->dir);
    cur->dir = directory;
    success = true;
  }
  else
    success = false;

  return success;
}
Example #26
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;
}
Example #27
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;
}
Example #28
0
/* Opens the file with the given NAME.
   Returns the new file if successful or a null pointer
   otherwise.
*/
void
filesys_open (const char *name, struct file **file, struct dir **dir, bool *isdir)
{
  if (strlen(name) == 0) {
    if (file) *file = NULL;
    if (dir) *dir = NULL;
    if (isdir) *isdir = false;
    return;
  }
  struct dir *dir_ = filesys_get_dir (name);
  char *name_ = filesys_get_name (name);
  struct inode *inode = NULL;
  bool isdir_ = false;

  if (strcmp (name_, "") == 0) {
    if (file ) *file = NULL;
    if (dir) *dir = dir_;
    if (isdir) *isdir = true;
    free (name_);
  }  else if (dir_ && !dir_lookup (dir_, name_, &inode, &isdir_)) {
    if (file) *file = NULL;
    if (dir) *dir = NULL;
    if (isdir) *isdir = false;
    dir_close (dir_);
    free (name_);
  } else {
    dir_close (dir_);
    free (name_);
    if (isdir_) {
      if (file) *file = NULL;
      ASSERT (dir);
      *dir = dir_open (inode);
      if (isdir) *isdir = true;
    } else {
      ASSERT (file);
      *file = file_open (inode);
      if (dir) *dir = NULL;
      if (isdir) *isdir = false;
    }
  }
}
Example #29
0
/* parsing the path, and return working directory */
struct dir* parse_path(char *path_name, char *file_name)
{
	struct dir *dir;
	char *token, *next_token, *save_ptr;
	struct inode *inode;
	if(path_name == NULL || file_name == NULL)
		return NULL;
	if(strlen(path_name) == 0)
		return NULL;
	/* if path name is absulute path */
	if(path_name[0] == '/')
		dir = dir_open_root();
	else /* path name is relative path */
		dir = dir_reopen(thread_current()->thread_dir);
	token = strtok_r(path_name, "/", &save_ptr);
	next_token = strtok_r(NULL, "/", &save_ptr);
	while(token != NULL && next_token != NULL)
	{
		/* find file named token in dir directory */
		if(dir_lookup(dir, token, &inode) == false)
		{
			return NULL;
		}
		/* if token is not a directory */
		if(inode_is_dir(inode) == false){
			dir_close(dir);
			return NULL;
		}
		/* close dir */
		dir_close(dir);
		/* set dir */
		dir = dir_open(inode);
		/* advance */
		token = next_token;
		next_token = strtok_r(NULL, "/", &save_ptr);
	}
	/* copy token to file_name */
	strlcpy(file_name, token, strlen(token)+1);
	return dir;
}
Example #30
0
File: p2i.c Project: bajdcc/MiniOS
static struct inode *_path2inode(char *path, int parent, char *name) {
    struct inode *ip, *next;

    ip = 0;
    if (*path == '/') {
        ip = iget(ROOT_DEV, ROOT_INO);
    } else {
        ip = idup(proc->cwd);
    }     

    while ((path = skipelem(path, name)) != 0) {
        /* read from disk */
        ilock(ip);
        if (!S_ISDIR(ip->mode)) {
            iunlockput(ip);
            return 0;
        }

        if (parent && *path == '\0') {
            iunlock(ip);
            return ip;
        }

        if ((next = dir_lookup(ip, name, 0)) == 0) {
            iunlockput(ip);
            return 0;
        }

        iunlockput(ip);
        ip = next;
    }

    if (parent) {
        iput(ip);
        return 0;
    }

    return ip;
}