1 /* 2 * Copyright 2011, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Oliver Tappe <zooey@hirschkaefer.de> 7 */ 8 9 10 #include <package/RepositoryConfig.h> 11 12 #include <stdlib.h> 13 14 #include <new> 15 16 #include <Directory.h> 17 #include <driver_settings.h> 18 #include <File.h> 19 #include <FindDirectory.h> 20 #include <Path.h> 21 22 23 namespace BPackageKit { 24 25 26 BRepositoryConfig::BRepositoryConfig() 27 : 28 fInitStatus(B_NO_INIT), 29 fPriority(kUnsetPriority), 30 fIsUserSpecific(false) 31 { 32 } 33 34 35 BRepositoryConfig::BRepositoryConfig(const BEntry& entry) 36 { 37 SetTo(entry); 38 } 39 40 41 BRepositoryConfig::BRepositoryConfig(const BString& name, 42 const BString& baseURL, uint8 priority) 43 : 44 fInitStatus(B_OK), 45 fName(name), 46 fBaseURL(baseURL), 47 fPriority(priority), 48 fIsUserSpecific(false) 49 { 50 } 51 52 53 BRepositoryConfig::~BRepositoryConfig() 54 { 55 } 56 57 58 status_t 59 BRepositoryConfig::Store(const BEntry& entry) const 60 { 61 BFile file(&entry, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 62 status_t result = file.InitCheck(); 63 if (result != B_OK) 64 return result; 65 66 BString configString; 67 configString 68 << "url=" << fBaseURL << "\n" 69 << "priority=" << fPriority << "\n"; 70 71 int32 size = configString.Length(); 72 if ((result = file.Write(configString.String(), size)) < size) 73 return (result >= 0) ? B_ERROR : result; 74 75 return B_OK; 76 } 77 78 79 status_t 80 BRepositoryConfig::InitCheck() const 81 { 82 return fInitStatus; 83 } 84 85 86 status_t 87 BRepositoryConfig::SetTo(const BEntry& entry) 88 { 89 fEntry = entry; 90 fInitStatus = B_NO_INIT; 91 92 BFile file(&entry, B_READ_ONLY); 93 status_t result = file.InitCheck(); 94 if (result != B_OK) 95 return result; 96 97 off_t size; 98 if ((result = file.GetSize(&size)) != B_OK) 99 return result; 100 101 BString configString; 102 char* buffer = configString.LockBuffer(size); 103 if (buffer == NULL) 104 return B_NO_MEMORY; 105 106 if ((result = file.Read(buffer, size)) < size) { 107 configString.UnlockBuffer(0); 108 return (result >= 0) ? B_IO_ERROR : result; 109 } 110 111 buffer[size] = '\0'; 112 configString.UnlockBuffer(size); 113 114 void* settingsHandle = parse_driver_settings_string(configString.String()); 115 if (settingsHandle == NULL) 116 return B_BAD_DATA; 117 118 const char* url = get_driver_parameter(settingsHandle, "url", NULL, NULL); 119 const char* priorityString 120 = get_driver_parameter(settingsHandle, "priority", NULL, NULL); 121 122 unload_driver_settings(settingsHandle); 123 124 if (url == NULL || *url == '\0') 125 return B_BAD_DATA; 126 127 char name[B_FILE_NAME_LENGTH]; 128 if ((result = entry.GetName(name)) != B_OK) 129 return result; 130 131 fName = name; 132 fBaseURL = url; 133 fPriority = priorityString == NULL 134 ? kUnsetPriority : atoi(priorityString); 135 136 BPath userSettingsPath; 137 if (find_directory(B_USER_SETTINGS_DIRECTORY, &userSettingsPath) == B_OK) { 138 BDirectory userSettingsDir(userSettingsPath.Path()); 139 fIsUserSpecific = userSettingsDir.Contains(&entry); 140 } else 141 fIsUserSpecific = false; 142 143 fInitStatus = B_OK; 144 145 return B_OK; 146 } 147 148 149 const BString& 150 BRepositoryConfig::Name() const 151 { 152 return fName; 153 } 154 155 156 const BString& 157 BRepositoryConfig::BaseURL() const 158 { 159 return fBaseURL; 160 } 161 162 163 uint8 164 BRepositoryConfig::Priority() const 165 { 166 return fPriority; 167 } 168 169 170 bool 171 BRepositoryConfig::IsUserSpecific() const 172 { 173 return fIsUserSpecific; 174 } 175 176 177 const BEntry& 178 BRepositoryConfig::Entry() const 179 { 180 return fEntry; 181 } 182 183 184 void 185 BRepositoryConfig::SetName(const BString& name) 186 { 187 fName = name; 188 } 189 190 191 void 192 BRepositoryConfig::SetBaseURL(const BString& baseURL) 193 { 194 fBaseURL = baseURL; 195 } 196 197 198 void 199 BRepositoryConfig::SetPriority(uint8 priority) 200 { 201 fPriority = priority; 202 } 203 204 205 void 206 BRepositoryConfig::SetIsUserSpecific(bool isUserSpecific) 207 { 208 fIsUserSpecific = isUserSpecific; 209 } 210 211 212 } // namespace BPackageKit 213