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 #define _WIN32_WINNT 0x0500
23 #include "systools/win32/uwinapi.h"
27 #include "file_error.h"
29 #include "path_helper.hxx"
31 #include "osl/diagnose.h"
36 //#####################################################
37 // Allocate n number of t's on the stack return a pointer to it in p
39 #define STACK_ALLOC(p, t, n) (p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));
41 #define STACK_ALLOC(p, t, n) __try {(p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));} \
42 __except(EXCEPTION_EXECUTE_HANDLER) {(p) = 0;}
45 extern "C" oslFileHandle SAL_CALL
osl_createFileHandleFromOSHandle(HANDLE hFile
, sal_uInt32 uFlags
);
47 //#####################################################
48 // Temp file functions
49 //#####################################################
51 static oslFileError
osl_setup_base_directory_impl_(
52 rtl_uString
* pustrDirectoryURL
,
53 rtl_uString
** ppustr_base_dir
)
55 rtl_uString
* dir_url
= 0;
57 oslFileError error
= osl_File_E_None
;
59 if (pustrDirectoryURL
)
60 rtl_uString_assign(&dir_url
, pustrDirectoryURL
);
62 error
= osl_getTempDirURL(&dir_url
);
64 if (osl_File_E_None
== error
)
66 error
= _osl_getSystemPathFromFileURL(dir_url
, &dir
, sal_False
);
67 rtl_uString_release(dir_url
);
70 if (osl_File_E_None
== error
)
72 rtl_uString_assign(ppustr_base_dir
, dir
);
73 rtl_uString_release(dir
);
79 //#####################################################
80 static oslFileError
osl_setup_createTempFile_impl_(
81 rtl_uString
* pustrDirectoryURL
,
82 oslFileHandle
* pHandle
,
83 rtl_uString
** ppustrTempFileURL
,
84 rtl_uString
** ppustr_base_dir
,
85 sal_Bool
* b_delete_on_close
)
87 oslFileError osl_error
;
89 OSL_PRECOND(((0 != pHandle
) || (0 != ppustrTempFileURL
)), "Invalid parameter!");
91 if ((0 == pHandle
) && (0 == ppustrTempFileURL
))
93 osl_error
= osl_File_E_INVAL
;
97 osl_error
= osl_setup_base_directory_impl_(
98 pustrDirectoryURL
, ppustr_base_dir
);
100 *b_delete_on_close
= (sal_Bool
)(0 == ppustrTempFileURL
);
106 //#####################################################
107 static oslFileError
osl_win32_GetTempFileName_impl_(
108 rtl_uString
* base_directory
, LPWSTR temp_file_name
)
110 oslFileError osl_error
= osl_File_E_None
;
112 if (0 == GetTempFileNameW(
113 reinterpret_cast<LPCWSTR
>(rtl_uString_getStr(base_directory
)),
118 osl_error
= oslTranslateFileError(GetLastError());
124 //#####################################################
125 static sal_Bool
osl_win32_CreateFile_impl_(
126 LPCWSTR file_name
, sal_Bool b_delete_on_close
, oslFileHandle
* p_handle
)
128 DWORD flags
= FILE_ATTRIBUTE_NORMAL
;
131 OSL_ASSERT(p_handle
);
133 if (b_delete_on_close
)
134 flags
|= FILE_FLAG_DELETE_ON_CLOSE
;
138 GENERIC_READ
| GENERIC_WRITE
,
145 // @@@ ERROR HANDLING @@@
146 if (IsValidHandle(hFile
))
147 *p_handle
= osl_createFileHandleFromOSHandle(hFile
, osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
149 return (sal_Bool
)IsValidHandle(hFile
);
152 //#############################################
153 static oslFileError
osl_createTempFile_impl_(
154 rtl_uString
* base_directory
,
156 sal_Bool b_delete_on_close
,
157 oslFileHandle
* pHandle
,
158 rtl_uString
** ppustrTempFileURL
)
160 oslFileError osl_error
;
164 osl_error
= osl_win32_GetTempFileName_impl_(base_directory
, tmp_name
);
166 /* if file could not be opened try again */
168 if ((osl_File_E_None
!= osl_error
) || (0 == pHandle
) ||
169 osl_win32_CreateFile_impl_(tmp_name
, b_delete_on_close
, pHandle
))
172 } while(1); // try until success
174 if ((osl_File_E_None
== osl_error
) && !b_delete_on_close
)
176 rtl_uString
* pustr
= 0;
177 rtl_uString_newFromStr(&pustr
, reinterpret_cast<const sal_Unicode
*>(tmp_name
));
178 osl_getFileURLFromSystemPath(pustr
, ppustrTempFileURL
);
179 rtl_uString_release(pustr
);
185 //#############################################
186 oslFileError SAL_CALL
osl_createTempFile(
187 rtl_uString
* pustrDirectoryURL
,
188 oslFileHandle
* pHandle
,
189 rtl_uString
** ppustrTempFileURL
)
191 rtl_uString
* base_directory
= 0;
193 sal_Bool b_delete_on_close
;
194 oslFileError osl_error
;
196 osl_error
= osl_setup_createTempFile_impl_(
203 if (osl_File_E_None
!= osl_error
)
206 /* allocate enough space on the stack, the file name can not be longer than MAX_PATH */
207 STACK_ALLOC(tmp_name
, WCHAR
, (rtl_uString_getLength(base_directory
) + MAX_PATH
));
211 osl_createTempFile_impl_(
218 else // stack alloc failed
220 osl_error
= osl_File_E_NOMEM
;
224 rtl_uString_release(base_directory
);
229 //#############################################
230 oslFileError SAL_CALL
osl_getTempDirURL(rtl_uString
** pustrTempDir
)
232 ::osl::LongPathBuffer
< sal_Unicode
> aBuffer( MAX_LONG_PATH
);
233 LPWSTR lpBuffer
= ::osl::mingw_reinterpret_cast
<LPWSTR
>(aBuffer
);
234 DWORD nBufferLength
= aBuffer
.getBufSizeInSymbols() - 1;
239 nLength
= GetTempPathW( aBuffer
.getBufSizeInSymbols(), lpBuffer
);
241 if ( nLength
> nBufferLength
)
243 // the provided path has invalid length
244 error
= osl_File_E_NOENT
;
248 rtl_uString
*ustrTempPath
= NULL
;
250 if ( '\\' == lpBuffer
[nLength
-1] )
251 lpBuffer
[nLength
-1] = 0;
253 rtl_uString_newFromStr( &ustrTempPath
, reinterpret_cast<const sal_Unicode
*>(lpBuffer
) );
255 error
= osl_getFileURLFromSystemPath( ustrTempPath
, pustrTempDir
);
257 rtl_uString_release( ustrTempPath
);
260 error
= oslTranslateFileError( GetLastError() );
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */