1/*
2 * Copyright 2022, Raghav Sharma, raghavself28@gmail.com
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "Attribute.h"
8
9#include "LeafAttribute.h"
10#include "NodeAttribute.h"
11#include "ShortAttribute.h"
12
13
14Attribute::~Attribute()
15{
16}
17
18
19Attribute*
20Attribute::Init(Inode* inode)
21{
22	if (inode->AttrFormat() == XFS_DINODE_FMT_LOCAL) {
23		TRACE("Attribute:Init: LOCAL\n");
24		ShortAttribute* shortAttr = new(std::nothrow) ShortAttribute(inode);
25		return shortAttr;
26	}
27	if (inode->AttrFormat() == XFS_DINODE_FMT_EXTENTS) {
28		TRACE("Attribute::Init: EXTENTS\n");
29		// check if Inode has extents or not?
30		if (inode->AttrExtentsCount() == 0)
31			return NULL;
32
33		LeafAttribute* leafAttr = new(std::nothrow) LeafAttribute(inode);
34		if (leafAttr == NULL)
35			return NULL;
36
37		status_t status = leafAttr->Init();
38
39		if (status == B_OK)
40			return leafAttr;
41		delete leafAttr;
42
43		NodeAttribute* nodeAttr = new(std::nothrow) NodeAttribute(inode);
44		if (nodeAttr == NULL)
45			return NULL;
46
47		status = nodeAttr->Init();
48
49		if (status == B_OK)
50			return nodeAttr;
51		delete nodeAttr;
52
53		// Invalid format in extents return NULL
54		return NULL;
55	}
56
57	// Invalid format
58	return NULL;
59}