Skip to content
Open
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
1 change: 1 addition & 0 deletions src/neuroprovenance.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
310 changes: 310 additions & 0 deletions src/neuroprovenance.c~
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>

#include "provenance.h"
#include "neuroprovenance.h"

/*Initialization and Cleanup routines*/
ProvObjectPtr newProvenanceObject(const char* id)
{
ProvPtr p_prov = newProvenanceFactory(id);
// add new namespace
addNamespace(p_prov, "https://github.com/INCF/ProvenanceLibrary/wiki/terms", "ni");
//xmlNewNs(root_node, "http://incf.org/incf-schema", "incf");
//xmlNewNs(root_node, "http://this.namespace.needs.to/be.decided", "ni");
return((ProvObjectPtr)p_prov);
}

ProvObjectPtr newProvenanceObjectFromFile(const char* filename)
{
ProvPtr p_prov = newProvenanceFactoryFromFile(filename);
return((ProvObjectPtr)p_prov);
}

ProvObjectPtr newProvenanceObjectFromBuffer(const char* buffer, int bufferSize)
{
ProvPtr p_prov = newProvenanceFactoryFromMemoryBuffer(buffer, bufferSize);
return((ProvObjectPtr)p_prov);
}

int delProvenanceObject(ProvObjectPtr p_prov){
return(delProvenanceFactory((ProvPtr)p_prov));
}

/*IO routines*/
void printProvenance(ProvObjectPtr p_prov, const char* filename)
{
print_provenance((ProvPtr)p_prov, filename);
}

/*
return provenance as a string buffer
*/
void toBuffer(ProvObjectPtr p_prov, char** buffer, int* buffer_size)
{
dumpToMemoryBuffer((ProvPtr)p_prov, buffer, buffer_size);
}

/*
free the memory buffer
*/
void freeBuffer(char* buffer)
{
freeMemoryBuffer(buffer);
}

/* Record creation routines */

/* Create a new process */
ProcessPtr newProcess(ProvObjectPtr p_prov, const char* startTime, const char* endTime, const char* type)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF act_id = newActivity(p_record, NULL, startTime, endTime);
if (type != NULL)
addAttribute(p_record, act_id, "prov", NULL, "label", type);
assert(act_id);
return((ProcessPtr)act_id);
}

/* Associate an input with a process */
REFID newProcessInput(ProvObjectPtr p_prov, ProcessPtr p_proc, const char* name, const char* value, const char* type)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newEntity(p_record);
assert(id);
if (type == NULL)
addAttribute(p_record, id, "prov", "xsd:QName", "type", "ni:input");
else
addAttribute(p_record, id, "prov", "xsd:string", "type", type);
addAttribute(p_record, id, "ni", "xsd:string", "name", name);
addAttribute(p_record, id, "ni", "xsd:string", "value", value);
IDREF used_id = newUsedRecord(p_record, (IDREF)p_proc, id, NULL);
assert(used_id);
freeID(used_id);
return((REFID)id);
}

/* Associate an output with a process */
REFID newProcessOutput(ProvObjectPtr p_prov, ProcessPtr p_proc, const char* name, const char* value, const char* type)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newEntity(p_record);
assert(id);
if (type == NULL)
addAttribute(p_record, id, "prov", "xsd:QName", "type", "ni:output");
else
addAttribute(p_record, id, "prov", "xsd:string", "type", type);
addAttribute(p_record, id, "ni", "xsd:string", "name", name);
addAttribute(p_record, id, "ni", "xsd:string", "value", value);
IDREF gen_id = newGeneratedByRecord(p_record, id, (IDREF)p_proc, NULL);
assert(gen_id);
freeID(gen_id);
return((REFID)id);
}

/* Associated a input with a process */
int addInput(ProvObjectPtr p_prov, ProcessPtr p_proc, REFID input)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newUsedRecord(p_record, (IDREF)p_proc, input, NULL);
assert(id);
freeID(id);
return(0);
}

/* Associated an output with a process */
int addOutput(ProvObjectPtr p_prov, ProcessPtr p_proc, REFID output)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newGeneratedByRecord(p_record, output, (IDREF)p_proc, NULL);
assert(id);
freeID(id);
return(0);
}

/*
Does not use a lock. assumes file does not change while hash is
being computed.
*/
static char* get_md5_hash(const char* path)
{
FILE *file = fopen(path, "rb");
if(!file) return NULL;
const int bufSize = 32768;
unsigned char *buffer = malloc(bufSize);
int bytesRead = 0;
if(!buffer) return NULL;

EVP_MD_CTX mdctx;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;

EVP_DigestInit(&mdctx, EVP_md5());
while((bytesRead = fread(buffer, 1, bufSize, file)))
{
EVP_DigestUpdate(&mdctx, buffer, (size_t) bytesRead);
}
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
unsigned char* p_hash = (unsigned char*)malloc((2*md_len+1)*sizeof(unsigned char));
unsigned char* p_idx = p_hash;
int i;
for(i = 0; i < md_len; i++, p_idx+=2)
sprintf(p_idx, "%02x", md_value[i]);
fclose(file);
free(buffer);
return p_hash;
}

