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>
67 #include <hcrypto/md5.h>
68 #ifdef AFS_PTHREAD_ENV
70 pthread_key_t uclient_key;
73 static int lockFile(struct cmd_syndesc *, void *);
74 static int readFile(struct cmd_syndesc *, void *);
75 static int writeFile(struct cmd_syndesc *, void *);
76 static void printDatarate(void);
77 static void summarizeDatarate(struct timeval *, const char *);
78 static int CmdProlog(struct cmd_syndesc *, char **, char **,
80 static int ScanFid(char *, struct AFSFid *);
81 static afs_int32 GetVenusFidByFid(char *, char *, int, struct afscp_venusfid **);
82 static afs_int32 GetVenusFidByPath(char *, char *, struct afscp_venusfid **);
83 static int BreakUpPath(char *, char *, char *);
85 static char pnp[AFSPATHMAX]; /* filename of this program when called */
86 static int verbose = 0; /* Set if -verbose option given */
87 static int cellGiven = 0; /* Set if -cell option given */
88 static int force = 0; /* Set if -force option given */
89 static int readlock = 0; /* Set if -readlock option given */
90 static int waittime = 0; /* Set if -waittime option given */
91 static int useFid = 0; /* Set if fidwrite/fidread/fidappend invoked */
92 static int append = 0; /* Set if append/fidappend invoked */
93 static struct timeval starttime, opentime, readtime, writetime;
94 static afs_uint64 xfered = 0;
95 static struct timeval now;
97 static int Timezone; /* Roken gettimeofday ignores the timezone */
99 static struct timezone Timezone;
102 #define BUFFLEN 65536
103 #define WRITEBUFLEN (BUFFLEN * 1024)
104 #define MEGABYTE_F 1048576.0f
107 static int md5sum = 0; /* Set if -md5 option given */
111 afs_uint32 offset; /* offset inside the buffer */
112 afs_uint32 buflen; /* total length == BUFFLEN */
113 afs_uint32 used; /* bytes used inside buffer */
118 * returns difference in seconds between two times
120 * \param[in] from start time
121 * \param[in] to end time
123 * \post returns "to" minus "from" in seconds
127 time_elapsed(struct timeval *from, struct timeval *to)
129 return (float)(to->tv_sec + (to->tv_usec * 0.000001) - from->tv_sec -
130 (from->tv_usec * 0.000001));
134 * prints current average data transfer rate at no less than 30-second intervals
139 static float oldseconds = 0.0;
140 static afs_uint64 oldxfered = 0;
143 gettimeofday(&now, &Timezone);
144 seconds = time_elapsed(&opentime, &now);
145 if ((seconds - oldseconds) > 30) {
146 fprintf(stderr, "%llu MB transferred, present data rate = %.3f MB/sec.\n", xfered >> 20, /* total bytes transferred, in MB */
147 (xfered - oldxfered) / (seconds - oldseconds) / MEGABYTE_F);
149 oldseconds = seconds;
151 } /* printDatarate */
154 * prints overall average data transfer rate and elapsed time
156 * \param[in] tvp current time (to compare with file open time)
157 * \param[in] xfer_type string identify transfer type ("read" or "write")
160 summarizeDatarate(struct timeval *tvp, const char *xfer_type)
162 float seconds = time_elapsed(&opentime, tvp);
164 fprintf(stderr, "Transfer of %llu bytes took %.3f sec.\n",
166 fprintf(stderr, "Total data rate = %.03f MB/sec. for %s\n",
167 xfered / seconds / MEGABYTE_F, xfer_type);
168 } /* summarizeDatarate */
171 * prints final MD5 sum of all file data transferred
173 * \param[in] fname file name or FID
176 summarizeMD5(char *fname)
178 afs_uint32 md5int[4];
181 MD5_Final((char *) &md5int[0], &md5);
182 p = fname + strlen(fname);
189 fprintf(stderr, "%08x%08x%08x%08x %s\n", htonl(md5int[0]),
190 htonl(md5int[1]), htonl(md5int[2]), htonl(md5int[3]), p);
194 * parses all command-line arguments
196 * \param[in] as arguments list
197 * \param[out] cellp cell name
198 * \param[out] realmp realm name
199 * \param[out] fnp filename (either fid or path)
200 * \param[out] slp "synthesized" (made up) data given
202 * \post returns 0 on success or -1 on error
206 CmdProlog(struct cmd_syndesc *as, char **cellp, char **realmp,
207 char **fnp, char **slp)
210 struct cmd_parmdesc *pdp;
213 afs_com_err(pnp, EINVAL, "(syndesc is null)");
217 /* determine which command was requested */
218 if (strncmp(as->name, "fid", 3) == 0) /* fidread/fidwrite/fidappend */
220 if ( (strcmp(as->name, "append") == 0) ||
221 (strcmp(as->name, "fidappend") == 0) )
222 append = 1; /* global */
224 /* attempts to ensure loop is bounded: */
225 for (pdp = as->parms, i = 0; pdp && (i < as->nParms); i++, pdp++) {
226 if (pdp->items != NULL) {
227 if (strcmp(pdp->name, "-verbose") == 0)
229 else if (strcmp(pdp->name, "-md5") == 0)
230 md5sum = 1; /* global */
231 else if (strcmp(pdp->name, "-cell") == 0) {
232 cellGiven = 1; /* global */
233 *cellp = pdp->items->data;
234 } else if ( (strcmp(pdp->name, "-file") == 0) ||
235 (strcmp(pdp->name, "-fid") == 0) ||
236 (strcmp(pdp->name, "-vnode") == 0) )
237 *fnp = pdp->items->data;
238 else if (strcmp(pdp->name, "-force") == 0)
239 force = 1; /* global */
240 else if (strcmp(pdp->name, "-synthesize") == 0)
241 *slp = pdp->items->data;
242 else if (strcmp(pdp->name, "-realm") == 0)
243 *realmp = pdp->items->data;
244 else if (strcmp(pdp->name, "-wait") == 0)
245 waittime = atoi(pdp->items->data);
246 else if (strcmp(pdp->name, "-readlock") == 0)
254 main(int argc, char **argv)
256 struct cmd_syndesc *ts;
257 char baseName[AFSNAMEMAX];
259 /* try to get only the base name of this executable for use in logs */
260 if (BreakUpPath(argv[0], NULL, baseName) > 0)
261 strlcpy(pnp, baseName, AFSNAMEMAX);
263 strlcpy(pnp, argv[0], AFSPATHMAX);
265 #ifdef AFS_PTHREAD_ENV
266 assert(pthread_key_create(&uclient_key, NULL) == 0);
269 ts = cmd_CreateSyntax("lock", lockFile, (void *)LockWrite,
270 "lock a file in AFS");
271 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
272 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
273 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
275 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
276 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
277 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
279 ts = cmd_CreateSyntax("fidlock", lockFile, (void *)LockWrite,
280 "lock by FID a file from AFS");
281 cmd_IsAdministratorCommand(ts);
282 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
283 "volume.vnode.uniquifier");
284 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
285 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
287 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
288 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
289 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
291 ts = cmd_CreateSyntax("unlock", lockFile, (void *)LockRelease,
292 "unlock a file in AFS");
293 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
294 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
295 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
297 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
298 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
300 ts = cmd_CreateSyntax("fidunlock", lockFile, (void *)LockRelease,
301 "unlock by FID a file from AFS");
302 cmd_IsAdministratorCommand(ts);
303 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
304 "volume.vnode.uniquifier");
305 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
306 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
308 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
309 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
311 ts = cmd_CreateSyntax("read", readFile, NULL,
312 "read a file from AFS");
313 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
314 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
315 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
316 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
317 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
319 ts = cmd_CreateSyntax("fidread", readFile, CMD_REQUIRED,
320 "read on a non AFS-client a file from AFS");
321 cmd_IsAdministratorCommand(ts);
322 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
323 "volume.vnode.uniquifier");
324 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
325 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
326 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
327 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
329 ts = cmd_CreateSyntax("write", writeFile, NULL,
330 "write a file into AFS");
331 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
332 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
333 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
334 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
335 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
336 "overwrite existing file");
337 cmd_AddParm(ts, "-synthesize", CMD_SINGLE, CMD_OPTIONAL,
338 "create data pattern of specified length instead reading from stdin");
339 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
341 ts = cmd_CreateSyntax("fidwrite", writeFile, CMD_REQUIRED,
342 "write a file into AFS");
343 cmd_IsAdministratorCommand(ts);
344 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
345 "volume.vnode.uniquifier");
346 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
347 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
348 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
349 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
350 "overwrite existing file");
351 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
353 ts = cmd_CreateSyntax("append", writeFile, NULL,
354 "append to a file in AFS");
355 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
356 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
357 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
358 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
360 ts = cmd_CreateSyntax("fidappend", writeFile, NULL,
361 "append to a file in AFS");
362 cmd_IsAdministratorCommand(ts);
363 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
364 "volume.vnode.uniquifier");
365 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
366 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
367 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
369 if (afscp_Init(NULL) != 0)
372 cmd_Dispatch(argc, argv);
379 * standardized way of parsing a File ID (FID) from command line input
381 * \param[in] fidString dot-delimited FID triple
382 * \param[out] fid pointer to the AFSFid to fill in
384 * \post The FID pointed to by "fid" is filled in which the parsed Volume,
385 * Vnode, and Uniquifier data. The string should be in the format
386 * of three numbers separated by dot (.) delimiters, representing
387 * (in order) the volume id, vnode number, and uniquifier.
388 * Example: "576821346.1.1"
391 ScanFid(char *fidString, struct AFSFid *fid)
394 long unsigned int f1, f2, f3;
397 i = sscanf(fidString, "%lu.%lu.%lu", &f1, &f2, &f3);
398 fid->Volume = (afs_uint32) f1;
399 fid->Vnode = (afs_uint32) f2;
400 fid->Unique = (afs_uint32) f3;
407 afs_com_err(pnp, code, "(invalid FID triple: %s)", fidString);
414 * look up cell info and verify FID info from user input
416 * \param[in] fidString string containing FID info
417 * \param[in] cellName cell name string
418 * \param[in] onlyRW bool: 1 = RW vol only, 0 = any vol type
419 * \param[out] avfpp pointer to venusfid info
421 * \post *avfpp will contain the VenusFid info found for the FID
422 * given by the used in the string fidString and zero is
423 * returned. If not found, an appropriate afs error code
424 * is returned and *avfpp will be NULL.
426 * \note Any non-NULL avfpp returned should later be freed with
427 * afscp_FreeFid() when no longer needed.
430 GetVenusFidByFid(char *fidString, char *cellName, int onlyRW,
431 struct afscp_venusfid **avfpp)
435 struct afscp_volume *avolp;
437 if (*avfpp == NULL) {
438 *avfpp = malloc(sizeof(struct afscp_venusfid));
439 if ( *avfpp == NULL ) {
444 memset(*avfpp, 0, sizeof(struct afscp_venusfid));
446 if (cellName == NULL) {
447 (*avfpp)->cell = afscp_DefaultCell();
449 (*avfpp)->cell = afscp_CellByName(cellName, NULL);
451 if ((*avfpp)->cell == NULL) {
452 if (afscp_errno == 0)
459 code = ScanFid(fidString, &((*avfpp)->fid));
465 avolp = afscp_VolumeById((*avfpp)->cell, (*avfpp)->fid.Volume);
467 if (afscp_errno == 0)
471 afs_com_err(pnp, code, "(finding volume %lu)",
472 afs_printable_uint32_lu((*avfpp)->fid.Volume));
476 if ( onlyRW && (avolp->voltype != RWVOL) ) {
477 avolp = afscp_VolumeByName((*avfpp)->cell, avolp->name, RWVOL);
479 if (afscp_errno == 0)
483 afs_com_err(pnp, code, "(finding volume %lu)",
484 afs_printable_uint32_lu((*avfpp)->fid.Volume));
487 (*avfpp)->fid.Volume = avolp->id; /* is this safe? */
490 code = afscp_Stat((*avfpp), &sbuf);
492 afs_com_err(pnp, code, "(stat failed with code %d)", code);
496 } /* GetVenusFidByFid */
499 * Split a full path up into dirName and baseName components
501 * \param[in] fullPath can be absolute, relative, or local
502 * \param[out] dirName pointer to allocated char buffer or NULL
503 * \param[out] baseName pointer to allocated char buffer or NULL
505 * \post To the fulleset extent possible, the rightmost full path
506 * component will be copied into baseName and all other
507 * components into dirName (minus the trailing path separator).
508 * If either dirName or baseName are NULL, only the non-NULL
509 * pointer will be filled in (but both can't be null or it would
510 * be pointless) -- so the caller can retrieve, say, only baseName
511 * if desired. The return code is the number of strings copied:
512 * 0 if neither dirName nor baseName could be filled in
513 * 1 if either dirName or baseName were filled in
514 * 2 if both dirName and baseName were filled in
517 BreakUpPath(char *fullPath, char *dirName, char *baseName)
520 size_t dirNameLen = 0;
521 int code = 0, useDirName = 1, useBaseName = 1;
523 if (fullPath == NULL) {
529 if (baseName == NULL)
531 if (!useBaseName && !useDirName) {
532 /* would be pointless to continue -- must be error in call */
536 lastSlash = strrchr(fullPath, '\\');
538 lastSlash = strrchr(fullPath, '/');
540 if (lastSlash != NULL) {
541 /* then lastSlash points to the last path separator in fullPath */
543 dirNameLen = strlen(fullPath) - strlen(lastSlash);
544 strlcpy(dirName, fullPath, dirNameLen + 1);
549 strlcpy(baseName, lastSlash, strlen(lastSlash) + 1);
553 /* there are no path separators in fullPath -- it's just a baseName */
555 strlcpy(baseName, fullPath, strlen(fullPath) + 1);
563 * Get the VenusFid info available for the file at AFS path 'fullPath'.
564 * Works without pioctls/afsd by using libafscp. Analogous to
565 * get_file_cell() in the previous iteration of afsio.
567 * \param[in] fullPath the file name
568 * \param[in] cellName the cell name to look up
569 * \param[out] avfpp pointer to Venus FID info to be filled in
571 * \post If the path resolves successfully (using afscp_ResolvePath),
572 * then vfpp will contain the Venus FID info (cell info plus
573 * AFSFid) of the last path segment in fullPath.
576 GetVenusFidByPath(char *fullPath, char *cellName,
577 struct afscp_venusfid **avfpp)
581 if (fullPath == NULL) {
585 if (cellName != NULL) {
586 code = (afs_int32) afscp_SetDefaultCell(cellName);
592 *avfpp = afscp_ResolvePath(fullPath);
593 if (*avfpp == NULL) {
594 if (afscp_errno == 0)
601 } /* GetVenusFidByPath */
604 lockFile(struct cmd_syndesc *as, void *arock)
610 struct AFSFetchStatus OutStatus;
611 struct afscp_venusfid *avfp = NULL;
614 int locktype = (int)(intptr_t) arock;
617 /* stdout on Windows defaults to _O_TEXT mode */
618 _setmode(1, _O_BINARY);
621 gettimeofday(&starttime, &Timezone);
623 CmdProlog(as, &cell, &realm, &fname, NULL);
624 afscp_AnonymousAuth(1);
626 if ((locktype == LockWrite) && readlock)
630 code = afscp_SetDefaultRealm(realm);
633 code = afscp_SetDefaultCell(cell);
636 code = GetVenusFidByFid(fname, cell, 0, &avfp);
638 code = GetVenusFidByPath(fname, cell, &avfp);
640 afs_com_err(pnp, code, "(file not found: %s)", fname);
645 code = afscp_GetStatus(avfp, &OutStatus);
647 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
648 afs_com_err(pnp, code, "(failed to get status of file %s from"
649 "server %s, code = %d)", fname, ipv4_addr, code);
654 if (locktype != LockRelease) {
655 while (OutStatus.lockCount != 0) {
656 code = afscp_WaitForCallback(avfp, waittime);
657 if ((code == -1) && (afscp_errno == ETIMEDOUT))
659 if ((code = afscp_GetStatus(avfp, &OutStatus)) != 0)
663 if (OutStatus.lockCount == 0) {
669 code = afscp_Lock(avfp, locktype);
670 if ((code == -1) && (afscp_errno == EWOULDBLOCK))
679 afs_com_err(pnp, code, "(failed to change lock status: %d)", afscp_errno);
685 readFile(struct cmd_syndesc *as, void *unused)
691 struct AFSFetchStatus OutStatus;
692 struct afscp_venusfid *avfp = NULL;
695 afs_int64 length = 0, Len;
700 int bufflen = BUFFLEN;
703 /* stdout on Windows defaults to _O_TEXT mode */
704 _setmode(1, _O_BINARY);
707 gettimeofday(&starttime, &Timezone);
709 CmdProlog(as, &cell, &realm, &fname, NULL);
710 afscp_AnonymousAuth(1);
716 code = afscp_SetDefaultRealm(realm);
719 code = afscp_SetDefaultCell(cell);
722 code = GetVenusFidByFid(fname, cell, 0, &avfp);
724 code = GetVenusFidByPath(fname, cell, &avfp);
726 afs_com_err(pnp, code, "(file not found: %s)", fname);
730 if (avfp->fid.Vnode & 1) {
732 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
737 code = afscp_GetStatus(avfp, &OutStatus);
739 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
740 afs_com_err(pnp, code, "(failed to get status of file %s from"
741 "server %s, code = %d)", fname, ipv4_addr, code);
746 gettimeofday(&opentime, &Timezone);
748 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
749 time_elapsed(&starttime, &opentime));
750 Len = OutStatus.Length_hi;
752 Len += OutStatus.Length;
754 buf = (char *) malloc(bufflen * sizeof(char));
757 afs_com_err(pnp, code, "(cannot allocate buffer)");
761 memset(buf, 0, bufflen * sizeof(char));
763 while (!code && NonZeroInt64(length)) {
764 if (length > bufflen)
767 len = (afs_int32) length;
768 bytes = afscp_PRead(avfp, buf, len, Pos);
770 code = -3; /* what error name should we use here? */
772 MD5_Update(&md5, buf, len);
774 len = write(1, buf, len); /* to stdout */
787 gettimeofday(&readtime, &Timezone);
791 summarizeDatarate(&readtime, "read");
799 writeFile(struct cmd_syndesc *as, void *unused)
803 char *sSynthLen = NULL;
806 afs_int32 byteswritten;
807 struct AFSFetchStatus OutStatus;
808 struct AFSStoreStatus InStatus;
809 struct afscp_venusfid *dirvfp = NULL, *newvfp = NULL;
811 afs_int64 length, Len, synthlength = 0, offset = 0;
816 struct wbuf *bufchain = 0;
817 struct wbuf *previous, *tbuf;
818 char dirName[AFSPATHMAX];
819 char baseName[AFSNAMEMAX];
823 /* stdin on Windows defaults to _O_TEXT mode */
824 _setmode(0, _O_BINARY);
827 CmdProlog(as, &cell, &realm, &fname, &sSynthLen);
828 afscp_AnonymousAuth(1);
831 code = afscp_SetDefaultRealm(realm);
834 code = afscp_SetDefaultCell(cell);
837 code = util_GetInt64(sSynthLen, &synthlength);
839 afs_com_err(pnp, code, "(invalid value for synthesize length %s)",
847 code = GetVenusFidByFid(fname, cell, 1, &newvfp);
849 afs_com_err(pnp, code, "(GetVenusFidByFid returned code %d)", code);
853 code = GetVenusFidByPath(fname, cell, &newvfp);
854 if (code == 0) { /* file was found */
859 * file cannot already exist if specified by path and not
860 * appending to it unless user forces overwrite
863 afscp_FreeFid(newvfp);
864 afs_com_err(pnp, code, "(use -force to overwrite)");
867 } else { /* file not found */
870 afs_com_err(pnp, code, "(cannot append to non-existent file)");
874 if (!append && !overWrite) { /* must create a new file in this case */
875 if ( BreakUpPath(fname, dirName, baseName) != 2 ) {
877 afs_com_err(pnp, code, "(must provide full AFS path)");
878 afscp_FreeFid(newvfp);
882 code = GetVenusFidByPath(dirName, cell, &dirvfp);
883 afscp_FreeFid(newvfp); /* release now-unneeded fid */
886 afs_com_err(pnp, code, "(is dir %s in AFS?)", dirName);
892 if ( (newvfp != NULL) && (newvfp->fid.Vnode & 1) ) {
894 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
895 afscp_FreeFid(newvfp);
896 afscp_FreeFid(dirvfp);
899 gettimeofday(&starttime, &Timezone);
901 InStatus.UnixModeBits = 0644;
902 InStatus.Mask = AFS_SETMODE + AFS_FSYNC;
903 if (newvfp == NULL) {
904 code = afscp_CreateFile(dirvfp, baseName, &InStatus, &newvfp);
906 afs_com_err(pnp, code,
907 "(could not create file %s in directory %lu.%lu.%lu)",
908 baseName, afs_printable_uint32_lu(dirvfp->fid.Volume),
909 afs_printable_uint32_lu(dirvfp->fid.Vnode),
910 afs_printable_uint32_lu(dirvfp->fid.Unique));
914 code = afscp_GetStatus(newvfp, &OutStatus);
916 afs_inet_ntoa_r(newvfp->cell->fsservers[0]->addrs[0], ipv4_addr);
917 afs_com_err(pnp, code, "(failed to get status of file %s from"
918 "server %s, code = %d)", fname, ipv4_addr, code);
919 afscp_FreeFid(newvfp);
920 afscp_FreeFid(dirvfp);
924 if ( !append && !force &&
925 (OutStatus.Length != 0 || OutStatus.Length_hi !=0 ) ) {
927 * file exists, is of non-zero length, and we're not appending
928 * to it: user must force overwrite
929 * (covers fidwrite edge case)
932 afscp_FreeFid(newvfp);
933 afscp_FreeFid(dirvfp);
934 afs_com_err(pnp, code, "(use -force to overwrite)");
939 Pos = OutStatus.Length_hi;
940 Pos = (Pos << 32) | OutStatus.Length;
943 previous = (struct wbuf *)&bufchain;
948 * currently, these two while loops (1) read the whole source file in
949 * before (2) writing any of it out, meaning that afsio can't deal with
950 * files larger than the maximum amount of memory designated for
951 * reading a file in (WRITEBUFLEN).
952 * Consider going to a single loop, like in readFile(), though will
953 * have implications on timing statistics (such as the "Startup to
954 * find the file" time, below).
957 while (Len < WRITEBUFLEN) {
958 tbuf = (struct wbuf *)malloc(sizeof(struct wbuf));
962 afscp_FreeFid(newvfp);
963 afscp_FreeFid(dirvfp);
964 afs_com_err(pnp, code, "(cannot allocate buffer)");
969 memset(tbuf, 0, sizeof(struct wbuf));
970 tbuf->buflen = BUFFLEN;
972 afs_int64 ll, l = tbuf->buflen;
975 for (ll = 0; ll < l; ll += 4096) {
976 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
977 (unsigned int)((offset + ll) >> 32),
978 (unsigned int)((offset + ll) & 0xffffffff));
982 tbuf->used = (afs_int32) l;
984 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
985 if (tbuf->used == 0) {
990 MD5_Update(&md5, &tbuf->buf, tbuf->used);
991 previous->next = tbuf;
995 gettimeofday(&opentime, &Timezone);
997 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
998 time_elapsed(&starttime, &opentime));
1000 while (!code && bytes) {
1005 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1006 if (tbuf->used == 0)
1008 byteswritten = afscp_PWrite(newvfp, tbuf->buf,
1009 tbuf->used, Pos + xfered);
1010 if (byteswritten != tbuf->used) {
1011 fprintf(stderr,"Only %d instead of %" AFS_INT64_FMT " bytes transferred by rx_Write()\n", byteswritten, length);
1012 fprintf(stderr, "At %" AFS_UINT64_FMT " bytes from the end\n", length);
1016 xfered += tbuf->used;
1019 length -= tbuf->used;
1025 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1028 afs_int64 ll, l = tbuf->buflen;
1029 if (l > synthlength)
1031 for (ll = 0; ll < l; ll += 4096) {
1032 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
1033 (unsigned int)((offset + ll) >> 32),
1034 (unsigned int)((offset + ll) & 0xffffffff));
1038 tbuf->used = (afs_int32) l;
1040 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
1044 MD5_Update(&md5, &tbuf->buf, tbuf->used);
1046 bytes += tbuf->used;
1050 afscp_FreeFid(newvfp);
1051 afscp_FreeFid(dirvfp);
1053 gettimeofday(&writetime, &Timezone);
1055 afs_com_err(pnp, code, "(%s failed with code %d)", as->name,
1057 } else if (verbose) {
1058 summarizeDatarate(&writetime, "write");
1062 bufchain = tbuf->next;
1067 summarizeMD5(fname);