Loading fastboot/fastboot.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -396,7 +396,7 @@ static Transport* NetworkDeviceConnected(bool print = false) { ConnectedDevicesStorage storage; std::set<std::string> devices; { if (storage.Exists()) { FileLock lock = storage.Lock(); devices = storage.ReadDevices(lock); } Loading fastboot/filesystem.h +1 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ const char kPathSeparator = #endif std::string GetHomeDirPath(); bool FileExists(const std::string& path); bool EnsureDirectoryExists(const std::string& directory_path); class FileLock { Loading fastboot/storage.cpp +10 −12 Original line number Diff line number Diff line Loading @@ -23,24 +23,19 @@ #include "util.h" ConnectedDevicesStorage::ConnectedDevicesStorage() { const std::string home_path = GetHomeDirPath(); if (home_path.empty()) { return; } const std::string home_fastboot_path = home_path + kPathSeparator + ".fastboot"; if (!EnsureDirectoryExists(home_fastboot_path)) { LOG(FATAL) << "Cannot create directory: " << home_fastboot_path; } home_fastboot_path_ = GetHomeDirPath() + kPathSeparator + ".fastboot"; devices_path_ = home_fastboot_path_ + kPathSeparator + "devices"; // We're using a separate file for locking because the Windows LockFileEx does not // permit opening a file stream for the locked file, even within the same process. So, // we have to use fd or handle API to manipulate the storage files, which makes it // nearly impossible to fully rewrite a file content without having to recreate it. // Unfortunately, this is not an option during holding a lock. devices_path_ = home_fastboot_path + kPathSeparator + "devices"; devices_lock_path_ = home_fastboot_path + kPathSeparator + "devices.lock"; devices_lock_path_ = home_fastboot_path_ + kPathSeparator + "devices.lock"; } bool ConnectedDevicesStorage::Exists() const { return FileExists(devices_path_); } void ConnectedDevicesStorage::WriteDevices(const FileLock&, const std::set<std::string>& devices) { Loading @@ -63,5 +58,8 @@ void ConnectedDevicesStorage::Clear(const FileLock&) { } FileLock ConnectedDevicesStorage::Lock() const { if (!EnsureDirectoryExists(home_fastboot_path_)) { LOG(FATAL) << "Cannot create directory: " << home_fastboot_path_; } return FileLock(devices_lock_path_); } No newline at end of file fastboot/storage.h +3 −1 Original line number Diff line number Diff line Loading @@ -24,13 +24,15 @@ class ConnectedDevicesStorage { public: ConnectedDevicesStorage(); bool Exists() const; void WriteDevices(const FileLock&, const std::set<std::string>& devices); std::set<std::string> ReadDevices(const FileLock&); void Clear(const FileLock&); FileLock Lock() const; private: std::string home_fastboot_path_; std::string devices_path_; std::string devices_lock_path_; }; No newline at end of file Loading
fastboot/fastboot.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -396,7 +396,7 @@ static Transport* NetworkDeviceConnected(bool print = false) { ConnectedDevicesStorage storage; std::set<std::string> devices; { if (storage.Exists()) { FileLock lock = storage.Lock(); devices = storage.ReadDevices(lock); } Loading
fastboot/filesystem.h +1 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ const char kPathSeparator = #endif std::string GetHomeDirPath(); bool FileExists(const std::string& path); bool EnsureDirectoryExists(const std::string& directory_path); class FileLock { Loading
fastboot/storage.cpp +10 −12 Original line number Diff line number Diff line Loading @@ -23,24 +23,19 @@ #include "util.h" ConnectedDevicesStorage::ConnectedDevicesStorage() { const std::string home_path = GetHomeDirPath(); if (home_path.empty()) { return; } const std::string home_fastboot_path = home_path + kPathSeparator + ".fastboot"; if (!EnsureDirectoryExists(home_fastboot_path)) { LOG(FATAL) << "Cannot create directory: " << home_fastboot_path; } home_fastboot_path_ = GetHomeDirPath() + kPathSeparator + ".fastboot"; devices_path_ = home_fastboot_path_ + kPathSeparator + "devices"; // We're using a separate file for locking because the Windows LockFileEx does not // permit opening a file stream for the locked file, even within the same process. So, // we have to use fd or handle API to manipulate the storage files, which makes it // nearly impossible to fully rewrite a file content without having to recreate it. // Unfortunately, this is not an option during holding a lock. devices_path_ = home_fastboot_path + kPathSeparator + "devices"; devices_lock_path_ = home_fastboot_path + kPathSeparator + "devices.lock"; devices_lock_path_ = home_fastboot_path_ + kPathSeparator + "devices.lock"; } bool ConnectedDevicesStorage::Exists() const { return FileExists(devices_path_); } void ConnectedDevicesStorage::WriteDevices(const FileLock&, const std::set<std::string>& devices) { Loading @@ -63,5 +58,8 @@ void ConnectedDevicesStorage::Clear(const FileLock&) { } FileLock ConnectedDevicesStorage::Lock() const { if (!EnsureDirectoryExists(home_fastboot_path_)) { LOG(FATAL) << "Cannot create directory: " << home_fastboot_path_; } return FileLock(devices_lock_path_); } No newline at end of file
fastboot/storage.h +3 −1 Original line number Diff line number Diff line Loading @@ -24,13 +24,15 @@ class ConnectedDevicesStorage { public: ConnectedDevicesStorage(); bool Exists() const; void WriteDevices(const FileLock&, const std::set<std::string>& devices); std::set<std::string> ReadDevices(const FileLock&); void Clear(const FileLock&); FileLock Lock() const; private: std::string home_fastboot_path_; std::string devices_path_; std::string devices_lock_path_; }; No newline at end of file