Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / unx / tempfile.c
blob2aad4bbba613fc4c0a0ab940ed887366d1ec8d11
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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/time.h>
25 #include "system.h"
26 #include <osl/file.h>
27 #include <osl/thread.h>
28 #include <rtl/ustrbuf.h>
29 #include <osl/diagnose.h>
30 #include <sal/macros.h>
32 #include "file_url.h"
34 oslFileError SAL_CALL osl_getTempDirURL( rtl_uString** pustrTempDir )
36 oslFileError error;
37 /* described in environ(7) */
38 const char *pValue = getenv( "TMPDIR" );
39 rtl_uString *ustrTempPath = NULL;
41 if ( !pValue )
42 pValue = getenv( "TEMP" );
44 if ( !pValue )
45 pValue = getenv( "TMP" );
47 if ( !pValue )
48 pValue = "/tmp";
50 rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
51 OSL_ASSERT(ustrTempPath != NULL);
52 error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
53 rtl_uString_release( ustrTempPath );
55 return error;
58 /******************************************************************
59 * Generates a random unique file name. We're using the scheme
60 * from the standard c-lib function mkstemp to generate a more
61 * or less random unique file name
63 * @param rand_name
64 * receives the random name
65 ******************************************************************/
67 static const char LETTERS[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
68 static const int COUNT_OF_LETTERS = SAL_N_ELEMENTS(LETTERS) - 1;
70 #define RAND_NAME_LENGTH 6
72 static void osl_gen_random_name_impl_(rtl_uString** rand_name)
74 static uint64_t value;
76 char buffer[RAND_NAME_LENGTH];
77 struct timeval tv;
78 uint64_t v;
79 int i;
81 gettimeofday(&tv, NULL);
83 value += ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
85 v = value;
87 for (i = 0; i < RAND_NAME_LENGTH; i++)
89 buffer[i] = LETTERS[v % COUNT_OF_LETTERS];
90 v /= COUNT_OF_LETTERS;
93 rtl_string2UString(
94 rand_name,
95 buffer,
96 RAND_NAME_LENGTH,
97 RTL_TEXTENCODING_ASCII_US,
98 OSTRING_TO_OUSTRING_CVTFLAGS);
99 OSL_ASSERT(*rand_name != NULL);
102 /*****************************************************************
103 * Helper function
104 * Either use the directory provided or the result of
105 * osl_getTempDirUrl and return it as system path and file url
106 ****************************************************************/
108 static oslFileError osl_setup_base_directory_impl_(
109 rtl_uString* pustrDirectoryURL,
110 rtl_uString** ppustr_base_dir)
112 rtl_uString* dir_url = 0;
113 rtl_uString* dir = 0;
114 oslFileError error = osl_File_E_None;
116 if (pustrDirectoryURL)
117 rtl_uString_assign(&dir_url, pustrDirectoryURL);
118 else
119 error = osl_getTempDirURL(&dir_url);
121 if (osl_File_E_None == error)
123 error = osl_getSystemPathFromFileURL_Ex(dir_url, &dir);
124 rtl_uString_release(dir_url);
127 if (osl_File_E_None == error)
129 rtl_uString_assign(ppustr_base_dir, dir);
130 rtl_uString_release(dir);
133 return error;
136 /*****************************************************************
137 * osl_setup_createTempFile_impl
138 * validate input parameter, setup variables
139 ****************************************************************/
141 static oslFileError osl_setup_createTempFile_impl_(
142 rtl_uString* pustrDirectoryURL,
143 oslFileHandle* pHandle,
144 rtl_uString** ppustrTempFileURL,
145 rtl_uString** ppustr_base_dir,
146 sal_Bool* b_delete_on_close)
148 oslFileError osl_error;
150 OSL_PRECOND(((0 != pHandle) || (0 != ppustrTempFileURL)), "Invalid parameter!");
152 if ((0 == pHandle) && (0 == ppustrTempFileURL))
154 osl_error = osl_File_E_INVAL;
156 else
158 osl_error = osl_setup_base_directory_impl_(
159 pustrDirectoryURL, ppustr_base_dir);
161 *b_delete_on_close = (0 == ppustrTempFileURL);
164 return osl_error;
167 /*****************************************************************
168 * Create a unique file in the specified directory and return
169 * it's name
170 ****************************************************************/
172 static oslFileError osl_create_temp_file_impl_(
173 const rtl_uString* pustr_base_directory,
174 oslFileHandle* file_handle,
175 rtl_uString** ppustr_temp_file_name)
177 rtl_uString* rand_name = 0;
178 sal_uInt32 len_base_dir = 0;
179 rtl_uString* tmp_file_path = 0;
180 rtl_uString* tmp_file_url = 0;
181 sal_Int32 capacity = 0;
182 oslFileError osl_error = osl_File_E_None;
183 sal_Int32 offset_file_name;
184 const sal_Unicode* puchr;
186 OSL_PRECOND(pustr_base_directory, "Invalid Parameter");
187 OSL_PRECOND(file_handle, "Invalid Parameter");
188 OSL_PRECOND(ppustr_temp_file_name, "Invalid Parameter");
190 len_base_dir = rtl_uString_getLength(pustr_base_directory);
192 rtl_uStringbuffer_newFromStr_WithLength(
193 &tmp_file_path,
194 rtl_uString_getStr((rtl_uString*)pustr_base_directory),
195 len_base_dir);
197 rtl_uStringbuffer_ensureCapacity(
198 &tmp_file_path,
199 &capacity,
200 (len_base_dir + 1 + RAND_NAME_LENGTH));
202 offset_file_name = len_base_dir;
204 puchr = rtl_uString_getStr(tmp_file_path);
206 /* ensure that the last character is a '/' */
208 if ((sal_Unicode)'/' != puchr[len_base_dir - 1])
210 rtl_uStringbuffer_insert_ascii(
211 &tmp_file_path,
212 &capacity,
213 len_base_dir,
214 "/",
217 offset_file_name++;
220 while(1) /* try until success */
222 osl_gen_random_name_impl_(&rand_name);
224 rtl_uStringbuffer_insert(
225 &tmp_file_path,
226 &capacity,
227 offset_file_name,
228 rtl_uString_getStr(rand_name),
229 rtl_uString_getLength(rand_name));
231 osl_error = osl_getFileURLFromSystemPath(
232 tmp_file_path, &tmp_file_url);
234 if (osl_File_E_None == osl_error)
236 /* RW permission for the user only! */
237 mode_t old_mode = umask(077);
239 osl_error = osl_openFile(
240 tmp_file_url,
241 file_handle,
242 osl_File_OpenFlag_Read |
243 osl_File_OpenFlag_Write |
244 osl_File_OpenFlag_Create);
246 umask(old_mode);
249 /* in case of error osl_File_E_EXIST we simply try again else we give up */
251 if ((osl_File_E_None == osl_error) || (osl_error != osl_File_E_EXIST))
253 if (rand_name)
254 rtl_uString_release(rand_name);
256 if (tmp_file_url)
257 rtl_uString_release(tmp_file_url);
259 break;
261 } /* while(1) */
263 if (osl_File_E_None == osl_error)
264 rtl_uString_assign(ppustr_temp_file_name, tmp_file_path);
266 if (tmp_file_path)
267 rtl_uString_release(tmp_file_path);
269 return osl_error;
272 oslFileError SAL_CALL osl_createTempFile(
273 rtl_uString* pustrDirectoryURL,
274 oslFileHandle* pHandle,
275 rtl_uString** ppustrTempFileURL)
277 rtl_uString* base_directory = 0;
278 rtl_uString* temp_file_name = 0;
279 oslFileHandle temp_file_handle;
280 sal_Bool b_delete_on_close;
281 oslFileError osl_error;
283 osl_error = osl_setup_createTempFile_impl_(
284 pustrDirectoryURL,
285 pHandle,
286 ppustrTempFileURL,
287 &base_directory,
288 &b_delete_on_close);
290 if (osl_File_E_None != osl_error)
291 return osl_error;
293 osl_error = osl_create_temp_file_impl_(
294 base_directory, &temp_file_handle, &temp_file_name);
296 if (osl_File_E_None == osl_error)
298 rtl_uString* temp_file_url = 0;
300 /* assuming this works */
301 osl_getFileURLFromSystemPath(temp_file_name, &temp_file_url);
303 if (b_delete_on_close)
305 osl_error = osl_removeFile(temp_file_url);
307 if (osl_File_E_None == osl_error)
308 *pHandle = temp_file_handle;
309 else
310 osl_closeFile(temp_file_handle);
312 else
314 if (pHandle)
315 *pHandle = temp_file_handle;
316 else
317 osl_closeFile(temp_file_handle);
319 rtl_uString_assign(ppustrTempFileURL, temp_file_url);
322 if (temp_file_url)
323 rtl_uString_release(temp_file_url);
325 if (temp_file_name)
326 rtl_uString_release(temp_file_name);
329 if (base_directory)
330 rtl_uString_release(base_directory);
332 return osl_error;
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */