1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-22 08:08:50 -05:00

Commit Tom Regner's code for SFTP create missing directories. This patch

uses the --ftp-create-dirs flag to control if cURL will try and create
directories that are specified in an upload path, but don't exist.
This commit is contained in:
James Housley 2007-06-13 12:15:23 +00:00
parent 3ec7f8a25a
commit 96f4af4db9

View File

@ -110,12 +110,6 @@
#define _MPRINTF_REPLACE /* use our functions only */ #define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h> #include <curl/mprintf.h>
#if defined(WIN32) || defined(MSDOS) || defined(__EMX__)
#define DIRSEP '\\'
#else
#define DIRSEP '/'
#endif
#define _MPRINTF_REPLACE /* use our functions only */ #define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h> #include <curl/mprintf.h>
@ -136,31 +130,37 @@
#define S_IROTH 0 #define S_IROTH 0
#endif #endif
#define LIBSSH2_SFTP_S_IRUSR S_IRUSR /* File mode */
#define LIBSSH2_SFTP_S_IWUSR S_IWUSR /* Read, write, execute/search by owner */
#define LIBSSH2_SFTP_S_IRGRP S_IRGRP #define LIBSSH2_SFTP_S_IRWXU S_IRWXU /* RWX mask for owner */
#define LIBSSH2_SFTP_S_IROTH S_IROTH #define LIBSSH2_SFTP_S_IRUSR S_IRUSR /* R for owner */
#define LIBSSH2_SFTP_S_IRUSR S_IRUSR #define LIBSSH2_SFTP_S_IWUSR S_IWUSR /* W for owner */
#define LIBSSH2_SFTP_S_IWUSR S_IWUSR #define LIBSSH2_SFTP_S_IXUSR S_IXUSR /* X for owner */
#define LIBSSH2_SFTP_S_IRGRP S_IRGRP /* Read, write, execute/search by group */
#define LIBSSH2_SFTP_S_IROTH S_IROTH #define LIBSSH2_SFTP_S_IRWXG S_IRWXG /* RWX mask for group */
#define LIBSSH2_SFTP_S_IFMT S_IFMT #define LIBSSH2_SFTP_S_IRGRP S_IRGRP /* R for group */
#define LIBSSH2_SFTP_S_IFDIR S_IFDIR #define LIBSSH2_SFTP_S_IWGRP S_IWGRP /* W for group */
#define LIBSSH2_SFTP_S_IFLNK S_IFLNK #define LIBSSH2_SFTP_S_IXGRP S_IXGRP /* X for group */
#define LIBSSH2_SFTP_S_IFSOCK S_IFSOCK /* Read, write, execute/search by others */
#define LIBSSH2_SFTP_S_IFCHR S_IFCHR #define LIBSSH2_SFTP_S_IRWXO S_IRWXO /* RWX mask for other */
#define LIBSSH2_SFTP_S_IFBLK S_IFBLK #define LIBSSH2_SFTP_S_IROTH S_IROTH /* R for other */
#define LIBSSH2_SFTP_S_IXUSR S_IXUSR #define LIBSSH2_SFTP_S_IWOTH S_IWOTH /* W for other */
#define LIBSSH2_SFTP_S_IWGRP S_IWGRP #define LIBSSH2_SFTP_S_IXOTH S_IXOTH /* X for other */
#define LIBSSH2_SFTP_S_IXGRP S_IXGRP
#define LIBSSH2_SFTP_S_IWOTH S_IWOTH /* File type */
#define LIBSSH2_SFTP_S_IXOTH S_IXOTH #define LIBSSH2_SFTP_S_IFMT S_IFMT /* type of file mask */
#define LIBSSH2_SFTP_S_IFDIR S_IFDIR /* directory */
#define LIBSSH2_SFTP_S_IFLNK S_IFLNK /* symbolic link */
#define LIBSSH2_SFTP_S_IFSOCK S_IFSOCK /* socket */
#define LIBSSH2_SFTP_S_IFCHR S_IFCHR /* character special */
#define LIBSSH2_SFTP_S_IFBLK S_IFBLK /* block special */
#endif #endif
/* Local functions: */ /* Local functions: */
static const char *sftp_libssh2_strerror(unsigned long err); static const char *sftp_libssh2_strerror(unsigned long err);
static CURLcode sftp_sendquote(struct connectdata *conn, static CURLcode sftp_sendquote(struct connectdata *conn,
struct curl_slist *quote); struct curl_slist *quote);
static CURLcode sftp_create_dirs(struct connectdata *conn);
static LIBSSH2_ALLOC_FUNC(libssh2_malloc); static LIBSSH2_ALLOC_FUNC(libssh2_malloc);
static LIBSSH2_REALLOC_FUNC(libssh2_realloc); static LIBSSH2_REALLOC_FUNC(libssh2_realloc);
@ -1348,6 +1348,24 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp->sftp_handle &&
(libssh2_session_last_errno(sftp->ssh_session) !=
LIBSSH2_ERROR_EAGAIN)) {
err = libssh2_sftp_last_error(sftp->sftp_session);
if (((err == LIBSSH2_FX_NO_SUCH_FILE) ||
(err == LIBSSH2_FX_FAILURE) ||
(err == LIBSSH2_FX_NO_SUCH_PATH)) &&
(conn->data->set.ftp_create_missing_dirs &&
(strlen(sftp->path) > 1))) {
/* try to create the path remotely */
res = sftp_create_dirs(conn);
if (res == 0) {
do {
sftp->sftp_handle = libssh2_sftp_open(sftp->sftp_session,
sftp->path,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp->sftp_handle && if (!sftp->sftp_handle &&
(libssh2_session_last_errno(sftp->ssh_session) != (libssh2_session_last_errno(sftp->ssh_session) !=
LIBSSH2_ERROR_EAGAIN)) { LIBSSH2_ERROR_EAGAIN)) {
@ -1357,18 +1375,45 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
return sftp_libssh2_error_to_CURLE(err); return sftp_libssh2_error_to_CURLE(err);
} }
} while (!sftp->sftp_handle); } while (!sftp->sftp_handle);
}
}
if (!sftp->sftp_handle) {
err = libssh2_sftp_last_error(sftp->sftp_session);
failf(conn->data, "Could not open remote file for writing: %s",
sftp_libssh2_strerror(err));
return sftp_libssh2_error_to_CURLE(err);
}
}
} while (!sftp->sftp_handle);
#else /* !(LIBSSH2_APINO >= 200706012030) */ #else /* !(LIBSSH2_APINO >= 200706012030) */
sftp->sftp_handle = sftp->sftp_handle =
libssh2_sftp_open(sftp->sftp_session, sftp->path, libssh2_sftp_open(sftp->sftp_session, sftp->path,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp->sftp_handle) {
err = libssh2_sftp_last_error(sftp->sftp_session);
if (((err == LIBSSH2_FX_NO_SUCH_FILE) ||
(err == LIBSSH2_FX_FAILURE) ||
(err == LIBSSH2_FX_NO_SUCH_PATH)) &&
(conn->data->set.ftp_create_missing_dirs &&
(strlen(sftp->path) > 1))) {
/* try to create the path remotely */
res = sftp_create_dirs(conn);
if (res == 0) {
sftp->sftp_handle = libssh2_sftp_open(sftp->sftp_session, sftp->path,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
}
}
if (!sftp->sftp_handle) { if (!sftp->sftp_handle) {
err = libssh2_sftp_last_error(sftp->sftp_session); err = libssh2_sftp_last_error(sftp->sftp_session);
failf(conn->data, "Could not open remote file for writing: %s", failf(conn->data, "Could not open remote file for writing: %s",
sftp_libssh2_strerror(err)); sftp_libssh2_strerror(err));
return sftp_libssh2_error_to_CURLE(err); return sftp_libssh2_error_to_CURLE(err);
} }
}
#endif /* !(LIBSSH2_APINO >= 200706012030) */ #endif /* !(LIBSSH2_APINO >= 200706012030) */
/* upload data */ /* upload data */