summaryrefslogtreecommitdiff
path: root/sal/osl/unx/tempfile.c
blob: fdb4833dee6d26b193a0124aa5c4e4312140a624 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: tempfile.c,v $
 * $Revision: 1.11 $
 *
 * 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.
 *
 ************************************************************************/

/*****************************************************************/
/* Includes                                                      */
/*****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "system.h"
#include <osl/file.h>
#include <osl/thread.h>
#include <rtl/ustrbuf.h>
#include <osl/diagnose.h>

#ifndef _FILE_URL_H_
#include "file_url.h"
#endif
 
/*****************************************************************/
/* osl_getTempFirURL                                             */
/*****************************************************************/

oslFileError SAL_CALL osl_getTempDirURL( rtl_uString** pustrTempDir )
{
#ifdef MACOSX
    const char *pValue = getenv( "TMPDIR" );

    /* If TMPDIR environment variable is not set, use "/tmp" instead
       of P_tmpdir because its value is "/var/tmp" and it is not
       deleted on system start up */
    if ( !pValue )
        pValue = "/tmp";
#else

    const char *pValue = getenv( "TEMP" );

    if ( !pValue )
    {
        pValue = getenv( "TMP" );
#if defined(SOLARIS) || defined (LINUX) || defined (FREEBSD)
        if ( !pValue )
            pValue = P_tmpdir;
#endif
    }
#endif /* MACOSX */

    if ( pValue )
    {
        oslFileError error;
        rtl_uString	*ustrTempPath = NULL;

        rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
        OSL_ASSERT(ustrTempPath != NULL);
        error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
        rtl_uString_release( ustrTempPath );
        
        return error;
    }
    else
        return osl_File_E_NOENT;
}

/******************************************************************
 * Generates a random unique file name. We're using the scheme   
 * from the standard c-lib function mkstemp to generate a more   
 * or less random unique file name                               
 *                                                               
 * @param rand_name                                              
 *        receives the random name                               
 ******************************************************************/

static const char LETTERS[]        = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
static const int  COUNT_OF_LETTERS = sizeof(LETTERS)/sizeof(LETTERS[0]) - 1;

#define RAND_NAME_LENGTH 6

static void osl_gen_random_name_impl_(rtl_uString** rand_name)
{	   
    static uint64_t value; 
    
    char     buffer[RAND_NAME_LENGTH];
    struct   timeval tv;
    uint64_t v;
    int      i;
    
    gettimeofday(&tv, NULL);
    
    value += ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
    
    v = value;
    
    for (i = 0; i < RAND_NAME_LENGTH; i++)
    {
        buffer[i] = LETTERS[v % COUNT_OF_LETTERS];
        v        /= COUNT_OF_LETTERS;
    }
    
    rtl_string2UString(
            rand_name, 
            buffer,
            RAND_NAME_LENGTH,
            RTL_TEXTENCODING_ASCII_US,
            OSTRING_TO_OUSTRING_CVTFLAGS);
    OSL_ASSERT(*rand_name != NULL);
}

/*****************************************************************
 * Helper function                                               
 * Either use the directory provided or the result of            
 * osl_getTempDirUrl and return it as system path and file url   
 ****************************************************************/

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_Ex(dir_url, &dir, FURL_DENY_RELATIVE);       						
        rtl_uString_release(dir_url);
    }
        
    if (osl_File_E_None == error)
    {
        rtl_uString_assign(ppustr_base_dir, dir);
        rtl_uString_release(dir);
    }
    
    return error;	
}

/*****************************************************************
 * osl_setup_createTempFile_impl
 * validate input parameter, setup variables
 ****************************************************************/
 
 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 = (0 == ppustrTempFileURL);
    }
    
    return osl_error;
 }
 
/*****************************************************************
 * Create a unique file in the specified directory and return    
 * it's name                                                     
 ****************************************************************/

