qtlockedfile_unix.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU
  18. ** General Public License version 3 as published by the Free Software
  19. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  20. ** included in the packaging of this file. Please review the following
  21. ** information to ensure the GNU General Public License requirements will
  22. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  23. **
  24. ****************************************************************************/
  25. #include "qtlockedfile.h"
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. namespace SharedTools {
  31. bool QtLockedFile::lock(LockMode mode, bool block)
  32. {
  33. if (!isOpen()) {
  34. qWarning("QtLockedFile::lock(): file is not opened");
  35. return false;
  36. }
  37. if (mode == NoLock)
  38. return unlock();
  39. if (mode == m_lock_mode)
  40. return true;
  41. if (m_lock_mode != NoLock)
  42. unlock();
  43. struct flock fl;
  44. fl.l_whence = SEEK_SET;
  45. fl.l_start = 0;
  46. fl.l_len = 0;
  47. fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK;
  48. int cmd = block ? F_SETLKW : F_SETLK;
  49. int ret = fcntl(handle(), cmd, &fl);
  50. if (ret == -1) {
  51. if (errno != EINTR && errno != EAGAIN)
  52. qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
  53. return false;
  54. }
  55. m_lock_mode = mode;
  56. return true;
  57. }
  58. bool QtLockedFile::unlock()
  59. {
  60. if (!isOpen()) {
  61. qWarning("QtLockedFile::unlock(): file is not opened");
  62. return false;
  63. }
  64. if (!isLocked())
  65. return true;
  66. struct flock fl;
  67. fl.l_whence = SEEK_SET;
  68. fl.l_start = 0;
  69. fl.l_len = 0;
  70. fl.l_type = F_UNLCK;
  71. int ret = fcntl(handle(), F_SETLKW, &fl);
  72. if (ret == -1) {
  73. qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
  74. return false;
  75. }
  76. m_lock_mode = NoLock;
  77. remove();
  78. return true;
  79. }
  80. QtLockedFile::~QtLockedFile()
  81. {
  82. if (isOpen())
  83. unlock();
  84. }
  85. } // namespace SharedTools