summaryrefslogtreecommitdiffstats
path: root/src/vfs/sftpfs/file.c
blob: ab91b705f9eb71767d86d46adb27928602751738 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* Virtual File System: SFTP file system.
   The internal functions: files

   Copyright (C) 2011-2024
   Free Software Foundation, Inc.

   Written by:
   Ilia Maslakov <il.smind@gmail.com>, 2011
   Slava Zanko <slavazanko@gmail.com>, 2011, 2012

   This file is part of the Midnight Commander.

   The Midnight Commander 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 3 of the License,
   or (at your option) any later version.

   The Midnight Commander 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <errno.h>              /* ENOENT, EACCES */

#include <libssh2.h>
#include <libssh2_sftp.h>

#include "lib/global.h"
#include "lib/util.h"

#include "internal.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

#define SFTP_FILE_HANDLER(a) ((sftpfs_file_handler_t *) a)

/*** file scope type declarations ****************************************************************/

typedef struct
{
    vfs_file_handler_t base;    /* base class */

    LIBSSH2_SFTP_HANDLE *handle;
    int flags;
    mode_t mode;
} sftpfs_file_handler_t;

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/**
 * Reopen file by file handle.
 *
 * @param fh      the file handler
 * @param mcerror pointer to the error handler
 */
static void
sftpfs_reopen (vfs_file_handler_t * fh, GError ** mcerror)
{
    sftpfs_file_handler_t *file = SFTP_FILE_HANDLER (fh);
    int flags;
    mode_t mode;

    g_return_if_fail (mcerror == NULL || *mcerror == NULL);

    flags = file->flags;
    mode = file->mode;

    sftpfs_close_file (fh, mcerror);
    sftpfs_open_file (fh, flags, mode, mcerror);
}

/* --------------------------------------------------------------------------------------------- */

