1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* gnome-vfs-file-info.c - Handling of file information for the GNOME |
---|
3 | Virtual File System. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
7 | The Gnome Library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | The Gnome Library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. |
---|
21 | |
---|
22 | Author: Ettore Perazzoli <ettore@comm2000.it> |
---|
23 | */ |
---|
24 | |
---|
25 | #include <config.h> |
---|
26 | #include "gnome-vfs-file-info.h" |
---|
27 | |
---|
28 | #include <string.h> |
---|
29 | #include <glib.h> |
---|
30 | #include "gnome-vfs.h" |
---|
31 | #include "gnome-vfs-private.h" |
---|
32 | |
---|
33 | /* Mutex for making GnomeVFSFileInfo ref's/unref's atomic */ |
---|
34 | /* Note that an atomic increment function (such as is present in NSPR) is preferable */ |
---|
35 | /* FIXME: This mutex is probably not needed and might be causing performance issues */ |
---|
36 | static GStaticMutex file_info_ref_lock = G_STATIC_MUTEX_INIT; |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | * gnome_vfs_file_info_new: |
---|
41 | * |
---|
42 | * Allocate and initialize a new file information struct. |
---|
43 | * |
---|
44 | * Return value: A pointer to the new file information struct. |
---|
45 | **/ |
---|
46 | GnomeVFSFileInfo * |
---|
47 | gnome_vfs_file_info_new (void) |
---|
48 | { |
---|
49 | GnomeVFSFileInfo *new; |
---|
50 | |
---|
51 | new = g_new0 (GnomeVFSFileInfo, 1); |
---|
52 | |
---|
53 | /* `g_new0()' is enough to initialize everything (we just want |
---|
54 | all the members to be set to zero). */ |
---|
55 | |
---|
56 | new->refcount = 1; |
---|
57 | |
---|
58 | return new; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | * gnome_vfs_file_info_ref: |
---|
64 | * @info: Pointer to a file information struct |
---|
65 | * |
---|
66 | * Increment reference count |
---|
67 | **/ |
---|
68 | void |
---|
69 | gnome_vfs_file_info_ref (GnomeVFSFileInfo *info) |
---|
70 | { |
---|
71 | g_return_if_fail (info != NULL); |
---|
72 | g_return_if_fail (info->refcount > 0); |
---|
73 | |
---|
74 | g_static_mutex_lock (&file_info_ref_lock); |
---|
75 | info->refcount += 1; |
---|
76 | g_static_mutex_unlock (&file_info_ref_lock); |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * gnome_vfs_file_info_unref: |
---|
82 | * @info: Pointer to a file information struct |
---|
83 | * |
---|
84 | * Destroy @info |
---|
85 | **/ |
---|
86 | void |
---|
87 | gnome_vfs_file_info_unref (GnomeVFSFileInfo *info) |
---|
88 | { |
---|
89 | g_return_if_fail (info != NULL); |
---|
90 | g_return_if_fail (info->refcount > 0); |
---|
91 | |
---|
92 | g_static_mutex_lock (&file_info_ref_lock); |
---|
93 | info->refcount -= 1; |
---|
94 | g_static_mutex_unlock (&file_info_ref_lock); |
---|
95 | |
---|
96 | if (info->refcount == 0) { |
---|
97 | gnome_vfs_file_info_clear (info); |
---|
98 | g_free (info); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * gnome_vfs_file_info_clear: |
---|
105 | * @info: Pointer to a file information struct |
---|
106 | * |
---|
107 | * Clear @info so that it's ready to accept new data. This is |
---|
108 | * supposed to be used when @info already contains meaningful information which |
---|
109 | * we want to replace. |
---|
110 | **/ |
---|
111 | void |
---|
112 | gnome_vfs_file_info_clear (GnomeVFSFileInfo *info) |
---|
113 | { |
---|
114 | guint old_refcount; |
---|
115 | |
---|
116 | g_return_if_fail (info != NULL); |
---|
117 | |
---|
118 | g_free (info->name); |
---|
119 | g_free (info->symlink_name); |
---|
120 | g_free (info->mime_type); |
---|
121 | |
---|
122 | /* Ensure the ref count is maintained correctly */ |
---|
123 | g_static_mutex_lock (&file_info_ref_lock); |
---|
124 | |
---|
125 | old_refcount = info->refcount; |
---|
126 | memset (info, 0, sizeof (*info)); |
---|
127 | info->refcount = old_refcount; |
---|
128 | |
---|
129 | g_static_mutex_unlock (&file_info_ref_lock); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /** |
---|
134 | * gnome_vfs_file_info_get_mime_type: |
---|
135 | * @info: A pointer to a file information struct |
---|
136 | * |
---|
137 | * Retrieve MIME type from @info. |
---|
138 | * |
---|
139 | * Return value: A pointer to a string representing the MIME type. |
---|
140 | **/ |
---|
141 | const gchar * |
---|
142 | gnome_vfs_file_info_get_mime_type (GnomeVFSFileInfo *info) |
---|
143 | { |
---|
144 | g_return_val_if_fail (info != NULL, NULL); |
---|
145 | |
---|
146 | return info->mime_type; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * gnome_vfs_file_info_copy: |
---|
151 | * @dest: Pointer to a struct to copy @src's information into |
---|
152 | * @src: Pointer to the information to be copied into @dest |
---|
153 | * |
---|
154 | * Copy information from @src into @dest. |
---|
155 | **/ |
---|
156 | void |
---|
157 | gnome_vfs_file_info_copy (GnomeVFSFileInfo *dest, |
---|
158 | const GnomeVFSFileInfo *src) |
---|
159 | { |
---|
160 | guint old_refcount; |
---|
161 | |
---|
162 | g_return_if_fail (dest != NULL); |
---|
163 | g_return_if_fail (src != NULL); |
---|
164 | |
---|
165 | /* The primary purpose of this lock is to guarentee that the |
---|
166 | * refcount is correctly maintained, not to make the copy |
---|
167 | * atomic. If you want to make the copy atomic, you probably |
---|
168 | * want serialize access differently (or perhaps you shouldn't |
---|
169 | * use copy) |
---|
170 | */ |
---|
171 | g_static_mutex_lock (&file_info_ref_lock); |
---|
172 | |
---|
173 | old_refcount = dest->refcount; |
---|
174 | |
---|
175 | /* Copy basic information all at once; we will fix pointers later. */ |
---|
176 | |
---|
177 | memcpy (dest, src, sizeof (*src)); |
---|
178 | |
---|
179 | /* Duplicate dynamically allocated strings. */ |
---|
180 | |
---|
181 | dest->name = g_strdup (src->name); |
---|
182 | dest->symlink_name = g_strdup (src->symlink_name); |
---|
183 | dest->mime_type = g_strdup (src->mime_type); |
---|
184 | |
---|
185 | dest->refcount = old_refcount; |
---|
186 | |
---|
187 | g_static_mutex_unlock (&file_info_ref_lock); |
---|
188 | |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * gnome_vfs_file_info_dup: |
---|
193 | * @orig: Pointer to a file information structure to duplicate |
---|
194 | * |
---|
195 | * Returns a new file information struct that duplicates the information in @orig. |
---|
196 | **/ |
---|
197 | |
---|
198 | GnomeVFSFileInfo * |
---|
199 | gnome_vfs_file_info_dup (const GnomeVFSFileInfo *orig) |
---|
200 | { |
---|
201 | GnomeVFSFileInfo * ret; |
---|
202 | |
---|
203 | g_return_val_if_fail (orig != NULL, NULL); |
---|
204 | |
---|
205 | ret = gnome_vfs_file_info_new(); |
---|
206 | |
---|
207 | gnome_vfs_file_info_copy (ret, orig); |
---|
208 | |
---|
209 | return ret; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | /** |
---|
214 | * gnome_vfs_file_info_matches: |
---|
215 | * |
---|
216 | * Compare the two file info structs, return TRUE if they match. |
---|
217 | **/ |
---|
218 | gboolean |
---|
219 | gnome_vfs_file_info_matches (const GnomeVFSFileInfo *a, |
---|
220 | const GnomeVFSFileInfo *b) |
---|
221 | { |
---|
222 | g_return_val_if_fail (a != NULL, FALSE); |
---|
223 | g_return_val_if_fail (b != NULL, FALSE); |
---|
224 | g_return_val_if_fail (a->name != NULL, FALSE); |
---|
225 | g_return_val_if_fail (b->name != NULL, FALSE); |
---|
226 | |
---|
227 | if (a->type != b->type |
---|
228 | || a->size != b->size |
---|
229 | || a->block_count != b->block_count |
---|
230 | || a->atime != b->atime |
---|
231 | || a->mtime != b->mtime |
---|
232 | || a->ctime != b->ctime |
---|
233 | || strcmp (a->name, b->name) != 0) { |
---|
234 | return FALSE; |
---|
235 | } |
---|
236 | |
---|
237 | if (a->mime_type == NULL || b->mime_type == NULL) { |
---|
238 | return a->mime_type == b->mime_type; |
---|
239 | } |
---|
240 | |
---|
241 | g_assert (a->mime_type != NULL && b->mime_type != NULL); |
---|
242 | return g_strcasecmp (a->mime_type, b->mime_type) == 0; |
---|
243 | } |
---|
244 | |
---|
245 | GList * |
---|
246 | gnome_vfs_file_info_list_ref (GList *list) |
---|
247 | { |
---|
248 | g_list_foreach (list, (GFunc) gnome_vfs_file_info_ref, NULL); |
---|
249 | return list; |
---|
250 | } |
---|
251 | |
---|
252 | GList * |
---|
253 | gnome_vfs_file_info_list_unref (GList *list) |
---|
254 | { |
---|
255 | g_list_foreach (list, (GFunc) gnome_vfs_file_info_unref, NULL); |
---|
256 | return list; |
---|
257 | } |
---|
258 | |
---|
259 | GList * |
---|
260 | gnome_vfs_file_info_list_copy (GList *list) |
---|
261 | { |
---|
262 | return g_list_copy (gnome_vfs_file_info_list_ref (list)); |
---|
263 | } |
---|
264 | |
---|
265 | void |
---|
266 | gnome_vfs_file_info_list_free (GList *list) |
---|
267 | { |
---|
268 | g_list_free (gnome_vfs_file_info_list_unref (list)); |
---|
269 | } |
---|