group/pintos/src/filesys/filesys.c
/* 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. */
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.
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 fname[NAME_MAX];
    char *buff = malloc(sizeof(char) * PATH_MAX);
    char *dir_path = malloc(sizeof(char) * PATH_MAX);
    struct dir *dir = NULL;
    struct inode *inode = NULL;
    memset(buff, 0, sizeof(char) * PATH_MAX);
    memset(dir_path, 0, sizeof(char) * PATH_MAX);
    memset(fname, 0, NAME_MAX);
    if (*(path) == '.' && *(path + 1) == '.') {
        buff[0] = '.';
        to_abs(buff);
        if (simplify_path(buff) && get_dir_by_path(buff) == NULL)
            goto done;
    }
    else if (*path == '\0') {
        goto done;
    }
    memset(buff, 0, sizeof(char) * PATH_MAX);
    memcpy(buff, path, strlen(path));
    to_abs(buff);
    if (simplify_path(buff))
    {
        memcpy(dir_path, buff, strlen(buff));
        if (strcmp(buff, "/") == 0) {
            dir = dir_open_root();
            inode = dir_get_inode(dir);
        }
        else if (to_dir_path(dir_path)
                && (dir = get_dir_by_path(dir_path)) != NULL) {
            path_basename(buff, fname);
            dir_lookup(dir, fname, &inode);
            dir_close(dir);
        }
    }
done:
    free(dir_path);
    free(buff);
    return file_open(inode);
}
struct file * filesys_open ( const char * path ) { char fname [ NAME_MAX ] ; char * buff = malloc ( sizeof ( char ) * PATH_MAX ) ; char * dir_path = malloc ( sizeof ( char ) * PATH_MAX ) ; struct dir * dir = NULL ; struct inode * inode = NULL ; memset ( buff , 0 , sizeof ( char ) * PATH_MAX ) ; memset ( dir_path , 0 , sizeof ( char ) * PATH_MAX ) ; memset ( fname , 0 , NAME_MAX ) ; if ( * ( path ) == ' ' && * ( path + 1 ) == ' ' ) { buff [ 0 ] = ' ' ; to_abs ( buff ) ; if ( simplify_path ( buff ) && get_dir_by_path ( buff ) == NULL ) goto done ; } else if ( * path == ' \0 ' ) { goto done ; } memset ( buff , 0 , sizeof ( char ) * PATH_MAX ) ; memcpy ( buff , path , strlen ( path ) ) ; to_abs ( buff ) ; if ( simplify_path ( buff ) ) { memcpy ( dir_path , buff , strlen ( buff ) ) ; if ( strcmp ( buff , " " ) == 0 ) { dir = dir_open_root ( ) ; inode = dir_get_inode ( dir ) ; } else if ( to_dir_path ( dir_path ) && ( dir = get_dir_by_path ( dir_path ) ) != NULL ) { path_basename ( buff , fname ) ; dir_lookup ( dir , fname , & inode ) ; dir_close ( dir ) ; } } done : free ( dir_path ) ; free ( buff ) ; return file_open ( inode ) ; }
Opens the file with the given NAME.
Opens the file with the given NAME .
// if it's relative path, check cwd exist or not // 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);