sdsykes-ferret 0.11.6.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +102 -0
  4. data/Rakefile +338 -0
  5. data/TODO +17 -0
  6. data/TUTORIAL +231 -0
  7. data/bin/ferret-browser +79 -0
  8. data/ext/analysis.c +1555 -0
  9. data/ext/analysis.h +219 -0
  10. data/ext/api.c +69 -0
  11. data/ext/api.h +27 -0
  12. data/ext/array.c +123 -0
  13. data/ext/array.h +53 -0
  14. data/ext/bitvector.c +540 -0
  15. data/ext/bitvector.h +272 -0
  16. data/ext/compound_io.c +383 -0
  17. data/ext/config.h +42 -0
  18. data/ext/document.c +156 -0
  19. data/ext/document.h +53 -0
  20. data/ext/except.c +120 -0
  21. data/ext/except.h +168 -0
  22. data/ext/extconf.rb +14 -0
  23. data/ext/ferret.c +402 -0
  24. data/ext/ferret.h +91 -0
  25. data/ext/filter.c +156 -0
  26. data/ext/fs_store.c +483 -0
  27. data/ext/global.c +418 -0
  28. data/ext/global.h +117 -0
  29. data/ext/hash.c +567 -0
  30. data/ext/hash.h +473 -0
  31. data/ext/hashset.c +170 -0
  32. data/ext/hashset.h +187 -0
  33. data/ext/header.h +58 -0
  34. data/ext/helper.c +62 -0
  35. data/ext/helper.h +13 -0
  36. data/ext/inc/lang.h +48 -0
  37. data/ext/inc/threading.h +31 -0
  38. data/ext/index.c +6425 -0
  39. data/ext/index.h +961 -0
  40. data/ext/lang.h +66 -0
  41. data/ext/libstemmer.c +92 -0
  42. data/ext/libstemmer.h +79 -0
  43. data/ext/mempool.c +87 -0
  44. data/ext/mempool.h +35 -0
  45. data/ext/modules.h +162 -0
  46. data/ext/multimapper.c +310 -0
  47. data/ext/multimapper.h +51 -0
  48. data/ext/posh.c +1006 -0
  49. data/ext/posh.h +1007 -0
  50. data/ext/priorityqueue.c +151 -0
  51. data/ext/priorityqueue.h +143 -0
  52. data/ext/q_boolean.c +1608 -0
  53. data/ext/q_const_score.c +161 -0
  54. data/ext/q_filtered_query.c +209 -0
  55. data/ext/q_fuzzy.c +268 -0
  56. data/ext/q_match_all.c +148 -0
  57. data/ext/q_multi_term.c +677 -0
  58. data/ext/q_parser.c +2825 -0
  59. data/ext/q_phrase.c +1126 -0
  60. data/ext/q_prefix.c +100 -0
  61. data/ext/q_range.c +350 -0
  62. data/ext/q_span.c +2402 -0
  63. data/ext/q_term.c +337 -0
  64. data/ext/q_wildcard.c +171 -0
  65. data/ext/r_analysis.c +2575 -0
  66. data/ext/r_index.c +3472 -0
  67. data/ext/r_qparser.c +585 -0
  68. data/ext/r_search.c +4105 -0
  69. data/ext/r_store.c +513 -0
  70. data/ext/r_utils.c +963 -0
  71. data/ext/ram_store.c +471 -0
  72. data/ext/search.c +1741 -0
  73. data/ext/search.h +885 -0
  74. data/ext/similarity.c +150 -0
  75. data/ext/similarity.h +82 -0
  76. data/ext/sort.c +983 -0
  77. data/ext/stem_ISO_8859_1_danish.c +338 -0
  78. data/ext/stem_ISO_8859_1_danish.h +16 -0
  79. data/ext/stem_ISO_8859_1_dutch.c +635 -0
  80. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  81. data/ext/stem_ISO_8859_1_english.c +1156 -0
  82. data/ext/stem_ISO_8859_1_english.h +16 -0
  83. data/ext/stem_ISO_8859_1_finnish.c +792 -0
  84. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  85. data/ext/stem_ISO_8859_1_french.c +1276 -0
  86. data/ext/stem_ISO_8859_1_french.h +16 -0
  87. data/ext/stem_ISO_8859_1_german.c +512 -0
  88. data/ext/stem_ISO_8859_1_german.h +16 -0
  89. data/ext/stem_ISO_8859_1_italian.c +1091 -0
  90. data/ext/stem_ISO_8859_1_italian.h +16 -0
  91. data/ext/stem_ISO_8859_1_norwegian.c +296 -0
  92. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  93. data/ext/stem_ISO_8859_1_porter.c +776 -0
  94. data/ext/stem_ISO_8859_1_porter.h +16 -0
  95. data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
  96. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  97. data/ext/stem_ISO_8859_1_spanish.c +1119 -0
  98. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  99. data/ext/stem_ISO_8859_1_swedish.c +307 -0
  100. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  101. data/ext/stem_KOI8_R_russian.c +701 -0
  102. data/ext/stem_KOI8_R_russian.h +16 -0
  103. data/ext/stem_UTF_8_danish.c +344 -0
  104. data/ext/stem_UTF_8_danish.h +16 -0
  105. data/ext/stem_UTF_8_dutch.c +653 -0
  106. data/ext/stem_UTF_8_dutch.h +16 -0
  107. data/ext/stem_UTF_8_english.c +1176 -0
  108. data/ext/stem_UTF_8_english.h +16 -0
  109. data/ext/stem_UTF_8_finnish.c +808 -0
  110. data/ext/stem_UTF_8_finnish.h +16 -0
  111. data/ext/stem_UTF_8_french.c +1296 -0
  112. data/ext/stem_UTF_8_french.h +16 -0
  113. data/ext/stem_UTF_8_german.c +526 -0
  114. data/ext/stem_UTF_8_german.h +16 -0
  115. data/ext/stem_UTF_8_italian.c +1113 -0
  116. data/ext/stem_UTF_8_italian.h +16 -0
  117. data/ext/stem_UTF_8_norwegian.c +302 -0
  118. data/ext/stem_UTF_8_norwegian.h +16 -0
  119. data/ext/stem_UTF_8_porter.c +794 -0
  120. data/ext/stem_UTF_8_porter.h +16 -0
  121. data/ext/stem_UTF_8_portuguese.c +1055 -0
  122. data/ext/stem_UTF_8_portuguese.h +16 -0
  123. data/ext/stem_UTF_8_russian.c +709 -0
  124. data/ext/stem_UTF_8_russian.h +16 -0
  125. data/ext/stem_UTF_8_spanish.c +1137 -0
  126. data/ext/stem_UTF_8_spanish.h +16 -0
  127. data/ext/stem_UTF_8_swedish.c +313 -0
  128. data/ext/stem_UTF_8_swedish.h +16 -0
  129. data/ext/stopwords.c +401 -0
  130. data/ext/store.c +692 -0
  131. data/ext/store.h +777 -0
  132. data/ext/term_vectors.c +352 -0
  133. data/ext/threading.h +31 -0
  134. data/ext/utilities.c +446 -0
  135. data/ext/win32.h +54 -0
  136. data/lib/ferret.rb +29 -0
  137. data/lib/ferret/browser.rb +246 -0
  138. data/lib/ferret/browser/s/global.js +192 -0
  139. data/lib/ferret/browser/s/style.css +148 -0
  140. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  141. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  142. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  143. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  144. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  145. data/lib/ferret/browser/views/layout.rhtml +22 -0
  146. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  147. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  148. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  149. data/lib/ferret/browser/webrick.rb +14 -0
  150. data/lib/ferret/document.rb +130 -0
  151. data/lib/ferret/field_infos.rb +44 -0
  152. data/lib/ferret/index.rb +786 -0
  153. data/lib/ferret/number_tools.rb +157 -0
  154. data/lib/ferret_version.rb +3 -0
  155. data/setup.rb +1555 -0
  156. data/test/test_all.rb +5 -0
  157. data/test/test_helper.rb +24 -0
  158. data/test/threading/number_to_spoken.rb +132 -0
  159. data/test/threading/thread_safety_index_test.rb +79 -0
  160. data/test/threading/thread_safety_read_write_test.rb +76 -0
  161. data/test/threading/thread_safety_test.rb +133 -0
  162. data/test/unit/analysis/tc_analyzer.rb +548 -0
  163. data/test/unit/analysis/tc_token_stream.rb +646 -0
  164. data/test/unit/index/tc_index.rb +762 -0
  165. data/test/unit/index/tc_index_reader.rb +699 -0
  166. data/test/unit/index/tc_index_writer.rb +437 -0
  167. data/test/unit/index/th_doc.rb +315 -0
  168. data/test/unit/largefile/tc_largefile.rb +46 -0
  169. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  170. data/test/unit/search/tc_filter.rb +135 -0
  171. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  172. data/test/unit/search/tc_index_searcher.rb +61 -0
  173. data/test/unit/search/tc_multi_searcher.rb +128 -0
  174. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  175. data/test/unit/search/tc_search_and_sort.rb +179 -0
  176. data/test/unit/search/tc_sort.rb +49 -0
  177. data/test/unit/search/tc_sort_field.rb +27 -0
  178. data/test/unit/search/tc_spans.rb +190 -0
  179. data/test/unit/search/tm_searcher.rb +384 -0
  180. data/test/unit/store/tc_fs_store.rb +77 -0
  181. data/test/unit/store/tc_ram_store.rb +35 -0
  182. data/test/unit/store/tm_store.rb +34 -0
  183. data/test/unit/store/tm_store_lock.rb +68 -0
  184. data/test/unit/tc_document.rb +81 -0
  185. data/test/unit/ts_analysis.rb +2 -0
  186. data/test/unit/ts_index.rb +2 -0
  187. data/test/unit/ts_largefile.rb +4 -0
  188. data/test/unit/ts_query_parser.rb +2 -0
  189. data/test/unit/ts_search.rb +2 -0
  190. data/test/unit/ts_store.rb +2 -0
  191. data/test/unit/ts_utils.rb +2 -0
  192. data/test/unit/utils/tc_bit_vector.rb +295 -0
  193. data/test/unit/utils/tc_number_tools.rb +117 -0
  194. data/test/unit/utils/tc_priority_queue.rb +106 -0
  195. metadata +285 -0
