File: tm_file_entry.c

package info (click to toggle)
geany 1.22%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,444 kB
  • sloc: ansic: 76,211; cpp: 42,611; sh: 11,368; makefile: 650; perl: 60; python: 58; xml: 56; lisp: 48; ada: 48; fortran: 47; cs: 45; java: 41; tcl: 35; asm: 29; sql: 28; ruby: 6; php: 1
file content (282 lines) | stat: -rw-r--r-- 6,752 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
*
*   Copyright (c) 2001-2002, Biswapesh Chattopadhyay
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License.
*
*/

#include "general.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_FNMATCH_H
# include <fnmatch.h>
#endif
#include <glib/gstdio.h>

#include "tm_work_object.h"
#include "tm_file_entry.h"


#define FILE_NEW(T)		((T) = g_slice_new0(TMFileEntry))
#define FILE_FREE(T)	g_slice_free(TMFileEntry, (T))


void tm_file_entry_print(TMFileEntry *entry, gpointer __unused__ user_data
  , guint level)
{
	guint i;

	g_return_if_fail(entry);
	for (i=0; i < level; ++i)
		fputc('\t', stderr);
	fprintf(stderr, "%s\n", entry->name);
}

gint tm_file_entry_compare(TMFileEntry *e1, TMFileEntry *e2)
{
	g_return_val_if_fail(e1 && e2 && e1->name && e2->name, 0);
#ifdef TM_DEBUG
	g_message("Comparing %s and %s", e1->name, e2->name);
#endif
	return strcmp(e1->name, e2->name);
}

/* TTimo - modified to handle symlinks */
static TMFileType tm_file_entry_type(const char *path)
{
	struct stat s;

#ifndef G_OS_WIN32
	if (0 != g_lstat(path, &s))
		return tm_file_unknown_t;
#endif
	if S_ISDIR(s.st_mode)
		return tm_file_dir_t;
#ifndef G_OS_WIN32
	else if (S_ISLNK(s.st_mode))
		return tm_file_link_t;
#endif
	else if S_ISREG(s.st_mode)
		return tm_file_regular_t;
	else
		return tm_file_unknown_t;
}

static gboolean apply_filter(const char *name, GList *match, GList *unmatch
  , gboolean ignore_hidden)
{
	GList *tmp;
	gboolean matched = (match == NULL);
	g_return_val_if_fail(name, FALSE);
	if (ignore_hidden && ('.' == name[0]))
		return FALSE;
	/* TTimo - ignore .svn directories */
	if (!strcmp(name, ".svn"))
		return FALSE;
	for (tmp = match; tmp; tmp = g_list_next(tmp))
	{
		if (0 == fnmatch((char *) tmp->data, name, 0))
		{
			matched = TRUE;
			break;
		}
	}
	if (!matched)
		return FALSE;
	for (tmp = unmatch; tmp; tmp = g_list_next(tmp))
	{
		if (0 == fnmatch((char *) tmp->data, name, 0))
		{
			return FALSE;
		}
	}
	return matched;
}

