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