Tree @fresh-snapshots/main (Download .tar.gz)
- ..
- dat
- mrb
- proc
- accessor.c
- aggregators.c
- alloc.c
- arrow.cpp
- bulk.cpp
- c_sources.am
- cache.c
- cast.cpp
- CMakeLists.txt
- column.c
- com.c
- command.c
- config.c
- cpp_sources.am
- ctx.c
- ctx_impl_lua.c
- ctx_impl_mrb.c
- dat.cpp
- db.c
- dump.c
- encoding.c
- error.c
- expr.c
- expr_code.c
- expr_executor.c
- file_lock.c
- file_reader.c
- float.c
- geo.c
- grn.h
- grn_accessor.h
- grn_aggregators.h
- grn_alloc.h
- grn_arrow.h
- grn_arrow.hpp
- grn_bulk.h
- grn_cache.h
- grn_cast.h
- grn_column.h
- grn_com.h
- grn_config.h
- grn_ctx.h
- grn_ctx_impl.h
- grn_ctx_impl_lua.h
- grn_ctx_impl_mrb.h
- grn_dat.h
- grn_db.h
- grn_ecmascript.c
- grn_ecmascript.h
- grn_ecmascript.lemon
- grn_encoding.h
- grn_error.h
- grn_expr.h
- grn_expr_code.h
- grn_expr_executor.h
- grn_file_lock.h
- grn_float.h
- grn_geo.h
- grn_group.h
- grn_hash.h
- grn_ii.h
- grn_ii_select_cursor.h
- grn_index_column.h
- grn_index_cursor.h
- grn_io.h
- grn_load.h
- grn_logger.h
- grn_mrb.h
- grn_msgpack.h
- grn_nfkc.h
- grn_normalizer.h
- grn_obj.h
- grn_onigmo.h
- grn_options.h
- grn_output.h
- grn_output_columns.h
- grn_pat.h
- grn_plugin.h
- grn_posting.h
- grn_proc.h
- grn_report.h
- grn_request_canceler.h
- grn_request_timer.h
- grn_romaji.h
- grn_rset.h
- grn_scanner.h
- grn_scorer.h
- grn_scorers.h
- grn_selector.h
- grn_snip.h
- grn_store.h
- grn_str.h
- grn_string.h
- grn_table.h
- grn_table_module.h
- grn_time.h
- grn_token.h
- grn_token_column.h
- grn_token_cursor.h
- grn_token_filters.h
- grn_token_metadata.h
- grn_tokenizer.h
- grn_tokenizers.h
- grn_type.h
- grn_util.h
- grn_vector.h
- grn_window_function.h
- grn_window_function_executor.h
- grn_window_functions.h
- grn_windows.h
- group.c
- hash.c
- highlighter.c
- id.c
- ii.c
- index_column.c
- index_cursor.c
- io.c
- load.c
- logger.c
- Makefile.am
- Makefile.in
- memory.c
- metadata.rc
- metadata.rc.in
- mrb.c
- msgpack.c
- nfkc.c
- nfkc100.c
- nfkc121.c
- nfkc130.c
- nfkc50.c
- nfkc_dump.c
- normalizer.c
- obj.c
- onigmo.c
- operator.c
- options.c
- output.c
- output_columns.c
- pat.c
- plugin.c
- posting.c
- proc.c
- raw_string.c
- report.c
- request_canceler.c
- request_timer.c
- romaji.c
- rset.c
- scanner.c
- scorer.c
- scorers.c
- selector.c
- snip.c
- sort.c
- store.c
- str.c
- string.c
- table.c
- table_module.c
- thread.c
- time.c
- token.c
- token_column.cpp
- token_cursor.c
- token_filter.c
- token_filters.c
- token_metadata.c
- tokenizer.c
- tokenizers.c
- type.c
- util.c
- vector.cpp
- window_function.c
- window_function_executor.cpp
- window_functions.c
- windows.c
- windows_event_logger.c
file_lock.c @fresh-snapshots/main — raw · history · blame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | /* -*- c-basic-offset: 2 -*- */ /* Copyright(C) 2017-2018 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "grn_file_lock.h" #include "grn_ctx.h" #include <sys/stat.h> #ifdef WIN32 # include <io.h> # include <share.h> #else /* WIN32 */ # include <sys/types.h> # include <fcntl.h> #endif /* WIN32 */ #ifdef WIN32 # define GRN_FILE_LOCK_IS_INVALID(file_lock) \ ((file_lock)->handle == INVALID_HANDLE_VALUE) #else /* WIN32 */ # define GRN_FILE_LOCK_IS_INVALID(file_lock) \ ((file_lock)->fd == -1) #endif /* WIN32 */ void grn_file_lock_init(grn_ctx *ctx, grn_file_lock *file_lock, const char *path) { grn_strcpy(file_lock->path, PATH_MAX, path); #ifdef WIN32 file_lock->handle = INVALID_HANDLE_VALUE; #else /* WIN32 */ file_lock->fd = -1; #endif /* WIN32 */ } grn_bool grn_file_lock_acquire(grn_ctx *ctx, grn_file_lock *file_lock, int timeout, const char *error_message_tag) { int i; int n_lock_tries = timeout; for (i = 0; i < n_lock_tries; i++) { #ifdef WIN32 file_lock->handle = CreateFile(file_lock->path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); #else /* WIN32 */ file_lock->fd = open(file_lock->path, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); #endif if (!GRN_FILE_LOCK_IS_INVALID(file_lock)) { break; } grn_nanosleep(GRN_LOCK_WAIT_TIME_NANOSECOND); } if (GRN_FILE_LOCK_IS_INVALID(file_lock)) { ERR(GRN_NO_LOCKS_AVAILABLE, "%s failed to acquire lock: <%s>", error_message_tag, file_lock->path); return GRN_FALSE; } else { return GRN_TRUE; } } void grn_file_lock_release(grn_ctx *ctx, grn_file_lock *file_lock) { if (GRN_FILE_LOCK_IS_INVALID(file_lock)) { return; } #ifdef WIN32 CloseHandle(file_lock->handle); DeleteFile(file_lock->path); file_lock->handle = INVALID_HANDLE_VALUE; #else /* WIN32 */ close(file_lock->fd); unlink(file_lock->path); file_lock->fd = -1; #endif /* WIN32 */ grn_strcpy(file_lock->path, PATH_MAX, ""); } void grn_file_lock_fin(grn_ctx *ctx, grn_file_lock *file_lock) { if (!GRN_FILE_LOCK_IS_INVALID(file_lock)) { grn_file_lock_release(ctx, file_lock); } } |