xref: /trunk/main/unotools/source/ucbhelper/tempfile.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b5088357SAndrew Rist  * distributed with this work for additional information
6b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b5088357SAndrew Rist  * software distributed under the License is distributed on an
15b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17b5088357SAndrew Rist  * specific language governing permissions and limitations
18b5088357SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20b5088357SAndrew Rist  *************************************************************/
21b5088357SAndrew Rist 
22b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <unotools/tempfile.hxx>
28cdf0e10cSrcweir #include <tools/tempfile.hxx>
29cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
30cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
31cdf0e10cSrcweir #include <ucbhelper/fileidentifierconverter.hxx>
32cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir #include <rtl/instance.hxx>
35cdf0e10cSrcweir #include <osl/file.hxx>
36cdf0e10cSrcweir #include <tools/time.hxx>
37cdf0e10cSrcweir #include <tools/debug.hxx>
38cdf0e10cSrcweir #include <stdio.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #ifdef UNX
41cdf0e10cSrcweir #include <sys/stat.h>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace osl;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir namespace
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     struct TempNameBase_Impl
49cdf0e10cSrcweir         : public rtl::Static< ::rtl::OUString, TempNameBase_Impl > {};
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace utl
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 
55cdf0e10cSrcweir struct TempFile_Impl
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     String      aName;
58cdf0e10cSrcweir     String      aURL;
59cdf0e10cSrcweir     SvStream*   pStream;
60cdf0e10cSrcweir     sal_Bool    bIsDirectory;
61cdf0e10cSrcweir 
TempFile_Implutl::TempFile_Impl62cdf0e10cSrcweir                 TempFile_Impl()
63cdf0e10cSrcweir                     : pStream(0)
64cdf0e10cSrcweir                     {}
65cdf0e10cSrcweir };
66cdf0e10cSrcweir 
getParentName(const rtl::OUString & aFileName)67cdf0e10cSrcweir rtl::OUString getParentName( const rtl::OUString& aFileName )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     sal_Int32 lastIndex = aFileName.lastIndexOf( sal_Unicode('/') );
70cdf0e10cSrcweir     rtl::OUString aParent = aFileName.copy( 0,lastIndex );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     if( aParent[ aParent.getLength()-1] == sal_Unicode(':') && aParent.getLength() == 6 )
73cdf0e10cSrcweir         aParent += rtl::OUString::createFromAscii( "/" );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     if( 0 == aParent.compareToAscii( "file://" ) )
76cdf0e10cSrcweir         aParent = rtl::OUString::createFromAscii( "file:///" );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     return aParent;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
ensuredir(const rtl::OUString & rUnqPath)81cdf0e10cSrcweir sal_Bool ensuredir( const rtl::OUString& rUnqPath )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     rtl::OUString aPath;
84cdf0e10cSrcweir     if ( rUnqPath.getLength() < 1 )
85cdf0e10cSrcweir         return sal_False;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     // remove trailing slash
88cdf0e10cSrcweir     if ( rUnqPath[ rUnqPath.getLength() - 1 ] == sal_Unicode( '/' ) )
89cdf0e10cSrcweir         aPath = rUnqPath.copy( 0, rUnqPath.getLength() - 1 );
90cdf0e10cSrcweir     else
91cdf0e10cSrcweir         aPath = rUnqPath;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     // HACK: create directory on a mount point with nobrowse option
94cdf0e10cSrcweir     // returns ENOSYS in any case !!
95cdf0e10cSrcweir     osl::Directory aDirectory( aPath );
96cdf0e10cSrcweir #ifdef UNX
97cdf0e10cSrcweir /* RW permission for the user only! */
98cdf0e10cSrcweir  mode_t old_mode = umask(077);
99cdf0e10cSrcweir #endif
100cdf0e10cSrcweir     osl::FileBase::RC nError = aDirectory.open();
101cdf0e10cSrcweir #ifdef UNX
102cdf0e10cSrcweir umask(old_mode);
103cdf0e10cSrcweir #endif
104cdf0e10cSrcweir     aDirectory.close();
105cdf0e10cSrcweir     if( nError == osl::File::E_None )
106cdf0e10cSrcweir         return sal_True;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     // try to create the directory
109cdf0e10cSrcweir     nError = osl::Directory::create( aPath );
110cdf0e10cSrcweir     sal_Bool  bSuccess = ( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST );
111cdf0e10cSrcweir     if( !bSuccess )
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         // perhaps parent(s) don't exist
114cdf0e10cSrcweir         rtl::OUString aParentDir = getParentName( aPath );
115cdf0e10cSrcweir         if ( aParentDir != aPath )
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             bSuccess = ensuredir( getParentName( aPath ) );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             // After parent directory structure exists try it one's more
120cdf0e10cSrcweir             if ( bSuccess )
121cdf0e10cSrcweir             {
122cdf0e10cSrcweir                 // Parent directory exists, retry creation of directory
123cdf0e10cSrcweir                 nError = osl::Directory::create( aPath );
124cdf0e10cSrcweir                 bSuccess =( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST );
125cdf0e10cSrcweir             }
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     return bSuccess;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir #define TMPNAME_SIZE  ( 1 + 5 + 5 + 4 + 1 )
ConstructTempDir_Impl(const String * pParent)133cdf0e10cSrcweir String ConstructTempDir_Impl( const String* pParent )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     String aName;
136cdf0e10cSrcweir     if ( pParent && pParent->Len() )
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
139cdf0e10cSrcweir         if ( pBroker )
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
142cdf0e10cSrcweir                     pBroker->getContentProviderManagerInterface();
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             // if parent given try to use it
145cdf0e10cSrcweir             rtl::OUString aTmp( *pParent );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir             // test for valid filename
148cdf0e10cSrcweir             rtl::OUString aRet;
149cdf0e10cSrcweir             ::osl::FileBase::getFileURLFromSystemPath(
150cdf0e10cSrcweir                 ::ucbhelper::getSystemPathFromFileURL( xManager, aTmp ),
151cdf0e10cSrcweir                 aRet );
152cdf0e10cSrcweir             if ( aRet.getLength() )
153cdf0e10cSrcweir             {
154cdf0e10cSrcweir                 ::osl::DirectoryItem aItem;
155cdf0e10cSrcweir                 sal_Int32 i = aRet.getLength();
156cdf0e10cSrcweir                 if ( aRet[i-1] == '/' )
157cdf0e10cSrcweir                     i--;
158cdf0e10cSrcweir 
159*24c56ab9SHerbert Dürr                 if ( DirectoryItem::get( ::rtl::OUString( aRet.getStr(), i ), aItem ) == FileBase::E_None )
160cdf0e10cSrcweir                     aName = aRet;
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         else
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             DBG_WARNING( "::unotools::TempFile : UCB not present or not initialized!" );
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     if ( !aName.Len() )
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         ::rtl::OUString &rTempNameBase_Impl = TempNameBase_Impl::get();
172cdf0e10cSrcweir         if (rTempNameBase_Impl.getLength() == 0)
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             ::rtl::OUString ustrTempDirURL;
175cdf0e10cSrcweir             ::osl::FileBase::RC rc = ::osl::File::getTempDirURL(
176cdf0e10cSrcweir                 ustrTempDirURL );
177cdf0e10cSrcweir             if (rc == ::osl::FileBase::E_None)
178cdf0e10cSrcweir                 rTempNameBase_Impl = ustrTempDirURL;
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir         // if no parent or invalid parent : use default directory
181cdf0e10cSrcweir         DBG_ASSERT( rTempNameBase_Impl.getLength(), "No TempDir!" );
182cdf0e10cSrcweir         aName = rTempNameBase_Impl;
183cdf0e10cSrcweir         ensuredir( aName );
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     // Make sure that directory ends with a separator
187cdf0e10cSrcweir     xub_StrLen i = aName.Len();
188cdf0e10cSrcweir     if( i>0 && aName.GetChar(i-1) != '/' )
189cdf0e10cSrcweir         aName += '/';
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     return aName;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
CreateTempName_Impl(String & rName,sal_Bool bKeep,sal_Bool bDir=sal_True)194cdf0e10cSrcweir void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_True )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     // add a suitable tempname
197cdf0e10cSrcweir     // 36 ** 6 == 2176782336
198cdf0e10cSrcweir     unsigned const nRadix = 36;
199cdf0e10cSrcweir     unsigned long const nMax = (nRadix*nRadix*nRadix*nRadix*nRadix*nRadix);
200cdf0e10cSrcweir     String aName( rName );
201cdf0e10cSrcweir     aName += String::CreateFromAscii( "sv" );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     rName.Erase();
204cdf0e10cSrcweir     unsigned long nSeed = Time::GetSystemTicks() % nMax;
205cdf0e10cSrcweir     for ( unsigned long u = nSeed; ++u != nSeed; )
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         u %= nMax;
208cdf0e10cSrcweir         String aTmp( aName );
209cdf0e10cSrcweir         aTmp += String::CreateFromInt64( static_cast<sal_Int64>(u), nRadix );
210cdf0e10cSrcweir         aTmp += String::CreateFromAscii( ".tmp" );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         if ( bDir )
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir             FileBase::RC err = Directory::create( aTmp );
215cdf0e10cSrcweir             if (  err == FileBase::E_None )
216cdf0e10cSrcweir             {
217cdf0e10cSrcweir                 // !bKeep: only for creating a name, not a file or directory
218cdf0e10cSrcweir                 if ( bKeep || Directory::remove( aTmp ) == FileBase::E_None )
219cdf0e10cSrcweir                     rName = aTmp;
220cdf0e10cSrcweir                 break;
221cdf0e10cSrcweir             }
222cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
223cdf0e10cSrcweir             {
224cdf0e10cSrcweir                 // if f.e. name contains invalid chars stop trying to create dirs
225cdf0e10cSrcweir                 break;
226cdf0e10cSrcweir             }
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir         else
229cdf0e10cSrcweir         {
230cdf0e10cSrcweir             DBG_ASSERT( bKeep, "Too expensive, use directory for creating name!" );
231cdf0e10cSrcweir             File aFile( aTmp );
232cdf0e10cSrcweir #ifdef UNX
233cdf0e10cSrcweir /* RW permission for the user only! */
234cdf0e10cSrcweir  mode_t old_mode = umask(077);
235cdf0e10cSrcweir #endif
236cdf0e10cSrcweir             FileBase::RC err = aFile.open( osl_File_OpenFlag_Create | osl_File_OpenFlag_NoLock );
237cdf0e10cSrcweir #ifdef UNX
238cdf0e10cSrcweir umask(old_mode);
239cdf0e10cSrcweir #endif
240cdf0e10cSrcweir             if (  err == FileBase::E_None )
241cdf0e10cSrcweir             {
242cdf0e10cSrcweir                 rName = aTmp;
243cdf0e10cSrcweir                 aFile.close();
244cdf0e10cSrcweir                 break;
245cdf0e10cSrcweir             }
246cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
247cdf0e10cSrcweir             {
248cdf0e10cSrcweir                  // if f.e. name contains invalid chars stop trying to create files
249cdf0e10cSrcweir                  // but if there is a folder with such name proceed further
250cdf0e10cSrcweir 
251cdf0e10cSrcweir                  DirectoryItem aTmpItem;
252cdf0e10cSrcweir                  FileStatus aTmpStatus( FileStatusMask_Type );
253cdf0e10cSrcweir                  if ( DirectoryItem::get( aTmp, aTmpItem ) != FileBase::E_None
254cdf0e10cSrcweir                    || aTmpItem.getFileStatus( aTmpStatus ) != FileBase::E_None
255cdf0e10cSrcweir                    || aTmpStatus.getFileType() != FileStatus::Directory )
256cdf0e10cSrcweir                      break;
257cdf0e10cSrcweir             }
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
lcl_createName(TempFile_Impl & _rImpl,const String & rLeadingChars,sal_Bool _bStartWithZero,const String * pExtension,const String * pParent,sal_Bool bDirectory)262cdf0e10cSrcweir void lcl_createName(TempFile_Impl& _rImpl,const String& rLeadingChars,sal_Bool _bStartWithZero, const String* pExtension, const String* pParent, sal_Bool bDirectory)
263cdf0e10cSrcweir {
264cdf0e10cSrcweir     _rImpl.bIsDirectory = bDirectory;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     // get correct directory
267cdf0e10cSrcweir     String aName = ConstructTempDir_Impl( pParent );
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     sal_Bool bUseNumber = _bStartWithZero;
270cdf0e10cSrcweir     // now use special naming scheme ( name takes leading chars and an index counting up from zero
271cdf0e10cSrcweir     aName += rLeadingChars;
272cdf0e10cSrcweir     for ( sal_Int32 i=0;; i++ )
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         String aTmp( aName );
275cdf0e10cSrcweir         if ( bUseNumber )
276cdf0e10cSrcweir             aTmp += String::CreateFromInt32( i );
277cdf0e10cSrcweir         bUseNumber = sal_True;
278cdf0e10cSrcweir         if ( pExtension )
279cdf0e10cSrcweir             aTmp += *pExtension;
280cdf0e10cSrcweir         else
281cdf0e10cSrcweir             aTmp += String::CreateFromAscii( ".tmp" );
282cdf0e10cSrcweir         if ( bDirectory )
283cdf0e10cSrcweir         {
284cdf0e10cSrcweir             FileBase::RC err = Directory::create( aTmp );
285cdf0e10cSrcweir             if ( err == FileBase::E_None )
286cdf0e10cSrcweir             {
287cdf0e10cSrcweir                 _rImpl.aName = aTmp;
288cdf0e10cSrcweir                 break;
289cdf0e10cSrcweir             }
290cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
291cdf0e10cSrcweir                 // if f.e. name contains invalid chars stop trying to create dirs
292cdf0e10cSrcweir                 break;
293cdf0e10cSrcweir         }
294cdf0e10cSrcweir         else
295cdf0e10cSrcweir         {
296cdf0e10cSrcweir             File aFile( aTmp );
297cdf0e10cSrcweir #ifdef UNX
298cdf0e10cSrcweir /* RW permission for the user only! */
299cdf0e10cSrcweir  mode_t old_mode = umask(077);
300cdf0e10cSrcweir #endif
301cdf0e10cSrcweir             FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
302cdf0e10cSrcweir #ifdef UNX
303cdf0e10cSrcweir umask(old_mode);
304cdf0e10cSrcweir #endif
305cdf0e10cSrcweir             if ( err == FileBase::E_None )
306cdf0e10cSrcweir             {
307cdf0e10cSrcweir                 _rImpl.aName = aTmp;
308cdf0e10cSrcweir                 aFile.close();
309cdf0e10cSrcweir                 break;
310cdf0e10cSrcweir             }
311cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
312cdf0e10cSrcweir             {
313cdf0e10cSrcweir                 // if f.e. name contains invalid chars stop trying to create dirs
314cdf0e10cSrcweir                 // but if there is a folder with such name proceed further
315cdf0e10cSrcweir 
316cdf0e10cSrcweir                 DirectoryItem aTmpItem;
317cdf0e10cSrcweir                 FileStatus aTmpStatus( FileStatusMask_Type );
318cdf0e10cSrcweir                 if ( DirectoryItem::get( aTmp, aTmpItem ) != FileBase::E_None
319cdf0e10cSrcweir                   || aTmpItem.getFileStatus( aTmpStatus ) != FileBase::E_None
320cdf0e10cSrcweir                   || aTmpStatus.getFileType() != FileStatus::Directory )
321cdf0e10cSrcweir                     break;
322cdf0e10cSrcweir             }
323cdf0e10cSrcweir         }
324cdf0e10cSrcweir         if ( !_bStartWithZero )
325cdf0e10cSrcweir             aTmp += String::CreateFromInt32( i );
326cdf0e10cSrcweir     }
327cdf0e10cSrcweir }
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 
CreateTempName(const String * pParent)330cdf0e10cSrcweir String TempFile::CreateTempName( const String* pParent )
331cdf0e10cSrcweir {
332cdf0e10cSrcweir     // get correct directory
333cdf0e10cSrcweir     String aName = ConstructTempDir_Impl( pParent );
334cdf0e10cSrcweir 
335cdf0e10cSrcweir     // get TempFile name with default naming scheme
336cdf0e10cSrcweir     CreateTempName_Impl( aName, sal_False );
337cdf0e10cSrcweir 
338cdf0e10cSrcweir     // convert to file URL
339cdf0e10cSrcweir     rtl::OUString aTmp;
340cdf0e10cSrcweir     if ( aName.Len() )
341cdf0e10cSrcweir         FileBase::getSystemPathFromFileURL( aName, aTmp );
342cdf0e10cSrcweir     return aTmp;
343cdf0e10cSrcweir }
344cdf0e10cSrcweir 
TempFile(const String * pParent,sal_Bool bDirectory)345cdf0e10cSrcweir TempFile::TempFile( const String* pParent, sal_Bool bDirectory )
346cdf0e10cSrcweir     : pImp( new TempFile_Impl )
347cdf0e10cSrcweir     , bKillingFileEnabled( sal_False )
348cdf0e10cSrcweir {
349cdf0e10cSrcweir     pImp->bIsDirectory = bDirectory;
350cdf0e10cSrcweir 
351cdf0e10cSrcweir     // get correct directory
352cdf0e10cSrcweir     pImp->aName = ConstructTempDir_Impl( pParent );
353cdf0e10cSrcweir 
354cdf0e10cSrcweir     // get TempFile with default naming scheme
355cdf0e10cSrcweir     CreateTempName_Impl( pImp->aName, sal_True, bDirectory );
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
TempFile(const String & rLeadingChars,const String * pExtension,const String * pParent,sal_Bool bDirectory)358cdf0e10cSrcweir TempFile::TempFile( const String& rLeadingChars, const String* pExtension, const String* pParent, sal_Bool bDirectory)
359cdf0e10cSrcweir     : pImp( new TempFile_Impl )
360cdf0e10cSrcweir     , bKillingFileEnabled( sal_False )
361cdf0e10cSrcweir {
362cdf0e10cSrcweir     lcl_createName(*pImp,rLeadingChars,sal_True, pExtension, pParent, bDirectory);
363cdf0e10cSrcweir }
TempFile(const String & rLeadingChars,sal_Bool _bStartWithZero,const String * pExtension,const String * pParent,sal_Bool bDirectory)364cdf0e10cSrcweir TempFile::TempFile( const String& rLeadingChars,sal_Bool _bStartWithZero, const String* pExtension, const String* pParent, sal_Bool bDirectory)
365cdf0e10cSrcweir     : pImp( new TempFile_Impl )
366cdf0e10cSrcweir     , bKillingFileEnabled( sal_False )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir     lcl_createName(*pImp,rLeadingChars,_bStartWithZero, pExtension, pParent, bDirectory);
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
~TempFile()371cdf0e10cSrcweir TempFile::~TempFile()
372cdf0e10cSrcweir {
373cdf0e10cSrcweir     delete pImp->pStream;
374cdf0e10cSrcweir     if ( bKillingFileEnabled )
375cdf0e10cSrcweir     {
376cdf0e10cSrcweir         if ( pImp->bIsDirectory )
377cdf0e10cSrcweir         {
378cdf0e10cSrcweir             // at the moment no recursiv algorithm present
379cdf0e10cSrcweir             Directory::remove( pImp->aName );
380cdf0e10cSrcweir         }
381cdf0e10cSrcweir         else
382cdf0e10cSrcweir         {
383cdf0e10cSrcweir             File::remove( pImp->aName );
384cdf0e10cSrcweir         }
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir 
387cdf0e10cSrcweir     delete pImp;
388cdf0e10cSrcweir }
389cdf0e10cSrcweir 
IsValid() const390cdf0e10cSrcweir sal_Bool TempFile::IsValid() const
391cdf0e10cSrcweir {
392cdf0e10cSrcweir     return pImp->aName.Len() != 0;
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
GetFileName() const395cdf0e10cSrcweir String TempFile::GetFileName() const
396cdf0e10cSrcweir {
397cdf0e10cSrcweir     rtl::OUString aTmp;
398cdf0e10cSrcweir     FileBase::getSystemPathFromFileURL( pImp->aName, aTmp );
399cdf0e10cSrcweir     return aTmp;
400cdf0e10cSrcweir }
401cdf0e10cSrcweir 
GetURL() const402cdf0e10cSrcweir String TempFile::GetURL() const
403cdf0e10cSrcweir {
404cdf0e10cSrcweir     if ( !pImp->aURL.Len() )
405cdf0e10cSrcweir     {
406cdf0e10cSrcweir         String aTmp;
407cdf0e10cSrcweir         LocalFileHelper::ConvertPhysicalNameToURL( GetFileName(), aTmp );
408cdf0e10cSrcweir         pImp->aURL = aTmp;
409cdf0e10cSrcweir     }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir     return pImp->aURL;
412cdf0e10cSrcweir }
413cdf0e10cSrcweir 
GetStream(StreamMode eMode)414cdf0e10cSrcweir SvStream* TempFile::GetStream( StreamMode eMode )
415cdf0e10cSrcweir {
416cdf0e10cSrcweir     if ( !pImp->pStream )
417cdf0e10cSrcweir     {
418cdf0e10cSrcweir         if ( GetURL().Len() )
419cdf0e10cSrcweir             pImp->pStream = UcbStreamHelper::CreateStream( pImp->aURL, eMode, sal_True /* bFileExists */ );
420cdf0e10cSrcweir         else
421cdf0e10cSrcweir             pImp->pStream = new SvMemoryStream( eMode );
422cdf0e10cSrcweir     }
423cdf0e10cSrcweir 
424cdf0e10cSrcweir     return pImp->pStream;
425cdf0e10cSrcweir }
426cdf0e10cSrcweir 
CloseStream()427cdf0e10cSrcweir void TempFile::CloseStream()
428cdf0e10cSrcweir {
429cdf0e10cSrcweir     if ( pImp->pStream )
430cdf0e10cSrcweir     {
431cdf0e10cSrcweir         delete pImp->pStream;
432cdf0e10cSrcweir         pImp->pStream = NULL;
433cdf0e10cSrcweir     }
434cdf0e10cSrcweir }
435cdf0e10cSrcweir 
SetTempNameBaseDirectory(const String & rBaseName)436cdf0e10cSrcweir String TempFile::SetTempNameBaseDirectory( const String &rBaseName )
437cdf0e10cSrcweir {
438cdf0e10cSrcweir     if( !rBaseName.Len() )
439cdf0e10cSrcweir         return String();
440cdf0e10cSrcweir 
441cdf0e10cSrcweir     rtl::OUString aUnqPath( rBaseName );
442cdf0e10cSrcweir 
443cdf0e10cSrcweir     // remove trailing slash
444cdf0e10cSrcweir     if ( rBaseName.GetChar( rBaseName.Len() - 1 ) == sal_Unicode( '/' ) )
445cdf0e10cSrcweir         aUnqPath = rBaseName.Copy( 0, rBaseName.Len() - 1 );
446cdf0e10cSrcweir 
447cdf0e10cSrcweir     // try to create the directory
448cdf0e10cSrcweir     sal_Bool bRet = sal_False;
449cdf0e10cSrcweir     osl::FileBase::RC err = osl::Directory::create( aUnqPath );
450cdf0e10cSrcweir     if ( err != FileBase::E_None && err != FileBase::E_EXIST )
451cdf0e10cSrcweir         // perhaps parent(s) don't exist
452cdf0e10cSrcweir         bRet = ensuredir( aUnqPath );
453cdf0e10cSrcweir     else
454cdf0e10cSrcweir         bRet = sal_True;
455cdf0e10cSrcweir 
456cdf0e10cSrcweir     // failure to create base directory means returning an empty string
457cdf0e10cSrcweir     rtl::OUString aTmp;
458cdf0e10cSrcweir     if ( bRet )
459cdf0e10cSrcweir     {
460cdf0e10cSrcweir         // append own internal directory
461cdf0e10cSrcweir         bRet = sal_True;
462cdf0e10cSrcweir         ::rtl::OUString &rTempNameBase_Impl = TempNameBase_Impl::get();
463cdf0e10cSrcweir         rTempNameBase_Impl = rBaseName;
464cdf0e10cSrcweir         rTempNameBase_Impl += String( '/' );
465cdf0e10cSrcweir 
466cdf0e10cSrcweir         TempFile aBase( NULL, sal_True );
467cdf0e10cSrcweir         if ( aBase.IsValid() )
468cdf0e10cSrcweir             // use it in case of success
469cdf0e10cSrcweir             rTempNameBase_Impl = aBase.pImp->aName;
470cdf0e10cSrcweir 
471cdf0e10cSrcweir         // return system path of used directory
472cdf0e10cSrcweir         FileBase::getSystemPathFromFileURL( rTempNameBase_Impl, aTmp );
473cdf0e10cSrcweir     }
474cdf0e10cSrcweir 
475cdf0e10cSrcweir     return aTmp;
476cdf0e10cSrcweir }
477cdf0e10cSrcweir 
GetTempNameBaseDirectory()478cdf0e10cSrcweir String TempFile::GetTempNameBaseDirectory()
479cdf0e10cSrcweir {
480cdf0e10cSrcweir     const ::rtl::OUString &rTempNameBase_Impl = TempNameBase_Impl::get();
481cdf0e10cSrcweir     if ( !rTempNameBase_Impl.getLength() )
482cdf0e10cSrcweir         return String();
483cdf0e10cSrcweir 
484cdf0e10cSrcweir     rtl::OUString aTmp;
485cdf0e10cSrcweir     FileBase::getSystemPathFromFileURL( rTempNameBase_Impl, aTmp );
486cdf0e10cSrcweir     return aTmp;
487cdf0e10cSrcweir }
488cdf0e10cSrcweir 
489cdf0e10cSrcweir }
490