xref: /haiku/src/add-ons/kernel/file_systems/xfs/Directory.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2020, Shubham Bhagat, shubhambhagat111@yahoo.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "Directory.h"
8 
9 
10 DirectoryIterator::DirectoryIterator(Inode* inode)
11 {
12 	fInode = inode;
13 	fShortDir = NULL;
14 }
15 
16 
17 DirectoryIterator::~DirectoryIterator()
18 {
19 	delete fShortDir;
20 }
21 
22 
23 status_t
24 DirectoryIterator::Init()
25 {
26 	if (fInode->Format() == XFS_DINODE_FMT_LOCAL)
27 	{
28 		fShortDir = new(std::nothrow) ShortDirectory(fInode);
29 		if (fShortDir == NULL)
30 			return B_NO_MEMORY;
31 		return B_OK;
32 	}
33 	if (fInode->Format() == XFS_DINODE_FMT_EXTENTS) {
34 		// TODO: Only working with Block directories, not leaf.
35 		fExtentDir = new(std::nothrow) Extent(fInode);
36 		if (fExtentDir == NULL)
37 			return B_NO_MEMORY;
38 		status_t status = fExtentDir->Init();
39 		return status;
40 	}
41 
42 	/* Return B_OK so even if the shortform directory has an extent directory
43 	 * we can atleast still list the shortform directory
44 	 */
45 
46 	//TODO: Reading from extent based directories
47 	if (fInode->Format() == XFS_DINODE_FMT_EXTENTS) {
48 		TRACE("Iterator:GetNext: EXTENTS");
49 		return B_OK;
50 	}
51 
52 	//TODO: Reading from B+Trees based directories
53 	if (fInode->Format() == XFS_DINODE_FMT_BTREE) {
54 		TRACE("Iterator:GetNext: B+TREE");
55 		return B_OK;
56 	}
57 
58 	return B_BAD_VALUE;
59 }
60 
61 
62 status_t
63 DirectoryIterator::GetNext(char* name, size_t* length, xfs_ino_t* ino)
64 {
65 	if (fInode->Format() == XFS_DINODE_FMT_LOCAL) {
66 		status_t status = fShortDir->GetNext(name, length, ino);
67 		return status;
68 	}
69 
70 	//TODO: Reading from extent based directories
71 	if (fInode->Format() == XFS_DINODE_FMT_EXTENTS) {
72 		TRACE("Iterator:GetNext: EXTENTS");
73 		return B_NOT_SUPPORTED;
74 	}
75 
76 	//TODO: Reading from B+Trees based directories
77 	if (fInode->Format() == XFS_DINODE_FMT_BTREE) {
78 		TRACE("Iterator:GetNext: B+TREE");
79 		return B_NOT_SUPPORTED;
80 	}
81 
82 	// Only reaches here if Inode is a device or is corrupt.
83 	return B_BAD_VALUE;
84 }
85 
86 
87 status_t
88 DirectoryIterator::Lookup(const char* name, size_t length, xfs_ino_t* ino)
89 {
90 	if (fInode->Format() == XFS_DINODE_FMT_LOCAL) {
91 		status_t status = fShortDir->Lookup(name, length, ino);
92 		return status;
93 	}
94 
95 	//TODO: Reading from extent based dirs
96 	if (fInode->Format() == XFS_DINODE_FMT_EXTENTS) {
97 		TRACE("Iterator:Lookup: EXTENTS");
98 		#if 0
99 			status_t status = fExtentDir->Lookup(name, length, ino);
100 			return status;
101 		#endif
102 		return B_NOT_SUPPORTED;
103 	}
104 
105 	//TODO: Reading from B+Tree based dirs
106 	if (fInode->Format() == XFS_DINODE_FMT_BTREE) {
107 		TRACE("Iterator:Lookup: B+TREE");
108 		return B_NOT_SUPPORTED;
109 	}
110 
111 	return B_BAD_VALUE;
112 }
113