Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3990)

Unified Diff: chromecast/base/file_utils.cc

Issue 2203123003: [Chromecast] Remove usage of nonreentrant functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: slan@ comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromecast/base/file_utils.h ('k') | chromecast/crash/linux/crash_testing_utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « chromecast/base/file_utils.h ('k') | chromecast/crash/linux/crash_testing_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698