forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathsystem.h
More file actions
133 lines (113 loc) · 3.98 KB
/
system.h
File metadata and controls
133 lines (113 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd
*/
#ifndef SYSTEM_H
#define SYSTEM_H
/* This header glues the reftable library to the rest of Git */
#include "reftable-system.h"
#include "compat/zlib-compat.h"
#define REFTABLE_INLINE(type) static inline type
/*
* Return a random 32 bit integer. This function is expected to return
* pre-seeded data.
*/
uint32_t reftable_rand(void);
/*
* An implementation-specific temporary file. By making this specific to the
* implementation it becomes possible to tie temporary files into any kind of
* signal or atexit handlers for cleanup on abnormal situations.
*/
struct reftable_tmpfile {
const char *path;
int fd;
void *priv;
};
#define REFTABLE_TMPFILE_INIT ((struct reftable_tmpfile) { .fd = -1, })
/*
* Create a temporary file from a pattern similar to how mkstemp(3p) would.
* The `pattern` shall not be modified. On success, the structure at `out` has
* been initialized such that it is ready for use. Returns 0 on success, a
* reftable error code on error.
*/
int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern);
/*
* Close the temporary file's file descriptor without removing the file itself.
* This is a no-op in case the file has already been closed beforehand. Returns
* 0 on success, a reftable error code on error.
*/
int tmpfile_close(struct reftable_tmpfile *t);
/*
* Close the temporary file and delete it. This is a no-op in case the file has
* already been deleted or renamed beforehand. Returns 0 on success, a reftable
* error code on error.
*/
int tmpfile_delete(struct reftable_tmpfile *t);
/*
* Rename the temporary file to the provided path. The temporary file must be
* active. Return 0 on success, a reftable error code on error. Deactivates the
* temporary file.
*/
int tmpfile_rename(struct reftable_tmpfile *t, const char *path);
/*
* An implementation-specific file lock. Same as with `reftable_tmpfile`,
* making this specific to the implementation makes it possible to tie this
* into signal or atexit handlers such that we know to clean up stale locks on
* abnormal exits.
*/
struct reftable_flock {
const char *path;
int fd;
void *priv;
};
#define REFTABLE_FLOCK_INIT ((struct reftable_flock){ .fd = -1, })
/*
* Acquire the lock for the given target path by exclusively creating a file
* with ".lock" appended to it. If that lock exists, we wait up to `timeout_ms`
* to acquire the lock. If `timeout_ms` is 0 we don't wait, if it is negative
* we block indefinitely.
*
* Retrun 0 on success, a reftable error code on error. Specifically,
* `REFTABLE_LOCK_ERROR` should be returned in case the target path is already
* locked.
*/
int flock_acquire(struct reftable_flock *l, const char *target_path,
long timeout_ms);
/*
* Close the lockfile's file descriptor without removing the lock itself. This
* is a no-op in case the lockfile has already been closed beforehand. Returns
* 0 on success, a reftable error code on error.
*/
int flock_close(struct reftable_flock *l);
/*
* Release the lock by unlinking the lockfile. This is a no-op in case the
* lockfile has already been released or committed beforehand. Returns 0 on
* success, a reftable error code on error.
*/
int flock_release(struct reftable_flock *l);
/*
* Commit the lock by renaming the lockfile into place. Returns 0 on success, a
* reftable error code on error.
*/
int flock_commit(struct reftable_flock *l);
/* Report the time in milliseconds. */
uint64_t reftable_time_ms(void);
struct reftable_mmap {
void *data;
size_t size;
void *priv;
};
/*
* Map the file into memory. Returns 0 on success, a reftable error code on
* error.
*/
int reftable_mmap(struct reftable_mmap *out, int fd, size_t len);
/*
* Unmap the file from memory. Returns 0 on success, a reftable error code on
* error.
*/
int reftable_munmap(struct reftable_mmap *mmap);
#endif