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 struct flock lock_data
;
29 lock_data
.l_type
= F_WRLCK
;
30 lock_data
.l_whence
= SEEK_SET
;
31 lock_data
.l_start
= offset
;
32 lock_data
.l_len
= length
;
35 ret
= fcntl (fd
, F_SETLK
, &lock_data
);
36 } while(ret
== -1 && errno
== EINTR
);
38 LOGDEBUG ("%s: fcntl returns %d", __func__
, ret
);
42 * if locks are not available (NFS for example),
47 || errno
== EOPNOTSUPP
56 SetLastError (ERROR_LOCK_VIOLATION
);
64 _wapi_unlock_file_region (int fd
, off_t offset
, off_t length
)
66 struct flock lock_data
;
69 lock_data
.l_type
= F_UNLCK
;
70 lock_data
.l_whence
= SEEK_SET
;
71 lock_data
.l_start
= offset
;
72 lock_data
.l_len
= length
;
75 ret
= fcntl (fd
, F_SETLK
, &lock_data
);
76 } while(ret
== -1 && errno
== EINTR
);
78 LOGDEBUG ("%s: fcntl returns %d", __func__
, ret
);
82 * if locks are not available (NFS for example),
87 || errno
== EOPNOTSUPP
96 SetLastError (ERROR_LOCK_VIOLATION
);
104 LockFile (gpointer handle
, guint32 offset_low
, guint32 offset_high
,
105 guint32 length_low
, guint32 length_high
)
107 struct _WapiHandle_file
*file_handle
;
109 off_t offset
, length
;
110 int fd
= GPOINTER_TO_UINT(handle
);
112 ok
= _wapi_lookup_handle (handle
, WAPI_HANDLE_FILE
,
113 (gpointer
*)&file_handle
);
115 g_warning ("%s: error looking up file handle %p", __func__
,
117 SetLastError (ERROR_INVALID_HANDLE
);
121 if (!(file_handle
->fileaccess
& GENERIC_READ
) &&
122 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
123 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
124 LOGDEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__
, handle
, file_handle
->fileaccess
);
125 SetLastError (ERROR_ACCESS_DENIED
);
129 #ifdef HAVE_LARGE_FILE_SUPPORT
130 offset
= ((gint64
)offset_high
<< 32) | offset_low
;
131 length
= ((gint64
)length_high
<< 32) | length_low
;
133 LOGDEBUG ("%s: Locking handle %p, offset %lld, length %lld", __func__
, handle
, offset
, length
);
139 g_message ("%s: Locking handle %p, offset %ld, length %ld", __func__
,
140 handle
, offset
, length
);
144 return(_wapi_lock_file_region (fd
, offset
, length
));
148 UnlockFile (gpointer handle
, guint32 offset_low
,
149 guint32 offset_high
, guint32 length_low
,
152 struct _WapiHandle_file
*file_handle
;
154 off_t offset
, length
;
155 int fd
= GPOINTER_TO_UINT(handle
);
157 ok
= _wapi_lookup_handle (handle
, WAPI_HANDLE_FILE
,
158 (gpointer
*)&file_handle
);
160 g_warning ("%s: error looking up file handle %p", __func__
,
162 SetLastError (ERROR_INVALID_HANDLE
);
166 if (!(file_handle
->fileaccess
& GENERIC_READ
) &&
167 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
168 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
169 LOGDEBUG ("%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__
, handle
, file_handle
->fileaccess
);
170 SetLastError (ERROR_ACCESS_DENIED
);
174 #ifdef HAVE_LARGE_FILE_SUPPORT
175 offset
= ((gint64
)offset_high
<< 32) | offset_low
;
176 length
= ((gint64
)length_high
<< 32) | length_low
;
178 LOGDEBUG ("%s: Unlocking handle %p, offset %lld, length %lld", __func__
, handle
, offset
, length
);
183 LOGDEBUG ("%s: Unlocking handle %p, offset %ld, length %ld", __func__
, handle
, offset
, length
);
186 return(_wapi_unlock_file_region (fd
, offset
, length
));