@@ -0,0 +1,471 @@
1
+ #include "store.h"
2
+ #include <string.h>
3
+
4
+ extern Store *store_new();
5
+ extern void store_destroy(Store *store);
6
+ extern OutStream *os_new();
7
+ extern InStream *is_new();
8
+ extern int file_is_lock(char *filename);
9
+
10
+ static RAMFile *rf_new(const char *name)
11
+ {
12
+ RAMFile *rf = ALLOC(RAMFile);
13
+ rf->buffers = ALLOC(uchar *);
14
+ rf->buffers[0] = ALLOC_N(uchar, BUFFER_SIZE);
15
+ rf->name = estrdup(name);
16
+ rf->len = 0;
17
+ rf->bufcnt = 1;
18
+ rf->ref_cnt = 1;
19
+ return rf;
20
+ }
21
+
22
+ static void rf_extend_if_necessary(RAMFile *rf, int buf_num)
23
+ {
24
+ while (rf->bufcnt <= buf_num) {
25
+ REALLOC_N(rf->buffers, uchar *, (rf->bufcnt + 1));
26
+ rf->buffers[rf->bufcnt++] = ALLOC_N(uchar, BUFFER_SIZE);
27
+ }
28
+ }
29
+
30
+ static void rf_close(void *p)
31
+ {
32
+ int i;
33
+ RAMFile *rf = (RAMFile *)p;
34
+ if (rf->ref_cnt > 0) {
35
+ return;
36
+ }
37
+ free(rf->name);
38
+ for (i = 0; i < rf->bufcnt; i++) {
39
+ free(rf->buffers[i]);
40
+ }
41
+ free(rf->buffers);
42
+ free(rf);
43
+ }
44
+
45
+ static void ram_touch(Store *store, char *filename)
46
+ {
47
+ if (h_get(store->dir.ht, filename) == NULL) {
48
+ h_set(store->dir.ht, filename, rf_new(filename));
49
+ }
50
+ }
51
+
52
+ static int ram_exists(Store *store, char *filename)
53
+ {
54
+ if (h_get(store->dir.ht, filename) != NULL) {
55
+ return true;
56
+ }
57
+ else {
58
+ return false;
59
+ }
60
+ }
61
+
62
+ static int ram_remove(Store *store, char *filename)
63
+ {
64
+ RAMFile *rf = h_rem(store->dir.ht, filename, false);
65
+ if (rf != NULL) {
66
+ DEREF(rf);
67
+ rf_close(rf);
68
+ return true;
69
+ }
70
+ else {
71
+ return false;
72
+ }
73
+ }
74
+
75
+ static void ram_rename(Store *store, char *from, char *to)
76
+ {
77
+ RAMFile *rf = (RAMFile *)h_rem(store->dir.ht, from, false);
78
+ RAMFile *tmp;
79
+
80
+ if (rf == NULL) {
81
+ RAISE(IO_ERROR, "couldn't rename \"%s\" to \"%s\". \"%s\""
82
+ " doesn't exist", from, to, from);
83
+ }
84
+
85
+ free(rf->name);
86
+
87
+ rf->name = estrdup(to);
88
+
89
+ /* clean up the file we are overwriting */
90
+ tmp = (RAMFile *)h_get(store->dir.ht, to);
91
+ if (tmp != NULL) {
92
+ DEREF(tmp);
93
+ }
94
+
95
+ h_set(store->dir.ht, rf->name, rf);
96
+ }
97
+
98
+ static int ram_count(Store *store)
99
+ {
100
+ return store->dir.ht->size;
101
+ }
102
+
103
+ static void ram_each(Store *store,
104
+ void (*func)(char *fname, void *arg), void *arg)
105
+ {
106
+ HashTable *ht = store->dir.ht;
107
+ int i;
108
+ for (i = 0; i <= ht->mask; i++) {
109
+ RAMFile *rf = (RAMFile *)ht->table[i].value;
110
+ if (rf) {
111
+ if (strncmp(rf->name, LOCK_PREFIX, strlen(LOCK_PREFIX)) == 0) {
112
+ continue;
113
+ }
114
+ func(rf->name, arg);
115
+ }
116
+ }
117
+ }
118
+
119
+ static void ram_close_i(Store *store)
120
+ {
121
+ HashTable *ht = store->dir.ht;
122
+ int i;
123
+ for (i = 0; i <= ht->mask; i++) {
124
+ RAMFile *rf = (RAMFile *)ht->table[i].value;
125
+ if (rf) {
126
+ DEREF(rf);
127
+ }
128
+ }
129
+ h_destroy(store->dir.ht);
130
+ store_destroy(store);
131
+ }
132
+
133
+ /*
134
+ * Be sure to keep the locks
135
+ */
136
+ static void ram_clear(Store *store)
137
+ {
138
+ int i;
139
+ HashTable *ht = store->dir.ht;
140
+ for (i = 0; i <= ht->mask; i++) {
141
+ RAMFile *rf = (RAMFile *)ht->table[i].value;
142
+ if (rf && !file_is_lock(rf->name)) {
143
+ DEREF(rf);
144
+ h_del(ht, rf->name);
145
+ }
146
+ }
147
+ }
148
+
149
+ static void ram_clear_locks(Store *store)
150
+ {
151
+ int i;
152
+ HashTable *ht = store->dir.ht;
153
+ for (i = 0; i <= ht->mask; i++) {
154
+ RAMFile *rf = (RAMFile *)ht->table[i].value;
155
+ if (rf && file_is_lock(rf->name)) {
156
+ DEREF(rf);
157
+ h_del(ht, rf->name);
158
+ }
159
+ }
160
+ }
161
+
162
+ static void ram_clear_all(Store *store)
163
+ {
164
+ int i;
165
+ HashTable *ht = store->dir.ht;
166
+ for (i = 0; i <= ht->mask; i++) {
167
+ RAMFile *rf = (RAMFile *)ht->table[i].value;
168
+ if (rf) {
169
+ DEREF(rf);
170
+ h_del(ht, rf->name);
171
+ }
172
+ }
173
+ }
174
+
175
+ static off_t ram_length(Store *store, char *filename)
176
+ {
177
+ RAMFile *rf = (RAMFile *)h_get(store->dir.ht, filename);
178
+ if (rf != NULL) {
179
+ return rf->len;
180
+ }
181
+ else {
182
+ return 0;
183
+ }
184
+ }
185
+
186
+ off_t ramo_length(OutStream *os)
187
+ {
188
+ return os->file.rf->len;
189
+ }
190
+
191
+ static void ramo_flush_i(OutStream *os, uchar *src, int len)
192
+ {
193
+ uchar *buffer;
194
+ RAMFile *rf = os->file.rf;
195
+ int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
196
+ int src_offset;
197
+ off_t pointer = os->pointer;
198
+
199
+ buffer_number = (int)(pointer / BUFFER_SIZE);
200
+ buffer_offset = pointer % BUFFER_SIZE;
201
+ bytes_in_buffer = BUFFER_SIZE - buffer_offset;
202
+ bytes_to_copy = bytes_in_buffer < len ? bytes_in_buffer : len;
203
+
204
+ rf_extend_if_necessary(rf, buffer_number);
205
+
206
+ buffer = rf->buffers[buffer_number];
207
+ memcpy(buffer + buffer_offset, src, bytes_to_copy);
208
+
209
+ if (bytes_to_copy < len) {
210
+ src_offset = bytes_to_copy;
211
+ bytes_to_copy = len - bytes_to_copy;
212
+ buffer_number += 1;
213
+ rf_extend_if_necessary(rf, buffer_number);
214
+ buffer = rf->buffers[buffer_number];
215
+
216
+ memcpy(buffer, src + src_offset, bytes_to_copy);
217
+ }
218
+ os->pointer += len;
219
+
220
+ if (os->pointer > rf->len) {
221
+ rf->len = os->pointer;
222
+ }
223
+ }
224
+
225
+ static void ramo_seek_i(OutStream *os, off_t pos)
226
+ {
227
+ os->pointer = pos;
228
+ }
229
+
230
+ void ramo_reset(OutStream *os)
231
+ {
232
+ os_seek(os, 0);
233
+ os->file.rf->len = 0;
234
+ }
235
+
236
+ static void ramo_close_i(OutStream *os)
237
+ {
238
+ RAMFile *rf = os->file.rf;
239
+ DEREF(rf);
240
+ rf_close(rf);
241
+ }
242
+
243
+ void ramo_write_to(OutStream *os, OutStream *other_o)
244
+ {
245
+ int i, len;
246
+ RAMFile *rf = os->file.rf;
247
+ int last_buffer_number;
248
+ int last_buffer_offset;
249
+
250
+ os_flush(os);
251
+ last_buffer_number = (int) (rf->len / BUFFER_SIZE);
252
+ last_buffer_offset = rf->len % BUFFER_SIZE;
253
+ for (i = 0; i <= last_buffer_number; i++) {
254
+ len = (i == last_buffer_number ? last_buffer_offset : BUFFER_SIZE);
255
+ os_write_bytes(other_o, rf->buffers[i], len);
256
+ }
257
+ }
258
+
259
+ const struct OutStreamMethods RAM_OUT_STREAM_METHODS = {
260
+ ramo_flush_i,
261
+ ramo_seek_i,
262
+ ramo_close_i
263
+ };
264
+
265
+ OutStream *ram_new_buffer()
266
+ {
267
+ RAMFile *rf = rf_new("");
268
+ OutStream *os = os_new();
269
+
270
+ DEREF(rf);
271
+ os->file.rf = rf;
272
+ os->pointer = 0;
273
+ os->m = &RAM_OUT_STREAM_METHODS;
274
+ return os;
275
+ }
276
+
277
+ void ram_destroy_buffer(OutStream *os)
278
+ {
279
+ rf_close(os->file.rf);
280
+ free(os);
281
+ }
282
+
283
+ static OutStream *ram_new_output(Store *store, const char *filename)
284
+ {
285
+ RAMFile *rf = (RAMFile *)h_get(store->dir.ht, filename);
286
+ OutStream *os = os_new();
287
+
288
+ if (rf == NULL) {
289
+ rf = rf_new(filename);
290
+ h_set(store->dir.ht, rf->name, rf);
291
+ }
292
+ REF(rf);
293
+ os->pointer = 0;
294
+ os->file.rf = rf;
295
+ os->m = &RAM_OUT_STREAM_METHODS;
296
+ return os;
297
+ }
298
+
299
+ static void rami_read_i(InStream *is, uchar *b, int len)
300
+ {
301
+ RAMFile *rf = is->file.rf;
302
+
303
+ int offset = 0;
304
+ int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
305
+ int remainder = len;
306
+ off_t start = is->d.pointer;
307
+ uchar *buffer;
308
+
309
+ while (remainder > 0) {
310
+ buffer_number = (int) (start / BUFFER_SIZE);
311
+ buffer_offset = start % BUFFER_SIZE;
312
+ bytes_in_buffer = BUFFER_SIZE - buffer_offset;
313
+
314
+ if (bytes_in_buffer >= remainder) {
315
+ bytes_to_copy = remainder;
316
+ }
317
+ else {
318
+ bytes_to_copy = bytes_in_buffer;
319
+ }
320
+ buffer = rf->buffers[buffer_number];
321
+ memcpy(b + offset, buffer + buffer_offset, bytes_to_copy);
322
+ offset += bytes_to_copy;
323
+ start += bytes_to_copy;
324
+ remainder -= bytes_to_copy;
325
+ }
326
+
327
+ is->d.pointer += len;
328
+ }
329
+
330
+ static off_t rami_length_i(InStream *is)
331
+ {
332
+ return is->file.rf->len;
333
+ }
334
+
335
+ static void rami_seek_i(InStream *is, off_t pos)
336
+ {
337
+ is->d.pointer = pos;
338
+ }
339
+
340
+ static void rami_close_i(InStream *is)
341
+ {
342
+ RAMFile *rf = is->file.rf;
343
+ DEREF(rf);
344
+ rf_close(rf);
345
+ }
346
+
347
+ static const struct InStreamMethods RAM_IN_STREAM_METHODS = {
348
+ rami_read_i,
349
+ rami_seek_i,
350
+ rami_length_i,
351
+ rami_close_i
352
+ };
353
+
354
+ static InStream *ram_open_input(Store *store, const char *filename)
355
+ {
356
+ RAMFile *rf = (RAMFile *)h_get(store->dir.ht, filename);
357
+ InStream *is = NULL;
358
+
359
+ if (rf == NULL) {
360
+ RAISE(FILE_NOT_FOUND_ERROR,
361
+ "tried to open \"%s\" but it doesn't exist", filename);
362
+ }
363
+ REF(rf);
364
+ is = is_new();
365
+ is->file.rf = rf;
366
+ is->d.pointer = 0;
367
+ is->m = &RAM_IN_STREAM_METHODS;
368
+
369
+ return is;
370
+ }
371
+
372
+ #define LOCK_OBTAIN_TIMEOUT 5
373
+
374
+ static int ram_lock_obtain(Lock *lock)
375
+ {
376
+ int ret = true;
377
+ if (ram_exists(lock->store, lock->name))
378
+ ret = false;
379
+ ram_touch(lock->store, lock->name);
380
+ return ret;
381
+ }
382
+
383
+ static int ram_lock_is_locked(Lock *lock)
384
+ {
385
+ return ram_exists(lock->store, lock->name);
386
+ }
387
+
388
+ static void ram_lock_release(Lock *lock)
389
+ {
390
+ ram_remove(lock->store, lock->name);
391
+ }
392
+
393
+ static Lock *ram_open_lock_i(Store *store, char *lockname)
394
+ {
395
+ Lock *lock = ALLOC(Lock);
396
+ char lname[100];
397
+ snprintf(lname, 100, "%s%s.lck", LOCK_PREFIX, lockname);
398
+ lock->name = estrdup(lname);
399
+ lock->store = store;
400
+ lock->obtain = &ram_lock_obtain;
401
+ lock->release = &ram_lock_release;
402
+ lock->is_locked = &ram_lock_is_locked;
403
+ return lock;
404
+ }
405
+
406
+ static void ram_close_lock_i(Lock *lock)
407
+ {
408
+ free(lock->name);
409
+ free(lock);
410
+ }
411
+
412
+
413
+ Store *open_ram_store()
414
+ {
415
+ Store *new_store = store_new();
416
+
417
+ new_store->dir.ht = h_new_str(NULL, rf_close);
418
+ new_store->touch = &ram_touch;
419
+ new_store->exists = &ram_exists;
420
+ new_store->remove = &ram_remove;
421
+ new_store->rename = &ram_rename;
422
+ new_store->count = &ram_count;
423
+ new_store->clear = &ram_clear;
424
+ new_store->clear_all = &ram_clear_all;
425
+ new_store->clear_locks = &ram_clear_locks;
426
+ new_store->length = &ram_length;
427
+ new_store->each = &ram_each;
428
+ new_store->new_output = &ram_new_output;
429
+ new_store->open_input = &ram_open_input;
430
+ new_store->open_lock_i = &ram_open_lock_i;
431
+ new_store->close_lock_i = &ram_close_lock_i;
432
+ new_store->close_i = &ram_close_i;
433
+ return new_store;
434
+ }
435
+
436
+ struct CopyFileArg
437
+ {
438
+ Store *to_store, *from_store;
439
+ };
440
+
441
+ static void copy_files(char *fname, void *arg)
442
+ {
443
+ struct CopyFileArg *cfa = (struct CopyFileArg *)arg;
444
+ OutStream *os = cfa->to_store->new_output(cfa->to_store, fname);
445
+ InStream *is = cfa->from_store->open_input(cfa->from_store, fname);
446
+ int len = (int)is_length(is);
447
+ uchar *buffer = ALLOC_N(uchar, len + 1);
448
+
449
+ is_read_bytes(is, buffer, len);
450
+ os_write_bytes(os, buffer, len);
451
+
452
+ is_close(is);
453
+ os_close(os);
454
+ free(buffer);
455
+ }
456
+
457
+ Store *open_ram_store_and_copy(Store *from_store, bool close_dir)
458
+ {
459
+ Store *store = open_ram_store();
460
+ struct CopyFileArg cfa;
461
+ cfa.to_store = store;
462
+ cfa.from_store = from_store;
463
+
464
+ from_store->each(from_store, &copy_files, &cfa);
465
+
466
+ if (close_dir) {
467
+ store_deref(from_store);
468
+ }
469
+
470
+ return store;
471
+ }