[BACK]Return to sftp-int.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Diff for /src/usr.bin/ssh/Attic/sftp-int.c between version 1.46 and 1.46.2.2

version 1.46, 2002/03/30 18:51:15 version 1.46.2.2, 2002/10/11 14:51:52
Line 22 
Line 22 
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */   */
   
 /* XXX: globbed ls */  
 /* XXX: recursive operations */  /* XXX: recursive operations */
   
 #include "includes.h"  #include "includes.h"
Line 203 
Line 202 
         }          }
 }  }
   
   /* Strip one path (usually the pwd) from the start of another */
 static char *  static char *
   path_strip(char *path, char *strip)
   {
           size_t len;
   
           if (strip == NULL)
                   return (xstrdup(path));
   
           len = strlen(strip);
           if (strip != NULL && strncmp(path, strip, len) == 0) {
                   if (strip[len - 1] != '/' && path[len] == '/')
                           len++;
                   return (xstrdup(path + len));
           }
   
           return (xstrdup(path));
   }
   
   static char *
 path_append(char *p1, char *p2)  path_append(char *p1, char *p2)
 {  {
         char *ret;          char *ret;
Line 211 
Line 229 
   
         ret = xmalloc(len);          ret = xmalloc(len);
         strlcpy(ret, p1, len);          strlcpy(ret, p1, len);
         if (strcmp(p1, "/") != 0)          if (p1[strlen(p1) - 1] != '/')
                 strlcat(ret, "/", len);                  strlcat(ret, "/", len);
         strlcat(ret, p2, len);          strlcat(ret, p2, len);
   
Line 276 
Line 294 
 }  }
   
 static int  static int
   parse_ls_flags(const char **cpp, int *lflag)
   {
           const char *cp = *cpp;
   
           /* Check for flags */
           if (cp++[0] == '-') {
                   for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
                           switch (*cp) {
                           case 'l':
                                   *lflag = 1;
                                   break;
                           default:
                                   error("Invalid flag -%c", *cp);
                                   return(-1);
                           }
                   }
                   *cpp = cp + strspn(cp, WHITESPACE);
           }
   
           return(0);
   }
   
   static int
 get_pathname(const char **cpp, char **path)  get_pathname(const char **cpp, char **path)
 {  {
         const char *cp = *cpp, *end;          const char *cp = *cpp, *end;
Line 506 
Line 547 
 }  }
   
 static int  static int
 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,  sdirent_comp(const void *aa, const void *bb)
     char **path1, char **path2)  
 {  {
           SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
           SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
   
           return (strcmp(a->filename, b->filename));
   }
   
   /* sftp ls.1 replacement for directories */
   static int
   do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
   {
           int n;
           SFTP_DIRENT **d;
   
           if ((n = do_readdir(conn, path, &d)) != 0)
                   return (n);
   
           /* Count entries for sort */
           for (n = 0; d[n] != NULL; n++)
                   ;
   
           qsort(d, n, sizeof(*d), sdirent_comp);
   
           for (n = 0; d[n] != NULL; n++) {
                   char *tmp, *fname;
   
                   tmp = path_append(path, d[n]->filename);
                   fname = path_strip(tmp, strip_path);
                   xfree(tmp);
   
                   if (lflag) {
                           char *lname;
                           struct stat sb;
   
                           memset(&sb, 0, sizeof(sb));
                           attrib_to_stat(&d[n]->a, &sb);
                           lname = ls_file(fname, &sb, 1);
                           printf("%s\n", lname);
                           xfree(lname);
                   } else {
                           /* XXX - multicolumn display would be nice here */
                           printf("%s\n", fname);
                   }
   
                   xfree(fname);
           }
   
           free_sftp_dirents(d);
           return (0);
   }
   
   /* sftp ls.1 replacement which handles path globs */
   static int
   do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
       int lflag)
   {
           glob_t g;
           int i;
           Attrib *a;
           struct stat sb;
   
           memset(&g, 0, sizeof(g));
   
           if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
               NULL, &g)) {
                   error("Can't ls: \"%s\" not found", path);
                   return (-1);
           }
   
           /*
            * If the glob returns a single match, which is the same as the
            * input glob, and it is a directory, then just list its contents
            */
           if (g.gl_pathc == 1 &&
               strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
                   if ((a = do_lstat(conn, path, 1)) == NULL) {
                           globfree(&g);
                           return (-1);
                   }
                   if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
                       S_ISDIR(a->perm)) {
                           globfree(&g);
                           return (do_ls_dir(conn, path, strip_path, lflag));
                   }
           }
   
           for (i = 0; g.gl_pathv[i]; i++) {
                   char *fname, *lname;
   
                   fname = path_strip(g.gl_pathv[i], strip_path);
   
                   if (lflag) {
                           /*
                            * XXX: this is slow - 1 roundtrip per path
                            * A solution to this is to fork glob() and
                            * build a sftp specific version which keeps the
                            * attribs (which currently get thrown away)
                            * that the server returns as well as the filenames.
                            */
                           memset(&sb, 0, sizeof(sb));
                           a = do_lstat(conn, g.gl_pathv[i], 1);
                           if (a != NULL)
                                   attrib_to_stat(a, &sb);
                           lname = ls_file(fname, &sb, 1);
                           printf("%s\n", lname);
                           xfree(lname);
                   } else {
                           /* XXX - multicolumn display would be nice here */
                           printf("%s\n", fname);
                   }
                   xfree(fname);
           }
   
           if (g.gl_pathc)
                   globfree(&g);
   
           return (0);
   }
   
   static int
   parse_args(const char **cpp, int *pflag, int *lflag,
       unsigned long *n_arg, char **path1, char **path2)
   {
         const char *cmd, *cp = *cpp;          const char *cmd, *cp = *cpp;
         char *cp2;          char *cp2;
         int base = 0;          int base = 0;
Line 547 
Line 709 
         }          }
   
         /* Get arguments and parse flags */          /* Get arguments and parse flags */
         *pflag = *n_arg = 0;          *lflag = *pflag = *n_arg = 0;
         *path1 = *path2 = NULL;          *path1 = *path2 = NULL;
         switch (cmdnum) {          switch (cmdnum) {
         case I_GET:          case I_GET:
Line 594 
Line 756 
                 }                  }
                 break;                  break;
         case I_LS:          case I_LS:
                   if (parse_ls_flags(&cp, lflag))
                           return(-1);
                 /* Path is optional */                  /* Path is optional */
                 if (get_pathname(&cp, path1))                  if (get_pathname(&cp, path1))
                         return(-1);                          return(-1);
