]> git.proxmox.com Git - libgit2.git/blame - include/git2/sys/odb_backend.h
New upstream version 1.4.3+dfsg.1
[libgit2.git] / include / git2 / sys / odb_backend.h
CommitLineData
83cc70d9
RB
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#ifndef INCLUDE_sys_git_odb_backend_h__
8#define INCLUDE_sys_git_odb_backend_h__
9
10#include "git2/common.h"
11#include "git2/types.h"
12#include "git2/oid.h"
13#include "git2/odb.h"
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 */
22GIT_BEGIN_DECL
23
24/**
25 * An instance for a custom backend
26 */
27struct git_odb_backend {
28 unsigned int version;
29 git_odb *odb;
30
31 /* read and read_prefix each return to libgit2 a buffer which
32 * will be freed later. The buffer should be allocated using
22a2d3d5
UG
33 * the function git_odb_backend_data_alloc to ensure that libgit2
34 * can safely free it later. */
ac3d33df
JK
35 int GIT_CALLBACK(read)(
36 void **, size_t *, git_object_t *, git_odb_backend *, const git_oid *);
83cc70d9 37
f5753999
RB
38 /* To find a unique object given a prefix of its oid. The oid given
39 * must be so that the remaining (GIT_OID_HEXSZ - len)*4 bits are 0s.
83cc70d9 40 */
ac3d33df
JK
41 int GIT_CALLBACK(read_prefix)(
42 git_oid *, void **, size_t *, git_object_t *,
83cc70d9
RB
43 git_odb_backend *, const git_oid *, size_t);
44
ac3d33df
JK
45 int GIT_CALLBACK(read_header)(
46 size_t *, git_object_t *, git_odb_backend *, const git_oid *);
83cc70d9 47
7a3764be
CMN
48 /**
49 * Write an object into the backend. The id of the object has
50 * already been calculated and is passed in.
51 */
ac3d33df
JK
52 int GIT_CALLBACK(write)(
53 git_odb_backend *, const git_oid *, const void *, size_t, git_object_t);
83cc70d9 54
ac3d33df 55 int GIT_CALLBACK(writestream)(
22a2d3d5 56 git_odb_stream **, git_odb_backend *, git_object_size_t, git_object_t);
83cc70d9 57
ac3d33df
JK
58 int GIT_CALLBACK(readstream)(
59 git_odb_stream **, size_t *, git_object_t *,
eae0bfdc 60 git_odb_backend *, const git_oid *);
83cc70d9 61
ac3d33df 62 int GIT_CALLBACK(exists)(
83cc70d9
RB
63 git_odb_backend *, const git_oid *);
64
ac3d33df 65 int GIT_CALLBACK(exists_prefix)(
f5753999
RB
66 git_oid *, git_odb_backend *, const git_oid *, size_t);
67
b1a6c316 68 /**
69 * If the backend implements a refreshing mechanism, it should be exposed
70 * through this endpoint. Each call to `git_odb_refresh()` will invoke it.
71 *
e579e0f7
MB
72 * The odb layer will automatically call this when needed on failed
73 * lookups (ie. `exists()`, `read()`, `read_header()`).
b1a6c316 74 */
ac3d33df 75 int GIT_CALLBACK(refresh)(git_odb_backend *);
83cc70d9 76
ac3d33df 77 int GIT_CALLBACK(foreach)(
83cc70d9
RB
78 git_odb_backend *, git_odb_foreach_cb cb, void *payload);
79
ac3d33df 80 int GIT_CALLBACK(writepack)(
0b33fca0 81 git_odb_writepack **, git_odb_backend *, git_odb *odb,
22a2d3d5 82 git_indexer_progress_cb progress_cb, void *progress_payload);
83cc70d9 83
c25aa7cd
PP
84 /**
85 * If the backend supports pack files, this will create a
86 * `multi-pack-index` file which will contain an index of all objects
87 * across all the `.pack` files.
88 */
89 int GIT_CALLBACK(writemidx)(git_odb_backend *);
90
8f09a98e
ET
91 /**
92 * "Freshens" an already existing object, updating its last-used
93 * time. This occurs when `git_odb_write` was called, but the
94 * object already existed (and will not be re-written). The
95 * underlying implementation may want to update last-used timestamps.
96 *
97 * If callers implement this, they should return `0` if the object
98 * exists and was freshened, and non-zero otherwise.
99 */
ac3d33df 100 int GIT_CALLBACK(freshen)(git_odb_backend *, const git_oid *);
8f09a98e 101
d3b29fb9
AS
102 /**
103 * Frees any resources held by the odb (including the `git_odb_backend`
104 * itself). An odb backend implementation must provide this function.
105 */
ac3d33df 106 void GIT_CALLBACK(free)(git_odb_backend *);
83cc70d9
RB
107};
108
109#define GIT_ODB_BACKEND_VERSION 1
110#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION}
111
b9f81997
MB
112/**
113 * Initializes a `git_odb_backend` with default values. Equivalent to
114 * creating an instance with GIT_ODB_BACKEND_INIT.
115 *
41808d04 116 * @param backend the `git_odb_backend` struct to initialize.
bc91347b 117 * @param version Version the struct; pass `GIT_ODB_BACKEND_VERSION`
b9f81997
MB
118 * @return Zero on success; -1 on failure.
119 */
120GIT_EXTERN(int) git_odb_init_backend(
bc91347b
RB
121 git_odb_backend *backend,
122 unsigned int version);
b9f81997 123
22a2d3d5
UG
124/**
125 * Allocate data for an ODB object. Custom ODB backends may use this
126 * to provide data back to the ODB from their read function. This
127 * memory should not be freed once it is returned to libgit2. If a
128 * custom ODB uses this function but encounters an error and does not
129 * return this data to libgit2, then they should use the corresponding
130 * git_odb_backend_data_free function.
131 *
132 * @param backend the ODB backend that is allocating this memory
133 * @param len the number of bytes to allocate
134 * @return the allocated buffer on success or NULL if out of memory
135 */
136GIT_EXTERN(void *) git_odb_backend_data_alloc(git_odb_backend *backend, size_t len);
137
138/**
139 * Frees custom allocated ODB data. This should only be called when
140 * memory allocated using git_odb_backend_data_alloc is not returned
141 * to libgit2 because the backend encountered an error in the read
142 * function after allocation and did not return this data to libgit2.
143 *
144 * @param backend the ODB backend that is freeing this memory
145 * @param data the buffer to free
146 */
147GIT_EXTERN(void) git_odb_backend_data_free(git_odb_backend *backend, void *data);
148
149
150/*
151 * Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`.
152 */
153#ifndef GIT_DEPRECATE_HARD
154
155/**
156 * Allocate memory for an ODB object from a custom backend. This is
157 * an alias of `git_odb_backend_data_alloc` and is preserved for
158 * backward compatibility.
159 *
160 * This function is deprecated, but there is no plan to remove this
161 * function at this time.
162 *
163 * @deprecated git_odb_backend_data_alloc
164 * @see git_odb_backend_data_alloc
165 */
83cc70d9
RB
166GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len);
167
22a2d3d5
UG
168#endif
169
83cc70d9
RB
170GIT_END_DECL
171
172#endif