David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 5 | // This provides access to timestamps with nanosecond resolution in |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 6 | // struct stat, See NOTES in stat(2) for details. |
| 7 | #ifndef _BSD_SOURCE |
| 8 | #define _BSD_SOURCE |
| 9 | #endif |
| 10 | |
| 11 | #include "update_engine/p2p_manager.h" |
| 12 | |
| 13 | #include <attr/xattr.h> |
| 14 | #include <dirent.h> |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <glib.h> |
| 18 | #include <linux/falloc.h> |
| 19 | #include <signal.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/statvfs.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 25 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 26 | #include <algorithm> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 27 | #include <map> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 28 | #include <memory> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 29 | #include <utility> |
| 30 | #include <vector> |
| 31 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 32 | #include <base/files/file_path.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 33 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 34 | #include <base/strings/stringprintf.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 35 | |
Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 36 | #include "update_engine/glib_utils.h" |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 37 | #include "update_engine/utils.h" |
| 38 | |
| 39 | using base::FilePath; |
| 40 | using base::StringPrintf; |
| 41 | using base::Time; |
| 42 | using base::TimeDelta; |
| 43 | using std::map; |
| 44 | using std::pair; |
| 45 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 46 | using std::unique_ptr; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 47 | using std::vector; |
| 48 | |
| 49 | namespace chromeos_update_engine { |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | // The default p2p directory. |
| 54 | const char kDefaultP2PDir[] = "/var/cache/p2p"; |
| 55 | |
| 56 | // The p2p xattr used for conveying the final size of a file - see the |
| 57 | // p2p ddoc for details. |
| 58 | const char kCrosP2PFileSizeXAttrName[] = "user.cros-p2p-filesize"; |
| 59 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 60 | } // namespace |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 61 | |
| 62 | // The default P2PManager::Configuration implementation. |
| 63 | class ConfigurationImpl : public P2PManager::Configuration { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 64 | public: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 65 | ConfigurationImpl() {} |
| 66 | |
| 67 | virtual ~ConfigurationImpl() {} |
| 68 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 69 | virtual FilePath GetP2PDir() { |
| 70 | return FilePath(kDefaultP2PDir); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | virtual vector<string> GetInitctlArgs(bool is_start) { |
| 74 | vector<string> args; |
| 75 | args.push_back("initctl"); |
| 76 | args.push_back(is_start ? "start" : "stop"); |
| 77 | args.push_back("p2p"); |
| 78 | return args; |
| 79 | } |
| 80 | |
| 81 | virtual vector<string> GetP2PClientArgs(const string &file_id, |
| 82 | size_t minimum_size) { |
| 83 | vector<string> args; |
| 84 | args.push_back("p2p-client"); |
| 85 | args.push_back(string("--get-url=") + file_id); |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 86 | args.push_back(StringPrintf("--minimum-size=%zu", minimum_size)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 87 | return args; |
| 88 | } |
| 89 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 90 | private: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 91 | DISALLOW_COPY_AND_ASSIGN(ConfigurationImpl); |
| 92 | }; |
| 93 | |
| 94 | // The default P2PManager implementation. |
| 95 | class P2PManagerImpl : public P2PManager { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 96 | public: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 97 | P2PManagerImpl(Configuration *configuration, |
| 98 | PrefsInterface *prefs, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 99 | ClockInterface *clock, |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 100 | const string& file_extension, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 101 | const int num_files_to_keep, |
| 102 | const base::TimeDelta& max_file_age); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 103 | |
| 104 | // P2PManager methods. |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 105 | virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 106 | virtual bool IsP2PEnabled(); |
| 107 | virtual bool EnsureP2PRunning(); |
| 108 | virtual bool EnsureP2PNotRunning(); |
| 109 | virtual bool PerformHousekeeping(); |
| 110 | virtual void LookupUrlForFile(const string& file_id, |
| 111 | size_t minimum_size, |
| 112 | TimeDelta max_time_to_wait, |
| 113 | LookupCallback callback); |
| 114 | virtual bool FileShare(const string& file_id, |
| 115 | size_t expected_size); |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 116 | virtual FilePath FileGetPath(const string& file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 117 | virtual ssize_t FileGetSize(const string& file_id); |
| 118 | virtual ssize_t FileGetExpectedSize(const string& file_id); |
| 119 | virtual bool FileGetVisible(const string& file_id, |
| 120 | bool *out_result); |
| 121 | virtual bool FileMakeVisible(const string& file_id); |
| 122 | virtual int CountSharedFiles(); |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 123 | bool SetP2PEnabledPref(bool enabled) override; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 124 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 125 | private: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 126 | // Enumeration for specifying visibility. |
| 127 | enum Visibility { |
| 128 | kVisible, |
| 129 | kNonVisible |
| 130 | }; |
| 131 | |
| 132 | // Returns "." + |file_extension_| + ".p2p" if |visibility| is |
| 133 | // |kVisible|. Returns the same concatenated with ".tmp" otherwise. |
| 134 | string GetExt(Visibility visibility); |
| 135 | |
| 136 | // Gets the on-disk path for |file_id| depending on if the file |
| 137 | // is visible or not. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 138 | FilePath GetPath(const string& file_id, Visibility visibility); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 139 | |
| 140 | // Utility function used by EnsureP2PRunning() and EnsureP2PNotRunning(). |
| 141 | bool EnsureP2P(bool should_be_running); |
| 142 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 143 | // Utility function to delete a file given by |path| and log the |
| 144 | // path as well as |reason|. Returns false on failure. |
| 145 | bool DeleteP2PFile(const FilePath& path, const std::string& reason); |
| 146 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 147 | // The device policy being used or null if no policy is being used. |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 148 | const policy::DevicePolicy* device_policy_; |
| 149 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 150 | // Configuration object. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 151 | unique_ptr<Configuration> configuration_; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 152 | |
| 153 | // Object for persisted state. |
| 154 | PrefsInterface* prefs_; |
| 155 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 156 | // Object for telling the time. |
| 157 | ClockInterface* clock_; |
| 158 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 159 | // A short string unique to the application (for example "cros_au") |
| 160 | // used to mark a file as being owned by a particular application. |
| 161 | const string file_extension_; |
| 162 | |
| 163 | // If non-zero, this number denotes how many files in /var/cache/p2p |
| 164 | // owned by the application (cf. |file_extension_|) to keep after |
| 165 | // performing housekeeping. |
| 166 | const int num_files_to_keep_; |
| 167 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 168 | // If non-zero, files older than this will not be kept after |
| 169 | // performing housekeeping. |
| 170 | const base::TimeDelta max_file_age_; |
| 171 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 172 | // The string ".p2p". |
| 173 | static const char kP2PExtension[]; |
| 174 | |
| 175 | // The string ".tmp". |
| 176 | static const char kTmpExtension[]; |
| 177 | |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 178 | // Whether P2P service may be running; initially, we assume it may be. |
| 179 | bool may_be_running_ = true; |
| 180 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 181 | DISALLOW_COPY_AND_ASSIGN(P2PManagerImpl); |
| 182 | }; |
| 183 | |
| 184 | const char P2PManagerImpl::kP2PExtension[] = ".p2p"; |
| 185 | |
| 186 | const char P2PManagerImpl::kTmpExtension[] = ".tmp"; |
| 187 | |
| 188 | P2PManagerImpl::P2PManagerImpl(Configuration *configuration, |
| 189 | PrefsInterface *prefs, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 190 | ClockInterface *clock, |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 191 | const string& file_extension, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 192 | const int num_files_to_keep, |
| 193 | const base::TimeDelta& max_file_age) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 194 | : device_policy_(nullptr), |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 195 | prefs_(prefs), |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 196 | clock_(clock), |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 197 | file_extension_(file_extension), |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 198 | num_files_to_keep_(num_files_to_keep), |
| 199 | max_file_age_(max_file_age) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 200 | configuration_.reset(configuration != nullptr ? configuration : |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 201 | new ConfigurationImpl()); |
| 202 | } |
| 203 | |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 204 | void P2PManagerImpl::SetDevicePolicy( |
| 205 | const policy::DevicePolicy* device_policy) { |
| 206 | device_policy_ = device_policy; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bool P2PManagerImpl::IsP2PEnabled() { |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 210 | bool p2p_enabled = false; |
| 211 | |
| 212 | // The logic we want here is additive, e.g. p2p can be enabled by |
| 213 | // either the crosh flag OR by Enterprise Policy, e.g. the following |
| 214 | // truth table: |
| 215 | // |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 216 | // crosh_flag == FALSE && enterprise_policy == unset -> use_p2p == * |
| 217 | // crosh_flag == TRUE && enterprise_policy == unset -> use_p2p == TRUE |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 218 | // crosh_flag == FALSE && enterprise_policy == FALSE -> use_p2p == FALSE |
| 219 | // crosh_flag == FALSE && enterprise_policy == TRUE -> use_p2p == TRUE |
| 220 | // crosh_flag == TRUE && enterprise_policy == FALSE -> use_p2p == TRUE |
| 221 | // crosh_flag == TRUE && enterprise_policy == TRUE -> use_p2p == TRUE |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 222 | // |
| 223 | // *: TRUE if Enterprise Enrolled, FALSE otherwise. |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 224 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 225 | if (prefs_ != nullptr && |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 226 | prefs_->Exists(kPrefsP2PEnabled) && |
| 227 | prefs_->GetBoolean(kPrefsP2PEnabled, &p2p_enabled) && |
| 228 | p2p_enabled) { |
| 229 | LOG(INFO) << "The crosh flag indicates that p2p is enabled."; |
| 230 | return true; |
| 231 | } |
| 232 | |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 233 | if (device_policy_ != nullptr) { |
| 234 | if (device_policy_->GetAuP2PEnabled(&p2p_enabled)) { |
| 235 | if (p2p_enabled) { |
| 236 | LOG(INFO) << "Enterprise Policy indicates that p2p is enabled."; |
| 237 | return true; |
| 238 | } |
| 239 | } else { |
| 240 | // Enterprise-enrolled devices have an empty owner in their device policy. |
| 241 | string owner; |
| 242 | if (!device_policy_->GetOwner(&owner) || owner.empty()) { |
| 243 | LOG(INFO) << "No p2p_enabled setting in Enterprise Policy but device " |
| 244 | << "is Enterprise Enrolled so allowing p2p."; |
| 245 | return true; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 250 | LOG(INFO) << "Neither Enterprise Policy nor crosh flag indicates that p2p " |
| 251 | << "is enabled."; |
| 252 | return false; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool P2PManagerImpl::EnsureP2P(bool should_be_running) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 256 | gchar *standard_error = nullptr; |
| 257 | GError *error = nullptr; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 258 | gint exit_status = 0; |
| 259 | |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 260 | may_be_running_ = true; // Unless successful, we must be conservative. |
| 261 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 262 | vector<string> args = configuration_->GetInitctlArgs(should_be_running); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 263 | unique_ptr<gchar*, GLibStrvFreeDeleter> argv( |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 264 | utils::StringVectorToGStrv(args)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 265 | if (!g_spawn_sync(nullptr, // working_directory |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 266 | argv.get(), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 267 | nullptr, // envp |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 268 | static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 269 | nullptr, nullptr, // child_setup, user_data |
| 270 | nullptr, // standard_output |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 271 | &standard_error, |
| 272 | &exit_status, |
| 273 | &error)) { |
| 274 | LOG(ERROR) << "Error spawning " << utils::StringVectorToString(args) |
| 275 | << ": " << utils::GetAndFreeGError(&error); |
| 276 | return false; |
| 277 | } |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 278 | unique_ptr<gchar, GLibFreeDeleter> standard_error_deleter(standard_error); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 279 | |
| 280 | if (!WIFEXITED(exit_status)) { |
| 281 | LOG(ERROR) << "Error spawning '" << utils::StringVectorToString(args) |
| 282 | << "': WIFEXITED is false"; |
| 283 | return false; |
| 284 | } |
| 285 | |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 286 | // If initctl(8) does not exit normally (exit status other than zero), ensure |
| 287 | // that the error message is not benign by scanning stderr; this is a |
| 288 | // necessity because initctl does not offer actions such as "start if not |
| 289 | // running" or "stop if running". |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 290 | // TODO(zeuthen,chromium:277051): Avoid doing this. |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 291 | if (WEXITSTATUS(exit_status) != 0) { |
| 292 | const gchar *expected_error_message = should_be_running ? |
| 293 | "initctl: Job is already running: p2p\n" : |
| 294 | "initctl: Unknown instance \n"; |
| 295 | if (g_strcmp0(standard_error, expected_error_message) != 0) |
| 296 | return false; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 299 | may_be_running_ = should_be_running; // Successful after all. |
| 300 | return true; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | bool P2PManagerImpl::EnsureP2PRunning() { |
| 304 | return EnsureP2P(true); |
| 305 | } |
| 306 | |
| 307 | bool P2PManagerImpl::EnsureP2PNotRunning() { |
| 308 | return EnsureP2P(false); |
| 309 | } |
| 310 | |
| 311 | // Returns True if the timestamp in the first pair is greater than the |
| 312 | // timestamp in the latter. If used with std::sort() this will yield a |
| 313 | // sequence of elements where newer (high timestamps) elements precede |
| 314 | // older ones (low timestamps). |
| 315 | static bool MatchCompareFunc(const pair<FilePath, Time>& a, |
| 316 | const pair<FilePath, Time>& b) { |
| 317 | return a.second > b.second; |
| 318 | } |
| 319 | |
| 320 | string P2PManagerImpl::GetExt(Visibility visibility) { |
| 321 | string ext = string(".") + file_extension_ + kP2PExtension; |
| 322 | switch (visibility) { |
| 323 | case kVisible: |
| 324 | break; |
| 325 | case kNonVisible: |
| 326 | ext += kTmpExtension; |
| 327 | break; |
| 328 | // Don't add a default case to let the compiler warn about newly |
| 329 | // added enum values. |
| 330 | } |
| 331 | return ext; |
| 332 | } |
| 333 | |
| 334 | FilePath P2PManagerImpl::GetPath(const string& file_id, Visibility visibility) { |
| 335 | return configuration_->GetP2PDir().Append(file_id + GetExt(visibility)); |
| 336 | } |
| 337 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 338 | bool P2PManagerImpl::DeleteP2PFile(const FilePath& path, |
| 339 | const std::string& reason) { |
| 340 | LOG(INFO) << "Deleting p2p file " << path.value() |
| 341 | << " (reason: " << reason << ")"; |
| 342 | if (unlink(path.value().c_str()) != 0) { |
| 343 | PLOG(ERROR) << "Error deleting p2p file " << path.value(); |
| 344 | return false; |
| 345 | } |
| 346 | return true; |
| 347 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 348 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 349 | |
| 350 | bool P2PManagerImpl::PerformHousekeeping() { |
| 351 | // Open p2p dir. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 352 | FilePath p2p_dir = configuration_->GetP2PDir(); |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 353 | GError* error = nullptr; |
| 354 | GDir* dir = g_dir_open(p2p_dir.value().c_str(), 0, &error); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 355 | if (dir == nullptr) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 356 | LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": " |
| 357 | << utils::GetAndFreeGError(&error); |
| 358 | return false; |
| 359 | } |
| 360 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 361 | // Go through all files and collect their mtime. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 362 | string ext_visible = GetExt(kVisible); |
| 363 | string ext_non_visible = GetExt(kNonVisible); |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 364 | bool deletion_failed = false; |
| 365 | const char* name = nullptr; |
| 366 | vector<pair<FilePath, Time>> matches; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 367 | while ((name = g_dir_read_name(dir)) != nullptr) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 368 | if (!(g_str_has_suffix(name, ext_visible.c_str()) || |
| 369 | g_str_has_suffix(name, ext_non_visible.c_str()))) |
| 370 | continue; |
| 371 | |
| 372 | struct stat statbuf; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 373 | FilePath file = p2p_dir.Append(name); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 374 | if (stat(file.value().c_str(), &statbuf) != 0) { |
| 375 | PLOG(ERROR) << "Error getting file status for " << file.value(); |
| 376 | continue; |
| 377 | } |
| 378 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 379 | Time time = utils::TimeFromStructTimespec(&statbuf.st_mtim); |
| 380 | |
| 381 | // If instructed to keep only files younger than a given age |
| 382 | // (|max_file_age_| != 0), delete files satisfying this criteria |
| 383 | // right now. Otherwise add it to a list we'll consider for later. |
| 384 | if (clock_ != nullptr && max_file_age_ != base::TimeDelta() && |
| 385 | clock_->GetWallclockTime() - time > max_file_age_) { |
| 386 | if (!DeleteP2PFile(file, "file too old")) |
| 387 | deletion_failed = true; |
| 388 | } else { |
| 389 | matches.push_back(std::make_pair(file, time)); |
| 390 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 391 | } |
| 392 | g_dir_close(dir); |
| 393 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 394 | // If instructed to only keep N files (|max_files_to_keep_ != 0), |
| 395 | // sort list of matches, newest (biggest time) to oldest (lowest |
| 396 | // time). Then delete starting at element |num_files_to_keep_|. |
| 397 | if (num_files_to_keep_ > 0) { |
| 398 | std::sort(matches.begin(), matches.end(), MatchCompareFunc); |
| 399 | vector<pair<FilePath, Time>>::const_iterator i; |
| 400 | for (i = matches.begin() + num_files_to_keep_; i < matches.end(); ++i) { |
| 401 | if (!DeleteP2PFile(i->first, "too many files")) |
| 402 | deletion_failed = true; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 406 | return !deletion_failed; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Helper class for implementing LookupUrlForFile(). |
| 410 | class LookupData { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 411 | public: |
| 412 | explicit LookupData(P2PManager::LookupCallback callback) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 413 | : callback_(callback), |
| 414 | pid_(0), |
| 415 | stdout_fd_(-1), |
| 416 | stdout_channel_source_id_(0), |
| 417 | child_watch_source_id_(0), |
| 418 | timeout_source_id_(0), |
| 419 | reported_(false) {} |
| 420 | |
| 421 | ~LookupData() { |
| 422 | if (child_watch_source_id_ != 0) |
| 423 | g_source_remove(child_watch_source_id_); |
| 424 | if (stdout_channel_source_id_ != 0) |
| 425 | g_source_remove(stdout_channel_source_id_); |
| 426 | if (timeout_source_id_ != 0) |
| 427 | g_source_remove(timeout_source_id_); |
| 428 | if (stdout_fd_ != -1) |
| 429 | close(stdout_fd_); |
| 430 | if (pid_ != 0) |
| 431 | kill(pid_, SIGTERM); |
| 432 | } |
| 433 | |
| 434 | void InitiateLookup(gchar **argv, TimeDelta timeout) { |
| 435 | // NOTE: if we fail early (i.e. in this method), we need to schedule |
| 436 | // an idle to report the error. This is because we guarantee that |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 437 | // the callback is always called from the GLib mainloop (this |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 438 | // guarantee is useful for testing). |
| 439 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 440 | GError *error = nullptr; |
| 441 | if (!g_spawn_async_with_pipes(nullptr, // working_directory |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 442 | argv, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 443 | nullptr, // envp |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 444 | static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH | |
| 445 | G_SPAWN_DO_NOT_REAP_CHILD), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 446 | nullptr, // child_setup |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 447 | this, |
| 448 | &pid_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 449 | nullptr, // standard_input |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 450 | &stdout_fd_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 451 | nullptr, // standard_error |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 452 | &error)) { |
| 453 | LOG(ERROR) << "Error spawning p2p-client: " |
| 454 | << utils::GetAndFreeGError(&error); |
| 455 | ReportErrorAndDeleteInIdle(); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | GIOChannel* io_channel = g_io_channel_unix_new(stdout_fd_); |
| 460 | stdout_channel_source_id_ = g_io_add_watch( |
| 461 | io_channel, |
| 462 | static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP), |
| 463 | OnIOChannelActivity, this); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 464 | CHECK_NE(stdout_channel_source_id_, 0u); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 465 | g_io_channel_unref(io_channel); |
| 466 | |
| 467 | child_watch_source_id_ = g_child_watch_add(pid_, OnChildWatchActivity, |
| 468 | this); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 469 | CHECK_NE(child_watch_source_id_, 0u); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 470 | |
| 471 | if (timeout.ToInternalValue() > 0) { |
| 472 | timeout_source_id_ = g_timeout_add(timeout.InMilliseconds(), |
| 473 | OnTimeout, this); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 474 | CHECK_NE(timeout_source_id_, 0u); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 478 | private: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 479 | void ReportErrorAndDeleteInIdle() { |
| 480 | g_idle_add(static_cast<GSourceFunc>(OnIdleForReportErrorAndDelete), this); |
| 481 | } |
| 482 | |
| 483 | static gboolean OnIdleForReportErrorAndDelete(gpointer user_data) { |
| 484 | LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data); |
| 485 | lookup_data->ReportError(); |
| 486 | delete lookup_data; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 487 | return FALSE; // Remove source. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void IssueCallback(const string& url) { |
| 491 | if (!callback_.is_null()) |
| 492 | callback_.Run(url); |
| 493 | } |
| 494 | |
| 495 | void ReportError() { |
| 496 | if (reported_) |
| 497 | return; |
| 498 | IssueCallback(""); |
| 499 | reported_ = true; |
| 500 | } |
| 501 | |
| 502 | void ReportSuccess() { |
| 503 | if (reported_) |
| 504 | return; |
| 505 | |
| 506 | string url = stdout_; |
| 507 | size_t newline_pos = url.find('\n'); |
| 508 | if (newline_pos != string::npos) |
| 509 | url.resize(newline_pos); |
| 510 | |
| 511 | // Since p2p-client(1) is constructing this URL itself strictly |
| 512 | // speaking there's no need to validate it... but, anyway, can't |
| 513 | // hurt. |
| 514 | if (url.compare(0, 7, "http://") == 0) { |
| 515 | IssueCallback(url); |
| 516 | } else { |
| 517 | LOG(ERROR) << "p2p URL '" << url << "' does not look right. Ignoring."; |
| 518 | ReportError(); |
| 519 | } |
| 520 | |
| 521 | reported_ = true; |
| 522 | } |
| 523 | |
| 524 | static gboolean OnIOChannelActivity(GIOChannel *source, |
| 525 | GIOCondition condition, |
| 526 | gpointer user_data) { |
| 527 | LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 528 | gchar* str = nullptr; |
| 529 | GError* error = nullptr; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 530 | GIOStatus status = g_io_channel_read_line(source, |
| 531 | &str, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 532 | nullptr, // len |
| 533 | nullptr, // line_terminator |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 534 | &error); |
| 535 | if (status != G_IO_STATUS_NORMAL) { |
| 536 | // Ignore EOF since we usually get that before SIGCHLD and we |
| 537 | // need to examine exit status there. |
| 538 | if (status != G_IO_STATUS_EOF) { |
| 539 | LOG(ERROR) << "Error reading a line from p2p-client: " |
| 540 | << utils::GetAndFreeGError(&error); |
| 541 | lookup_data->ReportError(); |
| 542 | delete lookup_data; |
| 543 | } |
| 544 | } else { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 545 | if (str != nullptr) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 546 | lookup_data->stdout_ += str; |
| 547 | g_free(str); |
| 548 | } |
| 549 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 550 | return TRUE; // Don't remove source. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static void OnChildWatchActivity(GPid pid, |
| 554 | gint status, |
| 555 | gpointer user_data) { |
| 556 | LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data); |
| 557 | |
| 558 | if (!WIFEXITED(status)) { |
| 559 | LOG(ERROR) << "Child didn't exit normally"; |
| 560 | lookup_data->ReportError(); |
| 561 | } else if (WEXITSTATUS(status) != 0) { |
| 562 | LOG(INFO) << "Child exited with non-zero exit code " |
| 563 | << WEXITSTATUS(status); |
| 564 | lookup_data->ReportError(); |
| 565 | } else { |
| 566 | lookup_data->ReportSuccess(); |
| 567 | } |
| 568 | delete lookup_data; |
| 569 | } |
| 570 | |
| 571 | static gboolean OnTimeout(gpointer user_data) { |
| 572 | LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data); |
| 573 | lookup_data->ReportError(); |
| 574 | delete lookup_data; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 575 | return TRUE; // Don't remove source. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | P2PManager::LookupCallback callback_; |
| 579 | GPid pid_; |
| 580 | gint stdout_fd_; |
| 581 | guint stdout_channel_source_id_; |
| 582 | guint child_watch_source_id_; |
| 583 | guint timeout_source_id_; |
| 584 | string stdout_; |
| 585 | bool reported_; |
| 586 | }; |
| 587 | |
| 588 | void P2PManagerImpl::LookupUrlForFile(const string& file_id, |
| 589 | size_t minimum_size, |
| 590 | TimeDelta max_time_to_wait, |
| 591 | LookupCallback callback) { |
| 592 | LookupData *lookup_data = new LookupData(callback); |
| 593 | string file_id_with_ext = file_id + "." + file_extension_; |
| 594 | vector<string> args = configuration_->GetP2PClientArgs(file_id_with_ext, |
| 595 | minimum_size); |
| 596 | gchar **argv = utils::StringVectorToGStrv(args); |
| 597 | lookup_data->InitiateLookup(argv, max_time_to_wait); |
| 598 | g_strfreev(argv); |
| 599 | } |
| 600 | |
| 601 | bool P2PManagerImpl::FileShare(const string& file_id, |
| 602 | size_t expected_size) { |
| 603 | // Check if file already exist. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 604 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 605 | if (!path.empty()) { |
| 606 | // File exists - double check its expected size though. |
| 607 | ssize_t file_expected_size = FileGetExpectedSize(file_id); |
| 608 | if (file_expected_size == -1 || |
| 609 | static_cast<size_t>(file_expected_size) != expected_size) { |
| 610 | LOG(ERROR) << "Existing p2p file " << path.value() |
| 611 | << " with expected_size=" << file_expected_size |
| 612 | << " does not match the passed in" |
| 613 | << " expected_size=" << expected_size; |
| 614 | return false; |
| 615 | } |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | // Before creating the file, bail if statvfs(3) indicates that at |
| 620 | // least twice the size is not available in P2P_DIR. |
| 621 | struct statvfs statvfsbuf; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 622 | FilePath p2p_dir = configuration_->GetP2PDir(); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 623 | if (statvfs(p2p_dir.value().c_str(), &statvfsbuf) != 0) { |
| 624 | PLOG(ERROR) << "Error calling statvfs() for dir " << p2p_dir.value(); |
| 625 | return false; |
| 626 | } |
| 627 | size_t free_bytes = |
| 628 | static_cast<size_t>(statvfsbuf.f_bsize) * statvfsbuf.f_bavail; |
| 629 | if (free_bytes < 2 * expected_size) { |
| 630 | // This can easily happen and is worth reporting. |
| 631 | LOG(INFO) << "Refusing to allocate p2p file of " << expected_size |
| 632 | << " bytes since the directory " << p2p_dir.value() |
| 633 | << " only has " << free_bytes |
| 634 | << " bytes available and this is less than twice the" |
| 635 | << " requested size."; |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | // Okie-dokey looks like enough space is available - create the file. |
| 640 | path = GetPath(file_id, kNonVisible); |
| 641 | int fd = open(path.value().c_str(), O_CREAT | O_RDWR, 0644); |
| 642 | if (fd == -1) { |
| 643 | PLOG(ERROR) << "Error creating file with path " << path.value(); |
| 644 | return false; |
| 645 | } |
| 646 | ScopedFdCloser fd_closer(&fd); |
| 647 | |
| 648 | // If the final size is known, allocate the file (e.g. reserve disk |
| 649 | // space) and set the user.cros-p2p-filesize xattr. |
| 650 | if (expected_size != 0) { |
| 651 | if (fallocate(fd, |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 652 | FALLOC_FL_KEEP_SIZE, // Keep file size as 0. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 653 | 0, |
| 654 | expected_size) != 0) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 655 | if (errno == ENOSYS || errno == EOPNOTSUPP) { |
| 656 | // If the filesystem doesn't support the fallocate, keep |
| 657 | // going. This is helpful when running unit tests on build |
| 658 | // machines with ancient filesystems and/or OSes. |
| 659 | PLOG(WARNING) << "Ignoring fallocate(2) failure"; |
| 660 | } else { |
| 661 | // ENOSPC can happen (funky race though, cf. the statvfs() check |
| 662 | // above), handle it gracefully, e.g. use logging level INFO. |
| 663 | PLOG(INFO) << "Error allocating " << expected_size |
| 664 | << " bytes for file " << path.value(); |
| 665 | if (unlink(path.value().c_str()) != 0) { |
| 666 | PLOG(ERROR) << "Error deleting file with path " << path.value(); |
| 667 | } |
| 668 | return false; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 669 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 672 | string decimal_size = StringPrintf("%zu", expected_size); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 673 | if (fsetxattr(fd, kCrosP2PFileSizeXAttrName, |
| 674 | decimal_size.c_str(), decimal_size.size(), 0) != 0) { |
| 675 | PLOG(ERROR) << "Error setting xattr " << path.value(); |
| 676 | return false; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | FilePath P2PManagerImpl::FileGetPath(const string& file_id) { |
| 684 | struct stat statbuf; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 685 | FilePath path; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 686 | |
| 687 | path = GetPath(file_id, kVisible); |
| 688 | if (stat(path.value().c_str(), &statbuf) == 0) { |
| 689 | return path; |
| 690 | } |
| 691 | |
| 692 | path = GetPath(file_id, kNonVisible); |
| 693 | if (stat(path.value().c_str(), &statbuf) == 0) { |
| 694 | return path; |
| 695 | } |
| 696 | |
| 697 | path.clear(); |
| 698 | return path; |
| 699 | } |
| 700 | |
| 701 | bool P2PManagerImpl::FileGetVisible(const string& file_id, |
| 702 | bool *out_result) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 703 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 704 | if (path.empty()) { |
| 705 | LOG(ERROR) << "No file for id " << file_id; |
| 706 | return false; |
| 707 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 708 | if (out_result != nullptr) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 709 | *out_result = path.MatchesExtension(kP2PExtension); |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | bool P2PManagerImpl::FileMakeVisible(const string& file_id) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 714 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 715 | if (path.empty()) { |
| 716 | LOG(ERROR) << "No file for id " << file_id; |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | // Already visible? |
| 721 | if (path.MatchesExtension(kP2PExtension)) |
| 722 | return true; |
| 723 | |
| 724 | LOG_ASSERT(path.MatchesExtension(kTmpExtension)); |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 725 | FilePath new_path = path.RemoveExtension(); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 726 | LOG_ASSERT(new_path.MatchesExtension(kP2PExtension)); |
| 727 | if (rename(path.value().c_str(), new_path.value().c_str()) != 0) { |
| 728 | PLOG(ERROR) << "Error renaming " << path.value() |
| 729 | << " to " << new_path.value(); |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | ssize_t P2PManagerImpl::FileGetSize(const string& file_id) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 737 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 738 | if (path.empty()) |
| 739 | return -1; |
| 740 | |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 741 | return utils::FileSize(path.value()); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | ssize_t P2PManagerImpl::FileGetExpectedSize(const string& file_id) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 745 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 746 | if (path.empty()) |
| 747 | return -1; |
| 748 | |
| 749 | char ea_value[64] = { 0 }; |
| 750 | ssize_t ea_size; |
| 751 | ea_size = getxattr(path.value().c_str(), kCrosP2PFileSizeXAttrName, |
| 752 | &ea_value, sizeof(ea_value) - 1); |
| 753 | if (ea_size == -1) { |
| 754 | PLOG(ERROR) << "Error calling getxattr() on file " << path.value(); |
| 755 | return -1; |
| 756 | } |
| 757 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 758 | char* endp = nullptr; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 759 | long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 760 | if (*endp != '\0') { |
| 761 | LOG(ERROR) << "Error parsing the value '" << ea_value |
| 762 | << "' of the xattr " << kCrosP2PFileSizeXAttrName |
| 763 | << " as an integer"; |
| 764 | return -1; |
| 765 | } |
| 766 | |
| 767 | return val; |
| 768 | } |
| 769 | |
| 770 | int P2PManagerImpl::CountSharedFiles() { |
| 771 | GDir* dir; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 772 | GError* error = nullptr; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 773 | const char* name; |
| 774 | int num_files = 0; |
| 775 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 776 | FilePath p2p_dir = configuration_->GetP2PDir(); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 777 | dir = g_dir_open(p2p_dir.value().c_str(), 0, &error); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 778 | if (dir == nullptr) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 779 | LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": " |
| 780 | << utils::GetAndFreeGError(&error); |
| 781 | return -1; |
| 782 | } |
| 783 | |
| 784 | string ext_visible = GetExt(kVisible); |
| 785 | string ext_non_visible = GetExt(kNonVisible); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 786 | while ((name = g_dir_read_name(dir)) != nullptr) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 787 | if (g_str_has_suffix(name, ext_visible.c_str()) || |
| 788 | g_str_has_suffix(name, ext_non_visible.c_str())) { |
| 789 | num_files += 1; |
| 790 | } |
| 791 | } |
| 792 | g_dir_close(dir); |
| 793 | |
| 794 | return num_files; |
| 795 | } |
| 796 | |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 797 | bool P2PManagerImpl::SetP2PEnabledPref(bool enabled) { |
| 798 | if (!prefs_->SetBoolean(chromeos_update_engine::kPrefsP2PEnabled, enabled)) |
| 799 | return false; |
| 800 | |
| 801 | // If P2P should not be running, make sure it isn't. |
| 802 | if (may_be_running_ && !IsP2PEnabled()) |
| 803 | EnsureP2PNotRunning(); |
| 804 | |
| 805 | return true; |
| 806 | } |
| 807 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 808 | P2PManager* P2PManager::Construct(Configuration *configuration, |
| 809 | PrefsInterface *prefs, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 810 | ClockInterface *clock, |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 811 | const string& file_extension, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 812 | const int num_files_to_keep, |
| 813 | const base::TimeDelta& max_file_age) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 814 | return new P2PManagerImpl(configuration, |
| 815 | prefs, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 816 | clock, |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 817 | file_extension, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 818 | num_files_to_keep, |
| 819 | max_file_age); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | } // namespace chromeos_update_engine |