1 /* 2 * Copyright 2007-2009, Haiku, Inc. 3 * Distributed under the terms of the MIT license. 4 * 5 * Author: 6 * Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org> 7 */ 8 #ifndef PACKAGE_ITEM_H 9 #define PACKAGE_ITEM_H 10 11 12 #include <stdio.h> 13 14 #include <Directory.h> 15 #include <Entry.h> 16 #include <File.h> 17 #include <Path.h> 18 #include <String.h> 19 20 21 // Local macro for the parser debug output 22 //#define DEBUG_PARSER 23 #ifdef DEBUG_PARSER 24 # define parser_debug(format, args...) fprintf(stderr, format, ##args) 25 #else 26 # define parser_debug(format, args...) 27 #endif 28 29 30 enum { 31 P_INSTALL_PATH = 0, 32 P_SYSTEM_PATH, 33 P_USER_PATH 34 }; 35 36 // Existing item overwriting policy of a single file 37 enum { 38 P_EXISTS_ASK = 0, 39 P_EXISTS_OVERWRITE, 40 P_EXISTS_SKIP, 41 P_EXISTS_ABORT, 42 P_EXISTS_NONE 43 }; 44 45 const uint32 P_NO_KIND = 0; 46 const uint32 P_KIND_SCRIPT = 1; 47 const uint32 P_KIND_FILE = 2; 48 const uint32 P_KIND_DIRECTORY = 3; 49 const uint32 P_KIND_SYM_LINK = 4; 50 51 extern status_t inflate_data(uint8* in, uint32 inSize, uint8* out, 52 uint32 outSize); 53 54 55 struct ItemState { ItemStateItemState56 ItemState(uint8 _policy) 57 : 58 policy(_policy), 59 status(B_NO_INIT) 60 { 61 } 62 ~ItemStateItemState63 ~ItemState() 64 { 65 } 66 67 BPath destination; 68 BDirectory parent; 69 uint8 policy; 70 status_t status; 71 }; 72 73 74 class PackageItem { 75 public: 76 PackageItem(BFile* parent, const BString& path, 77 uint8 type, uint32 ctime, uint32 mtime, 78 uint64 offset = 0, uint64 size = 0); 79 virtual ~PackageItem(); 80 81 virtual status_t DoInstall(const char* path = NULL, 82 ItemState *state = NULL) = 0; 83 virtual void SetTo(BFile* parent, const BString& path, 84 uint8 type, uint32 ctime, uint32 mtime, 85 uint64 offset = 0, uint64 size = 0); ItemKind()86 virtual const uint32 ItemKind() {return P_NO_KIND;}; 87 88 protected: 89 status_t InitPath(const char* path, BPath* destination); 90 status_t HandleAttributes(BPath* destination, BNode* node, 91 const char* header); 92 93 status_t ParseAttribute(uint8* buffer, BNode* node, 94 char** attrName, uint32* nameSize, 95 uint32* attrType, uint8** attrData, 96 uint64* dataSize, uint8** temp, 97 uint64* tempSize, uint64* attrCSize, 98 uint64* attrOSize, bool* attrStarted, 99 bool* done); 100 status_t SkipAttribute(uint8* buffer, bool* attrStarted, 101 bool* done); 102 status_t ParseData(uint8* buffer, BFile* file, 103 uint64 originalSize, bool* done); 104 105 protected: 106 BString fPath; 107 uint64 fOffset; 108 uint64 fSize; 109 uint8 fPathType; 110 uint32 fCreationTime; 111 uint32 fModificationTime; 112 113 BFile* fPackage; 114 }; 115 116 117 class PackageDirectory : public PackageItem { 118 public: 119 PackageDirectory(BFile* parent, const BString& path, 120 uint8 type, uint32 ctime, uint32 mtime, 121 uint64 offset = 0, uint64 size = 0); 122 123 virtual status_t DoInstall(const char* path = NULL, 124 ItemState* state = NULL); 125 virtual const uint32 ItemKind(); 126 }; 127 128 129 class PackageScript : public PackageItem { 130 public: 131 PackageScript(BFile* parent, const BString& path, 132 uint8 type, uint64 offset = 0, uint64 size = 0, 133 uint64 originalSize = 0); 134 135 virtual status_t DoInstall(const char* path = NULL, 136 ItemState *state = NULL); 137 virtual const uint32 ItemKind(); 138 GetThreadId()139 thread_id GetThreadId() { return fThreadId; } SetThreadId(thread_id id)140 void SetThreadId(thread_id id) { fThreadId = id; } 141 142 private: 143 status_t _ParseScript(uint8* buffer, uint64 originalSize, 144 BString& script, bool* done); 145 status_t _RunScript(const char* workingDirectory, 146 const BString& script); 147 148 private: 149 uint64 fOriginalSize; 150 thread_id fThreadId; 151 }; 152 153 154 class PackageFile : public PackageItem { 155 public: 156 PackageFile(BFile* parent, const BString& path, 157 uint8 type, uint32 ctime, uint32 mtime, 158 uint64 offset, uint64 size, uint64 originalSize, 159 uint32 platform, const BString& mime, 160 const BString& signature, uint32 mode); 161 162 virtual status_t DoInstall(const char* path = NULL, 163 ItemState* state = NULL); 164 virtual const uint32 ItemKind(); 165 166 private: 167 uint64 fOriginalSize; 168 uint32 fPlatform; 169 uint32 fMode; 170 171 BString fMimeType; 172 BString fSignature; 173 }; 174 175 176 class PackageLink : public PackageItem { 177 public: 178 PackageLink(BFile* parent, const BString& path, 179 const BString& link, uint8 type, uint32 ctime, 180 uint32 mtime, uint32 mode, uint64 offset = 0, 181 uint64 size = 0); 182 183 virtual status_t DoInstall(const char* path = NULL, 184 ItemState *state = NULL); 185 virtual const uint32 ItemKind(); 186 187 private: 188 uint32 fMode; 189 BString fLink; 190 }; 191 192 #endif // PACKAGE_ITEM_H 193