2 * File handling utilities of libcaptive
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "captive/file.h" /* self */
23 #include <glib/gtypes.h>
24 #include <glib/gmessages.h>
25 #include <glib/ghash.h>
26 #include <sys/types.h>
28 #include <sys/types.h>
34 /* map: address -> GSIZE_TO_POINTER(length) */
35 static GHashTable *captive_file_mmap_hash;
37 /* initialize 'captive_file_mmap_hash' */
38 static void captive_file_mmap_hash_init(void)
40 if (captive_file_mmap_hash)
42 captive_file_mmap_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
47 * @lenp: returns the file length if successful. %NULL pointer permitted.
48 * @path: File pathname to open(2).
49 * @open_flags: open(2) parameter #flags such as %O_RDONLY.
50 * @mmap_prot: mmap(2) parameter #prot such as %PROT_READ.
51 * @mmap_flags: mmap(2) parameter #flags such as %MAP_SHARED.
53 * mmap(2) the whole file into memory.
55 * Returns: address base with mmap(2)ed file (and @lenp filled) or #NULL if error.
57 gpointer captive_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags)
64 captive_file_mmap_hash_init();
66 /* FIXME: use g_filename_from_utf8() on 'path' */
67 fd=open(path,open_flags);
69 g_error("captive_file_mmap(\"%s\"): open: %m",path);
73 lenoff=lseek(fd,0,SEEK_END);
74 if (lenoff==(off_t)-1) {
75 g_assert_not_reached();
91 g_assert_not_reached();
96 g_assert_not_reached();
100 /* should make no difference with g_hash_table_replace()
101 * as we are using (g_direct_hash,g_direct_equal)
103 g_hash_table_insert(captive_file_mmap_hash,
104 r,GSIZE_TO_POINTER(len)); /* key,value */
115 g_return_val_if_reached(NULL);
119 * captive_file_munmap:
120 * @base: file base address returned by captive_file_mmap(). %NULL pointer forbidden.
122 * munmap(2) the whole file and destroy all its resources.
124 void captive_file_munmap(gpointer base)
126 gpointer len_ptr; /* GSIZE_TO_POINTER(off_t len) */
129 g_return_if_fail(base!=NULL);
131 captive_file_mmap_hash_init();
133 /* report if 'base' not found */
134 found=g_hash_table_lookup_extended(captive_file_mmap_hash,
135 base, /* lookup_key */
137 &len_ptr); /* value */
138 g_return_if_fail(found==TRUE);
140 /* remove 'base' from our database */
141 found=g_hash_table_remove(captive_file_mmap_hash,
143 g_return_if_fail(found==TRUE); /* just a warning would be also possible */
145 /* munmap() has no return value */
146 munmap(base,GPOINTER_TO_SIZE(len_ptr));