2 * Copyright (c) 2007, Hartmut Reuter,
3 * RZG, Max-Planck-Institut f. Plasmaphysik.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Revised in 2010 by Chaz Chandler to enhance clientless operations.
29 * Now utilizes libafscp by Chaskiel Grundman.
30 * Work funded in part by Sine Nomine Associates (http://www.sinenomine.net/)
33 #include <afsconfig.h>
34 #include <afs/param.h>
42 #include <afs/smb_iocons.h>
44 #include <afs/cm_ioctl.h>
45 #include <afs/pioctl_nt.h>
46 #include <WINNT/syscfg.h>
48 #include <afs/afsint.h>
49 #define FSINT_COMMON_XG 1
55 #include <afs/vlserver.h>
56 #include <afs/ihandle.h>
57 #include <afs/com_err.h>
58 #include <afs/afscp.h>
63 #include <hcrypto/md5.h>
64 #ifdef AFS_PTHREAD_ENV
65 pthread_key_t uclient_key;
68 static int lockFile(struct cmd_syndesc *, void *);
69 static int readFile(struct cmd_syndesc *, void *);
70 static int writeFile(struct cmd_syndesc *, void *);
71 static void printDatarate(void);
72 static void summarizeDatarate(struct timeval *, const char *);
73 static int CmdProlog(struct cmd_syndesc *, char **, char **,
75 static int ScanFid(char *, struct AFSFid *);
76 static afs_int32 GetVenusFidByFid(char *, char *, int, struct afscp_venusfid **);
77 static afs_int32 GetVenusFidByPath(char *, char *, struct afscp_venusfid **);
78 static int BreakUpPath(char *, char *, char *, size_t);
80 static char pnp[AFSPATHMAX]; /* filename of this program when called */
81 static int verbose = 0; /* Set if -verbose option given */
82 static int cellGiven = 0; /* Set if -cell option given */
83 static int force = 0; /* Set if -force option given */
84 static int readlock = 0; /* Set if -readlock option given */
85 static int waittime = 0; /* Set if -waittime option given */
86 static int useFid = 0; /* Set if fidwrite/fidread/fidappend invoked */
87 static int append = 0; /* Set if append/fidappend invoked */
88 static struct timeval starttime, opentime, readtime, writetime;
89 static afs_uint64 xfered = 0;
90 static struct timeval now;
92 static int Timezone; /* Roken gettimeofday ignores the timezone */
94 static struct timezone Timezone;
98 #define WRITEBUFLEN (BUFFLEN * 1024)
99 #define MEGABYTE_F 1048576.0f
102 static int md5sum = 0; /* Set if -md5 option given */
106 afs_uint32 offset; /* offset inside the buffer */
107 afs_uint32 buflen; /* total length == BUFFLEN */
108 afs_uint32 used; /* bytes used inside buffer */
113 * returns difference in seconds between two times
115 * \param[in] from start time
116 * \param[in] to end time
118 * \post returns "to" minus "from" in seconds
122 time_elapsed(struct timeval *from, struct timeval *to)
124 return (float)(to->tv_sec + (to->tv_usec * 0.000001) - from->tv_sec -
125 (from->tv_usec * 0.000001));
129 * prints current average data transfer rate at no less than 30-second intervals
134 static float oldseconds = 0.0;
135 static afs_uint64 oldxfered = 0;
138 gettimeofday(&now, &Timezone);
139 seconds = time_elapsed(&opentime, &now);
140 if ((seconds - oldseconds) > 30) {
141 fprintf(stderr, "%llu MB transferred, present data rate = %.3f MB/sec.\n", xfered >> 20, /* total bytes transferred, in MB */
142 (xfered - oldxfered) / (seconds - oldseconds) / MEGABYTE_F);
144 oldseconds = seconds;
146 } /* printDatarate */
149 * prints overall average data transfer rate and elapsed time
151 * \param[in] tvp current time (to compare with file open time)
152 * \param[in] xfer_type string identify transfer type ("read" or "write")
155 summarizeDatarate(struct timeval *tvp, const char *xfer_type)
157 float seconds = time_elapsed(&opentime, tvp);
159 fprintf(stderr, "Transfer of %llu bytes took %.3f sec.\n",
161 fprintf(stderr, "Total data rate = %.03f MB/sec. for %s\n",
162 xfered / seconds / MEGABYTE_F, xfer_type);
163 } /* summarizeDatarate */
166 * prints final MD5 sum of all file data transferred
168 * \param[in] fname file name or FID
171 summarizeMD5(char *fname)
173 afs_uint32 md5int[4];
176 MD5_Final((char *) &md5int[0], &md5);
177 p = fname + strlen(fname);
184 fprintf(stderr, "%08x%08x%08x%08x %s\n", htonl(md5int[0]),
185 htonl(md5int[1]), htonl(md5int[2]), htonl(md5int[3]), p);
189 * parses all command-line arguments
191 * \param[in] as arguments list
192 * \param[out] cellp cell name
193 * \param[out] realmp realm name
194 * \param[out] fnp filename (either fid or path)
195 * \param[out] slp "synthesized" (made up) data given
197 * \post returns 0 on success or -1 on error
201 CmdProlog(struct cmd_syndesc *as, char **cellp, char **realmp,
202 char **fnp, char **slp)
205 struct cmd_parmdesc *pdp;
208 afs_com_err(pnp, EINVAL, "(syndesc is null)");
212 /* determine which command was requested */
213 if (strncmp(as->name, "fid", 3) == 0) /* fidread/fidwrite/fidappend */
215 if ( (strcmp(as->name, "append") == 0) ||
216 (strcmp(as->name, "fidappend") == 0) )
217 append = 1; /* global */
219 /* attempts to ensure loop is bounded: */
220 for (pdp = as->parms, i = 0; pdp && (i < as->nParms); i++, pdp++) {
221 if (pdp->items != NULL) {
222 if (strcmp(pdp->name, "-verbose") == 0)
224 else if (strcmp(pdp->name, "-md5") == 0)
225 md5sum = 1; /* global */
226 else if (strcmp(pdp->name, "-cell") == 0) {
227 cellGiven = 1; /* global */
228 *cellp = pdp->items->data;
229 } else if ( (strcmp(pdp->name, "-file") == 0) ||
230 (strcmp(pdp->name, "-fid") == 0) ||
231 (strcmp(pdp->name, "-vnode") == 0) )
232 *fnp = pdp->items->data;
233 else if (strcmp(pdp->name, "-force") == 0)
234 force = 1; /* global */
235 else if (strcmp(pdp->name, "-synthesize") == 0)
236 *slp = pdp->items->data;
237 else if (strcmp(pdp->name, "-realm") == 0)
238 *realmp = pdp->items->data;
239 else if (strcmp(pdp->name, "-wait") == 0)
240 waittime = atoi(pdp->items->data);
241 else if (strcmp(pdp->name, "-readlock") == 0)
249 main(int argc, char **argv)
251 struct cmd_syndesc *ts;
252 char baseName[AFSNAMEMAX];
254 /* try to get only the base name of this executable for use in logs */
255 if (BreakUpPath(argv[0], NULL, baseName, AFSNAMEMAX) > 0)
256 strlcpy(pnp, baseName, AFSNAMEMAX);
258 strlcpy(pnp, argv[0], AFSPATHMAX);
260 #ifdef AFS_PTHREAD_ENV
261 opr_Verify(pthread_key_create(&uclient_key, NULL) == 0);
264 ts = cmd_CreateSyntax("lock", lockFile, (void *)LockWrite,
265 "lock a file in AFS");
266 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
267 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
268 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
270 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
271 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
272 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
274 ts = cmd_CreateSyntax("fidlock", lockFile, (void *)LockWrite,
275 "lock by FID a file from AFS");
276 cmd_IsAdministratorCommand(ts);
277 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
278 "volume.vnode.uniquifier");
279 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
280 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
282 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
283 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
284 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
286 ts = cmd_CreateSyntax("unlock", lockFile, (void *)LockRelease,
287 "unlock a file in AFS");
288 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
289 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
290 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
292 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
293 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
295 ts = cmd_CreateSyntax("fidunlock", lockFile, (void *)LockRelease,
296 "unlock by FID a file from AFS");
297 cmd_IsAdministratorCommand(ts);
298 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
299 "volume.vnode.uniquifier");
300 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
301 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
303 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
304 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
306 ts = cmd_CreateSyntax("read", readFile, NULL,
307 "read a file from AFS");
308 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
309 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
310 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
311 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
312 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
314 ts = cmd_CreateSyntax("fidread", readFile, CMD_REQUIRED,
315 "read on a non AFS-client a file from AFS");
316 cmd_IsAdministratorCommand(ts);
317 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
318 "volume.vnode.uniquifier");
319 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
320 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
321 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
322 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
324 ts = cmd_CreateSyntax("write", writeFile, NULL,
325 "write a file into AFS");
326 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
327 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
328 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
329 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
330 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
331 "overwrite existing file");
332 cmd_AddParm(ts, "-synthesize", CMD_SINGLE, CMD_OPTIONAL,
333 "create data pattern of specified length instead reading from stdin");
334 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
336 ts = cmd_CreateSyntax("fidwrite", writeFile, CMD_REQUIRED,
337 "write a file into AFS");
338 cmd_IsAdministratorCommand(ts);
339 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
340 "volume.vnode.uniquifier");
341 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
342 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
343 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
344 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
345 "overwrite existing file");
346 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
348 ts = cmd_CreateSyntax("append", writeFile, NULL,
349 "append to a file in AFS");
350 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
351 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
352 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
353 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
355 ts = cmd_CreateSyntax("fidappend", writeFile, NULL,
356 "append to a file in AFS");
357 cmd_IsAdministratorCommand(ts);
358 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
359 "volume.vnode.uniquifier");
360 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
361 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
362 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
364 if (afscp_Init(NULL) != 0)
367 cmd_Dispatch(argc, argv);
374 * standardized way of parsing a File ID (FID) from command line input
376 * \param[in] fidString dot-delimited FID triple
377 * \param[out] fid pointer to the AFSFid to fill in
379 * \post The FID pointed to by "fid" is filled in which the parsed Volume,
380 * Vnode, and Uniquifier data. The string should be in the format
381 * of three numbers separated by dot (.) delimiters, representing
382 * (in order) the volume id, vnode number, and uniquifier.
383 * Example: "576821346.1.1"
386 ScanFid(char *fidString, struct AFSFid *fid)
389 long unsigned int f1, f2, f3;
392 i = sscanf(fidString, "%lu.%lu.%lu", &f1, &f2, &f3);
393 fid->Volume = (afs_uint32) f1;
394 fid->Vnode = (afs_uint32) f2;
395 fid->Unique = (afs_uint32) f3;
402 afs_com_err(pnp, code, "(invalid FID triple: %s)", fidString);
409 * look up cell info and verify FID info from user input
411 * \param[in] fidString string containing FID info
412 * \param[in] cellName cell name string
413 * \param[in] onlyRW bool: 1 = RW vol only, 0 = any vol type
414 * \param[out] avfpp pointer to venusfid info
416 * \post *avfpp will contain the VenusFid info found for the FID
417 * given by the used in the string fidString and zero is
418 * returned. If not found, an appropriate afs error code
419 * is returned and *avfpp will be NULL.
421 * \note Any non-NULL avfpp returned should later be freed with
422 * afscp_FreeFid() when no longer needed.
425 GetVenusFidByFid(char *fidString, char *cellName, int onlyRW,
426 struct afscp_venusfid **avfpp)
430 struct afscp_volume *avolp;
432 if (*avfpp == NULL) {
433 *avfpp = calloc(1, sizeof(struct afscp_venusfid));
434 if ( *avfpp == NULL ) {
440 if (cellName == NULL) {
441 (*avfpp)->cell = afscp_DefaultCell();
443 (*avfpp)->cell = afscp_CellByName(cellName, NULL);
445 if ((*avfpp)->cell == NULL) {
446 if (afscp_errno == 0)
453 code = ScanFid(fidString, &((*avfpp)->fid));
459 avolp = afscp_VolumeById((*avfpp)->cell, (*avfpp)->fid.Volume);
461 if (afscp_errno == 0)
465 afs_com_err(pnp, code, "(finding volume %lu)",
466 afs_printable_uint32_lu((*avfpp)->fid.Volume));
470 if ( onlyRW && (avolp->voltype != RWVOL) ) {
471 avolp = afscp_VolumeByName((*avfpp)->cell, avolp->name, RWVOL);
473 if (afscp_errno == 0)
477 afs_com_err(pnp, code, "(finding volume %lu)",
478 afs_printable_uint32_lu((*avfpp)->fid.Volume));
481 (*avfpp)->fid.Volume = avolp->id; /* is this safe? */
484 code = afscp_Stat((*avfpp), &sbuf);
486 afs_com_err(pnp, code, "(stat failed with code %d)", code);
490 } /* GetVenusFidByFid */
493 * Split a full path up into dirName and baseName components
495 * \param[in] fullPath can be absolute, relative, or local
496 * \param[out] dirName pointer to allocated char buffer or NULL
497 * \param[out] baseName pointer to allocated char buffer or NULL
499 * \post To the fulleset extent possible, the rightmost full path
500 * component will be copied into baseName and all other
501 * components into dirName (minus the trailing path separator).
502 * If either dirName or baseName are NULL, only the non-NULL
503 * pointer will be filled in (but both can't be null or it would
504 * be pointless) -- so the caller can retrieve, say, only baseName
505 * if desired. The return code is the number of strings copied:
506 * 0 if neither dirName nor baseName could be filled in
507 * 1 if either dirName or baseName were filled in
508 * 2 if both dirName and baseName were filled in
511 BreakUpPath(char *fullPath, char *dirName, char *baseName, size_t baseNameSize)
514 size_t dirNameLen = 0;
515 int code = 0, useDirName = 1, useBaseName = 1;
517 if (fullPath == NULL) {
523 if (baseName == NULL)
525 if (!useBaseName && !useDirName) {
526 /* would be pointless to continue -- must be error in call */
530 lastSlash = strrchr(fullPath, '\\');
532 lastSlash = strrchr(fullPath, '/');
534 if (lastSlash != NULL) {
535 /* then lastSlash points to the last path separator in fullPath */
537 dirNameLen = strlen(fullPath) - strlen(lastSlash);
538 strlcpy(dirName, fullPath, min(dirNameLen + 1, baseNameSize));
543 strlcpy(baseName, lastSlash, min(strlen(lastSlash) + 1, baseNameSize));
547 /* there are no path separators in fullPath -- it's just a baseName */
549 strlcpy(baseName, fullPath, min(strlen(fullPath) + 1, baseNameSize));
557 * Get the VenusFid info available for the file at AFS path 'fullPath'.
558 * Works without pioctls/afsd by using libafscp. Analogous to
559 * get_file_cell() in the previous iteration of afsio.
561 * \param[in] fullPath the file name
562 * \param[in] cellName the cell name to look up
563 * \param[out] avfpp pointer to Venus FID info to be filled in
565 * \post If the path resolves successfully (using afscp_ResolvePath),
566 * then vfpp will contain the Venus FID info (cell info plus
567 * AFSFid) of the last path segment in fullPath.
570 GetVenusFidByPath(char *fullPath, char *cellName,
571 struct afscp_venusfid **avfpp)
575 if (fullPath == NULL) {
579 if (cellName != NULL) {
580 code = (afs_int32) afscp_SetDefaultCell(cellName);
586 *avfpp = afscp_ResolvePath(fullPath);
587 if (*avfpp == NULL) {
588 if (afscp_errno == 0)
595 } /* GetVenusFidByPath */
598 lockFile(struct cmd_syndesc *as, void *arock)
604 struct AFSFetchStatus OutStatus;
605 struct afscp_venusfid *avfp = NULL;
608 int locktype = (int)(intptr_t) arock;
611 /* stdout on Windows defaults to _O_TEXT mode */
612 _setmode(1, _O_BINARY);
615 gettimeofday(&starttime, &Timezone);
617 CmdProlog(as, &cell, &realm, &fname, NULL);
618 afscp_AnonymousAuth(1);
620 if ((locktype == LockWrite) && readlock)
624 code = afscp_SetDefaultRealm(realm);
627 code = afscp_SetDefaultCell(cell);
630 code = GetVenusFidByFid(fname, cell, 0, &avfp);
632 code = GetVenusFidByPath(fname, cell, &avfp);
634 afs_com_err(pnp, code, "(file not found: %s)", fname);
639 code = afscp_GetStatus(avfp, &OutStatus);
641 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
642 afs_com_err(pnp, code, "(failed to get status of file %s from"
643 "server %s, code = %d)", fname, ipv4_addr, code);
648 if (locktype != LockRelease) {
649 while (OutStatus.lockCount != 0) {
650 code = afscp_WaitForCallback(avfp, waittime);
651 if ((code == -1) && (afscp_errno == ETIMEDOUT))
653 if ((code = afscp_GetStatus(avfp, &OutStatus)) != 0)
657 if (OutStatus.lockCount == 0) {
663 code = afscp_Lock(avfp, locktype);
664 if ((code == -1) && (afscp_errno == EWOULDBLOCK))
673 afs_com_err(pnp, code, "(failed to change lock status: %d)", afscp_errno);
679 readFile(struct cmd_syndesc *as, void *unused)
685 struct AFSFetchStatus OutStatus;
686 struct afscp_venusfid *avfp = NULL;
689 afs_int64 length = 0, Len;
694 int bufflen = BUFFLEN;
697 /* stdout on Windows defaults to _O_TEXT mode */
698 _setmode(1, _O_BINARY);
701 gettimeofday(&starttime, &Timezone);
703 CmdProlog(as, &cell, &realm, &fname, NULL);
704 afscp_AnonymousAuth(1);
710 code = afscp_SetDefaultRealm(realm);
713 code = afscp_SetDefaultCell(cell);
716 code = GetVenusFidByFid(fname, cell, 0, &avfp);
718 code = GetVenusFidByPath(fname, cell, &avfp);
720 afs_com_err(pnp, code, "(file not found: %s)", fname);
724 if (avfp->fid.Vnode & 1) {
726 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
731 code = afscp_GetStatus(avfp, &OutStatus);
733 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
734 afs_com_err(pnp, code, "(failed to get status of file %s from"
735 "server %s, code = %d)", fname, ipv4_addr, code);
740 gettimeofday(&opentime, &Timezone);
742 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
743 time_elapsed(&starttime, &opentime));
744 Len = OutStatus.Length_hi;
746 Len += OutStatus.Length;
748 buf = calloc(bufflen, sizeof(char));
751 afs_com_err(pnp, code, "(cannot allocate buffer)");
756 while (!code && NonZeroInt64(length)) {
757 if (length > bufflen)
760 len = (afs_int32) length;
761 bytes = afscp_PRead(avfp, buf, len, Pos);
763 code = -3; /* what error name should we use here? */
765 MD5_Update(&md5, buf, len);
767 len = write(1, buf, len); /* to stdout */
780 gettimeofday(&readtime, &Timezone);
784 summarizeDatarate(&readtime, "read");
792 writeFile(struct cmd_syndesc *as, void *unused)
796 char *sSynthLen = NULL;
799 afs_int32 byteswritten;
800 struct AFSFetchStatus OutStatus;
801 struct AFSStoreStatus InStatus;
802 struct afscp_venusfid *dirvfp = NULL, *newvfp = NULL;
804 afs_int64 length, Len, synthlength = 0, offset = 0;
809 struct wbuf *bufchain = 0;
810 struct wbuf *previous, *tbuf;
811 char dirName[AFSPATHMAX];
812 char baseName[AFSNAMEMAX];
816 /* stdin on Windows defaults to _O_TEXT mode */
817 _setmode(0, _O_BINARY);
820 CmdProlog(as, &cell, &realm, &fname, &sSynthLen);
821 afscp_AnonymousAuth(1);
824 code = afscp_SetDefaultRealm(realm);
827 code = afscp_SetDefaultCell(cell);
830 code = util_GetInt64(sSynthLen, &synthlength);
832 afs_com_err(pnp, code, "(invalid value for synthesize length %s)",
840 code = GetVenusFidByFid(fname, cell, 1, &newvfp);
842 afs_com_err(pnp, code, "(GetVenusFidByFid returned code %d)", code);
846 code = GetVenusFidByPath(fname, cell, &newvfp);
847 if (code == 0) { /* file was found */
852 * file cannot already exist if specified by path and not
853 * appending to it unless user forces overwrite
856 afscp_FreeFid(newvfp);
857 afs_com_err(pnp, code, "(use -force to overwrite)");
860 } else { /* file not found */
863 afs_com_err(pnp, code, "(cannot append to non-existent file)");
867 if (!append && !overWrite) { /* must create a new file in this case */
868 if ( BreakUpPath(fname, dirName, baseName, AFSNAMEMAX) != 2 ) {
870 afs_com_err(pnp, code, "(must provide full AFS path)");
871 afscp_FreeFid(newvfp);
875 code = GetVenusFidByPath(dirName, cell, &dirvfp);
876 afscp_FreeFid(newvfp); /* release now-unneeded fid */
879 afs_com_err(pnp, code, "(is dir %s in AFS?)", dirName);
885 if ( (newvfp != NULL) && (newvfp->fid.Vnode & 1) ) {
887 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
888 afscp_FreeFid(newvfp);
889 afscp_FreeFid(dirvfp);
892 gettimeofday(&starttime, &Timezone);
894 InStatus.UnixModeBits = 0644;
895 InStatus.Mask = AFS_SETMODE + AFS_FSYNC;
896 if (newvfp == NULL) {
897 code = afscp_CreateFile(dirvfp, baseName, &InStatus, &newvfp);
899 afs_com_err(pnp, code,
900 "(could not create file %s in directory %lu.%lu.%lu)",
901 baseName, afs_printable_uint32_lu(dirvfp->fid.Volume),
902 afs_printable_uint32_lu(dirvfp->fid.Vnode),
903 afs_printable_uint32_lu(dirvfp->fid.Unique));
907 code = afscp_GetStatus(newvfp, &OutStatus);
909 afs_inet_ntoa_r(newvfp->cell->fsservers[0]->addrs[0], ipv4_addr);
910 afs_com_err(pnp, code, "(failed to get status of file %s from"
911 "server %s, code = %d)", fname, ipv4_addr, code);
912 afscp_FreeFid(newvfp);
913 afscp_FreeFid(dirvfp);
917 if ( !append && !force &&
918 (OutStatus.Length != 0 || OutStatus.Length_hi !=0 ) ) {
920 * file exists, is of non-zero length, and we're not appending
921 * to it: user must force overwrite
922 * (covers fidwrite edge case)
925 afscp_FreeFid(newvfp);
926 afscp_FreeFid(dirvfp);
927 afs_com_err(pnp, code, "(use -force to overwrite)");
932 Pos = OutStatus.Length_hi;
933 Pos = (Pos << 32) | OutStatus.Length;
936 previous = (struct wbuf *)&bufchain;
941 * currently, these two while loops (1) read the whole source file in
942 * before (2) writing any of it out, meaning that afsio can't deal with
943 * files larger than the maximum amount of memory designated for
944 * reading a file in (WRITEBUFLEN).
945 * Consider going to a single loop, like in readFile(), though will
946 * have implications on timing statistics (such as the "Startup to
947 * find the file" time, below).
950 while (Len < WRITEBUFLEN) {
951 tbuf = calloc(1, sizeof(struct wbuf));
955 afscp_FreeFid(newvfp);
956 afscp_FreeFid(dirvfp);
957 afs_com_err(pnp, code, "(cannot allocate buffer)");
962 tbuf->buflen = BUFFLEN;
964 afs_int64 ll, l = tbuf->buflen;
967 for (ll = 0; ll < l; ll += 4096) {
968 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
969 (unsigned int)((offset + ll) >> 32),
970 (unsigned int)((offset + ll) & 0xffffffff));
974 tbuf->used = (afs_int32) l;
976 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
977 if (tbuf->used == 0) {
982 MD5_Update(&md5, &tbuf->buf, tbuf->used);
983 previous->next = tbuf;
987 gettimeofday(&opentime, &Timezone);
989 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
990 time_elapsed(&starttime, &opentime));
992 while (!code && bytes) {
997 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1000 byteswritten = afscp_PWrite(newvfp, tbuf->buf,
1001 tbuf->used, Pos + xfered);
1002 if (byteswritten != tbuf->used) {
1003 fprintf(stderr,"Only %d instead of %" AFS_INT64_FMT " bytes transferred by rx_Write()\n", byteswritten, length);
1004 fprintf(stderr, "At %" AFS_UINT64_FMT " bytes from the end\n", length);
1008 xfered += tbuf->used;
1011 length -= tbuf->used;
1017 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1020 afs_int64 ll, l = tbuf->buflen;
1021 if (l > synthlength)
1023 for (ll = 0; ll < l; ll += 4096) {
1024 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
1025 (unsigned int)((offset + ll) >> 32),
1026 (unsigned int)((offset + ll) & 0xffffffff));
1030 tbuf->used = (afs_int32) l;
1032 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
1036 MD5_Update(&md5, &tbuf->buf, tbuf->used);
1038 bytes += tbuf->used;
1042 afscp_FreeFid(newvfp);
1043 afscp_FreeFid(dirvfp);
1045 gettimeofday(&writetime, &Timezone);
1047 afs_com_err(pnp, code, "(%s failed with code %d)", as->name,
1049 } else if (verbose) {
1050 summarizeDatarate(&writetime, "write");
1054 bufchain = tbuf->next;
1059 summarizeMD5(fname);