/* find.c: Change finder.
 *
 * $Id: find.c,v 1.3 2002/10/28 22:14:04 andy Exp $
 *
 * Copyright (C) 2002 Andy Goth <unununium@openverse.com>
 * For more information visit http://ioioio.net/devel/install-log/
 * 
 * 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; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA  02111-1307, USA. */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include "install-log.h"

typedef struct dirent dirent_t;
typedef struct stat stat_t;

static void find_newer_wrapper(list_t* node);
static void find_newer(const char* cwd);

/* Find all files newer than timestamp */
void find_all(void)
{
	clear_list(&new_files);
	proc_list(&include, find_newer_wrapper);
}

/* For use with proc_list */
static void find_newer_wrapper(list_t* node)
{
	report(1, "Scanning %s\n", (char*)node->data);
	find_newer((char*)node->data);
}

/* Recursively find all files newer than timestamp, starting in cwd */
static void find_newer(const char* cwd)
{
	char* name = NULL;
	int name_len;
	char* chroot_name = NULL;
	int chroot_name_len;
	DIR* dir;
	dirent_t* d;
	stat_t stat;

	/* Scan all entries in this directory */
	safe_sprintf(&chroot_name, &chroot_name_len, "%s/%s", root, cwd);
	collapse(chroot_name, '/');

	if ((dir = opendir(chroot_name)) == NULL) {
		alert("cannot scan %s: %s\n", chroot_name, strerror(errno));
		return;
	}

	while (d = readdir(dir))
	{
		/* Skip '.' and '..' */
		if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
			continue;

		/* Calculate the filename */
		safe_sprintf(&name, &name_len, "%s/%s", cwd, d->d_name);
		safe_sprintf(&chroot_name, &chroot_name_len, "%s/%s", root,
				name);
		
		/* Process this directory entry */
		if (lstat(chroot_name, &stat) == -1) {
			/* lstat failed, so skip file */
			alert("lstat(%s): %s\n", chroot_name, strerror(errno));
		} else if (list_has_string(&exclude, name)) {
			report(2, "Excluding %s\n", name);
		} else if (S_ISDIR(stat.st_mode)) {
			report(3, "Descending into %s\n", name);
			find_newer(name);
		} else if (timestamp < stat.st_mtime) {
			report(4, "Adding %s\n", name);
			insert_string_node(&new_files, name);
		}
	}
	closedir(dir);
	free(name);
	free(chroot_name);
}

/* EOF */

