flock.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_arch_file_io.h"
  17. APR_DECLARE(fspr_status_t) fspr_file_lock(fspr_file_t *thefile, int type)
  18. {
  19. #ifdef _WIN32_WCE
  20. /* The File locking is unsuported on WCE */
  21. return APR_ENOTIMPL;
  22. #else
  23. const DWORD len = 0xffffffff;
  24. DWORD flags;
  25. flags = ((type & APR_FLOCK_NONBLOCK) ? LOCKFILE_FAIL_IMMEDIATELY : 0)
  26. + (((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
  27. ? 0 : LOCKFILE_EXCLUSIVE_LOCK);
  28. if (fspr_os_level >= APR_WIN_NT) {
  29. /* Syntax is correct, len is passed for LengthLow and LengthHigh*/
  30. OVERLAPPED offset;
  31. memset (&offset, 0, sizeof(offset));
  32. if (!LockFileEx(thefile->filehand, flags, 0, len, len, &offset))
  33. return fspr_get_os_error();
  34. }
  35. else {
  36. /* On Win9x, LockFile() never blocks. Hack in a crufty poll.
  37. *
  38. * Note that this hack exposes threads to being unserviced forever,
  39. * in the situation that the given lock has low availability.
  40. * When implemented in the kernel, LockFile will typically use
  41. * FIFO or round robin distribution to ensure all threads get
  42. * one crack at the lock; but in this case we can't emulate that.
  43. *
  44. * However Win9x are barely maintainable anyways, if the user does
  45. * choose to build to them, this is the best we can do.
  46. */
  47. while (!LockFile(thefile->filehand, 0, 0, len, 0)) {
  48. DWORD err = GetLastError();
  49. if ((err == ERROR_LOCK_VIOLATION) && !(type & APR_FLOCK_NONBLOCK))
  50. {
  51. Sleep(500); /* pause for a half second */
  52. continue; /* ... and then poll again */
  53. }
  54. return APR_FROM_OS_ERROR(err);
  55. }
  56. }
  57. return APR_SUCCESS;
  58. #endif /* !defined(_WIN32_WCE) */
  59. }
  60. APR_DECLARE(fspr_status_t) fspr_file_unlock(fspr_file_t *thefile)
  61. {
  62. #ifdef _WIN32_WCE
  63. return APR_ENOTIMPL;
  64. #else
  65. DWORD len = 0xffffffff;
  66. if (fspr_os_level >= APR_WIN_NT) {
  67. /* Syntax is correct, len is passed for LengthLow and LengthHigh*/
  68. OVERLAPPED offset;
  69. memset (&offset, 0, sizeof(offset));
  70. if (!UnlockFileEx(thefile->filehand, 0, len, len, &offset))
  71. return fspr_get_os_error();
  72. }
  73. else {
  74. if (!UnlockFile(thefile->filehand, 0, 0, len, 0))
  75. return fspr_get_os_error();
  76. }
  77. return APR_SUCCESS;
  78. #endif /* !defined(_WIN32_WCE) */
  79. }