2016-12-05 18:11:22 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
2016-12-05 22:23:58 +00:00
|
|
|
#include <string.h>
|
2017-08-28 18:04:27 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2016-12-05 18:11:22 +00:00
|
|
|
|
2016-12-21 13:08:44 +00:00
|
|
|
#include "ipfs/repo/init.h"
|
2016-12-05 18:11:22 +00:00
|
|
|
#include "ipfs/repo/fsrepo/fs_repo.h"
|
2017-08-28 18:04:27 +00:00
|
|
|
#include "ipfs/core/daemon.h"
|
2017-04-06 14:33:28 +00:00
|
|
|
#include "libp2p/os/utils.h"
|
2016-12-05 18:11:22 +00:00
|
|
|
|
2017-08-28 18:04:27 +00:00
|
|
|
int cp(const char *to, const char *from)
|
|
|
|
{
|
|
|
|
int fd_to, fd_from;
|
|
|
|
char buf[4096];
|
|
|
|
ssize_t nread;
|
|
|
|
int saved_errno;
|
|
|
|
|
|
|
|
fd_from = open(from, O_RDONLY);
|
|
|
|
if (fd_from < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
|
|
if (fd_to < 0)
|
|
|
|
goto out_error;
|
|
|
|
|
|
|
|
while (nread = read(fd_from, buf, sizeof buf), nread > 0)
|
|
|
|
{
|
|
|
|
char *out_ptr = buf;
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
do {
|
|
|
|
nwritten = write(fd_to, out_ptr, nread);
|
|
|
|
|
|
|
|
if (nwritten >= 0)
|
|
|
|
{
|
|
|
|
nread -= nwritten;
|
|
|
|
out_ptr += nwritten;
|
|
|
|
}
|
|
|
|
else if (errno != EINTR)
|
|
|
|
{
|
|
|
|
goto out_error;
|
|
|
|
}
|
|
|
|
} while (nread > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread == 0)
|
|
|
|
{
|
|
|
|
if (close(fd_to) < 0)
|
|
|
|
{
|
|
|
|
fd_to = -1;
|
|
|
|
goto out_error;
|
|
|
|
}
|
|
|
|
close(fd_from);
|
|
|
|
|
|
|
|
/* Success! */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_error:
|
|
|
|
saved_errno = errno;
|
|
|
|
|
|
|
|
close(fd_from);
|
|
|
|
if (fd_to >= 0)
|
|
|
|
close(fd_to);
|
|
|
|
|
|
|
|
errno = saved_errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-19 12:47:19 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:31:58 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2017-04-13 14:31:58 +00:00
|
|
|
* @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
|
|
|
|
2016-12-12 20:06:17 +00:00
|
|
|
if (os_utils_file_exists(path)) {
|
2017-04-27 16:35:26 +00:00
|
|
|
if (!drop_repository(path)) {
|
2016-12-12 20:06:17 +00:00
|
|
|
return 0;
|
2017-04-27 16:35:26 +00:00
|
|
|
}
|
2016-12-12 20:06:17 +00:00
|
|
|
}
|
2017-04-27 16:35:26 +00:00
|
|
|
mkdir(path, S_IRWXU);
|
2016-12-05 18:11:22 +00:00
|
|
|
|
2017-07-26 14:48:04 +00:00
|
|
|
return make_ipfs_repository(path, swarm_port, bootstrap_peers, peer_id);
|
2016-12-05 18:11:22 +00:00
|
|
|
}
|
2016-12-14 17:07:43 +00:00
|
|
|
|
2017-08-28 18:04:27 +00:00
|
|
|
/***
|
|
|
|
* Drop a repository and build a new one with the specified config file
|
|
|
|
* @param path where to create it
|
|
|
|
* @param fs_repo the results
|
|
|
|
* @param config_file_to_copy where to find the config file to copy
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int drop_build_open_repo(const char* path, struct FSRepo** fs_repo, const char* config_filename_to_copy) {
|
2017-04-17 16:58:47 +00:00
|
|
|
if (!drop_and_build_repository(path, 4001, NULL, NULL))
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
2017-04-17 16:58:47 +00:00
|
|
|
|
2017-08-28 18:04:27 +00:00
|
|
|
if (config_filename_to_copy != NULL) {
|
|
|
|
// attach config filename to path
|
2017-09-07 16:05:56 +00:00
|
|
|
char *config = (char*) malloc(strlen(path) + 8);
|
2017-08-28 18:04:27 +00:00
|
|
|
strcpy(config, path);
|
|
|
|
// erase slash if there is one
|
|
|
|
if (config[strlen(path)-1] == '/')
|
|
|
|
config[strlen(path)-1] = 0;
|
|
|
|
strcat(config, "/config");
|
|
|
|
// delete the old
|
2017-09-04 16:02:48 +00:00
|
|
|
if (unlink(config) != 0) {
|
|
|
|
free(config);
|
2017-08-28 18:04:27 +00:00
|
|
|
return 0;
|
2017-09-04 16:02:48 +00:00
|
|
|
}
|
2017-08-28 18:04:27 +00:00
|
|
|
// copy pre-built config file into directory
|
2017-09-04 16:02:48 +00:00
|
|
|
if (cp(config, config_filename_to_copy) < 0) {
|
|
|
|
fprintf(stderr, "Unable to copy %s to %s. Error number %d.\n", config_filename_to_copy, config, errno);
|
|
|
|
free(config);
|
2017-08-28 18:04:27 +00:00
|
|
|
return 0;
|
2017-09-04 16:02:48 +00:00
|
|
|
}
|
|
|
|
free(config);
|
2017-08-28 18:04:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:58:47 +00:00
|
|
|
if (!ipfs_repo_fsrepo_new(path, NULL, fs_repo))
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
2017-04-17 16:58:47 +00:00
|
|
|
|
|
|
|
if (!ipfs_repo_fsrepo_open(*fs_repo)) {
|
2016-12-14 17:07:43 +00:00
|
|
|
free(*fs_repo);
|
2017-04-17 16:58:47 +00:00
|
|
|
*fs_repo = NULL;
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-08-28 18:04:27 +00:00
|
|
|
/***
|
|
|
|
* Drop a repository and build a new one
|
|
|
|
* @param path where to create it
|
|
|
|
* @param fs_repo the results
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int drop_build_and_open_repo(const char* path, struct FSRepo** fs_repo) {
|
|
|
|
return drop_build_open_repo(path, fs_repo, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Start a daemon (usefull in a separate thread)
|
|
|
|
* @param arg a char string of the repo path
|
|
|
|
* @returns NULL
|
|
|
|
*/
|
|
|
|
void* test_daemon_start(void* arg) {
|
|
|
|
ipfs_daemon_start((char*)arg);
|
|
|
|
return NULL;
|
|
|
|
}
|