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"
20 #include "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_INVALID(file_lock) \
37 ((file_lock)->fd == -1)
38 #endif /* WIN32 */
39
40 void
41 grn_file_lock_init(grn_ctx *ctx,
42 grn_file_lock *file_lock,
43 const char *path)
44 {
45 grn_strcpy(file_lock->path, PATH_MAX, path);
46 #ifdef WIN32
47 file_lock->handle = INVALID_HANDLE_VALUE;
48 #else /* WIN32 */
49 file_lock->fd = -1;
50 #endif /* WIN32 */
51 }
52
53 bool
54 grn_file_lock_acquire(grn_ctx *ctx,
55 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->fd = open(file_lock->path, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
73 #endif
74 if (!GRN_FILE_LOCK_IS_INVALID(file_lock)) {
75 break;
76 }
77 grn_nanosleep(GRN_LOCK_WAIT_TIME_NANOSECOND);
78 }
79
80 if (GRN_FILE_LOCK_IS_INVALID(file_lock)) {
81 ERR(GRN_NO_LOCKS_AVAILABLE,
82 "%s failed to acquire lock: <%s>",
83 error_message_tag, file_lock->path);
84 return false;
85 }
86 return true;
87 }
88
89 void
90 grn_file_lock_release(grn_ctx *ctx, grn_file_lock *file_lock)
91 {
92 if (GRN_FILE_LOCK_IS_INVALID(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 close(file_lock->fd);
103 unlink(file_lock->path);
104
105 file_lock->fd = -1;
106 #endif /* WIN32 */
107 grn_strcpy(file_lock->path, PATH_MAX, "");
108 }
109
110 void
111 grn_file_lock_fin(grn_ctx *ctx, grn_file_lock *file_lock)
112 {
113 if (!GRN_FILE_LOCK_IS_INVALID(file_lock)) {
114 grn_file_lock_release(ctx, file_lock);
115 }
116 }