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.sys.odb_backend;
11 
12 
13 private static import libgit2.indexer;
14 private static import libgit2.odb;
15 private static import libgit2.oid;
16 private static import libgit2.types;
17 private import libgit2.common: GIT_EXTERN;
18 
19 /*
20  * @file git2/sys/backend.h
21  * @brief Git custom backend implementors functions
22  * @defgroup git_backend Git custom backend APIs
23  * @ingroup Git
24  * @{
25  */
26 extern (C):
27 nothrow @nogc:
28 
29 /**
30  * An instance for a custom backend
31  */
32 struct git_odb_backend
33 {
34 	uint version_;
35 	libgit2.types.git_odb* odb;
36 
37 	/**
38 	 * read and read_prefix each return to libgit2 a buffer which
39 	 * will be freed later. The buffer should be allocated using
40 	 * the function git_odb_backend_data_alloc to ensure that libgit2
41 	 * can safely free it later.
42 	 */
43 	int function(void**, size_t*, libgit2.types.git_object_t*, .git_odb_backend*, const (libgit2.oid.git_oid)*) read;
44 
45 	/*
46 	 * To find a unique object given a prefix of its oid.  The oid given
47 	 * must be so that the remaining (GIT_OID_SHA1_HEXSIZE - len)*4 bits are 0s.
48 	 */
49 	int function(libgit2.oid.git_oid*, void**, size_t*, libgit2.types.git_object_t*, .git_odb_backend*, const (libgit2.oid.git_oid)*, size_t) read_prefix;
50 
51 	int function(size_t*, libgit2.types.git_object_t*, .git_odb_backend*, const (libgit2.oid.git_oid)*) read_header;
52 
53 	/**
54 	 * Write an object into the backend. The id of the object has
55 	 * already been calculated and is passed in.
56 	 */
57 	int function(.git_odb_backend*, const (libgit2.oid.git_oid)*, const (void)*, size_t, libgit2.types.git_object_t) write;
58 
59 	int function(libgit2.types.git_odb_stream**, .git_odb_backend*, libgit2.types.git_object_size_t, libgit2.types.git_object_t) writestream;
60 
61 	int function(libgit2.types.git_odb_stream**, size_t*, libgit2.types.git_object_t*, .git_odb_backend*, const (libgit2.oid.git_oid)*) readstream;
62 
63 	int function(.git_odb_backend*, const (libgit2.oid.git_oid)*) exists;
64 
65 	int function(libgit2.oid.git_oid*, .git_odb_backend*, const (libgit2.oid.git_oid)*, size_t) exists_prefix;
66 
67 	/**
68 	 * If the backend implements a refreshing mechanism, it should be exposed
69 	 * through this endpoint. Each call to `git_odb_refresh()` will invoke it.
70 	 *
71 	 * The odb layer will automatically call this when needed on failed
72 	 * lookups (ie. `exists()`, `read()`, `read_header()`).
73 	 */
74 	int function(.git_odb_backend*) refresh;
75 
76 	int function(.git_odb_backend*, libgit2.odb.git_odb_foreach_cb cb, void* payload) foreach_;
77 
78 	int function(libgit2.types.git_odb_writepack**, .git_odb_backend*, libgit2.types.git_odb* odb, libgit2.indexer.git_indexer_progress_cb progress_cb, void* progress_payload) writepack;
79 
80 	/**
81 	 * If the backend supports pack files, this will create a
82 	 * `multi-pack-index` file which will contain an index of all objects
83 	 * across all the `.pack` files.
84 	 */
85 	int function(.git_odb_backend*) writemidx;
86 
87 	/**
88 	 * "Freshens" an already existing object, updating its last-used
89 	 * time.  This occurs when `git_odb_write` was called, but the
90 	 * object already existed (and will not be re-written).  The
91 	 * underlying implementation may want to update last-used timestamps.
92 	 *
93 	 * If callers implement this, they should return `0` if the object
94 	 * exists and was freshened, and non-zero otherwise.
95 	 */
96 	int function(.git_odb_backend*, const (libgit2.oid.git_oid)*) freshen;
97 
98 	/**
99 	 * Frees any resources held by the odb (including the `git_odb_backend`
100 	 * itself). An odb backend implementation must provide this function.
101 	 */
102 	void function(.git_odb_backend*) free;
103 }
104 
105 enum GIT_ODB_BACKEND_VERSION = 1;
106 
107 pragma(inline, true)
108 pure nothrow @safe @nogc @live
109 .git_odb_backend GIT_ODB_BACKEND_INIT()
110 
111 	do
112 	{
113 		.git_odb_backend OUTPUT =
114 		{
115 			version_: .GIT_ODB_BACKEND_VERSION,
116 		};
117 
118 		return OUTPUT;
119 	}
120 
121 /**
122  * Initializes a `git_odb_backend` with default values. Equivalent to
123  * creating an instance with GIT_ODB_BACKEND_INIT.
124  *
125  * Params:
126  *      backend = the `git_odb_backend` struct to initialize.
127  *      version_ = Version the struct; pass `GIT_ODB_BACKEND_VERSION`
128  *
129  * Returns: Zero on success; -1 on failure.
130  */
131 @GIT_EXTERN
132 int git_odb_init_backend(.git_odb_backend* backend, uint version_);
133 
134 /**
135  * Allocate data for an ODB object.  Custom ODB backends may use this
136  * to provide data back to the ODB from their read function.  This
137  * memory should not be freed once it is returned to libgit2.  If a
138  * custom ODB uses this function but encounters an error and does not
139  * return this data to libgit2, then they should use the corresponding
140  * git_odb_backend_data_free function.
141  *
142  * Params:
143  *      backend = the ODB backend that is allocating this memory
144  *      len = the number of bytes to allocate
145  *
146  * Returns: the allocated buffer on success or null if out of memory
147  */
148 @GIT_EXTERN
149 void* git_odb_backend_data_alloc(.git_odb_backend* backend, size_t len);
150 
151 /**
152  * Frees custom allocated ODB data.  This should only be called when
153  * memory allocated using git_odb_backend_data_alloc is not returned
154  * to libgit2 because the backend encountered an error in the read
155  * function after allocation and did not return this data to libgit2.
156  *
157  * Params:
158  *      backend = the ODB backend that is freeing this memory
159  *      data = the buffer to free
160  */
161 @GIT_EXTERN
162 void git_odb_backend_data_free(.git_odb_backend* backend, void* data);
163 
164 deprecated:
165 
166 /*
167  * Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`.
168  */
169 version (GIT_DEPRECATE_HARD) {
170 } else {
171 	/**
172 	 * Allocate memory for an ODB object from a custom backend.  This is
173 	 * an alias of `git_odb_backend_data_alloc` and is preserved for
174 	 * backward compatibility.
175 	 *
176 	 * This function is deprecated, but there is no plan to remove this
177 	 * function at this time.
178 	 *
179 	 * @deprecated git_odb_backend_data_alloc
180 	 * @see git_odb_backend_data_alloc
181 	 */
182 	@GIT_EXTERN
183 	void* git_odb_backend_malloc(.git_odb_backend* backend, size_t len);
184 }