2 * Internal (rtl/) 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/rtl-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>
32 #include <glib/gstring.h>
34 #include <glib/galloca.h>
35 #include "reactos/internal/mm.h" /* for PAGE_SIZE */
36 #include <glib/gmem.h>
39 /* map: address -> GSIZE_TO_POINTER(length) */
40 static GHashTable *captive_rtl_file_mmap_hash;
42 /* initialize 'captive_rtl_file_mmap_hash' */
43 static void captive_rtl_file_mmap_hash_init(void)
45 if (captive_rtl_file_mmap_hash)
47 captive_rtl_file_mmap_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
51 * captive_rtl_file_mmap:
52 * @lenp: returns the file length if successful. %NULL pointer permitted.
53 * @path: File pathname to open(2).
54 * @open_flags: open(2) parameter #flags such as %O_RDONLY.
55 * @mmap_prot: mmap(2) parameter #prot such as %PROT_READ.
56 * @mmap_flags: mmap(2) parameter #flags such as %MAP_SHARED.
58 * mmap(2) the whole file into memory.
60 * Returns: address base with mmap(2)ed file (and @lenp filled) or #NULL if error.
62 gpointer captive_rtl_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags)
69 captive_rtl_file_mmap_hash_init();
71 /* FIXME: use g_filename_from_utf8() on 'path' */
72 fd=open(path,open_flags);
74 g_error("captive_rtl_file_mmap(\"%s\"): open: %m",path);
78 lenoff=lseek(fd,0,SEEK_END);
79 if (lenoff==(off_t)-1) {
80 g_assert_not_reached();
96 g_assert_not_reached();
101 g_assert_not_reached();
105 /* should make no difference with g_hash_table_replace()
106 * as we are using (g_direct_hash,g_direct_equal)
108 g_hash_table_insert(captive_rtl_file_mmap_hash,
109 r,GSIZE_TO_POINTER(len)); /* key,value */
120 g_return_val_if_reached(NULL);
124 * captive_rtl_file_munmap:
125 * @base: file base address returned by captive_rtl_file_mmap(). %NULL pointer forbidden.
127 * munmap(2) the whole file and destroy all its resources.
129 void captive_rtl_file_munmap(gpointer base)
131 gpointer len_ptr; /* GSIZE_TO_POINTER(off_t len) */
134 g_return_if_fail(base!=NULL);
136 captive_rtl_file_mmap_hash_init();
138 /* report if 'base' not found */
139 found=g_hash_table_lookup_extended(captive_rtl_file_mmap_hash,
140 base, /* lookup_key */
142 &len_ptr); /* value */
143 g_return_if_fail(found==TRUE);
145 /* remove 'base' from our database */
146 found=g_hash_table_remove(captive_rtl_file_mmap_hash,
148 g_return_if_fail(found==TRUE); /* just a warning would be also possible */
150 /* munmap() has no return value */
151 munmap(base,GPOINTER_TO_SIZE(len_ptr));
156 * captive_rtl_file_read:
157 * @fd: file-descriptor to read data from.
158 * @bufsizep: Returns the size of resulting data.
159 * %NULL pointer is permitted.
161 * Reads the whole file into memory.
163 * Returns: address base of the memory being filled with file contents.
164 * Free the area by g_free() if no longer used.
166 gpointer captive_rtl_file_read(gint fd,gsize *bufsizep)
174 lenoff=lseek(fd,0,SEEK_END);
178 g_assert_not_reached();
184 gotoff=lseek(fd,0,SEEK_SET);
187 r_GString=g_string_sized_new(lenoff);
188 buf=g_malloc(lenoff); /* do not use g_alloca() - the buffer may be big! */
189 while ((readgot=read(fd,buf,lenoff))>0) {
190 g_assert(readgot<=lenoff);
191 r_GString=g_string_append_len(r_GString,buf,readgot);
193 g_assert(readgot==0); /* EOF */
197 *bufsizep=r_GString->len;
198 return g_string_free(r_GString,
199 FALSE); /* free_segment */