8000 Move `git_off_t` to `git_object_size_t` · libgit2/libgit2@88fc191 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88fc191

Browse files
committed
Move git_off_t to git_object_size_t
`git_off_t` is problematic: first, it's used to represent the size of a blob, but that's represented in git as an `unsigned long`, meaning that we've lost a bit (on 64-bit machines). Second, `git_off_t` is a poor name, suggesting that it's an offset, not a size (which makes sense, given that it's a signed type). Migrate `git_off_t` to `git_object_size_t`, an unsigned 64-bit type.
1 parent 37e4c1b commit 88fc191

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+218
-202
lines changed

include/git2/blob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
9494
* @param blob pointer to the blob
9595
* @return size on bytes
9696
*/
97-
GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
97+
GIT_EXTERN(git_object_size_t) git_blob_rawsize(const git_blob *blob);
9898

9999
/**
100100
* Get a buffer with the filtered content of a blob.

include/git2/deprecated.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ GIT_EXTERN(int) git_index_add_frombuffer(
289289

290290
GIT_EXTERN(size_t) git_object__size(git_object_t type);
291291

292+
typedef git_object_size_t git_off_t;
293+
292294
/**@}*/
293295

294296
/** @name Deprecated Reference Constants

include/git2/diff.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ typedef enum {
258258
* abbreviated to something reasonable, like 7 characters.
259259
*/
260260
typedef struct {
261-
git_oid id;
262-
const char *path;
263-
git_off_t size;
264-
uint32_t flags;
265-
uint16_t mode;
266-
uint16_t id_abbrev;
261+
git_oid id;
262+
const char *path;
263+
git_object_size_t size;
264+
uint32_t flags;
265+
uint16_t mode;
266+
uint16_t id_abbrev;
267267
} git_diff_file;
268268

269269
/**
@@ -414,10 +414,10 @@ typedef struct {
414414

415415
/**
416416
* A size (in bytes) above which a blob will be marked as binary
417-
* automatically; pass a negative value to disable.
417+
* automatically; pass `GIT_OBJECT_SIZE_MAX` to disable.
418418
* Defaults to 512MB.
419419
*/
420-
git_off_t max_size;
420+
git_object_size_t max_size;
421421

422422
/**
423423
* The virtual "directory" prefix for old file names in hunk headers.
@@ -603,7 +603,7 @@ typedef struct {
603603
int new_lineno; /**< Line number in new file or -1 for deleted line */
604604
int num_lines; /**< Number of newline characters in content */
605605
size_t content_len; /**< Number of bytes of data */
606-
git_off_t content_offset; /**< Offset in the original file to the content */
606+
git_object_size_t content_offset; /**< Offset in the original file to the content */
607607
const char *content; /**< Pointer to diff text, not NUL-byte terminated */
608608
} git_diff_line;
609609

include/git2/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
GIT_BEGIN_DECL
2323

24+
#define GIT_OBJECT_SIZE_MAX UINT64_MAX
25+
2426
/**
2527
* Lookup a reference to one of the objects in a repository.
2628
*

include/git2/odb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size
294294
* @param type type of the object that will be written
295295
* @return 0 if the stream was created; error code otherwise
296296
*/
297-
GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_off_t size, git_object_t type);
297+
GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_object_size_t size, git_object_t type);
298298

