Debian Codegraph @

lib/file_lock.c

raw symbols SCIP
1 /*
2 Copyright(C) 2017-2018 Brazil
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_file_lock.h"cxx . . $ `<file>/lib/grn_file_lock.h`/
20 #include "grn_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/
21
22 #include <sys/stat.h>
23
24 #ifdef WIN32
25 # include <io.h>
26 # include <share.h>
27 #else /* WIN32 */
28 # include <sys/types.h>
29 # include <fcntl.h>
30 #endif /* WIN32 */
31
32 #ifdef WIN32
33 # define GRN_FILE_LOCK_IS_INVALID(file_lock) \
34 ((file_lock)->handle == INVALID_HANDLE_VALUE)
35 #else /* WIN32 */
36 # define GRN_FILE_LOCK_IS_INVALIDcxx . . $ `lib/file_lock.c:36:10`!(file_lock) \
37 ((file_lock)->fd == -1)
38 #endif /* WIN32 */
39
40 void
41 grn_file_lock_initcxx . . $ grn_file_lock_init(b6f038a97f7c4fb7).(grn_ctxcxx . . $ grn_ctx# *ctx,
42 grn_file_lockcxx . . $ grn_file_lock# *file_lock,
43 const char *path)
44 {
45 grn_strcpycxx . . $ `include/groonga/portability.h:101:11`!(file_lock->path, PATH_MAX, path);
46 #ifdef WIN32
47 file_lock->handle = INVALID_HANDLE_VALUE;
48 #else /* WIN32 */
49 file_lock->fdcxx . . $ $anonymous_type_89a07738fa906650_0#fd. = -1;
50 #endif /* WIN32 */
51 }
52
53 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
54 grn_file_lock_acquirecxx . . $ grn_file_lock_acquire(ab0e5af768e5a780).(grn_ctxcxx . . $ grn_ctx# *ctx,
55 grn_file_lockcxx . . $ grn_file_lock# *file_lock,
56 int timeout,
57 const char *error_message_tag)
58 {
59 int i;
60 int n_lock_tries = timeout;
61
62 for (i = 0; i < n_lock_tries; i++) {
63 #ifdef WIN32
64 file_lock->handle = CreateFile(file_lock->path,
65 GENERIC_READ | GENERIC_WRITE,
66 0,
67 NULL,
68 CREATE_ALWAYS,
69 FILE_ATTRIBUTE_NORMAL,
70 NULL);
71 #else /* WIN32 */
72 file_lock->fdcxx . . $ $anonymous_type_89a07738fa906650_0#fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(file_lock->pathcxx . . $ $anonymous_type_89a07738fa906650_0#path., O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`! | O_EXCLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:50:10`!, S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`! | S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`!);
73 #endif
74 if (!GRN_FILE_LOCK_IS_INVALIDcxx . . $ `lib/file_lock.c:36:10`!(file_lock)) {
75 break;
76 }
77 grn_nanosleepcxx . . $ grn_nanosleep(2c44cd906ae4e829).(GRN_LOCK_WAIT_TIME_NANOSECONDcxx . . $ `build/config.h:32:9`!);
78 }
79
80 if (GRN_FILE_LOCK_IS_INVALIDcxx . . $ `lib/file_lock.c:36:10`!(file_lock)) {
81 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_LOCKS_AVAILABLE,
82 "%s failed to acquire lock: <%s>",
83 error_message_tag, file_lock->path);
84 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
85 }
86 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
87 }
88
89 void
90 grn_file_lock_releasecxx . . $ grn_file_lock_release(7fc6e84b96ea19e9).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_file_lockcxx . . $ grn_file_lock# *file_lock)
91 {
92 if (GRN_FILE_LOCK_IS_INVALIDcxx . . $ `lib/file_lock.c:36:10`!(file_lock)) {
93 return;
94 }
95
96 #ifdef WIN32
97 CloseHandle(file_lock->handle);
98 DeleteFile(file_lock->path);
99
100 file_lock->handle = INVALID_HANDLE_VALUE;
101 #else /* WIN32 */
102 closecxx . . $ close(7864480464b09eea).(file_lock->fdcxx . . $ $anonymous_type_89a07738fa906650_0#fd.);
103 unlinkcxx . . $ unlink(5c4306d8553b3696).(file_lock->pathcxx . . $ $anonymous_type_89a07738fa906650_0#path.);
104
105 file_lock->fdcxx . . $ $anonymous_type_89a07738fa906650_0#fd. = -1;
106 #endif /* WIN32 */
107 grn_strcpycxx . . $ `include/groonga/portability.h:101:11`!(file_lock->path, PATH_MAX, "");
108 }
109
110 void
111 grn_file_lock_fincxx . . $ grn_file_lock_fin(7fc6e84b96ea19e9).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_file_lockcxx . . $ grn_file_lock# *file_lock)
112 {
113 if (!GRN_FILE_LOCK_IS_INVALIDcxx . . $ `lib/file_lock.c:36:10`!(file_lock)) {
114 grn_file_lock_releasecxx . . $ grn_file_lock_release(7fc6e84b96ea19e9).(ctx, file_lock);
115 }
116 }