mirror of
https://github.com/upa/mscp.git
synced 2026-02-13 08:34:41 +08:00
add message.h and message.c, mscp_set|get_error()
Instead of pr_err(), libmscp uses mscp_set_error() and applications use mscp_get_errror() to get error message.
This commit is contained in:
@@ -39,7 +39,7 @@ list(APPEND MSCP_LINK_LIBS ${ZLIB_LIBRARIES})
|
||||
|
||||
|
||||
|
||||
set(LIBMSCP_SRC src/mscp.c src/ssh.c src/path.c src/pprint.c src/platform.c)
|
||||
set(LIBMSCP_SRC src/mscp.c src/ssh.c src/path.c src/pprint.c src/platform.c src/message.c)
|
||||
|
||||
# shared libmscp
|
||||
add_library(mscp-shared SHARED ${LIBMSCP_SRC})
|
||||
|
||||
26
src/atomic.h
26
src/atomic.h
@@ -2,8 +2,10 @@
|
||||
#define _ATOMIC_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <util.h>
|
||||
|
||||
#include <message.h>
|
||||
|
||||
typedef int refcnt;
|
||||
|
||||
@@ -28,31 +30,13 @@ static inline void lock_init(lock *l)
|
||||
static inline void lock_acquire(lock *l)
|
||||
{
|
||||
int ret = pthread_mutex_lock(l);
|
||||
if (ret < 0) {
|
||||
switch (ret) {
|
||||
case EINVAL:
|
||||
pr_err("invalid mutex\n");
|
||||
exit(1);
|
||||
case EDEADLK:
|
||||
pr_err("a deadlock would occur\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
static inline void lock_release(lock *l)
|
||||
{
|
||||
int ret = pthread_mutex_unlock(l);
|
||||
if (ret < 0) {
|
||||
switch (ret) {
|
||||
case EINVAL:
|
||||
pr_err("invalid mutex\n");
|
||||
exit(1);
|
||||
case EPERM:
|
||||
pr_err("this thread does not hold this mutex\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
static inline void lock_release_via_cleanup(void *l)
|
||||
|
||||
51
src/main.c
51
src/main.c
@@ -66,7 +66,7 @@ char *split_remote_and_path(const char *string, char **remote, char **path)
|
||||
*/
|
||||
|
||||
if (!(s = strdup(string))) {
|
||||
pr_err("failed to allocate memory: %s\n", strerrno());
|
||||
fprintf(stderr, "strdup: %s\n", strerrno());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ struct target *validate_targets(char **arg, int len)
|
||||
int n;
|
||||
|
||||
if ((t = calloc(len, sizeof(struct target))) == NULL) {
|
||||
pr_err("failed to allocate memory: %s\n", strerrno());
|
||||
fprintf(stderr, "calloc: %s\n", strerrno());
|
||||
return NULL;
|
||||
}
|
||||
memset(t, 0, len * sizeof(struct target));
|
||||
@@ -141,19 +141,19 @@ struct target *validate_targets(char **arg, int len)
|
||||
|
||||
/* check inconsistent remote position in args */
|
||||
if (t[0].remote == NULL && t[len - 1].remote == NULL) {
|
||||
pr_err("no remote host given\n");
|
||||
fprintf(stderr, "no remote host given\n");
|
||||
goto free_split_out;
|
||||
}
|
||||
|
||||
if (t[0].remote != NULL && t[len - 1].remote != NULL) {
|
||||
pr_err("no local path given\n");
|
||||
fprintf(stderr, "no local path given\n");
|
||||
goto free_split_out;
|
||||
}
|
||||
|
||||
return t;
|
||||
|
||||
invalid_remotes:
|
||||
pr_err("specified remote host invalid\n");
|
||||
fprintf(stderr, "specified remote host invalid\n");
|
||||
|
||||
free_split_out:
|
||||
for (n = 0; n < len; n++)
|
||||
@@ -190,7 +190,8 @@ int main(int argc, char **argv)
|
||||
case 'n':
|
||||
o.nr_threads = atoi(optarg);
|
||||
if (o.nr_threads < 1) {
|
||||
pr_err("invalid number of connections: %s\n", optarg);
|
||||
fprintf(stderr, "invalid number of connections: %s\n",
|
||||
optarg);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
@@ -223,42 +224,42 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
case 'l':
|
||||
if (strlen(optarg) > MSCP_SSH_MAX_LOGIN_NAME - 1) {
|
||||
pr_err("too long login name: %s\n", optarg);
|
||||
fprintf(stderr, "long login name: %s\n", optarg);
|
||||
return -1;
|
||||
}
|
||||
strncpy(s.login_name, optarg, MSCP_SSH_MAX_LOGIN_NAME - 1);
|
||||
break;
|
||||
case 'p':
|
||||
if (strlen(optarg) > MSCP_SSH_MAX_PORT_STR - 1) {
|
||||
pr_err("too long port string: %s\n", optarg);
|
||||
fprintf(stderr, "long port string: %s\n", optarg);
|
||||
return -1;
|
||||
}
|
||||
strncpy(s.port, optarg, MSCP_SSH_MAX_PORT_STR);
|
||||
break;
|
||||
case 'i':
|
||||
if (strlen(optarg) > MSCP_SSH_MAX_IDENTITY_PATH - 1) {
|
||||
pr_err("too long identity path: %s\n", optarg);
|
||||
fprintf(stderr, "long identity path: %s\n", optarg);
|
||||
return -1;
|
||||
}
|
||||
strncpy(s.identity, optarg, MSCP_SSH_MAX_IDENTITY_PATH);
|
||||
break;
|
||||
case 'c':
|
||||
if (strlen(optarg) > MSCP_SSH_MAX_CIPHER_STR - 1) {
|
||||
pr_err("too long cipher string: %s\n", optarg);
|
||||
fprintf(stderr, "long cipher string: %s\n", optarg);
|
||||
return -1;
|
||||
}
|
||||
strncpy(s.cipher, optarg, MSCP_SSH_MAX_CIPHER_STR);
|
||||
break;
|
||||
case 'M':
|
||||
if (strlen(optarg) > MSCP_SSH_MAX_HMAC_STR - 1) {
|
||||
pr_err("too long hmac string: %s\n", optarg);
|
||||
fprintf(stderr, "long hmac string: %s\n", optarg);
|
||||
return -1;
|
||||
}
|
||||
strncpy(s.hmac, optarg, MSCP_SSH_MAX_HMAC_STR);
|
||||
break;
|
||||
case 'C':
|
||||
if (strlen(optarg) > MSCP_SSH_MAX_COMP_STR - 1) {
|
||||
pr_err("too long compress string: %s\n", optarg);
|
||||
fprintf(stderr, "long compress string: %s\n", optarg);
|
||||
return -1;
|
||||
}
|
||||
strncpy(s.compress, optarg, MSCP_SSH_MAX_COMP_STR);
|
||||
@@ -301,32 +302,44 @@ int main(int argc, char **argv)
|
||||
remote = t[i - 1].remote;
|
||||
}
|
||||
|
||||
if ((m = mscp_init(remote, &o, &s)) == NULL)
|
||||
if ((m = mscp_init(remote, &o, &s)) == NULL) {
|
||||
fprintf(stderr, "mscp_init: %s\n", mscp_get_error());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mscp_connect(m) < 0)
|
||||
if (mscp_connect(m) < 0) {
|
||||
fprintf(stderr, "mscp_connect: %s\n", mscp_get_error());
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (n = 0; n < i - 1; n++) {
|
||||
if (mscp_add_src_path(m, t[n].path) < 0)
|
||||
if (mscp_add_src_path(m, t[n].path) < 0) {
|
||||
fprintf(stderr, "mscp_add_src_path: %s\n", mscp_get_error());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (mscp_set_dst_path(m, t[i - 1].path) < 0)
|
||||
if (mscp_set_dst_path(m, t[i - 1].path) < 0) {
|
||||
fprintf(stderr, "mscp_set_dst_path: %s\n", mscp_get_error());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mscp_prepare(m) < 0)
|
||||
if (mscp_prepare(m) < 0) {
|
||||
fprintf(stderr, "mscp_prepare: %s\n", mscp_get_error());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (print_stat_init() < 0)
|
||||
return -1;
|
||||
|
||||
if (signal(SIGINT, sigint_handler) == SIG_ERR) {
|
||||
pr_err("cannot set handler for SIGINT: %s\n", strerrno());
|
||||
fprintf(stderr, "signal: %s\n", strerrno());
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = mscp_start(m);
|
||||
if (ret < 0)
|
||||
fprintf(stderr, "%s\n", mscp_get_error());
|
||||
|
||||
print_stat_final();
|
||||
|
||||
@@ -488,7 +501,7 @@ int print_stat_init()
|
||||
memset(&x, 0, sizeof(x));
|
||||
|
||||
if (signal(SIGALRM, print_stat_handler) == SIG_ERR) {
|
||||
pr_err("signal: %s\n", strerrno());
|
||||
fprintf(stderr, "signal: %s\n", strerrno());
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
25
src/message.c
Normal file
25
src/message.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <message.h>
|
||||
|
||||
#define MSCP_ERRMSG_SIZE (PATH_MAX * 2)
|
||||
|
||||
static char errmsg[MSCP_ERRMSG_SIZE];
|
||||
|
||||
void _mscp_set_error(const char *fmt, ...)
|
||||
{
|
||||
va_list va;
|
||||
|
||||
memset(errmsg, 0, sizeof(errmsg));
|
||||
va_start(va, fmt);
|
||||
vsnprintf(errmsg, sizeof(errmsg) - 1, fmt, va);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
const char *mscp_get_error()
|
||||
{
|
||||
return errmsg;
|
||||
}
|
||||
32
src/message.h
Normal file
32
src/message.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef _MESSAGE_H_
|
||||
#define _MESSAGE_H_
|
||||
|
||||
#include <libgen.h>
|
||||
|
||||
enum {
|
||||
MSCP_SEVERITY_ERR = 0,
|
||||
MSCP_SEVERITY_WARN,
|
||||
MSCP_SEVERITY_NOTICE,
|
||||
MSCP_SEVERITY_INFO,
|
||||
MSCP_SEVERITY_DEBUG,
|
||||
};
|
||||
|
||||
/* message print. printed messages are passed to application via msg_fd */
|
||||
//void mprint_set_severity(int severity);
|
||||
//void mprint(int severity, const char *fmt, ...);
|
||||
|
||||
#define mpr_err(fmt, ...) mprint(MSCP_SEVERITY_ERR, fmt, ##__VA_ARGS__)
|
||||
#define mpr_warn(fmt, ...) mprint(MSCP_SEVERITY_WARN, fmt, ##__VA_ARGS__)
|
||||
#define mpr_notice(fmt, ...) mprint(MSCP_SEVERITY_NOTICE, fmt, ##__VA_ARGS__)
|
||||
#define mpr_info(fmt, ...) mprint(MSCP_SEVERITY_INFO, fmt, ##__VA_ARGS__)
|
||||
#define mpr_debug(fmt, ...) mprint(MSCP_SEVERITY_DEBUG, fmt, ##__VA_ARGS__)
|
||||
|
||||
|
||||
/* error message buffer */
|
||||
#define mscp_set_error(fmt, ...) \
|
||||
_mscp_set_error("%s:%d:%s: " fmt, \
|
||||
basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__)
|
||||
|
||||
void _mscp_set_error(const char *fmt, ...);
|
||||
|
||||
#endif /* _MESSAGE_H_ */
|
||||
54
src/mscp.c
54
src/mscp.c
@@ -11,6 +11,7 @@
|
||||
#include <pprint.h>
|
||||
#include <atomic.h>
|
||||
#include <platform.h>
|
||||
#include <message.h>
|
||||
#include <mscp.h>
|
||||
|
||||
struct mscp {
|
||||
@@ -82,7 +83,7 @@ static int expand_coremask(const char *coremask, int **cores, int *nr_cores)
|
||||
|
||||
core_list = realloc(NULL, sizeof(int) * 64);
|
||||
if (!core_list) {
|
||||
pr_err("failed to realloc: %s\n", strerrno());
|
||||
mscp_set_error("failed to realloc: %s", strerrno());
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -92,7 +93,7 @@ static int expand_coremask(const char *coremask, int **cores, int *nr_cores)
|
||||
c[0] = _coremask[n];
|
||||
v = strtol(c, NULL, 16);
|
||||
if (v == LONG_MIN || v == LONG_MAX) {
|
||||
pr_err("invalid coremask: %s\n", coremask);
|
||||
mscp_set_error("invalid coremask: %s", coremask);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -104,7 +105,7 @@ static int expand_coremask(const char *coremask, int **cores, int *nr_cores)
|
||||
nr_usable++;
|
||||
core_list = realloc(core_list, sizeof(int) * nr_usable);
|
||||
if (!core_list) {
|
||||
pr_err("failed to realloc: %s\n", strerrno());
|
||||
mscp_set_error("realloc: %s", strerrno());
|
||||
return -1;
|
||||
}
|
||||
core_list[nr_usable - 1] = nr_all - 1;
|
||||
@@ -113,7 +114,7 @@ static int expand_coremask(const char *coremask, int **cores, int *nr_cores)
|
||||
}
|
||||
|
||||
if (nr_usable < 1) {
|
||||
pr_err("invalid core mask: %s\n", coremask);
|
||||
mscp_set_error("invalid core mask: %s", coremask);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -131,18 +132,18 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
|
||||
{
|
||||
if (!(o->direction == MSCP_DIRECTION_L2R ||
|
||||
o->direction == MSCP_DIRECTION_R2L)) {
|
||||
pr_err("invalid copy direction: %d\n", o->direction);
|
||||
mscp_set_error("invalid copy direction: %d", o->direction);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (o->nr_threads < 0) {
|
||||
pr_err("invalid nr_threads: %d\n", o->nr_threads);
|
||||
mscp_set_error("invalid nr_threads: %d", o->nr_threads);
|
||||
return -1;
|
||||
} else if (o->nr_threads == 0)
|
||||
o->nr_threads = default_nr_threads();
|
||||
|
||||
if (o->nr_ahead < 0) {
|
||||
pr_err("invalid nr_ahead: %d\n", o->nr_ahead);
|
||||
mscp_set_error("invalid nr_ahead: %d", o->nr_ahead);
|
||||
return -1;
|
||||
} else if (o->nr_ahead == 0)
|
||||
o->nr_ahead = DEFAULT_NR_AHEAD;
|
||||
@@ -152,9 +153,9 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
|
||||
else {
|
||||
if (o->min_chunk_sz < getpagesize() ||
|
||||
o->min_chunk_sz % getpagesize() != 0) {
|
||||
pr_err("min chunk size must be "
|
||||
"larget than and multiple of page size %d: %lu\n",
|
||||
getpagesize(), o->min_chunk_sz);
|
||||
mscp_set_error("min chunk size must be "
|
||||
"larget than and multiple of page size %d: %lu",
|
||||
getpagesize(), o->min_chunk_sz);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -162,13 +163,14 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
|
||||
if (o->max_chunk_sz) {
|
||||
if (o->max_chunk_sz < getpagesize() ||
|
||||
o->max_chunk_sz % getpagesize() != 0) {
|
||||
pr_err("min chunk size must be "
|
||||
"larget than and multiple of page size %d: %lu\n",
|
||||
getpagesize(), o->max_chunk_sz);
|
||||
mscp_set_error("min chunk size must be larget than and "
|
||||
"multiple of page size %d: %lu",
|
||||
getpagesize(), o->max_chunk_sz);
|
||||
}
|
||||
if (o->min_chunk_sz > o->max_chunk_sz) {
|
||||
pr_err("smaller max chunk size than min chunk size: %lu < %lu\n",
|
||||
o->max_chunk_sz, o->min_chunk_sz);
|
||||
mscp_set_error("smaller max chunk size than "
|
||||
"min chunk size: %lu < %lu",
|
||||
o->max_chunk_sz, o->min_chunk_sz);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -176,7 +178,7 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
|
||||
if (o->buf_sz == 0)
|
||||
o->buf_sz = DEFAULT_BUF_SZ;
|
||||
else if (o->buf_sz == 0) {
|
||||
pr_err("invalid buf size: %lu\n", o->buf_sz);
|
||||
mscp_set_error("invalid buf size: %lu", o->buf_sz);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -191,7 +193,7 @@ struct mscp *mscp_init(const char *remote_host,
|
||||
|
||||
m = malloc(sizeof(*m));
|
||||
if (!m) {
|
||||
pr_err("failed to allocate memory: %s\n", strerrno());
|
||||
mscp_set_error("failed to allocate memory: %s", strerrno());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -205,7 +207,7 @@ struct mscp *mscp_init(const char *remote_host,
|
||||
lock_init(&m->chunk_lock);
|
||||
m->remote = strdup(remote_host);
|
||||
if (!m->remote) {
|
||||
pr_err("failed to allocate memory: %s\n", strerrno());
|
||||
mscp_set_error("failed to allocate memory: %s", strerrno());
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
@@ -245,14 +247,14 @@ int mscp_add_src_path(struct mscp *m, const char *src_path)
|
||||
|
||||
s = malloc(sizeof(*s));
|
||||
if (!s) {
|
||||
pr_err("failed to allocate memory: %s\n", strerrno());
|
||||
mscp_set_error("failed to allocate memory: %s", strerrno());
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->path = strdup(src_path);
|
||||
if (!s->path) {
|
||||
pr_err("failed to allocate memory: %s\n", strerrno());
|
||||
mscp_set_error("failed to allocate memory: %s", strerrno());
|
||||
free(s);
|
||||
return -1;
|
||||
}
|
||||
@@ -264,7 +266,7 @@ int mscp_add_src_path(struct mscp *m, const char *src_path)
|
||||
int mscp_set_dst_path(struct mscp *m, const char *dst_path)
|
||||
{
|
||||
if (strlen(dst_path) + 1 >= PATH_MAX) {
|
||||
pr_err("too long dst path: %s\n", dst_path);
|
||||
mscp_set_error("too long dst path: %s", dst_path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -296,7 +298,7 @@ int mscp_prepare(struct mscp *m)
|
||||
dst_sftp = NULL;
|
||||
break;
|
||||
default:
|
||||
pr_err("invalid copy direction: %d\n", m->opts->direction);
|
||||
mscp_set_error("invalid copy direction: %d", m->opts->direction);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -313,7 +315,7 @@ int mscp_prepare(struct mscp *m)
|
||||
/* walk a src_path recusively, and resolve path->dst_path for each src */
|
||||
list_for_each_entry(s, &m->src_list, list) {
|
||||
if (mscp_stat(s->path, &ss, src_sftp) < 0) {
|
||||
pr_err("stat: %s\n", mscp_strerror(src_sftp));
|
||||
mscp_set_error("stat: %s", mscp_strerror(src_sftp));
|
||||
return -1;
|
||||
}
|
||||
src_path_is_dir = mstat_is_dir(ss);
|
||||
@@ -398,7 +400,7 @@ int mscp_start(struct mscp *m)
|
||||
struct mscp_thread *t = &m->threads[n];
|
||||
ret = pthread_create(&t->tid, NULL, mscp_copy_thread, t);
|
||||
if (ret < 0) {
|
||||
pr_err("pthread_create error: %d\n", ret);
|
||||
mscp_set_error("pthread_create error: %d", ret);
|
||||
mscp_stop(m);
|
||||
ret = 1;
|
||||
goto join_out;
|
||||
@@ -504,8 +506,8 @@ void *mscp_copy_thread(void *arg)
|
||||
pthread_cleanup_pop(1);
|
||||
|
||||
if (t->ret < 0)
|
||||
pr_err("copy failed: chunk %s 0x%010lx-0x%010lx\n",
|
||||
c->p->path, c->off, c->off + c->len);
|
||||
mscp_set_error("copy failed: chunk %s 0x%010lx-0x%010lx",
|
||||
c->p->path, c->off, c->off + c->len);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -63,6 +63,12 @@ struct mscp;
|
||||
struct mscp *mscp_init(const char *remote_host,
|
||||
struct mscp_opts *o, struct mscp_ssh_opts *s);
|
||||
|
||||
/* return a fd for read message from mscp */
|
||||
int mscp_msg_fd(struct mscp *m);
|
||||
|
||||
/* get message for the most recent error (not thread safe) */
|
||||
const char *mscp_get_error();
|
||||
|
||||
/* establish the first SFTP session. mscp_prepare() and mscp_start()
|
||||
* requires mscp_connect() beforehand */
|
||||
int mscp_connect(struct mscp *m);
|
||||
|
||||
47
src/path.c
47
src/path.c
@@ -11,7 +11,7 @@
|
||||
#include <atomic.h>
|
||||
#include <path.h>
|
||||
#include <pprint.h>
|
||||
|
||||
#include <message.h>
|
||||
|
||||
static int append_path(sftp_session sftp, const char *path, mstat s,
|
||||
struct list_head *path_list)
|
||||
@@ -19,7 +19,7 @@ static int append_path(sftp_session sftp, const char *path, mstat s,
|
||||
struct path *p;
|
||||
|
||||
if (!(p = malloc(sizeof(*p)))) {
|
||||
pr_err("failed to allocate memory: %s\n", strerrno());
|
||||
mscp_set_error("failed to allocate memory: %s", strerrno());
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ static int walk_path_recursive(sftp_session sftp, const char *path,
|
||||
continue;
|
||||
|
||||
if (strlen(path) + 1 + strlen(mdirent_name(e)) > PATH_MAX) {
|
||||
pr_err("too long path: %s/%s\n", path, mdirent_name(e));
|
||||
mscp_set_error("too long path: %s/%s", path, mdirent_name(e));
|
||||
return -1;
|
||||
}
|
||||
snprintf(next_path, sizeof(next_path), "%s/%s", path, mdirent_name(e));
|
||||
@@ -114,7 +114,7 @@ static int src2dst_path(const char *src_path, const char *src_file_path,
|
||||
strncpy(copy, src_path, PATH_MAX - 1);
|
||||
prefix = dirname(copy);
|
||||
if (!prefix) {
|
||||
pr_err("dirname: %s\n", strerrno());
|
||||
mscp_set_error("dirname: %s", strerrno());
|
||||
return -1;
|
||||
}
|
||||
if (strlen(prefix) == 1 && prefix[0] == '.')
|
||||
@@ -184,7 +184,7 @@ static struct chunk *alloc_chunk(struct path *p)
|
||||
struct chunk *c;
|
||||
|
||||
if (!(c = malloc(sizeof(*c)))) {
|
||||
pr_err("%s\n", strerrno());
|
||||
mscp_set_error("malloc %s", strerrno());
|
||||
return NULL;
|
||||
}
|
||||
memset(c, 0, sizeof(*c));
|
||||
@@ -292,7 +292,8 @@ static int touch_dst_path(struct path *p, sftp_session sftp)
|
||||
if (mscp_stat_check_err_noent(sftp) == 0) {
|
||||
/* no file on the path. create directory. */
|
||||
if (mscp_mkdir(path, mode, sftp) < 0) {
|
||||
pr_err("mkdir %s: %s", path, mscp_strerror(sftp));
|
||||
mscp_set_error("mkdir %s: %s", path,
|
||||
mscp_strerror(sftp));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -357,8 +358,8 @@ static int copy_chunk_l2r(struct chunk *c, int fd, sftp_file sf,
|
||||
reqs[idx].len = sftp_async_write(sf, read_to_buf, reqs[idx].len, &fd,
|
||||
&reqs[idx].id);
|
||||
if (reqs[idx].len < 0) {
|
||||
pr_err("sftp_async_write: %s or %s\n",
|
||||
sftp_get_ssh_error(sf->sftp), strerrno());
|
||||
mscp_set_error("sftp_async_write: %s or %s",
|
||||
sftp_get_ssh_error(sf->sftp), strerrno());
|
||||
return -1;
|
||||
}
|
||||
thrown -= reqs[idx].len;
|
||||
@@ -367,8 +368,8 @@ static int copy_chunk_l2r(struct chunk *c, int fd, sftp_file sf,
|
||||
for (idx = 0; remaind > 0; idx = (idx + 1) % nr_ahead) {
|
||||
ret = sftp_async_write_end(sf, reqs[idx].id, 1);
|
||||
if (ret != SSH_OK) {
|
||||
pr_err("sftp_async_write_end: %s\n",
|
||||
sftp_get_ssh_error(sf->sftp));
|
||||
mscp_set_error("sftp_async_write_end: %s",
|
||||
sftp_get_ssh_error(sf->sftp));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -385,16 +386,17 @@ static int copy_chunk_l2r(struct chunk *c, int fd, sftp_file sf,
|
||||
reqs[idx].len = sftp_async_write(sf, read_to_buf, reqs[idx].len, &fd,
|
||||
&reqs[idx].id);
|
||||
if (reqs[idx].len < 0) {
|
||||
pr_err("sftp_async_write: %s or %s\n",
|
||||
sftp_get_ssh_error(sf->sftp), strerrno());
|
||||
mscp_set_error("sftp_async_write: %s or %s",
|
||||
sftp_get_ssh_error(sf->sftp), strerrno());
|
||||
return -1;
|
||||
}
|
||||
thrown -= reqs[idx].len;
|
||||
}
|
||||
|
||||
if (remaind < 0) {
|
||||
pr_err("invalid remaind bytes %ld. last async_write_end bytes %lu.",
|
||||
remaind, reqs[idx].len);
|
||||
mscp_set_error("invalid remaind bytes %ld. "
|
||||
"last async_write_end bytes %lu.",
|
||||
remaind, reqs[idx].len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -422,8 +424,8 @@ static int copy_chunk_r2l(struct chunk *c, sftp_file sf, int fd,
|
||||
reqs[idx].len = min(thrown, sizeof(buf));
|
||||
reqs[idx].id = sftp_async_read_begin(sf, reqs[idx].len);
|
||||
if (reqs[idx].id < 0) {
|
||||
pr_err("sftp_async_read_begin: %d\n",
|
||||
sftp_get_error(sf->sftp));
|
||||
mscp_set_error("sftp_async_read_begin: %d",
|
||||
sftp_get_error(sf->sftp));
|
||||
return -1;
|
||||
}
|
||||
thrown -= reqs[idx].len;
|
||||
@@ -432,7 +434,8 @@ static int copy_chunk_r2l(struct chunk *c, sftp_file sf, int fd,
|
||||
for (idx = 0; remaind > 0; idx = (idx + 1) % nr_ahead) {
|
||||
read_bytes = sftp_async_read(sf, buf, reqs[idx].len, reqs[idx].id);
|
||||
if (read_bytes == SSH_ERROR) {
|
||||
pr_err("sftp_async_read: %d\n", sftp_get_error(sf->sftp));
|
||||
mscp_set_error("sftp_async_read: %d",
|
||||
sftp_get_error(sf->sftp));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -444,12 +447,12 @@ static int copy_chunk_r2l(struct chunk *c, sftp_file sf, int fd,
|
||||
|
||||
write_bytes = write(fd, buf, read_bytes);
|
||||
if (write_bytes < 0) {
|
||||
pr_err("write: %s\n", strerrno());
|
||||
mscp_set_error("write: %s", strerrno());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (write_bytes < read_bytes) {
|
||||
pr_err("failed to write full bytes\n");
|
||||
mscp_set_error("failed to write full bytes");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -458,9 +461,9 @@ static int copy_chunk_r2l(struct chunk *c, sftp_file sf, int fd,
|
||||
}
|
||||
|
||||
if (remaind < 0) {
|
||||
pr_err("invalid remaind bytes %ld. last async_read bytes %ld. "
|
||||
"last write bytes %ld\n",
|
||||
remaind, read_bytes, write_bytes);
|
||||
mscp_set_error("invalid remaind bytes %ld. last async_read bytes %ld. "
|
||||
"last write bytes %ld",
|
||||
remaind, read_bytes, write_bytes);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
29
src/path.h
29
src/path.h
@@ -9,6 +9,7 @@
|
||||
#include <list.h>
|
||||
#include <atomic.h>
|
||||
#include <ssh.h>
|
||||
#include <message.h>
|
||||
|
||||
struct path {
|
||||
struct list_head list; /* mscp->path_list */
|
||||
@@ -99,15 +100,15 @@ static mdir *mscp_opendir(const char *path, sftp_session sftp)
|
||||
if (sftp) {
|
||||
d->r = sftp_opendir(sftp, path);
|
||||
if (!d->r) {
|
||||
pr_err("sftp_opendir '%s': %s\n",
|
||||
path, sftp_get_ssh_error(sftp));
|
||||
mscp_set_error("sftp_opendir '%s': %s",
|
||||
path, sftp_get_ssh_error(sftp));
|
||||
free(d);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
d->l = opendir(path);
|
||||
if (!d->l) {
|
||||
pr_err("opendir '%s': %s\n", path, strerrno());
|
||||
mscp_set_error("opendir '%s': %s", path, strerrno());
|
||||
free(d);
|
||||
return NULL;
|
||||
}
|
||||
@@ -207,14 +208,13 @@ static int mscp_mkdir(const char *path, mode_t mode, sftp_session sftp)
|
||||
ret = sftp_mkdir(sftp, path, mode);
|
||||
if (ret < 0 &&
|
||||
sftp_get_error(sftp) != SSH_FX_FILE_ALREADY_EXISTS) {
|
||||
pr_err("failed to create %s: %s\n",
|
||||
path, sftp_get_ssh_error(sftp));
|
||||
mscp_set_error("sftp_mkdir '%s': %s",
|
||||
path, sftp_get_ssh_error(sftp));
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (mkdir(path, mode) == -1 && errno != EEXIST) {
|
||||
pr_err("failed to create %s: %s\n",
|
||||
path, strerrno());
|
||||
mscp_set_error("mkdir '%s': %s", path, strerrno());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -240,12 +240,14 @@ static mfh mscp_open(const char *path, int flags, mode_t mode, size_t off,
|
||||
if (sftp) {
|
||||
h.sf = sftp_open(sftp, path, flags, mode);
|
||||
if (!h.sf) {
|
||||
pr_err("sftp_open '%s': %s\n", path, sftp_get_ssh_error(sftp));
|
||||
mscp_set_error("sftp_open '%s': %s",
|
||||
path, sftp_get_ssh_error(sftp));
|
||||
return h;
|
||||
}
|
||||
|
||||
if (sftp_seek64(h.sf, off) < 0) {
|
||||
pr_err("sftp_seek64 '%s': %s\n", path, sftp_get_ssh_error(sftp));
|
||||
mscp_set_error("sftp_seek64 '%s': %s",
|
||||
path, sftp_get_ssh_error(sftp));
|
||||
sftp_close(h.sf);
|
||||
h.sf = NULL;
|
||||
return h;
|
||||
@@ -253,11 +255,11 @@ static mfh mscp_open(const char *path, int flags, mode_t mode, size_t off,
|
||||
} else {
|
||||
h.fd = open(path, flags, mode);
|
||||
if (h.fd < 0) {
|
||||
pr_err("open '%s': %s\n", path, strerrno());
|
||||
mscp_set_error("open '%s': %s", path, strerrno());
|
||||
return h;
|
||||
}
|
||||
if (lseek(h.fd, off, SEEK_SET) < 0) {
|
||||
pr_err("lseek '%s': %s\n", path, strerrno());
|
||||
mscp_set_error("lseek '%s': %s", path, strerrno());
|
||||
close(h.fd);
|
||||
h.fd = -1;
|
||||
return h;
|
||||
@@ -285,12 +287,13 @@ static int mscp_chmod(const char *path, mode_t mode, sftp_session sftp)
|
||||
{
|
||||
if (sftp) {
|
||||
if (sftp_chmod(sftp, path, mode) < 0) {
|
||||
pr_err("sftp_chmod '%s': %s\n", path, sftp_get_ssh_error(sftp));
|
||||
mscp_set_error("sftp_chmod '%s': %s",
|
||||
path, sftp_get_ssh_error(sftp));
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (chmod(path, mode) < 0) {
|
||||
pr_err("chmod '%s': %s\n", path, strerrno());
|
||||
mscp_set_error("chmod '%s': %s", path, strerrno());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <util.h>
|
||||
#include <platform.h>
|
||||
#include <message.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
int nr_cpus()
|
||||
@@ -18,7 +19,7 @@ int nr_cpus()
|
||||
size_t size = sizeof(n);
|
||||
|
||||
if (sysctlbyname("machdep.cpu.core_count", &n, &size, NULL, 0) != 0) {
|
||||
pr_err("failed to get number of cpu cores: %s\n", strerrno());
|
||||
mscp_set_error("failed to get number of cpu cores: %s", strerrno());
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -51,8 +52,8 @@ int set_thread_affinity(pthread_t tid, int core)
|
||||
CPU_SET(core, &target_cpu_set);
|
||||
ret = pthread_setaffinity_np(tid, sizeof(target_cpu_set), &target_cpu_set);
|
||||
if (ret < 0)
|
||||
pr_err("failed to set thread/cpu affinity for core %d: %s",
|
||||
core, strerrno());
|
||||
mscp_set_error("failed to set thread/cpu affinity for core %d: %s",
|
||||
core, strerrno());
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
32
src/ssh.c
32
src/ssh.c
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <ssh.h>
|
||||
#include <util.h>
|
||||
#include <message.h>
|
||||
|
||||
static int ssh_verify_known_hosts(ssh_session session);
|
||||
|
||||
@@ -19,47 +20,47 @@ static int ssh_set_opts(ssh_session ssh, struct mscp_ssh_opts *opts)
|
||||
|
||||
if (is_specified(opts->login_name) &&
|
||||
ssh_options_set(ssh, SSH_OPTIONS_USER, opts->login_name) < 0) {
|
||||
pr_err("failed to set login name\n");
|
||||
mscp_set_error("failed to set login name");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (is_specified(opts->port) &&
|
||||
ssh_options_set(ssh, SSH_OPTIONS_PORT_STR, opts->port) < 0) {
|
||||
pr_err("failed to set port number\n");
|
||||
mscp_set_error("failed to set port number");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (is_specified(opts->identity) &&
|
||||
ssh_options_set(ssh, SSH_OPTIONS_IDENTITY, opts->identity) < 0) {
|
||||
pr_err("failed to set identity\n");
|
||||
mscp_set_error("failed to set identity");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (is_specified(opts->cipher)) {
|
||||
if (ssh_options_set(ssh, SSH_OPTIONS_CIPHERS_C_S, opts->cipher) < 0) {
|
||||
pr_err("failed to set cipher for client to server\n");
|
||||
mscp_set_error("failed to set cipher for client to server");
|
||||
return -1;
|
||||
}
|
||||
if (ssh_options_set(ssh, SSH_OPTIONS_CIPHERS_S_C, opts->cipher) < 0) {
|
||||
pr_err("failed to set cipher for server to client\n");
|
||||
mscp_set_error("failed to set cipher for server to client");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_specified(opts->hmac)) {
|
||||
if (ssh_options_set(ssh, SSH_OPTIONS_HMAC_C_S, opts->hmac) < 0) {
|
||||
pr_err("failed to set hmac for client to server\n");
|
||||
mscp_set_error("failed to set hmac for client to server");
|
||||
return -1;
|
||||
}
|
||||
if (ssh_options_set(ssh, SSH_OPTIONS_HMAC_S_C, opts->hmac) < 0) {
|
||||
pr_err("failed to set hmac for server to client\n");
|
||||
mscp_set_error("failed to set hmac for server to client");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_specified(opts->compress) &&
|
||||
ssh_options_set(ssh, SSH_OPTIONS_COMPRESSION, opts->compress) < 0) {
|
||||
pr_err("failed to enable ssh compression\n");
|
||||
mscp_set_error("failed to enable ssh compression");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ static int ssh_set_opts(ssh_session ssh, struct mscp_ssh_opts *opts)
|
||||
if (!opts->enable_nagle) {
|
||||
int v = 1;
|
||||
if (ssh_options_set(ssh, SSH_OPTIONS_NODELAY, &v) < 0) {
|
||||
pr_err("failed to set TCP_NODELAY\n");
|
||||
mscp_set_error("failed to set TCP_NODELAY");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -152,17 +153,17 @@ static ssh_session ssh_init_session(const char *sshdst, struct mscp_ssh_opts *op
|
||||
goto free_out;
|
||||
|
||||
if (ssh_options_set(ssh, SSH_OPTIONS_HOST, sshdst) != SSH_OK) {
|
||||
pr_err("failed to set destination host\n");
|
||||
mscp_set_error("failed to set destination host");
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
if (ssh_connect(ssh) != SSH_OK) {
|
||||
pr_err("failed to connect ssh server: %s\n", ssh_get_error(ssh));
|
||||
mscp_set_error("failed to connect ssh server: %s", ssh_get_error(ssh));
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
if (ssh_authenticate(ssh, opts) != 0) {
|
||||
pr_err("authentication failed: %s\n", ssh_get_error(ssh));
|
||||
mscp_set_error("authentication failed: %s", ssh_get_error(ssh));
|
||||
goto disconnect_out;
|
||||
}
|
||||
|
||||
@@ -190,13 +191,14 @@ sftp_session ssh_init_sftp_session(const char *sshdst, struct mscp_ssh_opts *opt
|
||||
|
||||
sftp = sftp_new(ssh);
|
||||
if (!sftp) {
|
||||
pr_err("failed to allocate sftp session: %s\n", ssh_get_error(ssh));
|
||||
mscp_set_error("failed to allocate sftp session: %s",
|
||||
ssh_get_error(ssh));
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
if (sftp_init(sftp) != SSH_OK) {
|
||||
pr_err("failed to initialize sftp session: err code %d\n",
|
||||
sftp_get_error(sftp));
|
||||
mscp_set_error("failed to initialize sftp session: err code %d",
|
||||
sftp_get_error(sftp));
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user