captive_file_* -> captive_rtl_file_*
[captive.git] / src / libcaptive / rtl / file.c
1 /* $Id$
2  * Internal (rtl/) file handling utilities of libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
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>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <sys/mman.h>
32 #include <glib/gstring.h>
33 #include <errno.h>
34 #include <glib/galloca.h>
35 #include "reactos/internal/mm.h"        /* for PAGE_SIZE */
36 #include <glib/gmem.h>
37
38
39 /* map: address -> GSIZE_TO_POINTER(length) */
40 static GHashTable *captive_rtl_file_mmap_hash;
41
42 /* initialize 'captive_rtl_file_mmap_hash' */
43 static void captive_rtl_file_mmap_hash_init(void)
44 {
45         if (captive_rtl_file_mmap_hash)
46                 return;
47         captive_rtl_file_mmap_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
48 }
49
50 /**
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.
57  *
58  * mmap(2) the whole file into memory.
59  *
60  * Returns: address base with mmap(2)ed file (and @lenp filled) or #NULL if error.
61  */
62 gpointer captive_rtl_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags)
63 {
64 gpointer r;
65 int fd;
66 size_t len;
67 off_t lenoff;
68
69         captive_rtl_file_mmap_hash_init();
70
71         /* FIXME: use g_filename_from_utf8() on 'path' */
72         fd=open(path,open_flags);
73         if (fd==-1) {
74                 g_error("captive_rtl_file_mmap(\"%s\"): open: %m",path);
75                 goto err;
76                 }
77
78         lenoff=lseek(fd,0,SEEK_END);
79         if (lenoff==(off_t)-1) {
80                 g_assert_not_reached();
81                 goto err_close;
82                 }
83         len=(size_t)lenoff;
84         if (lenp)
85                 *lenp=len;
86
87         r=mmap(
88                         0,      /* start */
89                         len,    /* len */
90                         mmap_prot,
91                         mmap_flags,
92                         fd,
93                         0       /* offset */
94                         );
95         if (r==NULL) {
96                 g_assert_not_reached();
97                 goto err_close;
98                 }
99
100         if (close(fd)) {
101                 g_assert_not_reached();
102                 goto err_munmap;
103                 }
104
105         /* should make no difference with g_hash_table_replace()
106          * as we are using (g_direct_hash,g_direct_equal)
107          */
108         g_hash_table_insert(captive_rtl_file_mmap_hash,
109                         r,GSIZE_TO_POINTER(len));       /* key,value */
110
111         return r;
112
113 err_munmap:
114         /* errors ignored */
115         munmap(r,len);
116 err_close:
117         /* errors ignored */
118         close(fd);
119 err:
120         g_return_val_if_reached(NULL);
121 }
122
123 /**
124  * captive_rtl_file_munmap:
125  * @base: file base address returned by captive_rtl_file_mmap(). %NULL pointer forbidden.
126  *
127  * munmap(2) the whole file and destroy all its resources.
128  */
129 void captive_rtl_file_munmap(gpointer base)
130 {
131 gpointer len_ptr;       /* GSIZE_TO_POINTER(off_t len) */
132 gboolean found;
133
134         g_return_if_fail(base!=NULL);
135
136         captive_rtl_file_mmap_hash_init();
137
138         /* report if 'base' not found */
139         found=g_hash_table_lookup_extended(captive_rtl_file_mmap_hash,
140                         base,   /* lookup_key */
141                         NULL,   /* orig_key */
142                         &len_ptr);      /* value */
143         g_return_if_fail(found==TRUE);
144
145         /* remove 'base' from our database */
146         found=g_hash_table_remove(captive_rtl_file_mmap_hash,
147                         base);  /* key */
148         g_return_if_fail(found==TRUE);  /* just a warning would be also possible */
149
150         /* munmap() has no return value */
151         munmap(base,GPOINTER_TO_SIZE(len_ptr));
152 }
153
154
155 /**
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.
160  *
161  * Reads the whole file into memory.
162  *
163  * Returns: address base of the memory being filled with file contents.
164  * Free the area by g_free() if no longer used.
165  */
166 gpointer captive_rtl_file_read(gint fd,gsize *bufsizep)
167 {
168 off_t lenoff,gotoff;
169 GString *r_GString;
170 void *buf;
171 ssize_t readgot;
172
173         errno=0;
174         lenoff=lseek(fd,0,SEEK_END);
175         switch (lenoff) {
176                 case (off_t)-1:
177                         if (errno!=ESPIPE)
178                                 g_assert_not_reached();
179                         lenoff=PAGE_SIZE;
180                         break;
181                 case 0:
182                         return NULL;
183                 default:
184                         gotoff=lseek(fd,0,SEEK_SET);
185                         g_assert(gotoff==0);
186                 }
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);
192                 }
193         g_assert(readgot==0);    /* EOF */
194         g_free(buf);
195
196         if (bufsizep)
197                 *bufsizep=r_GString->len;
198         return g_string_free(r_GString,
199                         FALSE); /* free_segment */
200 }