[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
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:

Classes

struct  dir
 
struct  dir_entry
 

Functions

bool dir_create (disk_sector_t sector, size_t entry_cnt)
 
struct dirdir_open (struct inode *inode)
 
struct dirdir_open_root (void)
 
struct dirdir_reopen (struct dir *dir)
 
void dir_close (struct dir *dir)
 
struct inodedir_get_inode (struct dir *dir)
 
static bool lookup (const struct dir *dir, const char *name, struct dir_entry *ep, off_t *ofsp)
 
bool dir_lookup (const struct dir *dir, const char *name, struct inode **inode)
 
bool dir_add (struct dir *dir, const char *name, disk_sector_t inode_sector)
 
bool dir_remove (struct dir *dir, const char *name)
 
bool dir_readdir (struct dir *dir, char name[NAME_MAX+1])
 

Function Documentation

◆ dir_add()

bool dir_add ( struct dir dir,
const char *  name,
disk_sector_t  inode_sector 
)
127 {
128 struct dir_entry e;
129 off_t ofs;
130 bool success = false;
131
132 ASSERT (dir != NULL);
133 ASSERT (name != NULL);
134
135 /* Check NAME for validity. */
136 if (*name == '\0' || strlen (name) > NAME_MAX)
137 return false;
138
139 /* Check that NAME is not in use. */
140 if (lookup (dir, name, NULL, NULL))
141 goto done;
142
143 /* Set OFS to offset of free slot.
144 * If there are no free slots, then it will be set to the
145 * current end-of-file.
146
147 * inode_read_at() will only return a short read at end of file.
148 * Otherwise, we'd need to verify that we didn't get a short
149 * read due to something intermittent such as low memory. */
150 for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
151 ofs += sizeof e)
152 if (!e.in_use)
153 break;
154
155 /* Write slot. */
156 e.in_use = true;
157 strlcpy (e.name, name, sizeof e.name);
158 e.inode_sector = inode_sector;
159 success = inode_write_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
160
161done:
162 return success;
163}
#define ASSERT(CONDITION)
Definition: debug.h:30
static bool lookup(const struct dir *dir, const char *name, struct dir_entry *ep, off_t *ofsp)
Definition: directory.c:80
#define NAME_MAX
Definition: directory.h:12
off_t inode_write_at(struct inode *inode, const void *buffer_, off_t size, off_t offset)
Definition: inode.c:234
off_t inode_read_at(struct inode *inode, void *buffer_, off_t size, off_t offset)
Definition: inode.c:183
int32_t off_t
Definition: off_t.h:9
#define NULL
Definition: stddef.h:4
size_t strlen(const char *)
Definition: string.c:271
size_t strlcpy(char *, const char *, size_t)
Definition: string.c:302
Definition: directory.c:16
disk_sector_t inode_sector
Definition: directory.c:17
char name[NAME_MAX+1]
Definition: directory.c:18
Definition: directory.c:10
struct inode * inode
Definition: directory.c:11
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_close()

void dir_close ( struct dir dir)
61 {
62 if (dir != NULL) {
64 free (dir);
65 }
66}
void inode_close(struct inode *inode)
Definition: inode.c:150
void free(void *)
Definition: malloc.c:202
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_create()

bool dir_create ( disk_sector_t  sector,
size_t  entry_cnt 
)
25 {
26 return inode_create (sector, entry_cnt * sizeof (struct dir_entry));
27}
bool inode_create(disk_sector_t sector, off_t length)
Definition: inode.c:68
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)
70 {
71 return dir->inode;
72}

◆ dir_lookup()

bool dir_lookup ( const struct dir dir,
const char *  name,
struct inode **  inode 
)
106 {
107 struct dir_entry e;
108
109 ASSERT (dir != NULL);
110 ASSERT (name != NULL);
111
112 if (lookup (dir, name, &e, NULL))
113 *inode = inode_open (e.inode_sector);
114 else
115 *inode = NULL;
116
117 return *inode != NULL;
118}
struct inode * inode_open(disk_sector_t sector)
Definition: inode.c:103
Definition: inode.c:30
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)
32 {
33 struct dir *dir = calloc (1, sizeof *dir);
34 if (inode != NULL && dir != NULL) {
35 dir->inode = inode;
36 dir->pos = 0;
37 return dir;
38 } else {
40 free (dir);
41 return NULL;
42 }
43}
void * calloc(size_t, size_t) __attribute__((malloc))
Definition: malloc.c:149
off_t pos
Definition: directory.c:12
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  )
48 {
50}
struct dir * dir_open(struct inode *inode)
Definition: directory.c:32
#define ROOT_DIR_SECTOR
Definition: filesys.h:9
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] 
)
205 {
206 struct dir_entry e;
207
208 while (inode_read_at (dir->inode, &e, sizeof e, dir->pos) == sizeof e) {
209 dir->pos += sizeof e;
210 if (e.in_use) {
211 strlcpy (name, e.name, NAME_MAX + 1);
212 return true;
213 }
214 }
215 return false;
216}
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 
)
169 {
170 struct dir_entry e;
171 struct inode *inode = NULL;
172 bool success = false;
173 off_t ofs;
174
175 ASSERT (dir != NULL);
176 ASSERT (name != NULL);
177
178 /* Find directory entry. */
179 if (!lookup (dir, name, &e, &ofs))
180 goto done;
181
182 /* Open inode. */
183 inode = inode_open (e.inode_sector);
184 if (inode == NULL)
185 goto done;
186
187 /* Erase directory entry. */
188 e.in_use = false;
189 if (inode_write_at (dir->inode, &e, sizeof e, ofs) != sizeof e)
190 goto done;
191
192 /* Remove inode. */
194 success = true;
195
196done:
198 return success;
199}
void inode_remove(struct inode *inode)
Definition: inode.c:174
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)
55 {
56 return dir_open (inode_reopen (dir->inode));
57}
struct inode * inode_reopen(struct inode *inode)
Definition: inode.c:134
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
81 {
82 struct dir_entry e;
83 size_t ofs;
84
85 ASSERT (dir != NULL);
86 ASSERT (name != NULL);
87
88 for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
89 ofs += sizeof e)
90 if (e.in_use && !strcmp (name, e.name)) {
91 if (ep != NULL)
92 *ep = e;
93 if (ofsp != NULL)
94 *ofsp = ofs;
95 return true;
96 }
97 return false;
98}
int strcmp(const char *, const char *)
Definition: string.c:67
Here is the call graph for this function:
Here is the caller graph for this function: