mirror of
https://github.com/upa/mscp.git
synced 2026-02-15 01:34:44 +08:00
move direction from mscp_opts to mscp_init argument
This commit is contained in:
@@ -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);
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
30
src/mscp.c
30
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;
|
||||
|
||||
Reference in New Issue
Block a user