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.
20 #include <sys/types.h>
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
40 g_return_if_fail(entry
);
41 for (i
=0; i
< level
; ++i
)
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);
50 g_message("Comparing %s and %s", e1
->name
, e2
->name
);
52 return strcmp(e1
->name
, e2
->name
);
55 /* TTimo - modified to handle symlinks */
56 static TMFileType
tm_file_entry_type(const char *path
)
61 if (0 != g_lstat(path
, &s
))
62 return tm_file_unknown_t
;
67 else if (S_ISLNK(s
.st_mode
))
68 return tm_file_link_t
;
70 else if S_ISREG(s
.st_mode
)
71 return tm_file_regular_t
;
73 return tm_file_unknown_t
;
76 static gboolean
apply_filter(const char *name
, GList
*match
, GList
*unmatch
77 , gboolean ignore_hidden
)
80 gboolean matched
= (match
== NULL
);
81 g_return_val_if_fail(name
, FALSE
);
82 if (ignore_hidden
&& ('.' == name
[0]))
84 /* TTimo - ignore .svn directories */
85 if (!strcmp(name
, ".svn"))
87 for (tmp
= match
; tmp
; tmp
= g_list_next(tmp
))
89 if (0 == fnmatch((char *) tmp
->data
, name
, 0))
97 for (tmp
= unmatch
; tmp
; tmp
= g_list_next(tmp
))
99 if (0 == fnmatch((char *) tmp
->data
, name
, 0))
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
)
116 struct dirent
*dir_entry
;
117 TMFileEntry
*new_entry
;
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
)
127 real_path
= tm_get_real_path(path
);
131 entry
->type
= tm_file_entry_type(real_path
);
132 entry
->parent
= parent
;
133 entry
->path
= real_path
;
134 entry
->name
= strrchr(entry
->path
, '/');
138 entry
->name
= entry
->path
;
141 case tm_file_unknown_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
);
155 if (parent
&& !(recurse
&& apply_filter(entry
->name
, dir_match
156 , dir_unmatch
, ignore_hidden_dirs
)))
158 tm_file_entry_free(entry
);
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
))
167 entries
= g_new(char, s
.st_size
+ 2);
168 if (0 > (fd
= open(file_name
, O_RDONLY
)))
176 off_t total_read
= 1;
177 while (0 < (n
= read(fd
, entries
+ total_read
, s
.st_size
- total_read
)))
179 entries
[s
.st_size
] = '\0';
182 entry
->version
= g_strdup("D");
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
, "..")))
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
);
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
)
212 new_entry
->version
= g_strdup(name_pos
+ len
);
218 entry
->children
= g_slist_prepend(entry
->children
, new_entry
);
223 entry
->children
= g_slist_sort(entry
->children
, (GCompareFunc
) tm_file_entry_compare
);
230 void tm_file_entry_free(gpointer entry
)
234 TMFileEntry
*file_entry
= TM_FILE_ENTRY(entry
);
235 if (file_entry
->children
)
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
))
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
))
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
)
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
);
280 files
= g_list_reverse(files
);