group/pintos/src/filesys/filesys.c
/* 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. */
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.
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 *path, off_t initial_size)
{
    if (strlen(path) >= PATH_MAX)
        return false;
    char *buff = malloc(sizeof(char) * PATH_MAX);
    char fname[NAME_MAX + 1];
    struct dir *dir = NULL;
    block_sector_t inode_sector = 0;
    bool success = false;
    memset(buff, 0, sizeof(char) * PATH_MAX);
    memcpy(buff, path, strlen(path));
    to_abs(buff);
    if (simplify_path(buff)) {
        path_basename(buff, fname);
        if (to_dir_path(buff)
                && (dir = get_dir_by_path(buff)) != NULL
                && free_map_allocate(1, &inode_sector)
                && inode_create(inode_sector, initial_size)
                && dir_add(dir, fname, inode_sector))
        {
            success = true;
        }
    }
    if (inode_sector != 0 && success == false)
        free_map_release(inode_sector, 1);
    dir_close(dir);
    free(buff);
    return success;
}
bool filesys_create ( const char * path , off_t initial_size ) { if ( strlen ( path ) >= PATH_MAX ) return false ; char * buff = malloc ( sizeof ( char ) * PATH_MAX ) ; char fname [ NAME_MAX + 1 ] ; struct dir * dir = NULL ; block_sector_t inode_sector = 0 ; bool success = false ; memset ( buff , 0 , sizeof ( char ) * PATH_MAX ) ; memcpy ( buff , path , strlen ( path ) ) ; to_abs ( buff ) ; if ( simplify_path ( buff ) ) { path_basename ( buff , fname ) ; if ( to_dir_path ( buff ) && ( dir = get_dir_by_path ( buff ) ) != NULL && free_map_allocate ( 1 , & inode_sector ) && inode_create ( inode_sector , initial_size ) && dir_add ( dir , fname , inode_sector ) ) { success = true ; } } if ( inode_sector != 0 && success == false ) free_map_release ( inode_sector , 1 ) ; dir_close ( dir ) ; free ( buff ) ; return success ; }
Creates a file named NAME with the given INITIAL_SIZE.
Creates a file named NAME with the given INITIAL_SIZE .
// check path length // block_sector_t inode_sector = 0; // struct dir *dir = dir_open_root (); // bool success = (dir != NULL //                 && free_map_allocate (1, &inode_sector) //                 && inode_create (inode_sector, initial_size) //                 && dir_add (dir, name, inode_sector)); // if (!success && inode_sector != 0) //   free_map_release (inode_sector, 1); // dir_close (dir); // return success;