Remove duplicate NEWS item
[geany-mirror.git] / tagmanager / tm_file_entry.c
blob7ab935ab39e0a2c6ba23ded551ab56edaf922a31
1 /*
3 * Copyright (c) 2001-2002, Biswapesh Chattopadhyay
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 */
10 #include "general.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <dirent.h>
17 #ifdef HAVE_FCNTL_H
18 # include <fcntl.h>
19 #endif
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #ifdef HAVE_FNMATCH_H
23 # include <fnmatch.h>
24 #endif
25 #include <glib/gstdio.h>
27 #include "tm_work_object.h"
28 #include "tm_file_entry.h"
31 #define FILE_NEW(T) ((T) = g_slice_new0(TMFileEntry))
32 #define FILE_FREE(T) g_slice_free(TMFileEntry, (T))
35 void tm_file_entry_print(TMFileEntry *entry, gpointer __unused__ user_data
36 , guint level)
38 guint i;
40 g_return_if_fail(entry);
41 for (i=0; i < level; ++i)
42 fputc('\t', stderr);
43 fprintf(stderr, "%s\n", entry->name);
46 gint tm_file_entry_compare(TMFileEntry *e1, TMFileEntry *e2)
48 g_return_val_if_fail(e1 && e2 && e1->name && e2->name, 0);
49 #ifdef TM_DEBUG
50 g_message("Comparing %s and %s", e1->name, e2->name);
51 #endif
52 return strcmp(e1->name, e2->name);
55 /* TTimo - modified to handle symlinks */
56 static TMFileType tm_file_entry_type(const char *path)
58 struct stat s;
60 #ifndef G_OS_WIN32
61 if (0 != g_lstat(path, &s))
62 return tm_file_unknown_t;
63 #endif
64 if S_ISDIR(s.st_mode)
65 return tm_file_dir_t;
66 #ifndef G_OS_WIN32
67 else if (S_ISLNK(s.st_mode))
68 return tm_file_link_t;
69 #endif
70 else if S_ISREG(s.st_mode)
71 return tm_file_regular_t;
72 else
73 return tm_file_unknown_t;
76 static gboolean apply_filter(const char *name, GList *match, GList *unmatch
77 , gboolean ignore_hidden)
79 GList *tmp;
80 gboolean matched = (match == NULL);
81 g_return_val_if_fail(name, FALSE);
82 if (ignore_hidden && ('.' == name[0]))
83 return FALSE;
84 /* TTimo - ignore .svn directories */
85 if (!strcmp(name, ".svn"))
86 return FALSE;
87 for (tmp = match; tmp; tmp = g_list_next(tmp))
89 if (0 == fnmatch((char *) tmp->data, name, 0))
91 matched = TRUE;
92 break;
95 if (!matched)
96 return FALSE;
97 for (tmp = unmatch; tmp; tmp = g_list_next(tmp))
99 if (0 == fnmatch((char *) tmp->data, name, 0))
101 return FALSE;
104 return matched;
107 TMFileEntry *tm_file_entry_new(const char *path, TMFileEntry *parent
108 , gboolean recurse, GList *file_match, GList *file_unmatch
109 , GList *dir_match, GList *dir_unmatch, gboolean ignore_hidden_files
110 , gboolean ignore_hidden_dirs)
112 TMFileEntry *entry;
113 /* GList *tmp; */
114 char *real_path;
115 DIR *dir;
116 struct dirent *dir_entry;
117 TMFileEntry *new_entry;
118 char *file_name;
119 struct stat s;
120 char *entries = NULL;
122 g_return_val_if_fail (path != NULL, NULL);
124 /* TTimo - don't follow symlinks */
125 if (tm_file_entry_type(path) == tm_file_link_t)
126 return NULL;
127 real_path = tm_get_real_path(path);
128 if (!real_path)
129 return NULL;
130 FILE_NEW(entry);
131 entry->type = tm_file_entry_type(real_path);
132 entry->parent = parent;
133 entry->path = real_path;
134 entry->name = strrchr(entry->path, '/');
135 if (entry->name)
136 ++ (entry->name);
137 else
138 entry->name = entry->path;
139 switch(entry->type)
141 case tm_file_unknown_t:
142 g_free(real_path);
143 FILE_FREE(entry);
144 return NULL;
145 case tm_file_link_t:
146 case tm_file_regular_t:
147 if (parent && !apply_filter(entry->name, file_match, file_unmatch
148 , ignore_hidden_files))
150 tm_file_entry_free(entry);
151 return NULL;
153 break;
154 case tm_file_dir_t:
155 if (parent && !(recurse && apply_filter(entry->name, dir_match
156 , dir_unmatch, ignore_hidden_dirs)))
158 tm_file_entry_free(entry);
159 return NULL;
161 file_name = g_strdup_printf("%s/CVS/Entries", entry->path);
162 if (0 == g_stat(file_name, &s))
164 if (S_ISREG(s.st_mode))
166 int fd;
167 entries = g_new(char, s.st_size + 2);
168 if (0 > (fd = open(file_name, O_RDONLY)))
170 g_free(entries);
171 entries = NULL;
173 else
175 off_t n =0;
176 off_t total_read = 1;
177 while (0 < (n = read(fd, entries + total_read, s.st_size - total_read)))
178 total_read += n;
179 entries[s.st_size] = '\0';
180 entries[0] = '\n';
181 close(fd);
182 entry->version = g_strdup("D");
186 g_free(file_name);
187 if (NULL != (dir = opendir(entry->path)))
189 while (NULL != (dir_entry = readdir(dir)))
191 if ((0 == strcmp(dir_entry->d_name, "."))
192 || (0 == strcmp(dir_entry->d_name, "..")))
193 continue;
194 file_name = g_strdup_printf("%s/%s", entry->path, dir_entry->d_name);
195 new_entry = tm_file_entry_new(file_name, entry, recurse
196 , file_match, file_unmatch, dir_match, dir_unmatch
197 , ignore_hidden_files, ignore_hidden_dirs);
198 g_free(file_name);
199 if (new_entry)
201 if (entries)
203 char *str = g_strconcat("\n/", new_entry->name, "/", NULL);
204 char *name_pos = strstr(entries, str);
205 if (NULL != name_pos)
207 int len = strlen(str);
208 char *version_pos = strchr(name_pos + len, '/');
209 if (NULL != version_pos)
211 *version_pos = '\0';
212 new_entry->version = g_strdup(name_pos + len);
213 *version_pos = '/';
216 g_free(str);
218 entry->children = g_slist_prepend(entry->children, new_entry);
222 closedir(dir);
223 entry->children = g_slist_sort(entry->children, (GCompareFunc) tm_file_entry_compare);
224 g_free(entries);
225 break;
227 return entry;
230 void tm_file_entry_free(gpointer entry)
232 if (entry)
234 TMFileEntry *file_entry = TM_FILE_ENTRY(entry);
235 if (file_entry->children)
237 GSList *tmp;
238 for (tmp = file_entry->children; tmp; tmp = g_slist_next(tmp))
239 tm_file_entry_free(tmp->data);
240 g_slist_free(file_entry->children);
242 g_free(file_entry->version);
243 g_free(file_entry->path);
244 FILE_FREE(file_entry);
248 void tm_file_entry_foreach(TMFileEntry *entry, TMFileEntryFunc func
249 , gpointer user_data, guint level, gboolean reverse)
251 g_return_if_fail (entry != NULL);
252 g_return_if_fail (func != NULL);
254 if ((reverse) && (entry->children))
256 GSList *tmp;
257 for (tmp = entry->children; tmp; tmp = g_slist_next(tmp))
258 tm_file_entry_foreach(TM_FILE_ENTRY(tmp->data), func
259 , user_data, level + 1, TRUE);
261 func(entry, user_data, level);
262 if ((!reverse) && (entry->children))
264 GSList *tmp;
265 for (tmp = entry->children; tmp; tmp = g_slist_next(tmp))
266 tm_file_entry_foreach(TM_FILE_ENTRY(tmp->data), func
267 , user_data, level + 1, FALSE);
271 GList *tm_file_entry_list(TMFileEntry *entry, GList *files)
273 GSList *tmp;
274 files = g_list_prepend(files, g_strdup(entry->path));
275 for (tmp = entry->children; tmp; tmp = g_slist_next(tmp))
277 files = tm_file_entry_list((TMFileEntry *) tmp->data, files);
279 if (!files)
280 files = g_list_reverse(files);
281 return files;