xref: /aoo42x/main/sal/osl/os2/tempfile.c (revision 509a48ff)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19647f063dSAndrew Rist  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /*****************************************************************/
25cdf0e10cSrcweir /* Includes                                                      */
26cdf0e10cSrcweir /*****************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include <stdlib.h>
30cdf0e10cSrcweir #include <sys/types.h>
31cdf0e10cSrcweir #include <sys/stat.h>
32cdf0e10cSrcweir #include <sys/time.h>
33cdf0e10cSrcweir #include "system.h"
34cdf0e10cSrcweir #include <osl/file.h>
35cdf0e10cSrcweir #include <osl/thread.h>
36cdf0e10cSrcweir #include <rtl/ustrbuf.h>
37cdf0e10cSrcweir #include <osl/diagnose.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #ifndef _FILE_URL_H_
40cdf0e10cSrcweir #include "file_url.h"
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir 
43cdf0e10cSrcweir /*****************************************************************/
44cdf0e10cSrcweir /* osl_getTempFirURL                                             */
45cdf0e10cSrcweir /*****************************************************************/
46cdf0e10cSrcweir 
osl_getTempDirURL(rtl_uString ** pustrTempDir)47cdf0e10cSrcweir oslFileError SAL_CALL osl_getTempDirURL( rtl_uString** pustrTempDir )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     const char *pValue = getenv( "TEMP" );
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     if ( !pValue )
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir         pValue = getenv( "TMP" );
54cdf0e10cSrcweir #if defined(SOLARIS) || defined (LINUX) || defined (FREEBSD) || defined (MACOSX)
55cdf0e10cSrcweir 		if ( !pValue )
56cdf0e10cSrcweir 			pValue = P_tmpdir;
57cdf0e10cSrcweir #endif
58cdf0e10cSrcweir 	}
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	if ( pValue )
61cdf0e10cSrcweir 	{
62cdf0e10cSrcweir 		oslFileError error;
63cdf0e10cSrcweir 		rtl_uString	*ustrTempPath = NULL;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 		rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
66cdf0e10cSrcweir         OSL_ASSERT(ustrTempPath != NULL);
67cdf0e10cSrcweir 		error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
68cdf0e10cSrcweir 		rtl_uString_release( ustrTempPath );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 		return error;
71cdf0e10cSrcweir 	}
72cdf0e10cSrcweir 	else
73cdf0e10cSrcweir 		return osl_File_E_NOENT;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir /******************************************************************
77cdf0e10cSrcweir  * Generates a random unique file name. We're using the scheme
78cdf0e10cSrcweir  * from the standard c-lib function mkstemp to generate a more
79cdf0e10cSrcweir  * or less random unique file name
80cdf0e10cSrcweir  *
81cdf0e10cSrcweir  * @param rand_name
82cdf0e10cSrcweir  *        receives the random name
83cdf0e10cSrcweir  ******************************************************************/
84cdf0e10cSrcweir 
85cdf0e10cSrcweir static const char LETTERS[]        = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
86cdf0e10cSrcweir static const int  COUNT_OF_LETTERS = sizeof(LETTERS)/sizeof(LETTERS[0]) - 1;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir #define RAND_NAME_LENGTH 6
89cdf0e10cSrcweir 
osl_gen_random_name_impl_(rtl_uString ** rand_name)90cdf0e10cSrcweir static void osl_gen_random_name_impl_(rtl_uString** rand_name)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	static uint64_t value;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	char     buffer[RAND_NAME_LENGTH];
95cdf0e10cSrcweir 	struct   timeval tv;
96cdf0e10cSrcweir 	uint64_t v;
97cdf0e10cSrcweir 	int      i;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	gettimeofday(&tv, NULL);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	value += ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	v = value;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	for (i = 0; i < RAND_NAME_LENGTH; i++)
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		buffer[i] = LETTERS[v % COUNT_OF_LETTERS];
108cdf0e10cSrcweir 		v        /= COUNT_OF_LETTERS;
109cdf0e10cSrcweir 	}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	rtl_string2UString(
112cdf0e10cSrcweir 			rand_name,
113cdf0e10cSrcweir 			buffer,
114cdf0e10cSrcweir 			RAND_NAME_LENGTH,
115cdf0e10cSrcweir 			RTL_TEXTENCODING_ASCII_US,
116cdf0e10cSrcweir 			OSTRING_TO_OUSTRING_CVTFLAGS);
117cdf0e10cSrcweir     OSL_ASSERT(*rand_name != NULL);
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir /*****************************************************************
121cdf0e10cSrcweir  * Helper function
122cdf0e10cSrcweir  * Either use the directory provided or the result of
123cdf0e10cSrcweir  * osl_getTempDirUrl and return it as system path and file url
124cdf0e10cSrcweir  ****************************************************************/
125cdf0e10cSrcweir 
osl_setup_base_directory_impl_(rtl_uString * pustrDirectoryURL,rtl_uString ** ppustr_base_dir)126cdf0e10cSrcweir static oslFileError osl_setup_base_directory_impl_(
127cdf0e10cSrcweir 	rtl_uString*  pustrDirectoryURL,
128cdf0e10cSrcweir 	rtl_uString** ppustr_base_dir)
129cdf0e10cSrcweir {
130*509a48ffSpfg 	rtl_uString* dir_url = NULL;
131*509a48ffSpfg 	rtl_uString* dir     = NULL;
132cdf0e10cSrcweir     oslFileError error   = osl_File_E_None;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	if (pustrDirectoryURL)
135cdf0e10cSrcweir 		rtl_uString_assign(&dir_url, pustrDirectoryURL);
136cdf0e10cSrcweir 	else
137cdf0e10cSrcweir 		error = osl_getTempDirURL(&dir_url);
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     if (osl_File_E_None == error)
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir         error = osl_getSystemPathFromFileURL_Ex(dir_url, &dir, FURL_DENY_RELATIVE);
142cdf0e10cSrcweir 		rtl_uString_release(dir_url);
143cdf0e10cSrcweir 	}
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	if (osl_File_E_None == error)
146cdf0e10cSrcweir 	{
147cdf0e10cSrcweir 		rtl_uString_assign(ppustr_base_dir, dir);
148cdf0e10cSrcweir 		rtl_uString_release(dir);
149cdf0e10cSrcweir 	}
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     return error;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir /*****************************************************************
155cdf0e10cSrcweir  * osl_setup_createTempFile_impl
156cdf0e10cSrcweir  * validate input parameter, setup variables
157cdf0e10cSrcweir  ****************************************************************/
158cdf0e10cSrcweir 
osl_setup_createTempFile_impl_(rtl_uString * pustrDirectoryURL,oslFileHandle * pHandle,rtl_uString ** ppustrTempFileURL,rtl_uString ** ppustr_base_dir,sal_Bool * b_delete_on_close)159cdf0e10cSrcweir  static oslFileError osl_setup_createTempFile_impl_(
160cdf0e10cSrcweir  	rtl_uString*   pustrDirectoryURL,
161cdf0e10cSrcweir 	oslFileHandle* pHandle,
162cdf0e10cSrcweir 	rtl_uString**  ppustrTempFileURL,
163cdf0e10cSrcweir 	rtl_uString**  ppustr_base_dir,
164cdf0e10cSrcweir 	sal_Bool*	   b_delete_on_close)
165cdf0e10cSrcweir  {
166cdf0e10cSrcweir  	oslFileError osl_error;
167cdf0e10cSrcweir 
168*509a48ffSpfg 	OSL_PRECOND(((NULL != pHandle) || (NULL != ppustrTempFileURL)),
169*509a48ffSpfg 		    "Invalid parameter!");
170cdf0e10cSrcweir 
171*509a48ffSpfg 	if ((NULL == pHandle) && (NULL == ppustrTempFileURL))
172cdf0e10cSrcweir 	{
173cdf0e10cSrcweir 		osl_error = osl_File_E_INVAL;
174cdf0e10cSrcweir 	}
175cdf0e10cSrcweir 	else
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir 		osl_error = osl_setup_base_directory_impl_(
178cdf0e10cSrcweir 			pustrDirectoryURL, ppustr_base_dir);
179cdf0e10cSrcweir 
180*509a48ffSpfg 		*b_delete_on_close = (NULL == ppustrTempFileURL);
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	return osl_error;
184cdf0e10cSrcweir  }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir /*****************************************************************
187cdf0e10cSrcweir  * Create a unique file in the specified directory and return
188cdf0e10cSrcweir  * it's name
189cdf0e10cSrcweir  ****************************************************************/
190cdf0e10cSrcweir 
osl_create_temp_file_impl_(const rtl_uString * pustr_base_directory,oslFileHandle * file_handle,rtl_uString ** ppustr_temp_file_name)191cdf0e10cSrcweir static oslFileError osl_create_temp_file_impl_(
192cdf0e10cSrcweir 	const rtl_uString* pustr_base_directory,
193cdf0e10cSrcweir 	oslFileHandle* file_handle,
194cdf0e10cSrcweir 	rtl_uString** ppustr_temp_file_name)
195cdf0e10cSrcweir {
196*509a48ffSpfg 	rtl_uString*        rand_name        = NULL;
197cdf0e10cSrcweir 	sal_uInt32          len_base_dir     = 0;
198*509a48ffSpfg 	rtl_uString*        tmp_file_path    = NULL;
199*509a48ffSpfg 	rtl_uString*        tmp_file_url     = NULL;
200cdf0e10cSrcweir 	sal_Int32           capacity         = 0;
201cdf0e10cSrcweir 	oslFileError        osl_error        = osl_File_E_None;
202cdf0e10cSrcweir 	sal_Int32           offset_file_name;
203cdf0e10cSrcweir 	const sal_Unicode*  puchr;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	OSL_PRECOND(pustr_base_directory, "Invalid Parameter");
206cdf0e10cSrcweir 	OSL_PRECOND(file_handle, "Invalid Parameter");
207cdf0e10cSrcweir 	OSL_PRECOND(ppustr_temp_file_name, "Invalid Parameter");
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 	len_base_dir = rtl_uString_getLength(pustr_base_directory);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	rtl_uStringbuffer_newFromStr_WithLength(
212cdf0e10cSrcweir 		&tmp_file_path,
213cdf0e10cSrcweir 		rtl_uString_getStr((rtl_uString*)pustr_base_directory),
214cdf0e10cSrcweir 		len_base_dir);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	rtl_uStringbuffer_ensureCapacity(
217cdf0e10cSrcweir 		&tmp_file_path,
218cdf0e10cSrcweir 		&capacity,
219cdf0e10cSrcweir 		(len_base_dir + 1 + RAND_NAME_LENGTH));
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	offset_file_name = len_base_dir;
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 	puchr = rtl_uString_getStr(tmp_file_path);
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	/* ensure that the last character is a '\' */
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	if ((sal_Unicode)'\\' != puchr[len_base_dir - 1])
228cdf0e10cSrcweir 	{
229cdf0e10cSrcweir 		rtl_uStringbuffer_insert_ascii(
230cdf0e10cSrcweir 			&tmp_file_path,
231cdf0e10cSrcweir 			&capacity,
232cdf0e10cSrcweir 			len_base_dir,
233cdf0e10cSrcweir 			"\\",
234cdf0e10cSrcweir 			1);
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 		offset_file_name++;
237cdf0e10cSrcweir 	}
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 	while(1) /* try until success */
240cdf0e10cSrcweir 	{
241cdf0e10cSrcweir 		osl_gen_random_name_impl_(&rand_name);
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 		rtl_uStringbuffer_insert(
244cdf0e10cSrcweir 			&tmp_file_path,
245cdf0e10cSrcweir 			&capacity,
246cdf0e10cSrcweir 			offset_file_name,
247cdf0e10cSrcweir 			rtl_uString_getStr(rand_name),
248cdf0e10cSrcweir 			rtl_uString_getLength(rand_name));
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 		osl_error = osl_getFileURLFromSystemPath(
251cdf0e10cSrcweir 		    tmp_file_path, &tmp_file_url);
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 		if (osl_File_E_None == osl_error)
254cdf0e10cSrcweir 		{
255cdf0e10cSrcweir 		    /* RW permission for the user only! */
256cdf0e10cSrcweir 		    mode_t old_mode = umask(077);
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 		    osl_error = osl_openFile(
259cdf0e10cSrcweir 			    tmp_file_url,
260cdf0e10cSrcweir 			    file_handle,
261cdf0e10cSrcweir 			    osl_File_OpenFlag_Read |
262cdf0e10cSrcweir 			    osl_File_OpenFlag_Write |
263cdf0e10cSrcweir 			    osl_File_OpenFlag_Create);
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 			umask(old_mode);
266cdf0e10cSrcweir 		}
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 		/* in case of error osl_File_E_EXIST we simply try again else we give up */
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 		if ((osl_File_E_None == osl_error) || (osl_error != osl_File_E_EXIST))
271cdf0e10cSrcweir 		{
272cdf0e10cSrcweir 			if (rand_name)
273cdf0e10cSrcweir 				rtl_uString_release(rand_name);
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 			if (tmp_file_url)
276cdf0e10cSrcweir 				rtl_uString_release(tmp_file_url);
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 			break;
279cdf0e10cSrcweir 		}
280cdf0e10cSrcweir 	} /* while(1) */
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 	if (osl_File_E_None == osl_error)
283cdf0e10cSrcweir 		rtl_uString_assign(ppustr_temp_file_name, tmp_file_path);
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	if (tmp_file_path)
286cdf0e10cSrcweir 		rtl_uString_release(tmp_file_path);
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 	return osl_error;
289cdf0e10cSrcweir }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir /*****************************************************************
292cdf0e10cSrcweir  * osl_createTempFile
293cdf0e10cSrcweir  *****************************************************************/
294cdf0e10cSrcweir 
osl_createTempFile(rtl_uString * pustrDirectoryURL,oslFileHandle * pHandle,rtl_uString ** ppustrTempFileURL)295cdf0e10cSrcweir oslFileError SAL_CALL osl_createTempFile(
296cdf0e10cSrcweir 	rtl_uString*   pustrDirectoryURL,
297cdf0e10cSrcweir 	oslFileHandle* pHandle,
298cdf0e10cSrcweir 	rtl_uString**  ppustrTempFileURL)
299cdf0e10cSrcweir {
300*509a48ffSpfg 	rtl_uString*  base_directory     = NULL;
301*509a48ffSpfg 	rtl_uString*  temp_file_name     = NULL;
302cdf0e10cSrcweir 	oslFileHandle temp_file_handle;
303cdf0e10cSrcweir 	sal_Bool      b_delete_on_close;
304cdf0e10cSrcweir 	oslFileError  osl_error;
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	osl_error = osl_setup_createTempFile_impl_(
307cdf0e10cSrcweir 		pustrDirectoryURL,
308cdf0e10cSrcweir 		pHandle,
309cdf0e10cSrcweir 		ppustrTempFileURL,
310cdf0e10cSrcweir 		&base_directory,
311cdf0e10cSrcweir 		&b_delete_on_close);
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 	if (osl_File_E_None != osl_error)
314cdf0e10cSrcweir 		return osl_error;
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 	osl_error = osl_create_temp_file_impl_(
317cdf0e10cSrcweir 	    base_directory, &temp_file_handle, &temp_file_name);
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 	if (osl_File_E_None == osl_error)
320cdf0e10cSrcweir 	{
321*509a48ffSpfg 		rtl_uString* temp_file_url = NULL;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 		/* assuming this works */
324cdf0e10cSrcweir 		osl_getFileURLFromSystemPath(temp_file_name, &temp_file_url);
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 		if (b_delete_on_close)
327cdf0e10cSrcweir 		{
328cdf0e10cSrcweir 			osl_error = osl_removeFile(temp_file_url);
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 			if (osl_File_E_None == osl_error)
331cdf0e10cSrcweir 				*pHandle = temp_file_handle;
332cdf0e10cSrcweir 			else
333cdf0e10cSrcweir 				osl_closeFile(temp_file_handle);
334cdf0e10cSrcweir 		}
335cdf0e10cSrcweir 		else
336cdf0e10cSrcweir 		{
337cdf0e10cSrcweir 			if (pHandle)
338cdf0e10cSrcweir 				*pHandle = temp_file_handle;
339cdf0e10cSrcweir 			else
340cdf0e10cSrcweir 				osl_closeFile(temp_file_handle);
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 			rtl_uString_assign(ppustrTempFileURL, temp_file_url);
343cdf0e10cSrcweir         }
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 		if (temp_file_url)
346cdf0e10cSrcweir 			rtl_uString_release(temp_file_url);
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         if (temp_file_name)
349cdf0e10cSrcweir             rtl_uString_release(temp_file_name);
350cdf0e10cSrcweir 	}
351cdf0e10cSrcweir 
352cdf0e10cSrcweir     if (base_directory)
353cdf0e10cSrcweir     	rtl_uString_release(base_directory);
354cdf0e10cSrcweir 
355cdf0e10cSrcweir     return osl_error;
356cdf0e10cSrcweir }
357