]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: shuffle a few utility functions into sftp-client.c; from
authordjm@openbsd.org <djm@openbsd.org>
Fri, 4 Dec 2020 02:41:10 +0000 (02:41 +0000)
committerDamien Miller <djm@mindrot.org>
Fri, 4 Dec 2020 02:43:01 +0000 (13:43 +1100)
Jakub Jelen

OpenBSD-Commit-ID: fdeb1aae1f6149b193f12cd2af158f948c514a2a

sftp-client.c
sftp-client.h
sftp.c

index d82e31aeeec493c8fbc7aa74de912b60d33e642b..4603b0098779bf1efc8faf572a7b29a47a05b7cb 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp-client.c,v 1.138 2020/11/20 03:16:56 dtucker Exp $ */
+/* $OpenBSD: sftp-client.c,v 1.139 2020/12/04 02:41:10 djm Exp $ */
 /*
  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
  *
@@ -1959,3 +1959,52 @@ path_append(const char *p1, const char *p2)
        return(ret);
 }
 
+char *
+make_absolute(char *p, const char *pwd)
+{
+       char *abs_str;
+
+       /* Derelativise */
+       if (p && !path_absolute(p)) {
+               abs_str = path_append(pwd, p);
+               free(p);
+               return(abs_str);
+       } else
+               return(p);
+}
+
+int
+remote_is_dir(struct sftp_conn *conn, const char *path)
+{
+       Attrib *a;
+
+       /* XXX: report errors? */
+       if ((a = do_stat(conn, path, 1)) == NULL)
+               return(0);
+       if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
+               return(0);
+       return(S_ISDIR(a->perm));
+}
+
+
+int
+local_is_dir(const char *path)
+{
+       struct stat sb;
+
+       /* XXX: report errors? */
+       if (stat(path, &sb) == -1)
+               return(0);
+
+       return(S_ISDIR(sb.st_mode));
+}
+
+/* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
+int
+globpath_is_dir(const char *pathname)
+{
+       size_t l = strlen(pathname);
+
+       return l > 0 && pathname[l - 1] == '/';
+}
+
index 63a9b8b13bdc3f23eb5f58235735041b590fb6f4..32a24a3c4302d62cd71a895d021ff12695350212 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp-client.h,v 1.28 2019/01/16 23:23:45 djm Exp $ */
+/* $OpenBSD: sftp-client.h,v 1.29 2020/12/04 02:41:10 djm Exp $ */
 
 /*
  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
@@ -142,4 +142,17 @@ int upload_dir(struct sftp_conn *, const char *, const char *, int, int, int,
 /* Concatenate paths, taking care of slashes. Caller must free result. */
 char *path_append(const char *, const char *);
 
+/* Make absolute path if relative path and CWD is given. Does not modify
+ * original if the the path is already absolute. */
+char *make_absolute(char *, const char *);
+
+/* Check if remote path is directory */
+int remote_is_dir(struct sftp_conn *conn, const char *path);
+
+/* Check if local path is directory */
+int local_is_dir(const char *path);
+
+/* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
+int globpath_is_dir(const char *pathname);
+
 #endif
diff --git a/sftp.c b/sftp.c
index b641be2bc4c7094d9520a76c1e3dc61b61e58c39..248d452100d8316723215dd7655c264beef9ea9b 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp.c,v 1.204 2020/10/29 02:52:43 djm Exp $ */
+/* $OpenBSD: sftp.c,v 1.205 2020/12/04 02:41:10 djm Exp $ */
 /*
  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
  *
@@ -385,20 +385,6 @@ path_strip(const char *path, const char *strip)
        return (xstrdup(path));
 }
 
-static char *
-make_absolute(char *p, const char *pwd)
-{
-       char *abs_str;
-
-       /* Derelativise */
-       if (p && !path_absolute(p)) {
-               abs_str = path_append(pwd, p);
-               free(p);
-               return(abs_str);
-       } else
-               return(p);
-}
-
 static int
 parse_getput_flags(const char *cmd, char **argv, int argc,
     int *aflag, int *fflag, int *pflag, int *rflag)
@@ -607,40 +593,6 @@ parse_no_flags(const char *cmd, char **argv, int argc)
        return optind;
 }
 
-static int
-is_dir(const char *path)
-{
-       struct stat sb;
-
-       /* XXX: report errors? */
-       if (stat(path, &sb) == -1)
-               return(0);
-
-       return(S_ISDIR(sb.st_mode));
-}
-
-static int
-remote_is_dir(struct sftp_conn *conn, const char *path)
-{
-       Attrib *a;
-
-       /* XXX: report errors? */
-       if ((a = do_stat(conn, path, 1)) == NULL)
-               return(0);
-       if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
-               return(0);
-       return(S_ISDIR(a->perm));
-}
-
-/* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
-static int
-pathname_is_dir(const char *pathname)
-{
-       size_t l = strlen(pathname);
-
-       return l > 0 && pathname[l - 1] == '/';
-}
-
 static int
 process_get(struct sftp_conn *conn, const char *src, const char *dst,
     const char *pwd, int pflag, int rflag, int resume, int fflag)
@@ -670,7 +622,7 @@ process_get(struct sftp_conn *conn, const char *src, const char *dst,
         * If multiple matches then dst must be a directory or
         * unspecified.
         */
-       if (g.gl_matchc > 1 && dst != NULL && !is_dir(dst)) {
+       if (g.gl_matchc > 1 && dst != NULL && !local_is_dir(dst)) {
                error("Multiple source paths, but destination "
                    "\"%s\" is not a directory", dst);
                err = -1;
@@ -687,7 +639,7 @@ process_get(struct sftp_conn *conn, const char *src, const char *dst,
                }
 
                if (g.gl_matchc == 1 && dst) {
-                       if (is_dir(dst)) {
+                       if (local_is_dir(dst)) {
                                abs_dst = path_append(dst, filename);
                        } else {
                                abs_dst = xstrdup(dst);
@@ -706,7 +658,7 @@ process_get(struct sftp_conn *conn, const char *src, const char *dst,
                else if (!quiet && !resume)
                        mprintf("Fetching %s to %s\n",
                            g.gl_pathv[i], abs_dst);
-               if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
+               if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
                        if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
                            pflag || global_pflag, 1, resume,
                            fflag || global_fflag) == -1)
@@ -799,7 +751,7 @@ process_put(struct sftp_conn *conn, const char *src, const char *dst,
                else if (!quiet && !resume)
                        mprintf("Uploading %s to %s\n",
                            g.gl_pathv[i], abs_dst);
-               if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
+               if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
                        if (upload_dir(conn, g.gl_pathv[i], abs_dst,
                            pflag || global_pflag, 1, resume,
                            fflag || global_fflag) == -1)