blob: 525332a657dd06a9b3d29f5bf7b952231d793b9c [file] [log] [blame]
Narayan Kamathd1c606f2014-06-09 16:50:19 +01001/*
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 Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_
18#define ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_
Narayan Kamathd1c606f2014-06-09 16:50:19 +010019
20#include <memory>
21#include <string>
22
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/unique_fd.h>
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010024
David Sehr1979c642018-04-26 14:41:18 -070025#include "macros.h"
26#include "os.h"
27#include "unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010028
29namespace art {
30
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010031class LockedFile;
32class LockedFileCloseNoFlush;
Narayan Kamathd1c606f2014-06-09 16:50:19 +010033
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010034// A scoped File object that calls Close without flushing.
Vladimir Marko4f990712021-07-14 12:45:13 +010035using ScopedFlock = std::unique_ptr<LockedFile, LockedFileCloseNoFlush>;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010036
37class LockedFile : public unix_file::FdFile {
38 public:
Narayan Kamathd1c606f2014-06-09 16:50:19 +010039 // 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 Juravle877fd962016-01-05 14:29:29 +000042 // 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 Kamatha3d27eb2017-05-11 13:50:59 +010048 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 Lighta59dd802014-07-02 16:28:08 -070054 // 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 Kamatha3d27eb2017-05-11 13:50:59 +010057 static ScopedFlock DupOf(const int fd, const std::string& path,
58 const bool read_only_mode, std::string* error_message);
Narayan Kamathd1c606f2014-06-09 16:50:19 +010059
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010060 // Release a lock held on this file, if any.
61 void ReleaseLock();
Alex Lighta59dd802014-07-02 16:28:08 -070062
Narayan Kamathd1c606f2014-06-09 16:50:19 +010063 private:
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010064 // 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
76class LockedFileCloseNoFlush {
77 public:
78 void operator()(LockedFile* ptr) {
79 ptr->ReleaseLock();
80 UNUSED(ptr->Close());
81
82 delete ptr;
83 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010084};
85
86} // namespace art
87
David Sehrc431b9d2018-03-02 12:01:51 -080088#endif // ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_