| 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..6a278a7ad8ebec51f587ba2e1b00edabfd1aa688
|
| --- /dev/null
|
| +++ b/chromecast/base/file_utils.cc
|
| @@ -0,0 +1,40 @@
|
| +// 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 base::FilePath& path, int flag) {
|
| + int fd, ret;
|
| + const char* file = path.value().c_str();
|
| +
|
| + if ((fd = open(file, O_RDONLY)) < 0) {
|
| + LOG(ERROR) << "Error opening " << file << " error = " << strerror(errno);
|
| + return false;
|
| + }
|
| + if ((ret = TEMP_FAILURE_RETRY(flock(fd, flag))) < 0)
|
| + LOG(ERROR) << "Error locking " << file << " error = " << strerror(errno);
|
| +
|
| + close(fd);
|
| + return !ret;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace chromecast {
|
| +
|
| +bool LockFile(const base::FilePath& path) {
|
| + return AttemptLockOnFileWithFlag(path, LOCK_EX);
|
| +}
|
| +
|
| +bool UnlockFile(const base::FilePath& path) {
|
| + return AttemptLockOnFileWithFlag(path, LOCK_UN);
|
| +}
|
| +
|
| +} // namspace chromecast
|
|
|