xref: /AOO41X/main/tools/source/fsys/tempfile.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_tools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <tools/tempfile.hxx>
32*cdf0e10cSrcweir #include "comdep.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <rtl/ustring.hxx>
35*cdf0e10cSrcweir #include <osl/file.hxx>
36*cdf0e10cSrcweir #include <rtl/instance.hxx>
37*cdf0e10cSrcweir #include <tools/time.hxx>
38*cdf0e10cSrcweir #include <tools/debug.hxx>
39*cdf0e10cSrcweir #include <stdio.h>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #ifdef UNX
42*cdf0e10cSrcweir #define _MAX_PATH 260
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace osl;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir namespace { struct TempNameBase_Impl : public rtl::Static< ::rtl::OUString, TempNameBase_Impl > {}; }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir struct TempFile_Impl
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     String      aName;
52*cdf0e10cSrcweir     sal_Bool    bIsDirectory;
53*cdf0e10cSrcweir };
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir String GetSystemTempDir_Impl()
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     char sBuf[_MAX_PATH];
58*cdf0e10cSrcweir     const char *pDir = TempDirImpl(sBuf);
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	::rtl::OString aTmpA( pDir );
61*cdf0e10cSrcweir     ::rtl::OUString aTmp = ::rtl::OStringToOUString( aTmpA, osl_getThreadTextEncoding() );
62*cdf0e10cSrcweir     rtl::OUString aRet;
63*cdf0e10cSrcweir     FileBase::getFileURLFromSystemPath( aTmp, aRet );
64*cdf0e10cSrcweir     String aName = aRet;
65*cdf0e10cSrcweir     if( aName.GetChar(aName.Len()-1) != '/' )
66*cdf0e10cSrcweir         aName += '/';
67*cdf0e10cSrcweir     return aName;
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir #define TMPNAME_SIZE  ( 1 + 5 + 5 + 4 + 1 )
71*cdf0e10cSrcweir String ConstructTempDir_Impl( const String* pParent )
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir     String aName;
74*cdf0e10cSrcweir     if ( pParent && pParent->Len() )
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         // if parent given try to use it
77*cdf0e10cSrcweir         rtl::OUString aTmp( *pParent );
78*cdf0e10cSrcweir         rtl::OUString aRet;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         // test for valid filename
81*cdf0e10cSrcweir         {
82*cdf0e10cSrcweir             ::osl::DirectoryItem aItem;
83*cdf0e10cSrcweir             sal_Int32 i = aRet.getLength();
84*cdf0e10cSrcweir             if ( aRet[i-1] == '/' )
85*cdf0e10cSrcweir                 i--;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             if ( DirectoryItem::get( ::rtl::OUString( aRet, i ), aItem ) == FileBase::E_None )
88*cdf0e10cSrcweir                 aName = aRet;
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     if ( !aName.Len() )
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         // if no parent or invalid parent : use system directory
95*cdf0e10cSrcweir 	::rtl::OUString& rTempNameBase_Impl = TempNameBase_Impl::get();
96*cdf0e10cSrcweir         if ( !rTempNameBase_Impl.getLength() )
97*cdf0e10cSrcweir             rTempNameBase_Impl = GetSystemTempDir_Impl();
98*cdf0e10cSrcweir         aName = rTempNameBase_Impl;
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     // Make sure that directory ends with a separator
102*cdf0e10cSrcweir     xub_StrLen i = aName.Len();
103*cdf0e10cSrcweir     if( i>0 && aName.GetChar(i-1) != '/' )
104*cdf0e10cSrcweir         aName += '/';
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     return aName;
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_True )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir     // add a suitable tempname
112*cdf0e10cSrcweir     // Prefix can have 5 chars, leaving 3 for numbers. 26 ** 3 == 17576
113*cdf0e10cSrcweir 	// ER 13.07.00  why not radix 36 [0-9A-Z] ?!?
114*cdf0e10cSrcweir 	const unsigned nRadix = 26;
115*cdf0e10cSrcweir     String aName( rName );
116*cdf0e10cSrcweir     aName += String::CreateFromAscii( "sv" );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     rName.Erase();
119*cdf0e10cSrcweir     static unsigned long u = Time::GetSystemTicks();
120*cdf0e10cSrcweir     for ( unsigned long nOld = u; ++u != nOld; )
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         u %= (nRadix*nRadix*nRadix);
123*cdf0e10cSrcweir         String aTmp( aName );
124*cdf0e10cSrcweir         aTmp += String::CreateFromInt32( (sal_Int32) (unsigned) u, nRadix );
125*cdf0e10cSrcweir         aTmp += String::CreateFromAscii( ".tmp" );
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         if ( bDir )
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir             FileBase::RC err = Directory::create( aTmp );
130*cdf0e10cSrcweir             if (  err == FileBase::E_None )
131*cdf0e10cSrcweir             {
132*cdf0e10cSrcweir                 // !bKeep: only for creating a name, not a file or directory
133*cdf0e10cSrcweir                 if ( bKeep || Directory::remove( aTmp ) == FileBase::E_None )
134*cdf0e10cSrcweir                     rName = aTmp;
135*cdf0e10cSrcweir                 break;
136*cdf0e10cSrcweir             }
137*cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
138*cdf0e10cSrcweir             {
139*cdf0e10cSrcweir                 // if f.e. name contains invalid chars stop trying to create dirs
140*cdf0e10cSrcweir                 break;
141*cdf0e10cSrcweir             }
142*cdf0e10cSrcweir         }
143*cdf0e10cSrcweir         else
144*cdf0e10cSrcweir         {
145*cdf0e10cSrcweir             DBG_ASSERT( bKeep, "Too expensive, use directory for creating name!" );
146*cdf0e10cSrcweir             File aFile( aTmp );
147*cdf0e10cSrcweir             FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
148*cdf0e10cSrcweir             if (  err == FileBase::E_None )
149*cdf0e10cSrcweir             {
150*cdf0e10cSrcweir                 rName = aTmp;
151*cdf0e10cSrcweir                 aFile.close();
152*cdf0e10cSrcweir                 break;
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
155*cdf0e10cSrcweir             {
156*cdf0e10cSrcweir                  // if f.e. name contains invalid chars stop trying to create files
157*cdf0e10cSrcweir                  break;
158*cdf0e10cSrcweir             }
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir String TempFile::CreateTempName( const String* pParent )
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir     // get correct directory
166*cdf0e10cSrcweir     String aName = ConstructTempDir_Impl( pParent );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     // get TempFile name with default naming scheme
169*cdf0e10cSrcweir     CreateTempName_Impl( aName, sal_False );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     // convert to file URL
172*cdf0e10cSrcweir     rtl::OUString aTmp;
173*cdf0e10cSrcweir     if ( aName.Len() )
174*cdf0e10cSrcweir 		aTmp = aName;
175*cdf0e10cSrcweir     return aTmp;
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir TempFile::TempFile( const String* pParent, sal_Bool bDirectory )
179*cdf0e10cSrcweir     : pImp( new TempFile_Impl )
180*cdf0e10cSrcweir     , bKillingFileEnabled( sal_False )
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir     pImp->bIsDirectory = bDirectory;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     // get correct directory
185*cdf0e10cSrcweir     pImp->aName = ConstructTempDir_Impl( pParent );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     // get TempFile with default naming scheme
188*cdf0e10cSrcweir     CreateTempName_Impl( pImp->aName, sal_True, bDirectory );
189*cdf0e10cSrcweir }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir TempFile::TempFile( const String& rLeadingChars, const String* pExtension, const String* pParent, sal_Bool bDirectory )
192*cdf0e10cSrcweir     : pImp( new TempFile_Impl )
193*cdf0e10cSrcweir     , bKillingFileEnabled( sal_False )
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     pImp->bIsDirectory = bDirectory;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     // get correct directory
198*cdf0e10cSrcweir     String aName = ConstructTempDir_Impl( pParent );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     // now use special naming scheme ( name takes leading chars and an index counting up from zero
201*cdf0e10cSrcweir     aName += rLeadingChars;
202*cdf0e10cSrcweir     for ( sal_Int32 i=0;; i++ )
203*cdf0e10cSrcweir     {
204*cdf0e10cSrcweir         String aTmp( aName );
205*cdf0e10cSrcweir         aTmp += String::CreateFromInt32( i );
206*cdf0e10cSrcweir         if ( pExtension )
207*cdf0e10cSrcweir             aTmp += *pExtension;
208*cdf0e10cSrcweir         else
209*cdf0e10cSrcweir             aTmp += String::CreateFromAscii( ".tmp" );
210*cdf0e10cSrcweir         if ( bDirectory )
211*cdf0e10cSrcweir         {
212*cdf0e10cSrcweir             FileBase::RC err = Directory::create( aTmp );
213*cdf0e10cSrcweir             if ( err == FileBase::E_None )
214*cdf0e10cSrcweir             {
215*cdf0e10cSrcweir                 pImp->aName = aTmp;
216*cdf0e10cSrcweir                 break;
217*cdf0e10cSrcweir             }
218*cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
219*cdf0e10cSrcweir                 // if f.e. name contains invalid chars stop trying to create dirs
220*cdf0e10cSrcweir                 break;
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir         else
223*cdf0e10cSrcweir         {
224*cdf0e10cSrcweir             File aFile( aTmp );
225*cdf0e10cSrcweir             FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
226*cdf0e10cSrcweir             if ( err == FileBase::E_None )
227*cdf0e10cSrcweir             {
228*cdf0e10cSrcweir                 pImp->aName = aTmp;
229*cdf0e10cSrcweir                 aFile.close();
230*cdf0e10cSrcweir                 break;
231*cdf0e10cSrcweir             }
232*cdf0e10cSrcweir             else if ( err != FileBase::E_EXIST )
233*cdf0e10cSrcweir                 // if f.e. name contains invalid chars stop trying to create dirs
234*cdf0e10cSrcweir                 break;
235*cdf0e10cSrcweir         }
236*cdf0e10cSrcweir     }
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir TempFile::~TempFile()
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir     if ( bKillingFileEnabled )
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         if ( pImp->bIsDirectory )
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             // at the moment no recursiv algorithm present
246*cdf0e10cSrcweir             Directory::remove( pImp->aName );
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir         else
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             File::remove( pImp->aName );
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir     }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     delete pImp;
255*cdf0e10cSrcweir }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir sal_Bool TempFile::IsValid() const
258*cdf0e10cSrcweir {
259*cdf0e10cSrcweir     return pImp->aName.Len() != 0;
260*cdf0e10cSrcweir }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir String TempFile::GetName() const
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir     rtl::OUString aTmp;
265*cdf0e10cSrcweir 	aTmp = pImp->aName;
266*cdf0e10cSrcweir     return aTmp;
267*cdf0e10cSrcweir }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir String TempFile::SetTempNameBaseDirectory( const String &rBaseName )
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir     String aName( rBaseName );
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir     ::rtl::OUString& rTempNameBase_Impl = TempNameBase_Impl::get();
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     FileBase::RC err= Directory::create( aName );
276*cdf0e10cSrcweir     if ( err == FileBase::E_None || err == FileBase::E_EXIST )
277*cdf0e10cSrcweir     {
278*cdf0e10cSrcweir         rTempNameBase_Impl  = aName;
279*cdf0e10cSrcweir         rTempNameBase_Impl += String( '/' );
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         TempFile aBase( NULL, sal_True );
282*cdf0e10cSrcweir         if ( aBase.IsValid() )
283*cdf0e10cSrcweir             rTempNameBase_Impl = aBase.pImp->aName;
284*cdf0e10cSrcweir     }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     rtl::OUString aTmp;
287*cdf0e10cSrcweir     aTmp = rTempNameBase_Impl;
288*cdf0e10cSrcweir     return aTmp;
289*cdf0e10cSrcweir }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir String TempFile::GetTempNameBaseDirectory()
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir     ::rtl::OUString& rTempNameBase_Impl = TempNameBase_Impl::get();
294*cdf0e10cSrcweir     if ( !rTempNameBase_Impl.getLength() )
295*cdf0e10cSrcweir         rTempNameBase_Impl = GetSystemTempDir_Impl();
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir     rtl::OUString aTmp;
298*cdf0e10cSrcweir     aTmp = rTempNameBase_Impl;
299*cdf0e10cSrcweir     return aTmp;
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302