1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* Copyright (C) 2007-2008 by CERN/IT/GD/ITR
* All rights reserved
*/
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: dpmcopy_push.c,v $ $Revision: 1.1 $ $Date: 2008/09/24 11:25:00 $ CERN IT-GD/ITR Jean-Philippe Baud";
#endif /* not lint */
/* dpmcopy_push - copy files when source is local and target is remote */
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "dpm.h"
#include "dpm_api.h"
#include "dpm_server.h"
#include "dpmcopy_server.h"
#include "serrno.h"
#define DEFPOLLINT 5
extern int nb_file_err;
int
dpmcopy_push (struct dpm_dbfd *dbfd, struct dpm_req *dpm_req, struct dpm_copy_filereq *cpr_entry, int nbfiles)
{
char errbuf[256];
char errstring[256];
struct dpm_filestatus *filestatuses;
int *filstatus = NULL;
char func[16];
struct dpm_getfilereq *getfilereqs;
struct dpm_getfilestatus *getfilestatuses = NULL;
int i;
int j = 0;
int nbgetreplies;
int nbputreplies;
int nbreplies;
char *p;
static char *protocols[] = {"gsiftp"};
struct dpm_putfilereq *putfilereqs;
struct dpm_putfilestatus *putfilestatus = NULL;
char *q;
int r = 0;
int r1;
char r_tokeng[CA_MAXDPMTOKENLEN+1];
char *r_tokenp;
dpm_dbrec_addr rec_addrf;
char srm_endpoint[256];
int status;
int statusg;
strcpy (func, "dpmcopy_push");
if (nbfiles == 0)
return (0); /* nothing to be done */
nb_file_err = 0;
/* build the get/put requests */
if ((getfilereqs = calloc (nbfiles, sizeof(struct dpm_getfilereq))) == NULL ||
(putfilereqs = calloc (nbfiles, sizeof(struct dpm_putfilereq))) == NULL ||
(filstatus = calloc (nbfiles, sizeof(int))) == NULL) {
dpm_req->status = DPM_FAILED | ENOMEM;
return (-1);
}
for (i = 0; i < dpm_req->nbreqfiles; i++) {
(void) dpm_start_tr (0, dbfd);
if (dpm_get_cpr_by_fullid (dbfd, dpm_req->r_token, i,
cpr_entry, 1, &rec_addrf) < 0) {
dpm_abort_tr (dbfd);
continue;
}
if (cpr_entry->status != DPM_QUEUED) { /* No need to process */
dpm_abort_tr (dbfd);
if (cpr_entry->status == DPM_ABORTED ||
(cpr_entry->status & 0xF000) == DPM_FAILED)
nb_file_err++;
continue;
}
if ((p = strdup (cpr_entry->from_surl)) == NULL ||
(q = strdup (cpr_entry->to_surl)) == NULL) {
cpr_entry->status = DPM_FAILED | ENOMEM;
(void) upd_cpr (dbfd, &rec_addrf, cpr_entry);
continue;
}
cpr_entry->status = DPM_ACTIVE;
(void) upd_cpr (dbfd, &rec_addrf, cpr_entry);
(getfilereqs+j)->from_surl = p;
(putfilereqs+j)->to_surl = q;
(putfilereqs+j)->f_lifetime = cpr_entry->f_lifetime;
(putfilereqs+j)->f_type =
cpr_entry->f_type == '_' ? '\0' : cpr_entry->f_type;
strcpy ((putfilereqs+j)->s_token, cpr_entry->s_token);
(putfilereqs+j)->ret_policy =
cpr_entry->ret_policy == '_' ? '\0' : cpr_entry->ret_policy;
(putfilereqs+j)->ac_latency =
cpr_entry->ac_latency == '_' ? '\0' : cpr_entry->ac_latency;
j++;
}
if (j == 0)
return (0); /* nothing to be done */
dpm_seterrbuf (errbuf, sizeof(errbuf));
if (get_srm_endpoint (putfilereqs->to_surl, srm_endpoint, sizeof(srm_endpoint)) < 0) {
dpm_req->status = DPM_FAILED | EINVAL;
strcpy (dpm_req->errstring, "SRM endpoint too long");
return (-1);
}
/* get source TURL(s) */
dpmlogit (func, "calling dpm_get\n");
errbuf[0] = '\0';
*r_tokeng = '\0';
statusg = dpm_get (j, getfilereqs, 1, protocols, NULL, dpm_req->retrytime,
r_tokeng, &nbgetreplies, &getfilestatuses);
if (*r_tokeng)
dpmlogit (func, "dpm_get returned request token %s\n", r_tokeng);
/* wait for any source TURL ready */
while (1) {
for (i = 0; i < j; i++) {
if (*(filstatus+i))
continue; /* already processed */
if (! getfilestatuses) {
if (statusg < 0) {
*(filstatus+i) = DPM_FAILED | serrno;
dpmcopy_set_errstring ("get failed", errbuf, NULL,
errstring, sizeof(errstring));
(void) upd_cpr2 (dbfd, dpm_req->r_token,
(getfilereqs+i)->from_surl,
(putfilereqs+i)->to_surl,
0, *(filstatus+i),
errstring, 0, cpr_entry);
}
continue;
}
if (statusg >= 0 && ((getfilestatuses+i)->status == DPM_QUEUED ||
(getfilestatuses+i)->status == DPM_ACTIVE))
continue;
if ((getfilestatuses+i)->status != DPM_READY) {
if ((getfilestatuses+i)->status < DPM_READY)
*(filstatus+i) = statusg < 0 ? (DPM_FAILED | serrno) : statusg;
else
*(filstatus+i) = (getfilestatuses+i)->status;
dpmcopy_set_errstring ("get failed", errbuf,
(getfilestatuses+i)->errstring,
errstring, sizeof(errstring));
(void) upd_cpr2 (dbfd, dpm_req->r_token,
(getfilereqs+i)->from_surl, (putfilereqs+i)->to_surl,
0, *(filstatus+i), errstring, 0, cpr_entry);
continue;
}
/* source TURL ready */
(putfilereqs+i)->requested_size = (getfilestatuses+i)->filesize;
/* get destination TURL */
errbuf[0] = '\0';
r1 = 0;
putfilestatus = NULL;
r_tokenp = NULL;
dpmlogit (func, "calling srmv2_put\n");
status = srmv2_put (1, putfilereqs+i, 1, protocols,
(dpm_req->flags & 1), dpm_req->retrytime, &r_tokenp,
&nbputreplies, &putfilestatus);
if (r_tokenp)
dpmlogit (func, "srmv2_put returned request token %s\n", r_tokenp);
/* wait for destination TURL ready */
while (status == DPM_QUEUED || status == DPM_ACTIVE) {
dpm_free_pfilest (nbputreplies, putfilestatus);
sleep ((r1++ == 0) ? 1 : DEFPOLLINT);
errbuf[0] = '\0';
putfilestatus = NULL;
status = srmv2_getstatus_putreq (r_tokenp, 0, NULL,
srm_endpoint, &nbputreplies, &putfilestatus,
dpm_req->retrytime);
}
if (! putfilestatus) {
*(filstatus+i) = status < 0 ? (DPM_FAILED | serrno) : status;
dpmcopy_set_errstring ("put failed", errbuf, NULL,
errstring, sizeof(errstring));
goto upd_cfr;
}
if (putfilestatus->status != DPM_READY) {
if (putfilestatus->status < DPM_FAILED)
*(filstatus+i) = status < 0 ? (DPM_FAILED | serrno) : status;
else
*(filstatus+i) = putfilestatus->status;
dpmcopy_set_errstring ("put failed", errbuf,
putfilestatus->errstring,
errstring, sizeof(errstring));
goto upd_cfr;
}
/* destination TURL ready, copy file */
errbuf[0] = '\0';
*(filstatus+i) = DPM_ACTIVE;
dpmlogit (func, "copying %s to %s\n",
(getfilestatuses+i)->turl, putfilestatus->turl);
status = dpm_copyfile ((getfilestatuses+i)->turl,
putfilestatus->turl, 5, 0);
filestatuses = NULL;
if (status < 0) {
dpmlogit (func, "dpm_copyfile returned %d, serrno = %d\n",
status, serrno);
status = DPM_FAILED | serrno;
dpmcopy_set_errstring ("copy failed", errbuf, NULL,
errstring, sizeof(errstring));
(void) srmv2_abortfiles (r_tokenp, 1, &putfilestatus->to_surl,
&nbreplies, &filestatuses, dpm_req->retrytime);
} else {
dpmlogit (func, "dpm_copyfile returned %d\n", status);
errbuf[0] = '\0';
dpmlogit (func, "calling srmv2_putdone\n");
status = srmv2_putdone (r_tokenp, 1, &putfilestatus->to_surl,
&nbreplies, &filestatuses, dpm_req->retrytime);
if (status < 0) {
status = DPM_FAILED | serrno;
dpmcopy_set_errstring ("putdone failed", errbuf, NULL,
errstring, sizeof(errstring));
} else
errstring[0] = '\0';
}
dpm_free_filest (nbreplies, filestatuses);
*(filstatus+i) = status ? status : DPM_DONE;
/* try to get target SURL lifetime */
if (status == 0) {
dpm_free_pfilest (nbputreplies, putfilestatus);
putfilestatus = NULL;
if (srmv2_getstatus_putreq (r_tokenp, 0, NULL,
srm_endpoint, &nbputreplies, &putfilestatus,
dpm_req->retrytime) == 0 &&
putfilestatus)
cpr_entry->f_lifetime = putfilestatus->f_lifetime;
}
upd_cfr:
filestatuses = NULL;
(void) dpm_relfiles (r_tokeng, 1, &(getfilestatuses+i)->from_surl,
0, &nbreplies, &filestatuses);
dpm_free_filest (nbreplies, filestatuses);
(void) upd_cpr2 (dbfd, dpm_req->r_token,
(getfilereqs+i)->from_surl, (putfilereqs+i)->to_surl,
(*(filstatus+i) == DPM_DONE) ? (getfilestatuses+i)->filesize : 0,
(*(filstatus+i) == DPM_DONE) ? 0 : *(filstatus+i),
errstring, cpr_entry->f_lifetime, cpr_entry);
dpm_free_pfilest (nbputreplies, putfilestatus);
if (r_tokenp)
free (r_tokenp);
}
for (i = 0; i < j; i++) {
if (*(filstatus+i) == 0 || *(filstatus+i) == DPM_ACTIVE)
break;
}
if (i >= j) break; /* no more pending file request */
dpm_free_gfilest (nbgetreplies, getfilestatuses);
getfilestatuses = NULL;
errbuf[0] = '\0';
sleep ((r++ == 0) ? 1 : DEFPOLLINT);
statusg = dpm_getstatus_getreq (r_tokeng, 0, NULL,
&nbgetreplies, &getfilestatuses);
}
return (0);
}
|