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 if (offset
< 0 || length
< 0) {
38 SetLastError (ERROR_INVALID_PARAMETER
);
42 lock_data
.l_type
= F_WRLCK
;
43 lock_data
.l_whence
= SEEK_SET
;
44 lock_data
.l_start
= offset
;
45 lock_data
.l_len
= length
;
48 ret
= fcntl (fd
, F_SETLK
, &lock_data
);
49 } while(ret
== -1 && errno
== EINTR
);
51 DEBUG ("%s: fcntl returns %d", __func__
, ret
);
55 * if locks are not available (NFS for example),
60 || errno
== EOPNOTSUPP
69 SetLastError (ERROR_LOCK_VIOLATION
);
74 #endif /* __native_client__ */
78 _wapi_unlock_file_region (int fd
, off_t offset
, off_t length
)
80 #if defined(__native_client__)
81 printf("WARNING: locking.c: _wapi_unlock_file_region(): fcntl() not available on Native Client!\n");
84 struct flock lock_data
;
87 lock_data
.l_type
= F_UNLCK
;
88 lock_data
.l_whence
= SEEK_SET
;
89 lock_data
.l_start
= offset
;
90 lock_data
.l_len
= length
;
93 ret
= fcntl (fd
, F_SETLK
, &lock_data
);
94 } while(ret
== -1 && errno
== EINTR
);
96 DEBUG ("%s: fcntl returns %d", __func__
, ret
);
100 * if locks are not available (NFS for example),
105 || errno
== EOPNOTSUPP
114 SetLastError (ERROR_LOCK_VIOLATION
);
119 #endif /* __native_client__ */
123 LockFile (gpointer handle
, guint32 offset_low
, guint32 offset_high
,
124 guint32 length_low
, guint32 length_high
)
126 struct _WapiHandle_file
*file_handle
;
128 off_t offset
, length
;
129 int fd
= GPOINTER_TO_UINT(handle
);
131 ok
= _wapi_lookup_handle (handle
, WAPI_HANDLE_FILE
,
132 (gpointer
*)&file_handle
);
134 g_warning ("%s: error looking up file handle %p", __func__
,
136 SetLastError (ERROR_INVALID_HANDLE
);
140 if (!(file_handle
->fileaccess
& GENERIC_READ
) &&
141 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
142 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
143 DEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__
, handle
, file_handle
->fileaccess
);
144 SetLastError (ERROR_ACCESS_DENIED
);
148 #ifdef HAVE_LARGE_FILE_SUPPORT
149 offset
= ((gint64
)offset_high
<< 32) | offset_low
;
150 length
= ((gint64
)length_high
<< 32) | length_low
;
152 DEBUG ("%s: Locking handle %p, offset %lld, length %lld", __func__
, handle
, offset
, length
);
154 if (offset_high
> 0 || length_high
> 0) {
155 SetLastError (ERROR_INVALID_PARAMETER
);
161 DEBUG ("%s: Locking handle %p, offset %ld, length %ld", __func__
,
162 handle
, offset
, length
);
165 return(_wapi_lock_file_region (fd
, offset
, length
));
169 UnlockFile (gpointer handle
, guint32 offset_low
,
170 guint32 offset_high
, guint32 length_low
,
173 struct _WapiHandle_file
*file_handle
;
175 off_t offset
, length
;
176 int fd
= GPOINTER_TO_UINT(handle
);
178 ok
= _wapi_lookup_handle (handle
, WAPI_HANDLE_FILE
,
179 (gpointer
*)&file_handle
);
181 g_warning ("%s: error looking up file handle %p", __func__
,
183 SetLastError (ERROR_INVALID_HANDLE
);
187 if (!(file_handle
->fileaccess
& GENERIC_READ
) &&
188 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
189 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
190 DEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__
, handle
, file_handle
->fileaccess
);
191 SetLastError (ERROR_ACCESS_DENIED
);
195 #ifdef HAVE_LARGE_FILE_SUPPORT
196 offset
= ((gint64
)offset_high
<< 32) | offset_low
;
197 length
= ((gint64
)length_high
<< 32) | length_low
;
199 DEBUG ("%s: Unlocking handle %p, offset %lld, length %lld", __func__
, handle
, offset
, length
);
204 DEBUG ("%s: Unlocking handle %p, offset %ld, length %ld", __func__
, handle
, offset
, length
);
207 return(_wapi_unlock_file_region (fd
, offset
, length
));