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.
19 #include <sys/types.h>
22 #include "tm_work_object.h"
23 #include "tm_file_entry.h"
25 static GMemChunk
*file_mem_chunk
= NULL
;
27 #define FILE_NEW(T) {\
28 if (!file_mem_chunk) \
29 file_mem_chunk = g_mem_chunk_new("TMFileEntry MemChunk", sizeof(TMFileEntry), 1024 \
30 , G_ALLOC_AND_FREE); \
31 (T) = g_chunk_new0(TMFileEntry, file_mem_chunk);}
33 #define FILE_FREE(T) g_mem_chunk_free(file_mem_chunk, (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
)
60 if (0 != lstat(path
, &s
))
61 return tm_file_unknown_t
;
63 return tm_file_link_t
;
64 else if S_ISDIR(s
.st_mode
)
66 else if S_ISREG(s
.st_mode
)
67 return tm_file_regular_t
;
69 return tm_file_unknown_t
;
72 static gboolean
apply_filter(const char *name
, GList
*match
, GList
*unmatch
73 , gboolean ignore_hidden
)
76 gboolean matched
= (match
== NULL
);
77 g_return_val_if_fail(name
, FALSE
);
78 if (ignore_hidden
&& ('.' == name
[0]))
80 /* TTimo - ignore .svn directories */
81 if (!strcmp(name
, ".svn"))
83 for (tmp
= match
; tmp
; tmp
= g_list_next(tmp
))
85 if (0 == fnmatch((char *) tmp
->data
, name
, 0))
93 for (tmp
= unmatch
; tmp
; tmp
= g_list_next(tmp
))
95 if (0 == fnmatch((char *) tmp
->data
, name
, 0))
103 TMFileEntry
*tm_file_entry_new(const char *path
, TMFileEntry
*parent
104 , gboolean recurse
, GList
*file_match
, GList
*file_unmatch
105 , GList
*dir_match
, GList
*dir_unmatch
, gboolean ignore_hidden_files
106 , gboolean ignore_hidden_dirs
)
112 struct dirent
*dir_entry
;
113 TMFileEntry
*new_entry
;
114 char file_name
[PATH_MAX
];
116 char *entries
= NULL
;
118 g_return_val_if_fail (path
!= NULL
, NULL
);
120 /* TTimo - don't follow symlinks */
121 if (tm_file_entry_type(path
) == tm_file_link_t
)
124 real_path
= tm_get_real_path(path
);
129 entry
->type
= tm_file_entry_type(real_path
);
130 entry
->parent
= parent
;
131 entry
->path
= real_path
;
132 entry
->name
= strrchr(entry
->path
, '/');
136 entry
->name
= entry
->path
;
142 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 g_snprintf(file_name
, PATH_MAX
, "%s/CVS/Entries", entry
->path
);
162 if (0 == 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");
186 if (NULL
!= (dir
= opendir(entry
->path
)))
188 while (NULL
!= (dir_entry
= readdir(dir
)))
190 if ((0 == strcmp(dir_entry
->d_name
, "."))
191 || (0 == strcmp(dir_entry
->d_name
, "..")))
193 g_snprintf(file_name
, PATH_MAX
, "%s/%s", entry
->path
194 , 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
);
202 char *str
= g_strconcat("\n/", new_entry
->name
, "/", NULL
);
203 char *name_pos
= strstr(entries
, str
);
204 if (NULL
!= name_pos
)
206 int len
= strlen(str
);
207 char *version_pos
= strchr(name_pos
+ len
, '/');
208 if (NULL
!= version_pos
)
211 new_entry
->version
= g_strdup(name_pos
+ len
);
217 entry
->children
= g_slist_prepend(entry
->children
, new_entry
);
222 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 if (file_entry
->version
)
243 g_free(file_entry
->version
);
244 g_free(file_entry
->path
);
245 FILE_FREE(file_entry
);
249 void tm_file_entry_foreach(TMFileEntry
*entry
, TMFileEntryFunc func
250 , gpointer user_data
, guint level
, gboolean reverse
)
252 g_return_if_fail (entry
!= NULL
);
253 g_return_if_fail (func
!= NULL
);
255 if ((reverse
) && (entry
->children
))
258 for (tmp
= entry
->children
; tmp
; tmp
= g_slist_next(tmp
))
259 tm_file_entry_foreach(TM_FILE_ENTRY(tmp
->data
), func
260 , user_data
, level
+ 1, TRUE
);
262 func(entry
, user_data
, level
);
263 if ((!reverse
) && (entry
->children
))
266 for (tmp
= entry
->children
; tmp
; tmp
= g_slist_next(tmp
))
267 tm_file_entry_foreach(TM_FILE_ENTRY(tmp
->data
), func
268 , user_data
, level
+ 1, FALSE
);
272 GList
*tm_file_entry_list(TMFileEntry
*entry
, GList
*files
)
275 files
= g_list_prepend(files
, g_strdup(entry
->path
));
276 for (tmp
= entry
->children
; tmp
; tmp
= g_slist_next(tmp
))
278 files
= tm_file_entry_list((TMFileEntry
*) tmp
->data
, files
);
281 files
= g_list_reverse(files
);