xref: /haiku/src/add-ons/kernel/file_systems/packagefs/volume/PackagesDirectory.h (revision 13581b3d2a71545960b98fefebc5225b5bf29072)
1 /*
2  * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef PACKAGES_DIRECTORY_H
6 #define PACKAGES_DIRECTORY_H
7 
8 
9 #include <sys/stat.h>
10 
11 #include <Referenceable.h>
12 #include <util/DoublyLinkedList.h>
13 #include <util/OpenHashTable.h>
14 
15 #include "NodeRef.h"
16 #include "String.h"
17 
18 
19 class PackagesDirectory : public BReferenceable,
20 	public DoublyLinkedListLinkImpl<PackagesDirectory> {
21 public:
22 								PackagesDirectory();
23 								~PackagesDirectory();
24 
25 			const String&		StateName() const
26 									{ return fStateName; }
27 									// empty for the latest state
28 			const char*			Path() const
29 									{ return fPath; }
30 			int					DirectoryFD() const
31 									{ return fDirFD; }
32 			const node_ref&		NodeRef() const
33 									{ return fNodeRef; }
34 			dev_t				DeviceID() const
35 									{ return fNodeRef.device; }
36 			ino_t				NodeID() const
37 									{ return fNodeRef.node; }
38 
39 			status_t			Init(const char* path, dev_t mountPointDeviceID,
40 									ino_t mountPointNodeID, struct stat& _st);
41 			status_t			InitOldState(dev_t adminDirDeviceID,
42 									ino_t adminDirNodeID,
43 									const char* stateName);
44 
45 	static	bool				IsNewer(const PackagesDirectory* a,
46 									const PackagesDirectory* b);
47 
48 			PackagesDirectory*&	HashTableNext()
49 									{ return fHashNext; }
50 
51 private:
52 			status_t			_Init(struct vnode* vnode, struct stat& _st);
53 
54 private:
55 			String				fStateName;
56 			char*				fPath;
57 			int					fDirFD;
58 			node_ref			fNodeRef;
59 			PackagesDirectory*	fHashNext;
60 };
61 
62 
63 struct PackagesDirectoryHashDefinition {
64 	typedef const node_ref&		KeyType;
65 	typedef	PackagesDirectory	ValueType;
66 
67 	size_t HashKey(const node_ref& key) const
68 	{
69 		return (size_t)key.device ^ (size_t)key.node;
70 	}
71 
72 	size_t Hash(const PackagesDirectory* value) const
73 	{
74 		return HashKey(value->NodeRef());
75 	}
76 
77 	bool Compare(const node_ref& key, const PackagesDirectory* value) const
78 	{
79 		return key == value->NodeRef();
80 	}
81 
82 	PackagesDirectory*& GetLink(PackagesDirectory* value) const
83 	{
84 		return value->HashTableNext();
85 	}
86 };
87 
88 
89 typedef BOpenHashTable<PackagesDirectoryHashDefinition>
90 	PackagesDirectoryHashTable;
91 typedef DoublyLinkedList<PackagesDirectory> PackagesDirectoryList;
92 
93 
94 #endif	// PACKAGES_DIRECTORY_H
95