lockFile.cpp

Go to the documentation of this file.
00001 
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <fcntl.h>
00018 #include <dlrUtilities/lockFile.h>
00019 #include <dlrUtilities/timeUtilities.h>
00020 
00021 namespace dlr {
00022 
00023   namespace utilities {
00024     
00025     // The constructor attempts to create the lock file.
00026     LockFile::
00027     LockFile(const std::string& fileName,
00028              double timeout)
00029       : m_fileDescriptor(open(fileName.c_str(), O_WRONLY | O_CREAT | O_EXCL,
00030                               S_IRUSR)),
00031         m_fileName(fileName)
00032     {
00033       if(timeout != 0.0) {
00034         double stopTime = std::numeric_limits<double>::max();
00035         double sleepTime = 0.1;
00036         if(timeout > 0.0) {
00037           stopTime = getCurrentTime() + timeout;
00038           sleepTime = std::min(1.0, timeout / 100.0);
00039         }
00040         while(m_fileDescriptor < 0) {
00041           portableSleep(sleepTime);
00042           m_fileDescriptor =
00043             open(fileName.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
00044           if(getCurrentTime() > stopTime) {
00045             break;
00046           }
00047         }
00048       }
00049       if(m_fileDescriptor != -1) {
00050         close(m_fileDescriptor);
00051       }
00052     }
00053   
00054 
00055     // The constructor attempts to create a non-empty lock file.
00056     LockFile::
00057     LockFile(const std::string& fileName,
00058              const std::string& contents,
00059              double timeout)
00060       : m_fileDescriptor(open(fileName.c_str(), O_WRONLY | O_CREAT | O_EXCL,
00061                               S_IWUSR)),
00062         m_fileName(fileName)
00063     {
00064       if(timeout != 0.0) {
00065         double stopTime = std::numeric_limits<double>::max();
00066         double sleepTime = 0.1;
00067         if(timeout > 0.0) {
00068           stopTime = getCurrentTime() + timeout;
00069           sleepTime = std::min(1.0, timeout / 100.0);
00070         }
00071         while(m_fileDescriptor < 0) {
00072           portableSleep(sleepTime);
00073           m_fileDescriptor =
00074             open(fileName.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
00075           if(getCurrentTime() > stopTime) {
00076             break;
00077           }
00078         }
00079       }
00080       if(m_fileDescriptor != -1) {
00081         // Success of these function calls is largely irrelevant.
00082         write(m_fileDescriptor, (const void*)contents.c_str(), contents.size());
00083         close(m_fileDescriptor);
00084         chmod(m_fileName.c_str(), S_IRUSR);
00085       }
00086     }
00087       
00088 
00089     // The destructor destroys the LockFile instance and deletes any
00090     // file created by the constructor, thereby releasing the lock.
00091     LockFile::
00092     ~LockFile()
00093     {
00094       if(m_fileDescriptor != -1) {
00095         // There's no really good way to handle a failure in one of
00096         // these two calls, since we don't want to throw an exception
00097         // from a destructor.  Instead we just assume they'll succeed.
00098         chmod(m_fileName.c_str(), S_IWUSR);
00099         unlink(m_fileName.c_str());
00100       }
00101     }
00102 
00103 
00104     // This method reports whether or not the lock was successfully
00105     // obtained.
00106     bool
00107     LockFile::
00108     isValid()
00109     {
00110       return (m_fileDescriptor != -1);
00111     }
00112 
00113   } // namespace utilities
00114   
00115 } // namespace dlr

Generated on Mon Jul 9 20:34:03 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2