Commit 13224ea4aad9a1b3c9cc4c992ceaea9af623e047

Vicent Martí 2012-02-27T04:28:31

buffer: Unify `git_fbuffer` and `git_buf` This makes so much sense that I can't believe it hasn't been done before. Kill the old `git_fbuffer` and read files straight into `git_buf` objects. Also: In order to fully support 4GB files in 32-bit systems, the `git_buf` implementation has been changed from using `ssize_t` for storage and storing negative values on allocation failure, to using `size_t` and changing the buffer pointer to a magical pointer on allocation failure. Hopefully this won't break anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
diff --git a/src/attr_file.c b/src/attr_file.c
index 7911381..a1b69a5 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -111,7 +111,7 @@ int git_attr_file__from_file(
 	git_repository *repo, const char *path, git_attr_file *file)
 {
 	int error = GIT_SUCCESS;
-	git_fbuffer fbuf = GIT_FBUFFER_INIT;
+	git_buf fbuf = GIT_BUF_INIT;
 
 	assert(path && file);
 
@@ -120,9 +120,9 @@ int git_attr_file__from_file(
 
 	if (error == GIT_SUCCESS &&
 		(error = git_futils_readbuffer(&fbuf, path)) == GIT_SUCCESS)
-		error = git_attr_file__from_buffer(repo, fbuf.data, file);
+		error = git_attr_file__from_buffer(repo, fbuf.ptr, file);
 
-	git_futils_freebuffer(&fbuf);
+	git_buf_free(&fbuf);
 	if (error != GIT_SUCCESS)
 		git__rethrow(error, "Could not open attribute file '%s'", path);
 
diff --git a/src/buffer.c b/src/buffer.c
index 183da7c..b9f62cc 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -7,14 +7,17 @@
 #include "buffer.h"
 #include "posix.h"
 #include <stdarg.h>
+#include <ctype.h>
 
 /* Used as default value for git_buf->ptr so that people can always
  * assume ptr is non-NULL and zero terminated even for new git_bufs.
  */
 char git_buf_initbuf[1];
 
+static char git_buf__oom;
+
 #define ENSURE_SIZE(b, d) \
-	if ((ssize_t)(d) > buf->asize && git_buf_grow(b, (d)) < GIT_SUCCESS)\
+	if ((d) > buf->asize && git_buf_grow(b, (d)) < GIT_SUCCESS)\
 		return GIT_ENOMEM;
 
 
@@ -31,8 +34,10 @@ void git_buf_init(git_buf *buf, size_t initial_size)
 int git_buf_grow(git_buf *buf, size_t target_size)
 {
 	int error = git_buf_try_grow(buf, target_size);
-	if (error != GIT_SUCCESS)
-		buf->asize = -1;
+	if (error != GIT_SUCCESS) {
+		buf->ptr = &git_buf__oom;
+	}
+
 	return error;
 }
 
@@ -41,17 +46,17 @@ int git_buf_try_grow(git_buf *buf, size_t target_size)
 	char *new_ptr;
 	size_t new_size;
 
-	if (buf->asize < 0)
+	if (buf->ptr == &git_buf__oom)
 		return GIT_ENOMEM;
 
-	if (target_size <= (size_t)buf->asize)
+	if (target_size <= buf->asize)
 		return GIT_SUCCESS;
 
 	if (buf->asize == 0) {
 		new_size = target_size;
 		new_ptr = NULL;
 	} else {
-		new_size = (size_t)buf->asize;
+		new_size = buf->asize;
 		new_ptr = buf->ptr;
 	}
 
@@ -64,7 +69,6 @@ int git_buf_try_grow(git_buf *buf, size_t target_size)
 	new_size = (new_size + 7) & ~7;
 
 	new_ptr = git__realloc(new_ptr, new_size);
-	/* if realloc fails, return without modifying the git_buf */
 	if (!new_ptr)
 		return GIT_ENOMEM;
 
@@ -83,7 +87,7 @@ void git_buf_free(git_buf *buf)
 {
 	if (!buf) return;
 
-	if (buf->ptr != git_buf_initbuf)
+	if (buf->ptr != git_buf_initbuf && buf->ptr != &git_buf__oom)
 		git__free(buf->ptr);
 
 	git_buf_init(buf, 0);
@@ -98,12 +102,12 @@ void git_buf_clear(git_buf *buf)
 
 int git_buf_oom(const git_buf *buf)
 {
-	return (buf->asize < 0);
+	return (buf->ptr == &git_buf__oom);
 }
 
 int git_buf_lasterror(const git_buf *buf)
 {
-	return (buf->asize < 0) ? GIT_ENOMEM : GIT_SUCCESS;
+	return (buf->ptr == &git_buf__oom) ? GIT_ENOMEM : GIT_SUCCESS;
 }
 
 int git_buf_set(git_buf *buf, const char *data, size_t len)
@@ -162,11 +166,12 @@ int git_buf_printf(git_buf *buf, const char *format, ...)
 		va_end(arglist);
 
 		if (len < 0) {
-			buf->asize = -1;
+			free(buf->ptr);
+			buf->ptr = &git_buf__oom;
 			return GIT_ENOMEM;
 		}
 
-		if (len + 1 <= buf->asize - buf->size) {
+		if ((size_t)len + 1 <= buf->asize - buf->size) {
 			buf->size += len;
 			break;
 		}
@@ -205,9 +210,9 @@ void git_buf_consume(git_buf *buf, const char *end)
 	}
 }
 
-void git_buf_truncate(git_buf *buf, ssize_t len)
+void git_buf_truncate(git_buf *buf, size_t len)
 {
-	if (len >= 0 && len < buf->size) {
+	if (len < buf->size) {
 		buf->size = len;
 		buf->ptr[buf->size] = '\0';
 	}
@@ -238,7 +243,7 @@ char *git_buf_detach(git_buf *buf)
 	return data;
 }
 
-void git_buf_attach(git_buf *buf, char *ptr, ssize_t asize)
+void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
 {
 	git_buf_free(buf);
 
@@ -372,3 +377,13 @@ int git_buf_join(
 
 	return error;
 }
+
+void git_buf_rtrim(git_buf *buf)
+{
+	while (buf->size > 0) {
+		if (!isspace(buf->ptr[buf->size - 1]))
+			break;
+
+		buf->size--;
+	}
+}
diff --git a/src/buffer.h b/src/buffer.h
index 3969f46..3e9cb17 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -11,7 +11,7 @@
 
 typedef struct {
 	char *ptr;
-	ssize_t asize, size;
+	size_t asize, size;
 } git_buf;
 
 extern char git_buf_initbuf[];
@@ -47,7 +47,7 @@ int git_buf_try_grow(git_buf *buf, size_t target_size);
 void git_buf_free(git_buf *buf);
 void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
 char *git_buf_detach(git_buf *buf);
-void git_buf_attach(git_buf *buf, char *ptr, ssize_t asize);
+void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
 
 /**
  * Test if there have been any reallocation failures with this git_buf.
@@ -83,7 +83,7 @@ int git_buf_puts(git_buf *buf, const char *string);
 int git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
 void git_buf_clear(git_buf *buf);
 void git_buf_consume(git_buf *buf, const char *end);
-void git_buf_truncate(git_buf *buf, ssize_t len);
+void git_buf_truncate(git_buf *buf, size_t len);
 void git_buf_rtruncate_at_char(git_buf *path, char separator);
 
 int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...);
@@ -115,4 +115,7 @@ GIT_INLINE(int) git_buf_rfind_next(git_buf *buf, char ch)
 	return idx;
 }
 
+/* Remove whitespace from the end of the buffer */
+void git_buf_rtrim(git_buf *buf);
+
 #endif
diff --git a/src/config_file.c b/src/config_file.c
index c9c7d11..ce76493 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -73,7 +73,7 @@ typedef struct {
 	git_hashtable *values;
 
 	struct {
-		git_fbuffer buffer;
+		git_buf buffer;
 		char *read_ptr;
 		int line_number;
 		int eof;
@@ -151,6 +151,7 @@ static int config_open(git_config_file *cfg)
 	if (b->values == NULL)
 		return GIT_ENOMEM;
 
+	git_buf_init(&b->reader.buffer, 0);
 	error = git_futils_readbuffer(&b->reader.buffer, b->file_path);
 
 	/* It's fine if the file doesn't exist */
@@ -164,14 +165,14 @@ static int config_open(git_config_file *cfg)
 	if (error < GIT_SUCCESS)
 		goto cleanup;
 
-	git_futils_freebuffer(&b->reader.buffer);
+	git_buf_free(&b->reader.buffer);
 
 	return GIT_SUCCESS;
 
  cleanup:
 	free_vars(b->values);
 	b->values = NULL;
-	git_futils_freebuffer(&b->reader.buffer);
+	git_buf_free(&b->reader.buffer);
 
 	return git__rethrow(error, "Failed to open config");
 }
@@ -765,7 +766,7 @@ static int skip_bom(diskfile_backend *cfg)
 {
 	static const char utf8_bom[] = "\xef\xbb\xbf";
 
-	if (cfg->reader.buffer.len < sizeof(utf8_bom))
+	if (cfg->reader.buffer.size < sizeof(utf8_bom))
 		return GIT_SUCCESS;
 
 	if (memcmp(cfg->reader.read_ptr, utf8_bom, sizeof(utf8_bom)) == 0)
@@ -847,7 +848,7 @@ static int config_parse(diskfile_backend *cfg_file)
 	git_buf buf = GIT_BUF_INIT;
 
 	/* Initialize the reading position */
-	cfg_file->reader.read_ptr = cfg_file->reader.buffer.data;
+	cfg_file->reader.read_ptr = cfg_file->reader.buffer.ptr;
 	cfg_file->reader.eof = 0;
 
 	/* If the file is empty, there's nothing for us to do */
@@ -976,10 +977,9 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 		cfg->reader.read_ptr = NULL;
 		cfg->reader.eof = 1;
 		data_start = NULL;
-		cfg->reader.buffer.len = 0;
-		cfg->reader.buffer.data = NULL;
+		git_buf_clear(&cfg->reader.buffer);
 	} else {
-		cfg->reader.read_ptr = cfg->reader.buffer.data;
+		cfg->reader.read_ptr = cfg->reader.buffer.ptr;
 		cfg->reader.eof = 0;
 		data_start = cfg->reader.read_ptr;
 	}
@@ -1093,7 +1093,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 
 			/* And then the write out rest of the file */
 			error = git_filebuf_write(&file, post_start,
-						cfg->reader.buffer.len - (post_start - data_start));
+						cfg->reader.buffer.size - (post_start - data_start));
 
 			if (error < GIT_SUCCESS) {
 				git__rethrow(error, "Failed to write the rest of the file");
@@ -1128,7 +1128,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 		goto cleanup;
 	}
 
-	error = git_filebuf_write(&file, cfg->reader.buffer.data, cfg->reader.buffer.len);
+	error = git_filebuf_write(&file, cfg->reader.buffer.ptr, cfg->reader.buffer.size);
 	if (error < GIT_SUCCESS) {
 		git__rethrow(error, "Failed to write original config content");
 		goto cleanup;
@@ -1155,7 +1155,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 	else
 		error = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
 
-	git_futils_freebuffer(&cfg->reader.buffer);
+	git_buf_free(&cfg->reader.buffer);
 	return error;
 }
 
diff --git a/src/fileops.c b/src/fileops.c
index 3241c68..d2b4af5 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -97,87 +97,77 @@ mode_t git_futils_canonical_mode(mode_t raw_mode)
 		return 0;
 }
 
-int git_futils_readbuffer_updated(git_fbuffer *obj, const char *path, time_t *mtime, int *updated)
+int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime, int *updated)
 {
 	git_file fd;
 	size_t len;
 	struct stat st;
-	unsigned char *buff;
 
-	assert(obj && path && *path);
+	assert(buf && path && *path);
 
 	if (updated != NULL)
 		*updated = 0;
 
-	if (p_stat(path, &st) < 0)
-		return git__throw(GIT_ENOTFOUND, "Failed to stat file %s", path);
+	if ((fd = p_open(path, O_RDONLY)) < 0) {
+		return git__throw(GIT_ENOTFOUND, "Failed to read file '%s': %s", path, strerror(errno));
+	}
 
-	if (S_ISDIR(st.st_mode))
-		return git__throw(GIT_ERROR, "Can't read a dir into a buffer");
+	if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
+		close(fd);
+		return git__throw(GIT_EOSERR, "Failed to stat file '%s'", path);
+	}
 
 	/*
 	 * If we were given a time, we only want to read the file if it
 	 * has been modified.
 	 */
-	if (mtime != NULL && *mtime >= st.st_mtime)
-		return GIT_SUCCESS;
+	if (mtime != NULL && *mtime >= st.st_mtime) {
+		close(fd);
+		return 0;
+	}
 
 	if (mtime != NULL)
 		*mtime = st.st_mtime;
-	if (!git__is_sizet(st.st_size+1))
-		return git__throw(GIT_ERROR, "Failed to read file `%s`. An error occured while calculating its size", path);
 
 	len = (size_t) st.st_size;
 
-	if ((fd = p_open(path, O_RDONLY)) < 0)
-		return git__throw(GIT_EOSERR, "Failed to open %s for reading", path);
+	git_buf_clear(buf);
 
-	if ((buff = git__malloc(len + 1)) == NULL) {
-		p_close(fd);
+	if (git_buf_grow(buf, len + 1) < 0) {
+		close(fd);
 		return GIT_ENOMEM;
 	}
 
-	if (p_read(fd, buff, len) < 0) {
-		p_close(fd);
-		git__free(buff);
-		return git__throw(GIT_ERROR, "Failed to read file `%s`", path);
+	buf->ptr[len] = '\0';
+
+	while (len > 0) {
+		ssize_t read_size = p_read(fd, buf->ptr, len);
+
+		if (read_size < 0) {
+			close(fd);
+			return git__throw(GIT_EOSERR, "Failed to read from FD");
+		}
+
+		len -= read_size;
+		buf->size += read_size;
 	}
-	buff[len] = '\0';
 
 	p_close(fd);
 
 	if (mtime != NULL)
 		*mtime = st.st_mtime;
+
 	if (updated != NULL)
 		*updated = 1;
 
-	obj->data = buff;
-	obj->len = len;
-
-	return GIT_SUCCESS;
-}
-
-int git_futils_readbuffer(git_fbuffer *obj, const char *path)
-{
-	return git_futils_readbuffer_updated(obj, path, NULL, NULL);
+	return 0;
 }
 
-void git_futils_fbuffer_rtrim(git_fbuffer *obj)
+int git_futils_readbuffer(git_buf *buf, const char *path)
 {
-	unsigned char *buff = obj->data;
-	while (obj->len > 0 && isspace(buff[obj->len - 1]))
-		obj->len--;
-	buff[obj->len] = '\0';
+	return git_futils_readbuffer_updated(buf, path, NULL, NULL);
 }
 
-void git_futils_freebuffer(git_fbuffer *obj)
-{
-	assert(obj);
-	git__free(obj->data);
-	obj->data = NULL;
-}
-
-
 int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
 {
 	if (git_futils_mkpath2file(to, dirmode) < GIT_SUCCESS)
diff --git a/src/fileops.h b/src/fileops.h
index 4c11402..43ef215 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -17,17 +17,8 @@
  *
  * Read whole files into an in-memory buffer for processing
  */
-#define GIT_FBUFFER_INIT {NULL, 0}
-
-typedef struct { /* file io buffer */
-	void *data; /* data bytes	*/
-	size_t len; /* data length */
-} git_fbuffer;
-
-extern int git_futils_readbuffer(git_fbuffer *obj, const char *path);
-extern int git_futils_readbuffer_updated(git_fbuffer *obj, const char *path, time_t *mtime, int *updated);
-extern void git_futils_freebuffer(git_fbuffer *obj);
-extern void git_futils_fbuffer_rtrim(git_fbuffer *obj);
+extern int git_futils_readbuffer(git_buf *obj, const char *path);
+extern int git_futils_readbuffer_updated(git_buf *obj, const char *path, time_t *mtime, int *updated);
 
 /**
  * File utils
diff --git a/src/ignore.c b/src/ignore.c
index 30f86b8..a3bf0a2 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -11,7 +11,7 @@ static int load_ignore_file(
 	git_repository *repo, const char *path, git_attr_file *ignores)
 {
 	int error = GIT_SUCCESS;
-	git_fbuffer fbuf = GIT_FBUFFER_INIT;
+	git_buf fbuf = GIT_BUF_INIT;
 	git_attr_fnmatch *match = NULL;
 	const char *scan = NULL;
 	char *context = NULL;
@@ -28,7 +28,7 @@ static int load_ignore_file(
 	if (error == GIT_SUCCESS)
 		error = git_futils_readbuffer(&fbuf, path);
 
-	scan = fbuf.data;
+	scan = fbuf.ptr;
 
 	while (error == GIT_SUCCESS && *scan) {
 		if (!match && !(match = git__calloc(1, sizeof(git_attr_fnmatch)))) {
@@ -53,7 +53,7 @@ static int load_ignore_file(
 		}
 	}
 
-	git_futils_freebuffer(&fbuf);
+	git_buf_free(&fbuf);
 	git__free(match);
 	git__free(context);
 
diff --git a/src/index.c b/src/index.c
index 4dccad5..5ac99de 100644
--- a/src/index.c
+++ b/src/index.c
@@ -216,7 +216,7 @@ void git_index_clear(git_index *index)
 int git_index_read(git_index *index)
 {
 	int error = GIT_SUCCESS, updated;
-	git_fbuffer buffer = GIT_FBUFFER_INIT;
+	git_buf buffer = GIT_BUF_INIT;
 	time_t mtime;
 
 	assert(index->index_file_path);
@@ -235,12 +235,12 @@ int git_index_read(git_index *index)
 
 	if (updated) {
 		git_index_clear(index);
-		error = parse_index(index, buffer.data, buffer.len);
+		error = parse_index(index, buffer.ptr, buffer.size);
 
 		if (error == GIT_SUCCESS)
 			index->last_modified = mtime;
 
-		git_futils_freebuffer(&buffer);
+		git_buf_free(&buffer);
 	}
 
 	if (error < GIT_SUCCESS)
diff --git a/src/odb.c b/src/odb.c
index 4eaf289..81fc82b 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -393,8 +393,8 @@ static int add_default_backends(git_odb *db, const char *objects_dir, int as_alt
 static int load_alternates(git_odb *odb, const char *objects_dir)
 {
 	git_buf alternates_path = GIT_BUF_INIT;
+	git_buf alternates_buf = GIT_BUF_INIT;
 	char *buffer;
-	git_fbuffer alternates_buf = GIT_FBUFFER_INIT;
 	const char *alternate;
 	int error;
 
@@ -412,7 +412,7 @@ static int load_alternates(git_odb *odb, const char *objects_dir)
 		return git__throw(GIT_EOSERR, "Failed to add backend. Can't read alternates");
 	}
 
-	buffer = (char *)alternates_buf.data;
+	buffer = (char *)alternates_buf.ptr;
 	error = GIT_SUCCESS;
 
 	/* add each alternate as a new backend; one alternate per line */
@@ -433,7 +433,8 @@ static int load_alternates(git_odb *odb, const char *objects_dir)
 	}
 
 	git_buf_free(&alternates_path);
-	git_futils_freebuffer(&alternates_buf);
+	git_buf_free(&alternates_buf);
+
 	if (error < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to load alternates");
 	return error;
diff --git a/src/odb_loose.c b/src/odb_loose.c
index bb2b7b5..f5f6e35 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -75,13 +75,13 @@ static int object_file_name(git_buf *name, const char *dir, const git_oid *id)
 }
 
 
-static size_t get_binary_object_header(obj_hdr *hdr, git_fbuffer *obj)
+static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj)
 {
 	unsigned char c;
-	unsigned char *data = obj->data;
+	unsigned char *data = (unsigned char *)obj->ptr;
 	size_t shift, size, used = 0;
 
-	if (obj->len == 0)
+	if (obj->size == 0)
 		return 0;
 
 	c = data[used++];
@@ -90,7 +90,7 @@ static size_t get_binary_object_header(obj_hdr *hdr, git_fbuffer *obj)
 	size = c & 15;
 	shift = 4;
 	while (c & 0x80) {
-		if (obj->len <= used)
+		if (obj->size <= used)
 			return 0;
 		if (sizeof(size_t) * 8 <= shift)
 			return 0;
@@ -177,12 +177,12 @@ static void set_stream_output(z_stream *s, void *out, size_t len)
 }
 
 
-static int start_inflate(z_stream *s, git_fbuffer *obj, void *out, size_t len)
+static int start_inflate(z_stream *s, git_buf *obj, void *out, size_t len)
 {
 	int status;
 
 	init_stream(s, out, len);
-	set_stream_input(s, obj->data, obj->len);
+	set_stream_input(s, obj->ptr, obj->size);
 
 	if ((status = inflateInit(s)) < Z_OK)
 		return status;
@@ -287,7 +287,7 @@ static void *inflate_tail(z_stream *s, void *hb, size_t used, obj_hdr *hdr)
  * of loose object data into packs. This format is no longer used, but
  * we must still read it.
  */
-static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_fbuffer *obj)
+static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_buf *obj)
 {
 	unsigned char *in, *buf;
 	obj_hdr hdr;
@@ -310,8 +310,8 @@ static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_fbuffer *obj)
 	if (!buf)
 		return GIT_ENOMEM;
 
-	in = ((unsigned char *)obj->data) + used;
-	len = obj->len - used;
+	in = ((unsigned char *)obj->ptr) + used;
+	len = obj->size - used;
 	if (inflate_buffer(in, len, buf, hdr.size)) {
 		git__free(buf);
 		return git__throw(GIT_ERROR, "Failed to inflate loose object. Could not inflate buffer");
@@ -325,7 +325,7 @@ static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_fbuffer *obj)
 	return GIT_SUCCESS;
 }
 
-static int inflate_disk_obj(git_rawobj *out, git_fbuffer *obj)
+static int inflate_disk_obj(git_rawobj *out, git_buf *obj)
 {
 	unsigned char head[64], *buf;
 	z_stream zs;
@@ -335,7 +335,7 @@ static int inflate_disk_obj(git_rawobj *out, git_fbuffer *obj)
 	/*
 	 * check for a pack-like loose object
 	 */
-	if (!is_zlib_compressed_data(obj->data))
+	if (!is_zlib_compressed_data((unsigned char *)obj->ptr))
 		return inflate_packlike_loose_disk_obj(out, obj);
 
 	/*
@@ -383,7 +383,7 @@ static int inflate_disk_obj(git_rawobj *out, git_fbuffer *obj)
 static int read_loose(git_rawobj *out, git_buf *loc)
 {
 	int error;
-	git_fbuffer obj = GIT_FBUFFER_INIT;
+	git_buf obj = GIT_BUF_INIT;
 
 	assert(out && loc);
 
@@ -398,7 +398,7 @@ static int read_loose(git_rawobj *out, git_buf *loc)
 		return git__throw(GIT_ENOTFOUND, "Failed to read loose object. File not found");
 
 	error = inflate_disk_obj(out, &obj);
-	git_futils_freebuffer(&obj);
+	git_buf_free(&obj);
 
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to read loose object");
 }
diff --git a/src/reflog.c b/src/reflog.c
index 9f5ccd3..6ca9418 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -183,7 +183,7 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref)
 {
 	int error;
 	git_buf log_path = GIT_BUF_INIT;
-	git_fbuffer log_file = GIT_FBUFFER_INIT;
+	git_buf log_file = GIT_BUF_INIT;
 	git_reflog *log = NULL;
 
 	*reflog = NULL;
@@ -201,7 +201,7 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref)
 		goto cleanup;
 	}
 
-	if ((error = reflog_parse(log, log_file.data, log_file.len)) < GIT_SUCCESS)
+	if ((error = reflog_parse(log, log_file.ptr, log_file.size)) < GIT_SUCCESS)
 		git__rethrow(error, "Failed to read reflog");
 	else
 		*reflog = log;
@@ -209,7 +209,7 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref)
 cleanup:
 	if (error != GIT_SUCCESS && log != NULL)
 		git_reflog_free(log);
-	git_futils_freebuffer(&log_file);
+	git_buf_free(&log_file);
 	git_buf_free(&log_path);
 
 	return error;
diff --git a/src/refs.c b/src/refs.c
index 8e911c1..2e1d92d 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -32,15 +32,15 @@ struct packref {
 static const int default_table_size = 32;
 
 static int reference_read(
-	git_fbuffer *file_content,
+	git_buf *file_content,
 	time_t *mtime,
 	const char *repo_path,
 	const char *ref_name,
 	int *updated);
 
 /* loose refs */
-static int loose_parse_symbolic(git_reference *ref, git_fbuffer *file_content);
-static int loose_parse_oid(git_oid *ref, git_fbuffer *file_content);
+static int loose_parse_symbolic(git_reference *ref, git_buf *file_content);
+static int loose_parse_oid(git_oid *ref, git_buf *file_content);
 static int loose_lookup(git_reference *ref);
 static int loose_lookup_to_packfile(struct packref **ref_out,
 	git_repository *repo, const char *name);
@@ -113,7 +113,7 @@ static int reference_alloc(
 	return GIT_SUCCESS;
 }
 
-static int reference_read(git_fbuffer *file_content, time_t *mtime, const char *repo_path, const char *ref_name, int *updated)
+static int reference_read(git_buf *file_content, time_t *mtime, const char *repo_path, const char *ref_name, int *updated)
 {
 	git_buf path = GIT_BUF_INIT;
 	int     error = GIT_SUCCESS;
@@ -129,15 +129,15 @@ static int reference_read(git_fbuffer *file_content, time_t *mtime, const char *
 	return error;
 }
 
-static int loose_parse_symbolic(git_reference *ref, git_fbuffer *file_content)
+static int loose_parse_symbolic(git_reference *ref, git_buf *file_content)
 {
 	const unsigned int header_len = strlen(GIT_SYMREF);
 	const char *refname_start;
 	char *eol;
 
-	refname_start = (const char *)file_content->data;
+	refname_start = (const char *)file_content->ptr;
 
-	if (file_content->len < (header_len + 1))
+	if (file_content->size < (header_len + 1))
 		return git__throw(GIT_EOBJCORRUPTED,
 			"Failed to parse loose reference. Object too short");
 
@@ -165,15 +165,15 @@ static int loose_parse_symbolic(git_reference *ref, git_fbuffer *file_content)
 	return GIT_SUCCESS;
 }
 
-static int loose_parse_oid(git_oid *oid, git_fbuffer *file_content)
+static int loose_parse_oid(git_oid *oid, git_buf *file_content)
 {
 	int error;
 	char *buffer;
 
-	buffer = (char *)file_content->data;
+	buffer = (char *)file_content->ptr;
 
 	/* File format: 40 chars (OID) + newline */
-	if (file_content->len < GIT_OID_HEXSZ + 1)
+	if (file_content->size < GIT_OID_HEXSZ + 1)
 		return git__throw(GIT_EOBJCORRUPTED,
 			"Failed to parse loose reference. Reference too short");
 
@@ -193,26 +193,26 @@ static int loose_parse_oid(git_oid *oid, git_fbuffer *file_content)
 
 static git_rtype loose_guess_rtype(const git_buf *full_path)
 {
-	git_fbuffer ref_file = GIT_FBUFFER_INIT;
+	git_buf ref_file = GIT_BUF_INIT;
 	git_rtype type;
 
 	type = GIT_REF_INVALID;
 
 	if (git_futils_readbuffer(&ref_file, full_path->ptr) == GIT_SUCCESS) {
-		if (git__prefixcmp((const char *)(ref_file.data), GIT_SYMREF) == 0)
+		if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0)
 			type = GIT_REF_SYMBOLIC;
 		else
 			type = GIT_REF_OID;
 	}
 
-	git_futils_freebuffer(&ref_file);
+	git_buf_free(&ref_file);
 	return type;
 }
 
 static int loose_lookup(git_reference *ref)
 {
 	int error = GIT_SUCCESS, updated;
-	git_fbuffer ref_file = GIT_FBUFFER_INIT;
+	git_buf ref_file = GIT_BUF_INIT;
 
 	if (reference_read(&ref_file, &ref->mtime,
 			ref->owner->path_repository, ref->name, &updated) < GIT_SUCCESS)
@@ -228,7 +228,7 @@ static int loose_lookup(git_reference *ref)
 
 	ref->flags = 0;
 
-	if (git__prefixcmp((const char *)(ref_file.data), GIT_SYMREF) == 0) {
+	if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0) {
 		ref->flags |= GIT_REF_SYMBOLIC;
 		error = loose_parse_symbolic(ref, &ref_file);
 	} else {
@@ -236,7 +236,7 @@ static int loose_lookup(git_reference *ref)
 		error = loose_parse_oid(&ref->target.oid, &ref_file);
 	}
 
-	git_futils_freebuffer(&ref_file);
+	git_buf_free(&ref_file);
 
 	if (error < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to lookup loose reference");
@@ -250,7 +250,7 @@ static int loose_lookup_to_packfile(
 		const char *name)
 {
 	int error = GIT_SUCCESS;
-	git_fbuffer ref_file = GIT_FBUFFER_INIT;
+	git_buf ref_file = GIT_BUF_INIT;
 	struct packref *ref = NULL;
 	size_t name_len;
 
@@ -273,11 +273,11 @@ static int loose_lookup_to_packfile(
 	ref->flags = GIT_PACKREF_WAS_LOOSE;
 
 	*ref_out = ref;
-	git_futils_freebuffer(&ref_file);
+	git_buf_free(&ref_file);
 	return GIT_SUCCESS;
 
 cleanup:
-	git_futils_freebuffer(&ref_file);
+	git_buf_free(&ref_file);
 	free(ref);
 	return git__rethrow(error, "Failed to lookup loose reference");
 }
@@ -427,7 +427,7 @@ cleanup:
 static int packed_load(git_repository *repo)
 {
 	int error = GIT_SUCCESS, updated;
-	git_fbuffer packfile = GIT_FBUFFER_INIT;
+	git_buf packfile = GIT_BUF_INIT;
 	const char *buffer_start, *buffer_end;
 	git_refcache *ref_cache = &repo->references;
 
@@ -468,8 +468,8 @@ static int packed_load(git_repository *repo)
 
 	git_hashtable_clear(ref_cache->packfile);
 
-	buffer_start = (const char *)packfile.data;
-	buffer_end = (const char *)(buffer_start) + packfile.len;
+	buffer_start = (const char *)packfile.ptr;
+	buffer_end = (const char *)(buffer_start) + packfile.size;
 
 	while (buffer_start < buffer_end && buffer_start[0] == '#') {
 		buffer_start = strchr(buffer_start, '\n');
@@ -500,13 +500,13 @@ static int packed_load(git_repository *repo)
 		}
 	}
 
-	git_futils_freebuffer(&packfile);
+	git_buf_free(&packfile);
 	return GIT_SUCCESS;
 
 cleanup:
 	git_hashtable_free(ref_cache->packfile);
 	ref_cache->packfile = NULL;
-	git_futils_freebuffer(&packfile);
+	git_buf_free(&packfile);
 	return git__rethrow(error, "Failed to load packed references");
 }
 
diff --git a/src/repository.c b/src/repository.c
index f394d06..c46dd9d 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -467,7 +467,7 @@ static int retrieve_ceiling_directories_offset(
  */
 static int read_gitfile(git_buf *path_out, const char *file_path, const char *base_path)
 {
-	git_fbuffer file;
+	git_buf file = GIT_BUF_INIT;
 	int error;
 
 	assert(path_out && file_path);
@@ -476,22 +476,22 @@ static int read_gitfile(git_buf *path_out, const char *file_path, const char *ba
 	if (error < GIT_SUCCESS)
 		return error;
 
-	if (git__prefixcmp((char *)file.data, GIT_FILE_CONTENT_PREFIX)) {
-		git_futils_freebuffer(&file);
+	if (git__prefixcmp((char *)file.ptr, GIT_FILE_CONTENT_PREFIX)) {
+		git_buf_free(&file);
 		return git__throw(GIT_ENOTFOUND, "Invalid gitfile format `%s`", file_path);
 	}
 
-	git_futils_fbuffer_rtrim(&file);
+	git_buf_rtrim(&file);
 
-	if (strlen(GIT_FILE_CONTENT_PREFIX) == file.len) {
-		git_futils_freebuffer(&file);
+	if (strlen(GIT_FILE_CONTENT_PREFIX) == file.size) {
+		git_buf_free(&file);
 		return git__throw(GIT_ENOTFOUND, "No path in git file `%s`", file_path);
 	}
 
 	error = git_path_prettify_dir(path_out,
-		((char *)file.data) + strlen(GIT_FILE_CONTENT_PREFIX), base_path);
+		((char *)file.ptr) + strlen(GIT_FILE_CONTENT_PREFIX), base_path);
 
-	git_futils_freebuffer(&file);
+	git_buf_free(&file);
 
 	if (error == GIT_SUCCESS && git_path_exists(path_out->ptr) == 0)
 		return GIT_SUCCESS;
diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c
index 740cd85..870525b 100644
--- a/tests-clar/core/buffer.c
+++ b/tests-clar/core/buffer.c
@@ -218,8 +218,8 @@ check_buf_append(
 	const char* data_a,
 	const char* data_b,
 	const char* expected_data,
-	ssize_t expected_size,
-	ssize_t expected_asize)
+	size_t expected_size,
+	size_t expected_asize)
 {
 	git_buf tgt = GIT_BUF_INIT;
 
@@ -371,8 +371,8 @@ void test_core_buffer__7(void)
 	git_buf_attach(&a, b, 0);
 
 	cl_assert_strequal(fun, a.ptr);
-	cl_assert(a.size == (ssize_t)strlen(fun));
-	cl_assert(a.asize == (ssize_t)strlen(fun) + 1);
+	cl_assert(a.size == strlen(fun));
+	cl_assert(a.asize == strlen(fun) + 1);
 
 	git_buf_free(&a);
 
@@ -380,8 +380,8 @@ void test_core_buffer__7(void)
 	git_buf_attach(&a, b, strlen(fun) + 1);
 
 	cl_assert_strequal(fun, a.ptr);
-	cl_assert(a.size == (ssize_t)strlen(fun));
-	cl_assert(a.asize == (ssize_t)strlen(fun) + 1);
+	cl_assert(a.size == strlen(fun));
+	cl_assert(a.asize == strlen(fun) + 1);
 
 	git_buf_free(&a);
 }
diff --git a/tests-clar/core/path.c b/tests-clar/core/path.c
index 3ff5d7d..c07362f 100644
--- a/tests-clar/core/path.c
+++ b/tests-clar/core/path.c
@@ -243,7 +243,7 @@ void test_core_path__07_path_to_dir(void)
 void test_core_path__08_self_join(void)
 {
 	git_buf path = GIT_BUF_INIT;
-	ssize_t asize = 0;
+	size_t asize = 0;
 
 	asize = path.asize;
 	cl_git_pass(git_buf_sets(&path, "/foo"));
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index 42c8031..8373584 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -182,7 +182,7 @@ int cmp_objects(git_rawobj *o, object_data *d)
 
 int copy_file(const char *src, const char *dst)
 {
-	git_fbuffer source_buf;
+	git_buf source_buf = GIT_BUF_INIT;
 	git_file dst_fd;
 	int error = GIT_ERROR;
 
@@ -193,10 +193,10 @@ int copy_file(const char *src, const char *dst)
 	if (dst_fd < 0)
 		goto cleanup;
 
-	error = p_write(dst_fd, source_buf.data, source_buf.len);
+	error = p_write(dst_fd, source_buf.ptr, source_buf.size);
 
 cleanup:
-	git_futils_freebuffer(&source_buf);
+	git_buf_free(&source_buf);
 	p_close(dst_fd);
 
 	return error;
@@ -204,22 +204,23 @@ cleanup:
 
 int cmp_files(const char *a, const char *b)
 {
-	git_fbuffer buf_a, buf_b;
+	git_buf buf_a = GIT_BUF_INIT;
+	git_buf buf_b = GIT_BUF_INIT;
 	int error = GIT_ERROR;
 
 	if (git_futils_readbuffer(&buf_a, a) < GIT_SUCCESS)
 		return GIT_ERROR;
 
 	if (git_futils_readbuffer(&buf_b, b) < GIT_SUCCESS) {
-		git_futils_freebuffer(&buf_a);
+		git_buf_free(&buf_a);
 		return GIT_ERROR;
 	}
 
-	if (buf_a.len == buf_b.len && !memcmp(buf_a.data, buf_b.data, buf_a.len))
+	if (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size))
 		error = GIT_SUCCESS;
 
-	git_futils_freebuffer(&buf_a);
-	git_futils_freebuffer(&buf_b);
+	git_buf_free(&buf_a);
+	git_buf_free(&buf_b);
 
 	return error;
 }