2 * io.c: File, console and find handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 * Copyright (c) 2002-2009 Novell, Inc.
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <mono/io-layer/wapi.h>
17 #include <mono/io-layer/wapi-private.h>
18 #include <mono/io-layer/handles-private.h>
19 #include <mono/io-layer/io-private.h>
20 #include <mono/io-layer/io-trace.h>
21 #include <mono/utils/mono-logger-internals.h>
24 _wapi_lock_file_region (int fd, off_t offset, off_t length)
26 #if defined(__native_client__)
27 printf("WARNING: locking.c: _wapi_lock_file_region(): fcntl() not available on Native Client!\n");
28 // behave as below -- locks are not available
31 struct flock lock_data;
34 if (offset < 0 || length < 0) {
35 SetLastError (ERROR_INVALID_PARAMETER);
39 lock_data.l_type = F_WRLCK;
40 lock_data.l_whence = SEEK_SET;
41 lock_data.l_start = offset;
42 lock_data.l_len = length;
45 ret = fcntl (fd, F_SETLK, &lock_data);
46 } while(ret == -1 && errno == EINTR);
48 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
52 * if locks are not available (NFS for example),
57 || errno == EOPNOTSUPP
66 SetLastError (ERROR_LOCK_VIOLATION);
71 #endif /* __native_client__ */
75 _wapi_unlock_file_region (int fd, off_t offset, off_t length)
77 #if defined(__native_client__)
78 printf("WARNING: locking.c: _wapi_unlock_file_region(): fcntl() not available on Native Client!\n");
81 struct flock lock_data;
84 lock_data.l_type = F_UNLCK;
85 lock_data.l_whence = SEEK_SET;
86 lock_data.l_start = offset;
87 lock_data.l_len = length;
90 ret = fcntl (fd, F_SETLK, &lock_data);
91 } while(ret == -1 && errno == EINTR);
93 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
97 * if locks are not available (NFS for example),
102 || errno == EOPNOTSUPP
111 SetLastError (ERROR_LOCK_VIOLATION);
116 #endif /* __native_client__ */
120 LockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
121 guint32 length_low, guint32 length_high)
123 struct _WapiHandle_file *file_handle;
125 off_t offset, length;
126 int fd = GPOINTER_TO_UINT(handle);
128 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
129 (gpointer *)&file_handle);
131 g_warning ("%s: error looking up file handle %p", __func__,
133 SetLastError (ERROR_INVALID_HANDLE);
137 if (!(file_handle->fileaccess & GENERIC_READ) &&
138 !(file_handle->fileaccess & GENERIC_WRITE) &&
139 !(file_handle->fileaccess & GENERIC_ALL)) {
140 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
141 SetLastError (ERROR_ACCESS_DENIED);
145 #ifdef HAVE_LARGE_FILE_SUPPORT
146 offset = ((gint64)offset_high << 32) | offset_low;
147 length = ((gint64)length_high << 32) | length_low;
149 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
151 if (offset_high > 0 || length_high > 0) {
152 SetLastError (ERROR_INVALID_PARAMETER);
158 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %ld, length %ld", __func__,
159 handle, offset, length);
162 return(_wapi_lock_file_region (fd, offset, length));
166 UnlockFile (gpointer handle, guint32 offset_low,
167 guint32 offset_high, guint32 length_low,
170 struct _WapiHandle_file *file_handle;
172 off_t offset, length;
173 int fd = GPOINTER_TO_UINT(handle);
175 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
176 (gpointer *)&file_handle);
178 g_warning ("%s: error looking up file handle %p", __func__,
180 SetLastError (ERROR_INVALID_HANDLE);
184 if (!(file_handle->fileaccess & GENERIC_READ) &&
185 !(file_handle->fileaccess & GENERIC_WRITE) &&
186 !(file_handle->fileaccess & GENERIC_ALL)) {
187 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
188 SetLastError (ERROR_ACCESS_DENIED);
192 #ifdef HAVE_LARGE_FILE_SUPPORT
193 offset = ((gint64)offset_high << 32) | offset_low;
194 length = ((gint64)length_high << 32) | length_low;
196 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
201 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
204 return(_wapi_unlock_file_region (fd, offset, length));