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>
21 //#define LOGDEBUG(...) g_message (__VA_ARGS__)
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 lock_data.l_type = F_WRLCK;
35 lock_data.l_whence = SEEK_SET;
36 lock_data.l_start = offset;
37 lock_data.l_len = length;
40 ret = fcntl (fd, F_SETLK, &lock_data);
41 } while(ret == -1 && errno == EINTR);
43 LOGDEBUG ("%s: fcntl returns %d", __func__, ret);
47 * if locks are not available (NFS for example),
52 || errno == EOPNOTSUPP
61 SetLastError (ERROR_LOCK_VIOLATION);
66 #endif /* __native_client__ */
70 _wapi_unlock_file_region (int fd, off_t offset, off_t length)
72 #if defined(__native_client__)
73 printf("WARNING: locking.c: _wapi_unlock_file_region(): fcntl() not available on Native Client!\n");
76 struct flock lock_data;
79 lock_data.l_type = F_UNLCK;
80 lock_data.l_whence = SEEK_SET;
81 lock_data.l_start = offset;
82 lock_data.l_len = length;
85 ret = fcntl (fd, F_SETLK, &lock_data);
86 } while(ret == -1 && errno == EINTR);
88 LOGDEBUG ("%s: fcntl returns %d", __func__, ret);
92 * if locks are not available (NFS for example),
97 || errno == EOPNOTSUPP
106 SetLastError (ERROR_LOCK_VIOLATION);
111 #endif /* __native_client__ */
115 LockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
116 guint32 length_low, guint32 length_high)
118 struct _WapiHandle_file *file_handle;
120 off_t offset, length;
121 int fd = GPOINTER_TO_UINT(handle);
123 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
124 (gpointer *)&file_handle);
126 g_warning ("%s: error looking up file handle %p", __func__,
128 SetLastError (ERROR_INVALID_HANDLE);
132 if (!(file_handle->fileaccess & GENERIC_READ) &&
133 !(file_handle->fileaccess & GENERIC_WRITE) &&
134 !(file_handle->fileaccess & GENERIC_ALL)) {
135 LOGDEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
136 SetLastError (ERROR_ACCESS_DENIED);
140 #ifdef HAVE_LARGE_FILE_SUPPORT
141 offset = ((gint64)offset_high << 32) | offset_low;
142 length = ((gint64)length_high << 32) | length_low;
144 LOGDEBUG ("%s: Locking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
150 g_message ("%s: Locking handle %p, offset %ld, length %ld", __func__,
151 handle, offset, length);
155 return(_wapi_lock_file_region (fd, offset, length));
159 UnlockFile (gpointer handle, guint32 offset_low,
160 guint32 offset_high, guint32 length_low,
163 struct _WapiHandle_file *file_handle;
165 off_t offset, length;
166 int fd = GPOINTER_TO_UINT(handle);
168 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
169 (gpointer *)&file_handle);
171 g_warning ("%s: error looking up file handle %p", __func__,
173 SetLastError (ERROR_INVALID_HANDLE);
177 if (!(file_handle->fileaccess & GENERIC_READ) &&
178 !(file_handle->fileaccess & GENERIC_WRITE) &&
179 !(file_handle->fileaccess & GENERIC_ALL)) {
180 LOGDEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
181 SetLastError (ERROR_ACCESS_DENIED);
185 #ifdef HAVE_LARGE_FILE_SUPPORT
186 offset = ((gint64)offset_high << 32) | offset_low;
187 length = ((gint64)length_high << 32) | length_low;
189 LOGDEBUG ("%s: Unlocking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
194 LOGDEBUG ("%s: Unlocking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
197 return(_wapi_unlock_file_region (fd, offset, length));