TMFileEntry *tm_file_entry_new(const char *path, TMFileEntry *parent
  , gboolean recurse, GList *file_match, GList *file_unmatch
  , GList *dir_match, GList *dir_unmatch, gboolean ignore_hidden_files
  , gboolean ignore_hidden_dirs)
{
	TMFileEntry *entry;
	/* GList *tmp; */
	char *real_path;
	DIR *dir;
	struct dirent *dir_entry;
	TMFileEntry *new_entry;
	char *file_name;
	struct stat s;
	char *entries = NULL;

	g_return_val_if_fail (path != NULL, NULL);

	/* TTimo - don't follow symlinks */
	if (tm_file_entry_type(path) == tm_file_link_t)
		return NULL;
	real_path = tm_get_real_path(path);
	if (!real_path)
		return NULL;
	FILE_NEW(entry);
	entry->type = tm_file_entry_type(real_path);
	entry->parent = parent;
	entry->path = real_path;
	entry->name = strrchr(entry->path, '/');
	if (entry->name)
		++ (entry->name);
	else
		entry->name = entry->path;
	switch(entry->type)
	{
		case tm_file_unknown_t:
			g_free(real_path);
			FILE_FREE(entry);
			return NULL;
		case tm_file_link_t:
		case tm_file_regular_t:
			if (parent && !apply_filter(entry->name, file_match, file_unmatch
			  , ignore_hidden_files))
			{
				tm_file_entry_free(entry);
				return NULL;
			}
			break;
		case tm_file_dir_t:
			if (parent && !(recurse && apply_filter(entry->name, dir_match
			  , dir_unmatch, ignore_hidden_dirs)))
			{
				tm_file_entry_free(entry);
				return NULL;
			}
			file_name = g_strdup_printf("%s/CVS/Entries", entry->path);
			if (0 == g_stat(file_name, &s))
			{
				if (S_ISREG(s.st_mode))
				{
					int fd;
					entries = g_new(char, s.st_size + 2);
					if (0 > (fd = open(file_name, O_RDONLY)))
					{
						g_free(entries);
						entries = NULL;
					}
					else
					{
						off_t n =0;
						off_t total_read = 1;
						while (0 < (n = read(fd, entries + total_read, s.st_size - total_read)))
							total_read += n;
						entries[s.st_size] = '\0';
						entries[0] = '\n';
						close(fd);
						entry->version = g_strdup("D");
					}
				}
			}
			g_free(file_name);
			if (NULL != (dir = opendir(entry->path)))
			{
				while (NULL != (dir_entry = readdir(dir)))
				{
					if ((0 == strcmp(dir_entry->d_name, "."))
					  || (0 == strcmp(dir_entry->d_name, "..")))
						continue;
					file_name = g_strdup_printf("%s/%s", entry->path, dir_entry->d_name);
					new_entry = tm_file_entry_new(file_name, entry, recurse
					  , file_match, file_unmatch, dir_match, dir_unmatch
			  		  , ignore_hidden_files, ignore_hidden_dirs);
					g_free(file_name);
					if (new_entry)
					{
						if (entries)
						{
							char *str = g_strconcat("\n/", new_entry->name, "/", NULL);
							char *name_pos = strstr(entries, str);
							if (NULL != name_pos)
							{
								int len = strlen(str);
								char *version_pos = strchr(name_pos + len, '/');
								if (NULL != version_pos)
								{
									*version_pos = '\0';
									new_entry->version = g_strdup(name_pos + len);
									*version_pos = '/';
								}
							}
							g_free(str);
						}
						entry->children = g_slist_prepend(entry->children, new_entry);
					}
				}
			}
			closedir(dir);
			entry->children = g_slist_sort(entry->children, (GCompareFunc) tm_file_entry_compare);
			g_free(entries);
			break;
	}
	return entry;
}

void tm_file_entry_free(gpointer entry)
{
	if (entry)
	{
		TMFileEntry *file_entry = TM_FILE_ENTRY(entry);
		if (file_entry->children)
		{
			GSList *tmp;
			for (tmp = file_entry->children; tmp; tmp = g_slist_next(tmp))
				tm_file_entry_free(tmp->data);
			g_slist_free(file_entry->children);
		}
		g_free(file_entry->version);
		g_free(file_entry->path);
		FILE_FREE(file_entry);
	}
}

void tm_file_entry_foreach(TMFileEntry *entry, TMFileEntryFunc func
  , gpointer user_data, guint level, gboolean reverse)
{
	g_return_if_fail (entry != NULL);
	g_return_if_fail (func != NULL);

	if ((reverse) && (entry->children))
	{
		GSList *tmp;
		for (tmp = entry->children; tmp; tmp = g_slist_next(tmp))
			tm_file_entry_foreach(TM_FILE_ENTRY(tmp->data), func
			  , user_data, level + 1, TRUE);
	}
	func(entry, user_data, level);
	if ((!reverse) && (entry->children))
	{
		GSList *tmp;
		for (tmp = entry->children; tmp; tmp = g_slist_next(tmp))
			tm_file_entry_foreach(TM_FILE_ENTRY(tmp->data), func
			  , user_data, level + 1, FALSE);
	}
}

GList *tm_file_entry_list(TMFileEntry *entry, GList *files)
{
	GSList *tmp;
	files = g_list_prepend(files, g_strdup(entry->path));
	for (tmp = entry->children; tmp; tmp = g_slist_next(tmp))
	{
		files = tm_file_entry_list((TMFileEntry *) tmp->data, files);
	}
	if (!files)
		files = g_list_reverse(files);
	return files;
}