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 { 56 ItemState() : policy(P_EXISTS_NONE), status(B_NO_INIT) {} 57 ~ItemState() {} 58 59 inline void Reset(int32 currentPolicy) 60 { 61 destination.Unset(); 62 parent.Unset(); 63 status = B_NO_INIT; 64 policy = currentPolicy; 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); 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 BString fPath; 106 uint64 fOffset; 107 uint64 fSize; 108 uint8 fPathType; 109 uint32 fCreationTime; 110 uint32 fModificationTime; 111 112 BFile* fPackage; 113 }; 114 115 116 class PackageDirectory : public PackageItem { 117 public: 118 PackageDirectory(BFile* parent, const BString& path, 119 uint8 type, uint32 ctime, uint32 mtime, 120 uint64 offset = 0, uint64 size = 0); 121 122 virtual status_t DoInstall(const char* path = NULL, 123 ItemState *state = NULL); 124 virtual const uint32 ItemKind(); 125 }; 126 127 128 class PackageScript : public PackageItem { 129 public: 130 PackageScript(BFile* parent, uint64 offset = 0, 131 uint64 size = 0, uint64 originalSize = 0); 132 133 virtual status_t DoInstall(const char* path = NULL, 134 ItemState *state = NULL); 135 virtual const uint32 ItemKind(); 136 137 thread_id GetThreadId() { return fThreadId; } 138 void SetThreadId(thread_id id) { fThreadId = id; } 139 140 private: 141 status_t _ParseScript(uint8 *buffer, uint64 originalSize, 142 bool *done); 143 status_t _RunScript(uint8 *script, uint32 len); 144 145 uint64 fOriginalSize; 146 thread_id fThreadId; 147 }; 148 149 150 class PackageFile : public PackageItem { 151 public: 152 PackageFile(BFile* parent, const BString& path, 153 uint8 type, uint32 ctime, uint32 mtime, 154 uint64 offset, uint64 size, uint64 originalSize, 155 uint32 platform, const BString& mime, 156 const BString& signature, uint32 mode); 157 158 virtual status_t DoInstall(const char* path = NULL, 159 ItemState *state = NULL); 160 virtual const uint32 ItemKind(); 161 162 private: 163 uint64 fOriginalSize; 164 uint32 fPlatform; 165 uint32 fMode; 166 167 BString fMimeType; 168 BString fSignature; 169 }; 170 171 172 class PackageLink : public PackageItem { 173 public: 174 PackageLink(BFile* parent, const BString& path, 175 const BString& link, uint8 type, uint32 ctime, 176 uint32 mtime, uint32 mode, uint64 offset = 0, 177 uint64 size = 0); 178 179 virtual status_t DoInstall(const char* path = NULL, 180 ItemState *state = NULL); 181 virtual const uint32 ItemKind(); 182 183 private: 184 uint32 fMode; 185 BString fLink; 186 }; 187 188 #endif // PACKAGE_ITEM_H 189