CS318 - Pintos
Pintos source browser for JHU CS318 course
Data Structures | Functions
directory.c File Reference
#include "filesys/directory.h"
#include <stdio.h>
#include <string.h>
#include <list.h>
#include "filesys/filesys.h"
#include "filesys/inode.h"
#include "threads/malloc.h"
Include dependency graph for directory.c:

Go to the source code of this file.

Data Structures

struct  dir
 A directory. More...
 
struct  dir_entry
 A single directory entry. More...
 

Functions

bool dir_create (block_sector_t sector, size_t entry_cnt)
 Creates a directory with space for ENTRY_CNT entries in the given SECTOR. More...
 
struct dirdir_open (struct inode *inode)
 Opens and returns the directory for the given INODE, of which it takes ownership. More...
 
struct dirdir_open_root (void)
 Opens the root directory and returns a directory for it. More...
 
struct dirdir_reopen (struct dir *dir)
 Opens and returns a new directory for the same inode as DIR. More...
 
void dir_close (struct dir *dir)
 Destroys DIR and frees associated resources. More...
 
struct inodedir_get_inode (struct dir *dir)
 Returns the inode encapsulated by DIR. More...
 
static bool lookup (const struct dir *dir, const char *name, struct dir_entry *ep, off_t *ofsp)
 Searches DIR for a file with the given NAME. More...
 
bool dir_lookup (const struct dir *dir, const char *name, struct inode **inode)
 Searches DIR for a file with the given NAME and returns true if one exists, false otherwise. More...
 
bool dir_add (struct dir *dir, const char *name, block_sector_t inode_sector)
 Adds a file named NAME to DIR, which must not already contain a file by that name. More...
 
bool dir_remove (struct dir *dir, const char *name)
 Removes any entry for NAME in DIR. More...
 
bool dir_readdir (struct dir *dir, char name[NAME_MAX+1])
 Reads the next directory entry in DIR and stores the name in NAME. More...
 

Function Documentation

◆ dir_add()

bool dir_add ( struct dir dir,
const char *  name,
block_sector_t  inode_sector 
)

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.

Definition at line 142 of file directory.c.

References ASSERT, dir_entry::in_use, dir::inode, inode_read_at(), dir_entry::inode_sector, inode_write_at(), lookup(), dir_entry::name, name, NAME_MAX, NULL, strlcpy(), and strlen().

Referenced by filesys_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_close()

void dir_close ( struct dir dir)

Destroys DIR and frees associated resources.

Definition at line 70 of file directory.c.

References free(), dir::inode, inode_close(), and NULL.

Referenced by filesys_create(), filesys_open(), filesys_remove(), and fsutil_ls().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_create()

bool dir_create ( block_sector_t  sector,
size_t  entry_cnt 
)

Creates a directory with space for ENTRY_CNT entries in the given SECTOR.

Opening and closing directories.

Returns true if successful, false on failure.

Definition at line 27 of file directory.c.

References inode_create().

Referenced by do_format().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_get_inode()

struct inode* dir_get_inode ( struct dir dir)

Returns the inode encapsulated by DIR.

Definition at line 81 of file directory.c.

References dir::inode.

◆ dir_lookup()

bool dir_lookup ( const struct dir dir,
const char *  name,
struct inode **  inode 
)

Searches DIR for a file with the given NAME and returns true if one exists, false otherwise.

Reading and writing.

On success, sets *INODE to an inode for the file, otherwise to a null pointer. The caller must close *INODE.

Definition at line 119 of file directory.c.

References ASSERT, inode_open(), dir_entry::inode_sector, lookup(), name, and NULL.

Referenced by filesys_open().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_open()

struct dir* dir_open ( struct inode inode)

Opens and returns the directory for the given INODE, of which it takes ownership.

Returns a null pointer on failure.

Definition at line 35 of file directory.c.

References calloc(), free(), dir::inode, inode_close(), NULL, and dir::pos.

Referenced by dir_open_root(), and dir_reopen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_open_root()

struct dir* dir_open_root ( void  )

Opens the root directory and returns a directory for it.

Return true if successful, false on failure.

Definition at line 55 of file directory.c.

References dir_open(), inode_open(), and ROOT_DIR_SECTOR.

Referenced by filesys_create(), filesys_open(), filesys_remove(), and fsutil_ls().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_readdir()

bool dir_readdir ( struct dir dir,
char  name[NAME_MAX+1] 
)

Reads the next directory entry in DIR and stores the name in NAME.

filesys/directory.h

Returns true if successful, false if the directory contains no more entries.

Definition at line 222 of file directory.c.

References dir_entry::in_use, dir::inode, inode_read_at(), dir_entry::name, name, NAME_MAX, dir::pos, and strlcpy().

Referenced by fsutil_ls().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_remove()

bool dir_remove ( struct dir dir,
const char *  name 
)

Removes any entry for NAME in DIR.

Returns true if successful, false on failure, which occurs only if there is no file with the given NAME.

Definition at line 185 of file directory.c.

References ASSERT, dir_entry::in_use, dir::inode, inode_close(), inode_open(), inode_remove(), dir_entry::inode_sector, inode_write_at(), lookup(), name, and NULL.

Referenced by filesys_remove().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_reopen()

struct dir* dir_reopen ( struct dir dir)

Opens and returns a new directory for the same inode as DIR.

Returns a null pointer on failure.

Definition at line 63 of file directory.c.

References dir_open(), dir::inode, and inode_reopen().

Here is the call graph for this function:

◆ lookup()

static bool lookup ( const struct dir dir,
const char *  name,
struct dir_entry ep,
off_t ofsp 
)
static

Searches DIR for a file with the given NAME.

If successful, returns true, sets *EP to the directory entry if EP is non-null, and sets *OFSP to the byte offset of the directory entry if OFSP is non-null. otherwise, returns false and ignores EP and OFSP.

Definition at line 92 of file directory.c.

References ASSERT, dir_entry::in_use, dir::inode, inode_read_at(), dir_entry::name, name, NULL, and strcmp().

Referenced by dir_add(), dir_lookup(), and dir_remove().

Here is the call graph for this function:
Here is the caller graph for this function: