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_git_odb_backend_h__
8 #define INCLUDE_git_odb_backend_h__
9 
10 #include "common.h"
11 #include "types.h"
12 #include "indexer.h"
13 
14 /**
15  * @file git2/backend.h
16  * @brief Git custom backend functions
17  * @defgroup git_odb Git object database routines
18  * @ingroup Git
19  * @{
20  */
21 GIT_BEGIN_DECL
22 
23 /*
24  * Constructors for in-box ODB backends.
25  */
26 
27 /**
28  * Create a backend for the packfiles.
29  *
30  * @param out location to store the odb backend pointer
31  * @param objects_dir the Git repository's objects directory
32  *
33  * @return 0 or an error code
34  */
35 GIT_EXTERN(int) git_odb_backend_pack(git_odb_backend **out, const char *objects_dir);
36 
37 /**
38  * Create a backend for loose objects
39  *
40  * @param out location to store the odb backend pointer
41  * @param objects_dir the Git repository's objects directory
42  * @param compression_level zlib compression level to use
43  * @param do_fsync whether to do an fsync() after writing
44  * @param dir_mode permissions to use creating a directory or 0 for defaults
45  * @param file_mode permissions to use creating a file or 0 for defaults
46  *
47  * @return 0 or an error code
48  */
49 GIT_EXTERN(int) git_odb_backend_loose(
50 	git_odb_backend **out,
51 	const char *objects_dir,
52 	int compression_level,
53 	int do_fsync,
54 	unsigned int dir_mode,
55 	unsigned int file_mode);
56 
57 /**
58  * Create a backend out of a single packfile
59  *
60  * This can be useful for inspecting the contents of a single
61  * packfile.
62  *
63  * @param out location to store the odb backend pointer
64  * @param index_file path to the packfile's .idx file
65  *
66  * @return 0 or an error code
67  */
68 GIT_EXTERN(int) git_odb_backend_one_pack(git_odb_backend **out, const char *index_file);
69 
70 /** Streaming mode */
71 typedef enum {
72 	GIT_STREAM_RDONLY = (1 << 1),
73 	GIT_STREAM_WRONLY = (1 << 2),
74 	GIT_STREAM_RW = (GIT_STREAM_RDONLY | GIT_STREAM_WRONLY),
75 } git_odb_stream_t;
76 
77 /**
78  * A stream to read/write from a backend.
79  *
80  * This represents a stream of data being written to or read from a
81  * backend. When writing, the frontend functions take care of
82  * calculating the object's id and all `finalize_write` needs to do is
83  * store the object with the id it is passed.
84  */
85 struct git_odb_stream {
86 	git_odb_backend *backend;
87 	unsigned int mode;
88 	void *hash_ctx;
89 
90 	git_object_size_t declared_size;
91 	git_object_size_t received_bytes;
92 
93 	/**
94 	 * Write at most `len` bytes into `buffer` and advance the stream.
95 	 */
96 	int GIT_CALLBACK(read)(git_odb_stream *stream, char *buffer, size_t len);
97 
98 	/**
99 	 * Write `len` bytes from `buffer` into the stream.
100 	 */
101 	int GIT_CALLBACK(write)(git_odb_stream *stream, const char *buffer, size_t len);
102 
103 	/**
104 	 * Store the contents of the stream as an object with the id
105 	 * specified in `oid`.
106 	 *
107 	 * This method might not be invoked if:
108 	 * - an error occurs earlier with the `write` callback,
109 	 * - the object referred to by `oid` already exists in any backend, or
110 	 * - the final number of received bytes differs from the size declared
111 	 *   with `git_odb_open_wstream()`
112 	 */
113 	int GIT_CALLBACK(finalize_write)(git_odb_stream *stream, const git_oid *oid);
114 
115 	/**
116 	 * Free the stream's memory.
117 	 *
118 	 * This method might be called without a call to `finalize_write` if
119 	 * an error occurs or if the object is already present in the ODB.
120 	 */
121 	void GIT_CALLBACK(free)(git_odb_stream *stream);
122 };
123 
124 /** A stream to write a pack file to the ODB */
125 struct git_odb_writepack {
126 	git_odb_backend *backend;
127 
128 	int GIT_CALLBACK(append)(git_odb_writepack *writepack, const void *data, size_t size, git_indexer_progress *stats);
129 	int GIT_CALLBACK(commit)(git_odb_writepack *writepack, git_indexer_progress *stats);
130 	void GIT_CALLBACK(free)(git_odb_writepack *writepack);
131 };
132 
133 GIT_END_DECL
134 
135 #endif
136