1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_filter.hxx"
26 
27 
28 #include <osl/file.h>
29 
30 #if defined( UNX) || defined(OS2)
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <osl/thread.h>
35 
my_getTempDirURL(rtl_uString ** pustrTempDir)36 oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
37 {
38     const char *pValue = getenv( "TEMP" );
39 
40     if ( !pValue )
41 	{
42         pValue = getenv( "TMP" );
43 #if defined(SOLARIS) || defined (LINUX)
44 		if ( !pValue )
45 			pValue = P_tmpdir;
46 #endif
47 	}
48 
49 	if ( pValue )
50 	{
51 		oslFileError error;
52 		rtl_uString	*ustrTempPath = NULL;
53 
54 		rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
55 		error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
56 		rtl_uString_release( ustrTempPath );
57 
58 		return error;
59 	}
60 	else
61 		return osl_File_E_NOENT;
62 }
63 #else
64 
65 #ifdef NDEBUG
66 #    define NO_DEBUG_CRT
67 #endif
68 
69 #ifndef _WIN32_WINNT
70 #	define _WIN32_WINNT 0x0400
71 #	define _CTYPE_DISABLE_MACROS /* wg. dynamischer C-Runtime MH */
72 #endif
73 
74 #if defined _MSC_VER
75 #pragma warning(push, 1)
76 #endif
77 
78 #define WIN32_LEAN_AND_MEAN
79 #include <windows.h>
80 #include <malloc.h>
81 
82 #if defined _MSC_VER
83 #pragma warning(pop)
84 #endif
85 
86 #define elementsof(arr) (sizeof(arr)/sizeof(arr[0]))
87 
my_getTempDirURL(rtl_uString ** pustrTempDir)88 oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
89 {
90 	WCHAR	szBuffer[MAX_PATH];
91 	LPWSTR	lpBuffer = szBuffer;
92 	DWORD	nBufferLength = elementsof(szBuffer) - 1;
93 
94 	DWORD			nLength;
95 	oslFileError	error;
96 
97 	do
98 	{
99 		nLength = GetTempPathW( elementsof(szBuffer), lpBuffer );
100 		if ( nLength > nBufferLength )
101 		{
102 			nLength++;
103 			lpBuffer = (LPWSTR)alloca( sizeof(WCHAR) * nLength );
104 			nBufferLength = nLength - 1;
105 		}
106 	} while ( nLength > nBufferLength );
107 
108 	if ( nLength )
109 	{
110 		rtl_uString	*ustrTempPath = NULL;
111 
112 		if ( '\\' == lpBuffer[nLength-1] )
113 			lpBuffer[nLength-1] = 0;
114 
115 		rtl_uString_newFromStr( &ustrTempPath, reinterpret_cast<const sal_Unicode*>(lpBuffer) );
116 
117 		error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
118 
119 		rtl_uString_release( ustrTempPath );
120 	}
121 	else
122 		error = GetLastError() == ERROR_SUCCESS ? osl_File_E_None : osl_File_E_INVAL;
123 
124 	return error;
125 }
126 #endif
127 
128 #include "tempfile.hxx"
129 
130 using namespace rtl;
131 
TempFile(const OUString & rTempFileURL)132 TempFile::TempFile( const OUString& rTempFileURL )
133 :osl::File( rTempFileURL ), maURL( rTempFileURL )
134 {
135 }
136 
~TempFile()137 TempFile::~TempFile()
138 {
139 	close();
140 
141 	if( maURL.getLength() )
142 		osl::File::remove( maURL );
143 }
144 
createTempFileURL()145 OUString TempFile::createTempFileURL()
146 {
147 	OUString aTempFileURL;
148 
149 	const sal_uInt32 nRadix = 26;
150 
151 	OUString aTempDirURL;
152 	/* oslFileError nRC = */ my_getTempDirURL( &aTempDirURL.pData );
153 
154 	static sal_uInt32 u = osl_getGlobalTimer();
155 	for ( sal_uInt32 nOld = u; ++u != nOld; )
156 	{
157 		u %= (nRadix*nRadix*nRadix);
158 		OUString aTmp( aTempDirURL );
159         if( aTmp.getStr()[ aTmp.getLength() - 1 ] != sal_Unicode( '/' ) )
160             aTmp += OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ));
161 		aTmp += OUString::valueOf( (sal_Int32) (unsigned) u, nRadix );
162 		aTmp += OUString::createFromAscii( ".tmp" );
163 
164 		osl::File aFile( aTmp );
165 		osl::FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
166 		if (  err == FileBase::E_None )
167 		{
168 			aTempFileURL = aTmp;
169 			aFile.close();
170 			break;
171 		}
172 		else if ( err != FileBase::E_EXIST )
173 		{
174 			 // if f.e. name contains invalid chars stop trying to create files
175 			 break;
176 		}
177 	}
178 
179 	return aTempFileURL;
180 }
181 
getFileURL()182 OUString TempFile::getFileURL()
183 {
184 	return maURL;
185 }
186