Beispiel #1
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;
}
Beispiel #2
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);
}
Beispiel #3
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;
}
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;
}