Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_ |
| 18 | #define ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_ |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 19 | |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 23 | #include <android-base/unique_fd.h> |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 24 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 25 | #include "macros.h" |
| 26 | #include "os.h" |
| 27 | #include "unix_file/fd_file.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 31 | class LockedFile; |
| 32 | class LockedFileCloseNoFlush; |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 33 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 34 | // A scoped File object that calls Close without flushing. |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 35 | using ScopedFlock = std::unique_ptr<LockedFile, LockedFileCloseNoFlush>; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 36 | |
| 37 | class LockedFile : public unix_file::FdFile { |
| 38 | public: |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 39 | // Attempts to acquire an exclusive file lock (see flock(2)) on the file |
| 40 | // at filename, and blocks until it can do so. |
| 41 | // |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 42 | // It is an error if its inode changed (usually due to a new file being |
| 43 | // created at the same path) between attempts to lock it. In blocking mode, |
| 44 | // locking will be retried if the file changed. In non-blocking mode, false |
| 45 | // is returned and no attempt is made to re-acquire the lock. |
| 46 | // |
| 47 | // The file is opened with the provided flags. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 48 | static ScopedFlock Open(const char* filename, int flags, bool block, |
| 49 | std::string* error_msg); |
| 50 | |
| 51 | // Calls Open(filename, O_CREAT | O_RDWR, true, errror_msg) |
| 52 | static ScopedFlock Open(const char* filename, std::string* error_msg); |
| 53 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 54 | // Attempt to acquire an exclusive file lock (see flock(2)) on 'file'. |
| 55 | // Returns true if the lock could be acquired or false if an error |
| 56 | // occured. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 57 | static ScopedFlock DupOf(const int fd, const std::string& path, |
| 58 | const bool read_only_mode, std::string* error_message); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 59 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 60 | // Release a lock held on this file, if any. |
| 61 | void ReleaseLock(); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 62 | |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 63 | private: |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 64 | // Constructors should not be invoked directly, use one of the factory |
| 65 | // methods instead. |
| 66 | explicit LockedFile(FdFile&& other) : FdFile(std::move(other)) { |
| 67 | } |
| 68 | |
| 69 | // Constructors should not be invoked directly, use one of the factory |
| 70 | // methods instead. |
| 71 | LockedFile(int fd, const std::string& path, bool check_usage, bool read_only_mode) |
| 72 | : FdFile(fd, path, check_usage, read_only_mode) { |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | class LockedFileCloseNoFlush { |
| 77 | public: |
| 78 | void operator()(LockedFile* ptr) { |
| 79 | ptr->ReleaseLock(); |
| 80 | UNUSED(ptr->Close()); |
| 81 | |
| 82 | delete ptr; |
| 83 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace art |
| 87 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 88 | #endif // ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_ |