BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / commit / 2d05318728e0aa3fca502a2aeb1738773fffc98b / vfs / ext2fs / file.c

// Related

Nucleus

Barry Directory listing in VFS 2d05318 (3 years, 2 months ago)
diff --git a/vfs/ext2fs/file.c b/vfs/ext2fs/file.c
index d19df72..a84da51 100644
--- a/vfs/ext2fs/file.c
+++ b/vfs/ext2fs/file.c
@@ -1,14 +1,15 @@
 #include <string.h>
+#include <errno.h>
 #include <nucleus/vfs.h>
 #include "ext2fs.h"
 
 size_t ext2fs_read(File *file, char *buf, size_t size, off_t offset);
-int ext2fs_readdir(File *file, DirEntry *de, off_t index);
+int ext2fs_readdir(File *file, struct dirent *dent, off_t index);
 int ext2fs_open(File *file);
 
 FileOps ext2fsFileOps = {
 	.read = ext2fs_read,
-//	.readdir = ext2fs_readdir,
+	.readdir = ext2fs_readdir,
 	.open = ext2fs_open,
 };
 
@@ -42,8 +43,33 @@ ext2fs_read(File *file, char *buf, size_t size, off_t offset)
 
 /* Read an ext2fs directory entry */
 int
-ext2fs_readdir(File *file, DirEntry *de, off_t index)
+ext2fs_readdir(File *file, struct dirent *dent, off_t index)
 {
+	char buf[PAGE_SIZE];
+	uint32_t block, blk = 0;
+	struct Ext2DirEntry *de;
+	struct Ext2Inode inode;
+	SuperBlock *sb = file->inode->super;
+	ext2fs_read_inode(sb, file->inode->ino, &inode);
+	for (blk = 0; blk < 0xFFFF; blk++) {
+		block = ext2fs_get_data_addr(sb, &inode, blk);
+		if (!block)
+			return -ENOENT;
+		ext2fs_read_block(sb, block, buf);
+		for (de = (struct Ext2DirEntry *) buf;
+		     index && de < (struct Ext2DirEntry *) (buf + PAGE_SIZE);
+		     de = (void *) de + de->size, index--);
+		if (de >= (struct Ext2DirEntry *) (buf + PAGE_SIZE))
+			return -ENOENT;
+		if (!index)
+			break;
+	}
+	if (!de->ino)
+		return -ENOENT;
+	dent->d_ino = de->ino;
+	dent->d_type = de->type;
+	dent->d_namelen = de->namelen + 1;
+	strncpy(dent->d_name, de->name, de->size);
 	return 0;
 }