OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromecast/base/file_utils.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <sys/file.h> |
| 10 |
| 11 namespace { |
| 12 |
| 13 bool AttemptLockOnFileWithFlag(const base::FilePath& path, int flag) { |
| 14 int fd, ret; |
| 15 const char* file = path.value().c_str(); |
| 16 |
| 17 if ((fd = open(file, O_RDONLY)) < 0) { |
| 18 LOG(ERROR) << "Error opening " << file << " error = " << strerror(errno); |
| 19 return false; |
| 20 } |
| 21 if ((ret = TEMP_FAILURE_RETRY(flock(fd, flag))) < 0) |
| 22 LOG(ERROR) << "Error locking " << file << " error = " << strerror(errno); |
| 23 |
| 24 close(fd); |
| 25 return !ret; |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 namespace chromecast { |
| 31 |
| 32 bool LockFile(const base::FilePath& path) { |
| 33 return AttemptLockOnFileWithFlag(path, LOCK_EX); |
| 34 } |
| 35 |
| 36 bool UnlockFile(const base::FilePath& path) { |
| 37 return AttemptLockOnFileWithFlag(path, LOCK_UN); |
| 38 } |
| 39 |
| 40 } // namspace chromecast |
OLD | NEW |