Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "LockFile.h" 11 : #include "MooseError.h" 12 : #include <fcntl.h> 13 : #include <sys/file.h> 14 : #include <unistd.h> 15 : 16 157257 : LockFile::LockFile(const std::string & filename, bool do_lock) 17 157257 : : _do_lock(do_lock), _fd(-1), _filename(filename) 18 : { 19 : // for now just do not do any locking on Windows 20 : #ifndef __WIN32__ 21 157257 : if (_do_lock) 22 : { 23 116386 : _fd = open(filename.c_str(), O_RDWR | O_CREAT, 0666); 24 116386 : if (_fd == -1) 25 0 : mooseError("Failed to open file", filename); 26 116386 : if (flock(_fd, LOCK_EX) != 0) 27 0 : mooseWarning("Failed to lock file ", filename); 28 : } 29 : #endif 30 157257 : } 31 : 32 314514 : LockFile::~LockFile() 33 : { 34 : #ifndef __WIN32__ 35 157257 : if (_do_lock) 36 : { 37 116386 : if (flock(_fd, LOCK_UN) != 0) 38 0 : mooseWarning("Failed to unlock file ", _filename); 39 116386 : close(_fd); 40 : } 41 : #endif 42 157257 : }