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