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