From ab39159607581d5949e5a01107c9ddc701fc1066 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 7 Apr 2008 17:16:41 +0200 Subject: [PATCH] Add virtual IO plugin system. This is the initial commit of the vio plugin system. The idea is based on gnomevfs. --- CMakeLists.txt | 1 + modules/CMakeLists.txt | 34 +++++ modules/csync_smb.c | 230 +++++++++++++++++++++++++++++ src/CMakeLists.txt | 3 + src/vio/csync_vio.c | 27 ++++ src/vio/csync_vio.h | 55 +++++++ src/vio/csync_vio_file_stat.h | 97 ++++++++++++ src/vio/csync_vio_handle.c | 53 +++++++ src/vio/csync_vio_handle.h | 29 ++++ src/vio/csync_vio_handle_private.h | 35 +++++ src/vio/csync_vio_method.h | 80 ++++++++++ src/vio/csync_vio_module.h | 31 ++++ tests/CMakeLists.txt | 3 + tests/vio_tests/check_vio_handle.c | 71 +++++++++ 14 files changed, 749 insertions(+) create mode 100644 modules/CMakeLists.txt create mode 100644 modules/csync_smb.c create mode 100644 src/vio/csync_vio.c create mode 100644 src/vio/csync_vio.h create mode 100644 src/vio/csync_vio_file_stat.h create mode 100644 src/vio/csync_vio_handle.c create mode 100644 src/vio/csync_vio_handle.h create mode 100644 src/vio/csync_vio_handle_private.h create mode 100644 src/vio/csync_vio_method.h create mode 100644 src/vio/csync_vio_module.h create mode 100644 tests/vio_tests/check_vio_handle.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 605b89267..173405788 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ macro_copy_file(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_CURRENT_BI add_subdirectory(iniparser/src) add_subdirectory(src) +add_subdirectory(modules) add_subdirectory(client) add_subdirectory(config) add_subdirectory(doc) diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt new file mode 100644 index 000000000..c7d5d43c8 --- /dev/null +++ b/modules/CMakeLists.txt @@ -0,0 +1,34 @@ +project(modules) + +find_package(Libsmbclient REQUIRED) + +set(MODULES_PUBLIC_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR} + CACHE INTERNAL "smb_plugin include directories" +) + +set(MODULES_PRIVATE_INCLUDE_DIRS + ${CSTDLIB_PUBLIC_INCLUDE_DIRS} + ${CSYNC_PUBLIC_INCLUDE_DIRS} + ${LIBSMBCLIENT_INCLUDE_DIRS} +) + +set(SMB_PLUGIN + csync_smb +) + +include_directories( + ${MODULES_PUBLIC_INCLUDE_DIRS} + ${MODULES_PRIVATE_INCLUDE_DIRS} +) + +macro_add_plugin(${SMB_PLUGIN} csync_smb.c) +target_link_libraries(${SMB_PLUGIN} ${LIBSMBCLIENT_LIBRARIES} ${CSTDLIB_LIBRARY}) + +INSTALL( + TARGETS + ${SMB_PLUGIN} + DESTINATION + ${PLUGIN_INSTALL_DIR} +) + diff --git a/modules/csync_smb.c b/modules/csync_smb.c new file mode 100644 index 000000000..b0d71d461 --- /dev/null +++ b/modules/csync_smb.c @@ -0,0 +1,230 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software = NULL, you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation = NULL, either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY = NULL, without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program = NULL, if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ts=2 sw=2 et cindent + */ + +#include +#include + +#include "c_lib.h" +#include "vio/csync_vio_module.h" + +SMBCCTX *smb_context; + +/* + * Authentication callback for libsmbclient + */ +static void get_auth_data_fn(const char *pServer, + const char *pShare, + char *pWorkgroup, int maxLenWorkgroup, + char *pUsername, int maxLenUsername, + char *pPassword, int maxLenPassword) { + /* FIXME: need to handle non kerberos authentication for libsmbclient + * here, currently it is only a placeholder so that libsmbclient can be + * initialized */ + return; +} + +typedef struct smb_fhandle_s { + int fd; +} smb_fhandle_t; + + +/* + * file functions + */ + +static csync_vio_method_handle_t *_open(const char *durl, int flags, mode_t mode) { + smb_fhandle_t *handle = NULL; + int fd = -1; + + if ((fd = smbc_open(durl, flags, mode)) < 0) { + return NULL; + } + + handle = c_malloc(sizeof(smb_fhandle_t)); + if (handle == NULL) { + return NULL; + } + + handle->fd = fd; + return (csync_vio_method_handle_t *) handle; +} + +static csync_vio_method_handle_t *_creat(const char *durl, mode_t mode) { + smb_fhandle_t *handle = NULL; + int fd = -1; + + if ((fd = smbc_creat(durl, mode)) < 0) { + return NULL; + } + + handle = c_malloc(sizeof(smb_fhandle_t)); + if (handle == NULL) { + return NULL; + } + + handle->fd = fd; + return (csync_vio_method_handle_t *) handle; +} + +static int _close(csync_vio_method_handle_t *fhandle) { + int rc = -1; + smb_fhandle_t *handle = NULL; + + handle = (smb_fhandle_t *) fhandle; + + rc = smbc_close(handle->fd); + + SAFE_FREE(handle); + + return rc; +} + +static ssize_t _read(csync_vio_method_handle_t *fhandle, void *buf, size_t count) { + smb_fhandle_t *handle = NULL; + + handle = (smb_fhandle_t *) fhandle; + + return smbc_read(handle->fd, buf, count); +} + +static ssize_t _write(csync_vio_method_handle_t *fhandle, const void *buf, size_t count) { + smb_fhandle_t *handle = NULL; + + handle = (smb_fhandle_t *) fhandle; + + return smbc_write(handle->fd, (char *) buf, count); +} + +static off_t _lseek(csync_vio_method_handle_t *fhandle, off_t offset, int whence) { + smb_fhandle_t *handle = NULL; + + handle = (smb_fhandle_t *) fhandle; + + return smbc_lseek(handle->fd, offset, whence); +} + +/* + * directory functions + */ + +typedef struct smb_dhandle_s { + int dh; + char *path; +} smb_dhandle_t; + +static csync_vio_method_handle_t *_opendir(const char *name) { + smb_dhandle_t *handle = NULL; + + handle = c_malloc(sizeof(smb_dhandle_t)); + if (handle == NULL) { + return NULL; + } + + handle->dh = smbc_opendir(name); + handle->path = c_strdup(name); + + return (csync_vio_method_handle_t *) handle; +} + +static int _closedir(csync_vio_method_t *dhandle) { + smb_dhandle_t *handle = NULL; + int rc = -1; + + handle = (smb_dhandle_t *) dhandle; + + rc = smbc_closedir(handle->dh); + + SAFE_FREE(handle->path); + SAFE_FREE(handle); + + return rc; +} + +static csync_vio_file_stat_t *_readdir(csync_vio_method_handle_t *dhandle) { + smbc_dirent *dirent = NULL; + smb_dhandle_t *handle = NULL; + + handle = (smb_dhandle_t *) dhandle; + + dirent = smbc_opendir(handle->dh); + + return NULL; +} + +csync_vio_method_t method = { + .method_table_size = sizeof(csync_vio_method_t), + .open = _open, + .creat = _creat, + .close = _close, + .read = _read, + .write = _write, + .lseek = _lseek, + .opendir = _opendir, + .closedir = _closedir, + .readdir = NULL, + .mkdir = NULL, + .rmdir = NULL, + .stat = NULL, + .rename = NULL, + .unlink = NULL, + .chmod = NULL, + .chwon = NULL, + .utimes = NULL +}; + +csync_vio_method_t *vio_module_init(const char *method_name, const char *args) { + smb_context = smbc_new_context(); + + if (smb_context == NULL) { + fprintf(stderr, "csync_smb: Failed to create new smbc context\n"); + return NULL; + } + + /* set debug level and authentication function callback */ + smb_context->debug = 0; + smb_context->callbacks.auth_fn = get_auth_data_fn; + + if (smbc_init_context(smb_context) == NULL) { + fprintf(stderr, "CSYNC_SMB: Failed to initialize the smbc context"); + smbc_free_context(smb_context, 0); + return NULL; + } + +#if defined(SMB_CTX_FLAG_USE_KERBEROS) && defined(SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) + smb_context->flags |= (SMB_CTX_FLAG_USE_KERBEROS | SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS); +#endif + + smbc_set_context(smb_context); + + return &method; +} + +void vio_module_shutdown(csync_vio_method_t *method) { + if (smb_context != NULL) { + /* + * If we have a context, all connections and files will be closed even + * if they are busy. + */ + smbc_free_context(smb_context, 1); + } +} + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 485d23066..c143237e1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,9 @@ set(csync_SRCS csync_journal.c csync_lock.c csync_util.c + + vio/csync_vio.c + vio/csync_vio_handle.c ) set(csync_HDRS diff --git a/src/vio/csync_vio.c b/src/vio/csync_vio.c new file mode 100644 index 000000000..82d3d2796 --- /dev/null +++ b/src/vio/csync_vio.c @@ -0,0 +1,27 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ts=2 sw=2 et cindent + */ + +#include "vio/csync_vio.h" + +int csync_vfs_init(void) { + return 0; +} diff --git a/src/vio/csync_vio.h b/src/vio/csync_vio.h new file mode 100644 index 000000000..18f14464a --- /dev/null +++ b/src/vio/csync_vio.h @@ -0,0 +1,55 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ft=c.doxygen ts=2 sw=2 et cindent + */ + +#ifndef _CSYNC_VIO_H +#define _CSYNC_VIO_H + +#include "vio/csync_vio_handle.h" +#include "vio/csync_vio_file_stat.h" + +int csync_vfs_init(void); +int csync_vfs_shutdown(void); + +csync_vio_handle_t *csync_vfs_open(const char *uri, int flags, mode_t mode); +csync_vio_handle_t *csync_vfs_creat(const char *uri, mode_t mode); +int csync_vio_close(csync_vio_handle_t *handle); +ssize_t csync_vio_read(csync_vio_handle_t *fhandle, void *buf, size_t count); +ssize_t csync_vio_write(csync_vio_handle_t *fhandle, const void *buf, size_t count); +off_t csync_vio_seek(csync_vio_handle_t *fhandle, off_t offset, int whence); + +csync_vio_handle_t *csync_vio_opendir(const char *name); +int csync_vio_closedir(csync_vio_handle_t *dhandle); +csync_vio_file_stat_t *csync_vio_readdir(csync_vio_handle_t *dhandle); + +int csync_vio_mkdir(const char *uri, mode_t mode); +int csync_vio_rmdir(const char *uri); + +int csync_vio_stat(const char *uri, csync_vio_file_stat_t *buf); +int csync_vio_rename(const char *olduri, const char *newuri); +int csync_vio_unlink(const char *uri); + +int csync_vio_chmod(const char *uri, mode_t mode); +int csync_vio_chown(const char *uri, uid_t owner, gid_t group); + +int csync_vio_utimes(const char *uri, const struct timeval times[2]); + +#endif /* _CSYNC_VIO_H */ diff --git a/src/vio/csync_vio_file_stat.h b/src/vio/csync_vio_file_stat.h new file mode 100644 index 000000000..9a2dcf208 --- /dev/null +++ b/src/vio/csync_vio_file_stat.h @@ -0,0 +1,97 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ft=c.doxygen ts=2 sw=2 et cindent + */ + +#ifndef _CSYNC_VIO_FILE_INFO_H +#define _CSYNC_VIO_FILE_INFO_H + +#include +#include +#include + +typedef struct csync_vio_file_stat_s csync_vio_file_stat_t; + +enum csync_vio_file_flags_e { + CSYNC_VIO_FILE_FLAGS_NONE = 0, + CSYNC_VIO_FILE_FLAGS_SYMLINK = 1 << 0, + CSYNC_VIO_FILE_FLAGS_LOCAL = 1 << 1 +}; + +enum csync_vio_file_type_e { + CSYNC_VIO_FILE_TYPE_UNKNOWN, + CSYNC_VIO_FILE_TYPE_REGULAR, + CSYNC_VIO_FILE_TYPE_DIRECTORY, + CSYNC_VIO_FILE_TYPE_FIFO, + CSYNC_VIO_FILE_TYPE_SOCKET, + CSYNC_VIO_FILE_TYPE_CHARACTER_DEVICE, + CSYNC_VIO_FILE_TYPE_BLOCK_DEVICE, + CSYNC_VIO_FILE_TYPE_SYMBOLIC_LINK +}; + +enum csync_vio_file_stat_fields_e { + CSYNC_VIO_FILE_INFO_FIELDS_NONE = 0, + CSYNC_VIO_FILE_INFO_FIELDS_TYPE = 1 << 0, + CSYNC_VIO_FILE_INFO_FIELDS_PERMISSIONS = 1 << 1, + CSYNC_VIO_FILE_INFO_FIELDS_FLAGS = 1 << 2, + CSYNC_VIO_FILE_INFO_FIELDS_DEVICE = 1 << 3, + CSYNC_VIO_FILE_INFO_FIELDS_INODE = 1 << 4, + CSYNC_VIO_FILE_INFO_FIELDS_LINK_COUNT = 1 << 5, + CSYNC_VIO_FILE_INFO_FIELDS_SIZE = 1 << 6, + CSYNC_VIO_FILE_INFO_FIELDS_BLOCK_COUNT = 1 << 7, + CSYNC_VIO_FILE_INFO_FIELDS_IO_BLOCK_SIZE = 1 << 8, + CSYNC_VIO_FILE_INFO_FIELDS_ATIME = 1 << 9, + CSYNC_VIO_FILE_INFO_FIELDS_MTIME = 1 << 10, + CSYNC_VIO_FILE_INFO_FIELDS_CTIME = 1 << 11, + CSYNC_VIO_FILE_INFO_FIELDS_SYMLINK_NAME = 1 << 12, + CSYNC_VIO_FILE_INFO_FIELDS_ACL = 1 << 13, +}; + + +struct csync_vio_file_stat_s { + char *name; + + enum csync_vio_file_stat_fields_e fields; + enum csync_vio_file_type_e type; + mode_t mode; + + enum csync_vio_file_flags_e flags; + + dev_t device; + ino_t inode; + nlink_t link_count; + + off_t size; + off_t blksize; + unsigned long blkcount; + + time_t atime; + time_t mtime; + time_t ctime; + + char *symlink_name; + void *acl; + + void *reserved1; + void *reserved2; + void *reserved3; +}; + +#endif /* _CSYNC_VIO_METHOD_H */ diff --git a/src/vio/csync_vio_handle.c b/src/vio/csync_vio_handle.c new file mode 100644 index 000000000..31d85c99a --- /dev/null +++ b/src/vio/csync_vio_handle.c @@ -0,0 +1,53 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ts=2 sw=2 et cindent + */ + +#include "c_lib.h" + +#include "vio/csync_vio_handle.h" +#include "vio/csync_vio_handle_private.h" + +csync_vio_handle_t *csync_vio_handle_new(char *uri, csync_vio_method_handle_t *method_handle) { + csync_vio_handle_t *new = NULL; + + if (uri == NULL || method_handle == NULL) { + return NULL; + } + + new = c_malloc(sizeof(csync_vio_handle_t)); + if (new == NULL) { + return NULL; + } + + new->uri = uri; + new->method_handle = method_handle; + + return new; +} + +void csync_vio_handle_destroy(csync_vio_handle_t *handle) { + if (handle == NULL || handle->uri == NULL) { + return; + } + + SAFE_FREE(handle->uri); + SAFE_FREE(handle); +} diff --git a/src/vio/csync_vio_handle.h b/src/vio/csync_vio_handle.h new file mode 100644 index 000000000..9dff21e64 --- /dev/null +++ b/src/vio/csync_vio_handle.h @@ -0,0 +1,29 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ft=c.doxygen ts=2 sw=2 et cindent + */ + +#ifndef _CSYNC_VIO_HANDLE_H +#define _CSYNC_VIO_HANDLE_H + +typedef void *csync_vio_method_handle_t; +typedef struct csync_vio_handle_s csync_vio_handle_t; + +#endif /* _CSYNC_VIO_HANDLE_H */ diff --git a/src/vio/csync_vio_handle_private.h b/src/vio/csync_vio_handle_private.h new file mode 100644 index 000000000..3415c6d98 --- /dev/null +++ b/src/vio/csync_vio_handle_private.h @@ -0,0 +1,35 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ft=c.doxygen ts=2 sw=2 et cindent + */ + +#ifndef _CSYNC_VIO_HANDLE_PRIVATE_H +#define _CSYNC_VIO_HANDLE_PRIVATE_H + +struct csync_vio_handle_s { + char *uri; + + csync_vio_method_handle_t *method_handle; +}; + +csync_vio_handle_t *csync_vio_handle_new(char *uri, csync_vio_method_handle_t *method_handle); +void csync_vio_handle_destroy(csync_vio_handle_t *handle); + +#endif /* _CSYNC_VIO_HANDLE_PRIVATE_H */ diff --git a/src/vio/csync_vio_method.h b/src/vio/csync_vio_method.h new file mode 100644 index 000000000..60f689e33 --- /dev/null +++ b/src/vio/csync_vio_method.h @@ -0,0 +1,80 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ft=c.doxygen ts=2 sw=2 et cindent + */ + +#ifndef _CSYNC_VIO_METHOD_H +#define _CSYNC_VIO_METHOD_H + +#include + +#include "vio/csync_vio_file_stat.h" +#include "vio/csync_vio_handle.h" + +typedef struct csync_vio_method_s csync_vio_method_t; + +typedef csync_vio_method_t *(*csync_vio_method_init_fn)(const char *method_name, const char *config_args); +typedef void (*csync_vio_method_finish_fn)(csync_vio_method_t *method); + +typedef csync_vio_method_handle_t *(*csync_method_open_fn)(const char *durl, int flags, mode_t mode); +typedef csync_vio_method_handle_t *(*csync_method_creat_fn)(const char *durl, mode_t mode); +typedef int (*csync_method_close_fn)(csync_vio_method_handle_t *fhandle); +typedef ssize_t (*csync_method_read_fn)(csync_vio_method_handle_t *fhandle, void *buf, size_t count); +typedef ssize_t (*csync_method_write_fn)(csync_vio_method_handle_t *fhandle, const void *buf, size_t count); +typedef off_t (*csync_method_lseek_fn)(csync_vio_method_handle_t *fhandle, off_t offset, int whence); + +typedef csync_vio_method_handle_t *(*csync_method_opendir_fn)(const char *name); +typedef int (*csync_method_closedir_fn)(csync_vio_method_t *dhandle); +typedef csync_vio_file_stat_t *(*csync_method_readdir_fn)(csync_vio_method_handle_t *dhandle); + +typedef int (*csync_method_mkdir_fn)(const char *uri, mode_t mode); +typedef int (*csync_method_rmdir_fn)(const char *uri); + +typedef int (*csync_method_stat_fn)(const char *uri, csync_vio_file_stat_t *buf); +typedef int (*csync_method_rename_fn)(const char *olduri, const char *newuri); +typedef int (*csync_method_unlink_fn)(const char *uri); + +typedef int (*csync_method_chmod_fn)(const char *uri, mode_t mode); +typedef int (*csync_method_chown_fn)(const char *uri, uid_t owner, gid_t group); + +typedef int (*csync_method_utimes_fn)(const char *uri, const struct timeval times[2]); + +struct csync_vio_method_s { + size_t method_table_size; /* Used for versioning */ + csync_method_open_fn open; + csync_method_creat_fn creat; + csync_method_close_fn close; + csync_method_read_fn read; + csync_method_write_fn write; + csync_method_lseek_fn lseek; + csync_method_opendir_fn opendir; + csync_method_closedir_fn closedir; + csync_method_readdir_fn readdir; + csync_method_mkdir_fn mkdir; + csync_method_rmdir_fn rmdir; + csync_method_stat_fn stat; + csync_method_rename_fn rename; + csync_method_unlink_fn unlink; + csync_method_chmod_fn chmod; + csync_method_chown_fn chwon; + csync_method_utimes_fn utimes; +}; + +#endif /* _CSYNC_VIO_H */ diff --git a/src/vio/csync_vio_module.h b/src/vio/csync_vio_module.h new file mode 100644 index 000000000..ca3f4d8e9 --- /dev/null +++ b/src/vio/csync_vio_module.h @@ -0,0 +1,31 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2008 by Andreas Schneider + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * vim: ft=c.doxygen ts=2 sw=2 et cindent + */ + +#ifndef _CSYNC_VIO_MODULE_H +#define _CSYNC_VIO_MODULE_H + +#include "vio/csync_vio_method.h" + +extern csync_vio_method_t *vio_module_init (const char *method_name, const char *args); +extern void vio_module_shutdown(csync_vio_method_t *method); + +#endif /* _CSYNC_VIO_MODULE_H */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 132224855..30fb65a57 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -36,3 +36,6 @@ macro_add_check_test(check_csync_config csync_tests/check_csync_config.c ${TEST_ macro_add_check_test(check_csync_exclude csync_tests/check_csync_exclude.c ${TEST_TARGET_LIBRARIES}) macro_add_check_test(check_csync_journal csync_tests/check_csync_journal.c ${TEST_TARGET_LIBRARIES}) +# vio +macro_add_check_test(check_vio_handle vio_tests/check_vio_handle.c ${TEST_TARGET_LIBRARIES}) + diff --git a/tests/vio_tests/check_vio_handle.c b/tests/vio_tests/check_vio_handle.c new file mode 100644 index 000000000..6c73ca85f --- /dev/null +++ b/tests/vio_tests/check_vio_handle.c @@ -0,0 +1,71 @@ +#define _GNU_SOURCE /* asprintf */ +#include + +#include "support.h" + +#include "vio/csync_vio_handle.h" +#include "vio/csync_vio_handle_private.h" + +START_TEST (check_csync_vio_handle_new) +{ + int *number = NULL; + csync_vio_handle_t *handle = NULL; + + number = c_malloc(sizeof(int)); + *number = 42; + + handle = csync_vio_handle_new(c_strdup("/tmp"), (csync_vio_method_handle_t *) number); + fail_if(handle == NULL, NULL); + fail_unless(strcmp(handle->uri, "/tmp") == 0, NULL); + + SAFE_FREE(handle->method_handle); + + csync_vio_handle_destroy(handle); +} +END_TEST + +START_TEST (check_csync_vio_handle_new_null) +{ + int *number = NULL; + csync_vio_handle_t *handle = NULL; + + number = c_malloc(sizeof(int)); + *number = 42; + + handle = csync_vio_handle_new(NULL, (csync_vio_method_handle_t *) number); + fail_unless(handle == NULL, NULL); + + handle = csync_vio_handle_new("/tmp", NULL); + fail_unless(handle == NULL, NULL); + + SAFE_FREE(number); +} +END_TEST + + +static Suite *csync_vio_suite(void) { + Suite *s = suite_create("csync_vio_handle"); + + create_case(s, "check_csync_vio_handle_new", check_csync_vio_handle_new); + create_case(s, "check_csync_vio_handle_new_null", check_csync_vio_handle_new_null); + + return s; +} + +int main(void) { + int nf; + + Suite *s = csync_vio_suite(); + + SRunner *sr; + sr = srunner_create(s); +#if 0 + srunner_set_fork_status(sr, CK_NOFORK); +#endif + srunner_run_all(sr, CK_VERBOSE); + nf = srunner_ntests_failed(sr); + srunner_free(sr); + + return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} +