source: branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c @ 860

Last change on this file since 860 was 860, checked in by djay, 6 years ago

Add status_code key to the lenv section to support returning a specific HTTP error code from the service code. Fix callback invocation to support inputs arrays at step 1 and 2. Fix issue with cpu usage. Fix issue with mapserver publication when an input is optional. Fix callback invocation at step 7 in case the service has failed on the HPC side.

  • Property svn:keywords set to Id
File size: 20.2 KB
Line 
1/*
2 *
3 * Author : Gérald FENOY
4 *
5 * Copyright 2017 GeoLabs SARL. All rights reserved.
6 *
7 * This work was supported by public funds received in the framework of GEOSUD,
8 * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed
9 * by the French National Research Agency
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30#include "sshapi.h"
31#include "service_internal.h"
32
33SSHCON *sessions[MAX_PARALLEL_SSH_CON];
34
35/**
36 * Wait until one or more file descriptor has been changed for the socket for a
37 * time defined by timeout
38 * @param socket_fd int defining the sockket file descriptor
39 * @param session an exeisting LIBSSH2_SESSION
40 */ 
41int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
42{
43    struct timeval timeout;
44    int rc;
45    fd_set fd;
46    fd_set *writefd = NULL;
47    fd_set *readfd = NULL;
48    int dir;
49 
50    timeout.tv_sec = 10;
51    timeout.tv_usec = 0;
52 
53    FD_ZERO(&fd);
54 
55    FD_SET(socket_fd, &fd);
56 
57    /* now make sure we wait in the correct direction */ 
58    dir = libssh2_session_block_directions(session);
59
60 
61    if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
62        readfd = &fd;
63 
64    if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
65        writefd = &fd;
66 
67    rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
68 
69    return rc;
70}
71
72/**
73 * Connect to a remote host using SSH protocol
74 * @param conf maps The main configuration maps
75 * @return the libssh2 sessions pointer or NULL in case any failure occured.
76 */
77SSHCON *ssh_connect(maps* conf){
78  unsigned long hostaddr;
79  int i, use_pw = 1;
80  struct sockaddr_in sin;
81  const char *fingerprint;
82  SSHCON *result=(SSHCON*)malloc((2*sizeof(int))+sizeof(LIBSSH2_SESSION*)+sizeof(LIBSSH2_SFTP*));
83  result->sock_id=NULL;
84  result->index=NULL;
85  result->session=NULL;
86  result->sftp_session=NULL;
87  const char *user;
88  const char *password=NULL;
89  const char *public_key;
90  char *private_key;
91  int rc;
92  FILE *local;
93  char mem[1024 * 1000];
94  char error[1024];
95  int port=22;
96
97  map* hpc_config=getMapFromMaps(conf,"lenv","configId");
98 
99  map* hpc_host=getMapFromMaps(conf,hpc_config->value,"ssh_host");
100  map* hpc_port=getMapFromMaps(conf,hpc_config->value,"ssh_port");
101  map* hpc_user=getMapFromMaps(conf,hpc_config->value,"ssh_user");
102  map* hpc_password=getMapFromMaps(conf,hpc_config->value,"ssh_password");
103  map* hpc_public_key=getMapFromMaps(conf,hpc_config->value,"ssh_key");
104
105  char ip[100];
106  struct hostent *my_hostent;
107  struct in_addr **addrs;
108 
109  if (hpc_host != NULL) {
110    // Fetch ip address for the hostname
111    if ( (my_hostent = gethostbyname( hpc_host->value ) ) == NULL){
112      herror("gethostbyname");
113      setMapInMaps(conf,"lenv","message",_("Issue when invoking gethostbyname!"));
114      return NULL;
115    }
116 
117    addrs = (struct in_addr **) my_hostent->h_addr_list;
118 
119    for(i = 0; addrs[i] != NULL; i++) {
120      strcpy(ip , inet_ntoa(*addrs[i]) );
121      break;
122    }
123  }
124
125#ifdef WIN32
126  WSADATA wsadata;
127  int err;
128  err = WSAStartup(MAKEWORD(2,0), &wsadata);
129  if (err != 0) {
130    sprintf(error, "WSAStartup failed with error: %d\n", err);
131    setMapInMaps(conf,"lenv","message",error);
132    return NULL;
133  }
134#endif
135
136  if (hpc_host != NULL) {
137    hostaddr = inet_addr(ip);
138  } else {
139    setMapInMaps(conf,"lenv","message","No host parameter found in your main.cfg file!\n");
140    return NULL;
141  }
142
143  // Default port is 22
144  if(hpc_port!=NULL){
145    port=atoi(hpc_port->value);
146  }
147
148  // In case there is no HPC > user the it must failed
149  if (hpc_user != NULL) {
150    user = hpc_user->value;
151  }else{
152    setMapInMaps(conf,"lenv","message","No user parameter found in your main.cfg file!");
153    return NULL;
154  }
155
156  // TODO: in case password is available but there is also the public key
157  // defined, then we can consider this password as the pass phrase.
158  if (hpc_password != NULL) {
159    password = hpc_password->value;
160  }else{
161    use_pw=-1;
162    if (hpc_public_key != NULL) {
163      public_key = hpc_public_key->value;
164      private_key = strdup(hpc_public_key->value);
165      private_key[strlen(public_key)-4]=0;
166    }else{
167      setMapInMaps(conf,"lenv","message","No method found to authenticate!");
168      return NULL;
169    }
170  }
171
172  rc = libssh2_init (0);
173  if (rc != 0) {
174    sprintf (error, "libssh2 initialization failed (%d)\n", rc);
175    setMapInMaps(conf,"lenv","message",error);
176    return NULL;
177  }
178
179  result->sock_id = socket(AF_INET, SOCK_STREAM, 0);
180  sin.sin_family = AF_INET;
181  sin.sin_port = htons(port);
182  sin.sin_addr.s_addr = hostaddr;
183  if (connect(result->sock_id, (struct sockaddr*)(&sin),sizeof(struct sockaddr_in)) != 0) {
184    setMapInMaps(conf,"lenv","message","Failed to connect to remote host!");
185    return NULL;
186  }
187
188  result->session = libssh2_session_init();
189  result->sftp_session = NULL;
190  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
191  if(tmp!=NULL){
192    char nb_sessions[10];
193    int nb_sess=atoi(tmp->value);
194    sprintf(nb_sessions,"%d",nb_sess+1);
195    setMapInMaps(conf,"lenv","nb_sessions",nb_sessions);
196    result->index=nb_sess+1;
197    sessions[nb_sess+1]=result;
198  }else{
199    setMapInMaps(conf,"lenv","nb_sessions","0");
200    sessions[0]=result;
201    result->index=0;
202  }
203
204  if (!result->session)
205    return NULL;
206
207  libssh2_session_set_blocking(result->session, 0);
208
209  while ((rc = libssh2_session_handshake(result->session, result->sock_id))
210         == LIBSSH2_ERROR_EAGAIN);
211
212  if (rc) {
213    sprintf(error, "Failure establishing SSH session: %d\n", rc);
214    setMapInMaps(conf,"lenv","message",error);
215    return NULL;
216  }
217
218  fingerprint = libssh2_hostkey_hash(result->session, LIBSSH2_HOSTKEY_HASH_SHA1);
219 
220  if (use_pw>0) {
221    while ((rc = libssh2_userauth_password(result->session, user, password)) ==
222           LIBSSH2_ERROR_EAGAIN);
223    if (rc) {
224      setMapInMaps(conf,"lenv","message","Authentication by password failed.");
225      ssh_close(conf);
226      return NULL;
227    }
228  } else {
229    while ((rc = libssh2_userauth_publickey_fromfile(result->session, user,
230                                                     public_key,
231                                                     private_key,
232                                                     password)) ==
233           LIBSSH2_ERROR_EAGAIN);
234    if (rc) {
235      setMapInMaps(conf,"lenv","message","Authentication by public key failed");
236      ssh_close(conf);
237      return NULL;
238    }
239    free(private_key);
240  }
241
242  return result;
243
244}
245
246/**
247 * Get the number of opened SSH connections
248 * @param conf maps pointer to the main configuration maps
249 * @return the number of opened SSH connections
250 */
251int ssh_get_cnt(maps* conf){
252  int result=0;
253  map* myMap=getMapFromMaps(conf,"lenv","nb_sessions");
254  if(myMap!=NULL){
255    result=atoi(myMap->value);
256  }
257  return result;
258}
259
260/**
261 * Verify if a file exists on the remote host
262 * @param conf maps pointer to the main configuration maps
263 * @param targetPath const char* defining the path for storing the file on the
264 * remote host
265 * @return true in case of success, false if failure occured
266 */
267size_t ssh_file_exists(maps* conf,const char* targetPath,int cnt){
268  size_t result=-1;
269  if(cnt>0)
270    cnt-=1;
271  int rc;
272  LIBSSH2_SFTP_ATTRIBUTES attrs;
273  LIBSSH2_SFTP_HANDLE *sftp_handle;
274  do{
275    sftp_handle =
276      libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath, LIBSSH2_FXF_READ,
277                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
278                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
279    if (!sftp_handle) {
280      fprintf(stderr, "Unable to open file with SFTP: %d %ld\n",__LINE__,
281              libssh2_sftp_last_error(sessions[cnt]->sftp_session));
282      return 0;
283    }
284  }while(!sftp_handle);
285#ifdef SSH_DEBUG
286  fprintf(stderr, "libssh2_sftp_open() is done, get file information\n");
287#endif
288  do {
289  rc = libssh2_sftp_stat_ex(sessions[cnt]->sftp_session, targetPath, strlen(targetPath),
290                            LIBSSH2_SFTP_LSTAT, &attrs );
291  if (rc<0 &&
292      (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN))
293    {
294      fprintf(stderr, "error trying to fstat_ex, returned %d\n", rc);
295      break;
296    }
297  else
298    {
299#ifdef SSH_DEBUG
300      fprintf(stderr, "Stat Data: RetCode=%d\n", rc);
301      fprintf(stderr, "Stat Data: Size=%llu\n", attrs.filesize);
302      fprintf(stderr, "Stat Data: Perm=%lx\n",  attrs.permissions);
303      fprintf(stderr, "Stat Data: mtime=%lu\n",  attrs.mtime);
304#endif
305      if(rc==0)
306        break;
307      result=attrs.filesize;
308    }
309  } while (true);
310  libssh2_sftp_close(sftp_handle);
311  //libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
312
313  return result;
314}
315
316/**
317 * Upload a file over an opened SSH connection
318 * @param conf maps pointer to the main configuration maps
319 * @param localPath const char* defining the local path for accessing the file
320 * @param targetPath const char* defining the path for storing the file on the
321 * remote host
322 * @return true in case of success, false if failure occured
323 */
324bool ssh_copy(maps* conf,const char* localPath,const char* targetPath,int cnt){
325  char mem[1024 * 1000];
326  size_t nread;
327  size_t memuse=0;
328  time_t start;
329  long total = 0;
330  int duration;
331  int rc;
332  LIBSSH2_SFTP_HANDLE *sftp_handle;
333  //map* myMap=getMapFromMaps(conf,"lenv","cnt_session");
334  if(getMapFromMaps(conf,"lenv","cnt_session")!=NULL){
335    char tmp[10];
336    sprintf(tmp,"%d",cnt+1);
337    setMapInMaps(conf,"lenv","cnt_session",tmp);
338  }else
339    setMapInMaps(conf,"lenv","cnt_session","0");
340  FILE *local = fopen(localPath, "rb");
341  if (!local) {
342    fprintf(stderr, "Can't open local file %s\n", localPath);
343    return false;
344  }
345 
346  do {
347    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
348    if (!sessions[cnt]->sftp_session &&
349        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
350     
351      fprintf(stderr, "Unable to init SFTP session\n");
352      return false;
353    }
354    if(!sessions[cnt]->sftp_session)
355      zSleep(10);
356  } while (!sessions[cnt]->sftp_session);
357
358  do {
359    sftp_handle =
360      libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,
361                        LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
362                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
363                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
364
365    if (!sftp_handle &&
366        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
367     
368      fprintf(stderr, "Unable to open file with SFTP\n");
369      return false;
370    }
371    if(!sftp_handle)
372      zSleep(10);
373  } while (!sftp_handle);
374  start = time(NULL);
375 
376  do {
377    nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local);
378    if (nread <= 0) {
379      if (memuse > 0)
380        nread = 0;
381      else
382        break;
383    }
384    memuse += nread;
385    total += nread;
386   
387    while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) ==
388           LIBSSH2_ERROR_EAGAIN) {
389      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
390    }
391    if(rc < 0)
392      break;
393   
394    if(memuse - rc) {
395      memmove(&mem[0], &mem[rc], memuse - rc);
396      memuse -= rc;
397    }
398    else
399      memuse = 0;
400   
401  } while (rc > 0);
402 
403  duration = (int)(time(NULL)-start);
404  fclose(local);
405  libssh2_sftp_close_handle(sftp_handle);
406
407  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
408  return true;
409}
410
411/**
412 * Download a file over an opened SSH connection
413 * @param conf maps pointer to the main configuration maps
414 * @param localPath const char* defining the local path for storing the file
415 * @param targetPath const char* defining the path for accessing the file on the
416 * remote host
417 * @return 0 in case of success, -1 if failure occured
418 */
419int ssh_fetch(maps* conf,const char* localPath,const char* targetPath,int cnt){
420  char mem[1024];
421  size_t nread;
422  size_t memuse=0;
423  time_t start;
424  long total = 0;
425  int duration;
426  int rc;
427  LIBSSH2_SFTP_HANDLE *sftp_handle;
428  FILE *local = fopen(localPath, "wb");
429  if (!local) {
430    fprintf(stderr, "Can't open local file %s\n", localPath);
431    return -1;
432  }
433
434  do {
435    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
436    if (!sessions[cnt]->sftp_session &&
437        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
438     
439      fprintf(stderr, "Unable to init SFTP session\n");
440      return -1;
441    }
442    if(!sessions[cnt]->sftp_session)
443      zSleep(10);
444  } while (!sessions[cnt]->sftp_session);
445  do {
446    sftp_handle = libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,   
447                                    LIBSSH2_FXF_READ, 0);
448    if (!sftp_handle) {
449      if (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN) {
450        fprintf(stderr, " ** Unable to open file with SFTP\n");
451        return -1;
452      }
453      else {
454        //non-blocking open
455        waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 
456      }
457    }
458  } while (!sftp_handle);
459 
460  int result=0;
461  do {
462    do {
463      rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
464      /*fprintf(stderr, "libssh2_sftp_read returned %d\n",
465        rc);*/
466      if(rc > 0) {
467        //write(2, mem, rc);
468        fwrite(mem, rc, 1, local);
469      }
470    } while (rc > 0);
471   
472    if(rc != LIBSSH2_ERROR_EAGAIN) {
473      result=-1;
474      break;
475    }
476   
477    struct timeval timeout;
478    fd_set fd;
479    timeout.tv_sec = 10;
480    timeout.tv_usec = 0;
481 
482    FD_ZERO(&fd);
483 
484    FD_SET(sessions[cnt]->sock_id, &fd);
485 
486    rc = select(sessions[cnt]->sock_id+1, &fd, &fd, NULL, &timeout);
487    if(rc <= 0) {
488      if(rc==0)
489        fprintf(stderr, "SFTP download timed out: %d\n", rc);
490      else
491        fprintf(stderr, "SFTP download error: %d\n", rc);
492      return -1;
493    }
494   
495  } while (1);
496  duration = (int)(time(NULL)-start);
497  fclose(local);
498  libssh2_sftp_close_handle(sftp_handle);
499  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
500  return 0;
501}
502
503/**
504 * Execute a command over an opened SSH connection
505 * @param conf maps pointer to the main configuration maps
506 * @param command const char pointer to the command to be executed
507 * @return bytecount resulting from the execution of the command
508 */
509int ssh_exec(maps* conf,const char* command,int cnt){
510  LIBSSH2_CHANNEL *channel;
511  int rc;
512  int bytecount = 0;
513  int exitcode;
514  char *exitsignal=(char *)"none";
515  while( (channel = libssh2_channel_open_session(sessions[cnt]->session)) == NULL &&
516         libssh2_session_last_error(sessions[cnt]->session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN ) {
517    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
518  }
519  if( channel == NULL ){
520    fprintf(stderr,"Error\n");
521    return -1;
522  }
523  while( (rc = libssh2_channel_exec(channel, command)) == LIBSSH2_ERROR_EAGAIN ) {
524    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
525  }
526  if( rc != 0 ) {
527    fprintf(stderr,"Error\n");
528    return -1;
529  }
530
531  map* tmpPath=getMapFromMaps(conf,"main","tmpPath");
532  map* uuid=getMapFromMaps(conf,"lenv","usid");
533  char *logPath=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+11)*sizeof(char));
534  sprintf(logPath,"%s/exec_out_%s",tmpPath->value,uuid->value);
535  FILE* logFile=fopen(logPath,"wb");
536  free(logPath);
537  while(true){
538    int rc;
539    do {
540      char buffer[0x4000];
541      rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
542     
543      if( rc > 0 ){
544        int i;
545        bytecount += rc;
546        buffer[rc]=0;
547        fprintf(logFile,"%s",buffer);
548        fflush(logFile);
549      }
550    }
551    while( rc > 0 );
552   
553    if( rc == LIBSSH2_ERROR_EAGAIN ) {
554      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
555    }
556    else
557      break;
558  }
559  fclose(logFile);
560  exitcode = 127;
561  while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
562    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
563 
564  if( rc == 0 ) {
565    exitcode = libssh2_channel_get_exit_status( channel );
566    libssh2_channel_get_exit_signal(channel, &exitsignal,
567                                    NULL, NULL, NULL, NULL, NULL);
568  }
569 
570  if (exitsignal)
571    fprintf(stderr, "\nGot signal: %s\n", exitsignal);
572  else
573    fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
574 
575  libssh2_channel_free(channel);
576
577  return bytecount;
578}
579
580/**
581 * Close an opened SSH connection
582 * @param conf maps pointer to the main configuration maps
583 * @param con SSHCON pointer to the SSH connection
584 * @return true in case of success, false if failure occured
585 */
586bool ssh_close_session(maps* conf,SSHCON* con){
587  while (libssh2_session_disconnect(con->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
588         == LIBSSH2_ERROR_EAGAIN);
589#ifdef WIN32
590  closesocket(con->sock_id);
591#else
592  close(con->sock_id);
593#endif
594  libssh2_session_free(con->session);
595  return true;
596}
597
598/**
599 * Close all the opened SSH connections
600 * @param conf maps pointer to the main configuration maps
601 * @return true in case of success, false if failure occured
602 */
603bool ssh_close(maps* conf){
604  int i,nb_sessions;
605  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
606  if(tmp!=NULL){
607    nb_sessions=atoi(tmp->value);
608    for(i=0;i<nb_sessions;i++)
609      ssh_close_session(conf,sessions[i]);
610  }
611  libssh2_exit();
612  return true;
613}
614
615bool addToUploadQueue(maps** conf,maps* input){
616  map* queueMap=getMapFromMaps(*conf,"uploadQueue","length");
617  if(queueMap==NULL){
618    maps* queueMaps=createMaps("uploadQueue");
619    queueMaps->content=createMap("length","0");
620    addMapsToMaps(conf,queueMaps);
621    freeMaps(&queueMaps);
622    free(queueMaps);
623    queueMap=getMapFromMaps(*conf,"uploadQueue","length");
624  }
625  maps* queueMaps=getMaps(*conf,"uploadQueue");
626  int queueIndex=atoi(queueMap->value);
627  if(input!=NULL){
628    if(getMap(input->content,"cache_file")!=NULL){
629      map* length=getMap(input->content,"length");
630      if(length==NULL){
631        addToMap(input->content,"length","1");
632        length=getMap(input->content,"length");
633      }
634      int len=atoi(length->value);
635      int i=0;
636      for(i=0;i<len;i++){
637       
638        map* tmp[2]={getMapArray(input->content,"localPath",i),
639                     getMapArray(input->content,"targetPath",i)};
640
641        setMapArray(queueMaps->content,"input",queueIndex,input->name);
642        setMapArray(queueMaps->content,"localPath",queueIndex,tmp[0]->value);
643        setMapArray(queueMaps->content,"targetPath",queueIndex,tmp[1]->value);
644        queueIndex+=1;
645      }
646    }
647  }
648#ifdef SSH_DEBUG 
649  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
650  fflush(stderr);
651  dumpMaps(queueMaps);
652  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
653  fflush(stderr);
654  dumpMaps(*conf);
655  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
656  fflush(stderr);
657#endif
658  return true;
659}
660
661bool runUpload(maps** conf){
662  SSHCON *test=ssh_connect(*conf);
663  if(test==NULL){
664    return false;
665  }
666#ifdef SSH_DEBUG
667  fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__);
668  fflush(stderr);
669  dumpMaps(getMaps(*conf,"uploadQueue"));
670  fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__);
671  fflush(stderr);
672#endif
673  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
674  maps* queueMaps=getMaps(*conf,"uploadQueue");
675  if(queueLengthMap!=NULL){
676    int cnt=atoi(queueLengthMap->value);
677    int i=0;
678    for(i=0;i<cnt;i++){
679      map* argv[3]={
680        getMapArray(queueMaps->content,"input",i),
681        getMapArray(queueMaps->content,"localPath",i),
682        getMapArray(queueMaps->content,"targetPath",i)
683      };
684#ifdef SSH_DEBUG     
685      fprintf(stderr,"*** %s %d %s %s\n",__FILE__,__LINE__,argv[1]->value,argv[2]->value);
686#endif     
687      /**/zooLock* lck;
688      if((lck=lockFile(*conf,argv[1]->value,'w'))!=NULL){/**/
689        if(ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf))!=true){
690          char* templateStr=_("Unable to copy over SSH the file requested for setting the value of %s.");
691          char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char));
692          sprintf(tmpMessage,templateStr,argv[0]->value);
693          setMapInMaps(*conf,"lenv","message",tmpMessage);
694          free(tmpMessage);
695          unlockFile(*conf,lck);
696          return false;
697        }
698        /**/unlockFile(*conf,lck);
699      }else{
700        setMapInMaps(*conf,"lenv","message",_("Unable to lock the file for upload!"));
701        return false;
702      }/**/
703    }   
704  }
705  return true; 
706}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png