Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / w32 / tempfile.cxx
blobd85410316b8f52aa3747d2f9ac05a3cc3c2d5e5f
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 #define UNICODE
21 #define _UNICODE
22 #include "systools/win32/uwinapi.h"
24 #include "osl/file.h"
26 #include "file_error.h"
27 #include "file_url.h"
28 #include "path_helper.hxx"
30 #include "osl/diagnose.h"
32 #include <malloc.h>
33 #include <tchar.h>
35 //#####################################################
36 // Allocate n number of t's on the stack return a pointer to it in p
37 #ifdef __MINGW32__
38 #define STACK_ALLOC(p, t, n) (p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));
39 #else
40 #define STACK_ALLOC(p, t, n) __try {(p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));} \
41 __except(EXCEPTION_EXECUTE_HANDLER) {(p) = 0;}
42 #endif
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;
55 rtl_uString* dir = 0;
56 oslFileError error = osl_File_E_None;
58 if (pustrDirectoryURL)
59 rtl_uString_assign(&dir_url, pustrDirectoryURL);
60 else
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);
75 return error;
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;
94 else
96 osl_error = osl_setup_base_directory_impl_(
97 pustrDirectoryURL, ppustr_base_dir);
99 *b_delete_on_close = (sal_Bool)(0 == ppustrTempFileURL);
102 return osl_error;
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)),
113 L"",
115 temp_file_name))
117 osl_error = oslTranslateFileError(GetLastError());
120 return osl_error;
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;
128 HANDLE hFile;
130 OSL_ASSERT(p_handle);
132 if (b_delete_on_close)
133 flags |= FILE_FLAG_DELETE_ON_CLOSE;
135 hFile = CreateFileW(
136 file_name,
137 GENERIC_READ | GENERIC_WRITE,
139 NULL,
140 TRUNCATE_EXISTING,
141 flags,
142 NULL);
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,
154 LPWSTR tmp_name,
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))
169 break;
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);
181 return osl_error;
184 //#############################################
185 oslFileError SAL_CALL osl_createTempFile(
186 rtl_uString* pustrDirectoryURL,
187 oslFileHandle* pHandle,
188 rtl_uString** ppustrTempFileURL)
190 rtl_uString* base_directory = 0;
191 LPWSTR tmp_name;
192 sal_Bool b_delete_on_close;
193 oslFileError osl_error;
195 osl_error = osl_setup_createTempFile_impl_(
196 pustrDirectoryURL,
197 pHandle,
198 ppustrTempFileURL,
199 &base_directory,
200 &b_delete_on_close);
202 if (osl_File_E_None != osl_error)
203 return 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));
208 if (tmp_name)
210 osl_createTempFile_impl_(
211 base_directory,
212 tmp_name,
213 b_delete_on_close,
214 pHandle,
215 ppustrTempFileURL);
217 else // stack alloc failed
219 osl_error = osl_File_E_NOMEM;
222 if (base_directory)
223 rtl_uString_release(base_directory);
225 return osl_error;
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;
235 DWORD nLength;
236 oslFileError error;
238 nLength = GetTempPathW( aBuffer.getBufSizeInSymbols(), lpBuffer );
240 if ( nLength > nBufferLength )
242 // the provided path has invalid length
243 error = osl_File_E_NOENT;
245 else if ( nLength )
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 );
258 else
259 error = oslTranslateFileError( GetLastError() );
261 return error;
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */