root/src/vfs/sftpfs/dir.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. sftpfs_opendir
  2. sftpfs_readdir
  3. sftpfs_closedir
  4. sftpfs_mkdir
  5. sftpfs_rmdir

   1 /* Virtual File System: SFTP file system.
   2    The internal functions: dirs
   3 
   4    Copyright (C) 2011-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Ilia Maslakov <il.smind@gmail.com>, 2011
   9    Slava Zanko <slavazanko@gmail.com>, 2011, 2012
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25  */
  26 
  27 #include <config.h>
  28 
  29 #include <libssh2.h>
  30 #include <libssh2_sftp.h>
  31 
  32 #include "lib/global.h"
  33 #include "lib/util.h"
  34 
  35 #include "internal.h"
  36 
  37 /*** global variables ****************************************************************************/
  38 
  39 /*** file scope macro definitions ****************************************************************/
  40 
  41 /*** file scope type declarations ****************************************************************/
  42 
  43 typedef struct
  44 {
  45     LIBSSH2_SFTP_HANDLE *handle;
  46     sftpfs_super_t *super;
  47 } sftpfs_dir_data_t;
  48 
  49 /*** file scope variables ************************************************************************/
  50 
  51 /*** file scope functions ************************************************************************/
  52 /* --------------------------------------------------------------------------------------------- */
  53 
  54 /* --------------------------------------------------------------------------------------------- */
  55 /*** public functions ****************************************************************************/
  56 /* --------------------------------------------------------------------------------------------- */
  57 /**
  58  * Open a directory stream corresponding to the directory name.
  59  *
  60  * @param vpath   path to directory
  61  * @param mcerror pointer to the error handler
  62  * @return directory data handler if success, NULL otherwise
  63  */
  64 
  65 void *
  66 sftpfs_opendir (const vfs_path_t * vpath, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
  67 {
  68     sftpfs_dir_data_t *sftpfs_dir;
  69     sftpfs_super_t *sftpfs_super;
  70     const vfs_path_element_t *path_element;
  71     LIBSSH2_SFTP_HANDLE *handle;
  72     const GString *fixfname;
  73 
  74     if (!sftpfs_op_init (&sftpfs_super, &path_element, vpath, mcerror))
  75         return NULL;
  76 
  77     fixfname = sftpfs_fix_filename (path_element->path);
  78 
  79     while (TRUE)
  80     {
  81         int libssh_errno;
  82 
  83         handle =
  84             libssh2_sftp_open_ex (sftpfs_super->sftp_session, fixfname->str, fixfname->len, 0, 0,
  85                                   LIBSSH2_SFTP_OPENDIR);
  86         if (handle != NULL)
  87             break;
  88 
  89         libssh_errno = libssh2_session_last_errno (sftpfs_super->session);
  90         if (!sftpfs_waitsocket (sftpfs_super, libssh_errno, mcerror))
  91             return NULL;
  92     }
  93 
  94     sftpfs_dir = g_new0 (sftpfs_dir_data_t, 1);
  95     sftpfs_dir->handle = handle;
  96     sftpfs_dir->super = sftpfs_super;
  97 
  98     return (void *) sftpfs_dir;
  99 }
 100 
 101 /* --------------------------------------------------------------------------------------------- */
 102 /**
 103  * Get a pointer to a structure representing the next directory entry.
 104  *
 105  * @param data    directory data handler
 106  * @param mcerror pointer to the error handler
 107  * @return information about direntry if success, NULL otherwise
 108  */
 109 
 110 struct vfs_dirent *
 111 sftpfs_readdir (void *data, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 112 {
 113     char mem[BUF_MEDIUM];
 114     LIBSSH2_SFTP_ATTRIBUTES attrs;
 115     sftpfs_dir_data_t *sftpfs_dir = (sftpfs_dir_data_t *) data;
 116     int rc;
 117 
 118     mc_return_val_if_error (mcerror, NULL);
 119 
 120     do
 121     {
 122         rc = libssh2_sftp_readdir (sftpfs_dir->handle, mem, sizeof (mem), &attrs);
 123         if (rc >= 0)
 124             break;
 125 
 126         if (!sftpfs_waitsocket (sftpfs_dir->super, rc, mcerror))
 127             return NULL;
 128     }
 129     while (rc == LIBSSH2_ERROR_EAGAIN);
 130 
 131     return (rc != 0 ? vfs_dirent_init (NULL, mem, 0) : NULL);   /* FIXME: inode */
 132 }
 133 
 134 /* --------------------------------------------------------------------------------------------- */
 135 /**
 136  * Close the directory stream.
 137  *
 138  * @param data    directory data handler
 139  * @param mcerror pointer to the error handler
 140  * @return 0 if success, negative value otherwise
 141  */
 142 
 143 int
 144 sftpfs_closedir (void *data, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 145 {
 146     int rc;
 147     sftpfs_dir_data_t *sftpfs_dir = (sftpfs_dir_data_t *) data;
 148 
 149     mc_return_val_if_error (mcerror, -1);
 150 
 151     rc = libssh2_sftp_closedir (sftpfs_dir->handle);
 152     g_free (sftpfs_dir);
 153     return rc;
 154 }
 155 
 156 /* --------------------------------------------------------------------------------------------- */
 157 /**
 158  * Create a new directory.
 159  *
 160  * @param vpath   path directory
 161  * @param mode    mode (see man 2 open)
 162  * @param mcerror pointer to the error handler
 163  * @return 0 if success, negative value otherwise
 164  */
 165 
 166 int
 167 sftpfs_mkdir (const vfs_path_t * vpath, mode_t mode, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 168 {
 169     int res;
 170     sftpfs_super_t *sftpfs_super;
 171     const vfs_path_element_t *path_element;
 172     const GString *fixfname;
 173 
 174     if (!sftpfs_op_init (&sftpfs_super, &path_element, vpath, mcerror))
 175         return -1;
 176 
 177     fixfname = sftpfs_fix_filename (path_element->path);
 178 
 179     do
 180     {
 181         res =
 182             libssh2_sftp_mkdir_ex (sftpfs_super->sftp_session, fixfname->str, fixfname->len, mode);
 183         if (res >= 0)
 184             break;
 185 
 186         if (!sftpfs_waitsocket (sftpfs_super, res, mcerror))
 187             return -1;
 188     }
 189     while (res == LIBSSH2_ERROR_EAGAIN);
 190 
 191     return res;
 192 }
 193 
 194 /* --------------------------------------------------------------------------------------------- */
 195 /**
 196  * Remove a directory.
 197  *
 198  * @param vpath   path directory
 199  * @param mcerror pointer to the error handler
 200  * @return 0 if success, negative value otherwise
 201  */
 202 
 203 int
 204 sftpfs_rmdir (const vfs_path_t * vpath, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 205 {
 206     int res;
 207     sftpfs_super_t *sftpfs_super;
 208     const vfs_path_element_t *path_element;
 209     const GString *fixfname;
 210 
 211     if (!sftpfs_op_init (&sftpfs_super, &path_element, vpath, mcerror))
 212         return -1;
 213 
 214     fixfname = sftpfs_fix_filename (path_element->path);
 215 
 216     do
 217     {
 218         res = libssh2_sftp_rmdir_ex (sftpfs_super->sftp_session, fixfname->str, fixfname->len);
 219         if (res >= 0)
 220             break;
 221 
 222         if (!sftpfs_waitsocket (sftpfs_super, res, mcerror))
 223             return -1;
 224     }
 225     while (res == LIBSSH2_ERROR_EAGAIN);
 226 
 227     return res;
 228 }
 229 
 230 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */