vtools_genesis 1
vtools_genesis 2 #include "diff.h"
vtools_genesis 3 #include <error.h>
vtools_genesis 4 #include <filenamecat.h>
vtools_genesis 5 #include <setjmp.h>
vtools_genesis 6 #include <xalloc.h>
vtools_genesis 7 #include <dirent.h>
vtools_genesis 8 #include <stdlib.h>
vtools_genesis 9
vtools_genesis 10
vtools_genesis 11 vector of filenames for its contents. |dir->desc == -1| means this
vtools_genesis 12 directory is known to be nonexistent, so set |dirdata| to an empty
vtools_genesis 13 vector. Return -1 (setting |errno|) if error, 0 otherwise. */
vtools_genesis 14
vtools_genesis 15 struct dirdata {
vtools_genesis 16 size_t nnames;
vtools_genesis 17 char const **names;
vtools_genesis 18 char *data;
vtools_genesis 19 };
vtools_genesis 20
vtools_genesis 21 static bool dir_loop(struct comparison const *, int);
vtools_genesis 22
vtools_genesis 23
vtools_genesis 24
vtools_genesis 25
vtools_genesis 26 static bool
vtools_genesis 27 dir_read(struct file_data const *dir, struct dirdata *dirdata) {
vtools_genesis 28 register struct dirent *next;
vtools_genesis 29 register size_t i;
vtools_genesis 30
vtools_genesis 31
vtools_genesis 32 char const **names;
vtools_genesis 33
vtools_genesis 34
vtools_genesis 35 size_t nnames;
vtools_genesis 36
vtools_genesis 37
vtools_genesis 38 char *data;
vtools_genesis 39 size_t data_alloc, data_used;
vtools_genesis 40
vtools_genesis 41 dirdata->names = 0;
vtools_genesis 42 dirdata->data = 0;
vtools_genesis 43 nnames = 0;
vtools_genesis 44 data = 0;
vtools_genesis 45
vtools_genesis 46 if (dir->desc != -1) {
vtools_genesis 47
vtools_genesis 48 register DIR *reading = opendir(dir->name);
vtools_genesis 49 if (!reading)
vtools_genesis 50 return false;
vtools_genesis 51
vtools_genesis 52
vtools_genesis 53
vtools_genesis 54 data_alloc = 512;
vtools_genesis 55 data_used = 0;
vtools_genesis 56 dirdata->data = data = xmalloc(data_alloc);
vtools_genesis 57
vtools_genesis 58
vtools_genesis 59 into the |data| table. */
vtools_genesis 60
vtools_genesis 61 while ((errno = 0, (next = readdir(reading)) != 0)) {
vtools_genesis 62 char *d_name = next->d_name;
vtools_genesis 63 size_t d_size = strlen (next->d_name) + 1;
vtools_genesis 64
vtools_genesis 65
vtools_genesis 66 if (d_name[0] == '.'
vtools_genesis 67 && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0)))
vtools_genesis 68 continue;
vtools_genesis 69
vtools_genesis 70 while (data_alloc < data_used + d_size) {
vtools_genesis 71 if (PTRDIFF_MAX / 2 <= data_alloc)
vtools_genesis 72 xalloc_die();
vtools_genesis 73 dirdata->data = data = xrealloc(data, data_alloc *= 2);
vtools_genesis 74 }
vtools_genesis 75
vtools_genesis 76 memcpy (data + data_used, d_name, d_size);
vtools_genesis 77 data_used += d_size;
vtools_genesis 78 nnames++;
vtools_genesis 79 }
vtools_genesis 80 if (errno) {
vtools_genesis 81 int e = errno;
vtools_genesis 82 closedir(reading);
vtools_genesis 83 errno = e;
vtools_genesis 84 return false;
vtools_genesis 85 }
vtools_genesis 86 if (closedir(reading) != 0)
vtools_genesis 87 return false;
vtools_genesis 88 }
vtools_genesis 89
vtools_genesis 90
vtools_genesis 91 if (PTRDIFF_MAX / sizeof *names - 1 <= nnames)
vtools_genesis 92 xalloc_die();
vtools_genesis 93 dirdata->names = names = xmalloc((nnames + 1) * sizeof *names);
vtools_genesis 94 dirdata->nnames = nnames;
vtools_genesis 95 for (i = 0; i < nnames; i++) {
vtools_genesis 96 names[i] = data;
vtools_genesis 97 data += strlen(data) + 1;
vtools_genesis 98 }
vtools_genesis 99 names[nnames] = 0;
vtools_genesis 100 return true;
vtools_genesis 101 }
vtools_genesis 102
vtools_genesis 103
vtools_genesis 104
vtools_genesis 105 #define compare_names strcmp
vtools_genesis 106
vtools_genesis 107
vtools_genesis 108
vtools_genesis 109 static int
vtools_genesis 110 compare_names_for_qsort(void const *file1, void const *file2) {
vtools_genesis 111 char const *const *f1 = file1;
vtools_genesis 112 char const *const *f2 = file2;
vtools_genesis 113 char const *name1 = *f1;
vtools_genesis 114 char const *name2 = *f2;
vtools_genesis 115 return compare_names(name1, name2);
vtools_genesis 116 }
vtools_genesis 117
vtools_genesis 118
vtools_genesis 119 top-level routine; it does everything necessary for diff on two
vtools_genesis 120 directories.
vtools_genesis 121
vtools_genesis 122 |cmp->file[0].desc == -1| says directory |cmp->file[0]| doesn't
vtools_genesis 123 exist, but pretend it is empty. Likewise for |cmp->file[1]|.
vtools_genesis 124
vtools_genesis 125 |handle_file| is a caller-provided subroutine called to handle each file.
vtools_genesis 126 It gets three operands: |cmp|, name of file in dir 0, name of file in dir 1.
vtools_genesis 127 These names are relative to the original working directory.
vtools_genesis 128
vtools_genesis 129 For a file that appears in only one of the dirs, one of the name-args
vtools_genesis 130 to |handle_file| is zero.
vtools_genesis 131
vtools_genesis 132 Returns the maximum of all the values returned by |handle_file|,
vtools_genesis 133 or |exit_trouble| if trouble is encountered in opening files. */
vtools_genesis 134
vtools_genesis 135 int
vtools_genesis 136 diff_dirs(struct comparison const *cmp,
vtools_genesis 137 int (*handle_file)(struct comparison const *,
vtools_genesis 138 char const *, char const *)) {
vtools_genesis 139 struct dirdata dirdata[2];
vtools_genesis 140 int volatile val = EXIT_SUCCESS;
vtools_genesis 141 int i;
vtools_genesis 142
vtools_genesis 143 if ((cmp->file[0].desc == -1 || dir_loop(cmp, 0))
vtools_genesis 144 && (cmp->file[1].desc == -1 || dir_loop(cmp, 1))) {
vtools_genesis 145 error(0, 0, "%s: recursive directory loop",
vtools_genesis 146 cmp->file[cmp->file[0].desc == -1].name);
vtools_genesis 147 return EXIT_TROUBLE;
vtools_genesis 148 }
vtools_genesis 149
vtools_genesis 150
vtools_genesis 151 for (i = 0; i < 2; i++)
vtools_genesis 152 if (!dir_read(&cmp->file[i], &dirdata[i])) {
vtools_genesis 153 perror_with_name(cmp->file[i].name);
vtools_genesis 154 val = EXIT_TROUBLE;
vtools_genesis 155 }
vtools_genesis 156
vtools_genesis 157 if (val == EXIT_SUCCESS) {
vtools_genesis 158 char const **volatile names[2];
vtools_genesis 159 names[0] = dirdata[0].names;
vtools_genesis 160 names[1] = dirdata[1].names;
vtools_genesis 161
vtools_genesis 162
vtools_genesis 163 for (i = 0; i < 2; i++)
vtools_genesis 164 qsort(names[i], dirdata[i].nnames, sizeof *dirdata[i].names,
vtools_genesis 165 compare_names_for_qsort);
vtools_genesis 166
vtools_genesis 167
vtools_genesis 168 comparison, ignore all file names less than the specified
vtools_genesis 169 starting name. */
vtools_genesis 170
vtools_genesis 171 if (starting_file && !cmp->parent) {
vtools_genesis 172 while (*names[0] && compare_names(*names[0], starting_file) < 0)
vtools_genesis 173 names[0]++;
vtools_genesis 174 while (*names[1] && compare_names(*names[1], starting_file) < 0)
vtools_genesis 175 names[1]++;
vtools_genesis 176 }
vtools_genesis 177
vtools_genesis 178
vtools_genesis 179 while (*names[0] || *names[1]) {
vtools_genesis 180
vtools_genesis 181 the end of a dir, pretend the "next name" in that dir
vtools_genesis 182 is very large. */
vtools_genesis 183 int nameorder = (!*names[0] ? 1 : !*names[1] ? -1
vtools_genesis 184 : compare_names(*names[0], *names[1]));
vtools_genesis 185 int v1 = (*handle_file)(cmp,
vtools_genesis 186 0 < nameorder ? 0 : *names[0]++,
vtools_genesis 187 nameorder < 0 ? 0 : *names[1]++);
vtools_genesis 188 if (val < v1)
vtools_genesis 189 val = v1;
vtools_genesis 190 }
vtools_genesis 191 }
vtools_genesis 192
vtools_genesis 193 for (i = 0; i < 2; i++) {
vtools_genesis 194 free(dirdata[i].names);
vtools_genesis 195 free(dirdata[i].data);
vtools_genesis 196 }
vtools_genesis 197
vtools_genesis 198 return val;
vtools_genesis 199 }
vtools_genesis 200
vtools_genesis 201
vtools_genesis 202
vtools_genesis 203 static bool
vtools_genesis 204 dir_loop(struct comparison const *cmp, int i) {
vtools_genesis 205 struct comparison const *p = cmp;
vtools_genesis 206 while ((p = p->parent))
vtools_genesis 207 if (0 < same_file (&p->file[i].stat, &cmp->file[i].stat))
vtools_genesis 208 return true;
vtools_genesis 209 return false;
vtools_genesis 210 }
vtools_genesis 211
vtools_genesis 212
vtools_genesis 213
vtools_genesis 214 char *
vtools_genesis 215 find_dir_file_pathname(char const *dir, char const *file) {
vtools_genesis 216 const char *match = file;
vtools_genesis 217 return file_name_concat(dir, match, NULL);
vtools_genesis 218 }