diff options
author | Chris Sherlock <chris.sherlock79@gmail.com> | 2017-06-24 15:22:29 +1000 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-06-25 11:57:06 +0200 |
commit | 651031aab433d0bd4120c096c19169e581527374 (patch) | |
tree | 5f735f38e93a9d7a85d91e9e31b0e87fce0cceba /sal | |
parent | a56e7f790dda566edb75a82fdfcc9e0cbb4e8dc3 (diff) |
osl: whitespace cleanup of unx/file.cxx
Change-Id: I10c9a8d1474920afb7f6afc4d7d34172c398ab4d
Reviewed-on: https://gerrit.libreoffice.org/39201
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Tested-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/osl/unx/file.cxx | 514 |
1 files changed, 286 insertions, 228 deletions
diff --git a/sal/osl/unx/file.cxx b/sal/osl/unx/file.cxx index 1a469d78d8d1..dd3c63cff0d7 100644 --- a/sal/osl/unx/file.cxx +++ b/sal/osl/unx/file.cxx @@ -90,7 +90,7 @@ struct FileHandle_Impl size_t m_bufsiz; sal_uInt8 * m_buffer; - explicit FileHandle_Impl (int fd, Kind kind = KIND_FD, char const * path = "<anon>"); + explicit FileHandle_Impl(int fd, Kind kind = KIND_FD, char const * path = "<anon>"); ~FileHandle_Impl(); static void* operator new (size_t n); @@ -99,44 +99,44 @@ struct FileHandle_Impl static size_t getpagesize(); sal_uInt64 getPos() const; - oslFileError setPos (sal_uInt64 uPos); + oslFileError setPos(sal_uInt64 uPos); sal_uInt64 getSize() const; - oslFileError setSize (sal_uInt64 uSize); + oslFileError setSize(sal_uInt64 uSize); - oslFileError readAt ( + oslFileError readAt( off_t nOffset, - void * pBuffer, + void* pBuffer, size_t nBytesRequested, - sal_uInt64 * pBytesRead); + sal_uInt64* pBytesRead); - oslFileError writeAt ( + oslFileError writeAt( off_t nOffset, - void const * pBuffer, + void const* pBuffer, size_t nBytesToWrite, - sal_uInt64 * pBytesWritten); + sal_uInt64* pBytesWritten); - oslFileError readFileAt ( + oslFileError readFileAt( off_t nOffset, - void * pBuffer, + void* pBuffer, size_t nBytesRequested, - sal_uInt64 * pBytesRead); + sal_uInt64* pBytesRead); - oslFileError writeFileAt ( + oslFileError writeFileAt( off_t nOffset, - void const * pBuffer, + void const* pBuffer, size_t nBytesToWrite, - sal_uInt64 * pBytesWritten); + sal_uInt64* pBytesWritten); - oslFileError readLineAt ( + oslFileError readLineAt( off_t nOffset, - sal_Sequence ** ppSequence, - sal_uInt64 * pBytesRead); + sal_Sequence** ppSequence, + sal_uInt64* pBytesRead); - static oslFileError writeSequence_Impl ( - sal_Sequence ** ppSequence, - size_t * pnOffset, - const void * pBuffer, + static oslFileError writeSequence_Impl( + sal_Sequence** ppSequence, + size_t* pnOffset, + const void* pBuffer, size_t nBytes); oslFileError syncFile(); @@ -145,16 +145,16 @@ struct FileHandle_Impl */ class Allocator { - rtl_cache_type * m_cache; + rtl_cache_type* m_cache; size_t m_bufsiz; public: Allocator(const Allocator&) = delete; Allocator& operator=(const Allocator&) = delete; - static Allocator & get(); + static Allocator& get(); - void allocate (sal_uInt8 ** ppBuffer, size_t * pnSize); - void deallocate (sal_uInt8 * pBuffer); + void allocate(sal_uInt8 **ppBuffer, size_t *pnSize); + void deallocate(sal_uInt8 *pBuffer); protected: Allocator(); @@ -163,70 +163,73 @@ struct FileHandle_Impl class Guard { - pthread_mutex_t * m_mutex; + pthread_mutex_t *m_mutex; public: - explicit Guard(pthread_mutex_t * pMutex); + explicit Guard(pthread_mutex_t *pMutex); ~Guard(); }; }; -FileHandle_Impl::Allocator & -FileHandle_Impl::Allocator::get() +FileHandle_Impl::Allocator& FileHandle_Impl::Allocator::get() { static Allocator g_aBufferAllocator; return g_aBufferAllocator; } FileHandle_Impl::Allocator::Allocator() - : m_cache (nullptr), - m_bufsiz (0) + : m_cache(nullptr), + m_bufsiz(0) { size_t const pagesize = FileHandle_Impl::getpagesize(); if (pagesize != size_t(-1)) { - m_cache = rtl_cache_create ( + m_cache = rtl_cache_create( "osl_file_buffer_cache", pagesize, 0, nullptr, nullptr, nullptr, nullptr, nullptr, 0); + if (m_cache != nullptr) m_bufsiz = pagesize; } } + FileHandle_Impl::Allocator::~Allocator() { - rtl_cache_destroy (m_cache); + rtl_cache_destroy(m_cache); m_cache = nullptr; } -void FileHandle_Impl::Allocator::allocate (sal_uInt8 ** ppBuffer, size_t * pnSize) +void FileHandle_Impl::Allocator::allocate(sal_uInt8 **ppBuffer, size_t *pnSize) { SAL_WARN_IF((!ppBuffer) || (!pnSize), "sal.osl", "FileHandle_Impl::Allocator::allocate(): contract violation"); assert(ppBuffer && pnSize); - if ((ppBuffer != nullptr) && (pnSize != nullptr)) + if ((ppBuffer) && (pnSize)) { *ppBuffer = static_cast< sal_uInt8* >(rtl_cache_alloc(m_cache)); *pnSize = m_bufsiz; } } -void FileHandle_Impl::Allocator::deallocate (sal_uInt8 * pBuffer) + +void FileHandle_Impl::Allocator::deallocate(sal_uInt8 * pBuffer) { - if (pBuffer != nullptr) - rtl_cache_free (m_cache, pBuffer); + if (pBuffer) + rtl_cache_free(m_cache, pBuffer); } FileHandle_Impl::Guard::Guard(pthread_mutex_t * pMutex) - : m_mutex (pMutex) + : m_mutex(pMutex) { assert(m_mutex != nullptr); - (void) pthread_mutex_lock (m_mutex); // ignoring EINVAL ... + (void) pthread_mutex_lock(m_mutex); // ignoring EINVAL ... } + FileHandle_Impl::Guard::~Guard() { assert(m_mutex != nullptr); - (void) pthread_mutex_unlock (m_mutex); + (void) pthread_mutex_unlock(m_mutex); } -FileHandle_Impl::FileHandle_Impl (int fd, enum Kind kind, char const * path) - : m_strFilePath (nullptr), +FileHandle_Impl::FileHandle_Impl(int fd, enum Kind kind, char const * path) + : m_strFilePath(nullptr), m_fd (fd), m_kind (kind), m_state (STATE_SEEKABLE | STATE_READABLE), @@ -239,21 +242,24 @@ FileHandle_Impl::FileHandle_Impl (int fd, enum Kind kind, char const * path) m_buffer (nullptr) { (void) pthread_mutex_init(&m_mutex, nullptr); - rtl_string_newFromStr (&m_strFilePath, path); - if (m_kind == KIND_FD) { + rtl_string_newFromStr(&m_strFilePath, path); + if (m_kind == KIND_FD) + { Allocator::get().allocate (&m_buffer, &m_bufsiz); - if (m_buffer != nullptr) - memset (m_buffer, 0, m_bufsiz); + if (m_buffer) + memset(m_buffer, 0, m_bufsiz); } } + FileHandle_Impl::~FileHandle_Impl() { if (m_kind == KIND_FD) { - Allocator::get().deallocate (m_buffer); + Allocator::get().deallocate(m_buffer); m_buffer = nullptr; } - rtl_string_release (m_strFilePath); + + rtl_string_release(m_strFilePath); m_strFilePath = nullptr; (void) pthread_mutex_destroy(&m_mutex); // ignoring EBUSY ... } @@ -262,6 +268,7 @@ void* FileHandle_Impl::operator new (size_t n) { return rtl_allocateMemory(n); } + void FileHandle_Impl::operator delete (void * p) { rtl_freeMemory(p); @@ -277,7 +284,7 @@ sal_uInt64 FileHandle_Impl::getPos() const return sal::static_int_cast< sal_uInt64 >(m_fileptr); } -oslFileError FileHandle_Impl::setPos (sal_uInt64 uPos) +oslFileError FileHandle_Impl::setPos(sal_uInt64 uPos) { m_fileptr = sal::static_int_cast< off_t >(uPos); return osl_File_E_None; @@ -289,13 +296,13 @@ sal_uInt64 FileHandle_Impl::getSize() const return std::max(m_size, sal::static_int_cast< sal_uInt64 >(bufend)); } -oslFileError FileHandle_Impl::setSize (sal_uInt64 uSize) +oslFileError FileHandle_Impl::setSize(sal_uInt64 uSize) { off_t const nSize = sal::static_int_cast< off_t >(uSize); - if (ftruncate_with_name (m_fd, nSize, m_strFilePath) == -1) + if (ftruncate_with_name(m_fd, nSize, m_strFilePath) == -1) { /* Failure. Save original result. Try fallback algorithm */ - oslFileError result = oslTranslateFileError (OSL_FET_ERROR, errno); + oslFileError result = oslTranslateFileError(OSL_FET_ERROR, errno); /* Check against current size. Fail upon 'shrink' */ if (uSize <= getSize()) @@ -305,23 +312,23 @@ oslFileError FileHandle_Impl::setSize (sal_uInt64 uSize) } /* Save current position */ - off_t const nCurPos = lseek (m_fd, (off_t)0, SEEK_CUR); + off_t const nCurPos = lseek(m_fd, (off_t)0, SEEK_CUR); if (nCurPos == (off_t)(-1)) return result; /* Try 'expand' via 'lseek()' and 'write()' */ - if (lseek (m_fd, (off_t)(nSize - 1), SEEK_SET) == -1) + if (lseek(m_fd, (off_t)(nSize - 1), SEEK_SET) == -1) return result; - if (write (m_fd, "", (size_t)1) == -1) + if (write(m_fd, "", (size_t)1) == -1) { /* Failure. Restore saved position */ - (void) lseek (m_fd, (off_t)(nCurPos), SEEK_SET); + (void) lseek(m_fd, (off_t)(nCurPos), SEEK_SET); return result; } /* Success. Restore saved position */ - if (lseek (m_fd, (off_t)nCurPos, SEEK_SET) == -1) + if (lseek(m_fd, (off_t)nCurPos, SEEK_SET) == -1) return result; } @@ -330,7 +337,7 @@ oslFileError FileHandle_Impl::setSize (sal_uInt64 uSize) return osl_File_E_None; } -oslFileError FileHandle_Impl::readAt ( +oslFileError FileHandle_Impl::readAt( off_t nOffset, void * pBuffer, size_t nBytesRequested, @@ -351,7 +358,9 @@ oslFileError FileHandle_Impl::readAt ( m_offset = nOffset; if ((sal_uInt64) m_offset >= m_size) + { nBytes = 0; + } else { nBytes = std::min(nBytesRequested, (size_t) (m_size - m_offset)); @@ -362,8 +371,8 @@ oslFileError FileHandle_Impl::readAt ( return osl_File_E_None; } - ssize_t nBytes = ::pread (m_fd, pBuffer, nBytesRequested, nOffset); - if ((nBytes == -1) && (EOVERFLOW == errno)) + ssize_t nBytes = ::pread(m_fd, pBuffer, nBytesRequested, nOffset); + if ((nBytes == -1) && (errno == EOVERFLOW)) { /* Some 'pread()'s fail with EOVERFLOW when reading at (or past) * end-of-file, different from 'lseek() + read()' behaviour. @@ -371,14 +380,16 @@ oslFileError FileHandle_Impl::readAt ( */ nBytes = 0; } + if (nBytes == -1) - return oslTranslateFileError (OSL_FET_ERROR, errno); + return oslTranslateFileError(OSL_FET_ERROR, errno); *pBytesRead = nBytes; + return osl_File_E_None; } -oslFileError FileHandle_Impl::writeAt ( +oslFileError FileHandle_Impl::writeAt( off_t nOffset, void const * pBuffer, size_t nBytesToWrite, @@ -392,38 +403,42 @@ oslFileError FileHandle_Impl::writeAt ( if (!(m_state & STATE_WRITEABLE)) return osl_File_E_BADF; - ssize_t nBytes = ::pwrite (m_fd, pBuffer, nBytesToWrite, nOffset); + ssize_t nBytes = ::pwrite(m_fd, pBuffer, nBytesToWrite, nOffset); if (nBytes == -1) - return oslTranslateFileError (OSL_FET_ERROR, errno); + return oslTranslateFileError(OSL_FET_ERROR, errno); - m_size = std::max (m_size, sal::static_int_cast< sal_uInt64 >(nOffset + nBytes)); + m_size = std::max(m_size, sal::static_int_cast< sal_uInt64 >(nOffset + nBytes)); *pBytesWritten = nBytes; + return osl_File_E_None; } -oslFileError FileHandle_Impl::readFileAt ( +oslFileError FileHandle_Impl::readFileAt( off_t nOffset, - void * pBuffer, + void* pBuffer, size_t nBytesRequested, - sal_uInt64 * pBytesRead) + sal_uInt64* pBytesRead) { if ((m_state & STATE_SEEKABLE) == 0) { // not seekable (pipe) - ssize_t nBytes = ::read (m_fd, pBuffer, nBytesRequested); + ssize_t nBytes = ::read(m_fd, pBuffer, nBytesRequested); if (nBytes == -1) - return oslTranslateFileError (OSL_FET_ERROR, errno); + return oslTranslateFileError(OSL_FET_ERROR, errno); + *pBytesRead = nBytes; + return osl_File_E_None; } + if (m_kind == KIND_MEM || m_buffer == nullptr) { // not buffered - return readAt (nOffset, pBuffer, nBytesRequested, pBytesRead); + return readAt(nOffset, pBuffer, nBytesRequested, pBytesRead); } - sal_uInt8 * buffer = static_cast<sal_uInt8*>(pBuffer); + sal_uInt8 *buffer = static_cast<sal_uInt8*>(pBuffer); for (*pBytesRead = 0; nBytesRequested > 0; ) { off_t const bufptr = (nOffset / m_bufsiz) * m_bufsiz; @@ -435,6 +450,7 @@ oslFileError FileHandle_Impl::readFileAt ( oslFileError result = syncFile(); if (result != osl_File_E_None) return result; + m_bufptr = -1; m_buflen = 0; @@ -442,40 +458,44 @@ oslFileError FileHandle_Impl::readFileAt ( { // buffer too small, read through from file sal_uInt64 uDone = 0; - result = readAt (nOffset, &(buffer[*pBytesRead]), nBytesRequested, &uDone); + result = readAt(nOffset, &(buffer[*pBytesRead]), nBytesRequested, &uDone); if (result != osl_File_E_None) return result; *pBytesRead += uDone; + return osl_File_E_None; } // update buffer (pointer) sal_uInt64 uDone = 0; - result = readAt (bufptr, m_buffer, m_bufsiz, &uDone); + result = readAt(bufptr, m_buffer, m_bufsiz, &uDone); if (result != osl_File_E_None) return result; + m_bufptr = bufptr; m_buflen = uDone; } + if (bufpos >= m_buflen) { // end of file return osl_File_E_None; } - size_t const bytes = std::min (m_buflen - bufpos, nBytesRequested); + size_t const bytes = std::min(m_buflen - bufpos, nBytesRequested); SAL_INFO("sal.file", "FileHandle_Impl::readFileAt(" << m_fd << ", " << nOffset << ", " << bytes << ")"); - memcpy (&(buffer[*pBytesRead]), &(m_buffer[bufpos]), bytes); + memcpy(&(buffer[*pBytesRead]), &(m_buffer[bufpos]), bytes); nBytesRequested -= bytes; *pBytesRead += bytes; nOffset += bytes; } + return osl_File_E_None; } -oslFileError FileHandle_Impl::writeFileAt ( +oslFileError FileHandle_Impl::writeFileAt( off_t nOffset, void const * pBuffer, size_t nBytesToWrite, @@ -484,22 +504,25 @@ oslFileError FileHandle_Impl::writeFileAt ( if ((m_state & STATE_SEEKABLE) == 0) { // not seekable (pipe) - ssize_t nBytes = ::write (m_fd, pBuffer, nBytesToWrite); + ssize_t nBytes = ::write(m_fd, pBuffer, nBytesToWrite); if (nBytes == -1) - return oslTranslateFileError (OSL_FET_ERROR, errno); + return oslTranslateFileError(OSL_FET_ERROR, errno); + *pBytesWritten = nBytes; + return osl_File_E_None; } - if (m_buffer == nullptr) + + if (!m_buffer) { // not buffered - return writeAt (nOffset, pBuffer, nBytesToWrite, pBytesWritten); + return writeAt(nOffset, pBuffer, nBytesToWrite, pBytesWritten); } sal_uInt8 const * buffer = static_cast<sal_uInt8 const *>(pBuffer); - for (*pBytesWritten = 0; nBytesToWrite > 0; ) + for (*pBytesWritten = 0; nBytesToWrite > 0;) { - off_t const bufptr = (nOffset / m_bufsiz) * m_bufsiz; + off_t const bufptr = (nOffset / m_bufsiz) * m_bufsiz; size_t const bufpos = (nOffset % m_bufsiz); if (bufptr != m_bufptr) { @@ -514,29 +537,32 @@ oslFileError FileHandle_Impl::writeFileAt ( { // buffer to small, write through to file sal_uInt64 uDone = 0; - result = writeAt (nOffset, &(buffer[*pBytesWritten]), nBytesToWrite, &uDone); + result = writeAt(nOffset, &(buffer[*pBytesWritten]), nBytesToWrite, &uDone); if (result != osl_File_E_None) return result; + if (uDone != nBytesToWrite) return osl_File_E_IO; *pBytesWritten += uDone; + return osl_File_E_None; } // update buffer (pointer) sal_uInt64 uDone = 0; - result = readAt (bufptr, m_buffer, m_bufsiz, &uDone); + result = readAt(bufptr, m_buffer, m_bufsiz, &uDone); if (result != osl_File_E_None) return result; + m_bufptr = bufptr; m_buflen = uDone; } - size_t const bytes = std::min (m_bufsiz - bufpos, nBytesToWrite); + size_t const bytes = std::min(m_bufsiz - bufpos, nBytesToWrite); SAL_INFO("sal.file", "FileHandle_Impl::writeFileAt(" << m_fd << ", " << nOffset << ", " << bytes << ")"); - memcpy (&(m_buffer[bufpos]), &(buffer[*pBytesWritten]), bytes); + memcpy(&(m_buffer[bufpos]), &(buffer[*pBytesWritten]), bytes); nBytesToWrite -= bytes; *pBytesWritten += bytes; nOffset += bytes; @@ -544,10 +570,11 @@ oslFileError FileHandle_Impl::writeFileAt ( m_buflen = std::max(m_buflen, bufpos + bytes); m_state |= STATE_MODIFIED; } + return osl_File_E_None; } -oslFileError FileHandle_Impl::readLineAt ( +oslFileError FileHandle_Impl::readLineAt( off_t nOffset, sal_Sequence ** ppSequence, sal_uInt64 * pBytesRead) @@ -564,7 +591,7 @@ oslFileError FileHandle_Impl::readLineAt ( /* update buffer (pointer) */ sal_uInt64 uDone = 0; - result = readAt (bufptr, m_buffer, m_bufsiz, &uDone); + result = readAt(bufptr, m_buffer, m_bufsiz, &uDone); if (result != osl_File_E_None) return result; @@ -577,7 +604,7 @@ oslFileError FileHandle_Impl::readLineAt ( static int const LINE_STATE_LF = 2; size_t bufpos = nOffset - m_bufptr, curpos = bufpos, dstpos = 0; - int state = (bufpos >= m_buflen) ? LINE_STATE_LF : LINE_STATE_BEGIN; + int state = (bufpos >= m_buflen) ? LINE_STATE_LF : LINE_STATE_BEGIN; for ( ; state != LINE_STATE_LF; ) { @@ -587,10 +614,11 @@ oslFileError FileHandle_Impl::readLineAt ( if (0 < (curpos - bufpos)) { /* flush buffer to sequence */ - result = writeSequence_Impl ( + result = writeSequence_Impl( ppSequence, &dstpos, &(m_buffer[bufpos]), curpos - bufpos); if (result != osl_File_E_None) return result; + *pBytesRead += curpos - bufpos; nOffset += curpos - bufpos; } @@ -600,9 +628,10 @@ oslFileError FileHandle_Impl::readLineAt ( { /* update buffer (pointer) */ sal_uInt64 uDone = 0; - result = readAt (bufptr, m_buffer, m_bufsiz, &uDone); + result = readAt(bufptr, m_buffer, m_bufsiz, &uDone); if (result != osl_File_E_None) return result; + m_bufptr = bufptr; m_buflen = uDone; } @@ -612,6 +641,7 @@ oslFileError FileHandle_Impl::readLineAt ( if (bufpos >= m_buflen) break; } + switch (state) { case LINE_STATE_CR: @@ -647,10 +677,11 @@ oslFileError FileHandle_Impl::readLineAt ( curpos++; /* flush buffer to sequence */ - result = writeSequence_Impl ( + result = writeSequence_Impl( ppSequence, &dstpos, &(m_buffer[bufpos]), curpos - bufpos - 1); if (result != osl_File_E_None) return result; + *pBytesRead += curpos - bufpos; nOffset += curpos - bufpos; } @@ -658,17 +689,20 @@ oslFileError FileHandle_Impl::readLineAt ( } } - result = writeSequence_Impl (ppSequence, &dstpos, nullptr, 0); + result = writeSequence_Impl(ppSequence, &dstpos, nullptr, 0); if (result != osl_File_E_None) return result; + if (0 < dstpos) return osl_File_E_None; + if (bufpos >= m_buflen) return osl_File_E_AGAIN; + return osl_File_E_None; } -oslFileError FileHandle_Impl::writeSequence_Impl ( +oslFileError FileHandle_Impl::writeSequence_Impl( sal_Sequence ** ppSequence, size_t * pnOffset, const void * pBuffer, @@ -685,12 +719,14 @@ oslFileError FileHandle_Impl::writeSequence_Impl ( /* resize sequence */ rtl_byte_sequence_realloc(ppSequence, nElements); } + if (*ppSequence != nullptr && nBytes != 0) { /* fill sequence */ memcpy(&((*ppSequence)->elements[*pnOffset]), pBuffer, nBytes); *pnOffset += nBytes; } + return (*ppSequence != nullptr) ? osl_File_E_None : osl_File_E_NOMEM; } @@ -700,26 +736,29 @@ oslFileError FileHandle_Impl::syncFile() if (m_state & STATE_MODIFIED) { sal_uInt64 uDone = 0; - result = writeAt (m_bufptr, m_buffer, m_buflen, &uDone); + result = writeAt(m_bufptr, m_buffer, m_buflen, &uDone); if (result != osl_File_E_None) return result; + if (uDone != m_buflen) return osl_File_E_IO; + m_state &= ~STATE_MODIFIED; } + return result; } -oslFileHandle osl::detail::createFileHandleFromFD( int fd ) +oslFileHandle osl::detail::createFileHandleFromFD(int fd) { if (fd == -1) return nullptr; // EINVAL struct stat aFileStat; - if (fstat (fd, &aFileStat) == -1) + if (fstat(fd, &aFileStat) == -1) return nullptr; // EBADF - FileHandle_Impl * pImpl = new FileHandle_Impl (fd); + FileHandle_Impl *pImpl = new FileHandle_Impl(fd); // assume writeable pImpl->m_state |= FileHandle_Impl::STATE_WRITEABLE; @@ -735,10 +774,11 @@ oslFileHandle osl::detail::createFileHandleFromFD( int fd ) } SAL_INFO("sal.file", "osl::detail::createFileHandleFromFD(" << pImpl->m_fd << ", writeable) => " << pImpl->m_strFilePath); + return static_cast<oslFileHandle>(pImpl); } -static int osl_file_adjustLockFlags (const char * path, int flags) +static int osl_file_adjustLockFlags(const char *path, int flags) { #ifdef MACOSX /* @@ -749,7 +789,7 @@ static int osl_file_adjustLockFlags (const char * path, int flags) * for the filesystem name. */ struct statfs s; - if( 0 <= statfs( path, &s ) ) + if( 0 <= statfs(path, &s) ) { if( 0 == strncmp("afpfs", s.f_fstypename, 5) ) { @@ -768,7 +808,7 @@ static int osl_file_adjustLockFlags (const char * path, int flags) return flags; } -static bool osl_file_queryLocking (sal_uInt32 uFlags) +static bool osl_file_queryLocking(sal_uInt32 uFlags) { #if !defined HAVE_O_EXLOCK if (!(uFlags & osl_File_OpenFlag_NoLock) @@ -788,10 +828,10 @@ static bool osl_file_queryLocking (sal_uInt32 uFlags) namespace { -static oslFileError -openMemoryAsFile( void *address, size_t size, oslFileHandle *pHandle, const char *path ) +static oslFileError openMemoryAsFile(void *address, size_t size, oslFileHandle *pHandle, + const char *path) { - FileHandle_Impl * pImpl = new FileHandle_Impl (-1, FileHandle_Impl::KIND_MEM, path); + FileHandle_Impl *pImpl = new FileHandle_Impl(-1, FileHandle_Impl::KIND_MEM, path); pImpl->m_size = sal::static_int_cast< sal_uInt64 >(size); *pHandle = (oslFileHandle)(pImpl); @@ -817,9 +857,8 @@ openMemoryAsFile( void *address, size_t size, oslFileHandle *pHandle, const char #define OPEN_CREATE_FLAGS ( O_CREAT | O_RDWR ) #endif -oslFileError -openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, - mode_t mode) +oslFileError openFilePath(const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, + mode_t mode) { oslFileError eRet; @@ -827,23 +866,27 @@ openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, /* Opening a file from /assets read-only means * we should mmap it from the .apk file */ - if (strncmp (cpFilePath, "/assets/", sizeof ("/assets/") - 1) == 0) + if (strncmp(cpFilePath, "/assets/", sizeof ("/assets/") - 1) == 0) { void* address; size_t size; AAssetManager* mgr = lo_get_native_assetmgr(); AAsset* asset = AAssetManager_open(mgr, cpFilePath + sizeof("/assets/")-1, AASSET_MODE_BUFFER); - if (NULL == asset) { + if (NULL == asset) + { address = NULL; errno = ENOENT; __android_log_print(ANDROID_LOG_ERROR,"libo:sal/osl/unx/file", "failed to open %s", cpFilePath); return osl_File_E_NOENT; - } else { + } + else + { size = AAsset_getLength(asset); - address = malloc (sizeof(char)*size); - AAsset_read (asset,address,size); + address = malloc(sizeof(char)*size); + AAsset_read(asset,address,size); AAsset_close(asset); } + if (uFlags & osl_File_OpenFlag_Write) { // It seems to work better to silently "open" it read-only @@ -857,29 +900,28 @@ openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, #endif /* set mode and flags */ - int defmode = (uFlags & osl_File_OpenFlag_Private) - ? S_IRUSR : S_IRUSR | S_IRGRP | S_IROTH; + int defmode = (uFlags & osl_File_OpenFlag_Private) ? S_IRUSR : S_IRUSR | S_IRGRP | S_IROTH; int flags = O_RDONLY; + if (uFlags & osl_File_OpenFlag_Write) { - defmode |= (uFlags & osl_File_OpenFlag_Private) - ? S_IWUSR : S_IWUSR | S_IWGRP | S_IWOTH; + defmode |= (uFlags & osl_File_OpenFlag_Private) ? S_IWUSR : S_IWUSR | S_IWGRP | S_IWOTH; flags = OPEN_WRITE_FLAGS; } + if (uFlags & osl_File_OpenFlag_Create) { - defmode |= (uFlags & osl_File_OpenFlag_Private) - ? S_IWUSR : S_IWUSR | S_IWGRP | S_IWOTH; + defmode |= (uFlags & osl_File_OpenFlag_Private) ? S_IWUSR : S_IWUSR | S_IWGRP | S_IWOTH; flags = OPEN_CREATE_FLAGS; } + if (mode == mode_t(-1)) - { mode = defmode; - } /* Check for flags passed in from SvFileStream::Open() */ if (uFlags & osl_File_OpenFlag_Trunc) flags |= O_TRUNC; + if (!(uFlags & osl_File_OpenFlag_NoExcl)) flags |= O_EXCL; @@ -909,46 +951,50 @@ openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, fd = open_c( cpFilePath, rdonly_flags, mode ); } #endif + if (fd == -1) { int saved_errno = errno; SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << ((flags & O_RDWR) ? "writeable":"readonly") << ") failed: " << strerror(saved_errno)); - return oslTranslateFileError (OSL_FET_ERROR, saved_errno); + return oslTranslateFileError(OSL_FET_ERROR, saved_errno); } #if !HAVE_FEATURE_MACOSX_SANDBOX /* reset O_NONBLOCK flag */ if (flags & O_NONBLOCK) { - int f = fcntl (fd, F_GETFL, 0); + int f = fcntl(fd, F_GETFL, 0); if (f == -1) { int saved_errno = errno; SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << ((flags & O_RDWR) ? "writeable":"readonly") << "): fcntl(" << fd << ", F_GETFL) failed: " << strerror(saved_errno)); - eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno); + eRet = oslTranslateFileError(OSL_FET_ERROR, saved_errno); (void) close(fd); return eRet; } - if (fcntl (fd, F_SETFL, (f & ~O_NONBLOCK)) == -1) + + if (fcntl(fd, F_SETFL, (f & ~O_NONBLOCK)) == -1) { int saved_errno = errno; - SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << ((flags & O_RDWR) ? "writeable":"readonly") << "): fcntl(" << fd << ", F_SETFL) failed: " << strerror(saved_errno)); - eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno); + SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << ((flags & O_RDWR) ? "writeable":"readonly") << "): fcntl(" << fd << ", F_SETFL) failed: " << strerror(saved_errno)); + eRet = oslTranslateFileError(OSL_FET_ERROR, saved_errno); (void) close(fd); return eRet; } } #endif + /* get file status (mode, size) */ struct stat aFileStat; - if (fstat (fd, &aFileStat) == -1) + if (fstat(fd, &aFileStat) == -1) { int saved_errno = errno; SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << ((flags & O_RDWR) ? "writeable":"readonly") << "): fstat(" << fd << ") failed: " << strerror(saved_errno)); - eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno); + eRet = oslTranslateFileError(OSL_FET_ERROR, saved_errno); (void) close(fd); return eRet; } + if (!S_ISREG(aFileStat.st_mode)) { /* we only open regular files here */ @@ -957,15 +1003,15 @@ openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, return osl_File_E_INVAL; } - if (osl_file_queryLocking (uFlags)) + if (osl_file_queryLocking(uFlags)) { #ifdef MACOSX - if (-1 == flock (fd, LOCK_EX | LOCK_NB)) + if (flock(fd, LOCK_EX | LOCK_NB) == -1) { /* Mac OSX returns ENOTSUP for webdav drives. We should try read lock */ if ((errno != ENOTSUP) || ((-1 == flock (fd, LOCK_SH | LOCK_NB)) && (errno != ENOTSUP))) { - eRet = oslTranslateFileError (OSL_FET_ERROR, errno); + eRet = oslTranslateFileError(OSL_FET_ERROR, errno); (void) close(fd); return eRet; } @@ -979,11 +1025,11 @@ openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, aflock.l_start = 0; aflock.l_len = 0; - if (fcntl (fd, F_SETLK, &aflock) == -1) + if (fcntl(fd, F_SETLK, &aflock) == -1) { int saved_errno = errno; SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << ((flags & O_RDWR) ? "writeable":"readonly") << "): fcntl(" << fd << ", F_SETLK) failed: " << strerror(saved_errno)); - eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno); + eRet = oslTranslateFileError(OSL_FET_ERROR, saved_errno); (void) close(fd); return eRet; } @@ -992,25 +1038,25 @@ openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags, } /* allocate memory for impl structure */ - FileHandle_Impl * pImpl = new FileHandle_Impl (fd, FileHandle_Impl::KIND_FD, cpFilePath); + FileHandle_Impl *pImpl = new FileHandle_Impl(fd, FileHandle_Impl::KIND_FD, cpFilePath); if (flags & O_RDWR) pImpl->m_state |= FileHandle_Impl::STATE_WRITEABLE; + pImpl->m_size = sal::static_int_cast< sal_uInt64 >(aFileStat.st_size); SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << ((flags & O_RDWR) ? "writeable":"readonly") << ") => " << pImpl->m_fd); *pHandle = static_cast<oslFileHandle>(pImpl); + return osl_File_E_None; } -oslFileError -SAL_CALL osl_openFile( rtl_uString* ustrFileURL, oslFileHandle* pHandle, sal_uInt32 uFlags ) +oslFileError SAL_CALL osl_openFile(rtl_uString* ustrFileURL, oslFileHandle* pHandle, sal_uInt32 uFlags) { return openFile(ustrFileURL, pHandle, uFlags, mode_t(-1)); } -oslFileError -SAL_CALL openFile( rtl_uString* ustrFileURL, oslFileHandle* pHandle, sal_uInt32 uFlags, mode_t mode ) +oslFileError SAL_CALL openFile(rtl_uString* ustrFileURL, oslFileHandle* pHandle, sal_uInt32 uFlags, mode_t mode) { oslFileError eRet; @@ -1019,22 +1065,21 @@ SAL_CALL openFile( rtl_uString* ustrFileURL, oslFileHandle* pHandle, sal_uInt32 /* convert file URL to system path */ char buffer[PATH_MAX]; - eRet = FileURLToPath (buffer, sizeof(buffer), ustrFileURL); + eRet = FileURLToPath(buffer, sizeof(buffer), ustrFileURL); if (eRet != osl_File_E_None) return eRet; #ifdef MACOSX - if (macxp_resolveAlias (buffer, sizeof(buffer)) != 0) - return oslTranslateFileError (OSL_FET_ERROR, errno); + if (macxp_resolveAlias(buffer, sizeof(buffer)) != 0) + return oslTranslateFileError(OSL_FET_ERROR, errno); #endif /* MACOSX */ - return openFilePath (buffer, pHandle, uFlags, mode); + return openFilePath(buffer, pHandle, uFlags, mode); } -oslFileError -SAL_CALL osl_closeFile( oslFileHandle Handle ) +oslFileError SAL_CALL osl_closeFile(oslFileHandle Handle) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if (pImpl == nullptr) return osl_File_E_INVAL; @@ -1054,28 +1099,27 @@ SAL_CALL osl_closeFile( oslFileHandle Handle ) if (pImpl->m_fd < 0) return osl_File_E_INVAL; - (void) pthread_mutex_lock (&(pImpl->m_mutex)); + (void) pthread_mutex_lock(&(pImpl->m_mutex)); /* close(2) implicitly (and unconditionally) unlocks */ oslFileError result = pImpl->syncFile(); if (result != osl_File_E_None) { /* close, ignoring double failure */ - (void) close (pImpl->m_fd); + (void) close(pImpl->m_fd); } - else if (close (pImpl->m_fd) == -1) + else if (close(pImpl->m_fd) == -1) { /* translate error code */ - result = oslTranslateFileError (OSL_FET_ERROR, errno); + result = oslTranslateFileError(OSL_FET_ERROR, errno); } - (void) pthread_mutex_unlock (&(pImpl->m_mutex)); + (void) pthread_mutex_unlock(&(pImpl->m_mutex)); delete pImpl; return result; } -oslFileError -SAL_CALL osl_syncFile(oslFileHandle Handle) +oslFileError SAL_CALL osl_syncFile(oslFileHandle Handle) { FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); @@ -1085,22 +1129,24 @@ SAL_CALL osl_syncFile(oslFileHandle Handle) if (pImpl->m_kind == FileHandle_Impl::KIND_MEM) return osl_File_E_None; - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); SAL_INFO("sal.file", "osl_syncFile(" << pImpl->m_fd << ")"); + oslFileError result = pImpl->syncFile(); + if (result != osl_File_E_None) return result; - if (fsync (pImpl->m_fd) == -1) - return oslTranslateFileError (OSL_FET_ERROR, errno); + + if (fsync(pImpl->m_fd) == -1) + return oslTranslateFileError(OSL_FET_ERROR, errno); return osl_File_E_None; } const off_t MAX_OFF_T = std::numeric_limits< off_t >::max(); -oslFileError -SAL_CALL osl_mapFile ( +oslFileError SAL_CALL osl_mapFile( oslFileHandle Handle, void** ppAddr, sal_uInt64 uLength, @@ -1108,14 +1154,16 @@ SAL_CALL osl_mapFile ( sal_uInt32 uFlags ) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1)) || (ppAddr == nullptr)) return osl_File_E_INVAL; + *ppAddr = nullptr; if (uLength > SAL_MAX_SIZE) return osl_File_E_OVERFLOW; + size_t const nLength = sal::static_int_cast< size_t >(uLength); sal_uInt64 const limit_off_t = MAX_OFF_T; @@ -1131,8 +1179,10 @@ SAL_CALL osl_mapFile ( off_t const nOffset = sal::static_int_cast< off_t >(uOffset); void* p = mmap(nullptr, nLength, PROT_READ, MAP_SHARED, pImpl->m_fd, nOffset); + if (MAP_FAILED == p) return oslTranslateFileError(OSL_FET_ERROR, errno); + *ppAddr = p; if (uFlags & osl_File_MapFlag_RandomAccess) @@ -1145,8 +1195,8 @@ SAL_CALL osl_mapFile ( * Pagein, touching first byte of every memory page. * Note: volatile disables optimizing the loop away. */ - sal_uInt8 * pData (static_cast<sal_uInt8*>(*ppAddr)); - size_t nSize (nLength); + sal_uInt8 *pData(static_cast<sal_uInt8*>(*ppAddr)); + size_t nSize(nLength); volatile sal_uInt8 c = 0; while (nSize > nPageSize) @@ -1155,12 +1205,12 @@ SAL_CALL osl_mapFile ( pData += nPageSize; nSize -= nPageSize; } + if (nSize > 0) - { c^= pData[0]; - } } } + if (uFlags & osl_File_MapFlag_WillNeed) { // On Linux, madvise(..., MADV_WILLNEED) appears to have the undesirable @@ -1173,28 +1223,25 @@ SAL_CALL osl_mapFile ( #if defined MACOSX || (defined(__sun) && (!defined(__XOPEN_OR_POSIX) || defined(_XPG6) || defined(__EXTENSIONS__))) int e = posix_madvise(p, nLength, POSIX_MADV_WILLNEED); if (e != 0) - { SAL_INFO("sal.file", "posix_madvise(..., POSIX_MADV_WILLNEED) failed with " << e); - } + #elif defined __sun if (madvise(static_cast< caddr_t >(p), nLength, MADV_WILLNEED) != 0) - { SAL_INFO("sal.file", "madvise(..., MADV_WILLNEED) failed with " << strerror(errno)); - } #endif } + return osl_File_E_None; } -static -oslFileError -unmapFile (void* pAddr, sal_uInt64 uLength) +static oslFileError unmapFile(void* pAddr, sal_uInt64 uLength) { if (pAddr == nullptr) return osl_File_E_INVAL; if (uLength > SAL_MAX_SIZE) return osl_File_E_OVERFLOW; + size_t const nLength = sal::static_int_cast< size_t >(uLength); if (munmap(pAddr, nLength) == -1) @@ -1210,58 +1257,56 @@ unmapFile (void* pAddr, sal_uInt64 uLength) // folder osl_mapFile just returns a pointer to the file inside the // already mmapped .apk archive. -oslFileError -SAL_CALL osl_unmapFile (void* pAddr, sal_uInt64 uLength) +oslFileError SAL_CALL osl_unmapFile(void* pAddr, sal_uInt64 uLength) { - return unmapFile (pAddr, uLength); + return unmapFile(pAddr, uLength); } #endif -oslFileError -SAL_CALL osl_unmapMappedFile (oslFileHandle Handle, void* pAddr, sal_uInt64 uLength) +oslFileError SAL_CALL osl_unmapMappedFile(oslFileHandle Handle, void* pAddr, sal_uInt64 uLength) { - FileHandle_Impl * pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl *pImpl = static_cast<FileHandle_Impl*>(Handle); if (pImpl == nullptr) return osl_File_E_INVAL; if (pImpl->m_kind == FileHandle_Impl::KIND_FD) - return unmapFile (pAddr, uLength); + return unmapFile(pAddr, uLength); // For parts of already mmapped "parent" files, whose mapping we // can't change, not much we can or should do... return osl_File_E_None; } -oslFileError -SAL_CALL osl_readLine ( +oslFileError SAL_CALL osl_readLine( oslFileHandle Handle, sal_Sequence ** ppSequence) { - FileHandle_Impl * pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl *pImpl = static_cast<FileHandle_Impl*>(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1)) || (ppSequence == nullptr)) return osl_File_E_INVAL; + sal_uInt64 uBytesRead = 0; // read at current fileptr; fileptr += uBytesRead; - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); - oslFileError result = pImpl->readLineAt ( - pImpl->m_fileptr, ppSequence, &uBytesRead); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); + oslFileError result = pImpl->readLineAt(pImpl->m_fileptr, ppSequence, &uBytesRead); + if (result == osl_File_E_None) pImpl->m_fileptr += uBytesRead; + return result; } -oslFileError -SAL_CALL osl_readFile ( +oslFileError SAL_CALL osl_readFile( oslFileHandle Handle, void * pBuffer, sal_uInt64 uBytesRequested, sal_uInt64 * pBytesRead) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1)) || (pBuffer == nullptr) || (pBytesRead == nullptr)) return osl_File_E_INVAL; @@ -1269,77 +1314,82 @@ SAL_CALL osl_readFile ( static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max(); if (g_limit_ssize_t < uBytesRequested) return osl_File_E_OVERFLOW; + size_t const nBytesRequested = sal::static_int_cast< size_t >(uBytesRequested); // read at current fileptr; fileptr += *pBytesRead; - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); - oslFileError result = pImpl->readFileAt ( - pImpl->m_fileptr, pBuffer, nBytesRequested, pBytesRead); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); + oslFileError result = pImpl->readFileAt(pImpl->m_fileptr, pBuffer, nBytesRequested, pBytesRead); + if (result == osl_File_E_None) pImpl->m_fileptr += *pBytesRead; + return result; } -oslFileError -SAL_CALL osl_writeFile ( +oslFileError SAL_CALL osl_writeFile( oslFileHandle Handle, const void * pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64 * pBytesWritten) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || (pImpl->m_fd == -1) || (pBuffer == nullptr) || (pBytesWritten == nullptr)) return osl_File_E_INVAL; + if ((pImpl->m_state & FileHandle_Impl::STATE_WRITEABLE) == 0) return osl_File_E_BADF; static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max(); if (g_limit_ssize_t < uBytesToWrite) return osl_File_E_OVERFLOW; + size_t const nBytesToWrite = sal::static_int_cast< size_t >(uBytesToWrite); // write at current fileptr; fileptr += *pBytesWritten; - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); - oslFileError result = pImpl->writeFileAt ( - pImpl->m_fileptr, pBuffer, nBytesToWrite, pBytesWritten); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); + oslFileError result = pImpl->writeFileAt(pImpl->m_fileptr, pBuffer, nBytesToWrite, pBytesWritten); if (result == osl_File_E_None) pImpl->m_fileptr += *pBytesWritten; + return result; } -oslFileError -SAL_CALL osl_readFileAt ( +oslFileError SAL_CALL osl_readFileAt( oslFileHandle Handle, sal_uInt64 uOffset, void* pBuffer, sal_uInt64 uBytesRequested, sal_uInt64* pBytesRead) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1)) || (pBuffer == nullptr) || (pBytesRead == nullptr)) return osl_File_E_INVAL; + if ((pImpl->m_state & FileHandle_Impl::STATE_SEEKABLE) == 0) return osl_File_E_SPIPE; sal_uInt64 const limit_off_t = MAX_OFF_T; if (uOffset > limit_off_t) return osl_File_E_OVERFLOW; + off_t const nOffset = sal::static_int_cast< off_t >(uOffset); static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max(); if (g_limit_ssize_t < uBytesRequested) return osl_File_E_OVERFLOW; + size_t const nBytesRequested = sal::static_int_cast< size_t >(uBytesRequested); // read at specified fileptr - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); - return pImpl->readFileAt (nOffset, pBuffer, nBytesRequested, pBytesRead); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); + + return pImpl->readFileAt(nOffset, pBuffer, nBytesRequested, pBytesRead); } -oslFileError -SAL_CALL osl_writeFileAt ( +oslFileError SAL_CALL osl_writeFileAt( oslFileHandle Handle, sal_uInt64 uOffset, const void* pBuffer, @@ -1350,56 +1400,60 @@ SAL_CALL osl_writeFileAt ( if ((pImpl == nullptr) || (pImpl->m_fd == -1) || (pBuffer == nullptr) || (pBytesWritten == nullptr)) return osl_File_E_INVAL; + if ((pImpl->m_state & FileHandle_Impl::STATE_SEEKABLE) == 0) return osl_File_E_SPIPE; + if ((pImpl->m_state & FileHandle_Impl::STATE_WRITEABLE) == 0) return osl_File_E_BADF; sal_uInt64 const limit_off_t = MAX_OFF_T; if (limit_off_t < uOffset) return osl_File_E_OVERFLOW; + off_t const nOffset = sal::static_int_cast< off_t >(uOffset); static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max(); if (g_limit_ssize_t < uBytesToWrite) return osl_File_E_OVERFLOW; + size_t const nBytesToWrite = sal::static_int_cast< size_t >(uBytesToWrite); // write at specified fileptr - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); - return pImpl->writeFileAt (nOffset, pBuffer, nBytesToWrite, pBytesWritten); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); + + return pImpl->writeFileAt(nOffset, pBuffer, nBytesToWrite, pBytesWritten); } -oslFileError -SAL_CALL osl_isEndOfFile( oslFileHandle Handle, sal_Bool *pIsEOF ) +oslFileError SAL_CALL osl_isEndOfFile(oslFileHandle Handle, sal_Bool *pIsEOF) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1)) || (pIsEOF == nullptr)) return osl_File_E_INVAL; - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); *pIsEOF = (pImpl->getPos() == pImpl->getSize()); + return osl_File_E_None; } -oslFileError -SAL_CALL osl_getFilePos( oslFileHandle Handle, sal_uInt64* pPos ) +oslFileError SAL_CALL osl_getFilePos(oslFileHandle Handle, sal_uInt64* pPos) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1)) || (pPos == nullptr)) return osl_File_E_INVAL; - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); *pPos = pImpl->getPos(); + return osl_File_E_None; } -oslFileError -SAL_CALL osl_setFilePos (oslFileHandle Handle, sal_uInt32 uHow, sal_Int64 uOffset) +oslFileError SAL_CALL osl_setFilePos(oslFileHandle Handle, sal_uInt32 uHow, sal_Int64 uOffset) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1))) return osl_File_E_INVAL; @@ -1407,10 +1461,11 @@ SAL_CALL osl_setFilePos (oslFileHandle Handle, sal_uInt32 uHow, sal_Int64 uOffse sal_Int64 const limit_off_t = MAX_OFF_T; if (uOffset > limit_off_t) return osl_File_E_OVERFLOW; + off_t nPos = 0, nOffset = sal::static_int_cast< off_t >(uOffset); - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); - switch(uHow) + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); + switch (uHow) { case osl_Pos_Absolut: if (0 > nOffset) @@ -1421,6 +1476,7 @@ SAL_CALL osl_setFilePos (oslFileHandle Handle, sal_uInt32 uHow, sal_Int64 uOffse nPos = sal::static_int_cast< off_t >(pImpl->getPos()); if ((0 > nOffset) && (-1*nOffset > nPos)) return osl_File_E_INVAL; + if (limit_off_t < (sal_Int64) nPos + nOffset) return osl_File_E_OVERFLOW; break; @@ -1429,6 +1485,7 @@ SAL_CALL osl_setFilePos (oslFileHandle Handle, sal_uInt32 uHow, sal_Int64 uOffse nPos = sal::static_int_cast< off_t >(pImpl->getSize()); if ((0 > nOffset) && (-1*nOffset > nPos)) return osl_File_E_INVAL; + if (limit_off_t < (sal_Int64) nPos + nOffset) return osl_File_E_OVERFLOW; break; @@ -1437,29 +1494,29 @@ SAL_CALL osl_setFilePos (oslFileHandle Handle, sal_uInt32 uHow, sal_Int64 uOffse return osl_File_E_INVAL; } - return pImpl->setPos (nPos + nOffset); + return pImpl->setPos(nPos + nOffset); } -oslFileError -SAL_CALL osl_getFileSize( oslFileHandle Handle, sal_uInt64* pSize ) +oslFileError SAL_CALL osl_getFileSize(oslFileHandle Handle, sal_uInt64* pSize) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (pImpl->m_fd == -1)) || (pSize == nullptr)) return osl_File_E_INVAL; - FileHandle_Impl::Guard lock (&(pImpl->m_mutex)); + FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); *pSize = pImpl->getSize(); + return osl_File_E_None; } -oslFileError -SAL_CALL osl_setFileSize( oslFileHandle Handle, sal_uInt64 uSize ) +oslFileError SAL_CALL osl_setFileSize(oslFileHandle Handle, sal_uInt64 uSize) { - FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle); + FileHandle_Impl* pImpl = static_cast< FileHandle_Impl* >(Handle); if ((pImpl == nullptr) || (pImpl->m_fd == -1)) return osl_File_E_INVAL; + if ((pImpl->m_state & FileHandle_Impl::STATE_WRITEABLE) == 0) return osl_File_E_BADF; @@ -1470,10 +1527,11 @@ SAL_CALL osl_setFileSize( oslFileHandle Handle, sal_uInt64 uSize ) oslFileError result = pImpl->syncFile(); if (result != osl_File_E_None) return result; + pImpl->m_bufptr = -1; pImpl->m_buflen = 0; - return pImpl->setSize (uSize); + return pImpl->setSize(uSize); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |