2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
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
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #define _FILE_OFFSET_BITS 64
21 #define _LARGEFILE_SOURCE
22 #define _LARGEFILE64_SOURCE
46 static struct file *file_list;
48 static GHashTable *file_name_hash;
49 static int file_name_id;
50 static struct cache *file_cache;
52 struct file_cache_id {
60 file_create(char *name)
63 struct file *file= g_new0(struct file,1);
67 file->fd=open(name, O_RDONLY|O_LARGEFILE | O_BINARY);
72 fstat(file->fd, &stat);
73 file->size=stat.st_size;
74 file->name = g_strdup(name);
75 dbg_assert(file != NULL);
81 int file_is_dir(char *name)
84 if (! stat(name, &buf)) {
85 return S_ISDIR(buf.st_mode);
91 int file_mkdir(char *name, int pflag)
93 char buffer[strlen(name)+1];
96 dbg(1,"enter %s %d\n",name,pflag);
98 if (file_is_dir(name))
100 return mkdir(name, 0777);
102 strcpy(buffer, name);
104 while ((next=strchr(next, '/'))) {
107 ret=file_mkdir(buffer, 0);
115 return file_mkdir(buffer, 0);
119 file_mmap(struct file *file)
121 #if defined(_WIN32) || defined(__CEGCC__)
122 file->begin = (char*)mmap_readonly_win32( file->name, &file->map_handle, &file->map_file );
124 file->begin=mmap(NULL, file->size, PROT_READ|PROT_WRITE, MAP_PRIVATE, file->fd, 0);
125 dbg_assert(file->begin != NULL);
126 if (file->begin == (void *)0xffffffff) {
131 dbg_assert(file->begin != (void *)0xffffffff);
132 file->end=file->begin+file->size;
138 file_data_read(struct file *file, long long offset, int size)
142 return file->begin+offset;
144 struct file_cache_id id={offset,size,file->name_id,0};
145 ret=cache_lookup(file_cache,&id);
148 ret=cache_insert_new(file_cache,&id,size);
151 lseek(file->fd, offset, SEEK_SET);
152 if (read(file->fd, ret, size) != size) {
153 file_data_free(file, ret);
161 uncompress_int(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
166 stream.next_in = (Bytef*)source;
167 stream.avail_in = (uInt)sourceLen;
168 stream.next_out = dest;
169 stream.avail_out = (uInt)*destLen;
171 stream.zalloc = (alloc_func)0;
172 stream.zfree = (free_func)0;
174 err = inflateInit2(&stream, -MAX_WBITS);
175 if (err != Z_OK) return err;
177 err = inflate(&stream, Z_FINISH);
178 if (err != Z_STREAM_END) {
180 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
184 *destLen = stream.total_out;
186 err = inflateEnd(&stream);
191 file_data_read_compressed(struct file *file, long long offset, int size, int size_uncomp)
195 uLongf destLen=size_uncomp;
198 struct file_cache_id id={offset,size,file->name_id,1};
199 ret=cache_lookup(file_cache,&id);
202 ret=cache_insert_new(file_cache,&id,size_uncomp);
204 ret=g_malloc(size_uncomp);
205 lseek(file->fd, offset, SEEK_SET);
206 if (read(file->fd, buffer, size) != size) {
210 if (uncompress_int(ret, &destLen, (Bytef *)buffer, size) != Z_OK) {
211 dbg(0,"uncompress failed\n");
220 file_data_free(struct file *file, unsigned char *data)
222 if (file->begin && data >= file->begin && data < file->end)
225 cache_entry_destroy(file_cache, data);
231 file_exists(char *name)
234 if (! stat(name, &buf))
240 file_remap_readonly(struct file *f)
242 #if defined(_WIN32) || defined(__CEGCC__)
245 munmap(f->begin, f->size);
246 begin=mmap(f->begin, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
247 if (f->begin != begin)
248 printf("remap failed\n");
253 file_remap_readonly_all(void)
255 struct file *f=file_list;
258 while (f && limit-- > 0) {
259 file_remap_readonly(f);
265 file_unmap(struct file *f)
267 #if defined(_WIN32) || defined(__CEGCC__)
268 mmap_unmap_win32( f->begin, f->map_handle , f->map_file );
270 munmap(f->begin, f->size);
277 struct file *f=file_list;
280 while (f && limit-- > 0) {
289 file_opendir(char *dir)
295 file_readdir(void *hnd)
306 file_closedir(void *hnd)
312 file_create_caseinsensitive(char *name)
314 char dirname[strlen(name)+1];
320 ret=file_create(name);
324 strcpy(dirname, name);
325 p=dirname+strlen(name);
326 while (p > dirname) {
332 d=file_opendir(dirname);
335 while ((filename=file_readdir(d))) {
336 if (!strcasecmp(filename, p)) {
338 ret=file_create(dirname);
349 file_destroy(struct file *f)
353 if ( f->begin != NULL )
362 struct file_wordexp {
367 struct file_wordexp *
368 file_wordexp_new(const char *pattern)
370 struct file_wordexp *ret=g_new0(struct file_wordexp, 1);
372 ret->err=wordexp(pattern, &ret->we, 0);
374 dbg(0,"wordexp('%s') returned %d\n", pattern, ret->err);
379 file_wordexp_get_count(struct file_wordexp *wexp)
381 return wexp->we.we_wordc;
385 file_wordexp_get_array(struct file_wordexp *wexp)
387 return wexp->we.we_wordv;
391 file_wordexp_destroy(struct file_wordexp *wexp)
400 file_get_param(struct file *file, struct param_list *param, int count)
403 param_add_string("Filename", file->name, ¶m, &count);
404 param_add_hex("Size", file->size, ¶m, &count);
412 file_name_hash=g_hash_table_new(g_str_hash, g_str_equal);
413 file_cache=cache_new(sizeof(struct file_cache_id), 2*1024*1024);