From 110f4eae607d41b4d6c203674845231080a396dc Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 1 Jan 2017 12:45:36 +0300 Subject: [PATCH] sftpfs: (sftpfs_fix_filename): refactoring: return length of result. Signed-off-by: Andrew Borodin --- src/vfs/sftpfs/dir.c | 20 +++++----- src/vfs/sftpfs/file.c | 7 ++-- src/vfs/sftpfs/internal.c | 77 ++++++++++++++++++++++----------------- src/vfs/sftpfs/internal.h | 2 +- 4 files changed, 58 insertions(+), 48 deletions(-) diff --git a/src/vfs/sftpfs/dir.c b/src/vfs/sftpfs/dir.c index 2b29af328..63fed6139 100644 --- a/src/vfs/sftpfs/dir.c +++ b/src/vfs/sftpfs/dir.c @@ -83,13 +83,14 @@ sftpfs_opendir (const vfs_path_t * vpath, GError ** mcerror) while (TRUE) { const char *fixfname; + unsigned int fixfname_len = 0; int libssh_errno; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); handle = - libssh2_sftp_open_ex (super_data->sftp_session, fixfname, sftpfs_filename_buffer->len, - 0, 0, LIBSSH2_SFTP_OPENDIR); + libssh2_sftp_open_ex (super_data->sftp_session, fixfname, fixfname_len, 0, 0, + LIBSSH2_SFTP_OPENDIR); if (handle != NULL) break; @@ -212,12 +213,11 @@ sftpfs_mkdir (const vfs_path_t * vpath, mode_t mode, GError ** mcerror) do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); - res = - libssh2_sftp_mkdir_ex (super_data->sftp_session, - fixfname, sftpfs_filename_buffer->len, mode); + res = libssh2_sftp_mkdir_ex (super_data->sftp_session, fixfname, fixfname_len, mode); if (res >= 0) break; @@ -269,11 +269,11 @@ sftpfs_rmdir (const vfs_path_t * vpath, GError ** mcerror) do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); - res = - libssh2_sftp_rmdir_ex (super_data->sftp_session, fixfname, sftpfs_filename_buffer->len); + res = libssh2_sftp_rmdir_ex (super_data->sftp_session, fixfname, fixfname_len); if (res >= 0) break; diff --git a/src/vfs/sftpfs/file.c b/src/vfs/sftpfs/file.c index 39f6d5849..bfdd4f369 100644 --- a/src/vfs/sftpfs/file.c +++ b/src/vfs/sftpfs/file.c @@ -127,13 +127,14 @@ sftpfs_open_file (vfs_file_handler_t * file_handler, int flags, mode_t mode, GEr while (TRUE) { const char *fixfname; + unsigned int fixfname_len = 0; int libssh_errno; - fixfname = sftpfs_fix_filename (name); + fixfname = sftpfs_fix_filename (name, &fixfname_len); file_handler_data->handle = - libssh2_sftp_open_ex (super_data->sftp_session, fixfname, sftpfs_filename_buffer->len, - sftp_open_flags, sftp_open_mode, LIBSSH2_SFTP_OPENFILE); + libssh2_sftp_open_ex (super_data->sftp_session, fixfname, fixfname_len, sftp_open_flags, + sftp_open_mode, LIBSSH2_SFTP_OPENFILE); if (file_handler_data->handle != NULL) break; diff --git a/src/vfs/sftpfs/internal.c b/src/vfs/sftpfs/internal.c index 38346b24c..ebb300457 100644 --- a/src/vfs/sftpfs/internal.c +++ b/src/vfs/sftpfs/internal.c @@ -74,13 +74,16 @@ sftpfs_ssherror_to_gliberror (sftpfs_super_data_t * super_data, int libssh_errno * Fix filename for SFTP operations: add leading slash to file name. * * @param file_name file name - * @return newly allocated string contains the file name with leading slash + * @param length length of returned string + * + * @return pointer to string that contains the file name with leading slash */ const char * -sftpfs_fix_filename (const char *file_name) +sftpfs_fix_filename (const char *file_name, unsigned int *length) { g_string_printf (sftpfs_filename_buffer, "%c%s", PATH_SEP, file_name); + *length = sftpfs_filename_buffer->len; return sftpfs_filename_buffer->str; } @@ -171,11 +174,13 @@ sftpfs_lstat (const vfs_path_t * vpath, struct stat *buf, GError ** mcerror) do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); - res = libssh2_sftp_stat_ex (super_data->sftp_session, fixfname, - sftpfs_filename_buffer->len, LIBSSH2_SFTP_LSTAT, &attrs); + res = + libssh2_sftp_stat_ex (super_data->sftp_session, fixfname, fixfname_len, + LIBSSH2_SFTP_LSTAT, &attrs); if (res >= 0) break; @@ -251,12 +256,13 @@ sftpfs_stat (const vfs_path_t * vpath, struct stat *buf, GError ** mcerror) do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); res = - libssh2_sftp_stat_ex (super_data->sftp_session, - fixfname, sftpfs_filename_buffer->len, LIBSSH2_SFTP_STAT, &attrs); + libssh2_sftp_stat_ex (super_data->sftp_session, fixfname, fixfname_len, + LIBSSH2_SFTP_STAT, &attrs); if (res >= 0) break; @@ -333,12 +339,13 @@ sftpfs_readlink (const vfs_path_t * vpath, char *buf, size_t size, GError ** mce do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); res = - libssh2_sftp_symlink_ex (super_data->sftp_session, fixfname, - sftpfs_filename_buffer->len, buf, size, LIBSSH2_SFTP_READLINK); + libssh2_sftp_symlink_ex (super_data->sftp_session, fixfname, fixfname_len, buf, size, + LIBSSH2_SFTP_READLINK); if (res >= 0) break; @@ -391,8 +398,7 @@ sftpfs_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2, GError ** if (super_data->sftp_session == NULL) return -1; - tmp_path = (char *) sftpfs_fix_filename (path_element2->path); - tmp_path_len = sftpfs_filename_buffer->len; + tmp_path = (char *) sftpfs_fix_filename (path_element2->path, &tmp_path_len); tmp_path = g_strndup (tmp_path, tmp_path_len); path_element1 = vfs_path_get_by_index (vpath1, -1); @@ -400,14 +406,13 @@ sftpfs_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2, GError ** do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element1->path); + fixfname = sftpfs_fix_filename (path_element1->path, &fixfname_len); res = - libssh2_sftp_symlink_ex (super_data->sftp_session, - fixfname, - sftpfs_filename_buffer->len, tmp_path, tmp_path_len, - LIBSSH2_SFTP_SYMLINK); + libssh2_sftp_symlink_ex (super_data->sftp_session, fixfname, fixfname_len, tmp_path, + tmp_path_len, LIBSSH2_SFTP_SYMLINK); if (res >= 0) break; @@ -467,11 +472,13 @@ sftpfs_chmod (const vfs_path_t * vpath, mode_t mode, GError ** mcerror) do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); - res = libssh2_sftp_stat_ex (super_data->sftp_session, fixfname, - sftpfs_filename_buffer->len, LIBSSH2_SFTP_LSTAT, &attrs); + res = + libssh2_sftp_stat_ex (super_data->sftp_session, fixfname, fixfname_len, + LIBSSH2_SFTP_LSTAT, &attrs); if (res >= 0) break; @@ -491,14 +498,17 @@ sftpfs_chmod (const vfs_path_t * vpath, mode_t mode, GError ** mcerror) do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); - res = libssh2_sftp_stat_ex (super_data->sftp_session, fixfname, - sftpfs_filename_buffer->len, LIBSSH2_SFTP_SETSTAT, &attrs); + res = + libssh2_sftp_stat_ex (super_data->sftp_session, fixfname, fixfname_len, + LIBSSH2_SFTP_SETSTAT, &attrs); if (res >= 0) break; - else if (res != LIBSSH2_ERROR_EAGAIN) + + if (res != LIBSSH2_ERROR_EAGAIN) { sftpfs_ssherror_to_gliberror (super_data, res, mcerror); return -1; @@ -545,12 +555,11 @@ sftpfs_unlink (const vfs_path_t * vpath, GError ** mcerror) do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element->path); + fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len); - res = - libssh2_sftp_unlink_ex (super_data->sftp_session, fixfname, - sftpfs_filename_buffer->len); + res = libssh2_sftp_unlink_ex (super_data->sftp_session, fixfname, fixfname_len); if (res >= 0) break; @@ -602,8 +611,7 @@ sftpfs_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2, GError ** m if (super_data->sftp_session == NULL) return -1; - tmp_path = (char *) sftpfs_fix_filename (path_element2->path); - tmp_path_len = sftpfs_filename_buffer->len; + tmp_path = (char *) sftpfs_fix_filename (path_element2->path, &tmp_path_len); tmp_path = g_strndup (tmp_path, tmp_path_len); path_element1 = vfs_path_get_by_index (vpath1, -1); @@ -611,12 +619,13 @@ sftpfs_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2, GError ** m do { const char *fixfname; + unsigned int fixfname_len = 0; - fixfname = sftpfs_fix_filename (path_element1->path); + fixfname = sftpfs_fix_filename (path_element1->path, &fixfname_len); res = - libssh2_sftp_rename_ex (super_data->sftp_session, fixfname, sftpfs_filename_buffer->len, - tmp_path, tmp_path_len, LIBSSH2_SFTP_SYMLINK); + libssh2_sftp_rename_ex (super_data->sftp_session, fixfname, fixfname_len, tmp_path, + tmp_path_len, LIBSSH2_SFTP_SYMLINK); if (res >= 0) break; diff --git a/src/vfs/sftpfs/internal.h b/src/vfs/sftpfs/internal.h index ea7e70124..2c08e5f55 100644 --- a/src/vfs/sftpfs/internal.h +++ b/src/vfs/sftpfs/internal.h @@ -70,7 +70,7 @@ void sftpfs_ssherror_to_gliberror (sftpfs_super_data_t * super_data, int libssh_ GError ** mcerror); int sftpfs_waitsocket (sftpfs_super_data_t * super_data, GError ** mcerror); -const char *sftpfs_fix_filename (const char *file_name); +const char *sftpfs_fix_filename (const char *file_name, unsigned int *length); void sftpfs_blksize (struct stat *s); int sftpfs_lstat (const vfs_path_t * vpath, struct stat *buf, GError ** mcerror); int sftpfs_stat (const vfs_path_t * vpath, struct stat *buf, GError ** mcerror);