Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
* |
4 |
|
|
* Private utility functions for libcvmfs_server |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
#ifndef CVMFS_PUBLISH_REPOSITORY_UTIL_H_ |
8 |
|
|
#define CVMFS_PUBLISH_REPOSITORY_UTIL_H_ |
9 |
|
|
|
10 |
|
|
#include <string> |
11 |
|
|
|
12 |
|
|
#include "crypto/hash.h" |
13 |
|
|
#include "util/single_copy.h" |
14 |
|
|
|
15 |
|
|
namespace publish { |
16 |
|
|
|
17 |
|
|
class CheckoutMarker { |
18 |
|
|
public: |
19 |
|
✗ |
CheckoutMarker(const std::string &t, |
20 |
|
|
const std::string &b, |
21 |
|
|
const shash::Any &h, |
22 |
|
|
const std::string &p) |
23 |
|
✗ |
: tag_(t), branch_(b), hash_(h), previous_branch_(p) { } |
24 |
|
|
|
25 |
|
|
static CheckoutMarker *CreateFrom(const std::string &path); |
26 |
|
|
void SaveAs(const std::string &path) const; |
27 |
|
|
|
28 |
|
✗ |
std::string tag() const { return tag_; } |
29 |
|
✗ |
std::string branch() const { return branch_; } |
30 |
|
✗ |
shash::Any hash() const { return hash_; } |
31 |
|
✗ |
std::string previous_branch() const { return previous_branch_; } |
32 |
|
|
|
33 |
|
|
private: |
34 |
|
|
std::string tag_; |
35 |
|
|
std::string branch_; |
36 |
|
|
shash::Any hash_; |
37 |
|
|
std::string previous_branch_; |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* A server lock file is a POSIX lock file used to obtain mutually |
43 |
|
|
* exclusive access to the repository while conducting an operation |
44 |
|
|
* that modifies the repository state. |
45 |
|
|
* |
46 |
|
|
* Since the lock is implemented using a POSIX lock file, the lock |
47 |
|
|
* will automatically be released when the creating process exits. |
48 |
|
|
*/ |
49 |
|
|
class ServerLockFile { |
50 |
|
|
public: |
51 |
|
✗ |
explicit ServerLockFile(const std::string &path) : path_(path), fd_(-1) { } |
52 |
|
|
|
53 |
|
|
void Lock(); |
54 |
|
|
bool TryLock(); |
55 |
|
|
void Unlock(); |
56 |
|
|
|
57 |
|
✗ |
const std::string &path() const { return path_; } |
58 |
|
|
|
59 |
|
|
private: |
60 |
|
|
std::string path_; |
61 |
|
|
int fd_; |
62 |
|
|
}; |
63 |
|
|
|
64 |
|
|
/** |
65 |
|
|
* RAII try-lock owner for ServerLockFile |
66 |
|
|
* |
67 |
|
|
* TODO(mcb30): C++11 - replace by std::unique_lock<ServerLockFile> |
68 |
|
|
* (with std::try_to_lock constructor) |
69 |
|
|
*/ |
70 |
|
|
class ServerLockFileCheck : SingleCopy { |
71 |
|
|
public: |
72 |
|
✗ |
explicit ServerLockFileCheck(ServerLockFile &lock) : lock_(lock) { |
73 |
|
✗ |
owns_lock_ = lock_.TryLock(); |
74 |
|
|
} |
75 |
|
✗ |
~ServerLockFileCheck() { |
76 |
|
✗ |
if (owns_lock_) |
77 |
|
✗ |
lock_.Unlock(); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
✗ |
const bool owns_lock() const { return owns_lock_; } |
81 |
|
|
|
82 |
|
|
private: |
83 |
|
|
ServerLockFile &lock_; |
84 |
|
|
bool owns_lock_; |
85 |
|
|
}; |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* RAII lock owner for ServerLockFile |
89 |
|
|
* |
90 |
|
|
* TODO(mcb30): C++11 - replace by std::lock_guard<ServerLockFile> |
91 |
|
|
*/ |
92 |
|
|
class ServerLockFileGuard : SingleCopy { |
93 |
|
|
public: |
94 |
|
✗ |
explicit ServerLockFileGuard(ServerLockFile &lock) : lock_(lock) { |
95 |
|
✗ |
lock_.Lock(); |
96 |
|
|
} |
97 |
|
✗ |
~ServerLockFileGuard() { lock_.Unlock(); } |
98 |
|
|
|
99 |
|
|
private: |
100 |
|
|
ServerLockFile &lock_; |
101 |
|
|
}; |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* A server flag file is a file used to indicate a single-bit state |
105 |
|
|
* that extends beyond the lifetime of a process, such as the |
106 |
|
|
* indication that a transaction is open. |
107 |
|
|
*/ |
108 |
|
|
class ServerFlagFile { |
109 |
|
|
public: |
110 |
|
✗ |
explicit ServerFlagFile(const std::string &path) : path_(path) { } |
111 |
|
|
|
112 |
|
|
void Set(); |
113 |
|
|
void Clear(); |
114 |
|
|
bool IsSet() const; |
115 |
|
|
|
116 |
|
|
const std::string &path() const { return path_; } |
117 |
|
|
|
118 |
|
|
private: |
119 |
|
|
std::string path_; |
120 |
|
|
}; |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Callout to cvmfs_suid_helper $verb $fqrn |
124 |
|
|
*/ |
125 |
|
|
void RunSuidHelper(const std::string &verb, const std::string &fqrn); |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* Replaces or creates $key=$value in the config file $path. Creates $path |
130 |
|
|
* if necessary. If value is empty, the key is removed. |
131 |
|
|
*/ |
132 |
|
|
void SetInConfig(const std::string &path, const std::string &key, |
133 |
|
|
const std::string &value); |
134 |
|
|
|
135 |
|
|
std::string SendTalkCommand(const std::string &socket, const std::string &cmd); |
136 |
|
|
|
137 |
|
|
} // namespace publish |
138 |
|
|
|
139 |
|
|
#endif // CVMFS_PUBLISH_REPOSITORY_UTIL_H_ |
140 |
|
|
|