bootstrap
[captive.git] / src / libcaptive / rtl / file.c
1 /* $Id$
2  * 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/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
33
34 /* map: address -> GSIZE_TO_POINTER(length) */
35 static GHashTable *captive_file_mmap_hash;
36
37 /* initialize 'captive_file_mmap_hash' */
38 static void captive_file_mmap_hash_init(void)
39 {
40         if (captive_file_mmap_hash)
41                 return;
42         captive_file_mmap_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
43 }
44
45 /**
46  * captive_file_mmap:
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.
52  *
53  * mmap(2) the whole file into memory.
54  *
55  * Returns: address base with mmap(2)ed file (and @lenp filled) or #NULL if error.
56  */
57 gpointer captive_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags)
58 {
59 gpointer r;
60 int fd;
61 size_t len;
62 off_t lenoff;
63
64         captive_file_mmap_hash_init();
65
66         /* FIXME: use g_filename_from_utf8() on 'path' */
67         fd=open(path,open_flags);
68         if (fd==-1) {
69                 g_error("captive_file_mmap(\"%s\"): open: %m",path);
70                 goto err;
71                 }
72
73         lenoff=lseek(fd,0,SEEK_END);
74         if (lenoff==(off_t)-1) {
75                 g_assert_not_reached();
76                 goto err_close;
77                 }
78         len=(size_t)lenoff;
79         if (lenp)
80                 *lenp=len;
81
82         r=mmap(
83                         0,      /* start */
84                         len,    /* len */
85                         mmap_prot,
86                         mmap_flags,
87                         fd,
88                         0       /* offset */
89                         );
90         if (r==NULL) {
91                 g_assert_not_reached();
92                 goto err_close;
93                 }
94
95         if (close(fd)) {
96                 g_assert_not_reached();
97                 goto err_munmap;
98                 }
99
100         /* should make no difference with g_hash_table_replace()
101          * as we are using (g_direct_hash,g_direct_equal)
102          */
103         g_hash_table_insert(captive_file_mmap_hash,
104                         r,GSIZE_TO_POINTER(len));       /* key,value */
105
106         return r;
107
108 err_munmap:
109         /* errors ignored */
110         munmap(r,len);
111 err_close:
112         /* errors ignored */
113         close(fd);
114 err:
115         g_return_val_if_reached(NULL);
116 }
117
118 /**
119  * captive_file_munmap:
120  * @base: file base address returned by captive_file_mmap(). %NULL pointer forbidden.
121  *
122  * munmap(2) the whole file and destroy all its resources.
123  */
124 void captive_file_munmap(gpointer base)
125 {
126 gpointer len_ptr;       /* GSIZE_TO_POINTER(off_t len) */
127 gboolean found;
128
129         g_return_if_fail(base!=NULL);
130
131         captive_file_mmap_hash_init();
132
133         /* report if 'base' not found */
134         found=g_hash_table_lookup_extended(captive_file_mmap_hash,
135                         base,   /* lookup_key */
136                         NULL,   /* orig_key */
137                         &len_ptr);      /* value */
138         g_return_if_fail(found==TRUE);
139
140         /* remove 'base' from our database */
141         found=g_hash_table_remove(captive_file_mmap_hash,
142                         base);  /* key */
143         g_return_if_fail(found==TRUE);  /* just a warning would be also possible */
144
145         /* munmap() has no return value */
146         munmap(base,GPOINTER_TO_SIZE(len_ptr));
147 }