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