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..acf9b954a4cee2253d19689b943db98d430c57be |
--- /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 { |
+ |
+bool CallFlockOnFileWithFlag(const int fd, int flag) { |
slan
2016/08/08 16:37:10
nit: Add a comment explaining retval.
ameyak
2016/08/08 18:00:43
Done.
|
+ int ret; |
slan
2016/08/08 16:37:10
nit: init this.
ameyak
2016/08/08 18:00:43
Done.
|
+ if ((ret = TEMP_FAILURE_RETRY(flock(fd, flag))) < 0) { |
+ LOG(ERROR) << "Error locking " << fd << " error = " << strerror(errno); |
slan
2016/08/08 16:37:10
PLOG would be appropriate here:
https://cs.chromi
ameyak
2016/08/08 18:00:43
Done.
|
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+namespace chromecast { |
+ |
+int OpenAndLockFile(const base::FilePath& path) { |
+ int fd; |
slan
2016/08/08 16:37:10
nit: init this.
ameyak
2016/08/08 18:00:43
Done.
|
+ const char* file = path.value().c_str(); |
+ |
+ if ((fd = open(file, O_RDONLY)) < 0) { |
slan
2016/08/08 16:37:10
The comment on this function doesn't indicate that
ameyak
2016/08/08 18:00:43
Done.
|
+ LOG(ERROR) << "Error opening " << file << " error = " << strerror(errno); |
slan
2016/08/08 16:37:10
nit: PLOG
ameyak
2016/08/08 18:00:43
Done.
|
+ } 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 |