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>
44 #include <afs/smb_iocons.h>
46 #include <afs/cm_ioctl.h>
47 #include <afs/pioctl_nt.h>
48 #include <WINNT/syscfg.h>
51 #include <afs/afsint.h>
52 #define FSINT_COMMON_XG 1
57 #include <afs/vlserver.h>
58 #include <afs/ihandle.h>
59 #include <afs/com_err.h>
60 #include <afs/afscp.h>
68 #include <sys/malloc.h>
72 #include <hcrypto/md5.h>
73 #ifdef AFS_PTHREAD_ENV
75 pthread_key_t uclient_key;
78 static int lockFile(struct cmd_syndesc *, void *);
79 static int readFile(struct cmd_syndesc *, void *);
80 static int writeFile(struct cmd_syndesc *, void *);
81 static void printDatarate(void);
82 static void summarizeDatarate(struct timeval *, const char *);
83 static int CmdProlog(struct cmd_syndesc *, char **, char **,
85 static int ScanFid(char *, struct AFSFid *);
86 static afs_int32 GetVenusFidByFid(char *, char *, int, struct afscp_venusfid **);
87 static afs_int32 GetVenusFidByPath(char *, char *, struct afscp_venusfid **);
88 static int BreakUpPath(char *, char *, char *);
90 static char pnp[AFSPATHMAX]; /* filename of this program when called */
91 static int verbose = 0; /* Set if -verbose option given */
92 static int cellGiven = 0; /* Set if -cell option given */
93 static int force = 0; /* Set if -force option given */
94 static int readlock = 0; /* Set if -readlock option given */
95 static int waittime = 0; /* Set if -waittime option given */
96 static int useFid = 0; /* Set if fidwrite/fidread/fidappend invoked */
97 static int append = 0; /* Set if append/fidappend invoked */
98 static struct timeval starttime, opentime, readtime, writetime;
99 static afs_uint64 xfered = 0;
100 static struct timeval now;
102 static int Timezone; /* Roken gettimeofday ignores the timezone */
104 static struct timezone Timezone;
107 #define BUFFLEN 65536
108 #define WRITEBUFLEN (BUFFLEN * 1024)
109 #define MEGABYTE_F 1048576.0f
112 static int md5sum = 0; /* Set if -md5 option given */
116 afs_uint32 offset; /* offset inside the buffer */
117 afs_uint32 buflen; /* total length == BUFFLEN */
118 afs_uint32 used; /* bytes used inside buffer */
123 * returns difference in seconds between two times
125 * \param[in] from start time
126 * \param[in] to end time
128 * \post returns "to" minus "from" in seconds
132 time_elapsed(struct timeval *from, struct timeval *to)
134 return (float)(to->tv_sec + (to->tv_usec * 0.000001) - from->tv_sec -
135 (from->tv_usec * 0.000001));
139 * prints current average data transfer rate at no less than 30-second intervals
144 static float oldseconds = 0.0;
145 static afs_uint64 oldxfered = 0;
148 gettimeofday(&now, &Timezone);
149 seconds = time_elapsed(&opentime, &now);
150 if ((seconds - oldseconds) > 30) {
151 fprintf(stderr, "%llu MB transferred, present data rate = %.3f MB/sec.\n", xfered >> 20, /* total bytes transferred, in MB */
152 (xfered - oldxfered) / (seconds - oldseconds) / MEGABYTE_F);
154 oldseconds = seconds;
156 } /* printDatarate */
159 * prints overall average data transfer rate and elapsed time
161 * \param[in] tvp current time (to compare with file open time)
162 * \param[in] xfer_type string identify transfer type ("read" or "write")
165 summarizeDatarate(struct timeval *tvp, const char *xfer_type)
167 float seconds = time_elapsed(&opentime, tvp);
169 fprintf(stderr, "Transfer of %llu bytes took %.3f sec.\n",
171 fprintf(stderr, "Total data rate = %.03f MB/sec. for %s\n",
172 xfered / seconds / MEGABYTE_F, xfer_type);
173 } /* summarizeDatarate */
176 * prints final MD5 sum of all file data transferred
178 * \param[in] fname file name or FID
181 summarizeMD5(char *fname)
183 afs_uint32 md5int[4];
186 MD5_Final((char *) &md5int[0], &md5);
187 p = fname + strlen(fname);
194 fprintf(stderr, "%08x%08x%08x%08x %s\n", htonl(md5int[0]),
195 htonl(md5int[1]), htonl(md5int[2]), htonl(md5int[3]), p);
199 * parses all command-line arguments
201 * \param[in] as arguments list
202 * \param[out] cellp cell name
203 * \param[out] realmp realm name
204 * \param[out] fnp filename (either fid or path)
205 * \param[out] slp "synthesized" (made up) data given
207 * \post returns 0 on success or -1 on error
211 CmdProlog(struct cmd_syndesc *as, char **cellp, char **realmp,
212 char **fnp, char **slp)
215 struct cmd_parmdesc *pdp;
218 afs_com_err(pnp, EINVAL, "(syndesc is null)");
222 /* determine which command was requested */
223 if (strncmp(as->name, "fid", 3) == 0) /* fidread/fidwrite/fidappend */
225 if ( (strcmp(as->name, "append") == 0) ||
226 (strcmp(as->name, "fidappend") == 0) )
227 append = 1; /* global */
229 /* attempts to ensure loop is bounded: */
230 for (pdp = as->parms, i = 0; pdp && (i < as->nParms); i++, pdp++) {
231 if (pdp->items != NULL) {
232 if (strcmp(pdp->name, "-verbose") == 0)
234 else if (strcmp(pdp->name, "-md5") == 0)
235 md5sum = 1; /* global */
236 else if (strcmp(pdp->name, "-cell") == 0) {
237 cellGiven = 1; /* global */
238 *cellp = pdp->items->data;
239 } else if ( (strcmp(pdp->name, "-file") == 0) ||
240 (strcmp(pdp->name, "-fid") == 0) ||
241 (strcmp(pdp->name, "-vnode") == 0) )
242 *fnp = pdp->items->data;
243 else if (strcmp(pdp->name, "-force") == 0)
244 force = 1; /* global */
245 else if (strcmp(pdp->name, "-synthesize") == 0)
246 *slp = pdp->items->data;
247 else if (strcmp(pdp->name, "-realm") == 0)
248 *realmp = pdp->items->data;
249 else if (strcmp(pdp->name, "-wait") == 0)
250 waittime = atoi(pdp->items->data);
251 else if (strcmp(pdp->name, "-readlock") == 0)
259 main(int argc, char **argv)
262 struct cmd_syndesc *ts;
263 char baseName[AFSNAMEMAX];
265 /* try to get only the base name of this executable for use in logs */
266 if (BreakUpPath(argv[0], NULL, baseName) > 0)
267 strlcpy(pnp, baseName, AFSNAMEMAX);
269 strlcpy(pnp, argv[0], AFSPATHMAX);
271 #ifdef AFS_PTHREAD_ENV
272 assert(pthread_key_create(&uclient_key, NULL) == 0);
275 ts = cmd_CreateSyntax("lock", lockFile, (void *)LockWrite,
276 "lock a file in AFS");
277 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
278 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
279 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
281 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
282 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
283 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
285 ts = cmd_CreateSyntax("fidlock", lockFile, (void *)LockWrite,
286 "lock by FID a file from AFS");
287 cmd_IsAdministratorCommand(ts);
288 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
289 "volume.vnode.uniquifier");
290 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
291 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
293 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
294 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
295 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
297 ts = cmd_CreateSyntax("unlock", lockFile, (void *)LockRelease,
298 "unlock a file in AFS");
299 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
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("fidunlock", lockFile, (void *)LockRelease,
307 "unlock by FID a file from AFS");
308 cmd_IsAdministratorCommand(ts);
309 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
310 "volume.vnode.uniquifier");
311 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
312 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
314 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
315 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
317 ts = cmd_CreateSyntax("read", readFile, NULL,
318 "read a file from AFS");
319 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
320 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
321 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
322 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
323 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
325 ts = cmd_CreateSyntax("fidread", readFile, CMD_REQUIRED,
326 "read on a non AFS-client a file from AFS");
327 cmd_IsAdministratorCommand(ts);
328 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
329 "volume.vnode.uniquifier");
330 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
331 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
332 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
333 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
335 ts = cmd_CreateSyntax("write", writeFile, NULL,
336 "write a file into AFS");
337 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
338 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
339 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
340 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
341 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
342 "overwrite existing file");
343 cmd_AddParm(ts, "-synthesize", CMD_SINGLE, CMD_OPTIONAL,
344 "create data pattern of specified length instead reading from stdin");
345 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
347 ts = cmd_CreateSyntax("fidwrite", writeFile, CMD_REQUIRED,
348 "write a file into AFS");
349 cmd_IsAdministratorCommand(ts);
350 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
351 "volume.vnode.uniquifier");
352 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
353 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
354 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
355 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
356 "overwrite existing file");
357 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
359 ts = cmd_CreateSyntax("append", writeFile, NULL,
360 "append to a file in AFS");
361 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
362 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
363 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
364 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
366 ts = cmd_CreateSyntax("fidappend", writeFile, NULL,
367 "append to a file in AFS");
368 cmd_IsAdministratorCommand(ts);
369 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
370 "volume.vnode.uniquifier");
371 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
372 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
373 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
375 if (afscp_Init(NULL) != 0)
378 code = cmd_Dispatch(argc, argv);
385 * standardized way of parsing a File ID (FID) from command line input
387 * \param[in] fidString dot-delimited FID triple
388 * \param[out] fid pointer to the AFSFid to fill in
390 * \post The FID pointed to by "fid" is filled in which the parsed Volume,
391 * Vnode, and Uniquifier data. The string should be in the format
392 * of three numbers separated by dot (.) delimiters, representing
393 * (in order) the volume id, vnode number, and uniquifier.
394 * Example: "576821346.1.1"
397 ScanFid(char *fidString, struct AFSFid *fid)
400 long unsigned int f1, f2, f3;
403 i = sscanf(fidString, "%lu.%lu.%lu", &f1, &f2, &f3);
404 fid->Volume = (afs_uint32) f1;
405 fid->Vnode = (afs_uint32) f2;
406 fid->Unique = (afs_uint32) f3;
413 afs_com_err(pnp, code, "(invalid FID triple: %s)", fidString);
420 * look up cell info and verify FID info from user input
422 * \param[in] fidString string containing FID info
423 * \param[in] cellName cell name string
424 * \param[in] onlyRW bool: 1 = RW vol only, 0 = any vol type
425 * \param[out] avfpp pointer to venusfid info
427 * \post *avfpp will contain the VenusFid info found for the FID
428 * given by the used in the string fidString and zero is
429 * returned. If not found, an appropriate afs error code
430 * is returned and *avfpp will be NULL.
432 * \note Any non-NULL avfpp returned should later be freed with
433 * afscp_FreeFid() when no longer needed.
436 GetVenusFidByFid(char *fidString, char *cellName, int onlyRW,
437 struct afscp_venusfid **avfpp)
441 struct afscp_volume *avolp;
443 if (*avfpp == NULL) {
444 *avfpp = malloc(sizeof(struct afscp_venusfid));
445 if ( *avfpp == NULL ) {
450 memset(*avfpp, 0, sizeof(struct afscp_venusfid));
452 if (cellName == NULL) {
453 (*avfpp)->cell = afscp_DefaultCell();
455 (*avfpp)->cell = afscp_CellByName(cellName, NULL);
457 if ((*avfpp)->cell == NULL) {
458 if (afscp_errno == 0)
465 code = ScanFid(fidString, &((*avfpp)->fid));
471 avolp = afscp_VolumeById((*avfpp)->cell, (*avfpp)->fid.Volume);
473 if (afscp_errno == 0)
477 afs_com_err(pnp, code, "(finding volume %lu)",
478 afs_printable_uint32_lu((*avfpp)->fid.Volume));
482 if ( onlyRW && (avolp->voltype != RWVOL) ) {
483 avolp = afscp_VolumeByName((*avfpp)->cell, avolp->name, RWVOL);
485 if (afscp_errno == 0)
489 afs_com_err(pnp, code, "(finding volume %lu)",
490 afs_printable_uint32_lu((*avfpp)->fid.Volume));
493 (*avfpp)->fid.Volume = avolp->id; /* is this safe? */
496 code = afscp_Stat((*avfpp), &sbuf);
498 afs_com_err(pnp, code, "(stat failed with code %d)", code);
502 } /* GetVenusFidByFid */
505 * Split a full path up into dirName and baseName components
507 * \param[in] fullPath can be absolute, relative, or local
508 * \param[out] dirName pointer to allocated char buffer or NULL
509 * \param[out] baseName pointer to allocated char buffer or NULL
511 * \post To the fulleset extent possible, the rightmost full path
512 * component will be copied into baseName and all other
513 * components into dirName (minus the trailing path separator).
514 * If either dirName or baseName are NULL, only the non-NULL
515 * pointer will be filled in (but both can't be null or it would
516 * be pointless) -- so the caller can retrieve, say, only baseName
517 * if desired. The return code is the number of strings copied:
518 * 0 if neither dirName nor baseName could be filled in
519 * 1 if either dirName or baseName were filled in
520 * 2 if both dirName and baseName were filled in
523 BreakUpPath(char *fullPath, char *dirName, char *baseName)
526 size_t dirNameLen = 0;
527 int code = 0, useDirName = 1, useBaseName = 1;
529 if (fullPath == NULL) {
535 if (baseName == NULL)
537 if (!useBaseName && !useDirName) {
538 /* would be pointless to continue -- must be error in call */
542 lastSlash = strrchr(fullPath, '\\');
544 lastSlash = strrchr(fullPath, '/');
546 if (lastSlash != NULL) {
547 /* then lastSlash points to the last path separator in fullPath */
549 dirNameLen = strlen(fullPath) - strlen(lastSlash);
550 strlcpy(dirName, fullPath, dirNameLen + 1);
555 strlcpy(baseName, lastSlash, strlen(lastSlash) + 1);
559 /* there are no path separators in fullPath -- it's just a baseName */
561 strlcpy(baseName, fullPath, strlen(fullPath) + 1);
569 * Get the VenusFid info available for the file at AFS path 'fullPath'.
570 * Works without pioctls/afsd by using libafscp. Analogous to
571 * get_file_cell() in the previous iteration of afsio.
573 * \param[in] fullPath the file name
574 * \param[in] cellName the cell name to look up
575 * \param[out] avfpp pointer to Venus FID info to be filled in
577 * \post If the path resolves successfully (using afscp_ResolvePath),
578 * then vfpp will contain the Venus FID info (cell info plus
579 * AFSFid) of the last path segment in fullPath.
582 GetVenusFidByPath(char *fullPath, char *cellName,
583 struct afscp_venusfid **avfpp)
587 if (fullPath == NULL) {
591 if (cellName != NULL) {
592 code = (afs_int32) afscp_SetDefaultCell(cellName);
598 *avfpp = afscp_ResolvePath(fullPath);
599 if (*avfpp == NULL) {
600 if (afscp_errno == 0)
607 } /* GetVenusFidByPath */
610 lockFile(struct cmd_syndesc *as, void *arock)
616 struct AFSFetchStatus OutStatus;
617 struct afscp_venusfid *avfp = NULL;
620 int locktype = (int)(intptr_t) arock;
623 /* stdout on Windows defaults to _O_TEXT mode */
624 _setmode(1, _O_BINARY);
627 gettimeofday(&starttime, &Timezone);
629 CmdProlog(as, &cell, &realm, &fname, NULL);
630 afscp_AnonymousAuth(1);
632 if ((locktype == LockWrite) && readlock)
636 code = afscp_SetDefaultRealm(realm);
639 code = afscp_SetDefaultCell(cell);
642 code = GetVenusFidByFid(fname, cell, 0, &avfp);
644 code = GetVenusFidByPath(fname, cell, &avfp);
646 afs_com_err(pnp, code, "(file not found: %s)", fname);
651 code = afscp_GetStatus(avfp, &OutStatus);
653 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
654 afs_com_err(pnp, code, "(failed to get status of file %s from"
655 "server %s, code = %d)", fname, ipv4_addr, code);
660 if (locktype != LockRelease) {
661 while (OutStatus.lockCount != 0) {
662 code = afscp_WaitForCallback(avfp, waittime);
663 if ((code == -1) && (afscp_errno == ETIMEDOUT))
665 if ((code = afscp_GetStatus(avfp, &OutStatus)) != 0)
669 if (OutStatus.lockCount == 0) {
675 code = afscp_Lock(avfp, locktype);
676 if ((code == -1) && (afscp_errno == EWOULDBLOCK))
685 afs_com_err(pnp, code, "(failed to change lock status: %d)", afscp_errno);
691 readFile(struct cmd_syndesc *as, void *unused)
697 struct AFSFetchStatus OutStatus;
698 struct afscp_venusfid *avfp = NULL;
701 afs_int64 length = 0, Len;
706 int bufflen = BUFFLEN;
709 /* stdout on Windows defaults to _O_TEXT mode */
710 _setmode(1, _O_BINARY);
713 gettimeofday(&starttime, &Timezone);
715 CmdProlog(as, &cell, &realm, &fname, NULL);
716 afscp_AnonymousAuth(1);
722 code = afscp_SetDefaultRealm(realm);
725 code = afscp_SetDefaultCell(cell);
728 code = GetVenusFidByFid(fname, cell, 0, &avfp);
730 code = GetVenusFidByPath(fname, cell, &avfp);
732 afs_com_err(pnp, code, "(file not found: %s)", fname);
736 if (avfp->fid.Vnode & 1) {
738 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
743 code = afscp_GetStatus(avfp, &OutStatus);
745 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
746 afs_com_err(pnp, code, "(failed to get status of file %s from"
747 "server %s, code = %d)", fname, ipv4_addr, code);
752 gettimeofday(&opentime, &Timezone);
754 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
755 time_elapsed(&starttime, &opentime));
756 Len = OutStatus.Length_hi;
758 Len += OutStatus.Length;
760 buf = (char *) malloc(bufflen * sizeof(char));
763 afs_com_err(pnp, code, "(cannot allocate buffer)");
767 memset(buf, 0, bufflen * sizeof(char));
769 while (!code && NonZeroInt64(length)) {
770 if (length > bufflen)
773 len = (afs_int32) length;
774 bytes = afscp_PRead(avfp, buf, len, Pos);
776 code = -3; /* what error name should we use here? */
778 MD5_Update(&md5, buf, len);
780 len = write(1, buf, len); /* to stdout */
793 gettimeofday(&readtime, &Timezone);
797 summarizeDatarate(&readtime, "read");
805 writeFile(struct cmd_syndesc *as, void *unused)
809 char *sSynthLen = NULL;
812 afs_int32 byteswritten;
813 struct AFSFetchStatus OutStatus;
814 struct AFSStoreStatus InStatus;
815 struct afscp_venusfid *dirvfp = NULL, *newvfp = NULL;
817 afs_int64 length, Len, synthlength = 0, offset = 0;
822 struct wbuf *bufchain = 0;
823 struct wbuf *previous, *tbuf;
824 char dirName[AFSPATHMAX];
825 char baseName[AFSNAMEMAX];
829 /* stdin on Windows defaults to _O_TEXT mode */
830 _setmode(0, _O_BINARY);
833 CmdProlog(as, &cell, &realm, &fname, &sSynthLen);
834 afscp_AnonymousAuth(1);
837 code = afscp_SetDefaultRealm(realm);
840 code = afscp_SetDefaultCell(cell);
843 code = util_GetInt64(sSynthLen, &synthlength);
845 afs_com_err(pnp, code, "(invalid value for synthesize length %s)",
853 code = GetVenusFidByFid(fname, cell, 1, &newvfp);
855 afs_com_err(pnp, code, "(GetVenusFidByFid returned code %d)", code);
859 code = GetVenusFidByPath(fname, cell, &newvfp);
860 if (code == 0) { /* file was found */
865 * file cannot already exist if specified by path and not
866 * appending to it unless user forces overwrite
869 afscp_FreeFid(newvfp);
870 afs_com_err(pnp, code, "(use -force to overwrite)");
873 } else { /* file not found */
876 afs_com_err(pnp, code, "(cannot append to non-existent file)");
880 if (!append && !overWrite) { /* must create a new file in this case */
881 if ( BreakUpPath(fname, dirName, baseName) != 2 ) {
883 afs_com_err(pnp, code, "(must provide full AFS path)");
884 afscp_FreeFid(newvfp);
888 code = GetVenusFidByPath(dirName, cell, &dirvfp);
889 afscp_FreeFid(newvfp); /* release now-unneeded fid */
892 afs_com_err(pnp, code, "(is dir %s in AFS?)", dirName);
898 if ( (newvfp != NULL) && (newvfp->fid.Vnode & 1) ) {
900 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
901 afscp_FreeFid(newvfp);
902 afscp_FreeFid(dirvfp);
905 gettimeofday(&starttime, &Timezone);
907 InStatus.UnixModeBits = 0644;
908 InStatus.Mask = AFS_SETMODE + AFS_FSYNC;
909 if (newvfp == NULL) {
910 code = afscp_CreateFile(dirvfp, baseName, &InStatus, &newvfp);
912 afs_com_err(pnp, code,
913 "(could not create file %s in directory %lu.%lu.%lu)",
914 baseName, afs_printable_uint32_lu(dirvfp->fid.Volume),
915 afs_printable_uint32_lu(dirvfp->fid.Vnode),
916 afs_printable_uint32_lu(dirvfp->fid.Unique));
920 code = afscp_GetStatus(newvfp, &OutStatus);
922 afs_inet_ntoa_r(newvfp->cell->fsservers[0]->addrs[0], ipv4_addr);
923 afs_com_err(pnp, code, "(failed to get status of file %s from"
924 "server %s, code = %d)", fname, ipv4_addr, code);
925 afscp_FreeFid(newvfp);
926 afscp_FreeFid(dirvfp);
930 if ( !append && !force &&
931 (OutStatus.Length != 0 || OutStatus.Length_hi !=0 ) ) {
933 * file exists, is of non-zero length, and we're not appending
934 * to it: user must force overwrite
935 * (covers fidwrite edge case)
938 afscp_FreeFid(newvfp);
939 afscp_FreeFid(dirvfp);
940 afs_com_err(pnp, code, "(use -force to overwrite)");
945 Pos = OutStatus.Length_hi;
946 Pos = (Pos << 32) | OutStatus.Length;
949 previous = (struct wbuf *)&bufchain;
954 * currently, these two while loops (1) read the whole source file in
955 * before (2) writing any of it out, meaning that afsio can't deal with
956 * files larger than the maximum amount of memory designated for
957 * reading a file in (WRITEBUFLEN).
958 * Consider going to a single loop, like in readFile(), though will
959 * have implications on timing statistics (such as the "Startup to
960 * find the file" time, below).
963 while (Len < WRITEBUFLEN) {
964 tbuf = (struct wbuf *)malloc(sizeof(struct wbuf));
968 afscp_FreeFid(newvfp);
969 afscp_FreeFid(dirvfp);
970 afs_com_err(pnp, code, "(cannot allocate buffer)");
975 memset(tbuf, 0, sizeof(struct wbuf));
976 tbuf->buflen = BUFFLEN;
978 afs_int64 ll, l = tbuf->buflen;
981 for (ll = 0; ll < l; ll += 4096) {
982 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
983 (unsigned int)((offset + ll) >> 32),
984 (unsigned int)((offset + ll) & 0xffffffff));
988 tbuf->used = (afs_int32) l;
990 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
991 if (tbuf->used == 0) {
996 MD5_Update(&md5, &tbuf->buf, tbuf->used);
997 previous->next = tbuf;
1001 gettimeofday(&opentime, &Timezone);
1003 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
1004 time_elapsed(&starttime, &opentime));
1006 while (!code && bytes) {
1011 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1012 if (tbuf->used == 0)
1014 byteswritten = afscp_PWrite(newvfp, tbuf->buf,
1015 tbuf->used, Pos + xfered);
1016 if (byteswritten != tbuf->used) {
1017 fprintf(stderr,"Only %d instead of %" AFS_INT64_FMT " bytes transferred by rx_Write()\n", byteswritten, length);
1018 fprintf(stderr, "At %" AFS_UINT64_FMT " bytes from the end\n", length);
1022 xfered += tbuf->used;
1025 length -= tbuf->used;
1031 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1034 afs_int64 ll, l = tbuf->buflen;
1035 if (l > synthlength)
1037 for (ll = 0; ll < l; ll += 4096) {
1038 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
1039 (unsigned int)((offset + ll) >> 32),
1040 (unsigned int)((offset + ll) & 0xffffffff));
1044 tbuf->used = (afs_int32) l;
1046 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
1050 MD5_Update(&md5, &tbuf->buf, tbuf->used);
1052 bytes += tbuf->used;
1056 afscp_FreeFid(newvfp);
1057 afscp_FreeFid(dirvfp);
1059 gettimeofday(&writetime, &Timezone);
1061 afs_com_err(pnp, code, "(%s failed with code %d)", as->name,
1063 } else if (verbose) {
1064 summarizeDatarate(&writetime, "write");
1068 bufchain = tbuf->next;
1073 summarizeMD5(fname);