2020#include < cstddef>
2121#include < fstream>
2222#include < map>
23+ #include < memory>
24+ #include < mutex>
2325#include < ostream>
2426#include < string>
2527#include < string_view>
@@ -68,7 +70,23 @@ std::ostream& operator<<(std::ostream& os, const CvdFile& cvd_file) {
6870 return os;
6971}
7072
73+ FetcherConfig::FetcherConfig () : mutex_(std::make_unique<std::mutex>()) {}
74+
75+ FetcherConfig::FetcherConfig (FetcherConfig&& other) {
76+ mutex_ = std::move (other.mutex_ );
77+ other.mutex_ = std::make_unique<std::mutex>();
78+ }
79+
80+ FetcherConfig& FetcherConfig::operator =(FetcherConfig&& other) {
81+ mutex_ = std::move (other.mutex_ );
82+ other.mutex_ = std::make_unique<std::mutex>();
83+
84+ return *this ;
85+ }
86+
7187bool FetcherConfig::SaveToFile (const std::string& file) const {
88+ std::scoped_lock lock (*mutex_);
89+
7290 std::ofstream ofs (file);
7391 if (!ofs.is_open ()) {
7492 LOG (ERROR) << " Unable to write to file " << file;
@@ -79,6 +97,8 @@ bool FetcherConfig::SaveToFile(const std::string& file) const {
7997}
8098
8199bool FetcherConfig::LoadFromFile (const std::string& file) {
100+ std::scoped_lock lock (*mutex_);
101+
82102 auto real_file_path = AbsolutePath (file);
83103 if (real_file_path.empty ()) {
84104 LOG (ERROR) << " Could not get real path for file " << file;
@@ -135,6 +155,8 @@ Json::Value CvdFileToJson(const CvdFile& cvd_file) {
135155} // namespace
136156
137157bool FetcherConfig::add_cvd_file (const CvdFile& file, bool override_entry) {
158+ std::scoped_lock lock (*mutex_);
159+
138160 if (!dictionary_.isMember (kCvdFiles )) {
139161 Json::Value files_json (Json::objectValue);
140162 dictionary_[kCvdFiles ] = files_json;
@@ -147,6 +169,8 @@ bool FetcherConfig::add_cvd_file(const CvdFile& file, bool override_entry) {
147169}
148170
149171std::map<std::string, CvdFile> FetcherConfig::get_cvd_files () const {
172+ std::scoped_lock lock (*mutex_);
173+
150174 if (!dictionary_.isMember (kCvdFiles )) {
151175 return {};
152176 }
@@ -160,6 +184,8 @@ std::map<std::string, CvdFile> FetcherConfig::get_cvd_files() const {
160184
161185std::string FetcherConfig::FindCvdFileWithSuffix (
162186 const std::string& suffix) const {
187+ std::scoped_lock lock (*mutex_);
188+
163189 if (!dictionary_.isMember (kCvdFiles )) {
164190 return {};
165191 }
@@ -186,6 +212,9 @@ Result<void> FetcherConfig::AddFilesToConfig(
186212 FileSource purpose, const std::string& build_id,
187213 const std::string& build_target, const std::vector<std::string>& paths,
188214 const std::string& directory_prefix, bool override_entry) {
215+ // This neither acesses dictionary_ directly or locks *mutex, but it calls
216+ // `add_cvd_file` which does.
217+
189218 for (const std::string& path : paths) {
190219 std::string_view local_path (path);
191220 if (!android::base::ConsumePrefix (&local_path, directory_prefix)) {
@@ -208,6 +237,8 @@ Result<void> FetcherConfig::AddFilesToConfig(
208237}
209238
210239Result<void > FetcherConfig::RemoveFileFromConfig (const std::string& path) {
240+ std::scoped_lock lock (*mutex_);
241+
211242 if (!dictionary_.isMember (kCvdFiles )) {
212243 return {};
213244 }
0 commit comments