Back to index

nordugrid-arc-nox  1.1.0~rc6
FileLock.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <cstdlib>
00008 // NOTE: On Solaris errno is not working properly if cerrno is included first
00009 #include <cerrno>
00010 
00011 #include <sys/types.h>
00012 #include <sys/stat.h>
00013 #include <fcntl.h>
00014 #include <unistd.h>
00015 #include <glibmm.h>
00016 
00017 #include <arc/FileLock.h>
00018 
00019 namespace Arc {
00020 
00021   FileLock::FileLock(const std::string& filename)
00022     : lockfile(filename + ".lock") {
00023 
00024     fd = open(lockfile.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
00025     while ((fd == -1) && (errno == EEXIST)) {
00026       if (fd != -1)
00027         close(fd);
00028       Glib::usleep(10000);
00029       fd = open(lockfile.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
00030     }
00031     if (fd != -1)
00032       close(fd);
00033   }
00034 
00035   FileLock::~FileLock() {
00036     unlink(lockfile.c_str());
00037   }
00038 
00039   FileLock::operator bool() {
00040     return (fd != -1);
00041   }
00042 
00043   bool FileLock::operator!() {
00044     return (fd == -1);
00045   }
00046 
00047 } // namespace Arc