2008-09-29 14:11:29 +00:00
|
|
|
/* Copyright (c) 2004-2008, Sara Golemon <sarag@libssh2.org>
|
2010-05-05 15:44:15 +07:00
|
|
|
* Copyright (c) 2007 Eli Fant <elifantu@mail.ru>
|
2010-04-01 13:20:55 +02:00
|
|
|
* Copyright (c) 2009-2010 by Daniel Stenberg
|
2004-12-22 00:20:02 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
* that the following conditions are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
* following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* Neither the name of the copyright holder nor the names
|
|
|
|
* of any other contributors may be used to endorse or
|
|
|
|
* promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2009-03-27 22:24:09 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2004-12-22 00:20:02 +00:00
|
|
|
#include "libssh2_priv.h"
|
|
|
|
#include "libssh2_sftp.h"
|
2009-03-27 20:20:48 +00:00
|
|
|
#include "channel.h"
|
2010-04-24 20:21:22 +02:00
|
|
|
#include "session.h"
|
2010-10-25 01:43:54 +02:00
|
|
|
#include "sftp.h"
|
2004-12-22 00:20:02 +00:00
|
|
|
|
|
|
|
/* Note: Version 6 was documented at the time of writing
|
|
|
|
* However it was marked as "DO NOT IMPLEMENT" due to pending changes
|
|
|
|
*
|
|
|
|
* This release of libssh2 implements Version 5 with automatic downgrade
|
|
|
|
* based on server's declaration
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* SFTP packet types */
|
2007-02-02 16:21:20 +00:00
|
|
|
#define SSH_FXP_INIT 1
|
|
|
|
#define SSH_FXP_VERSION 2
|
|
|
|
#define SSH_FXP_OPEN 3
|
|
|
|
#define SSH_FXP_CLOSE 4
|
|
|
|
#define SSH_FXP_READ 5
|
|
|
|
#define SSH_FXP_WRITE 6
|
|
|
|
#define SSH_FXP_LSTAT 7
|
|
|
|
#define SSH_FXP_FSTAT 8
|
|
|
|
#define SSH_FXP_SETSTAT 9
|
|
|
|
#define SSH_FXP_FSETSTAT 10
|
|
|
|
#define SSH_FXP_OPENDIR 11
|
|
|
|
#define SSH_FXP_READDIR 12
|
|
|
|
#define SSH_FXP_REMOVE 13
|
|
|
|
#define SSH_FXP_MKDIR 14
|
|
|
|
#define SSH_FXP_RMDIR 15
|
|
|
|
#define SSH_FXP_REALPATH 16
|
|
|
|
#define SSH_FXP_STAT 17
|
|
|
|
#define SSH_FXP_RENAME 18
|
|
|
|
#define SSH_FXP_READLINK 19
|
|
|
|
#define SSH_FXP_SYMLINK 20
|
|
|
|
#define SSH_FXP_STATUS 101
|
|
|
|
#define SSH_FXP_HANDLE 102
|
|
|
|
#define SSH_FXP_DATA 103
|
|
|
|
#define SSH_FXP_NAME 104
|
|
|
|
#define SSH_FXP_ATTRS 105
|
|
|
|
#define SSH_FXP_EXTENDED 200
|
2007-04-21 18:16:23 +00:00
|
|
|
#define SSH_FXP_EXTENDED_REPLY 201
|
|
|
|
|
2007-02-02 16:21:20 +00:00
|
|
|
#define LIBSSH2_SFTP_HANDLE_FILE 0
|
|
|
|
#define LIBSSH2_SFTP_HANDLE_DIR 1
|
2004-12-22 00:20:02 +00:00
|
|
|
|
2005-05-04 18:06:07 +00:00
|
|
|
/* S_IFREG */
|
2007-02-02 16:21:20 +00:00
|
|
|
#define LIBSSH2_SFTP_ATTR_PFILETYPE_FILE 0100000
|
2005-05-04 18:06:07 +00:00
|
|
|
/* S_IFDIR */
|
2007-02-02 16:21:20 +00:00
|
|
|
#define LIBSSH2_SFTP_ATTR_PFILETYPE_DIR 0040000
|
2005-05-04 18:06:07 +00:00
|
|
|
|
2010-05-22 01:48:04 -07:00
|
|
|
#define SSH_FXE_STATVFS_ST_RDONLY 0x00000001
|
|
|
|
#define SSH_FXE_STATVFS_ST_NOSUID 0x00000002
|
|
|
|
|
2009-03-27 20:20:48 +00:00
|
|
|
static int sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle);
|
|
|
|
|
2010-04-28 00:23:09 +02:00
|
|
|
/* sftp_attrsize
|
|
|
|
* Size that attr with this flagset will occupy when turned into a bin struct
|
|
|
|
*/
|
2010-04-28 17:38:00 +01:00
|
|
|
static int sftp_attrsize(unsigned long flags)
|
|
|
|
{
|
|
|
|
return (4 + /* flags(4) */
|
2010-04-28 17:41:17 +01:00
|
|
|
((flags & LIBSSH2_SFTP_ATTR_SIZE) ? 8 : 0) +
|
|
|
|
((flags & LIBSSH2_SFTP_ATTR_UIDGID) ? 8 : 0) +
|
|
|
|
((flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) ? 4 : 0) +
|
|
|
|
((flags & LIBSSH2_SFTP_ATTR_ACMODTIME) ? 8 : 0));
|
2010-04-28 17:38:00 +01:00
|
|
|
/* atime + mtime as u32 */
|
|
|
|
}
|
2010-04-28 00:23:09 +02:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
/* _libssh2_store_u64
|
2009-03-27 07:03:00 +00:00
|
|
|
*/
|
2010-04-17 13:18:15 +02:00
|
|
|
static void _libssh2_store_u64(unsigned char **ptr, libssh2_uint64_t value)
|
2009-03-27 07:03:00 +00:00
|
|
|
{
|
2010-04-17 13:34:44 +02:00
|
|
|
uint32_t msl = (uint32_t)(value >> 32);
|
2010-04-17 13:18:15 +02:00
|
|
|
unsigned char *buf = *ptr;
|
2009-03-27 07:03:00 +00:00
|
|
|
|
2009-07-07 13:44:17 +02:00
|
|
|
buf[0] = (unsigned char)((msl >> 24) & 0xFF);
|
|
|
|
buf[1] = (unsigned char)((msl >> 16) & 0xFF);
|
|
|
|
buf[2] = (unsigned char)((msl >> 8) & 0xFF);
|
|
|
|
buf[3] = (unsigned char)( msl & 0xFF);
|
2009-03-27 07:03:00 +00:00
|
|
|
|
2009-07-07 13:44:17 +02:00
|
|
|
buf[4] = (unsigned char)((value >> 24) & 0xFF);
|
|
|
|
buf[5] = (unsigned char)((value >> 16) & 0xFF);
|
|
|
|
buf[6] = (unsigned char)((value >> 8) & 0xFF);
|
|
|
|
buf[7] = (unsigned char)( value & 0xFF);
|
2010-04-17 13:18:15 +02:00
|
|
|
|
|
|
|
*ptr += 8;
|
2009-03-27 07:03:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
/*
|
|
|
|
* sftp_packet_add
|
|
|
|
*
|
2004-12-22 00:20:02 +00:00
|
|
|
* Add a packet to the SFTP packet brigade
|
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
static int
|
2009-03-26 15:41:14 +00:00
|
|
|
sftp_packet_add(LIBSSH2_SFTP *sftp, unsigned char *data,
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_SESSION *session = sftp->channel->session;
|
|
|
|
LIBSSH2_PACKET *packet;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Received packet %d (len %d)",
|
2008-12-16 15:35:33 +00:00
|
|
|
(int) data[0], data_len);
|
2007-05-28 17:56:08 +00:00
|
|
|
packet = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PACKET));
|
|
|
|
if (!packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate datablock for SFTP packet");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
|
|
|
memset(packet, 0, sizeof(LIBSSH2_PACKET));
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
packet->data = data;
|
|
|
|
packet->data_len = data_len;
|
|
|
|
packet->data_head = 5;
|
2009-08-20 00:56:05 +02:00
|
|
|
|
|
|
|
_libssh2_list_add(&sftp->packets, &packet->node);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return 0;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
/*
|
|
|
|
* sftp_packet_read
|
|
|
|
*
|
2004-12-22 00:20:02 +00:00
|
|
|
* Frame an SFTP packet off the channel
|
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
static int
|
2009-03-26 15:41:14 +00:00
|
|
|
sftp_packet_read(LIBSSH2_SFTP *sftp)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-04-21 18:16:23 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned char buffer[4]; /* To store the packet length */
|
2007-04-21 18:16:23 +00:00
|
|
|
unsigned char *packet;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t packet_len, packet_received;
|
2007-06-07 16:01:12 +00:00
|
|
|
ssize_t bytes_received;
|
2007-04-21 18:16:23 +00:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "recv packet");
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
/* If there was a previous partial, start using it */
|
|
|
|
if (sftp->partial_packet) {
|
2008-12-16 15:35:33 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
packet = sftp->partial_packet;
|
|
|
|
packet_len = sftp->partial_len;
|
|
|
|
packet_received = sftp->partial_received;
|
|
|
|
sftp->partial_packet = NULL;
|
2008-12-16 15:35:33 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2008-12-16 15:35:33 +00:00
|
|
|
"partial read cont, len: %lu", packet_len);
|
2008-12-16 12:31:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-08-30 17:00:49 +02:00
|
|
|
rc = _libssh2_channel_read(channel, 0, (char *) buffer, 4);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2008-12-16 15:35:33 +00:00
|
|
|
}
|
|
|
|
else if (4 != rc) {
|
|
|
|
/* TODO: this is stupid since we can in fact get 1-3 bytes in a
|
|
|
|
legitimate working case as well if the connection happens to be
|
|
|
|
super slow or something */
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
|
|
|
|
"Read part of packet");
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
packet_len = _libssh2_ntohu32(buffer);
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-02-02 16:21:20 +00:00
|
|
|
"Data begin - Packet Length: %lu", packet_len);
|
|
|
|
if (packet_len > LIBSSH2_SFTP_PACKET_MAXLEN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
|
|
|
|
"SFTP packet too large");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-02-02 16:21:20 +00:00
|
|
|
packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate SFTP packet");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-02-02 16:21:20 +00:00
|
|
|
packet_received = 0;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
/* Read as much of the packet as we can */
|
|
|
|
while (packet_len > packet_received) {
|
2007-08-06 20:48:04 +00:00
|
|
|
bytes_received =
|
2009-08-30 17:00:49 +02:00
|
|
|
_libssh2_channel_read(channel, 0,
|
|
|
|
(char *) packet + packet_received,
|
|
|
|
packet_len - packet_received);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
if (bytes_received == LIBSSH2_ERROR_EAGAIN) {
|
2007-04-21 18:16:23 +00:00
|
|
|
/*
|
2007-08-06 20:48:04 +00:00
|
|
|
* We received EAGAIN, save what we have and
|
2007-04-21 18:16:23 +00:00
|
|
|
* return to EAGAIN to the caller
|
|
|
|
*/
|
|
|
|
sftp->partial_packet = packet;
|
|
|
|
sftp->partial_len = packet_len;
|
|
|
|
sftp->partial_received = packet_received;
|
|
|
|
packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-08-25 00:54:47 +02:00
|
|
|
return bytes_received;
|
2008-12-16 12:31:58 +00:00
|
|
|
}
|
|
|
|
else if (bytes_received < 0) {
|
2007-04-21 18:16:23 +00:00
|
|
|
LIBSSH2_FREE(session, packet);
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, bytes_received,
|
|
|
|
"Receive error waiting for SFTP packet");
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
|
|
|
packet_received += bytes_received;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-08-25 00:23:34 +02:00
|
|
|
rc = sftp_packet_add(sftp, packet, packet_len);
|
|
|
|
if (rc) {
|
2007-04-21 18:16:23 +00:00
|
|
|
LIBSSH2_FREE(session, packet);
|
2009-08-25 00:23:34 +02:00
|
|
|
return rc;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
return packet[0];
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
/*
|
|
|
|
* sftp_packet_ask()
|
|
|
|
*
|
|
|
|
* Checks if there's a matching SFTP packet available.
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
static int
|
2009-03-26 15:41:14 +00:00
|
|
|
sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type,
|
2010-04-15 20:36:31 +02:00
|
|
|
int request_id, unsigned char **data,
|
|
|
|
size_t *data_len)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-04-21 18:16:23 +00:00
|
|
|
LIBSSH2_SESSION *session = sftp->channel->session;
|
2009-08-20 00:56:05 +02:00
|
|
|
LIBSSH2_PACKET *packet = _libssh2_list_first(&sftp->packets);
|
2007-04-21 18:16:23 +00:00
|
|
|
unsigned char match_buf[5];
|
2009-03-13 22:15:27 +00:00
|
|
|
int match_len;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Asking for %d packet",
|
2007-08-06 20:48:04 +00:00
|
|
|
(int) packet_type);
|
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
match_buf[0] = packet_type;
|
|
|
|
if (packet_type == SSH_FXP_VERSION) {
|
|
|
|
/* Special consideration when matching VERSION packet */
|
|
|
|
match_len = 1;
|
|
|
|
} else {
|
2009-03-13 22:15:27 +00:00
|
|
|
match_len = 5;
|
2009-03-17 13:48:35 +00:00
|
|
|
_libssh2_htonu32(match_buf + 1, request_id);
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
while (packet) {
|
2009-03-13 22:15:27 +00:00
|
|
|
if (!memcmp((char *) packet->data, (char *) match_buf, match_len)) {
|
|
|
|
|
|
|
|
/* Match! Fetch the data */
|
2007-04-21 18:16:23 +00:00
|
|
|
*data = packet->data;
|
|
|
|
*data_len = packet->data_len;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
/* unlink and free this struct */
|
2009-08-20 00:56:05 +02:00
|
|
|
_libssh2_list_remove(&packet->node);
|
2007-04-21 18:16:23 +00:00
|
|
|
LIBSSH2_FREE(session, packet);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-13 22:15:27 +00:00
|
|
|
/* check next struct in the list */
|
2009-08-20 00:56:05 +02:00
|
|
|
packet = _libssh2_list_next(&packet->node);
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
|
|
|
return -1;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* sftp_packet_require
|
2004-12-22 00:20:02 +00:00
|
|
|
* A la libssh2_packet_require
|
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
static int
|
2009-03-26 15:41:14 +00:00
|
|
|
sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type,
|
2010-04-15 20:36:31 +02:00
|
|
|
int request_id, unsigned char **data,
|
|
|
|
size_t *data_len)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_SESSION *session = sftp->channel->session;
|
2010-11-01 09:33:36 +01:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Requiring packet %d id %ld",
|
2009-03-13 22:15:27 +00:00
|
|
|
(int) packet_type, request_id);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
if (sftp_packet_ask(sftp, packet_type, request_id, data, data_len) == 0) {
|
2007-05-28 17:56:08 +00:00
|
|
|
/* The right packet was available in the packet brigade */
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Got %d",
|
2009-03-13 22:15:27 +00:00
|
|
|
(int) packet_type);
|
2010-11-01 09:33:36 +01:00
|
|
|
return LIBSSH2_ERROR_NONE;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
|
2010-11-01 09:33:36 +01:00
|
|
|
rc = sftp_packet_read(sftp);
|
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN)
|
|
|
|
return rc;
|
|
|
|
else if (rc <= 0)
|
|
|
|
/* TODO: isn't this supposed to be < 0 only? */
|
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
/* data was read, check the queue again */
|
|
|
|
if (!sftp_packet_ask(sftp, packet_type, request_id, data, data_len)) {
|
|
|
|
/* The right packet was available in the packet brigade */
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Got %d",
|
2009-03-13 22:15:27 +00:00
|
|
|
(int) packet_type);
|
2010-11-01 09:33:36 +01:00
|
|
|
return LIBSSH2_ERROR_NONE;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
/* Only reached if the socket died */
|
2009-08-25 00:23:34 +02:00
|
|
|
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* sftp_packet_requirev
|
2008-12-16 15:35:33 +00:00
|
|
|
* Require one of N possible reponses
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
static int
|
2009-03-26 15:41:14 +00:00
|
|
|
sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
|
2008-12-15 22:58:07 +00:00
|
|
|
const unsigned char *valid_responses,
|
2010-04-15 20:36:31 +02:00
|
|
|
int request_id, unsigned char **data,
|
|
|
|
size_t *data_len)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-04-21 18:16:23 +00:00
|
|
|
int i;
|
2010-11-01 09:33:36 +01:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2008-12-15 22:58:07 +00:00
|
|
|
/* If no timeout is active, start a new one */
|
2010-11-01 09:33:36 +01:00
|
|
|
if (sftp->requirev_start == 0)
|
2007-04-21 18:16:23 +00:00
|
|
|
sftp->requirev_start = time(NULL);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
while (sftp->channel->session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
|
|
|
|
for(i = 0; i < num_valid_responses; i++) {
|
2008-12-16 15:35:33 +00:00
|
|
|
if (sftp_packet_ask(sftp, valid_responses[i], request_id,
|
2009-03-13 22:15:27 +00:00
|
|
|
data, data_len) == 0) {
|
2007-04-21 18:16:23 +00:00
|
|
|
/*
|
2007-08-06 20:48:04 +00:00
|
|
|
* Set to zero before all returns to say
|
2007-04-21 18:16:23 +00:00
|
|
|
* the timeout is not active
|
|
|
|
*/
|
|
|
|
sftp->requirev_start = 0;
|
2010-11-01 09:33:36 +01:00
|
|
|
return LIBSSH2_ERROR_NONE;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-11-01 09:33:36 +01:00
|
|
|
rc = sftp_packet_read(sftp);
|
|
|
|
if ((rc < 0) && (rc != LIBSSH2_ERROR_EAGAIN)) {
|
2007-04-21 18:16:23 +00:00
|
|
|
sftp->requirev_start = 0;
|
2010-11-01 09:33:36 +01:00
|
|
|
return rc;
|
|
|
|
} else if (rc <= 0) {
|
2007-04-21 18:16:23 +00:00
|
|
|
/* prevent busy-looping */
|
2007-08-06 20:48:04 +00:00
|
|
|
long left =
|
2009-07-07 13:44:17 +02:00
|
|
|
LIBSSH2_READ_TIMEOUT - (long)(time(NULL) - sftp->requirev_start);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-18 22:38:32 +00:00
|
|
|
if (left <= 0) {
|
|
|
|
sftp->requirev_start = 0;
|
2010-04-17 13:18:15 +02:00
|
|
|
return LIBSSH2_ERROR_TIMEOUT;
|
2009-03-26 15:41:14 +00:00
|
|
|
}
|
2010-11-01 09:33:36 +01:00
|
|
|
else if (rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
return rc;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
sftp->requirev_start = 0;
|
2010-11-01 09:33:36 +01:00
|
|
|
|
|
|
|
/* Only reached if the socket died */
|
|
|
|
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* sftp_attr2bin
|
2004-12-22 00:20:02 +00:00
|
|
|
* Populate attributes into an SFTP block
|
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
static int
|
2009-03-23 13:35:20 +00:00
|
|
|
sftp_attr2bin(unsigned char *p, const LIBSSH2_SFTP_ATTRIBUTES * attrs)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
unsigned char *s = p;
|
2010-04-15 20:36:31 +02:00
|
|
|
uint32_t flag_mask =
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP_ATTR_SIZE | LIBSSH2_SFTP_ATTR_UIDGID |
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_SFTP_ATTR_PERMISSIONS | LIBSSH2_SFTP_ATTR_ACMODTIME;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* TODO: When we add SFTP4+ functionality flag_mask can get additional
|
|
|
|
bits */
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (!attrs) {
|
2009-03-17 13:48:35 +00:00
|
|
|
_libssh2_htonu32(s, 0);
|
2007-05-28 17:56:08 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, attrs->flags & flag_mask);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u64(&s, attrs->filesize);
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, attrs->uid);
|
|
|
|
_libssh2_store_u32(&s, attrs->gid);
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, attrs->permissions);
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, attrs->atime);
|
|
|
|
_libssh2_store_u32(&s, attrs->mtime);
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return (s - p);
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* sftp_bin2attr
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
static int
|
2009-03-23 13:35:20 +00:00
|
|
|
sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES * attrs, const unsigned char *p)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
const unsigned char *s = p;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
|
2009-03-17 13:48:35 +00:00
|
|
|
attrs->flags = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
|
2009-03-17 13:48:35 +00:00
|
|
|
attrs->filesize = _libssh2_ntohu64(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 8;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
|
2009-03-17 13:48:35 +00:00
|
|
|
attrs->uid = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
2009-03-17 13:48:35 +00:00
|
|
|
attrs->gid = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
|
2009-03-17 13:48:35 +00:00
|
|
|
attrs->permissions = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
|
2009-03-17 13:48:35 +00:00
|
|
|
attrs->atime = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
2009-03-17 13:48:35 +00:00
|
|
|
attrs->mtime = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return (s - p);
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2004-12-22 00:20:02 +00:00
|
|
|
/* ************
|
2010-04-16 00:18:51 +02:00
|
|
|
* SFTP API *
|
|
|
|
************ */
|
2004-12-22 00:20:02 +00:00
|
|
|
|
2007-02-02 16:21:20 +00:00
|
|
|
LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor);
|
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* libssh2_sftp_dtor
|
2004-12-22 00:20:02 +00:00
|
|
|
* Shutdown an SFTP stream when the channel closes
|
|
|
|
*/
|
2007-02-02 16:21:20 +00:00
|
|
|
LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor)
|
|
|
|
{
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP *sftp = (LIBSSH2_SFTP *) (*channel_abstract);
|
|
|
|
|
|
|
|
(void) session_abstract;
|
|
|
|
(void) channel;
|
|
|
|
|
2008-12-16 12:31:58 +00:00
|
|
|
/* Free the partial packet storage for sftp_packet_read */
|
2007-04-21 18:16:23 +00:00
|
|
|
if (sftp->partial_packet) {
|
|
|
|
LIBSSH2_FREE(session, sftp->partial_packet);
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
/* Free the packet storage for _libssh2_sftp_packet_readdir */
|
|
|
|
if (sftp->readdir_packet) {
|
|
|
|
LIBSSH2_FREE(session, sftp->readdir_packet);
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
LIBSSH2_FREE(session, sftp);
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
/*
|
2009-03-26 15:41:14 +00:00
|
|
|
* sftp_init
|
2009-03-13 22:15:27 +00:00
|
|
|
*
|
2004-12-22 00:20:02 +00:00
|
|
|
* Startup an SFTP session
|
|
|
|
*/
|
2009-03-26 15:41:14 +00:00
|
|
|
static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-06-06 12:34:06 +00:00
|
|
|
unsigned char *data, *s;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
2007-06-06 12:34:06 +00:00
|
|
|
int rc;
|
2009-05-25 10:50:49 +02:00
|
|
|
LIBSSH2_SFTP *sftp_handle;
|
2007-06-12 18:27:37 +00:00
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
if (session->sftpInit_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Initializing SFTP subsystem");
|
2007-06-12 18:27:37 +00:00
|
|
|
|
2009-05-25 10:50:49 +02:00
|
|
|
/*
|
|
|
|
* The 'sftpInit_sftp' and 'sftpInit_channel' struct fields within the
|
2009-05-25 10:58:13 +02:00
|
|
|
* session struct are only to be used during the setup phase. As soon
|
2009-05-25 10:50:49 +02:00
|
|
|
* as the SFTP session is created they are cleared and can thus be
|
|
|
|
* re-used again to allow any amount of SFTP handles per sessions.
|
|
|
|
*
|
2009-08-19 13:58:15 +02:00
|
|
|
* Note that you MUST NOT try to call libssh2_sftp_init() again to get
|
|
|
|
* another handle until the previous call has finished and either
|
2009-05-25 10:50:49 +02:00
|
|
|
* succesffully made a handle or failed and returned error (not
|
|
|
|
* including *EAGAIN).
|
|
|
|
*/
|
2009-03-27 22:24:09 +00:00
|
|
|
|
2009-05-25 10:50:49 +02:00
|
|
|
assert(session->sftpInit_sftp == NULL);
|
2007-06-12 18:27:37 +00:00
|
|
|
session->sftpInit_sftp = NULL;
|
2007-06-06 12:34:06 +00:00
|
|
|
session->sftpInit_state = libssh2_NB_state_created;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-05-25 10:50:49 +02:00
|
|
|
sftp_handle = session->sftpInit_sftp;
|
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
if (session->sftpInit_state == libssh2_NB_state_created) {
|
2007-08-06 20:48:04 +00:00
|
|
|
session->sftpInit_channel =
|
2009-03-29 22:09:29 +00:00
|
|
|
_libssh2_channel_open(session, "session", sizeof("session") - 1,
|
|
|
|
LIBSSH2_CHANNEL_WINDOW_DEFAULT,
|
|
|
|
LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0);
|
2007-06-06 12:34:06 +00:00
|
|
|
if (!session->sftpInit_channel) {
|
|
|
|
if (libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block starting up channel");
|
2009-03-27 22:24:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
|
|
|
|
"Unable to startup channel");
|
2007-06-06 12:34:06 +00:00
|
|
|
session->sftpInit_state = libssh2_NB_state_idle;
|
|
|
|
}
|
2009-03-27 22:24:09 +00:00
|
|
|
return NULL;
|
2007-06-06 12:34:06 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
session->sftpInit_state = libssh2_NB_state_sent;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
if (session->sftpInit_state == libssh2_NB_state_sent) {
|
2009-03-29 22:09:29 +00:00
|
|
|
rc = _libssh2_channel_process_startup(session->sftpInit_channel,
|
|
|
|
"subsystem",
|
|
|
|
sizeof("subsystem") - 1, "sftp",
|
|
|
|
strlen("sftp"));
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block to request SFTP subsystem");
|
2007-06-06 12:34:06 +00:00
|
|
|
return NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
|
|
|
|
"Unable to request SFTP subsystem");
|
2007-06-12 18:27:37 +00:00
|
|
|
goto sftp_init_error;
|
2007-06-06 12:34:06 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
session->sftpInit_state = libssh2_NB_state_sent1;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
if (session->sftpInit_state == libssh2_NB_state_sent1) {
|
2009-03-27 22:24:09 +00:00
|
|
|
rc = _libssh2_channel_extended_data(session->sftpInit_channel,
|
|
|
|
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block requesting handle extended data");
|
2007-06-06 12:34:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-05-25 10:50:49 +02:00
|
|
|
sftp_handle =
|
|
|
|
session->sftpInit_sftp =
|
|
|
|
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP));
|
|
|
|
if (!sftp_handle) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate a new SFTP structure");
|
2007-06-12 18:27:37 +00:00
|
|
|
goto sftp_init_error;
|
2007-06-06 12:34:06 +00:00
|
|
|
}
|
2009-05-25 10:50:49 +02:00
|
|
|
memset(sftp_handle, 0, sizeof(LIBSSH2_SFTP));
|
|
|
|
sftp_handle->channel = session->sftpInit_channel;
|
|
|
|
sftp_handle->request_id = 0;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
_libssh2_htonu32(session->sftpInit_buffer, 5);
|
2007-06-06 12:34:06 +00:00
|
|
|
session->sftpInit_buffer[4] = SSH_FXP_INIT;
|
2009-03-17 13:48:35 +00:00
|
|
|
_libssh2_htonu32(session->sftpInit_buffer + 5, LIBSSH2_SFTP_VERSION);
|
2010-04-01 13:20:55 +02:00
|
|
|
session->sftpInit_sent = 0; /* nothing's sent yet */
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Sending FXP_INIT packet advertising version %d support",
|
|
|
|
(int) LIBSSH2_SFTP_VERSION);
|
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
session->sftpInit_state = libssh2_NB_state_sent2;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
if (session->sftpInit_state == libssh2_NB_state_sent2) {
|
2010-04-01 13:20:55 +02:00
|
|
|
/* sent off what's left of the init buffer to send */
|
2009-03-29 22:09:29 +00:00
|
|
|
rc = _libssh2_channel_write(session->sftpInit_channel, 0,
|
2010-10-23 00:11:59 +02:00
|
|
|
session->sftpInit_buffer +
|
2010-04-01 13:20:55 +02:00
|
|
|
session->sftpInit_sent,
|
|
|
|
9 - session->sftpInit_sent);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block sending SSH_FXP_INIT");
|
2007-06-06 12:34:06 +00:00
|
|
|
return NULL;
|
2010-04-01 13:20:55 +02:00
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"Unable to send SSH_FXP_INIT");
|
2007-06-12 18:27:37 +00:00
|
|
|
goto sftp_init_error;
|
2007-06-06 12:34:06 +00:00
|
|
|
}
|
2010-04-01 13:20:55 +02:00
|
|
|
else {
|
|
|
|
/* add up the number of bytes sent */
|
|
|
|
session->sftpInit_sent += rc;
|
|
|
|
|
|
|
|
if(session->sftpInit_sent == 9)
|
|
|
|
/* move on */
|
|
|
|
session->sftpInit_state = libssh2_NB_state_sent3;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-01 13:20:55 +02:00
|
|
|
/* if less than 9, we remain in this state to send more later on */
|
|
|
|
}
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-05-25 10:50:49 +02:00
|
|
|
rc = sftp_packet_require(sftp_handle, SSH_FXP_VERSION,
|
2008-12-16 15:35:33 +00:00
|
|
|
0, &data, &data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block waiting for response from SFTP subsystem");
|
2007-06-06 12:34:06 +00:00
|
|
|
return NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for response from SFTP subsystem");
|
2007-06-12 18:27:37 +00:00
|
|
|
goto sftp_init_error;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
|
|
|
if (data_len < 5) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"Invalid SSH_FXP_VERSION response");
|
2007-06-12 18:27:37 +00:00
|
|
|
goto sftp_init_error;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
s = data + 1;
|
2009-05-25 10:50:49 +02:00
|
|
|
sftp_handle->version = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
2009-05-25 10:50:49 +02:00
|
|
|
if (sftp_handle->version > LIBSSH2_SFTP_VERSION) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Truncating remote SFTP version from %lu",
|
2009-05-25 10:50:49 +02:00
|
|
|
sftp_handle->version);
|
|
|
|
sftp_handle->version = LIBSSH2_SFTP_VERSION;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Enabling SFTP version %lu compatability",
|
2009-05-25 10:50:49 +02:00
|
|
|
sftp_handle->version);
|
2007-04-21 18:16:23 +00:00
|
|
|
while (s < (data + data_len)) {
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t extname_len, extdata_len;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
extname_len = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
2010-04-17 19:05:53 +02:00
|
|
|
/* the extension name starts here */
|
2007-08-06 20:48:04 +00:00
|
|
|
s += extname_len;
|
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
extdata_len = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
s += 4;
|
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
/* TODO: Actually process extensions */
|
2010-04-17 19:05:53 +02:00
|
|
|
s += extdata_len;
|
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-27 20:20:48 +00:00
|
|
|
/* Make sure that when the channel gets closed, the SFTP service is shut
|
|
|
|
down too */
|
2009-05-25 10:50:49 +02:00
|
|
|
sftp_handle->channel->abstract = sftp_handle;
|
|
|
|
sftp_handle->channel->close_cb = libssh2_sftp_dtor;
|
2007-04-21 18:16:23 +00:00
|
|
|
|
2007-06-06 12:34:06 +00:00
|
|
|
session->sftpInit_state = libssh2_NB_state_idle;
|
2009-05-25 10:50:49 +02:00
|
|
|
|
|
|
|
/* clear the sftp and channel pointers in this session struct now */
|
|
|
|
session->sftpInit_sftp = NULL;
|
|
|
|
session->sftpInit_channel = NULL;
|
|
|
|
|
2009-08-19 13:58:15 +02:00
|
|
|
_libssh2_list_init(&sftp_handle->sftp_handles);
|
|
|
|
|
2009-05-25 10:50:49 +02:00
|
|
|
return sftp_handle;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
|
|
|
sftp_init_error:
|
2010-06-11 13:05:55 +02:00
|
|
|
while (_libssh2_channel_free(session->sftpInit_channel) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN);
|
2007-06-12 18:27:37 +00:00
|
|
|
session->sftpInit_channel = NULL;
|
|
|
|
if (session->sftpInit_sftp) {
|
|
|
|
LIBSSH2_FREE(session, session->sftpInit_sftp);
|
|
|
|
session->sftpInit_sftp = NULL;
|
|
|
|
}
|
|
|
|
session->sftpInit_state = libssh2_NB_state_idle;
|
|
|
|
return NULL;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/*
|
|
|
|
* libssh2_sftp_init
|
|
|
|
*
|
|
|
|
* Startup an SFTP session
|
|
|
|
*/
|
|
|
|
LIBSSH2_API LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session)
|
|
|
|
{
|
|
|
|
LIBSSH2_SFTP *ptr;
|
2010-06-07 13:10:51 +02:00
|
|
|
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!session)
|
|
|
|
return NULL;
|
|
|
|
|
2010-06-07 13:10:51 +02:00
|
|
|
if(!(session->state & LIBSSH2_STATE_AUTHENTICATED)) {
|
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_INVAL,
|
|
|
|
"session not authenticated yet");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST_ERRNO(ptr, session, sftp_init(session));
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2009-03-27 20:20:48 +00:00
|
|
|
/*
|
|
|
|
* sftp_shutdown
|
|
|
|
*
|
2004-12-22 00:20:02 +00:00
|
|
|
* Shutsdown the SFTP subsystem
|
|
|
|
*/
|
2009-03-27 20:20:48 +00:00
|
|
|
static int
|
|
|
|
sftp_shutdown(LIBSSH2_SFTP *sftp)
|
2007-02-02 16:21:20 +00:00
|
|
|
{
|
2009-03-27 20:20:48 +00:00
|
|
|
int rc;
|
|
|
|
LIBSSH2_SESSION *session = sftp->channel->session;
|
2007-06-07 16:43:20 +00:00
|
|
|
/*
|
|
|
|
* Make sure all memory used in the state variables are free
|
|
|
|
*/
|
|
|
|
if (sftp->partial_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->partial_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->partial_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->open_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->open_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->open_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->readdir_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->readdir_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->readdir_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->write_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->write_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->write_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->fstat_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->fstat_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->fstat_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->unlink_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->unlink_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->unlink_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->rename_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->rename_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->rename_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
2010-05-22 01:48:04 -07:00
|
|
|
if (sftp->fstatvfs_packet) {
|
|
|
|
LIBSSH2_FREE(session, sftp->fstatvfs_packet);
|
|
|
|
sftp->fstatvfs_packet = NULL;
|
|
|
|
}
|
|
|
|
if (sftp->statvfs_packet) {
|
|
|
|
LIBSSH2_FREE(session, sftp->statvfs_packet);
|
|
|
|
sftp->statvfs_packet = NULL;
|
|
|
|
}
|
2007-06-07 16:43:20 +00:00
|
|
|
if (sftp->mkdir_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->mkdir_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->mkdir_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->rmdir_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->rmdir_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->rmdir_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->stat_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->stat_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->stat_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
|
|
|
if (sftp->symlink_packet) {
|
2009-03-27 20:20:48 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->symlink_packet);
|
2007-06-12 18:27:37 +00:00
|
|
|
sftp->symlink_packet = NULL;
|
2007-06-07 16:43:20 +00:00
|
|
|
}
|
2009-05-24 23:44:52 +02:00
|
|
|
|
2009-08-19 13:58:15 +02:00
|
|
|
/* TODO: We should consider walking over the sftp_handles list and kill
|
|
|
|
* any remaining sftp handles ... */
|
|
|
|
|
2009-03-27 20:20:48 +00:00
|
|
|
rc = _libssh2_channel_free(sftp->channel);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* libssh2_sftp_shutdown
|
|
|
|
* Shutsdown the SFTP subsystem
|
|
|
|
*/
|
|
|
|
LIBSSH2_API int
|
|
|
|
libssh2_sftp_shutdown(LIBSSH2_SFTP *sftp)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-08-30 17:00:49 +02:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session, sftp_shutdown(sftp));
|
2009-03-27 20:20:48 +00:00
|
|
|
return rc;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2004-12-22 00:20:02 +00:00
|
|
|
/* *******************************
|
2010-04-16 00:18:51 +02:00
|
|
|
* SFTP File and Directory Ops *
|
|
|
|
******************************* */
|
2004-12-22 00:20:02 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* sftp_open
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2009-03-26 15:41:14 +00:00
|
|
|
static LIBSSH2_SFTP_HANDLE *
|
|
|
|
sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t filename_len, uint32_t flags, long mode,
|
2009-03-26 15:41:14 +00:00
|
|
|
int open_type)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
|
|
|
LIBSSH2_SFTP_HANDLE *fp;
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs = {
|
|
|
|
LIBSSH2_SFTP_ATTR_PERMISSIONS, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
2010-04-27 23:59:55 +02:00
|
|
|
unsigned char *s;
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2010-04-27 23:59:55 +02:00
|
|
|
int open_file = (open_type == LIBSSH2_SFTP_OPENFILE)?1:0;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->open_state == libssh2_NB_state_idle) {
|
2008-12-16 15:35:33 +00:00
|
|
|
/* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) +
|
|
|
|
flags(4) */
|
2007-08-06 20:48:04 +00:00
|
|
|
sftp->open_packet_len = filename_len + 13 +
|
2010-04-28 00:23:09 +02:00
|
|
|
(open_file? (4 + sftp_attrsize(LIBSSH2_SFTP_ATTR_PERMISSIONS)) : 0);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-28 00:12:47 +02:00
|
|
|
/* surprise! this starts out with nothing sent */
|
|
|
|
sftp->open_packet_sent = 0;
|
2007-06-07 16:01:12 +00:00
|
|
|
s = sftp->open_packet = LIBSSH2_ALLOC(session, sftp->open_packet_len);
|
|
|
|
if (!sftp->open_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_OPEN or "
|
|
|
|
"FXP_OPENDIR packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Filetype in SFTP 3 and earlier */
|
2007-08-06 20:48:04 +00:00
|
|
|
attrs.permissions = mode |
|
2010-04-27 23:59:55 +02:00
|
|
|
(open_file ? LIBSSH2_SFTP_ATTR_PFILETYPE_FILE :
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP_ATTR_PFILETYPE_DIR);
|
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->open_packet_len - 4);
|
2010-04-27 23:59:55 +02:00
|
|
|
*(s++) = open_file? SSH_FXP_OPEN : SSH_FXP_OPENDIR;
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->open_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->open_request_id);
|
|
|
|
_libssh2_store_str(&s, filename, filename_len);
|
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
if (open_file) {
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, flags);
|
2009-03-27 21:52:49 +00:00
|
|
|
s += sftp_attr2bin(s, &attrs);
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Sending %s open request",
|
2010-04-27 23:59:55 +02:00
|
|
|
open_file? "file" : "directory");
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->open_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->open_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, sftp->open_packet+
|
2010-04-28 00:12:47 +02:00
|
|
|
sftp->open_packet_sent,
|
|
|
|
sftp->open_packet_len -
|
|
|
|
sftp->open_packet_sent);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block sending FXP_OPEN or FXP_OPENDIR command");
|
2007-06-07 16:01:12 +00:00
|
|
|
return NULL;
|
2008-12-16 15:35:33 +00:00
|
|
|
}
|
2010-04-28 00:12:47 +02:00
|
|
|
else if(rc < 0) {
|
|
|
|
_libssh2_error(session, rc, "Unable to send FXP_OPEN*");
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->open_packet);
|
|
|
|
sftp->open_packet = NULL;
|
|
|
|
sftp->open_state = libssh2_NB_state_idle;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-28 00:12:47 +02:00
|
|
|
/* bump the sent counter and remain in this state until the whole
|
|
|
|
data is off */
|
|
|
|
sftp->open_packet_sent += rc;
|
|
|
|
|
|
|
|
if(sftp->open_packet_len == sftp->open_packet_sent) {
|
|
|
|
LIBSSH2_FREE(session, sftp->open_packet);
|
|
|
|
sftp->open_packet = NULL;
|
|
|
|
|
|
|
|
sftp->open_state = libssh2_NB_state_sent;
|
|
|
|
}
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->open_state == libssh2_NB_state_sent) {
|
2010-04-27 23:59:55 +02:00
|
|
|
size_t data_len;
|
|
|
|
unsigned char *data;
|
|
|
|
static const unsigned char fopen_responses[2] =
|
|
|
|
{ SSH_FXP_HANDLE, SSH_FXP_STATUS };
|
2008-12-15 22:58:07 +00:00
|
|
|
rc = sftp_packet_requirev(sftp, 2, fopen_responses,
|
|
|
|
sftp->open_request_id, &data,
|
|
|
|
&data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block waiting for status message");
|
2007-06-07 16:01:12 +00:00
|
|
|
return NULL;
|
2008-12-16 15:35:33 +00:00
|
|
|
}
|
2010-04-27 23:59:55 +02:00
|
|
|
sftp->open_state = libssh2_NB_state_idle;
|
|
|
|
if (rc) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, rc, "Timeout waiting for status message");
|
2007-06-07 16:01:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
/* OPEN can basically get STATUS or HANDLE back, where HANDLE implies
|
|
|
|
a fine response while STATUS means error. It seems though that at
|
|
|
|
times we get an SSH_FX_OK back in a STATUS, followed the "real"
|
|
|
|
HANDLE so we need to properly deal with that. */
|
|
|
|
if (data[0] == SSH_FXP_STATUS) {
|
|
|
|
int badness = 1;
|
|
|
|
|
|
|
|
if(data_len < 9) {
|
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"Too small FXP_STATUS");
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-16 15:35:33 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
sftp->last_errno = _libssh2_ntohu32(data + 5);
|
|
|
|
|
|
|
|
if(LIBSSH2_FX_OK == sftp->last_errno) {
|
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "got HANDLE FXOK!");
|
|
|
|
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
|
|
|
|
/* silly situation, but check for a HANDLE */
|
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_HANDLE,
|
|
|
|
sftp->open_request_id, &data,
|
|
|
|
&data_len);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
/* go back to sent state and wait for something else */
|
|
|
|
sftp->open_state = libssh2_NB_state_sent;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if(!rc)
|
|
|
|
/* we got the handle so this is not a bad situation */
|
|
|
|
badness = 0;
|
|
|
|
}
|
2008-12-16 15:35:33 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
if(badness) {
|
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"Failed opening remote file");
|
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "got FXP_STATUS %d",
|
|
|
|
sftp->last_errno);
|
|
|
|
LIBSSH2_FREE(session, data);
|
2008-12-16 15:35:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
if(data_len < 10) {
|
2010-04-16 00:18:51 +02:00
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
2010-04-27 23:59:55 +02:00
|
|
|
"Too small FXP_HANDLE");
|
2008-12-16 15:35:33 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
fp = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE));
|
|
|
|
if (!fp) {
|
|
|
|
_libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate new SFTP handle structure");
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(fp, 0, sizeof(LIBSSH2_SFTP_HANDLE));
|
|
|
|
fp->handle_type = open_file ? LIBSSH2_SFTP_HANDLE_FILE :
|
|
|
|
LIBSSH2_SFTP_HANDLE_DIR;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
fp->handle_len = _libssh2_ntohu32(data + 5);
|
|
|
|
if (fp->handle_len > SFTP_HANDLE_MAXLEN)
|
|
|
|
/* SFTP doesn't allow handles longer than 256 characters */
|
|
|
|
fp->handle_len = SFTP_HANDLE_MAXLEN;
|
2008-12-17 10:45:20 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
if(fp->handle_len > (data_len - 9))
|
|
|
|
/* do not reach beyond the end of the data we got */
|
|
|
|
fp->handle_len = data_len - 9;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
memcpy(fp->handle, data + 9, fp->handle_len);
|
2009-08-19 13:58:15 +02:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
|
|
|
|
/* add this file handle to the list kept in the sftp session */
|
|
|
|
_libssh2_list_add(&sftp->sftp_handles, &fp->node);
|
|
|
|
|
|
|
|
fp->sftp = sftp; /* point to the parent struct */
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
fp->u.file.offset = 0;
|
2010-10-25 01:43:54 +02:00
|
|
|
fp->u.file.offset_sent = 0;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-27 23:59:55 +02:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Open command successful");
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_open_ex
|
|
|
|
*/
|
|
|
|
LIBSSH2_API LIBSSH2_SFTP_HANDLE *
|
|
|
|
libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, const char *filename,
|
|
|
|
unsigned int filename_len, unsigned long flags, long mode,
|
|
|
|
int open_type)
|
|
|
|
{
|
|
|
|
LIBSSH2_SFTP_HANDLE *hnd;
|
2010-06-11 13:05:55 +02:00
|
|
|
|
|
|
|
if(!sftp)
|
|
|
|
return NULL;
|
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST_ERRNO(hnd, sftp->channel->session,
|
|
|
|
sftp_open(sftp, filename, filename_len, flags, mode,
|
|
|
|
open_type));
|
|
|
|
return hnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sftp_read
|
2007-06-07 16:01:12 +00:00
|
|
|
* Read from an SFTP file handle
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2009-03-26 15:41:14 +00:00
|
|
|
static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
|
|
|
|
size_t buffer_maxlen)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP *sftp = handle->sftp;
|
2007-04-22 13:13:51 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len, request_id = 0;
|
2008-12-16 15:35:33 +00:00
|
|
|
/* 25 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) +
|
|
|
|
offset(8) + length(4) */
|
2007-04-22 13:13:51 +00:00
|
|
|
ssize_t packet_len = handle->handle_len + 25;
|
|
|
|
unsigned char *packet, *s, *data;
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
size_t bytes_requested = 0;
|
|
|
|
size_t total_read = 0;
|
|
|
|
int retcode;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
if (sftp->read_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Reading %lu bytes from SFTP handle",
|
|
|
|
(unsigned long) buffer_maxlen);
|
2008-12-22 12:38:41 +00:00
|
|
|
packet = handle->request_packet;
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->read_state = libssh2_NB_state_allocated;
|
2007-04-22 13:13:51 +00:00
|
|
|
} else {
|
|
|
|
packet = sftp->read_packet;
|
|
|
|
request_id = sftp->read_request_id;
|
|
|
|
total_read = sftp->read_total_read;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-22 13:13:51 +00:00
|
|
|
while (total_read < buffer_maxlen) {
|
|
|
|
s = packet;
|
|
|
|
/*
|
|
|
|
* If buffer_maxlen bytes will be requested, server may return all
|
|
|
|
* with one packet. But libssh2 have packet length limit.
|
|
|
|
* So we request data by pieces.
|
|
|
|
*/
|
|
|
|
bytes_requested = buffer_maxlen - total_read;
|
2008-12-16 15:35:33 +00:00
|
|
|
/* 10 = packet_type(1) + request_id(4) + data_length(4) +
|
2008-12-19 22:21:36 +00:00
|
|
|
end_of_line_flag(1)
|
|
|
|
|
|
|
|
10 is changed to 13 below simple because it seems there's a
|
|
|
|
"GlobalScape" SFTP server that responds with a slightly too big
|
|
|
|
buffer at times and we can apparently compensate for that by doing
|
|
|
|
this trick.
|
|
|
|
|
|
|
|
Further details on this issue:
|
|
|
|
|
|
|
|
https://sourceforge.net/mailarchive/forum.php?thread_name=9c3275a90811261517v6c0b1da2u918cc1b8370abf83%40mail.gmail.com&forum_name=libssh2-devel
|
|
|
|
|
|
|
|
http://forums.globalscape.com/tm.aspx?m=15249
|
|
|
|
|
|
|
|
*/
|
|
|
|
if (bytes_requested > LIBSSH2_SFTP_PACKET_MAXLEN - 13) {
|
|
|
|
bytes_requested = LIBSSH2_SFTP_PACKET_MAXLEN - 13;
|
2007-04-22 13:13:51 +00:00
|
|
|
}
|
2007-04-01 08:04:32 +00:00
|
|
|
#ifdef LIBSSH2_DEBUG_SFTP
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Requesting %lu bytes from SFTP handle",
|
|
|
|
(unsigned long) bytes_requested);
|
2007-04-01 08:04:32 +00:00
|
|
|
#endif
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
if (sftp->read_state == libssh2_NB_state_allocated) {
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
2007-04-22 13:13:51 +00:00
|
|
|
*(s++) = SSH_FXP_READ;
|
|
|
|
request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, request_id);
|
|
|
|
_libssh2_store_str(&s, handle->handle, handle->handle_len);
|
|
|
|
_libssh2_store_u64(&s, handle->u.file.offset);
|
|
|
|
_libssh2_store_u32(&s, bytes_requested);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->read_state = libssh2_NB_state_created;
|
2007-04-22 13:13:51 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->read_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
retcode = _libssh2_channel_write(channel, 0, packet, packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (retcode == LIBSSH2_ERROR_EAGAIN) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->read_packet = packet;
|
|
|
|
sftp->read_request_id = request_id;
|
|
|
|
sftp->read_total_read = total_read;
|
2009-08-25 00:54:47 +02:00
|
|
|
return retcode;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != retcode) {
|
2008-12-16 15:35:33 +00:00
|
|
|
/* TODO: a partial write is not a critical error when in
|
|
|
|
non-blocking mode! */
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->read_packet = NULL;
|
|
|
|
sftp->read_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"_libssh2_channel_write() failed");
|
2007-04-22 13:13:51 +00:00
|
|
|
}
|
|
|
|
sftp->read_packet = packet;
|
|
|
|
sftp->read_request_id = request_id;
|
|
|
|
sftp->read_total_read = total_read;
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->read_state = libssh2_NB_state_sent;
|
2007-04-22 13:13:51 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->read_state == libssh2_NB_state_sent) {
|
2010-04-28 00:39:23 +02:00
|
|
|
static const unsigned char read_responses[2] =
|
|
|
|
{ SSH_FXP_DATA, SSH_FXP_STATUS };
|
2007-08-06 20:48:04 +00:00
|
|
|
retcode =
|
2008-12-15 22:58:07 +00:00
|
|
|
sftp_packet_requirev(sftp, 2, read_responses,
|
|
|
|
request_id, &data, &data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (retcode == LIBSSH2_ERROR_EAGAIN) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, retcode,
|
|
|
|
"Would block waiting for status message");
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (retcode) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->read_packet = NULL;
|
|
|
|
sftp->read_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, retcode,
|
|
|
|
"Timeout waiting for status message");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->read_state = libssh2_NB_state_sent1;
|
2007-04-22 13:13:51 +00:00
|
|
|
}
|
2009-03-13 22:15:27 +00:00
|
|
|
else
|
|
|
|
/* internal error, 'data' is not assigned */
|
|
|
|
return -1;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-22 13:13:51 +00:00
|
|
|
switch (data[0]) {
|
2007-08-06 20:48:04 +00:00
|
|
|
case SSH_FXP_STATUS:
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
sftp->read_packet = NULL;
|
|
|
|
sftp->read_state = libssh2_NB_state_idle;
|
|
|
|
|
|
|
|
if (retcode == LIBSSH2_FX_EOF) {
|
|
|
|
return total_read;
|
|
|
|
} else {
|
|
|
|
sftp->last_errno = retcode;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-08-06 20:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case SSH_FXP_DATA:
|
2009-03-17 13:48:35 +00:00
|
|
|
bytes_read = _libssh2_ntohu32(data + 5);
|
2007-08-06 20:48:04 +00:00
|
|
|
if (bytes_read > (data_len - 9)) {
|
2007-04-22 13:13:51 +00:00
|
|
|
sftp->read_packet = NULL;
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->read_state = libssh2_NB_state_idle;
|
2007-08-06 20:48:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-04-01 08:04:32 +00:00
|
|
|
#ifdef LIBSSH2_DEBUG_SFTP
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%lu bytes returned",
|
2007-08-06 20:48:04 +00:00
|
|
|
(unsigned long) bytes_read);
|
2007-04-01 08:04:32 +00:00
|
|
|
#endif
|
2007-08-06 20:48:04 +00:00
|
|
|
memcpy(buffer + total_read, data + 9, bytes_read);
|
|
|
|
handle->u.file.offset += bytes_read;
|
|
|
|
total_read += bytes_read;
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
/*
|
|
|
|
* Set the state back to allocated, so a new one will be
|
|
|
|
* created to either request more data or get EOF
|
|
|
|
*/
|
|
|
|
sftp->read_state = libssh2_NB_state_allocated;
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-04-22 13:13:51 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-22 13:13:51 +00:00
|
|
|
sftp->read_packet = NULL;
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->read_state = libssh2_NB_state_idle;
|
2007-04-22 13:13:51 +00:00
|
|
|
return total_read;
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_read
|
|
|
|
* Read from an SFTP file handle
|
|
|
|
*/
|
|
|
|
LIBSSH2_API ssize_t
|
|
|
|
libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *hnd, char *buffer,
|
|
|
|
size_t buffer_maxlen)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!hnd)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
|
|
|
|
sftp_read(hnd, buffer, buffer_maxlen));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sftp_readdir
|
2007-06-07 16:01:12 +00:00
|
|
|
* Read from an SFTP directory handle
|
2007-02-02 16:21:20 +00:00
|
|
|
*/
|
2009-03-26 15:41:14 +00:00
|
|
|
static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
|
2007-08-06 20:48:04 +00:00
|
|
|
size_t buffer_maxlen, char *longentry,
|
|
|
|
size_t longentry_maxlen,
|
2009-03-26 15:41:14 +00:00
|
|
|
LIBSSH2_SFTP_ATTRIBUTES *attrs)
|
2007-02-02 16:21:20 +00:00
|
|
|
{
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP *sftp = handle->sftp;
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len, filename_len, longentry_len, num_names;
|
2007-04-21 18:16:23 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
|
2007-05-28 17:56:08 +00:00
|
|
|
ssize_t packet_len = handle->handle_len + 13;
|
2007-06-17 23:34:35 +00:00
|
|
|
unsigned char *s, *data;
|
2010-04-28 00:31:46 +02:00
|
|
|
static const unsigned char read_responses[2] = {
|
|
|
|
SSH_FXP_NAME, SSH_FXP_STATUS };
|
2007-05-28 17:56:08 +00:00
|
|
|
int retcode;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
if (sftp->readdir_state == libssh2_NB_state_idle) {
|
2007-02-02 16:21:20 +00:00
|
|
|
if (handle->u.dir.names_left) {
|
2007-04-21 18:16:23 +00:00
|
|
|
/*
|
2007-08-06 20:48:04 +00:00
|
|
|
* A prior request returned more than one directory entry,
|
|
|
|
* feed it back from the buffer
|
2007-04-21 18:16:23 +00:00
|
|
|
*/
|
2010-04-15 01:15:35 +02:00
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs_dummy;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t real_longentry_len;
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned char *s = (unsigned char *) handle->u.dir.next_name;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t real_filename_len = _libssh2_ntohu32(s);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
s += 4;
|
2010-04-15 01:15:35 +02:00
|
|
|
|
|
|
|
filename_len = real_filename_len;
|
|
|
|
if (filename_len >= buffer_maxlen)
|
|
|
|
/* leave room for the trailing zero */
|
|
|
|
filename_len = buffer_maxlen - 1;
|
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
memcpy(buffer, s, filename_len);
|
2010-04-15 01:15:35 +02:00
|
|
|
buffer[filename_len] = '\0'; /* zero terminate */
|
2007-04-21 18:16:23 +00:00
|
|
|
s += real_filename_len;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-15 01:15:35 +02:00
|
|
|
real_longentry_len = _libssh2_ntohu32(s);
|
|
|
|
s += 4;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-15 01:15:35 +02:00
|
|
|
if (longentry && (longentry_maxlen>1)) {
|
2007-06-15 17:22:49 +00:00
|
|
|
longentry_len = real_longentry_len;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-15 01:15:35 +02:00
|
|
|
if (longentry_len >= longentry_maxlen)
|
|
|
|
/* leave room for the trailing zero */
|
|
|
|
longentry_len = longentry_maxlen -1;
|
|
|
|
|
|
|
|
memcpy(longentry, s, longentry_len);
|
|
|
|
longentry[longentry_len] = '\0'; /* zero terminate */
|
2007-06-15 17:22:49 +00:00
|
|
|
}
|
2010-04-15 01:15:35 +02:00
|
|
|
s += real_longentry_len;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-15 01:15:35 +02:00
|
|
|
if (attrs)
|
2007-04-21 18:16:23 +00:00
|
|
|
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
|
2010-04-15 01:15:35 +02:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
s += sftp_bin2attr(attrs ? attrs : &attrs_dummy, s);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
|
|
|
handle->u.dir.next_name = (char *) s;
|
2010-04-15 01:15:35 +02:00
|
|
|
if ((--handle->u.dir.names_left) == 0)
|
2007-04-21 18:16:23 +00:00
|
|
|
LIBSSH2_FREE(session, handle->u.dir.names_packet);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"libssh2_sftp_readdir_ex() return %d",
|
|
|
|
filename_len);
|
2007-04-21 18:16:23 +00:00
|
|
|
return filename_len;
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-02-02 16:21:20 +00:00
|
|
|
/* Request another entry(entries?) */
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-17 23:34:35 +00:00
|
|
|
s = sftp->readdir_packet = LIBSSH2_ALLOC(session, packet_len);
|
2010-04-15 01:15:35 +02:00
|
|
|
if (!sftp->readdir_packet)
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for "
|
|
|
|
"FXP_READDIR packet");
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
2007-02-02 16:21:20 +00:00
|
|
|
*(s++) = SSH_FXP_READDIR;
|
2007-06-17 23:34:35 +00:00
|
|
|
sftp->readdir_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->readdir_request_id);
|
|
|
|
_libssh2_store_str(&s, handle->handle, handle->handle_len);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->readdir_state = libssh2_NB_state_created;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->readdir_state == libssh2_NB_state_created) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Reading entries from directory handle");
|
2010-10-23 00:11:59 +02:00
|
|
|
retcode = _libssh2_channel_write(channel, 0, sftp->readdir_packet,
|
2009-03-29 22:09:29 +00:00
|
|
|
packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (retcode == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return retcode;
|
2009-03-13 22:15:27 +00:00
|
|
|
}
|
|
|
|
else if (packet_len != retcode) {
|
2007-06-17 23:34:35 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->readdir_packet);
|
|
|
|
sftp->readdir_packet = NULL;
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->readdir_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"_libssh2_channel_write() failed");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-17 23:34:35 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->readdir_packet);
|
2007-04-21 18:16:23 +00:00
|
|
|
sftp->readdir_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-17 23:34:35 +00:00
|
|
|
sftp->readdir_state = libssh2_NB_state_sent;
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-02-02 16:21:20 +00:00
|
|
|
|
2010-04-15 01:15:35 +02:00
|
|
|
retcode = sftp_packet_requirev(sftp, 2, read_responses,
|
|
|
|
sftp->readdir_request_id, &data,
|
|
|
|
&data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (retcode == LIBSSH2_ERROR_EAGAIN)
|
2009-08-25 00:54:47 +02:00
|
|
|
return retcode;
|
2010-04-15 01:15:35 +02:00
|
|
|
else if (retcode) {
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->readdir_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-02-02 16:21:20 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (data[0] == SSH_FXP_STATUS) {
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
if (retcode == LIBSSH2_FX_EOF) {
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->readdir_state = libssh2_NB_state_idle;
|
2007-05-28 17:56:08 +00:00
|
|
|
return 0;
|
2010-04-15 01:15:35 +02:00
|
|
|
}
|
|
|
|
else {
|
2007-05-28 17:56:08 +00:00
|
|
|
sftp->last_errno = retcode;
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->readdir_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-15 01:15:35 +02:00
|
|
|
sftp->readdir_state = libssh2_NB_state_idle;
|
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
num_names = _libssh2_ntohu32(data + 5);
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%lu entries returned",
|
2007-08-06 20:48:04 +00:00
|
|
|
num_names);
|
2007-05-28 17:56:08 +00:00
|
|
|
if (num_names <= 0) {
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
return (num_names == 0) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->u.dir.names_left = num_names;
|
|
|
|
handle->u.dir.names_packet = data;
|
2007-08-06 20:48:04 +00:00
|
|
|
handle->u.dir.next_name = (char *) data + 9;
|
2007-02-02 16:21:20 +00:00
|
|
|
|
2010-04-15 01:15:35 +02:00
|
|
|
/* use the name popping mechanism from the start of the function */
|
|
|
|
return sftp_readdir(handle, buffer, buffer_maxlen, longentry,
|
|
|
|
longentry_maxlen, attrs);
|
2007-04-21 18:16:23 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_readdir_ex
|
|
|
|
* Read from an SFTP directory handle
|
|
|
|
*/
|
|
|
|
LIBSSH2_API int
|
|
|
|
libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *hnd, char *buffer,
|
|
|
|
size_t buffer_maxlen, char *longentry,
|
|
|
|
size_t longentry_maxlen,
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES *attrs)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!hnd)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
|
|
|
|
sftp_readdir(hnd, buffer, buffer_maxlen, longentry,
|
|
|
|
longentry_maxlen, attrs));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-05-20 14:31:46 +02:00
|
|
|
/*
|
|
|
|
* sftp_write
|
|
|
|
*
|
2009-08-24 22:28:27 +02:00
|
|
|
* Write data to an SFTP handle. Returns the number of bytes written, or
|
|
|
|
* a negative error code.
|
2010-10-25 01:43:54 +02:00
|
|
|
*
|
|
|
|
* We recommend sending very large data buffers to this function!
|
|
|
|
*
|
|
|
|
* Concept:
|
|
|
|
*
|
|
|
|
* - Split all outgoing data in packets no larger than N.
|
|
|
|
* - Each N bytes packet gets created as a separate SFTP packet.
|
|
|
|
* - Put all created packets in a linked list.
|
|
|
|
* - Send the parts, as many as possible until EAGAIN, and check for
|
|
|
|
* ACKs.
|
|
|
|
* - If the first packet(s) in the list has been ACKed, remove them from
|
|
|
|
* the list and return back the total byte count (TOTAL) for those packets.
|
|
|
|
*
|
|
|
|
* - On following call, the 'buffer' is expected to have advanced TOTAL
|
|
|
|
* bytes.
|
|
|
|
* - Detect how much of the given buffer that was already sent in a previous
|
|
|
|
* call by inspecting the linked list.
|
|
|
|
* - Split up the remainder and put in list to send.
|
|
|
|
* - Move through list and count ACKed packets and try to send more.
|
|
|
|
* - Return TOTAL bytes acked so far.
|
|
|
|
*
|
|
|
|
* Caveats:
|
|
|
|
* - be careful: we must not return a higher number than what was given!
|
|
|
|
*
|
2010-10-25 16:04:00 +02:00
|
|
|
* TODO:
|
|
|
|
* Introduce an option that disables this sort of "speculative" ahead writing
|
|
|
|
* as there's a risk that it will do harm to some app.
|
2007-02-02 16:21:20 +00:00
|
|
|
*/
|
2010-10-25 01:43:54 +02:00
|
|
|
|
2009-05-20 14:31:46 +02:00
|
|
|
static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
|
2009-03-26 15:41:14 +00:00
|
|
|
size_t count)
|
2007-02-02 16:21:20 +00:00
|
|
|
{
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP *sftp = handle->sftp;
|
2007-04-22 15:56:06 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len, retcode;
|
2009-05-20 14:31:46 +02:00
|
|
|
ssize_t packet_len;
|
2007-06-06 19:52:11 +00:00
|
|
|
unsigned char *s, *data;
|
2007-04-22 15:56:06 +00:00
|
|
|
int rc;
|
2010-10-25 01:43:54 +02:00
|
|
|
struct sftp_write_chunk *chunk;
|
|
|
|
struct sftp_write_chunk *next;
|
|
|
|
size_t sent = 0;
|
|
|
|
size_t acked = 0;
|
|
|
|
size_t org_count = count;
|
|
|
|
|
2010-10-25 16:04:00 +02:00
|
|
|
/* number of bytes sent off that haven't been acked and therefor
|
|
|
|
we will get passed in here again */
|
|
|
|
size_t unacked = handle->u.file.offset_sent - handle->u.file.offset;
|
2010-10-25 01:43:54 +02:00
|
|
|
|
2010-10-25 16:04:00 +02:00
|
|
|
/* skip the part already made into packets */
|
|
|
|
buffer += unacked;
|
|
|
|
count -= unacked;
|
2010-10-25 01:43:54 +02:00
|
|
|
|
|
|
|
while(count) {
|
2010-10-25 16:04:00 +02:00
|
|
|
/* TODO: Possibly this should have some logic to prevent a very very
|
2010-10-25 01:43:54 +02:00
|
|
|
small fraction to be left but lets ignore that for now */
|
|
|
|
size_t size = MIN(MAX_SFTP_OUTGOING_SIZE, count);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
/* 25 = packet_len(4) + packet_type(1) + request_id(4) +
|
|
|
|
handle_len(4) + offset(8) + count(4) */
|
|
|
|
packet_len = handle->handle_len + size + 25;
|
2009-05-20 14:31:46 +02:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
chunk = LIBSSH2_ALLOC(session, packet_len +
|
|
|
|
sizeof(struct sftp_write_chunk));
|
|
|
|
if (!chunk)
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
2010-10-25 01:43:54 +02:00
|
|
|
"malloc fail for FXP_WRITE");
|
|
|
|
|
|
|
|
chunk->org_buflen = size;
|
|
|
|
chunk->sent = 0;
|
|
|
|
chunk->lefttosend = packet_len;
|
|
|
|
|
|
|
|
s = chunk->packet;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
|
|
|
|
2007-04-22 15:56:06 +00:00
|
|
|
*(s++) = SSH_FXP_WRITE;
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->write_request_id = sftp->request_id++;
|
2010-10-25 01:43:54 +02:00
|
|
|
chunk->request_id = sftp->write_request_id;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->write_request_id);
|
|
|
|
_libssh2_store_str(&s, handle->handle, handle->handle_len);
|
2010-10-25 01:43:54 +02:00
|
|
|
_libssh2_store_u64(&s, handle->u.file.offset_sent);
|
|
|
|
handle->u.file.offset_sent += size; /* advance offset at once */
|
|
|
|
_libssh2_store_str(&s, buffer, size);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
/* add this new entry LAST in the list */
|
|
|
|
_libssh2_list_add(&handle->write_list, &chunk->node);
|
|
|
|
|
|
|
|
buffer += size;
|
|
|
|
count -= size; /* deduct the size we used, as we might have
|
|
|
|
to create more packets */
|
2007-04-22 15:56:06 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
/* move through the packets that haven't been sent and send as many
|
|
|
|
as possible - remember that we don't block */
|
|
|
|
chunk = _libssh2_list_first(&handle->write_list);
|
|
|
|
|
|
|
|
while(chunk) {
|
|
|
|
if(chunk->lefttosend) {
|
|
|
|
rc = _libssh2_channel_write(channel, 0,
|
|
|
|
&chunk->packet[chunk->sent],
|
|
|
|
chunk->lefttosend);
|
|
|
|
if(rc < 0) {
|
|
|
|
if(rc != LIBSSH2_ERROR_EAGAIN)
|
|
|
|
/* error */
|
|
|
|
return rc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remember where to continue sending the next time */
|
|
|
|
chunk->lefttosend -= rc;
|
|
|
|
chunk->sent += rc;
|
|
|
|
sent += rc;
|
|
|
|
|
|
|
|
if(chunk->lefttosend)
|
|
|
|
/* data left to send, get out of loop */
|
|
|
|
break;
|
|
|
|
|
|
|
|
chunk = _libssh2_list_next(&chunk->node);
|
|
|
|
|
|
|
|
sftp->write_state = libssh2_NB_state_sent;
|
2007-06-06 19:52:11 +00:00
|
|
|
}
|
2010-10-25 01:43:54 +02:00
|
|
|
else {
|
|
|
|
/* move on to the next chunk with data to send */
|
|
|
|
chunk = _libssh2_list_next(&chunk->node);
|
2009-05-20 14:31:46 +02:00
|
|
|
}
|
2007-04-22 15:56:06 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
/*
|
|
|
|
* Count all ACKed packets
|
|
|
|
*/
|
|
|
|
chunk = _libssh2_list_first(&handle->write_list);
|
|
|
|
|
|
|
|
while(chunk) {
|
|
|
|
/* we can expect the packets to be ACKed in order */
|
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
|
|
|
|
chunk->request_id, &data, &data_len);
|
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN)
|
|
|
|
break;
|
|
|
|
else if (rc) {
|
|
|
|
sftp->write_state = libssh2_NB_state_idle;
|
|
|
|
return _libssh2_error(session, rc, "Waiting for SFTP status");
|
|
|
|
}
|
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
sftp->last_errno = retcode;
|
|
|
|
if (retcode == LIBSSH2_FX_OK) {
|
|
|
|
acked += chunk->org_buflen; /* number of payload data that was
|
|
|
|
acked here */
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
/* we increase the offset value for all acks */
|
|
|
|
handle->u.file.offset += chunk->org_buflen;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
next = _libssh2_list_next(&chunk->node);
|
|
|
|
|
|
|
|
_libssh2_list_remove(&chunk->node); /* remove from list */
|
|
|
|
LIBSSH2_FREE(session, chunk); /* free memory */
|
|
|
|
|
|
|
|
chunk = next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* TODO: handle errors here! */
|
|
|
|
break;
|
|
|
|
}
|
2007-04-22 15:56:06 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 01:43:54 +02:00
|
|
|
sftp->write_state = libssh2_NB_state_idle;
|
|
|
|
|
|
|
|
if(acked)
|
|
|
|
/* we got data acked so return that amount, but no more than what
|
|
|
|
was asked to get sent! */
|
|
|
|
return MIN(acked, org_count);
|
|
|
|
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
|
|
|
|
"Would block");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_write
|
|
|
|
* Write data to a file handle
|
|
|
|
*/
|
|
|
|
LIBSSH2_API ssize_t
|
|
|
|
libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *hnd, const char *buffer,
|
|
|
|
size_t count)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!hnd)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
|
|
|
|
sftp_write(hnd, buffer, count));
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sftp_fstat
|
|
|
|
*
|
2004-12-22 00:20:02 +00:00
|
|
|
* Get or Set stat on a file
|
|
|
|
*/
|
2009-03-26 15:41:14 +00:00
|
|
|
static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES *attrs, int setstat)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP *sftp = handle->sftp;
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
2007-06-07 16:01:12 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
|
2007-08-06 20:48:04 +00:00
|
|
|
ssize_t packet_len =
|
2010-04-28 00:23:09 +02:00
|
|
|
handle->handle_len + 13 + (setstat ? sftp_attrsize(attrs->flags) : 0);
|
2007-06-07 16:01:12 +00:00
|
|
|
unsigned char *s, *data;
|
2007-08-06 20:48:04 +00:00
|
|
|
static const unsigned char fstat_responses[2] =
|
|
|
|
{ SSH_FXP_ATTRS, SSH_FXP_STATUS };
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->fstat_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Issuing %s command",
|
2007-08-06 20:48:04 +00:00
|
|
|
setstat ? "set-stat" : "stat");
|
2007-06-07 16:01:12 +00:00
|
|
|
s = sftp->fstat_packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!sftp->fstat_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for "
|
|
|
|
"FSTAT/FSETSTAT packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
2007-06-07 16:01:12 +00:00
|
|
|
*(s++) = setstat ? SSH_FXP_FSETSTAT : SSH_FXP_FSTAT;
|
|
|
|
sftp->fstat_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->fstat_request_id);
|
|
|
|
_libssh2_store_str(&s, handle->handle, handle->handle_len);
|
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (setstat) {
|
2009-03-23 13:35:20 +00:00
|
|
|
s += sftp_attr2bin(s, attrs);
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->fstat_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->fstat_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, sftp->fstat_packet,
|
2009-03-29 22:09:29 +00:00
|
|
|
packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->fstat_packet);
|
|
|
|
sftp->fstat_packet = NULL;
|
|
|
|
sftp->fstat_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
(setstat ? "Unable to send FXP_FSETSTAT"
|
|
|
|
: "Unable to send FXP_FSTAT command"));
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
LIBSSH2_FREE(session, sftp->fstat_packet);
|
|
|
|
sftp->fstat_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->fstat_state = libssh2_NB_state_sent;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2008-12-15 22:58:07 +00:00
|
|
|
rc = sftp_packet_requirev(sftp, 2, fstat_responses,
|
|
|
|
sftp->fstat_request_id, &data,
|
|
|
|
&data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->fstat_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->fstat_state = libssh2_NB_state_idle;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (data[0] == SSH_FXP_STATUS) {
|
|
|
|
int retcode;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
if (retcode == LIBSSH2_FX_OK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sftp->last_errno = retcode;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
sftp_bin2attr(attrs, data + 5);
|
2009-11-24 14:54:10 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return 0;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_fstat_ex
|
|
|
|
* Get or Set stat on a file
|
|
|
|
*/
|
|
|
|
LIBSSH2_API int
|
2010-06-11 13:05:55 +02:00
|
|
|
libssh2_sftp_fstat_ex(LIBSSH2_SFTP_HANDLE *hnd,
|
2009-03-26 15:41:14 +00:00
|
|
|
LIBSSH2_SFTP_ATTRIBUTES *attrs, int setstat)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!hnd || !attrs)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, hnd->sftp->channel->session,
|
|
|
|
sftp_fstat(hnd, attrs, setstat));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* libssh2_sftp_seek
|
2004-12-22 00:20:02 +00:00
|
|
|
* Set the read/write pointer to an arbitrary position within the file
|
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API void
|
2010-06-11 13:05:55 +02:00
|
|
|
libssh2_sftp_seek(LIBSSH2_SFTP_HANDLE *handle, size_t offset)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2010-06-11 13:05:55 +02:00
|
|
|
if(handle)
|
|
|
|
handle->u.file.offset = offset;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* libssh2_sftp_seek64
|
2008-09-29 14:11:29 +00:00
|
|
|
* Set the read/write pointer to an arbitrary position within the file
|
|
|
|
*/
|
|
|
|
LIBSSH2_API void
|
2010-06-11 13:05:55 +02:00
|
|
|
libssh2_sftp_seek64(LIBSSH2_SFTP_HANDLE *handle, libssh2_uint64_t offset)
|
2008-09-29 14:11:29 +00:00
|
|
|
{
|
2010-06-11 13:05:55 +02:00
|
|
|
if(handle)
|
|
|
|
handle->u.file.offset = offset;
|
2008-09-29 14:11:29 +00:00
|
|
|
}
|
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* libssh2_sftp_tell
|
2004-12-22 00:20:02 +00:00
|
|
|
* Return the current read/write pointer's offset
|
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API size_t
|
2010-06-11 13:05:55 +02:00
|
|
|
libssh2_sftp_tell(LIBSSH2_SFTP_HANDLE *handle)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!handle)
|
|
|
|
return 0; /* no handle, no size */
|
|
|
|
|
2009-07-07 13:44:17 +02:00
|
|
|
/* NOTE: this may very well truncate the size if it is larger than what
|
|
|
|
size_t can hold, so libssh2_sftp_tell64() is really the function you
|
|
|
|
should use */
|
|
|
|
return (size_t)(handle->u.file.offset);
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* libssh2_sftp_tell64
|
2008-12-22 12:46:45 +00:00
|
|
|
* Return the current read/write pointer's offset
|
|
|
|
*/
|
|
|
|
LIBSSH2_API libssh2_uint64_t
|
2010-06-11 13:05:55 +02:00
|
|
|
libssh2_sftp_tell64(LIBSSH2_SFTP_HANDLE *handle)
|
2008-12-22 12:46:45 +00:00
|
|
|
{
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!handle)
|
|
|
|
return 0; /* no handle, no size */
|
|
|
|
|
2008-12-22 12:46:45 +00:00
|
|
|
return handle->u.file.offset;
|
|
|
|
}
|
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* sftp_close_handle
|
|
|
|
*
|
2004-12-22 00:20:02 +00:00
|
|
|
* Close a file or directory handle
|
|
|
|
* Also frees handle resource and unlinks it from the SFTP structure
|
|
|
|
*/
|
2009-03-26 15:41:14 +00:00
|
|
|
static int
|
|
|
|
sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_SFTP *sftp = handle->sftp;
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
|
|
|
int retcode;
|
2007-06-07 16:01:12 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
|
|
|
|
ssize_t packet_len = handle->handle_len + 13;
|
2010-04-24 13:03:27 +02:00
|
|
|
unsigned char *s, *data = NULL;
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2010-10-25 16:30:09 +02:00
|
|
|
struct sftp_write_chunk *chunk;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (handle->close_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Closing handle");
|
2007-07-09 15:11:37 +00:00
|
|
|
s = handle->close_packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!handle->close_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_CLOSE "
|
|
|
|
"packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
2007-06-07 16:01:12 +00:00
|
|
|
*(s++) = SSH_FXP_CLOSE;
|
|
|
|
handle->close_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, handle->close_request_id);
|
|
|
|
_libssh2_store_str(&s, handle->handle, handle->handle_len);
|
2007-06-07 16:01:12 +00:00
|
|
|
handle->close_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (handle->close_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, handle->close_packet,
|
2009-03-29 22:09:29 +00:00
|
|
|
packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != rc) {
|
2007-07-09 15:11:37 +00:00
|
|
|
LIBSSH2_FREE(session, handle->close_packet);
|
|
|
|
handle->close_packet = NULL;
|
2007-06-07 16:01:12 +00:00
|
|
|
handle->close_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"Unable to send FXP_CLOSE command");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-07-09 15:11:37 +00:00
|
|
|
LIBSSH2_FREE(session, handle->close_packet);
|
|
|
|
handle->close_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
handle->close_state = libssh2_NB_state_sent;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (handle->close_state == libssh2_NB_state_sent) {
|
2008-12-16 15:35:33 +00:00
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
|
|
|
|
handle->close_request_id, &data,
|
|
|
|
&data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
handle->close_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
handle->close_state = libssh2_NB_state_sent1;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-24 13:03:27 +02:00
|
|
|
if(!data)
|
|
|
|
/* if it reaches this point with data unset, something unwanted
|
|
|
|
happened (like this function is called again when in
|
|
|
|
libssh2_NB_state_sent1 state) and we just bail out */
|
|
|
|
return LIBSSH2_ERROR_INVAL;
|
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (retcode != LIBSSH2_FX_OK) {
|
|
|
|
sftp->last_errno = retcode;
|
2007-06-07 16:01:12 +00:00
|
|
|
handle->close_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-08-19 13:58:15 +02:00
|
|
|
/* remove this handle from the parent's list */
|
|
|
|
_libssh2_list_remove(&handle->node);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
|
|
|
if ((handle->handle_type == LIBSSH2_SFTP_HANDLE_DIR)
|
|
|
|
&& handle->u.dir.names_left) {
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, handle->u.dir.names_packet);
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-10-25 16:30:09 +02:00
|
|
|
/* remove pending write chunks */
|
|
|
|
do {
|
|
|
|
chunk = _libssh2_list_first(&handle->write_list);
|
|
|
|
if(chunk) {
|
|
|
|
struct sftp_write_chunk *next =
|
|
|
|
_libssh2_list_next(&chunk->node);
|
|
|
|
_libssh2_list_remove(&chunk->node);
|
|
|
|
LIBSSH2_FREE(session, chunk);
|
|
|
|
chunk = next;
|
|
|
|
}
|
|
|
|
} while(chunk);
|
|
|
|
|
2007-06-15 01:41:58 +00:00
|
|
|
handle->close_state = libssh2_NB_state_idle;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, handle);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return 0;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_close_handle
|
|
|
|
*
|
|
|
|
* Close a file or directory handle
|
|
|
|
* Also frees handle resource and unlinks it from the SFTP structure
|
|
|
|
*/
|
|
|
|
LIBSSH2_API int
|
|
|
|
libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE *hnd)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!hnd)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, hnd->sftp->channel->session, sftp_close_handle(hnd));
|
|
|
|
return rc;
|
|
|
|
}
|
2004-12-22 00:20:02 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* sftp_unlink
|
2004-12-22 00:20:02 +00:00
|
|
|
* Delete a file from the remote server
|
|
|
|
*/
|
2009-03-26 15:41:14 +00:00
|
|
|
static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned int filename_len)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
|
|
|
int retcode;
|
2007-06-07 16:01:12 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) */
|
|
|
|
ssize_t packet_len = filename_len + 13;
|
|
|
|
unsigned char *s, *data;
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->unlink_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Unlinking %s", filename);
|
2007-06-07 16:01:12 +00:00
|
|
|
s = sftp->unlink_packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!sftp->unlink_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_REMOVE "
|
|
|
|
"packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
2007-06-07 16:01:12 +00:00
|
|
|
*(s++) = SSH_FXP_REMOVE;
|
|
|
|
sftp->unlink_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->unlink_request_id);
|
|
|
|
_libssh2_store_str(&s, filename, filename_len);
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->unlink_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->unlink_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, sftp->unlink_packet,
|
2009-03-29 22:09:29 +00:00
|
|
|
packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->unlink_packet);
|
|
|
|
sftp->unlink_packet = NULL;
|
|
|
|
sftp->unlink_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"Unable to send FXP_REMOVE command");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
LIBSSH2_FREE(session, sftp->unlink_packet);
|
|
|
|
sftp->unlink_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->unlink_state = libssh2_NB_state_sent;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2008-12-16 15:35:33 +00:00
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
|
|
|
|
sftp->unlink_request_id, &data,
|
|
|
|
&data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2008-12-16 15:35:33 +00:00
|
|
|
}
|
|
|
|
else if (rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->unlink_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->unlink_state = libssh2_NB_state_idle;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (retcode == LIBSSH2_FX_OK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sftp->last_errno = retcode;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_unlink_ex
|
|
|
|
* Delete a file from the remote server
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API int
|
2009-03-26 15:41:14 +00:00
|
|
|
libssh2_sftp_unlink_ex(LIBSSH2_SFTP *sftp, const char *filename,
|
|
|
|
unsigned int filename_len)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session,
|
|
|
|
sftp_unlink(sftp, filename, filename_len));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sftp_rename
|
|
|
|
*
|
|
|
|
* Rename a file on the remote server
|
|
|
|
*/
|
|
|
|
static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned int source_filename_len,
|
|
|
|
const char *dest_filename,
|
|
|
|
unsigned int dest_filename_len, long flags)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
2009-04-06 14:50:29 +00:00
|
|
|
int retcode;
|
2007-08-06 20:48:04 +00:00
|
|
|
ssize_t packet_len =
|
|
|
|
source_filename_len + dest_filename_len + 17 + (sftp->version >=
|
|
|
|
5 ? 4 : 0);
|
2007-05-28 17:56:08 +00:00
|
|
|
/* packet_len(4) + packet_type(1) + request_id(4) +
|
2007-08-06 20:48:04 +00:00
|
|
|
source_filename_len(4) + dest_filename_len(4) + flags(4){SFTP5+) */
|
2007-06-07 16:01:12 +00:00
|
|
|
unsigned char *data;
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (sftp->version < 2) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"Server does not support RENAME");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->rename_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Renaming %s to %s",
|
2007-08-06 20:48:04 +00:00
|
|
|
source_filename, dest_filename);
|
|
|
|
sftp->rename_s = sftp->rename_packet =
|
|
|
|
LIBSSH2_ALLOC(session, packet_len);
|
2007-06-07 16:01:12 +00:00
|
|
|
if (!sftp->rename_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_RENAME "
|
|
|
|
"packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&sftp->rename_s, packet_len - 4);
|
2007-06-07 16:01:12 +00:00
|
|
|
*(sftp->rename_s++) = SSH_FXP_RENAME;
|
|
|
|
sftp->rename_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&sftp->rename_s, sftp->rename_request_id);
|
|
|
|
_libssh2_store_str(&sftp->rename_s, source_filename,
|
|
|
|
source_filename_len);
|
|
|
|
_libssh2_store_str(&sftp->rename_s, dest_filename, dest_filename_len);
|
|
|
|
|
|
|
|
if (sftp->version >= 5)
|
|
|
|
_libssh2_store_u32(&sftp->rename_s, flags);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->rename_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->rename_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, sftp->rename_packet,
|
2009-03-29 22:09:29 +00:00
|
|
|
sftp->rename_s - sftp->rename_packet);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->rename_packet);
|
|
|
|
sftp->rename_packet = NULL;
|
|
|
|
sftp->rename_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"Unable to send FXP_RENAME command");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
LIBSSH2_FREE(session, sftp->rename_packet);
|
|
|
|
sftp->rename_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->rename_state = libssh2_NB_state_sent;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2008-12-16 15:35:33 +00:00
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
|
|
|
|
sftp->rename_request_id, &data,
|
|
|
|
&data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->rename_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->rename_state = libssh2_NB_state_idle;
|
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-03-03 23:04:05 +01:00
|
|
|
sftp->last_errno = retcode;
|
|
|
|
|
|
|
|
/* now convert the SFTP error code to libssh2 return code or error
|
|
|
|
message */
|
2007-05-28 17:56:08 +00:00
|
|
|
switch (retcode) {
|
2007-08-06 20:48:04 +00:00
|
|
|
case LIBSSH2_FX_OK:
|
2010-03-03 23:04:05 +01:00
|
|
|
retcode = LIBSSH2_ERROR_NONE;
|
2007-08-06 20:48:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBSSH2_FX_FILE_ALREADY_EXISTS:
|
2010-04-16 00:18:51 +02:00
|
|
|
retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"File already exists and "
|
|
|
|
"SSH_FXP_RENAME_OVERWRITE not specified");
|
2007-08-06 20:48:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBSSH2_FX_OP_UNSUPPORTED:
|
2010-04-16 00:18:51 +02:00
|
|
|
retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"Operation Not Supported");
|
2007-08-06 20:48:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-04-16 00:18:51 +02:00
|
|
|
retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2010-03-03 23:04:05 +01:00
|
|
|
break;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return retcode;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_rename_ex
|
|
|
|
* Rename a file on the remote server
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API int
|
2009-03-26 15:41:14 +00:00
|
|
|
libssh2_sftp_rename_ex(LIBSSH2_SFTP *sftp, const char *source_filename,
|
|
|
|
unsigned int source_filename_len,
|
|
|
|
const char *dest_filename,
|
|
|
|
unsigned int dest_filename_len, long flags)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session,
|
|
|
|
sftp_rename(sftp, source_filename, source_filename_len,
|
|
|
|
dest_filename, dest_filename_len, flags));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-05-22 01:48:04 -07:00
|
|
|
/*
|
|
|
|
* sftp_fstatvfs
|
|
|
|
*
|
|
|
|
* Get file system statistics
|
|
|
|
*/
|
|
|
|
static int sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
|
|
|
|
{
|
|
|
|
LIBSSH2_SFTP *sftp = handle->sftp;
|
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
|
|
|
size_t data_len;
|
|
|
|
/* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4)
|
|
|
|
+ handle_len (4) */
|
|
|
|
/* 20 = strlen ("fstatvfs@openssh.com") */
|
|
|
|
ssize_t packet_len = handle->handle_len + 20 + 17;
|
|
|
|
unsigned char *packet, *s, *data;
|
|
|
|
int rc;
|
|
|
|
unsigned int flag;
|
|
|
|
|
|
|
|
if (sftp->fstatvfs_state == libssh2_NB_state_idle) {
|
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
|
|
|
"Getting file system statistics");
|
|
|
|
s = packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!packet) {
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_EXTENDED "
|
|
|
|
"packet");
|
|
|
|
}
|
|
|
|
|
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
|
|
|
*(s++) = SSH_FXP_EXTENDED;
|
|
|
|
sftp->fstatvfs_request_id = sftp->request_id++;
|
|
|
|
_libssh2_store_u32(&s, sftp->fstatvfs_request_id);
|
|
|
|
_libssh2_store_str(&s, "fstatvfs@openssh.com", 20);
|
|
|
|
_libssh2_store_str(&s, handle->handle, handle->handle_len);
|
|
|
|
|
|
|
|
sftp->fstatvfs_state = libssh2_NB_state_created;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
packet = sftp->fstatvfs_packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sftp->fstatvfs_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, packet, packet_len);
|
2010-05-22 01:48:04 -07:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN || (0 <= rc && rc < packet_len)) {
|
|
|
|
sftp->fstatvfs_packet = packet;
|
|
|
|
return LIBSSH2_ERROR_EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBSSH2_FREE(session, packet);
|
|
|
|
sftp->fstatvfs_packet = NULL;
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
sftp->fstatvfs_state = libssh2_NB_state_idle;
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"_libssh2_channel_write() failed");
|
|
|
|
}
|
|
|
|
sftp->fstatvfs_state = libssh2_NB_state_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_EXTENDED_REPLY,
|
|
|
|
sftp->fstatvfs_request_id, &data, &data_len);
|
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
return rc;
|
|
|
|
} else if (rc) {
|
|
|
|
sftp->fstatvfs_state = libssh2_NB_state_idle;
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
|
|
|
} else if (data_len < 93) {
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
sftp->fstatvfs_state = libssh2_NB_state_idle;
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error: short response");
|
|
|
|
}
|
|
|
|
|
|
|
|
sftp->fstatvfs_state = libssh2_NB_state_idle;
|
|
|
|
|
|
|
|
st->f_bsize = _libssh2_ntohu64(data + 5);
|
|
|
|
st->f_frsize = _libssh2_ntohu64(data + 13);
|
|
|
|
st->f_blocks = _libssh2_ntohu64(data + 21);
|
|
|
|
st->f_bfree = _libssh2_ntohu64(data + 29);
|
|
|
|
st->f_bavail = _libssh2_ntohu64(data + 37);
|
|
|
|
st->f_files = _libssh2_ntohu64(data + 45);
|
|
|
|
st->f_ffree = _libssh2_ntohu64(data + 53);
|
|
|
|
st->f_favail = _libssh2_ntohu64(data + 61);
|
|
|
|
st->f_fsid = _libssh2_ntohu64(data + 69);
|
|
|
|
flag = _libssh2_ntohu64(data + 77);
|
|
|
|
st->f_namemax = _libssh2_ntohu64(data + 85);
|
|
|
|
|
|
|
|
st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY)
|
|
|
|
? LIBSSH2_SFTP_ST_RDONLY : 0;
|
|
|
|
st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID)
|
|
|
|
? LIBSSH2_SFTP_ST_NOSUID : 0;
|
|
|
|
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* libssh2_sftp_fstatvfs
|
|
|
|
* Get filesystem space and inode utilization (requires fstatvfs@openssh.com
|
|
|
|
* support on the server)
|
|
|
|
*/
|
|
|
|
LIBSSH2_API int
|
|
|
|
libssh2_sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!handle || !st)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2010-05-22 01:48:04 -07:00
|
|
|
BLOCK_ADJUST(rc, handle->sftp->channel->session, sftp_fstatvfs(handle, st));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sftp_statvfs
|
|
|
|
*
|
|
|
|
* Get file system statistics
|
|
|
|
*/
|
|
|
|
static int sftp_statvfs(LIBSSH2_SFTP *sftp, const char *path,
|
|
|
|
unsigned int path_len, LIBSSH2_SFTP_STATVFS *st)
|
|
|
|
{
|
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
|
|
|
size_t data_len;
|
|
|
|
/* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4)
|
|
|
|
+ path_len (4) */
|
|
|
|
/* 19 = strlen ("statvfs@openssh.com") */
|
|
|
|
ssize_t packet_len = path_len + 19 + 17;
|
|
|
|
unsigned char *packet, *s, *data;
|
|
|
|
int rc;
|
|
|
|
unsigned int flag;
|
|
|
|
|
|
|
|
if (sftp->statvfs_state == libssh2_NB_state_idle) {
|
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
|
|
|
"Getting file system statistics of %s", path);
|
|
|
|
s = packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!packet) {
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_EXTENDED "
|
|
|
|
"packet");
|
|
|
|
}
|
|
|
|
|
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
|
|
|
*(s++) = SSH_FXP_EXTENDED;
|
|
|
|
sftp->statvfs_request_id = sftp->request_id++;
|
|
|
|
_libssh2_store_u32(&s, sftp->statvfs_request_id);
|
|
|
|
_libssh2_store_str(&s, "statvfs@openssh.com", 19);
|
|
|
|
_libssh2_store_str(&s, path, path_len);
|
|
|
|
|
|
|
|
sftp->statvfs_state = libssh2_NB_state_created;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
packet = sftp->statvfs_packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sftp->statvfs_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, packet, packet_len);
|
2010-05-22 01:48:04 -07:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN || (0 <= rc && rc < packet_len)) {
|
|
|
|
sftp->statvfs_packet = packet;
|
|
|
|
return LIBSSH2_ERROR_EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBSSH2_FREE(session, packet);
|
|
|
|
sftp->statvfs_packet = NULL;
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
sftp->statvfs_state = libssh2_NB_state_idle;
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"_libssh2_channel_write() failed");
|
|
|
|
}
|
|
|
|
sftp->statvfs_state = libssh2_NB_state_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_EXTENDED_REPLY,
|
|
|
|
sftp->statvfs_request_id, &data, &data_len);
|
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
return rc;
|
|
|
|
} else if (rc) {
|
|
|
|
sftp->statvfs_state = libssh2_NB_state_idle;
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
|
|
|
} else if (data_len < 93) {
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
sftp->fstatvfs_state = libssh2_NB_state_idle;
|
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error: short response");
|
|
|
|
}
|
|
|
|
|
|
|
|
sftp->statvfs_state = libssh2_NB_state_idle;
|
|
|
|
|
|
|
|
st->f_bsize = _libssh2_ntohu64(data + 5);
|
|
|
|
st->f_frsize = _libssh2_ntohu64(data + 13);
|
|
|
|
st->f_blocks = _libssh2_ntohu64(data + 21);
|
|
|
|
st->f_bfree = _libssh2_ntohu64(data + 29);
|
|
|
|
st->f_bavail = _libssh2_ntohu64(data + 37);
|
|
|
|
st->f_files = _libssh2_ntohu64(data + 45);
|
|
|
|
st->f_ffree = _libssh2_ntohu64(data + 53);
|
|
|
|
st->f_favail = _libssh2_ntohu64(data + 61);
|
|
|
|
st->f_fsid = _libssh2_ntohu64(data + 69);
|
|
|
|
flag = _libssh2_ntohu64(data + 77);
|
|
|
|
st->f_namemax = _libssh2_ntohu64(data + 85);
|
|
|
|
|
|
|
|
st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY)
|
|
|
|
? LIBSSH2_SFTP_ST_RDONLY : 0;
|
|
|
|
st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID)
|
|
|
|
? LIBSSH2_SFTP_ST_NOSUID : 0;
|
|
|
|
|
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* libssh2_sftp_statvfs_ex
|
|
|
|
* Get filesystem space and inode utilization (requires statvfs@openssh.com
|
|
|
|
* support on the server)
|
|
|
|
*/
|
|
|
|
LIBSSH2_API int
|
2010-05-28 17:21:39 +02:00
|
|
|
libssh2_sftp_statvfs(LIBSSH2_SFTP *sftp, const char *path,
|
|
|
|
size_t path_len, LIBSSH2_SFTP_STATVFS *st)
|
2010-05-22 01:48:04 -07:00
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp || !st)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2010-05-22 01:48:04 -07:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session, sftp_statvfs(sftp, path, path_len,
|
2010-05-28 17:21:39 +02:00
|
|
|
st));
|
2010-05-22 01:48:04 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/*
|
|
|
|
* sftp_mkdir
|
|
|
|
*
|
|
|
|
* Create an SFTP directory
|
|
|
|
*/
|
|
|
|
static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned int path_len, long mode)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-04-22 19:51:24 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs = {
|
|
|
|
LIBSSH2_SFTP_ATTR_PERMISSIONS, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
|
|
|
int retcode;
|
2007-06-07 16:01:12 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
|
2010-04-28 00:23:09 +02:00
|
|
|
ssize_t packet_len = path_len + 13 +
|
|
|
|
sftp_attrsize(LIBSSH2_SFTP_ATTR_PERMISSIONS);
|
2007-04-22 19:51:24 +00:00
|
|
|
unsigned char *packet, *s, *data;
|
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
if (sftp->mkdir_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
|
2007-08-06 20:48:04 +00:00
|
|
|
"Creating directory %s with mode 0%lo", path, mode);
|
2007-02-02 16:21:20 +00:00
|
|
|
s = packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_MKDIR "
|
|
|
|
"packet");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
|
|
|
/* Filetype in SFTP 3 and earlier */
|
|
|
|
attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
2007-02-02 16:21:20 +00:00
|
|
|
*(s++) = SSH_FXP_MKDIR;
|
2009-03-13 22:15:27 +00:00
|
|
|
sftp->mkdir_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->mkdir_request_id);
|
|
|
|
_libssh2_store_str(&s, path, path_len);
|
|
|
|
|
2009-03-27 21:52:49 +00:00
|
|
|
s += sftp_attr2bin(s, &attrs);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->mkdir_state = libssh2_NB_state_created;
|
2009-03-23 13:35:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-04-22 19:51:24 +00:00
|
|
|
packet = sftp->mkdir_packet;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->mkdir_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, packet, packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->mkdir_packet = packet;
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
if (packet_len != rc) {
|
|
|
|
LIBSSH2_FREE(session, packet);
|
|
|
|
sftp->mkdir_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"_libssh2_channel_write() failed");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-04-22 19:51:24 +00:00
|
|
|
LIBSSH2_FREE(session, packet);
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->mkdir_state = libssh2_NB_state_sent;
|
2007-04-22 19:51:24 +00:00
|
|
|
sftp->mkdir_packet = NULL;
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-13 22:15:27 +00:00
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, sftp->mkdir_request_id,
|
|
|
|
&data, &data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->mkdir_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-06 19:52:11 +00:00
|
|
|
sftp->mkdir_state = libssh2_NB_state_idle;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-04-22 19:51:24 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-04-22 19:51:24 +00:00
|
|
|
if (retcode == LIBSSH2_FX_OK) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "OK!");
|
2007-04-22 19:51:24 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sftp->last_errno = retcode;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-04-22 19:51:24 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/*
|
|
|
|
* libssh2_sftp_mkdir_ex
|
|
|
|
*
|
|
|
|
* Create an SFTP directory
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API int
|
2009-03-26 15:41:14 +00:00
|
|
|
libssh2_sftp_mkdir_ex(LIBSSH2_SFTP *sftp, const char *path,
|
|
|
|
unsigned int path_len, long mode)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session,
|
|
|
|
sftp_mkdir(sftp, path, path_len, mode));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sftp_rmdir
|
|
|
|
* Remove a directory
|
|
|
|
*/
|
|
|
|
static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned int path_len)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
|
|
|
int retcode;
|
2007-06-07 16:01:12 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
|
|
|
|
ssize_t packet_len = path_len + 13;
|
|
|
|
unsigned char *s, *data;
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->rmdir_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Removing directory: %s",
|
2007-08-06 20:48:04 +00:00
|
|
|
path);
|
2007-06-07 16:01:12 +00:00
|
|
|
s = sftp->rmdir_packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!sftp->rmdir_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_RMDIR "
|
|
|
|
"packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
2007-06-07 16:01:12 +00:00
|
|
|
*(s++) = SSH_FXP_RMDIR;
|
|
|
|
sftp->rmdir_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->rmdir_request_id);
|
|
|
|
_libssh2_store_str(&s, path, path_len);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->rmdir_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->rmdir_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, sftp->rmdir_packet,
|
2009-03-29 22:09:29 +00:00
|
|
|
packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->rmdir_packet);
|
|
|
|
sftp->rmdir_packet = NULL;
|
|
|
|
sftp->rmdir_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"Unable to send FXP_RMDIR command");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
LIBSSH2_FREE(session, sftp->rmdir_packet);
|
|
|
|
sftp->rmdir_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->rmdir_state = libssh2_NB_state_sent;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2008-12-16 15:35:33 +00:00
|
|
|
rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
|
|
|
|
sftp->rmdir_request_id, &data, &data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->rmdir_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-06-07 16:01:12 +00:00
|
|
|
|
|
|
|
sftp->rmdir_state = libssh2_NB_state_idle;
|
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (retcode == LIBSSH2_FX_OK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sftp->last_errno = retcode;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_rmdir_ex
|
|
|
|
* Remove a directory
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API int
|
2009-03-26 15:41:14 +00:00
|
|
|
libssh2_sftp_rmdir_ex(LIBSSH2_SFTP *sftp, const char *path,
|
|
|
|
unsigned int path_len)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session,
|
|
|
|
sftp_rmdir(sftp, path, path_len));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sftp_stat
|
|
|
|
* Stat a file or symbolic link
|
|
|
|
*/
|
|
|
|
static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned int path_len, int stat_type,
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES * attrs)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len;
|
2007-06-07 16:01:12 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
|
2007-08-06 20:48:04 +00:00
|
|
|
ssize_t packet_len =
|
|
|
|
path_len + 13 +
|
|
|
|
((stat_type ==
|
2010-04-28 00:23:09 +02:00
|
|
|
LIBSSH2_SFTP_SETSTAT) ? sftp_attrsize(attrs->flags) : 0);
|
2007-06-07 16:01:12 +00:00
|
|
|
unsigned char *s, *data;
|
2007-08-06 20:48:04 +00:00
|
|
|
static const unsigned char stat_responses[2] =
|
|
|
|
{ SSH_FXP_ATTRS, SSH_FXP_STATUS };
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->stat_state == libssh2_NB_state_idle) {
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%s %s",
|
2007-06-07 16:01:12 +00:00
|
|
|
(stat_type == LIBSSH2_SFTP_SETSTAT) ? "Set-statting" :
|
2007-08-06 20:48:04 +00:00
|
|
|
(stat_type ==
|
|
|
|
LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), path);
|
2007-06-07 16:01:12 +00:00
|
|
|
s = sftp->stat_packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!sftp->stat_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for FXP_*STAT "
|
|
|
|
"packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
switch (stat_type) {
|
2007-08-06 20:48:04 +00:00
|
|
|
case LIBSSH2_SFTP_SETSTAT:
|
|
|
|
*(s++) = SSH_FXP_SETSTAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBSSH2_SFTP_LSTAT:
|
|
|
|
*(s++) = SSH_FXP_LSTAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBSSH2_SFTP_STAT:
|
|
|
|
default:
|
|
|
|
*(s++) = SSH_FXP_STAT;
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
sftp->stat_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->stat_request_id);
|
|
|
|
_libssh2_store_str(&s, path, path_len);
|
|
|
|
|
|
|
|
if (stat_type == LIBSSH2_SFTP_SETSTAT)
|
2009-03-23 13:35:20 +00:00
|
|
|
s += sftp_attr2bin(s, attrs);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->stat_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->stat_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, sftp->stat_packet, packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->stat_packet);
|
|
|
|
sftp->stat_packet = NULL;
|
|
|
|
sftp->stat_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"Unable to send STAT/LSTAT/SETSTAT command");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
LIBSSH2_FREE(session, sftp->stat_packet);
|
|
|
|
sftp->stat_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->stat_state = libssh2_NB_state_sent;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2008-12-15 22:58:07 +00:00
|
|
|
rc = sftp_packet_requirev(sftp, 2, stat_responses,
|
|
|
|
sftp->stat_request_id, &data, &data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->stat_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->stat_state = libssh2_NB_state_idle;
|
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (data[0] == SSH_FXP_STATUS) {
|
|
|
|
int retcode;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-02-02 16:21:20 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-05-28 17:56:08 +00:00
|
|
|
if (retcode == LIBSSH2_FX_OK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sftp->last_errno = retcode;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
|
2009-03-23 13:35:20 +00:00
|
|
|
sftp_bin2attr(attrs, data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return 0;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_stat_ex
|
|
|
|
* Stat a file or symbolic link
|
2004-12-22 00:20:02 +00:00
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API int
|
2009-03-26 15:41:14 +00:00
|
|
|
libssh2_sftp_stat_ex(LIBSSH2_SFTP *sftp, const char *path,
|
|
|
|
unsigned int path_len, int stat_type,
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES *attrs)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session,
|
|
|
|
sftp_stat(sftp, path, path_len, stat_type, attrs));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sftp_symlink
|
|
|
|
* Read or set a symlink
|
|
|
|
*/
|
|
|
|
static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
|
2007-08-06 20:48:04 +00:00
|
|
|
unsigned int path_len, char *target,
|
|
|
|
unsigned int target_len, int link_type)
|
2004-12-22 00:20:02 +00:00
|
|
|
{
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_CHANNEL *channel = sftp->channel;
|
|
|
|
LIBSSH2_SESSION *session = channel->session;
|
2010-04-15 20:36:31 +02:00
|
|
|
size_t data_len, link_len;
|
2007-06-07 16:01:12 +00:00
|
|
|
/* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
|
2007-08-06 20:48:04 +00:00
|
|
|
ssize_t packet_len =
|
|
|
|
path_len + 13 +
|
|
|
|
((link_type == LIBSSH2_SFTP_SYMLINK) ? (4 + target_len) : 0);
|
2007-06-07 16:01:12 +00:00
|
|
|
unsigned char *s, *data;
|
2007-08-06 20:48:04 +00:00
|
|
|
static const unsigned char link_responses[2] =
|
|
|
|
{ SSH_FXP_NAME, SSH_FXP_STATUS };
|
2007-05-28 17:56:08 +00:00
|
|
|
int rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
|
|
|
if ((sftp->version < 3) && (link_type != LIBSSH2_SFTP_REALPATH)) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"Server does not support SYMLINK or READLINK");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->symlink_state == libssh2_NB_state_idle) {
|
|
|
|
s = sftp->symlink_packet = LIBSSH2_ALLOC(session, packet_len);
|
|
|
|
if (!sftp->symlink_packet) {
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
|
|
|
|
"Unable to allocate memory for "
|
|
|
|
"SYMLINK/READLINK/REALPATH packet");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-12-08 08:52:03 +01:00
|
|
|
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%s %s on %s",
|
2007-08-06 20:48:04 +00:00
|
|
|
(link_type ==
|
|
|
|
LIBSSH2_SFTP_SYMLINK) ? "Creating" : "Reading",
|
|
|
|
(link_type ==
|
|
|
|
LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", path);
|
|
|
|
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, packet_len - 4);
|
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
switch (link_type) {
|
2007-08-06 20:48:04 +00:00
|
|
|
case LIBSSH2_SFTP_REALPATH:
|
|
|
|
*(s++) = SSH_FXP_REALPATH;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBSSH2_SFTP_SYMLINK:
|
|
|
|
*(s++) = SSH_FXP_SYMLINK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBSSH2_SFTP_READLINK:
|
|
|
|
default:
|
|
|
|
*(s++) = SSH_FXP_READLINK;
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
sftp->symlink_request_id = sftp->request_id++;
|
2010-04-17 13:18:15 +02:00
|
|
|
_libssh2_store_u32(&s, sftp->symlink_request_id);
|
|
|
|
_libssh2_store_str(&s, path, path_len);
|
|
|
|
|
|
|
|
if (link_type == LIBSSH2_SFTP_SYMLINK)
|
|
|
|
_libssh2_store_str(&s, target, target_len);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->symlink_state = libssh2_NB_state_created;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
if (sftp->symlink_state == libssh2_NB_state_created) {
|
2010-10-23 00:11:59 +02:00
|
|
|
rc = _libssh2_channel_write(channel, 0, sftp->symlink_packet,
|
2009-03-29 22:09:29 +00:00
|
|
|
packet_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2007-08-06 20:48:04 +00:00
|
|
|
} else if (packet_len != rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
LIBSSH2_FREE(session, sftp->symlink_packet);
|
|
|
|
sftp->symlink_packet = NULL;
|
|
|
|
sftp->symlink_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
|
|
|
|
"Unable to send SYMLINK/READLINK command");
|
2007-06-07 16:01:12 +00:00
|
|
|
}
|
|
|
|
LIBSSH2_FREE(session, sftp->symlink_packet);
|
|
|
|
sftp->symlink_packet = NULL;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->symlink_state = libssh2_NB_state_sent;
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2008-12-15 22:58:07 +00:00
|
|
|
rc = sftp_packet_requirev(sftp, 2, link_responses,
|
|
|
|
sftp->symlink_request_id, &data,
|
|
|
|
&data_len);
|
2010-04-17 13:18:15 +02:00
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) {
|
2009-08-25 00:54:47 +02:00
|
|
|
return rc;
|
2008-12-15 22:58:07 +00:00
|
|
|
}
|
|
|
|
else if (rc) {
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->symlink_state = libssh2_NB_state_idle;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
|
|
|
"Timeout waiting for status message");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-06-07 16:01:12 +00:00
|
|
|
sftp->symlink_state = libssh2_NB_state_idle;
|
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
if (data[0] == SSH_FXP_STATUS) {
|
|
|
|
int retcode;
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
retcode = _libssh2_ntohu32(data + 5);
|
2007-05-28 17:56:08 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
|
|
|
if (retcode == LIBSSH2_FX_OK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
sftp->last_errno = retcode;
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"SFTP Protocol Error");
|
2007-02-02 16:21:20 +00:00
|
|
|
}
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
if (_libssh2_ntohu32(data + 5) < 1) {
|
2007-02-02 16:21:20 +00:00
|
|
|
LIBSSH2_FREE(session, data);
|
2010-04-16 00:18:51 +02:00
|
|
|
return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
|
|
|
|
"Invalid READLINK/REALPATH response, "
|
|
|
|
"no name entries");
|
2007-05-28 17:56:08 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-17 13:48:35 +00:00
|
|
|
link_len = _libssh2_ntohu32(data + 9);
|
2007-05-28 17:56:08 +00:00
|
|
|
if (link_len >= target_len) {
|
|
|
|
link_len = target_len - 1;
|
|
|
|
}
|
|
|
|
memcpy(target, data + 13, link_len);
|
|
|
|
target[link_len] = 0;
|
|
|
|
LIBSSH2_FREE(session, data);
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2007-05-28 17:56:08 +00:00
|
|
|
return link_len;
|
2004-12-22 00:20:02 +00:00
|
|
|
}
|
2007-08-06 20:48:04 +00:00
|
|
|
|
2009-03-26 15:41:14 +00:00
|
|
|
/* libssh2_sftp_symlink_ex
|
|
|
|
* Read or set a symlink
|
|
|
|
*/
|
|
|
|
LIBSSH2_API int
|
|
|
|
libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp, const char *path,
|
|
|
|
unsigned int path_len, char *target,
|
|
|
|
unsigned int target_len, int link_type)
|
|
|
|
{
|
|
|
|
int rc;
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return LIBSSH2_ERROR_BAD_USE;
|
2009-03-26 15:41:14 +00:00
|
|
|
BLOCK_ADJUST(rc, sftp->channel->session,
|
|
|
|
sftp_symlink(sftp, path, path_len, target, target_len,
|
|
|
|
link_type));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-03-23 13:35:20 +00:00
|
|
|
/* libssh2_sftp_last_error
|
2004-12-22 21:55:08 +00:00
|
|
|
* Returns the last error code reported by SFTP
|
|
|
|
*/
|
2007-08-06 20:48:04 +00:00
|
|
|
LIBSSH2_API unsigned long
|
2009-03-26 15:41:14 +00:00
|
|
|
libssh2_sftp_last_error(LIBSSH2_SFTP *sftp)
|
2004-12-22 21:55:08 +00:00
|
|
|
{
|
2010-06-11 13:05:55 +02:00
|
|
|
if(!sftp)
|
|
|
|
return 0;
|
|
|
|
|
2007-04-21 18:16:23 +00:00
|
|
|
return sftp->last_errno;
|
|
|
|
}
|