blob: dc733b99464ebe4095fab96331f962a72d50a148 [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>
21
22#include "storage.h"
23#include "util.h"
24
25ConnectedDevicesStorage::ConnectedDevicesStorage() {
Dmitrii Merkurev6c008ff2023-06-02 12:44:31 +010026 home_fastboot_path_ = GetHomeDirPath() + kPathSeparator + ".fastboot";
27 devices_path_ = home_fastboot_path_ + kPathSeparator + "devices";
Dmitrii Merkurev91297172023-02-09 02:08:14 +000028
29 // We're using a separate file for locking because the Windows LockFileEx does not
30 // permit opening a file stream for the locked file, even within the same process. So,
31 // we have to use fd or handle API to manipulate the storage files, which makes it
32 // nearly impossible to fully rewrite a file content without having to recreate it.
33 // Unfortunately, this is not an option during holding a lock.
Dmitrii Merkurev6c008ff2023-06-02 12:44:31 +010034 devices_lock_path_ = home_fastboot_path_ + kPathSeparator + "devices.lock";
35}
36
37bool ConnectedDevicesStorage::Exists() const {
38 return FileExists(devices_path_);
Dmitrii Merkurev91297172023-02-09 02:08:14 +000039}
40
Dmitrii Merkurev0ee285a2023-02-11 00:02:22 +000041void ConnectedDevicesStorage::WriteDevices(const FileLock&, const std::set<std::string>& devices) {
Dmitrii Merkurev91297172023-02-09 02:08:14 +000042 std::ofstream devices_stream(devices_path_);
43 std::copy(devices.begin(), devices.end(),
44 std::ostream_iterator<std::string>(devices_stream, "\n"));
45}
46
Dmitrii Merkurev0ee285a2023-02-11 00:02:22 +000047std::set<std::string> ConnectedDevicesStorage::ReadDevices(const FileLock&) {
Dmitrii Merkurev91297172023-02-09 02:08:14 +000048 std::ifstream devices_stream(devices_path_);
49 std::istream_iterator<std::string> start(devices_stream), end;
50 std::set<std::string> devices(start, end);
51 return devices;
52}
53
Dmitrii Merkurev0ee285a2023-02-11 00:02:22 +000054void ConnectedDevicesStorage::Clear(const FileLock&) {
Dmitrii Merkurev91297172023-02-09 02:08:14 +000055 if (!android::base::RemoveFileIfExists(devices_path_)) {
56 LOG(FATAL) << "Failed to clear connected device list: " << devices_path_;
57 }
58}
59
60FileLock ConnectedDevicesStorage::Lock() const {
Dmitrii Merkurev6c008ff2023-06-02 12:44:31 +010061 if (!EnsureDirectoryExists(home_fastboot_path_)) {
62 LOG(FATAL) << "Cannot create directory: " << home_fastboot_path_;
63 }
Dmitrii Merkurev91297172023-02-09 02:08:14 +000064 return FileLock(devices_lock_path_);
65}