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

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

HPC support update. Add inputs for create options in Gdal_Dem.

  • Property svn:keywords set to Id
File size: 20.8 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  fprintf(stderr, "libssh2_sftp_open() is done, get file information\n");
286  do {
287  rc = libssh2_sftp_stat_ex(sessions[cnt]->sftp_session, targetPath, strlen(targetPath),
288                            LIBSSH2_SFTP_LSTAT, &attrs );
289  if (rc<0 &&
290      (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN))
291    {
292      fprintf(stderr, "error trying to fstat_ex, returned %d\n", rc);
293      break;
294    }
295  else
296    {
297      fprintf(stderr, "Stat Data: RetCode=%d\n", rc);
298      fprintf(stderr, "Stat Data: Size=%llu\n", attrs.filesize);
299      fprintf(stderr, "Stat Data: Perm=%lx\n",  attrs.permissions);
300      fprintf(stderr, "Stat Data: mtime=%lu\n",  attrs.mtime);
301      if(rc==0)
302        break;
303      result=attrs.filesize;
304    }
305  } while (true);
306  libssh2_sftp_close(sftp_handle);
307  //libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
308
309  return result;
310}
311
312/**
313 * Upload a file over an opened SSH connection
314 * @param conf maps pointer to the main configuration maps
315 * @param localPath const char* defining the local path for accessing the file
316 * @param targetPath const char* defining the path for storing the file on the
317 * remote host
318 * @return true in case of success, false if failure occured
319 */
320bool ssh_copy(maps* conf,const char* localPath,const char* targetPath,int cnt){
321  char mem[1024 * 1000];
322  size_t nread;
323  size_t memuse=0;
324  time_t start;
325  long total = 0;
326  int duration;
327  int rc;
328  LIBSSH2_SFTP_HANDLE *sftp_handle;
329  //map* myMap=getMapFromMaps(conf,"lenv","cnt_session");
330  if(getMapFromMaps(conf,"lenv","cnt_session")!=NULL){
331    char tmp[10];
332    sprintf(tmp,"%d",cnt+1);
333    setMapInMaps(conf,"lenv","cnt_session",tmp);
334  }else
335    setMapInMaps(conf,"lenv","cnt_session","0");
336  FILE *local = fopen(localPath, "rb");
337  if (!local) {
338    fprintf(stderr, "Can't open local file %s\n", localPath);
339    return false;
340  }
341 
342  do {
343    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
344    if (!sessions[cnt]->sftp_session &&
345        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
346     
347      fprintf(stderr, "Unable to init SFTP session\n");
348      return false;
349    }
350  } while (!sessions[cnt]->sftp_session);
351
352  do {
353    sftp_handle =
354      libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,
355                        LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
356                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
357                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
358
359    if (!sftp_handle &&
360        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
361     
362      fprintf(stderr, "Unable to open file with SFTP\n");
363      return false;
364    }
365  } while (!sftp_handle);
366  start = time(NULL);
367 
368  do {
369    nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local);
370    if (nread <= 0) {
371      if (memuse > 0)
372        nread = 0;
373      else
374        break;
375    }
376    memuse += nread;
377    total += nread;
378   
379    while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) ==
380           LIBSSH2_ERROR_EAGAIN) {
381      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
382    }
383    if(rc < 0)
384      break;
385   
386    if(memuse - rc) {
387      memmove(&mem[0], &mem[rc], memuse - rc);
388      memuse -= rc;
389    }
390    else
391      memuse = 0;
392   
393  } while (rc > 0);
394 
395  duration = (int)(time(NULL)-start);
396  fclose(local);
397  libssh2_sftp_close_handle(sftp_handle);
398
399  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
400  return true;
401}
402
403/**
404 * Download a file over an opened SSH connection
405 * @param conf maps pointer to the main configuration maps
406 * @param localPath const char* defining the local path for storing the file
407 * @param targetPath const char* defining the path for accessing the file on the
408 * remote host
409 * @return 0 in case of success, -1 if failure occured
410 */
411int ssh_fetch(maps* conf,const char* localPath,const char* targetPath,int cnt){
412  char mem[1024];
413  size_t nread;
414  size_t memuse=0;
415  time_t start;
416  long total = 0;
417  int duration;
418  int rc;
419  LIBSSH2_SFTP_HANDLE *sftp_handle;
420  FILE *local = fopen(localPath, "wb");
421  if (!local) {
422    fprintf(stderr, "Can't open local file %s\n", localPath);
423    return -1;
424  }
425
426  do {
427    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
428    if (!sessions[cnt]->sftp_session &&
429        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
430     
431      fprintf(stderr, "Unable to init SFTP session\n");
432      return -1;
433    }
434  } while (!sessions[cnt]->sftp_session);
435  do {
436    sftp_handle = libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,   
437                                    LIBSSH2_FXF_READ, 0);
438    if (!sftp_handle) {
439      if (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN) {
440        fprintf(stderr, " ** Unable to open file with SFTP\n");
441        return -1;
442      }
443      else {
444        //non-blocking open
445        waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 
446      }
447    }
448  } while (!sftp_handle);
449 
450  int result=0;
451  do {
452    do {
453      rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
454      /*fprintf(stderr, "libssh2_sftp_read returned %d\n",
455        rc);*/
456      if(rc > 0) {
457        //write(2, mem, rc);
458        fwrite(mem, rc, 1, local);
459      }
460    } while (rc > 0);
461   
462    if(rc != LIBSSH2_ERROR_EAGAIN) {
463      result=-1;
464      break;
465    }
466   
467    struct timeval timeout;
468    fd_set fd;
469    timeout.tv_sec = 10;
470    timeout.tv_usec = 0;
471 
472    FD_ZERO(&fd);
473 
474    FD_SET(sessions[cnt]->sock_id, &fd);
475 
476    rc = select(sessions[cnt]->sock_id+1, &fd, &fd, NULL, &timeout);
477    if(rc <= 0) {
478      if(rc==0)
479        fprintf(stderr, "SFTP download timed out: %d\n", rc);
480      else
481        fprintf(stderr, "SFTP download error: %d\n", rc);
482      return -1;
483    }
484 
485  } while (1);
486  duration = (int)(time(NULL)-start);
487  fclose(local);
488  libssh2_sftp_close_handle(sftp_handle);
489  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
490  return 0;
491}
492
493/**
494 * Execute a command over an opened SSH connection
495 * @param conf maps pointer to the main configuration maps
496 * @param command const char pointer to the command to be executed
497 * @return bytecount resulting from the execution of the command
498 */
499int ssh_exec(maps* conf,const char* command,int cnt){
500  LIBSSH2_CHANNEL *channel;
501  int rc;
502  int bytecount = 0;
503  int exitcode;
504  char *exitsignal=(char *)"none";
505  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
506  fflush(stderr);
507  while( (channel = libssh2_channel_open_session(sessions[cnt]->session)) == NULL &&
508         libssh2_session_last_error(sessions[cnt]->session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN ) {
509    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
510    fflush(stderr);
511      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
512  }
513  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
514  fflush(stderr);
515  if( channel == NULL ){
516    fprintf(stderr,"Error\n");
517    return -1;
518  }
519  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
520  fflush(stderr);
521  while( (rc = libssh2_channel_exec(channel, command)) == LIBSSH2_ERROR_EAGAIN ) {
522    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
523    fflush(stderr);
524    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
525  }
526  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
527  fflush(stderr);
528  if( rc != 0 ) {
529    fprintf(stderr,"Error\n");
530    return -1;
531  }
532  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
533  fflush(stderr);
534
535  map* tmpPath=getMapFromMaps(conf,"main","tmpPath");
536  map* uuid=getMapFromMaps(conf,"lenv","usid");
537  char *logPath=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+11)*sizeof(char));
538  sprintf(logPath,"%s/exec_out_%s",tmpPath->value,uuid->value);
539  FILE* logFile=fopen(logPath,"wb");
540  free(logPath);
541  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
542  fflush(stderr);
543  while(true){
544    int rc;
545    do {
546      char buffer[0x4000];
547      rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
548     
549      if( rc > 0 ){
550        int i;
551        bytecount += rc;
552        buffer[rc]=0;
553        fprintf(logFile,"%s",buffer);
554        fflush(logFile);
555      }
556    }
557    while( rc > 0 );
558   
559    if( rc == LIBSSH2_ERROR_EAGAIN ) {
560      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
561    }
562    else
563      break;
564  }
565  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
566  fflush(stderr);
567  fclose(logFile);
568  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
569  fflush(stderr);
570  exitcode = 127;
571  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
572  fflush(stderr);
573  while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
574    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
575  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
576  fflush(stderr);
577 
578  if( rc == 0 ) {
579    exitcode = libssh2_channel_get_exit_status( channel );
580    libssh2_channel_get_exit_signal(channel, &exitsignal,
581                                    NULL, NULL, NULL, NULL, NULL);
582  }
583  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
584  fflush(stderr);
585 
586  if (exitsignal)
587    fprintf(stderr, "\nGot signal: %s\n", exitsignal);
588  else
589    fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
590  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
591  fflush(stderr);
592 
593  libssh2_channel_free(channel);
594
595  return bytecount;
596}
597
598/**
599 * Close an opened SSH connection
600 * @param conf maps pointer to the main configuration maps
601 * @param con SSHCON pointer to the SSH connection
602 * @return true in case of success, false if failure occured
603 */
604bool ssh_close_session(maps* conf,SSHCON* con){
605  while (libssh2_session_disconnect(con->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
606         == LIBSSH2_ERROR_EAGAIN);
607#ifdef WIN32
608  closesocket(con->sock_id);
609#else
610  close(con->sock_id);
611#endif
612  libssh2_session_free(con->session);
613  return true;
614}
615
616/**
617 * Close all the opened SSH connections
618 * @param conf maps pointer to the main configuration maps
619 * @return true in case of success, false if failure occured
620 */
621bool ssh_close(maps* conf){
622  int i,nb_sessions;
623  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
624  if(tmp!=NULL){
625    nb_sessions=atoi(tmp->value);
626    for(i=0;i<nb_sessions;i++)
627      ssh_close_session(conf,sessions[i]);
628  }
629  libssh2_exit();
630  return true;
631}
632
633bool addToUploadQueue(maps** conf,maps* input){
634  map* queueMap=getMapFromMaps(*conf,"uploadQueue","length");
635  if(queueMap==NULL){
636    maps* queueMaps=createMaps("uploadQueue");
637    queueMaps->content=createMap("length","0");
638    addMapsToMaps(conf,queueMaps);
639    freeMaps(&queueMaps);
640    free(queueMaps);
641    queueMap=getMapFromMaps(*conf,"uploadQueue","length");
642  }
643  maps* queueMaps=getMaps(*conf,"uploadQueue");
644  int queueIndex=atoi(queueMap->value);
645  if(input!=NULL){
646    if(getMap(input->content,"cache_file")!=NULL){
647      map* length=getMap(input->content,"length");
648      if(length==NULL){
649        addToMap(input->content,"length","1");
650        length=getMap(input->content,"length");
651      }
652      int len=atoi(length->value);
653      int i=0;
654      for(i=0;i<len;i++){
655       
656        map* tmp[2]={getMapArray(input->content,"localPath",i),
657                     getMapArray(input->content,"targetPath",i)};
658
659        setMapArray(queueMaps->content,"input",queueIndex,input->name);
660        setMapArray(queueMaps->content,"localPath",queueIndex,tmp[0]->value);
661        setMapArray(queueMaps->content,"targetPath",queueIndex,tmp[1]->value);
662        queueIndex+=1;
663      }
664    }
665  }
666#ifdef DEBUG 
667  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
668  fflush(stderr);
669  dumpMaps(queueMaps);
670  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
671  fflush(stderr);
672  dumpMaps(*conf);
673  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
674  fflush(stderr);
675#endif
676  return true;
677}
678
679bool runUpload(maps** conf){
680  SSHCON *test=ssh_connect(*conf);
681  if(test==NULL){
682    return false;
683  }
684#ifdef DEBUG
685  fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__);
686  fflush(stderr);
687  dumpMaps(getMaps(*conf,"uploadQueue"));
688  fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__);
689  fflush(stderr);
690#endif
691  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
692  maps* queueMaps=getMaps(*conf,"uploadQueue");
693  if(queueLengthMap!=NULL){
694    int cnt=atoi(queueLengthMap->value);
695    int i=0;
696    for(i=0;i<cnt;i++){
697      map* argv[3]={
698        getMapArray(queueMaps->content,"input",i),
699        getMapArray(queueMaps->content,"localPath",i),
700        getMapArray(queueMaps->content,"targetPath",i)
701      };
702      fprintf(stderr,"*** %s %d %s %s\n",__FILE__,__LINE__,argv[1]->value,argv[2]->value);
703      /**/zooLock* lck;
704      if((lck=lockFile(*conf,argv[1]->value,'w'))!=NULL){/**/
705        if(ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf))!=true){
706          char* templateStr=_("Unable to copy over SSH the file requested for setting the value of %s.");
707          char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char));
708          sprintf(tmpMessage,templateStr,argv[0]->value);
709          setMapInMaps(*conf,"lenv","message",tmpMessage);
710          free(tmpMessage);
711          unlockFile(*conf,lck);
712          return false;
713        }
714        /**/unlockFile(*conf,lck);
715      }else{
716        setMapInMaps(*conf,"lenv","message",_("Unable to lock the file for upload!"));
717        return false;
718      }/**/
719    }   
720  }
721  return true; 
722}
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