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> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 16 | #include <linux/falloc.h> |
| 17 | #include <signal.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/statvfs.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 23 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 24 | #include <algorithm> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 25 | #include <map> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 26 | #include <memory> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 30 | #include <base/bind.h> |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 31 | #include <base/files/file_enumerator.h> |
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 Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 34 | #include <base/strings/string_util.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(); |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 321 | const string ext_visible = GetExt(kVisible); |
| 322 | const string ext_non_visible = GetExt(kNonVisible); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 323 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 324 | bool deletion_failed = false; |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 325 | vector<pair<FilePath, Time>> matches; |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 326 | |
| 327 | base::FileEnumerator dir(p2p_dir, false, base::FileEnumerator::FILES); |
| 328 | // Go through all files and collect their mtime. |
| 329 | for (FilePath name = dir.Next(); !name.empty(); name = dir.Next()) { |
| 330 | if (!(base::EndsWith(name.value(), ext_visible, true) || |
| 331 | base::EndsWith(name.value(), ext_non_visible, true))) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 332 | continue; |
| 333 | |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 334 | Time time = dir.GetInfo().GetLastModifiedTime(); |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 335 | |
| 336 | // If instructed to keep only files younger than a given age |
| 337 | // (|max_file_age_| != 0), delete files satisfying this criteria |
| 338 | // 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] | 339 | if (clock_ != nullptr && max_file_age_ != TimeDelta() && |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 340 | clock_->GetWallclockTime() - time > max_file_age_) { |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 341 | if (!DeleteP2PFile(name, "file too old")) |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 342 | deletion_failed = true; |
| 343 | } else { |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 344 | matches.push_back(std::make_pair(name, time)); |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 345 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 346 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 347 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 348 | // If instructed to only keep N files (|max_files_to_keep_ != 0), |
| 349 | // sort list of matches, newest (biggest time) to oldest (lowest |
| 350 | // time). Then delete starting at element |num_files_to_keep_|. |
| 351 | if (num_files_to_keep_ > 0) { |
| 352 | std::sort(matches.begin(), matches.end(), MatchCompareFunc); |
| 353 | vector<pair<FilePath, Time>>::const_iterator i; |
| 354 | for (i = matches.begin() + num_files_to_keep_; i < matches.end(); ++i) { |
| 355 | if (!DeleteP2PFile(i->first, "too many files")) |
| 356 | deletion_failed = true; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 360 | return !deletion_failed; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Helper class for implementing LookupUrlForFile(). |
| 364 | class LookupData { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 365 | public: |
| 366 | explicit LookupData(P2PManager::LookupCallback callback) |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 367 | : callback_(callback) {} |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 368 | |
| 369 | ~LookupData() { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 370 | if (timeout_task_ != MessageLoop::kTaskIdNull) |
| 371 | MessageLoop::current()->CancelTask(timeout_task_); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 372 | if (child_pid_) |
| 373 | Subprocess::Get().KillExec(child_pid_); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 376 | void InitiateLookup(const vector<string>& cmd, TimeDelta timeout) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 377 | // NOTE: if we fail early (i.e. in this method), we need to schedule |
| 378 | // an idle to report the error. This is because we guarantee that |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 379 | // the callback is always called from the message loop (this |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 380 | // guarantee is useful for testing). |
| 381 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 382 | // We expect to run just "p2p-client" and find it in the path. |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 383 | child_pid_ = Subprocess::Get().ExecFlags( |
| 384 | cmd, Subprocess::kSearchPath, |
| 385 | Bind(&LookupData::OnLookupDone, base::Unretained(this))); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 386 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 387 | if (!child_pid_) { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 388 | LOG(ERROR) << "Error spawning " << utils::StringVectorToString(cmd); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 389 | ReportErrorAndDeleteInIdle(); |
| 390 | return; |
| 391 | } |
| 392 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 393 | if (timeout > TimeDelta()) { |
| 394 | timeout_task_ = MessageLoop::current()->PostDelayedTask( |
| 395 | FROM_HERE, |
| 396 | Bind(&LookupData::OnTimeout, base::Unretained(this)), |
| 397 | timeout); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 401 | private: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 402 | void ReportErrorAndDeleteInIdle() { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 403 | MessageLoop::current()->PostTask(FROM_HERE, Bind( |
| 404 | &LookupData::OnIdleForReportErrorAndDelete, |
| 405 | base::Unretained(this))); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 408 | void OnIdleForReportErrorAndDelete() { |
| 409 | ReportError(); |
| 410 | delete this; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void IssueCallback(const string& url) { |
| 414 | if (!callback_.is_null()) |
| 415 | callback_.Run(url); |
| 416 | } |
| 417 | |
| 418 | void ReportError() { |
| 419 | if (reported_) |
| 420 | return; |
| 421 | IssueCallback(""); |
| 422 | reported_ = true; |
| 423 | } |
| 424 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 425 | void ReportSuccess(const string& output) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 426 | if (reported_) |
| 427 | return; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 428 | string url = output; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 429 | size_t newline_pos = url.find('\n'); |
| 430 | if (newline_pos != string::npos) |
| 431 | url.resize(newline_pos); |
| 432 | |
| 433 | // Since p2p-client(1) is constructing this URL itself strictly |
| 434 | // speaking there's no need to validate it... but, anyway, can't |
| 435 | // hurt. |
| 436 | if (url.compare(0, 7, "http://") == 0) { |
| 437 | IssueCallback(url); |
| 438 | } else { |
| 439 | LOG(ERROR) << "p2p URL '" << url << "' does not look right. Ignoring."; |
| 440 | ReportError(); |
| 441 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 442 | reported_ = true; |
| 443 | } |
| 444 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 445 | void OnLookupDone(int return_code, const string& output) { |
| 446 | child_pid_ = 0; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 447 | if (return_code != 0) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 448 | LOG(INFO) << "Child exited with non-zero exit code " |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 449 | << return_code; |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 450 | ReportError(); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 451 | } else { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 452 | ReportSuccess(output); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 453 | } |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 454 | delete this; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 457 | void OnTimeout() { |
| 458 | timeout_task_ = MessageLoop::kTaskIdNull; |
| 459 | ReportError(); |
| 460 | delete this; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | P2PManager::LookupCallback callback_; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 464 | |
| 465 | // The Subprocess tag of the running process. A value of 0 means that the |
| 466 | // process is not running. |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 467 | pid_t child_pid_{0}; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 468 | |
| 469 | // The timeout task_id we are waiting on, if any. |
| 470 | MessageLoop::TaskId timeout_task_{MessageLoop::kTaskIdNull}; |
| 471 | |
| 472 | bool reported_{false}; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | void P2PManagerImpl::LookupUrlForFile(const string& file_id, |
| 476 | size_t minimum_size, |
| 477 | TimeDelta max_time_to_wait, |
| 478 | LookupCallback callback) { |
| 479 | LookupData *lookup_data = new LookupData(callback); |
| 480 | string file_id_with_ext = file_id + "." + file_extension_; |
| 481 | vector<string> args = configuration_->GetP2PClientArgs(file_id_with_ext, |
| 482 | minimum_size); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 483 | lookup_data->InitiateLookup(args, max_time_to_wait); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | bool P2PManagerImpl::FileShare(const string& file_id, |
| 487 | size_t expected_size) { |
| 488 | // Check if file already exist. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 489 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 490 | if (!path.empty()) { |
| 491 | // File exists - double check its expected size though. |
| 492 | ssize_t file_expected_size = FileGetExpectedSize(file_id); |
| 493 | if (file_expected_size == -1 || |
| 494 | static_cast<size_t>(file_expected_size) != expected_size) { |
| 495 | LOG(ERROR) << "Existing p2p file " << path.value() |
| 496 | << " with expected_size=" << file_expected_size |
| 497 | << " does not match the passed in" |
| 498 | << " expected_size=" << expected_size; |
| 499 | return false; |
| 500 | } |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | // Before creating the file, bail if statvfs(3) indicates that at |
| 505 | // least twice the size is not available in P2P_DIR. |
| 506 | struct statvfs statvfsbuf; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 507 | FilePath p2p_dir = configuration_->GetP2PDir(); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 508 | if (statvfs(p2p_dir.value().c_str(), &statvfsbuf) != 0) { |
| 509 | PLOG(ERROR) << "Error calling statvfs() for dir " << p2p_dir.value(); |
| 510 | return false; |
| 511 | } |
| 512 | size_t free_bytes = |
| 513 | static_cast<size_t>(statvfsbuf.f_bsize) * statvfsbuf.f_bavail; |
| 514 | if (free_bytes < 2 * expected_size) { |
| 515 | // This can easily happen and is worth reporting. |
| 516 | LOG(INFO) << "Refusing to allocate p2p file of " << expected_size |
| 517 | << " bytes since the directory " << p2p_dir.value() |
| 518 | << " only has " << free_bytes |
| 519 | << " bytes available and this is less than twice the" |
| 520 | << " requested size."; |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | // Okie-dokey looks like enough space is available - create the file. |
| 525 | path = GetPath(file_id, kNonVisible); |
| 526 | int fd = open(path.value().c_str(), O_CREAT | O_RDWR, 0644); |
| 527 | if (fd == -1) { |
| 528 | PLOG(ERROR) << "Error creating file with path " << path.value(); |
| 529 | return false; |
| 530 | } |
| 531 | ScopedFdCloser fd_closer(&fd); |
| 532 | |
| 533 | // If the final size is known, allocate the file (e.g. reserve disk |
| 534 | // space) and set the user.cros-p2p-filesize xattr. |
| 535 | if (expected_size != 0) { |
| 536 | if (fallocate(fd, |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 537 | FALLOC_FL_KEEP_SIZE, // Keep file size as 0. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 538 | 0, |
| 539 | expected_size) != 0) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 540 | if (errno == ENOSYS || errno == EOPNOTSUPP) { |
| 541 | // If the filesystem doesn't support the fallocate, keep |
| 542 | // going. This is helpful when running unit tests on build |
| 543 | // machines with ancient filesystems and/or OSes. |
| 544 | PLOG(WARNING) << "Ignoring fallocate(2) failure"; |
| 545 | } else { |
| 546 | // ENOSPC can happen (funky race though, cf. the statvfs() check |
| 547 | // above), handle it gracefully, e.g. use logging level INFO. |
| 548 | PLOG(INFO) << "Error allocating " << expected_size |
| 549 | << " bytes for file " << path.value(); |
| 550 | if (unlink(path.value().c_str()) != 0) { |
| 551 | PLOG(ERROR) << "Error deleting file with path " << path.value(); |
| 552 | } |
| 553 | return false; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 554 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 557 | string decimal_size = StringPrintf("%zu", expected_size); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 558 | if (fsetxattr(fd, kCrosP2PFileSizeXAttrName, |
| 559 | decimal_size.c_str(), decimal_size.size(), 0) != 0) { |
| 560 | PLOG(ERROR) << "Error setting xattr " << path.value(); |
| 561 | return false; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return true; |
| 566 | } |
| 567 | |
| 568 | FilePath P2PManagerImpl::FileGetPath(const string& file_id) { |
| 569 | struct stat statbuf; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 570 | FilePath path; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 571 | |
| 572 | path = GetPath(file_id, kVisible); |
| 573 | if (stat(path.value().c_str(), &statbuf) == 0) { |
| 574 | return path; |
| 575 | } |
| 576 | |
| 577 | path = GetPath(file_id, kNonVisible); |
| 578 | if (stat(path.value().c_str(), &statbuf) == 0) { |
| 579 | return path; |
| 580 | } |
| 581 | |
| 582 | path.clear(); |
| 583 | return path; |
| 584 | } |
| 585 | |
| 586 | bool P2PManagerImpl::FileGetVisible(const string& file_id, |
| 587 | bool *out_result) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 588 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 589 | if (path.empty()) { |
| 590 | LOG(ERROR) << "No file for id " << file_id; |
| 591 | return false; |
| 592 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 593 | if (out_result != nullptr) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 594 | *out_result = path.MatchesExtension(kP2PExtension); |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | bool P2PManagerImpl::FileMakeVisible(const string& file_id) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 599 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 600 | if (path.empty()) { |
| 601 | LOG(ERROR) << "No file for id " << file_id; |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | // Already visible? |
| 606 | if (path.MatchesExtension(kP2PExtension)) |
| 607 | return true; |
| 608 | |
| 609 | LOG_ASSERT(path.MatchesExtension(kTmpExtension)); |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 610 | FilePath new_path = path.RemoveExtension(); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 611 | LOG_ASSERT(new_path.MatchesExtension(kP2PExtension)); |
| 612 | if (rename(path.value().c_str(), new_path.value().c_str()) != 0) { |
| 613 | PLOG(ERROR) << "Error renaming " << path.value() |
| 614 | << " to " << new_path.value(); |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | ssize_t P2PManagerImpl::FileGetSize(const string& file_id) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 622 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 623 | if (path.empty()) |
| 624 | return -1; |
| 625 | |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 626 | return utils::FileSize(path.value()); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | ssize_t P2PManagerImpl::FileGetExpectedSize(const string& file_id) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 630 | FilePath path = FileGetPath(file_id); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 631 | if (path.empty()) |
| 632 | return -1; |
| 633 | |
| 634 | char ea_value[64] = { 0 }; |
| 635 | ssize_t ea_size; |
| 636 | ea_size = getxattr(path.value().c_str(), kCrosP2PFileSizeXAttrName, |
| 637 | &ea_value, sizeof(ea_value) - 1); |
| 638 | if (ea_size == -1) { |
| 639 | PLOG(ERROR) << "Error calling getxattr() on file " << path.value(); |
| 640 | return -1; |
| 641 | } |
| 642 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 643 | char* endp = nullptr; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 644 | long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 645 | if (*endp != '\0') { |
| 646 | LOG(ERROR) << "Error parsing the value '" << ea_value |
| 647 | << "' of the xattr " << kCrosP2PFileSizeXAttrName |
| 648 | << " as an integer"; |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | return val; |
| 653 | } |
| 654 | |
| 655 | int P2PManagerImpl::CountSharedFiles() { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 656 | int num_files = 0; |
| 657 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 658 | FilePath p2p_dir = configuration_->GetP2PDir(); |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 659 | const string ext_visible = GetExt(kVisible); |
| 660 | const string ext_non_visible = GetExt(kNonVisible); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 661 | |
Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 662 | base::FileEnumerator dir(p2p_dir, false, base::FileEnumerator::FILES); |
| 663 | for (FilePath name = dir.Next(); !name.empty(); name = dir.Next()) { |
| 664 | if (base::EndsWith(name.value(), ext_visible, true) || |
| 665 | base::EndsWith(name.value(), ext_non_visible, true)) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 666 | num_files += 1; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 667 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 668 | |
| 669 | return num_files; |
| 670 | } |
| 671 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 672 | void P2PManagerImpl::ScheduleEnabledStatusChange() { |
| 673 | if (waiting_for_enabled_status_change_) |
| 674 | return; |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 675 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 676 | Callback<void(EvalStatus, const bool&)> callback = Bind( |
| 677 | &P2PManagerImpl::OnEnabledStatusChange, base::Unretained(this)); |
| 678 | update_manager_->AsyncPolicyRequest(callback, &Policy::P2PEnabledChanged, |
| 679 | is_enabled_); |
| 680 | waiting_for_enabled_status_change_ = true; |
Gilad Arnold | ccd0957 | 2014-10-27 13:37:50 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 683 | void P2PManagerImpl::OnEnabledStatusChange(EvalStatus status, |
| 684 | const bool& result) { |
| 685 | waiting_for_enabled_status_change_ = false; |
| 686 | |
| 687 | if (status == EvalStatus::kSucceeded) { |
| 688 | if (result == is_enabled_) { |
| 689 | LOG(WARNING) << "P2P enabled status did not change, which means that it " |
| 690 | "is permanent; not scheduling further checks."; |
| 691 | waiting_for_enabled_status_change_ = true; |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | is_enabled_ = result; |
| 696 | |
| 697 | // If P2P is running but shouldn't be, make sure it isn't. |
| 698 | if (may_be_running_ && !is_enabled_ && !EnsureP2PNotRunning()) { |
| 699 | LOG(WARNING) << "Failed to stop P2P service."; |
| 700 | } |
| 701 | } else { |
| 702 | LOG(WARNING) |
| 703 | << "P2P enabled tracking failed (possibly timed out); retrying."; |
| 704 | } |
| 705 | |
| 706 | ScheduleEnabledStatusChange(); |
| 707 | } |
| 708 | |
| 709 | P2PManager* P2PManager::Construct( |
| 710 | Configuration *configuration, |
| 711 | ClockInterface *clock, |
| 712 | UpdateManager* update_manager, |
| 713 | const string& file_extension, |
| 714 | const int num_files_to_keep, |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 715 | const TimeDelta& max_file_age) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 716 | return new P2PManagerImpl(configuration, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 717 | clock, |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 718 | update_manager, |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 719 | file_extension, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 720 | num_files_to_keep, |
| 721 | max_file_age); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | } // namespace chromeos_update_engine |