libkdegames Library API Documentation

kfilelock.cpp

00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2003 Nicolas Hadacek <hadacek@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <config.h>
00021 
00022 #include "kfilelock.h"
00023 
00024 #include <unistd.h>
00025 #include <sys/file.h>
00026 #include <errno.h>
00027 
00028 #include <kdebug.h>
00029 
00030 
00031 KFileLock::KFileLock(int fd)
00032     : _fd(fd), _locked(false)
00033 {}
00034 
00035 int KFileLock::lock()
00036 {
00037     kdDebug(11002) << "lock fd=" << _fd << endl;
00038 #ifdef F_SETLK
00039 # ifndef SEEK_SET
00040 #  define SEEK_SET 0
00041 # endif
00042     struct flock lock_data;
00043     lock_data.l_type = F_WRLCK;
00044     lock_data.l_whence = SEEK_SET;
00045     lock_data.l_start = lock_data.l_len = 0;
00046     if ( fcntl(_fd, F_SETLK, &lock_data)==-1 ) {
00047         if ( errno==EAGAIN ) return -2;
00048         return -1;
00049     }
00050 #else
00051 # ifdef LOCK_EX
00052     if ( flock (_fd, LOCK_EX|LOCK_NB)==-1 ) {
00053         if ( errno==EWOULDBLOCK ) return -2;
00054         return -1;
00055     }
00056 # else
00057     if ( lockf(_fd, F_TLOCK, 0)==-1 ) {
00058         if ( errno==EACCES ) return -2;
00059         return -1;
00060     }
00061 # endif
00062 #endif
00063     _locked = true;
00064     return 0;
00065 }
00066 
00067 KFileLock::~KFileLock()
00068 {
00069     unlock();
00070 }
00071 
00072 void KFileLock::unlock()
00073 {
00074     if ( !_locked ) return;
00075     kdDebug(11002) << "unlock" << endl;
00076 # ifdef F_SETLK
00077     struct flock lock_data;
00078     lock_data.l_type = F_UNLCK;
00079     lock_data.l_whence = SEEK_SET;
00080     lock_data.l_start = lock_data.l_len = 0;
00081     (void)fcntl(_fd, F_SETLK, &lock_data);
00082 # else
00083 #  ifdef F_ULOCK
00084     lockf(_fd, F_ULOCK, 0);
00085 #  else
00086     flock(_fd, LOCK_UN);
00087 #  endif
00088 # endif
00089     _locked = false;
00090 }
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Sep 12 05:17:50 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003