First set of licensing changes
[mono.git] / mono / utils / mono-filemap.c
1 /*
2  * mono-filemap.c: Unix/Windows implementation for filemap.
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2008-2008 Novell, Inc.
8  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 #include "config.h"
12
13 #if HAVE_SYS_STAT_H
14 #include <sys/stat.h>
15 #endif
16 #include <fcntl.h>
17 #include <string.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include "mono-mmap.h"
25
26 MonoFileMap *
27 mono_file_map_open (const char* name)
28 {
29 #ifdef WIN32
30         gunichar2 *wname = g_utf8_to_utf16 (name, -1, 0, 0, 0);
31         MonoFileMap *result;
32
33         if (wname == NULL)
34                 return NULL;
35         result = (MonoFileMap *) _wfopen ((wchar_t *) wname, L"rb");
36         g_free (wname);
37         return result;
38 #else
39         int fd = open (name, O_RDONLY);
40         if (fd < 0)
41                 return NULL;
42         return (MonoFileMap *)(size_t)fd;
43 #endif
44 }
45
46 guint64 
47 mono_file_map_size (MonoFileMap *fmap)
48 {
49         struct stat stat_buf;
50         if (fstat (mono_file_map_fd (fmap), &stat_buf) < 0)
51                 return 0;
52         return stat_buf.st_size;
53 }
54
55 int
56 mono_file_map_fd (MonoFileMap *fmap)
57 {
58 #ifdef WIN32
59         return fileno ((FILE*)fmap);
60 #else
61         return (int)(size_t)fmap;
62 #endif
63 }
64
65 int 
66 mono_file_map_close (MonoFileMap *fmap)
67 {
68 #ifdef WIN32
69         return fclose ((FILE*)fmap);
70 #else
71         return close (mono_file_map_fd (fmap));
72 #endif
73 }
74
75 #if !defined (HOST_WIN32)
76
77 static mono_file_map_alloc_fn alloc_fn = (mono_file_map_alloc_fn) malloc;
78 static mono_file_map_release_fn release_fn = (mono_file_map_release_fn) free;
79
80 void
81 mono_file_map_set_allocator (mono_file_map_alloc_fn alloc, mono_file_map_release_fn release)
82 {
83         alloc_fn = alloc == NULL     ? (mono_file_map_alloc_fn) malloc : alloc;
84         release_fn = release == NULL ? (mono_file_map_release_fn) free : release;
85 }
86
87 void *
88 mono_file_map_fileio (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
89 {
90         guint64 cur_offset;
91         size_t bytes_read;
92         void *ptr = (*alloc_fn) (length);
93         if (!ptr)
94                 return NULL;
95         cur_offset = lseek (fd, 0, SEEK_CUR);
96         if (lseek (fd, offset, SEEK_SET) != offset) {
97                 (*release_fn) (ptr);
98                 return NULL;
99         }
100         bytes_read = read (fd, ptr, length);
101         if (bytes_read != length)
102                 return NULL;
103         lseek (fd, cur_offset, SEEK_SET);
104         *ret_handle = NULL;
105         return ptr;
106 }
107
108 int
109 mono_file_unmap_fileio (void *addr, void *handle)
110 {
111         (*release_fn) (addr);
112         return 0;
113 }
114 #if !defined(HAVE_MMAP)
115 void *
116 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
117 {
118         return mono_file_map_fileio (length, flags, fd, offset, ret_handle);
119 }
120
121 int
122 mono_file_unmap (void *addr, void *handle)
123 {
124         return mono_file_unmap_fileio(addr, handle);
125 }
126 #endif
127 #endif