Back to index

lightning-sunbird  0.9+nobinonly
win32_errors.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "prerror.h"
00039 #include "prlog.h"
00040 #include <errno.h>
00041 #include <windows.h>
00042 
00043 /*
00044  * On Win32, we map three kinds of error codes:
00045  * - GetLastError(): for Win32 functions
00046  * - WSAGetLastError(): for Winsock functions
00047  * - errno: for standard C library functions
00048  * 
00049  * GetLastError() and WSAGetLastError() return error codes in
00050  * non-overlapping ranges, so their error codes (ERROR_* and
00051  * WSAE*) can be mapped by the same function.  On the other hand,
00052  * errno and GetLastError() have overlapping ranges, so we need
00053  * to use a separate function to map errno.
00054  *
00055  * We do not check for WSAEINPROGRESS and WSAEINTR because we do not
00056  * use blocking Winsock 1.1 calls.
00057  *
00058  * Except for the 'socket' call, we do not check for WSAEINITIALISED.
00059  * It is assumed that if Winsock is not initialized, that fact will
00060  * be detected at the time we create new sockets.
00061  */
00062 
00063 static void _MD_win32_map_default_errno(PRInt32 err)
00064 {
00065     PRErrorCode prError;
00066 
00067     switch (err) {
00068         case EACCES:
00069             prError = PR_NO_ACCESS_RIGHTS_ERROR;
00070             break;
00071         case ENOENT:
00072             prError = PR_FILE_NOT_FOUND_ERROR;
00073             break;
00074         default:
00075             prError = PR_UNKNOWN_ERROR;
00076             break;
00077     }
00078     PR_SetError(prError, err);
00079 }
00080 
00081 void _MD_win32_map_default_error(PRInt32 err)
00082 {
00083     PRErrorCode prError;
00084 
00085     switch (err) {
00086         case ERROR_ACCESS_DENIED:
00087             prError = PR_NO_ACCESS_RIGHTS_ERROR;
00088             break;
00089         case ERROR_ALREADY_EXISTS:
00090             prError = PR_FILE_EXISTS_ERROR;
00091             break;
00092         case ERROR_CALL_NOT_IMPLEMENTED:
00093             prError = PR_NOT_IMPLEMENTED_ERROR;
00094             break;
00095         case ERROR_DISK_CORRUPT:
00096             prError = PR_IO_ERROR; 
00097             break;
00098         case ERROR_DISK_FULL:
00099             prError = PR_NO_DEVICE_SPACE_ERROR;
00100             break;
00101         case ERROR_DISK_OPERATION_FAILED:
00102             prError = PR_IO_ERROR;
00103             break;
00104         case ERROR_DRIVE_LOCKED:
00105             prError = PR_FILE_IS_LOCKED_ERROR;
00106             break;
00107         case ERROR_FILENAME_EXCED_RANGE:
00108             prError = PR_NAME_TOO_LONG_ERROR;
00109             break;
00110         case ERROR_FILE_CORRUPT:
00111             prError = PR_IO_ERROR;
00112             break;
00113         case ERROR_FILE_EXISTS:
00114             prError = PR_FILE_EXISTS_ERROR;
00115             break;
00116         case ERROR_FILE_INVALID:
00117             prError = PR_BAD_DESCRIPTOR_ERROR;
00118             break;
00119         case ERROR_FILE_NOT_FOUND:
00120             prError = PR_FILE_NOT_FOUND_ERROR;
00121             break;
00122         case ERROR_HANDLE_DISK_FULL:
00123             prError = PR_NO_DEVICE_SPACE_ERROR;
00124             break;
00125         case ERROR_INVALID_ADDRESS:
00126             prError = PR_ACCESS_FAULT_ERROR;
00127             break;
00128         case ERROR_INVALID_HANDLE:
00129             prError = PR_BAD_DESCRIPTOR_ERROR;
00130             break;
00131         case ERROR_INVALID_NAME:
00132             prError = PR_INVALID_ARGUMENT_ERROR;
00133             break;
00134         case ERROR_INVALID_PARAMETER:
00135             prError = PR_INVALID_ARGUMENT_ERROR;
00136             break;
00137         case ERROR_INVALID_USER_BUFFER:
00138             prError = PR_INSUFFICIENT_RESOURCES_ERROR;
00139             break;
00140         case ERROR_LOCKED:
00141             prError = PR_FILE_IS_LOCKED_ERROR;
00142             break;
00143         case ERROR_NETNAME_DELETED:
00144             prError = PR_CONNECT_RESET_ERROR;
00145             break;
00146         case ERROR_NOACCESS:
00147             prError = PR_ACCESS_FAULT_ERROR;
00148             break;
00149         case ERROR_NOT_ENOUGH_MEMORY:
00150             prError = PR_INSUFFICIENT_RESOURCES_ERROR;
00151             break;
00152         case ERROR_NOT_ENOUGH_QUOTA:
00153             prError = PR_OUT_OF_MEMORY_ERROR;
00154             break;
00155         case ERROR_NOT_READY:
00156             prError = PR_IO_ERROR;
00157             break;
00158         case ERROR_NO_MORE_FILES:
00159             prError = PR_NO_MORE_FILES_ERROR;
00160             break;
00161         case ERROR_OPEN_FAILED:
00162             prError = PR_IO_ERROR;
00163             break;
00164         case ERROR_OPEN_FILES:
00165             prError = PR_IO_ERROR;
00166             break;
00167         case ERROR_OPERATION_ABORTED:
00168             prError = PR_OPERATION_ABORTED_ERROR;
00169             break;
00170         case ERROR_OUTOFMEMORY:
00171             prError = PR_INSUFFICIENT_RESOURCES_ERROR;
00172             break;
00173         case ERROR_PATH_BUSY:
00174             prError = PR_IO_ERROR;
00175             break;
00176         case ERROR_PATH_NOT_FOUND:
00177             prError = PR_FILE_NOT_FOUND_ERROR;
00178             break;
00179         case ERROR_SEEK_ON_DEVICE:
00180             prError = PR_IO_ERROR;
00181             break;
00182         case ERROR_SHARING_VIOLATION:
00183             prError = PR_FILE_IS_BUSY_ERROR;
00184             break;
00185         case ERROR_STACK_OVERFLOW:
00186             prError = PR_ACCESS_FAULT_ERROR;
00187             break;
00188         case ERROR_TOO_MANY_OPEN_FILES:
00189             prError = PR_SYS_DESC_TABLE_FULL_ERROR;
00190             break;
00191         case ERROR_WRITE_PROTECT:
00192             prError = PR_NO_ACCESS_RIGHTS_ERROR;
00193             break;
00194         case WSAEACCES:
00195             prError = PR_NO_ACCESS_RIGHTS_ERROR;
00196             break;
00197         case WSAEADDRINUSE:
00198             prError = PR_ADDRESS_IN_USE_ERROR;
00199             break;
00200         case WSAEADDRNOTAVAIL:
00201             prError = PR_ADDRESS_NOT_AVAILABLE_ERROR;
00202             break;
00203         case WSAEAFNOSUPPORT:
00204             prError = PR_ADDRESS_NOT_SUPPORTED_ERROR;
00205             break;
00206         case WSAEALREADY:
00207             prError = PR_ALREADY_INITIATED_ERROR;
00208             break;
00209         case WSAEBADF:
00210             prError = PR_BAD_DESCRIPTOR_ERROR;
00211             break;
00212         case WSAECONNABORTED:
00213             prError = PR_CONNECT_ABORTED_ERROR;
00214             break;
00215         case WSAECONNREFUSED:
00216             prError = PR_CONNECT_REFUSED_ERROR;
00217             break;
00218         case WSAECONNRESET:
00219             prError = PR_CONNECT_RESET_ERROR;
00220             break;
00221         case WSAEDESTADDRREQ:
00222             prError = PR_INVALID_ARGUMENT_ERROR;
00223             break;
00224         case WSAEFAULT:
00225             prError = PR_ACCESS_FAULT_ERROR;
00226             break;
00227         case WSAEHOSTUNREACH:
00228             prError = PR_HOST_UNREACHABLE_ERROR;
00229             break;
00230         case WSAEINVAL:
00231             prError = PR_INVALID_ARGUMENT_ERROR;
00232             break;
00233         case WSAEISCONN:
00234             prError = PR_IS_CONNECTED_ERROR;
00235             break;
00236         case WSAEMFILE:
00237             prError = PR_PROC_DESC_TABLE_FULL_ERROR;
00238             break;
00239         case WSAEMSGSIZE:
00240             prError = PR_BUFFER_OVERFLOW_ERROR;
00241             break;
00242         case WSAENETDOWN:
00243             prError = PR_NETWORK_DOWN_ERROR;
00244             break;
00245         case WSAENETRESET:
00246             prError = PR_CONNECT_ABORTED_ERROR;
00247             break;
00248         case WSAENETUNREACH:
00249             prError = PR_NETWORK_UNREACHABLE_ERROR;
00250             break;
00251         case WSAENOBUFS:
00252             prError = PR_INSUFFICIENT_RESOURCES_ERROR;
00253             break;
00254         case WSAENOPROTOOPT:
00255             prError = PR_INVALID_ARGUMENT_ERROR;
00256             break;
00257         case WSAENOTCONN:
00258             prError = PR_NOT_CONNECTED_ERROR;
00259             break;
00260         case WSAENOTSOCK:
00261             prError = PR_NOT_SOCKET_ERROR;
00262             break;
00263         case WSAEOPNOTSUPP:
00264             prError = PR_OPERATION_NOT_SUPPORTED_ERROR;
00265             break;
00266         case WSAEPROTONOSUPPORT:
00267             prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR;
00268             break;
00269         case WSAEPROTOTYPE:
00270             prError = PR_INVALID_ARGUMENT_ERROR;
00271             break;
00272         case WSAESHUTDOWN:
00273             prError = PR_SOCKET_SHUTDOWN_ERROR;
00274             break;
00275         case WSAESOCKTNOSUPPORT:
00276             prError = PR_INVALID_ARGUMENT_ERROR;
00277             break;
00278         case WSAETIMEDOUT:
00279             prError = PR_CONNECT_ABORTED_ERROR;
00280             break;
00281         case WSAEWOULDBLOCK:
00282             prError = PR_WOULD_BLOCK_ERROR;
00283             break;
00284         default:
00285             prError = PR_UNKNOWN_ERROR;
00286             break;
00287     }
00288     PR_SetError(prError, err);
00289 }
00290 
00291 void _MD_win32_map_opendir_error(PRInt32 err)
00292 {
00293     _MD_win32_map_default_error(err);
00294 }
00295 
00296 void _MD_win32_map_closedir_error(PRInt32 err)
00297 {
00298     _MD_win32_map_default_error(err);
00299 }
00300 
00301 void _MD_unix_readdir_error(PRInt32 err)
00302 {
00303     _MD_win32_map_default_error(err);
00304 }
00305 
00306 void _MD_win32_map_delete_error(PRInt32 err)
00307 {
00308     _MD_win32_map_default_error(err);
00309 }
00310 
00311 /* The error code for stat() is in errno. */
00312 void _MD_win32_map_stat_error(PRInt32 err)
00313 {
00314     _MD_win32_map_default_errno(err);
00315 }
00316 
00317 void _MD_win32_map_fstat_error(PRInt32 err)
00318 {
00319     _MD_win32_map_default_error(err);
00320 }
00321 
00322 void _MD_win32_map_rename_error(PRInt32 err)
00323 {
00324     _MD_win32_map_default_error(err);
00325 }
00326 
00327 /* The error code for access() is in errno. */
00328 void _MD_win32_map_access_error(PRInt32 err)
00329 {
00330     _MD_win32_map_default_errno(err);
00331 }
00332 
00333 void _MD_win32_map_mkdir_error(PRInt32 err)
00334 {
00335     _MD_win32_map_default_error(err);
00336 }
00337 
00338 void _MD_win32_map_rmdir_error(PRInt32 err)
00339 {
00340     _MD_win32_map_default_error(err);
00341 }
00342 
00343 void _MD_win32_map_read_error(PRInt32 err)
00344 {
00345     _MD_win32_map_default_error(err);
00346 }
00347 
00348 void _MD_win32_map_transmitfile_error(PRInt32 err)
00349 {
00350     _MD_win32_map_default_error(err);
00351 }
00352 
00353 void _MD_win32_map_write_error(PRInt32 err)
00354 {
00355     _MD_win32_map_default_error(err);
00356 }
00357 
00358 void _MD_win32_map_lseek_error(PRInt32 err)
00359 {
00360     _MD_win32_map_default_error(err);
00361 }
00362 
00363 void _MD_win32_map_fsync_error(PRInt32 err)
00364 {
00365     _MD_win32_map_default_error(err);
00366 }
00367 
00368 /*
00369  * For both CloseHandle() and closesocket().
00370  */
00371 void _MD_win32_map_close_error(PRInt32 err)
00372 {
00373     _MD_win32_map_default_error(err);
00374 }
00375 
00376 void _MD_win32_map_socket_error(PRInt32 err)
00377 {
00378     PR_ASSERT(err != WSANOTINITIALISED);
00379     _MD_win32_map_default_error(err);
00380 }
00381 
00382 void _MD_win32_map_recv_error(PRInt32 err)
00383 {
00384     _MD_win32_map_default_error(err);
00385 }
00386 
00387 void _MD_win32_map_recvfrom_error(PRInt32 err)
00388 {
00389     _MD_win32_map_default_error(err);
00390 }
00391 
00392 void _MD_win32_map_send_error(PRInt32 err)
00393 {
00394     PRErrorCode prError;
00395 
00396     switch (err) {
00397         case WSAEMSGSIZE:
00398             prError = PR_INVALID_ARGUMENT_ERROR;
00399             break;
00400         default:
00401             _MD_win32_map_default_error(err);
00402             return;
00403     }
00404     PR_SetError(prError, err);
00405 }
00406 
00407 void _MD_win32_map_sendto_error(PRInt32 err)
00408 {
00409     PRErrorCode prError;
00410 
00411     switch (err) {
00412         case WSAEMSGSIZE:
00413             prError = PR_INVALID_ARGUMENT_ERROR;
00414             break;
00415         default:
00416             _MD_win32_map_default_error(err);
00417             return;
00418     }
00419     PR_SetError(prError, err);
00420 }
00421 
00422 void _MD_win32_map_accept_error(PRInt32 err)
00423 {
00424     PRErrorCode prError;
00425 
00426     switch (err) {
00427         case WSAEOPNOTSUPP:
00428             prError = PR_NOT_TCP_SOCKET_ERROR;
00429             break;
00430         case WSAEINVAL:
00431             prError = PR_INVALID_STATE_ERROR;
00432             break;
00433         default:
00434             _MD_win32_map_default_error(err);
00435             return;
00436     }
00437     PR_SetError(prError, err);
00438 }
00439 
00440 void _MD_win32_map_acceptex_error(PRInt32 err)
00441 {
00442     _MD_win32_map_default_error(err);
00443 }
00444 
00445 void _MD_win32_map_connect_error(PRInt32 err)
00446 {
00447     PRErrorCode prError;
00448 
00449     switch (err) {
00450         case WSAEWOULDBLOCK:
00451             prError = PR_IN_PROGRESS_ERROR;
00452             break;
00453         case WSAEINVAL:
00454             prError = PR_ALREADY_INITIATED_ERROR;
00455             break;
00456         case WSAETIMEDOUT:
00457             prError = PR_IO_TIMEOUT_ERROR;
00458             break;
00459         default:
00460             _MD_win32_map_default_error(err);
00461             return;
00462     }
00463     PR_SetError(prError, err);
00464 }
00465 
00466 void _MD_win32_map_bind_error(PRInt32 err)
00467 {
00468     PRErrorCode prError;
00469 
00470     switch (err) {
00471         case WSAEINVAL:
00472             prError = PR_SOCKET_ADDRESS_IS_BOUND_ERROR;
00473             break;
00474         default:
00475             _MD_win32_map_default_error(err);
00476             return;
00477     }
00478     PR_SetError(prError, err);
00479 }
00480 
00481 void _MD_win32_map_listen_error(PRInt32 err)
00482 {
00483     PRErrorCode prError;
00484 
00485     switch (err) {
00486         case WSAEOPNOTSUPP:
00487             prError = PR_NOT_TCP_SOCKET_ERROR;
00488             break;
00489         case WSAEINVAL:
00490             prError = PR_INVALID_STATE_ERROR;
00491             break;
00492         default:
00493             _MD_win32_map_default_error(err);
00494             return;
00495     }
00496     PR_SetError(prError, err);
00497 }
00498 
00499 void _MD_win32_map_shutdown_error(PRInt32 err)
00500 {
00501     _MD_win32_map_default_error(err);
00502 }
00503 
00504 void _MD_win32_map_getsockname_error(PRInt32 err)
00505 {
00506     PRErrorCode prError;
00507 
00508     switch (err) {
00509         case WSAEINVAL:
00510             prError = PR_INVALID_STATE_ERROR;
00511             break;
00512         default:
00513             _MD_win32_map_default_error(err);
00514             return;
00515     }
00516     PR_SetError(prError, err);
00517 }
00518 
00519 void _MD_win32_map_getpeername_error(PRInt32 err)
00520 {
00521     _MD_win32_map_default_error(err);
00522 }
00523 
00524 void _MD_win32_map_getsockopt_error(PRInt32 err)
00525 {
00526     _MD_win32_map_default_error(err);
00527 }
00528 
00529 void _MD_win32_map_setsockopt_error(PRInt32 err)
00530 {
00531     _MD_win32_map_default_error(err);
00532 }
00533 
00534 void _MD_win32_map_open_error(PRInt32 err)
00535 {
00536     _MD_win32_map_default_error(err);
00537 }
00538 
00539 void _MD_win32_map_gethostname_error(PRInt32 err)
00540 {
00541     _MD_win32_map_default_error(err);
00542 }
00543 
00544 /* Win32 select() only works on sockets.  So in this
00545 ** context, WSAENOTSOCK is equivalent to EBADF on Unix.  
00546 */
00547 void _MD_win32_map_select_error(PRInt32 err)
00548 {
00549     PRErrorCode prError;
00550 
00551     switch (err) {
00552         case WSAENOTSOCK:
00553             prError = PR_BAD_DESCRIPTOR_ERROR;
00554             break;
00555         default:
00556             _MD_win32_map_default_error(err);
00557             return;
00558     }
00559     PR_SetError(prError, err);
00560 }
00561 
00562 void _MD_win32_map_lockf_error(PRInt32 err)
00563 {
00564     _MD_win32_map_default_error(err);
00565 }