1/*
2 * Copyright 2007, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * All rights reserved. Distributed under the terms of the MIT license.
4 */
5
6#include "AllocationInfo.h"
7#include "DebugSupport.h"
8#include "Entry.h"
9#include "EntryIterator.h"
10#include "Node.h"
11#include "Volume.h"
12
13// constructor
14Entry::Entry(const char *name, Node *node, Directory *parent)
15	: fParent(parent),
16	  fNode(NULL),
17	  fName(name),
18	  fReferrerLink(),
19	  fIterators()
20{
21	if (node)
22		Link(node);
23}
24
25// destructor
26Entry::~Entry()
27{
28	if (fNode)
29		Unlink();
30}
31
32// InitCheck
33status_t
34Entry::InitCheck() const
35{
36	return (fName.GetString() ? B_OK : B_NO_INIT);
37}
38
39// Link
40status_t
41Entry::Link(Node *node)
42{
43	if (node == NULL)
44		return B_BAD_VALUE;
45	if (node == fNode)
46		return B_OK;
47
48	// We first link to the new node and then unlink the old one.
49	Node *oldNode = fNode;
50	status_t error = node->Link(this);
51	if (error == B_OK) {
52		fNode = node;
53		if (oldNode)
54			oldNode->Unlink(this);
55	}
56	return error;
57}
58
59// Unlink
60status_t
61Entry::Unlink()
62{
63	status_t error = (fNode ? B_OK : B_BAD_VALUE);
64	if (error == B_OK && (error = fNode->Unlink(this)) == B_OK)
65		fNode = NULL;
66	return error;
67}
68
69// SetName
70status_t
71Entry::SetName(const char *newName)
72{
73	status_t error = (newName ? B_OK : B_BAD_VALUE);
74	if (error == B_OK) {
75		if (!fName.SetTo(newName))
76			SET_ERROR(error, B_NO_MEMORY);
77	}
78	return error;
79}
80
81// AttachEntryIterator
82void
83Entry::AttachEntryIterator(EntryIterator *iterator)
84{
85	if (iterator && iterator->GetCurrent() == this && !iterator->IsSuspended())
86		fIterators.Insert(iterator);
87}
88
89// DetachEntryIterator
90void
91Entry::DetachEntryIterator(EntryIterator *iterator)
92{
93	if (iterator && iterator->GetCurrent() == this && iterator->IsSuspended())
94		fIterators.Remove(iterator);
95}
96
97// GetAllocationInfo
98void
99Entry::GetAllocationInfo(AllocationInfo &info)
100{
101	info.AddEntryAllocation();
102	info.AddStringAllocation(fName.GetLength());
103	fNode->GetAllocationInfo(info);
104}
105