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..bd3ee2ef5c9bc635d0947c33a125f3f60d762251 |
--- /dev/null |
+++ b/chromecast/base/file_utils.cc |
@@ -0,0 +1,47 @@ |
+// 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 { |
+ |
+// Calls flock on valid file descriptor |fd| with flag |flag|. Returns true |
+// on success, false on failure. |
+bool CallFlockOnFileWithFlag(const int fd, int flag) { |
+ int ret = -1; |
+ if ((ret = TEMP_FAILURE_RETRY(flock(fd, flag))) < 0) |
+ PLOG(ERROR) << "Error locking " << fd; |
+ |
+ return !ret; |
+} |
+ |
+} // namespace |
+ |
+namespace chromecast { |
+ |
+int OpenAndLockFile(const base::FilePath& path, bool write) { |
+ int fd = -1; |
+ const char* file = path.value().c_str(); |
+ |
+ if ((fd = open(file, write ? O_RDWR : O_RDONLY)) < 0) { |
+ PLOG(ERROR) << "Error opening " << file; |
+ } else if (!CallFlockOnFileWithFlag(fd, LOCK_EX)) { |
+ close(fd); |
+ fd = -1; |
+ } |
+ |
+ return fd; |
+} |
+ |
+bool UnlockAndCloseFile(const int fd) { |
+ if (!CallFlockOnFileWithFlag(fd, LOCK_UN)) |
+ return false; |
+ return !close(fd); |
+} |
+ |
+} // namespace chromecast |