6a9623aa9f6187e7b4f62f6ba426426de7b1b0ca
[project/luci.git] / libs / lmo / src / lmo_core.c
1 /*
2  * lmo - Lua Machine Objects - Base functions
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #include "lmo.h"
20
21 extern char _lmo_error[1024];
22
23 static int lmo_read32( int fd, uint32_t *val )
24 {
25         if( read(fd, val, 4) < 4 )
26                 return -1;
27
28         *val = ntohl(*val);
29
30         return 4;
31 }
32
33 static char * error(const char *message, int add_errno)
34 {
35         memset(_lmo_error, 0, sizeof(_lmo_error));
36
37         if( add_errno )
38                 snprintf(_lmo_error, sizeof(_lmo_error),
39                         "%s: %s", message, strerror(errno));
40         else
41                 snprintf(_lmo_error, sizeof(_lmo_error), "%s", message);
42
43         return NULL;
44 }
45
46 const char * lmo_error(void)
47 {
48         return _lmo_error;
49 }
50
51 lmo_archive_t * lmo_open(const char *file)
52 {
53         int in = -1;
54         uint32_t idx_offset = 0;
55         uint32_t i;
56         struct stat s;
57
58         lmo_archive_t *ar    = NULL;
59         lmo_entry_t   *head  = NULL;
60         lmo_entry_t   *entry = NULL;
61
62         if( stat(file, &s) == -1 )
63         {
64                 error("Can not stat file", 1);
65                 goto cleanup;
66         }
67
68         if( (in = open(file, O_RDONLY)) == -1 )
69         {
70                 error("Can not open file", 1);
71                 goto cleanup;
72         }
73
74         if( lseek(in, -sizeof(uint32_t), SEEK_END) == -1 )
75         {
76                 error("Can not seek to eof", 1);
77                 goto cleanup;
78         }
79
80         if( lmo_read32(in, &idx_offset) != 4 )
81         {
82                 error("Unexpected EOF while reading index offset", 0);
83                 goto cleanup;
84         }
85
86         if( lseek(in, (off_t)idx_offset, SEEK_SET) == -1 )
87         {
88                 error("Can not seek to index offset", 1);
89                 goto cleanup;
90         }
91
92         if( (ar = (lmo_archive_t *) malloc(sizeof(lmo_archive_t))) != NULL )
93         {
94                 ar->fd     = in;
95                 ar->length = idx_offset;
96
97                 for( i = idx_offset;
98                      i < (s.st_size - sizeof(uint32_t));
99                      i += (4 * sizeof(uint32_t))
100                 ) {
101                         if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
102                         {
103                                 if( (lmo_read32(ar->fd, &entry->key_id) == 4) &&
104                                     (lmo_read32(ar->fd, &entry->val_id) == 4) &&
105                                     (lmo_read32(ar->fd, &entry->offset) == 4) &&
106                                     (lmo_read32(ar->fd, &entry->length) == 4)
107                                 ) {
108                                         entry->next = head;
109                                         head = entry;
110                                 }
111                                 else
112                                 {
113                                         error("Unexpected EOF while reading index entry", 0);
114                                         goto cleanup;
115                                 }
116                         }
117                         else
118                         {
119                                 error("Out of memory", 0);
120                                 goto cleanup;
121                         }
122                 }
123
124                 ar->index = head;
125
126                 if( lseek(ar->fd, 0, SEEK_SET) == -1 )
127                 {
128                         error("Can not seek to start", 1);
129                         goto cleanup;
130                 }
131
132                 if( (ar->mmap = mmap(NULL, ar->length, PROT_READ, MAP_PRIVATE, ar->fd, 0)) == MAP_FAILED )
133                 {
134                         error("Failed to memory map archive contents", 1);
135                         goto cleanup;
136                 }
137
138                 return ar;
139         }
140         else
141         {
142                 error("Out of memory", 0);
143                 goto cleanup;
144         }
145
146
147         cleanup:
148
149         if( in > -1 )
150                 close(in);
151
152         if( head != NULL )
153         {
154                 entry = head;
155
156                 while( entry != NULL )
157                 {
158                         head = entry->next;
159                         free(entry);
160                         entry = head;
161                 }
162
163                 head = entry = NULL;
164         }
165
166         if( ar != NULL )
167         {
168                 if( (ar->mmap != NULL) && (ar->mmap != MAP_FAILED) )
169                         munmap(ar->mmap, ar->length);
170
171                 free(ar);
172                 ar = NULL;
173         }
174
175         return NULL;
176 }
177
178 void lmo_close(lmo_archive_t *ar)
179 {
180         lmo_entry_t *head  = NULL;
181         lmo_entry_t *entry = NULL;
182
183         if( ar != NULL )
184         {
185                 entry = ar->index;
186
187                 while( entry != NULL )
188                 {
189                         head = entry->next;
190                         free(entry);
191                         entry = head;
192                 }
193
194                 head = entry = NULL;
195
196                 if( (ar->mmap != NULL) && (ar->mmap != MAP_FAILED) )
197                         munmap(ar->mmap, ar->length);
198
199                 close(ar->fd);
200                 free(ar);
201
202                 ar = NULL;
203         }
204 }
205
206 int lmo_lookup(lmo_archive_t *ar, const char *key, char *dest, int len)
207 {
208         uint32_t look_key = sfh_hash(key, strlen(key));
209         int copy_len = -1;
210         lmo_entry_t *entry;
211
212         if( !ar )
213                 return copy_len;
214
215         entry = ar->index;
216
217         while( entry != NULL )
218         {
219                 if( entry->key_id == look_key )
220                 {
221                         copy_len = ((len - 1) > entry->length) ? entry->length : (len - 1);
222                         memcpy(dest, &ar->mmap[entry->offset], copy_len);
223                         dest[copy_len] = '\0';
224
225                         break;
226                 }
227
228                 entry = entry->next;
229         }
230
231         return copy_len;
232 }
233