1 /* 2 * Copyright 2011-2018, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Oliver Tappe <zooey@hirschkaefer.de> 7 * Andrew Lindesay <apl@lindesay.co.nz> 8 */ 9 10 11 #include <package/RepositoryConfig.h> 12 13 #include <stdlib.h> 14 15 #include <new> 16 17 #include <Directory.h> 18 #include <driver_settings.h> 19 #include <File.h> 20 #include <FindDirectory.h> 21 #include <Path.h> 22 23 #include <DriverSettings.h> 24 25 26 #define STORAGE_VERSION 2 27 28 #define KEY_BASE_URL "baseurl" 29 #define KEY_BASE_URL_LEGACY "url" 30 // deprecated 31 #define KEY_URL "url" 32 #define KEY_PRIORITY "priority" 33 #define KEY_CONFIG_VERSION "cfgversion" 34 35 namespace BPackageKit { 36 37 38 BRepositoryConfig::BRepositoryConfig() 39 : 40 fInitStatus(B_NO_INIT), 41 fPriority(kUnsetPriority), 42 fIsUserSpecific(false) 43 { 44 } 45 46 47 BRepositoryConfig::BRepositoryConfig(const BEntry& entry) 48 { 49 SetTo(entry); 50 } 51 52 53 BRepositoryConfig::BRepositoryConfig(const BString& name, 54 const BString& baseURL, uint8 priority) 55 : 56 fInitStatus(B_OK), 57 fName(name), 58 fBaseURL(baseURL), 59 fPriority(priority), 60 fIsUserSpecific(false) 61 { 62 } 63 64 65 BRepositoryConfig::~BRepositoryConfig() 66 { 67 } 68 69 70 status_t 71 BRepositoryConfig::Store(const BEntry& entry) const 72 { 73 BFile file(&entry, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 74 status_t result = file.InitCheck(); 75 if (result != B_OK) 76 return result; 77 78 BString configString; 79 configString << KEY_CONFIG_VERSION << "=" << STORAGE_VERSION << "\n"; 80 configString << "\n"; 81 configString << "# This is the URL where the repository data can be " 82 "accessed.\n"; 83 configString << KEY_BASE_URL << "=" << fBaseURL << "\n"; 84 configString << "\n"; 85 configString << "# This URL is an identifier for the repository that is " 86 "consistent across mirrors\n"; 87 88 if (fURL.IsEmpty()) 89 configString << "# " << KEY_URL << "=???\n"; 90 else 91 configString << KEY_URL << "=" << fURL << "\n"; 92 configString << "\n"; 93 configString << KEY_PRIORITY << "=" << fPriority << "\n"; 94 95 int32 size = configString.Length(); 96 if ((result = file.Write(configString.String(), size)) < size) 97 return (result >= 0) ? B_ERROR : result; 98 99 return B_OK; 100 } 101 102 103 status_t 104 BRepositoryConfig::InitCheck() const 105 { 106 return fInitStatus; 107 } 108 109 110 status_t 111 BRepositoryConfig::SetTo(const BEntry& entry) 112 { 113 fEntry = entry; 114 fInitStatus = B_NO_INIT; 115 116 entry_ref ref; 117 status_t result = entry.GetRef(&ref); 118 if (result != B_OK) 119 return result; 120 121 BDriverSettings driverSettings; 122 result = driverSettings.Load(ref); 123 if (result != B_OK) 124 return result; 125 126 const char* url = NULL; 127 const char* version = driverSettings.GetParameterValue(KEY_CONFIG_VERSION); 128 const char *baseUrlKey = KEY_BASE_URL; 129 130 if (version == NULL || atoi(version) < 2) 131 baseUrlKey = KEY_BASE_URL_LEGACY; 132 else 133 url = driverSettings.GetParameterValue(KEY_URL); 134 135 const char* baseUrl = driverSettings.GetParameterValue(baseUrlKey); 136 const char* priorityString = driverSettings.GetParameterValue(KEY_PRIORITY); 137 138 if (baseUrl == NULL || *baseUrl == '\0') 139 return B_BAD_DATA; 140 141 fName = entry.Name(); 142 fBaseURL = baseUrl; 143 fPriority = priorityString == NULL 144 ? kUnsetPriority : atoi(priorityString); 145 fURL = url == NULL ? "" : url; 146 147 BPath userSettingsPath; 148 if (find_directory(B_USER_SETTINGS_DIRECTORY, &userSettingsPath) == B_OK) { 149 BDirectory userSettingsDir(userSettingsPath.Path()); 150 fIsUserSpecific = userSettingsDir.Contains(&entry); 151 } else 152 fIsUserSpecific = false; 153 154 fInitStatus = B_OK; 155 156 return B_OK; 157 } 158 159 160 const BString& 161 BRepositoryConfig::Name() const 162 { 163 return fName; 164 } 165 166 167 const BString& 168 BRepositoryConfig::BaseURL() const 169 { 170 return fBaseURL; 171 } 172 173 174 const BString& 175 BRepositoryConfig::URL() const 176 { 177 return fURL; 178 } 179 180 181 uint8 182 BRepositoryConfig::Priority() const 183 { 184 return fPriority; 185 } 186 187 188 bool 189 BRepositoryConfig::IsUserSpecific() const 190 { 191 return fIsUserSpecific; 192 } 193 194 195 const BEntry& 196 BRepositoryConfig::Entry() const 197 { 198 return fEntry; 199 } 200 201 202 BString 203 BRepositoryConfig::PackagesURL() const 204 { 205 if (fBaseURL.IsEmpty()) 206 return BString(); 207 return BString().SetToFormat("%s/packages", fBaseURL.String()); 208 } 209 210 211 void 212 BRepositoryConfig::SetName(const BString& name) 213 { 214 fName = name; 215 } 216 217 218 void 219 BRepositoryConfig::SetBaseURL(const BString& baseURL) 220 { 221 fBaseURL = baseURL; 222 } 223 224 225 void 226 BRepositoryConfig::SetURL(const BString& URL) 227 { 228 fURL = URL; 229 } 230 231 232 void 233 BRepositoryConfig::SetPriority(uint8 priority) 234 { 235 fPriority = priority; 236 } 237 238 239 void 240 BRepositoryConfig::SetIsUserSpecific(bool isUserSpecific) 241 { 242 fIsUserSpecific = isUserSpecific; 243 } 244 245 246 } // namespace BPackageKit 247