sftpfs: refactoring: get rid of code duplication.

* (sftpfs_op_init): make public and use it...
  * (sftpfs_opendir): here,
  * (sftpfs_mkdir): here,
  * (sftpfs_rmdir): and here.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2023-07-16 12:48:07 +03:00
parent ec98810986
commit b926c08ec9
3 changed files with 32 additions and 60 deletions

View File

@ -66,21 +66,14 @@ void *
sftpfs_opendir (const vfs_path_t * vpath, GError ** mcerror)
{
sftpfs_dir_data_t *sftpfs_dir;
struct vfs_s_super *super;
sftpfs_super_t *sftpfs_super;
const vfs_path_element_t *path_element;
LIBSSH2_SFTP_HANDLE *handle;
const GString *fixfname;
mc_return_val_if_error (mcerror, NULL);
path_element = vfs_path_get_by_index (vpath, -1);
if (vfs_s_get_path (vpath, &super, 0) == NULL)
if (!sftpfs_op_init (&sftpfs_super, &path_element, vpath, mcerror))
return NULL;
sftpfs_super = SFTP_SUPER (super);
fixfname = sftpfs_fix_filename (path_element->path);
while (TRUE)
@ -174,23 +167,11 @@ int
sftpfs_mkdir (const vfs_path_t * vpath, mode_t mode, GError ** mcerror)
{
int res;
struct vfs_s_super *super;
sftpfs_super_t *sftpfs_super;
const vfs_path_element_t *path_element;
const GString *fixfname;
mc_return_val_if_error (mcerror, -1);
path_element = vfs_path_get_by_index (vpath, -1);
if (vfs_s_get_path (vpath, &super, 0) == NULL)
return -1;
if (super == NULL)
return -1;
sftpfs_super = SFTP_SUPER (super);
if (sftpfs_super->sftp_session == NULL)
if (!sftpfs_op_init (&sftpfs_super, &path_element, vpath, mcerror))
return -1;
fixfname = sftpfs_fix_filename (path_element->path);
@ -223,23 +204,11 @@ int
sftpfs_rmdir (const vfs_path_t * vpath, GError ** mcerror)
{
int res;
struct vfs_s_super *super;
sftpfs_super_t *sftpfs_super;
const vfs_path_element_t *path_element;
const GString *fixfname;
mc_return_val_if_error (mcerror, -1);
path_element = vfs_path_get_by_index (vpath, -1);
if (vfs_s_get_path (vpath, &super, 0) == NULL)
return -1;
if (super == NULL)
return -1;
sftpfs_super = SFTP_SUPER (super);
if (sftpfs_super->sftp_session == NULL)
if (!sftpfs_op_init (&sftpfs_super, &path_element, vpath, mcerror))
return -1;
fixfname = sftpfs_fix_filename (path_element->path);

View File

@ -113,32 +113,6 @@ sftpfs_internal_waitsocket (sftpfs_super_t * super, GError ** mcerror)
/* --------------------------------------------------------------------------------------------- */
static gboolean
sftpfs_op_init (sftpfs_super_t ** super, const vfs_path_element_t ** path_element,
const vfs_path_t * vpath, GError ** mcerror)
{
struct vfs_s_super *lc_super = NULL;
mc_return_val_if_error (mcerror, FALSE);
if (vfs_s_get_path (vpath, &lc_super, 0) == NULL)
return FALSE;
if (lc_super == NULL)
return FALSE;
*super = SFTP_SUPER (lc_super);
if ((*super)->sftp_session == NULL)
return FALSE;
*path_element = vfs_path_get_by_index (vpath, -1);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static int
sftpfs_stat_init (sftpfs_super_t ** super, const vfs_path_element_t ** path_element,
const vfs_path_t * vpath, GError ** mcerror, int stat_type,
@ -245,6 +219,31 @@ sftpfs_fix_filename (const char *file_name)
/* --------------------------------------------------------------------------------------------- */
gboolean
sftpfs_op_init (sftpfs_super_t ** super, const vfs_path_element_t ** path_element,
const vfs_path_t * vpath, GError ** mcerror)
{
struct vfs_s_super *lc_super = NULL;
mc_return_val_if_error (mcerror, FALSE);
if (vfs_s_get_path (vpath, &lc_super, 0) == NULL)
return FALSE;
if (lc_super == NULL)
return FALSE;
*super = SFTP_SUPER (lc_super);
if ((*super)->sftp_session == NULL)
return FALSE;
*path_element = vfs_path_get_by_index (vpath, -1);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
void
sftpfs_attr_to_stat (const LIBSSH2_SFTP_ATTRIBUTES * attrs, struct stat *s)
{

View File

@ -74,6 +74,10 @@ void sftpfs_ssherror_to_gliberror (sftpfs_super_t * super, int libssh_errno, GEr
gboolean sftpfs_waitsocket (sftpfs_super_t * super, int sftp_res, GError ** mcerror);
const GString *sftpfs_fix_filename (const char *file_name);
gboolean sftpfs_op_init (sftpfs_super_t ** super, const vfs_path_element_t ** path_element,
const vfs_path_t * vpath, GError ** mcerror);
void sftpfs_attr_to_stat (const LIBSSH2_SFTP_ATTRIBUTES * attrs, 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);