sync.c
来自「ftam等标准协议服务器和客户端的源代码。」· C语言 代码 · 共 653 行 · 第 1/2 页
C
653 行
/* * RFA - Remote File Access * * Access and Management for a partial file system tree that exists * at two sites either as master files or slave files * * sync.c synchronize local dir with remote site * * Contributed by Oliver Wenzel, GMD Berlin, 1990 * * $Header: /xtel/isode/isode/others/rfa/RCS/sync.c,v 9.0 1992/06/16 12:47:25 isode Rel $ * * $Log: sync.c,v $ * Revision 9.0 1992/06/16 12:47:25 isode * Release 8.0 * */#ifndef lintstatic char *rcsid = "$Header: /xtel/isode/isode/others/rfa/RCS/sync.c,v 9.0 1992/06/16 12:47:25 isode Rel $";#endif/* * NOTICE * * Acquisition, use, and distribution of this module and related * materials are subject to the restrictions of a license agreement. * Consult the Preface in the User's Manual for the full terms of * this agreement. * */#include <ctype.h>#include "general.h" #include <stdio.h>#include <pwd.h>#include <unistd.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <fcntl.h>#include <utime.h>#include "RFA-ops.h" #include "RFA-types.h" #include "rfa.h"#include "rfainfo.h"extern FILE *err, *out;extern int interactive;char *incstr = "\t*** INCONSISTENCY : ";/*--------------------------------------------------------------*//* createEmptyFile *//*--------------------------------------------------------------*/int createEmptyFile(dir, rfa) char *dir; struct RfaInfo *rfa;{ int fd; char buf[BUFSIZ]; char fn[BUFSIZ]; struct utimbuf utb; sprintf(fn, "%s%s%s", dir, *(dir+strlen(dir)-1) == '/' ? "" : "/", rfa->ri_filename); /*-- create empty file for now --*/ if ((fd = open(makeFN(fn), O_WRONLY | O_CREAT, rfa->ri_mode & 07555)) == -1) { fprintf(err,"\t*** can't create local SLAVE for %s (%s) ***\n" , fn, sys_errname(errno)); return NOTOK; } strcpy(buf,"This file has been created by RFA as a dummy."); strcat(buf," Use RFA commands 'get' and 'setauto'\nto retrieve"); strcat(buf," the actual content of this file\n"); if (write(fd, buf, strlen(buf)) == 0) { fprintf(err,"\t*** can't write local SLAVE for %s (%s) ***\n" , fn, sys_errname(errno)); unlink(makeFN(fn)); close(fd); return NOTOK; } close(fd); utb.modtime = rfa->ri_modTime - 60; utb.actime = rfa->ri_accTime - 60; if (utime(makeFN(fn), &utb) == -1) { fprintf(err,"\t*** can't create local SLAVE for %s (%s) ***\n" , fn, sys_errname(errno)); unlink(makeFN(fn)); return NOTOK; } changeFileOwner(fn, rfa); return OK;}/*--------------------------------------------------------------*//* removeDir - check if dir is empty and remove it *//*--------------------------------------------------------------*/int removeDir(dir) char *dir;{ struct dirent *dp; DIR *dirp; /*--- dir is a directory name, so open dir ---*/ if ((dirp = opendir(makeFN(dir))) == NULL) { fprintf(err, "*** can't open %s - %s ***", dir, sys_errname(errno)); return NOTOK; } for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if (strncmp(dp->d_name, "..", 2) && strncmp(dp->d_name, ".", 1) && strncmp(dp->d_name, ".rfainfo", 8)) { closedir(dirp); fprintf(err, "%scan't remove %s - not empty ***\n", incstr, dir); return NOTOK; } } closedir(dirp); fprintf(err,"\tremoved local SLAVE directory '%s'\n", dir); return OK;}/*--------------------------------------------------------------*//* checkState - check state of files and perform actions to *//* resolve inconsistencies if possible *//*--------------------------------------------------------------*/int checkState(rfa, rrfa, dir, wrp) struct RfaInfo *rfa, *rrfa; char *dir; int *wrp;{ char fn[BUFSIZ]; if ((rfa == NULL) && (rrfa == NULL)) return OK; sprintf(fn, "%s%s%s", dir, *(dir+strlen(dir)-1) == '/' ? "" : "/", rfa ? rfa->ri_filename : rrfa->ri_filename); /*-- checks if file exists only local --*/ if (rrfa == NULL) switch (RI_STATUS(rfa->ri_status)) { case RI_MASTER: case RI_UNREGISTERED: return OK; case RI_SLAVE: fprintf(err, "%sno remote MASTER for local SLAVE '%s' ***\n" , incstr, rfa->ri_filename); if (doRmWidows) { if (rfa->ri_mode & S_IFDIR) removeDir(fn); else { unlink(makeFN(fn)); fprintf(err,"\tremoved local SLAVE file '%s'\n", rfa->ri_filename); } return OK; } else return NOTOK; default: fprintf(err, "%sinvalid state for local '%s' ***\n", incstr, rfa->ri_filename); return NOTOK; } /*-- checks if file exists only remote --*/ if (rfa == NULL) switch (RI_STATUS(rrfa->ri_status)) { case RI_MASTER: case RI_UNREGISTERED: return OK; case RI_SLAVE: fprintf(err, "%sno local MASTER for remote SLAVE '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; default: fprintf(err, "%sinvalid state for remote '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; } /*-- checks if file exists at both sides --*/ switch (RI_STATUS(rfa->ri_status)) { case RI_UNREGISTERED: switch (RI_STATUS(rrfa->ri_status)) { case RI_UNREGISTERED: return OK; case RI_SLAVE: fprintf(err, "%sUNREG/SLAVE state for '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; case RI_MASTER: fprintf(err, "%sUNREG/MASTER state for '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; default: fprintf(err, "%sinvalid state for remote '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; } /* NOTREACHED */ case RI_MASTER: switch (RI_STATUS(rrfa->ri_status)) { case RI_UNREGISTERED: fprintf(err, "%sMASTER/UNREG state for '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; case RI_SLAVE: return OK; case RI_MASTER: fprintf(err, "%sMASTER/MASTER state for '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; default: fprintf(err, "%sinvalid state for remote '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; } /* NOTREACHED */ case RI_SLAVE: switch (RI_STATUS(rrfa->ri_status)) { case RI_UNREGISTERED: fprintf(err, "%sSLAVE/UNREG state for '%s' ***\n", incstr, rrfa->ri_filename); if (doRmWidows) { SET_STATUS(rfa->ri_status, RI_UNREGISTERED); SET_TRANSFER(rfa->ri_status, RI_TR_REQ); fprintf(err,"\tchanged status of '%s' to UNREGISTERED\n" , rfa->ri_filename); *wrp = 1; } return NOTOK; case RI_SLAVE: fprintf(err, "%sSLAVE/SLAVE state for '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; case RI_MASTER: return OK; default: fprintf(err, "%sinvalid state for remote '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; } /* NOTREACHED */ default: fprintf(err, "%sinvalid state for local '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK; } /* NOTREACHED */}/*--------------------------------------------------------------*//* checkMasterSlave - do a consistency check on master and *//* slave file infos *//*--------------------------------------------------------------*/int checkMasterSlave(m, s, ms, ss) struct RfaInfo *m, *s; char *ms, *ss;{ if (m->ri_modTime < s->ri_modTime) { fprintf(err,"%s %s SLAVE version of '%s' is newer than %s MASTER ***\n" , incstr, ss, m->ri_filename, ms); return NOTOK; } if ((m->ri_modTime == s->ri_modTime) && (m->ri_size != s->ri_size)) { fprintf(err,"%s %s MASTER of '%s'", incstr, ms, m->ri_filename); fprintf(err," has different size than %s SLAVE ***\n", ss); return NOTOK; } return OK;}/*--------------------------------------------------------------*//* handleDir *//*--------------------------------------------------------------*/int handleDir(dir, localRfaListPtr, rrfa, rec, wrp) char *dir; struct RfaInfo **localRfaListPtr; struct RfaInfo *rrfa; int rec; int *wrp;{ struct RfaInfo *rfa; char fn[BUFSIZ]; int rc; if (!strcmp(rrfa->ri_filename, ".") || !strcmp(rrfa->ri_filename, "..")) return OK; sprintf(fn, "%s/%s", strcmp(dir,"/") ? dir : "", rrfa->ri_filename); rfa = findRfaInfo(rrfa->ri_filename, *localRfaListPtr); switch (RI_STATUS(rrfa->ri_status)) { case RI_UNREGISTERED: if (rfa) checkState(rfa, rrfa, dir, wrp); return OK; case RI_SLAVE: if (rfa == NULL) { fprintf(err,"%sno local MASTER for remote SLAVE dir '%s' ***\n", incstr, rrfa->ri_filename); return NOTOK_INCONSISTENCY; } if ((rfa->ri_mode & S_IFDIR & S_IFMT) == 0) { fprintf(err, "%slocal file '%s' conflicts with remote directory ***\n", incstr, rrfa->ri_filename); return NOTOK_INCONSISTENCY; } if (checkState(rfa, rrfa, dir, wrp) == NOTOK) return NOTOK_INCONSISTENCY; break; case RI_MASTER:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?