| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2025 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn.h"cxx . . $ `<file>/lib/grn.h`/ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | #include "grn_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/ |
| 29 | #include "grn_io.h"cxx . . $ `<file>/lib/grn_io.h`/ |
| 30 | #include "grn_plugin.h"cxx . . $ `<file>/lib/grn_plugin.h`/ |
| 31 | #include "grn_hash.h"cxx . . $ `<file>/lib/grn_hash.h`/ |
| 32 | #include "grn_ctx_impl.h"cxx . . $ `<file>/lib/grn_ctx_impl.h`/ |
| 33 | #include "grn_util.h"cxx . . $ `<file>/lib/grn_util.h`/ |
| 34 | |
| 35 | #ifdef WIN32 |
| 36 | # include <io.h> |
| 37 | # include <share.h> |
| 38 | #endif /* WIN32 */ |
| 39 | |
| 40 | #define GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`! "GROONGA:IO:00001" |
| 41 | #define GRN_IO_IDSTR_LENcxx . . $ `lib/io.c:41:9`! (sizeof(GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`!) - 1) |
| 42 | |
| 43 | #define GRN_IO_VERSION_DEFAULTcxx . . $ `lib/io.c:43:9`! 1 |
| 44 | |
| 45 | #define GRN_IO_FILE_SIZE_V1cxx . . $ `lib/io.c:45:9`! 1073741824UL |
| 46 | |
| 47 | #ifdef WIN32 |
| 48 | # define GRN_IO_FILE_SIZE_V0 134217728L |
| 49 | #else /* WIN32 */ |
| 50 | # define GRN_IO_FILE_SIZE_V0cxx . . $ `lib/io.c:50:11`! GRN_IO_FILE_SIZE_V1cxx . . $ `lib/io.c:45:9`! |
| 51 | #endif /* WIN32 */ |
| 52 | |
| 53 | #ifndef WIN32 |
| 54 | # ifdef HAVE_FUTIMENScxx . . $ `build/config.h:170:9`! |
| 55 | # define grn_futimenscxx . . $ `lib/io.c:55:13`!(fd, time_values, time_specs) \ |
| 56 | futimens((fd), (time_specs)) |
| 57 | # define grn_futimens_namecxx . . $ `lib/io.c:57:13`! "futimens" |
| 58 | # elif defined(HAVE_FUTIMES) /* HAVE_FUTIMENS */ |
| 59 | # define grn_futimens(fd, time_values, time_specs) \ |
| 60 | futimes((fd), (time_values)) |
| 61 | # define grn_futimens_name "futimes" |
| 62 | # else /* HAVE_FUTIMENS */ |
| 63 | # error Support for either the futimens() or futimens() function is required |
| 64 | # endif /* HAVE_FUTIMENS */ |
| 65 | #endif /* WIN32 */ |
| 66 | |
| 67 | typedef struct _grn_io_fileinfocxx . . $ _grn_io_fileinfo# { |
| 68 | char *pathcxx . . $ _grn_io_fileinfo#path.; |
| 69 | #ifdef WIN32 |
| 70 | HANDLE fh; |
| 71 | HANDLE fmo; |
| 72 | grn_critical_section cs; |
| 73 | #else /* WIN32 */ |
| 74 | int fdcxx . . $ _grn_io_fileinfo#fd.; |
| 75 | dev_tcxx . . $ dev_t# devcxx . . $ _grn_io_fileinfo#dev.; |
| 76 | ino_tcxx . . $ ino_t# inodecxx . . $ _grn_io_fileinfo#inode.; |
| 77 | #endif /* WIN32 */ |
| 78 | } fileinfocxx . . $ fileinfo#; |
| 79 | |
| 80 | #define IO_HEADER_SIZEcxx . . $ `lib/io.c:80:9`! 64 |
| 81 | |
| 82 | static uint32_tcxx . . $ uint32_t# grn_io_version_defaultcxx . . $ grn_io_version_default. = GRN_IO_VERSION_DEFAULTcxx . . $ `lib/io.c:43:9`!; |
| 83 | |
| 84 | static inline grn_rccxx . . $ grn_rc# |
| 85 | grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, const char *path, int flags); |
| 86 | static inline void |
| 87 | grn_fileinfo_initcxx . . $ grn_fileinfo_init(4f59c63a7bfebd5).(fileinfocxx . . $ fileinfo# *fis, uint32_tcxx . . $ uint32_t# nfis); |
| 88 | static inline int |
| 89 | grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fileinfocxx . . $ fileinfo# *fi); |
| 90 | static inline grn_rccxx . . $ grn_rc# |
| 91 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi); |
| 92 | #ifdef WIN32 |
| 93 | inline static void * |
| 94 | grn_mmap(grn_ctx *ctx, |
| 95 | grn_io *io, |
| 96 | HANDLE *fmo, |
| 97 | fileinfo *fi, |
| 98 | int64_t offset, |
| 99 | size_t length, |
| 100 | const char *file, |
| 101 | int line, |
| 102 | const char *func); |
| 103 | static inline int |
| 104 | grn_munmap(grn_ctx *ctx, |
| 105 | grn_io *io, |
| 106 | HANDLE *fmo, |
| 107 | fileinfo *fi, |
| 108 | void *start, |
| 109 | size_t length); |
| 110 | # define GRN_MMAP(ctx, io, fmo, fi, offset, length) \ |
| 111 | (grn_mmap((ctx), \ |
| 112 | (io), \ |
| 113 | (fmo), \ |
| 114 | (fi), \ |
| 115 | (offset), \ |
| 116 | (length), \ |
| 117 | __FILE__, \ |
| 118 | __LINE__, \ |
| 119 | __FUNCTION__)) |
| 120 | # define GRN_MUNMAP(ctx, io, fmo, fi, start, length) \ |
| 121 | (grn_munmap((ctx), (io), (fmo), (fi), (start), (length))) |
| 122 | #else /* WIN32 */ |
| 123 | inline static void * |
| 124 | grn_mmapcxx . . $ grn_mmap(e90cb56b1a570dbb).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 125 | grn_iocxx . . $ grn_io# *io, |
| 126 | fileinfocxx . . $ fileinfo# *fi, |
| 127 | int64_tcxx . . $ int64_t# offset, |
| 128 | size_t length, |
| 129 | const char *file, |
| 130 | int line, |
| 131 | const char *func); |
| 132 | static inline int |
| 133 | grn_munmapcxx . . $ grn_munmap(5f599ae7e068e8bf).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, fileinfocxx . . $ fileinfo# *fi, void *start, size_t length); |
| 134 | # define GRN_MMAPcxx . . $ `lib/io.c:134:11`!(ctx, io, fmo, fi, offset, length) \ |
| 135 | (grn_mmap((ctx), \ |
| 136 | (io), \ |
| 137 | (fi), \ |
| 138 | (offset), \ |
| 139 | (length), \ |
| 140 | __FILE__, \ |
| 141 | __LINE__, \ |
| 142 | __FUNCTION__)) |
| 143 | # define GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, io, fmo, fi, start, length) \ |
| 144 | (grn_munmap((ctx), (io), (fi), (start), (length))) |
| 145 | #endif /* WIN32 */ |
| 146 | |
| 147 | inline static int |
| 148 | grn_msynccxx . . $ grn_msync(16cbec3d163fddf4).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, void *start, size_t length); |
| 149 | #define GRN_MSYNCcxx . . $ `lib/io.c:149:9`!(ctx, fi, start, length) \ |
| 150 | (grn_msync((ctx), (fi), (start), (length))) |
| 151 | |
| 152 | inline static grn_rccxx . . $ grn_rc# |
| 153 | grn_preadcxx . . $ grn_pread(4577714838bf4b55).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, void *buf, size_t count, off_tcxx . . $ off_t# offset); |
| 154 | static inline grn_rccxx . . $ grn_rc# |
| 155 | grn_pwritecxx . . $ grn_pwrite(4577714838bf4b55).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, void *buf, size_t count, off_tcxx . . $ off_t# offset); |
| 156 | |
| 157 | void |
| 158 | grn_io_init_from_envcxx . . $ grn_io_init_from_env(49f6e7a06ebc5aa8).(void) |
| 159 | { |
| 160 | { |
| 161 | char version_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!]; |
| 162 | |
| 163 | grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_IO_VERSION", version_env, GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!); |
| 164 | if (version_env[0]) { |
| 165 | grn_io_version_defaultcxx . . $ grn_io_version_default. = (uint32_tcxx . . $ uint32_t#)atoicxx . . $ atoi(5c4306d8553b3696).(version_env); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static inline uint32_tcxx . . $ uint32_t# |
| 171 | grn_io_compute_basecxx . . $ grn_io_compute_base(2141b656ce2cc063).(uint32_tcxx . . $ uint32_t# header_size) |
| 172 | { |
| 173 | uint32_tcxx . . $ uint32_t# total_header_size; |
| 174 | total_header_size = IO_HEADER_SIZEcxx . . $ `lib/io.c:80:9`! + header_size; |
| 175 | return (total_header_size + (uint32_tcxx . . $ uint32_t#)grn_pagesizecxx . . $ grn_pagesize. - 1) & |
| 176 | (uint32_tcxx . . $ uint32_t#)(~(grn_pagesizecxx . . $ grn_pagesize. - 1)); |
| 177 | } |
| 178 | |
| 179 | static inline uint32_tcxx . . $ uint32_t# |
| 180 | grn_io_compute_base_segmentcxx . . $ grn_io_compute_base_segment(83f10ac8959d5577).(uint32_tcxx . . $ uint32_t# base, uint32_tcxx . . $ uint32_t# segment_size) |
| 181 | { |
| 182 | return (base + segment_size - 1) / segment_size; |
| 183 | } |
| 184 | |
| 185 | static uint32_tcxx . . $ uint32_t# |
| 186 | grn_io_compute_max_n_filescxx . . $ grn_io_compute_max_n_files(67243219083b5ecc).(uint32_tcxx . . $ uint32_t# segment_size, |
| 187 | uint32_tcxx . . $ uint32_t# max_segment, |
| 188 | unsigned int base_segument, |
| 189 | uint32_tcxx . . $ uint32_t# file_size) |
| 190 | { |
| 191 | uint64_tcxx . . $ uint64_t# last_segment_end; |
| 192 | last_segment_end = ((uint64_tcxx . . $ uint64_t#)segment_size) * (max_segment + base_segument); |
| 193 | return (uint32_tcxx . . $ uint32_t#)((last_segment_end + file_size - 1) / file_size); |
| 194 | } |
| 195 | |
| 196 | static inline uint32_tcxx . . $ uint32_t# |
| 197 | grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(uint32_tcxx . . $ uint32_t# version) |
| 198 | { |
| 199 | if (version == 0) { |
| 200 | return GRN_IO_FILE_SIZE_V0cxx . . $ `lib/io.c:50:11`!; |
| 201 | } else { |
| 202 | return GRN_IO_FILE_SIZE_V1cxx . . $ `lib/io.c:45:9`!; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static inline uint32_tcxx . . $ uint32_t# |
| 207 | grn_io_max_segmentcxx . . $ grn_io_max_segment(ee95b3f473df551b).(grn_iocxx . . $ grn_io# *io) |
| 208 | { |
| 209 | if (io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail.) { |
| 210 | return io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail.; |
| 211 | } else { |
| 212 | return io->headercxx . . $ _grn_io#header.->max_segmentcxx . . $ _grn_io_header#max_segment.; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static uint32_tcxx . . $ uint32_t# |
| 217 | grn_io_max_n_filescxx . . $ grn_io_max_n_files(ee95b3f473df551b).(grn_iocxx . . $ grn_io# *io) |
| 218 | { |
| 219 | uint32_tcxx . . $ uint32_t# file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 220 | return grn_io_compute_max_n_filescxx . . $ grn_io_compute_max_n_files(67243219083b5ecc).(io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size., |
| 221 | grn_io_max_segmentcxx . . $ grn_io_max_segment(ee95b3f473df551b).(io), |
| 222 | io->base_segcxx . . $ _grn_io#base_seg., |
| 223 | file_size); |
| 224 | } |
| 225 | |
| 226 | static inline uint32_tcxx . . $ uint32_t# |
| 227 | grn_io_compute_nth_file_infocxx . . $ grn_io_compute_nth_file_info(edffd242529b3d8b).(grn_iocxx . . $ grn_io# *io, uint32_tcxx . . $ uint32_t# nth_segment) |
| 228 | { |
| 229 | uint32_tcxx . . $ uint32_t# segment_size; |
| 230 | uint32_tcxx . . $ uint32_t# file_size; |
| 231 | uint32_tcxx . . $ uint32_t# n_segments_per_file; |
| 232 | uint32_tcxx . . $ uint32_t# resolved_nth_segment; |
| 233 | |
| 234 | segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 235 | file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 236 | n_segments_per_file = file_size / segment_size; |
| 237 | resolved_nth_segment = nth_segment + io->base_segcxx . . $ _grn_io#base_seg.; |
| 238 | return resolved_nth_segment / n_segments_per_file; |
| 239 | } |
| 240 | |
| 241 | static grn_iocxx . . $ grn_io# * |
| 242 | grn_io_create_tmpcxx . . $ grn_io_create_tmp(c3a908de4c247567).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 243 | uint32_tcxx . . $ uint32_t# header_size, |
| 244 | uint32_tcxx . . $ uint32_t# segment_size, |
| 245 | uint32_tcxx . . $ uint32_t# max_segment, |
| 246 | grn_io_modecxx . . $ grn_io_mode# mode, |
| 247 | uint32_tcxx . . $ uint32_t# flags) |
| 248 | { |
| 249 | grn_iocxx . . $ grn_io# *io; |
| 250 | uint32_tcxx . . $ uint32_t# b; |
| 251 | struct _grn_io_headercxx . . $ _grn_io_header# *header; |
| 252 | b = grn_io_compute_basecxx . . $ grn_io_compute_base(2141b656ce2cc063).(header_size); |
| 253 | header = (struct _grn_io_headercxx . . $ _grn_io_header# *)GRN_MMAPcxx . . $ `lib/io.c:134:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULL, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 0, b); |
| 254 | if (header) { |
| 255 | header->versioncxx . . $ _grn_io_header#version. = grn_io_version_defaultcxx . . $ grn_io_version_default.; |
| 256 | header->header_sizecxx . . $ _grn_io_header#header_size. = header_size; |
| 257 | header->segment_sizecxx . . $ _grn_io_header#segment_size. = segment_size; |
| 258 | header->max_segmentcxx . . $ _grn_io_header#max_segment. = max_segment; |
| 259 | header->n_arrayscxx . . $ _grn_io_header#n_arrays. = 0; |
| 260 | header->flagscxx . . $ _grn_io_header#flags. = flags; |
| 261 | header->lockcxx . . $ _grn_io_header#lock. = 0; |
| 262 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(header->idstr, GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`!, 16); |
| 263 | if ((io = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_io)))) { |
| 264 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *maps = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 265 | if ((maps = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_io_mapinfo) * max_segment))) { |
| 266 | io->headercxx . . $ _grn_io#header. = header; |
| 267 | io->user_headercxx . . $ _grn_io#user_header. = (((bytecxx . . $ byte# *)header) + IO_HEADER_SIZEcxx . . $ `lib/io.c:80:9`!); |
| 268 | io->mapscxx . . $ _grn_io#maps. = maps; |
| 269 | io->basecxx . . $ _grn_io#base. = b; |
| 270 | io->base_segcxx . . $ _grn_io#base_seg. = 0; |
| 271 | io->modecxx . . $ _grn_io#mode. = mode; |
| 272 | io->headercxx . . $ _grn_io#header.->curr_sizecxx . . $ _grn_io_header#curr_size. = b; |
| 273 | io->fiscxx . . $ _grn_io#fis. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 274 | io->ainfocxx . . $ _grn_io#ainfo. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 275 | io->max_map_segcxx . . $ _grn_io#max_map_seg. = 0; |
| 276 | io->nmapscxx . . $ _grn_io#nmaps. = 0; |
| 277 | io->countcxx . . $ _grn_io#count. = 0; |
| 278 | io->flagscxx . . $ _grn_io#flags. = GRN_IO_TEMPORARYcxx . . $ `lib/grn_io.h:196:9`!; |
| 279 | io->lockcxx . . $ _grn_io#lock. = &header->lockcxx . . $ _grn_io_header#lock.; |
| 280 | io->pathcxx . . $ _grn_io#path.[0] = '\0'; |
| 281 | return io; |
| 282 | } |
| 283 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io); |
| 284 | } |
| 285 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULL, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, header, b); |
| 286 | } |
| 287 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | grn_io_registercxx . . $ grn_io_register(341fd28fcb683921).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 292 | { |
| 293 | if (io->fiscxx . . $ _grn_io#fis. && (io->flagscxx . . $ _grn_io#flags. & (GRN_IO_EXPIRE_GTICKcxx . . $ `lib/grn_io.h:194:9`! | GRN_IO_EXPIRE_SEGMENTcxx . . $ `lib/grn_io.h:195:9`!))) { |
| 294 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! succeeded = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 295 | CRITICAL_SECTION_ENTERcxx . . $ `lib/grn.h:227:11`!(grn_glock); |
| 296 | if (grn_gctxcxx . . $ grn_gctx..implcxx . . $ _grn_ctx#impl. && grn_gctxcxx . . $ grn_gctx..implcxx . . $ _grn_ctx#impl.->ioscxx . . $ _grn_ctx_impl#ios. && |
| 297 | grn_hash_addcxx . . $ grn_hash_add(251a256fc515235e).(&grn_gctxcxx . . $ grn_gctx., |
| 298 | grn_gctxcxx . . $ grn_gctx..implcxx . . $ _grn_ctx#impl.->ioscxx . . $ _grn_ctx_impl#ios., |
| 299 | io->pathcxx . . $ _grn_io#path., |
| 300 | (unsigned int)strlencxx . . $ strlen(751346f8406fd082).(io->pathcxx . . $ _grn_io#path.), |
| 301 | (void **)&io, |
| 302 | NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)) { |
| 303 | succeeded = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 304 | } |
| 305 | CRITICAL_SECTION_LEAVEcxx . . $ `lib/grn.h:228:11`!(grn_glock); |
| 306 | if (!succeeded) { |
| 307 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_WARNING, "grn_io_register(%s) failed", io->path); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static void |
| 313 | grn_io_unregistercxx . . $ grn_io_unregister(341fd28fcb683921).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 314 | { |
| 315 | if (io->fiscxx . . $ _grn_io#fis. && (io->flagscxx . . $ _grn_io#flags. & (GRN_IO_EXPIRE_GTICKcxx . . $ `lib/grn_io.h:194:9`! | GRN_IO_EXPIRE_SEGMENTcxx . . $ `lib/grn_io.h:195:9`!))) { |
| 316 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! succeeded = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 317 | CRITICAL_SECTION_ENTERcxx . . $ `lib/grn.h:227:11`!(grn_glock); |
| 318 | if (grn_gctxcxx . . $ grn_gctx..implcxx . . $ _grn_ctx#impl. && grn_gctxcxx . . $ grn_gctx..implcxx . . $ _grn_ctx#impl.->ioscxx . . $ _grn_ctx_impl#ios.) { |
| 319 | grn_hash_deletecxx . . $ grn_hash_delete(c1719313595c9549).(&grn_gctxcxx . . $ grn_gctx., |
| 320 | grn_gctxcxx . . $ grn_gctx..implcxx . . $ _grn_ctx#impl.->ioscxx . . $ _grn_ctx_impl#ios., |
| 321 | io->pathcxx . . $ _grn_io#path., |
| 322 | (unsigned int)strlencxx . . $ strlen(751346f8406fd082).(io->pathcxx . . $ _grn_io#path.), |
| 323 | NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!); |
| 324 | succeeded = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 325 | } |
| 326 | CRITICAL_SECTION_LEAVEcxx . . $ `lib/grn.h:228:11`!(grn_glock); |
| 327 | if (!succeeded) { |
| 328 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_WARNING, "grn_io_unregister(%s) failed", io->path); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | grn_iocxx . . $ grn_io# * |
| 334 | grn_io_createcxx . . $ grn_io_create(d9bf0f53c110ad98).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 335 | const char *path, |
| 336 | uint32_tcxx . . $ uint32_t# header_size, |
| 337 | uint32_tcxx . . $ uint32_t# segment_size, |
| 338 | uint32_tcxx . . $ uint32_t# max_segment, |
| 339 | grn_io_modecxx . . $ grn_io_mode# mode, |
| 340 | uint32_tcxx . . $ uint32_t# flags) |
| 341 | { |
| 342 | grn_iocxx . . $ grn_io# *io; |
| 343 | fileinfocxx . . $ fileinfo# *fis; |
| 344 | size_t path_length; |
| 345 | uint32_tcxx . . $ uint32_t# b, max_nfiles; |
| 346 | uint32_tcxx . . $ uint32_t# bs; |
| 347 | struct _grn_io_headercxx . . $ _grn_io_header# *header; |
| 348 | uint32_tcxx . . $ uint32_t# version = grn_io_version_defaultcxx . . $ grn_io_version_default.; |
| 349 | uint32_tcxx . . $ uint32_t# file_size; |
| 350 | |
| 351 | if (!path) { |
| 352 | return grn_io_create_tmpcxx . . $ grn_io_create_tmp(c3a908de4c247567).(ctx, |
| 353 | header_size, |
| 354 | segment_size, |
| 355 | max_segment, |
| 356 | mode, |
| 357 | flags); |
| 358 | } |
| 359 | path_length = strlencxx . . $ strlen(751346f8406fd082).(path); |
| 360 | if (path_length == 0 || (path_length > PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! - 4)) { |
| 361 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 362 | } |
| 363 | b = grn_io_compute_basecxx . . $ grn_io_compute_base(2141b656ce2cc063).(header_size); |
| 364 | bs = grn_io_compute_base_segmentcxx . . $ grn_io_compute_base_segment(83f10ac8959d5577).(b, segment_size); |
| 365 | file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(version); |
| 366 | max_nfiles = |
| 367 | grn_io_compute_max_n_filescxx . . $ grn_io_compute_max_n_files(67243219083b5ecc).(segment_size, max_segment, bs, file_size); |
| 368 | if ((fis = GRN_MALLOCNcxx . . $ `lib/grn_alloc.h:50:9`!(fileinfo, max_nfiles))) { |
| 369 | grn_fileinfo_initcxx . . $ grn_fileinfo_init(4f59c63a7bfebd5).(fis, max_nfiles); |
| 370 | if (!grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, fis, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`! | O_EXCLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:50:10`!)) { |
| 371 | header = |
| 372 | (struct _grn_io_headercxx . . $ _grn_io_header# *)GRN_MMAPcxx . . $ `lib/io.c:134:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &fis->fmo, fis, 0, b); |
| 373 | if (header) { |
| 374 | header->versioncxx . . $ _grn_io_header#version. = version; |
| 375 | header->header_sizecxx . . $ _grn_io_header#header_size. = header_size; |
| 376 | header->segment_sizecxx . . $ _grn_io_header#segment_size. = segment_size; |
| 377 | header->max_segmentcxx . . $ _grn_io_header#max_segment. = max_segment; |
| 378 | header->n_arrayscxx . . $ _grn_io_header#n_arrays. = 0; |
| 379 | header->flagscxx . . $ _grn_io_header#flags. = flags; |
| 380 | header->lockcxx . . $ _grn_io_header#lock. = 0; |
| 381 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(header->idstr, GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`!, 16); |
| 382 | GRN_MSYNCcxx . . $ `lib/io.c:149:9`!(ctx, &(fis[0]), header, b); |
| 383 | if ((io = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_io)))) { |
| 384 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *maps = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 385 | if ((maps = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_io_mapinfo) * max_segment))) { |
| 386 | grn_strncpycxx . . $ `include/groonga/portability.h:108:11`!(io->path, PATH_MAX, path, path_length + 1); |
| 387 | io->headercxx . . $ _grn_io#header. = header; |
| 388 | io->user_headercxx . . $ _grn_io#user_header. = (((bytecxx . . $ byte# *)header) + IO_HEADER_SIZEcxx . . $ `lib/io.c:80:9`!); |
| 389 | io->mapscxx . . $ _grn_io#maps. = maps; |
| 390 | io->basecxx . . $ _grn_io#base. = b; |
| 391 | io->base_segcxx . . $ _grn_io#base_seg. = bs; |
| 392 | io->modecxx . . $ _grn_io#mode. = mode; |
| 393 | io->headercxx . . $ _grn_io#header.->curr_sizecxx . . $ _grn_io_header#curr_size. = b; |
| 394 | io->fiscxx . . $ _grn_io#fis. = fis; |
| 395 | io->ainfocxx . . $ _grn_io#ainfo. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 396 | io->max_map_segcxx . . $ _grn_io#max_map_seg. = 0; |
| 397 | io->nmapscxx . . $ _grn_io#nmaps. = 0; |
| 398 | io->countcxx . . $ _grn_io#count. = 0; |
| 399 | io->flagscxx . . $ _grn_io#flags. = flags; |
| 400 | io->lockcxx . . $ _grn_io#lock. = &header->lockcxx . . $ _grn_io_header#lock.; |
| 401 | grn_io_registercxx . . $ grn_io_register(341fd28fcb683921).(ctx, io); |
| 402 | return io; |
| 403 | } |
| 404 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io); |
| 405 | } |
| 406 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &fis->fmo, fis, header, b); |
| 407 | } |
| 408 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, fis); |
| 409 | if (grn_unlinkcxx . . $ `include/groonga/portability.h:88:11`!(path) == 0) { |
| 410 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 411 | GRN_LOG_INFO, |
| 412 | "[io][create][error] removed path: <%s>", |
| 413 | path); |
| 414 | } else { |
| 415 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("[io][create][error] failed to remove path: <%s>", path); |
| 416 | } |
| 417 | } |
| 418 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(fis); |
| 419 | } |
| 420 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 421 | } |
| 422 | |
| 423 | static grn_rccxx . . $ grn_rc# |
| 424 | array_init_cxx . . $ array_init_(c816c576d43a1546).( |
| 425 | grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, uint32_tcxx . . $ uint32_t# n_arrays, size_t hsize, size_t msize) |
| 426 | { |
| 427 | uint32_tcxx . . $ uint32_t# i; |
| 428 | uint32_tcxx . . $ uint32_t# ws; |
| 429 | bytecxx . . $ byte# *hp, *mp; |
| 430 | grn_io_array_speccxx . . $ grn_io_array_spec# *array_specs = (grn_io_array_speccxx . . $ grn_io_array_spec# *)io->user_headercxx . . $ _grn_io#user_header.; |
| 431 | hp = io->user_headercxx . . $ _grn_io#user_header.; |
| 432 | if (!(mp = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(msize))) { |
| 433 | return GRN_NO_MEMORY_AVAILABLEcxx . . $ GRN_NO_MEMORY_AVAILABLE.; |
| 434 | } |
| 435 | io->ainfocxx . . $ _grn_io#ainfo. = (grn_io_array_infocxx . . $ grn_io_array_info# *)mp; |
| 436 | hp += sizeof(grn_io_array_speccxx . . $ grn_io_array_spec#) * n_arrays; |
| 437 | mp += sizeof(grn_io_array_infocxx . . $ grn_io_array_info#) * n_arrays; |
| 438 | for (ws = 0; (uint32_tcxx . . $ uint32_t#)(1 << ws) < io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; ws++) |
| 439 | ; |
| 440 | for (i = 0; i < n_arrays; i++) { |
| 441 | uint32_tcxx . . $ uint32_t# we = ws - array_specs[i].w_of_elementcxx . . $ _grn_io_array_spec#w_of_element.; |
| 442 | io->ainfocxx . . $ _grn_io#ainfo.[i].w_of_elm_in_a_segmentcxx . . $ _grn_io_array_info#w_of_elm_in_a_segment. = we; |
| 443 | io->ainfocxx . . $ _grn_io#ainfo.[i].elm_mask_in_a_segmentcxx . . $ _grn_io_array_info#elm_mask_in_a_segment. = (1U << we) - 1U; |
| 444 | io->ainfocxx . . $ _grn_io#ainfo.[i].max_n_segmentscxx . . $ _grn_io_array_info#max_n_segments. = array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 445 | io->ainfocxx . . $ _grn_io#ainfo.[i].element_sizecxx . . $ _grn_io_array_info#element_size. = 1 << array_specs[i].w_of_elementcxx . . $ _grn_io_array_spec#w_of_element.; |
| 446 | io->ainfocxx . . $ _grn_io#ainfo.[i].segmentscxx . . $ _grn_io_array_info#segments. = (uint32_tcxx . . $ uint32_t# *)hp; |
| 447 | io->ainfocxx . . $ _grn_io#ainfo.[i].addrscxx . . $ _grn_io_array_info#addrs. = (void **)mp; |
| 448 | hp += sizeof(uint32_tcxx . . $ uint32_t#) * array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 449 | mp += sizeof(void *) * array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 450 | } |
| 451 | io->user_headercxx . . $ _grn_io#user_header. += hsize; |
| 452 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 453 | } |
| 454 | |
| 455 | static grn_rccxx . . $ grn_rc# |
| 456 | array_initcxx . . $ array_init(91a4d4589c2b26cd).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, uint32_tcxx . . $ uint32_t# n_arrays) |
| 457 | { |
| 458 | if (n_arrays) { |
| 459 | uint32_tcxx . . $ uint32_t# i; |
| 460 | grn_io_array_speccxx . . $ grn_io_array_spec# *array_specs = (grn_io_array_speccxx . . $ grn_io_array_spec# *)io->user_headercxx . . $ _grn_io#user_header.; |
| 461 | size_t hsize = sizeof(grn_io_array_speccxx . . $ grn_io_array_spec#) * n_arrays; |
| 462 | size_t msize = sizeof(grn_io_array_infocxx . . $ grn_io_array_info#) * n_arrays; |
| 463 | for (i = 0; i < n_arrays; i++) { |
| 464 | hsize += sizeof(uint32_tcxx . . $ uint32_t#) * array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 465 | msize += sizeof(void *) * array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 466 | } |
| 467 | return array_init_cxx . . $ array_init_(c816c576d43a1546).(ctx, io, n_arrays, hsize, msize); |
| 468 | } |
| 469 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 470 | } |
| 471 | |
| 472 | grn_iocxx . . $ grn_io# * |
| 473 | grn_io_create_with_arraycxx . . $ grn_io_create_with_array(adb444cbd60a11cc).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 474 | const char *path, |
| 475 | uint32_tcxx . . $ uint32_t# header_size, |
| 476 | uint32_tcxx . . $ uint32_t# segment_size, |
| 477 | grn_io_modecxx . . $ grn_io_mode# mode, |
| 478 | uint32_tcxx . . $ uint32_t# n_arrays, |
| 479 | grn_io_array_speccxx . . $ grn_io_array_spec# *array_specs) |
| 480 | { |
| 481 | if (n_arrays) { |
| 482 | uint32_tcxx . . $ uint32_t# i; |
| 483 | grn_iocxx . . $ grn_io# *io; |
| 484 | bytecxx . . $ byte# *hp; |
| 485 | uint32_tcxx . . $ uint32_t# nsegs = 0; |
| 486 | size_t hsize = sizeof(grn_io_array_speccxx . . $ grn_io_array_spec#) * n_arrays; |
| 487 | size_t msize = sizeof(grn_io_array_infocxx . . $ grn_io_array_info#) * n_arrays; |
| 488 | for (i = 0; i < n_arrays; i++) { |
| 489 | nsegs += array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 490 | hsize += sizeof(uint32_tcxx . . $ uint32_t#) * array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 491 | msize += sizeof(void *) * array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.; |
| 492 | } |
| 493 | if ((io = grn_io_createcxx . . $ grn_io_create(d9bf0f53c110ad98).(ctx, |
| 494 | path, |
| 495 | (uint32_tcxx . . $ uint32_t#)(header_size + hsize), |
| 496 | segment_size, |
| 497 | nsegs, |
| 498 | mode, |
| 499 | GRN_IO_EXPIRE_GTICKcxx . . $ `lib/grn_io.h:194:9`!))) { |
| 500 | grn_rccxx . . $ grn_rc# rc; |
| 501 | hp = io->user_headercxx . . $ _grn_io#user_header.; |
| 502 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(hp, array_specs, sizeof(grn_io_array_spec) * n_arrays); |
| 503 | io->headercxx . . $ _grn_io#header.->n_arrayscxx . . $ _grn_io_header#n_arrays. = n_arrays; |
| 504 | io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail. = 1; |
| 505 | rc = array_init_cxx . . $ array_init_(c816c576d43a1546).(ctx, io, n_arrays, hsize, msize); |
| 506 | if (rc == GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 507 | return io; |
| 508 | } |
| 509 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_MEMORY_AVAILABLE, "grn_io_create_with_array failed"); |
| 510 | grn_io_closecxx . . $ grn_io_close(117c07c672382ce3).(ctx, io); |
| 511 | } |
| 512 | } |
| 513 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 514 | } |
| 515 | |
| 516 | static inline uint32_tcxx . . $ uint32_t# |
| 517 | segment_alloccxx . . $ segment_alloc(7752bae3ff5e951e).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 518 | { |
| 519 | uint32_tcxx . . $ uint32_t# n, s; |
| 520 | grn_io_array_infocxx . . $ grn_io_array_info# *ai; |
| 521 | if (io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail.) { |
| 522 | if (io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail. > io->headercxx . . $ _grn_io#header.->max_segmentcxx . . $ _grn_io_header#max_segment.) { |
| 523 | s = 0; |
| 524 | } else { |
| 525 | s = io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail.++; |
| 526 | } |
| 527 | } else { |
| 528 | char *used = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(io->header->max_segment + 1); |
| 529 | if (!used) { |
| 530 | return 0; |
| 531 | } |
| 532 | for (n = io->headercxx . . $ _grn_io#header.->n_arrayscxx . . $ _grn_io_header#n_arrays., ai = io->ainfocxx . . $ _grn_io#ainfo.; n; n--, ai++) { |
| 533 | for (s = 0; s < ai->max_n_segmentscxx . . $ _grn_io_array_info#max_n_segments.; s++) { |
| 534 | used[ai->segmentscxx . . $ _grn_io_array_info#segments.[s]] = 1; |
| 535 | } |
| 536 | } |
| 537 | for (s = 1;; s++) { |
| 538 | if (s > io->headercxx . . $ _grn_io#header.->max_segmentcxx . . $ _grn_io_header#max_segment.) { |
| 539 | io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail. = s; |
| 540 | s = 0; |
| 541 | break; |
| 542 | } |
| 543 | if (!used[s]) { |
| 544 | io->headercxx . . $ _grn_io#header.->segment_tailcxx . . $ _grn_io_header#segment_tail. = s + 1; |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(used); |
| 549 | } |
| 550 | return s; |
| 551 | } |
| 552 | |
| 553 | void |
| 554 | grn_io_segment_alloccxx . . $ grn_io_segment_alloc(334338f53ffa5aeb).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 555 | grn_iocxx . . $ grn_io# *io, |
| 556 | grn_io_array_infocxx . . $ grn_io_array_info# *ai, |
| 557 | uint32_tcxx . . $ uint32_t# lseg, |
| 558 | int *flags, |
| 559 | void **p) |
| 560 | { |
| 561 | uint32_tcxx . . $ uint32_t# *sp = &ai->segmentscxx . . $ _grn_io_array_info#segments.[lseg]; |
| 562 | if (!*sp) { |
| 563 | if ((*flags & GRN_TABLE_ADDcxx . . $ `lib/grn_io.h:188:9`!)) { |
| 564 | if ((*sp = segment_alloccxx . . $ segment_alloc(7752bae3ff5e951e).(ctx, io))) { |
| 565 | *flags |= GRN_TABLE_ADDEDcxx . . $ `lib/grn_io.h:189:9`!; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | if (*sp) { |
| 570 | uint32_tcxx . . $ uint32_t# pseg = *sp - 1; |
| 571 | *p = grn_io_seg_refcxx . . $ grn_io_seg_ref(fe74d77a0ed8c416).(ctx, io, pseg); |
| 572 | if (*p) { |
| 573 | grn_io_seg_unrefcxx . . $ grn_io_seg_unref(30134023abeb0ec6).(ctx, io, pseg); |
| 574 | }; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | uint32_tcxx . . $ uint32_t# |
| 579 | grn_io_detect_typecxx . . $ grn_io_detect_type(1be90328514dd2c4).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *path) |
| 580 | { |
| 581 | struct _grn_io_headercxx . . $ _grn_io_header# h; |
| 582 | uint32_tcxx . . $ uint32_t# res = 0; |
| 583 | int fd; |
| 584 | grn_opencxx . . $ `include/groonga/portability.h:172:11`!(fd, path, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`! | GRN_OPEN_FLAG_BINARYcxx . . $ `include/groonga/portability.h:171:11`!); |
| 585 | if (fd != -1) { |
| 586 | struct statcxx . . $ stat# s; |
| 587 | if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &s) != -1 && |
| 588 | (size_t)(s.st_sizecxx . . $ stat#st_size.) >= sizeof(struct _grn_io_headercxx . . $ _grn_io_header#)) { |
| 589 | if (grn_readcxx . . $ `include/groonga/portability.h:161:11`!(fd, &h, sizeof(struct _grn_io_header)) == |
| 590 | sizeof(struct _grn_io_headercxx . . $ _grn_io_header#)) { |
| 591 | if (!memcmpcxx . . $ memcmp(36f949851efa3055).(h.idstrcxx . . $ _grn_io_header#idstr., GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`!, GRN_IO_IDSTR_LENcxx . . $ `lib/io.c:41:9`!)) { |
| 592 | res = h.typecxx . . $ _grn_io_header#type.; |
| 593 | } else { |
| 594 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 595 | "failed to detect type: format ID is different: <%s>: <%.*s>", |
| 596 | path, |
| 597 | (int)GRN_IO_IDSTR_LENcxx . . $ `lib/io.c:41:9`!, |
| 598 | GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`!); |
| 599 | } |
| 600 | } else { |
| 601 | SERRcxx . . $ `lib/grn_error.h:411:11`!("failed to read enough data for detecting type: <%s>", path); |
| 602 | } |
| 603 | } else { |
| 604 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_FORMAT, "grn_io_detect_type failed"); |
| 605 | } |
| 606 | grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fd); |
| 607 | } else { |
| 608 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("failed to open path for detecting type: <%s>", path); |
| 609 | } |
| 610 | return res; |
| 611 | } |
| 612 | |
| 613 | grn_iocxx . . $ grn_io# * |
| 614 | grn_io_opencxx . . $ grn_io_open(f25995ba89d9e599).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *path, grn_io_modecxx . . $ grn_io_mode# mode) |
| 615 | { |
| 616 | size_t max_path_len = PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! - 4; |
| 617 | size_t path_length; |
| 618 | struct statcxx . . $ stat# s; |
| 619 | fileinfocxx . . $ fileinfo# fi; |
| 620 | uint32_tcxx . . $ uint32_t# flags = 0; |
| 621 | uint32_tcxx . . $ uint32_t# b; |
| 622 | uint32_tcxx . . $ uint32_t# header_size = 0, segment_size = 0, max_segment = 0, bs; |
| 623 | if (!path || !*path) { |
| 624 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "[io][open] path is missing"); |
| 625 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 626 | } |
| 627 | path_length = strlencxx . . $ strlen(751346f8406fd082).(path); |
| 628 | if (path_length > max_path_len) { |
| 629 | int truncate_length = 10; |
| 630 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 631 | "[io][open] path is too long: " |
| 632 | "<%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ">(max: %" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "): <%.*s...>", |
| 633 | path_length, |
| 634 | max_path_len, |
| 635 | truncate_length, |
| 636 | path); |
| 637 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 638 | } |
| 639 | { |
| 640 | struct _grn_io_headercxx . . $ _grn_io_header# h; |
| 641 | int fd; |
| 642 | ssize_tcxx . . $ ssize_t# read_bytes; |
| 643 | grn_opencxx . . $ `include/groonga/portability.h:172:11`!(fd, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | GRN_OPEN_FLAG_BINARYcxx . . $ `include/groonga/portability.h:171:11`!); |
| 644 | if (fd == -1) { |
| 645 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("[io][open] failed to open path: <%s>", path); |
| 646 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 647 | } |
| 648 | if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &s) == -1) { |
| 649 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("[io][open] failed to file status: <%s>", path); |
| 650 | grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fd); |
| 651 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 652 | } |
| 653 | if ((size_t)(s.st_sizecxx . . $ stat#st_size.) < sizeof(struct _grn_io_headercxx . . $ _grn_io_header#)) { |
| 654 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 655 | "[io][open] file size is too small: " |
| 656 | "<%" GRN_FMT_INT64Dcxx . . $ `lib/grn.h:369:11`! ">(required: >= %" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "): <%s>", |
| 657 | (int64_t)(s.st_size), |
| 658 | sizeof(struct _grn_io_header), |
| 659 | path); |
| 660 | grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fd); |
| 661 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 662 | } |
| 663 | read_bytes = grn_readcxx . . $ `include/groonga/portability.h:161:11`!(fd, &h, sizeof(struct _grn_io_header)); |
| 664 | if (read_bytes != sizeof(struct _grn_io_headercxx . . $ _grn_io_header#)) { |
| 665 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("[io][open] failed to read header data: " |
| 666 | "<%" GRN_FMT_SSIZEcxx . . $ `lib/grn.h:409:11`! ">(expected: %" GRN_FMT_SSIZEcxx . . $ `lib/grn.h:409:11`! "): <%s>", |
| 667 | read_bytes, |
| 668 | sizeof(struct _grn_io_header), |
| 669 | path); |
| 670 | grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fd); |
| 671 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 672 | } |
| 673 | if (memcmpcxx . . $ memcmp(36f949851efa3055).(h.idstrcxx . . $ _grn_io_header#idstr., GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`!, GRN_IO_IDSTR_LENcxx . . $ `lib/io.c:41:9`!) != 0) { |
| 674 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 675 | "[io][open] failed to open: format ID is different: <%s>: <%.*s>", |
| 676 | path, |
| 677 | (int)GRN_IO_IDSTR_LENcxx . . $ `lib/io.c:41:9`!, |
| 678 | GRN_IO_IDSTRcxx . . $ `lib/io.c:40:9`!); |
| 679 | grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fd); |
| 680 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 681 | } |
| 682 | header_size = h.header_sizecxx . . $ _grn_io_header#header_size.; |
| 683 | segment_size = h.segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 684 | max_segment = h.max_segmentcxx . . $ _grn_io_header#max_segment.; |
| 685 | flags = h.flagscxx . . $ _grn_io_header#flags.; |
| 686 | grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fd); |
| 687 | if (segment_size == 0) { |
| 688 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 689 | "[io][open] failed to open: segment size is 0: <%s>", |
| 690 | path); |
| 691 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 692 | } |
| 693 | } |
| 694 | b = grn_io_compute_basecxx . . $ grn_io_compute_base(2141b656ce2cc063).(header_size); |
| 695 | bs = grn_io_compute_base_segmentcxx . . $ grn_io_compute_base_segment(83f10ac8959d5577).(b, segment_size); |
| 696 | grn_fileinfo_initcxx . . $ grn_fileinfo_init(4f59c63a7bfebd5).(&fi, 1); |
| 697 | grn_rccxx . . $ grn_rc# rc = grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, &fi, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!); |
| 698 | if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 699 | ERRcxx . . $ `lib/grn_error.h:156:9`!(rc, "[io][open] failed to open fileinfo: <%s>", path); |
| 700 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 701 | } |
| 702 | |
| 703 | struct _grn_io_headercxx . . $ _grn_io_header# *header; |
| 704 | header = GRN_MMAPcxx . . $ `lib/io.c:134:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &(fi.fmo), &fi, 0, b); |
| 705 | if (!header) { |
| 706 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, &fi); |
| 707 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_MEMORY_AVAILABLE, "[io][open] failed to map: <%s>", path); |
| 708 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 709 | } |
| 710 | |
| 711 | uint32_tcxx . . $ uint32_t# file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(header->versioncxx . . $ _grn_io_header#version.); |
| 712 | uint32_tcxx . . $ uint32_t# max_nfiles = |
| 713 | grn_io_compute_max_n_filescxx . . $ grn_io_compute_max_n_files(67243219083b5ecc).(segment_size, max_segment, bs, file_size); |
| 714 | fileinfocxx . . $ fileinfo# *fis = GRN_MALLOCNcxx . . $ `lib/grn_alloc.h:50:9`!(fileinfo, max_nfiles); |
| 715 | if (!fis) { |
| 716 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &(fi.fmo), &fi, header, b); |
| 717 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, &fi); |
| 718 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_MEMORY_AVAILABLE, |
| 719 | "[io][open] failed to allocate fileinfo: <%s>", |
| 720 | path); |
| 721 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 722 | } |
| 723 | |
| 724 | grn_fileinfo_initcxx . . $ grn_fileinfo_init(4f59c63a7bfebd5).(fis, max_nfiles); |
| 725 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(fis, &fi, sizeof(fileinfo)); |
| 726 | grn_iocxx . . $ grn_io# *io = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_io)); |
| 727 | if (!io) { |
| 728 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(fis); |
| 729 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &(fi.fmo), &fi, header, b); |
| 730 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, &fi); |
| 731 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_MEMORY_AVAILABLE, |
| 732 | "[io][open] failed to allocate grn_io: <%s>", |
| 733 | path); |
| 734 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 735 | } |
| 736 | |
| 737 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *maps = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_io_mapinfo) * max_segment); |
| 738 | if (!maps) { |
| 739 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io); |
| 740 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(fis); |
| 741 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &(fi.fmo), &fi, header, b); |
| 742 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, &fi); |
| 743 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_MEMORY_AVAILABLE, |
| 744 | "[io][open] failed to allocate grn_io_mapinfo: <%s>", |
| 745 | path); |
| 746 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 747 | } |
| 748 | |
| 749 | grn_strncpycxx . . $ `include/groonga/portability.h:108:11`!(io->path, PATH_MAX, path, path_length + 1); |
| 750 | io->headercxx . . $ _grn_io#header. = header; |
| 751 | io->user_headercxx . . $ _grn_io#user_header. = (((bytecxx . . $ byte# *)header) + IO_HEADER_SIZEcxx . . $ `lib/io.c:80:9`!); |
| 752 | io->mapscxx . . $ _grn_io#maps. = maps; |
| 753 | io->basecxx . . $ _grn_io#base. = b; |
| 754 | io->base_segcxx . . $ _grn_io#base_seg. = bs; |
| 755 | io->modecxx . . $ _grn_io#mode. = mode; |
| 756 | io->fiscxx . . $ _grn_io#fis. = fis; |
| 757 | io->ainfocxx . . $ _grn_io#ainfo. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 758 | io->max_map_segcxx . . $ _grn_io#max_map_seg. = 0; |
| 759 | io->nmapscxx . . $ _grn_io#nmaps. = 0; |
| 760 | io->countcxx . . $ _grn_io#count. = 0; |
| 761 | io->flagscxx . . $ _grn_io#flags. = header->flagscxx . . $ _grn_io_header#flags.; |
| 762 | io->lockcxx . . $ _grn_io#lock. = &header->lockcxx . . $ _grn_io_header#lock.; |
| 763 | rc = array_initcxx . . $ array_init(91a4d4589c2b26cd).(ctx, io, io->headercxx . . $ _grn_io#header.->n_arrayscxx . . $ _grn_io_header#n_arrays.); |
| 764 | if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 765 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io->maps); |
| 766 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io); |
| 767 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(fis); |
| 768 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &(fi.fmo), &fi, header, b); |
| 769 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, &fi); |
| 770 | ERRcxx . . $ `lib/grn_error.h:156:9`!(rc, "[io][open] failed to initialize array: <%s>", path); |
| 771 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 772 | } |
| 773 | |
| 774 | grn_io_registercxx . . $ grn_io_register(341fd28fcb683921).(ctx, io); |
| 775 | return io; |
| 776 | } |
| 777 | |
| 778 | grn_rccxx . . $ grn_rc# |
| 779 | grn_io_closecxx . . $ grn_io_close(117c07c672382ce3).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 780 | { |
| 781 | uint32_tcxx . . $ uint32_t# max_nfiles; |
| 782 | |
| 783 | max_nfiles = grn_io_max_n_filescxx . . $ grn_io_max_n_files(ee95b3f473df551b).(io); |
| 784 | grn_io_unregistercxx . . $ grn_io_unregister(341fd28fcb683921).(ctx, io); |
| 785 | if (io->ainfocxx . . $ _grn_io#ainfo.) { |
| 786 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io->ainfo); |
| 787 | } |
| 788 | if (io->mapscxx . . $ _grn_io#maps.) { |
| 789 | uint32_tcxx . . $ uint32_t# i; |
| 790 | uint32_tcxx . . $ uint32_t# max_segment; |
| 791 | uint32_tcxx . . $ uint32_t# segment_size; |
| 792 | uint32_tcxx . . $ uint32_t# file_size; |
| 793 | uint32_tcxx . . $ uint32_t# n_segments_per_file; |
| 794 | |
| 795 | max_segment = grn_io_max_segmentcxx . . $ grn_io_max_segment(ee95b3f473df551b).(io); |
| 796 | segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 797 | file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 798 | n_segments_per_file = file_size / segment_size; |
| 799 | for (i = 0; i < max_segment; i++) { |
| 800 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi; |
| 801 | mi = &(io->mapscxx . . $ _grn_io#maps.[i]); |
| 802 | if (mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) { |
| 803 | fileinfocxx . . $ fileinfo# *fi = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 804 | /* if (atomic_read(mi->nref)) { return STILL_IN_USE ; } */ |
| 805 | if (io->fiscxx . . $ _grn_io#fis.) { |
| 806 | uint32_tcxx . . $ uint32_t# bseg = i + io->base_segcxx . . $ _grn_io#base_seg.; |
| 807 | uint32_tcxx . . $ uint32_t# fno = bseg / n_segments_per_file; |
| 808 | fi = &io->fiscxx . . $ _grn_io#fis.[fno]; |
| 809 | } |
| 810 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, io, &mi->fmo, fi, mi->map, segment_size); |
| 811 | } |
| 812 | } |
| 813 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io->maps); |
| 814 | } |
| 815 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, |
| 816 | io, |
| 817 | (io->fis ? &io->fis->fmo : NULL), |
| 818 | io->fis, |
| 819 | io->header, |
| 820 | io->base); |
| 821 | if (io->fiscxx . . $ _grn_io#fis.) { |
| 822 | uint32_tcxx . . $ uint32_t# i; |
| 823 | for (i = 0; i < max_nfiles; i++) { |
| 824 | fileinfocxx . . $ fileinfo# *fi = &(io->fiscxx . . $ _grn_io#fis.[i]); |
| 825 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, fi); |
| 826 | } |
| 827 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io->fis); |
| 828 | } |
| 829 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(io); |
| 830 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 831 | } |
| 832 | |
| 833 | uint32_tcxx . . $ uint32_t# |
| 834 | grn_io_base_segcxx . . $ grn_io_base_seg(ee95b3f473df551b).(grn_iocxx . . $ grn_io# *io) |
| 835 | { |
| 836 | return io->base_segcxx . . $ _grn_io#base_seg.; |
| 837 | } |
| 838 | |
| 839 | const char * |
| 840 | grn_io_pathcxx . . $ grn_io_path(53b2e212b3c13636).(grn_iocxx . . $ grn_io# *io) |
| 841 | { |
| 842 | return io->pathcxx . . $ _grn_io#path.; |
| 843 | } |
| 844 | |
| 845 | void * |
| 846 | grn_io_headercxx . . $ grn_io_header(611b9b393b7178f5).(grn_iocxx . . $ grn_io# *io) |
| 847 | { |
| 848 | return io->user_headercxx . . $ _grn_io#user_header.; |
| 849 | } |
| 850 | |
| 851 | grn_rccxx . . $ grn_rc# |
| 852 | grn_io_set_typecxx . . $ grn_io_set_type(d505e6e1d36b3afe).(grn_iocxx . . $ grn_io# *io, uint32_tcxx . . $ uint32_t# type) |
| 853 | { |
| 854 | if (!io || !io->headercxx . . $ _grn_io#header.) { |
| 855 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 856 | } |
| 857 | io->headercxx . . $ _grn_io#header.->typecxx . . $ _grn_io_header#type. = type; |
| 858 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 859 | } |
| 860 | |
| 861 | uint32_tcxx . . $ uint32_t# |
| 862 | grn_io_get_typecxx . . $ grn_io_get_type(ee95b3f473df551b).(grn_iocxx . . $ grn_io# *io) |
| 863 | { |
| 864 | if (!io || !io->headercxx . . $ _grn_io#header.) { |
| 865 | return GRN_VOIDcxx . . $ `include/groonga/groonga.h:863:9`!; |
| 866 | } |
| 867 | return io->headercxx . . $ _grn_io#header.->typecxx . . $ _grn_io_header#type.; |
| 868 | } |
| 869 | |
| 870 | static inline void |
| 871 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(const char *path, char *buffer, uint32_tcxx . . $ uint32_t# fno) |
| 872 | { |
| 873 | size_t len = strlencxx . . $ strlen(751346f8406fd082).(path); |
| 874 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(buffer, path, len); |
| 875 | if (fno) { |
| 876 | buffer[len] = '.'; |
| 877 | grn_itohcxx . . $ grn_itoh(8d45ad636653b6f2).(fno, buffer + len + 1, 3); |
| 878 | buffer[len + 4] = '\0'; |
| 879 | } else { |
| 880 | buffer[len] = '\0'; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | static uint32_tcxx . . $ uint32_t# |
| 885 | grn_io_n_filescxx . . $ grn_io_n_files(7752bae3ff5e951e).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 886 | { |
| 887 | unsigned long file_size; |
| 888 | file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 889 | return (uint32_tcxx . . $ uint32_t#)((io->headercxx . . $ _grn_io#header.->curr_sizecxx . . $ _grn_io_header#curr_size. + file_size - 1) / file_size); |
| 890 | } |
| 891 | |
| 892 | grn_rccxx . . $ grn_rc# |
| 893 | grn_io_sizecxx . . $ grn_io_size(cad0b01aa2d38ece).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, uint64_tcxx . . $ uint64_t# *size) |
| 894 | { |
| 895 | uint32_tcxx . . $ uint32_t# fno; |
| 896 | struct statcxx . . $ stat# s; |
| 897 | uint64_tcxx . . $ uint64_t# tsize = 0; |
| 898 | char buffer[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 899 | uint32_tcxx . . $ uint32_t# n_files; |
| 900 | |
| 901 | n_files = grn_io_n_filescxx . . $ grn_io_n_files(7752bae3ff5e951e).(ctx, io); |
| 902 | for (fno = 0; fno < n_files; fno++) { |
| 903 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., buffer, fno); |
| 904 | if (statcxx . . $ stat(a9cd1e23e96f423c).(buffer, &s)) { |
| 905 | SERRcxx . . $ `lib/grn_error.h:411:11`!("failed to stat path to compute size: <%s>", buffer); |
| 906 | } else { |
| 907 | tsize += (uint64_tcxx . . $ uint64_t#)(s.st_sizecxx . . $ stat#st_size.); |
| 908 | } |
| 909 | } |
| 910 | *size = tsize; |
| 911 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 912 | } |
| 913 | |
| 914 | grn_rccxx . . $ grn_rc# |
| 915 | grn_io_remove_rawcxx . . $ grn_io_remove_raw(cae1c943be750b4c).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *path) |
| 916 | { |
| 917 | grn_rccxx . . $ grn_rc# rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 918 | uint32_tcxx . . $ uint32_t# fno; |
| 919 | char buffer[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 920 | |
| 921 | if (grn_unlinkcxx . . $ `include/groonga/portability.h:88:11`!(path) != 0) { |
| 922 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("[io][remove] failed to remove path: <%s>", path); |
| 923 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 924 | } |
| 925 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_INFO, "[io][remove] removed path: <%s>", path); |
| 926 | |
| 927 | for (fno = 1;; fno++) { |
| 928 | struct statcxx . . $ stat# s; |
| 929 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(path, buffer, fno); |
| 930 | if (statcxx . . $ stat(a9cd1e23e96f423c).(buffer, &s) != 0) { |
| 931 | break; |
| 932 | } |
| 933 | if (grn_unlinkcxx . . $ `include/groonga/portability.h:88:11`!(buffer) == 0) { |
| 934 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 935 | GRN_LOG_INFO, |
| 936 | "[io][remove] removed numbered path: <%d>: <%s>", |
| 937 | fno, |
| 938 | buffer); |
| 939 | } else { |
| 940 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("[io][remove] failed to remove numbered path: <%d>: <%s>", |
| 941 | fno, |
| 942 | buffer); |
| 943 | rc = ctx->rccxx . . $ _grn_ctx#rc.; |
| 944 | } |
| 945 | } |
| 946 | return rc; |
| 947 | } |
| 948 | |
| 949 | grn_rccxx . . $ grn_rc# |
| 950 | grn_io_removecxx . . $ grn_io_remove(cae1c943be750b4c).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *path) |
| 951 | { |
| 952 | struct statcxx . . $ stat# s; |
| 953 | |
| 954 | if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &s) != 0) { |
| 955 | SERRcxx . . $ `lib/grn_error.h:411:11`!("failed to stat: <%s>", path); |
| 956 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 957 | } |
| 958 | |
| 959 | return grn_io_remove_rawcxx . . $ grn_io_remove_raw(cae1c943be750b4c).(ctx, path); |
| 960 | } |
| 961 | |
| 962 | grn_rccxx . . $ grn_rc# |
| 963 | grn_io_remove_if_existcxx . . $ grn_io_remove_if_exist(cae1c943be750b4c).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *path) |
| 964 | { |
| 965 | struct statcxx . . $ stat# s; |
| 966 | if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &s) == 0) { |
| 967 | return grn_io_remove_rawcxx . . $ grn_io_remove_raw(cae1c943be750b4c).(ctx, path); |
| 968 | } |
| 969 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 970 | } |
| 971 | |
| 972 | grn_rccxx . . $ grn_rc# |
| 973 | grn_io_renamecxx . . $ grn_io_rename(acfed50c3cac5568).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *old_name, const char *new_name) |
| 974 | { |
| 975 | const char *tag = "[io][rename]"; |
| 976 | struct statcxx . . $ stat# s; |
| 977 | if (statcxx . . $ stat(a9cd1e23e96f423c).(old_name, &s)) { |
| 978 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to stat path to be renamed: <%s>", tag, old_name); |
| 979 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 980 | } else if (renamecxx . . $ rename(5ae29d726ae57f88).(old_name, new_name)) { |
| 981 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to rename path: <%s> -> <%s>", tag, old_name, new_name); |
| 982 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 983 | } else { |
| 984 | uint32_tcxx . . $ uint32_t# fno; |
| 985 | char old_buffer[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 986 | char new_buffer[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 987 | for (fno = 1;; fno++) { |
| 988 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(old_name, old_buffer, fno); |
| 989 | if (statcxx . . $ stat(a9cd1e23e96f423c).(old_buffer, &s) == 0) { |
| 990 | break; |
| 991 | } |
| 992 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(new_name, new_buffer, fno); |
| 993 | if (renamecxx . . $ rename(5ae29d726ae57f88).(old_buffer, new_buffer)) { |
| 994 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to rename path: <%s> -> <%s>", |
| 995 | tag, |
| 996 | old_buffer, |
| 997 | new_buffer); |
| 998 | } |
| 999 | } |
| 1000 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | typedef structcxx . . $ $anonymous_type_f9b45d9e4903aa16_0# { |
| 1005 | grn_io_ja_eheadcxx . . $ grn_io_ja_ehead# headcxx . . $ $anonymous_type_f9b45d9e4903aa16_0#head.; |
| 1006 | char bodycxx . . $ $anonymous_type_f9b45d9e4903aa16_0#body.[256]; |
| 1007 | } ja_elementcxx . . $ ja_element#; |
| 1008 | |
| 1009 | grn_rccxx . . $ grn_rc# |
| 1010 | grn_io_read_jacxx . . $ grn_io_read_ja(77e40c6d8fdca877).(grn_iocxx . . $ grn_io# *io, |
| 1011 | grn_ctxcxx . . $ grn_ctx# *ctx, |
| 1012 | grn_io_ja_einfocxx . . $ grn_io_ja_einfo# *einfo, |
| 1013 | uint32_tcxx . . $ uint32_t# epos, |
| 1014 | uint32_tcxx . . $ uint32_t# key, |
| 1015 | uint32_tcxx . . $ uint32_t# segment, |
| 1016 | uint32_tcxx . . $ uint32_t# offset, |
| 1017 | void **value, |
| 1018 | uint32_tcxx . . $ uint32_t# *value_len) |
| 1019 | { |
| 1020 | size_t rest = 0; |
| 1021 | size_t size = *value_len + sizeof(grn_io_ja_eheadcxx . . $ grn_io_ja_ehead#); |
| 1022 | uint32_tcxx . . $ uint32_t# segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 1023 | uint32_tcxx . . $ uint32_t# file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 1024 | uint32_tcxx . . $ uint32_t# n_segments_per_file = file_size / segment_size; |
| 1025 | uint32_tcxx . . $ uint32_t# bseg = segment + io->base_segcxx . . $ _grn_io#base_seg.; |
| 1026 | uint32_tcxx . . $ uint32_t# fno = bseg / n_segments_per_file; |
| 1027 | fileinfocxx . . $ fileinfo# *fi = &io->fiscxx . . $ _grn_io#fis.[fno]; |
| 1028 | off_tcxx . . $ off_t# base = fno ? 0 : (off_tcxx . . $ off_t#)(io->basecxx . . $ _grn_io#base. - segment_size * io->base_segcxx . . $ _grn_io#base_seg.); |
| 1029 | off_tcxx . . $ off_t# pos = segment_size * (bseg % n_segments_per_file) + offset + base; |
| 1030 | ja_elementcxx . . $ ja_element# *v = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(size); |
| 1031 | if (!v) { |
| 1032 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1033 | *value_len = 0; |
| 1034 | return GRN_NO_MEMORY_AVAILABLEcxx . . $ GRN_NO_MEMORY_AVAILABLE.; |
| 1035 | } |
| 1036 | if (pos + (off_tcxx . . $ off_t#)size > (off_tcxx . . $ off_t#)file_size) { |
| 1037 | rest = (size_t)(pos + (off_tcxx . . $ off_t#)size - file_size); |
| 1038 | size = (size_t)(file_size - pos); |
| 1039 | } |
| 1040 | if (!grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fi)) { |
| 1041 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1042 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, fno); |
| 1043 | if (grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, fi, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!)) { |
| 1044 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1045 | *value_len = 0; |
| 1046 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1047 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 1048 | } |
| 1049 | } |
| 1050 | if (grn_preadcxx . . $ grn_pread(4577714838bf4b55).(ctx, fi, v, size, pos)) { |
| 1051 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1052 | *value_len = 0; |
| 1053 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1054 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 1055 | } |
| 1056 | if (einfo->poscxx . . $ _grn_io_ja_einfo#pos. != epos) { |
| 1057 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1058 | GRN_LOG_WARNING, |
| 1059 | "einfo pos changed %x => %x", |
| 1060 | einfo->pos, |
| 1061 | epos); |
| 1062 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1063 | *value_len = 0; |
| 1064 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1065 | return GRN_FILE_CORRUPTcxx . . $ GRN_FILE_CORRUPT.; |
| 1066 | } |
| 1067 | if (einfo->sizecxx . . $ _grn_io_ja_einfo#size. != *value_len) { |
| 1068 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1069 | GRN_LOG_WARNING, |
| 1070 | "einfo size changed %d => %d", |
| 1071 | einfo->size, |
| 1072 | *value_len); |
| 1073 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1074 | *value_len = 0; |
| 1075 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1076 | return GRN_FILE_CORRUPTcxx . . $ GRN_FILE_CORRUPT.; |
| 1077 | } |
| 1078 | if (v->headcxx . . $ $anonymous_type_f9b45d9e4903aa16_0#head..keycxx . . $ _grn_io_ja_ehead#key. != key) { |
| 1079 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_ERROR, "ehead key unmatch %x => %x", key, v->head.key); |
| 1080 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1081 | *value_len = 0; |
| 1082 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1083 | return GRN_INVALID_FORMATcxx . . $ GRN_INVALID_FORMAT.; |
| 1084 | } |
| 1085 | if (v->headcxx . . $ $anonymous_type_f9b45d9e4903aa16_0#head..sizecxx . . $ _grn_io_ja_ehead#size. != *value_len) { |
| 1086 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1087 | GRN_LOG_ERROR, |
| 1088 | "ehead size unmatch %d => %d", |
| 1089 | *value_len, |
| 1090 | v->head.size); |
| 1091 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1092 | *value_len = 0; |
| 1093 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1094 | return GRN_INVALID_FORMATcxx . . $ GRN_INVALID_FORMAT.; |
| 1095 | } |
| 1096 | if (rest) { |
| 1097 | bytecxx . . $ byte# *vr = (bytecxx . . $ byte# *)v + size; |
| 1098 | do { |
| 1099 | fi = &io->fiscxx . . $ _grn_io#fis.[++fno]; |
| 1100 | if (!grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fi)) { |
| 1101 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1102 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, fno); |
| 1103 | if (grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, fi, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!)) { |
| 1104 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1105 | *value_len = 0; |
| 1106 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1107 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 1108 | } |
| 1109 | } |
| 1110 | size = rest > file_size ? file_size : rest; |
| 1111 | if (grn_preadcxx . . $ grn_pread(4577714838bf4b55).(ctx, fi, vr, size, 0)) { |
| 1112 | *value = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1113 | *value_len = 0; |
| 1114 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(v); |
| 1115 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 1116 | } |
| 1117 | vr += size; |
| 1118 | rest -= size; |
| 1119 | } while (rest); |
| 1120 | } |
| 1121 | *value = v->bodycxx . . $ $anonymous_type_f9b45d9e4903aa16_0#body.; |
| 1122 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1123 | } |
| 1124 | |
| 1125 | grn_rccxx . . $ grn_rc# |
| 1126 | grn_io_write_jacxx . . $ grn_io_write_ja(83ed76a22e0380a9).(grn_iocxx . . $ grn_io# *io, |
| 1127 | grn_ctxcxx . . $ grn_ctx# *ctx, |
| 1128 | uint32_tcxx . . $ uint32_t# key, |
| 1129 | uint32_tcxx . . $ uint32_t# segment, |
| 1130 | uint32_tcxx . . $ uint32_t# offset, |
| 1131 | void *value, |
| 1132 | uint32_tcxx . . $ uint32_t# value_len) |
| 1133 | { |
| 1134 | grn_rccxx . . $ grn_rc# rc; |
| 1135 | size_t rest = 0; |
| 1136 | size_t size = value_len + sizeof(grn_io_ja_eheadcxx . . $ grn_io_ja_ehead#); |
| 1137 | uint32_tcxx . . $ uint32_t# segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 1138 | uint32_tcxx . . $ uint32_t# file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 1139 | uint32_tcxx . . $ uint32_t# n_segments_per_file = file_size / segment_size; |
| 1140 | uint32_tcxx . . $ uint32_t# bseg = segment + io->base_segcxx . . $ _grn_io#base_seg.; |
| 1141 | uint32_tcxx . . $ uint32_t# fno = bseg / n_segments_per_file; |
| 1142 | fileinfocxx . . $ fileinfo# *fi = &io->fiscxx . . $ _grn_io#fis.[fno]; |
| 1143 | off_tcxx . . $ off_t# base = fno ? 0 : (off_tcxx . . $ off_t#)(io->basecxx . . $ _grn_io#base.) - segment_size * io->base_segcxx . . $ _grn_io#base_seg.; |
| 1144 | off_tcxx . . $ off_t# pos = segment_size * (bseg % n_segments_per_file) + offset + base; |
| 1145 | if (pos + (off_tcxx . . $ off_t#)size > (off_tcxx . . $ off_t#)file_size) { |
| 1146 | rest = (size_t)(pos + (off_tcxx . . $ off_t#)size - file_size); |
| 1147 | size = (size_t)(file_size - pos); |
| 1148 | } |
| 1149 | if (!grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fi)) { |
| 1150 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1151 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, fno); |
| 1152 | if ((rc = grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, fi, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!))) { |
| 1153 | return rc; |
| 1154 | } |
| 1155 | } |
| 1156 | if (value_len <= 256) { |
| 1157 | ja_elementcxx . . $ ja_element# je; |
| 1158 | je.headcxx . . $ $anonymous_type_f9b45d9e4903aa16_0#head..sizecxx . . $ _grn_io_ja_ehead#size. = value_len; |
| 1159 | je.headcxx . . $ $anonymous_type_f9b45d9e4903aa16_0#head..keycxx . . $ _grn_io_ja_ehead#key. = key; |
| 1160 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(je.body, value, value_len); |
| 1161 | rc = grn_pwritecxx . . $ grn_pwrite(4577714838bf4b55).(ctx, fi, &je, size, pos); |
| 1162 | } else { |
| 1163 | grn_io_ja_eheadcxx . . $ grn_io_ja_ehead# eh; |
| 1164 | eh.sizecxx . . $ _grn_io_ja_ehead#size. = value_len; |
| 1165 | eh.keycxx . . $ _grn_io_ja_ehead#key. = key; |
| 1166 | if ((rc = grn_pwritecxx . . $ grn_pwrite(4577714838bf4b55).(ctx, fi, &eh, sizeof(grn_io_ja_eheadcxx . . $ grn_io_ja_ehead#), pos))) { |
| 1167 | return rc; |
| 1168 | } |
| 1169 | pos += (off_tcxx . . $ off_t#)sizeof(grn_io_ja_eheadcxx . . $ grn_io_ja_ehead#); |
| 1170 | rc = grn_pwritecxx . . $ grn_pwrite(4577714838bf4b55).(ctx, fi, value, size - sizeof(grn_io_ja_eheadcxx . . $ grn_io_ja_ehead#), pos); |
| 1171 | } |
| 1172 | if (rc) { |
| 1173 | return rc; |
| 1174 | } |
| 1175 | if (rest) { |
| 1176 | bytecxx . . $ byte# *vr = (bytecxx . . $ byte# *)value + size - sizeof(grn_io_ja_eheadcxx . . $ grn_io_ja_ehead#); |
| 1177 | do { |
| 1178 | fi = &io->fiscxx . . $ _grn_io#fis.[++fno]; |
| 1179 | if (!grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fi)) { |
| 1180 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1181 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, fno); |
| 1182 | if ((rc = grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, fi, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!))) { |
| 1183 | return rc; |
| 1184 | } |
| 1185 | } |
| 1186 | size = rest > file_size ? file_size : rest; |
| 1187 | if ((rc = grn_pwritecxx . . $ grn_pwrite(4577714838bf4b55).(ctx, fi, vr, size, 0))) { |
| 1188 | return rc; |
| 1189 | } |
| 1190 | vr += size; |
| 1191 | rest -= size; |
| 1192 | } while (rest); |
| 1193 | } |
| 1194 | return rc; |
| 1195 | } |
| 1196 | |
| 1197 | grn_rccxx . . $ grn_rc# |
| 1198 | grn_io_write_ja_eheadcxx . . $ grn_io_write_ja_ehead(ba5be44a0ced10b1).(grn_iocxx . . $ grn_io# *io, |
| 1199 | grn_ctxcxx . . $ grn_ctx# *ctx, |
| 1200 | uint32_tcxx . . $ uint32_t# key, |
| 1201 | uint32_tcxx . . $ uint32_t# segment, |
| 1202 | uint32_tcxx . . $ uint32_t# offset, |
| 1203 | uint32_tcxx . . $ uint32_t# value_len) |
| 1204 | { |
| 1205 | grn_rccxx . . $ grn_rc# rc; |
| 1206 | uint32_tcxx . . $ uint32_t# segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 1207 | uint32_tcxx . . $ uint32_t# file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 1208 | uint32_tcxx . . $ uint32_t# n_segments_per_file = file_size / segment_size; |
| 1209 | uint32_tcxx . . $ uint32_t# bseg = segment + io->base_segcxx . . $ _grn_io#base_seg.; |
| 1210 | uint32_tcxx . . $ uint32_t# fno = bseg / n_segments_per_file; |
| 1211 | fileinfocxx . . $ fileinfo# *fi = &io->fiscxx . . $ _grn_io#fis.[fno]; |
| 1212 | off_tcxx . . $ off_t# base = fno ? 0 : (off_tcxx . . $ off_t#)(io->basecxx . . $ _grn_io#base.) - segment_size + io->base_segcxx . . $ _grn_io#base_seg.; |
| 1213 | off_tcxx . . $ off_t# pos = segment_size * (bseg % n_segments_per_file) + offset + base; |
| 1214 | if (!grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fi)) { |
| 1215 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1216 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, fno); |
| 1217 | if ((rc = grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, fi, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!))) { |
| 1218 | return rc; |
| 1219 | } |
| 1220 | } |
| 1221 | { |
| 1222 | grn_io_ja_eheadcxx . . $ grn_io_ja_ehead# eh; |
| 1223 | eh.sizecxx . . $ _grn_io_ja_ehead#size. = value_len; |
| 1224 | eh.keycxx . . $ _grn_io_ja_ehead#key. = key; |
| 1225 | return grn_pwritecxx . . $ grn_pwrite(4577714838bf4b55).(ctx, fi, &eh, sizeof(grn_io_ja_eheadcxx . . $ grn_io_ja_ehead#), pos); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | void * |
| 1230 | grn_io_win_mapcxx . . $ grn_io_win_map(17c5f72fc42c8edd).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 1231 | grn_iocxx . . $ grn_io# *io, |
| 1232 | grn_io_wincxx . . $ grn_io_win# *iw, |
| 1233 | uint32_tcxx . . $ uint32_t# segment, |
| 1234 | uint32_tcxx . . $ uint32_t# offset, |
| 1235 | uint32_tcxx . . $ uint32_t# size, |
| 1236 | grn_io_rw_modecxx . . $ grn_io_rw_mode# mode) |
| 1237 | { |
| 1238 | uint32_tcxx . . $ uint32_t# nseg, segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 1239 | if (offset >= segment_size) { |
| 1240 | segment += offset / segment_size; |
| 1241 | offset = offset % segment_size; |
| 1242 | } |
| 1243 | nseg = (offset + size + segment_size - 1) / segment_size; |
| 1244 | if (!size || !ctx || segment + nseg > io->headercxx . . $ _grn_io#header.->max_segmentcxx . . $ _grn_io_header#max_segment.) { |
| 1245 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1246 | } |
| 1247 | iw->ctxcxx . . $ $anonymous_type_fee031cfc08e20b0_2#ctx. = ctx; |
| 1248 | iw->diffcxx . . $ $anonymous_type_fee031cfc08e20b0_2#diff. = 0; |
| 1249 | iw->iocxx . . $ $anonymous_type_fee031cfc08e20b0_2#io. = io; |
| 1250 | iw->modecxx . . $ $anonymous_type_fee031cfc08e20b0_2#mode. = (uint8_tcxx . . $ uint8_t#)mode; |
| 1251 | iw->tiny_pcxx . . $ $anonymous_type_fee031cfc08e20b0_2#tiny_p. = 0; |
| 1252 | iw->segmentcxx . . $ $anonymous_type_fee031cfc08e20b0_2#segment. = segment; |
| 1253 | iw->offsetcxx . . $ $anonymous_type_fee031cfc08e20b0_2#offset. = offset; |
| 1254 | iw->nsegcxx . . $ $anonymous_type_fee031cfc08e20b0_2#nseg. = nseg; |
| 1255 | iw->sizecxx . . $ $anonymous_type_fee031cfc08e20b0_2#size. = size; |
| 1256 | if (nseg == 1) { |
| 1257 | bytecxx . . $ byte# *addr = grn_io_seg_refcxx . . $ grn_io_seg_ref(fe74d77a0ed8c416).(ctx, io, segment); |
| 1258 | if (!addr) { |
| 1259 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1260 | } |
| 1261 | iw->cachedcxx . . $ $anonymous_type_fee031cfc08e20b0_2#cached. = 1; |
| 1262 | iw->addrcxx . . $ $anonymous_type_fee031cfc08e20b0_2#addr. = addr + offset; |
| 1263 | } else { |
| 1264 | if (!(iw->addrcxx . . $ $anonymous_type_fee031cfc08e20b0_2#addr. = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(size))) { |
| 1265 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1266 | } |
| 1267 | iw->cachedcxx . . $ $anonymous_type_fee031cfc08e20b0_2#cached. = 0; |
| 1268 | switch (mode) { |
| 1269 | case GRN_IO_RDONLYcxx . . $ GRN_IO_RDONLY.: |
| 1270 | case GRN_IO_RDWRcxx . . $ GRN_IO_RDWR.: |
| 1271 | { |
| 1272 | bytecxx . . $ byte# *p, *q = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1273 | uint32_tcxx . . $ uint32_t# s, r; |
| 1274 | for (p = iw->addrcxx . . $ $anonymous_type_fee031cfc08e20b0_2#addr., r = size; r; p += s, r -= s, segment++, offset = 0) { |
| 1275 | q = grn_io_seg_refcxx . . $ grn_io_seg_ref(fe74d77a0ed8c416).(ctx, io, segment); |
| 1276 | if (!q) { |
| 1277 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(iw->addr); |
| 1278 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1279 | } |
| 1280 | s = (offset + r > segment_size) ? segment_size - offset : r; |
| 1281 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(p, q + offset, s); |
| 1282 | grn_io_seg_unrefcxx . . $ grn_io_seg_unref(30134023abeb0ec6).(ctx, io, segment); |
| 1283 | } |
| 1284 | } |
| 1285 | break; |
| 1286 | case GRN_IO_WRONLYcxx . . $ GRN_IO_WRONLY.: |
| 1287 | break; |
| 1288 | default: |
| 1289 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1290 | } |
| 1291 | } |
| 1292 | return iw->addrcxx . . $ $anonymous_type_fee031cfc08e20b0_2#addr.; |
| 1293 | } |
| 1294 | |
| 1295 | grn_rccxx . . $ grn_rc# |
| 1296 | grn_io_win_unmapcxx . . $ grn_io_win_unmap(2a637fdbe33921a9).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_io_wincxx . . $ grn_io_win# *iw) |
| 1297 | { |
| 1298 | if (!iw || !iw->iocxx . . $ $anonymous_type_fee031cfc08e20b0_2#io. || !iw->ctxcxx . . $ $anonymous_type_fee031cfc08e20b0_2#ctx.) { |
| 1299 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 1300 | } |
| 1301 | if (iw->cachedcxx . . $ $anonymous_type_fee031cfc08e20b0_2#cached.) { |
| 1302 | if (!iw->tiny_pcxx . . $ $anonymous_type_fee031cfc08e20b0_2#tiny_p.) { |
| 1303 | grn_io_seg_unrefcxx . . $ grn_io_seg_unref(30134023abeb0ec6).(ctx, iw->iocxx . . $ $anonymous_type_fee031cfc08e20b0_2#io., iw->segmentcxx . . $ $anonymous_type_fee031cfc08e20b0_2#segment.); |
| 1304 | } |
| 1305 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1306 | } |
| 1307 | { |
| 1308 | grn_iocxx . . $ grn_io# *io = iw->iocxx . . $ $anonymous_type_fee031cfc08e20b0_2#io.; |
| 1309 | grn_ctxcxx . . $ grn_ctx# *ctx = iw->ctxcxx . . $ $anonymous_type_fee031cfc08e20b0_2#ctx.; |
| 1310 | switch (iw->modecxx . . $ $anonymous_type_fee031cfc08e20b0_2#mode.) { |
| 1311 | case GRN_IO_RDONLYcxx . . $ GRN_IO_RDONLY.: |
| 1312 | if (!iw->addrcxx . . $ $anonymous_type_fee031cfc08e20b0_2#addr.) { |
| 1313 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 1314 | } |
| 1315 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(iw->addr); |
| 1316 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1317 | case GRN_IO_RDWRcxx . . $ GRN_IO_RDWR.: |
| 1318 | case GRN_IO_WRONLYcxx . . $ GRN_IO_WRONLY.: |
| 1319 | { |
| 1320 | bytecxx . . $ byte# *p, *q = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1321 | uint32_tcxx . . $ uint32_t# segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 1322 | uint32_tcxx . . $ uint32_t# s, r, offset = iw->offsetcxx . . $ $anonymous_type_fee031cfc08e20b0_2#offset., segment = iw->segmentcxx . . $ $anonymous_type_fee031cfc08e20b0_2#segment.; |
| 1323 | for (p = iw->addrcxx . . $ $anonymous_type_fee031cfc08e20b0_2#addr., r = iw->sizecxx . . $ $anonymous_type_fee031cfc08e20b0_2#size.; r; |
| 1324 | p += s, r -= s, segment++, offset = 0) { |
| 1325 | q = grn_io_seg_refcxx . . $ grn_io_seg_ref(fe74d77a0ed8c416).(ctx, io, segment); |
| 1326 | if (!q) { |
| 1327 | return GRN_NO_MEMORY_AVAILABLEcxx . . $ GRN_NO_MEMORY_AVAILABLE.; |
| 1328 | } |
| 1329 | s = (offset + r > segment_size) ? segment_size - offset : r; |
| 1330 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(q + offset, p, s); |
| 1331 | grn_io_seg_unrefcxx . . $ grn_io_seg_unref(30134023abeb0ec6).(ctx, io, segment); |
| 1332 | } |
| 1333 | } |
| 1334 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(iw->addr); |
| 1335 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1336 | default: |
| 1337 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | #define DO_MAPcxx . . $ `lib/io.c:1342:9`!(io, fmo, fi, pos, size, segno, res) \ |
| 1343 | do { \ |
| 1344 | (res) = GRN_MMAPcxx . . $ `lib/io.c:134:11`!(ctx, (io), (fmo), (fi), (pos), (size)); \ |
| 1345 | if ((res)) { \ |
| 1346 | uint32_t nmaps; \ |
| 1347 | if (io->max_map_seg < segno) { \ |
| 1348 | io->max_map_seg = segno; \ |
| 1349 | } \ |
| 1350 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(&io->nmaps, 1, nmaps); \ |
| 1351 | { \ |
| 1352 | uint64_t tail = io->base + (uint64_t)(size) * ((segno) + 1); \ |
| 1353 | if (tail > io->header->curr_size) { \ |
| 1354 | io->header->curr_size = tail; \ |
| 1355 | } \ |
| 1356 | } \ |
| 1357 | } \ |
| 1358 | } while (0) |
| 1359 | |
| 1360 | void |
| 1361 | grn_io_seg_map_cxx . . $ grn_io_seg_map_(ac1ae5c59b1408f9).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, uint32_tcxx . . $ uint32_t# segno, grn_io_mapinfocxx . . $ grn_io_mapinfo# *info) |
| 1362 | { |
| 1363 | uint32_tcxx . . $ uint32_t# segment_size = io->headercxx . . $ _grn_io#header.->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 1364 | if ((io->flagscxx . . $ _grn_io#flags. & GRN_IO_TEMPORARYcxx . . $ `lib/grn_io.h:196:9`!)) { |
| 1365 | DO_MAPcxx . . $ `lib/io.c:1342:9`!(io, &info->fmo, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 0, segment_size, segno, info->map); |
| 1366 | } else { |
| 1367 | uint32_tcxx . . $ uint32_t# file_size = grn_io_compute_file_sizecxx . . $ grn_io_compute_file_size(2141b656ce2cc063).(io->headercxx . . $ _grn_io#header.->versioncxx . . $ _grn_io_header#version.); |
| 1368 | uint32_tcxx . . $ uint32_t# n_segments_per_file = file_size / segment_size; |
| 1369 | uint32_tcxx . . $ uint32_t# bseg = segno + io->base_segcxx . . $ _grn_io#base_seg.; |
| 1370 | uint32_tcxx . . $ uint32_t# fno = bseg / n_segments_per_file; |
| 1371 | off_tcxx . . $ off_t# base = fno ? 0 : (off_tcxx . . $ off_t#)(io->basecxx . . $ _grn_io#base.) - segment_size * io->base_segcxx . . $ _grn_io#base_seg.; |
| 1372 | off_tcxx . . $ off_t# pos = segment_size * (bseg % n_segments_per_file) + base; |
| 1373 | fileinfocxx . . $ fileinfo# *fi = &io->fiscxx . . $ _grn_io#fis.[fno]; |
| 1374 | if (!grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fi)) { |
| 1375 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1376 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, fno); |
| 1377 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! path_exist = grn_path_existcxx . . $ grn_path_exist(c5f7da30cbc0f4e9).(path); |
| 1378 | if (!grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(ctx, fi, path, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!)) { |
| 1379 | DO_MAPcxx . . $ `lib/io.c:1342:9`!(io, &info->fmo, fi, pos, segment_size, segno, info->map); |
| 1380 | if (!info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. && !path_exist) { |
| 1381 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(ctx, fi); |
| 1382 | if (grn_unlinkcxx . . $ `include/groonga/portability.h:88:11`!(path) == 0) { |
| 1383 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1384 | GRN_LOG_INFO, |
| 1385 | "[io][map][error] memory mapping is failed and then " |
| 1386 | "removed created map file: <%s>", |
| 1387 | path); |
| 1388 | } else { |
| 1389 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("[io][map][error] memory mapping is failed and then " |
| 1390 | "failed to remove created map file: <%s>", |
| 1391 | path); |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | } else { |
| 1396 | DO_MAPcxx . . $ `lib/io.c:1342:9`!(io, &info->fmo, fi, pos, segment_size, segno, info->map); |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | grn_rccxx . . $ grn_rc# |
| 1402 | grn_io_seg_expirecxx . . $ grn_io_seg_expire(e0580d1d0e28a8d4).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, uint32_tcxx . . $ uint32_t# segno, uint32_tcxx . . $ uint32_t# nretry) |
| 1403 | { |
| 1404 | uint32_tcxx . . $ uint32_t# retry, *pnref; |
| 1405 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *info; |
| 1406 | if (!io->mapscxx . . $ _grn_io#maps. || segno >= io->headercxx . . $ _grn_io#header.->max_segmentcxx . . $ _grn_io_header#max_segment.) { |
| 1407 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 1408 | } |
| 1409 | info = &io->mapscxx . . $ _grn_io#maps.[segno]; |
| 1410 | if (!info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) { |
| 1411 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 1412 | } |
| 1413 | pnref = &info->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.; |
| 1414 | for (retry = 0;; retry++) { |
| 1415 | uint32_tcxx . . $ uint32_t# nref; |
| 1416 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, 1, nref); |
| 1417 | if (nref) { |
| 1418 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, -1, nref); |
| 1419 | if (retry >= GRN_IO_MAX_RETRYcxx . . $ `lib/grn_io.h:191:9`!) { |
| 1420 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1421 | GRN_LOG_CRIT, |
| 1422 | "deadlock detected! in grn_io_seg_expire(%p, %u, %u)", |
| 1423 | io, |
| 1424 | segno, |
| 1425 | nref); |
| 1426 | return GRN_RESOURCE_DEADLOCK_AVOIDEDcxx . . $ GRN_RESOURCE_DEADLOCK_AVOIDED.; |
| 1427 | } |
| 1428 | } else { |
| 1429 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, GRN_IO_MAX_REFcxx . . $ `lib/grn_io.h:192:9`!, nref); |
| 1430 | uint32_tcxx . . $ uint32_t# reset_max_ref_by_overflow = GRN_IO_MAX_REFcxx . . $ `lib/grn_io.h:192:9`! - 1; |
| 1431 | if (nref > 1) { |
| 1432 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, reset_max_ref_by_overflow, nref); |
| 1433 | GRN_FUTEX_WAKEcxx . . $ `lib/grn.h:720:11`!(pnref); |
| 1434 | if (retry >= GRN_IO_MAX_RETRYcxx . . $ `lib/grn_io.h:191:9`!) { |
| 1435 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1436 | GRN_LOG_CRIT, |
| 1437 | "deadlock detected!! in grn_io_seg_expire(%p, %u, %u)", |
| 1438 | io, |
| 1439 | segno, |
| 1440 | nref); |
| 1441 | return GRN_RESOURCE_DEADLOCK_AVOIDEDcxx . . $ GRN_RESOURCE_DEADLOCK_AVOIDED.; |
| 1442 | } |
| 1443 | } else { |
| 1444 | uint32_tcxx . . $ uint32_t# nmaps; |
| 1445 | fileinfocxx . . $ fileinfo# *fi = &(io->fiscxx . . $ _grn_io#fis.[segno]); |
| 1446 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, |
| 1447 | io, |
| 1448 | &info->fmo, |
| 1449 | fi, |
| 1450 | info->map, |
| 1451 | io->header->segment_size); |
| 1452 | info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1453 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, reset_max_ref_by_overflow, nref); |
| 1454 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(&io->nmaps, -1, nmaps); |
| 1455 | GRN_FUTEX_WAKEcxx . . $ `lib/grn.h:720:11`!(pnref); |
| 1456 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1457 | } |
| 1458 | } |
| 1459 | if (retry >= nretry) { |
| 1460 | return GRN_RESOURCE_DEADLOCK_AVOIDEDcxx . . $ GRN_RESOURCE_DEADLOCK_AVOIDED.; |
| 1461 | } |
| 1462 | GRN_FUTEX_WAITcxx . . $ `lib/grn.h:719:11`!(pnref); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | uint32_tcxx . . $ uint32_t# |
| 1467 | grn_io_expirecxx . . $ grn_io_expire(2a17e79e7b39bf8e).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, uint32_tcxx . . $ uint32_t# count_thresh, uint32_tcxx . . $ uint32_t# limit) |
| 1468 | { |
| 1469 | uint32_tcxx . . $ uint32_t# m, n = 0, ln = io->nmapscxx . . $ _grn_io#nmaps.; |
| 1470 | switch ((io->flagscxx . . $ _grn_io#flags. & (GRN_IO_EXPIRE_GTICKcxx . . $ `lib/grn_io.h:194:9`! | GRN_IO_EXPIRE_SEGMENTcxx . . $ `lib/grn_io.h:195:9`!))) { |
| 1471 | case GRN_IO_EXPIRE_GTICKcxx . . $ `lib/grn_io.h:194:9`!: |
| 1472 | { |
| 1473 | uint32_tcxx . . $ uint32_t# nref, nmaps, *pnref = &io->nrefcxx . . $ _grn_io#nref.; |
| 1474 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, 1, nref); |
| 1475 | if (!nref && grn_gtickcxx . . $ grn_gtick. - io->countcxx . . $ _grn_io#count. > count_thresh) { |
| 1476 | { |
| 1477 | uint32_tcxx . . $ uint32_t# i = io->headercxx . . $ _grn_io#header.->n_arrayscxx . . $ _grn_io_header#n_arrays.; |
| 1478 | grn_io_array_speccxx . . $ grn_io_array_spec# *array_specs = (grn_io_array_speccxx . . $ grn_io_array_spec# *)io->user_headercxx . . $ _grn_io#user_header.; |
| 1479 | while (i--) { |
| 1480 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(io->ainfocxx . . $ _grn_io#ainfo.[i].addrscxx . . $ _grn_io_array_info#addrs., |
| 1481 | 0, |
| 1482 | sizeof(void *) * array_specs[i].max_n_segmentscxx . . $ _grn_io_array_spec#max_n_segments.); |
| 1483 | } |
| 1484 | } |
| 1485 | { |
| 1486 | uint32_tcxx . . $ uint32_t# fno; |
| 1487 | for (fno = 0; fno < io->max_map_segcxx . . $ _grn_io#max_map_seg.; fno++) { |
| 1488 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *info = &(io->mapscxx . . $ _grn_io#maps.[fno]); |
| 1489 | if (info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) { |
| 1490 | fileinfocxx . . $ fileinfo# *fi = &(io->fiscxx . . $ _grn_io#fis.[fno]); |
| 1491 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, |
| 1492 | io, |
| 1493 | &info->fmo, |
| 1494 | fi, |
| 1495 | info->map, |
| 1496 | io->header->segment_size); |
| 1497 | info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1498 | info->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. = 0; |
| 1499 | info->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. = grn_gtickcxx . . $ grn_gtick.; |
| 1500 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(&io->nmaps, -1, nmaps); |
| 1501 | n++; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, -1, nref); |
| 1507 | } |
| 1508 | break; |
| 1509 | case GRN_IO_EXPIRE_SEGMENTcxx . . $ `lib/grn_io.h:195:9`!: |
| 1510 | for (m = io->max_map_segcxx . . $ _grn_io#max_map_seg.; n < limit && m; m--) { |
| 1511 | if (!grn_io_seg_expirecxx . . $ grn_io_seg_expire(e0580d1d0e28a8d4).(ctx, io, m, 0)) { |
| 1512 | n++; |
| 1513 | } |
| 1514 | } |
| 1515 | break; |
| 1516 | case (GRN_IO_EXPIRE_GTICKcxx . . $ `lib/grn_io.h:194:9`! | GRN_IO_EXPIRE_SEGMENTcxx . . $ `lib/grn_io.h:195:9`!): |
| 1517 | { |
| 1518 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *info = io->mapscxx . . $ _grn_io#maps.; |
| 1519 | for (m = io->max_map_segcxx . . $ _grn_io#max_map_seg.; n < limit && m; info++, m--) { |
| 1520 | if (info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. && (grn_gtickcxx . . $ grn_gtick. - info->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count.) > count_thresh) { |
| 1521 | uint32_tcxx . . $ uint32_t# nmaps, nref, *pnref = &info->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.; |
| 1522 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, 1, nref); |
| 1523 | if (!nref && info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. && (grn_gtickcxx . . $ grn_gtick. - info->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count.) > count_thresh) { |
| 1524 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, |
| 1525 | io, |
| 1526 | &info->fmo, |
| 1527 | NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, |
| 1528 | info->map, |
| 1529 | io->header->segment_size); |
| 1530 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(&io->nmaps, -1, nmaps); |
| 1531 | info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 1532 | info->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. = grn_gtickcxx . . $ grn_gtick.; |
| 1533 | n++; |
| 1534 | } |
| 1535 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, -1, nref); |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | break; |
| 1540 | } |
| 1541 | if (n) { |
| 1542 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1543 | GRN_LOG_DEBUG, |
| 1544 | "<%p:%x> expired i=%p max=%d (%d/%d)", |
| 1545 | ctx, |
| 1546 | grn_gtick, |
| 1547 | io, |
| 1548 | io->max_map_seg, |
| 1549 | n, |
| 1550 | ln); |
| 1551 | } |
| 1552 | return n; |
| 1553 | } |
| 1554 | |
| 1555 | void * |
| 1556 | grn_io_anon_mapcxx . . $ grn_io_anon_map(9de643cca07c394).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi, size_t length) |
| 1557 | { |
| 1558 | return (mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = GRN_MMAPcxx . . $ `lib/io.c:134:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &mi->fmo, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 0, length)); |
| 1559 | } |
| 1560 | |
| 1561 | void |
| 1562 | grn_io_anon_unmapcxx . . $ grn_io_anon_unmap(e34ce909368ead97).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi, size_t length) |
| 1563 | { |
| 1564 | GRN_MUNMAPcxx . . $ `lib/io.c:143:11`!(ctx, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, &mi->fmo, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, mi->map, length); |
| 1565 | } |
| 1566 | |
| 1567 | grn_rccxx . . $ grn_rc# |
| 1568 | grn_io_lockcxx . . $ grn_io_lock(2c16bfdb6acf067b).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, int timeout) |
| 1569 | { |
| 1570 | static int _ncalls = 0, _ncolls = 0; |
| 1571 | uint32_tcxx . . $ uint32_t# count, count_log_border = 1000; |
| 1572 | uint32_tcxx . . $ uint32_t# rc_check_interval = 1000; |
| 1573 | _ncalls++; |
| 1574 | if (!io) { |
| 1575 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 1576 | } |
| 1577 | for (count = 0;; count++) { |
| 1578 | uint32_tcxx . . $ uint32_t# lock; |
| 1579 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(io->lock, 1, lock); |
| 1580 | if (lock) { |
| 1581 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(io->lock, -1, lock); |
| 1582 | if (count == count_log_border) { |
| 1583 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1584 | GRN_LOG_NOTICE, |
| 1585 | "io(%s) collisions(%d/%d): lock failed %d times", |
| 1586 | io->path, |
| 1587 | _ncolls, |
| 1588 | _ncalls, |
| 1589 | count_log_border); |
| 1590 | } |
| 1591 | if (!timeout || (timeout > 0 && (uint32_tcxx . . $ uint32_t#)timeout == count)) { |
| 1592 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1593 | GRN_LOG_WARNING, |
| 1594 | "[DB Locked] time out(%d): io(%s) collisions(%d/%d)", |
| 1595 | timeout, |
| 1596 | io->path, |
| 1597 | _ncolls, |
| 1598 | _ncalls); |
| 1599 | break; |
| 1600 | } |
| 1601 | if (!(++_ncolls % 1000000) && (_ncolls > _ncalls)) { |
| 1602 | if (_ncolls < 0 || _ncalls < 0) { |
| 1603 | _ncolls = 0; |
| 1604 | _ncalls = 0; |
| 1605 | } else { |
| 1606 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, |
| 1607 | GRN_LOG_NOTICE, |
| 1608 | "io(%s) collisions(%d/%d)", |
| 1609 | io->path, |
| 1610 | _ncolls, |
| 1611 | _ncalls); |
| 1612 | } |
| 1613 | } |
| 1614 | if ((count % rc_check_interval) == 0) { |
| 1615 | if (ctx->rccxx . . $ _grn_ctx#rc. != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 1616 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 1617 | } |
| 1618 | } |
| 1619 | grn_nanosleepcxx . . $ grn_nanosleep(2c44cd906ae4e829).(GRN_LOCK_WAIT_TIME_NANOSECONDcxx . . $ `build/config.h:32:9`!); |
| 1620 | continue; |
| 1621 | } |
| 1622 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1623 | } |
| 1624 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_RESOURCE_DEADLOCK_AVOIDED, "grn_io_lock failed"); |
| 1625 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 1626 | } |
| 1627 | |
| 1628 | void |
| 1629 | grn_io_unlockcxx . . $ grn_io_unlock(341fd28fcb683921).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 1630 | { |
| 1631 | if (io) { |
| 1632 | uint32_tcxx . . $ uint32_t# lock; |
| 1633 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(io->lock, -1, lock); |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | void |
| 1638 | grn_io_clear_lockcxx . . $ grn_io_clear_lock(69bc83933b8864bc).(grn_iocxx . . $ grn_io# *io) |
| 1639 | { |
| 1640 | if (io) { |
| 1641 | *io->lockcxx . . $ _grn_io#lock. = 0; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | uint32_tcxx . . $ uint32_t# |
| 1646 | grn_io_is_lockedcxx . . $ grn_io_is_locked(ee95b3f473df551b).(grn_iocxx . . $ grn_io# *io) |
| 1647 | { |
| 1648 | return io ? *io->lockcxx . . $ _grn_io#lock. : 0; |
| 1649 | } |
| 1650 | |
| 1651 | grn_rccxx . . $ grn_rc# |
| 1652 | grn_io_flushcxx . . $ grn_io_flush(117c07c672382ce3).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 1653 | { |
| 1654 | grn_rccxx . . $ grn_rc# rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1655 | struct _grn_io_headercxx . . $ _grn_io_header# *header; |
| 1656 | uint32_tcxx . . $ uint32_t# aligned_header_size; |
| 1657 | |
| 1658 | if (io->pathcxx . . $ _grn_io#path.[0] == '\0') { |
| 1659 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 1660 | } |
| 1661 | |
| 1662 | header = io->headercxx . . $ _grn_io#header.; |
| 1663 | aligned_header_size = grn_io_compute_basecxx . . $ grn_io_compute_base(2141b656ce2cc063).(header->header_sizecxx . . $ _grn_io_header#header_size.); |
| 1664 | |
| 1665 | if (GRN_MSYNCcxx . . $ `lib/io.c:149:9`!(ctx, &(io->fis[0]), header, aligned_header_size) != 0) { |
| 1666 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 1667 | } |
| 1668 | |
| 1669 | if (io->mapscxx . . $ _grn_io#maps.) { |
| 1670 | uint32_tcxx . . $ uint32_t# i; |
| 1671 | uint32_tcxx . . $ uint32_t# max_mapped_segment; |
| 1672 | uint32_tcxx . . $ uint32_t# segment_size; |
| 1673 | |
| 1674 | max_mapped_segment = grn_io_max_segmentcxx . . $ grn_io_max_segment(ee95b3f473df551b).(io); |
| 1675 | segment_size = header->segment_sizecxx . . $ _grn_io_header#segment_size.; |
| 1676 | for (i = 0; i < max_mapped_segment; i++) { |
| 1677 | grn_io_mapinfocxx . . $ grn_io_mapinfo# *info = &(io->mapscxx . . $ _grn_io#maps.[i]); |
| 1678 | uint32_tcxx . . $ uint32_t# nth_file_info; |
| 1679 | uint32_tcxx . . $ uint32_t# *pnref; |
| 1680 | uint32_tcxx . . $ uint32_t# nref; |
| 1681 | int msync_result; |
| 1682 | |
| 1683 | if (!info) { |
| 1684 | continue; |
| 1685 | } |
| 1686 | |
| 1687 | pnref = &info->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.; |
| 1688 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, 1, nref); |
| 1689 | if (nref != 0) { |
| 1690 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, -1, nref); |
| 1691 | continue; |
| 1692 | } |
| 1693 | |
| 1694 | if (!info->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) { |
| 1695 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, -1, nref); |
| 1696 | GRN_FUTEX_WAKEcxx . . $ `lib/grn.h:720:11`!(pnref); |
| 1697 | continue; |
| 1698 | } |
| 1699 | |
| 1700 | nth_file_info = grn_io_compute_nth_file_infocxx . . $ grn_io_compute_nth_file_info(edffd242529b3d8b).(io, i); |
| 1701 | msync_result = |
| 1702 | GRN_MSYNCcxx . . $ `lib/io.c:149:9`!(ctx, &(io->fis[nth_file_info]), info->map, segment_size); |
| 1703 | GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(pnref, -1, nref); |
| 1704 | GRN_FUTEX_WAKEcxx . . $ `lib/grn.h:720:11`!(pnref); |
| 1705 | |
| 1706 | if (msync_result != 0) { |
| 1707 | rc = ctx->rccxx . . $ _grn_ctx#rc.; |
| 1708 | break; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | return rc; |
| 1714 | } |
| 1715 | |
| 1716 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 1717 | grn_io_is_corruptcxx . . $ grn_io_is_corrupt(e3678e942125f788).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 1718 | { |
| 1719 | uint32_tcxx . . $ uint32_t# i; |
| 1720 | uint32_tcxx . . $ uint32_t# n_files; |
| 1721 | |
| 1722 | if (!io) { |
| 1723 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 1724 | } |
| 1725 | |
| 1726 | n_files = grn_io_n_filescxx . . $ grn_io_n_files(7752bae3ff5e951e).(ctx, io); |
| 1727 | for (i = 0; i < n_files; i++) { |
| 1728 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1729 | struct statcxx . . $ stat# s; |
| 1730 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, i); |
| 1731 | if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &s) != 0) { |
| 1732 | SERRcxx . . $ `lib/grn_error.h:411:11`!("[io][corrupt] used path doesn't exist: <%s>", path); |
| 1733 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 1738 | } |
| 1739 | |
| 1740 | size_t |
| 1741 | grn_io_get_disk_usagecxx . . $ grn_io_get_disk_usage(c4749d9481ec000d).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 1742 | { |
| 1743 | size_t usage = 0; |
| 1744 | uint32_tcxx . . $ uint32_t# i; |
| 1745 | uint32_tcxx . . $ uint32_t# n_files; |
| 1746 | |
| 1747 | if (!io) { |
| 1748 | return usage; |
| 1749 | } |
| 1750 | |
| 1751 | n_files = grn_io_n_filescxx . . $ grn_io_n_files(7752bae3ff5e951e).(ctx, io); |
| 1752 | for (i = 0; i < n_files; i++) { |
| 1753 | char path[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 1754 | struct statcxx . . $ stat# s; |
| 1755 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., path, i); |
| 1756 | if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &s) != 0) { |
| 1757 | continue; |
| 1758 | } |
| 1759 | usage += (size_t)(s.st_sizecxx . . $ stat#st_size.); |
| 1760 | } |
| 1761 | |
| 1762 | return usage; |
| 1763 | } |
| 1764 | |
| 1765 | /** mmap abstraction **/ |
| 1766 | |
| 1767 | static size_t mmap_sizecxx . . $ mmap_size. = 0; |
| 1768 | |
| 1769 | size_t |
| 1770 | grn_get_memory_map_sizecxx . . $ grn_get_memory_map_size(f708dce9a8856c70).(void) |
| 1771 | { |
| 1772 | return mmap_sizecxx . . $ mmap_size.; |
| 1773 | } |
| 1774 | |
| 1775 | #ifdef WIN32 |
| 1776 | |
| 1777 | inline static grn_rc |
| 1778 | grn_fileinfo_open_v1(grn_ctx *ctx, fileinfo *fi, const char *path, int flags) |
| 1779 | { |
| 1780 | CRITICAL_SECTION_INIT(fi->cs); |
| 1781 | return GRN_SUCCESS; |
| 1782 | } |
| 1783 | |
| 1784 | static inline void * |
| 1785 | grn_mmap_v1( |
| 1786 | grn_ctx *ctx, HANDLE *fmo, fileinfo *fi, int64_t offset, size_t length) |
| 1787 | { |
| 1788 | void *res; |
| 1789 | if (!fi) { |
| 1790 | if (fmo) { |
| 1791 | *fmo = NULL; |
| 1792 | } |
| 1793 | /* TODO: Try to support VirtualAlloc() as anonymous mmap in POSIX. |
| 1794 | * If VirtualAlloc() provides better performance rather than malloc(), |
| 1795 | * we'll use it. |
| 1796 | */ |
| 1797 | return GRN_CALLOC(length); |
| 1798 | } |
| 1799 | /* CRITICAL_SECTION_ENTER(fi->cs); */ |
| 1800 | /* try to create fmo */ |
| 1801 | /* TODO: Add support for 32bit over offset by using dwMaximumSizeHigh. */ |
| 1802 | *fmo = CreateFileMapping(fi->fh, |
| 1803 | NULL, |
| 1804 | PAGE_READWRITE, |
| 1805 | 0, |
| 1806 | (DWORD)(offset + length), |
| 1807 | NULL); |
| 1808 | if (!*fmo) { |
| 1809 | SERR("CreateFileMapping(%lu + %" GRN_FMT_SIZE ") failed " |
| 1810 | "<%" GRN_FMT_SIZE ">", |
| 1811 | (DWORD)offset, |
| 1812 | length, |
| 1813 | mmap_size); |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | res = MapViewOfFile(*fmo, FILE_MAP_WRITE, 0, (DWORD)offset, (SIZE_T)length); |
| 1817 | if (!res) { |
| 1818 | SERR("MapViewOfFile(%lu,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1819 | (DWORD)offset, |
| 1820 | length, |
| 1821 | mmap_size); |
| 1822 | return NULL; |
| 1823 | } |
| 1824 | /* CRITICAL_SECTION_LEAVE(fi->cs); */ |
| 1825 | mmap_size += length; |
| 1826 | return res; |
| 1827 | } |
| 1828 | |
| 1829 | static inline int |
| 1830 | grn_munmap_v1( |
| 1831 | grn_ctx *ctx, HANDLE *fmo, fileinfo *fi, void *start, size_t length) |
| 1832 | { |
| 1833 | int r = 0; |
| 1834 | |
| 1835 | if (!fi) { |
| 1836 | GRN_FREE(start); |
| 1837 | return r; |
| 1838 | } |
| 1839 | |
| 1840 | if (!fmo) { |
| 1841 | GRN_FREE(start); |
| 1842 | return r; |
| 1843 | } |
| 1844 | |
| 1845 | if (*fmo) { |
| 1846 | if (!FlushViewOfFile(start, length)) { |
| 1847 | SERR("FlushViewOfFile(<%p>, <%" GRN_FMT_SIZE ">) failed on unmap", |
| 1848 | start, |
| 1849 | length); |
| 1850 | r = -1; |
| 1851 | } |
| 1852 | if (UnmapViewOfFile(start)) { |
| 1853 | mmap_size -= length; |
| 1854 | } else { |
| 1855 | SERR("UnmapViewOfFile(%p,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1856 | start, |
| 1857 | length, |
| 1858 | mmap_size); |
| 1859 | r = -1; |
| 1860 | } |
| 1861 | if (!CloseHandle(*fmo)) { |
| 1862 | SERR("CloseHandle(%p,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1863 | start, |
| 1864 | length, |
| 1865 | mmap_size); |
| 1866 | } |
| 1867 | *fmo = NULL; |
| 1868 | } else { |
| 1869 | GRN_FREE(start); |
| 1870 | } |
| 1871 | |
| 1872 | return r; |
| 1873 | } |
| 1874 | |
| 1875 | static inline grn_rc |
| 1876 | grn_fileinfo_open_v0(grn_ctx *ctx, fileinfo *fi, const char *path, int flags) |
| 1877 | { |
| 1878 | /* signature may be wrong.. */ |
| 1879 | fi->fmo = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, NULL); |
| 1880 | /* open failed */ |
| 1881 | if (fi->fmo == NULL) { |
| 1882 | // flock |
| 1883 | /* retry to open */ |
| 1884 | fi->fmo = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, NULL); |
| 1885 | /* failed again */ |
| 1886 | if (fi->fmo == NULL) { |
| 1887 | /* try to create fmo */ |
| 1888 | fi->fmo = CreateFileMapping(fi->fh, |
| 1889 | NULL, |
| 1890 | PAGE_READWRITE, |
| 1891 | 0, |
| 1892 | GRN_IO_FILE_SIZE_V0, |
| 1893 | NULL); |
| 1894 | } |
| 1895 | // funlock |
| 1896 | } |
| 1897 | if (fi->fmo != NULL) { |
| 1898 | if (GetLastError() != ERROR_ALREADY_EXISTS) { |
| 1899 | CRITICAL_SECTION_INIT(fi->cs); |
| 1900 | return GRN_SUCCESS; |
| 1901 | } else { |
| 1902 | GRN_LOG(ctx, |
| 1903 | GRN_LOG_ERROR, |
| 1904 | "fmo object already exists! handle=%p", |
| 1905 | fi->fh); |
| 1906 | CloseHandle(fi->fmo); |
| 1907 | } |
| 1908 | } else { |
| 1909 | GRN_LOG(ctx, |
| 1910 | GRN_LOG_ALERT, |
| 1911 | "failed to get FileMappingObject #%lu", |
| 1912 | GetLastError()); |
| 1913 | } |
| 1914 | CloseHandle(fi->fh); |
| 1915 | SERR("OpenFileMapping"); |
| 1916 | if (ctx->rc != GRN_SUCCESS && fi->path) { |
| 1917 | free(fi->path); |
| 1918 | fi->path = NULL; |
| 1919 | } |
| 1920 | return ctx->rc; |
| 1921 | } |
| 1922 | |
| 1923 | static inline void * |
| 1924 | grn_mmap_v0(grn_ctx *ctx, fileinfo *fi, int64_t offset, size_t length) |
| 1925 | { |
| 1926 | void *res; |
| 1927 | if (!fi) { |
| 1928 | return GRN_CALLOC(length); |
| 1929 | } |
| 1930 | /* file must be exceeded to GRN_IO_FILE_SIZE_V0 when FileMappingObject |
| 1931 | created. and, after fmo created, it's not allowed to expand the size of file. |
| 1932 | DWORD tail = (DWORD)(offset + length); |
| 1933 | DWORD filesize = GetFileSize(fi->fh, NULL); |
| 1934 | if (filesize < tail) { |
| 1935 | if (SetFilePointer(fi->fh, tail, NULL, FILE_BEGIN) != tail) { |
| 1936 | grn_log("SetFilePointer failed"); |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | if (!SetEndOfFile(fi->fh)) { |
| 1940 | grn_log("SetEndOfFile failed"); |
| 1941 | return NULL; |
| 1942 | } |
| 1943 | filesize = tail; |
| 1944 | } |
| 1945 | */ |
| 1946 | /* TODO: Add support for 32bit over offset by using dwFileOffsetHigh. */ |
| 1947 | res = |
| 1948 | MapViewOfFile(fi->fmo, FILE_MAP_WRITE, 0, (DWORD)offset, (SIZE_T)length); |
| 1949 | if (!res) { |
| 1950 | MERR("MapViewOfFile failed: <%" GRN_FMT_SIZE ">: %s", |
| 1951 | mmap_size, |
| 1952 | grn_error_get_current_system_message()); |
| 1953 | return NULL; |
| 1954 | } |
| 1955 | mmap_size += length; |
| 1956 | return res; |
| 1957 | } |
| 1958 | |
| 1959 | static inline int |
| 1960 | grn_munmap_v0(grn_ctx *ctx, fileinfo *fi, void *start, size_t length) |
| 1961 | { |
| 1962 | int r = 0; |
| 1963 | |
| 1964 | if (!fi) { |
| 1965 | GRN_FREE(start); |
| 1966 | return 0; |
| 1967 | } |
| 1968 | |
| 1969 | if (!FlushViewOfFile(start, length)) { |
| 1970 | SERR("FlushViewOfFile(<%p>, <%" GRN_FMT_SIZE ">) failed on unmap", |
| 1971 | start, |
| 1972 | length); |
| 1973 | r = -1; |
| 1974 | } |
| 1975 | if (UnmapViewOfFile(start)) { |
| 1976 | mmap_size -= length; |
| 1977 | return r; |
| 1978 | } else { |
| 1979 | SERR("UnmapViewOfFile(%p,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1980 | start, |
| 1981 | length, |
| 1982 | mmap_size); |
| 1983 | return -1; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | static inline bool |
| 1988 | grn_fileinfo_open_common(grn_ctx *ctx, |
| 1989 | fileinfo *fi, |
| 1990 | const char *path, |
| 1991 | int flags) |
| 1992 | { |
| 1993 | bool success = true; |
| 1994 | |
| 1995 | fi->path = grn_strdup_raw(path); |
| 1996 | |
| 1997 | /* may be wrong if flags is just only O_RDWR */ |
| 1998 | if ((flags & O_CREAT)) { |
| 1999 | bool exist = false; |
| 2000 | DWORD dwCreationDisposition; |
| 2001 | const char *flags_description; |
| 2002 | |
| 2003 | { |
| 2004 | struct stat stat_buffer; |
| 2005 | if (stat(path, &stat_buffer) == 0) { |
| 2006 | exist = true; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | if (flags & O_EXCL) { |
| 2011 | dwCreationDisposition = CREATE_NEW; |
| 2012 | flags_description = "O_RDWR|O_CREAT|O_EXCL"; |
| 2013 | } else { |
| 2014 | dwCreationDisposition = OPEN_ALWAYS; |
| 2015 | flags_description = "O_RDWR|O_CREAT"; |
| 2016 | } |
| 2017 | fi->fh = CreateFile(path, |
| 2018 | GRN_IO_FILE_CREATE_MODE, |
| 2019 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2020 | NULL, |
| 2021 | dwCreationDisposition, |
| 2022 | FILE_ATTRIBUTE_NORMAL, |
| 2023 | NULL); |
| 2024 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2025 | SERR("CreateFile(<%s>, <%s>) failed", path, flags_description); |
| 2026 | success = false; |
| 2027 | if (!exist) { |
| 2028 | struct stat stat_buffer; |
| 2029 | if (stat(path, &stat_buffer) == 0) { |
| 2030 | GRN_LOG(ctx, |
| 2031 | GRN_LOG_INFO, |
| 2032 | "[io][open] " |
| 2033 | "delete a newly created file because of open error: <%s>", |
| 2034 | path); |
| 2035 | DeleteFile(path); |
| 2036 | } |
| 2037 | } |
| 2038 | goto exit; |
| 2039 | } |
| 2040 | |
| 2041 | switch (dwCreationDisposition) { |
| 2042 | case CREATE_NEW: |
| 2043 | GRN_LOG(ctx, GRN_LOG_INFO, "[io][open] create new file: <%s>", path); |
| 2044 | break; |
| 2045 | case OPEN_ALWAYS: |
| 2046 | if (GetLastError() == ERROR_ALREADY_EXISTS) { |
| 2047 | GRN_LOG(ctx, |
| 2048 | GRN_LOG_INFO, |
| 2049 | "[io][open] open existing file because it exists: <%s>", |
| 2050 | path); |
| 2051 | } else { |
| 2052 | GRN_LOG(ctx, |
| 2053 | GRN_LOG_INFO, |
| 2054 | "[io][open] create new file because it doesn't exist: <%s>", |
| 2055 | path); |
| 2056 | } |
| 2057 | break; |
| 2058 | default: |
| 2059 | break; |
| 2060 | } |
| 2061 | |
| 2062 | { |
| 2063 | FILE_SET_SPARSE_BUFFER buffer; |
| 2064 | buffer.SetSparse = TRUE; |
| 2065 | DWORD returned_bytes; |
| 2066 | if (!DeviceIoControl(fi->fh, |
| 2067 | FSCTL_SET_SPARSE, |
| 2068 | &buffer, |
| 2069 | sizeof(FILE_SET_SPARSE_BUFFER), |
| 2070 | NULL, |
| 2071 | 0, |
| 2072 | &returned_bytes, |
| 2073 | NULL)) { |
| 2074 | GRN_LOG(ctx, |
| 2075 | GRN_LOG_INFO, |
| 2076 | "Tried to make file sparse but failed: " |
| 2077 | "DeviceIoControl(FSCTL_SET_SPARSE): " |
| 2078 | "<%s>: <%s>", |
| 2079 | path, |
| 2080 | grn_error_get_current_system_message()); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | goto exit; |
| 2085 | } |
| 2086 | |
| 2087 | if ((flags & O_TRUNC)) { |
| 2088 | CloseHandle(fi->fh); |
| 2089 | /* unable to assign OPEN_ALWAYS and TRUNCATE_EXISTING at once */ |
| 2090 | fi->fh = CreateFile(path, |
| 2091 | GRN_IO_FILE_CREATE_MODE, |
| 2092 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2093 | NULL, |
| 2094 | TRUNCATE_EXISTING, |
| 2095 | FILE_ATTRIBUTE_NORMAL, |
| 2096 | NULL); |
| 2097 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2098 | SERR("CreateFile(<%s>, <O_RDWR|O_TRUNC>) failed", path); |
| 2099 | success = false; |
| 2100 | goto exit; |
| 2101 | } |
| 2102 | GRN_LOG(ctx, GRN_LOG_INFO, "[io][open] truncated: <%s>", path); |
| 2103 | goto exit; |
| 2104 | } |
| 2105 | /* O_RDWR only */ |
| 2106 | fi->fh = CreateFile(path, |
| 2107 | GRN_IO_FILE_CREATE_MODE, |
| 2108 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2109 | NULL, |
| 2110 | OPEN_EXISTING, |
| 2111 | FILE_ATTRIBUTE_NORMAL, |
| 2112 | NULL); |
| 2113 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2114 | SERR("CreateFile(<%s>, <O_RDWR>) failed", path); |
| 2115 | success = false; |
| 2116 | goto exit; |
| 2117 | } |
| 2118 | GRN_LOG(ctx, GRN_LOG_INFO, "[io][open] open existing file: <%s>", path); |
| 2119 | |
| 2120 | exit: |
| 2121 | if (!success && fi->path) { |
| 2122 | free(fi->path); |
| 2123 | fi->path = NULL; |
| 2124 | } |
| 2125 | return success; |
| 2126 | } |
| 2127 | |
| 2128 | static inline grn_rc |
| 2129 | grn_fileinfo_open(grn_ctx *ctx, fileinfo *fi, const char *path, int flags) |
| 2130 | { |
| 2131 | struct _grn_io_header io_header; |
| 2132 | LARGE_INTEGER file_size; |
| 2133 | int version = grn_io_version_default; |
| 2134 | |
| 2135 | if (!grn_fileinfo_open_common(ctx, fi, path, flags)) { |
| 2136 | if (fi->fh != INVALID_HANDLE_VALUE) { |
| 2137 | CloseHandle(fi->fh); |
| 2138 | fi->fh = INVALID_HANDLE_VALUE; |
| 2139 | } |
| 2140 | return ctx->rc; |
| 2141 | } |
| 2142 | |
| 2143 | if (GetFileSizeEx(fi->fh, &file_size) && file_size.QuadPart > 0) { |
| 2144 | DWORD header_size; |
| 2145 | DWORD read_bytes; |
| 2146 | header_size = sizeof(struct _grn_io_header); |
| 2147 | ReadFile(fi->fh, &io_header, header_size, &read_bytes, NULL); |
| 2148 | if (read_bytes == header_size) { |
| 2149 | version = io_header.version; |
| 2150 | } |
| 2151 | SetFilePointer(fi->fh, 0, NULL, FILE_BEGIN); |
| 2152 | } |
| 2153 | |
| 2154 | if (version == 0) { |
| 2155 | return grn_fileinfo_open_v0(ctx, fi, path, flags); |
| 2156 | } else { |
| 2157 | return grn_fileinfo_open_v1(ctx, fi, path, flags); |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | static inline int |
| 2162 | grn_guess_io_version(grn_ctx *ctx, grn_io *io, fileinfo *fi) |
| 2163 | { |
| 2164 | if (io) { |
| 2165 | return io->header->version; |
| 2166 | } |
| 2167 | |
| 2168 | if (fi) { |
| 2169 | if (fi->fmo) { |
| 2170 | return 0; |
| 2171 | } else { |
| 2172 | return 1; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | return grn_io_version_default; |
| 2177 | } |
| 2178 | |
| 2179 | static inline void * |
| 2180 | grn_mmap(grn_ctx *ctx, |
| 2181 | grn_io *io, |
| 2182 | HANDLE *fmo, |
| 2183 | fileinfo *fi, |
| 2184 | int64_t offset, |
| 2185 | size_t length, |
| 2186 | const char *file, |
| 2187 | int line, |
| 2188 | const char *func) |
| 2189 | { |
| 2190 | if (grn_fail_malloc_should_fail(length, file, line, func)) { |
| 2191 | MERR("[alloc][fail][mmap] <%u>: <%" GRN_FMT_SIZE ">: <%s>: " |
| 2192 | "<%p:%" GRN_FMT_INT64D ":%" GRN_FMT_SIZE ">: " |
| 2193 | "%s:%d: %s", |
| 2194 | grn_alloc_count(), |
| 2195 | mmap_size, |
| 2196 | io ? (io->path[0] ? io->path : "(memory)") : "(null)", |
| 2197 | fi ? fi->fh : NULL, |
| 2198 | offset, |
| 2199 | length, |
| 2200 | file, |
| 2201 | line, |
| 2202 | func); |
| 2203 | return NULL; |
| 2204 | } else { |
| 2205 | int version; |
| 2206 | |
| 2207 | version = grn_guess_io_version(ctx, io, fi); |
| 2208 | |
| 2209 | if (version == 0) { |
| 2210 | return grn_mmap_v0(ctx, fi, offset, length); |
| 2211 | } else { |
| 2212 | return grn_mmap_v1(ctx, fmo, fi, offset, length); |
| 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | static inline int |
| 2218 | grn_munmap(grn_ctx *ctx, |
| 2219 | grn_io *io, |
| 2220 | HANDLE *fmo, |
| 2221 | fileinfo *fi, |
| 2222 | void *start, |
| 2223 | size_t length) |
| 2224 | { |
| 2225 | int version; |
| 2226 | |
| 2227 | version = grn_guess_io_version(ctx, io, fi); |
| 2228 | |
| 2229 | if (version == 0) { |
| 2230 | return grn_munmap_v0(ctx, fi, start, length); |
| 2231 | } else { |
| 2232 | return grn_munmap_v1(ctx, fmo, fi, start, length); |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | static inline grn_rc |
| 2237 | grn_fileinfo_close(grn_ctx *ctx, fileinfo *fi) |
| 2238 | { |
| 2239 | if (fi->fmo != NULL) { |
| 2240 | CloseHandle(fi->fmo); |
| 2241 | fi->fmo = NULL; |
| 2242 | } |
| 2243 | if (fi->fh != INVALID_HANDLE_VALUE) { |
| 2244 | if (grn_logger_pass(ctx, GRN_LOG_DEBUG)) { |
| 2245 | GRN_LOG(ctx, GRN_LOG_DEBUG, "[io][close] <%s>", fi->path); |
| 2246 | } |
| 2247 | CloseHandle(fi->fh); |
| 2248 | CRITICAL_SECTION_FIN(fi->cs); |
| 2249 | fi->fh = INVALID_HANDLE_VALUE; |
| 2250 | } |
| 2251 | if (fi->path) { |
| 2252 | free(fi->path); |
| 2253 | fi->path = NULL; |
| 2254 | } |
| 2255 | return GRN_SUCCESS; |
| 2256 | } |
| 2257 | |
| 2258 | static inline void |
| 2259 | grn_fileinfo_init(fileinfo *fis, uint32_t nfis) |
| 2260 | { |
| 2261 | for (; nfis--; fis++) { |
| 2262 | fis->path = NULL; |
| 2263 | fis->fh = INVALID_HANDLE_VALUE; |
| 2264 | fis->fmo = NULL; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | static inline int |
| 2269 | grn_fileinfo_opened(fileinfo *fi) |
| 2270 | { |
| 2271 | return fi->fh != INVALID_HANDLE_VALUE; |
| 2272 | } |
| 2273 | |
| 2274 | static inline int |
| 2275 | grn_msync(grn_ctx *ctx, fileinfo *fi, void *start, size_t length) |
| 2276 | { |
| 2277 | BOOL succeeded; |
| 2278 | SYSTEMTIME system_time; |
| 2279 | FILETIME file_time; |
| 2280 | |
| 2281 | succeeded = FlushViewOfFile(start, length); |
| 2282 | if (!succeeded) { |
| 2283 | SERR("FlushViewOfFile(<%p>, <%" GRN_FMT_SIZE ">) failed on sync", |
| 2284 | start, |
| 2285 | length); |
| 2286 | return -1; |
| 2287 | } |
| 2288 | |
| 2289 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2290 | return 0; |
| 2291 | } |
| 2292 | |
| 2293 | GetSystemTime(&system_time); |
| 2294 | succeeded = SystemTimeToFileTime(&system_time, &file_time); |
| 2295 | if (!succeeded) { |
| 2296 | SERR("SystemTimeToFileTime(<%04u-%02u-%02uT%02u:%02u:%02u.%03u>) failed", |
| 2297 | system_time.wYear, |
| 2298 | system_time.wMonth, |
| 2299 | system_time.wDay, |
| 2300 | system_time.wHour, |
| 2301 | system_time.wMinute, |
| 2302 | system_time.wSecond, |
| 2303 | system_time.wMilliseconds); |
| 2304 | return -1; |
| 2305 | } |
| 2306 | |
| 2307 | succeeded = SetFileTime(fi->fh, NULL, NULL, &file_time); |
| 2308 | if (!succeeded) { |
| 2309 | SERR("SetFileTime(<%p>, <%p>, <%" GRN_FMT_SIZE ">) failed", |
| 2310 | fi->fh, |
| 2311 | start, |
| 2312 | length); |
| 2313 | return -1; |
| 2314 | } |
| 2315 | |
| 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | static inline grn_rc |
| 2320 | grn_pread(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset) |
| 2321 | { |
| 2322 | DWORD r, len; |
| 2323 | CRITICAL_SECTION_ENTER(fi->cs); |
| 2324 | r = SetFilePointer(fi->fh, offset, NULL, FILE_BEGIN); |
| 2325 | if (r == INVALID_SET_FILE_POINTER) { |
| 2326 | SERR("SetFilePointer"); |
| 2327 | } else { |
| 2328 | if (!ReadFile(fi->fh, buf, (DWORD)count, &len, NULL)) { |
| 2329 | SERR("ReadFile"); |
| 2330 | } else if (len != count) { |
| 2331 | /* todo : should retry ? */ |
| 2332 | ERR(GRN_INPUT_OUTPUT_ERROR, |
| 2333 | "ReadFile %" GRN_FMT_SIZE " != %lu", |
| 2334 | count, |
| 2335 | len); |
| 2336 | } |
| 2337 | } |
| 2338 | CRITICAL_SECTION_LEAVE(fi->cs); |
| 2339 | return ctx->rc; |
| 2340 | } |
| 2341 | |
| 2342 | static inline grn_rc |
| 2343 | grn_pwrite(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset) |
| 2344 | { |
| 2345 | DWORD r, len; |
| 2346 | CRITICAL_SECTION_ENTER(fi->cs); |
| 2347 | r = SetFilePointer(fi->fh, offset, NULL, FILE_BEGIN); |
| 2348 | if (r == INVALID_SET_FILE_POINTER) { |
| 2349 | SERR("SetFilePointer"); |
| 2350 | } else { |
| 2351 | if (!WriteFile(fi->fh, buf, (DWORD)count, &len, NULL)) { |
| 2352 | SERR("WriteFile"); |
| 2353 | } else if (len != count) { |
| 2354 | /* todo : should retry ? */ |
| 2355 | ERR(GRN_INPUT_OUTPUT_ERROR, |
| 2356 | "WriteFile %" GRN_FMT_SIZE " != %lu", |
| 2357 | count, |
| 2358 | len); |
| 2359 | } |
| 2360 | } |
| 2361 | CRITICAL_SECTION_LEAVE(fi->cs); |
| 2362 | return ctx->rc; |
| 2363 | } |
| 2364 | |
| 2365 | #else /* WIN32 */ |
| 2366 | |
| 2367 | inline static grn_rccxx . . $ grn_rc# |
| 2368 | grn_fileinfo_opencxx . . $ grn_fileinfo_open(ac8f5bf2db69e073).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, const char *path, int flags) |
| 2369 | { |
| 2370 | struct statcxx . . $ stat# st; |
| 2371 | fi->pathcxx . . $ _grn_io_fileinfo#path. = grn_strdup_rawcxx . . $ `include/groonga/portability.h:82:11`!(path); |
| 2372 | grn_opencxx . . $ `include/groonga/portability.h:172:11`!(fi->fd, path, flags); |
| 2373 | if (fi->fdcxx . . $ _grn_io_fileinfo#fd. == -1) { |
| 2374 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("failed to open file info path: <%s>", path); |
| 2375 | freecxx . . $ free(5d22bdacc48458e8).(fi->pathcxx . . $ _grn_io_fileinfo#path.); |
| 2376 | fi->pathcxx . . $ _grn_io_fileinfo#path. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 2377 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2378 | } |
| 2379 | if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fi->fdcxx . . $ _grn_io_fileinfo#fd., &st) == -1) { |
| 2380 | ERRNO_ERRcxx . . $ `lib/grn_error.h:569:11`!("failed to stat file info path: <%s>", path); |
| 2381 | grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fi->fd); |
| 2382 | freecxx . . $ free(5d22bdacc48458e8).(fi->pathcxx . . $ _grn_io_fileinfo#path.); |
| 2383 | fi->pathcxx . . $ _grn_io_fileinfo#path. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 2384 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2385 | } |
| 2386 | fi->devcxx . . $ _grn_io_fileinfo#dev. = st.st_devcxx . . $ stat#st_dev.; |
| 2387 | fi->inodecxx . . $ _grn_io_fileinfo#inode. = st.st_inocxx . . $ stat#st_ino.; |
| 2388 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_DUMP, "[io][open] <%s>", path); |
| 2389 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 2390 | } |
| 2391 | |
| 2392 | static inline void |
| 2393 | grn_fileinfo_initcxx . . $ grn_fileinfo_init(4f59c63a7bfebd5).(fileinfocxx . . $ fileinfo# *fis, uint32_tcxx . . $ uint32_t# nfis) |
| 2394 | { |
| 2395 | for (; nfis--; fis++) { |
| 2396 | fis->pathcxx . . $ _grn_io_fileinfo#path. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 2397 | fis->fdcxx . . $ _grn_io_fileinfo#fd. = -1; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | static inline int |
| 2402 | grn_fileinfo_openedcxx . . $ grn_fileinfo_opened(600504bf85ebe923).(fileinfocxx . . $ fileinfo# *fi) |
| 2403 | { |
| 2404 | return fi->fdcxx . . $ _grn_io_fileinfo#fd. != -1; |
| 2405 | } |
| 2406 | |
| 2407 | static inline grn_rccxx . . $ grn_rc# |
| 2408 | grn_fileinfo_closecxx . . $ grn_fileinfo_close(91b895c7c0cdf7c3).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi) |
| 2409 | { |
| 2410 | if (fi->fdcxx . . $ _grn_io_fileinfo#fd. != -1) { |
| 2411 | if (grn_closecxx . . $ `include/groonga/portability.h:179:11`!(fi->fd) == -1) { |
| 2412 | SERRcxx . . $ `lib/grn_error.h:411:11`!("close"); |
| 2413 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2414 | } |
| 2415 | fi->fdcxx . . $ _grn_io_fileinfo#fd. = -1; |
| 2416 | if (fi->pathcxx . . $ _grn_io_fileinfo#path.) { |
| 2417 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_DUMP, "[io][close] <%s>", fi->path); |
| 2418 | } |
| 2419 | } |
| 2420 | if (fi->pathcxx . . $ _grn_io_fileinfo#path.) { |
| 2421 | freecxx . . $ free(5d22bdacc48458e8).(fi->pathcxx . . $ _grn_io_fileinfo#path.); |
| 2422 | fi->pathcxx . . $ _grn_io_fileinfo#path. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 2423 | } |
| 2424 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 2425 | } |
| 2426 | |
| 2427 | # if defined(MAP_ANONcxx . . $ `<external>/17226767460805440860/mman-linux.h:57:9`!) && !defined(MAP_ANONYMOUScxx . . $ `<external>/17226767460805440860/mman-linux.h:55:10`!) |
| 2428 | # define MAP_ANONYMOUS MAP_ANON |
| 2429 | # endif |
| 2430 | |
| 2431 | # include <sys/mman.h> |
| 2432 | |
| 2433 | inline static void * |
| 2434 | grn_mmapcxx . . $ grn_mmap(e90cb56b1a570dbb).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 2435 | grn_iocxx . . $ grn_io# *io, |
| 2436 | fileinfocxx . . $ fileinfo# *fi, |
| 2437 | int64_tcxx . . $ int64_t# offset, |
| 2438 | size_t length, |
| 2439 | const char *file, |
| 2440 | int line, |
| 2441 | const char *func) |
| 2442 | { |
| 2443 | if (grn_fail_malloc_should_failcxx . . $ grn_fail_malloc_should_fail(8ab3715db869f8d3).(length, file, line, func)) { |
| 2444 | MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][fail][mmap] <%u>: <%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ">: <%s>: " |
| 2445 | "<%d:%" GRN_FMT_INT64Dcxx . . $ `lib/grn.h:369:11`! ":%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ">: " |
| 2446 | "%s:%d: %s", |
| 2447 | grn_alloc_count(), |
| 2448 | mmap_size, |
| 2449 | io ? (io->path[0] ? io->path : "(memory)") : "(null)", |
| 2450 | fi ? fi->fd : 0, |
| 2451 | offset, |
| 2452 | length, |
| 2453 | file, |
| 2454 | line, |
| 2455 | func); |
| 2456 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 2457 | } else { |
| 2458 | void *res; |
| 2459 | int fd, flags; |
| 2460 | if (fi) { |
| 2461 | struct statcxx . . $ stat# s; |
| 2462 | int64_tcxx . . $ int64_t# tail = offset + (int64_tcxx . . $ int64_t#)length; |
| 2463 | fd = fi->fdcxx . . $ _grn_io_fileinfo#fd.; |
| 2464 | if ((fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &s) == -1) || |
| 2465 | (s.st_sizecxx . . $ stat#st_size. < tail && ftruncatecxx . . $ ftruncate(bba9d6e9fb34fad7).(fd, tail) == -1)) { |
| 2466 | SERRcxx . . $ `lib/grn_error.h:411:11`!("fstat"); |
| 2467 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 2468 | } |
| 2469 | flags = MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!; |
| 2470 | } else { |
| 2471 | fd = -1; |
| 2472 | flags = MAP_PRIVATEcxx . . $ `<external>/17226767460805440860/mman-linux.h:43:9`! | MAP_ANONYMOUScxx . . $ `<external>/17226767460805440860/mman-linux.h:55:10`!; |
| 2473 | # ifdef MAP_ALIGNED_SUPER |
| 2474 | flags |= MAP_ALIGNED_SUPER; |
| 2475 | # endif |
| 2476 | } |
| 2477 | res = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, length, PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`! | PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!, flags, fd, offset); |
| 2478 | if (MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`! == res) { |
| 2479 | MERRcxx . . $ `lib/grn_error.h:158:9`!("mmap(%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ",%d,%" GRN_FMT_INT64Dcxx . . $ `lib/grn.h:369:11`! ")=%s <%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! |
| 2480 | ">", |
| 2481 | length, |
| 2482 | fd, |
| 2483 | offset, |
| 2484 | strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!), |
| 2485 | mmap_size); |
| 2486 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 2487 | } |
| 2488 | mmap_sizecxx . . $ mmap_size. += length; |
| 2489 | return res; |
| 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | static inline int |
| 2494 | grn_msynccxx . . $ grn_msync(16cbec3d163fddf4).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, void *start, size_t length) |
| 2495 | { |
| 2496 | int r = msynccxx . . $ msync(e72f86de37bb8b46).(start, length, MS_SYNCcxx . . $ `<external>/17226767460805440860/mman-linux.h:83:9`!); |
| 2497 | if (r == -1) { |
| 2498 | SERRcxx . . $ `lib/grn_error.h:411:11`!("msync"); |
| 2499 | return r; |
| 2500 | } |
| 2501 | |
| 2502 | if (fi->fdcxx . . $ _grn_io_fileinfo#fd. > 0) { |
| 2503 | struct timevalcxx . . $ timeval# time_values[2]; |
| 2504 | struct timespeccxx . . $ timespec# time_specs[2]; |
| 2505 | gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&(time_values[0]), NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!); |
| 2506 | time_values[1] = time_values[0]; |
| 2507 | time_specs[0].tv_seccxx . . $ timespec#tv_sec. = time_values[0].tv_seccxx . . $ timeval#tv_sec.; |
| 2508 | time_specs[0].tv_nseccxx . . $ timespec#tv_nsec. = time_values[0].tv_useccxx . . $ timeval#tv_usec. * 1000; |
| 2509 | time_specs[1] = time_specs[0]; |
| 2510 | r = grn_futimenscxx . . $ `lib/io.c:55:13`!(fi->fd, time_values, time_specs); |
| 2511 | if (r == -1) { |
| 2512 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed: <%d>", grn_futimens_namecxx . . $ `lib/io.c:57:13`!, fi->fd); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | return r; |
| 2517 | } |
| 2518 | |
| 2519 | static inline int |
| 2520 | grn_munmapcxx . . $ grn_munmap(5f599ae7e068e8bf).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, fileinfocxx . . $ fileinfo# *fi, void *start, size_t length) |
| 2521 | { |
| 2522 | int res; |
| 2523 | res = munmapcxx . . $ munmap(c0a5f96701d3f89).(start, length); |
| 2524 | if (res == 0) { |
| 2525 | mmap_sizecxx . . $ mmap_size. -= length; |
| 2526 | } else { |
| 2527 | SERRcxx . . $ `lib/grn_error.h:411:11`!("munmap(%p,%" GRN_FMT_LLUcxx . . $ `lib/grn.h:407:11`! ") failed <%" GRN_FMT_LLUcxx . . $ `lib/grn.h:407:11`! ">", |
| 2528 | start, |
| 2529 | (unsigned long long int)length, |
| 2530 | (unsigned long long int)mmap_size); |
| 2531 | } |
| 2532 | return res; |
| 2533 | } |
| 2534 | |
| 2535 | static inline grn_rccxx . . $ grn_rc# |
| 2536 | grn_preadcxx . . $ grn_pread(4577714838bf4b55).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, void *buf, size_t count, off_tcxx . . $ off_t# offset) |
| 2537 | { |
| 2538 | ssize_tcxx . . $ ssize_t# r = preadcxx . . $ pread(b119fdc0c07358b4).(fi->fdcxx . . $ _grn_io_fileinfo#fd., buf, count, offset); |
| 2539 | if (r == -1) { |
| 2540 | SERRcxx . . $ `lib/grn_error.h:411:11`!("pread"); |
| 2541 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2542 | } |
| 2543 | if ((size_t)r != count) { |
| 2544 | /* todo : should retry ? */ |
| 2545 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INPUT_OUTPUT_ERROR, |
| 2546 | "pread returned %" GRN_FMT_SSIZEcxx . . $ `lib/grn.h:409:11`! " != %" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`!, |
| 2547 | r, |
| 2548 | count); |
| 2549 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2550 | } |
| 2551 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 2552 | } |
| 2553 | |
| 2554 | static inline grn_rccxx . . $ grn_rc# |
| 2555 | grn_pwritecxx . . $ grn_pwrite(4577714838bf4b55).(grn_ctxcxx . . $ grn_ctx# *ctx, fileinfocxx . . $ fileinfo# *fi, void *buf, size_t count, off_tcxx . . $ off_t# offset) |
| 2556 | { |
| 2557 | ssize_tcxx . . $ ssize_t# r = pwritecxx . . $ pwrite(6a64eb0a75db039).(fi->fdcxx . . $ _grn_io_fileinfo#fd., buf, count, offset); |
| 2558 | if (r == -1) { |
| 2559 | SERRcxx . . $ `lib/grn_error.h:411:11`!("pwrite"); |
| 2560 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2561 | } |
| 2562 | if ((size_t)r != count) { |
| 2563 | /* todo : should retry ? */ |
| 2564 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INPUT_OUTPUT_ERROR, |
| 2565 | "pwrite returned %" GRN_FMT_SSIZEcxx . . $ `lib/grn.h:409:11`! " != %" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`!, |
| 2566 | r, |
| 2567 | count); |
| 2568 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2569 | } |
| 2570 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 2571 | } |
| 2572 | |
| 2573 | #endif /* WIN32 */ |
| 2574 | |
| 2575 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 2576 | grn_io_warm_pathcxx . . $ grn_io_warm_path(c90feafdc377c47a).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io, const char *path) |
| 2577 | { |
| 2578 | FILEcxx . . $ FILE# *input = grn_fopencxx . . $ `include/groonga/portability.h:76:11`!(path, "rb"); |
| 2579 | if (!input) { |
| 2580 | SERRcxx . . $ `lib/grn_error.h:411:11`!("[io][warm] failed to open a file: <%s>", path); |
| 2581 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 2582 | } |
| 2583 | |
| 2584 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_DUMP, "[io][warm] <%s>", path); |
| 2585 | char buffer[4096]; |
| 2586 | while (freadcxx . . $ fread(738fc8ac1d711e58).(buffer, 1, sizeof(buffer), input) != 0) { |
| 2587 | /* Do nothing */ |
| 2588 | } |
| 2589 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(input); |
| 2590 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 2591 | } |
| 2592 | |
| 2593 | grn_rccxx . . $ grn_rc# |
| 2594 | grn_io_warmcxx . . $ grn_io_warm(117c07c672382ce3).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_iocxx . . $ grn_io# *io) |
| 2595 | { |
| 2596 | if (io->pathcxx . . $ _grn_io#path.[0] == '\0') { |
| 2597 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 2598 | } |
| 2599 | |
| 2600 | char buffer[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!]; |
| 2601 | uint32_tcxx . . $ uint32_t# i; |
| 2602 | uint32_tcxx . . $ uint32_t# n_files = grn_io_n_filescxx . . $ grn_io_n_files(7752bae3ff5e951e).(ctx, io); |
| 2603 | for (i = 0; i < n_files; i++) { |
| 2604 | gen_pathnamecxx . . $ gen_pathname(c194a158e5e34a1c).(io->pathcxx . . $ _grn_io#path., buffer, i); |
| 2605 | if (!grn_io_warm_pathcxx . . $ grn_io_warm_path(c90feafdc377c47a).(ctx, io, buffer)) { |
| 2606 | break; |
| 2607 | } |
| 2608 | } |
| 2609 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 2610 | } |