Index: chromecast/base/file_utils.cc |
diff --git a/chromecast/base/file_utils.cc b/chromecast/base/file_utils.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6b71a5594e93f45569044180641a5139c6a26edc |
--- /dev/null |
+++ b/chromecast/base/file_utils.cc |
@@ -0,0 +1,37 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromecast/base/file_utils.h" |
+ |
+#include <errno.h> |
+#include <fcntl.h> |
+#include <sys/file.h> |
+ |
+namespace { |
+ |
+bool AttemptLockOnFileWithFlag(const char* file, int flag) { |
+ int fd; |
+ if ((fd = open(file, O_RDONLY)) < 0) { |
+ LOG(ERROR) << "Error finding " << file << " error = " << strerror(errno); |
bcf
2016/08/03 02:40:23
Error opening . open may fail for other reasons to
ameyak
2016/08/03 18:50:32
Done.
|
+ return false; |
+ } |
+ |
+ flag = flock(fd, flag); |
bcf
2016/08/03 02:40:23
Handle EINTR with TEMP_FAILURE_RETRY https://www.g
ameyak
2016/08/03 18:50:32
Done.
|
+ close(fd); |
+ return !flag; |
+} |
+ |
+} // namespace |
+ |
+namespace chromecast { |
+ |
+bool LockFile(const base::FilePath& path) { |
+ return AttemptLockOnFileWithFlag(path.value().c_str(), LOCK_EX); |
+} |
+ |
+bool UnlockFile(const base::FilePath& path) { |
+ return AttemptLockOnFileWithFlag(path.value().c_str(), LOCK_UN); |
+} |
+ |
+} // namspace chromecast |