1/*
2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "Directory.h"
8
9#include "DebugSupport.h"
10#include "UnpackingAttributeCookie.h"
11#include "UnpackingAttributeDirectoryCookie.h"
12#include "Utils.h"
13
14
15Directory::Directory(ino_t id)
16	:
17	Node(id)
18{
19}
20
21
22Directory::~Directory()
23{
24	Node* child = fChildTable.Clear(true);
25	while (child != NULL) {
26		Node* next = child->NameHashTableNext();
27		child->ReleaseReference();
28		child = next;
29	}
30}
31
32
33status_t
34Directory::Init(Directory* parent, const String& name)
35{
36	status_t error = Node::Init(parent, name);
37	if (error != B_OK)
38		return error;
39
40	return fChildTable.Init();
41}
42
43
44mode_t
45Directory::Mode() const
46{
47	return S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
48}
49
50
51off_t
52Directory::FileSize() const
53{
54	return 0;
55}
56
57
58status_t
59Directory::Read(off_t offset, void* buffer, size_t* bufferSize)
60{
61	return B_IS_A_DIRECTORY;
62}
63
64
65status_t
66Directory::Read(io_request* request)
67{
68	return B_IS_A_DIRECTORY;
69}
70
71
72status_t
73Directory::ReadSymlink(void* buffer, size_t* bufferSize)
74{
75	return B_IS_A_DIRECTORY;
76}
77
78
79void
80Directory::AddChild(Node* node)
81{
82	fChildTable.Insert(node);
83	fChildList.Add(node);
84	node->AcquireReference();
85}
86
87
88void
89Directory::RemoveChild(Node* node)
90{
91	Node* nextNode = fChildList.GetNext(node);
92
93	fChildTable.Remove(node);
94	fChildList.Remove(node);
95	node->ReleaseReference();
96
97	// adjust directory iterators pointing to the removed child
98	for (DirectoryIteratorList::Iterator it = fIterators.GetIterator();
99			DirectoryIterator* iterator = it.Next();) {
100		if (iterator->node == node)
101			iterator->node = nextNode;
102	}
103}
104
105
106Node*
107Directory::FindChild(const StringKey& name)
108{
109	return fChildTable.Lookup(name);
110}
111
112
113void
114Directory::AddDirectoryIterator(DirectoryIterator* iterator)
115{
116	fIterators.Add(iterator);
117}
118
119
120void
121Directory::RemoveDirectoryIterator(DirectoryIterator* iterator)
122{
123	fIterators.Remove(iterator);
124}
125