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