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 171127 : LockFile::LockFile(const std::string & filename, bool do_lock) 17 171127 : : _do_lock(do_lock), _fd(-1), _filename(filename) 18 : { 19 : // for now just do not do any locking on Windows 20 : #ifndef __WIN32__ 21 171127 : if (_do_lock) 22 : { 23 129700 : _fd = open(filename.c_str(), O_RDWR | O_CREAT, 0666); 24 129700 : if (_fd == -1) 25 0 : mooseError("Failed to open file", filename); 26 129700 : if (flock(_fd, LOCK_EX) != 0) 27 0 : mooseWarning("Failed to lock file ", filename); 28 : } 29 : #endif 30 171127 : } 31 : 32 342254 : LockFile::~LockFile() 33 : { 34 : #ifndef __WIN32__ 35 171127 : if (_do_lock) 36 : { 37 129700 : if (flock(_fd, LOCK_UN) != 0) 38 0 : mooseWarning("Failed to unlock file ", _filename); 39 129700 : close(_fd); 40 : } 41 : #endif 42 171127 : }