c-ipfs/test/test_helper.c

169 lines
3.5 KiB
C
Raw Normal View History

2016-12-05 18:11:22 +00:00
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
2016-12-05 18:11:22 +00:00
#include "ipfs/repo/init.h"
2016-12-05 18:11:22 +00:00
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "libp2p/os/utils.h"
2016-12-05 18:11:22 +00:00
/***
* Helper to create a test file in the OS
*/
int create_file(const char* fileName, unsigned char* bytes, size_t num_bytes) {
FILE* file = fopen(fileName, "wb");
fwrite(bytes, num_bytes, 1, file);
fclose(file);
return 1;
}
/***
* Create a buffer with some data
*/
int create_bytes(unsigned char* buffer, size_t num_bytes) {
int counter = 0;
for(int i = 0; i < num_bytes; i++) {
buffer[i] = counter++;
if (counter > 15)
counter = 0;
}
return 1;
}
2017-04-27 16:35:26 +00:00
/***
* Remove a directory and everything in it
* @param path the directory to remove
* @returns true(1) on success, otherwise false(0)
*/
2016-12-05 18:11:22 +00:00
int remove_directory(const char *path)
{
DIR *d = opendir(path);
size_t path_len = strlen(path);
int r = -1;
if (d)
{
struct dirent *p;
2017-04-27 16:35:26 +00:00
r = 1;
2016-12-05 18:11:22 +00:00
2017-04-27 16:35:26 +00:00
while (r && (p=readdir(d)))
2016-12-05 18:11:22 +00:00
{
int r2 = -1;
char *buf;
size_t len;
/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
{
continue;
}
len = path_len + strlen(p->d_name) + 2;
buf = malloc(len);
if (buf)
{
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, p->d_name);
if (!stat(buf, &statbuf))
{
if (S_ISDIR(statbuf.st_mode))
{
r2 = remove_directory(buf);
}
else
{
2017-04-27 16:35:26 +00:00
r2 = !unlink(buf);
2016-12-05 18:11:22 +00:00
}
}
free(buf);
}
r = r2;
}
closedir(d);
}
2017-04-27 16:35:26 +00:00
if (r)
2016-12-05 18:11:22 +00:00
{
2017-04-27 16:35:26 +00:00
r = !rmdir(path);
2016-12-05 18:11:22 +00:00
}
return r;
}
2017-04-27 16:35:26 +00:00
/***
* Drop a repository by removing the directory
*/
int drop_repository(const char* path) {
if (os_utils_file_exists(path)) {
return remove_directory(path);
/*
// the config file
if (!os_utils_filepath_join(path, "config", currDirectory, 1024))
return 0;
unlink(currDirectory);
// the datastore directory
if (!os_utils_filepath_join(path, "datastore", currDirectory, 1024))
return 0;
if (!remove_directory(currDirectory))
return 0;
// the blockstore directory
if (!os_utils_filepath_join(path, "blockstore", currDirectory, 1024))
return 0;
if (!remove_directory(currDirectory))
return 0;
return remove_directory(path);
*/
}
return 1;
}
/**
* drops and builds a repository at the specified path
* @param path the path
* @param swarm_port the port that the swarm should run on
* @param bootstrap_peers a vector of fellow peers as MultiAddresses, can be NULL
2017-04-27 20:52:20 +00:00
* @param peer_id a place to store the generated peer id
* @returns true(1) on success, otherwise false(0)
*/
int drop_and_build_repository(const char* path, int swarm_port, struct Libp2pVector* bootstrap_peers, char **peer_id) {
2016-12-05 18:11:22 +00:00
if (os_utils_file_exists(path)) {
2017-04-27 16:35:26 +00:00
if (!drop_repository(path)) {
return 0;
2017-04-27 16:35:26 +00:00
}
}
2017-04-27 16:35:26 +00:00
mkdir(path, S_IRWXU);
2016-12-05 18:11:22 +00:00
return make_ipfs_repository(path, swarm_port, bootstrap_peers, peer_id);
2016-12-05 18:11:22 +00:00
}
int drop_build_and_open_repo(const char* path, struct FSRepo** fs_repo) {
if (!drop_and_build_repository(path, 4001, NULL, NULL))
return 0;
if (!ipfs_repo_fsrepo_new(path, NULL, fs_repo))
return 0;
if (!ipfs_repo_fsrepo_open(*fs_repo)) {
free(*fs_repo);
*fs_repo = NULL;
return 0;
}
return 1;
}