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
17PackageNode::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
29PackageNode::~PackageNode()
30{
31	while (PackageNodeAttribute* attribute = fAttributes.RemoveHead())
32		delete attribute;
33}
34
35
36BReference<Package>
37PackageNode::GetPackage() const
38{
39	return fPackage.GetReference();
40}
41
42
43status_t
44PackageNode::Init(PackageDirectory* parent, const String& name)
45{
46	fParent = parent;
47	fName = name;
48	return B_OK;
49}
50
51
52status_t
53PackageNode::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
67void
68PackageNode::VFSUninit()
69{
70	BReference<Package> package(GetPackage());
71	package->Close();
72	package->ReleaseReference();
73}
74
75
76off_t
77PackageNode::FileSize() const
78{
79	return 0;
80}
81
82
83void
84PackageNode::AddAttribute(PackageNodeAttribute* attribute)
85{
86	fAttributes.Add(attribute);
87}
88
89
90void
91PackageNode::RemoveAttribute(PackageNodeAttribute* attribute)
92{
93	fAttributes.Remove(attribute);
94}
95
96
97PackageNodeAttribute*
98PackageNode::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
110void
111PackageNode::UnsetIndexCookie(void* attributeCookie)
112{
113	((PackageNodeAttribute*)attributeCookie)->SetIndexCookie(NULL);
114}
115
116
117bool
118PackageNode::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