-
Notifications
You must be signed in to change notification settings - Fork 732
initial code to support ext4 #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cleverca22
wants to merge
1
commit into
littlekernel:master
Choose a base branch
from
librerpi:ext4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,12 +14,34 @@ | |
|
|
||
| #define LOCAL_TRACE 0 | ||
|
|
||
| struct dircookie { | ||
| ext2_t *fs; | ||
| struct ext2_inode dir_inode; | ||
| uint fileblock; | ||
| uint cursor; | ||
| }; | ||
|
|
||
| // TODO, move to ext2.c | ||
| static void ext2_dump_inode(struct ext2_inode *inode) { | ||
| #if LOCAL_TRACE | ||
| printf("mode: %d\n", inode->i_mode); | ||
| printf("uid: %d\n", inode->i_uid); | ||
| printf("size: %d\n", inode->i_size); | ||
| printf("blocks: %d\n", inode->i_blocks); | ||
| printf("flags: 0x%x\n", inode->i_flags); | ||
| #endif | ||
| } | ||
|
|
||
| /* read in the dir, look for the entry */ | ||
| static int ext2_dir_lookup(ext2_t *ext2, struct ext2_inode *dir_inode, const char *name, inodenum_t *inum) { | ||
| uint file_blocknum; | ||
| int err; | ||
| uint8_t *buf; | ||
| size_t namelen = strlen(name); | ||
| #if LOCAL_TRACE | ||
| printf("looking for %s in inode %p\n", name, dir_inode); | ||
| ext2_dump_inode(dir_inode); | ||
| #endif | ||
|
|
||
| if (!S_ISDIR(dir_inode->i_mode)) | ||
| return ERR_NOT_DIR; | ||
|
|
@@ -41,8 +63,8 @@ static int ext2_dir_lookup(ext2_t *ext2, struct ext2_inode *dir_inode, const cha | |
| while (pos < EXT2_BLOCK_SIZE(ext2->sb)) { | ||
| ent = (struct ext2_dir_entry_2 *)&buf[pos]; | ||
|
|
||
| LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n", | ||
| file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/); | ||
| LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d, type: %d, name '%s'\n", | ||
| file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len, ent->file_type , ent->name); | ||
|
|
||
| /* sanity check the record length */ | ||
| if (LE16(ent->rec_len) == 0) | ||
|
|
@@ -78,6 +100,7 @@ static int ext2_walk(ext2_t *ext2, char *path, struct ext2_inode *start_inode, i | |
| bool done; | ||
|
|
||
| LTRACEF("path '%s', start_inode %p, inum %p, recurse %d\n", path, start_inode, inum, recurse); | ||
| ext2_dump_inode(start_inode); | ||
|
|
||
| if (recurse > 4) | ||
| return ERR_RECURSE_TOO_DEEP; | ||
|
|
@@ -150,7 +173,7 @@ static int ext2_walk(ext2_t *ext2, char *path, struct ext2_inode *start_inode, i | |
| } else { | ||
| if (!done) { | ||
| /* we aren't done and this walked over a nondir, abort */ | ||
| LTRACEF("not finished and component is nondir\n"); | ||
| LTRACEF("not finished and component is nondir: i_mode: 0x%x\n", inode.i_mode); | ||
| return ERR_NOT_FOUND; | ||
| } | ||
| } | ||
|
|
@@ -178,3 +201,99 @@ int ext2_lookup(ext2_t *ext2, const char *_path, inodenum_t *inum) { | |
| return ext2_walk(ext2, path, &ext2->root_inode, inum, 1); | ||
| } | ||
|
|
||
| status_t ext2_opendir(fscookie *cookie, const char *name, dircookie **dcookie) { | ||
| LTRACEF("cookie %p name '%s' dircookie %p\n", cookie, name, dcookie); | ||
| ext2_t *ext2 = (ext2_t *)cookie; | ||
| int err; | ||
| inodenum_t inum; | ||
| //uint file_blocknum; | ||
| if (name[0] == 0) { | ||
| inum = EXT2_ROOT_INO; | ||
| } else { | ||
| err = ext2_lookup(ext2, name, &inum); | ||
| if (err < 0) { | ||
| LTRACEF("err = %d\n", err); | ||
| return err; | ||
| } | ||
| } | ||
| LTRACEF("inum = %d\n", inum); | ||
|
|
||
| // allocate a dir cookie, point it at the first file, and stuff it in the dircookie jar | ||
| dircookie *dir = calloc(1, sizeof(*dir)); | ||
| if (!dir) return ERR_NO_MEMORY; | ||
| dir->fs = ext2; | ||
| /* load the next inode */ | ||
| err = ext2_load_inode(ext2, inum, &dir->dir_inode); | ||
| if (err < 0) { | ||
| free(dir); | ||
| return err; | ||
| } | ||
|
|
||
| if (!S_ISDIR(dir->dir_inode.i_mode)) { | ||
| free(dir); | ||
| return ERR_NOT_DIR; | ||
cleverca22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| *dcookie = dir; | ||
|
|
||
| return 0; | ||
| } | ||
| status_t ext2_readdir(dircookie *cookie, struct dirent *ent_out) { | ||
| int err; | ||
| uint8_t *buf; | ||
| struct ext2_dir_entry_2 *ent; | ||
| uint pos; | ||
|
|
||
| if (!ent_out) | ||
| return ERR_INVALID_ARGS; | ||
|
|
||
| LTRACEF("dircookie: %p, direnv: %p, %d:%d\n", cookie, ent_out, cookie->fileblock, cookie->cursor); | ||
|
|
||
| if (cookie->fileblock >= cookie->dir_inode.i_blocks) { | ||
| return ERR_NOT_FOUND; | ||
| } | ||
|
|
||
| // TODO, write a function that will get a reference to the block cache | ||
| // then copy and byte-swap the ext2_dir_entry_2 in one action | ||
| buf = malloc(EXT2_BLOCK_SIZE(cookie->fs->sb)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. preferably find algorithm that does't need to malloc a buffer, but in the short term check for failure to malloc and abort. |
||
|
|
||
| err = ext2_read_inode(cookie->fs, &cookie->dir_inode, buf, cookie->fileblock * EXT2_BLOCK_SIZE(cookie->fs->sb), EXT2_BLOCK_SIZE(cookie->fs->sb)); | ||
| if (err <= 0) { | ||
| free(buf); | ||
| return -1; | ||
| } | ||
|
|
||
| pos = cookie->cursor; | ||
| ent = (struct ext2_dir_entry_2 *)&buf[pos]; | ||
|
|
||
| LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n", cookie->fileblock, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/); | ||
|
|
||
| /* sanity check the record length */ | ||
| if (LE16(ent->rec_len) == 0) { | ||
| free(buf); | ||
| return -1; | ||
| } | ||
|
|
||
| pos += ROUNDUP(LE16(ent->rec_len), 4); | ||
| if (ent->inode == 0) { // deleted file | ||
| // TODO, assumes end of dir listing | ||
| // it should continue to the next record, potentially in the next block | ||
| free(buf); | ||
| return ERR_NOT_FOUND; | ||
| } | ||
| if (pos >= EXT2_BLOCK_SIZE(cookie->fs->sb)) { | ||
| cookie->fileblock++; | ||
| cookie->cursor = 0; | ||
| } else { | ||
| cookie->cursor = pos; | ||
| } | ||
| int size = MIN(ent->name_len, FS_MAX_FILE_LEN-1); | ||
| memcpy(ent_out->name, ent->name, size); | ||
| ent_out->name[size] = 0; | ||
| free(buf); | ||
| return NO_ERROR; | ||
| } | ||
| status_t ext2_closedir(dircookie *cookie) { | ||
| free(cookie); | ||
| return NO_ERROR; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.