static int
sftpfs_file__handle_error (sftpfs_super_t * super, int sftp_res, GError ** mcerror)
{
    if (sftpfs_is_sftp_error (super->sftp_session, sftp_res, LIBSSH2_FX_PERMISSION_DENIED))
        return -EACCES;

    if (sftpfs_is_sftp_error (super->sftp_session, sftp_res, LIBSSH2_FX_NO_SUCH_FILE))
        return -ENOENT;

    if (!sftpfs_waitsocket (super, sftp_res, mcerror))
        return -1;

    return 0;
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

vfs_file_handler_t *
sftpfs_fh_new (struct vfs_s_inode * ino, gboolean changed)
{
    sftpfs_file_handler_t *fh;

    fh = g_new0 (sftpfs_file_handler_t, 1);
    vfs_s_init_fh (VFS_FILE_HANDLER (fh), ino, changed);

    return VFS_FILE_HANDLER (fh);
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Open new SFTP file.
 *
 * @param fh      the file handler
 * @param flags   flags (see man 2 open)
 * @param mode    mode (see man 2 open)
 * @param mcerror pointer to the error handler
 * @return TRUE if connection was created successfully, FALSE otherwise
 */

gboolean
sftpfs_open_file (vfs_file_handler_t * fh, int flags, mode_t mode, GError ** mcerror)
{
    unsigned long sftp_open_flags = 0;
    int sftp_open_mode = 0;
    gboolean do_append = FALSE;
    sftpfs_file_handler_t *file = SFTP_FILE_HANDLER (fh);
    sftpfs_super_t *super = SFTP_SUPER (fh->ino->super);
    char *name;
    const GString *fixfname;

    (void) mode;
    mc_return_val_if_error (mcerror, FALSE);

    name = vfs_s_fullpath (vfs_sftpfs_ops, fh->ino);
    if (name == NULL)
        return FALSE;

    if ((flags & O_CREAT) != 0 || (flags & O_WRONLY) != 0)
    {
        sftp_open_flags = (flags & O_WRONLY) != 0 ? LIBSSH2_FXF_WRITE : 0;
        sftp_open_flags |= (flags & O_CREAT) != 0 ? LIBSSH2_FXF_CREAT : 0;
        if ((flags & O_APPEND) != 0)
        {
            sftp_open_flags |= LIBSSH2_FXF_APPEND;
            do_append = TRUE;
        }
        sftp_open_flags |= (flags & O_TRUNC) != 0 ? LIBSSH2_FXF_TRUNC : 0;

        sftp_open_mode = LIBSSH2_SFTP_S_IRUSR |
            LIBSSH2_SFTP_S_IWUSR | LIBSSH2_SFTP_S_IRGRP | LIBSSH2_SFTP_S_IROTH;
    }
    else
        sftp_open_flags = LIBSSH2_FXF_READ;

    fixfname = sftpfs_fix_filename (name);

    while (TRUE)
    {
        int libssh_errno;

        file->handle =
            libssh2_sftp_open_ex (super->sftp_session, fixfname->str, fixfname->len,
                                  sftp_open_flags, sftp_open_mode, LIBSSH2_SFTP_OPENFILE);
        if (file->handle != NULL)
            break;

        libssh_errno = libssh2_session_last_errno (super->session);
        if (libssh_errno != LIBSSH2_ERROR_EAGAIN)
        {
            sftpfs_ssherror_to_gliberror (super, libssh_errno, mcerror);
            g_free (name);
            return FALSE;
        }
    }

    g_free (name);

    file->flags = flags;
    file->mode = mode;

    if (do_append)
    {
        struct stat file_info = {
            .st_dev = 0
        };
        /* In case of

           struct stat file_info = { 0 };

           gcc < 4.7 [1] generates the following:

           error: missing initializer [-Werror=missing-field-initializers]
           error: (near initialization for 'file_info.st_dev') [-Werror=missing-field-initializers]

           [1] http://stackoverflow.com/questions/13373695/how-to-remove-the-warning-in-gcc-4-6-missing-initializer-wmissing-field-initi/27461062#27461062
         */

        if (sftpfs_fstat (fh, &file_info, mcerror) == 0)
            libssh2_sftp_seek64 (file->handle, file_info.st_size);
    }
    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Stats the file specified by the file descriptor.
 *
 * @param data    file handler
 * @param buf     buffer for store stat-info
 * @param mcerror pointer to the error handler
 * @return 0 if success, negative value otherwise
 */

int
sftpfs_fstat (void *data, struct stat *buf, GError ** mcerror)
{
    int res;
    LIBSSH2_SFTP_ATTRIBUTES attrs;
    vfs_file_handler_t *fh = VFS_FILE_HANDLER (data);
    sftpfs_file_handler_t *sftpfs_fh = (sftpfs_file_handler_t *) data;
    struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
    sftpfs_super_t *sftpfs_super = SFTP_SUPER (super);

    mc_return_val_if_error (mcerror, -1);

    if (sftpfs_fh->handle == NULL)
        return -1;

    do
    {
        int err;

        res = libssh2_sftp_fstat_ex (sftpfs_fh->handle, &attrs, 0);
        if (res >= 0)
            break;

        err = sftpfs_file__handle_error (sftpfs_super, res, mcerror);
        if (err < 0)
            return err;
    }
    while (res == LIBSSH2_ERROR_EAGAIN);

    sftpfs_attr_to_stat (&attrs, buf);

    return 0;
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Read up to 'count' bytes from the file descriptor 'fh' to the buffer starting at 'buffer'.
 *
 * @param fh      file handler
 * @param buffer  buffer for data
 * @param count   data size
 * @param mcerror pointer to the error handler
 *
 * @return 0 on success, negative value otherwise
 */

ssize_t
sftpfs_read_file (vfs_file_handler_t * fh, char *buffer, size_t count, GError ** mcerror)
{
    ssize_t rc;
    sftpfs_file_handler_t *file = SFTP_FILE_HANDLER (fh);
    sftpfs_super_t *super;

    mc_return_val_if_error (mcerror, -1);

    if (fh == NULL)
    {
        mc_propagate_error (mcerror, 0, "%s",
                            _("sftp: No file handler data present for reading file"));
        return -1;
    }

    super = SFTP_SUPER (VFS_FILE_HANDLER_SUPER (fh));

    do
    {
        int err;

        rc = libssh2_sftp_read (file->handle, buffer, count);
        if (rc >= 0)
            break;

        err = sftpfs_file__handle_error (super, (int) rc, mcerror);
        if (err < 0)
            return err;
    }
    while (rc == LIBSSH2_ERROR_EAGAIN);

    fh->pos = (off_t) libssh2_sftp_tell64 (file->handle);

    return rc;
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Write up to 'count' bytes from  the buffer starting at 'buffer' to the descriptor 'fh'.
 *
 * @param fh      file handler
 * @param buffer  buffer for data
 * @param count   data size
 * @param mcerror pointer to the error handler
 *
 * @return 0 on success, negative value otherwise
 */

ssize_t
sftpfs_write_file (vfs_file_handler_t * fh, const char *buffer, size_t count, GError ** mcerror)
{
    ssize_t rc;
    sftpfs_file_handler_t *file = SFTP_FILE_HANDLER (fh);
    sftpfs_super_t *super = SFTP_SUPER (VFS_FILE_HANDLER_SUPER (fh));

    mc_return_val_if_error (mcerror, -1);

    fh->pos = (off_t) libssh2_sftp_tell64 (file->handle);

    do
    {
        int err;

        rc = libssh2_sftp_write (file->handle, buffer, count);
        if (rc >= 0)
            break;

        err = sftpfs_file__handle_error (super, (int) rc, mcerror);
        if (err < 0)
            return err;
    }
    while (rc == LIBSSH2_ERROR_EAGAIN);

    return rc;
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Close a file descriptor.
 *
 * @param fh      file handler
 * @param mcerror pointer to the error handler
 *
 * @return 0 on success, negative value otherwise
 */

int
sftpfs_close_file (vfs_file_handler_t * fh, GError ** mcerror)
{
    int ret;

    mc_return_val_if_error (mcerror, -1);

    ret = libssh2_sftp_close (SFTP_FILE_HANDLER (fh)->handle);

    return ret == 0 ? 0 : -1;
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Reposition the offset of the open file associated with the file descriptor.
 *
 * @param fh      file handler
 * @param offset  file offset
 * @param whence  method of seek (at begin, at current, at end)
 * @param mcerror pointer to the error handler
 *
 * @return 0 on success, negative value otherwise
 */

off_t
sftpfs_lseek (vfs_file_handler_t * fh, off_t offset, int whence, GError ** mcerror)
{
    sftpfs_file_handler_t *file = SFTP_FILE_HANDLER (fh);

    mc_return_val_if_error (mcerror, 0);

    switch (whence)
    {
    case SEEK_SET:
        /* Need reopen file because:
           "You MUST NOT seek during writing or reading a file with SFTP, as the internals use
           outstanding packets and changing the "file position" during transit will results in
           badness." */
        if (fh->pos > offset || offset == 0)
        {
            sftpfs_reopen (fh, mcerror);
            mc_return_val_if_error (mcerror, 0);
        }
        fh->pos = offset;
        break;
    case SEEK_CUR:
        fh->pos += offset;
        break;
    case SEEK_END:
        if (fh->pos > fh->ino->st.st_size - offset)
        {
            sftpfs_reopen (fh, mcerror);
            mc_return_val_if_error (mcerror, 0);
        }
        fh->pos = fh->ino->st.st_size - offset;
        break;
    default:
        break;
    }

    libssh2_sftp_seek64 (file->handle, fh->pos);
    fh->pos = (off_t) libssh2_sftp_tell64 (file->handle);

    return fh->pos;
}

/* --------------------------------------------------------------------------------------------- */