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.
15 #include <mono/io-layer/wapi.h>
16 #include <mono/io-layer/wapi-private.h>
17 #include <mono/io-layer/handles-private.h>
18 #include <mono/io-layer/io-private.h>
19 #include <mono/io-layer/io-trace.h>
20 #include <mono/utils/mono-logger-internals.h>
23 _wapi_lock_file_region (int fd, off_t offset, off_t length)
25 #if defined(__native_client__)
26 printf("WARNING: locking.c: _wapi_lock_file_region(): fcntl() not available on Native Client!\n");
27 // behave as below -- locks are not available
30 struct flock lock_data;
33 if (offset < 0 || length < 0) {
34 SetLastError (ERROR_INVALID_PARAMETER);
38 lock_data.l_type = F_WRLCK;
39 lock_data.l_whence = SEEK_SET;
40 lock_data.l_start = offset;
41 lock_data.l_len = length;
44 ret = fcntl (fd, F_SETLK, &lock_data);
45 } while(ret == -1 && errno == EINTR);
47 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
51 * if locks are not available (NFS for example),
56 || errno == EOPNOTSUPP
65 SetLastError (ERROR_LOCK_VIOLATION);
70 #endif /* __native_client__ */
74 _wapi_unlock_file_region (int fd, off_t offset, off_t length)
76 #if defined(__native_client__)
77 printf("WARNING: locking.c: _wapi_unlock_file_region(): fcntl() not available on Native Client!\n");
80 struct flock lock_data;
83 lock_data.l_type = F_UNLCK;
84 lock_data.l_whence = SEEK_SET;
85 lock_data.l_start = offset;
86 lock_data.l_len = length;
89 ret = fcntl (fd, F_SETLK, &lock_data);
90 } while(ret == -1 && errno == EINTR);
92 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
96 * if locks are not available (NFS for example),
101 || errno == EOPNOTSUPP
110 SetLastError (ERROR_LOCK_VIOLATION);
115 #endif /* __native_client__ */
119 LockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
120 guint32 length_low, guint32 length_high)
122 struct _WapiHandle_file *file_handle;
124 off_t offset, length;
125 int fd = GPOINTER_TO_UINT(handle);
127 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
128 (gpointer *)&file_handle);
130 g_warning ("%s: error looking up file handle %p", __func__,
132 SetLastError (ERROR_INVALID_HANDLE);
136 if (!(file_handle->fileaccess & GENERIC_READ) &&
137 !(file_handle->fileaccess & GENERIC_WRITE) &&
138 !(file_handle->fileaccess & GENERIC_ALL)) {
139 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);
140 SetLastError (ERROR_ACCESS_DENIED);
144 #ifdef HAVE_LARGE_FILE_SUPPORT
145 offset = ((gint64)offset_high << 32) | offset_low;
146 length = ((gint64)length_high << 32) | length_low;
148 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
150 if (offset_high > 0 || length_high > 0) {
151 SetLastError (ERROR_INVALID_PARAMETER);
157 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %ld, length %ld", __func__,
158 handle, offset, length);
161 return(_wapi_lock_file_region (fd, offset, length));
165 UnlockFile (gpointer handle, guint32 offset_low,
166 guint32 offset_high, guint32 length_low,
169 struct _WapiHandle_file *file_handle;
171 off_t offset, length;
172 int fd = GPOINTER_TO_UINT(handle);
174 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
175 (gpointer *)&file_handle);
177 g_warning ("%s: error looking up file handle %p", __func__,
179 SetLastError (ERROR_INVALID_HANDLE);
183 if (!(file_handle->fileaccess & GENERIC_READ) &&
184 !(file_handle->fileaccess & GENERIC_WRITE) &&
185 !(file_handle->fileaccess & GENERIC_ALL)) {
186 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);
187 SetLastError (ERROR_ACCESS_DENIED);
191 #ifdef HAVE_LARGE_FILE_SUPPORT
192 offset = ((gint64)offset_high << 32) | offset_low;
193 length = ((gint64)length_high << 32) | length_low;
195 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
200 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
203 return(_wapi_unlock_file_region (fd, offset, length));