xref: /haiku/src/add-ons/kernel/file_systems/packagefs/package/PackageNode.cpp (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
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 "DebugSupport.h"
13 #include "Package.h"
14 #include "Utils.h"
15 
16 
17 PackageNode::PackageNode(Package* package, mode_t mode)
18 	:
19 	fPackage(package),
20 	fParent(NULL),
21 	fName(),
22 	fMode(mode),
23 	fUserID(0),
24 	fGroupID(0)
25 {
26 }
27 
28 
29 PackageNode::~PackageNode()
30 {
31 	while (PackageNodeAttribute* attribute = fAttributes.RemoveHead())
32 		delete attribute;
33 }
34 
35 
36 BReference<Package>
37 PackageNode::GetPackage() const
38 {
39 	return fPackage.GetReference();
40 }
41 
42 
43 status_t
44 PackageNode::Init(PackageDirectory* parent, const String& name)
45 {
46 	fParent = parent;
47 	fName = name;
48 	return B_OK;
49 }
50 
51 
52 status_t
53 PackageNode::VFSInit(dev_t deviceID, ino_t nodeID)
54 {
55 	BReference<Package> package(GetPackage());
56 
57 	// open the package
58 	int fd = package->Open();
59 	if (fd < 0)
60 		RETURN_ERROR(fd);
61 
62 	package->AcquireReference();
63 	return B_OK;
64 }
65 
66 
67 void
68 PackageNode::VFSUninit()
69 {
70 	BReference<Package> package(GetPackage());
71 	package->Close();
72 	package->ReleaseReference();
73 }
74 
75 
76 off_t
77 PackageNode::FileSize() const
78 {
79 	return 0;
80 }
81 
82 
83 void
84 PackageNode::AddAttribute(PackageNodeAttribute* attribute)
85 {
86 	fAttributes.Add(attribute);
87 }
88 
89 
90 void
91 PackageNode::RemoveAttribute(PackageNodeAttribute* attribute)
92 {
93 	fAttributes.Remove(attribute);
94 }
95 
96 
97 PackageNodeAttribute*
98 PackageNode::FindAttribute(const StringKey& name) const
99 {
100 	for (PackageNodeAttributeList::ConstIterator it = fAttributes.GetIterator();
101 			PackageNodeAttribute* attribute = it.Next();) {
102 		if (name == attribute->Name())
103 			return attribute;
104 	}
105 
106 	return NULL;
107 }
108 
109 
110 void
111 PackageNode::UnsetIndexCookie(void* attributeCookie)
112 {
113 	((PackageNodeAttribute*)attributeCookie)->SetIndexCookie(NULL);
114 }
115 
116 
117 bool
118 PackageNode::HasPrecedenceOver(const PackageNode* other) const
119 {
120 	uint32 packageFlags = 0, otherPackageFlags = 0;
121 	BReference<Package> package(GetPackage()), otherPackage(other->GetPackage());
122 	if (package)
123 		packageFlags = package->Flags();
124 	if (otherPackage)
125 		otherPackageFlags = otherPackage->Flags();
126 
127 	const bool isSystemPkg = (packageFlags
128 			& BPackageKit::B_PACKAGE_FLAG_SYSTEM_PACKAGE) != 0,
129 		otherIsSystemPkg = (otherPackageFlags
130 			& BPackageKit::B_PACKAGE_FLAG_SYSTEM_PACKAGE) != 0;
131 	if (isSystemPkg && !otherIsSystemPkg)
132 		return true;
133 	if (!isSystemPkg && otherIsSystemPkg)
134 		return false;
135 	return fModifiedTime > other->fModifiedTime;
136 }
137