25#include <com/sun/star/io/BufferSizeExceededException.hpp>
26#include <com/sun/star/io/NotConnectedException.hpp>
27#include <com/sun/star/lang/IllegalArgumentException.hpp>
29#include <rtl/ustring.hxx>
31#include <osl/mutex.hxx>
32#include <osl/detail/file.h>
33#include <osl/file.hxx>
41#elif defined( _WIN32 )
49 OUString gTempNameBase_Impl;
57 size_t lastIndex = aFileName.rfind(
'/' );
60 if (lastIndex != std::u16string_view::npos)
62 aParent = aFileName.substr(0, lastIndex);
64 if (aParent.endsWith(
":") && aParent.getLength() == 6)
67 if (aParent.equalsIgnoreAsciiCase(
"file://"))
77 if ( rUnqPath.isEmpty() )
81 if ( rUnqPath.endsWith(
"/") )
82 aPath = rUnqPath.copy( 0, rUnqPath.getLength() - 1 );
88 osl::Directory aDirectory( aPath );
89 osl::FileBase::RC nError = aDirectory.open();
91 if( nError == osl::File::E_None )
95 nError = osl::Directory::create( aPath );
96 bool bSuccess = ( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST );
101 if ( aParentDir != aPath )
109 nError = osl::Directory::create( aPath );
110 bSuccess =( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST );
125 if ( pParent && !pParent->isEmpty() )
129 if ((osl::FileBase::getSystemPathFromFileURL(*pParent, aRet)
130 == osl::FileBase::E_None)
131 && (osl::FileBase::getFileURLFromSystemPath(aRet, aRet)
132 == osl::FileBase::E_None))
134 ::osl::DirectoryItem aItem;
135 sal_Int32
i = aRet.getLength();
136 if ( aRet[
i-1] ==
'/' )
139 if (
DirectoryItem::get( aRet.copy(0,
i), aItem ) == FileBase::E_None || bCreateParentDirs )
145 (void) bCreateParentDirs;
148 if (
aName.isEmpty() )
150 if (gTempNameBase_Impl.isEmpty())
152 OUString ustrTempDirURL;
153 ::osl::FileBase::RC rc = ::osl::File::getTempDirURL(
155 if (rc == ::osl::FileBase::E_None)
156 gTempNameBase_Impl = ustrTempDirURL;
160 DBG_ASSERT( !gTempNameBase_Impl.isEmpty(),
"No TempDir!" );
161 aName = gTempNameBase_Impl;
165 if( !
aName.isEmpty() && !
aName.endsWith(
"/") )
175 virtual bool next(OUString *) = 0;
181class SequentialTokens:
public Tokens {
183 explicit SequentialTokens(
bool showZero):
m_value(0),
m_show(showZero) {}
185 bool next(OUString * token)
override {
186 assert(token !=
nullptr);
187 if (
m_value == SAL_MAX_UINT32) {
201class UniqueTokens:
public Tokens {
205 bool next(OUString * token)
override {
206 assert(token !=
nullptr);
210 sal_uInt32 radix = 36;
211 sal_uInt32
max = radix * radix * radix * radix * radix * radix;
218 osl::MutexGuard g(osl::Mutex::getGlobalMutex());
225 *token = OUString::number(v, radix);
242 class TempDirCreatedObserver :
public DirectoryCreationObserver
245 virtual void DirectoryCreated(
const OUString& aDirectoryUrl)
override
247 File::setAttributes( aDirectoryUrl, osl_File_Attribute_OwnRead |
248 osl_File_Attribute_OwnWrite | osl_File_Attribute_OwnExe );
254 std::u16string_view rLeadingChars, Tokens & tokens, std::u16string_view pExtension,
255 const OUString* pParent,
bool bDirectory,
bool bKeep,
bool bLock,
256 bool bCreateParentDirs )
259 if ( bCreateParentDirs )
261 size_t nOffset = rLeadingChars.rfind(
u"/");
263 if (std::u16string_view::npos != nOffset)
264 aDirName =
aName + rLeadingChars.substr( 0, nOffset );
267 TempDirCreatedObserver observer;
268 FileBase::RC
err = Directory::createPath( aDirName, &observer );
269 if (
err != FileBase::E_None &&
err != FileBase::E_EXIST )
272 aName += rLeadingChars;
275 while (
tokens.next(&token))
277 OUString aTmp(
aName + token );
278 if ( !pExtension.empty() )
284 FileBase::RC
err = Directory::create(
286 (osl_File_OpenFlag_Read | osl_File_OpenFlag_Write
287 | osl_File_OpenFlag_Private));
288 if (
err == FileBase::E_None )
291 if ( bKeep || Directory::remove( aTmp ) == FileBase::E_None )
296 else if (
err != FileBase::E_EXIST )
302 DBG_ASSERT( bKeep,
"Too expensive, use directory for creating name!" );
304 FileBase::RC
err = aFile.open(
305 osl_File_OpenFlag_Create | osl_File_OpenFlag_Private
306 | (bLock ? 0 : osl_File_OpenFlag_NoLock));
307 if (
err == FileBase::E_None || (bLock &&
err == FileBase::E_NOLCK) )
312 else if (
err != FileBase::E_EXIST )
317 DirectoryItem aTmpItem;
318 FileStatus aTmpStatus( osl_FileStatus_Mask_Type );
320 || aTmpItem.getFileStatus( aTmpStatus ) != FileBase::E_None
321 || aTmpStatus.getFileType() != FileStatus::Directory )
331 OUString aEyeCatcher =
"lu";
334 const char* eye = getenv(
"LO_TESTNAME");
337 aEyeCatcher = OUString(eye, strlen(eye), RTL_TEXTENCODING_ASCII_US);
340 static const pid_t pid = getpid();
341 static const OUString aPidString = OUString::number(pid);
342 aEyeCatcher += aPidString;
345 static const int pid = _getpid();
346 static const OUString aPidString = OUString::number(pid);
347 aEyeCatcher += aPidString;
356 OUString aEyeCatcher =
"lu";
359 const char* eye = getenv(
"LO_TESTNAME");
362 aEyeCatcher = OUString(eye, strlen(eye), RTL_TEXTENCODING_ASCII_US);
365 static const pid_t pid = getpid();
366 static const OUString aPidString = OUString::number(pid);
367 aEyeCatcher += aPidString;
370 static const int pid = _getpid();
371 static const OUString aPidString = OUString::number(pid);
372 aEyeCatcher += aPidString;
388 if ( !
aName.isEmpty() )
389 FileBase::getSystemPathFromFileURL(
aName, aTmp );
398 mxStream(std::move(other.mxStream))
433 if (!
aName.isEmpty() && (osl::FileBase::getFileURLFromSystemPath(
aName,
aName) == osl::FileBase::E_None))
444OUString
CreateTempURL( std::u16string_view rLeadingChars,
bool _bStartWithZero,
445 std::u16string_view pExtension,
const OUString* pParent,
446 bool bCreateParentDirs )
448 SequentialTokens
t(_bStartWithZero);
450 true,
true, bCreateParentDirs );
454 : bIsDirectory( bDirectory )
455 , bKillingFileEnabled( false )
461 std::u16string_view pExtension,
const OUString* pParent,
462 bool bCreateParentDirs )
463 : bIsDirectory( false )
464 , bKillingFileEnabled( false )
466 SequentialTokens
t(_bStartWithZero);
468 true,
true, bCreateParentDirs );
472 aName(std::move(other.aName)), pStream(std::move(other.pStream)), bIsDirectory(other.bIsDirectory),
473 bKillingFileEnabled(other.bKillingFileEnabled)
475 other.bKillingFileEnabled =
false;
490 File::remove(
aName );
496 return !
aName.isEmpty();
502 FileBase::getSystemPathFromFileURL(
aName, aTmp);
522 if (!
aName.isEmpty())
538 if( rBaseName.isEmpty() )
541 OUString aUnqPath( rBaseName );
544 if ( rBaseName.endsWith(
"/") )
545 aUnqPath = rBaseName.copy( 0, rBaseName.getLength() - 1 );
549 osl::FileBase::RC
err = osl::Directory::create( aUnqPath );
550 if (
err != FileBase::E_None &&
err != FileBase::E_EXIST )
561 OUString &rTempNameBase_Impl = gTempNameBase_Impl;
562 rTempNameBase_Impl = rBaseName +
"/";
565 if ( aBase.IsValid() )
567 rTempNameBase_Impl = aBase.
aName;
570 FileBase::getSystemPathFromFileURL( rTempNameBase_Impl, aTmp );
584, mbOutClosed( false )
598 std::unique_lock aGuard(
maMutex );
600 throw css::io::NotConnectedException ( OUString(), getXWeak() );
603 if (nBytesToRead < 0)
604 throw css::io::BufferSizeExceededException( OUString(), getXWeak());
606 if (
aData.getLength() < nBytesToRead)
607 aData.realloc(nBytesToRead);
613 aData.realloc( nRead );
621 std::unique_lock aGuard(
maMutex );
623 throw css::io::NotConnectedException ( OUString(), getXWeak() );
628 if (nMaxBytesToRead < 0)
629 throw css::io::BufferSizeExceededException( OUString(), getXWeak() );
642 std::unique_lock aGuard(
maMutex );
644 throw css::io::NotConnectedException ( OUString(), getXWeak() );
654 std::unique_lock aGuard(
maMutex );
656 throw css::io::NotConnectedException ( OUString(), getXWeak() );
668 std::unique_lock aGuard(
maMutex );
670 throw css::io::NotConnectedException ( OUString(), getXWeak() );
686 std::unique_lock aGuard(
maMutex );
688 throw css::io::NotConnectedException ( OUString(), getXWeak() );
693 if ( nWritten !=
static_cast<sal_uInt32
>(
aData.getLength()))
694 throw css::io::BufferSizeExceededException( OUString(), getXWeak() );
699 std::unique_lock aGuard(
maMutex );
701 throw css::io::NotConnectedException ( OUString(), getXWeak() );
710 std::unique_lock aGuard(
maMutex );
712 throw css::io::NotConnectedException ( OUString(), getXWeak() );
733 throw css::io::NotConnectedException ( OUString(),
const_cast < TempFileFastService *
> (
this)->getXWeak() );
739 throw css::io::NotConnectedException ( OUString(), getXWeak() );
746 std::unique_lock aGuard(
maMutex );
750 throw css::lang::IllegalArgumentException();
752 sal_Int64 nNewLoc =
mpStream->
Seek(
static_cast<sal_uInt32
>(nLocation) );
753 if ( nNewLoc != nLocation )
754 throw css::lang::IllegalArgumentException();
760 std::unique_lock aGuard(
maMutex );
765 return static_cast<sal_Int64
>(
nPos);
770 std::unique_lock aGuard(
maMutex );
796 std::unique_lock aGuard(
maMutex );
virtual sal_uInt64 TellEnd()
std::size_t WriteBytes(const void *pData, std::size_t nSize)
bool SetStreamSize(sal_uInt64 nSize)
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
sal_uInt64 SeekRel(sal_Int64 nPos)
sal_uInt64 remainingSize()
static bool deleteDirRecursively(const OUString &rDirURL)
virtual ::sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< ::sal_Int8 > &aData, ::sal_Int32 nMaxBytesToRead) override
virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL getOutputStream() override
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getInputStream() override
virtual ::sal_Int32 SAL_CALL readBytes(css::uno::Sequence< ::sal_Int8 > &aData, ::sal_Int32 nBytesToRead) override
virtual void SAL_CALL seek(sal_Int64 location) override
virtual sal_Int64 SAL_CALL getLength() override
virtual void SAL_CALL flush() override
std::optional< utl::TempFileFast > mpTempFile
virtual void SAL_CALL skipBytes(::sal_Int32 nBytesToSkip) override
virtual ~TempFileFastService() override
virtual void SAL_CALL closeInput() override
virtual void SAL_CALL writeBytes(const css::uno::Sequence< ::sal_Int8 > &aData) override
virtual sal_Int64 SAL_CALL getPosition() override
virtual ::sal_Int32 SAL_CALL available() override
virtual void SAL_CALL truncate() override
virtual void SAL_CALL closeOutput() override
This is the "fast" temp file.
SvStream * GetStream(StreamMode eMode)
Returns a stream to the tempfiles data; the stream is owned by the tempfile object,...
std::unique_ptr< SvFileStream > mxStream
void CloseStream()
Close and destroy the owned stream object if any.
The class TempFile gives access to temporary files in the local file system.
std::unique_ptr< SvStream > pStream
bool IsValid() const
Returns sal_True if it has a valid file name.
SvStream * GetStream(StreamMode eMode)
Returns a stream to the tempfiles data; the stream is owned by the tempfile object,...
~TempFileNamed()
TempFile will be removed from disk in dtor if EnableKillingFile(true) was called before.
OUString const & GetURL() const
Returns the URL of the tempfile object.
void CloseStream()
Let the TempFile object close and destroy the owned stream object if any.
TempFileNamed(const OUString *pParent=nullptr, bool bDirectory=false)
Create a temporary file or directory, in the default tempfile folder or if possible in a given folder...
OUString GetFileName() const
Returns the system path name of the tempfile in host notation If you want to have the URL,...
#define DBG_ASSERT(sCon, aError)
constexpr OUStringLiteral aData
OUString get(TranslateId sContextAndId, const std::locale &loc)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
OUString CreateTempURL(const OUString *pParent, bool bDirectory)
OUString GetTempNameBaseDirectory()
OUString SetTempNameBaseDirectory(const OUString &rBaseName)
The TempNameBaseDirectory is a subfolder in the folder that is passed as a "physical" file name in th...
static OUString ConstructTempDir_Impl(const OUString *pParent, bool bCreateParentDirs)
static OUString CreateTempName_Impl(const OUString *pParent, bool bKeep, bool bDir=true)
static OUString lcl_createName(std::u16string_view rLeadingChars, Tokens &tokens, std::u16string_view pExtension, const OUString *pParent, bool bDirectory, bool bKeep, bool bLock, bool bCreateParentDirs)
static OUString CreateTempNameFast()
static OUString getParentName(std::u16string_view aFileName)
static bool ensuredir(const OUString &rUnqPath)
OUString CreateTempName()
Only create a "physical" file name for a temporary file that would be valid at that moment.
static sal_uInt32 globalValue