299299
/**
300300
* Write to an odb stream

include/git2/odb_backend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ struct git_odb_stream {
8787
unsigned int mode;
8888
void *hash_ctx;
8989

90-
git_off_t declared_size;
91-
git_off_t received_bytes;
90+
git_object_size_t declared_size;
91+
git_object_size_t received_bytes;
9292

9393
/**
9494
* Write at most `len` bytes into `buffer` and advance the stream.

include/git2/sys/odb_backend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct git_odb_backend {
5353
git_odb_backend *, const git_oid *, const void *, size_t, git_object_t);
5454

5555
int GIT_CALLBACK(writestream)(
56-
git_odb_stream **, git_odb_backend *, git_off_t, git_object_t);
56+
git_odb_stream **, git_odb_backend *, git_object_size_t, git_object_t);
5757

5858
int GIT_CALLBACK(readstream)(
5959
git_odb_stream **, size_t *, git_object_t *,

include/git2/types.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,14 @@ GIT_BEGIN_DECL
3838

3939
#if defined(_MSC_VER)
4040

41-
typedef __int64 git_off_t;
4241
typedef __time64_t git_time_t;
4342

4443
#elif defined(__MINGW32__)
4544

46-
typedef off64_t git_off_t;
4745
typedef __time64_t git_time_t;
4846

4947
#elif defined(__HAIKU__)
5048

51-
typedef __haiku_std_int64 git_off_t;
5249
typedef __haiku_std_int64 git_time_t;
5350

5451
#else /* POSIX */
@@ -58,7 +55,6 @@ typedef __haiku_std_int64 git_time_t;
5855
* before us (directly or indirectly), they'll get 32 bit off_t in their client
5956
* app, even though /we/ define _FILE_OFFSET_BITS=64.
6057
*/
61-
typedef int64_t git_off_t;
6258
typedef int64_t git_time_t; /**< time in seconds from epoch */
6359

6460
#endif
@@ -78,6 +74,9 @@ typedef enum {
7874
GIT_OBJECT_REF_DELTA = 7, /**< A delta, base is given by object id. */
7975
} git_object_t;
8076

77+
/** The maximum size of an object */
78+
typedef uint64_t git_object_size_t;
79+
8180
/** An open object database handle. */
8281
typedef struct git_odb git_odb;
8382

