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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
/*
* $Id: rfcp.c 3172 2010-02-15 08:51:54Z baud $
*/
/*
* Copyright (C) 1994-2010 by CERN/IT/PDP/DM
* All rights reserved
*/
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: rfcp.c,v $ $Revision: 3172 $ $Date: 2010-02-15 09:51:54 +0100 (Mon, 15 Feb 2010) $ CERN/IT/DS/HSM Felix Hassine, Olof Barring, Jean-Damien Durand";
#endif /* not lint */
#include <signal.h>
#include <fcntl.h>
#define RFIO_KERNEL 1
#include <rfio.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <osdep.h>
#include <u64subr.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <Castor_limits.h>
#ifndef TRANSFER_UNIT
#define TRANSFER_UNIT 131072
#endif
#define SYERR 2
#define USERR 1
#define OK 0
extern int serrno ;
extern int rfio_errno ;
extern char *getconfent() ;
#if sgi
extern char *strdup _PROTO((CONST char *));
#endif
void usage();
off64_t copyfile _PROTO((int, int, char *, u_signed64));
#ifdef CNS_ROOT
void copyfile_stgcleanup _PROTO((int));
#endif /* CNS_ROOT */
int have_maxsize;
u_signed64 maxsize;
int v2 = 0;
u_signed64 inpfile_size = 0;
#ifndef _WIN32
/* Signal handler - Simplify the POSIX sigaction calls */
#ifdef __STDC__
typedef void Sigfunc(int);
Sigfunc *_rfio_signal(int, Sigfunc *);
#else
typedef void Sigfunc();
Sigfunc *_rfio_signal();
#endif
#else
#define _rfio_signal(a,b) signal(a,b)
#endif
int main(argc, argv)
int argc;
char *argv[];
{
int argvindx; /* argument index in program argv */
#if defined(_WIN32)
int binmode = O_BINARY;
#else
int binmode = 0;
#endif
int c;
int cfargc; /* number of arguments in command file */
char **cfargv; /* arguments in command file */
int cfargvindx; /* argument index in command file */
char *curargv; /* current argv */
int fd1, fd2;
int incmdfile = 0; /* 1 = processing command file */
struct stat64 sbuf, sbuf2;
off64_t size;
time_t starttime, endtime;
int rc ;
int rc2 ;
char filename[BUFSIZ], filename_sav[BUFSIZ], inpfile[BUFSIZ], outfile[BUFSIZ] ;
char *cp , *ifce ;
char ifce1[8] ;
char ifce2[8] ;
char *host1, *host2, *path1, *path2 ;
char shost1[32];
int l1, l2 ;
int v;
extern char * getifnam() ;
#if defined(_WIN32)
WSADATA wsadata;
#endif
int input_is_local = 1;
have_maxsize = -1;
maxsize = 0;
cfargc = 0;
cfargvindx = 0;
inpfile[0] = '\0';
outfile[0] = '\0';
memset(&sbuf,'\0',sizeof(sbuf));
memset(&sbuf2,'\0',sizeof(sbuf2));
for (argvindx = 1; argvindx < argc || cfargvindx < cfargc; ) {
if (cfargvindx >= cfargc) {
incmdfile = 0;
}
curargv = incmdfile ? cfargv[cfargvindx++] : argv[argvindx++];
#if defined(_WIN32)
if (strcmp (curargv, "-a") == 0) {
binmode = O_TEXT;
continue;
} else if (strcmp (curargv, "-b") == 0) {
binmode = O_BINARY;
continue;
} else if (*curargv == '@') {
if ((cfargc = cmdf2argv (curargv+1, &cfargv)) < 0) {
exit (USERR);
}
if (cfargc == 0) {
continue;
}
incmdfile = 1;
cfargvindx = 0;
continue;
}
#endif
if (strcmp (curargv, "-s") == 0) {
curargv = incmdfile ? cfargv[cfargvindx++] : argv[argvindx++];
/* We verify that curargv do not contain other characters but digits */
if (strspn(curargv,"0123456789") != strlen(curargv)) {
fprintf(stderr,"-s option value should contain only digits\n");
exit(USERR);
}
maxsize = strtou64(curargv);
if (maxsize == 0) {
usage();
}
have_maxsize = 1;
curargv = incmdfile ? cfargv[cfargvindx++] : argv[argvindx++];
if (strcmp(curargv, "-v2") == 0) {
if (v2) {
usage(); /* Option yet parsed */
}
v2 = 1;
curargv = incmdfile ? cfargv[cfargvindx++] : argv[argvindx++];
}
} else if (strcmp(curargv, "-v2") == 0) {
if (v2) {
usage(); /* Option yet parsed */
}
v2 = 1;
continue;
}
/* Special treatment for filenames starting with /scratch/... */
if (inpfile[0] == '\0') {
if (!strncmp ("/scratch/", curargv, 9) &&
(cp = getconfent ("SHIFT", "SCRATCH", 0)) != NULL) {
strcpy (inpfile, cp);
strcat (inpfile, curargv+9);
} else
strcpy (inpfile, curargv);
} else {
if (!strncmp ("/scratch/", curargv, 9) &&
(cp = getconfent ("SHIFT", "SCRATCH", 0)) != NULL) {
strcpy (outfile, cp);
strcat (outfile, curargv+9);
} else
strcpy (outfile, curargv);
}
}
if (! outfile[0]) {
usage();
}
/* Check that files are not identical ! */
#if defined(_WIN32)
if (WSAStartup (MAKEWORD (2, 0), &wsadata)) {
fprintf (stderr, "WSAStartup unsuccessful\n");
exit (SYERR);
}
#endif
if ( rfio_stat64( outfile, &sbuf2) == 0 &&
S_ISDIR(sbuf2.st_mode) ) {
/* Not allowed if inpfile is stdin */
if (strcmp(inpfile,"-") == 0) {
fprintf (stderr, "Not valid output when input is the stdin\n");
exit (USERR);
}
if ( (cp = strrchr(inpfile,'/')) != NULL ) {
cp++;
} else {
cp = inpfile;
}
sprintf(filename, "%s/%s", outfile, cp);
} else {
strcpy(filename,outfile);
}
strcpy(filename_sav,filename);
l1 = rfio_parseln( inpfile , &host1, &path1, NORDLINKS ) ;
if (l1 < 0) {
fprintf(stderr,"%s\n",sstrerror(serrno));
exit (USERR);
}
if ( l1 ) {
strcpy(shost1,host1) ;
input_is_local = 0;
}
strcpy( filename, path1 );
l2 = rfio_parseln( filename_sav , &host2, &path2, NORDLINKS ) ;
if (l2 < 0) {
fprintf(stderr,"%s\n",sstrerror(serrno));
exit(USERR);
}
/* Command is of the form cp f1 f2. */
serrno = rfio_errno = 0;
if (strcmp(inpfile,"-") != 0) {
rc = rfio_stat64(inpfile, &sbuf);
if ( rc == 0 && ( S_ISDIR(sbuf.st_mode) || S_ISCHR(sbuf.st_mode)
#if !defined(_WIN32)
|| S_ISBLK(sbuf.st_mode)
#endif
) ) {
fprintf(stderr,"file %s: Not a regular file\n",inpfile);
#if defined(_WIN32)
WSACleanup();
#endif
exit(USERR) ;
} else if (rc == 0) {
inpfile_size = (u_signed64) sbuf.st_size;
} else {
rfio_perror(inpfile);
#if defined(_WIN32)
WSACleanup();
#endif
exit(USERR) ;
}
} else {
sbuf.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; /* Default open mode */
}
strcpy(filename,filename_sav);
if ( ( l1 == 0 && l2 == 0 && (!memcmp(&sbuf,&sbuf2,sizeof(sbuf))) ) ||
( l1 && l2 && !strcmp( shost1, host2 ) && (!memcmp(&sbuf,&sbuf2,sizeof(sbuf))) ) ) {
fprintf(stderr,"files are identical \n");
#if defined(_WIN32)
WSACleanup();
#endif
exit (USERR) ;
}
/* Activate new transfer mode for source file */
if (! v2) {
v = RFIO_STREAM;
rfiosetopt(RFIO_READOPT,&v,4);
}
serrno = rfio_errno = 0;
#ifdef CNS_ROOT
if (rfio_HsmIf_IsHsmFile(inpfile)) {
/* The input is a CASTOR file - we need a signal handler because rfio_open() calls the stager */
#if ! defined(_WIN32)
_rfio_signal (SIGHUP, copyfile_stgcleanup);
_rfio_signal (SIGQUIT, copyfile_stgcleanup);
#endif
_rfio_signal (SIGINT, copyfile_stgcleanup);
_rfio_signal (SIGTERM, copyfile_stgcleanup);
}
if (rfio_HsmIf_IsHsmFile(inpfile) && (have_maxsize > 0) && (inpfile_size > 0)) {
/* User specified a CASTOR file in input with a limited size in bytes */
/* We cannot use rfio_open() directly because then the stager will try to */
/* stage the full file */
serrno = rfio_errno = 0;
fd1 = rfio_HsmIf_open_limbysz(inpfile,O_RDONLY|binmode ,0644, (maxsize < inpfile_size) ? maxsize : inpfile_size, 1);
} else {
#endif /* CNS_ROOT */
serrno = rfio_errno = 0;
if (strcmp(inpfile,"-") == 0) {
fd1 = fileno(stdin);
} else {
fd1 = rfio_open64(inpfile,O_RDONLY|binmode ,0644);
}
#ifdef CNS_ROOT
}
#endif /* CNS_ROOT */
if (fd1 < 0) {
if (serrno) {
rfio_perror(inpfile);
#ifdef CNS_ROOT
c = (serrno == EACCES || serrno == EINVAL || serrno == ENOENT) ? USERR : SYERR;
#else
c = SYERR;
#endif
} else {
switch (rfio_errno) {
case EACCES:
case EISDIR:
case ENOENT:
case EPERM :
rfio_perror(inpfile);
c = USERR;
break ;
case ENOSPC :
rfio_perror(inpfile);
c = ENOSPC;
break ;
case EBUSY :
rfio_perror(inpfile);
c = EBUSY;
break ;
case 0:
switch(errno) {
case EACCES:
case EISDIR:
case ENOENT:
case EPERM :
rfio_perror(inpfile);
c = USERR;
break;
case ENOSPC :
rfio_perror(inpfile);
c = ENOSPC;
break;
case EBUSY :
rfio_perror(inpfile);
c = EBUSY;
break;
default:
rfio_perror(inpfile);
c = SYERR;
}
break;
default:
rfio_perror(inpfile);
c = SYERR;
}
}
#if defined(_WIN32)
WSACleanup();
#endif
exit (c);
}
if ( (ifce=getifnam(fd1))==NULL ) {
strcpy(ifce1,"local");
} else {
strcpy(ifce1,ifce);
}
/* Activate new transfer mode for target file */
if (! v2) {
v = RFIO_STREAM;
rfiosetopt(RFIO_READOPT,&v,4);
}
serrno = rfio_errno = 0;
fd2 = rfio_open64(filename , O_WRONLY|O_CREAT|O_TRUNC|binmode ,sbuf.st_mode & 0777);
if (fd2 < 0) {
if (serrno) {
rfio_perror(outfile);
#ifdef CNS_ROOT
c = (serrno == EACCES || serrno == EINVAL || serrno == ENOENT) ? USERR : SYERR;
#else
c = SYERR;
#endif
} else {
switch (rfio_errno) {
case EACCES:
case EISDIR:
case ENOENT:
case EPERM :
rfio_perror(outfile);
c = USERR;
break;
case ENOSPC :
rfio_perror(outfile);
c = ENOSPC;
break;
case EBUSY :
rfio_perror(outfile);
c = EBUSY;
break;
case 0:
switch(errno) {
case EACCES:
case EISDIR:
case ENOENT:
case EPERM :
rfio_perror(outfile);
c = USERR;
break;
case ENOSPC :
rfio_perror(outfile);
c = ENOSPC;
break;
case EBUSY :
rfio_perror(outfile);
c = EBUSY;
break;
default:
rfio_perror(outfile);
c = SYERR;
}
break;
default:
rfio_perror(outfile);
c = SYERR;
}
}
#if defined(_WIN32)
WSACleanup();
#endif
exit (c);
}
/*
* Get interfaces names
*/
if ( (ifce=getifnam(fd2))==NULL ) {
strcpy(ifce2,"local");
} else {
strcpy(ifce2,ifce);
}
time(&starttime);
size = copyfile(fd1, fd2, outfile, maxsize);
if ( rfio_HsmIf_GetHsmType(fd1,NULL) == RFIO_HSM_CNS ) {
if (rfio_close(fd1) < 0) {
rfio_perror("close source");
#if defined(_WIN32)
WSACleanup();
#endif
exit(SYERR);
}
}
if ( rfio_HsmIf_GetHsmType(fd2,NULL) == RFIO_HSM_CNS ) {
if (rfio_close(fd2) < 0) {
struct stat64 sbuf;
mode_t mode;
rfio_perror("close target");
rfio_stat64(outfile, &sbuf); /* check for special files */
mode = sbuf.st_mode & S_IFMT;
if (mode == S_IFREG) {
rfio_unlink(filename);
}
#if defined(_WIN32)
WSACleanup();
#endif
exit(SYERR);
}
}
time(&endtime);
if (size>0) {
char tmpbuf[21];
char tmpbuf2[21];
fprintf(stdout,"%s bytes in %d seconds through %s (in) and %s (out)", u64tostr(size, tmpbuf, 0), (int) (endtime-starttime),ifce1,ifce2);
l1 = endtime-starttime ;
if ( l1 > 0) {
fprintf(stdout," (%d KB/sec)\n",(int) (size/(1024*l1)) );
} else {
fprintf(stdout,"\n");
}
if (have_maxsize < 0) {
rc2 = (inpfile_size == size ? OK : ((strcmp(inpfile,"-") == 0) ? OK : SYERR));
if (rc2 == SYERR) {
#ifdef _WIN32
if (binmode != O_BINARY) {
fprintf(stderr,"%s : got %s bytes instead of %s bytes\n", "Warning", u64tostr(size, tmpbuf, 0), u64tostr(inpfile_size, tmpbuf2, 0));
rc = OK;
} else {
#endif
fprintf(stderr,"%s : got %s bytes instead of %s bytes\n", sstrerror(SESYSERR), u64tostr(size, tmpbuf, 0), u64tostr(inpfile_size, tmpbuf2, 0));
#ifdef _WIN32
}
#endif
}
} else {
if (maxsize < inpfile_size) { /* If input is "-", inpfile_size is zero */
rc2 = (maxsize == size ? OK : SYERR);
if (rc2 == SYERR) {
#ifdef _WIN32
if (binmode != O_BINARY) {
fprintf(stderr,"%s : got %s bytes instead of %s bytes\n", "Warning", u64tostr(size, tmpbuf, 0), u64tostr(maxsize, tmpbuf2, 0));
rc = OK;
} else {
#endif
fprintf(stderr,"%s : got %s bytes instead of %s bytes\n", sstrerror(SESYSERR), u64tostr(size, tmpbuf, 0), u64tostr(maxsize, tmpbuf2, 0));
#ifdef _WIN32
}
#endif
}
} else {
rc2 = (inpfile_size == size ? OK : ((strcmp(inpfile,"-") == 0) ? OK : SYERR));
if (rc2 == SYERR) {
#ifdef _WIN32
if (binmode != O_BINARY) {
fprintf(stderr,"%s : got %s bytes instead of %s bytes\n", "Warning", u64tostr(size, tmpbuf, 0), u64tostr(inpfile_size, tmpbuf2, 0));
rc = OK;
} else {
#endif
fprintf(stderr,"%s : got %s bytes instead of %s bytes\n", sstrerror(SESYSERR), u64tostr(size, tmpbuf, 0), u64tostr(inpfile_size, tmpbuf2, 0));
#ifdef _WIN32
}
#endif
}
}
}
} else if (size < 0) {
rc2 = SYERR;
} else {
char tmpbuf[21];
fprintf(stdout,"%s bytes transferred !!\n",u64tostr(size, tmpbuf, 0));
rc2 = (inpfile_size == 0 ? OK : SYERR);
}
#if defined(_WIN32)
WSACleanup();
#endif
exit(rc2);
}
off64_t copyfile(fd1, fd2, name, maxsize)
int fd1, fd2;
u_signed64 maxsize;
char *name;
{
int n;
mode_t mode;
int m = -1;
struct stat64 sbuf;
off64_t effsize=0;
char *p;
int bufsize = TRANSFER_UNIT;
char * cpbuf;
extern char *getenv(); /* External declaration */
u_signed64 total_bytes = 0;
if ((p=getenv("RFCPBUFSIZ")) == NULL) {
if ((p=getconfent("RFCP","BUFFERSIZE",0)) == NULL) {
bufsize=TRANSFER_UNIT;
} else {
bufsize=atoi(p);
}
} else {
bufsize=atoi(p);
}
if (bufsize<=0) bufsize=TRANSFER_UNIT;
if ( ( cpbuf = malloc(bufsize) ) == NULL ) {
perror("malloc");
#if defined(_WIN32)
WSACleanup();
#endif
exit (SYERR);
}
do {
if (have_maxsize != -1) {
if ((maxsize - total_bytes) < bufsize)
bufsize = maxsize - total_bytes;
}
serrno = rfio_errno = 0;
n = rfio_read(fd1, cpbuf, bufsize);
if (n > 0) {
int count = 0;
total_bytes += n;
effsize += n;
serrno = rfio_errno = 0;
while (count != n && (m = rfio_write(fd2, cpbuf+count, n-count)) > 0) {
count += m;
}
if (m < 0) {
int save_err = rfio_serrno();
/* Write failed. Don't keep truncated regular file. */
rfio_perror("rfcp");
if ( rfio_HsmIf_GetHsmType(fd2,NULL) != RFIO_HSM_CNS ) {
if (rfio_close(fd2) < 0) {
rfio_perror("close target");
}
}
rfio_stat64(name, &sbuf); /* check for special files */
mode = sbuf.st_mode & S_IFMT;
if (mode == S_IFREG) rfio_unlink(name);
#if defined(_WIN32)
WSACleanup();
#endif
free(cpbuf);
exit((save_err == ENOSPC) ? ENOSPC : ((serrno == EBUSY) ? EBUSY : SYERR));
}
} else {
if (n < 0) {
rfio_perror("cp: read error");
break;
}
}
} while ((total_bytes != maxsize) && (n > 0));
serrno = rfio_errno = 0;
if (rfio_HsmIf_GetHsmType(fd1,NULL) != RFIO_HSM_CNS && rfio_close(fd1) < 0) {
rfio_perror("close source");
#if defined(_WIN32)
WSACleanup();
#endif
free(cpbuf);
exit(SYERR);
}
serrno = rfio_errno = 0;
if (rfio_HsmIf_GetHsmType(fd2,NULL) != RFIO_HSM_CNS && rfio_close(fd2) < 0) {
rfio_perror("close target");
rfio_stat64(name, &sbuf); /* check for special files */
mode = sbuf.st_mode & S_IFMT;
if (mode == S_IFREG) {
rfio_unlink(name);
}
#if defined(_WIN32)
WSACleanup();
#endif
free(cpbuf);
exit(SYERR);
}
free(cpbuf);
return(effsize);
}
void usage()
{
fprintf(stderr,"Usage: rfcp [-s maxsize] [-v2] f1 f2; or rfcp f1 <dir2>\n");
exit(USERR);
}
#ifdef CNS_ROOT
void copyfile_stgcleanup(sig)
int sig;
{
TRACE(2,"rfio","copyfile_stgcleanup: Received signal No %d", sig);
}
#endif /* CNS_ROOT */
#ifndef _WIN32
Sigfunc *_rfio_signal(signo, func)
int signo;
Sigfunc *func;
{
struct sigaction act, oact;
int n = 0;
act.sa_handler = func;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
#endif
} else {
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART; /* SVR4, 44BSD */
#endif
}
n = sigaction(signo, &act, &oact);
if (n < 0) {
return(SIG_ERR);
}
switch (signo) {
#if ! defined(_WIN32)
case SIGHUP:
TRACE(2,"rfio","_rfio_signal: Trapping SIGHUP");
break;
case SIGQUIT:
TRACE(2,"rfio","_rfio_signal: Trapping SIGQUIT");
break;
#endif
case SIGINT:
TRACE(2,"rfio","_rfio_signal: Trapping SIGINT");
break;
case SIGTERM:
TRACE(2,"rfio","_rfio_signal: Trapping SIGTERM");
break;
default:
TRACE(2,"rfio","_rfio_signal: Trapping signal No %d", signo);
break;
}
return(oact.sa_handler);
}
#endif /* #ifndef _WIN32 */
|