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>
48 #include <afs/com_err.h>
49 #include <afs/cellconfig.h>
50 #include <afs/afsutil.h>
51 #include <afs/fileutil.h>
53 #include <sys/statfs.h>
62 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
63 short uid, short gid, afs_uint32 mode,
64 afs_int32 atime, afs_int32 mtime);
65 static int RenameNewFiles(struct filestr *modFiles);
66 static int PathsAreEquivalent(char *path1, char *path2);
69 GetServer(char *aname)
71 register struct hostent *th;
74 th = gethostbyname(aname);
76 printf("host %s not found \n", aname);
79 memcpy(&addr, th->h_addr, sizeof(addr));
87 /* this sucks but it works for now.
93 #include "AFS_component_version_number.c"
97 main(int argc, char **argv)
99 struct rx_connection *conn;
100 struct rx_call *call;
101 struct afsconf_dir *cdir;
103 struct rx_securityClass *sc;
106 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
111 char hostname[MAXSIZE];
113 afs_int32 time, length, atime;
117 unsigned int interval;
122 char dirbuf[MAXSIZE], filename[MAXSIZE];
123 struct filestr *dirname, *ModFiles, *okhostfiles;
126 * The following signal action for AIX is necessary so that in case of a
127 * crash (i.e. core is generated) we can include the user's data section
128 * in the core dump. Unfortunately, by default, only a partial core is
129 * generated which, in many cases, isn't too useful.
131 struct sigaction nsa;
133 sigemptyset(&nsa.sa_mask);
134 nsa.sa_handler = SIG_DFL;
135 nsa.sa_flags = SA_FULLDUMP;
136 sigaction(SIGABRT, &nsa, NULL);
137 sigaction(SIGSEGV, &nsa, NULL);
141 /* dummy signal call to force afsprocmgmt.dll to load on NT */
142 signal(SIGUSR1, SIG_DFL);
144 /* initialize winsock */
145 if (afs_winsockInit() < 0) {
146 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
147 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
152 /* Initialize dirpaths */
153 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
155 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
157 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
168 level = rxkad_crypt; /* safest default */
169 strcpy(hostname, "");
171 /* Note that IsArg only checks as many bytes as specified in the command line arg,
172 * so that, for instance, -t still matches -time.
174 for (a = 1; a < argc; a++) {
175 if (argv[a][0] == '-') { /* parse options */
176 int arglen = strlen(argv[a]);
178 lcstring(arg, argv[a], sizeof(arg));
179 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
181 interval = atol(argv[++a]);
182 else if (IsArg("-crypt"))
184 else if (IsArg("-clear"))
186 else if (IsArg("-verbose"))
191 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
194 } else if (strlen(hostname) == 0)
195 strcpy(hostname, argv[a]);
197 strcpy(filename, argv[a]);
198 FilepathNormalize(filename);
199 AddToList(&dirname, filename);
204 if (strlen(hostname) == 0)
206 host = GetServer(hostname);
207 if (interval < retrytime)
208 retrytime = interval;
212 errcode = rx_Init(0);
214 printf("Rx initialize failed \n");
215 afs_com_err(whoami, errcode, "calling Rx init");
219 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
221 fprintf(stderr, "Can't get server configuration info (%s)\n",
222 AFSDIR_SERVER_ETC_DIRPATH);
226 if (level == rxkad_crypt)
227 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
228 else if (level == rxkad_clear)
229 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
231 printf("Unsupported security level %d\n", level);
235 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
241 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
244 while (1) { /*keep doing it */
246 for (df = dirname; df; df = df->next) { /*for each directory do */
250 printf("Checking dir %s\n", df->name);
251 /* initialize lists */
253 ZapList(&okhostfiles);
255 /* construct local path from canonical (wire-format) path */
256 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
257 afs_com_err(whoami, errcode, "Unable to construct local path");
261 if (stat(curDir, &tstat) < 0) {
262 /* try to make the dir */
264 if (mkdir(curDir) < 0) {
266 if (mkdir(curDir, 0700) < 0) {
268 afs_com_err(whoami, errno, "can't create dir");
269 printf("upclient: Can't update dir %s\n", curDir);
273 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
274 printf(" file %s is not a directory; aborting\n", curDir);
278 call = rx_NewCall(conn);
280 /* scratch pad file */
281 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
283 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 */
284 error = rx_EndCall(call, 0);
285 if (error && !errcode) {
286 printf("could not end rx call \n");
287 afs_com_err(whoami, error, "calling EndCall");
292 ("warning: could not fetch the contents of directory %s \n",
294 afs_com_err(whoami, errcode, "calling FetchFile");
299 stream = fopen(dirbuf, "r");
300 if (stream == NULL) {
301 printf("fopen failed on %s \n", dirbuf);
302 afs_com_err(whoami, errno, "fopen");
307 /* while there is more info about files in file dirbuf */
309 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
310 &c1, &time, &length, &mode, &u_uid, &u_gid,
314 AddToList(&okhostfiles, filename);
315 /*record all the file names which exist on the remote
316 * sync site, to enable purging of redundant files */
318 printf(" checking %s\n", filename);
319 if (!IsCompatible(filename, time, length)) {
320 /* if the file info has changed , record all the
321 *changed files in the ModFiles array*/
323 printf(" getting %s\n", filename);
324 AddToList(&ModFiles, filename);
326 /* now get the file from the server. The received
327 * file is created under the name filename.NEW */
329 GetFileFromUpServer(conn, filename, uid, gid, mode,
331 if (errcode == 1) /* this file failed, but keep trying */
333 if (errcode == -1) { /* time to quit */
344 { /*delete all the redundant files on the client */
347 char filename[MAXSIZE];
349 dirp = opendir(curDir);
351 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
355 while ((dp = readdir(dirp))) {
356 /* for all the files in the directory df->name do */
357 strcpy(filename, curDir);
358 strcat(filename, "/");
359 strcat(filename, dp->d_name);
360 /* if the file filename is redundant, delete it */
361 errcode = NotOnHost(filename, okhostfiles);
366 printf(" flushing %s\n", filename);
367 errcode = unlink(filename);
369 printf("could not delete file %s \n", filename);
370 afs_com_err(whoami, errno, "could not delete file %s",
377 /* Now, rename the .NEW files created by FetchFile */
378 if (RenameNewFiles(ModFiles))
382 } /* end for each dir loop */
383 /*delete the file with info on files in directory df->name */
384 IOMGR_Sleep(interval);
391 IOMGR_Sleep(retrytime);
393 rx_DestroyConnection(conn);
396 /* start the cycle again */
401 /* returns 1 if the file is upto date else returns 0*/
402 /*check the dir case more carefully */
404 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
410 /* construct a local path from canonical (wire-format) path */
411 if ((error = ConstructLocalPath(filename, "/", &localname))) {
412 afs_com_err(whoami, error, "Unable to construct local path");
416 error = stat(localname, &status);
421 return 0; /*a non-existent file, has to be fetched fresh */
422 if ((status.st_mode & S_IFMT) == S_IFDIR
423 || ((status.st_mtime == time) && (status.st_size == length)))
430 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
432 int fd = -1, error = 0;
436 if (StartUPDATE_FetchInfo(call, remoteFile))
439 if (StartUPDATE_FetchFile(call, remoteFile))
442 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
444 printf("Could not create %s\n", localFile);
445 afs_com_err(whoami, errno, "Could not create %s", localFile);
446 error = UPDATE_ERROR;
449 if (fstat(fd, &status) < 0) {
450 afs_com_err(whoami, errno, "Could not stat %s", localFile);
452 printf("could not stast %s\n", localFile);
455 if (update_ReceiveFile(fd, call, &status))
456 error = UPDATE_ERROR;
466 update_ReceiveFile(register int fd, register struct rx_call *call, register struct stat *status)
468 register char *buffer = (char *)0;
470 register int blockSize;
471 afs_int32 error = 0, len;
473 struct statfs tstatfs;
476 len = rx_Read(call, &length, sizeof(afs_int32));
477 length = ntohl(length);
478 if (len != sizeof(afs_int32))
481 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
482 fstatfs(fd, &tstatfs);
483 blockSize = tstatfs.f_bsize;
487 blockSize = status->st_blksize;
489 buffer = (char *)malloc(blockSize);
491 printf("malloc failed\n");
494 while (!error && length) {
495 register int nbytes = (length > blockSize ? blockSize : length);
496 nbytes = rx_Read(call, buffer, nbytes);
498 error = UPDATE_ERROR;
499 if (write(fd, buffer, nbytes) != nbytes) {
500 afs_com_err(whoami, errno, "File system write failed!");
501 printf("File system write failed!\n");
502 error = UPDATE_ERROR;
515 * PathsAreEquivalent() -- determine if paths are equivalent
518 PathsAreEquivalent(char *path1, char *path2)
521 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
524 /* case-insensitive comparison of normalized, same-flavor (short) paths */
527 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
528 if (status == 0 || status > AFSDIR_PATH_MAX) {
529 /* can't convert path to short version; just use long version */
530 strcpy(pathNorm1, path1);
532 FilepathNormalize(pathNorm1);
534 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
535 if (status == 0 || status > AFSDIR_PATH_MAX) {
536 /* can't convert path to short version; just use long version */
537 strcpy(pathNorm2, path2);
539 FilepathNormalize(pathNorm2);
541 if (_stricmp(pathNorm1, pathNorm2) == 0) {
545 /* case-sensitive comparison of normalized paths */
546 strcpy(pathNorm1, path1);
547 FilepathNormalize(pathNorm1);
549 strcpy(pathNorm2, path2);
550 FilepathNormalize(pathNorm2);
552 if (strcmp(pathNorm1, pathNorm2) == 0) {
555 #endif /* AFS_NT40_ENV */
561 /* returns 1 if filename does not exist on the host site (=> it should be
562 * deleted on client site) else it returns 0 */
565 NotOnHost(char *filename, struct filestr *okhostfiles)
572 stat(filename, &status);
574 if ((status.st_mode & S_IFMT) == S_IFDIR)
576 i = strlen(filename);
577 if (!strcmp(&filename[i - 4], ".NEW"))
580 for (tf = okhostfiles; tf; tf = tf->next) {
581 /* construct local path from canonical (wire-format) path */
582 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
583 afs_com_err(whoami, rc, "Unable to construct local path");
586 if (PathsAreEquivalent(hostfile, filename)) {
596 /* RenameNewFiles() - renames all the newly copied files from the
597 * server. Looks for files with .NEW extension and renames them
600 RenameNewFiles(struct filestr *modFiles)
602 char newname[MAXSIZE];
607 for (tf = modFiles; tf; tf = tf->next) {
608 /* construct local path from canonical (wire-format) path */
609 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
610 afs_com_err(whoami, errcode, "Unable to construct local path");
613 strcpy(newname, fname);
614 strcat(newname, ".NEW");
616 printf(" renaming %s\n", newname);
617 errcode = renamefile(newname, fname);
619 printf("could not rename %s to %s\n", newname, fname);
620 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
630 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
631 * file from the upserver.
634 * -1 - Serious error. Quit right away.
635 * 1 - Error, but keep trying for the other files
637 * The file obtained is written to the localized version of the filename.NEW
638 * and the uid, gid, file mode, access and modification times will be set to
639 * the passed in values.
642 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
643 char *filename, /* name of file to be fetched */
644 short uid, short gid, /* uid/gid for fetched file */
645 afs_uint32 mode, /* file mode */
646 afs_int32 atime, afs_int32 mtime)
647 { /* access/modification times */
648 struct rx_call *call;
652 struct _utimbuf utbuf;
654 struct timeval tvp[2];
656 char newfile[MAXSIZE];
658 /* construct local path from canonical (wire-format) path */
659 errcode = ConstructLocalPath(filename, "/", &lfile);
661 afs_com_err(whoami, errcode, "Unable to construct local path");
664 strcpy(newfile, lfile);
667 strcat(newfile, ".NEW");
669 /* fetch filename into newfile from the host, since the current file
670 * is outdated. the new versions of changed files is stored as
672 call = rx_NewCall(conn);
673 errcode = FetchFile(call, filename, newfile, 0);
674 errcode = rx_EndCall(call, errcode);
677 printf("failed to fetch file %s \n", filename);
678 afs_com_err(whoami, errcode, "fetching file");
682 /* now set the rest of the file status */
683 errcode = chmod(newfile, mode);
685 printf("could not change protection on %s to %u\n", newfile,
687 afs_com_err(whoami, errno, "could not change protection on %s to %u",
692 utbuf.actime = atime;
693 utbuf.modtime = mtime;
694 errcode = _utime(newfile, &utbuf);
696 errcode = chown(newfile, uid, gid);
698 printf("warning: could not change uid and gid on %s to %u and %u \n",
700 afs_com_err(whoami, errno,
701 "warning: could not change uid and gid on %s to %u and %u",
704 tvp[0].tv_sec = atime;
706 tvp[1].tv_sec = mtime;
708 errcode = utimes(newfile, tvp);
711 printf("could not change access and modify times on %s to %u %u\n",
712 newfile, (unsigned int)atime, (unsigned int)mtime);
713 afs_com_err(whoami, errno,
714 "could not change access and modify times on %s to %u %u",
715 newfile, atime, mtime);