1 /* 2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef PACKAGE_H 6 #define PACKAGE_H 7 8 9 #include <package/hpkg/DataReader.h> 10 #include <package/PackageArchitecture.h> 11 12 #include <Referenceable.h> 13 14 #include <util/DoublyLinkedList.h> 15 #include <util/khash.h> 16 #include <util/OpenHashTable.h> 17 18 #include <lock.h> 19 20 #include "Dependency.h" 21 #include "PackageNode.h" 22 #include "Resolvable.h" 23 #include "String.h" 24 25 26 using BPackageKit::BPackageArchitecture; 27 using BPackageKit::BHPKG::BAbstractBufferedDataReader; 28 29 30 class PackageLinkDirectory; 31 class Volume; 32 class Version; 33 34 35 class Package : public BReferenceable, 36 public DoublyLinkedListLinkImpl<Package> { 37 public: 38 Package(::Volume* volume, dev_t deviceID, 39 ino_t nodeID); 40 ~Package(); 41 42 status_t Init(const char* fileName); 43 status_t Load(); 44 45 ::Volume* Volume() const { return fVolume; } 46 const String& FileName() const { return fFileName; } 47 48 void SetName(const String& name); 49 const String& Name() const { return fName; } 50 51 dev_t DeviceID() const 52 { return fDeviceID; } 53 ino_t NodeID() const 54 { return fNodeID; } 55 56 void SetInstallPath(const String& installPath); 57 const String& InstallPath() const { return fInstallPath; } 58 59 void SetVersion(::Version* version); 60 // takes over object ownership 61 ::Version* Version() const 62 { return fVersion; } 63 64 void SetArchitecture( 65 BPackageArchitecture architecture) 66 { fArchitecture = architecture; } 67 BPackageArchitecture Architecture() const 68 { return fArchitecture; } 69 const char* ArchitectureName() const; 70 71 void SetLinkDirectory( 72 PackageLinkDirectory* linkDirectory) 73 { fLinkDirectory = linkDirectory; } 74 PackageLinkDirectory* LinkDirectory() const 75 { return fLinkDirectory; } 76 77 Package*& FileNameHashTableNext() 78 { return fFileNameHashTableNext; } 79 80 void AddNode(PackageNode* node); 81 void AddResolvable(Resolvable* resolvable); 82 void AddDependency(Dependency* dependency); 83 84 int Open(); 85 void Close(); 86 87 status_t CreateDataReader(const PackageData& data, 88 BAbstractBufferedDataReader*& _reader); 89 90 const PackageNodeList& Nodes() const { return fNodes; } 91 const ResolvableList& Resolvables() const 92 { return fResolvables; } 93 const DependencyList& Dependencies() const 94 { return fDependencies; } 95 96 private: 97 struct LoaderErrorOutput; 98 struct LoaderContentHandler; 99 struct LoaderContentHandlerV1; 100 struct HeapReader; 101 struct HeapReaderV1; 102 struct HeapReaderV2; 103 struct CachingPackageReader; 104 105 private: 106 mutex fLock; 107 ::Volume* fVolume; 108 String fFileName; 109 String fName; 110 String fInstallPath; 111 ::Version* fVersion; 112 BPackageArchitecture fArchitecture; 113 PackageLinkDirectory* fLinkDirectory; 114 int fFD; 115 uint32 fOpenCount; 116 HeapReader* fHeapReader; 117 Package* fFileNameHashTableNext; 118 ino_t fNodeID; 119 dev_t fDeviceID; 120 PackageNodeList fNodes; 121 ResolvableList fResolvables; 122 DependencyList fDependencies; 123 }; 124 125 126 struct PackageCloser { 127 PackageCloser(Package* package) 128 : 129 fPackage(package) 130 { 131 } 132 133 ~PackageCloser() 134 { 135 if (fPackage != NULL) 136 fPackage->Close(); 137 } 138 139 void Detach() 140 { 141 fPackage = NULL; 142 } 143 144 private: 145 Package* fPackage; 146 }; 147 148 149 struct PackageFileNameHashDefinition { 150 typedef const char* KeyType; 151 typedef Package ValueType; 152 153 size_t HashKey(const char* key) const 154 { 155 return hash_hash_string(key); 156 } 157 158 size_t Hash(const Package* value) const 159 { 160 return value->FileName().Hash(); 161 } 162 163 bool Compare(const char* key, const Package* value) const 164 { 165 return strcmp(value->FileName(), key) == 0; 166 } 167 168 Package*& GetLink(Package* value) const 169 { 170 return value->FileNameHashTableNext(); 171 } 172 }; 173 174 175 typedef BOpenHashTable<PackageFileNameHashDefinition> PackageFileNameHashTable; 176 typedef DoublyLinkedList<Package> PackageList; 177 178 179 #endif // PACKAGE_H 180