group/pintos/src/filesys/directory.c
/* 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. */
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  or a disk or memory
error occurs.
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 or a disk or memory error occurs .
bool
dir_add (struct dir *dir, const char *name, block_sector_t inode_sector)
{
  struct dir_entry e;
  off_t ofs;
  bool success = false;
  ASSERT (dir != NULL);
  ASSERT (name != NULL);
  if (*name == '\0' || strlen (name) > NAME_MAX)
    return false;
  if (lookup (dir, name, NULL, NULL))
    goto done;
  for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
       ofs += sizeof e)
    if (!e.in_use)
      break;
  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:
  return success;
}
bool dir_add ( struct dir * dir , const char * name , block_sector_t inode_sector ) { struct dir_entry e ; off_t ofs ; bool success = false ; ASSERT ( dir != NULL ) ; ASSERT ( name != NULL ) ; if ( * name == ' \0 ' || strlen ( name ) > NAME_MAX ) return false ; if ( lookup ( dir , name , NULL , NULL ) ) goto done ; for ( ofs = 0 ; inode_read_at ( dir -> inode , & e , sizeof e , ofs ) == sizeof e ; ofs += sizeof e ) if ( ! e . in_use ) break ; 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 : return success ; }
Adds a file named NAME to DIR, which must not already contain a
file by that name.
Adds a file named NAME to DIR which must not already contain a file by that name .
/* Check NAME for validity. */ /* Check that NAME is not in use. */ /* 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. */ /* Write slot. */