/*
** SSYNC: Simple minded filesystem sync utility
** Copyright (C) 2002 Michael W. Shaffer <mwshaffer@angrypot.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.  
**
** You should have received a copy of the GNU General Public License
** along with this program (see the file COPYING). If not, write to:
**
** The Free Software Foundation, Inc.
** 59 Temple Place, Suite 330,
** Boston, MA  02111-1307  USA
*/

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <dirent.h>
#include <utime.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "hash.h"
#include "list.h"
#include "conf.h"
#include "ssync.h"
#include "log.h"

char *conf_path_default = ".ssyncrc";

static void main_loop (void)
{
	int i = 0;
	struct stat s;
	struct stat d;
	char *src = NULL;
	char *dst = NULL;
	struct datum *da = NULL;
	struct list_item *curr = NULL;
	unsigned long count = 0;
	unsigned long stime = 0;
	unsigned long etime = 0;
	unsigned long dtime = 0;
	char bytes[256];
	char msg[256];

	count = 0;
	LOG (info, "begin run");

	memset (&stats, 0, sizeof (struct stats));

	stime = time (NULL);
	for (i = 0 ; i < work.size ; i++) {
		for (curr = work.tbl[i].head ; curr ; curr = curr->next) {
			da = (struct datum *) curr->data;
			if (da) {
				src = (char *) da->key;
				dst = (char *) da->val;

				memset (&s, 0, sizeof (struct stat));
				memset (&d, 0, sizeof (struct stat));

				lstat (src, &s);
				lstat (dst, &d);

				memset (msg, 0, sizeof (msg));
				snprintf (msg, (sizeof (msg) - 1),
					"processing work item: %s --> %s",
					src, dst);
				LOG (info, msg);

				process (src, &s, dst, &d);
				count++;
			}
		}
	}
	etime = time (NULL);

	memset (msg, 0, sizeof (msg));
	snprintf (msg, (sizeof (msg) - 1),
		"end run: %ld work items processed",
		count);
	LOG (info, msg);

	dtime = etime - stime;
	memset (bytes, 0, sizeof (bytes));
	get_bytes (bytes, (sizeof (bytes) - 1));
	memset (msg, 0, sizeof (msg));
	snprintf (msg, (sizeof (msg) - 1),
		"statistics: time: %02ld:%02ld:%02ld "
		"dirs: %ld files: %ld links: %ld "
		"updated: %ldd/%ldf/%ldl (%s) "
		"deleted: %ld",
		(dtime / 3600), ((dtime % 3600) / 60), ((dtime % 3600) % 60),
		stats.dirs, stats.files, stats.links,
		stats.dirs_updated, stats.regs_updated, stats.lnks_updated, bytes,
		stats.deleted);
	LOG (info, msg);

	memset (msg, 0, sizeof (msg));
	snprintf (msg, (sizeof (msg) - 1),
		"exceptions: unknowns: %ld errors: %ld",
		stats.unknowns, stats.errors);
	LOG (info, msg);

	return;
}

static void signal_handler (int sig)
{
	switch (sig) {
		case SIGHUP:
			break;
		case SIGINT:
		case SIGTERM:
			ssync_shutdown ();
			log_free ();
			config_free ();
			exit (0);
			break;
		default:
			break;
	}

	return;
}

int main (int argc, char **argv)
{
	signal (SIGINT, signal_handler);
	signal (SIGTERM, signal_handler);
	signal (SIGHUP, signal_handler);

	config_load (argc, argv);
	log_init ();

	if (ssync_startup (argc, argv) == -1)
		exit (-1);
	main_loop ();
	ssync_shutdown ();

	log_free ();
	config_free ();

	exit (0);

	return (0);
}



syntax highlighted by Code2HTML, v. 0.9.1