mirror of
https://github.com/upa/mscp.git
synced 2026-02-04 03:24:58 +08:00
rename mscp_prepare to mscp_scan
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
* 2. connect to remote host with mscp_connect()
|
* 2. connect to remote host with mscp_connect()
|
||||||
* 3. add path to source files with mscp_add_src_path()
|
* 3. add path to source files with mscp_add_src_path()
|
||||||
* 4. set path to destination with mscp_set_dst_path()
|
* 4. set path to destination with mscp_set_dst_path()
|
||||||
* 5. finish preparation with mscp_prepare()
|
* 5. start to scan source files with mscp_scan()
|
||||||
* 6. start copy with mscp_start()
|
* 6. start copy with mscp_start()
|
||||||
* 7. wait for copy finished with mscp_join()
|
* 7. wait for copy finished with mscp_join()
|
||||||
* 8. cleanup mscp instance with mscp_cleanup() and mscp_free()
|
* 8. cleanup mscp instance with mscp_cleanup() and mscp_free()
|
||||||
@@ -110,7 +110,7 @@ struct mscp *mscp_init(const char *remote_host, int direction,
|
|||||||
/**
|
/**
|
||||||
* @brief Connect the first SSH connection. mscp_connect connects to
|
* @brief Connect the first SSH connection. mscp_connect connects to
|
||||||
* remote host and initialize a SFTP session over the
|
* remote host and initialize a SFTP session over the
|
||||||
* connection. mscp_prepare() and mscp_start() require mscp_connect()
|
* connection. mscp_scan() and mscp_start() require mscp_connect()
|
||||||
* beforehand.
|
* beforehand.
|
||||||
*
|
*
|
||||||
* @param m mscp instance.
|
* @param m mscp instance.
|
||||||
@@ -150,31 +150,31 @@ int mscp_add_src_path(struct mscp *m, const char *src_path);
|
|||||||
*/
|
*/
|
||||||
int mscp_set_dst_path(struct mscp *m, const char *dst_path);
|
int mscp_set_dst_path(struct mscp *m, const char *dst_path);
|
||||||
|
|
||||||
/* check source files, resolve destination file paths for all source
|
/* scan source files, resolve destination file paths for all source
|
||||||
* files, and prepare chunks for all files. */
|
* files, and calculate chunks for all files. */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Prepare for file transfer. This function checks all source
|
* @brief Scan source paths and prepare. This function checks all
|
||||||
* files (recursively), resolve paths on the destination side, and
|
* source files (recursively), resolve paths on the destination side,
|
||||||
* calculate file chunks. This function is non-blocking.
|
* and calculate file chunks. This function is non-blocking.
|
||||||
*
|
*
|
||||||
* @param m mscp instance.
|
* @param m mscp instance.
|
||||||
*
|
*
|
||||||
* @return 0 on success, < 0 if an error occured.
|
* @return 0 on success, < 0 if an error occured.
|
||||||
* mscp_get_error() can be used to retrieve error message.
|
* mscp_get_error() can be used to retrieve error message.
|
||||||
*/
|
*/
|
||||||
int mscp_prepare(struct mscp *m);
|
int mscp_scan(struct mscp *m);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Join prepare thread invoked by mscp_prepare(). mscp_join()
|
* @brief Join scna thread invoked by mscp_scan(). mscp_join()
|
||||||
* involves this, so that mscp_prepare_join() should be called when
|
* involves this, so that mscp_scan_join() should be called when
|
||||||
* mscp_prepare() is called by mscp_start() is not.
|
* mscp_scan() is called by mscp_start() is not.
|
||||||
*
|
*
|
||||||
* @param m mscp instance.
|
* @param m mscp instance.
|
||||||
* @return 0 on success, < 0 if an error occured.
|
* @return 0 on success, < 0 if an error occured.
|
||||||
* mscp_get_error() can be used to retrieve error message.
|
* mscp_get_error() can be used to retrieve error message.
|
||||||
*/
|
*/
|
||||||
int mscp_prepare_join(struct mscp *m);
|
int mscp_scan_join(struct mscp *m);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Start to copy files. mscp_start() returns immediately. You
|
* @brief Start to copy files. mscp_start() returns immediately. You
|
||||||
|
|||||||
14
mscp/mscp.py
14
mscp/mscp.py
@@ -37,7 +37,7 @@ SEVERITY_DEBUG = pymscp.SEVERITY_DEBUG
|
|||||||
|
|
||||||
STATE_INIT = 0
|
STATE_INIT = 0
|
||||||
STATE_CONNECTED = 1
|
STATE_CONNECTED = 1
|
||||||
STATE_PREPARED = 2
|
STATE_SCANNED = 2
|
||||||
STATE_RUNNING = 3
|
STATE_RUNNING = 3
|
||||||
STATE_STOPPED = 4
|
STATE_STOPPED = 4
|
||||||
STATE_JOINED = 5
|
STATE_JOINED = 5
|
||||||
@@ -47,7 +47,7 @@ STATE_RELEASED = 7
|
|||||||
_state_str = {
|
_state_str = {
|
||||||
STATE_INIT: "init",
|
STATE_INIT: "init",
|
||||||
STATE_CONNECTED: "connected",
|
STATE_CONNECTED: "connected",
|
||||||
STATE_PREPARED: "prepared",
|
STATE_SCANNED: "scanned",
|
||||||
STATE_RUNNING: "running",
|
STATE_RUNNING: "running",
|
||||||
STATE_STOPPED: "stopped",
|
STATE_STOPPED: "stopped",
|
||||||
STATE_JOINED: "joined",
|
STATE_JOINED: "joined",
|
||||||
@@ -114,7 +114,7 @@ class mscp:
|
|||||||
self.dst_path = dst_path
|
self.dst_path = dst_path
|
||||||
pymscp.mscp_set_dst_path(m = self.m, dst_path = dst_path);
|
pymscp.mscp_set_dst_path(m = self.m, dst_path = dst_path);
|
||||||
|
|
||||||
def prepare(self):
|
def scan(self):
|
||||||
if self.state != STATE_CONNECTED:
|
if self.state != STATE_CONNECTED:
|
||||||
raise RuntimeError("invalid mscp state: {}".format(self.__state2str()))
|
raise RuntimeError("invalid mscp state: {}".format(self.__state2str()))
|
||||||
if not self.src_paths:
|
if not self.src_paths:
|
||||||
@@ -122,11 +122,11 @@ class mscp:
|
|||||||
if self.dst_path == None:
|
if self.dst_path == None:
|
||||||
raise RuntimeError("dst path is not set")
|
raise RuntimeError("dst path is not set")
|
||||||
|
|
||||||
pymscp.mscp_prepare(m = self.m)
|
pymscp.mscp_scan(m = self.m)
|
||||||
self.state = STATE_PREPARED
|
self.state = STATE_SCANNED
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
if self.state != STATE_PREPARED:
|
if self.state != STATE_SCANNED:
|
||||||
raise RuntimeError("invalid mscp state: {}".format(self.__state2str()))
|
raise RuntimeError("invalid mscp state: {}".format(self.__state2str()))
|
||||||
|
|
||||||
pymscp.mscp_start(m = self.m)
|
pymscp.mscp_start(m = self.m)
|
||||||
@@ -174,7 +174,7 @@ class mscp:
|
|||||||
|
|
||||||
self.set_dst_path(dst)
|
self.set_dst_path(dst)
|
||||||
|
|
||||||
self.prepare()
|
self.scan()
|
||||||
self.start()
|
self.start()
|
||||||
if nonblock:
|
if nonblock:
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -358,13 +358,13 @@ int main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mscp_prepare(m) < 0) {
|
if (mscp_scan(m) < 0) {
|
||||||
fprintf(stderr, "mscp_prepare: %s\n", mscp_get_error());
|
fprintf(stderr, "mscp_scan: %s\n", mscp_get_error());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dryrun) {
|
if (dryrun) {
|
||||||
ret = mscp_prepare_join(m);
|
ret = mscp_scan_join(m);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
42
src/mscp.c
42
src/mscp.c
@@ -36,8 +36,8 @@ struct mscp {
|
|||||||
struct list_head path_list;
|
struct list_head path_list;
|
||||||
struct chunk_pool cp;
|
struct chunk_pool cp;
|
||||||
|
|
||||||
pthread_t tid_prepare; /* tid for prepare thread */
|
pthread_t tid_scan; /* tid for scan thread */
|
||||||
int ret_prepare; /* return code from prepare thread */
|
int ret_scan; /* return code from scan thread */
|
||||||
|
|
||||||
size_t total_bytes; /* total bytes to be transferred */
|
size_t total_bytes; /* total bytes to be transferred */
|
||||||
struct mscp_thread *threads;
|
struct mscp_thread *threads;
|
||||||
@@ -345,19 +345,19 @@ static void mscp_stop_copy_thread(struct mscp *m)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mscp_stop_prepare_thread(struct mscp *m)
|
static void mscp_stop_scan_thread(struct mscp *m)
|
||||||
{
|
{
|
||||||
if (m->tid_prepare)
|
if (m->tid_scan)
|
||||||
pthread_cancel(m->tid_prepare);
|
pthread_cancel(m->tid_scan);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mscp_stop(struct mscp *m)
|
void mscp_stop(struct mscp *m)
|
||||||
{
|
{
|
||||||
mscp_stop_prepare_thread(m);
|
mscp_stop_scan_thread(m);
|
||||||
mscp_stop_copy_thread(m);
|
mscp_stop_copy_thread(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *mscp_prepare_thread(void *arg)
|
void *mscp_scan_thread(void *arg)
|
||||||
{
|
{
|
||||||
struct mscp *m = arg;
|
struct mscp *m = arg;
|
||||||
sftp_session src_sftp = NULL, dst_sftp = NULL;
|
sftp_session src_sftp = NULL, dst_sftp = NULL;
|
||||||
@@ -367,7 +367,7 @@ void *mscp_prepare_thread(void *arg)
|
|||||||
struct src *s;
|
struct src *s;
|
||||||
mstat ss, ds;
|
mstat ss, ds;
|
||||||
|
|
||||||
m->ret_prepare = 0;
|
m->ret_scan = 0;
|
||||||
|
|
||||||
switch (m->direction) {
|
switch (m->direction) {
|
||||||
case MSCP_DIRECTION_L2R:
|
case MSCP_DIRECTION_L2R:
|
||||||
@@ -430,21 +430,21 @@ void *mscp_prepare_thread(void *arg)
|
|||||||
|
|
||||||
mpr_info(m->msg_fp, "walk source path(s) done\n");
|
mpr_info(m->msg_fp, "walk source path(s) done\n");
|
||||||
|
|
||||||
m->ret_prepare = 0;
|
m->ret_scan = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
err_out:
|
err_out:
|
||||||
m->ret_prepare = -1;
|
m->ret_scan = -1;
|
||||||
mscp_stop_copy_thread(m);
|
mscp_stop_copy_thread(m);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mscp_prepare(struct mscp *m)
|
int mscp_scan(struct mscp *m)
|
||||||
{
|
{
|
||||||
int ret = pthread_create(&m->tid_prepare, NULL, mscp_prepare_thread, m);
|
int ret = pthread_create(&m->tid_scan, NULL, mscp_scan_thread, m);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
mscp_set_error("pthread_create_error: %d", ret);
|
mscp_set_error("pthread_create_error: %d", ret);
|
||||||
m->tid_prepare = 0;
|
m->tid_scan = 0;
|
||||||
mscp_stop(m);
|
mscp_stop(m);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -458,12 +458,12 @@ int mscp_prepare(struct mscp *m)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mscp_prepare_join(struct mscp *m)
|
int mscp_scan_join(struct mscp *m)
|
||||||
{
|
{
|
||||||
if (m->tid_prepare) {
|
if (m->tid_scan) {
|
||||||
pthread_join(m->tid_prepare, NULL);
|
pthread_join(m->tid_scan, NULL);
|
||||||
m->tid_prepare = 0;
|
m->tid_scan = 0;
|
||||||
return m->ret_prepare;
|
return m->ret_scan;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -482,7 +482,7 @@ int mscp_start(struct mscp *m)
|
|||||||
m->opts->nr_threads = n;
|
m->opts->nr_threads = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prepare thread instances */
|
/* scan thread instances */
|
||||||
m->threads = calloc(m->opts->nr_threads, sizeof(struct mscp_thread));
|
m->threads = calloc(m->opts->nr_threads, sizeof(struct mscp_thread));
|
||||||
memset(m->threads, 0, m->opts->nr_threads * sizeof(struct mscp_thread));
|
memset(m->threads, 0, m->opts->nr_threads * sizeof(struct mscp_thread));
|
||||||
for (n = 0; n < m->opts->nr_threads; n++) {
|
for (n = 0; n < m->opts->nr_threads; n++) {
|
||||||
@@ -509,8 +509,8 @@ int mscp_join(struct mscp *m)
|
|||||||
{
|
{
|
||||||
int n, ret = 0;
|
int n, ret = 0;
|
||||||
|
|
||||||
/* waiting for prepare thread joins... */
|
/* waiting for scan thread joins... */
|
||||||
ret = mscp_prepare_join(m);
|
ret = mscp_scan_join(m);
|
||||||
|
|
||||||
/* waiting for copy threads join... */
|
/* waiting for copy threads join... */
|
||||||
for (n = 0; n < m->opts->nr_threads; n++) {
|
for (n = 0; n < m->opts->nr_threads; n++) {
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ static PyObject *wrap_mscp_set_dst_path(PyObject *self, PyObject *args, PyObject
|
|||||||
return Py_BuildValue("");
|
return Py_BuildValue("");
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *wrap_mscp_prepare(PyObject *self, PyObject *args, PyObject *kw)
|
static PyObject *wrap_mscp_scan(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
{
|
{
|
||||||
char *keywords[] = { "m", NULL };
|
char *keywords[] = { "m", NULL };
|
||||||
unsigned long long addr;
|
unsigned long long addr;
|
||||||
@@ -283,7 +283,7 @@ static PyObject *wrap_mscp_prepare(PyObject *self, PyObject *args, PyObject *kw)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mscp_prepare(m) < 0) {
|
if (mscp_scan(m) < 0) {
|
||||||
PyErr_Format(PyExc_RuntimeError, mscp_get_error());
|
PyErr_Format(PyExc_RuntimeError, mscp_get_error());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -437,7 +437,7 @@ static PyMethodDef pymscpMethods[] = {
|
|||||||
METH_VARARGS | METH_KEYWORDS, NULL
|
METH_VARARGS | METH_KEYWORDS, NULL
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"mscp_prepare", (PyCFunction)wrap_mscp_prepare,
|
"mscp_scan", (PyCFunction)wrap_mscp_scan,
|
||||||
METH_VARARGS | METH_KEYWORDS, NULL
|
METH_VARARGS | METH_KEYWORDS, NULL
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user