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