#include "diff.h"
#include <error.h>
#include <exclude.h>
#include <setjmp.h>
#include <xalloc.h>
struct dirdata
{
size_t nnames;
char const **names;
char *data;
};
static bool locale_specific_sorting;
static jmp_buf failed_strcoll;
static bool dir_loop (struct comparison const *, int);
static int compare_names_for_qsort (void const *, void const *);
static bool
dir_read (struct file_data const *dir, struct dirdata *dirdata)
{
register struct dirent *next;
register size_t i;
char const **names;
size_t nnames;
char *data;
size_t data_alloc, data_used;
dirdata->names = 0;
dirdata->data = 0;
nnames = 0;
data = 0;
if (dir->desc != -1)
{
register DIR *reading = opendir (dir->name);
if (!reading)
return 0;
data_alloc = 512;
data_used = 0;
dirdata->data = data = xmalloc (data_alloc);
while ((errno = 0, (next = readdir (reading)) != 0))
{
char *d_name = next->d_name;
size_t d_size = NAMLEN (next) + 1;
if (d_name[0] == '.'
&& (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0)))
continue;
if (excluded_filename (excluded, d_name))
continue;
while (data_alloc < data_used + d_size)
{
if (PTRDIFF_MAX / 2 <= data_alloc)
xalloc_die ();
dirdata->data = data = xrealloc (data, data_alloc *= 2);
}
memcpy (data + data_used, d_name, d_size);
data_used += d_size;
nnames++;
}
if (errno)
{
int e = errno;
closedir (reading);
errno = e;
return 0;
}
#if CLOSEDIR_VOID
closedir (reading);
#else
if (closedir (reading) != 0)
return 0;
#endif
}
if (PTRDIFF_MAX / sizeof *names - 1 <= nnames)
xalloc_die ();
dirdata->names = names = xmalloc ((nnames + 1) * sizeof *names);
dirdata->nnames = nnames;
for (i = 0; i < nnames; i++)
{
names[i] = data;
data += strlen (data) + 1;
}
names[nnames] = 0;
return 1;
}
static int
compare_names (char const *name1, char const *name2)
{
if (ignore_file_name_case)
{
int r = strcasecmp (name1, name2);
if (r)
return r;
}
if (locale_specific_sorting)
{
int r;
errno = 0;
r = strcoll (name1, name2);
if (errno)
{
error (0, errno, _("cannot compare file names `%s' and `%s'"),
name1, name2);
longjmp (failed_strcoll, 1);
}
if (r)
return r;
}
return file_name_cmp (name1, name2);
}
static int
compare_names_for_qsort (void const *file1, void const *file2)
{
char const *const *f1 = file1;
char const *const *f2 = file2;
return compare_names (*f1, *f2);
}
int
diff_dirs (struct comparison const *cmp,
int (*handle_file) (struct comparison const *,
char const *, char const *))
{
struct dirdata dirdata[2];
int volatile val = EXIT_SUCCESS;
int i;
if ((cmp->file[0].desc == -1 || dir_loop (cmp, 0))
&& (cmp->file[1].desc == -1 || dir_loop (cmp, 1)))
{
error (0, 0, "%s: recursive directory loop",
cmp->file[cmp->file[0].desc == -1].name);
return EXIT_TROUBLE;
}
for (i = 0; i < 2; i++)
if (! dir_read (&cmp->file[i], &dirdata[i]))
{
perror_with_name (cmp->file[i].name);
val = EXIT_TROUBLE;
}
if (val == EXIT_SUCCESS)
{
char const **volatile names[2];
names[0] = dirdata[0].names;
names[1] = dirdata[1].names;
locale_specific_sorting = 1;
if (setjmp (failed_strcoll))
locale_specific_sorting = 0;
for (i = 0; i < 2; i++)
qsort (names[i], dirdata[i].nnames, sizeof *dirdata[i].names,
compare_names_for_qsort);
if (starting_file && ! cmp->parent)
{
while (*names[0] && compare_names (*names[0], starting_file) < 0)
names[0]++;
while (*names[1] && compare_names (*names[1], starting_file) < 0)
names[1]++;
}
while (*names[0] || *names[1])
{
int nameorder = (!*names[0] ? 1 : !*names[1] ? -1
: compare_names (*names[0], *names[1]));
int v1 = (*handle_file) (cmp,
0 < nameorder ? 0 : *names[0]++,
nameorder < 0 ? 0 : *names[1]++);
if (val < v1)
val = v1;
}
}
for (i = 0; i < 2; i++)
{
if (dirdata[i].names)
free (dirdata[i].names);
if (dirdata[i].data)
free (dirdata[i].data);
}
return val;
}
static bool
dir_loop (struct comparison const *cmp, int i)
{
struct comparison const *p = cmp;
while ((p = p->parent))
if (0 < same_file (&p->file[i].stat, &cmp->file[i].stat))
return 1;
return 0;
}