2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
20 #include <sys/types.h>
25 #include <WINNT/afsevent.h>
26 #include <sys/utime.h>
27 #include <afs/dirent.h>
31 #include <afs/procmgmt.h>
35 #include <netinet/in.h>
54 #include <afs/com_err.h>
55 #include <afs/cellconfig.h>
56 #include <afs/afsutil.h>
57 #include <afs/fileutil.h>
59 #include <sys/statfs.h>
68 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
69 short uid, short gid, afs_uint32 mode,
70 afs_int32 atime, afs_int32 mtime);
71 static int RenameNewFiles(struct filestr *modFiles);
72 static int PathsAreEquivalent(char *path1, char *path2);
75 GetServer(char *aname)
77 register struct hostent *th;
80 th = gethostbyname(aname);
82 printf("host %s not found \n", aname);
85 memcpy(&addr, th->h_addr, sizeof(addr));
93 /* this sucks but it works for now.
99 #include "AFS_component_version_number.c"
103 main(int argc, char **argv)
105 struct rx_connection *conn;
106 struct rx_call *call;
107 struct afsconf_dir *cdir;
109 struct rx_securityClass *sc;
112 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
117 char hostname[MAXSIZE];
119 afs_int32 time, length, atime;
123 unsigned int interval;
128 char dirbuf[MAXSIZE], filename[MAXSIZE];
129 struct filestr *dirname, *ModFiles, *okhostfiles;
132 * The following signal action for AIX is necessary so that in case of a
133 * crash (i.e. core is generated) we can include the user's data section
134 * in the core dump. Unfortunately, by default, only a partial core is
135 * generated which, in many cases, isn't too useful.
137 struct sigaction nsa;
139 sigemptyset(&nsa.sa_mask);
140 nsa.sa_handler = SIG_DFL;
141 nsa.sa_flags = SA_FULLDUMP;
142 sigaction(SIGABRT, &nsa, NULL);
143 sigaction(SIGSEGV, &nsa, NULL);
147 /* dummy signal call to force afsprocmgmt.dll to load on NT */
148 signal(SIGUSR1, SIG_DFL);
150 /* initialize winsock */
151 if (afs_winsockInit() < 0) {
152 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
153 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
158 /* Initialize dirpaths */
159 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
161 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
163 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
174 level = rxkad_crypt; /* safest default */
175 strcpy(hostname, "");
177 /* Note that IsArg only checks as many bytes as specified in the command line arg,
178 * so that, for instance, -t still matches -time.
180 for (a = 1; a < argc; a++) {
181 if (argv[a][0] == '-') { /* parse options */
182 int arglen = strlen(argv[a]);
184 lcstring(arg, argv[a], sizeof(arg));
185 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
187 interval = atol(argv[++a]);
188 else if (IsArg("-crypt"))
190 else if (IsArg("-clear"))
192 else if (IsArg("-verbose"))
197 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
200 } else if (strlen(hostname) == 0)
201 strcpy(hostname, argv[a]);
203 strcpy(filename, argv[a]);
204 FilepathNormalize(filename);
205 AddToList(&dirname, filename);
210 if (strlen(hostname) == 0)
212 host = GetServer(hostname);
213 if (interval < retrytime)
214 retrytime = interval;
218 errcode = rx_Init(0);
220 printf("Rx initialize failed \n");
221 afs_com_err(whoami, errcode, "calling Rx init");
225 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
227 fprintf(stderr, "Can't get server configuration info (%s)\n",
228 AFSDIR_SERVER_ETC_DIRPATH);
232 if (level == rxkad_crypt)
233 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
234 else if (level == rxkad_clear)
235 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
237 printf("Unsupported security level %d\n", level);
241 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
247 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
250 while (1) { /*keep doing it */
252 for (df = dirname; df; df = df->next) { /*for each directory do */
256 printf("Checking dir %s\n", df->name);
257 /* initialize lists */
259 ZapList(&okhostfiles);
261 /* construct local path from canonical (wire-format) path */
262 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
263 afs_com_err(whoami, errcode, "Unable to construct local path");
267 if (stat(curDir, &tstat) < 0) {
268 /* try to make the dir */
270 if (mkdir(curDir) < 0) {
272 if (mkdir(curDir, 0700) < 0) {
274 afs_com_err(whoami, errno, "can't create dir");
275 printf("upclient: Can't update dir %s\n", curDir);
279 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
280 printf(" file %s is not a directory; aborting\n", curDir);
284 call = rx_NewCall(conn);
286 /* scratch pad file */
287 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
289 errcode = FetchFile(call, df->name, dirbuf, 1); /* get the names and relevant info about all the files in the directory df->name into file dirbuf */
290 error = rx_EndCall(call, 0);
291 if (error && !errcode) {
292 printf("could not end rx call \n");
293 afs_com_err(whoami, error, "calling EndCall");
298 ("warning: could not fetch the contents of directory %s \n",
300 afs_com_err(whoami, errcode, "calling FetchFile");
305 stream = fopen(dirbuf, "r");
306 if (stream == NULL) {
307 printf("fopen failed on %s \n", dirbuf);
308 afs_com_err(whoami, errno, "fopen");
313 /* while there is more info about files in file dirbuf */
315 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
316 &c1, &time, &length, &mode, &u_uid, &u_gid,
320 AddToList(&okhostfiles, filename);
321 /*record all the file names which exist on the remote
322 * sync site, to enable purging of redundant files */
324 printf(" checking %s\n", filename);
325 if (!IsCompatible(filename, time, length)) {
326 /* if the file info has changed , record all the
327 *changed files in the ModFiles array*/
329 printf(" getting %s\n", filename);
330 AddToList(&ModFiles, filename);
332 /* now get the file from the server. The received
333 * file is created under the name filename.NEW */
335 GetFileFromUpServer(conn, filename, uid, gid, mode,
337 if (errcode == 1) /* this file failed, but keep trying */
339 if (errcode == -1) { /* time to quit */
350 { /*delete all the redundant files on the client */
353 char filename[MAXSIZE];
355 dirp = opendir(curDir);
357 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
361 while ((dp = readdir(dirp))) {
362 /* for all the files in the directory df->name do */
363 strcpy(filename, curDir);
364 strcat(filename, "/");
365 strcat(filename, dp->d_name);
366 /* if the file filename is redundant, delete it */
367 errcode = NotOnHost(filename, okhostfiles);
372 printf(" flushing %s\n", filename);
373 errcode = unlink(filename);
375 printf("could not delete file %s \n", filename);
376 afs_com_err(whoami, errno, "could not delete file %s",
383 /* Now, rename the .NEW files created by FetchFile */
384 if (RenameNewFiles(ModFiles))
388 } /* end for each dir loop */
389 /*delete the file with info on files in directory df->name */
390 IOMGR_Sleep(interval);
397 IOMGR_Sleep(retrytime);
399 rx_DestroyConnection(conn);
402 /* start the cycle again */
407 /* returns 1 if the file is upto date else returns 0*/
408 /*check the dir case more carefully */
410 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
416 /* construct a local path from canonical (wire-format) path */
417 if ((error = ConstructLocalPath(filename, "/", &localname))) {
418 afs_com_err(whoami, error, "Unable to construct local path");
422 error = stat(localname, &status);
427 return 0; /*a non-existent file, has to be fetched fresh */
428 if ((status.st_mode & S_IFMT) == S_IFDIR
429 || ((status.st_mtime == time) && (status.st_size == length)))
436 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
438 int fd = -1, error = 0;
442 if (StartUPDATE_FetchInfo(call, remoteFile))
445 if (StartUPDATE_FetchFile(call, remoteFile))
448 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
450 printf("Could not create %s\n", localFile);
451 afs_com_err(whoami, errno, "Could not create %s", localFile);
452 error = UPDATE_ERROR;
455 if (fstat(fd, &status) < 0) {
456 afs_com_err(whoami, errno, "Could not stat %s", localFile);
458 printf("could not stast %s\n", localFile);
461 if (update_ReceiveFile(fd, call, &status))
462 error = UPDATE_ERROR;
472 update_ReceiveFile(register int fd, register struct rx_call *call, register struct stat *status)
474 register char *buffer = (char *)0;
476 register int blockSize;
477 afs_int32 error = 0, len;
479 struct statfs tstatfs;
482 len = rx_Read(call, &length, sizeof(afs_int32));
483 length = ntohl(length);
484 if (len != sizeof(afs_int32))
487 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
488 fstatfs(fd, &tstatfs);
489 blockSize = tstatfs.f_bsize;
493 blockSize = status->st_blksize;
495 buffer = (char *)malloc(blockSize);
497 printf("malloc failed\n");
500 while (!error && length) {
501 register int nbytes = (length > blockSize ? blockSize : length);
502 nbytes = rx_Read(call, buffer, nbytes);
504 error = UPDATE_ERROR;
505 if (write(fd, buffer, nbytes) != nbytes) {
506 afs_com_err(whoami, errno, "File system write failed!");
507 printf("File system write failed!\n");
508 error = UPDATE_ERROR;
521 * PathsAreEquivalent() -- determine if paths are equivalent
524 PathsAreEquivalent(char *path1, char *path2)
527 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
530 /* case-insensitive comparison of normalized, same-flavor (short) paths */
533 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
534 if (status == 0 || status > AFSDIR_PATH_MAX) {
535 /* can't convert path to short version; just use long version */
536 strcpy(pathNorm1, path1);
538 FilepathNormalize(pathNorm1);
540 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
541 if (status == 0 || status > AFSDIR_PATH_MAX) {
542 /* can't convert path to short version; just use long version */
543 strcpy(pathNorm2, path2);
545 FilepathNormalize(pathNorm2);
547 if (_stricmp(pathNorm1, pathNorm2) == 0) {
551 /* case-sensitive comparison of normalized paths */
552 strcpy(pathNorm1, path1);
553 FilepathNormalize(pathNorm1);
555 strcpy(pathNorm2, path2);
556 FilepathNormalize(pathNorm2);
558 if (strcmp(pathNorm1, pathNorm2) == 0) {
561 #endif /* AFS_NT40_ENV */
567 /* returns 1 if filename does not exist on the host site (=> it should be
568 * deleted on client site) else it returns 0 */
571 NotOnHost(char *filename, struct filestr *okhostfiles)
578 stat(filename, &status);
580 if ((status.st_mode & S_IFMT) == S_IFDIR)
582 i = strlen(filename);
583 if (!strcmp(&filename[i - 4], ".NEW"))
586 for (tf = okhostfiles; tf; tf = tf->next) {
587 /* construct local path from canonical (wire-format) path */
588 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
589 afs_com_err(whoami, rc, "Unable to construct local path");
592 if (PathsAreEquivalent(hostfile, filename)) {
602 /* RenameNewFiles() - renames all the newly copied files from the
603 * server. Looks for files with .NEW extension and renames them
606 RenameNewFiles(struct filestr *modFiles)
608 char newname[MAXSIZE];
613 for (tf = modFiles; tf; tf = tf->next) {
614 /* construct local path from canonical (wire-format) path */
615 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
616 afs_com_err(whoami, errcode, "Unable to construct local path");
619 strcpy(newname, fname);
620 strcat(newname, ".NEW");
622 printf(" renaming %s\n", newname);
623 errcode = renamefile(newname, fname);
625 printf("could not rename %s to %s\n", newname, fname);
626 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
636 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
637 * file from the upserver.
640 * -1 - Serious error. Quit right away.
641 * 1 - Error, but keep trying for the other files
643 * The file obtained is written to the localized version of the filename.NEW
644 * and the uid, gid, file mode, access and modification times will be set to
645 * the passed in values.
648 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
649 char *filename, /* name of file to be fetched */
650 short uid, short gid, /* uid/gid for fetched file */
651 afs_uint32 mode, /* file mode */
652 afs_int32 atime, afs_int32 mtime)
653 { /* access/modification times */
654 struct rx_call *call;
658 struct _utimbuf utbuf;
660 struct timeval tvp[2];
662 char newfile[MAXSIZE];
664 /* construct local path from canonical (wire-format) path */
665 errcode = ConstructLocalPath(filename, "/", &lfile);
667 afs_com_err(whoami, errcode, "Unable to construct local path");
670 strcpy(newfile, lfile);
673 strcat(newfile, ".NEW");
675 /* fetch filename into newfile from the host, since the current file
676 * is outdated. the new versions of changed files is stored as
678 call = rx_NewCall(conn);
679 errcode = FetchFile(call, filename, newfile, 0);
680 errcode = rx_EndCall(call, errcode);
683 printf("failed to fetch file %s \n", filename);
684 afs_com_err(whoami, errcode, "fetching file");
688 /* now set the rest of the file status */
689 errcode = chmod(newfile, mode);
691 printf("could not change protection on %s to %u\n", newfile,
693 afs_com_err(whoami, errno, "could not change protection on %s to %u",
698 utbuf.actime = atime;
699 utbuf.modtime = mtime;
700 errcode = _utime(newfile, &utbuf);
702 errcode = chown(newfile, uid, gid);
704 printf("warning: could not change uid and gid on %s to %u and %u \n",
706 afs_com_err(whoami, errno,
707 "warning: could not change uid and gid on %s to %u and %u",
710 tvp[0].tv_sec = atime;
712 tvp[1].tv_sec = mtime;
714 errcode = utimes(newfile, tvp);
717 printf("could not change access and modify times on %s to %u %u\n",
718 newfile, (unsigned int)atime, (unsigned int)mtime);
719 afs_com_err(whoami, errno,
720 "could not change access and modify times on %s to %u %u",
721 newfile, atime, mtime);