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

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: bcf@ 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_util.cc » ('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..d15b249c1ab067a424672426d242170e7935d142
--- /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) {
bcf 2016/08/03 20:45:10 nit: pass in const base::FilePath& so you only hav
ameyak 2016/08/04 15:25:30 Done.
+ int fd;
+ if ((fd = open(file, O_RDONLY)) < 0) {
+ LOG(ERROR) << "Error opening " << file << " error = " << strerror(errno);
+ return false;
+ }
+
+ flag = TEMP_FAILURE_RETRY(flock(fd, flag));
bcf 2016/08/03 20:45:10 Should log on error.
bcf 2016/08/03 20:45:10 You shouldn't reuse this variable for another purp
ameyak 2016/08/04 15:25:30 Done.
ameyak 2016/08/04 15:25:30 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
« no previous file with comments | « chromecast/base/file_utils.h ('k') | chromecast/crash/linux/crash_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698