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