• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RUNTIME_BASE_SCOPED_FLOCK_H_
18 #define ART_RUNTIME_BASE_SCOPED_FLOCK_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "android-base/unique_fd.h"
24 
25 #include "base/logging.h"
26 #include "base/macros.h"
27 #include "base/unix_file/fd_file.h"
28 #include "os.h"
29 
30 namespace art {
31 
32 class LockedFile;
33 class LockedFileCloseNoFlush;
34 
35 // A scoped File object that calls Close without flushing.
36 typedef std::unique_ptr<LockedFile, LockedFileCloseNoFlush> ScopedFlock;
37 
38 class LockedFile : public unix_file::FdFile {
39  public:
40   // Attempts to acquire an exclusive file lock (see flock(2)) on the file
41   // at filename, and blocks until it can do so.
42   //
43   // 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.
49   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 
55   // 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.
58   static ScopedFlock DupOf(const int fd, const std::string& path,
59                            const bool read_only_mode, std::string* error_message);
60 
61   // Release a lock held on this file, if any.
62   void ReleaseLock();
63 
64  private:
65   // Constructors should not be invoked directly, use one of the factory
66   // methods instead.
LockedFile(FdFile && other)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.
LockedFile(int fd,const std::string & path,bool check_usage,bool read_only_mode)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 
77 class LockedFileCloseNoFlush {
78  public:
operator()79   void operator()(LockedFile* ptr) {
80     ptr->ReleaseLock();
81     UNUSED(ptr->Close());
82 
83     delete ptr;
84   }
85 };
86 
87 }  // namespace art
88 
89 #endif  // ART_RUNTIME_BASE_SCOPED_FLOCK_H_
90