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,156 @@
1
+ #include "search.h"
2
+ #include <string.h>
3
+
4
+ /***************************************************************************
5
+ *
6
+ * Filter
7
+ *
8
+ ***************************************************************************/
9
+
10
+ void filt_destroy_i(Filter *filt)
11
+ {
12
+ h_destroy(filt->cache);
13
+ free(filt->name);
14
+ free(filt);
15
+ }
16
+ void filt_deref(Filter *filt)
17
+ {
18
+ if (--(filt->ref_cnt) == 0) {
19
+ filt->destroy_i(filt);
20
+ }
21
+ }
22
+
23
+ BitVector *filt_get_bv(Filter *filt, IndexReader *ir)
24
+ {
25
+ CacheObject *co = h_get(filt->cache, ir);
26
+
27
+ if (!co) {
28
+ BitVector *bv;
29
+ if (!ir->cache) {
30
+ ir_add_cache(ir);
31
+ }
32
+ bv = filt->get_bv_i(filt, ir);
33
+ co = co_create(filt->cache, ir->cache, filt, ir,
34
+ (free_ft)&bv_destroy, (void *)bv);
35
+ }
36
+ return (BitVector *)co->obj;
37
+ }
38
+
39
+ static char *filt_to_s_i(Filter *filt)
40
+ {
41
+ return estrdup(filt->name);
42
+ }
43
+
44
+ unsigned long filt_hash_default(Filter *filt)
45
+ {
46
+ (void)filt;
47
+ return 0;
48
+ }
49
+
50
+ int filt_eq_default(Filter *filt, Filter *o)
51
+ {
52
+ (void)filt; (void)o;
53
+ return false;
54
+ }
55
+
56
+ Filter *filt_create(size_t size, const char *name)
57
+ {
58
+ Filter *filt = (Filter *)emalloc(size);
59
+ filt->cache = co_hash_create();
60
+ filt->name = estrdup(name);
61
+ filt->to_s = &filt_to_s_i;
62
+ filt->hash = &filt_hash_default;
63
+ filt->eq = &filt_eq_default;
64
+ filt->destroy_i = &filt_destroy_i;
65
+ filt->ref_cnt = 1;
66
+ return filt;
67
+ }
68
+
69
+ unsigned long filt_hash(Filter *filt)
70
+ {
71
+ return str_hash(filt->name) ^ filt->hash(filt);
72
+ }
73
+
74
+ int filt_eq(Filter *filt, Filter *o)
75
+ {
76
+ return ((filt == o)
77
+ || ((strcmp(filt->name, o->name) == 0)
78
+ && (filt->eq == o->eq)
79
+ && (filt->eq(filt, o))));
80
+ }
81
+
82
+ /***************************************************************************
83
+ *
84
+ * QueryFilter
85
+ *
86
+ ***************************************************************************/
87
+
88
+ #define QF(filt) ((QueryFilter *)(filt))
89
+ typedef struct QueryFilter
90
+ {
91
+ Filter super;
92
+ Query *query;
93
+ } QueryFilter;
94
+
95
+ static char *qfilt_to_s(Filter *filt)
96
+ {
97
+ Query *query = QF(filt)->query;
98
+ char *query_str = query->to_s(query, "");
99
+ char *filter_str = strfmt("QueryFilter< %s >", query_str);
100
+ free(query_str);
101
+ return filter_str;
102
+ }
103
+
104
+ static BitVector *qfilt_get_bv_i(Filter *filt, IndexReader *ir)
105
+ {
106
+ BitVector *bv = bv_new_capa(ir->max_doc(ir));
107
+ Searcher *sea = isea_new(ir);
108
+ Weight *weight = q_weight(QF(filt)->query, sea);
109
+ Scorer *scorer = weight->scorer(weight, ir);
110
+ if (scorer) {
111
+ while (scorer->next(scorer)) {
112
+ bv_set(bv, scorer->doc);
113
+ }
114
+ scorer->destroy(scorer);
115
+ }
116
+ weight->destroy(weight);
117
+ free(sea);
118
+ return bv;
119
+ }
120
+
121
+ static unsigned long qfilt_hash(Filter *filt)
122
+ {
123
+ return q_hash(QF(filt)->query);
124
+ }
125
+
126
+ static int qfilt_eq(Filter *filt, Filter *o)
127
+ {
128
+ return q_eq(QF(filt)->query, QF(o)->query);
129
+ }
130
+
131
+ static void qfilt_destroy_i(Filter *filt)
132
+ {
133
+ Query *query = QF(filt)->query;
134
+ q_deref(query);
135
+ filt_destroy_i(filt);
136
+ }
137
+
138
+ Filter *qfilt_new_nr(Query *query)
139
+ {
140
+ Filter *filt = filt_new(QueryFilter);
141
+
142
+ QF(filt)->query = query;
143
+
144
+ filt->get_bv_i = &qfilt_get_bv_i;
145
+ filt->hash = &qfilt_hash;
146
+ filt->eq = &qfilt_eq;
147
+ filt->to_s = &qfilt_to_s;
148
+ filt->destroy_i = &qfilt_destroy_i;
149
+ return filt;
150
+ }
151
+
152
+ Filter *qfilt_new(Query *query)
153
+ {
154
+ REF(query);
155
+ return qfilt_new_nr(query);
156
+ }
@@ -0,0 +1,483 @@
1
+ #include "store.h"
2
+ #include <time.h>
3
+ #include <sys/types.h>
4
+ #include <fcntl.h>
5
+ #include <sys/stat.h>
6
+ #include <errno.h>
7
+ #include <string.h>
8
+ #include <stdio.h>
9
+ #ifdef POSH_OS_WIN32
10
+ # include <io.h>
11
+ # include "win32.h"
12
+ # ifndef sleep
13
+ # define sleep _sleep
14
+ # endif
15
+ # ifndef DIR_SEPARATOR
16
+ # define DIR_SEPARATOR "\\"
17
+ # endif
18
+ # ifndef S_IRUSR
19
+ # define S_IRUSR _S_IREAD
20
+ # endif
21
+ # ifndef S_IWUSR
22
+ # define S_IWUSR _S_IWRITE
23
+ # endif
24
+ #else
25
+ # define DIR_SEPARATOR "/"
26
+ # include <unistd.h>
27
+ # include <dirent.h>
28
+ #endif
29
+ #ifndef O_BINARY
30
+ # define O_BINARY 0
31
+ #endif
32
+
33
+ extern Store *store_new();
34
+ extern void store_destroy(Store *store);
35
+ extern OutStream *os_new();
36
+ extern InStream *is_new();
37
+ extern int file_is_lock(char *filename);
38
+
39
+ /**
40
+ * Create a filepath for a file in the store using the operating systems
41
+ * default file seperator.
42
+ */
43
+ static char *join_path(char *buf, const char *base, const char *filename)
44
+ {
45
+ snprintf(buf, MAX_FILE_PATH, "%s"DIR_SEPARATOR"%s", base, filename);
46
+ return buf;
47
+ }
48
+
49
+ static void fs_touch(Store *store, char *filename)
50
+ {
51
+ int f;
52
+ char path[MAX_FILE_PATH];
53
+ join_path(path, store->dir.path, filename);
54
+ if ((f = creat(path, store->file_mode)) == 0) {
55
+ RAISE(IO_ERROR, "couldn't create file %s: <%s>", path,
56
+ strerror(errno));
57
+ }
58
+ close(f);
59
+ }
60
+
61
+ static int fs_exists(Store *store, char *filename)
62
+ {
63
+ int fd;
64
+ char path[MAX_FILE_PATH];
65
+ join_path(path, store->dir.path, filename);
66
+ fd = open(path, 0);
67
+ if (fd < 0) {
68
+ if (errno != ENOENT) {
69
+ RAISE(IO_ERROR, "checking existance of %s: <%s>", path,
70
+ strerror(errno));
71
+ }
72
+ return false;
73
+ }
74
+ close(fd);
75
+ return true;
76
+ }
77
+
78
+ static int fs_remove(Store *store, char *filename)
79
+ {
80
+ char path[MAX_FILE_PATH];
81
+ return remove(join_path(path, store->dir.path, filename));
82
+ }
83
+
84
+ static void fs_rename(Store *store, char *from, char *to)
85
+ {
86
+ char path1[MAX_FILE_PATH], path2[MAX_FILE_PATH];
87
+
88
+ #ifdef POSH_OS_WIN32
89
+ remove(join_path(path1, store->dir.path, to));
90
+ #endif
91
+
92
+ if (rename(join_path(path1, store->dir.path, from),
93
+ join_path(path2, store->dir.path, to)) < 0) {
94
+ RAISE(IO_ERROR, "couldn't rename file \"%s\" to \"%s\": <%s>",
95
+ path1, path2, strerror(errno));
96
+ }
97
+ }
98
+
99
+ static int fs_count(Store *store)
100
+ {
101
+ int cnt = 0;
102
+ struct dirent *de;
103
+ DIR *d = opendir(store->dir.path);
104
+
105
+ if (!d) {
106
+ RAISE(IO_ERROR, "counting files in %s: <%s>",
107
+ store->dir.path, strerror(errno));
108
+ }
109
+
110
+ while ((de = readdir(d)) != NULL) {
111
+ if (de->d_name[0] > '/') { /* skip ., .., / and '\0'*/
112
+ cnt++;
113
+ }
114
+ }
115
+ closedir(d);
116
+
117
+ return cnt;
118
+ }
119
+
120
+ static void fs_each(Store *store, void (*func)(char *fname, void *arg), void *arg)
121
+ {
122
+ struct dirent *de;
123
+ DIR *d = opendir(store->dir.path);
124
+
125
+ if (!d) {
126
+ RAISE(IO_ERROR, "doing 'each' in %s: <%s>",
127
+ store->dir.path, strerror(errno));
128
+ }
129
+
130
+ while ((de = readdir(d)) != NULL) {
131
+ if (de->d_name[0] > '/' /* skip ., .., / and '\0'*/
132
+ && !file_is_lock(de->d_name)) {
133
+ func(de->d_name, arg);
134
+ }
135
+ }
136
+ closedir(d);
137
+ }
138
+
139
+ static void fs_clear_locks(Store *store)
140
+ {
141
+ struct dirent *de;
142
+ DIR *d = opendir(store->dir.path);
143
+
144
+ if (!d) {
145
+ RAISE(IO_ERROR, "clearing locks in %s: <%s>",
146
+ store->dir.path, strerror(errno));
147
+ }
148
+
149
+ while ((de = readdir(d)) != NULL) {
150
+ if (file_is_lock(de->d_name)) {
151
+ char path[MAX_FILE_PATH];
152
+ remove(join_path(path, store->dir.path, de->d_name));
153
+ }
154
+ }
155
+ closedir(d);
156
+ }
157
+
158
+ static void fs_clear(Store *store)
159
+ {
160
+ struct dirent *de;
161
+ DIR *d = opendir(store->dir.path);
162
+
163
+ if (!d) {
164
+ RAISE(IO_ERROR, "clearing files in %s: <%s>",
165
+ store->dir.path, strerror(errno));
166
+ }
167
+
168
+ while ((de = readdir(d)) != NULL) {
169
+ if (de->d_name[0] > '/' /* skip ., .., / and '\0'*/
170
+ && !file_is_lock(de->d_name)) {
171
+ char path[MAX_FILE_PATH];
172
+ remove(join_path(path, store->dir.path, de->d_name));
173
+ }
174
+ }
175
+ closedir(d);
176
+ }
177
+
178
+ static void fs_clear_all(Store *store)
179
+ {
180
+ struct dirent *de;
181
+ DIR *d = opendir(store->dir.path);
182
+
183
+ if (!d) {
184
+ RAISE(IO_ERROR, "clearing all files in %s: <%s>",
185
+ store->dir.path, strerror(errno));
186
+ }
187
+
188
+ while ((de = readdir(d)) != NULL) {
189
+ if (de->d_name[0] > '/') { /* skip ., .., / and '\0'*/
190
+ char path[MAX_FILE_PATH];
191
+ remove(join_path(path, store->dir.path, de->d_name));
192
+ }
193
+ }
194
+ closedir(d);
195
+ }
196
+
197
+ /**
198
+ * Destroy the store.
199
+ *
200
+ * @param p the store to destroy
201
+ * @raise IO_ERROR if there is an error deleting the locks
202
+ */
203
+ static void fs_destroy(Store *store)
204
+ {
205
+ TRY
206
+ fs_clear_locks(store);
207
+ XCATCHALL
208
+ HANDLED();
209
+ XENDTRY
210
+ free(store->dir.path);
211
+ store_destroy(store);
212
+ }
213
+
214
+ static off_t fs_length(Store *store, char *filename)
215
+ {
216
+ char path[MAX_FILE_PATH];
217
+ struct stat stt;
218
+
219
+ if (stat(join_path(path, store->dir.path, filename), &stt)) {
220
+ RAISE(IO_ERROR, "getting lenth of %s: <%s>", path,
221
+ strerror(errno));
222
+ }
223
+
224
+ return stt.st_size;
225
+ }
226
+
227
+ static void fso_flush_i(OutStream *os, uchar *src, int len)
228
+ {
229
+ if (len != write(os->file.fd, src, len)) {
230
+ RAISE(IO_ERROR, "flushing src of length %d, <%s>", len,
231
+ strerror(errno));
232
+ }
233
+ }
234
+
235
+ static void fso_seek_i(OutStream *os, off_t pos)
236
+ {
237
+ if (lseek(os->file.fd, pos, SEEK_SET) < 0) {
238
+ RAISE(IO_ERROR, "seeking position %"F_OFF_T_PFX"d: <%s>",
239
+ pos, strerror(errno));
240
+ }
241
+ }
242
+
243
+ static void fso_close_i(OutStream *os)
244
+ {
245
+ if (close(os->file.fd)) {
246
+ RAISE(IO_ERROR, "closing file: <%s>", strerror(errno));
247
+ }
248
+ }
249
+
250
+ const struct OutStreamMethods FS_OUT_STREAM_METHODS = {
251
+ fso_flush_i,
252
+ fso_seek_i,
253
+ fso_close_i
254
+ };
255
+
256
+ static OutStream *fs_new_output(Store *store, const char *filename)
257
+ {
258
+ char path[MAX_FILE_PATH];
259
+ int fd = open(join_path(path, store->dir.path, filename),
260
+ O_WRONLY | O_CREAT | O_BINARY, store->file_mode);
261
+ OutStream *os;
262
+ if (fd < 0) {
263
+ RAISE(IO_ERROR, "couldn't create OutStream %s: <%s>",
264
+ path, strerror(errno));
265
+ }
266
+
267
+ os = os_new();
268
+ os->file.fd = fd;
269
+ os->m = &FS_OUT_STREAM_METHODS;
270
+ return os;
271
+ }
272
+
273
+ static void fsi_read_i(InStream *is, uchar *path, int len)
274
+ {
275
+ int fd = is->file.fd;
276
+ off_t pos = is_pos(is);
277
+ if (pos != lseek(fd, 0, SEEK_CUR)) {
278
+ lseek(fd, pos, SEEK_SET);
279
+ }
280
+ if (read(fd, path, len) != len) {
281
+ /* win: the wrong value can be returned for some reason so double check */
282
+ if (lseek(fd, 0, SEEK_CUR) != (pos + len)) {
283
+ RAISE(IO_ERROR, "couldn't read %d chars from %s: <%s>",
284
+ len, path, strerror(errno));
285
+ }
286
+ }
287
+ }
288
+
289
+ static void fsi_seek_i(InStream *is, off_t pos)
290
+ {
291
+ if (lseek(is->file.fd, pos, SEEK_SET) < 0) {
292
+ RAISE(IO_ERROR, "seeking pos %"F_OFF_T_PFX"d: <%s>",
293
+ pos, strerror(errno));
294
+ }
295
+ }
296
+
297
+ static void fsi_close_i(InStream *is)
298
+ {
299
+ if (close(is->file.fd)) {
300
+ RAISE(IO_ERROR, strerror(errno));
301
+ }
302
+ free(is->d.path);
303
+ }
304
+
305
+ static off_t fsi_length_i(InStream *is)
306
+ {
307
+ struct stat stt;
308
+ if (fstat(is->file.fd, &stt)) {
309
+ RAISE(IO_ERROR, "fstat failed: <%s>", strerror(errno));
310
+ }
311
+ return stt.st_size;
312
+ }
313
+
314
+ static const struct InStreamMethods FS_IN_STREAM_METHODS = {
315
+ fsi_read_i,
316
+ fsi_seek_i,
317
+ fsi_length_i,
318
+ fsi_close_i
319
+ };
320
+
321
+ static InStream *fs_open_input(Store *store, const char *filename)
322
+ {
323
+ InStream *is;
324
+ char path[MAX_FILE_PATH];
325
+ int fd = open(join_path(path, store->dir.path, filename), O_RDONLY | O_BINARY);
326
+ if (fd < 0) {
327
+ RAISE(FILE_NOT_FOUND_ERROR,
328
+ "tried to open \"%s\" but it doesn't exist: <%s>",
329
+ path, strerror(errno));
330
+ }
331
+ is = is_new();
332
+ is->file.fd = fd;
333
+ is->d.path = estrdup(path);
334
+ is->m = &FS_IN_STREAM_METHODS;
335
+ return is;
336
+ }
337
+
338
+ #define LOCK_OBTAIN_TIMEOUT 10
339
+
340
+ #ifdef RUBY_BINDINGS
341
+ struct timeval rb_time_interval _((VALUE));
342
+ #endif
343
+
344
+ static int fs_lock_obtain(Lock *lock)
345
+ {
346
+ int f;
347
+ int trys = LOCK_OBTAIN_TIMEOUT;
348
+ while (((f =
349
+ open(lock->name, O_CREAT | O_EXCL | O_RDWR,
350
+ S_IRUSR | S_IWUSR)) < 0) && (trys > 0)) {
351
+
352
+ /* sleep for 10 milliseconds */
353
+ micro_sleep(10000);
354
+ trys--;
355
+ }
356
+ if (f >= 0) {
357
+ close(f);
358
+ return true;
359
+ }
360
+ else {
361
+ return false;
362
+ }
363
+ }
364
+
365
+ static int fs_lock_is_locked(Lock *lock)
366
+ {
367
+ int f = open(lock->name, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
368
+ if (f >= 0) {
369
+ if (close(f) || remove(lock->name)) {
370
+ RAISE(IO_ERROR, "couldn't close lock \"%s\": <%s>", lock->name,
371
+ strerror(errno));
372
+ }
373
+ return false;
374
+ }
375
+ else {
376
+ return true;
377
+ }
378
+ }
379
+
380
+ void fs_lock_release(Lock *lock)
381
+ {
382
+ remove(lock->name);
383
+ }
384
+
385
+ static Lock *fs_open_lock_i(Store *store, char *lockname)
386
+ {
387
+ Lock *lock = ALLOC(Lock);
388
+ char lname[100];
389
+ char path[MAX_FILE_PATH];
390
+ snprintf(lname, 100, "%s%s.lck", LOCK_PREFIX, lockname);
391
+ lock->name = estrdup(join_path(path, store->dir.path, lname));
392
+ lock->store = store;
393
+ lock->obtain = &fs_lock_obtain;
394
+ lock->release = &fs_lock_release;
395
+ lock->is_locked = &fs_lock_is_locked;
396
+ return lock;
397
+ }
398
+
399
+ static void fs_close_lock_i(Lock *lock)
400
+ {
401
+ remove(lock->name);
402
+ free(lock->name);
403
+ free(lock);
404
+ }
405
+
406
+ static HashTable stores = {
407
+ /* fill */ 0,
408
+ /* used */ 0,
409
+ /* mask */ HASH_MINSIZE - 1,
410
+ /* ref_cnt */ 1,
411
+ /* table */ stores.smalltable,
412
+ /* smalltable */ {{0, NULL, NULL}},
413
+ /* lookup */ (h_lookup_ft)&h_lookup_str,
414
+ /* hash */ NULL,
415
+ /* eq */ NULL,
416
+ /* free_key */ (free_ft)&dummy_free,
417
+ /* free_value */ (free_ft)&fs_destroy
418
+ };
419
+
420
+ #ifndef UNTHREADED
421
+ static mutex_t stores_mutex = MUTEX_INITIALIZER;
422
+ #endif
423
+
424
+ static void fs_close_i(Store *store)
425
+ {
426
+ mutex_lock(&stores_mutex);
427
+ h_del(&stores, store->dir.path);
428
+ mutex_unlock(&stores_mutex);
429
+ }
430
+
431
+ static Store *fs_store_new(const char *pathname)
432
+ {
433
+ struct stat stt;
434
+ Store *new_store = store_new();
435
+
436
+ new_store->file_mode = S_IRUSR | S_IWUSR;
437
+ #ifndef POSH_OS_WIN32
438
+ if (!stat(pathname, &stt) && stt.st_gid == getgid()) {
439
+ if (stt.st_mode & S_IWGRP) {
440
+ umask(S_IWOTH);
441
+ }
442
+ new_store->file_mode |= stt.st_mode & (S_IRGRP | S_IWGRP);
443
+ }
444
+ #endif
445
+
446
+ new_store->dir.path = estrdup(pathname);
447
+ new_store->touch = &fs_touch;
448
+ new_store->exists = &fs_exists;
449
+ new_store->remove = &fs_remove;
450
+ new_store->rename = &fs_rename;
451
+ new_store->count = &fs_count;
452
+ new_store->close_i = &fs_close_i;
453
+ new_store->clear = &fs_clear;
454
+ new_store->clear_all = &fs_clear_all;
455
+ new_store->clear_locks = &fs_clear_locks;
456
+ new_store->length = &fs_length;
457
+ new_store->each = &fs_each;
458
+ new_store->new_output = &fs_new_output;
459
+ new_store->open_input = &fs_open_input;
460
+ new_store->open_lock_i = &fs_open_lock_i;
461
+ new_store->close_lock_i = &fs_close_lock_i;
462
+ return new_store;
463
+ }
464
+
465
+ Store *open_fs_store(const char *pathname)
466
+ {
467
+ Store *store = NULL;
468
+
469
+ mutex_lock(&stores_mutex);
470
+ store = h_get(&stores, pathname);
471
+ if (store) {
472
+ mutex_lock(&store->mutex);
473
+ store->ref_cnt++;
474
+ mutex_unlock(&store->mutex);
475
+ }
476
+ else {
477
+ store = fs_store_new(pathname);
478
+ h_set(&stores, store->dir.path, store);
479
+ }
480
+ mutex_unlock(&stores_mutex);
481
+
482
+ return store;
483
+ }