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
66 pthread_key_t uclient_key;
69 static int lockFile(struct cmd_syndesc *, void *);
70 static int readFile(struct cmd_syndesc *, void *);
71 static int writeFile(struct cmd_syndesc *, void *);
72 static void printDatarate(void);
73 static void summarizeDatarate(struct timeval *, const char *);
74 static int CmdProlog(struct cmd_syndesc *, char **, char **,
76 static int ScanFid(char *, struct AFSFid *);
77 static afs_int32 GetVenusFidByFid(char *, char *, int, struct afscp_venusfid **);
78 static afs_int32 GetVenusFidByPath(char *, char *, struct afscp_venusfid **);
79 static int BreakUpPath(char *, char *, char *, size_t);
81 static char pnp[AFSPATHMAX]; /* filename of this program when called */
82 static int verbose = 0; /* Set if -verbose option given */
83 static int cellGiven = 0; /* Set if -cell option given */
84 static int force = 0; /* Set if -force option given */
85 static int readlock = 0; /* Set if -readlock option given */
86 static int waittime = 0; /* Set if -waittime option given */
87 static int useFid = 0; /* Set if fidwrite/fidread/fidappend invoked */
88 static int append = 0; /* Set if append/fidappend invoked */
89 static struct timeval starttime, opentime, readtime, writetime;
90 static afs_uint64 xfered = 0;
91 static struct timeval now;
93 static int Timezone; /* Roken gettimeofday ignores the timezone */
95 static struct timezone Timezone;
99 #define WRITEBUFLEN (BUFFLEN * 1024)
100 #define MEGABYTE_F 1048576.0f
103 static int md5sum = 0; /* Set if -md5 option given */
107 afs_uint32 offset; /* offset inside the buffer */
108 afs_uint32 buflen; /* total length == BUFFLEN */
109 afs_uint32 used; /* bytes used inside buffer */
114 * returns difference in seconds between two times
116 * \param[in] from start time
117 * \param[in] to end time
119 * \post returns "to" minus "from" in seconds
123 time_elapsed(struct timeval *from, struct timeval *to)
125 return (float)(to->tv_sec + (to->tv_usec * 0.000001) - from->tv_sec -
126 (from->tv_usec * 0.000001));
130 * prints current average data transfer rate at no less than 30-second intervals
135 static float oldseconds = 0.0;
136 static afs_uint64 oldxfered = 0;
139 gettimeofday(&now, &Timezone);
140 seconds = time_elapsed(&opentime, &now);
141 if ((seconds - oldseconds) > 30) {
142 fprintf(stderr, "%llu MB transferred, present data rate = %.3f MB/sec.\n", xfered >> 20, /* total bytes transferred, in MB */
143 (xfered - oldxfered) / (seconds - oldseconds) / MEGABYTE_F);
145 oldseconds = seconds;
147 } /* printDatarate */
150 * prints overall average data transfer rate and elapsed time
152 * \param[in] tvp current time (to compare with file open time)
153 * \param[in] xfer_type string identify transfer type ("read" or "write")
156 summarizeDatarate(struct timeval *tvp, const char *xfer_type)
158 float seconds = time_elapsed(&opentime, tvp);
160 fprintf(stderr, "Transfer of %llu bytes took %.3f sec.\n",
162 fprintf(stderr, "Total data rate = %.03f MB/sec. for %s\n",
163 xfered / seconds / MEGABYTE_F, xfer_type);
164 } /* summarizeDatarate */
167 * prints final MD5 sum of all file data transferred
169 * \param[in] fname file name or FID
172 summarizeMD5(char *fname)
174 afs_uint32 md5int[4];
177 MD5_Final((char *) &md5int[0], &md5);
178 p = fname + strlen(fname);
185 fprintf(stderr, "%08x%08x%08x%08x %s\n", htonl(md5int[0]),
186 htonl(md5int[1]), htonl(md5int[2]), htonl(md5int[3]), p);
190 * parses all command-line arguments
192 * \param[in] as arguments list
193 * \param[out] cellp cell name
194 * \param[out] realmp realm name
195 * \param[out] fnp filename (either fid or path)
196 * \param[out] slp "synthesized" (made up) data given
198 * \post returns 0 on success or -1 on error
202 CmdProlog(struct cmd_syndesc *as, char **cellp, char **realmp,
203 char **fnp, char **slp)
206 struct cmd_parmdesc *pdp;
209 afs_com_err(pnp, EINVAL, "(syndesc is null)");
213 /* determine which command was requested */
214 if (strncmp(as->name, "fid", 3) == 0) /* fidread/fidwrite/fidappend */
216 if ( (strcmp(as->name, "append") == 0) ||
217 (strcmp(as->name, "fidappend") == 0) )
218 append = 1; /* global */
220 /* attempts to ensure loop is bounded: */
221 for (pdp = as->parms, i = 0; pdp && (i < as->nParms); i++, pdp++) {
222 if (pdp->items != NULL) {
223 if (strcmp(pdp->name, "-verbose") == 0)
225 else if (strcmp(pdp->name, "-md5") == 0)
226 md5sum = 1; /* global */
227 else if (strcmp(pdp->name, "-cell") == 0) {
228 cellGiven = 1; /* global */
229 *cellp = pdp->items->data;
230 } else if ( (strcmp(pdp->name, "-file") == 0) ||
231 (strcmp(pdp->name, "-fid") == 0) ||
232 (strcmp(pdp->name, "-vnode") == 0) )
233 *fnp = pdp->items->data;
234 else if (strcmp(pdp->name, "-force") == 0)
235 force = 1; /* global */
236 else if (strcmp(pdp->name, "-synthesize") == 0)
237 *slp = pdp->items->data;
238 else if (strcmp(pdp->name, "-realm") == 0)
239 *realmp = pdp->items->data;
240 else if (strcmp(pdp->name, "-wait") == 0)
241 waittime = atoi(pdp->items->data);
242 else if (strcmp(pdp->name, "-readlock") == 0)
250 main(int argc, char **argv)
252 struct cmd_syndesc *ts;
253 char baseName[AFSNAMEMAX];
255 /* try to get only the base name of this executable for use in logs */
256 if (BreakUpPath(argv[0], NULL, baseName, AFSNAMEMAX) > 0)
257 strlcpy(pnp, baseName, AFSNAMEMAX);
259 strlcpy(pnp, argv[0], AFSPATHMAX);
261 #ifdef AFS_PTHREAD_ENV
262 assert(pthread_key_create(&uclient_key, NULL) == 0);
265 ts = cmd_CreateSyntax("lock", lockFile, (void *)LockWrite,
266 "lock a file in AFS");
267 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
268 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
269 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
271 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
272 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
273 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
275 ts = cmd_CreateSyntax("fidlock", lockFile, (void *)LockWrite,
276 "lock by FID a file from AFS");
277 cmd_IsAdministratorCommand(ts);
278 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
279 "volume.vnode.uniquifier");
280 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
281 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
283 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
284 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
285 cmd_AddParm(ts, "-readlock", CMD_FLAG, CMD_OPTIONAL, "read lock only");
287 ts = cmd_CreateSyntax("unlock", lockFile, (void *)LockRelease,
288 "unlock a file in AFS");
289 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
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");
296 ts = cmd_CreateSyntax("fidunlock", lockFile, (void *)LockRelease,
297 "unlock by FID a file from AFS");
298 cmd_IsAdministratorCommand(ts);
299 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
300 "volume.vnode.uniquifier");
301 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
302 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
304 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
305 cmd_AddParm(ts, "-waitseconds", CMD_SINGLE, CMD_OPTIONAL, "seconds to wait before giving up");
307 ts = cmd_CreateSyntax("read", readFile, NULL,
308 "read a file from AFS");
309 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
310 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
311 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
312 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
313 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
315 ts = cmd_CreateSyntax("fidread", readFile, CMD_REQUIRED,
316 "read on a non AFS-client a file from AFS");
317 cmd_IsAdministratorCommand(ts);
318 cmd_AddParm(ts, "-fid", CMD_SINGLE, CMD_REQUIRED,
319 "volume.vnode.uniquifier");
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("write", writeFile, NULL,
326 "write a file into AFS");
327 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
328 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
329 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
330 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
331 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
332 "overwrite existing file");
333 cmd_AddParm(ts, "-synthesize", CMD_SINGLE, CMD_OPTIONAL,
334 "create data pattern of specified length instead reading from stdin");
335 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
337 ts = cmd_CreateSyntax("fidwrite", writeFile, CMD_REQUIRED,
338 "write a file into AFS");
339 cmd_IsAdministratorCommand(ts);
340 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
341 "volume.vnode.uniquifier");
342 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
343 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
344 cmd_AddParm(ts, "-md5", CMD_FLAG, CMD_OPTIONAL, "calculate md5 checksum");
345 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
346 "overwrite existing file");
347 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
349 ts = cmd_CreateSyntax("append", writeFile, NULL,
350 "append to a file in AFS");
351 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_REQUIRED, "AFS-filename");
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, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
356 ts = cmd_CreateSyntax("fidappend", writeFile, NULL,
357 "append to a file in AFS");
358 cmd_IsAdministratorCommand(ts);
359 cmd_AddParm(ts, "-vnode", CMD_SINGLE, CMD_REQUIRED,
360 "volume.vnode.uniquifier");
361 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cellname");
362 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, (char *)0);
363 cmd_AddParm(ts, "-realm", CMD_SINGLE, CMD_OPTIONAL, "REALMNAME");
365 if (afscp_Init(NULL) != 0)
368 cmd_Dispatch(argc, argv);
375 * standardized way of parsing a File ID (FID) from command line input
377 * \param[in] fidString dot-delimited FID triple
378 * \param[out] fid pointer to the AFSFid to fill in
380 * \post The FID pointed to by "fid" is filled in which the parsed Volume,
381 * Vnode, and Uniquifier data. The string should be in the format
382 * of three numbers separated by dot (.) delimiters, representing
383 * (in order) the volume id, vnode number, and uniquifier.
384 * Example: "576821346.1.1"
387 ScanFid(char *fidString, struct AFSFid *fid)
390 long unsigned int f1, f2, f3;
393 i = sscanf(fidString, "%lu.%lu.%lu", &f1, &f2, &f3);
394 fid->Volume = (afs_uint32) f1;
395 fid->Vnode = (afs_uint32) f2;
396 fid->Unique = (afs_uint32) f3;
403 afs_com_err(pnp, code, "(invalid FID triple: %s)", fidString);
410 * look up cell info and verify FID info from user input
412 * \param[in] fidString string containing FID info
413 * \param[in] cellName cell name string
414 * \param[in] onlyRW bool: 1 = RW vol only, 0 = any vol type
415 * \param[out] avfpp pointer to venusfid info
417 * \post *avfpp will contain the VenusFid info found for the FID
418 * given by the used in the string fidString and zero is
419 * returned. If not found, an appropriate afs error code
420 * is returned and *avfpp will be NULL.
422 * \note Any non-NULL avfpp returned should later be freed with
423 * afscp_FreeFid() when no longer needed.
426 GetVenusFidByFid(char *fidString, char *cellName, int onlyRW,
427 struct afscp_venusfid **avfpp)
431 struct afscp_volume *avolp;
433 if (*avfpp == NULL) {
434 *avfpp = calloc(1, sizeof(struct afscp_venusfid));
435 if ( *avfpp == NULL ) {
441 if (cellName == NULL) {
442 (*avfpp)->cell = afscp_DefaultCell();
444 (*avfpp)->cell = afscp_CellByName(cellName, NULL);
446 if ((*avfpp)->cell == NULL) {
447 if (afscp_errno == 0)
454 code = ScanFid(fidString, &((*avfpp)->fid));
460 avolp = afscp_VolumeById((*avfpp)->cell, (*avfpp)->fid.Volume);
462 if (afscp_errno == 0)
466 afs_com_err(pnp, code, "(finding volume %lu)",
467 afs_printable_uint32_lu((*avfpp)->fid.Volume));
471 if ( onlyRW && (avolp->voltype != RWVOL) ) {
472 avolp = afscp_VolumeByName((*avfpp)->cell, avolp->name, RWVOL);
474 if (afscp_errno == 0)
478 afs_com_err(pnp, code, "(finding volume %lu)",
479 afs_printable_uint32_lu((*avfpp)->fid.Volume));
482 (*avfpp)->fid.Volume = avolp->id; /* is this safe? */
485 code = afscp_Stat((*avfpp), &sbuf);
487 afs_com_err(pnp, code, "(stat failed with code %d)", code);
491 } /* GetVenusFidByFid */
494 * Split a full path up into dirName and baseName components
496 * \param[in] fullPath can be absolute, relative, or local
497 * \param[out] dirName pointer to allocated char buffer or NULL
498 * \param[out] baseName pointer to allocated char buffer or NULL
500 * \post To the fulleset extent possible, the rightmost full path
501 * component will be copied into baseName and all other
502 * components into dirName (minus the trailing path separator).
503 * If either dirName or baseName are NULL, only the non-NULL
504 * pointer will be filled in (but both can't be null or it would
505 * be pointless) -- so the caller can retrieve, say, only baseName
506 * if desired. The return code is the number of strings copied:
507 * 0 if neither dirName nor baseName could be filled in
508 * 1 if either dirName or baseName were filled in
509 * 2 if both dirName and baseName were filled in
512 BreakUpPath(char *fullPath, char *dirName, char *baseName, size_t baseNameSize)
515 size_t dirNameLen = 0;
516 int code = 0, useDirName = 1, useBaseName = 1;
518 if (fullPath == NULL) {
524 if (baseName == NULL)
526 if (!useBaseName && !useDirName) {
527 /* would be pointless to continue -- must be error in call */
531 lastSlash = strrchr(fullPath, '\\');
533 lastSlash = strrchr(fullPath, '/');
535 if (lastSlash != NULL) {
536 /* then lastSlash points to the last path separator in fullPath */
538 dirNameLen = strlen(fullPath) - strlen(lastSlash);
539 strlcpy(dirName, fullPath, min(dirNameLen + 1, baseNameSize));
544 strlcpy(baseName, lastSlash, min(strlen(lastSlash) + 1, baseNameSize));
548 /* there are no path separators in fullPath -- it's just a baseName */
550 strlcpy(baseName, fullPath, min(strlen(fullPath) + 1, baseNameSize));
558 * Get the VenusFid info available for the file at AFS path 'fullPath'.
559 * Works without pioctls/afsd by using libafscp. Analogous to
560 * get_file_cell() in the previous iteration of afsio.
562 * \param[in] fullPath the file name
563 * \param[in] cellName the cell name to look up
564 * \param[out] avfpp pointer to Venus FID info to be filled in
566 * \post If the path resolves successfully (using afscp_ResolvePath),
567 * then vfpp will contain the Venus FID info (cell info plus
568 * AFSFid) of the last path segment in fullPath.
571 GetVenusFidByPath(char *fullPath, char *cellName,
572 struct afscp_venusfid **avfpp)
576 if (fullPath == NULL) {
580 if (cellName != NULL) {
581 code = (afs_int32) afscp_SetDefaultCell(cellName);
587 *avfpp = afscp_ResolvePath(fullPath);
588 if (*avfpp == NULL) {
589 if (afscp_errno == 0)
596 } /* GetVenusFidByPath */
599 lockFile(struct cmd_syndesc *as, void *arock)
605 struct AFSFetchStatus OutStatus;
606 struct afscp_venusfid *avfp = NULL;
609 int locktype = (int)(intptr_t) arock;
612 /* stdout on Windows defaults to _O_TEXT mode */
613 _setmode(1, _O_BINARY);
616 gettimeofday(&starttime, &Timezone);
618 CmdProlog(as, &cell, &realm, &fname, NULL);
619 afscp_AnonymousAuth(1);
621 if ((locktype == LockWrite) && readlock)
625 code = afscp_SetDefaultRealm(realm);
628 code = afscp_SetDefaultCell(cell);
631 code = GetVenusFidByFid(fname, cell, 0, &avfp);
633 code = GetVenusFidByPath(fname, cell, &avfp);
635 afs_com_err(pnp, code, "(file not found: %s)", fname);
640 code = afscp_GetStatus(avfp, &OutStatus);
642 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
643 afs_com_err(pnp, code, "(failed to get status of file %s from"
644 "server %s, code = %d)", fname, ipv4_addr, code);
649 if (locktype != LockRelease) {
650 while (OutStatus.lockCount != 0) {
651 code = afscp_WaitForCallback(avfp, waittime);
652 if ((code == -1) && (afscp_errno == ETIMEDOUT))
654 if ((code = afscp_GetStatus(avfp, &OutStatus)) != 0)
658 if (OutStatus.lockCount == 0) {
664 code = afscp_Lock(avfp, locktype);
665 if ((code == -1) && (afscp_errno == EWOULDBLOCK))
674 afs_com_err(pnp, code, "(failed to change lock status: %d)", afscp_errno);
680 readFile(struct cmd_syndesc *as, void *unused)
686 struct AFSFetchStatus OutStatus;
687 struct afscp_venusfid *avfp = NULL;
690 afs_int64 length = 0, Len;
695 int bufflen = BUFFLEN;
698 /* stdout on Windows defaults to _O_TEXT mode */
699 _setmode(1, _O_BINARY);
702 gettimeofday(&starttime, &Timezone);
704 CmdProlog(as, &cell, &realm, &fname, NULL);
705 afscp_AnonymousAuth(1);
711 code = afscp_SetDefaultRealm(realm);
714 code = afscp_SetDefaultCell(cell);
717 code = GetVenusFidByFid(fname, cell, 0, &avfp);
719 code = GetVenusFidByPath(fname, cell, &avfp);
721 afs_com_err(pnp, code, "(file not found: %s)", fname);
725 if (avfp->fid.Vnode & 1) {
727 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
732 code = afscp_GetStatus(avfp, &OutStatus);
734 afs_inet_ntoa_r(avfp->cell->fsservers[0]->addrs[0], ipv4_addr);
735 afs_com_err(pnp, code, "(failed to get status of file %s from"
736 "server %s, code = %d)", fname, ipv4_addr, code);
741 gettimeofday(&opentime, &Timezone);
743 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
744 time_elapsed(&starttime, &opentime));
745 Len = OutStatus.Length_hi;
747 Len += OutStatus.Length;
749 buf = calloc(bufflen, sizeof(char));
752 afs_com_err(pnp, code, "(cannot allocate buffer)");
757 while (!code && NonZeroInt64(length)) {
758 if (length > bufflen)
761 len = (afs_int32) length;
762 bytes = afscp_PRead(avfp, buf, len, Pos);
764 code = -3; /* what error name should we use here? */
766 MD5_Update(&md5, buf, len);
768 len = write(1, buf, len); /* to stdout */
781 gettimeofday(&readtime, &Timezone);
785 summarizeDatarate(&readtime, "read");
793 writeFile(struct cmd_syndesc *as, void *unused)
797 char *sSynthLen = NULL;
800 afs_int32 byteswritten;
801 struct AFSFetchStatus OutStatus;
802 struct AFSStoreStatus InStatus;
803 struct afscp_venusfid *dirvfp = NULL, *newvfp = NULL;
805 afs_int64 length, Len, synthlength = 0, offset = 0;
810 struct wbuf *bufchain = 0;
811 struct wbuf *previous, *tbuf;
812 char dirName[AFSPATHMAX];
813 char baseName[AFSNAMEMAX];
817 /* stdin on Windows defaults to _O_TEXT mode */
818 _setmode(0, _O_BINARY);
821 CmdProlog(as, &cell, &realm, &fname, &sSynthLen);
822 afscp_AnonymousAuth(1);
825 code = afscp_SetDefaultRealm(realm);
828 code = afscp_SetDefaultCell(cell);
831 code = util_GetInt64(sSynthLen, &synthlength);
833 afs_com_err(pnp, code, "(invalid value for synthesize length %s)",
841 code = GetVenusFidByFid(fname, cell, 1, &newvfp);
843 afs_com_err(pnp, code, "(GetVenusFidByFid returned code %d)", code);
847 code = GetVenusFidByPath(fname, cell, &newvfp);
848 if (code == 0) { /* file was found */
853 * file cannot already exist if specified by path and not
854 * appending to it unless user forces overwrite
857 afscp_FreeFid(newvfp);
858 afs_com_err(pnp, code, "(use -force to overwrite)");
861 } else { /* file not found */
864 afs_com_err(pnp, code, "(cannot append to non-existent file)");
868 if (!append && !overWrite) { /* must create a new file in this case */
869 if ( BreakUpPath(fname, dirName, baseName, AFSNAMEMAX) != 2 ) {
871 afs_com_err(pnp, code, "(must provide full AFS path)");
872 afscp_FreeFid(newvfp);
876 code = GetVenusFidByPath(dirName, cell, &dirvfp);
877 afscp_FreeFid(newvfp); /* release now-unneeded fid */
880 afs_com_err(pnp, code, "(is dir %s in AFS?)", dirName);
886 if ( (newvfp != NULL) && (newvfp->fid.Vnode & 1) ) {
888 afs_com_err(pnp, code, "(%s is a directory, not a file)", fname);
889 afscp_FreeFid(newvfp);
890 afscp_FreeFid(dirvfp);
893 gettimeofday(&starttime, &Timezone);
895 InStatus.UnixModeBits = 0644;
896 InStatus.Mask = AFS_SETMODE + AFS_FSYNC;
897 if (newvfp == NULL) {
898 code = afscp_CreateFile(dirvfp, baseName, &InStatus, &newvfp);
900 afs_com_err(pnp, code,
901 "(could not create file %s in directory %lu.%lu.%lu)",
902 baseName, afs_printable_uint32_lu(dirvfp->fid.Volume),
903 afs_printable_uint32_lu(dirvfp->fid.Vnode),
904 afs_printable_uint32_lu(dirvfp->fid.Unique));
908 code = afscp_GetStatus(newvfp, &OutStatus);
910 afs_inet_ntoa_r(newvfp->cell->fsservers[0]->addrs[0], ipv4_addr);
911 afs_com_err(pnp, code, "(failed to get status of file %s from"
912 "server %s, code = %d)", fname, ipv4_addr, code);
913 afscp_FreeFid(newvfp);
914 afscp_FreeFid(dirvfp);
918 if ( !append && !force &&
919 (OutStatus.Length != 0 || OutStatus.Length_hi !=0 ) ) {
921 * file exists, is of non-zero length, and we're not appending
922 * to it: user must force overwrite
923 * (covers fidwrite edge case)
926 afscp_FreeFid(newvfp);
927 afscp_FreeFid(dirvfp);
928 afs_com_err(pnp, code, "(use -force to overwrite)");
933 Pos = OutStatus.Length_hi;
934 Pos = (Pos << 32) | OutStatus.Length;
937 previous = (struct wbuf *)&bufchain;
942 * currently, these two while loops (1) read the whole source file in
943 * before (2) writing any of it out, meaning that afsio can't deal with
944 * files larger than the maximum amount of memory designated for
945 * reading a file in (WRITEBUFLEN).
946 * Consider going to a single loop, like in readFile(), though will
947 * have implications on timing statistics (such as the "Startup to
948 * find the file" time, below).
951 while (Len < WRITEBUFLEN) {
952 tbuf = calloc(1, sizeof(struct wbuf));
956 afscp_FreeFid(newvfp);
957 afscp_FreeFid(dirvfp);
958 afs_com_err(pnp, code, "(cannot allocate buffer)");
963 tbuf->buflen = BUFFLEN;
965 afs_int64 ll, l = tbuf->buflen;
968 for (ll = 0; ll < l; ll += 4096) {
969 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
970 (unsigned int)((offset + ll) >> 32),
971 (unsigned int)((offset + ll) & 0xffffffff));
975 tbuf->used = (afs_int32) l;
977 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
978 if (tbuf->used == 0) {
983 MD5_Update(&md5, &tbuf->buf, tbuf->used);
984 previous->next = tbuf;
988 gettimeofday(&opentime, &Timezone);
990 fprintf(stderr, "Startup to find the file took %.3f sec.\n",
991 time_elapsed(&starttime, &opentime));
993 while (!code && bytes) {
998 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1001 byteswritten = afscp_PWrite(newvfp, tbuf->buf,
1002 tbuf->used, Pos + xfered);
1003 if (byteswritten != tbuf->used) {
1004 fprintf(stderr,"Only %d instead of %" AFS_INT64_FMT " bytes transferred by rx_Write()\n", byteswritten, length);
1005 fprintf(stderr, "At %" AFS_UINT64_FMT " bytes from the end\n", length);
1009 xfered += tbuf->used;
1012 length -= tbuf->used;
1018 for (tbuf = bufchain; tbuf; tbuf = tbuf->next) {
1021 afs_int64 ll, l = tbuf->buflen;
1022 if (l > synthlength)
1024 for (ll = 0; ll < l; ll += 4096) {
1025 sprintf(&tbuf->buf[ll], "Offset (0x%x, 0x%x)\n",
1026 (unsigned int)((offset + ll) >> 32),
1027 (unsigned int)((offset + ll) & 0xffffffff));
1031 tbuf->used = (afs_int32) l;
1033 tbuf->used = read(0, &tbuf->buf, tbuf->buflen); /* from stdin */
1037 MD5_Update(&md5, &tbuf->buf, tbuf->used);
1039 bytes += tbuf->used;
1043 afscp_FreeFid(newvfp);
1044 afscp_FreeFid(dirvfp);
1046 gettimeofday(&writetime, &Timezone);
1048 afs_com_err(pnp, code, "(%s failed with code %d)", as->name,
1050 } else if (verbose) {
1051 summarizeDatarate(&writetime, "write");
1055 bufchain = tbuf->next;
1060 summarizeMD5(fname);