1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 /**
8  * License: GPL-2.0(Linking Exception)
9  */
10 module libgit2.odb_backend;
11 
12 
13 private static import libgit2.indexer;
14 private static import libgit2.oid;
15 private static import libgit2.types;
16 private import libgit2.common: GIT_EXTERN;
17 
18 /*
19  * @file git2/backend.h
20  * @brief Git custom backend functions
21  * @defgroup git_odb Git object database routines
22  * @ingroup Git
23  * @{
24  */
25 extern (C):
26 nothrow @nogc:
27 public:
28 
29 /*
30  * Constructors for in-box ODB backends.
31  */
32 
33 /**
34  * Options for configuring a packfile object backend.
35  */
36 struct git_odb_backend_pack_options
37 {
38 	/**
39 	 * version for the struct
40 	 */
41 	uint version_;
42 
43 	/**
44 	 * Type of object IDs to use for this object database, or
45 	 * 0 for default (currently SHA1).
46 	 */
47 	libgit2.oid.git_oid_t oid_type;
48 }
49 
50 /**
51  * The current version of the diff options structure
52  */
53 enum GIT_ODB_BACKEND_PACK_OPTIONS_VERSION = 1;
54 
55 /**
56  * Stack initializer for odb pack backend options.  Alternatively use
57  * `git_odb_backend_pack_options_init` programmatic initialization.
58  */
59 pragma(inline, true)
60 pure nothrow @safe @nogc @live
61 .git_odb_backend_pack_options GIT_ODB_BACKEND_PACK_OPTIONS_INIT()
62 
63 	do
64 	{
65 		.git_odb_backend_pack_options OUTPUT =
66 		{
67 			version_: .GIT_ODB_BACKEND_PACK_OPTIONS_VERSION,
68 		};
69 
70 		return OUTPUT;
71 	}
72 
73 version (GIT_EXPERIMENTAL_SHA256) {
74 	/**
75 	 * Create a backend for the packfiles.
76 	 *
77 	 * Params:
78 	 *      out_ = location to store the odb backend pointer
79 	 *      objects_dir = the Git repository's objects directory
80 	 *      opts = ?
81 	 *
82 	 * Returns: 0 or an error code
83 	 */
84 	@GIT_EXTERN
85 	int git_odb_backend_pack(libgit2.types.git_odb_backend** out_, const (char)* objects_dir, const (.git_odb_backend_pack_options)* opts);
86 } else {
87 	/**
88 	 * Create a backend for the packfiles.
89 	 *
90 	 * Params:
91 	 *      out_ = location to store the odb backend pointer
92 	 *      objects_dir = the Git repository's objects directory
93 	 *
94 	 * Returns: 0 or an error code
95 	 */
96 	@GIT_EXTERN
97 	int git_odb_backend_pack(libgit2.types.git_odb_backend** out_, const (char)* objects_dir);
98 }
99 
100 version (GIT_EXPERIMENTAL_SHA256) {
101 	/**
102 	 * Create a backend out of a single packfile
103 	 *
104 	 * This can be useful for inspecting the contents of a single
105 	 * packfile.
106 	 *
107 	 * @param out_ location to store the odb backend pointer
108 	 * @param index_file path to the packfile's .idx file
109 	 *      opts = ?
110 	 *
111 	 * @return 0 or an error code
112 	 */
113 	@GIT_EXTERN
114 	int git_odb_backend_one_pack(libgit2.types.git_odb_backend** out_, const (char)* index_file, const (git_odb_backend_pack_options)* opts);
115 } else {
116 	/**
117 	 * Create a backend out of a single packfile
118 	 *
119 	 * This can be useful for inspecting the contents of a single
120 	 * packfile.
121 	 *
122 	 * @param out_ location to store the odb backend pointer
123 	 * @param index_file path to the packfile's .idx file
124 	 *
125 	 * @return 0 or an error code
126 	 */
127 	@GIT_EXTERN
128 	int git_odb_backend_one_pack(libgit2.types.git_odb_backend** out_, const (char)* index_file);
129 }
130 
131 enum git_odb_backend_loose_flag_t
132 {
133 	GIT_ODB_BACKEND_LOOSE_FSYNC = 1 << 0,
134 }
135 
136 //Declaration name in C language
137 enum
138 {
139 	GIT_ODB_BACKEND_LOOSE_FSYNC = .git_odb_backend_loose_flag_t.GIT_ODB_BACKEND_LOOSE_FSYNC,
140 }
141 
142 /**
143  * Options for configuring a loose object backend.
144  */
145 struct git_odb_backend_loose_options
146 {
147 	/**
148 	 * version for the struct
149 	 */
150 	uint version_;
151 
152 	/**
153 	 * A combination of the `git_odb_backend_loose_flag_t` types.
154 	 */
155 	uint flags;
156 
157 	/**
158 	 * zlib compression level to use (0-9), where 1 is the fastest
159 	 * at the expense of larger files, and 9 produces the best
160 	 * compression at the expense of speed.  0 indicates that no
161 	 * compression should be performed.  -1 is the default (currently
162 	 * optimizing for speed).
163 	 */
164 	int compression_level;
165 
166 	/**
167 	 * Permissions to use creating a directory or 0 for defaults
168 	 */
169 	uint dir_mode;
170 
171 	/**
172 	 * Permissions to use creating a file or 0 for defaults
173 	 */
174 	uint file_mode;
175 
176 	/**
177 	 * Type of object IDs to use for this object database, or
178 	 * 0 for default (currently SHA1).
179 	 */
180 	libgit2.oid.git_oid_t oid_type;
181 }
182 
183 /**
184  * The current version of the diff options structure
185  */
186 enum GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION = 1;
187 
188 /**
189  * Stack initializer for odb loose backend options.  Alternatively use
190  * `git_odb_backend_loose_options_init` programmatic initialization.
191  */
192 pragma(inline, true)
193 pure nothrow @safe @nogc @live
194 .git_odb_backend_loose_options GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT()
195 
196 	do
197 	{
198 		.git_odb_backend_loose_options OUTPUT =
199 		{
200 			version_: .GIT_ODB_BACKEND_LOOSE_OPTIONS_VERSION,
201 		};
202 
203 		return OUTPUT;
204 	}
205 
206 version (GIT_EXPERIMENTAL_SHA256) {
207 	/**
208 	 * Create a backend for loose objects
209 	 *
210 	 * Params:
211 	 *      out_ = location to store the odb backend pointer
212 	 *      objects_dir = the Git repository's objects directory
213 	 *      opts = options for the loose object backend or NULL
214 	 *
215 	 * Returns: 0 or an error code
216 	 */
217 	@GIT_EXTERN
218 	int git_odb_backend_loose(libgit2.types.git_odb_backend** out_, const (char)* objects_dir, .git_odb_backend_loose_options* opts);
219 } else {
220 	/**
221 	 * Create a backend for loose objects
222 	 *
223 	 * Params:
224 	 *      out_ = location to store the odb backend pointer
225 	 *      objects_dir = the Git repository's objects directory
226 	 *      compression_level = zlib compression level to use
227 	 *      do_fsync = whether to do an fsync() after writing
228 	 *      dir_mode = permissions to use creating a directory or 0 for defaults
229 	 *      file_mode = permissions to use creating a file or 0 for defaults
230 	 *
231 	 * Returns: 0 or an error code
232 	 */
233 	@GIT_EXTERN
234 	int git_odb_backend_loose(libgit2.types.git_odb_backend** out_, const (char)* objects_dir, int compression_level, int do_fsync, uint dir_mode, uint file_mode);
235 }
236 
237 /**
238  * Streaming mode
239  */
240 enum git_odb_stream_t
241 {
242 	GIT_STREAM_RDONLY = 1 << 1,
243 	GIT_STREAM_WRONLY = 1 << 2,
244 	GIT_STREAM_RW = GIT_STREAM_RDONLY | GIT_STREAM_WRONLY,
245 }
246 
247 //Declaration name in C language
248 enum
249 {
250 	GIT_STREAM_RDONLY = .git_odb_stream_t.GIT_STREAM_RDONLY,
251 	GIT_STREAM_WRONLY = .git_odb_stream_t.GIT_STREAM_WRONLY,
252 	GIT_STREAM_RW = .git_odb_stream_t.GIT_STREAM_RW,
253 }
254 
255 /**
256  * A stream to read/write from a backend.
257  *
258  * This represents a stream of data being written to or read from a
259  * backend. When writing, the frontend functions take care of
260  * calculating the object's id and all `finalize_write` needs to do is
261  * store the object with the id it is passed.
262  */
263 struct git_odb_stream
264 {
265 	libgit2.types.git_odb_backend* backend;
266 	uint mode;
267 	void* hash_ctx;
268 
269 	version (GIT_EXPERIMENTAL_SHA256) {
270 		libgit2.oid.git_oid_t oid_type;
271 	}
272 
273 	libgit2.types.git_object_size_t declared_size;
274 	libgit2.types.git_object_size_t received_bytes;
275 
276 	/**
277 	 * Write at most `len` bytes into `buffer` and advance the stream.
278 	 */
279 	int function(.git_odb_stream* stream, char* buffer, size_t len) read;
280 
281 	/**
282 	 * Write `len` bytes from `buffer` into the stream.
283 	 */
284 	int function(.git_odb_stream* stream, const (char)* buffer, size_t len) write;
285 
286 	/**
287 	 * Store the contents of the stream as an object with the id
288 	 * specified in `oid`.
289 	 *
290 	 * This method might not be invoked if:
291 	 * - an error occurs earlier with the `write` callback,
292 	 * - the object referred to by `oid` already exists in any backend, or
293 	 * - the final number of received bytes differs from the size declared
294 	 *   with `git_odb_open_wstream()`
295 	 */
296 	int function(.git_odb_stream* stream, const (libgit2.oid.git_oid)* oid) finalize_write;
297 
298 	/**
299 	 * Free the stream's memory.
300 	 *
301 	 * This method might be called without a call to `finalize_write` if
302 	 * an error occurs or if the object is already present in the ODB.
303 	 */
304 	void function(.git_odb_stream* stream) free;
305 }
306 
307 /**
308  * A stream to write a pack file to the ODB
309  */
310 struct git_odb_writepack
311 {
312 	libgit2.types.git_odb_backend* backend;
313 
314 	int function(.git_odb_writepack* writepack, const (void)* data, size_t size, libgit2.indexer.git_indexer_progress* stats) append;
315 	int function(.git_odb_writepack* writepack, libgit2.indexer.git_indexer_progress* stats) commit;
316 	void function(.git_odb_writepack* writepack) free;
317 }