/* Create a new file record */
REFID newFile(ProvObjectPtr p_prov, const char* filename, const char* type)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newEntity(p_record);
unsigned char* p_hash;
if (type == NULL)
addAttribute(p_record, id, "prov", "xsd:QName", "type", "ni:file");
else
addAttribute(p_record, id, "prov", "xsd:QName", "type", type);
//addAttribute(p_record, id, "ni", "xsd:string", "path", filename);
p_hash = get_md5_hash(filename);
if (p_hash != NULL){
addAttribute(p_record, id, "ni", NULL, "md5sum", p_hash);
free(p_hash);
}
assert(id);
return((REFID)id);
}

/* Create a new file collection record */
REFID newFileCollection(ProvObjectPtr p_prov, const char** filenames, int n_files, const char* type)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newEntity(p_record);
int i;
if (type == NULL)
addAttribute(p_record, id, "prov", "xsd:QName", "type", "ni:filelist");
else
addAttribute(p_record, id, "prov", "xsd:string", "type", type);
for(i=0; i<n_files; i++)
addAttribute(p_record, id, "ni", NULL, "path", filenames[i]);
assert(id);
return((REFID)id);
}

/* Associated a environment variable with a process */
REFID addEnvironVariable(ProvObjectPtr p_prov, ProcessPtr p_proc, const char* name)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newEntity(p_record);
addAttribute(p_record, id, "prov", "xsd:QName", "type", "ni:environ");
addAttribute(p_record, id, "ni", NULL, name, getenv(name));
assert(id);
return((REFID)id);
}

/* Associated a environment variable with a process */
REFID addAllEnvironVariables(ProvObjectPtr p_prov, ProcessPtr p_proc, char **envp)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = newEntity(p_record);
char buffer[255];
int i;
addAttribute(p_record, id, "prov", "xsd:QName", "type", "ni:environ");
//addAttribute(p_record, id, "ni", "foo", "foo");
char** env;
for (env = envp; *env != 0; env++)
{
char* thisEnv = *env;
char *name;
char* p_index = thisEnv;
int pos = 0;
while (thisEnv[pos++] != '=');
name = strndup(thisEnv, pos-1);
if (name[0] != '_')
{
sprintf(buffer, "%s\0", &thisEnv[pos]);
//fprintf(stderr, "<%s>=%s\n", name, &buffer);
addAttribute(p_record, id, "ni", NULL, name, &buffer);
}
free(name);
}
assert(id);
return((REFID)id);
}

/* Add a key-value informratin pair to a process */
int addKeyValuePair(ProvObjectPtr p_prov, ProcessPtr p_proc, const char* key, const char* value)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
addAttribute(p_record, (IDREF)p_proc, "ni", NULL, key, value);
return(0);
}

/* utility: convert inputs to a commandline */
static char * get_cmdline(int argc, char **argv){
int total_len = 0, i;
for(i = 0; i < argc; i++)
total_len += strlen(argv[i]) + 1;
char * cmdline = (char *)malloc(total_len*sizeof(char));
char * p_index = cmdline;
for(i = 0; i < argc; i++){
strcpy(p_index, argv[i]);
if ((i + 1) < argc) strcat(p_index, " ");
p_index += (strlen(argv[i]) + 1);
}
return cmdline;
}

/* Add command line to a process */
int addCommandLine(ProvObjectPtr p_prov, ProcessPtr p_proc, int argc, char** argv)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
IDREF id = (IDREF)p_proc;
char *cmdline = get_cmdline(argc, argv);
addAttribute(p_record, id, "ni", NULL, "cmdline", cmdline);
free(cmdline);
return(0);
}

/* Link up two processes (not implemented) */
int addDependency(ProvObjectPtr p_prov, ProcessPtr parent, ProcessPtr child)
{
return(0);
}

/* Add additional type information to a record object (process/input/output) */
int addType(ProvObjectPtr p_prov, REFID id, const char* type, const char* xsdType)
{
RecordPtr p_record = ((ProvPtr)p_prov)->p_record;
if (xsdType == NULL)
addAttribute(p_record, id, "prov", "xsd:string", "type", type);
else
addAttribute(p_record, id, "prov", xsdType, "type", type);
return(0);
}

int changeREFID(ProvObjectPtr p_prov, REFID id, const char* new_id)
{
return(changeID(((ProvPtr)p_prov)->p_record, (IDREF)id, new_id));
}

int freeREFID(REFID id)
{
freeID((IDREF)id);
return(0);
}

int freeProcess(ProcessPtr p_proc)
{
return(freeREFID((REFID)p_proc));
}

int addProvenanceRecord(ProvObjectPtr p_curprov, const ProvObjectPtr p_otherprov, const char *prefix)
{
return(addProvAsAccount(((ProvPtr)p_curprov)->p_record, (ProvPtr)p_otherprov, prefix));
}
2 changes: 1 addition & 1 deletion src/provenance.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// Created by Satrajit Ghosh on 11/25/11.
// Copyright (c) 2011 TankThink Labs LLC. All rights reserved.
//

#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
Loading