Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/standard/flock_compat.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | https://www.php.net/license/3_01.txt                                 |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Author: Sascha Schumann <sascha@schumann.cx>                         |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#include "php.h"
18
#include <errno.h>
19
#include "ext/standard/flock_compat.h"
20
21
#ifndef HAVE_FLOCK
22
PHPAPI int flock(int fd, int operation)
23
{
24
  return php_flock(fd, operation);
25
}
26
#endif /* !defined(HAVE_FLOCK) */
27
28
PHPAPI int php_flock(int fd, int operation)
29
#ifdef HAVE_STRUCT_FLOCK /* {{{ */
30
0
{
31
0
  struct flock flck;
32
0
  int ret;
33
34
0
  flck.l_start = flck.l_len = 0;
35
0
  flck.l_whence = SEEK_SET;
36
37
0
  if (operation & LOCK_SH)
38
0
    flck.l_type = F_RDLCK;
39
0
  else if (operation & LOCK_EX)
40
0
    flck.l_type = F_WRLCK;
41
0
  else if (operation & LOCK_UN)
42
0
    flck.l_type = F_UNLCK;
43
0
  else {
44
0
    errno = EINVAL;
45
0
    return -1;
46
0
  }
47
48
0
  ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck);
49
50
0
  if ((operation & LOCK_NB) && ret == -1 &&
51
0
      (errno == EACCES || errno == EAGAIN))
52
0
    errno = EWOULDBLOCK;
53
54
0
  if (ret != -1) ret = 0;
55
56
0
  return ret;
57
0
}
58
/* }}} */
59
#elif defined(PHP_WIN32) /* {{{ */
60
/*
61
 * Program:   Unix compatibility routines
62
 *
63
 * Author:  Mark Crispin
64
 *      Networks and Distributed Computing
65
 *      Computing & Communications
66
 *      University of Washington
67
 *      Administration Building, AG-44
68
 *      Seattle, WA  98195
69
 *      Internet: MRC@CAC.Washington.EDU
70
 *
71
 * Date:    14 September 1996
72
 * Last Edited: 14 August 1997
73
 *
74
 * Copyright 1997 by the University of Washington
75
 *
76
 *  Permission to use, copy, modify, and distribute this software and its
77
 * documentation for any purpose and without fee is hereby granted, provided
78
 * that the above copyright notice appears in all copies and that both the
79
 * above copyright notice and this permission notice appear in supporting
80
 * documentation, and that the name of the University of Washington not be
81
 * used in advertising or publicity pertaining to distribution of the software
82
 * without specific, written prior permission.  This software is made available
83
 * "as is", and
84
 * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
85
 * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
86
 * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
87
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
88
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
89
 * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
90
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
91
 *
92
 */
93
/*              DEDICATION
94
95
 *  This file is dedicated to my dog, Unix, also known as Yun-chan and
96
 * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast.  Unix
97
 * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
98
 * a two-month bout with cirrhosis of the liver.
99
 *
100
 *  He was a dear friend, and I miss him terribly.
101
 *
102
 *  Lift a leg, Yunie.  Luv ya forever!!!!
103
 */
104
{
105
  HANDLE hdl = (HANDLE) _get_osfhandle(fd);
106
  DWORD low = 0xFFFFFFFF, high = 0xFFFFFFFF;
107
  OVERLAPPED offset = {0, 0, {{0}}, NULL};
108
  DWORD err;
109
110
  if (INVALID_HANDLE_VALUE == hdl) {
111
    _set_errno(EBADF);
112
    return -1;              /* error in file descriptor */
113
  }
114
  /* bug for bug compatible with Unix */
115
  UnlockFileEx(hdl, 0, low, high, &offset);
116
  switch (operation & ~LOCK_NB) {    /* translate to LockFileEx() op */
117
    case LOCK_EX:           /* exclusive */
118
      if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
119
            ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
120
               0, low, high, &offset))
121
        return 0;
122
      break;
123
    case LOCK_SH:           /* shared */
124
      if (LockFileEx(hdl, ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
125
               0, low, high, &offset))
126
        return 0;
127
      break;
128
    case LOCK_UN:           /* unlock */
129
      return 0;           /* always succeeds */
130
    default:                /* default */
131
      break;
132
  }
133
134
  err = GetLastError();
135
  if (ERROR_LOCK_VIOLATION == err || ERROR_SHARING_VIOLATION == err) {
136
    _set_errno(EWOULDBLOCK);
137
  } else {
138
    _set_errno(EINVAL);             /* bad call */
139
  }
140
141
  return -1;
142
}
143
/* }}} */
144
#else
145
#warning no proper flock support for your site
146
{
147
  errno = 0;
148
  return 0;
149
}
150
#endif