• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • highscore
tdefilelock.cpp
1 /*
2  This file is part of the TDE games library
3  Copyright (C) 2003 Nicolas Hadacek <hadacek@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "tdefilelock.h"
21 
22 #include <unistd.h>
23 #include <sys/file.h>
24 #include <errno.h>
25 
26 #include <kdebug.h>
27 
28 
29 KFileLock::KFileLock(int fd)
30  : _fd(fd), _locked(false)
31 {}
32 
33 int KFileLock::lock()
34 {
35  kdDebug(11002) << "lock fd=" << _fd << endl;
36 #ifdef F_SETLK
37 # ifndef SEEK_SET
38 # define SEEK_SET 0
39 # endif
40  struct flock lock_data;
41  lock_data.l_type = F_WRLCK;
42  lock_data.l_whence = SEEK_SET;
43  lock_data.l_start = lock_data.l_len = 0;
44  if ( fcntl(_fd, F_SETLK, &lock_data)==-1 ) {
45  if ( errno==EAGAIN ) return -2;
46  return -1;
47  }
48 #else
49 # ifdef LOCK_EX
50  if ( flock (_fd, LOCK_EX|LOCK_NB)==-1 ) {
51  if ( errno==EWOULDBLOCK ) return -2;
52  return -1;
53  }
54 # else
55  if ( lockf(_fd, F_TLOCK, 0)==-1 ) {
56  if ( errno==EACCES ) return -2;
57  return -1;
58  }
59 # endif
60 #endif
61  _locked = true;
62  return 0;
63 }
64 
65 KFileLock::~KFileLock()
66 {
67  unlock();
68 }
69 
70 void KFileLock::unlock()
71 {
72  if ( !_locked ) return;
73  kdDebug(11002) << "unlock" << endl;
74 # ifdef F_SETLK
75  struct flock lock_data;
76  lock_data.l_type = F_UNLCK;
77  lock_data.l_whence = SEEK_SET;
78  lock_data.l_start = lock_data.l_len = 0;
79  (void)fcntl(_fd, F_SETLK, &lock_data);
80 # else
81 # ifdef F_ULOCK
82  lockf(_fd, F_ULOCK, 0);
83 # else
84  flock(_fd, LOCK_UN);
85 # endif
86 # endif
87  _locked = false;
88 }

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.1
This website is maintained by Timothy Pearson.