xref: /haiku/src/add-ons/kernel/file_systems/packagefs/package/PackageNode.cpp (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
1 /*
2  * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "PackageNode.h"
8 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <time_private.h>
13 
14 #include "DebugSupport.h"
15 #include "Package.h"
16 #include "Utils.h"
17 
18 
19 DEFINE_INLINE_REFERENCEABLE_METHODS(PackageNode, fReferenceable);
20 
21 
22 PackageNode::PackageNode(Package* package, mode_t mode)
23 	:
24 	fPackage(package),
25 	fParent(NULL),
26 	fName(),
27 	fMode(mode)
28 {
29 }
30 
31 
32 PackageNode::~PackageNode()
33 {
34 	while (PackageNodeAttribute* attribute = fAttributes.RemoveHead())
35 		delete attribute;
36 }
37 
38 
39 BReference<Package>
40 PackageNode::GetPackage() const
41 {
42 	return fPackage.GetReference();
43 }
44 
45 
46 status_t
47 PackageNode::Init(PackageDirectory* parent, const String& name)
48 {
49 	fParent = parent;
50 	fName = name;
51 	return B_OK;
52 }
53 
54 
55 status_t
56 PackageNode::VFSInit(dev_t deviceID, ino_t nodeID)
57 {
58 	BReference<Package> package(GetPackage());
59 
60 	// open the package
61 	int fd = package->Open();
62 	if (fd < 0)
63 		RETURN_ERROR(fd);
64 
65 	package->AcquireReference();
66 	return B_OK;
67 }
68 
69 
70 void
71 PackageNode::VFSUninit()
72 {
73 	BReference<Package> package(GetPackage());
74 	package->Close();
75 	package->ReleaseReference();
76 }
77 
78 
79 void
80 PackageNode::SetModifiedTime(const timespec& time)
81 {
82 	if (!timespec_to_bigtime(time, fModifiedTime))
83 		fModifiedTime = 0;
84 }
85 
86 
87 timespec
88 PackageNode::ModifiedTime() const
89 {
90 	timespec modifiedTime;
91 	bigtime_to_timespec(fModifiedTime, modifiedTime);
92 	return modifiedTime;
93 }
94 
95 
96 off_t
97 PackageNode::FileSize() const
98 {
99 	return 0;
100 }
101 
102 
103 void
104 PackageNode::AddAttribute(PackageNodeAttribute* attribute)
105 {
106 	fAttributes.Add(attribute);
107 }
108 
109 
110 PackageNodeAttribute*
111 PackageNode::FindAttribute(const StringKey& name) const
112 {
113 	for (PackageNodeAttributeList::ConstIterator it = fAttributes.GetIterator();
114 			PackageNodeAttribute* attribute = it.Next();) {
115 		if (name == attribute->Name())
116 			return attribute;
117 	}
118 
119 	return NULL;
120 }
121 
122 
123 void
124 PackageNode::UnsetIndexCookie(void* attributeCookie)
125 {
126 	((PackageNodeAttribute*)attributeCookie)->SetIndexCookie(NULL);
127 }
128 
129 
130 bool
131 PackageNode::HasPrecedenceOver(const PackageNode* other) const
132 {
133 	uint32 packageFlags = 0, otherPackageFlags = 0;
134 	BReference<Package> package(GetPackage()), otherPackage(other->GetPackage());
135 	if (package)
136 		packageFlags = package->Flags();
137 	if (otherPackage)
138 		otherPackageFlags = otherPackage->Flags();
139 
140 	const bool isSystemPkg = (packageFlags
141 			& BPackageKit::B_PACKAGE_FLAG_SYSTEM_PACKAGE) != 0,
142 		otherIsSystemPkg = (otherPackageFlags
143 			& BPackageKit::B_PACKAGE_FLAG_SYSTEM_PACKAGE) != 0;
144 	if (isSystemPkg && !otherIsSystemPkg)
145 		return true;
146 	if (!isSystemPkg && otherIsSystemPkg)
147 		return false;
148 	return ModifiedTime() > other->ModifiedTime();
149 }
150