LCOV - code coverage report
Current view: top level - unotools/source/ucbhelper - tempfile.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 222 0.0 %
Date: 2014-04-14 Functions: 0 18 0.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 "sal/config.h"
      21             : 
      22             : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include <unotools/tempfile.hxx>
      25             : #include <unotools/localfilehelper.hxx>
      26             : #include <unotools/ucbstreamhelper.hxx>
      27             : #include <ucbhelper/fileidentifierconverter.hxx>
      28             : #include <rtl/ustring.hxx>
      29             : #include <rtl/instance.hxx>
      30             : #include <osl/file.hxx>
      31             : #include <tools/time.hxx>
      32             : #include <tools/debug.hxx>
      33             : #include <stdio.h>
      34             : 
      35             : #ifdef UNX
      36             : #include <sys/stat.h>
      37             : #endif
      38             : 
      39             : using namespace osl;
      40             : 
      41             : namespace
      42             : {
      43             :     struct TempNameBase_Impl
      44             :         : public rtl::Static< OUString, TempNameBase_Impl > {};
      45             : }
      46             : 
      47             : namespace utl
      48             : {
      49             : 
      50           0 : struct TempFile_Impl
      51             : {
      52             :     OUString    aName;
      53             :     OUString    aURL;
      54             :     SvStream*   pStream;
      55             :     bool        bIsDirectory;
      56             : 
      57           0 :     TempFile_Impl()
      58             :         : pStream(0)
      59           0 :         , bIsDirectory(false)
      60             :     {
      61           0 :     }
      62             : };
      63             : 
      64           0 : OUString getParentName( const OUString& aFileName )
      65             : {
      66           0 :     sal_Int32 lastIndex = aFileName.lastIndexOf( '/' );
      67           0 :     OUString aParent = aFileName.copy( 0, lastIndex );
      68             : 
      69           0 :     if( aParent.endsWith(":") && aParent.getLength() == 6 )
      70           0 :         aParent += "/";
      71             : 
      72           0 :     if( aParent.equalsAscii( "file://" ) )
      73           0 :         aParent = "file:///";
      74             : 
      75           0 :     return aParent;
      76             : }
      77             : 
      78           0 : bool ensuredir( const OUString& rUnqPath )
      79             : {
      80           0 :     OUString aPath;
      81           0 :     if ( rUnqPath.isEmpty() )
      82           0 :         return false;
      83             : 
      84             :     // remove trailing slash
      85           0 :     if ( rUnqPath.endsWith("/") )
      86           0 :         aPath = rUnqPath.copy( 0, rUnqPath.getLength() - 1 );
      87             :     else
      88           0 :         aPath = rUnqPath;
      89             : 
      90             :     // HACK: create directory on a mount point with nobrowse option
      91             :     // returns ENOSYS in any case !!
      92           0 :     osl::Directory aDirectory( aPath );
      93             : #ifdef UNX
      94             : /* RW permission for the user only! */
      95           0 :  mode_t old_mode = umask(077);
      96             : #endif
      97           0 :     osl::FileBase::RC nError = aDirectory.open();
      98             : #ifdef UNX
      99           0 : umask(old_mode);
     100             : #endif
     101           0 :     aDirectory.close();
     102           0 :     if( nError == osl::File::E_None )
     103           0 :         return true;
     104             : 
     105             :     // try to create the directory
     106           0 :     nError = osl::Directory::create( aPath );
     107           0 :     bool  bSuccess = ( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST );
     108           0 :     if( !bSuccess )
     109             :     {
     110             :         // perhaps parent(s) don't exist
     111           0 :         OUString aParentDir = getParentName( aPath );
     112           0 :         if ( aParentDir != aPath )
     113             :         {
     114           0 :             bSuccess = ensuredir( getParentName( aPath ) );
     115             : 
     116             :             // After parent directory structure exists try it one's more
     117           0 :             if ( bSuccess )
     118             :             {
     119             :                 // Parent directory exists, retry creation of directory
     120           0 :                 nError = osl::Directory::create( aPath );
     121           0 :                 bSuccess =( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST );
     122             :             }
     123           0 :         }
     124             :     }
     125             : 
     126           0 :     return bSuccess;
     127             : }
     128             : 
     129           0 : OUString ConstructTempDir_Impl( const OUString* pParent )
     130             : {
     131           0 :     OUString aName;
     132           0 :     if ( pParent && !pParent->isEmpty() )
     133             :     {
     134             :         com::sun::star::uno::Reference<
     135             :             com::sun::star::ucb::XUniversalContentBroker > pBroker(
     136             :                 com::sun::star::ucb::UniversalContentBroker::create(
     137           0 :                     comphelper::getProcessComponentContext() ) );
     138             : 
     139             :         // if parent given try to use it
     140           0 :         OUString aTmp( *pParent );
     141             : 
     142             :         // test for valid filename
     143           0 :         OUString aRet;
     144             :         ::osl::FileBase::getFileURLFromSystemPath(
     145             :             ::ucbhelper::getSystemPathFromFileURL( pBroker, aTmp ),
     146           0 :             aRet );
     147           0 :         if ( !aRet.isEmpty() )
     148             :         {
     149           0 :             ::osl::DirectoryItem aItem;
     150           0 :             sal_Int32 i = aRet.getLength();
     151           0 :             if ( aRet[i-1] == '/' )
     152           0 :                 i--;
     153             : 
     154           0 :             if ( DirectoryItem::get( aRet.copy(0, i), aItem ) == FileBase::E_None )
     155           0 :                 aName = aRet;
     156           0 :         }
     157             :     }
     158             : 
     159           0 :     if ( aName.isEmpty() )
     160             :     {
     161           0 :         OUString &rTempNameBase_Impl = TempNameBase_Impl::get();
     162           0 :         if (rTempNameBase_Impl.isEmpty())
     163             :         {
     164           0 :             OUString ustrTempDirURL;
     165             :             ::osl::FileBase::RC rc = ::osl::File::getTempDirURL(
     166           0 :                 ustrTempDirURL );
     167           0 :             if (rc == ::osl::FileBase::E_None)
     168           0 :                 rTempNameBase_Impl = ustrTempDirURL;
     169             :         }
     170             :         // if no parent or invalid parent : use default directory
     171             :         DBG_ASSERT( !rTempNameBase_Impl.isEmpty(), "No TempDir!" );
     172           0 :         aName = rTempNameBase_Impl;
     173           0 :         ensuredir( aName );
     174             :     }
     175             : 
     176             :     // Make sure that directory ends with a separator
     177           0 :     if( !aName.isEmpty() && !aName.endsWith("/") )
     178           0 :         aName += "/";
     179             : 
     180           0 :     return aName;
     181             : }
     182             : 
     183           0 : void CreateTempName_Impl( OUString& rName, bool bKeep, bool bDir = true )
     184             : {
     185             :     // add a suitable tempname
     186             :     // 36 ** 6 == 2176782336
     187           0 :     unsigned const nRadix = 36;
     188           0 :     unsigned long const nMax = (nRadix*nRadix*nRadix*nRadix*nRadix*nRadix);
     189           0 :     OUString aName;
     190           0 :     OUString aEyeCatcher = "lu";
     191             : #ifdef DBG_UTIL
     192             : #ifdef UNX
     193             :     const char* eye = getenv("LO_TESTNAME");
     194             :     if(eye)
     195             :     {
     196             :         aEyeCatcher = OUString(eye, strlen(eye), RTL_TEXTENCODING_ASCII_US);
     197             :     }
     198             : #endif
     199             : #endif
     200           0 :     aName = rName + aEyeCatcher;
     201           0 :     rName = "";
     202           0 :     static unsigned long u = Time::GetSystemTicks() % nMax;
     203           0 :     for ( unsigned long nSeed = u; ++u != nSeed; )
     204             :     {
     205           0 :         u %= nMax;
     206           0 :         OUString aTmp( aName );
     207           0 :         aTmp += OUString::number(u, nRadix);
     208           0 :         aTmp += ".tmp";
     209             : 
     210           0 :         if ( bDir )
     211             :         {
     212             : #ifdef UNX /* RW permission for the user only! */
     213           0 :             mode_t old_mode = umask(077);
     214             : #endif
     215           0 :             FileBase::RC err = Directory::create( aTmp );
     216             : #ifdef UNX
     217           0 :             umask(old_mode);
     218             : #endif
     219           0 :             if ( err == FileBase::E_None )
     220             :             {
     221             :                 // !bKeep: only for creating a name, not a file or directory
     222           0 :                 if ( bKeep || Directory::remove( aTmp ) == FileBase::E_None )
     223           0 :                     rName = aTmp;
     224           0 :                 break;
     225             :             }
     226           0 :             else if ( err != FileBase::E_EXIST )
     227             :             {
     228             :                 // if f.e. name contains invalid chars stop trying to create dirs
     229           0 :                 break;
     230             :             }
     231             :         }
     232             :         else
     233             :         {
     234             :             DBG_ASSERT( bKeep, "Too expensive, use directory for creating name!" );
     235           0 :             File aFile( aTmp );
     236             : #ifdef UNX /* RW permission for the user only! */
     237           0 :             mode_t old_mode = umask(077);
     238             : #endif
     239           0 :             FileBase::RC err = aFile.open( osl_File_OpenFlag_Create | osl_File_OpenFlag_NoLock );
     240             : #ifdef UNX
     241           0 :             umask(old_mode);
     242             : #endif
     243           0 :             if (  err == FileBase::E_None )
     244             :             {
     245           0 :                 rName = aTmp;
     246           0 :                 aFile.close();
     247           0 :                 break;
     248             :             }
     249           0 :             else if ( err != FileBase::E_EXIST )
     250             :             {
     251             :                  // if f.e. name contains invalid chars stop trying to create files
     252             :                  // but if there is a folder with such name proceed further
     253             : 
     254           0 :                  DirectoryItem aTmpItem;
     255           0 :                  FileStatus aTmpStatus( osl_FileStatus_Mask_Type );
     256           0 :                  if ( DirectoryItem::get( aTmp, aTmpItem ) != FileBase::E_None
     257           0 :                    || aTmpItem.getFileStatus( aTmpStatus ) != FileBase::E_None
     258           0 :                    || aTmpStatus.getFileType() != FileStatus::Directory )
     259           0 :                      break;
     260           0 :             }
     261             :         }
     262           0 :     }
     263           0 : }
     264             : 
     265           0 : void lcl_createName(TempFile_Impl& _rImpl,const OUString& rLeadingChars, bool _bStartWithZero,
     266             :                     const OUString* pExtension, const OUString* pParent, bool bDirectory)
     267             : {
     268           0 :     _rImpl.bIsDirectory = bDirectory;
     269             : 
     270             :     // get correct directory
     271           0 :     OUString aName = ConstructTempDir_Impl( pParent );
     272             : 
     273           0 :     bool bUseNumber = _bStartWithZero;
     274             :     // now use special naming scheme ( name takes leading chars and an index counting up from zero
     275           0 :     aName += rLeadingChars;
     276           0 :     for ( sal_Int32 i=0;; i++ )
     277             :     {
     278           0 :         OUString aTmp( aName );
     279           0 :         if ( bUseNumber )
     280           0 :             aTmp += OUString::number( i );
     281           0 :         bUseNumber = true;
     282           0 :         if ( pExtension )
     283           0 :             aTmp += *pExtension;
     284             :         else
     285           0 :             aTmp += ".tmp";
     286           0 :         if ( bDirectory )
     287             :         {
     288           0 :             FileBase::RC err = Directory::create( aTmp );
     289           0 :             if ( err == FileBase::E_None )
     290             :             {
     291           0 :                 _rImpl.aName = aTmp;
     292           0 :                 break;
     293             :             }
     294           0 :             else if ( err != FileBase::E_EXIST )
     295             :                 // if f.e. name contains invalid chars stop trying to create dirs
     296           0 :                 break;
     297             :         }
     298             :         else
     299             :         {
     300           0 :             File aFile( aTmp );
     301             : #ifdef UNX
     302             : /* RW permission for the user only! */
     303           0 :  mode_t old_mode = umask(077);
     304             : #endif
     305           0 :             FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
     306             : #ifdef UNX
     307           0 : umask(old_mode);
     308             : #endif
     309           0 :             if ( err == FileBase::E_None || err == FileBase::E_NOLCK )
     310             :             {
     311           0 :                 _rImpl.aName = aTmp;
     312           0 :                 aFile.close();
     313           0 :                 break;
     314             :             }
     315           0 :             else if ( err != FileBase::E_EXIST )
     316             :             {
     317             :                 // if f.e. name contains invalid chars stop trying to create dirs
     318             :                 // but if there is a folder with such name proceed further
     319             : 
     320           0 :                 DirectoryItem aTmpItem;
     321           0 :                 FileStatus aTmpStatus( osl_FileStatus_Mask_Type );
     322           0 :                 if ( DirectoryItem::get( aTmp, aTmpItem ) != FileBase::E_None
     323           0 :                   || aTmpItem.getFileStatus( aTmpStatus ) != FileBase::E_None
     324           0 :                   || aTmpStatus.getFileType() != FileStatus::Directory )
     325           0 :                     break;
     326           0 :             }
     327             :         }
     328           0 :         if ( !_bStartWithZero )
     329           0 :             aTmp += OUString::number( i );
     330           0 :     }
     331           0 : }
     332             : 
     333           0 : OUString TempFile::CreateTempName( const OUString* pParent )
     334             : {
     335             :     // get correct directory
     336           0 :     OUString aName = ConstructTempDir_Impl( pParent );
     337             : 
     338             :     // get TempFile name with default naming scheme
     339           0 :     CreateTempName_Impl( aName, false );
     340             : 
     341             :     // convert to file URL
     342           0 :     OUString aTmp;
     343           0 :     if ( !aName.isEmpty() )
     344           0 :         FileBase::getSystemPathFromFileURL( aName, aTmp );
     345           0 :     return aTmp;
     346             : }
     347             : 
     348           0 : TempFile::TempFile( const OUString* pParent, bool bDirectory )
     349           0 :     : pImp( new TempFile_Impl )
     350           0 :     , bKillingFileEnabled( false )
     351             : {
     352           0 :     pImp->bIsDirectory = bDirectory;
     353             : 
     354             :     // get correct directory
     355           0 :     pImp->aName = ConstructTempDir_Impl( pParent );
     356             : 
     357             :     // get TempFile with default naming scheme
     358           0 :     CreateTempName_Impl( pImp->aName, true, bDirectory );
     359           0 : }
     360             : 
     361           0 : TempFile::TempFile( const OUString& rLeadingChars, const OUString* pExtension, const OUString* pParent, bool bDirectory)
     362           0 :     : pImp( new TempFile_Impl )
     363           0 :     , bKillingFileEnabled( false )
     364             : {
     365           0 :     lcl_createName(*pImp, rLeadingChars, true, pExtension, pParent, bDirectory);
     366           0 : }
     367           0 : TempFile::TempFile( const OUString& rLeadingChars, bool _bStartWithZero, const OUString* pExtension, const OUString* pParent, bool bDirectory)
     368           0 :     : pImp( new TempFile_Impl )
     369           0 :     , bKillingFileEnabled( false )
     370             : {
     371           0 :     lcl_createName(*pImp, rLeadingChars, _bStartWithZero, pExtension, pParent, bDirectory);
     372           0 : }
     373             : 
     374           0 : TempFile::~TempFile()
     375             : {
     376           0 :     delete pImp->pStream;
     377           0 :     if ( bKillingFileEnabled )
     378             :     {
     379           0 :         if ( pImp->bIsDirectory )
     380             :         {
     381             :             // at the moment no recursiv algorithm present
     382           0 :             Directory::remove( pImp->aName );
     383             :         }
     384             :         else
     385             :         {
     386           0 :             File::remove( pImp->aName );
     387             :         }
     388             :     }
     389             : 
     390           0 :     delete pImp;
     391           0 : }
     392             : 
     393           0 : bool TempFile::IsValid() const
     394             : {
     395           0 :     return !(pImp->aName.isEmpty());
     396             : }
     397             : 
     398           0 : OUString TempFile::GetFileName() const
     399             : {
     400           0 :     OUString aTmp;
     401           0 :     FileBase::getSystemPathFromFileURL( pImp->aName, aTmp );
     402           0 :     return aTmp;
     403             : }
     404             : 
     405           0 : OUString TempFile::GetURL() const
     406             : {
     407           0 :     if ( pImp->aURL.isEmpty() )
     408             :     {
     409           0 :         OUString aTmp;
     410           0 :         LocalFileHelper::ConvertPhysicalNameToURL( GetFileName(), aTmp );
     411           0 :         pImp->aURL = aTmp;
     412             :     }
     413             : 
     414           0 :     return pImp->aURL;
     415             : }
     416             : 
     417           0 : SvStream* TempFile::GetStream( StreamMode eMode )
     418             : {
     419           0 :     if ( !pImp->pStream )
     420             :     {
     421           0 :         if ( !GetURL().isEmpty() )
     422           0 :             pImp->pStream = UcbStreamHelper::CreateStream( pImp->aURL, eMode, true /* bFileExists */ );
     423             :         else
     424           0 :             pImp->pStream = new SvMemoryStream( eMode );
     425             :     }
     426             : 
     427           0 :     return pImp->pStream;
     428             : }
     429             : 
     430           0 : void TempFile::CloseStream()
     431             : {
     432           0 :     if ( pImp->pStream )
     433             :     {
     434           0 :         delete pImp->pStream;
     435           0 :         pImp->pStream = NULL;
     436             :     }
     437           0 : }
     438             : 
     439           0 : OUString TempFile::SetTempNameBaseDirectory( const OUString &rBaseName )
     440             : {
     441           0 :     if( rBaseName.isEmpty() )
     442           0 :         return OUString();
     443             : 
     444           0 :     OUString aUnqPath( rBaseName );
     445             : 
     446             :     // remove trailing slash
     447           0 :     if ( rBaseName.endsWith("/") )
     448           0 :         aUnqPath = rBaseName.copy( 0, rBaseName.getLength() - 1 );
     449             : 
     450             :     // try to create the directory
     451           0 :     bool bRet = false;
     452           0 :     osl::FileBase::RC err = osl::Directory::create( aUnqPath );
     453           0 :     if ( err != FileBase::E_None && err != FileBase::E_EXIST )
     454             :         // perhaps parent(s) don't exist
     455           0 :         bRet = ensuredir( aUnqPath );
     456             :     else
     457           0 :         bRet = true;
     458             : 
     459             :     // failure to create base directory means returning an empty string
     460           0 :     OUString aTmp;
     461           0 :     if ( bRet )
     462             :     {
     463             :         // append own internal directory
     464           0 :         bRet = true;
     465           0 :         OUString &rTempNameBase_Impl = TempNameBase_Impl::get();
     466           0 :         rTempNameBase_Impl = rBaseName;
     467           0 :         rTempNameBase_Impl += OUString('/');
     468             : 
     469           0 :         TempFile aBase( NULL, true );
     470           0 :         if ( aBase.IsValid() )
     471             :             // use it in case of success
     472           0 :             rTempNameBase_Impl = aBase.pImp->aName;
     473             : 
     474             :         // return system path of used directory
     475           0 :         FileBase::getSystemPathFromFileURL( rTempNameBase_Impl, aTmp );
     476             :     }
     477             : 
     478           0 :     return aTmp;
     479             : }
     480             : }
     481             : 
     482             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10