示例#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);
}
示例#2
0
/* Opens the directory for the given path. If the path is null, opens the
 * CWD */
struct dir *
dir_open_path (const char *path)
{
  block_sector_t sector = path_traverse (path, NULL);
  if (sector == INODE_INVALID_BLOCK_SECTOR) return NULL;
  struct dir *d = dir_open (inode_open (sector));
  if (inode_is_removed (d->inode)) return NULL;
  return d;
}
示例#3
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;
}