/
filemap.c
278 lines (238 loc) · 6.96 KB
/
filemap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (C) 2013 Masatoshi Teruya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* filemap.c
* Created by Masatoshi Teruya on 13/05/27.
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <lauxlib.h>
#include <lualib.h>
#define FILEMAP_LUA "filemap"
#define PATH_TMPL "/tmp/filemap-XXXXXX"
#define EMMAP ((void*)-1)
#define ELSEEK ((off_t)-1)
typedef struct {
int fd;
size_t csize;
size_t rsize;
} filemap_t;
#define pusherror(L,msg) (lua_settop(L,0),lua_pushnil(L),lua_pushstring(L,msg))
static int init_lua( lua_State *L )
{
char tmpl[] = PATH_TMPL;
filemap_t *fmap = lua_newuserdata( L, sizeof( filemap_t ) );
if( !fmap ){
return luaL_error( L, "failed to init(): %s", lua_tostring( L, -1 ) );
}
luaL_getmetatable( L, FILEMAP_LUA );
lua_setmetatable( L, -2 );
fmap->csize = 0;
fmap->rsize = 0;
// create descriptor
if( ( fmap->fd = mkstemp( tmpl ) ) == -1 ){
pusherror( L, strerror( errno ) );
return 2;
}
else if( fcntl( fmap->fd, F_SETFD, FD_CLOEXEC ) == -1 ){
close( fmap->fd );
pusherror( L, strerror( errno ) );
return 2;
}
return 1;
}
static filemap_t *fmap_getself( lua_State *L )
{
int argc = lua_gettop( L );
filemap_t *fmap = NULL;
void *udata = NULL;
if( argc && ( udata = lua_touserdata( L, 1 ) ) )
{
// validate metatable
if( lua_getmetatable( L, 1 ) )
{
lua_pushliteral( L, FILEMAP_LUA );
lua_rawget( L, LUA_REGISTRYINDEX );
if( lua_rawequal( L, -1, -2 ) ){
fmap = (filemap_t*)udata;
}
}
}
lua_settop( L, argc );
return fmap;
}
static int fd_lua( lua_State *L )
{
filemap_t *fmap = fmap_getself( L );
if( !fmap ){
pusherror( L, "argument#1 must be instance of " FILEMAP_LUA );
}
else {
lua_pushinteger( L, fmap->fd );
lua_pushnil( L );
}
return 2;
}
static int fmap_append( filemap_t *fmap, int fd, size_t size, size_t *head )
{
int rc = -1;
size_t rsize = fmap->csize + fmap->rsize + size;
// resizing
if( ftruncate( fmap->fd, rsize ) != -1 )
{
char *min = NULL;
// rewind
lseek( fd, 0, SEEK_SET );
min = (void*)mmap( NULL, size, PROT_READ, MAP_SHARED, fd, 0 );
if( min != EMMAP )
{
char *mout = (void*)mmap( NULL, rsize, PROT_WRITE, MAP_SHARED, fmap->fd, 0 );
if( mout != EMMAP )
{
size_t i = 0;
*head = fmap->csize + fmap->rsize;
// copy
for( i = 0; i < size; i++ ){
mout[*head+i] = min[i];
}
msync( mout, rsize, MS_SYNC );
munmap( mout, rsize );
fmap->rsize += size;
rc = 0;
}
munmap( min, size );
}
}
return rc;
}
static int add_lua( lua_State *L )
{
filemap_t *fmap = fmap_getself( L );
char *errmsg = NULL;
if( !fmap ){
errmsg = "argument#1 must be instance of " FILEMAP_LUA;
}
else if( lua_type( L, 2 ) != LUA_TSTRING ){
errmsg = "argument#2 must be filepath";
}
else
{
int fd = 0;
size_t size = 0;
size_t head = 0;
size_t len = 0;
const char *path = lua_tolstring( L, 2, &len );
if( !len ){
errmsg = "argument#2 invalid path lentgth";
}
// append file data
else if( ( fd = open( path, O_RDONLY ) ) == -1 ||
( size = lseek( fd, 0, SEEK_END ) ) == ELSEEK ||
fmap_append( fmap, fd, size, &head ) == -1 ){
errmsg = strerror( errno );
}
else {
lua_newtable( L );
lua_pushstring( L, "head" );
lua_pushinteger( L, head );
lua_rawset( L, -3 );
lua_pushstring( L, "size" );
lua_pushinteger( L, size );
lua_rawset( L, -3 );
lua_pushnil( L );
}
// cleanup
if( fd ){
close( fd );
}
}
if( errmsg ){
pusherror( L, errmsg );
}
return 2;
}
static int rewind_lua( lua_State *L )
{
filemap_t *fmap = lua_touserdata( L, 1 );
// rewind
lseek( fmap->fd, 0, SEEK_SET );
return 0;
}
static int tostring_lua( lua_State *L )
{
filemap_t *fmap = lua_touserdata( L, 1 );
lua_pushfstring( L, FILEMAP_LUA ": %p", fmap );
return 1;
}
static int gc_lua( lua_State *L )
{
filemap_t *fmap = lua_touserdata( L, 1 );
if( fmap->fd ){
close( fmap->fd );
}
return 0;
}
LUALIB_API int luaopen_filemap( lua_State *L )
{
struct luaL_Reg metaReg[] = {
{ "__gc", gc_lua },
{ "__tostring", tostring_lua },
{ NULL, NULL }
};
struct luaL_Reg methodReg[] = {
{ "fd", fd_lua },
{ "add", add_lua },
{ "rewind", rewind_lua },
{ NULL, NULL }
};
int top = lua_gettop( L );
int i = 0;
// metatable for userdata
luaL_newmetatable( L, FILEMAP_LUA );
// add metamethods
for( i = 0; metaReg[i].name; i++ ){
lua_pushstring( L, metaReg[i].name );
lua_pushcfunction( L, metaReg[i].func );
lua_rawset( L, -3 );
}
// add method
lua_pushstring( L, "__index" );
lua_newtable( L );
for( i = 0; methodReg[i].name; i++ ){
lua_pushstring( L, methodReg[i].name );
lua_pushcfunction( L, methodReg[i].func );
lua_rawset( L, -3 );
}
lua_rawset( L, -3 );
lua_settop( L, top );
// exports
lua_newtable( L );
lua_pushstring( L, "init" );
lua_pushcfunction( L, init_lua );
lua_rawset( L, -3 );
return 1;
}