xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Entry.cpp (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
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
11 Entry::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
23 Entry::~Entry()
24 {
25 	if (fNode)
26 		Unlink();
27 }
28 
29 // InitCheck
30 status_t
31 Entry::InitCheck() const
32 {
33 	return (fName.GetString() ? B_OK : B_NO_INIT);
34 }
35 
36 // Link
37 status_t
38 Entry::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
56 status_t
57 Entry::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
66 status_t
67 Entry::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 			SET_ERROR(error, B_NO_MEMORY);
73 	}
74 	return error;
75 }
76 
77 // AttachEntryIterator
78 void
79 Entry::AttachEntryIterator(EntryIterator *iterator)
80 {
81 	if (iterator && iterator->GetCurrent() == this && !iterator->IsSuspended())
82 		fIterators.Insert(iterator);
83 }
84 
85 // DetachEntryIterator
86 void
87 Entry::DetachEntryIterator(EntryIterator *iterator)
88 {
89 	if (iterator && iterator->GetCurrent() == this && iterator->IsSuspended())
90 		fIterators.Remove(iterator);
91 }
92 
93 // GetAllocationInfo
94 void
95 Entry::GetAllocationInfo(AllocationInfo &info)
96 {
97 	info.AddEntryAllocation();
98 	info.AddStringAllocation(fName.GetLength());
99 	fNode->GetAllocationInfo(info);
100 }
101 
102