ScopedFlock: Refactor it to be a subclass of FdFile.
Makes callers cleaner, since they only have to worry about
dealing with regular File objects that they know will be locked
for the duration of their existence. Prevents issues and other
clunky code relating to acquire
Test: scoped_flock_test, test_art_host
Bug: 36369345
Change-Id: I2c9644e448acde6ddac472d88108c7d9a4e1a892
diff --git a/profman/profile_assistant.cc b/profman/profile_assistant.cc
index b9a85bc..c238f0d 100644
--- a/profman/profile_assistant.cc
+++ b/profman/profile_assistant.cc
@@ -33,7 +33,7 @@
ProfileCompilationInfo info;
// Load the reference profile.
- if (!info.Load(reference_profile_file.GetFile()->Fd())) {
+ if (!info.Load(reference_profile_file->Fd())) {
LOG(WARNING) << "Could not load reference profile file";
return kErrorBadProfiles;
}
@@ -45,7 +45,7 @@
// Merge all current profiles.
for (size_t i = 0; i < profile_files.size(); i++) {
ProfileCompilationInfo cur_info;
- if (!cur_info.Load(profile_files[i].GetFile()->Fd())) {
+ if (!cur_info.Load(profile_files[i]->Fd())) {
LOG(WARNING) << "Could not load profile file at index " << i;
return kErrorBadProfiles;
}
@@ -62,11 +62,11 @@
}
// We were successful in merging all profile information. Update the reference profile.
- if (!reference_profile_file.GetFile()->ClearContent()) {
+ if (!reference_profile_file->ClearContent()) {
PLOG(WARNING) << "Could not clear reference profile file";
return kErrorIO;
}
- if (!info.Save(reference_profile_file.GetFile()->Fd())) {
+ if (!info.Save(reference_profile_file->Fd())) {
LOG(WARNING) << "Could not save reference profile file";
return kErrorIO;
}
@@ -74,26 +74,15 @@
return kCompile;
}
-static bool InitFlock(const std::string& filename, ScopedFlock& flock, std::string* error) {
- return flock.Init(filename.c_str(), O_RDWR, /* block */ true, error);
-}
-
-static bool InitFlock(int fd, ScopedFlock& flock, std::string* error) {
- DCHECK_GE(fd, 0);
- // We do not own the descriptor, so disable auto-close and don't check usage.
- File file(fd, false);
- file.DisableAutoClose();
- return flock.Init(&file, error);
-}
-
-class ScopedCollectionFlock {
+class ScopedFlockList {
public:
- explicit ScopedCollectionFlock(size_t size) : flocks_(size) {}
+ explicit ScopedFlockList(size_t size) : flocks_(size) {}
// Will block until all the locks are acquired.
bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
for (size_t i = 0; i < filenames.size(); i++) {
- if (!InitFlock(filenames[i], flocks_[i], error)) {
+ flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block */ true, error);
+ if (flocks_[i].get() == nullptr) {
*error += " (index=" + std::to_string(i) + ")";
return false;
}
@@ -105,7 +94,9 @@
bool Init(const std::vector<int>& fds, /* out */ std::string* error) {
for (size_t i = 0; i < fds.size(); i++) {
DCHECK_GE(fds[i], 0);
- if (!InitFlock(fds[i], flocks_[i], error)) {
+ flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
+ true /* read_only_mode */, error);
+ if (flocks_[i].get() == nullptr) {
*error += " (index=" + std::to_string(i) + ")";
return false;
}
@@ -123,39 +114,47 @@
const std::vector<int>& profile_files_fd,
int reference_profile_file_fd) {
DCHECK_GE(reference_profile_file_fd, 0);
+
std::string error;
- ScopedCollectionFlock profile_files_flocks(profile_files_fd.size());
- if (!profile_files_flocks.Init(profile_files_fd, &error)) {
+ ScopedFlockList profile_files(profile_files_fd.size());
+ if (!profile_files.Init(profile_files_fd, &error)) {
LOG(WARNING) << "Could not lock profile files: " << error;
return kErrorCannotLock;
}
- ScopedFlock reference_profile_file_flock;
- if (!InitFlock(reference_profile_file_fd, reference_profile_file_flock, &error)) {
+
+ // The reference_profile_file is opened in read/write mode because it's
+ // cleared after processing.
+ ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd,
+ "reference-profile",
+ false /* read_only_mode */,
+ &error);
+ if (reference_profile_file.get() == nullptr) {
LOG(WARNING) << "Could not lock reference profiled files: " << error;
return kErrorCannotLock;
}
- return ProcessProfilesInternal(profile_files_flocks.Get(),
- reference_profile_file_flock);
+ return ProcessProfilesInternal(profile_files.Get(), reference_profile_file);
}
ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
const std::vector<std::string>& profile_files,
const std::string& reference_profile_file) {
std::string error;
- ScopedCollectionFlock profile_files_flocks(profile_files.size());
- if (!profile_files_flocks.Init(profile_files, &error)) {
+
+ ScopedFlockList profile_files_list(profile_files.size());
+ if (!profile_files_list.Init(profile_files, &error)) {
LOG(WARNING) << "Could not lock profile files: " << error;
return kErrorCannotLock;
}
- ScopedFlock reference_profile_file_flock;
- if (!InitFlock(reference_profile_file, reference_profile_file_flock, &error)) {
+
+ ScopedFlock locked_reference_profile_file = LockedFile::Open(
+ reference_profile_file.c_str(), O_RDWR, /* block */ true, &error);
+ if (locked_reference_profile_file.get() == nullptr) {
LOG(WARNING) << "Could not lock reference profile files: " << error;
return kErrorCannotLock;
}
- return ProcessProfilesInternal(profile_files_flocks.Get(),
- reference_profile_file_flock);
+ return ProcessProfilesInternal(profile_files_list.Get(), locked_reference_profile_file);
}
} // namespace art