1/*
2 * Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
8#ifndef PACKAGE_FILE_H
9#define PACKAGE_FILE_H
10
11
12#include <Node.h>
13#include <package/PackageInfo.h>
14
15#include <NotOwningEntryRef.h>
16#include <Referenceable.h>
17#include <util/OpenHashTable.h>
18
19
20using namespace BPackageKit;
21
22
23class PackageFileManager;
24
25
26class PackageFile : public BReferenceable {
27public:
28								PackageFile();
29								~PackageFile();
30
31			status_t			Init(const entry_ref& entryRef,
32									PackageFileManager* owner);
33
34			const node_ref&		NodeRef() const
35									{ return fNodeRef; }
36			const BString&		FileName() const
37									{ return fFileName; }
38
39			const node_ref&		DirectoryRef() const
40									{ return fDirectoryRef; }
41			void				SetDirectoryRef(const node_ref& directoryRef)
42									{ fDirectoryRef = directoryRef; }
43
44			NotOwningEntryRef	EntryRef() const;
45
46			const BPackageInfo & Info() const
47									{ return fInfo; }
48
49			BString				RevisionedName() const;
50			BString				RevisionedNameThrows() const;
51
52			int32				EntryCreatedIgnoreLevel() const
53									{ return fIgnoreEntryCreated; }
54			void				IncrementEntryCreatedIgnoreLevel()
55									{ fIgnoreEntryCreated++; }
56			void				DecrementEntryCreatedIgnoreLevel()
57									{ fIgnoreEntryCreated--; }
58
59			int32				EntryRemovedIgnoreLevel() const
60									{ return fIgnoreEntryRemoved; }
61			void				IncrementEntryRemovedIgnoreLevel()
62									{ fIgnoreEntryRemoved++; }
63			void				DecrementEntryRemovedIgnoreLevel()
64									{ fIgnoreEntryRemoved--; }
65
66			PackageFile*&		EntryRefHashTableNext()
67									{ return fEntryRefHashTableNext; }
68
69protected:
70	virtual	void				LastReferenceReleased();
71
72private:
73			node_ref			fNodeRef;
74			node_ref			fDirectoryRef;
75			BString				fFileName;
76			BPackageInfo		fInfo;
77			PackageFile*		fEntryRefHashTableNext;
78			PackageFileManager*	fOwner;
79			int32				fIgnoreEntryCreated;
80			int32				fIgnoreEntryRemoved;
81};
82
83
84inline NotOwningEntryRef
85PackageFile::EntryRef() const
86{
87	return NotOwningEntryRef(fDirectoryRef, fFileName);
88}
89
90
91struct PackageFileEntryRefHashDefinition {
92	typedef entry_ref		KeyType;
93	typedef	PackageFile		ValueType;
94
95	size_t HashKey(const entry_ref& key) const
96	{
97		size_t hash = BString::HashValue(key.name);
98		hash ^= (size_t)key.device;
99		hash ^= (size_t)key.directory;
100		return hash;
101	}
102
103	size_t Hash(const PackageFile* value) const
104	{
105		return HashKey(value->EntryRef());
106	}
107
108	bool Compare(const entry_ref& key, const PackageFile* value) const
109	{
110		return value->EntryRef() == key;
111	}
112
113	PackageFile*& GetLink(PackageFile* value) const
114	{
115		return value->EntryRefHashTableNext();
116	}
117};
118
119
120typedef BOpenHashTable<PackageFileEntryRefHashDefinition>
121	PackageFileEntryRefHashTable;
122
123
124#endif	// PACKAGE_FILE_H
125