~ubuntu-branches/ubuntu/trusty/libssh/trusty

« back to all changes in this revision

Viewing changes to examples/scp_download.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-12-12 14:29:12 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20091212142912-ha5g2iibt6nfnjq8
Tags: 0.4.0-1
* New upstream release.
  - Bump soname
  - Adjust .symbols file
* Readd static library in -dev package
* Let dh_lintian install override file
* debian/README.Debian: Update file
* debian/rules: Add list-missing rule

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* scp_download.c
 
2
 * Sample implementation of a tiny SCP downloader client
 
3
 */
 
4
 
 
5
/*
 
6
Copyright 2009 Aris Adamantiadis
 
7
 
 
8
This file is part of the SSH Library
 
9
 
 
10
You are free to copy this file, modify it in any way, consider it being public
 
11
domain. This does not apply to the rest of the library though, but it is
 
12
allowed to cut-and-paste working code from this file to any license of
 
13
program.
 
14
 */
 
15
 
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <string.h>
 
19
#include <errno.h>
 
20
#include <sys/stat.h>
 
21
 
 
22
#include <libssh/libssh.h>
 
23
#include "examples_common.h"
 
24
 
 
25
int verbosity=0;
 
26
const char *createcommand="rm -fr /tmp/libssh_tests && mkdir /tmp/libssh_tests && cd /tmp/libssh_tests && date > a && date > b && mkdir c && date > d";
 
27
char *host=NULL;
 
28
static void usage(const char *argv0){
 
29
  fprintf(stderr,"Usage : %s [options] host\n"
 
30
      "sample tiny scp downloader client - libssh-%s\n"
 
31
          "This program will create files in /tmp and try to fetch them\n",
 
32
//      "Options :\n",
 
33
//      "  -r : use RSA to verify host public key\n",
 
34
      argv0,
 
35
      ssh_version(0));
 
36
  exit(0);
 
37
}
 
38
 
 
39
static int opts(int argc, char **argv){
 
40
  int i;
 
41
  while((i=getopt(argc,argv,"v"))!=-1){
 
42
    switch(i){
 
43
      case 'v':
 
44
        verbosity++;
 
45
        break;
 
46
      default:
 
47
        fprintf(stderr,"unknown option %c\n",optopt);
 
48
        usage(argv[0]);
 
49
        return -1;
 
50
    }
 
51
  }
 
52
  host = argv[optind];
 
53
  if(host == NULL)
 
54
          usage(argv[0]);
 
55
  return 0;
 
56
}
 
57
 
 
58
static void create_files(ssh_session session){
 
59
        ssh_channel channel=channel_new(session);
 
60
        char buffer[1];
 
61
        if(channel == NULL){
 
62
                fprintf(stderr,"Error creating channel: %s\n",ssh_get_error(session));
 
63
                exit(EXIT_FAILURE);
 
64
        }
 
65
        if(channel_open_session(channel) != SSH_OK){
 
66
                fprintf(stderr,"Error creating channel: %s\n",ssh_get_error(session));
 
67
                exit(EXIT_FAILURE);
 
68
        }
 
69
        if(channel_request_exec(channel,createcommand) != SSH_OK){
 
70
                fprintf(stderr,"Error executing command: %s\n",ssh_get_error(session));
 
71
                exit(EXIT_FAILURE);
 
72
        }
 
73
        while(!channel_is_eof(channel)){
 
74
                channel_read(channel,buffer,1,1);
 
75
                write(1,buffer,1);
 
76
        }
 
77
        channel_close(channel);
 
78
        channel_free(channel);
 
79
}
 
80
 
 
81
 
 
82
static int fetch_files(ssh_session session){
 
83
  int size;
 
84
  char buffer[16384];
 
85
  int mode;
 
86
  char *filename;
 
87
  int r;
 
88
  ssh_scp scp=ssh_scp_new(session, SSH_SCP_READ | SSH_SCP_RECURSIVE, "/tmp/libssh_tests/*");
 
89
  if(ssh_scp_init(scp) != SSH_OK){
 
90
          fprintf(stderr,"error initializing scp: %s\n",ssh_get_error(session));
 
91
          return -1;
 
92
  }
 
93
  printf("Trying to download 3 files (a,b,d) and 1 directory (c)\n");
 
94
  do {
 
95
 
 
96
          r=ssh_scp_pull_request(scp);
 
97
          switch(r){
 
98
          case SSH_SCP_REQUEST_NEWFILE:
 
99
                  size=ssh_scp_request_get_size(scp);
 
100
                  filename=strdup(ssh_scp_request_get_filename(scp));
 
101
                  mode=ssh_scp_request_get_permissions(scp);
 
102
                  printf("downloading file %s, size %d, perms 0%o\n",filename,size,mode);
 
103
                  free(filename);
 
104
                  ssh_scp_accept_request(scp);
 
105
                  r=ssh_scp_read(scp,buffer,sizeof(buffer));
 
106
                  if(r==SSH_ERROR){
 
107
                          fprintf(stderr,"Error reading scp: %s\n",ssh_get_error(session));
 
108
                          return -1;
 
109
                  }
 
110
                  printf("done\n");
 
111
                  break;
 
112
          case SSH_ERROR:
 
113
                  fprintf(stderr,"Error: %s\n",ssh_get_error(session));
 
114
                  return -1;
 
115
          case SSH_SCP_REQUEST_WARNING:
 
116
                  fprintf(stderr,"Warning: %s\n",ssh_scp_request_get_warning(scp));
 
117
                  break;
 
118
          case SSH_SCP_REQUEST_NEWDIR:
 
119
                  filename=strdup(ssh_scp_request_get_filename(scp));
 
120
                  mode=ssh_scp_request_get_permissions(scp);
 
121
                  printf("downloading directory %s, perms 0%o\n",filename,mode);
 
122
                  free(filename);
 
123
                  ssh_scp_accept_request(scp);
 
124
                  break;
 
125
          case SSH_SCP_REQUEST_ENDDIR:
 
126
                  printf("End of directory\n");
 
127
                  break;
 
128
          case SSH_SCP_REQUEST_EOF:
 
129
                  printf("End of requests\n");
 
130
                  goto end;
 
131
          }
 
132
  } while (1);
 
133
  end:
 
134
  return 0;
 
135
}
 
136
 
 
137
int main(int argc, char **argv){
 
138
  ssh_session session;
 
139
  if(opts(argc,argv)<0)
 
140
    return EXIT_FAILURE;
 
141
  session=connect_ssh(host,NULL,verbosity);
 
142
  if(session == NULL)
 
143
          return EXIT_FAILURE;
 
144
  create_files(session);
 
145
  fetch_files(session);
 
146
  ssh_disconnect(session);
 
147
  ssh_finalize();
 
148
  return 0;
 
149
}