| Dmitrii Merkurev | 9129717 | 2023-02-09 02:08:14 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2023 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 | #include <android-base/file.h> | 
|  | 18 | #include <android-base/logging.h> | 
|  | 19 |  | 
|  | 20 | #include <fstream> | 
|  | 21 |  | 
|  | 22 | #include "storage.h" | 
|  | 23 | #include "util.h" | 
|  | 24 |  | 
|  | 25 | ConnectedDevicesStorage::ConnectedDevicesStorage() { | 
|  | 26 | const std::string home_path = GetHomeDirPath(); | 
|  | 27 | if (home_path.empty()) { | 
|  | 28 | return; | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | const std::string home_fastboot_path = home_path + kPathSeparator + ".fastboot"; | 
|  | 32 |  | 
|  | 33 | if (!EnsureDirectoryExists(home_fastboot_path)) { | 
|  | 34 | LOG(FATAL) << "Cannot create directory: " << home_fastboot_path; | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | // We're using a separate file for locking because the Windows LockFileEx does not | 
|  | 38 | // permit opening a file stream for the locked file, even within the same process. So, | 
|  | 39 | // we have to use fd or handle API to manipulate the storage files, which makes it | 
|  | 40 | // nearly impossible to fully rewrite a file content without having to recreate it. | 
|  | 41 | // Unfortunately, this is not an option during holding a lock. | 
|  | 42 | devices_path_ = home_fastboot_path + kPathSeparator + "devices"; | 
|  | 43 | devices_lock_path_ = home_fastboot_path + kPathSeparator + "devices.lock"; | 
|  | 44 | } | 
|  | 45 |  | 
| Dmitrii Merkurev | 0ee285a | 2023-02-11 00:02:22 +0000 | [diff] [blame] | 46 | void ConnectedDevicesStorage::WriteDevices(const FileLock&, const std::set<std::string>& devices) { | 
| Dmitrii Merkurev | 9129717 | 2023-02-09 02:08:14 +0000 | [diff] [blame] | 47 | std::ofstream devices_stream(devices_path_); | 
|  | 48 | std::copy(devices.begin(), devices.end(), | 
|  | 49 | std::ostream_iterator<std::string>(devices_stream, "\n")); | 
|  | 50 | } | 
|  | 51 |  | 
| Dmitrii Merkurev | 0ee285a | 2023-02-11 00:02:22 +0000 | [diff] [blame] | 52 | std::set<std::string> ConnectedDevicesStorage::ReadDevices(const FileLock&) { | 
| Dmitrii Merkurev | 9129717 | 2023-02-09 02:08:14 +0000 | [diff] [blame] | 53 | std::ifstream devices_stream(devices_path_); | 
|  | 54 | std::istream_iterator<std::string> start(devices_stream), end; | 
|  | 55 | std::set<std::string> devices(start, end); | 
|  | 56 | return devices; | 
|  | 57 | } | 
|  | 58 |  | 
| Dmitrii Merkurev | 0ee285a | 2023-02-11 00:02:22 +0000 | [diff] [blame] | 59 | void ConnectedDevicesStorage::Clear(const FileLock&) { | 
| Dmitrii Merkurev | 9129717 | 2023-02-09 02:08:14 +0000 | [diff] [blame] | 60 | if (!android::base::RemoveFileIfExists(devices_path_)) { | 
|  | 61 | LOG(FATAL) << "Failed to clear connected device list: " << devices_path_; | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | FileLock ConnectedDevicesStorage::Lock() const { | 
|  | 66 | return FileLock(devices_lock_path_); | 
|  | 67 | } |