static oslFileError osl_create_temp_file_impl_(
    const rtl_uString* pustr_base_directory, 
    oslFileHandle* file_handle,
    rtl_uString** ppustr_temp_file_name)
{
    rtl_uString*        rand_name        = 0;
    sal_uInt32          len_base_dir     = 0;
    rtl_uString*        tmp_file_path    = 0;
    rtl_uString*        tmp_file_url     = 0;
    sal_Int32           capacity         = 0;
    oslFileError        osl_error        = osl_File_E_None;
    sal_Int32           offset_file_name;
    const sal_Unicode*  puchr;
        
    OSL_PRECOND(pustr_base_directory, "Invalid Parameter");
    OSL_PRECOND(file_handle, "Invalid Parameter");
    OSL_PRECOND(ppustr_temp_file_name, "Invalid Parameter");
    
    len_base_dir = rtl_uString_getLength(pustr_base_directory);
    
    rtl_uStringbuffer_newFromStr_WithLength(
        &tmp_file_path, 
        rtl_uString_getStr((rtl_uString*)pustr_base_directory),
        len_base_dir);
                        
    rtl_uStringbuffer_ensureCapacity(
        &tmp_file_path, 
        &capacity, 
        (len_base_dir + 1 + RAND_NAME_LENGTH));
        
    offset_file_name = len_base_dir;
        
    puchr = rtl_uString_getStr(tmp_file_path);
    
    /* ensure that the last character is a '/' */
    
    if ((sal_Unicode)'/' != puchr[len_base_dir - 1])
    {		
        rtl_uStringbuffer_insert_ascii(
            &tmp_file_path, 
            &capacity, 
            len_base_dir,
            "/",
            1);
        
        offset_file_name++;					
    }
         
    while(1) /* try until success */
    {							
        osl_gen_random_name_impl_(&rand_name);
                    
        rtl_uStringbuffer_insert(
            &tmp_file_path,
            &capacity,
            offset_file_name,
            rtl_uString_getStr(rand_name),
            rtl_uString_getLength(rand_name));

        osl_error = osl_getFileURLFromSystemPath(
            tmp_file_path, &tmp_file_url);				
        
        if (osl_File_E_None == osl_error)
        {
            /* RW permission for the user only! */
            mode_t old_mode = umask(077);
            
            osl_error = osl_openFile(
                tmp_file_url, 
                file_handle, 
                osl_File_OpenFlag_Read | 
                osl_File_OpenFlag_Write | 
                osl_File_OpenFlag_Create);
                
            umask(old_mode);
        }
        
        /* in case of error osl_File_E_EXIST we simply try again else we give up */	
        
        if ((osl_File_E_None == osl_error) || (osl_error != osl_File_E_EXIST))
        {
            if (rand_name)
                rtl_uString_release(rand_name);
            
            if (tmp_file_url)
                rtl_uString_release(tmp_file_url);
                                
            break;
        }					
    } /* while(1) */
    
    if (osl_File_E_None == osl_error)
        rtl_uString_assign(ppustr_temp_file_name, tmp_file_path);
    
    if (tmp_file_path)
        rtl_uString_release(tmp_file_path);
        
    return osl_error;
}

/*****************************************************************
 * osl_createTempFile                                            
 *****************************************************************/

oslFileError SAL_CALL osl_createTempFile(
    rtl_uString*   pustrDirectoryURL,
    oslFileHandle* pHandle, 
    rtl_uString**  ppustrTempFileURL)
{
    rtl_uString*  base_directory     = 0;	
    rtl_uString*  temp_file_name     = 0;
    oslFileHandle temp_file_handle;
    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;
        
    osl_error = osl_create_temp_file_impl_(
        base_directory, &temp_file_handle, &temp_file_name);
                                
    if (osl_File_E_None == osl_error) 
    {		
        rtl_uString* temp_file_url = 0;		
        
        /* assuming this works */
        osl_getFileURLFromSystemPath(temp_file_name, &temp_file_url);
                                                
        if (b_delete_on_close)
        {			
            osl_error = osl_removeFile(temp_file_url);
            
            if (osl_File_E_None == osl_error)
                *pHandle = temp_file_handle;
            else
                osl_closeFile(temp_file_handle);
        }
        else 
        {		
            if (pHandle)
                *pHandle = temp_file_handle;				
            else
                osl_closeFile(temp_file_handle);	
                                     
            rtl_uString_assign(ppustrTempFileURL, temp_file_url);                                       
        }
        
        if (temp_file_url)
            rtl_uString_release(temp_file_url);
                
        if (temp_file_name)
            rtl_uString_release(temp_file_name);
    }
    
    if (base_directory)
        rtl_uString_release(base_directory);      
        
    return osl_error;
}