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 StateName()25 const String& StateName() const 26 { return fStateName; } 27 // empty for the latest state Path()28 const char* Path() const 29 { return fPath; } DirectoryFD()30 int DirectoryFD() const 31 { return fDirFD; } NodeRef()32 const node_ref& NodeRef() const 33 { return fNodeRef; } DeviceID()34 dev_t DeviceID() const 35 { return fNodeRef.device; } NodeID()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 HashTableNext()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 HashKeyPackagesDirectoryHashDefinition67 size_t HashKey(const node_ref& key) const 68 { 69 return (size_t)key.device ^ (size_t)key.node; 70 } 71 HashPackagesDirectoryHashDefinition72 size_t Hash(const PackagesDirectory* value) const 73 { 74 return HashKey(value->NodeRef()); 75 } 76 ComparePackagesDirectoryHashDefinition77 bool Compare(const node_ref& key, const PackagesDirectory* value) const 78 { 79 return key == value->NodeRef(); 80 } 81 GetLinkPackagesDirectoryHashDefinition82 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