blob: 629ebc88eff71bb8864c468b8f1ff6ffbb80ade9 [file] [log] [blame]
Dmitrii Merkurev91297172023-02-09 02:08:14 +00001/*
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>
Biswapriyo Nath816480b2023-06-13 00:28:14 +053021#include <iterator>
Dmitrii Merkurev91297172023-02-09 02:08:14 +000022
23#include "storage.h"
24#include "util.h"
25
26ConnectedDevicesStorage::ConnectedDevicesStorage() {
Dmitrii Merkurev6c008ff2023-06-02 12:44:31 +010027 home_fastboot_path_ = GetHomeDirPath() + kPathSeparator + ".fastboot";
28 devices_path_ = home_fastboot_path_ + kPathSeparator + "devices";
Dmitrii Merkurev91297172023-02-09 02:08:14 +000029
30 // We're using a separate file for locking because the Windows LockFileEx does not
31 // permit opening a file stream for the locked file, even within the same process. So,
32 // we have to use fd or handle API to manipulate the storage files, which makes it
33 // nearly impossible to fully rewrite a file content without having to recreate it.
34 // Unfortunately, this is not an option during holding a lock.
Dmitrii Merkurev6c008ff2023-06-02 12:44:31 +010035 devices_lock_path_ = home_fastboot_path_ + kPathSeparator + "devices.lock";
36}
37
38bool ConnectedDevicesStorage::Exists() const {
39 return FileExists(devices_path_);
Dmitrii Merkurev91297172023-02-09 02:08:14 +000040}
41
Dmitrii Merkurev0ee285a2023-02-11 00:02:22 +000042void ConnectedDevicesStorage::WriteDevices(const FileLock&, const std::set<std::string>& devices) {
Dmitrii Merkurev91297172023-02-09 02:08:14 +000043 std::ofstream devices_stream(devices_path_);
44 std::copy(devices.begin(), devices.end(),
45 std::ostream_iterator<std::string>(devices_stream, "\n"));
46}
47
Dmitrii Merkurev0ee285a2023-02-11 00:02:22 +000048std::set<std::string> ConnectedDevicesStorage::ReadDevices(const FileLock&) {
Dmitrii Merkurev91297172023-02-09 02:08:14 +000049 std::ifstream devices_stream(devices_path_);
50 std::istream_iterator<std::string> start(devices_stream), end;
51 std::set<std::string> devices(start, end);
52 return devices;
53}
54
Dmitrii Merkurev0ee285a2023-02-11 00:02:22 +000055void ConnectedDevicesStorage::Clear(const FileLock&) {
Dmitrii Merkurev91297172023-02-09 02:08:14 +000056 if (!android::base::RemoveFileIfExists(devices_path_)) {
57 LOG(FATAL) << "Failed to clear connected device list: " << devices_path_;
58 }
59}
60
61FileLock ConnectedDevicesStorage::Lock() const {
Dmitrii Merkurev6c008ff2023-06-02 12:44:31 +010062 if (!EnsureDirectoryExists(home_fastboot_path_)) {
63 LOG(FATAL) << "Cannot create directory: " << home_fastboot_path_;
64 }
Dmitrii Merkurev91297172023-02-09 02:08:14 +000065 return FileLock(devices_lock_path_);
Biswapriyo Nath816480b2023-06-13 00:28:14 +053066}