You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
4 years ago
|
/*
|
||
|
* Created on Fri Aug 07 2020
|
||
|
*
|
||
|
* This file is a part of the source code for the Terrabase database
|
||
|
* Copyright (c) 2020, Sayan Nandan <ohsayan at outlook dot com>
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program 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 Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <errno.h>
|
||
|
#include <fcntl.h>
|
||
|
|
||
4 years ago
|
/* Lock a file */
|
||
4 years ago
|
int lock_file(int descriptor) {
|
||
|
if (descriptor < 0) {
|
||
4 years ago
|
return EBADF;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
struct flock fl;
|
||
|
|
||
|
/* Lock the whole file - not just a part of it! */
|
||
|
fl.l_type = F_WRLCK;
|
||
|
fl.l_whence = SEEK_SET;
|
||
|
fl.l_start = 0;
|
||
|
fl.l_len = 0;
|
||
4 years ago
|
|
||
4 years ago
|
if (fcntl(descriptor, F_SETLKW, &fl) == -1) {
|
||
|
return errno;
|
||
4 years ago
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
4 years ago
|
/* Unlock a file */
|
||
4 years ago
|
int unlock_file(int descriptor) {
|
||
4 years ago
|
struct flock fl;
|
||
4 years ago
|
|
||
|
if (descriptor < 0) {
|
||
4 years ago
|
return EBADF;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
/* Unlock the whole file - not just a part of it! */
|
||
|
fl.l_type = F_UNLCK;
|
||
|
fl.l_whence = SEEK_SET;
|
||
|
fl.l_start = 0;
|
||
|
fl.l_len = 0;
|
||
4 years ago
|
|
||
4 years ago
|
if (fcntl(descriptor, F_SETLKW, &fl) == -1) {
|
||
4 years ago
|
return errno;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|