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 .
20 #include <systools/win32/uwinapi.h>
23 #include <o3tl/char16_t2wchar_t.hxx>
24 #include <rtl/ustring.hxx>
26 #include "file-impl.hxx"
27 #include "file_error.hxx"
28 #include "file_url.hxx"
29 #include "path_helper.hxx"
34 // Allocate n number of t's on the stack return a pointer to it in p
35 #define STACK_ALLOC(p, t, n) __try {(p) = static_cast<t*>(_alloca((n)*sizeof(t)));} \
36 __except(EXCEPTION_EXECUTE_HANDLER) {(p) = nullptr;}
38 // Temp file functions
40 static oslFileError
osl_setup_base_directory_impl_(
41 rtl_uString
* pustrDirectoryURL
,
42 rtl_uString
** ppustr_base_dir
)
46 oslFileError error
= osl_File_E_None
;
48 if (pustrDirectoryURL
)
49 dir_url
= pustrDirectoryURL
;
51 error
= osl_getTempDirURL(&dir_url
.pData
);
53 if (error
== osl_File_E_None
)
54 error
= osl_getSystemPathFromFileURL_(dir_url
, &dir
.pData
, false);
56 if (error
== osl_File_E_None
)
57 rtl_uString_assign(ppustr_base_dir
, dir
.pData
);
62 static oslFileError
osl_setup_createTempFile_impl_(
63 rtl_uString
* pustrDirectoryURL
,
64 oslFileHandle
* pHandle
,
65 rtl_uString
** ppustrTempFileURL
,
66 rtl_uString
** ppustr_base_dir
,
67 sal_Bool
* b_delete_on_close
)
69 oslFileError osl_error
;
71 OSL_PRECOND(((pHandle
!= nullptr) || (ppustrTempFileURL
!= nullptr)), "Invalid parameter!");
73 if ((pHandle
== nullptr) && (ppustrTempFileURL
== nullptr))
75 osl_error
= osl_File_E_INVAL
;
79 osl_error
= osl_setup_base_directory_impl_(
80 pustrDirectoryURL
, ppustr_base_dir
);
82 *b_delete_on_close
= (ppustrTempFileURL
== nullptr);
88 static oslFileError
osl_win32_GetTempFileName_impl_(
89 rtl_uString
* base_directory
, LPWSTR temp_file_name
)
91 oslFileError osl_error
= osl_File_E_None
;
94 o3tl::toW(rtl_uString_getStr(base_directory
)),
99 osl_error
= oslTranslateFileError(GetLastError());
105 static bool osl_win32_CreateFile_impl_(
106 LPCWSTR file_name
, bool b_delete_on_close
, oslFileHandle
* p_handle
)
108 DWORD flags
= FILE_ATTRIBUTE_NORMAL
;
113 if (b_delete_on_close
)
114 flags
|= FILE_FLAG_DELETE_ON_CLOSE
;
118 GENERIC_READ
| GENERIC_WRITE
,
125 // @@@ ERROR HANDLING @@@
126 if (IsValidHandle(hFile
))
127 *p_handle
= osl_createFileHandleFromOSHandle(hFile
, osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
129 return IsValidHandle(hFile
);
132 static oslFileError
osl_createTempFile_impl_(
133 rtl_uString
* base_directory
,
135 bool b_delete_on_close
,
136 oslFileHandle
* pHandle
,
137 rtl_uString
** ppustrTempFileURL
)
139 oslFileError osl_error
;
143 osl_error
= osl_win32_GetTempFileName_impl_(base_directory
, tmp_name
);
145 /* if file could not be opened try again */
147 if ((osl_File_E_None
!= osl_error
) || (nullptr == pHandle
) ||
148 osl_win32_CreateFile_impl_(tmp_name
, b_delete_on_close
, pHandle
))
151 } while(true); // try until success
153 if ((osl_error
== osl_File_E_None
) && !b_delete_on_close
)
155 rtl_uString
* pustr
= nullptr;
156 rtl_uString_newFromStr(&pustr
, o3tl::toU(tmp_name
));
157 osl_getFileURLFromSystemPath(pustr
, ppustrTempFileURL
);
158 rtl_uString_release(pustr
);
164 oslFileError SAL_CALL
osl_createTempFile(
165 rtl_uString
* pustrDirectoryURL
,
166 oslFileHandle
* pHandle
,
167 rtl_uString
** ppustrTempFileURL
)
169 rtl_uString
* base_directory
= nullptr;
171 sal_Bool b_delete_on_close
;
172 oslFileError osl_error
;
174 osl_error
= osl_setup_createTempFile_impl_(
181 if (osl_error
!= osl_File_E_None
)
184 /* allocate enough space on the stack, the file name can not be longer than MAX_PATH */
185 STACK_ALLOC(tmp_name
, WCHAR
, (rtl_uString_getLength(base_directory
) + MAX_PATH
));
189 osl_error
= osl_createTempFile_impl_(
196 else // stack alloc failed
198 osl_error
= osl_File_E_NOMEM
;
202 rtl_uString_release(base_directory
);
207 oslFileError SAL_CALL
osl_getTempDirURL(rtl_uString
** pustrTempDir
)
209 ::osl::LongPathBuffer
< sal_Unicode
> aBuffer( MAX_LONG_PATH
);
210 LPWSTR lpBuffer
= o3tl::toW(aBuffer
);
211 DWORD nBufferLength
= aBuffer
.getBufSizeInSymbols() - 1;
216 nLength
= GetTempPathW( aBuffer
.getBufSizeInSymbols(), lpBuffer
);
218 if ( nLength
> nBufferLength
)
220 // the provided path has invalid length
221 error
= osl_File_E_NOENT
;
225 if ( '\\' == lpBuffer
[nLength
-1] )
228 const OUString
ustrTempPath(o3tl::toU(lpBuffer
), static_cast<sal_Int32
>(nLength
));
230 error
= osl_getFileURLFromSystemPath(ustrTempPath
.pData
, pustrTempDir
);
233 error
= oslTranslateFileError( GetLastError() );
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */