afscp: Add -s option
[openafs.git] / src / tests / afscp.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/select.h>
14 #include <sys/stat.h>
15 #include <netdb.h>
16
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>
24
25 #include <afs/afs_consts.h>
26
27 /*#include <rx/rxkad.h>*/
28 #include <rx/rx_null.h>
29 #include <rx/rx_prototypes.h>
30
31 /*#include <krb.h>*/
32 #include <afs/com_err.h>
33
34 struct VenusFid {
35     afs_int32 Cell;
36     struct AFSFid Fid;
37 };
38
39 int
40 statfile(char *path, char *cellname, afs_uint32 * server, struct AFSFid *f)
41 {
42
43     struct ViceIoctl v;
44     struct VenusFid vf;
45     afs_int32 srvbuf[AFS_MAXHOSTS];
46     int code;
47
48     if (!strncmp(path, "@afs:", 5)) {
49         char *pdup, *p;
50         struct hostent *he;
51
52         pdup = strdup(path);
53         strtok(pdup, ":");
54
55         if (!(p = strtok(NULL, ":"))) {
56             free(pdup);
57             return -1;
58         }
59         strncpy(cellname, p, MAXCELLCHARS);
60
61         if (!(p = strtok(NULL, ":"))) {
62             free(pdup);
63             return -1;
64         }
65         he = gethostbyname(p);
66         if (!he) {
67             printf("Unknown host %s\n", p);
68             free(pdup);
69             return -1;
70         }
71         memcpy(server, he->h_addr, he->h_length);
72
73         if (!(p = strtok(NULL, ":"))) {
74             free(pdup);
75             return -1;
76         }
77         f->Volume = atoi(p);
78
79         if (!(p = strtok(NULL, ":"))) {
80             free(pdup);
81             return -1;
82         }
83         f->Vnode = atoi(p);
84
85         if (!(p = strtok(NULL, ":"))) {
86             free(pdup);
87             return -1;
88         }
89         f->Unique = atoi(p);
90
91         if (strtok(NULL, ":")) {
92             printf("Too much extra stuff after @afs:...\n");
93             free(pdup);
94             return -1;
95         }
96
97         return 0;
98     }
99
100     v.in = 0;
101     v.in_size = 0;
102     v.out = cellname;
103     v.out_size = MAXCELLCHARS;
104     if ((code = pioctl(path, VIOC_FILE_CELL_NAME, &v, 1)))
105         return code;
106
107     v.out = (char *)&vf;
108     v.out_size = sizeof(struct VenusFid);
109     if ((code = pioctl(path, VIOCGETFID, &v, 1)))
110         return code;
111     memcpy(f, &vf.Fid, sizeof(struct AFSFid));
112
113     v.out = (char *)srvbuf;
114     v.out_size = sizeof(srvbuf);
115     if ((code = pioctl(path, VIOCWHEREIS, &v, 1)))
116         return code;
117     if (v.out_size <= sizeof(afs_int32))
118         return EINVAL;
119
120     memcpy(server, srvbuf, sizeof(afs_int32));
121     return 0;
122 }
123
124
125 struct rx_securityClass *sc;
126
127 extern int
128 start_cb_server(void)
129 {
130     struct rx_service *s;
131
132     sc = rxnull_NewServerSecurityObject();
133     s = rx_NewService(0, 1, "afs", &sc, 1, RXAFSCB_ExecuteRequest);
134     if (!s)
135         return 1;
136     rx_StartServer(0);
137     return 0;
138 }
139
140
141 /*extern int rx_socket;*/
142 extern unsigned short rx_port;
143 int
144 do_rx_Init(void)
145 {
146     struct sockaddr_in s;
147     int len;
148
149     if (rx_Init(0)) {
150         fprintf(stderr, "Cannot initialize rx\n");
151         return 1;
152     }
153
154     len = sizeof(struct sockaddr_in);
155     if (getsockname(rx_socket, (struct sockaddr *)&s, (socklen_t *)&len)) {
156         perror("getsockname");
157         return 1;
158     }
159     rx_port = ntohs(s.sin_port);
160
161     return 0;
162 }
163
164 struct rx_securityClass *
165 get_sc(char *cellname)
166 {
167 #if 0
168     char realm[REALM_SZ];
169     CREDENTIALS c;
170 #endif
171
172     return rxnull_NewClientSecurityObject();
173 #if 0
174
175     ucstring(realm, cellname, REALM_SZ);
176
177     if (krb_get_cred("afs", "", realm, &c)) {
178         if (get_ad_tkt("afs", "", realm, DEFAULT_TKT_LIFE)) {
179             return NULL;
180         } else {
181             if (krb_get_cred("afs", "", realm, &c)) {
182                 return NULL;
183             }
184         }
185     }
186
187     return rxkad_NewClientSecurityObject(rxkad_clear, c.session, c.kvno,
188                                          c.ticket_st.length, c.ticket_st.dat);
189 #endif
190 }
191
192 #define scindex_NULL 0
193 #define scindex_RXKAD 2
194
195 #define scindex scindex_RXKAD
196 int
197 main(int argc, char **argv)
198 {
199     char scell[MAXCELLCHARS], dcell[MAXCELLCHARS];
200     afs_uint32 ssrv, dsrv;
201     char *databuffer, *srcf = NULL, *destd = NULL, *destf = NULL, *destpath = NULL;
202     struct stat statbuf;
203
204     struct AFSStoreStatus sst;
205     struct AFSFetchStatus fst, dfst;
206     struct AFSVolSync vs;
207     struct AFSCallBack scb, dcb;
208     struct AFSFid sf, dd, df;
209
210     int filesz = 0;
211     int ch, blksize, bytesremaining, bytes;
212     struct timeval start, finish;
213     struct timezone tz;
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;
221     int sleeptime = 0;
222
223     struct AFSCBFids theFids;
224     struct AFSCBs theCBs;
225
226
227     blksize = 8 * 1024;
228
229     while ((ch = getopt(argc, argv, "iouUb:s:")) != -1) {
230         switch (ch) {
231         case 'b':
232             blksize = atoi(optarg);
233             break;
234         case 'i':
235             slcl = 1;
236             break;
237         case 'o':
238             dlcl = 1;
239             break;
240         case 's':
241             sleeptime = atoi(optarg);
242             break;
243         case 'u':
244             unauth = 1;
245             break;
246         case 'U':
247             unlock = 1;
248             break;
249         default:
250             printf("Unknown option '%c'\n", ch);
251             exit(1);
252         }
253     }
254
255
256     if (argc - optind + unlock < 2) {
257         fprintf(stderr,
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");
267         exit(1);
268     }
269     srcf = argv[optind++];
270     if (!unlock) {
271         destpath = argv[optind++];
272         destd = strdup(destpath);
273         if (!destd) {
274             perror("strdup");
275             exit(1);
276         }
277         if ((destf = strrchr(destd, '/'))) {
278             *destf++ = 0;
279         } else {
280             destf = destd;
281             destd = ".";
282         }
283     } else if (slcl) {
284         fprintf(stderr, "-i and -U cannot be used together\n");
285     }
286
287     if (!slcl && statfile(srcf, scell, &ssrv, &sf)) {
288         fprintf(stderr, "Cannot get attributes of %s\n", srcf);
289         exit(1);
290     }
291     if (!unlock && !dlcl && statfile(destd, dcell, &dsrv, &dd)) {
292         fprintf(stderr, "Cannot get attributes of %s\n", destd);
293         exit(1);
294     }
295
296     if ((databuffer = malloc(blksize)) == NULL) {
297         perror("malloc");
298         exit(1);
299     }
300
301     if (do_rx_Init())
302         exit(1);
303
304     if (start_cb_server()) {
305         printf("Cannot start callback service\n");
306         goto Fail_rx;
307     }
308
309     if (!slcl) {
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); */
315         }
316         sscindex = scindex_NULL;
317
318         if ((sconn =
319              rx_NewConnection(ssrv, htons(AFSCONF_FILEPORT), 1, ssc,
320                               sscindex))
321             == NULL) {
322             struct in_addr s;
323             s.s_addr = ssrv;
324             printf("Cannot initialize rx connection to source server (%s)\n",
325                    inet_ntoa(s));
326             goto Fail_sc;
327         }
328     }
329
330     if (!dlcl && !unlock) {
331         if (!slcl && ssrv == dsrv) {
332             dconn = sconn;
333             dsc = NULL;
334         } else {
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); */
341                 }
342                 dscindex = scindex_NULL;
343             } else {
344                 dsc = ssc;
345                 dscindex = sscindex;
346             }
347
348             if ((dconn =
349                  rx_NewConnection(dsrv, htons(AFSCONF_FILEPORT), 1, dsc,
350                                   dscindex))
351                 == NULL) {
352                 struct in_addr s;
353                 s.s_addr = dsrv;
354                 printf
355                     ("Cannot initialize rx connection to dest server (%s)\n",
356                      inet_ntoa(s));
357                 goto Fail_sconn;
358             }
359         }
360     }
361
362
363     memset(&sst, 0, sizeof(struct AFSStoreStatus));
364
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);
370             if (dfd < 0) {
371                 fprintf(stderr, "Cannot open %s (%s)\n", destpath,
372                         afs_error_message(errno));
373                 goto Fail_dconn;
374             }
375         } else if (dfd < 0) {
376             fprintf(stderr, "Cannot open %s (%s)\n", destpath,
377                     afs_error_message(errno));
378             goto Fail_dconn;
379         }
380     } else if (!unlock) {
381         if ((code =
382              RXAFS_CreateFile(dconn, &dd, destf, &sst, &df, &fst, &dfst, &dcb,
383                               &vs))) {
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",
388                             destpath);
389                 else
390                     code = 0;
391             } else {
392                 printf("Cannot create %s (%s)\n", destpath,
393                        afs_error_message(code));
394                 if (code)
395                     goto Fail_dconn;
396             }
397         }
398     }
399
400     if (slcl) {
401         sfd = open(srcf, O_RDONLY, 0);
402         if (sfd < 0) {
403             fprintf(stderr, "Cannot open %s (%s)\n", srcf,
404                     afs_error_message(errno));
405             goto Fail_dconn;
406         }
407         if (fstat(sfd, &statbuf) < 0) {
408             fprintf(stderr, "Cannot stat %s (%s)\n", srcf,
409                     afs_error_message(errno));
410             close(sfd);
411             goto Fail_dconn;
412         }
413     } else {
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));
417             goto Fail_dconn;
418         }
419     }
420
421
422
423     if (slcl) {
424         filesz = statbuf.st_size;
425     } else {
426         filesz = fst.Length;
427     }
428
429     printcallerrs = 0;
430     fetchcode = 0;
431     storecode = 0;
432     if (!slcl && !unlock)
433         scall = rx_NewCall(sconn);
434     if (!dlcl && !unlock)
435         dcall = rx_NewCall(dconn);
436     gettimeofday(&start, &tz);
437     if (unlock) {
438         if (fst.lockCount) {
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));
443             }
444         } else {
445             printf("No locks for %s\n", srcf);
446         }
447         fetchcode = code;
448         goto Finish;
449     }
450
451     if (!slcl) {
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));
455             goto Fail_call;
456         }
457     }
458
459     if (!dlcl) {
460         if (slcl) {
461             sst.Mask = AFS_SETMODTIME | AFS_SETMODE;
462             sst.ClientModTime = statbuf.st_mtime;
463             sst.UnixModeBits =
464                 statbuf.st_mode & ~(S_IFMT | S_ISUID | S_ISGID);
465         } else {
466             sst.Mask = AFS_SETMODTIME | AFS_SETMODE;
467             sst.ClientModTime = fst.ClientModTime;
468             sst.UnixModeBits =
469                 fst.UnixModeBits & ~(S_IFMT | S_ISUID | S_ISGID);
470         }
471
472         if ((code =
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));
476             goto Fail_call;
477         }
478     }
479
480     if (slcl) {
481         bytesremaining = statbuf.st_size;
482     } else {
483         rx_Read(scall, (char *)&bytesremaining, sizeof(afs_int32));
484         bytesremaining = ntohl(bytesremaining);
485     }
486
487     if (sleeptime > 0) {
488 #ifdef AFS_PTHREAD_ENV
489         sleep(sleeptime);
490 #else
491         IOMGR_Sleep(sleeptime);
492 #endif
493     }
494
495     while (bytesremaining > 0) {
496         /*printf("%d bytes remaining\n",bytesremaining); */
497         if (slcl) {
498             if ((bytes =
499                  read(sfd, databuffer, min(blksize, bytesremaining))) <= 0) {
500                 fetchcode = errno;
501                 break;
502             }
503         } else {
504             if ((bytes =
505                  rx_Read(scall, databuffer,
506                          min(blksize, bytesremaining))) <= 0)
507                 break;
508         }
509         if (dlcl) {
510             if (write(dfd, databuffer, bytes) != bytes) {
511                 storecode = errno;
512                 break;
513             }
514         } else {
515             if (rx_Write(dcall, databuffer, bytes) != bytes)
516                 break;
517         }
518         bytesremaining -= bytes;
519         /*printf("%d bytes copied\n",bytes); */
520     }
521
522
523     if (bytesremaining > 0) {
524         printf("Some network error occured while copying data\n");
525         goto Fail_call;
526     }
527
528     if (!slcl)
529         fetchcode = EndRXAFS_FetchData(scall, &fst, &scb, &vs);
530     if (!dlcl)
531         storecode = EndRXAFS_StoreData(dcall, &fst, &vs);
532     printcallerrs = 1;
533   Fail_call:
534
535     if (slcl) {
536         if (close(sfd) && !fetchcode)
537             fetchcode = errno;
538     } else {
539         fetchcode = rx_EndCall(scall, fetchcode);
540     }
541     if (fetchcode)
542         printf("Error returned from fetch: %s\n", afs_error_message(fetchcode));
543
544     if (dlcl) {
545         if (close(dfd) && !storecode)
546             storecode = errno;
547     } else if (!unlock) {
548         storecode = rx_EndCall(dcall, storecode);
549     }
550     if (storecode)
551         printf("Error returned from store: %s\n", afs_error_message(storecode));
552 Finish:
553     gettimeofday(&finish, &tz);
554
555     if (!slcl) {
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));
564     }
565
566     if (!dlcl) {
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));
575     }
576
577     if (code == 0)
578         code = storecode;
579     if (code == 0)
580         code = fetchcode;
581
582   Fail_dconn:
583     if (!dlcl && !unlock && (slcl || dconn != sconn))
584         rx_DestroyConnection(dconn);
585   Fail_sconn:
586     if (!slcl)
587         rx_DestroyConnection(sconn);
588   Fail_sc:
589     if (dsc && dsc != ssc)
590         RXS_Close(dsc);
591     if (ssc)
592         RXS_Close(ssc);
593   Fail_rx:
594     rx_Finalize();
595
596     free(databuffer);
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));
602         } else {
603             printf("Copied %d bytes in %d seconds\n", filesz,
604                    (int)(finish.tv_sec - start.tv_sec));
605         }
606
607         size = filesz / 1024.0;
608         time =
609             finish.tv_sec - start.tv_sec + (finish.tv_usec -
610                                             start.tv_usec) / 1e+06;
611         rate = size / time;
612         printf("Transfer rate %g Kbytes/sec\n", rate);
613
614     }
615
616     exit(code != 0);
617 }