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