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 "BPlusTree.h"
8#include "Directory.h"
9#include "Extent.h"
10#include "LeafDirectory.h"
11#include "Node.h"
12#include "ShortDirectory.h"
13
14
15DirectoryIterator::~DirectoryIterator()
16{
17}
18
19
20DirectoryIterator*
21DirectoryIterator::Init(Inode* inode)
22{
23	if (inode->Format() == XFS_DINODE_FMT_LOCAL) {
24		TRACE("Iterator:Init: LOCAL");
25		ShortDirectory* shortDir = new(std::nothrow) ShortDirectory(inode);
26		return shortDir;
27	}
28
29	if (inode->Format() == XFS_DINODE_FMT_EXTENTS) {
30		TRACE("Iterator:Init: EXTENTS");
31		status_t status;
32
33		// Check if it is extent based directory
34		Extent* extentDir = new(std::nothrow) Extent(inode);
35		if (extentDir == NULL)
36			return NULL;
37
38		if (extentDir->IsBlockType()) {
39			status = extentDir->Init();
40			if (status == B_OK)
41				return extentDir;
42		}
43
44		delete extentDir;
45
46		// Check if it is leaf based directory
47		LeafDirectory* leafDir = new(std::nothrow) LeafDirectory(inode);
48		if (leafDir == NULL)
49			return NULL;
50
51		if (leafDir->IsLeafType()) {
52			status = leafDir->Init();
53			if (status == B_OK)
54				return leafDir;
55		}
56
57		delete leafDir;
58
59		// Check if it is node based directory
60		NodeDirectory* nodeDir = new(std::nothrow) NodeDirectory(inode);
61		if (nodeDir == NULL)
62			return NULL;
63
64		if (nodeDir->IsNodeType()) {
65			status = nodeDir->Init();
66			if (status == B_OK)
67				return nodeDir;
68		}
69
70		delete nodeDir;
71	}
72
73	if (inode->Format() == XFS_DINODE_FMT_BTREE) {
74		TRACE("Iterator:Init(): B+TREE");
75		TreeDirectory* treeDir = new(std::nothrow) TreeDirectory(inode);
76		if (treeDir == NULL)
77			return NULL;
78
79		status_t status = treeDir->InitCheck();
80
81		if (status == B_OK)
82			return treeDir;
83	}
84
85	// Invalid format return NULL
86	return NULL;
87}