1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#define UNICODE
#define _UNICODE
#define _WIN32_WINNT 0x0500
#include "systools/win32/uwinapi.h"
#include "osl/file.h"
#include "file_error.h"
#include "file_url.h"
#include "path_helper.hxx"
#include "osl/diagnose.h"
#include <malloc.h>
#include <tchar.h>
//#####################################################
// Allocate n number of t's on the stack return a pointer to it in p
#ifdef __MINGW32__
#define STACK_ALLOC(p, t, n) (p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));
#else
#define STACK_ALLOC(p, t, n) __try {(p) = reinterpret_cast<t*>(_alloca((n)*sizeof(t)));} \
__except(EXCEPTION_EXECUTE_HANDLER) {(p) = 0;}
#endif
extern "C" oslFileHandle SAL_CALL osl_createFileHandleFromOSHandle(HANDLE hFile, sal_uInt32 uFlags);
//#####################################################
// Temp file functions
//#####################################################
static oslFileError osl_setup_base_directory_impl_(
rtl_uString* pustrDirectoryURL,
rtl_uString** ppustr_base_dir)
{
rtl_uString* dir_url = 0;
rtl_uString* dir = 0;
oslFileError error = osl_File_E_None;
if (pustrDirectoryURL)
rtl_uString_assign(&dir_url, pustrDirectoryURL);
else
error = osl_getTempDirURL(&dir_url);
if (osl_File_E_None == error)
{
error = _osl_getSystemPathFromFileURL(dir_url, &dir, sal_False);
rtl_uString_release(dir_url);
}
if (osl_File_E_None == error )
{
rtl_uString_assign(ppustr_base_dir, dir);
rtl_uString_release(dir);
}
return error;
}
//#####################################################
static oslFileError osl_setup_createTempFile_impl_(
rtl_uString* pustrDirectoryURL,
oslFileHandle* pHandle,
rtl_uString** ppustrTempFileURL,
rtl_uString** ppustr_base_dir,
sal_Bool* b_delete_on_close)
{
oslFileError osl_error;
OSL_PRECOND(((0 != pHandle) || (0 != ppustrTempFileURL)), "Invalid parameter!");
if ((0 == pHandle) && (0 == ppustrTempFileURL))
{
osl_error = osl_File_E_INVAL;
}
else
{
osl_error = osl_setup_base_directory_impl_(
pustrDirectoryURL, ppustr_base_dir);
*b_delete_on_close = (sal_Bool)(0 == ppustrTempFileURL);
}
return osl_error;
}
//#####################################################
static oslFileError osl_win32_GetTempFileName_impl_(
rtl_uString* base_directory, LPWSTR temp_file_name)
{
oslFileError osl_error = osl_File_E_None;
if (0 == GetTempFileNameW(
reinterpret_cast<LPCWSTR>(rtl_uString_getStr(base_directory)),
L"",
0,
temp_file_name))
{
osl_error = oslTranslateFileError(GetLastError());
}
return osl_error;
}
//#####################################################
static sal_Bool osl_win32_CreateFile_impl_(
LPCWSTR file_name, sal_Bool b_delete_on_close, oslFileHandle* p_handle)
{
DWORD flags = FILE_ATTRIBUTE_NORMAL;
HANDLE hFile;
OSL_ASSERT(p_handle);
if (b_delete_on_close)
flags |= FILE_FLAG_DELETE_ON_CLOSE;
hFile = CreateFileW(
file_name,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
TRUNCATE_EXISTING,
flags,
NULL);
// @@@ ERROR HANDLING @@@
if (IsValidHandle(hFile))
*p_handle = osl_createFileHandleFromOSHandle(hFile, osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
return (sal_Bool)IsValidHandle(hFile);
}
//#############################################
static oslFileError osl_createTempFile_impl_(
rtl_uString* base_directory,
LPWSTR tmp_name,
sal_Bool b_delete_on_close,
oslFileHandle* pHandle,
rtl_uString** ppustrTempFileURL)
{
oslFileError osl_error;
do
{
osl_error = osl_win32_GetTempFileName_impl_(base_directory, tmp_name);
/* if file could not be opened try again */
if ((osl_File_E_None != osl_error) || (0 == pHandle) ||
osl_win32_CreateFile_impl_(tmp_name, b_delete_on_close, pHandle))
break;
} while(1); // try until success
if ((osl_File_E_None == osl_error) && !b_delete_on_close)
{
rtl_uString* pustr = 0;
rtl_uString_newFromStr(&pustr, reinterpret_cast<const sal_Unicode*>(tmp_name));
osl_getFileURLFromSystemPath(pustr, ppustrTempFileURL);
rtl_uString_release(pustr);
}
return osl_error;
}
//#############################################
oslFileError SAL_CALL osl_createTempFile(
rtl_uString* pustrDirectoryURL,
oslFileHandle* pHandle,
rtl_uString** ppustrTempFileURL)
{
rtl_uString* base_directory = 0;
LPWSTR tmp_name;
sal_Bool b_delete_on_close;
oslFileError osl_error;
osl_error = osl_setup_createTempFile_impl_(
pustrDirectoryURL,
pHandle,
ppustrTempFileURL,
&base_directory,
&b_delete_on_close);
if (osl_File_E_None != osl_error)
return osl_error;
/* allocate enough space on the stack, the file name can not be longer than MAX_PATH */
STACK_ALLOC(tmp_name, WCHAR, (rtl_uString_getLength(base_directory) + MAX_PATH));
if (tmp_name)
{
osl_createTempFile_impl_(
base_directory,
tmp_name,
b_delete_on_close,
pHandle,
ppustrTempFileURL);
}
else // stack alloc failed
{
osl_error = osl_File_E_NOMEM;
}
if (base_directory)
rtl_uString_release(base_directory);
return osl_error;
}
//#############################################
oslFileError SAL_CALL osl_getTempDirURL(rtl_uString** pustrTempDir)
{
::osl::LongPathBuffer< sal_Unicode > aBuffer( MAX_LONG_PATH );
LPWSTR lpBuffer = ::osl::mingw_reinterpret_cast<LPWSTR>(aBuffer);
DWORD nBufferLength = aBuffer.getBufSizeInSymbols() - 1;
DWORD nLength;
oslFileError error;
nLength = GetTempPathW( aBuffer.getBufSizeInSymbols(), lpBuffer );
if ( nLength > nBufferLength )
{
// the provided path has invalid length
error = osl_File_E_NOENT;
}
else if ( nLength )
{
rtl_uString *ustrTempPath = NULL;
if ( '\\' == lpBuffer[nLength-1] )
lpBuffer[nLength-1] = 0;
rtl_uString_newFromStr( &ustrTempPath, reinterpret_cast<const sal_Unicode*>(lpBuffer) );
error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
rtl_uString_release( ustrTempPath );
}
else
error = oslTranslateFileError( GetLastError() );
return error;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|