#ident "%W% %E% SMI"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "filesync.h"
#include "database.h"
#include "messages.h"
#define BASE_MAJOR 1
#define BASE_MINOR 2
#define BASE_TAG "filesync-BaseLine"
struct base omnibase;
struct base *bases;
static int num_bases;
static errmask_t bw_header(FILE *);
static errmask_t bw_base(FILE *, struct base *);
static errmask_t bw_file(FILE *, struct file *, int);
static struct file *add_file_to_list(struct file **, const char *);
static char showtype(int);
static long gettype(int);
struct base *
add_base(const char *src, const char *dst)
{ struct base *bp, **bpp;
for (bpp = &bases; (bp = *bpp) != 0; bpp = &bp->b_next) {
if (strcmp(src, bp->b_src_spec))
continue;
if (strcmp(dst, bp->b_dst_spec))
continue;
if (opt_debug & DBG_BASE)
fprintf(stderr, "BASE: FOUND base=%d, src=%s, dst=%s\n",
bp->b_ident, src, dst);
return (bp);
}
bp = malloc(sizeof (struct base));
if (bp == 0)
nomem("base structure");
memset((void *) bp, 0, sizeof (struct base));
bp->b_ident = ++num_bases;
bp->b_src_spec = strdup(src);
bp->b_dst_spec = strdup(dst);
if ((bp->b_src_name = expand(bp->b_src_spec)) == 0) {
fprintf(stderr, gettext(ERR_badbase), bp->b_src_spec);
exit(ERR_FILES);
}
if ((bp->b_dst_name = expand(bp->b_dst_spec)) == 0) {
fprintf(stderr, gettext(ERR_badbase), bp->b_dst_spec);
exit(ERR_FILES);
}
*bpp = bp;
if (opt_debug & DBG_BASE)
fprintf(stderr, "BASE: ADDED base=%d, src=%s, dst=%s\n",
bp->b_ident, src, dst);
return (bp);
}
static struct file *
add_file_to_list(struct file **pp, const char *name)
{ struct file *fp, *new;
int rslt;
static struct file **last_list;
static struct file *last_file;
if (last_list == pp && (new = last_file) != 0) {
if (strcmp(name, new->f_name) > 0) {
fp = new->f_next;
if (fp == 0) {
pp = &new->f_next;
goto makeit;
}
if (strcmp(name, fp->f_name) == 0) {
fp->f_flags &= ~F_NEW;
new = fp;
goto gotit;
}
}
}
last_list = pp;
for (fp = *pp; fp; pp = &fp->f_next, fp = *pp) {
rslt = strcmp(name, fp->f_name);
if (rslt == 0) {
fp->f_flags &= ~F_NEW;
new = fp;
goto gotit;
}
if (rslt < 0)
break;
}
makeit:
new = (struct file *) malloc(sizeof (*new));
if (new == 0)
nomem("file structure");
memset((void *) new, 0, sizeof (struct file));
new->f_name = strdup(name);
new->f_flags = F_NEW;
new->f_next = fp;
*pp = new;
gotit:
last_file = new;
return (new);
}
struct file *
add_file_to_base(struct base *bp, const char *name)
{ struct file *fp;
fp = add_file_to_list(&bp->b_files, name);
fp->f_base = bp;
fp->f_depth = 0;
if (opt_debug & DBG_LIST)
fprintf(stderr, "LIST: base=%d, %s file=%s\n",
bp->b_ident, (fp->f_flags&F_NEW) ? "NEW" : "FOUND",
name);
return (fp);
}
struct file *
add_file_to_dir(struct file *dp, const char *name)
{ struct file *fp;
fp = add_file_to_list(&dp->f_files, name);
fp->f_base = dp->f_base;
fp->f_depth = dp->f_depth + 1;
if (opt_debug & DBG_LIST)
fprintf(stderr, "LIST: dir=%s, %s file=%s\n",
dp->f_name, (fp->f_flags&F_NEW) ? "NEW" : "FOUND",
name);
return (fp);
}
errmask_t
read_baseline(char *name)
{ FILE *file;
errmask_t errs = 0;
char *s;
char *s1 = 0;
char type;
char *field = "???";
unsigned long l;
unsigned long long ll;
int level;
int major, minor;
struct base *bp = 0;
struct file *fp;
struct fileinfo *ip;
aclent_t *ap;
struct file *dirstack[ MAX_DEPTH ];
file = fopen(name, "r");
if (file == NULL) {
fprintf(stderr, gettext(ERR_open), gettext(TXT_base),
name);
return (ERR_FILES);
}
lex_linenum = 0;
if (opt_debug & DBG_FILES)
fprintf(stderr, "FILE: READ BASELINE %s\n", name);
while (!feof(file)) {
s = lex(file);
if (s == 0 || *s == 0 || *s == '#' || *s == '*')
continue;
field = "keyword";
if (strcmp(s, "VERSION") == 0 || strcmp(s, BASE_TAG) == 0) {
s = lex(0);
field = gettext(TXT_noargs);
if (s == 0)
goto bad;
major = strtol(s, &s1, 10);
field = gettext(TXT_badver);
if (*s1 != '.')
goto bad;
minor = strtol(&s1[1], 0, 10);
if (major != BASE_MAJOR || minor > BASE_MINOR) {
fprintf(stderr, gettext(ERR_badver),
major, minor, gettext(TXT_base), name);
errs |= ERR_FILES;
}
s1 = 0;
continue;
}
if (strcmp(s, "BASE_SRC") == 0) {
s = lex(0);
field = "source directory";
if (s == 0)
goto bad;
s1 = strdup(s);
bp = 0;
continue;
}
if (strcmp(s, "BASE_DST") == 0) {
s = lex(0);
field = "destination directory";
if (s == 0)
goto bad;
if (s1 == 0) {
field = "no source directory";
goto bad;
}
bp = add_base(s1, s);
free(s1);
s1 = 0;
continue;
}
if (strcmp(s, "FILE") == 0) {
if (bp == 0) {
field = "missing base";
goto bad;
}
s = lex(0);
field = "level";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
level = l;
s = lex(0);
field = "file type";
if (s == 0 || *s == 0)
goto bad;
type = *s;
if (gettype(type) < 0)
goto bad;
s = lex(0);
field = "file name";
if (s == 0 || *s == 0)
goto bad;
if (level == 0)
fp = add_file_to_base(bp, s);
else
fp = add_file_to_dir(dirstack[level-1], s);
fp->f_flags |= F_IN_BASELINE;
if (level >= MAX_DEPTH) {
fprintf(stderr, gettext(ERR_deep), s);
exit(ERR_OTHER);
}
dirstack[ level ] = fp;
ip = &fp->f_info[ OPT_BASE ];
ip->f_type = gettype(type);
s = lex(0);
field = "file modes";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
ip->f_mode = l;
s = lex(0);
field = "file UID";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
ip->f_uid = l;
s = lex(0);
field = "file GID";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
ip->f_gid = l;
s = lex(0);
field = "source i#";
if (s == 0 || *s == 0)
goto bad;
ll = strtoull(s, 0, 0);
fp->f_s_inum = (ino_t) ll;
s = lex(0);
field = "source major";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_s_maj = l;
s = lex(0);
field = "source minor";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_s_min = l;
s = lex(0);
field = "source nlink";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_s_nlink = l;
s = lex(0);
field = "source modtime";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_s_modtime = l;
s = lex(0);
field = "destination i#";
if (s == 0 || *s == 0)
goto bad;
ll = strtoull(s, 0, 0);
fp->f_d_inum = (ino_t) ll;
s = lex(0);
field = "destination major";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_d_maj = l;
s = lex(0);
field = "destination minor";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_d_min = l;
s = lex(0);
field = "dest nlink";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_d_nlink = l;
s = lex(0);
field = "dest modtime";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
fp->f_d_modtime = l;
s = lex(0);
if (type == 'C' || type == 'B') {
field = "rdev major";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
ip->f_rd_maj = l;
s = lex(0);
field = "rdev minor";
if (s == 0 || *s == 0)
goto bad;
l = strtoul(s, 0, 0);
ip->f_rd_min = l;
} else {
field = "file size";
if (s == 0 || *s == 0)
goto bad;
ll = strtoul(s, 0, 0);
ip->f_size = (off_t) ll;
}
s = lex(0);
field = "acl count";
if (s && *s) {
l = strtoul(s, 0, 0);
ip->f_numacls = l;
ip->f_acls = (aclent_t *) malloc(ip->f_numacls *
sizeof (aclent_t));
if (ip->f_acls == 0)
nomem("Access Control List");
}
continue;
}
if (strcmp(s, "ACL") == 0) {
if (ip == 0 || ip->f_acls == 0) {
field = "ACL w/o FILE/LIST";
goto bad;
}
s = lex(0);
field = "acl index";
if (s == 0)
goto bad;
l = strtoul(s, 0, 0);
if (l >= ip->f_numacls)
goto bad;
else
ap = &ip->f_acls[l];
s = lex(0);
field = "acl type";
if (s == 0)
goto bad;
l = strtoul(s, 0, 0);
ap->a_type = l;
s = lex(0);
field = "acl id";
if (s == 0)
goto bad;
l = strtoul(s, 0, 0);
ap->a_id = l;
s = lex(0);
field = "acl perm";
if (s == 0)
goto bad;
l = strtoul(s, 0, 0);
ap->a_perm = l;
continue;
}
bad:
fprintf(stderr, gettext(ERR_badinput), lex_linenum,
field, name);
errs |= ERR_FILES;
}
(void) fclose(file);
return (errs);
}
errmask_t
write_baseline(char *name)
{ FILE *newfile;
errmask_t errs = 0;
struct base *bp;
char tmpname[ MAX_PATH ];
if (opt_debug & DBG_FILES)
fprintf(stderr, "FILE: WRITE BASELINE %s\n", name);
if (opt_notouch)
return (0);
sprintf(tmpname, "%s-TMP", name);
newfile = fopen(tmpname, "w+");
if (newfile == NULL) {
fprintf(stderr, gettext(ERR_creat), gettext(TXT_base),
tmpname);
return (ERR_FILES);
}
errs |= bw_header(newfile);
for (bp = bases; bp; bp = bp->b_next)
errs |= bw_base(newfile, bp);
if (ferror(newfile)) {
fprintf(stderr, gettext(ERR_write), gettext(TXT_base),
tmpname);
errs |= ERR_FILES;
}
if (fclose(newfile)) {
fprintf(stderr, gettext(ERR_fclose), gettext(TXT_base),
tmpname);
errs |= ERR_FILES;
}
if (errs == 0)
if (rename(tmpname, name) != 0) {
fprintf(stderr, gettext(ERR_rename),
gettext(TXT_base), tmpname, name);
errs |= ERR_FILES;
}
return (errs);
}
static errmask_t
bw_header(FILE *file)
{ time_t now;
struct tm *local;
(void) time(&now);
local = localtime(&now);
fprintf(file, "%s %d.%d\n", BASE_TAG, BASE_MAJOR, BASE_MINOR);
fprintf(file, "#\n");
fprintf(file, "# filesync baseline, last written by %s, %s",
cuserid((char *) 0), asctime(local));
fprintf(file, "#\n");
return (0);
}
static errmask_t
bw_base(FILE *file, struct base *bp)
{ struct file *fp;
errmask_t errs = 0;
if (bp->b_flags & F_REMOVE)
return (0);
fprintf(file, "\n");
fprintf(file, "BASE_SRC %s\n", noblanks(bp->b_src_spec));
fprintf(file, "BASE_DST %s\n", noblanks(bp->b_dst_spec));
for (fp = bp->b_files; fp; fp = fp->f_next)
errs |= bw_file(file, fp, 0);
return (errs);
}
static errmask_t
bw_file(FILE *file, struct file *fp, int depth)
{ struct file *cp;
int i;
errmask_t errs = 0;
long long ll;
struct fileinfo *ip = &fp->f_info[OPT_BASE];
if (fp->f_flags & F_REMOVE)
return (0);
if (fp->f_flags & F_CONFLICT || (fp->f_flags&F_EVALUATE) == 0) {
fp->f_info[OPT_SRC].f_ino = fp->f_s_inum;
fp->f_info[OPT_SRC].f_nlink = fp->f_s_nlink;
fp->f_info[OPT_SRC].f_d_maj = fp->f_s_maj;
fp->f_info[OPT_SRC].f_d_min = fp->f_s_min;
fp->f_info[OPT_SRC].f_modtime = fp->f_s_modtime;
fp->f_info[OPT_DST].f_ino = fp->f_d_inum;
fp->f_info[OPT_DST].f_nlink = fp->f_d_nlink;
fp->f_info[OPT_DST].f_d_maj = fp->f_d_maj;
fp->f_info[OPT_DST].f_d_min = fp->f_d_min;
fp->f_info[OPT_DST].f_modtime = fp->f_d_modtime;
}
fprintf(file, "FILE %d %c %-20s 0%04o", depth, showtype(ip->f_type),
noblanks(fp->f_name), ip->f_mode);
fprintf(file, " %6ld %6ld", ip->f_uid, ip->f_gid);
ll = fp->f_info[OPT_SRC].f_ino;
fprintf(file, "\t%6lld %4ld %4ld %4d 0x%08lx",
ll,
fp->f_info[OPT_SRC].f_d_maj,
fp->f_info[OPT_SRC].f_d_min,
fp->f_info[OPT_SRC].f_nlink,
fp->f_info[OPT_SRC].f_modtime);
ll = fp->f_info[OPT_DST].f_ino;
fprintf(file, "\t%6lld %4ld %4ld %4d 0x%08lx",
ll,
fp->f_info[OPT_DST].f_d_maj,
fp->f_info[OPT_DST].f_d_min,
fp->f_info[OPT_DST].f_nlink,
fp->f_info[OPT_DST].f_modtime);
if (S_ISBLK(ip->f_type) || S_ISCHR(ip->f_type))
fprintf(file, "\t%4ld %4ld", ip->f_rd_maj, ip->f_rd_min);
else {
ll = ip->f_size;
fprintf(file, "\t%lld", ll);
}
fprintf(file, "\t%d", ip->f_numacls);
fprintf(file, "\n");
for (i = 0; i < ip->f_numacls; i++)
fprintf(file, "ACL %d %d %ld %o\n", i, ip->f_acls[i].a_type,
ip->f_acls[i].a_id, ip->f_acls[i].a_perm);
for (cp = fp->f_files; cp; cp = cp->f_next)
errs |= bw_file(file, cp, depth + 1);
return (errs);
}
static char types[16] = "-PC?DNB?F?S?s???";
static char showtype(int mode)
{
return (types[ (mode & S_IFMT) >> 12 ]);
}
static long gettype(int code)
{ int i;
for (i = 0; i < 16; i++)
if (types[i] == code)
return (i << 12);
return (-1);
}