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
10cdf0e10cSrcweir *
11647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
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.
19cdf0e10cSrcweir *
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 #ifdef MACOSX
50cdf0e10cSrcweir const char *pValue = getenv( "TMPDIR" );
51cdf0e10cSrcweir
52cdf0e10cSrcweir /* If TMPDIR environment variable is not set, use "/tmp" instead
53cdf0e10cSrcweir of P_tmpdir because its value is "/var/tmp" and it is not
54cdf0e10cSrcweir deleted on system start up */
55cdf0e10cSrcweir if ( !pValue )
56cdf0e10cSrcweir pValue = "/tmp";
57cdf0e10cSrcweir #else
58cdf0e10cSrcweir
59cdf0e10cSrcweir const char *pValue = getenv( "TEMP" );
60cdf0e10cSrcweir
61cdf0e10cSrcweir if ( !pValue )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir pValue = getenv( "TMP" );
64cdf0e10cSrcweir #if defined(SOLARIS) || defined (LINUX) || defined (FREEBSD)
65cdf0e10cSrcweir if ( !pValue )
66cdf0e10cSrcweir pValue = P_tmpdir;
67cdf0e10cSrcweir #endif
68cdf0e10cSrcweir }
69cdf0e10cSrcweir #endif /* MACOSX */
70cdf0e10cSrcweir
71cdf0e10cSrcweir if ( pValue )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir oslFileError error;
74cdf0e10cSrcweir rtl_uString *ustrTempPath = NULL;
75cdf0e10cSrcweir
76cdf0e10cSrcweir rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
77cdf0e10cSrcweir OSL_ASSERT(ustrTempPath != NULL);
78cdf0e10cSrcweir error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
79cdf0e10cSrcweir rtl_uString_release( ustrTempPath );
80cdf0e10cSrcweir
81cdf0e10cSrcweir return error;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir else
84cdf0e10cSrcweir return osl_File_E_NOENT;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
87cdf0e10cSrcweir /******************************************************************
88cdf0e10cSrcweir * Generates a random unique file name. We're using the scheme
89cdf0e10cSrcweir * from the standard c-lib function mkstemp to generate a more
90cdf0e10cSrcweir * or less random unique file name
91cdf0e10cSrcweir *
92cdf0e10cSrcweir * @param rand_name
93cdf0e10cSrcweir * receives the random name
94cdf0e10cSrcweir ******************************************************************/
95cdf0e10cSrcweir
96cdf0e10cSrcweir static const char LETTERS[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
97cdf0e10cSrcweir static const int COUNT_OF_LETTERS = sizeof(LETTERS)/sizeof(LETTERS[0]) - 1;
98cdf0e10cSrcweir
99cdf0e10cSrcweir #define RAND_NAME_LENGTH 6
100cdf0e10cSrcweir
osl_gen_random_name_impl_(rtl_uString ** rand_name)101cdf0e10cSrcweir static void osl_gen_random_name_impl_(rtl_uString** rand_name)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir static uint64_t value;
104cdf0e10cSrcweir
105cdf0e10cSrcweir char buffer[RAND_NAME_LENGTH];
106cdf0e10cSrcweir struct timeval tv;
107cdf0e10cSrcweir uint64_t v;
108cdf0e10cSrcweir int i;
109cdf0e10cSrcweir
110cdf0e10cSrcweir gettimeofday(&tv, NULL);
111cdf0e10cSrcweir
112cdf0e10cSrcweir value += ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
113cdf0e10cSrcweir
114cdf0e10cSrcweir v = value;
115cdf0e10cSrcweir
116cdf0e10cSrcweir for (i = 0; i < RAND_NAME_LENGTH; i++)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir buffer[i] = LETTERS[v % COUNT_OF_LETTERS];
119cdf0e10cSrcweir v /= COUNT_OF_LETTERS;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir
122cdf0e10cSrcweir rtl_string2UString(
123cdf0e10cSrcweir rand_name,
124cdf0e10cSrcweir buffer,
125cdf0e10cSrcweir RAND_NAME_LENGTH,
126cdf0e10cSrcweir RTL_TEXTENCODING_ASCII_US,
127cdf0e10cSrcweir OSTRING_TO_OUSTRING_CVTFLAGS);
128cdf0e10cSrcweir OSL_ASSERT(*rand_name != NULL);
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
131cdf0e10cSrcweir /*****************************************************************
132cdf0e10cSrcweir * Helper function
133cdf0e10cSrcweir * Either use the directory provided or the result of
134cdf0e10cSrcweir * osl_getTempDirUrl and return it as system path and file url
135cdf0e10cSrcweir ****************************************************************/
136cdf0e10cSrcweir
osl_setup_base_directory_impl_(rtl_uString * pustrDirectoryURL,rtl_uString ** ppustr_base_dir)137cdf0e10cSrcweir static oslFileError osl_setup_base_directory_impl_(
138cdf0e10cSrcweir rtl_uString* pustrDirectoryURL,
139cdf0e10cSrcweir rtl_uString** ppustr_base_dir)
140cdf0e10cSrcweir {
141*509a48ffSpfg rtl_uString* dir_url = NULL;
142*509a48ffSpfg rtl_uString* dir = NULL;
143cdf0e10cSrcweir oslFileError error = osl_File_E_None;
144cdf0e10cSrcweir
145cdf0e10cSrcweir if (pustrDirectoryURL)
146cdf0e10cSrcweir rtl_uString_assign(&dir_url, pustrDirectoryURL);
147cdf0e10cSrcweir else
148cdf0e10cSrcweir error = osl_getTempDirURL(&dir_url);
149cdf0e10cSrcweir
150cdf0e10cSrcweir if (osl_File_E_None == error)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir error = osl_getSystemPathFromFileURL_Ex(dir_url, &dir, FURL_DENY_RELATIVE);
153cdf0e10cSrcweir rtl_uString_release(dir_url);
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir if (osl_File_E_None == error)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir rtl_uString_assign(ppustr_base_dir, dir);
159cdf0e10cSrcweir rtl_uString_release(dir);
160cdf0e10cSrcweir }
161cdf0e10cSrcweir
162cdf0e10cSrcweir return error;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir
165cdf0e10cSrcweir /*****************************************************************
166cdf0e10cSrcweir * osl_setup_createTempFile_impl
167cdf0e10cSrcweir * validate input parameter, setup variables
168cdf0e10cSrcweir ****************************************************************/
169cdf0e10cSrcweir
osl_setup_createTempFile_impl_(rtl_uString * pustrDirectoryURL,oslFileHandle * pHandle,rtl_uString ** ppustrTempFileURL,rtl_uString ** ppustr_base_dir,sal_Bool * b_delete_on_close)170cdf0e10cSrcweir static oslFileError osl_setup_createTempFile_impl_(
171cdf0e10cSrcweir rtl_uString* pustrDirectoryURL,
172cdf0e10cSrcweir oslFileHandle* pHandle,
173cdf0e10cSrcweir rtl_uString** ppustrTempFileURL,
174cdf0e10cSrcweir rtl_uString** ppustr_base_dir,
175cdf0e10cSrcweir sal_Bool* b_delete_on_close)
176cdf0e10cSrcweir {
177cdf0e10cSrcweir oslFileError osl_error;
178cdf0e10cSrcweir
179*509a48ffSpfg OSL_PRECOND(((NULL != pHandle) || (NULL != ppustrTempFileURL)),
180*509a48ffSpfg "Invalid parameter!");
181cdf0e10cSrcweir
182*509a48ffSpfg if ((NULL == pHandle) && (NULL == ppustrTempFileURL))
183cdf0e10cSrcweir {
184cdf0e10cSrcweir osl_error = osl_File_E_INVAL;
185cdf0e10cSrcweir }
186cdf0e10cSrcweir else
187cdf0e10cSrcweir {
188cdf0e10cSrcweir osl_error = osl_setup_base_directory_impl_(
189cdf0e10cSrcweir pustrDirectoryURL, ppustr_base_dir);
190cdf0e10cSrcweir
191*509a48ffSpfg *b_delete_on_close = (NULL == ppustrTempFileURL);
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
194cdf0e10cSrcweir return osl_error;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir
197cdf0e10cSrcweir /*****************************************************************
198cdf0e10cSrcweir * Create a unique file in the specified directory and return
199cdf0e10cSrcweir * it's name
200cdf0e10cSrcweir ****************************************************************/
201cdf0e10cSrcweir
osl_create_temp_file_impl_(const rtl_uString * pustr_base_directory,oslFileHandle * file_handle,rtl_uString ** ppustr_temp_file_name)202cdf0e10cSrcweir static oslFileError osl_create_temp_file_impl_(
203cdf0e10cSrcweir const rtl_uString* pustr_base_directory,
204cdf0e10cSrcweir oslFileHandle* file_handle,
205cdf0e10cSrcweir rtl_uString** ppustr_temp_file_name)
206cdf0e10cSrcweir {
207*509a48ffSpfg rtl_uString* rand_name = NULL;
208cdf0e10cSrcweir sal_uInt32 len_base_dir = 0;
209*509a48ffSpfg rtl_uString* tmp_file_path = NULL;
210*509a48ffSpfg rtl_uString* tmp_file_url = NULL;
211cdf0e10cSrcweir sal_Int32 capacity = 0;
212cdf0e10cSrcweir oslFileError osl_error = osl_File_E_None;
213cdf0e10cSrcweir sal_Int32 offset_file_name;
214cdf0e10cSrcweir const sal_Unicode* puchr;
215cdf0e10cSrcweir
216cdf0e10cSrcweir OSL_PRECOND(pustr_base_directory, "Invalid Parameter");
217cdf0e10cSrcweir OSL_PRECOND(file_handle, "Invalid Parameter");
218cdf0e10cSrcweir OSL_PRECOND(ppustr_temp_file_name, "Invalid Parameter");
219cdf0e10cSrcweir
220cdf0e10cSrcweir len_base_dir = rtl_uString_getLength(pustr_base_directory);
221cdf0e10cSrcweir
222cdf0e10cSrcweir rtl_uStringbuffer_newFromStr_WithLength(
223cdf0e10cSrcweir &tmp_file_path,
224cdf0e10cSrcweir rtl_uString_getStr((rtl_uString*)pustr_base_directory),
225cdf0e10cSrcweir len_base_dir);
226cdf0e10cSrcweir
227cdf0e10cSrcweir rtl_uStringbuffer_ensureCapacity(
228cdf0e10cSrcweir &tmp_file_path,
229cdf0e10cSrcweir &capacity,
230cdf0e10cSrcweir (len_base_dir + 1 + RAND_NAME_LENGTH));
231cdf0e10cSrcweir
232cdf0e10cSrcweir offset_file_name = len_base_dir;
233cdf0e10cSrcweir
234cdf0e10cSrcweir puchr = rtl_uString_getStr(tmp_file_path);
235cdf0e10cSrcweir
236cdf0e10cSrcweir /* ensure that the last character is a '/' */
237cdf0e10cSrcweir
238cdf0e10cSrcweir if ((sal_Unicode)'/' != puchr[len_base_dir - 1])
239cdf0e10cSrcweir {
240cdf0e10cSrcweir rtl_uStringbuffer_insert_ascii(
241cdf0e10cSrcweir &tmp_file_path,
242cdf0e10cSrcweir &capacity,
243cdf0e10cSrcweir len_base_dir,
244cdf0e10cSrcweir "/",
245cdf0e10cSrcweir 1);
246cdf0e10cSrcweir
247cdf0e10cSrcweir offset_file_name++;
248cdf0e10cSrcweir }
249cdf0e10cSrcweir
250cdf0e10cSrcweir while(1) /* try until success */
251cdf0e10cSrcweir {
252cdf0e10cSrcweir osl_gen_random_name_impl_(&rand_name);
253cdf0e10cSrcweir
254cdf0e10cSrcweir rtl_uStringbuffer_insert(
255cdf0e10cSrcweir &tmp_file_path,
256cdf0e10cSrcweir &capacity,
257cdf0e10cSrcweir offset_file_name,
258cdf0e10cSrcweir rtl_uString_getStr(rand_name),
259cdf0e10cSrcweir rtl_uString_getLength(rand_name));
260cdf0e10cSrcweir
261cdf0e10cSrcweir osl_error = osl_getFileURLFromSystemPath(
262cdf0e10cSrcweir tmp_file_path, &tmp_file_url);
263cdf0e10cSrcweir
264cdf0e10cSrcweir if (osl_File_E_None == osl_error)
265cdf0e10cSrcweir {
266cdf0e10cSrcweir /* RW permission for the user only! */
267cdf0e10cSrcweir mode_t old_mode = umask(077);
268cdf0e10cSrcweir
269cdf0e10cSrcweir osl_error = osl_openFile(
270cdf0e10cSrcweir tmp_file_url,
271cdf0e10cSrcweir file_handle,
272cdf0e10cSrcweir osl_File_OpenFlag_Read |
273cdf0e10cSrcweir osl_File_OpenFlag_Write |
274cdf0e10cSrcweir osl_File_OpenFlag_Create);
275cdf0e10cSrcweir
276cdf0e10cSrcweir umask(old_mode);
277cdf0e10cSrcweir }
278cdf0e10cSrcweir
279cdf0e10cSrcweir /* in case of error osl_File_E_EXIST we simply try again else we give up */
280cdf0e10cSrcweir
281cdf0e10cSrcweir if ((osl_File_E_None == osl_error) || (osl_error != osl_File_E_EXIST))
282cdf0e10cSrcweir {
283cdf0e10cSrcweir if (rand_name)
284cdf0e10cSrcweir rtl_uString_release(rand_name);
285cdf0e10cSrcweir
286cdf0e10cSrcweir if (tmp_file_url)
287cdf0e10cSrcweir rtl_uString_release(tmp_file_url);
288cdf0e10cSrcweir
289cdf0e10cSrcweir break;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir } /* while(1) */
292cdf0e10cSrcweir
293cdf0e10cSrcweir if (osl_File_E_None == osl_error)
294cdf0e10cSrcweir rtl_uString_assign(ppustr_temp_file_name, tmp_file_path);
295cdf0e10cSrcweir
296cdf0e10cSrcweir if (tmp_file_path)
297cdf0e10cSrcweir rtl_uString_release(tmp_file_path);
298cdf0e10cSrcweir
299cdf0e10cSrcweir return osl_error;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir
302cdf0e10cSrcweir /*****************************************************************
303cdf0e10cSrcweir * osl_createTempFile
304cdf0e10cSrcweir *****************************************************************/
305cdf0e10cSrcweir
osl_createTempFile(rtl_uString * pustrDirectoryURL,oslFileHandle * pHandle,rtl_uString ** ppustrTempFileURL)306cdf0e10cSrcweir oslFileError SAL_CALL osl_createTempFile(
307cdf0e10cSrcweir rtl_uString* pustrDirectoryURL,
308cdf0e10cSrcweir oslFileHandle* pHandle,
309cdf0e10cSrcweir rtl_uString** ppustrTempFileURL)
310cdf0e10cSrcweir {
311*509a48ffSpfg rtl_uString* base_directory = NULL;
312*509a48ffSpfg rtl_uString* temp_file_name = NULL;
313cdf0e10cSrcweir oslFileHandle temp_file_handle;
314cdf0e10cSrcweir sal_Bool b_delete_on_close;
315cdf0e10cSrcweir oslFileError osl_error;
316cdf0e10cSrcweir
317cdf0e10cSrcweir osl_error = osl_setup_createTempFile_impl_(
318cdf0e10cSrcweir pustrDirectoryURL,
319cdf0e10cSrcweir pHandle,
320cdf0e10cSrcweir ppustrTempFileURL,
321cdf0e10cSrcweir &base_directory,
322cdf0e10cSrcweir &b_delete_on_close);
323cdf0e10cSrcweir
324cdf0e10cSrcweir if (osl_File_E_None != osl_error)
325cdf0e10cSrcweir return osl_error;
326cdf0e10cSrcweir
327cdf0e10cSrcweir osl_error = osl_create_temp_file_impl_(
328cdf0e10cSrcweir base_directory, &temp_file_handle, &temp_file_name);
329cdf0e10cSrcweir
330cdf0e10cSrcweir if (osl_File_E_None == osl_error)
331cdf0e10cSrcweir {
332*509a48ffSpfg rtl_uString* temp_file_url = NULL;
333cdf0e10cSrcweir
334cdf0e10cSrcweir /* assuming this works */
335cdf0e10cSrcweir osl_getFileURLFromSystemPath(temp_file_name, &temp_file_url);
336cdf0e10cSrcweir
337cdf0e10cSrcweir if (b_delete_on_close)
338cdf0e10cSrcweir {
339cdf0e10cSrcweir osl_error = osl_removeFile(temp_file_url);
340cdf0e10cSrcweir
341cdf0e10cSrcweir if (osl_File_E_None == osl_error)
342cdf0e10cSrcweir *pHandle = temp_file_handle;
343cdf0e10cSrcweir else
344cdf0e10cSrcweir osl_closeFile(temp_file_handle);
345cdf0e10cSrcweir }
346cdf0e10cSrcweir else
347cdf0e10cSrcweir {
348cdf0e10cSrcweir if (pHandle)
349cdf0e10cSrcweir *pHandle = temp_file_handle;
350cdf0e10cSrcweir else
351cdf0e10cSrcweir osl_closeFile(temp_file_handle);
352cdf0e10cSrcweir
353cdf0e10cSrcweir rtl_uString_assign(ppustrTempFileURL, temp_file_url);
354cdf0e10cSrcweir }
355cdf0e10cSrcweir
356cdf0e10cSrcweir if (temp_file_url)
357cdf0e10cSrcweir rtl_uString_release(temp_file_url);
358cdf0e10cSrcweir
359cdf0e10cSrcweir if (temp_file_name)
360cdf0e10cSrcweir rtl_uString_release(temp_file_name);
361cdf0e10cSrcweir }
362cdf0e10cSrcweir
363cdf0e10cSrcweir if (base_directory)
364cdf0e10cSrcweir rtl_uString_release(base_directory);
365cdf0e10cSrcweir
366cdf0e10cSrcweir return osl_error;
367cdf0e10cSrcweir }
368