aboutsummaryrefslogtreecommitdiffstats
path: root/wandering.c
blob: c3183e6c2d468a73e11cffa98e9af367efbc73f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "packages.h"
#include "swabber.h"
#include "wandering.h"


#define MAX_WANDERING_LISTS 10

struct top_not_found_node {
	char basename[FILENAME_LENGTH];
	unsigned int wandering_type;
	char found_path[FILENAME_LENGTH];
	struct not_found_node * not_found_base;
	struct top_not_found_node * next;
};

struct not_found_node {
	struct error_file_node * error_file;
	unsigned int count;
	struct not_found_node * next;
};

static void not_found_free(struct top_not_found_node * top_node)
{
	struct not_found_node * t, *next;
	for (t = top_node->not_found_base; next; t = next) {
		next = t->next;
		free(t);
	}
	free(top_node);
}

static char * basename(const char * filename)
{
	return strrchr(filename, '/')+1;
}


static int in_project(const char * path, const char * project)
{
	unsigned int len = FILENAME_LENGTH;
	if (strlen(path) < len)
		len = strlen(path);
	if (strlen(project) < len)
		len = strlen(project);

	return memcmp(path, project, len);
}

void wandering_process(struct top_tag_node *tag, const char * project)
{
	struct not_found_node * nfn;
	struct top_not_found_node * tnfn;
	unsigned int first = 0;

	/* Go through all the wandering entries and calculate type */
	for (tnfn = tag->wandering_problems_base; tnfn; tnfn = tnfn->next) {

		for (nfn = tnfn->not_found_base; nfn; nfn = nfn->next) {
			if (in_project(nfn->error_file->filename, project)) {
				switch (first) {
				case WANDERING_ONLY_HOST:
					first = WANDERING_HOST_THEN_PROJECT;
					goto done;
				case 0:
					first = WANDERING_ONLY_PROJECT;
				case WANDERING_ONLY_PROJECT:
				case WANDERING_HOST_THEN_PROJECT:
				case WANDERING_PROJECT_THEN_HOST: /* should never happen */
					break;
				}
			} else {
				switch (first) {
				case WANDERING_ONLY_PROJECT:
					first = WANDERING_PROJECT_THEN_HOST;
					goto done;
				case 0:
					first = WANDERING_ONLY_HOST;
				case WANDERING_ONLY_HOST:
				case WANDERING_PROJECT_THEN_HOST:
				case WANDERING_HOST_THEN_PROJECT: /* should never happen */
					break;
				}
			}
		}
done:
		tnfn->wandering_type = first;
		first = 0;
	}
}

void wandering_print_report(FILE * f, struct top_tag_node *tag,
	unsigned int wandering_type)
{
	struct not_found_node * nfn;
	struct top_not_found_node * tnfn;

	fprintf(f, "Wandering search errors\n");
	for (tnfn = tag->wandering_problems_base; tnfn; tnfn = tnfn->next) {
		if (tnfn->wandering_type!=wandering_type)
			continue;
		shrink_path_dirname(tnfn->found_path);
		fprintf(f, "  - %s found in %s\n",
		    tnfn->basename, tnfn->found_path);
		for (nfn = tnfn->not_found_base; nfn; nfn = nfn->next) {
			shrink_path_dirname(nfn->error_file->filename);
			fprintf(f, "      %s (%d)\n", nfn->error_file->filename,
				nfn->count);
		}
	}
}

void wandering_found(struct top_tag_node *tag, const char * filename)
{
	struct top_not_found_node * tnfn, *prev = NULL;
	char *b = basename(filename);
	char * p;


	for (tnfn = tag->wandering_base; tnfn; tnfn = tnfn->next) {
		if (strcmp(b, tnfn->basename) == 0) {
			/*
			 * Here, we need to move the tree over to the
			 * list of wandering problems found
			 */
			/* Copy just the dirname where we found it*/
			if ((p = strrchr(filename, '/'))) {
				memset(tnfn->found_path, '\0', FILENAME_LENGTH);
				memcpy(tnfn->found_path, filename, p-filename);
			}

			if (prev) {
				prev->next = tnfn->next;
			} else {
				tag->wandering_base = tnfn->next;
			}
			tnfn->next = tag->wandering_problems_base;
			tag->wandering_problems_base = tnfn;
			return;
		}
		prev = tnfn;
	}
}

void wandering_add_error(struct top_tag_node * tag,
	struct error_file_node * tf)
{
	unsigned int count = 0;
	struct top_not_found_node * tnfn, *prev = NULL, * next;
	struct not_found_node * nfn;
	char * b;

	b = basename(tf->filename);
	/* First, see if we have the basename somewhere */

	for (tnfn = tag->wandering_base; tnfn; tnfn = tnfn->next) {
		if (strncmp(tnfn->basename, b, FILENAME_LENGTH) == 0)
			goto found_basename;
		count++;
		prev = tnfn;
	}
	tnfn = malloc(sizeof(*tnfn));
	memset(tnfn, 0, sizeof(*tnfn));
	strncpy(tnfn->basename, b, FILENAME_LENGTH);
	tnfn->next = NULL;

	if (prev) {
		prev->next = tnfn;
	} else {
		tag->wandering_base = tnfn;
	}

	if (count > MAX_WANDERING_LISTS) {
		next =
		    ((struct top_not_found_node *)(tag->wandering_base))->next;
		not_found_free(tag->wandering_base);
		tag->wandering_base = next;
	}

found_basename:

	/* now see if we have a reference for the full path name */

	for (nfn = tnfn->not_found_base; nfn; nfn = nfn->next) {
		if (nfn->error_file == tf) {
			goto found_node;
		}
	}
	nfn = malloc(sizeof(*nfn));
	memset(nfn, 0, sizeof(*nfn));
	nfn->error_file = tf;
	nfn->next = tnfn->not_found_base;
	tnfn->not_found_base = nfn;

found_node:
	nfn->count++;
	return;
}