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 
17 #ifndef ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_
18 #define ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include <android-base/unique_fd.h>
24 
25 #include "macros.h"
26 #include "os.h"
27 #include "unix_file/fd_file.h"
28 
29 namespace art {
30 
31 class LockedFile;
32 class LockedFileCloseNoFlush;
33 
34 // A scoped File object that calls Close without flushing.
35 typedef std::unique_ptr<LockedFile, LockedFileCloseNoFlush> ScopedFlock;
36 
37 class LockedFile : public unix_file::FdFile {
38  public:
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   //
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.
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 
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.
57   static ScopedFlock DupOf(const int fd, const std::string& path,
58                            const bool read_only_mode, std::string* error_message);
59 
60   // Release a lock held on this file, if any.
61   void ReleaseLock();
62 
63  private:
64   // Constructors should not be invoked directly, use one of the factory
65   // methods instead.
LockedFile(FdFile && other)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.
LockedFile(int fd,const std::string & path,bool check_usage,bool read_only_mode)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:
operator()78   void operator()(LockedFile* ptr) {
79     ptr->ReleaseLock();
80     UNUSED(ptr->Close());
81 
82     delete ptr;
83   }
84 };
85 
86 }  // namespace art
87 
88 #endif  // ART_LIBARTBASE_BASE_SCOPED_FLOCK_H_
89