/Home/aooliver/work/stagersvc/org.glite.data.lcg_gfal/src/se_ifce.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005-2006 by CERN
00003  */
00004 
00005 /*
00006  * @(#)$RCSfile: se_ifce.c,v $ $Revision: 1.1.1.1 $ $Date: 2006/09/06 08:54:53 $ CERN Jean-Philippe Baud
00007  */
00008 
00009 #include <sys/types.h>
00010 #include <errno.h>
00011 #include <grp.h>
00012 #include <pwd.h>
00013 #include <stdio.h>
00014 #include <sys/stat.h>
00015 #include "gfal_api.h"
00016 #undef SOAP_FMAC3
00017 #define SOAP_FMAC3 static
00018 #undef SOAP_FMAC5
00019 #define SOAP_FMAC5 static
00020 #include "seH.h"
00021 #include "edg_se_webserviceSoapBinding+.nsmap"
00022 #ifdef GFAL_SECURE
00023 #include "cgsi_plugin.h"
00024 #endif
00025 #include "seC.c"
00026 #include "seClient.c"
00027 
00028 static int
00029 se_init (struct soap *soap, const char *surl, char *srm_endpoint,
00030         int srm_endpointsz, char *errbuf, int errbufsz)
00031 {
00032         int flags;
00033         char *sfn;
00034 
00035         if (parsesurl (surl, srm_endpoint, srm_endpointsz, &sfn, errbuf, errbufsz) < 0)
00036                 return (-1);
00037 
00038         soap_init (soap);
00039         soap->namespaces = namespaces_se;
00040 #ifdef GFAL_SECURE
00041         flags = CGSI_OPT_DISABLE_NAME_CHECK | CGSI_OPT_SSL_COMPATIBLE;
00042         soap_register_plugin_arg (soap, client_cgsi_plugin, &flags);
00043 #endif
00044         return (0);
00045 }
00046 
00047 se_deletesurl (const char *surl, char *errbuf, int errbufsz, int timeout)
00048 {
00049         struct ns1__deleteResponse out;
00050         struct soap soap;
00051         char srm_endpoint[256];
00052 
00053         if (se_init (&soap, surl, srm_endpoint, sizeof(srm_endpoint),
00054             errbuf, errbufsz) < 0)
00055                 return (-1);
00056         soap.send_timeout = timeout ;
00057         soap.recv_timeout = timeout ;
00058 
00059         /* issue "delete" request */
00060 
00061         if (soap_call_ns1__delete (&soap, srm_endpoint,
00062             "delete", (char *)surl + 6, &out)) {
00063                 if (soap.error == SOAP_EOF) {
00064                         gfal_errmsg(errbuf, errbufsz, "connection fails or timeout");
00065                         soap_end (&soap);
00066                         soap_done (&soap);
00067                         return (-1);
00068                 }
00069                 gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00070                 soap_end (&soap);
00071                 soap_done (&soap);
00072                 return (-1);
00073         }
00074         soap_end (&soap);
00075         soap_done (&soap);
00076         return (0);
00077 }
00078 
00079 se_mkdir (const char *dir, char *errbuf, int errbufsz, int timeout)
00080 {
00081         struct ns1__mkdirResponse out;
00082         int ret;
00083         int sav_errno;
00084         struct soap soap;
00085         char srm_endpoint[256];
00086 
00087         if (se_init (&soap, dir, srm_endpoint, sizeof(srm_endpoint),
00088             errbuf, errbufsz) < 0)
00089                 return (-1);
00090         soap.send_timeout = timeout ;
00091         soap.recv_timeout = timeout ;
00092         
00093         /* issue "mkdir" request */
00094         if ((ret = soap_call_ns1__mkdir (&soap, srm_endpoint,
00095             "mkdir", (char *)dir + 6, &out))) {
00096                 if (soap.error == SOAP_EOF) {
00097                         gfal_errmsg(errbuf, errbufsz, "connection fails or timeout");
00098                         soap_end (&soap);
00099                         soap_done (&soap);
00100                         return (-1);
00101                 }
00102 
00103                 if (ret == SOAP_FAULT || ret == SOAP_CLI_FAULT) {
00104                         if (strstr (soap.fault->faultstring, "does not exist"))
00105                                 sav_errno = ENOENT;
00106                         else
00107                                 sav_errno = ECOMM;
00108                 } else
00109                         sav_errno = ECOMM;
00110                 if (sav_errno == ECOMM) 
00111                         gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00112                 soap_end (&soap);
00113                 soap_done (&soap);
00114                 errno = sav_errno;
00115                 return (-1);
00116         }
00117         soap_end (&soap);
00118         soap_done (&soap);
00119         return (0);
00120 }
00121 
00122 int
00123 se_makedirp (const char *surl, char *errbuf, int errbufsz, int timeout)
00124 {
00125         int c;
00126         char *lastslash = NULL;
00127         char *p;
00128         char *p1;
00129         char *q;
00130         char sav_surl[1104];
00131         struct stat64 statbuf;
00132 
00133         if (strlen (surl) >= sizeof(sav_surl)) {
00134                 gfal_errmsg(errbuf, errbufsz, "Source URL too long.");
00135                 errno = ENAMETOOLONG;
00136                 return (-1);
00137         }
00138         strcpy (sav_surl, surl);
00139         p1 = strchr (sav_surl + 6, '/');
00140         p = strrchr (sav_surl + 6, '/');
00141         while (p > p1) {
00142                 if (lastslash == NULL) lastslash = p;
00143                 *p = '\0';
00144                 c = se_getfilemd (sav_surl, &statbuf, errbuf, errbufsz, timeout);
00145                 if (c == 0) {
00146                         *p = '/';
00147                         break;
00148                 }
00149                 if (errno != ENOENT)
00150                         return (c);
00151                 q = strrchr (sav_surl, '/');
00152                 *p = '/';
00153                 p = q;
00154         }
00155         c = 0;
00156         while (c == 0 && (p = strchr (p + 1, '/')) && p <= lastslash) {
00157                 *p = '\0';
00158                 c = se_mkdir (sav_surl, errbuf, errbufsz, timeout);
00159                 *p = '/';
00160         }
00161         return (c);
00162 }
00163 
00164 char *
00165 se_turlfromsurl (const char *surl, char **protocols, int oflag, int *reqid,
00166         int *fileid, char **token, char *errbuf, int errbufsz, int timeout)
00167 {
00168         int nbproto = 0;
00169         struct ns1__cacheResponse outg;
00170         struct ns1__createResponse outp;
00171         struct ns1__getTurlResponse outq;
00172         char *p;
00173         struct ArrayOf_USCORExsd_USCOREstring protoarray;
00174         int ret;
00175         int sav_errno;
00176         struct soap soap;
00177         char srm_endpoint[256];
00178         LONG64 zero = 0;
00179 
00180         if (se_init (&soap, surl, srm_endpoint, sizeof(srm_endpoint),
00181             errbuf, errbufsz) < 0)
00182                 return (NULL);
00183         soap.send_timeout = timeout ;
00184         soap.recv_timeout = timeout ;
00185 
00186         while (*protocols[nbproto]) nbproto++;
00187 
00188         /* issue "cache" or the "create" request */
00189 
00190         protoarray.__ptr = protocols;
00191         protoarray.__size = nbproto;
00192 
00193         if ((oflag & O_ACCMODE) == 0) {
00194                 if ((ret = soap_call_ns1__cache (&soap, srm_endpoint, "cache",
00195                     (char *)surl + 6, "read", 36000, &outg))) {
00196                         if (soap.error == SOAP_EOF) {
00197                                 gfal_errmsg(errbuf, errbufsz, "connection fails or timeout");
00198                                 soap_end (&soap);
00199                                 soap_done (&soap);
00200                                 return (NULL);
00201                         }
00202 
00203                         if (ret == SOAP_FAULT || ret == SOAP_CLI_FAULT) {
00204                                 if (strstr (soap.fault->faultstring, "STFN not found"))
00205                                         sav_errno = ENOENT;
00206                                 else
00207                                         sav_errno = ECOMM;
00208                         } else
00209                                 sav_errno = ECOMM;
00210                         if (errno == ECOMM)
00211                                 gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00212                         soap_end (&soap);
00213                         soap_done (&soap);
00214                         errno = sav_errno;
00215                         return (NULL);
00216                 }
00217                 if ((*token = strdup (outg._cacheReturn)) == NULL) {
00218                         soap_end (&soap);
00219                         soap_done (&soap);
00220                         return (NULL);
00221                 }
00222         } else {
00223 retry:
00224                 if ((ret = soap_call_ns1__create (&soap, srm_endpoint, "create",
00225                     (char *)surl + 6, zero, "x", 36000, &outp))) {
00226                         if (soap.error == SOAP_EOF) {
00227                                 gfal_errmsg(errbuf, errbufsz, "connection fails or timeout");
00228                                 soap_end (&soap);
00229                                 soap_done (&soap);
00230                                 return (NULL);
00231                         }
00232                         if (ret == SOAP_FAULT || ret == SOAP_CLI_FAULT) {
00233                                 if (strstr (soap.fault->faultstring, "o such file"))
00234                                         sav_errno = ENOENT;
00235                                         if (se_makedirp (surl, errbuf, errbufsz, timeout) == 0)
00236                                                 goto retry;
00237                                 else if (strstr (soap.fault->faultstring, "File exists"))
00238                                         sav_errno = EEXIST;
00239                                 else
00240                                         sav_errno = ECOMM;
00241                         } else
00242                                 sav_errno = ECOMM;
00243                         if (errno == ECOMM)
00244                                 gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00245                         soap_end (&soap);
00246                         soap_done (&soap);
00247                         errno = sav_errno;
00248                         return (NULL);
00249                 }
00250                 if ((*token = strdup (outp._createReturn)) == NULL) {
00251                         soap_end (&soap);
00252                         soap_done (&soap);
00253                         return (NULL);
00254                 }
00255         }
00256         if (soap_call_ns1__getTurl (&soap, srm_endpoint, "getTurl", *token,
00257             &protoarray, &outq)) {
00258                 if (soap.error == SOAP_EOF) {
00259                         gfal_errmsg(errbuf, errbufsz, "connection fails or timeout");
00260                         soap_end (&soap);
00261                         soap_done (&soap);
00262                         return (NULL);
00263                 }
00264                 gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00265                 soap_end (&soap);
00266                 soap_done (&soap);
00267                 return (NULL);
00268         }
00269         *reqid = 0;
00270         *fileid = 0;
00271         p = strdup (outq._getTurlReturn);
00272         soap_end (&soap);
00273         soap_done (&soap);
00274         return (p);
00275 }
00276 
00277 se_getfilemd (const char *surl, struct stat64 *statbuf, char *errbuf, int errbufsz, int timeout)
00278 {
00279         char *dp;
00280         struct group *gr;
00281         int i;
00282         struct ns1__getMetadataResponse out;
00283         char *p;
00284         struct passwd *pw;
00285         char *q;
00286         int ret;
00287         int sav_errno;
00288         struct soap soap;
00289         char srm_endpoint[256];
00290 
00291         if (se_init (&soap, surl, srm_endpoint, sizeof(srm_endpoint),
00292             errbuf, errbufsz) < 0)
00293                 return (-1);
00294         soap.send_timeout = timeout;
00295         soap.recv_timeout = timeout;
00296 
00297         /* issue "getMetadata" request */
00298 
00299         if ((ret = soap_call_ns1__getMetadata (&soap, srm_endpoint,
00300             "getMetadata", (char *)surl + 6, &out))) {
00301                 if (soap.error == SOAP_EOF) {
00302                         gfal_errmsg(errbuf, errbufsz, "connection fails or timeout");
00303                         soap_end (&soap);
00304                         soap_done (&soap);
00305                         return (-1);
00306                 }
00307 
00308                 if (ret == SOAP_FAULT || ret == SOAP_CLI_FAULT) {
00309                         if (strstr (soap.fault->faultstring, "does not exist"))
00310                                 sav_errno = ENOENT;
00311                         else if (strstr (soap.fault->faultstring, "expired")) {
00312                                 gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00313                                 sav_errno = EACCES;
00314                         } else
00315                                 sav_errno = ECOMM;
00316                 } else
00317                         sav_errno = ECOMM;
00318                 if (sav_errno == ECOMM)
00319                         gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00320                 soap_end (&soap);
00321                 soap_done (&soap);
00322                 errno = sav_errno;
00323                 return (-1);
00324         }
00325         memset (statbuf, 0, sizeof(struct stat64));
00326         statbuf->st_nlink = 1;
00327         statbuf->st_uid = 2;
00328         statbuf->st_gid = 2;
00329         for (i = 0; i < out._getMetadataReturn->__size; i++) {
00330                 p = out._getMetadataReturn->__ptr[i];
00331                 if ((q = strchr (p, ':'))) {
00332                         *q = '\0';
00333                         if (strcmp (p, "filesize") == 0)
00334                                 statbuf->st_size = strtoll (q + 1, &dp, 10);
00335                         else if (strcmp (p, "atime") == 0)
00336                                 statbuf->st_atime = strtol (q + 1, &dp, 10);
00337                         else if (strcmp (p, "mtime") == 0)
00338                                 statbuf->st_mtime = strtol (q + 1, &dp, 10);
00339                         else if (strcmp (p, "ctime") == 0)
00340                                 statbuf->st_ctime = strtol (q + 1, &dp, 10);
00341                 }
00342         }
00343         soap_end (&soap);
00344         soap_done (&soap);
00345         return (0);
00346 }
00347 
00348 se_set_xfer_done (const char *surl, int reqid, int fileid, char *token,
00349         int oflag, char *errbuf, int errbufsz, int timeout)
00350 {
00351         struct ns1__abandonResponse outa;
00352         struct ns1__commitResponse outc;
00353         struct soap soap;
00354         char srm_endpoint[256];
00355 
00356         if (se_init (&soap, surl, srm_endpoint, sizeof(srm_endpoint),
00357             errbuf, errbufsz) < 0)
00358                 return (-1);
00359         soap.send_timeout = timeout ;
00360         soap.recv_timeout = timeout ;
00361 
00362 
00363         if ((oflag & O_ACCMODE) == 0) {
00364 /*              not implemented yet 
00365                 if (soap_call_ns1__abandon (&soap, srm_endpoint,
00366                     "abandon", token, &outa)) {
00367                         gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00368                         soap_end (&soap);
00369                         soap_done (&soap);
00370                         return (-1);
00371                 }
00372 */
00373         } else {
00374                 if (soap_call_ns1__commit (&soap, srm_endpoint,
00375                     "commit", token, &outc)) {
00376                        if (soap.error == SOAP_EOF) {
00377                                 gfal_errmsg(errbuf, errbufsz, "connection fails or timeout");
00378                                 soap_end (&soap);
00379                                 soap_done (&soap);
00380                                 return (-1);
00381                         }
00382                         gfal_errmsg(errbuf, errbufsz, soap.fault->faultstring);
00383                         soap_end (&soap);
00384                         soap_done (&soap);
00385                         return (-1);
00386                 }
00387         }
00388         soap_end (&soap);
00389         soap_done (&soap);
00390         return (0);
00391 }
00392 
00393 se_set_xfer_running (const char *surl, int reqid, int fileid, char *token,
00394         char *errbuf, int errbufsz)
00395 {
00396         return (0);
00397 }

Generated on Fri Sep 8 20:20:47 2006 for GFAL by  doxygen 1.4.6