Line 654 
Line 818 
 parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd)  parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd)
 {  {
         char *path1, *path2, *tmp;          char *path1, *path2, *tmp;
         int pflag, cmdnum, i;          int pflag, lflag, cmdnum, i;
         unsigned long n_arg;          unsigned long n_arg;
         Attrib a, *aa;          Attrib a, *aa;
         char path_buf[MAXPATHLEN];          char path_buf[MAXPATHLEN];
Line 662 
Line 826 
         glob_t g;          glob_t g;
   
         path1 = path2 = NULL;          path1 = path2 = NULL;
         cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);          cmdnum = parse_args(&cmd, &pflag, &lflag, &n_arg,
               &path1, &path2);
   
         memset(&g, 0, sizeof(g));          memset(&g, 0, sizeof(g));
   
Line 734 
Line 899 
                 break;                  break;
         case I_LS:          case I_LS:
                 if (!path1) {                  if (!path1) {
                         do_ls(conn, *pwd);                          do_globbed_ls(conn, *pwd, *pwd, lflag);
                         break;                          break;
                 }                  }
   
                   /* Strip pwd off beginning of non-absolute paths */
                   tmp = NULL;
                   if (*path1 != '/')
                           tmp = *pwd;
   
                 path1 = make_absolute(path1, *pwd);                  path1 = make_absolute(path1, *pwd);
                 if ((tmp = do_realpath(conn, path1)) == NULL)  
                         break;                  do_globbed_ls(conn, path1, tmp, lflag);
                 xfree(path1);  
                 path1 = tmp;  
                 if ((aa = do_stat(conn, path1, 0)) == NULL)  
                         break;  
                 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&  
                     !S_ISDIR(aa->perm)) {  
                         error("Can't ls: \"%s\" is not a directory", path1);  
                         break;  
                 }  
                 do_ls(conn, path1);  
                 break;                  break;
         case I_LCHDIR:          case I_LCHDIR:
                 if (chdir(path1) == -1) {                  if (chdir(path1) == -1) {
Line 837 
Line 998 
                 help();                  help();
                 break;                  break;
         case I_VERSION:          case I_VERSION:
                 printf("SFTP protocol version %d\n", sftp_proto_version(conn));                  printf("SFTP protocol version %u\n", sftp_proto_version(conn));
                 break;                  break;
         default:          default:
                 fatal("%d is not implemented", cmdnum);                  fatal("%d is not implemented", cmdnum);

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.46.2.2