Commit 96f4af4d authored by James Housley's avatar James Housley
Browse files

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.
parent 3ec7f8a2
Loading
Loading
Loading
Loading
+136 −91
Original line number Diff line number Diff line
@@ -110,12 +110,6 @@
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

#if defined(WIN32) || defined(MSDOS) || defined(__EMX__)
#define DIRSEP '\\'
#else
#define DIRSEP '/'
#endif

#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

@@ -136,31 +130,37 @@
#define S_IROTH 0
#endif

#define LIBSSH2_SFTP_S_IRUSR S_IRUSR
#define LIBSSH2_SFTP_S_IWUSR S_IWUSR
#define LIBSSH2_SFTP_S_IRGRP S_IRGRP
#define LIBSSH2_SFTP_S_IROTH S_IROTH
#define LIBSSH2_SFTP_S_IRUSR S_IRUSR
#define LIBSSH2_SFTP_S_IWUSR S_IWUSR
#define LIBSSH2_SFTP_S_IRGRP S_IRGRP
#define LIBSSH2_SFTP_S_IROTH S_IROTH
#define LIBSSH2_SFTP_S_IFMT S_IFMT
#define LIBSSH2_SFTP_S_IFDIR S_IFDIR
#define LIBSSH2_SFTP_S_IFLNK S_IFLNK
#define LIBSSH2_SFTP_S_IFSOCK S_IFSOCK
#define LIBSSH2_SFTP_S_IFCHR S_IFCHR
#define LIBSSH2_SFTP_S_IFBLK S_IFBLK
#define LIBSSH2_SFTP_S_IXUSR S_IXUSR
#define LIBSSH2_SFTP_S_IWGRP S_IWGRP
#define LIBSSH2_SFTP_S_IXGRP S_IXGRP
#define LIBSSH2_SFTP_S_IWOTH S_IWOTH
#define LIBSSH2_SFTP_S_IXOTH S_IXOTH
/* File mode */
/* Read, write, execute/search by owner */
#define LIBSSH2_SFTP_S_IRWXU  S_IRWXU     /* RWX mask for owner */
#define LIBSSH2_SFTP_S_IRUSR  S_IRUSR     /* R for owner */
#define LIBSSH2_SFTP_S_IWUSR  S_IWUSR     /* W for owner */
#define LIBSSH2_SFTP_S_IXUSR  S_IXUSR     /* X for owner */
/* Read, write, execute/search by group */
#define LIBSSH2_SFTP_S_IRWXG  S_IRWXG     /* RWX mask for group */
#define LIBSSH2_SFTP_S_IRGRP  S_IRGRP     /* R for group */
#define LIBSSH2_SFTP_S_IWGRP  S_IWGRP     /* W for group */
#define LIBSSH2_SFTP_S_IXGRP  S_IXGRP     /* X for group */
/* Read, write, execute/search by others */
#define LIBSSH2_SFTP_S_IRWXO  S_IRWXO     /* RWX mask for other */
#define LIBSSH2_SFTP_S_IROTH  S_IROTH     /* R for other */
#define LIBSSH2_SFTP_S_IWOTH  S_IWOTH     /* W for other */
#define LIBSSH2_SFTP_S_IXOTH  S_IXOTH     /* X for other */

/* File type */
#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

/* Local functions: */
static const char *sftp_libssh2_strerror(unsigned long err);
static CURLcode sftp_sendquote(struct connectdata *conn,
                               struct curl_slist *quote);
static CURLcode sftp_create_dirs(struct connectdata *conn);

static LIBSSH2_ALLOC_FUNC(libssh2_malloc);
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_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
                        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 &&
                  (libssh2_session_last_errno(sftp->ssh_session) !=
                   LIBSSH2_ERROR_EAGAIN)) {
@@ -1357,18 +1375,45 @@ CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
                return sftp_libssh2_error_to_CURLE(err);
              }
            } 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) */
    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) {
      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) {
        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);
      }
    }
#endif /* !(LIBSSH2_APINO >= 200706012030) */

    /* upload data */