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>
18 #include <sys/types.h>
23 #include <WINNT/afsevent.h>
24 #include <sys/utime.h>
25 #include <afs/dirent.h>
29 #include <afs/procmgmt.h>
33 #include <netinet/in.h>
46 #include <afs/com_err.h>
47 #include <afs/cellconfig.h>
48 #include <afs/afsutil.h>
49 #include <afs/fileutil.h>
51 #include <sys/statfs.h>
60 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
61 short uid, short gid, afs_uint32 mode,
62 afs_int32 atime, afs_int32 mtime);
63 static int RenameNewFiles(struct filestr *modFiles);
64 static int PathsAreEquivalent(char *path1, char *path2);
67 GetServer(char *aname)
69 register struct hostent *th;
72 th = gethostbyname(aname);
74 printf("host %s not found \n", aname);
77 memcpy(&addr, th->h_addr, sizeof(addr));
85 /* this sucks but it works for now.
91 #include "AFS_component_version_number.c"
95 main(int argc, char **argv)
97 struct rx_connection *conn;
99 struct afsconf_dir *cdir;
101 struct rx_securityClass *sc;
104 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
109 char hostname[MAXSIZE];
111 afs_int32 time, length, atime;
115 unsigned int interval;
120 char dirbuf[MAXSIZE], filename[MAXSIZE];
121 struct filestr *dirname, *ModFiles, *okhostfiles;
124 * The following signal action for AIX is necessary so that in case of a
125 * crash (i.e. core is generated) we can include the user's data section
126 * in the core dump. Unfortunately, by default, only a partial core is
127 * generated which, in many cases, isn't too useful.
129 struct sigaction nsa;
131 sigemptyset(&nsa.sa_mask);
132 nsa.sa_handler = SIG_DFL;
133 nsa.sa_flags = SA_FULLDUMP;
134 sigaction(SIGABRT, &nsa, NULL);
135 sigaction(SIGSEGV, &nsa, NULL);
139 /* dummy signal call to force afsprocmgmt.dll to load on NT */
140 signal(SIGUSR1, SIG_DFL);
142 /* initialize winsock */
143 if (afs_winsockInit() < 0) {
144 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
145 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
150 /* Initialize dirpaths */
151 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
153 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
155 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
166 level = rxkad_crypt; /* safest default */
167 strcpy(hostname, "");
169 /* Note that IsArg only checks as many bytes as specified in the command line arg,
170 * so that, for instance, -t still matches -time.
172 for (a = 1; a < argc; a++) {
173 if (argv[a][0] == '-') { /* parse options */
174 int arglen = strlen(argv[a]);
176 lcstring(arg, argv[a], sizeof(arg));
177 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
179 interval = atol(argv[++a]);
180 else if (IsArg("-crypt"))
182 else if (IsArg("-clear"))
184 else if (IsArg("-verbose"))
189 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
192 } else if (strlen(hostname) == 0)
193 strcpy(hostname, argv[a]);
195 strcpy(filename, argv[a]);
196 FilepathNormalize(filename);
197 AddToList(&dirname, filename);
202 if (strlen(hostname) == 0)
204 host = GetServer(hostname);
205 if (interval < retrytime)
206 retrytime = interval;
210 errcode = rx_Init(0);
212 printf("Rx initialize failed \n");
213 afs_com_err(whoami, errcode, "calling Rx init");
217 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
219 fprintf(stderr, "Can't get server configuration info (%s)\n",
220 AFSDIR_SERVER_ETC_DIRPATH);
224 if (level == rxkad_crypt)
225 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
226 else if (level == rxkad_clear)
227 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
229 printf("Unsupported security level %d\n", level);
233 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
239 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
242 while (1) { /*keep doing it */
244 for (df = dirname; df; df = df->next) { /*for each directory do */
248 printf("Checking dir %s\n", df->name);
249 /* initialize lists */
251 ZapList(&okhostfiles);
253 /* construct local path from canonical (wire-format) path */
254 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
255 afs_com_err(whoami, errcode, "Unable to construct local path");
259 if (stat(curDir, &tstat) < 0) {
260 /* try to make the dir */
262 if (mkdir(curDir) < 0) {
264 if (mkdir(curDir, 0700) < 0) {
266 afs_com_err(whoami, errno, "can't create dir");
267 printf("upclient: Can't update dir %s\n", curDir);
271 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
272 printf(" file %s is not a directory; aborting\n", curDir);
276 call = rx_NewCall(conn);
278 /* scratch pad file */
279 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
281 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 */
282 error = rx_EndCall(call, 0);
283 if (error && !errcode) {
284 printf("could not end rx call \n");
285 afs_com_err(whoami, error, "calling EndCall");
290 ("warning: could not fetch the contents of directory %s \n",
292 afs_com_err(whoami, errcode, "calling FetchFile");
297 stream = fopen(dirbuf, "r");
298 if (stream == NULL) {
299 printf("fopen failed on %s \n", dirbuf);
300 afs_com_err(whoami, errno, "fopen");
305 /* while there is more info about files in file dirbuf */
307 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
308 &c1, &time, &length, &mode, &u_uid, &u_gid,
312 AddToList(&okhostfiles, filename);
313 /*record all the file names which exist on the remote
314 * sync site, to enable purging of redundant files */
316 printf(" checking %s\n", filename);
317 if (!IsCompatible(filename, time, length)) {
318 /* if the file info has changed , record all the
319 *changed files in the ModFiles array*/
321 printf(" getting %s\n", filename);
322 AddToList(&ModFiles, filename);
324 /* now get the file from the server. The received
325 * file is created under the name filename.NEW */
327 GetFileFromUpServer(conn, filename, uid, gid, mode,
329 if (errcode == 1) /* this file failed, but keep trying */
331 if (errcode == -1) { /* time to quit */
342 { /*delete all the redundant files on the client */
345 char filename[MAXSIZE];
347 dirp = opendir(curDir);
349 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
353 while ((dp = readdir(dirp))) {
354 /* for all the files in the directory df->name do */
355 strcpy(filename, curDir);
356 strcat(filename, "/");
357 strcat(filename, dp->d_name);
358 /* if the file filename is redundant, delete it */
359 errcode = NotOnHost(filename, okhostfiles);
364 printf(" flushing %s\n", filename);
365 errcode = unlink(filename);
367 printf("could not delete file %s \n", filename);
368 afs_com_err(whoami, errno, "could not delete file %s",
375 /* Now, rename the .NEW files created by FetchFile */
376 if (RenameNewFiles(ModFiles))
380 } /* end for each dir loop */
381 /*delete the file with info on files in directory df->name */
382 IOMGR_Sleep(interval);
389 IOMGR_Sleep(retrytime);
391 rx_DestroyConnection(conn);
394 /* start the cycle again */
399 /* returns 1 if the file is upto date else returns 0*/
400 /*check the dir case more carefully */
402 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
408 /* construct a local path from canonical (wire-format) path */
409 if ((error = ConstructLocalPath(filename, "/", &localname))) {
410 afs_com_err(whoami, error, "Unable to construct local path");
414 error = stat(localname, &status);
419 return 0; /*a non-existent file, has to be fetched fresh */
420 if ((status.st_mode & S_IFMT) == S_IFDIR
421 || ((status.st_mtime == time) && (status.st_size == length)))
428 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
430 int fd = -1, error = 0;
434 if (StartUPDATE_FetchInfo(call, remoteFile))
437 if (StartUPDATE_FetchFile(call, remoteFile))
440 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
442 printf("Could not create %s\n", localFile);
443 afs_com_err(whoami, errno, "Could not create %s", localFile);
444 error = UPDATE_ERROR;
447 if (fstat(fd, &status) < 0) {
448 afs_com_err(whoami, errno, "Could not stat %s", localFile);
450 printf("could not stat %s\n", localFile);
453 if (update_ReceiveFile(fd, call, &status))
454 error = UPDATE_ERROR;
464 update_ReceiveFile(register int fd, register struct rx_call *call, register struct stat *status)
466 register char *buffer = (char *)0;
468 register int blockSize;
469 afs_int32 error = 0, len;
471 struct statfs tstatfs;
474 len = rx_Read(call, &length, sizeof(afs_int32));
475 length = ntohl(length);
476 if (len != sizeof(afs_int32))
479 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
480 fstatfs(fd, &tstatfs);
481 blockSize = tstatfs.f_bsize;
485 blockSize = status->st_blksize;
487 buffer = (char *)malloc(blockSize);
489 printf("malloc failed\n");
492 while (!error && length) {
493 register int nbytes = (length > blockSize ? blockSize : length);
494 nbytes = rx_Read(call, buffer, nbytes);
496 error = UPDATE_ERROR;
497 if (write(fd, buffer, nbytes) != nbytes) {
498 afs_com_err(whoami, errno, "File system write failed!");
499 printf("File system write failed!\n");
500 error = UPDATE_ERROR;
513 * PathsAreEquivalent() -- determine if paths are equivalent
516 PathsAreEquivalent(char *path1, char *path2)
519 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
522 /* case-insensitive comparison of normalized, same-flavor (short) paths */
525 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
526 if (status == 0 || status > AFSDIR_PATH_MAX) {
527 /* can't convert path to short version; just use long version */
528 strcpy(pathNorm1, path1);
530 FilepathNormalize(pathNorm1);
532 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
533 if (status == 0 || status > AFSDIR_PATH_MAX) {
534 /* can't convert path to short version; just use long version */
535 strcpy(pathNorm2, path2);
537 FilepathNormalize(pathNorm2);
539 if (_stricmp(pathNorm1, pathNorm2) == 0) {
543 /* case-sensitive comparison of normalized paths */
544 strcpy(pathNorm1, path1);
545 FilepathNormalize(pathNorm1);
547 strcpy(pathNorm2, path2);
548 FilepathNormalize(pathNorm2);
550 if (strcmp(pathNorm1, pathNorm2) == 0) {
553 #endif /* AFS_NT40_ENV */
559 /* returns 1 if filename does not exist on the host site (=> it should be
560 * deleted on client site) else it returns 0 */
563 NotOnHost(char *filename, struct filestr *okhostfiles)
570 stat(filename, &status);
572 if ((status.st_mode & S_IFMT) == S_IFDIR)
574 i = strlen(filename);
575 if (!strcmp(&filename[i - 4], ".NEW"))
578 for (tf = okhostfiles; tf; tf = tf->next) {
579 /* construct local path from canonical (wire-format) path */
580 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
581 afs_com_err(whoami, rc, "Unable to construct local path");
584 if (PathsAreEquivalent(hostfile, filename)) {
594 /* RenameNewFiles() - renames all the newly copied files from the
595 * server. Looks for files with .NEW extension and renames them
598 RenameNewFiles(struct filestr *modFiles)
600 char newname[MAXSIZE];
605 for (tf = modFiles; tf; tf = tf->next) {
606 /* construct local path from canonical (wire-format) path */
607 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
608 afs_com_err(whoami, errcode, "Unable to construct local path");
611 strcpy(newname, fname);
612 strcat(newname, ".NEW");
614 printf(" renaming %s\n", newname);
615 errcode = renamefile(newname, fname);
617 printf("could not rename %s to %s\n", newname, fname);
618 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
628 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
629 * file from the upserver.
632 * -1 - Serious error. Quit right away.
633 * 1 - Error, but keep trying for the other files
635 * The file obtained is written to the localized version of the filename.NEW
636 * and the uid, gid, file mode, access and modification times will be set to
637 * the passed in values.
640 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
641 char *filename, /* name of file to be fetched */
642 short uid, short gid, /* uid/gid for fetched file */
643 afs_uint32 mode, /* file mode */
644 afs_int32 atime, afs_int32 mtime)
645 { /* access/modification times */
646 struct rx_call *call;
650 struct _utimbuf utbuf;
652 struct timeval tvp[2];
654 char newfile[MAXSIZE];
656 /* construct local path from canonical (wire-format) path */
657 errcode = ConstructLocalPath(filename, "/", &lfile);
659 afs_com_err(whoami, errcode, "Unable to construct local path");
662 strcpy(newfile, lfile);
665 strcat(newfile, ".NEW");
667 /* fetch filename into newfile from the host, since the current file
668 * is outdated. the new versions of changed files is stored as
670 call = rx_NewCall(conn);
671 errcode = FetchFile(call, filename, newfile, 0);
672 errcode = rx_EndCall(call, errcode);
675 printf("failed to fetch file %s \n", filename);
676 afs_com_err(whoami, errcode, "fetching file");
681 /* now set the rest of the file status */
682 errcode = chmod(newfile, mode);
684 printf("could not change protection on %s to %u\n", newfile,
686 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);