1 // 2 // Copyright (C) 2013 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef UPDATE_ENGINE_CROS_P2P_MANAGER_H_ 18 #define UPDATE_ENGINE_CROS_P2P_MANAGER_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include <base/callback.h> 24 #include <base/files/file_path.h> 25 #include <base/memory/ref_counted.h> 26 #include <base/time/time.h> 27 #include <policy/device_policy.h> 28 #include <policy/libpolicy.h> 29 30 #include "update_engine/common/prefs_interface.h" 31 #include "update_engine/update_manager/update_manager.h" 32 33 namespace chromeos_update_engine { 34 35 // Interface for sharing and discovering files via p2p. 36 class P2PManager { 37 public: 38 // Interface used for P2PManager implementations. The sole reason 39 // for this interface is unit testing. 40 class Configuration { 41 public: ~Configuration()42 virtual ~Configuration() {} 43 44 // Gets the path to the p2p dir being used, e.g. /var/cache/p2p. 45 virtual base::FilePath GetP2PDir() = 0; 46 47 // Gets the argument vector for starting (if |is_start| is True) 48 // resp. stopping (if |is_start| is False) the p2p service 49 // e.g. ["initctl", "start", "p2p"] or ["initctl", "stop", "p2p"]. 50 virtual std::vector<std::string> GetInitctlArgs(bool is_start) = 0; 51 52 // Gets the argument vector for invoking p2p-client, e.g. 53 // "p2p-client --get-url=file_id_we_want --minimum-size=42123". 54 virtual std::vector<std::string> GetP2PClientArgs( 55 const std::string& file_id, size_t minimum_size) = 0; 56 }; 57 ~P2PManager()58 virtual ~P2PManager() {} 59 60 // The type for the callback used in LookupUrlForFile(). 61 // If the lookup failed, |url| is empty. 62 typedef base::Callback<void(const std::string& url)> LookupCallback; 63 64 // Use the device policy specified by |device_policy|. If this is 65 // null, then no device policy is used. 66 virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy) = 0; 67 68 // Returns true iff P2P is currently allowed for use on this device. This 69 // value is determined and maintained by the Update Manager. 70 virtual bool IsP2PEnabled() = 0; 71 72 // Ensures that the p2p subsystem is running (e.g. starts it if it's 73 // not already running) and blocks until this is so. Returns false 74 // if an error occurred. 75 virtual bool EnsureP2PRunning() = 0; 76 77 // Ensures that the p2p subsystem is not running (e.g. stops it if 78 // it's running) and blocks until this is so. Returns false if an 79 // error occurred. 80 virtual bool EnsureP2PNotRunning() = 0; 81 82 // Cleans up files in /var/cache/p2p owned by this application as 83 // per the |file_extension| and |num_files_to_keep| values passed 84 // when the object was constructed. This may be called even if 85 // the p2p subsystem is not running. 86 virtual bool PerformHousekeeping() = 0; 87 88 // Asynchronously finds a peer that serves the file identified by 89 // |file_id|. If |minimum_size| is non-zero, will find a peer that 90 // has at least that many bytes. When the result is ready |callback| 91 // is called from the current message loop. 92 // 93 // This operation may take a very long time to complete because part 94 // of the p2p protocol involves waiting for the LAN-wide sum of all 95 // num-connections to drop below a given threshold. However, if 96 // |max_time_to_wait| is non-zero, the operation is guaranteed to 97 // not exceed this duration. 98 // 99 // If the file is not available on the LAN (or if mDNS/DNS-SD is 100 // filtered), this is guaranteed to not take longer than 5 seconds. 101 virtual void LookupUrlForFile(const std::string& file_id, 102 size_t minimum_size, 103 base::TimeDelta max_time_to_wait, 104 LookupCallback callback) = 0; 105 106 // Shares a file identified by |file_id| in the directory 107 // /var/cache/p2p. Initially the file will not be visible, that is, 108 // it will have a .tmp extension and not be shared via p2p. Use the 109 // FileMakeVisible() method to change this. 110 // 111 // If you know the final size of the file, pass it in the 112 // |expected_size| parameter. Otherwise pass zero. If non-zero, the 113 // amount of free space in /var/cache/p2p is checked and if there is 114 // less than twice the amount of space available, this method 115 // fails. Additionally, disk space will be reserved via fallocate(2) 116 // and |expected_size| is written to the user.cros-p2p-filesize 117 // xattr of the created file. 118 // 119 // If the file already exists, true is returned. Any on-disk xattr 120 // is not updated. 121 virtual bool FileShare(const std::string& file_id, size_t expected_size) = 0; 122 123 // Gets a fully qualified path for the file identified by |file_id|. 124 // If the file has not been shared already using the FileShare() 125 // method, an empty base::FilePath is returned - use FilePath::empty() to 126 // find out. 127 virtual base::FilePath FileGetPath(const std::string& file_id) = 0; 128 129 // Gets the actual size of the file identified by |file_id|. This is 130 // equivalent to reading the value of the st_size field of the 131 // struct stat on the file given by FileGetPath(). Returns -1 if an 132 // error occurs. 133 // 134 // For a file just created with FileShare() this will return 0. 135 virtual ssize_t FileGetSize(const std::string& file_id) = 0; 136 137 // Gets the expected size of the file identified by |file_id|. This 138 // is equivalent to reading the value of the user.cros-p2p-filesize 139 // xattr on the file given by FileGetPath(). Returns -1 if an error 140 // occurs. 141 // 142 // For a file just created with FileShare() this will return the 143 // value of the |expected_size| parameter passed to that method. 144 virtual ssize_t FileGetExpectedSize(const std::string& file_id) = 0; 145 146 // Gets whether the file identified by |file_id| is publicly 147 // visible. If |out_result| is not null, the result is returned 148 // there. Returns false if an error occurs. 149 virtual bool FileGetVisible(const std::string& file_id, bool* out_result) = 0; 150 151 // Makes the file identified by |file_id| publicly visible 152 // (e.g. removes the .tmp extension). If the file is already 153 // visible, this method does nothing. Returns False if 154 // the method fails or there is no file for |file_id|. 155 virtual bool FileMakeVisible(const std::string& file_id) = 0; 156 157 // Counts the number of shared files used by this application 158 // (cf. the |file_extension parameter|. Returns -1 if an error 159 // occurred. 160 virtual int CountSharedFiles() = 0; 161 162 // Creates a suitable P2PManager instance and initializes the object 163 // so it's ready for use. The |file_extension| parameter is used to 164 // identify your application, use e.g. "cros_au". If 165 // |configuration| is non-null, the P2PManager will take ownership 166 // of the Configuration object and use it (hence, it must be 167 // heap-allocated). 168 // 169 // The |num_files_to_keep| parameter specifies how many files to 170 // keep after performing housekeeping (cf. the PerformHousekeeping() 171 // method) - pass zero to allow infinitely many files. The 172 // |max_file_age| parameter specifies the maximum file age after 173 // performing housekeeping (pass zero to allow files of any age). 174 static P2PManager* Construct( 175 Configuration* configuration, 176 chromeos_update_manager::UpdateManager* update_manager, 177 const std::string& file_extension, 178 const int num_files_to_keep, 179 const base::TimeDelta& max_file_age); 180 }; 181 182 } // namespace chromeos_update_engine 183 184 #endif // UPDATE_ENGINE_CROS_P2P_MANAGER_H_ 185