src/attr_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ int git_attr_file__load(
125125
break;
126126
case GIT_ATTR_FILE__FROM_INDEX: {
127127
git_oid id;
128-
git_off_t blobsize;
128+
git_object_size_t blobsize;
129129

130130
if ((error = attr_file_oid_from_index(&id, repo, entry->path)) < 0 ||
131131
(error = git_blob_lookup(&blob, repo, &id)) < 0)

src/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ static git_blame_hunk *split_hunk_in_vector(
268268
static int index_blob_lines(git_blame *blame)
269269
{
270270
const char *buf = blame->final_buf;
271-
git_off_t len = blame->final_buf_size;
271+
git_object_size_t len = blame->final_buf_size;
272272
int num = 0, incomplete = 0, bol = 1;
273273
size_t *i;
274274

src/blame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct git_blame {
8484
git_blame__entry *ent;
8585
int num_lines;
8686
const char *final_buf;
87-
git_off_t final_buf_size;
87+
git_object_size_t final_buf_size;
8888
};
8989

9090
git_blame *git_blame__alloc(

src/blob.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ const void *git_blob_rawcontent(const git_blob *blob)
2525
return git_odb_object_data(blob->data.odb);
2626
}
2727

28-
git_off_t git_blob_rawsize(const git_blob *blob)
28+
git_object_size_t git_blob_rawsize(const git_blob *blob)
2929
{
3030
assert(blob);
3131
if (blob->raw)
3232
return blob->data.raw.size;
3333
else
34-
return (git_off_t)git_odb_object_size(blob->data.odb);
34+
return (git_object_size_t)git_odb_object_size(blob->data.odb);
3535
}
3636

3737
int git_blob__getbuf(git_buf *buffer, git_blob *blob)
3838
{
39-
git_off_t size = git_blob_rawsize(blob);
39+
git_object_size_t size = git_blob_rawsize(blob);
4040

4141
GIT_ERROR_CHECK_BLOBSIZE(size);
4242
return git_buf_set(buffer, git_blob_rawcontent(blob), (size_t)size);
@@ -91,13 +91,13 @@ int git_blob_create_from_buffer(
9191
}
9292

9393
static int write_file_stream(
94-
git_oid *id, git_odb *odb, const char *path, git_off_t file_size)
94+
git_oid *id, git_odb *odb, const char *path, git_object_size_t file_size)
9595
{
9696
int fd, error;
9797
char buffer[FILEIO_BUFSIZE];
9898
git_odb_stream *stream = NULL;
9999
ssize_t read_len = -1;
100-
git_off_t written = 0;
100+
git_object_size_t written = 0;
101101

102102
if ((error = git_odb_open_wstream(
103103
&stream, odb, file_size, GIT_OBJECT_BLOB)) < 0)
@@ -129,7 +129,7 @@ static int write_file_stream(
129129

130130
static int write_file_filtered(
131131
git_oid *id,
132-
git_off_t *size,
132+
git_object_size_t *size,
133133
git_odb *odb,
134134
const char *full_path,
135135
git_filter_list *fl)
@@ -184,7 +184,7 @@ int git_blob__create_from_paths(
184184
int error;
185185
struct stat st;
186186
git_odb *odb = NULL;
187-
git_off_t size;
187+
git_object_size_t size;
188188
mode_t mode;
189189
git_buf path = GIT_BUF_INIT;
190190

@@ -389,7 +389,7 @@ int git_blob_create_from_stream_commit(git_oid *out, git_writestream *_stream)
389389
int git_blob_is_binary(const git_blob *blob)
390390
{
391391
git_buf content = GIT_BUF_INIT;
392-
git_off_t size;
392+
git_object_size_t size;
393393

394394
assert(blob);
395395

src/blob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct git_blob {
2121
git_odb_object *odb;
2222
struct {
2323
const char *data;
24-
git_off_t size;
24+
git_object_size_t size;
2525
} raw;
2626
} data;
2727
unsigned int raw:1;

src/crlf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static int has_cr_in_index(const git_filter_source *src)
7878
const git_index_entry *entry;
7979
git_blob *blob;
8080
const void *blobcontent;
81-
git_off_t blobsize;
81+
git_object_size_t blobsize;
8282
bool found_cr;
8383

8484
if (!path)

src/diff_file.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int diff_file_content_init_common(
4646
{
4747
fc->opts_flags = opts ? opts->flags : GIT_DIFF_NORMAL;
4848

49-
if (opts && opts->max_size >= 0)
49+
if (opts && (int64_t)opts->max_size >= 0)
5050
fc->opts_max_size = opts->max_size ?
5151
opts->max_size : DIFF_MAX_FILESIZE;
5252

@@ -62,7 +62,7 @@ static int diff_file_content_init_common(
6262
git_diff_driver_update_options(&fc->opts_flags, fc->driver);
6363

6464
/* make sure file is conceivable mmap-able */
65-
if ((git_off_t)((size_t)fc->file->size) != fc->file->size)
65+
if ((git_object_size_t)((size_t)fc->file->size) != fc->file->size)
6666
fc->file->flags |= GIT_DIFF_FLAG_BINARY;
6767
/* check if user is forcing text diff the file */
6868
else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) {
@@ -330,9 +330,11 @@ static int diff_file_content_load_workdir_file(
330330
if (fd < 0)
331331
return fd;
332332

333-
if (!fc->file->size &&
334-
!(fc->file->size = git_futils_filesize(fd)))
335-
goto cleanup;
333+
if (!fc->file->size) {
334+
if ((error = git_futils_filesize(&fc->file->size, fd)) < 0 ||
335+
!fc->file->size)
336+
goto cleanup;
337+
}
336338

337339
if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 &&
338340
diff_file_content_binary_by_size(fc))

src/diff_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef struct {
2020
git_diff_driver *driver;
2121
uint32_t flags;
2222
uint32_t opts_flags;
23-
git_off_t opts_max_size;
23+
git_object_size_t opts_max_size;
2424
git_iterator_type_t src;
2525
const git_blob *blob;
2626
git_map map;

src/diff_generate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,11 @@ int git_diff__oid_for_file(
560560
git_diff *diff,
561561
const char *path,
562562
uint16_t mode,
563-
git_off_t size)
563+
git_object_size_t size)
564564
{
565565
git_index_entry entry;
566566

567-
if (size < 0 || size > UINT32_MAX) {
567+
if (size > UINT32_MAX) {
568568
git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", path);
569569
return -1;
570570
}

src/diff_generate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extern int git_diff__oid_for_file(
8888
git_diff *diff,
8989
const char *path,
9090
uint16_t mode,
91-
git_off_t size);
91+
git_object_size_t size);
9292

9393
extern int git_diff__oid_for_entry(
9494
git_oid *out,
@@ -120,7 +120,7 @@ GIT_INLINE(int) git_diff_file__resolve_zero_size(
120120
git_odb_free(odb);
121121

122122
if (!error)
123-
file->size = (git_off_t)len;
123+
file->size = (git_object_size_t)len;
124124

125125
return error;
126126
}

src/diff_stats.c

Lines changed: 1 addition & 1 deletion
F438
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int git_diff_file_stats__full_to_buf(
5555
{
5656
const char *old_path = NULL, *new_path = NULL;
5757
size_t padding;
58-
git_off_t old_size, new_size;
58+
git_object_size_t old_size, new_size;
5959

6060
old_path = delta->old_file.path;
6161
new_path = delta->new_file.path;

src/fileops.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int git_futils_truncate(const char *path, int mode)
112112
return 0;
113113
}
114114

115-
git_off_t git_futils_filesize(git_file fd)
115+
int git_futils_filesize(git_object_size_t *out, git_file fd)
116116
{
117117
struct stat sb;
118118

@@ -121,7 +121,8 @@ git_off_t git_futils_filesize(git_file fd)
121121
return -1;
122122
}
123123

124-
return sb.st_size;
124+
*out = sb.st_size;
125+
return 0;
125126
}
126127

127128
mode_t git_futils_canonical_mode(mode_t raw_mode)
@@ -301,24 +302,22 @@ int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmod
301302
return 0;
302303
}
303304

304-
int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
305+
int git_futils_mmap_ro(git_map *out, git_file fd, git_object_size_t begin, size_t len)
305306
{
306307
return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
307308
}
308309

309310
int git_futils_mmap_ro_file(git_map *out, const char *path)
310311
{
311312
git_file fd = git_futils_open_ro(path);
312-
git_o 10000 ff_t len;
313+
git_object_size_t len;
313314
int result;
314315

315316
if (fd < 0)
316317
return fd;
317318

318-
if ((len = git_futils_filesize(fd)) < 0) {
319-
result = -1;
319+
if ((result = git_futils_filesize(&len, fd)) < 0)
320320
goto out;
321-
}
322321

323322
if (!git__is_sizet(len)) {
324323
git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path);
@@ -1105,15 +1104,15 @@ int git_futils_filestamp_check(
11051104
#if defined(GIT_USE_NSEC)
11061105
stamp->mtime.tv_nsec == st.st_mtime_nsec &&
11071106
#endif
1108-
stamp->size == (git_off_t)st.st_size &&
1107+
stamp->size == (git_object_size_t)st.st_size &&
11091108
stamp->ino == (unsigned int)st.st_ino)
11101109
return 0;
11111110

11121111
stamp->mtime.tv_sec = st.st_mtime;
11131112
#if defined(GIT_USE_NSEC)
11141113
stamp->mtime.tv_nsec = st.st_mtime_nsec;
11151114
#endif
1116-
stamp->size = (git_off_t)st.st_size;
1115+
stamp->size = (git_object_size_t)st.st_size;
11171116
stamp->ino = (unsigned int)st.st_ino;
11181117

11191118
return 1;
@@ -1141,7 +1140,7 @@ void git_futils_filestamp_set_from_stat(
11411140
#else
11421141
stamp->mtime.tv_nsec = 0;
11431142
#endif
1144-
stamp->size = (git_off_t)st->st_size;
1143+
stamp->size = (git_object_size_t)st->st_size;
11451144
stamp->ino = (unsigned int)st->st_ino;
11461145
} else {
11471146
memset(stamp, 0, sizeof(*stamp));

0 commit comments

Comments
 (0)
0