8000 Move `git_off_t` to `git_object_size_t` by ethomson · Pull Request #5123 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

Move git_off_t to git_object_size_t #5123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
int lg2_blame(git_repository *repo, int argc, char *argv[])
{
int line, break_on_null_hunk;
git_off_t i, rawsize;
git_object_size_t i, rawsize;
char spec[1024] = {0};
struct opts o = {0};
const char *rawdata;
Expand Down
2 changes: 1 addition & 1 deletion include/git2/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
* @param blob pointer to the blob
* @return size on bytes
*/
GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
GIT_EXTERN(git_object_size_t) git_blob_rawsize(const git_blob *blob);

/**
* Flags to control the functionality of `git_blob_filter`.
Expand Down
12 changes: 6 additions & 6 deletions include/git2/diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ typedef enum {
* abbreviated to something reasonable, like 7 characters.
*/
typedef struct {
git_oid id;
const char *path;
git_off_t size;
uint32_t flags;
uint16_t mode;
uint16_t id_abbrev;
git_oid id;
const char *path;
git_object_size_t size;
uint32_t flags;
uint16_t mode;
uint16_t id_abbrev;
} git_diff_file;

/**
Expand Down
2 changes: 2 additions & 0 deletions include/git2/object.h
A935
Original file line numberDiff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
GIT_BEGIN_DECL

#define GIT_OBJECT_SIZE_MAX UINT64_MAX

/**
* Lookup a reference to one of the objects in a repository.
*
Expand Down
2 changes: 1 addition & 1 deletion include/git2/odb.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size
* @param type type of the object that will be written
* @return 0 if the stream was created; error code otherwise
*/
GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_off_t size, git_object_t type);
GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_object_size_t size, git_object_t type);

/**
* Write to an odb stream
Expand Down
4 changes: 2 additions & 2 deletions include/git2/odb_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ struct git_odb_stream {
unsigned int mode;
void *hash_ctx;

git_off_t declared_size;
git_off_t received_bytes;
git_object_size_t declared_size;
git_object_size_t received_bytes;

/**
* Write at most `len` bytes into `buffer` and advance the stream.
Expand Down
2 changes: 1 addition & 1 deletion include/git2/sys/odb_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct git_odb_backend {
git_odb_backend *, const git_oid *, const void *, size_t, git_object_t);

int GIT_CALLBACK(writestream)(
git_odb_stream **, git_odb_backend *, git_off_t, git_object_t);
git_odb_stream **, git_odb_backend *, git_object_size_t, git_object_t);

int GIT_CALLBACK(readstream)(
git_odb_stream **, size_t *, git_object_t *,
Expand Down
3 changes: 3 additions & 0 deletions include/git2/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ typedef int64_t git_time_t; /**< time in seconds from epoch */

#endif

/** The maximum size of an object */
typedef uint64_t git_object_size_t;

#include "buffer.h"
#include "oid.h"

