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.
71 lines
1.7 KiB
C
71 lines
1.7 KiB
C
/*
|
|
* Created on Fri Aug 07 2020
|
|
*
|
|
* This file is a part of Skybase
|
|
* Skybase (formerly known as TerrabaseDB) is a free and open-source
|
|
* NoSQL database written by Sayan Nandan ("the Author") with the
|
|
* vision to provide flexibility in data modelling without compromising
|
|
* on performance, queryability or scalability.
|
|
*
|
|
* Copyright (c) 2020, Sayan Nandan <ohsayan@outlook.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>
|
|
|
|
/* Lock a file */
|
|
int lock_file(int descriptor) {
|
|
if (descriptor < 0) {
|
|
return EBADF;
|
|
}
|
|
|
|
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;
|
|
|
|
if (fcntl(descriptor, F_SETLKW, &fl) == -1) {
|
|
return errno;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* Unlock a file */
|
|
int unlock_file(int descriptor) {
|
|
struct flock fl;
|
|
|
|
if (descriptor < 0) {
|
|
return EBADF;
|
|
}
|
|
|
|
/* 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;
|
|
|
|
if (fcntl(descriptor, F_SETLKW, &fl) == -1) {
|
|
return errno;
|
|
}
|
|
|
|
return 0;
|
|
}
|