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-2021
   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     struct vfs_s_super *super;
  70     sftpfs_super_t *sftpfs_super;
  71     const vfs_path_element_t *path_element;
  72     LIBSSH2_SFTP_HANDLE *handle;
  73 
  74     mc_return_val_if_error (mcerror, NULL);
  75 
  76     path_element = vfs_path_get_by_index (vpath, -1);
  77 
  78     if (vfs_s_get_path (vpath, &super, 0) == NULL)
  79         return NULL;
  80 
  81     sftpfs_super = SFTP_SUPER (super);
  82 
  83     while (TRUE)
  84     {
  85         const char *fixfname;
  86         unsigned int fixfname_len = 0;
  87         int libssh_errno;
  88 
  89         fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len);
  90 
  91         handle =
  92             libssh2_sftp_open_ex (sftpfs_super->sftp_session, fixfname, fixfname_len, 0, 0,
  93                                   LIBSSH2_SFTP_OPENDIR);
  94         if (handle != NULL)
  95             break;
  96 
  97         libssh_errno = libssh2_session_last_errno (sftpfs_super->session);
  98         if (!sftpfs_waitsocket (sftpfs_super, libssh_errno, mcerror))
  99             return NULL;
 100     }
 101 
 102     sftpfs_dir = g_new0 (sftpfs_dir_data_t, 1);
 103     sftpfs_dir->handle = handle;
 104     sftpfs_dir->super = sftpfs_super;
 105 
 106     return (void *) sftpfs_dir;
 107 }
 108 
 109 /* --------------------------------------------------------------------------------------------- */
 110 /**
 111  * Get a pointer to a structure representing the next directory entry.
 112  *
 113  * @param data    directory data handler
 114  * @param mcerror pointer to the error handler
 115  * @return information about direntry if success, NULL otherwise
 116  */
 117 
 118 struct vfs_dirent *
 119 sftpfs_readdir (void *data, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 120 {
 121     char mem[BUF_MEDIUM];
 122     LIBSSH2_SFTP_ATTRIBUTES attrs;
 123     sftpfs_dir_data_t *sftpfs_dir = (sftpfs_dir_data_t *) data;
 124     int rc;
 125 
 126     mc_return_val_if_error (mcerror, NULL);
 127 
 128     do
 129     {
 130         rc = libssh2_sftp_readdir (sftpfs_dir->handle, mem, sizeof (mem), &attrs);
 131         if (rc >= 0)
 132             break;
 133 
 134         if (!sftpfs_waitsocket (sftpfs_dir->super, rc, mcerror))
 135             return NULL;
 136     }
 137     while (rc == LIBSSH2_ERROR_EAGAIN);
 138 
 139     return (rc != 0 ? vfs_dirent_init (NULL, mem, 0) : NULL);   /* FIXME: inode */
 140 }
 141 
 142 /* --------------------------------------------------------------------------------------------- */
 143 /**
 144  * Close the directory stream.
 145  *
 146  * @param data    directory data handler
 147  * @param mcerror pointer to the error handler
 148  * @return 0 if success, negative value otherwise
 149  */
 150 
 151 int
 152 sftpfs_closedir (void *data, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 153 {
 154     int rc;
 155     sftpfs_dir_data_t *sftpfs_dir = (sftpfs_dir_data_t *) data;
 156 
 157     mc_return_val_if_error (mcerror, -1);
 158 
 159     rc = libssh2_sftp_closedir (sftpfs_dir->handle);
 160     g_free (sftpfs_dir);
 161     return rc;
 162 }
 163 
 164 /* --------------------------------------------------------------------------------------------- */
 165 /**
 166  * Create a new directory.
 167  *
 168  * @param vpath   path directory
 169  * @param mode    mode (see man 2 open)
 170  * @param mcerror pointer to the error handler
 171  * @return 0 if success, negative value otherwise
 172  */
 173 
 174 int
 175 sftpfs_mkdir (const vfs_path_t * vpath, mode_t mode, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 176 {
 177     int res;
 178     struct vfs_s_super *super;
 179     sftpfs_super_t *sftpfs_super;
 180     const vfs_path_element_t *path_element;
 181 
 182     mc_return_val_if_error (mcerror, -1);
 183 
 184     path_element = vfs_path_get_by_index (vpath, -1);
 185 
 186     if (vfs_s_get_path (vpath, &super, 0) == NULL)
 187         return -1;
 188 
 189     if (super == NULL)
 190         return -1;
 191 
 192     sftpfs_super = SFTP_SUPER (super);
 193     if (sftpfs_super->sftp_session == NULL)
 194         return -1;
 195 
 196     do
 197     {
 198         const char *fixfname;
 199         unsigned int fixfname_len = 0;
 200 
 201         fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len);
 202 
 203         res = libssh2_sftp_mkdir_ex (sftpfs_super->sftp_session, fixfname, fixfname_len, mode);
 204         if (res >= 0)
 205             break;
 206 
 207         if (!sftpfs_waitsocket (sftpfs_super, res, mcerror))
 208             return -1;
 209     }
 210     while (res == LIBSSH2_ERROR_EAGAIN);
 211 
 212     return res;
 213 }
 214 
 215 /* --------------------------------------------------------------------------------------------- */
 216 /**
 217  * Remove a directory.
 218  *
 219  * @param vpath   path directory
 220  * @param mcerror pointer to the error handler
 221  * @return 0 if success, negative value otherwise
 222  */
 223 
 224 int
 225 sftpfs_rmdir (const vfs_path_t * vpath, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 226 {
 227     int res;
 228     struct vfs_s_super *super;
 229     sftpfs_super_t *sftpfs_super;
 230     const vfs_path_element_t *path_element;
 231 
 232     mc_return_val_if_error (mcerror, -1);
 233 
 234     path_element = vfs_path_get_by_index (vpath, -1);
 235 
 236     if (vfs_s_get_path (vpath, &super, 0) == NULL)
 237         return -1;
 238 
 239     if (super == NULL)
 240         return -1;
 241 
 242     sftpfs_super = SFTP_SUPER (super);
 243     if (sftpfs_super->sftp_session == NULL)
 244         return -1;
 245 
 246     do
 247     {
 248         const char *fixfname;
 249         unsigned int fixfname_len = 0;
 250 
 251         fixfname = sftpfs_fix_filename (path_element->path, &fixfname_len);
 252 
 253         res = libssh2_sftp_rmdir_ex (sftpfs_super->sftp_session, fixfname, fixfname_len);
 254         if (res >= 0)
 255             break;
 256 
 257         if (!sftpfs_waitsocket (sftpfs_super, res, mcerror))
 258             return -1;
 259     }
 260     while (res == LIBSSH2_ERROR_EAGAIN);
 261 
 262     return res;
 263 }
 264 
 265 /* --------------------------------------------------------------------------------------------- */

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