/src/git/reftable/system.c
Line | Count | Source |
1 | | #include "../git-compat-util.h" |
2 | | |
3 | | #include "system.h" |
4 | | #include "basics.h" |
5 | | #include "reftable-error.h" |
6 | | #include "../lockfile.h" |
7 | | #include "../tempfile.h" |
8 | | |
9 | | uint32_t reftable_rand(void) |
10 | 0 | { |
11 | 0 | return git_rand(CSPRNG_BYTES_INSECURE); |
12 | 0 | } |
13 | | |
14 | | int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern) |
15 | 0 | { |
16 | 0 | struct tempfile *tempfile; |
17 | |
|
18 | 0 | tempfile = mks_tempfile(pattern); |
19 | 0 | if (!tempfile) |
20 | 0 | return REFTABLE_IO_ERROR; |
21 | | |
22 | 0 | out->path = tempfile->filename.buf; |
23 | 0 | out->fd = tempfile->fd; |
24 | 0 | out->priv = tempfile; |
25 | |
|
26 | 0 | return 0; |
27 | 0 | } |
28 | | |
29 | | int tmpfile_close(struct reftable_tmpfile *t) |
30 | 0 | { |
31 | 0 | struct tempfile *tempfile = t->priv; |
32 | 0 | int ret = close_tempfile_gently(tempfile); |
33 | 0 | t->fd = -1; |
34 | 0 | if (ret < 0) |
35 | 0 | return REFTABLE_IO_ERROR; |
36 | 0 | return 0; |
37 | 0 | } |
38 | | |
39 | | int tmpfile_delete(struct reftable_tmpfile *t) |
40 | 0 | { |
41 | 0 | struct tempfile *tempfile = t->priv; |
42 | 0 | int ret = delete_tempfile(&tempfile); |
43 | 0 | *t = REFTABLE_TMPFILE_INIT; |
44 | 0 | if (ret < 0) |
45 | 0 | return REFTABLE_IO_ERROR; |
46 | 0 | return 0; |
47 | 0 | } |
48 | | |
49 | | int tmpfile_rename(struct reftable_tmpfile *t, const char *path) |
50 | 0 | { |
51 | 0 | struct tempfile *tempfile = t->priv; |
52 | 0 | int ret = rename_tempfile(&tempfile, path); |
53 | 0 | *t = REFTABLE_TMPFILE_INIT; |
54 | 0 | if (ret < 0) |
55 | 0 | return REFTABLE_IO_ERROR; |
56 | 0 | return 0; |
57 | 0 | } |
58 | | |
59 | | int flock_acquire(struct reftable_flock *l, const char *target_path, |
60 | | long timeout_ms) |
61 | 0 | { |
62 | 0 | struct lock_file *lockfile; |
63 | 0 | int err; |
64 | |
|
65 | 0 | lockfile = reftable_malloc(sizeof(*lockfile)); |
66 | 0 | if (!lockfile) |
67 | 0 | return REFTABLE_OUT_OF_MEMORY_ERROR; |
68 | | |
69 | 0 | err = hold_lock_file_for_update_timeout(lockfile, target_path, LOCK_NO_DEREF, |
70 | 0 | timeout_ms); |
71 | 0 | if (err < 0) { |
72 | 0 | reftable_free(lockfile); |
73 | 0 | if (errno == EEXIST) |
74 | 0 | return REFTABLE_LOCK_ERROR; |
75 | 0 | return REFTABLE_IO_ERROR; |
76 | 0 | } |
77 | | |
78 | 0 | l->fd = get_lock_file_fd(lockfile); |
79 | 0 | l->path = get_lock_file_path(lockfile); |
80 | 0 | l->priv = lockfile; |
81 | |
|
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | | int flock_close(struct reftable_flock *l) |
86 | 0 | { |
87 | 0 | struct lock_file *lockfile = l->priv; |
88 | 0 | int ret; |
89 | |
|
90 | 0 | if (!lockfile) |
91 | 0 | return REFTABLE_API_ERROR; |
92 | | |
93 | 0 | ret = close_lock_file_gently(lockfile); |
94 | 0 | l->fd = -1; |
95 | 0 | if (ret < 0) |
96 | 0 | return REFTABLE_IO_ERROR; |
97 | | |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | int flock_release(struct reftable_flock *l) |
102 | 0 | { |
103 | 0 | struct lock_file *lockfile = l->priv; |
104 | 0 | int ret; |
105 | |
|
106 | 0 | if (!lockfile) |
107 | 0 | return 0; |
108 | | |
109 | 0 | ret = rollback_lock_file(lockfile); |
110 | 0 | reftable_free(lockfile); |
111 | 0 | *l = REFTABLE_FLOCK_INIT; |
112 | 0 | if (ret < 0) |
113 | 0 | return REFTABLE_IO_ERROR; |
114 | | |
115 | 0 | return 0; |
116 | 0 | } |
117 | | |
118 | | int flock_commit(struct reftable_flock *l) |
119 | 0 | { |
120 | 0 | struct lock_file *lockfile = l->priv; |
121 | 0 | int ret; |
122 | |
|
123 | 0 | if (!lockfile) |
124 | 0 | return REFTABLE_API_ERROR; |
125 | | |
126 | 0 | ret = commit_lock_file(lockfile); |
127 | 0 | reftable_free(lockfile); |
128 | 0 | *l = REFTABLE_FLOCK_INIT; |
129 | 0 | if (ret < 0) |
130 | 0 | return REFTABLE_IO_ERROR; |
131 | | |
132 | 0 | return 0; |
133 | 0 | } |