Add a way to get a blob's size and content
This commit is contained in:
47
c_src/blob.c
Normal file
47
c_src/blob.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <git2.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "geef.h"
|
||||
#include "object.h"
|
||||
#include "blob.h"
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_blob_size(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
geef_object *obj;
|
||||
const git_blob *blob;
|
||||
|
||||
if (!enif_get_resource(env, argv[0], geef_object_type, (void **) &obj))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
blob = (git_blob *)obj->obj;
|
||||
|
||||
return enif_make_uint64(env, git_blob_rawsize(blob));
|
||||
}
|
||||
|
||||
ERL_NIF_TERM
|
||||
geef_blob_content(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||
{
|
||||
geef_object *obj;
|
||||
const git_blob *blob;
|
||||
ErlNifBinary bin;
|
||||
size_t len;
|
||||
const void *content;
|
||||
|
||||
if (!enif_get_resource(env, argv[0], geef_object_type, (void **) &obj))
|
||||
return enif_make_badarg(env);
|
||||
|
||||
blob = (git_blob *)obj->obj;
|
||||
|
||||
len = git_blob_rawsize(blob);
|
||||
content = git_blob_rawcontent(blob);
|
||||
if (!content)
|
||||
return atoms.error;
|
||||
|
||||
if (!enif_alloc_binary(len, &bin))
|
||||
return atoms.error;
|
||||
|
||||
memcpy(bin.data, content, len);
|
||||
|
||||
return enif_make_tuple2(env, atoms.ok, enif_make_binary(env, &bin));
|
||||
}
|
||||
9
c_src/blob.h
Normal file
9
c_src/blob.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef GEEF_BLOB_H
|
||||
#define GEEF_BLOB_H
|
||||
|
||||
#include "object.h"
|
||||
|
||||
ERL_NIF_TERM geef_blob_size(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
ERL_NIF_TERM geef_blob_content(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
|
||||
|
||||
#endif
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "object.h"
|
||||
#include "commit.h"
|
||||
#include "tree.h"
|
||||
#include "blob.h"
|
||||
#include "geef.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -102,6 +103,8 @@ static ErlNifFunc geef_funcs[] =
|
||||
{"commit_tree", 1, geef_commit_tree},
|
||||
{"commit_tree_id", 1, geef_commit_tree_id},
|
||||
{"tree_bypath", 2, geef_tree_bypath},
|
||||
{"blob_size", 1, geef_blob_size},
|
||||
{"blob_content", 1, geef_blob_content},
|
||||
};
|
||||
|
||||
ERL_NIF_INIT(geef, geef_funcs, load, NULL, NULL, unload)
|
||||
|
||||
Reference in New Issue
Block a user