Better handling of locally stored files

yamux
John Jones 2017-05-11 07:04:28 -05:00
parent 910e84dd5c
commit ff4be03782
5 changed files with 15 additions and 5 deletions

View File

@ -33,9 +33,10 @@ void libp2p_peer_entry_free(struct PeerEntry* in);
/**
* Creates a new empty peerstore
* @param peer_id the peer id as a null terminated string
* @returns an empty peerstore or NULL on error
*/
struct Peerstore* libp2p_peerstore_new();
struct Peerstore* libp2p_peerstore_new(const char* peer_id);
/**
* Deallocate resources used by the peerstore

View File

@ -32,6 +32,6 @@ struct ProviderStore* libp2p_providerstore_new();
*/
void libp2p_providerstore_free(struct ProviderStore* in);
int libp2p_providerstore_add(struct ProviderStore* store, unsigned char* hash, int hash_size, unsigned char* peer_id, int peer_id_size);
int libp2p_providerstore_add(struct ProviderStore* store, const unsigned char* hash, int hash_size, const unsigned char* peer_id, int peer_id_size);
int libp2p_providerstore_get(struct ProviderStore* store, const unsigned char* hash, int hash_size, unsigned char** peer_id, int *peer_id_size);

View File

@ -33,13 +33,22 @@ struct PeerEntry* libp2p_peer_entry_copy(struct PeerEntry* in) {
/**
* Creates a new empty peerstore
* @param peer_id the peer id as a null terminated string
* @returns an empty peerstore or NULL on error
*/
struct Peerstore* libp2p_peerstore_new() {
struct Peerstore* libp2p_peerstore_new(const char* peer_id) {
struct Peerstore* out = (struct Peerstore*)malloc(sizeof(struct Peerstore));
if (out != NULL) {
out->head_entry = NULL;
out->last_entry = NULL;
// now add this peer as the first entry
struct Libp2pPeer* peer = libp2p_peer_new();
peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
peer->id_size = strlen(peer_id);
peer->id = malloc(peer->id_size);
memcpy(peer->id, peer_id, peer->id_size);
libp2p_peerstore_add_peer(out, peer);
libp2p_peer_free(peer);
}
return out;
}

View File

@ -61,7 +61,7 @@ void libp2p_providerstore_free(struct ProviderStore* in) {
}
}
int libp2p_providerstore_add(struct ProviderStore* store, unsigned char* hash, int hash_size, unsigned char* peer_id, int peer_id_size) {
int libp2p_providerstore_add(struct ProviderStore* store, unsigned char* hash, int hash_size, const unsigned char* peer_id, int peer_id_size) {
char hash_str[hash_size + 1];
memcpy(hash_str, hash, hash_size);
hash_str[hash_size] = 0;

View File

@ -24,7 +24,7 @@ int test_peer() {
* Test the peerstore
*/
int test_peerstore() {
struct Peerstore* peerstore = libp2p_peerstore_new();
struct Peerstore* peerstore = libp2p_peerstore_new("Qmabcdefg");
struct PeerEntry* peer_entry = NULL;
struct PeerEntry* results = NULL;
int retVal = 0;