28#include <osl/thread.h>
31#include <osl/file.hxx>
32#include <osl/detail/file.h>
40std::mutex& LockMutex()
42 static std::mutex SINGLETON;
46std::map<SvFileStream const *, osl::DirectoryItem> gLocks;
50 osl::DirectoryItem aItem;
51 if (osl::DirectoryItem::get( pStream->
GetFileName(), aItem) != osl::FileBase::E_None )
53 SAL_INFO(
"tools.stream",
"Failed to lookup stream for locking");
57 osl::FileStatus aStatus( osl_FileStatus_Mask_Type );
58 if ( aItem.getFileStatus( aStatus ) != osl::FileBase::E_None )
60 SAL_INFO(
"tools.stream",
"Failed to stat stream for locking");
63 if( aStatus.getFileType() == osl::FileStatus::Directory )
66 std::unique_lock aGuard( LockMutex() );
67 for(
const auto& [rLockStream, rLockItem] : gLocks )
69 if( aItem.isIdenticalTo( rLockItem ) )
71 StreamMode nLockMode = rLockStream->GetStreamMode();
83 gLocks[pStream] = aItem;
89 std::unique_lock aGuard( LockMutex() );
90 gLocks.erase(pStream);
97 static struct {
int nErr;
ErrCode sv; }
const errArr[] =
102#if defined(NETBSD) || \
103 defined(FREEBSD) || defined(MACOSX) || defined(OPENBSD) || \
104 defined(__FreeBSD_kernel__) || defined(DRAGONFLY) || \
105 defined(IOS) || defined(HAIKU)
119#if !defined(NETBSD) && !defined (FREEBSD) && \
120 !defined(MACOSX) && !defined(OPENBSD) && !defined(__FreeBSD_kernel__) && \
136 if ( errArr[
i].nErr == nErrno )
138 nRetVal = errArr[
i].sv;
143 while( errArr[
i].nErr != 0xFFFF );
149 static struct { oslFileError nErr;
ErrCode sv; }
const errArr[] =
176 if ( errArr[
i].nErr == nErrno )
178 nRetVal = errArr[
i].sv;
183 while( errArr[
i].nErr != oslFileError(0xFFFF) );
194 OUString aSystemFileName;
195 if( FileBase::getSystemPathFromFileURL( rFileName , aSystemFileName )
196 != FileBase::E_None )
198 aSystemFileName = rFileName;
200 Open( aSystemFileName, nOpenMode );
217 SAL_INFO(
"tools", OString::number(
static_cast<sal_Int64
>(nSize)) <<
" Bytes from " <<
aFilename);
219 sal_uInt64 nRead = 0;
222 oslFileError rc = osl_readFile(
mxFileHandle,
pData,
static_cast<sal_uInt64
>(nSize),&nRead);
223 if ( rc != osl_File_E_None )
229 return static_cast<std::size_t
>(nRead);
234 SAL_INFO(
"tools", OString::number(
static_cast<sal_Int64
>(nSize)) <<
" Bytes to " <<
aFilename);
236 sal_uInt64 nWrite = 0;
239 oslFileError rc = osl_writeFile(
mxFileHandle,
pData,
static_cast<sal_uInt64
>(nSize),&nWrite);
240 if ( rc != osl_File_E_None )
248 return static_cast<std::size_t
>(nWrite);
264 if ( rc != osl_File_E_None )
281 if (rc != osl_File_E_None)
322 if( !lockFile(
this ) )
324#if OSL_DEBUG_LEVEL > 1
325 fprintf( stderr,
"InternalLock on %s failed\n",
345 oslFileHandle nHandleTmp;
357 osl::DirectoryItem aItem;
358 osl::FileStatus aStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_LinkTargetURL );
361 if ( osl::File::getFileURLFromSystemPath(
aFilename, aFileURL ) != osl::FileBase::E_None )
365 bool bStatValid =
true;
368 bStatValid = ( osl::DirectoryItem::get( aFileURL, aItem) == osl::FileBase::E_None &&
369 aItem.getFileStatus( aStatus ) == osl::FileBase::E_None );
372 if( bStatValid && aStatus.getFileType() == osl::FileStatus::Directory )
380 uFlags = osl_File_OpenFlag_Read;
382 uFlags = osl_File_OpenFlag_Write;
384 uFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
389 uFlags |= osl_File_OpenFlag_Create;
391 uFlags |= osl_File_OpenFlag_Trunc;
393 uFlags |= osl_File_OpenFlag_NoExcl | osl_File_OpenFlag_NoLock;
399 if ( bStatValid && aStatus.getFileType() == osl::FileStatus::Link &&
400 aStatus.getLinkTargetURL().getLength() > 0 )
403 if (osl::File::remove( aFileURL ) == osl::FileBase::E_None )
405 File::copy( aStatus.getLinkTargetURL(), aFileURL );
406#if OSL_DEBUG_LEVEL > 0
408 "Removing link and replacing with file contents (%s) -> (%s).\n",
410 RTL_TEXTENCODING_UTF8).getStr(),
412 RTL_TEXTENCODING_UTF8).getStr() );
419 oslFileError rc = osl_openFile( aFileURL.pData, &nHandleTmp, uFlags );
420 if ( rc != osl_File_E_None )
422 if ( uFlags & osl_File_OpenFlag_Write )
425 uFlags &= ~osl_File_OpenFlag_Write;
426 rc = osl_openFile( aFileURL.pData, &nHandleTmp, uFlags );
429 if ( rc == osl_File_E_None )
433 if ( uFlags & osl_File_OpenFlag_Write )
438 osl_closeFile( nHandleTmp );
476 oslFileError rc = osl_setFileSize(
mxFileHandle, nSize );
477 if (rc != osl_File_E_None )
const OUString & GetFileName() const
virtual std::size_t GetData(void *pData, std::size_t nSize) override
Does not check for EOF, makes isEof callable.
virtual void ResetError() override
set filepointer to beginning of file
virtual void FlushData() override
void Open(const OUString &rFileName, StreamMode eOpenMode)
virtual ~SvFileStream() override
virtual void SetSize(sal_uInt64 nSize) override
virtual sal_uInt64 SeekPos(sal_uInt64 nPos) override
virtual std::size_t PutData(const void *pData, std::size_t nSize) override
SAL_DLLPRIVATE void ClearBuffer()
void SetBufferSize(sal_uInt16 m_nBufSize)
SAL_DLLPRIVATE void ClearError()
StreamMode GetStreamMode() const
void SetError(ErrCode nErrorCode)
void FlushBuffer()
If we have data in our internal buffers, write them out.
#define SVSTREAM_TOO_MANY_OPEN_FILES
#define SVSTREAM_ACCESS_DENIED
#define SVSTREAM_INVALID_PARAMETER
#define SVSTREAM_PATH_NOT_FOUND
#define SVSTREAM_LOCKING_VIOLATION
#define SVSTREAM_SEEK_ERROR
#define SVSTREAM_DISK_FULL
#define SVSTREAM_INVALID_HANDLE
#define SVSTREAM_GENERALERROR
#define SVSTREAM_CANNOT_MAKE
#define SVSTREAM_FILE_NOT_FOUND
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
#define STREAM_SEEK_TO_END
@ READ
allow read accesses
@ COPY_ON_SYMLINK
copy-on-write for symlinks (Unix-only)
@ TEMPORARY
temporary file attribute (Windows-only)
@ TRUNC
Truncate existing file to zero length.
@ NOCREATE
1 == Don't create file
@ WRITE
allow write accesses
static ErrCode GetSvError(int nErrno)