LCOV - code coverage report
Current view: top level - sal/osl/unx - tempfile.c (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 90 100 90.0 %
Date: 2014-04-11 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      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>
      31             : 
      32             : #include "file_url.h"
      33             : 
      34        1592 : oslFileError SAL_CALL osl_getTempDirURL( rtl_uString** pustrTempDir )
      35             : {
      36             :     oslFileError error;
      37             :     /* described in environ(7) */
      38        1592 :     const char *pValue = getenv( "TMPDIR" );
      39        1592 :     rtl_uString *ustrTempPath = NULL;
      40             : 
      41        1592 :     if ( !pValue )
      42           0 :         pValue = getenv( "TEMP" );
      43             : 
      44        1592 :     if ( !pValue )
      45           0 :         pValue = getenv( "TMP" );
      46             : 
      47        1592 :     if ( !pValue )
      48           0 :         pValue = "/tmp";
      49             : 
      50        1592 :     rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
      51             :     OSL_ASSERT(ustrTempPath != NULL);
      52        1592 :     error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
      53        1592 :     rtl_uString_release( ustrTempPath );
      54             : 
      55        1592 :     return error;
      56             : }
      57             : 
      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
      62             :  *
      63             :  * @param rand_name
      64             :  *        receives the random name
      65             :  ******************************************************************/
      66             : 
      67             : static const char LETTERS[]        = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
      68             : static const int  COUNT_OF_LETTERS = SAL_N_ELEMENTS(LETTERS) - 1;
      69             : 
      70             : #define RAND_NAME_LENGTH 6
      71             : 
      72       21707 : static void osl_gen_random_name_impl_(rtl_uString** rand_name)
      73             : {
      74             :     static uint64_t value;
      75             : 
      76             :     char     buffer[RAND_NAME_LENGTH];
      77             :     struct   timeval tv;
      78             :     uint64_t v;
      79             :     int      i;
      80             : 
      81       21707 :     gettimeofday(&tv, NULL);
      82             : 
      83       21707 :     value += ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
      84             : 
      85       21707 :     v = value;
      86             : 
      87      151949 :     for (i = 0; i < RAND_NAME_LENGTH; i++)
      88             :     {
      89      130242 :         buffer[i] = LETTERS[v % COUNT_OF_LETTERS];
      90      130242 :         v        /= COUNT_OF_LETTERS;
      91             :     }
      92             : 
      93       21707 :     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);
     100       21707 : }
     101             : 
     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             :  ****************************************************************/
     107             : 
     108       21707 : static oslFileError osl_setup_base_directory_impl_(
     109             :     rtl_uString*  pustrDirectoryURL,
     110             :     rtl_uString** ppustr_base_dir)
     111             : {
     112       21707 :     rtl_uString* dir_url = 0;
     113       21707 :     rtl_uString* dir     = 0;
     114       21707 :     oslFileError error   = osl_File_E_None;
     115             : 
     116       21707 :     if (pustrDirectoryURL)
     117       20323 :         rtl_uString_assign(&dir_url, pustrDirectoryURL);
     118             :     else
     119        1384 :         error = osl_getTempDirURL(&dir_url);
     120             : 
     121       21707 :     if (osl_File_E_None == error)
     122             :     {
     123       21707 :         error = osl_getSystemPathFromFileURL_Ex(dir_url, &dir);
     124       21707 :         rtl_uString_release(dir_url);
     125             :     }
     126             : 
     127       21707 :     if (osl_File_E_None == error)
     128             :     {
     129       21707 :         rtl_uString_assign(ppustr_base_dir, dir);
     130       21707 :         rtl_uString_release(dir);
     131             :     }
     132             : 
     133       21707 :     return error;
     134             : }
     135             : 
     136             : /*****************************************************************
     137             :  * osl_setup_createTempFile_impl
     138             :  * validate input parameter, setup variables
     139             :  ****************************************************************/
     140             : 
     141       21707 :  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)
     147             :  {
     148             :      oslFileError osl_error;
     149             : 
     150             :     OSL_PRECOND(((0 != pHandle) || (0 != ppustrTempFileURL)), "Invalid parameter!");
     151             : 
     152       21707 :     if ((0 == pHandle) && (0 == ppustrTempFileURL))
     153             :     {
     154           0 :         osl_error = osl_File_E_INVAL;
     155             :     }
     156             :     else
     157             :     {
     158       21707 :         osl_error = osl_setup_base_directory_impl_(
     159             :             pustrDirectoryURL, ppustr_base_dir);
     160             : 
     161       21707 :         *b_delete_on_close = (0 == ppustrTempFileURL);
     162             :     }
     163             : 
     164       21707 :     return osl_error;
     165             :  }
     166             : 
     167             : /*****************************************************************
     168             :  * Create a unique file in the specified directory and return
     169             :  * its name
     170             :  ****************************************************************/
     171             : 
     172       21707 : 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)
     176             : {
     177       21707 :     rtl_uString*        rand_name        = 0;
     178       21707 :     sal_uInt32          len_base_dir     = 0;
     179       21707 :     rtl_uString*        tmp_file_path    = 0;
     180       21707 :     rtl_uString*        tmp_file_url     = 0;
     181       21707 :     sal_Int32           capacity         = 0;
     182       21707 :     oslFileError        osl_error        = osl_File_E_None;
     183             :     sal_Int32           offset_file_name;
     184             :     const sal_Unicode*  puchr;
     185             : 
     186             :     OSL_PRECOND(pustr_base_directory, "Invalid Parameter");
     187             :     OSL_PRECOND(file_handle, "Invalid Parameter");
     188             :     OSL_PRECOND(ppustr_temp_file_name, "Invalid Parameter");
     189             : 
     190       21707 :     len_base_dir = rtl_uString_getLength(pustr_base_directory);
     191             : 
     192       43414 :     rtl_uStringbuffer_newFromStr_WithLength(
     193             :         &tmp_file_path,
     194       21707 :         rtl_uString_getStr((rtl_uString*)pustr_base_directory),
     195             :         len_base_dir);
     196             : 
     197       21707 :     rtl_uStringbuffer_ensureCapacity(
     198             :         &tmp_file_path,
     199             :         &capacity,
     200       21707 :         (len_base_dir + 1 + RAND_NAME_LENGTH));
     201             : 
     202       21707 :     offset_file_name = len_base_dir;
     203             : 
     204       21707 :     puchr = rtl_uString_getStr(tmp_file_path);
     205             : 
     206             :     /* ensure that the last character is a '/' */
     207             : 
     208       21707 :     if ((sal_Unicode)'/' != puchr[len_base_dir - 1])
     209             :     {
     210       21651 :         rtl_uStringbuffer_insert_ascii(
     211             :             &tmp_file_path,
     212             :             &capacity,
     213             :             len_base_dir,
     214             :             "/",
     215             :             1);
     216             : 
     217       21651 :         offset_file_name++;
     218             :     }
     219             : 
     220             :     while(1) /* try until success */
     221             :     {
     222       21707 :         osl_gen_random_name_impl_(&rand_name);
     223             : 
     224       43414 :         rtl_uStringbuffer_insert(
     225             :             &tmp_file_path,
     226             :             &capacity,
     227             :             offset_file_name,
     228       21707 :             rtl_uString_getStr(rand_name),
     229             :             rtl_uString_getLength(rand_name));
     230             : 
     231       21707 :         osl_error = osl_getFileURLFromSystemPath(
     232             :             tmp_file_path, &tmp_file_url);
     233             : 
     234       21707 :         if (osl_File_E_None == osl_error)
     235             :         {
     236             :             /* RW permission for the user only! */
     237       21707 :             mode_t old_mode = umask(077);
     238             : 
     239       21707 :             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);
     245             : 
     246       21707 :             umask(old_mode);
     247             :         }
     248             : 
     249             :         /* in case of error osl_File_E_EXIST we simply try again else we give up */
     250             : 
     251       21707 :         if ((osl_File_E_None == osl_error) || (osl_error != osl_File_E_EXIST))
     252             :         {
     253       21707 :             rtl_uString_release(rand_name);
     254             : 
     255       21707 :             if (tmp_file_url)
     256       21707 :                 rtl_uString_release(tmp_file_url);
     257             : 
     258       21707 :             break;
     259             :         }
     260           0 :     } /* while(1) */
     261             : 
     262       21707 :     if (osl_File_E_None == osl_error)
     263       21707 :         rtl_uString_assign(ppustr_temp_file_name, tmp_file_path);
     264             : 
     265       21707 :     rtl_uString_release(tmp_file_path);
     266             : 
     267       21707 :     return osl_error;
     268             : }
     269             : 
     270       21707 : oslFileError SAL_CALL osl_createTempFile(
     271             :     rtl_uString*   pustrDirectoryURL,
     272             :     oslFileHandle* pHandle,
     273             :     rtl_uString**  ppustrTempFileURL)
     274             : {
     275       21707 :     rtl_uString*  base_directory     = 0;
     276       21707 :     rtl_uString*  temp_file_name     = 0;
     277             :     oslFileHandle temp_file_handle;
     278             :     sal_Bool      b_delete_on_close;
     279             :     oslFileError  osl_error;
     280             : 
     281       21707 :     osl_error = osl_setup_createTempFile_impl_(
     282             :         pustrDirectoryURL,
     283             :         pHandle,
     284             :         ppustrTempFileURL,
     285             :         &base_directory,
     286             :         &b_delete_on_close);
     287             : 
     288       21707 :     if (osl_File_E_None != osl_error)
     289           0 :         return osl_error;
     290             : 
     291       21707 :     osl_error = osl_create_temp_file_impl_(
     292             :         base_directory, &temp_file_handle, &temp_file_name);
     293             : 
     294       21707 :     if (osl_File_E_None == osl_error)
     295             :     {
     296       21707 :         rtl_uString* temp_file_url = 0;
     297             : 
     298             :         /* assuming this works */
     299       21707 :         osl_getFileURLFromSystemPath(temp_file_name, &temp_file_url);
     300             : 
     301       21707 :         if (b_delete_on_close)
     302             :         {
     303           0 :             osl_error = osl_removeFile(temp_file_url);
     304             : 
     305           0 :             if (osl_File_E_None == osl_error)
     306           0 :                 *pHandle = temp_file_handle;
     307             :             else
     308           0 :                 osl_closeFile(temp_file_handle);
     309             :         }
     310             :         else
     311             :         {
     312       21707 :             if (pHandle)
     313       20173 :                 *pHandle = temp_file_handle;
     314             :             else
     315        1534 :                 osl_closeFile(temp_file_handle);
     316             : 
     317       21707 :             rtl_uString_assign(ppustrTempFileURL, temp_file_url);
     318             :         }
     319             : 
     320       21707 :         rtl_uString_release(temp_file_url);
     321             : 
     322       21707 :         rtl_uString_release(temp_file_name);
     323             :     }
     324             : 
     325       21707 :     rtl_uString_release(base_directory);
     326             : 
     327       21707 :     return osl_error;
     328             : }
     329             : 
     330             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10