Expand Down
2 changes: 1 addition & 1 deletion src/attr_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int git_attr_file__load(
int bom_offset;
git_bom_t bom;
git_oid id;
git_off_t blobsize;
git_object_size_t blobsize;

*out = NULL;

Expand Down
11 changes: 9 additions & 2 deletions src/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static git_blame_hunk *split_hunk_in_vector(
static int index_blob_lines(git_blame *blame)
{
const char *buf = blame->final_buf;
git_off_t len = blame->final_buf_size;
size_t len = blame->final_buf_size;
int num = 0, incomplete = 0, bol = 1;
size_t *i;

Expand Down Expand Up @@ -335,8 +335,15 @@ static int blame_internal(git_blame *blame)
if ((error = load_blob(blame)) < 0 ||
(error = git_blame__get_origin(&o, blame, blame->final, blame->path)) < 0)
goto cleanup;

if (git_blob_rawsize(blame->final_blob) > SIZE_MAX) {
git_error_set(GIT_ERROR_NOMEMORY, "blob is too large to blame");
error = -1;
goto cleanup;
}

blame->final_buf = git_blob_rawcontent(blame->final_blob);
blame->final_buf_size = git_blob_rawsize(blame->final_blob);
blame->final_buf_size = (size_t)git_blob_rawsize(blame->final_blob);

ent = git__calloc(1, sizeof(git_blame__entry));
GIT_ERROR_CHECK_ALLOC(ent);
Expand Down
2 changes: 1 addition & 1 deletion src/blame.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct git_blame {
git_blame__entry *ent;
int num_lines;
const char *final_buf;
git_off_t final_buf_size;
size_t final_buf_size;
};

git_blame *git_blame__alloc(
Expand Down
16 changes: 8 additions & 8 deletions src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ const void *git_blob_rawcontent(const git_blob *blob)
return git_odb_object_data(blob->data.odb);
}

git_off_t git_blob_rawsize(const git_blob *blob)
git_object_size_t git_blob_rawsize(const git_blob *blob)
{
assert(blob);
if (blob->raw)
return blob->data.raw.size;
else
return (git_off_t)git_odb_object_size(blob->data.odb);
return (git_object_size_t)git_odb_object_size(blob->data.odb);
}

int git_blob__getbuf(git_buf *buffer, git_blob *blob)
{
git_off_t size = git_blob_rawsize(blob);
git_object_size_t size = git_blob_rawsize(blob);

GIT_ERROR_CHECK_BLOBSIZE(size);
return git_buf_set(buffer, git_blob_rawcontent(blob), (size_t)size);
Expand Down Expand Up @@ -91,13 +91,13 @@ int git_blob_create_from_buffer(
}

static int write_file_stream(
git_oid *id, git_odb *odb, const char *path, git_off_t file_size)
git_oid *id, git_odb *odb, const char *path, git_object_size_t file_size)
{
int fd, error;
char buffer[FILEIO_BUFSIZE];
git_odb_stream *stream = NULL;
ssize_t read_len = -1;
git_off_t written = 0;
git_object_size_t written = 0;

if ((error = git_odb_open_wstream(
&stream, odb, file_size, GIT_OBJECT_BLOB)) < 0)
Expand Down Expand Up @@ -129,7 +129,7 @@ static int write_file_stream(

static int write_file_filtered(
git_oid *id,
git_off_t *size,
git_object_size_t *size,
git_odb *odb,
const char *full_path,
git_filter_list *fl)
Expand Down Expand Up @@ -184,7 +184,7 @@ int git_blob__create_from_paths(
int error;
struct stat st;
git_odb *odb = NULL;
git_off_t size;
git_object_size_t size;
mode_t mode;
git_buf path = GIT_BUF_INIT;

Expand Down Expand Up @@ -389,7 +389,7 @@ int git_blob_create_from_stream_commit(git_oid *out, git_writestream *_stream)
int git_blob_is_binary(const git_blob *blob)
{
git_buf content = GIT_BUF_INIT;
git_off_t size;
git_object_size_t size;

assert(blob);

Expand Down
2 changes: 1 addition & 1 deletion src/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct git_blob {
git_odb_object *odb;
struct {
const char *data;
git_off_t size;
git_object_size_t size;
} raw;
} data;
unsigned int raw:1;
Expand Down
2 changes: 1 addition & 1 deletion src/crlf.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static int has_cr_in_index(const git_filter_source *src)
const git_index_entry *entry;
git_blob *blob;
const void *blobcontent;
git_off_t blobsize;
git_object_size_t blobsize;
bool found_cr;

if (!path)
Expand Down
8 changes: 5 additions & 3 deletions src/diff_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static int diff_file_content_init_common(
git_diff_driver_update_options(&fc->opts_flags, fc->driver);

/* make sure file is conceivable mmap-able */
if ((git_off_t)((size_t)fc->file->size) != fc->file->size)
if ((size_t)fc->file->size != fc->file->size)
fc->file->flags |= GIT_DIFF_FLAG_BINARY;
/* check if user is forcing text diff the file */
else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) {
Expand Down Expand Up @@ -330,8 +330,10 @@ static int diff_file_content_load_workdir_file(
if (fd < 0)
return fd;

if (!fc->file->size &&
!(fc->file->size = git_futils_filesize(fd)))
if (!fc->file->size)
error = git_futils_filesize(&fc->file->size, fd);

if (error < 0 || !fc->file->size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does look a bit weird. Seems like we do not allow loading empty files from disk here. Your change only makes code a lot clearer here, so I'm fine with this as is.

goto cleanup;

if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 &&
Expand Down
2 changes: 1 addition & 1 deletion src/diff_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct {
git_diff_driver *driver;
uint32_t flags;
uint32_t opts_flags;
git_off_t opts_max_size;
git_object_size_t opts_max_size;
git_iterator_type_t src;
const git_blob *blob;
git_map map;
Expand Down
4 changes: 2 additions & 2 deletions src/diff_generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,11 @@ int git_diff__oid_for_file(
git_diff *diff,
const char *path,
uint16_t mode,
git_off_t size)
git_object_size_t size)
{
git_index_entry entry;

if (size < 0 || size > UINT32_MAX) {
if (size > UINT32_MAX) {
git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", path);
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/diff_generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern int git_diff__oid_for_file(
git_diff *diff,
const char *path,
uint16_t mode,
git_off_t size);
git_object_size_t size);

extern int git_diff__oid_for_entry(
git_oid *out,
Expand Down Expand Up @@ -120,7 +120,7 @@ GIT_INLINE(int) git_diff_file__resolve_zero_size(
git_odb_free(odb);

if (!error)
file->size = (git_off_t)len;
file->size = (git_object_size_t)len;

return error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/diff_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int git_diff_file_stats__full_to_buf(
{
const char *old_path = NULL, *new_path = NULL;
size_t padding;
git_off_t old_size, new_size;
git_object_size_t old_size, new_size;

old_path = delta->old_file.path;
new_path = delta->new_file.path;
Expand Down
2 changes: 1 addition & 1 deletion src/diff_tform.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ static int similarity_sig(
if (file->size != git_blob_rawsize(info->blob))
file->size = git_blob_rawsize(info->blob);

sz = (size_t)(git__is_sizet(file->size) ? file->size : -1);
sz = git__is_sizet(file->size) ? (size_t)file->size : (size_t)-1;

error = opts->metric->buffer_signature(
&cache[info->idx], info->file,
Expand Down
2 changes: 1 addition & 1 deletion src/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ int git_filter_list_apply_to_file(

static int buf_from_blob(git_buf *out, git_blob *blob)
{
git_off_t rawsize = git_blob_rawsize(blob);
git_object_size_t rawsize = git_blob_rawsize(blob);

if (!git__is_sizet(rawsize)) {
git_error_set(GIT_ERROR_OS, "blob is too large to filter");
Expand Down
24 changes: 14 additions & 10 deletions src/futils.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int git_futils_truncate(const char *path, int mode)
return 0;
}

git_off_t git_futils_filesize(git_file fd)
int git_futils_filesize(uint64_t *out, git_file fd)
{
struct stat sb;

Expand All @@ -121,7 +121,13 @@ git_off_t git_futils_filesize(git_file fd)
return -1;
}

return sb.st_size;
if (sb.st_size < 0) {
git_error_set(GIT_ERROR_INVALID, "invalid file size");
return -1;
}

*out = sb.st_size;
return 0;
}

mode_t git_futils_canonical_mode(mode_t raw_mode)
Expand Down Expand Up @@ -301,24 +307,22 @@ int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmod
return 0;
}

int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
int git_futils_mmap_ro(git_map *out, git_file fd, off64_t begin, size_t len)
{
return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
}

int git_futils_mmap_ro_file(git_map *out, const char *path)
{
git_file fd = git_futils_open_ro(path);
git_off_t len;
uint64_t len;
int result;

if (fd < 0)
return fd;

if ((len = git_futils_filesize(fd)) < 0) {
result = -1;
if ((result = git_futils_filesize(&len, fd)) < 0)
goto out;
}

if (!git__is_sizet(len)) {
git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path);
Expand Down Expand Up @@ -1106,15 +1110,15 @@ int git_futils_filestamp_check(
#if defined(GIT_USE_NSEC)
stamp->mtime.tv_nsec == st.st_mtime_nsec &&
#endif
stamp->size == (git_off_t)st.st_size &&
stamp->size == (uint64_t)st.st_size &&
stamp->ino == (unsigned int)st.st_ino)
return 0;

stamp->mtime.tv_sec = st.st_mtime;
#if defined(GIT_USE_NSEC)
stamp->mtime.tv_nsec = st.st_mtime_nsec;
#endif
stamp->size = (git_off_t)st.st_size;
stamp->size = (uint64_t)st.st_size;
stamp->ino = (unsigned int)st.st_ino;

return 1;
Expand Down Expand Up @@ -1142,7 +1146,7 @@ void git_futils_filestamp_set_from_stat(
#else
stamp->mtime.tv_nsec = 0;
#endif
stamp->size = (git_off_t)st->st_size;
stamp->size = (uint64_t)st->st_size;
stamp->ino = (unsigned int)st->st_ino;
} else {
memset(stamp, 0, sizeof(*stamp));
Expand Down
Loading
0