xref: /aoo41x/main/sal/osl/w32/file_error.c (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #define UNICODE
29 #define _UNICODE
30 #define _WIN32_WINNT_0x0500
31 #include "systools/win32/uwinapi.h"
32 
33 #include "file_error.h"
34 
35 #include "osl/diagnose.h"
36 #include "osl/thread.h"
37 
38 /* OS error to oslFileError values mapping table */
39 struct osl_file_error_entry
40 {
41 	unsigned long oscode; /* OS return value */
42 	int errnocode;        /* oslFileError code */
43 };
44 
45 static const struct osl_file_error_entry errtable[] = {
46   {  ERROR_SUCCESS,				   osl_File_E_None     },  /* 0 */
47   {  ERROR_INVALID_FUNCTION,       osl_File_E_INVAL    },  /* 1 */
48   {  ERROR_FILE_NOT_FOUND,         osl_File_E_NOENT    },  /* 2 */
49   {  ERROR_PATH_NOT_FOUND,         osl_File_E_NOENT    },  /* 3 */
50   {  ERROR_TOO_MANY_OPEN_FILES,    osl_File_E_MFILE    },  /* 4 */
51   {  ERROR_ACCESS_DENIED,          osl_File_E_ACCES    },  /* 5 */
52   {  ERROR_INVALID_HANDLE,         osl_File_E_BADF     },  /* 6 */
53   {  ERROR_ARENA_TRASHED,          osl_File_E_NOMEM    },  /* 7 */
54   {  ERROR_NOT_ENOUGH_MEMORY,      osl_File_E_NOMEM    },  /* 8 */
55   {  ERROR_INVALID_BLOCK,          osl_File_E_NOMEM    },  /* 9 */
56   {  ERROR_BAD_ENVIRONMENT,        osl_File_E_2BIG     },  /* 10 */
57   {  ERROR_BAD_FORMAT,             osl_File_E_NOEXEC   },  /* 11 */
58   {  ERROR_INVALID_ACCESS,         osl_File_E_INVAL    },  /* 12 */
59   {  ERROR_INVALID_DATA,           osl_File_E_INVAL    },  /* 13 */
60   {  ERROR_INVALID_DRIVE,          osl_File_E_NOENT    },  /* 15 */
61   {  ERROR_CURRENT_DIRECTORY,      osl_File_E_ACCES    },  /* 16 */
62   {  ERROR_NOT_SAME_DEVICE,        osl_File_E_XDEV     },  /* 17 */
63   {  ERROR_NO_MORE_FILES,          osl_File_E_NOENT    },  /* 18 */
64   {  ERROR_NOT_READY,              osl_File_E_NOTREADY },  /* 21 */
65   {  ERROR_LOCK_VIOLATION,         osl_File_E_ACCES    },  /* 33 */
66   {  ERROR_BAD_NETPATH,            osl_File_E_NOENT    },  /* 53 */
67   {  ERROR_NETWORK_ACCESS_DENIED,  osl_File_E_ACCES    },  /* 65 */
68   {  ERROR_BAD_NET_NAME,           osl_File_E_NOENT    },  /* 67 */
69   {  ERROR_FILE_EXISTS,            osl_File_E_EXIST    },  /* 80 */
70   {  ERROR_CANNOT_MAKE,            osl_File_E_ACCES    },  /* 82 */
71   {  ERROR_FAIL_I24,               osl_File_E_ACCES    },  /* 83 */
72   {  ERROR_INVALID_PARAMETER,      osl_File_E_INVAL    },  /* 87 */
73   {  ERROR_NO_PROC_SLOTS,          osl_File_E_AGAIN    },  /* 89 */
74   {  ERROR_DRIVE_LOCKED,           osl_File_E_ACCES    },  /* 108 */
75   {  ERROR_BROKEN_PIPE,            osl_File_E_PIPE     },  /* 109 */
76   {  ERROR_DISK_FULL,              osl_File_E_NOSPC    },  /* 112 */
77   {  ERROR_INVALID_TARGET_HANDLE,  osl_File_E_BADF     },  /* 114 */
78   {  ERROR_INVALID_HANDLE,         osl_File_E_INVAL    },  /* 124 */
79   {  ERROR_WAIT_NO_CHILDREN,       osl_File_E_CHILD    },  /* 128 */
80   {  ERROR_CHILD_NOT_COMPLETE,     osl_File_E_CHILD    },  /* 129 */
81   {  ERROR_DIRECT_ACCESS_HANDLE,   osl_File_E_BADF     },  /* 130 */
82   {  ERROR_NEGATIVE_SEEK,          osl_File_E_INVAL    },  /* 131 */
83   {  ERROR_SEEK_ON_DEVICE,         osl_File_E_ACCES    },  /* 132 */
84   {  ERROR_DIR_NOT_EMPTY,          osl_File_E_NOTEMPTY },  /* 145 */
85   {  ERROR_NOT_LOCKED,             osl_File_E_ACCES    },  /* 158 */
86   {  ERROR_BAD_PATHNAME,           osl_File_E_NOENT    },  /* 161 */
87   {  ERROR_MAX_THRDS_REACHED,      osl_File_E_AGAIN    },  /* 164 */
88   {  ERROR_LOCK_FAILED,            osl_File_E_ACCES    },  /* 167 */
89   {  ERROR_ALREADY_EXISTS,         osl_File_E_EXIST    },  /* 183 */
90   {  ERROR_FILENAME_EXCED_RANGE,   osl_File_E_NOENT    },  /* 206 */
91   {  ERROR_NESTING_NOT_ALLOWED,    osl_File_E_AGAIN    },  /* 215 */
92   {  ERROR_DIRECTORY,              osl_File_E_NOENT    },  /* 267 */
93   {  ERROR_NOT_ENOUGH_QUOTA,       osl_File_E_NOMEM    },  /* 1816 */
94   {  ERROR_UNEXP_NET_ERR,          osl_File_E_NETWORK  }   /* 59 */
95 };
96 
97 /* The following two constants must be the minimum and maximum
98    values in the (contiguous) range of osl_File_E_xec Failure errors.
99 */
100 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
101 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
102 
103 /* These are the low and high value in the range of errors that are
104    access violations
105 */
106 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
107 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
108 
109 oslFileError oslTranslateFileError (/*DWORD*/ unsigned long dwError)
110 {
111 	static const int n = sizeof(errtable)/sizeof(errtable[0]);
112 
113 	int i;
114 	for (i = 0; i < n; ++i )
115 	{
116 		if (dwError == errtable[i].oscode)
117 			return (oslFileError)(errtable[i].errnocode);
118 	}
119 
120 	/* The error code wasn't in the table.  We check for a range of
121 	   osl_File_E_ACCES errors or exec failure errors (ENOEXEC).
122 	   Otherwise osl_File_E_INVAL is returned.
123 	*/
124 	if ( (dwError >= MIN_EACCES_RANGE) && (dwError <= MAX_EACCES_RANGE) )
125 		return osl_File_E_ACCES;
126 	else if ( (dwError >= MIN_EXEC_ERROR) && (dwError <= MAX_EXEC_ERROR) )
127 		return osl_File_E_NOEXEC;
128 	else
129 		return osl_File_E_INVAL;
130 }
131 
132 //#####################################################
133 #if OSL_DEBUG_LEVEL > 0
134 void _osl_warnFile( const char *message, rtl_uString *ustrFile )
135 {
136 	char szBuffer[2048];
137 
138 	if (ustrFile)
139 	{
140 		rtl_String	*strFile = NULL;
141 
142 		rtl_uString2String( &strFile, rtl_uString_getStr( ustrFile ), rtl_uString_getLength( ustrFile ),
143 							osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
144 		snprintf( szBuffer, sizeof(szBuffer), message, strFile->buffer );
145 		rtl_string_release( strFile );
146 
147 		message = szBuffer;
148 	}
149 	OSL_ENSURE( 0, message );
150 }
151 #endif /* OSL_DEBUG_LEVEL */
152