summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyo Nakamura <upa@haeena.net>2023-03-11 21:48:07 +0900
committerRyo Nakamura <upa@haeena.net>2023-03-11 21:48:07 +0900
commit74d58e986a9d4d04e48cc344f13659e062fd3799 (patch)
tree52e2890416e3560fca3531042e7497aea05c2643
parent7e7bc61ff2643f42bfb9152de6a94aa1fcf3c664 (diff)
move direction from mscp_opts to mscp_init argument
-rw-r--r--include/mscp.h5
-rw-r--r--src/main.c7
-rw-r--r--src/mscp.c30
3 files changed, 25 insertions, 17 deletions
diff --git a/include/mscp.h b/include/mscp.h
index 3bdf7ed..92a59ae 100644
--- a/include/mscp.h
+++ b/include/mscp.h
@@ -37,8 +37,6 @@
* @brief Structure configuring mscp.
*/
struct mscp_opts {
- int direction; /** copy rirection. `MSCP_DIRECTION_*` */
-
int nr_threads; /** number of copy threads */
int nr_ahead; /** number of SFTP commands on-the-fly */
size_t min_chunk_sz; /** minimum chunk size (default 64MB) */
@@ -99,12 +97,13 @@ struct mscp;
* @brief Creates a new mscp instance.
*
* @param remote_host remote host for file transer.
+ * @param direction copy direction, `MSCP_DIRECTION_L2R` or `MSCP_DIRECTION_R2L`
* @param o options for configuring mscp.
* @param s options for configuring ssh connections.
*
* @retrun A new mscp instance or NULL on error.
*/
-struct mscp *mscp_init(const char *remote_host,
+struct mscp *mscp_init(const char *remote_host, int direction,
struct mscp_opts *o, struct mscp_ssh_opts *s);
/**
diff --git a/src/main.c b/src/main.c
index 811e626..7470928 100644
--- a/src/main.c
+++ b/src/main.c
@@ -196,6 +196,7 @@ int main(int argc, char **argv)
struct target *t;
int pipe_fd[2];
int ch, n, i, ret;
+ int direction = 0;
char *remote;
bool dryrun = false;
@@ -312,11 +313,11 @@ int main(int argc, char **argv)
if (t[0].remote) {
/* copy remote to local */
- o.direction = MSCP_DIRECTION_R2L;
+ direction = MSCP_DIRECTION_R2L;
remote = t[0].remote;
} else {
/* copy local to remote */
- o.direction = MSCP_DIRECTION_L2R;
+ direction = MSCP_DIRECTION_L2R;
remote = t[i - 1].remote;
}
@@ -329,7 +330,7 @@ int main(int argc, char **argv)
o.msg_fd = pipe_fd[1];
}
- if ((m = mscp_init(remote, &o, &s)) == NULL) {
+ if ((m = mscp_init(remote, direction, &o, &s)) == NULL) {
fprintf(stderr, "mscp_init: %s\n", mscp_get_error());
return -1;
}
diff --git a/src/mscp.c b/src/mscp.c
index 7c39d73..3b6b7ff 100644
--- a/src/mscp.c
+++ b/src/mscp.c
@@ -15,6 +15,7 @@
struct mscp {
char *remote; /* remote host (and uername) */
+ int direction; /* copy direction */
struct mscp_opts *opts;
struct mscp_ssh_opts *ssh_opts;
@@ -131,12 +132,6 @@ static int default_nr_threads()
static int validate_and_set_defaut_params(struct mscp_opts *o)
{
- if (!(o->direction == MSCP_DIRECTION_L2R ||
- o->direction == MSCP_DIRECTION_R2L)) {
- mscp_set_error("invalid copy direction: %d", o->direction);
- return -1;
- }
-
if (o->nr_threads < 0) {
mscp_set_error("invalid nr_threads: %d", o->nr_threads);
return -1;
@@ -186,12 +181,23 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
return 0;
}
-struct mscp *mscp_init(const char *remote_host,
+struct mscp *mscp_init(const char *remote_host, int direction,
struct mscp_opts *o, struct mscp_ssh_opts *s)
{
struct mscp *m;
int n;
+ if (!remote_host) {
+ mscp_set_error("empty remote host\n");
+ return NULL;
+ }
+
+ if (!(direction == MSCP_DIRECTION_L2R ||
+ direction == MSCP_DIRECTION_R2L)) {
+ mscp_set_error("invalid copy direction: %d", direction);
+ return NULL;
+ }
+
m = malloc(sizeof(*m));
if (!m) {
mscp_set_error("failed to allocate memory: %s", strerrno());
@@ -204,16 +210,18 @@ struct mscp *mscp_init(const char *remote_host,
goto free_out;
memset(m, 0, sizeof(*m));
- m->msg_fd = o->msg_fd;
INIT_LIST_HEAD(&m->src_list);
INIT_LIST_HEAD(&m->path_list);
INIT_LIST_HEAD(&m->chunk_list);
lock_init(&m->chunk_lock);
+
m->remote = strdup(remote_host);
if (!m->remote) {
mscp_set_error("failed to allocate memory: %s", strerrno());
goto free_out;
}
+ m->direction = direction;
+ m->msg_fd = o->msg_fd;
if (strlen(o->coremask) > 0) {
if (expand_coremask(o->coremask, &m->cores, &m->nr_cores) < 0)
@@ -297,7 +305,7 @@ int mscp_prepare(struct mscp *m)
src_path_is_dir = dst_path_is_dir = dst_path_should_dir = false;
- switch (m->opts->direction) {
+ switch (m->direction) {
case MSCP_DIRECTION_L2R:
src_sftp = NULL;
dst_sftp = m->first;
@@ -307,7 +315,7 @@ int mscp_prepare(struct mscp *m)
dst_sftp = NULL;
break;
default:
- mscp_set_error("invalid copy direction: %d", m->opts->direction);
+ mscp_set_error("invalid copy direction: %d", m->direction);
return -1;
}
@@ -481,7 +489,7 @@ void *mscp_copy_thread(void *arg)
struct mscp *m = t->m;
struct chunk *c;
- switch (m->opts->direction) {
+ switch (m->direction) {
case MSCP_DIRECTION_L2R:
src_sftp = NULL;
dst_sftp = t->sftp;