blob: 1b933c07f30a2e84869e5ef64db892551d548db5 [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
17#ifndef ART_RUNTIME_BASE_SCOPED_FLOCK_H_
18#define ART_RUNTIME_BASE_SCOPED_FLOCK_H_
19
20#include <memory>
21#include <string>
22
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010023#include "android-base/unique_fd.h"
24
25#include "base/logging.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010026#include "base/macros.h"
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010027#include "base/unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010028#include "os.h"
29
30namespace art {
31
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010032class LockedFile;
33class LockedFileCloseNoFlush;
Narayan Kamathd1c606f2014-06-09 16:50:19 +010034
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010035// A scoped File object that calls Close without flushing.
36typedef std::unique_ptr<LockedFile, LockedFileCloseNoFlush> ScopedFlock;
37
38class LockedFile : public unix_file::FdFile {
39 public:
Narayan Kamathd1c606f2014-06-09 16:50:19 +010040 // Attempts to acquire an exclusive file lock (see flock(2)) on the file
41 // at filename, and blocks until it can do so.
42 //
Calin Juravle877fd962016-01-05 14:29:29 +000043 // It is an error if its inode changed (usually due to a new file being
44 // created at the same path) between attempts to lock it. In blocking mode,
45 // locking will be retried if the file changed. In non-blocking mode, false
46 // is returned and no attempt is made to re-acquire the lock.
47 //
48 // The file is opened with the provided flags.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010049 static ScopedFlock Open(const char* filename, int flags, bool block,
50 std::string* error_msg);
51
52 // Calls Open(filename, O_CREAT | O_RDWR, true, errror_msg)
53 static ScopedFlock Open(const char* filename, std::string* error_msg);
54
Alex Lighta59dd802014-07-02 16:28:08 -070055 // Attempt to acquire an exclusive file lock (see flock(2)) on 'file'.
56 // Returns true if the lock could be acquired or false if an error
57 // occured.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010058 static ScopedFlock DupOf(const int fd, const std::string& path,
59 const bool read_only_mode, std::string* error_message);
Narayan Kamathd1c606f2014-06-09 16:50:19 +010060
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010061 // Release a lock held on this file, if any.
62 void ReleaseLock();
Alex Lighta59dd802014-07-02 16:28:08 -070063
Narayan Kamathd1c606f2014-06-09 16:50:19 +010064 private:
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010065 // Constructors should not be invoked directly, use one of the factory
66 // methods instead.
67 explicit LockedFile(FdFile&& other) : FdFile(std::move(other)) {
68 }
69
70 // Constructors should not be invoked directly, use one of the factory
71 // methods instead.
72 LockedFile(int fd, const std::string& path, bool check_usage, bool read_only_mode)
73 : FdFile(fd, path, check_usage, read_only_mode) {
74 }
75};
76
77class LockedFileCloseNoFlush {
78 public:
79 void operator()(LockedFile* ptr) {
80 ptr->ReleaseLock();
81 UNUSED(ptr->Close());
82
83 delete ptr;
84 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010085};
86
87} // namespace art
88
89#endif // ART_RUNTIME_BASE_SCOPED_FLOCK_H_