Skip to content

Commit fd66b15

Browse files
authored
Merge pull request cfengine#5562 from vpodzime/master-masterfiles_stage_lock
Add the GetMasterfilesStageLock() function
2 parents 1535857 + 54e6a2f commit fd66b15

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

libpromises/policy.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include <audit.h>
4444
#include <logging.h>
4545
#include <expand.h>
46+
#include <file_lib.h> /* FileLock */
47+
#include <known_dirs.h> /* GetStateDir() */
4648

4749
static const char *const POLICY_ERROR_BUNDLE_NAME_RESERVED =
4850
"Use of a reserved container name as a bundle name \"%s\"";
@@ -100,6 +102,29 @@ const char *NamespaceDefault(void)
100102

101103
/*************************************************************************/
102104

105+
#define MASTERFILES_STAGE_LOCK_FNAME "masterfiles-stage.lock"
106+
107+
bool GetMasterfilesStageLock(FileLock *lock, bool exclusive, bool wait)
108+
{
109+
assert(lock != NULL);
110+
char path[PATH_MAX];
111+
NDEBUG_UNUSED size_t ret = StringCopy(GetStateDir(), path, sizeof(path));
112+
assert(ret < sizeof(path));
113+
NDEBUG_UNUSED char *path_ret = JoinPaths(path, sizeof(path), MASTERFILES_STAGE_LOCK_FNAME);
114+
assert(path_ret != NULL);
115+
116+
if (exclusive)
117+
{
118+
return (ExclusiveFileLockPath(lock, path, wait) == 0);
119+
}
120+
else
121+
{
122+
return (SharedFileLockPath(lock, path, wait) == 0);
123+
}
124+
}
125+
126+
/*************************************************************************/
127+
103128
Policy *PolicyNew(void)
104129
{
105130
Policy *policy = xcalloc(1, sizeof(Policy));

libpromises/policy.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <sequence.h>
3232
#include <json.h>
3333
#include <set.h>
34+
#include <file_lib.h> /* FileLock */
3435

3536
typedef enum
3637
{
@@ -141,6 +142,18 @@ struct Constraint_
141142
SourceOffset offset;
142143
};
143144

145+
/**
146+
* Get lock for masterfiles stage (update)
147+
* @param lock file lock to initialize (not-%NULL)
148+
* @param exclusive %true to get exclusive lock (for write access),
149+
* %false for a shared lock (read access)
150+
* @param wait whether to wait for the lock (blocks) or give up immediately
151+
* @return whether the lock was successfully obtained or not
152+
* @note Unlock the lock with ExclusiveFileUnlock() or SharedFileUnlock()
153+
* when done.
154+
*/
155+
bool GetMasterfilesStageLock(FileLock *lock, bool exclusive, bool wait);
156+
144157
const char *NamespaceDefault(void);
145158

146159
Policy *PolicyNew(void);

0 commit comments

Comments
 (0)