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 DEBUG(...) g_message(__VA_ARGS__)
27 _wapi_lock_file_region (int fd, off_t offset, off_t length)
29 #if defined(__native_client__)
30 printf("WARNING: locking.c: _wapi_lock_file_region(): fcntl() not available on Native Client!\n");
31 // behave as below -- locks are not available
34 struct flock lock_data;
37 lock_data.l_type = F_WRLCK;
38 lock_data.l_whence = SEEK_SET;
39 lock_data.l_start = offset;
40 lock_data.l_len = length;
43 ret = fcntl (fd, F_SETLK, &lock_data);
44 } while(ret == -1 && errno == EINTR);
46 DEBUG ("%s: fcntl returns %d", __func__, ret);
50 * if locks are not available (NFS for example),
55 || errno == EOPNOTSUPP
64 SetLastError (ERROR_LOCK_VIOLATION);
69 #endif /* __native_client__ */
73 _wapi_unlock_file_region (int fd, off_t offset, off_t length)
75 #if defined(__native_client__)
76 printf("WARNING: locking.c: _wapi_unlock_file_region(): fcntl() not available on Native Client!\n");
79 struct flock lock_data;
82 lock_data.l_type = F_UNLCK;
83 lock_data.l_whence = SEEK_SET;
84 lock_data.l_start = offset;
85 lock_data.l_len = length;
88 ret = fcntl (fd, F_SETLK, &lock_data);
89 } while(ret == -1 && errno == EINTR);
91 DEBUG ("%s: fcntl returns %d", __func__, ret);
95 * if locks are not available (NFS for example),
100 || errno == EOPNOTSUPP
109 SetLastError (ERROR_LOCK_VIOLATION);
114 #endif /* __native_client__ */
118 LockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
119 guint32 length_low, guint32 length_high)
121 struct _WapiHandle_file *file_handle;
123 off_t offset, length;
124 int fd = GPOINTER_TO_UINT(handle);
126 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
127 (gpointer *)&file_handle);
129 g_warning ("%s: error looking up file handle %p", __func__,
131 SetLastError (ERROR_INVALID_HANDLE);
135 if (!(file_handle->fileaccess & GENERIC_READ) &&
136 !(file_handle->fileaccess & GENERIC_WRITE) &&
137 !(file_handle->fileaccess & GENERIC_ALL)) {
138 DEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
139 SetLastError (ERROR_ACCESS_DENIED);
143 #ifdef HAVE_LARGE_FILE_SUPPORT
144 offset = ((gint64)offset_high << 32) | offset_low;
145 length = ((gint64)length_high << 32) | length_low;
147 DEBUG ("%s: Locking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
152 DEBUG ("%s: Locking handle %p, offset %ld, length %ld", __func__,
153 handle, offset, length);
156 return(_wapi_lock_file_region (fd, offset, length));
160 UnlockFile (gpointer handle, guint32 offset_low,
161 guint32 offset_high, guint32 length_low,
164 struct _WapiHandle_file *file_handle;
166 off_t offset, length;
167 int fd = GPOINTER_TO_UINT(handle);
169 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
170 (gpointer *)&file_handle);
172 g_warning ("%s: error looking up file handle %p", __func__,
174 SetLastError (ERROR_INVALID_HANDLE);
178 if (!(file_handle->fileaccess & GENERIC_READ) &&
179 !(file_handle->fileaccess & GENERIC_WRITE) &&
180 !(file_handle->fileaccess & GENERIC_ALL)) {
181 DEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
182 SetLastError (ERROR_ACCESS_DENIED);
186 #ifdef HAVE_LARGE_FILE_SUPPORT
187 offset = ((gint64)offset_high << 32) | offset_low;
188 length = ((gint64)length_high << 32) | length_low;
190 DEBUG ("%s: Unlocking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
195 DEBUG ("%s: Unlocking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
198 return(_wapi_unlock_file_region (fd, offset, length));