tdf#154546 skip dispatch when presenter controller is not set
[LibreOffice.git] / sal / osl / w32 / tempfile.cxx
blobf0065bf2d8ef28e3556254886f2acec4bfc19d98
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <osl/file.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"
31 #include <malloc.h>
32 #include <cassert>
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)
44 OUString dir_url;
45 OUString dir;
46 oslFileError error = osl_File_E_None;
48 if (pustrDirectoryURL)
49 dir_url = pustrDirectoryURL;
50 else
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);
59 return error;
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;
77 else
79 osl_error = osl_setup_base_directory_impl_(
80 pustrDirectoryURL, ppustr_base_dir);
82 *b_delete_on_close = (ppustrTempFileURL == nullptr);
85 return osl_error;
88 static oslFileError osl_win32_GetTempFileName_impl_(
89 rtl_uString* base_directory, LPWSTR temp_file_name)
91 oslFileError osl_error = osl_File_E_None;
93 if (GetTempFileNameW(
94 o3tl::toW(rtl_uString_getStr(base_directory)),
95 L"",
97 temp_file_name) == 0)
99 osl_error = oslTranslateFileError(GetLastError());
102 return osl_error;
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;
109 HANDLE hFile;
111 assert(p_handle);
113 if (b_delete_on_close)
114 flags |= FILE_FLAG_DELETE_ON_CLOSE;
116 hFile = CreateFileW(
117 file_name,
118 GENERIC_READ | GENERIC_WRITE,
120 nullptr,
121 TRUNCATE_EXISTING,
122 flags,
123 nullptr);
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,
134 LPWSTR tmp_name,
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))
149 break;
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);
161 return osl_error;
164 oslFileError SAL_CALL osl_createTempFile(
165 rtl_uString* pustrDirectoryURL,
166 oslFileHandle* pHandle,
167 rtl_uString** ppustrTempFileURL)
169 rtl_uString* base_directory = nullptr;
170 LPWSTR tmp_name;
171 sal_Bool b_delete_on_close;
172 oslFileError osl_error;
174 osl_error = osl_setup_createTempFile_impl_(
175 pustrDirectoryURL,
176 pHandle,
177 ppustrTempFileURL,
178 &base_directory,
179 &b_delete_on_close);
181 if (osl_error != osl_File_E_None)
182 return osl_error;
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));
187 if (tmp_name)
189 osl_error = osl_createTempFile_impl_(
190 base_directory,
191 tmp_name,
192 b_delete_on_close,
193 pHandle,
194 ppustrTempFileURL);
196 else // stack alloc failed
198 osl_error = osl_File_E_NOMEM;
201 if (base_directory)
202 rtl_uString_release(base_directory);
204 return osl_error;
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;
213 DWORD nLength;
214 oslFileError error;
216 nLength = GetTempPathW( aBuffer.getBufSizeInSymbols(), lpBuffer );
218 if ( nLength > nBufferLength )
220 // the provided path has invalid length
221 error = osl_File_E_NOENT;
223 else if ( nLength )
225 if ( '\\' == lpBuffer[nLength-1] )
226 --nLength;
228 const OUString ustrTempPath(o3tl::toU(lpBuffer), static_cast<sal_Int32>(nLength));
230 error = osl_getFileURLFromSystemPath(ustrTempPath.pData, pustrTempDir);
232 else
233 error = oslTranslateFileError( GetLastError() );
235 return error;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */