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
19class PackagesDirectory : public BReferenceable,
20	public DoublyLinkedListLinkImpl<PackagesDirectory> {
21public:
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
51private:
52			status_t			_Init(struct vnode* vnode, struct stat& _st);
53
54private:
55			String				fStateName;
56			char*				fPath;
57			int					fDirFD;
58			node_ref			fNodeRef;
59			PackagesDirectory*	fHashNext;
60};
61
62
63struct 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
89typedef BOpenHashTable<PackagesDirectoryHashDefinition>
90	PackagesDirectoryHashTable;
91typedef DoublyLinkedList<PackagesDirectory> PackagesDirectoryList;
92
93
94#endif	// PACKAGES_DIRECTORY_H
95