bool dir_add(struct dir *dir, const char *name, disk_sector_t inode_sector)
인자로 받은 name이라는 이름을 가진 파일을 dir 디렉토리에 추가한다. inode_sector에는 파일의 inode가 있다.
bool dir_add(struct dir *dir, const char *name, disk_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;
/* 이미 해당 이름의 파일이 디렉토리에 있다면 -> success 반환 */
if (lookup(dir, name, NULL, NULL))
goto done;
/* 사용중이지 않은 엔트리를 만날때까지 inode에 해당하는 디스크를 읽음 */
for (ofs = 0; inode_read_at(dir->inode, &e, sizeof e, ofs) == sizeof e; ofs += sizeof e)
if (!e.in_use)
break;
/* 엔트리의 값을 채우고, write_at을 통해 써준다 */
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;
}
그림으로 그리면 이런 느낌이다.
얘를 부르는 함수들은 각각
이중에 filesys_create_link를 보자.
int filesys_create_link(const char *target, const char *linkpath)
{
struct thread *curr = thread_current();
/* Root Directory open */
char file_name[NAME_MAX + 1];
struct dir *dir = parse_path(linkpath, file_name); // 디렉토리
if (dir == NULL)
return -1;
/* struct disk_inode를 저장할 새로운 cluster 할당 */
cluster_t inode_cluster = fat_create_chain(0);
disk_sector_t inode_sector = cluster_to_sector(inode_cluster);
bool succ_create = inode_create_link(inode_sector, target); // link형태의 inode 만듬
bool succ_dir_add = dir_add(dir, file_name, inode_sector);
/* 할당 받은 cluster에 inode를 만들고 directory에 file 추가 */
bool success = (succ_create && succ_dir_add);
if (!success && inode_cluster != 0)
fat_remove_chain(inode_cluster, 0);
dir_close(dir);
return success - 1;
}
얘는 link형태의 inode를 만들고, linkpath를 parse해서 얻어낸 디렉토리에 파일을 더하는 역할을 한다.