8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
13 #include <sys/select.h>
17 #include <afs/afsint.h>
18 #define FSINT_COMMON_XG 1
19 #include <afs/afscbint.h>
20 #include <sys/ioctl.h>
21 #include <afs/venus.h>
22 #include <afs/cellconfig.h>
23 #include <afs/sys_prototypes.h>
25 #include <afs/afs_consts.h>
27 /*#include <rx/rxkad.h>*/
28 #include <rx/rx_null.h>
29 #include <rx/rx_prototypes.h>
32 #include <afs/com_err.h>
40 statfile(char *path, char *cellname, afs_uint32 * server, struct AFSFid *f)
45 afs_int32 srvbuf[AFS_MAXHOSTS];
48 if (!strncmp(path, "@afs:", 5)) {
55 if (!(p = strtok(NULL, ":"))) {
59 strncpy(cellname, p, MAXCELLCHARS);
61 if (!(p = strtok(NULL, ":"))) {
65 he = gethostbyname(p);
67 printf("Unknown host %s\n", p);
71 memcpy(server, he->h_addr, he->h_length);
73 if (!(p = strtok(NULL, ":"))) {
79 if (!(p = strtok(NULL, ":"))) {
85 if (!(p = strtok(NULL, ":"))) {
91 if (strtok(NULL, ":")) {
92 printf("Too much extra stuff after @afs:...\n");
103 v.out_size = MAXCELLCHARS;
104 if ((code = pioctl(path, VIOC_FILE_CELL_NAME, &v, 1)))
108 v.out_size = sizeof(struct VenusFid);
109 if ((code = pioctl(path, VIOCGETFID, &v, 1)))
111 memcpy(f, &vf.Fid, sizeof(struct AFSFid));
113 v.out = (char *)srvbuf;
114 v.out_size = sizeof(srvbuf);
115 if ((code = pioctl(path, VIOCWHEREIS, &v, 1)))
117 if (v.out_size <= sizeof(afs_int32))
120 memcpy(server, srvbuf, sizeof(afs_int32));
125 struct rx_securityClass *sc;
128 start_cb_server(void)
130 struct rx_service *s;
132 sc = rxnull_NewServerSecurityObject();
133 s = rx_NewService(0, 1, "afs", &sc, 1, RXAFSCB_ExecuteRequest);
141 /*extern int rx_socket;*/
142 extern unsigned short rx_port;
146 struct sockaddr_in s;
150 fprintf(stderr, "Cannot initialize rx\n");
154 len = sizeof(struct sockaddr_in);
155 if (getsockname(rx_socket, (struct sockaddr *)&s, (socklen_t *)&len)) {
156 perror("getsockname");
159 rx_port = ntohs(s.sin_port);
164 struct rx_securityClass *
165 get_sc(char *cellname)
168 char realm[REALM_SZ];
172 return rxnull_NewClientSecurityObject();
175 ucstring(realm, cellname, REALM_SZ);
177 if (krb_get_cred("afs", "", realm, &c)) {
178 if (get_ad_tkt("afs", "", realm, DEFAULT_TKT_LIFE)) {
181 if (krb_get_cred("afs", "", realm, &c)) {
187 return rxkad_NewClientSecurityObject(rxkad_clear, c.session, c.kvno,
188 c.ticket_st.length, c.ticket_st.dat);
192 #define scindex_NULL 0
193 #define scindex_RXKAD 2
195 #define scindex scindex_RXKAD
197 main(int argc, char **argv)
199 char scell[MAXCELLCHARS], dcell[MAXCELLCHARS];
200 afs_uint32 ssrv, dsrv;
201 char *databuffer, *srcf = NULL, *destd = NULL, *destf = NULL, *destpath = NULL;
204 struct AFSStoreStatus sst;
205 struct AFSFetchStatus fst, dfst;
206 struct AFSVolSync vs;
207 struct AFSCallBack scb, dcb;
208 struct AFSFid sf, dd, df;
211 int ch, blksize, bytesremaining, bytes;
212 struct timeval start, finish;
214 struct rx_securityClass *ssc = 0, *dsc = 0;
215 int sscindex, dscindex;
216 struct rx_connection *sconn = NULL, *dconn = NULL;
217 struct rx_call *scall = NULL, *dcall = NULL;
218 int code = 0, fetchcode, storecode, printcallerrs = 0;
219 int slcl = 0, dlcl = 0, unlock = 0;
220 int sfd = 0, dfd = 0, unauth = 0;
223 struct AFSCBFids theFids;
224 struct AFSCBs theCBs;
229 while ((ch = getopt(argc, argv, "iouUb:s:")) != -1) {
232 blksize = atoi(optarg);
241 sleeptime = atoi(optarg);
250 printf("Unknown option '%c'\n", ch);
256 if (argc - optind + unlock < 2) {
258 "Usage: afscp [-i|-o]] [-b xfersz] [-s time] [-u] [-U] source [dest]\n");
259 fprintf(stderr, " -b Set block size\n");
260 fprintf(stderr, " -i Source is local (copy into AFS)\n");
261 fprintf(stderr, " -o Dest is local (copy out of AFS)\n");
262 fprintf(stderr, " -s Set the seconds to sleep before reading/writing data\n");
263 fprintf(stderr, " -u Run unauthenticated\n");
264 fprintf(stderr, " -U Send an unlock request for source. (dest path not required)\n");
265 fprintf(stderr, "source and dest can be paths or specified as:\n");
266 fprintf(stderr, " @afs:cellname:servername:volume:vnode:uniq\n");
269 srcf = argv[optind++];
271 destpath = argv[optind++];
272 destd = strdup(destpath);
277 if ((destf = strrchr(destd, '/'))) {
284 fprintf(stderr, "-i and -U cannot be used together\n");
287 if (!slcl && statfile(srcf, scell, &ssrv, &sf)) {
288 fprintf(stderr, "Cannot get attributes of %s\n", srcf);
291 if (!unlock && !dlcl && statfile(destd, dcell, &dsrv, &dd)) {
292 fprintf(stderr, "Cannot get attributes of %s\n", destd);
296 if ((databuffer = malloc(blksize)) == NULL) {
304 if (start_cb_server()) {
305 printf("Cannot start callback service\n");
310 sscindex = scindex_RXKAD;
311 if (unauth || (ssc = get_sc(scell)) == NULL) {
312 ssc = rxnull_NewClientSecurityObject();
313 sscindex = scindex_NULL;
314 /*printf("Cannot get authentication for cell %s; running unauthenticated\n", scell); */
316 sscindex = scindex_NULL;
319 rx_NewConnection(ssrv, htons(AFSCONF_FILEPORT), 1, ssc,
324 printf("Cannot initialize rx connection to source server (%s)\n",
330 if (!dlcl && !unlock) {
331 if (!slcl && ssrv == dsrv) {
335 if (slcl || strcmp(scell, dcell)) {
336 dscindex = scindex_RXKAD;
337 if (unauth || (dsc = get_sc(dcell)) == NULL) {
338 dsc = rxnull_NewClientSecurityObject();
339 dscindex = scindex_NULL;
340 /*printf("Cannot get authentication for cell %s; running unauthenticated\n", dcell); */
342 dscindex = scindex_NULL;
349 rx_NewConnection(dsrv, htons(AFSCONF_FILEPORT), 1, dsc,
355 ("Cannot initialize rx connection to dest server (%s)\n",
363 memset(&sst, 0, sizeof(struct AFSStoreStatus));
365 if (dlcl && !unlock) {
366 dfd = open(destpath, O_RDWR | O_CREAT | O_EXCL, 0666);
367 if (dfd < 0 && errno == EEXIST) {
368 printf("%s already exists, overwriting\n", destpath);
369 dfd = open(destpath, O_RDWR | O_TRUNC, 0666);
371 fprintf(stderr, "Cannot open %s (%s)\n", destpath,
372 afs_error_message(errno));
375 } else if (dfd < 0) {
376 fprintf(stderr, "Cannot open %s (%s)\n", destpath,
377 afs_error_message(errno));
380 } else if (!unlock) {
382 RXAFS_CreateFile(dconn, &dd, destf, &sst, &df, &fst, &dfst, &dcb,
384 if (code == EEXIST) {
385 printf("%s already exits, overwriting\n", destpath);
386 if (statfile(destpath, dcell, &dsrv, &df))
387 fprintf(stderr, "Cannot get attributes of %s\n",
392 printf("Cannot create %s (%s)\n", destpath,
393 afs_error_message(code));
401 sfd = open(srcf, O_RDONLY, 0);
403 fprintf(stderr, "Cannot open %s (%s)\n", srcf,
404 afs_error_message(errno));
407 if (fstat(sfd, &statbuf) < 0) {
408 fprintf(stderr, "Cannot stat %s (%s)\n", srcf,
409 afs_error_message(errno));
414 if ((code = RXAFS_FetchStatus(sconn, &sf, &fst, &scb, &vs))) {
415 printf("Cannot fetchstatus of %d.%d (%s)\n", sf.Volume, sf.Vnode,
416 afs_error_message(code));
424 filesz = statbuf.st_size;
432 if (!slcl && !unlock)
433 scall = rx_NewCall(sconn);
434 if (!dlcl && !unlock)
435 dcall = rx_NewCall(dconn);
436 gettimeofday(&start, &tz);
439 printf("Sending 1 unlock for %s (%d locks)\n", srcf, fst.lockCount);
440 if ((code = RXAFS_ReleaseLock(sconn, &sf, &vs))) {
441 printf("Unable to unlock %s (%s)\n", srcf,
442 afs_error_message(code));
445 printf("No locks for %s\n", srcf);
452 if ((code = StartRXAFS_FetchData(scall, &sf, 0, filesz))) {
453 printf("Unable to fetch data from %s (%s)\n", srcf,
454 afs_error_message(code));
461 sst.Mask = AFS_SETMODTIME | AFS_SETMODE;
462 sst.ClientModTime = statbuf.st_mtime;
464 statbuf.st_mode & ~(S_IFMT | S_ISUID | S_ISGID);
466 sst.Mask = AFS_SETMODTIME | AFS_SETMODE;
467 sst.ClientModTime = fst.ClientModTime;
469 fst.UnixModeBits & ~(S_IFMT | S_ISUID | S_ISGID);
473 StartRXAFS_StoreData(dcall, &df, &sst, 0, filesz, filesz))) {
474 printf("Unable to store data to %s (%s)\n", destpath,
475 afs_error_message(code));
481 bytesremaining = statbuf.st_size;
483 rx_Read(scall, (char *)&bytesremaining, sizeof(afs_int32));
484 bytesremaining = ntohl(bytesremaining);
488 #ifdef AFS_PTHREAD_ENV
491 IOMGR_Sleep(sleeptime);
495 while (bytesremaining > 0) {
496 /*printf("%d bytes remaining\n",bytesremaining); */
499 read(sfd, databuffer, min(blksize, bytesremaining))) <= 0) {
505 rx_Read(scall, databuffer,
506 min(blksize, bytesremaining))) <= 0)
510 if (write(dfd, databuffer, bytes) != bytes) {
515 if (rx_Write(dcall, databuffer, bytes) != bytes)
518 bytesremaining -= bytes;
519 /*printf("%d bytes copied\n",bytes); */
523 if (bytesremaining > 0) {
524 printf("Some network error occured while copying data\n");
529 fetchcode = EndRXAFS_FetchData(scall, &fst, &scb, &vs);
531 storecode = EndRXAFS_StoreData(dcall, &fst, &vs);
536 if (close(sfd) && !fetchcode)
539 fetchcode = rx_EndCall(scall, fetchcode);
542 printf("Error returned from fetch: %s\n", afs_error_message(fetchcode));
545 if (close(dfd) && !storecode)
547 } else if (!unlock) {
548 storecode = rx_EndCall(dcall, storecode);
551 printf("Error returned from store: %s\n", afs_error_message(storecode));
553 gettimeofday(&finish, &tz);
556 theFids.AFSCBFids_len = 1;
557 theFids.AFSCBFids_val = &sf;
558 theCBs.AFSCBs_len = 1;
559 theCBs.AFSCBs_val = &scb;
560 scb.CallBackType = CB_DROPPED;
561 if ((code = RXAFS_GiveUpCallBacks(sconn, &theFids, &theCBs)))
562 printf("Could not give up source callback: %s\n",
563 afs_error_message(code));
567 theFids.AFSCBFids_len = 1;
568 theFids.AFSCBFids_val = &df;
569 theCBs.AFSCBs_len = 1;
570 theCBs.AFSCBs_val = &dcb;
571 dcb.CallBackType = CB_DROPPED;
572 if ((code = RXAFS_GiveUpCallBacks(dconn, &theFids, &theCBs)))
573 printf("Could not give up target callback: %s\n",
574 afs_error_message(code));
583 if (!dlcl && !unlock && (slcl || dconn != sconn))
584 rx_DestroyConnection(dconn);
587 rx_DestroyConnection(sconn);
589 if (dsc && dsc != ssc)
597 if (printcallerrs && !unlock) {
598 double rate, size, time;
599 if (finish.tv_sec == start.tv_sec) {
600 printf("Copied %d bytes in %d microseconds\n", filesz,
601 (int)(finish.tv_usec - start.tv_usec));
603 printf("Copied %d bytes in %d seconds\n", filesz,
604 (int)(finish.tv_sec - start.tv_sec));
607 size = filesz / 1024.0;
609 finish.tv_sec - start.tv_sec + (finish.tv_usec -
610 start.tv_usec) / 1e+06;
612 printf("Transfer rate %g Kbytes/sec\n", rate);