Android-cuttlefish cvd tool
lock_file.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 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#pragma once
17
18#include <memory>
19#include <optional>
20#include <string>
21
24
25namespace cuttlefish {
26
27enum class InUseState : char {
28 kInUse = 'I',
29 kNotInUse = 'N',
30};
31
32// Replicates tempfile.gettempdir() in Python
33std::string TempDir();
34
35// This class is not thread safe.
36class LockFile {
37 friend class LockFileManager;
38
39 /* b/303724170
40 *
41 * Unfortunately, the LockFile implementation forgot to release
42 * the Flock() it got on destruction. Also, unfortunately, the
43 * LockFile is being copied as we have used std::optional
44 * here and there, which means LockFile objects are copied.
45 *
46 * The goal is to disable LockFile(const LockFile&) and the
47 * copy assignment operator. For now, however, we add to LockFile
48 * a shared_ptr of this class, so that at the very last LockFile
49 * object for a given lock is destroyed, the LockFileReleaser will
50 * releases the lock.
51 *
52 * Must be created only in the LockFile constructors
53 */
55 public:
56 LockFileReleaser(const SharedFD& fd, const std::string& lock_file_path);
58
59 private:
61 const std::string lock_file_path_; // for error message
62 };
63
64 public:
65 const auto& LockFilePath() const { return lock_file_path_; }
68
69 // to put this into a set
70 bool operator<(const LockFile& other) const;
71
72 private:
73 LockFile(SharedFD fd, const std::string& lock_file_path);
74
76 const std::string lock_file_path_;
77 std::shared_ptr<LockFileReleaser> lock_file_lock_releaser_;
78};
79
81 public:
82 LockFileManager() = default;
83
84 Result<LockFile> AcquireLock(const std::string& lock_file_path);
85
87 const std::string& lock_file_path);
88
89 static Result<SharedFD> OpenLockFile(const std::string& file_path);
90};
91
92} // namespace cuttlefish
Definition: lock_file.h:80
Result< std::optional< LockFile > > TryAcquireLock(const std::string &lock_file_path)
Definition: lock_file.cpp:125
Result< LockFile > AcquireLock(const std::string &lock_file_path)
Definition: lock_file.cpp:118
static Result< SharedFD > OpenLockFile(const std::string &file_path)
Definition: lock_file.cpp:103
Definition: lock_file.h:54
const std::string lock_file_path_
Definition: lock_file.h:61
LockFileReleaser(const SharedFD &fd, const std::string &lock_file_path)
Definition: lock_file.cpp:48
~LockFileReleaser()
Definition: lock_file.cpp:52
SharedFD flocked_file_fd_
Definition: lock_file.h:60
Definition: lock_file.h:36
const auto & LockFilePath() const
Definition: lock_file.h:65
const std::string lock_file_path_
Definition: lock_file.h:76
LockFile(SharedFD fd, const std::string &lock_file_path)
Definition: lock_file.cpp:65
Result< InUseState > Status() const
Definition: lock_file.cpp:71
bool operator<(const LockFile &other) const
Definition: lock_file.cpp:90
std::shared_ptr< LockFileReleaser > lock_file_lock_releaser_
Definition: lock_file.h:77
SharedFD fd_
Definition: lock_file.h:75
Definition: shared_fd.h:124
Definition: alloc_driver.h:20
InUseState
Definition: lock_file.h:27
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
std::string TempDir()
Definition: known_paths.cpp:25