From 309371ed75ea5eebdde4d2e84ed6ad298dd38b12 Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Fri, 10 Mar 2023 21:10:06 +0900 Subject: now make install installs libmscp --- CMakeLists.txt | 27 ++++-- Doxyfile | 2 +- include/mscp.h | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mscp.h | 264 --------------------------------------------------------- 4 files changed, 283 insertions(+), 274 deletions(-) create mode 100644 include/mscp.h delete mode 100644 src/mscp.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 68902de..d890381 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,12 +47,11 @@ add_subdirectory(libssh EXCLUDE_FROM_ALL) -set(MSCP_COMPILE_OPTS "") -set(MSCP_INCLUDE_DIRS ${mscp_SOURCE_DIR}/src) - -list(APPEND MSCP_COMPILE_OPTS -iquote ${CMAKE_CURRENT_BINARY_DIR}/libssh/include) -list(APPEND MSCP_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/libssh/include) +set(MSCP_COMPILE_OPTS -iquote ${CMAKE_CURRENT_BINARY_DIR}/libssh/include) +set(MSCP_BUILD_INCLUDE_DIRS + ${mscp_SOURCE_DIR}/src + ${CMAKE_CURRENT_BINARY_DIR}/libssh/include) @@ -60,22 +59,31 @@ set(LIBMSCP_SRC src/mscp.c src/ssh.c src/path.c src/platform.c src/message.c) # shared libmscp add_library(mscp-shared SHARED ${LIBMSCP_SRC}) -target_include_directories(mscp-shared PRIVATE ${MSCP_INCLUDE_DIRS}) +target_include_directories(mscp-shared + PUBLIC $ + $ + PRIVATE ${MSCP_BUILD_INCLUDE_DIRS}) target_compile_options(mscp-shared PRIVATE ${MSCP_COMPILE_OPTS}) target_link_libraries(mscp-shared PRIVATE ssh-static) set_target_properties(mscp-shared PROPERTIES - OUTPUT_NAME mscp) + OUTPUT_NAME mscp + PUBLIC_HEADER ${mscp_SOURCE_DIR}/include/mscp.h) + +install(TARGETS mscp-shared) + # static libmscp add_library(mscp-static STATIC ${LIBMSCP_SRC}) -target_include_directories(mscp-static PRIVATE ${MSCP_INCLUDE_DIRS}) +target_include_directories(mscp-static + PRIVATE ${MSCP_BUILD_INCLUDE_DIRS} ${mscp_SOURCE_DIR}/include) target_compile_options(mscp-static PRIVATE ${MSCP_COMPILE_OPTS}) target_link_libraries(mscp-static PRIVATE ssh-static) set_target_properties(mscp-static PROPERTIES OUTPUT_NAME mscp) +install(TARGETS mscp-static) @@ -92,7 +100,8 @@ endif() # mscp executable add_executable(mscp src/main.c) -target_include_directories(mscp PRIVATE ${MSCP_INCLUDE_DIRS}) +target_include_directories(mscp + PRIVATE ${MSCP_BUILD_INCLUDE_DIRS} ${mscp_SOURCE_DIR}/include) target_link_directories(mscp PRIVATE ${MSCP_LINK_DIRS}) target_link_libraries(mscp mscp-static ${MSCP_LINK_LIBS}) if (BUILD_STATIC) diff --git a/Doxyfile b/Doxyfile index 75dba12..c5d2279 100644 --- a/Doxyfile +++ b/Doxyfile @@ -9,5 +9,5 @@ GENERATE_MAN = NO SOURCE_BROWSER = YES -INPUT = src +INPUT = src include diff --git a/include/mscp.h b/include/mscp.h new file mode 100644 index 0000000..0015548 --- /dev/null +++ b/include/mscp.h @@ -0,0 +1,264 @@ +#ifndef _MSCP_H_ +#define _MSCP_H_ + +/** + * @file mscp.h + * + * @brief mscp library header file. + * + * @mainpage + * + * libmscp is a library for multi-threaded scp. Project page is + * https://github.com/upa/mscp. + * + * All public APIs of libmscp are defined in mscp.h. Basic usage of + * libmscp is follows: + * + * 1. create mscp instance with mscp_init() + * 2. connect to remote host with mscp_connect() + * 3. add path to source files with mscp_add_src_path() + * 4. set path to destination with mscp_set_dst_path() + * 5. finish preparation with mscp_prepare() + * 6. start copy with mscp_start() + * 7. wait for copy finished with mscp_join() + * 8. cleanup mscp instance with mscp_cleanup() and mscp_free() + */ + +#include +#include + +#define MSCP_DIRECTION_L2R 1 /** Indicates local to remote copy */ +#define MSCP_DIRECTION_R2L 2 /** Indicates remote to local copy */ + +#define MSCP_MAX_COREMASK_STR 64 + +/** + * @struct mscp_opts + * @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) */ + size_t max_chunk_sz; /** maximum chunk size (default file size/nr_threads) */ + size_t buf_sz; /** buffer size, default 16k. */ + char coremask[MSCP_MAX_COREMASK_STR]; /** hex to specifiy usable cpu cores */ + + int severity; /** messaging severity. set MSCP_SERVERITY_* */ + int msg_fd; /** fd to output message. default STDOUT (0), + * and -1 disables output */ +}; + +#define MSCP_SSH_MAX_LOGIN_NAME 64 +#define MSCP_SSH_MAX_PORT_STR 32 +#define MSCP_SSH_MAX_IDENTITY_PATH PATH_MAX +#define MSCP_SSH_MAX_CIPHER_STR 32 +#define MSCP_SSH_MAX_HMAC_STR 32 +#define MSCP_SSH_MAX_COMP_STR 32 /* yes, no, zlib, zlib@openssh.com, none */ +#define MSCP_SSH_MAX_PASSWORD 128 +#define MSCP_SSH_MAX_PASSPHRASE 128 + +/** + * @struct mscp_ssh_opts + * @brief Structure configuring SSH connections + */ +struct mscp_ssh_opts { + /* ssh options */ + char login_name[MSCP_SSH_MAX_LOGIN_NAME]; /** ssh username */ + char port[MSCP_SSH_MAX_PORT_STR]; /** ssh port */ + char identity[MSCP_SSH_MAX_IDENTITY_PATH]; /** path to private key */ + char cipher[MSCP_SSH_MAX_CIPHER_STR]; /** cipher spec */ + char hmac[MSCP_SSH_MAX_HMAC_STR]; /** hmacp spec */ + char compress[MSCP_SSH_MAX_COMP_STR]; /** yes, no, zlib@openssh.com */ + + char password[MSCP_SSH_MAX_PASSWORD]; /** password auth passowrd */ + char passphrase[MSCP_SSH_MAX_PASSPHRASE]; /** passphrase for private key */ + + int debug_level; /** inclirement libssh debug output level */ + bool no_hostkey_check; /** do not check host keys */ + bool enable_nagle; /** enable Nagle's algorithm if true */ +}; + +/** + * @struct mscp_stats + * @brief Structure to get mscp statistics + */ +struct mscp_stats { + size_t total; /** total bytes to be transferred */ + size_t done; /** total bytes transferred */ + bool finished; /** true when all copy threads finished */ +}; + + +/** Structure representing mscp instance */ +struct mscp; + +/** + * @brief Creates a new mscp instance. + * + * @param remote_host remote host for file transer. + * @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_opts *o, struct mscp_ssh_opts *s); + +/** + * @brief Connect the first SSH connection. mscp_connect connects to + * remote host and initialize a SFTP session over the + * connection. mscp_prepare() and mscp_start() require mscp_connect() + * beforehand. + * + * @param m mscp instance. + * + * @return 0 on success, < 0 if an error occured. + * mscp_get_error() can be used to retrieve error message. + */ +int mscp_connect(struct mscp *m); + +/* add a source file path to be copied */ + +/** + * @brief Add a source file path to be copied. The path indicates + * either a file or directory. The path can be `user@host:path` + * notation. In this case, `dst_path` for mscp_set_dst_path() must + * not contain remote host notation. + * + * @param m mscp instance. + * @param src_path source file path to be copied. + * + * @return 0 on success, < 0 if an error occured. + * mscp_get_error() can be used to retrieve error message. + */ +int mscp_add_src_path(struct mscp *m, const char *src_path); + +/** + * @brief Set the destination file path. The path indicates either a + * file, directory, or nonexistent path. The path can be + * `user@host:path` notation. In this case, all source paths appended + * by mscp_set_src_path() must not contain remote host notation. + * + * @param m mscp instance. + * @param dst_path destination path to which source files copied. + * + * @return 0 on success, < 0 if an error occured. + * mscp_get_error() can be used to retrieve error message. + */ +int mscp_set_dst_path(struct mscp *m, const char *dst_path); + +/* check source files, resolve destination file paths for all source + * files, and prepare chunks for all files. */ + +/** + * @brief Prepare for file transfer. This function checks all source + * files (recursively), resolve paths on the destination side, and + * calculate file chunks. + * + * @param m mscp instance. + * + * @return 0 on success, < 0 if an error occured. + * mscp_get_error() can be used to retrieve error message. + */ +int mscp_prepare(struct mscp *m); + +/** + * @brief Start to copy files. mscp_start() returns immediately. You + * can get statistics via mscp_get_stats() or messages via pipe set by + * mscp_opts.msg_fd or mscp_set_msg_fd(). mscp_stop() cancels mscp + * copy threads, and mscp_join() joins the threads. + * + * @param m mscp instance. + * + * @return 0 on success, < 0 if an error occured. + * mscp_get_error() can be used to retrieve error message. + * + * @see mscp_join() + */ +int mscp_start(struct mscp *m); + + +/** + * @brief Stop coping files. + * + * @param m mscp instance. + */ +void mscp_stop(struct mscp *m); + + +/** + * @brief Join copy threads. This function is blocking until all copy + * have done. + * + * @param m mscp instance. + * + * @return 0 on success, < 0 if an error occured. + * mscp_get_error() can be used to retrieve error message. + */ +int mscp_join(struct mscp *m); + +/** + * @brief Get statistics of copy. + * + * @param m mscp instance. + * @param s[out] statistics. + */ +void mscp_get_stats(struct mscp *m, struct mscp_stats *s); + +/** + * @brief Cleanup the mscp instance. Before calling mscp_cleanup(), must + * call mscp_join(). After mscp_cleanup() called, the mscp instance + * can restart from mscp_connect(). + * + * @param m mscp instance. + */ +void mscp_cleanup(struct mscp *m); + +/** + * @brief Release the mscp instance. + * + * @param m mscp instance. + */ +void mscp_free(struct mscp *m); + + +/* messaging with mscp */ + +/** + * @enum mscp_serverity + * @brief Filter messages from libmscp with severity level. + */ +enum { + MSCP_SEVERITY_NONE = -1, + MSCP_SEVERITY_ERR = 0, + MSCP_SEVERITY_WARN = 1, + MSCP_SEVERITY_NOTICE = 2, + MSCP_SEVERITY_INFO = 3, + MSCP_SEVERITY_DEBUG = 4, +}; + + +/** + * @brief Set a file descriptor for receiving messages from mscp. + * This function has the same effect with setting mscp_opts->msg_fd. + * + * @param m mscp instance. + * @param fd fd to which libmscp writes messages. + */ +void mscp_set_msg_fd(struct mscp *m, int fd); + + +/** + * @brief Get the recent error message from libmscp. Note that this + * function is not thread-safe. + * + * @return pointer to the message. + */ +const char *mscp_get_error(); + + + +#endif /* _MSCP_H_ */ diff --git a/src/mscp.h b/src/mscp.h deleted file mode 100644 index 0015548..0000000 --- a/src/mscp.h +++ /dev/null @@ -1,264 +0,0 @@ -#ifndef _MSCP_H_ -#define _MSCP_H_ - -/** - * @file mscp.h - * - * @brief mscp library header file. - * - * @mainpage - * - * libmscp is a library for multi-threaded scp. Project page is - * https://github.com/upa/mscp. - * - * All public APIs of libmscp are defined in mscp.h. Basic usage of - * libmscp is follows: - * - * 1. create mscp instance with mscp_init() - * 2. connect to remote host with mscp_connect() - * 3. add path to source files with mscp_add_src_path() - * 4. set path to destination with mscp_set_dst_path() - * 5. finish preparation with mscp_prepare() - * 6. start copy with mscp_start() - * 7. wait for copy finished with mscp_join() - * 8. cleanup mscp instance with mscp_cleanup() and mscp_free() - */ - -#include -#include - -#define MSCP_DIRECTION_L2R 1 /** Indicates local to remote copy */ -#define MSCP_DIRECTION_R2L 2 /** Indicates remote to local copy */ - -#define MSCP_MAX_COREMASK_STR 64 - -/** - * @struct mscp_opts - * @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) */ - size_t max_chunk_sz; /** maximum chunk size (default file size/nr_threads) */ - size_t buf_sz; /** buffer size, default 16k. */ - char coremask[MSCP_MAX_COREMASK_STR]; /** hex to specifiy usable cpu cores */ - - int severity; /** messaging severity. set MSCP_SERVERITY_* */ - int msg_fd; /** fd to output message. default STDOUT (0), - * and -1 disables output */ -}; - -#define MSCP_SSH_MAX_LOGIN_NAME 64 -#define MSCP_SSH_MAX_PORT_STR 32 -#define MSCP_SSH_MAX_IDENTITY_PATH PATH_MAX -#define MSCP_SSH_MAX_CIPHER_STR 32 -#define MSCP_SSH_MAX_HMAC_STR 32 -#define MSCP_SSH_MAX_COMP_STR 32 /* yes, no, zlib, zlib@openssh.com, none */ -#define MSCP_SSH_MAX_PASSWORD 128 -#define MSCP_SSH_MAX_PASSPHRASE 128 - -/** - * @struct mscp_ssh_opts - * @brief Structure configuring SSH connections - */ -struct mscp_ssh_opts { - /* ssh options */ - char login_name[MSCP_SSH_MAX_LOGIN_NAME]; /** ssh username */ - char port[MSCP_SSH_MAX_PORT_STR]; /** ssh port */ - char identity[MSCP_SSH_MAX_IDENTITY_PATH]; /** path to private key */ - char cipher[MSCP_SSH_MAX_CIPHER_STR]; /** cipher spec */ - char hmac[MSCP_SSH_MAX_HMAC_STR]; /** hmacp spec */ - char compress[MSCP_SSH_MAX_COMP_STR]; /** yes, no, zlib@openssh.com */ - - char password[MSCP_SSH_MAX_PASSWORD]; /** password auth passowrd */ - char passphrase[MSCP_SSH_MAX_PASSPHRASE]; /** passphrase for private key */ - - int debug_level; /** inclirement libssh debug output level */ - bool no_hostkey_check; /** do not check host keys */ - bool enable_nagle; /** enable Nagle's algorithm if true */ -}; - -/** - * @struct mscp_stats - * @brief Structure to get mscp statistics - */ -struct mscp_stats { - size_t total; /** total bytes to be transferred */ - size_t done; /** total bytes transferred */ - bool finished; /** true when all copy threads finished */ -}; - - -/** Structure representing mscp instance */ -struct mscp; - -/** - * @brief Creates a new mscp instance. - * - * @param remote_host remote host for file transer. - * @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_opts *o, struct mscp_ssh_opts *s); - -/** - * @brief Connect the first SSH connection. mscp_connect connects to - * remote host and initialize a SFTP session over the - * connection. mscp_prepare() and mscp_start() require mscp_connect() - * beforehand. - * - * @param m mscp instance. - * - * @return 0 on success, < 0 if an error occured. - * mscp_get_error() can be used to retrieve error message. - */ -int mscp_connect(struct mscp *m); - -/* add a source file path to be copied */ - -/** - * @brief Add a source file path to be copied. The path indicates - * either a file or directory. The path can be `user@host:path` - * notation. In this case, `dst_path` for mscp_set_dst_path() must - * not contain remote host notation. - * - * @param m mscp instance. - * @param src_path source file path to be copied. - * - * @return 0 on success, < 0 if an error occured. - * mscp_get_error() can be used to retrieve error message. - */ -int mscp_add_src_path(struct mscp *m, const char *src_path); - -/** - * @brief Set the destination file path. The path indicates either a - * file, directory, or nonexistent path. The path can be - * `user@host:path` notation. In this case, all source paths appended - * by mscp_set_src_path() must not contain remote host notation. - * - * @param m mscp instance. - * @param dst_path destination path to which source files copied. - * - * @return 0 on success, < 0 if an error occured. - * mscp_get_error() can be used to retrieve error message. - */ -int mscp_set_dst_path(struct mscp *m, const char *dst_path); - -/* check source files, resolve destination file paths for all source - * files, and prepare chunks for all files. */ - -/** - * @brief Prepare for file transfer. This function checks all source - * files (recursively), resolve paths on the destination side, and - * calculate file chunks. - * - * @param m mscp instance. - * - * @return 0 on success, < 0 if an error occured. - * mscp_get_error() can be used to retrieve error message. - */ -int mscp_prepare(struct mscp *m); - -/** - * @brief Start to copy files. mscp_start() returns immediately. You - * can get statistics via mscp_get_stats() or messages via pipe set by - * mscp_opts.msg_fd or mscp_set_msg_fd(). mscp_stop() cancels mscp - * copy threads, and mscp_join() joins the threads. - * - * @param m mscp instance. - * - * @return 0 on success, < 0 if an error occured. - * mscp_get_error() can be used to retrieve error message. - * - * @see mscp_join() - */ -int mscp_start(struct mscp *m); - - -/** - * @brief Stop coping files. - * - * @param m mscp instance. - */ -void mscp_stop(struct mscp *m); - - -/** - * @brief Join copy threads. This function is blocking until all copy - * have done. - * - * @param m mscp instance. - * - * @return 0 on success, < 0 if an error occured. - * mscp_get_error() can be used to retrieve error message. - */ -int mscp_join(struct mscp *m); - -/** - * @brief Get statistics of copy. - * - * @param m mscp instance. - * @param s[out] statistics. - */ -void mscp_get_stats(struct mscp *m, struct mscp_stats *s); - -/** - * @brief Cleanup the mscp instance. Before calling mscp_cleanup(), must - * call mscp_join(). After mscp_cleanup() called, the mscp instance - * can restart from mscp_connect(). - * - * @param m mscp instance. - */ -void mscp_cleanup(struct mscp *m); - -/** - * @brief Release the mscp instance. - * - * @param m mscp instance. - */ -void mscp_free(struct mscp *m); - - -/* messaging with mscp */ - -/** - * @enum mscp_serverity - * @brief Filter messages from libmscp with severity level. - */ -enum { - MSCP_SEVERITY_NONE = -1, - MSCP_SEVERITY_ERR = 0, - MSCP_SEVERITY_WARN = 1, - MSCP_SEVERITY_NOTICE = 2, - MSCP_SEVERITY_INFO = 3, - MSCP_SEVERITY_DEBUG = 4, -}; - - -/** - * @brief Set a file descriptor for receiving messages from mscp. - * This function has the same effect with setting mscp_opts->msg_fd. - * - * @param m mscp instance. - * @param fd fd to which libmscp writes messages. - */ -void mscp_set_msg_fd(struct mscp *m, int fd); - - -/** - * @brief Get the recent error message from libmscp. Note that this - * function is not thread-safe. - * - * @return pointer to the message. - */ -const char *mscp_get_error(); - - - -#endif /* _MSCP_H_ */ -- cgit v1.2.3