SFUSE_Documentation
로딩중...
검색중...
일치하는것 없음
dir.c
이 파일의 문서화 페이지로 가기
1// File: src/dir.c
2
3#include "dir.h"
4#include "block.h"
5#include "inode.h"
6#include <errno.h>
7#include <string.h>
8#include <sys/types.h>
9
18int dir_lookup(const struct sfuse_fs *fs, uint32_t dir_ino, const char *name,
19 uint32_t *out_ino) {
20 struct sfuse_inode dir_inode;
21 if (inode_load(fs->backing_fd, &fs->sb, dir_ino, &dir_inode) < 0) {
22 return -ENOENT;
23 }
24
25 /* 모든 direct 블록 순회 */
26 for (uint32_t i = 0; i < 12; i++) {
27 uint32_t blk = dir_inode.direct[i];
28 if (blk == 0)
29 continue;
30
31 uint8_t block[SFUSE_BLOCK_SIZE];
32 if (read_block(fs->backing_fd, blk, block) < 0) {
33 return -EIO;
34 }
35
36 struct sfuse_dirent *entries = (struct sfuse_dirent *)block;
37 for (uint32_t j = 0; j < DENTS_PER_BLOCK; ++j) {
38 if (entries[j].inode == 0)
39 continue;
40 if (strncmp(entries[j].name, name, SFUSE_NAME_MAX) == 0) {
41 *out_ino = entries[j].inode;
42 return 0;
43 }
44 }
45 }
46 return -ENOENT;
47}
48
58int dir_list(const struct sfuse_fs *fs, uint32_t dir_ino, void *buf,
59 fuse_fill_dir_t filler, off_t offset) {
60 struct sfuse_inode dir_inode;
61 if (inode_load(fs->backing_fd, &fs->sb, dir_ino, &dir_inode) < 0) {
62 return -ENOENT;
63 }
64
65 /* "." 및 ".." 기본 엔트리 추가 */
66 filler(buf, ".", NULL, 0, 0);
67 filler(buf, "..", NULL, 0, 0);
68
69 /* 저장된 디렉터리 엔트리 나열 */
70 for (uint32_t i = 0; i < 12; i++) {
71 uint32_t blk = dir_inode.direct[i];
72 if (blk == 0)
73 break;
74
75 uint8_t block[SFUSE_BLOCK_SIZE];
76 if (read_block(fs->backing_fd, blk, block) < 0) {
77 return -EIO;
78 }
79
80 struct sfuse_dirent *entries = (struct sfuse_dirent *)block;
81 for (uint32_t j = 0; j < DENTS_PER_BLOCK; ++j) {
82 if (entries[j].inode == 0)
83 continue;
84 if (entries[j].name[0] == '\0')
85 continue;
86 filler(buf, entries[j].name, NULL, 0, 0);
87 }
88 }
89 return 0;
90}
ssize_t read_block(int fd, uint32_t blk, void *out_buf)
지정한 블록 번호의 데이터를 읽어 버퍼에 저장
Definition block.c:15
int dir_list(const struct sfuse_fs *fs, uint32_t dir_ino, void *buf, fuse_fill_dir_t filler, off_t offset)
디렉터리 엔트리 목록을 FUSE에 전달하여 나열
Definition dir.c:58
int dir_lookup(const struct sfuse_fs *fs, uint32_t dir_ino, const char *name, uint32_t *out_ino)
디렉터리 내에서 지정한 이름의 엔트리를 검색하여 inode 번호 반환
Definition dir.c:18
#define SFUSE_NAME_MAX
Definition dir.h:10
#define DENTS_PER_BLOCK
한 블록당 디렉터리 엔트리 수
Definition dir.h:14
int inode_load(int fd, const struct sfuse_superblock *sb, uint32_t ino, struct sfuse_inode *out)
디스크 이미지에서 아이노드를 읽어 구조체에 로드
Definition inode.c:32
디스크에 저장되는 디렉터리 엔트리 구조체
Definition dir.h:19
uint32_t inode
Definition dir.h:20
char name[256]
Definition dir.h:21
SFUSE 파일 시스템 컨텍스트 구조체
Definition fs.h:21
struct sfuse_superblock sb
Definition fs.h:23
int backing_fd
Definition fs.h:22
디스크에 저장되는 아이노드 구조체
Definition inode.h:22
uint32_t direct[12]
Definition inode.h:30
#define SFUSE_BLOCK_SIZE
블록 크기 (바이트 단위)
Definition super.h:20