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
5 Copyright (C) 1999 Free Software Foundation
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.
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.
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.
22 Author: Ettore Perazzoli <ettore@comm2000.it>
26 #include "gnome-vfs-file-info.h"
28 #include <glib/gmessages.h>
29 #include <glib/gstrfuncs.h>
30 #include <glib/gthread.h>
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;
40 * gnome_vfs_file_info_new
42 * Allocate and initialize a new file information struct.
44 * Returns: A pointer to the new file information struct.
47 gnome_vfs_file_info_new (void)
49 GnomeVFSFileInfo *new;
51 new = g_new0 (GnomeVFSFileInfo, 1);
53 /* `g_new0()' is enough to initialize everything (we just want
54 all the members to be set to zero). */
63 * gnome_vfs_file_info_ref
64 * @info: Pointer to a file information struct
66 * Increment reference count
69 gnome_vfs_file_info_ref (GnomeVFSFileInfo *info)
71 g_return_if_fail (info != NULL);
72 g_return_if_fail (info->refcount > 0);
74 g_static_mutex_lock (&file_info_ref_lock);
76 g_static_mutex_unlock (&file_info_ref_lock);
81 * gnome_vfs_file_info_unref
82 * @info: Pointer to a file information struct
87 gnome_vfs_file_info_unref (GnomeVFSFileInfo *info)
89 g_return_if_fail (info != NULL);
90 g_return_if_fail (info->refcount > 0);
92 g_static_mutex_lock (&file_info_ref_lock);
94 g_static_mutex_unlock (&file_info_ref_lock);
96 if (info->refcount == 0) {
97 gnome_vfs_file_info_clear (info);
104 * gnome_vfs_file_info_clear
105 * @info: Pointer to a file information struct
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.
112 gnome_vfs_file_info_clear (GnomeVFSFileInfo *info)
116 g_return_if_fail (info != NULL);
119 g_free (info->symlink_name);
120 g_free (info->mime_type);
122 /* Ensure the ref count is maintained correctly */
123 g_static_mutex_lock (&file_info_ref_lock);
125 old_refcount = info->refcount;
126 memset (info, 0, sizeof (*info));
127 info->refcount = old_refcount;
129 g_static_mutex_unlock (&file_info_ref_lock);
134 * gnome_vfs_file_info_get_mime_type
135 * @info: A pointer to a file information struct
137 * Retrieve MIME type from @info. There is no need to free the return
140 * Returns: A pointer to a string representing the MIME type.
143 gnome_vfs_file_info_get_mime_type (GnomeVFSFileInfo *info)
145 g_return_val_if_fail (info != NULL, NULL);
147 return info->mime_type;
151 * gnome_vfs_file_info_copy
152 * @dest: Pointer to a struct to copy @src's information into
153 * @src: Pointer to the information to be copied into @dest
155 * Copy information from @src into @dest.
158 gnome_vfs_file_info_copy (GnomeVFSFileInfo *dest,
159 const GnomeVFSFileInfo *src)
163 g_return_if_fail (dest != NULL);
164 g_return_if_fail (src != NULL);
166 /* The primary purpose of this lock is to guarentee that the
167 * refcount is correctly maintained, not to make the copy
168 * atomic. If you want to make the copy atomic, you probably
169 * want serialize access differently (or perhaps you shouldn't
172 g_static_mutex_lock (&file_info_ref_lock);
174 old_refcount = dest->refcount;
176 /* Copy basic information all at once; we will fix pointers later. */
178 memcpy (dest, src, sizeof (*src));
180 /* Duplicate dynamically allocated strings. */
182 dest->name = g_strdup (src->name);
183 dest->symlink_name = g_strdup (src->symlink_name);
184 dest->mime_type = g_strdup (src->mime_type);
186 dest->refcount = old_refcount;
188 g_static_mutex_unlock (&file_info_ref_lock);
193 * gnome_vfs_file_info_dup:
194 * @orig: Pointer to a file information structure to duplicate
196 * Duplicates @orig and returns it.
198 * Returns: a new file information struct that duplicates the information in @orig.
201 gnome_vfs_file_info_dup (const GnomeVFSFileInfo *orig)
203 GnomeVFSFileInfo * ret;
205 g_return_val_if_fail (orig != NULL, NULL);
207 ret = gnome_vfs_file_info_new();
209 gnome_vfs_file_info_copy (ret, orig);
216 * gnome_vfs_file_info_matches
217 * @a: first GnomeVFSFileInfo struct to compare
218 * @b: second GnomeVFSFileInfo struct to compare
220 * Compare the two file info structs, return TRUE if they match.
222 * Returns: TRUE if the two GnomeVFSFileInfos match, otherwise return FALSE.
225 gnome_vfs_file_info_matches (const GnomeVFSFileInfo *a,
226 const GnomeVFSFileInfo *b)
228 g_return_val_if_fail (a != NULL, FALSE);
229 g_return_val_if_fail (b != NULL, FALSE);
230 g_return_val_if_fail (a->name != NULL, FALSE);
231 g_return_val_if_fail (b->name != NULL, FALSE);
233 if (a->type != b->type
234 || a->size != b->size
235 || a->block_count != b->block_count
236 || a->atime != b->atime
237 || a->mtime != b->mtime
238 || a->ctime != b->ctime
239 || strcmp (a->name, b->name) != 0) {
243 if (a->mime_type == NULL || b->mime_type == NULL) {
244 return a->mime_type == b->mime_type;
247 g_assert (a->mime_type != NULL && b->mime_type != NULL);
248 return g_ascii_strcasecmp (a->mime_type, b->mime_type) == 0;
252 * gnome_vfs_file_info_list_ref:
253 * @list: list of GnomeVFSFileInfo elements
255 * Increments the reference count of the items in @list by one.
257 * Return value: @list
260 gnome_vfs_file_info_list_ref (GList *list)
262 g_list_foreach (list, (GFunc) gnome_vfs_file_info_ref, NULL);
267 * gnome_vfs_file_info_list_unref:
268 * @list: list of GnomeVFSFileInfo elements
270 * Decrements the reference count of the items in @list by one.
271 * Note that the list is *not freed* even if each member of the list
274 * Return value: @list
277 gnome_vfs_file_info_list_unref (GList *list)
279 g_list_foreach (list, (GFunc) gnome_vfs_file_info_unref, NULL);
284 * gnome_vfs_file_info_list_copy:
285 * @list: list of GnomeVFSFileInfo elements
287 * Creates a duplicate of @list, and references each member of
290 * Return value: a newly referenced duplicate of @list
293 gnome_vfs_file_info_list_copy (GList *list)
295 return g_list_copy (gnome_vfs_file_info_list_ref (list));
299 * gnome_vfs_file_info_list_free:
300 * @list: list of GnomeVFSFileInfo elements
302 * Decrements the reference count of each member of @list by one,
303 * and frees the list itself.
306 gnome_vfs_file_info_list_free (GList *list)
308 g_list_free (gnome_vfs_file_info_list_unref (list));