Skip to content

Commit 969a056

Browse files
authored
Merge pull request libgit2#6017 from libgit2/ethomson/buf_is_readonly
buf: deprecate public git_buf writing functions
2 parents b16a36e + d2316d5 commit 969a056

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

include/git2/deprecated.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,61 @@ GIT_EXTERN(int) git_treebuilder_write_with_buffer(
191191
*/
192192
/**@{*/
193193

194+
/**
195+
* Static initializer for git_buf from static buffer
196+
*/
197+
#define GIT_BUF_INIT_CONST(STR,LEN) { (char *)(STR), 0, (size_t)(LEN) }
198+
199+
/**
200+
* Resize the buffer allocation to make more space.
201+
*
202+
* This will attempt to grow the buffer to accommodate the target size.
203+
*
204+
* If the buffer refers to memory that was not allocated by libgit2 (i.e.
205+
* the `asize` field is zero), then `ptr` will be replaced with a newly
206+
* allocated block of data. Be careful so that memory allocated by the
207+
* caller is not lost. As a special variant, if you pass `target_size` as
208+
* 0 and the memory is not allocated by libgit2, this will allocate a new
209+
* buffer of size `size` and copy the external data into it.
210+
*
211+
* Currently, this will never shrink a buffer, only expand it.
212+
*
213+
* If the allocation fails, this will return an error and the buffer will be
214+
* marked as invalid for future operations, invaliding the contents.
215+
*
216+
* @param buffer The buffer to be resized; may or may not be allocated yet
217+
* @param target_size The desired available size
218+
* @return 0 on success, -1 on allocation failure
219+
*/
220+
GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size);
221+
222+
/**
223+
* Set buffer to a copy of some raw data.
224+
*
225+
* @param buffer The buffer to set
226+
* @param data The data to copy into the buffer
227+
* @param datalen The length of the data to copy into the buffer
228+
* @return 0 on success, -1 on allocation failure
229+
*/
230+
GIT_EXTERN(int) git_buf_set(
231+
git_buf *buffer, const void *data, size_t datalen);
232+
233+
/**
234+
* Check quickly if buffer looks like it contains binary data
235+
*
236+
* @param buf Buffer to check
237+
* @return 1 if buffer looks like non-text data
238+
*/
239+
GIT_EXTERN(int) git_buf_is_binary(const git_buf *buf);
240+
241+
/**
242+
* Check quickly if buffer contains a NUL byte
243+
*
244+
* @param buf Buffer to check
245+
* @return 1 if buffer contains a NUL byte
246+
*/
247+
GIT_EXTERN(int) git_buf_contains_nul(const git_buf *buf);
248+
194249
/**
195250
* Free the memory referred to by the git_buf. This is an alias of
196251
* `git_buf_dispose` and is preserved for backward compatibility.

src/buffer.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ extern char git_buf__oom[];
3838
/* Use to initialize buffer structure when git_buf is on stack */
3939
#define GIT_BUF_INIT { git_buf__initbuf, 0, 0 }
4040

41+
/**
42+
* Static initializer for git_buf from static buffer
43+
*/
44+
#ifdef GIT_DEPRECATE_HARD
45+
# define GIT_BUF_INIT_CONST(STR,LEN) { (char *)(STR), 0, (size_t)(LEN) }
46+
#endif
47+
4148
GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
4249
{
4350
return (buf->ptr != NULL && buf->asize > 0);
@@ -51,6 +58,33 @@ GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
5158
*/
5259
extern int git_buf_init(git_buf *buf, size_t initial_size);
5360

61+
#ifdef GIT_DEPRECATE_HARD
62+
63+
/**
64+
* Resize the buffer allocation to make more space.
65+
*
66+
* This will attempt to grow the buffer to accommodate the target size.
67+
*
68+
* If the buffer refers to memory that was not allocated by libgit2 (i.e.
69+
* the `asize` field is zero), then `ptr` will be replaced with a newly
70+
* allocated block of data. Be careful so that memory allocated by the
71+
* caller is not lost. As a special variant, if you pass `target_size` as
72+
* 0 and the memory is not allocated by libgit2, this will allocate a new
73+
* buffer of size `size` and copy the external data into it.
74+
*
75+
* Currently, this will never shrink a buffer, only expand it.
76+
*
77+
* If the allocation fails, this will return an error and the buffer will be
78+
* marked as invalid for future operations, invaliding the contents.
79+
*
80+
* @param buffer The buffer to be resized; may or may not be allocated yet
81+
* @param target_size The desired available size
82+
* @return 0 on success, -1 on allocation failure
83+
*/
84+
int git_buf_grow(git_buf *buffer, size_t target_size);
85+
86+
#endif
87+
5488
/**
5589
* Resize the buffer allocation to make more space.
5690
*
@@ -120,6 +154,11 @@ GIT_INLINE(bool) git_buf_oom(const git_buf *buf)
120154
* return code of these functions and call them in a series then just call
121155
* git_buf_oom at the end.
122156
*/
157+
158+
#ifdef GIT_DEPRECATE_HARD
159+
int git_buf_set(git_buf *buffer, const void *data, size_t datalen);
160+
#endif
161+
123162
int git_buf_sets(git_buf *buf, const char *string);
124163
int git_buf_putc(git_buf *buf, char c);
125164
int git_buf_putcn(git_buf *buf, char c, size_t len);
@@ -311,4 +350,24 @@ extern int git_buf_detect_bom(git_buf_bom_t *bom, const git_buf *buf);
311350
extern bool git_buf_gather_text_stats(
312351
git_buf_text_stats *stats, const git_buf *buf, bool skip_bom);
313352

353+
#ifdef GIT_DEPRECATE_HARD
354+
355+
/**
356+
* Check quickly if buffer looks like it contains binary data
357+
*
358+
* @param buf Buffer to check
359+
* @return 1 if buffer looks like non-text data
360+
*/
361+
int git_buf_is_binary(const git_buf *buf);
362+
363+
/**
364+
* Check quickly if buffer contains a NUL byte
365+
*
366+
* @param buf Buffer to check
367+
* @return 1 if buffer contains a NUL byte
368+
*/
369+
int git_buf_contains_nul(const git_buf *buf);
370+
371+
#endif
372+
314373
#endif

0 commit comments

Comments
 (0)