MCPcopy Create free account
hub / github.com/FirebirdSQL/firebird / UTIL_ex_lock

Function UTIL_ex_lock

src/utilities/guard/util.cpp:230–278  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

228
229
230int UTIL_ex_lock(const TEXT* file)
231{
232/**************************************
233 *
234 * U T I L _ e x _ l o c k
235 *
236 **************************************
237 *
238 * Functional description
239 *
240 * This function is used to exclusively lock a file.
241 *
242 * Return Codes:
243 * -1 Failure to open file.
244 * -2 Failure to obtain exclusive lock on file.
245 * otherwise Success - file descriptor is returned.
246 *
247 **************************************/
248
249 // get the file name and prepend the complete path etc
250 Firebird::PathName expanded_filename = fb_utils::getPrefix(Firebird::IConfigManager::DIR_GUARD, file);
251
252 // file fd for the opened and locked file
253 int fd_file = os_utils::open(expanded_filename.c_str(), O_RDWR | O_CREAT, 0660);
254 if (fd_file == -1)
255 {
256 fprintf(stderr, "Could not open %s for write\n", expanded_filename.c_str());
257 return (-1);
258 }
259
260 fb_assert(fd_file != -2);
261
262#ifndef HAVE_FLOCK
263 // get an exclusive lock on the GUARD file without blocking on the call
264 struct FLOCK lock;
265 lock.l_type = F_WRLCK;
266 lock.l_whence = 0;
267 lock.l_start = 0;
268 lock.l_len = 0;
269 if (fcntl(fd_file, F_SETLK, &lock) == -1)
270#else
271 if (flock(fd_file, LOCK_EX))
272#endif
273 {
274 close(fd_file);
275 return (-2);
276 }
277 return (fd_file);
278}
279
280
281void UTIL_ex_unlock( int fd_file)

Callers 1

CLIB_ROUTINE mainFunction · 0.85

Calls 3

getPrefixFunction · 0.85
openFunction · 0.50
c_strMethod · 0.45

Tested by

no test coverage detected