1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include "systools/win32/uwinapi.h"
26 #include "file_error.h"
28 #include "path_helper.hxx"
30 #include "osl/diagnose.h"
35 //#####################################################
36 // Allocate n number of t's on the stack return a pointer to it in p
38 #define STACK_ALLOC(p, t, n) (p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));
40 #define STACK_ALLOC(p, t, n) __try {(p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));} \
41 __except(EXCEPTION_EXECUTE_HANDLER) {(p) = 0;}
44 extern "C" oslFileHandle SAL_CALL
osl_createFileHandleFromOSHandle(HANDLE hFile
, sal_uInt32 uFlags
);
46 //#####################################################
47 // Temp file functions
48 //#####################################################
50 static oslFileError
osl_setup_base_directory_impl_(
51 rtl_uString
* pustrDirectoryURL
,
52 rtl_uString
** ppustr_base_dir
)
54 rtl_uString
* dir_url
= 0;
56 oslFileError error
= osl_File_E_None
;
58 if (pustrDirectoryURL
)
59 rtl_uString_assign(&dir_url
, pustrDirectoryURL
);
61 error
= osl_getTempDirURL(&dir_url
);
63 if (osl_File_E_None
== error
)
65 error
= _osl_getSystemPathFromFileURL(dir_url
, &dir
, sal_False
);
66 rtl_uString_release(dir_url
);
69 if (osl_File_E_None
== error
)
71 rtl_uString_assign(ppustr_base_dir
, dir
);
72 rtl_uString_release(dir
);
78 //#####################################################
79 static oslFileError
osl_setup_createTempFile_impl_(
80 rtl_uString
* pustrDirectoryURL
,
81 oslFileHandle
* pHandle
,
82 rtl_uString
** ppustrTempFileURL
,
83 rtl_uString
** ppustr_base_dir
,
84 sal_Bool
* b_delete_on_close
)
86 oslFileError osl_error
;
88 OSL_PRECOND(((0 != pHandle
) || (0 != ppustrTempFileURL
)), "Invalid parameter!");
90 if ((0 == pHandle
) && (0 == ppustrTempFileURL
))
92 osl_error
= osl_File_E_INVAL
;
96 osl_error
= osl_setup_base_directory_impl_(
97 pustrDirectoryURL
, ppustr_base_dir
);
99 *b_delete_on_close
= (sal_Bool
)(0 == ppustrTempFileURL
);
105 //#####################################################
106 static oslFileError
osl_win32_GetTempFileName_impl_(
107 rtl_uString
* base_directory
, LPWSTR temp_file_name
)
109 oslFileError osl_error
= osl_File_E_None
;
111 if (0 == GetTempFileNameW(
112 reinterpret_cast<LPCWSTR
>(rtl_uString_getStr(base_directory
)),
117 osl_error
= oslTranslateFileError(GetLastError());
123 //#####################################################
124 static sal_Bool
osl_win32_CreateFile_impl_(
125 LPCWSTR file_name
, sal_Bool b_delete_on_close
, oslFileHandle
* p_handle
)
127 DWORD flags
= FILE_ATTRIBUTE_NORMAL
;
130 OSL_ASSERT(p_handle
);
132 if (b_delete_on_close
)
133 flags
|= FILE_FLAG_DELETE_ON_CLOSE
;
137 GENERIC_READ
| GENERIC_WRITE
,
144 // @@@ ERROR HANDLING @@@
145 if (IsValidHandle(hFile
))
146 *p_handle
= osl_createFileHandleFromOSHandle(hFile
, osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
148 return (sal_Bool
)IsValidHandle(hFile
);
151 //#############################################
152 static oslFileError
osl_createTempFile_impl_(
153 rtl_uString
* base_directory
,
155 sal_Bool b_delete_on_close
,
156 oslFileHandle
* pHandle
,
157 rtl_uString
** ppustrTempFileURL
)
159 oslFileError osl_error
;
163 osl_error
= osl_win32_GetTempFileName_impl_(base_directory
, tmp_name
);
165 /* if file could not be opened try again */
167 if ((osl_File_E_None
!= osl_error
) || (0 == pHandle
) ||
168 osl_win32_CreateFile_impl_(tmp_name
, b_delete_on_close
, pHandle
))
171 } while(1); // try until success
173 if ((osl_File_E_None
== osl_error
) && !b_delete_on_close
)
175 rtl_uString
* pustr
= 0;
176 rtl_uString_newFromStr(&pustr
, reinterpret_cast<const sal_Unicode
*>(tmp_name
));
177 osl_getFileURLFromSystemPath(pustr
, ppustrTempFileURL
);
178 rtl_uString_release(pustr
);
184 //#############################################
185 oslFileError SAL_CALL
osl_createTempFile(
186 rtl_uString
* pustrDirectoryURL
,
187 oslFileHandle
* pHandle
,
188 rtl_uString
** ppustrTempFileURL
)
190 rtl_uString
* base_directory
= 0;
192 sal_Bool b_delete_on_close
;
193 oslFileError osl_error
;
195 osl_error
= osl_setup_createTempFile_impl_(
202 if (osl_File_E_None
!= osl_error
)
205 /* allocate enough space on the stack, the file name can not be longer than MAX_PATH */
206 STACK_ALLOC(tmp_name
, WCHAR
, (rtl_uString_getLength(base_directory
) + MAX_PATH
));
210 osl_createTempFile_impl_(
217 else // stack alloc failed
219 osl_error
= osl_File_E_NOMEM
;
223 rtl_uString_release(base_directory
);
228 //#############################################
229 oslFileError SAL_CALL
osl_getTempDirURL(rtl_uString
** pustrTempDir
)
231 ::osl::LongPathBuffer
< sal_Unicode
> aBuffer( MAX_LONG_PATH
);
232 LPWSTR lpBuffer
= ::osl::mingw_reinterpret_cast
<LPWSTR
>(aBuffer
);
233 DWORD nBufferLength
= aBuffer
.getBufSizeInSymbols() - 1;
238 nLength
= GetTempPathW( aBuffer
.getBufSizeInSymbols(), lpBuffer
);
240 if ( nLength
> nBufferLength
)
242 // the provided path has invalid length
243 error
= osl_File_E_NOENT
;
247 rtl_uString
*ustrTempPath
= NULL
;
249 if ( '\\' == lpBuffer
[nLength
-1] )
250 lpBuffer
[nLength
-1] = 0;
252 rtl_uString_newFromStr( &ustrTempPath
, reinterpret_cast<const sal_Unicode
*>(lpBuffer
) );
254 error
= osl_getFileURLFromSystemPath( ustrTempPath
, pustrTempDir
);
256 rtl_uString_release( ustrTempPath
);
259 error
= oslTranslateFileError( GetLastError() );
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */