LCOV - code coverage report
Current view: top level - sal/osl/unx - file.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 420 572 73.4 %
Date: 2014-04-11 Functions: 43 47 91.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <config_features.h>
      21             : 
      22             : #include "boost/noncopyable.hpp"
      23             : #include "osl/file.hxx"
      24             : #include "osl/detail/file.h"
      25             : 
      26             : #include "osl/diagnose.h"
      27             : #include "rtl/alloc.h"
      28             : 
      29             : #include <rtl/string.hxx>
      30             : 
      31             : #include <sal/log.hxx>
      32             : 
      33             : #include "system.h"
      34             : #include "createfilehandlefromfd.hxx"
      35             : #include "file_error_transl.h"
      36             : #include "file_url.h"
      37             : #include "uunxapi.h"
      38             : 
      39             : #include <algorithm>
      40             : #include <limits>
      41             : 
      42             : #include <string.h>
      43             : #include <pthread.h>
      44             : #include <sys/mman.h>
      45             : 
      46             : #if defined(MACOSX)
      47             : 
      48             : #include <sys/param.h>
      49             : #include <sys/mount.h>
      50             : #define HAVE_O_EXLOCK
      51             : 
      52             : #include <CoreFoundation/CoreFoundation.h>
      53             : 
      54             : #endif /* MACOSX */
      55             : 
      56             : #ifdef ANDROID
      57             : #include <osl/detail/android-bootstrap.h>
      58             : #endif
      59             : 
      60             : /*******************************************************************
      61             :  *
      62             :  * FileHandle_Impl interface
      63             :  *
      64             :  ******************************************************************/
      65             : struct FileHandle_Impl
      66             : {
      67             :     pthread_mutex_t m_mutex;
      68             :     rtl_String *    m_strFilePath; /* holds native file path */
      69             :     int             m_fd;
      70             : 
      71             :     enum Kind
      72             :     {
      73             :         KIND_FD = 1,
      74             :         KIND_MEM = 2
      75             :     };
      76             :     int          m_kind;
      77             :     /** State
      78             :      */
      79             :     enum StateBits
      80             :     {
      81             :         STATE_SEEKABLE  = 1, /* default */
      82             :         STATE_READABLE  = 2, /* default */
      83             :         STATE_WRITEABLE = 4, /* open() sets, write() requires, else osl_File_E_BADF */
      84             :         STATE_MODIFIED  = 8  /* write() sets, flush() resets  */
      85             :     };
      86             :     int          m_state;
      87             : 
      88             :     sal_uInt64   m_size;    /* file size */
      89             :     off_t        m_offset;  /* physical offset from begin of file */
      90             :     off_t        m_fileptr; /* logical offset from begin of file */
      91             : 
      92             :     off_t        m_bufptr;  /* buffer offset from begin of file */
      93             :     size_t       m_buflen;  /* buffer filled [0, m_bufsiz - 1] */
      94             : 
      95             :     size_t       m_bufsiz;
      96             :     sal_uInt8 *  m_buffer;
      97             : 
      98             :     explicit FileHandle_Impl (int fd, Kind kind = KIND_FD, char const * path = "<anon>");
      99             :     ~FileHandle_Impl();
     100             : 
     101             :     static void* operator new (size_t n);
     102             :     static void  operator delete (void * p);
     103             : 
     104             :     static size_t getpagesize();
     105             : 
     106             :     sal_uInt64   getPos() const;
     107             :     oslFileError setPos (sal_uInt64 uPos);
     108             : 
     109             :     sal_uInt64   getSize() const;
     110             :     oslFileError setSize (sal_uInt64 uSize);
     111             : 
     112             :     oslFileError readAt (
     113             :         off_t        nOffset,
     114             :         void *       pBuffer,
     115             :         size_t       nBytesRequested,
     116             :         sal_uInt64 * pBytesRead);
     117             : 
     118             :     oslFileError writeAt (
     119             :         off_t        nOffset,
     120             :         void const * pBuffer,
     121             :         size_t       nBytesToWrite,
     122             :         sal_uInt64 * pBytesWritten);
     123             : 
     124             :     oslFileError readFileAt (
     125             :         off_t        nOffset,
     126             :         void *       pBuffer,
     127             :         size_t       nBytesRequested,
     128             :         sal_uInt64 * pBytesRead);
     129             : 
     130             :     oslFileError writeFileAt (
     131             :         off_t        nOffset,
     132             :         void const * pBuffer,
     133             :         size_t       nBytesToWrite,
     134             :         sal_uInt64 * pBytesWritten);
     135             : 
     136             :     oslFileError readLineAt (
     137             :         off_t           nOffset,
     138             :         sal_Sequence ** ppSequence,
     139             :         sal_uInt64 *    pBytesRead);
     140             : 
     141             :     oslFileError writeSequence_Impl (
     142             :         sal_Sequence ** ppSequence,
     143             :         size_t *        pnOffset,
     144             :         const void *    pBuffer,
     145             :         size_t          nBytes);
     146             : 
     147             :     oslFileError syncFile();
     148             : 
     149             :     /** Buffer cache / allocator.
     150             :      */
     151             :     class Allocator: private boost::noncopyable
     152             :     {
     153             :         rtl_cache_type * m_cache;
     154             :         size_t           m_bufsiz;
     155             : 
     156             :     public:
     157             :         static Allocator & get();
     158             : 
     159             :         void allocate (sal_uInt8 ** ppBuffer, size_t * pnSize);
     160             :         void deallocate (sal_uInt8 * pBuffer);
     161             : 
     162             :     protected:
     163             :         Allocator();
     164             :         ~Allocator();
     165             :     };
     166             : 
     167             :     /** Guard.
     168             :      */
     169             :     class Guard
     170             :     {
     171             :         pthread_mutex_t * m_mutex;
     172             : 
     173             :     public:
     174             :         explicit Guard(pthread_mutex_t * pMutex);
     175             :         ~Guard();
     176             :     };
     177             : };
     178             : 
     179             : FileHandle_Impl::Allocator &
     180      195399 : FileHandle_Impl::Allocator::get()
     181             : {
     182      195399 :     static Allocator g_aBufferAllocator;
     183      195399 :     return g_aBufferAllocator;
     184             : }
     185             : 
     186        1468 : FileHandle_Impl::Allocator::Allocator()
     187             :     : m_cache  (0),
     188        1468 :       m_bufsiz (0)
     189             : {
     190        1468 :     size_t const pagesize = FileHandle_Impl::getpagesize();
     191        1468 :     if (size_t(-1) != pagesize)
     192             :     {
     193             :         m_cache  = rtl_cache_create (
     194        1468 :             "osl_file_buffer_cache", pagesize, 0, 0, 0, 0, 0, 0, 0);
     195        1468 :         if (0 != m_cache)
     196        1468 :             m_bufsiz = pagesize;
     197             :     }
     198        1468 : }
     199        1468 : FileHandle_Impl::Allocator::~Allocator()
     200             : {
     201        1468 :     rtl_cache_destroy (m_cache), m_cache = 0;
     202        1468 : }
     203             : 
     204       98114 : void FileHandle_Impl::Allocator::allocate (sal_uInt8 ** ppBuffer, size_t * pnSize)
     205             : {
     206             :     OSL_PRECOND((0 != ppBuffer) && (0 != pnSize), "FileHandle_Impl::Allocator::allocate(): contract violation");
     207       98114 :     if ((0 != ppBuffer) && (0 != pnSize))
     208       98114 :         *ppBuffer = static_cast< sal_uInt8* >(rtl_cache_alloc(m_cache)), *pnSize = m_bufsiz;
     209       98114 : }
     210       97285 : void FileHandle_Impl::Allocator::deallocate (sal_uInt8 * pBuffer)
     211             : {
     212       97285 :     if (0 != pBuffer)
     213       97285 :         rtl_cache_free (m_cache, pBuffer);
     214       97285 : }
     215             : 
     216    12515344 : FileHandle_Impl::Guard::Guard(pthread_mutex_t * pMutex)
     217    12515344 :     : m_mutex (pMutex)
     218             : {
     219             :     OSL_PRECOND (m_mutex != 0, "FileHandle_Impl::Guard::Guard(): null pointer.");
     220    12515344 :     (void) pthread_mutex_lock (m_mutex); // ignoring EINVAL ...
     221    12515344 : }
     222    12515343 : FileHandle_Impl::Guard::~Guard()
     223             : {
     224             :     OSL_PRECOND (m_mutex != 0, "FileHandle_Impl::Guard::~Guard(): null pointer.");
     225    12515343 :     (void) pthread_mutex_unlock (m_mutex);
     226    12515340 : }
     227             : 
     228       98114 : FileHandle_Impl::FileHandle_Impl (int fd, enum Kind kind, char const * path)
     229             :     : m_strFilePath (0),
     230             :       m_fd      (fd),
     231             :       m_kind    (kind),
     232             :       m_state   (STATE_SEEKABLE | STATE_READABLE),
     233             :       m_size    (0),
     234             :       m_offset  (0),
     235             :       m_fileptr (0),
     236             :       m_bufptr  (-1),
     237             :       m_buflen  (0),
     238             :       m_bufsiz  (0),
     239       98114 :       m_buffer  (0)
     240             : {
     241       98114 :     (void) pthread_mutex_init(&m_mutex, 0);
     242       98114 :     rtl_string_newFromStr (&m_strFilePath, path);
     243       98114 :     if (m_kind == KIND_FD) {
     244       98114 :         Allocator::get().allocate (&m_buffer, &m_bufsiz);
     245       98114 :         if (0 != m_buffer)
     246       98114 :             memset (m_buffer, 0, m_bufsiz);
     247             :     }
     248       98114 : }
     249       97285 : FileHandle_Impl::~FileHandle_Impl()
     250             : {
     251       97285 :     if (m_kind == KIND_FD)
     252       97285 :         Allocator::get().deallocate (m_buffer), m_buffer = 0;
     253       97285 :     rtl_string_release (m_strFilePath), m_strFilePath = 0;
     254       97285 :     (void) pthread_mutex_destroy(&m_mutex); // ignoring EBUSY ...
     255       97285 : }
     256             : 
     257       98114 : void* FileHandle_Impl::operator new (size_t n)
     258             : {
     259       98114 :     return rtl_allocateMemory(n);
     260             : }
     261       97285 : void FileHandle_Impl::operator delete (void * p)
     262             : {
     263       97285 :     rtl_freeMemory(p);
     264       97285 : }
     265             : 
     266        7990 : size_t FileHandle_Impl::getpagesize()
     267             : {
     268             : #if defined(FREEBSD) || defined(NETBSD) || defined(MACOSX) || \
     269             :     defined(OPENBSD) || defined(DRAGONFLY)
     270             :     return sal::static_int_cast< size_t >(::getpagesize());
     271             : #else /* POSIX */
     272        7990 :     return sal::static_int_cast< size_t >(::sysconf(_SC_PAGESIZE));
     273             : #endif /* xBSD || POSIX */
     274             : }
     275             : 
     276     4902001 : sal_uInt64 FileHandle_Impl::getPos() const
     277             : {
     278     4902001 :     return sal::static_int_cast< sal_uInt64 >(m_fileptr);
     279             : }
     280             : 
     281      392638 : oslFileError FileHandle_Impl::setPos (sal_uInt64 uPos)
     282             : {
     283             :     SAL_INFO("sal.file", "FileHandle_Impl::setPos(" << m_fd << ", " << getPos() << ") => " << uPos);
     284      392638 :     m_fileptr = sal::static_int_cast< off_t >(uPos);
     285      392638 :     return osl_File_E_None;
     286             : }
     287             : 
     288      112194 : sal_uInt64 FileHandle_Impl::getSize() const
     289             : {
     290      112194 :     off_t const bufend = std::max((off_t)(0), m_bufptr) + m_buflen;
     291      112194 :     return std::max(m_size, sal::static_int_cast< sal_uInt64 >(bufend));
     292             : }
     293             : 
     294        1129 : oslFileError FileHandle_Impl::setSize (sal_uInt64 uSize)
     295             : {
     296        1129 :     off_t const nSize = sal::static_int_cast< off_t >(uSize);
     297        1129 :     if (-1 == ftruncate_with_name (m_fd, nSize, m_strFilePath))
     298             :     {
     299             :         /* Failure. Save original result. Try fallback algorithm */
     300           0 :         oslFileError result = oslTranslateFileError (OSL_FET_ERROR, errno);
     301             : 
     302             :         /* Check against current size. Fail upon 'shrink' */
     303           0 :         if (uSize <= getSize())
     304             :         {
     305             :             /* Failure upon 'shrink'. Return original result */
     306           0 :             return (result);
     307             :         }
     308             : 
     309             :         /* Save current position */
     310           0 :         off_t const nCurPos = (off_t)lseek (m_fd, (off_t)0, SEEK_CUR);
     311           0 :         if (nCurPos == (off_t)(-1))
     312           0 :             return (result);
     313             : 
     314             :         /* Try 'expand' via 'lseek()' and 'write()' */
     315           0 :         if (-1 == lseek (m_fd, (off_t)(nSize - 1), SEEK_SET))
     316           0 :             return (result);
     317             : 
     318           0 :         if (-1 == write (m_fd, (char*)"", (size_t)1))
     319             :         {
     320             :             /* Failure. Restore saved position */
     321           0 :             (void) lseek (m_fd, (off_t)(nCurPos), SEEK_SET);
     322           0 :             return (result);
     323             :         }
     324             : 
     325             :         /* Success. Restore saved position */
     326           0 :         if (-1 == lseek (m_fd, (off_t)nCurPos, SEEK_SET))
     327           0 :             return (result);
     328             :     }
     329             : 
     330             :     SAL_INFO("sal.file", "osl_setFileSize(" << m_fd << ", " << getSize() << ") => " << nSize);
     331        1129 :     m_size = sal::static_int_cast< sal_uInt64 >(nSize);
     332        1129 :     return osl_File_E_None;
     333             : }
     334             : 
     335      195366 : oslFileError FileHandle_Impl::readAt (
     336             :     off_t        nOffset,
     337             :     void *       pBuffer,
     338             :     size_t       nBytesRequested,
     339             :     sal_uInt64 * pBytesRead)
     340             : {
     341             :     OSL_PRECOND((m_state & STATE_SEEKABLE), "FileHandle_Impl::readAt(): not seekable");
     342      195366 :     if (!(m_state & STATE_SEEKABLE))
     343           0 :         return osl_File_E_SPIPE;
     344             : 
     345             :     OSL_PRECOND((m_state & STATE_READABLE), "FileHandle_Impl::readAt(): not readable");
     346      195366 :     if (!(m_state & STATE_READABLE))
     347           0 :         return osl_File_E_BADF;
     348             : 
     349      195366 :     if (m_kind == KIND_MEM)
     350             :     {
     351             :         ssize_t nBytes;
     352             : 
     353           0 :         m_offset = nOffset;
     354             : 
     355           0 :         if ((sal_uInt64) m_offset >= m_size)
     356           0 :             nBytes = 0;
     357             :         else
     358             :         {
     359           0 :             nBytes = std::min(nBytesRequested, (size_t) (m_size - m_offset));
     360           0 :             memmove(pBuffer, m_buffer + m_offset, nBytes);
     361           0 :             m_offset += nBytes;
     362             :         }
     363           0 :         *pBytesRead = nBytes;
     364           0 :         return osl_File_E_None;
     365             :     }
     366             : 
     367      195366 :     ssize_t nBytes = ::pread (m_fd, pBuffer, nBytesRequested, nOffset);
     368      195366 :     if ((-1 == nBytes) && (EOVERFLOW == errno))
     369             :     {
     370             :         /* Some 'pread()'s fail with EOVERFLOW when reading at (or past)
     371             :          * end-of-file, different from 'lseek() + read()' behaviour.
     372             :          * Returning '0 bytes read' and 'osl_File_E_None' instead.
     373             :          */
     374           0 :         nBytes = 0;
     375             :     }
     376      195366 :     if (-1 == nBytes)
     377           0 :         return oslTranslateFileError (OSL_FET_ERROR, errno);
     378             : 
     379             :     SAL_INFO("sal.file", "FileHandle_Impl::readAt(" << m_fd << ", " << nOffset << ", " << nBytes << ")");
     380      195366 :     *pBytesRead = nBytes;
     381      195366 :     return osl_File_E_None;
     382             : }
     383             : 
     384       60398 : oslFileError FileHandle_Impl::writeAt (
     385             :     off_t        nOffset,
     386             :     void const * pBuffer,
     387             :     size_t       nBytesToWrite,
     388             :     sal_uInt64 * pBytesWritten)
     389             : {
     390             :     OSL_PRECOND((m_state & STATE_SEEKABLE), "FileHandle_Impl::writeAt(): not seekable");
     391       60398 :     if (!(m_state & STATE_SEEKABLE))
     392           0 :         return osl_File_E_SPIPE;
     393             : 
     394             :     OSL_PRECOND((m_state & STATE_WRITEABLE), "FileHandle_Impl::writeAt(): not writeable");
     395       60398 :     if (!(m_state & STATE_WRITEABLE))
     396           0 :         return osl_File_E_BADF;
     397             : 
     398       60398 :     ssize_t nBytes = ::pwrite (m_fd, pBuffer, nBytesToWrite, nOffset);
     399       60398 :     if (-1 == nBytes)
     400           0 :         return oslTranslateFileError (OSL_FET_ERROR, errno);
     401             : 
     402             :     SAL_INFO("sal.file", "FileHandle_Impl::writeAt(" << m_fd << ", " << nOffset << ", " << nBytes << ")");
     403       60398 :     m_size = std::max (m_size, sal::static_int_cast< sal_uInt64 >(nOffset + nBytes));
     404             : 
     405       60398 :     *pBytesWritten = nBytes;
     406       60398 :     return osl_File_E_None;
     407             : }
     408             : 
     409      789630 : oslFileError FileHandle_Impl::readFileAt (
     410             :     off_t        nOffset,
     411             :     void *       pBuffer,
     412             :     size_t       nBytesRequested,
     413             :     sal_uInt64 * pBytesRead)
     414             : {
     415      789630 :     if (0 == (m_state & STATE_SEEKABLE))
     416             :     {
     417             :         // not seekable (pipe)
     418      219055 :         ssize_t nBytes = ::read (m_fd, pBuffer, nBytesRequested);
     419      219055 :         if (-1 == nBytes)
     420           0 :             return oslTranslateFileError (OSL_FET_ERROR, errno);
     421      219055 :         *pBytesRead = nBytes;
     422      219055 :         return osl_File_E_None;
     423             :     }
     424      570575 :     else if (m_kind == KIND_MEM || 0 == m_buffer)
     425             :     {
     426             :         // not buffered
     427           0 :         return readAt (nOffset, pBuffer, nBytesRequested, pBytesRead);
     428             :     }
     429             :     else
     430             :     {
     431      570575 :         sal_uInt8 * buffer = static_cast<sal_uInt8*>(pBuffer);
     432     1588651 :         for (*pBytesRead = 0; nBytesRequested > 0; )
     433             :         {
     434      606724 :             off_t  const bufptr = (nOffset / m_bufsiz) * m_bufsiz;
     435      606724 :             size_t const bufpos = (nOffset % m_bufsiz);
     436             : 
     437      606724 :             if (bufptr != m_bufptr)
     438             :             {
     439             :                 // flush current buffer
     440      144314 :                 oslFileError result = syncFile();
     441      144314 :                 if (result != osl_File_E_None)
     442       76132 :                     return (result);
     443      144314 :                 m_bufptr = -1, m_buflen = 0;
     444             : 
     445      144314 :                 if (nBytesRequested >= m_bufsiz)
     446             :                 {
     447             :                     // buffer too small, read through from file
     448       76132 :                     sal_uInt64 uDone = 0;
     449       76132 :                     result = readAt (nOffset, &(buffer[*pBytesRead]), nBytesRequested, &uDone);
     450       76132 :                     if (result != osl_File_E_None)
     451           0 :                         return (result);
     452             : 
     453       76132 :                     *pBytesRead += uDone;
     454       76132 :                     return osl_File_E_None;
     455             :                 }
     456             : 
     457             :                 // update buffer (pointer)
     458       68182 :                 sal_uInt64 uDone = 0;
     459       68182 :                 result = readAt (bufptr, m_buffer, m_bufsiz, &uDone);
     460       68182 :                 if (result != osl_File_E_None)
     461           0 :                     return (result);
     462       68182 :                 m_bufptr = bufptr, m_buflen = uDone;
     463             :             }
     464      530592 :             if (bufpos >= m_buflen)
     465             :             {
     466             :                 // end of file
     467       83091 :                 return osl_File_E_None;
     468             :             }
     469             : 
     470      447501 :             size_t const bytes = std::min (m_buflen - bufpos, nBytesRequested);
     471             :             SAL_INFO("sal.file", "FileHandle_Impl::readFileAt(" << m_fd << ", " << nOffset << ", " << bytes << ")");
     472             : 
     473      447501 :             memcpy (&(buffer[*pBytesRead]), &(m_buffer[bufpos]), bytes);
     474      447501 :             nBytesRequested -= bytes, *pBytesRead += bytes, nOffset += bytes;
     475             :         }
     476      411352 :         return osl_File_E_None;
     477             :     }
     478             : }
     479             : 
     480     6298895 : oslFileError FileHandle_Impl::writeFileAt (
     481             :     off_t        nOffset,
     482             :     void const * pBuffer,
     483             :     size_t       nBytesToWrite,
     484             :     sal_uInt64 * pBytesWritten)
     485             : {
     486     6298895 :     if (0 == (m_state & STATE_SEEKABLE))
     487             :     {
     488             :         // not seekable (pipe)
     489           3 :         ssize_t nBytes = ::write (m_fd, pBuffer, nBytesToWrite);
     490           3 :         if (-1 == nBytes)
     491           0 :             return oslTranslateFileError (OSL_FET_ERROR, errno);
     492           3 :         *pBytesWritten = nBytes;
     493           3 :         return osl_File_E_None;
     494             :     }
     495     6298892 :     else if (0 == m_buffer)
     496             :     {
     497             :         // not buffered
     498           0 :         return writeAt (nOffset, pBuffer, nBytesToWrite, pBytesWritten);
     499             :     }
     500             :     else
     501             :     {
     502     6298892 :         sal_uInt8 const * buffer = static_cast<sal_uInt8 const *>(pBuffer);
     503    18850046 :         for (*pBytesWritten = 0; nBytesToWrite > 0; )
     504             :         {
     505     6262955 :             off_t  const bufptr = (nOffset / m_bufsiz) * m_bufsiz;
     506     6262955 :             size_t const bufpos = (nOffset % m_bufsiz);
     507     6262955 :             if (bufptr != m_bufptr)
     508             :             {
     509             :                 // flush current buffer
     510       60383 :                 oslFileError result = syncFile();
     511       60383 :                 if (result != osl_File_E_None)
     512       10693 :                     return (result);
     513       60383 :                 m_bufptr = -1, m_buflen = 0;
     514             : 
     515       60383 :                 if (nBytesToWrite >= m_bufsiz)
     516             :                 {
     517             :                     // buffer to small, write through to file
     518       10693 :                     sal_uInt64 uDone = 0;
     519       10693 :                     result = writeAt (nOffset, &(buffer[*pBytesWritten]), nBytesToWrite, &uDone);
     520       10693 :                     if (result != osl_File_E_None)
     521           0 :                         return (result);
     522       10693 :                     if (uDone != nBytesToWrite)
     523           0 :                         return osl_File_E_IO;
     524             : 
     525       10693 :                     *pBytesWritten += uDone;
     526       10693 :                     return osl_File_E_None;
     527             :                 }
     528             : 
     529             :                 // update buffer (pointer)
     530       49690 :                 sal_uInt64 uDone = 0;
     531       49690 :                 result = readAt (bufptr, m_buffer, m_bufsiz, &uDone);
     532       49690 :                 if (result != osl_File_E_None)
     533           0 :                     return (result);
     534       49690 :                 m_bufptr = bufptr, m_buflen = uDone;
     535             :             }
     536             : 
     537     6252262 :             size_t const bytes = std::min (m_bufsiz - bufpos, nBytesToWrite);
     538             :             SAL_INFO("sal.file", "FileHandle_Impl::writeFileAt(" << m_fd << ", " << nOffset << ", " << bytes << ")");
     539             : 
     540     6252262 :             memcpy (&(m_buffer[bufpos]), &(buffer[*pBytesWritten]), bytes);
     541     6252262 :             nBytesToWrite -= bytes, *pBytesWritten += bytes, nOffset += bytes;
     542             : 
     543     6252262 :             m_buflen = std::max(m_buflen, bufpos + bytes);
     544     6252262 :             m_state |= STATE_MODIFIED;
     545             :         }
     546     6288199 :         return osl_File_E_None;
     547             :     }
     548             : }
     549             : 
     550       22002 : oslFileError FileHandle_Impl::readLineAt (
     551             :     off_t           nOffset,
     552             :     sal_Sequence ** ppSequence,
     553             :     sal_uInt64 *    pBytesRead)
     554             : {
     555       22002 :     oslFileError result = osl_File_E_None;
     556             : 
     557       22002 :     off_t bufptr = nOffset / m_bufsiz * m_bufsiz;
     558       22002 :     if (bufptr != m_bufptr)
     559             :     {
     560             :         /* flush current buffer */
     561        1317 :         result = syncFile();
     562        1317 :         if (result != osl_File_E_None)
     563           0 :             return (result);
     564             : 
     565             :         /* update buffer (pointer) */
     566        1317 :         sal_uInt64 uDone = 0;
     567        1317 :         result = readAt (bufptr, m_buffer, m_bufsiz, &uDone);
     568        1317 :         if (result != osl_File_E_None)
     569           0 :             return (result);
     570             : 
     571        1317 :         m_bufptr = bufptr, m_buflen = uDone;
     572             :     }
     573             : 
     574             :     static int const LINE_STATE_BEGIN = 0;
     575             :     static int const LINE_STATE_CR    = 1;
     576             :     static int const LINE_STATE_LF    = 2;
     577             : 
     578       22002 :     size_t bufpos = nOffset - m_bufptr, curpos = bufpos, dstpos = 0;
     579       22002 :     int    state  = (bufpos >= m_buflen) ? LINE_STATE_LF : LINE_STATE_BEGIN;
     580             : 
     581     1378035 :     for ( ; state != LINE_STATE_LF; )
     582             :     {
     583     1334079 :         if (curpos >= m_buflen)
     584             :         {
     585             :             /* buffer examined */
     586          93 :             if (0 < (curpos - bufpos))
     587             :             {
     588             :                 /* flush buffer to sequence */
     589             :                 result = writeSequence_Impl (
     590          93 :                     ppSequence, &dstpos, &(m_buffer[bufpos]), curpos - bufpos);
     591          93 :                 if (result != osl_File_E_None)
     592           0 :                     return (result);
     593          93 :                 *pBytesRead += curpos - bufpos, nOffset += curpos - bufpos;
     594             :             }
     595             : 
     596          93 :             bufptr = nOffset / m_bufsiz * m_bufsiz;
     597          93 :             if (bufptr != m_bufptr)
     598             :             {
     599             :                 /* update buffer (pointer) */
     600          45 :                 sal_uInt64 uDone = 0;
     601          45 :                 result = readAt (bufptr, m_buffer, m_bufsiz, &uDone);
     602          45 :                 if (result != osl_File_E_None)
     603           0 :                     return (result);
     604          45 :                 m_bufptr = bufptr, m_buflen = uDone;
     605             :             }
     606             : 
     607          93 :             bufpos = nOffset - m_bufptr, curpos = bufpos;
     608          93 :             if (bufpos >= m_buflen)
     609          48 :                 break;
     610             :         }
     611     1334031 :         switch (state)
     612             :         {
     613             :         case LINE_STATE_CR:
     614           0 :             state = LINE_STATE_LF;
     615           0 :             switch (m_buffer[curpos])
     616             :             {
     617             :             case 0x0A: /* CRLF */
     618             :                 /* eat current char */
     619           0 :                 curpos++;
     620           0 :                 break;
     621             :             default: /* single CR */
     622             :                 /* keep current char */
     623           0 :                 break;
     624             :             }
     625           0 :             break;
     626             :         default:
     627             :             /* determine next state */
     628     1334031 :             switch (m_buffer[curpos])
     629             :             {
     630             :             case 0x0A: /* single LF */
     631       20690 :                 state = LINE_STATE_LF;
     632       20690 :                 break;
     633             :             case 0x0D: /* CR */
     634           0 :                 state = LINE_STATE_CR;
     635           0 :                 break;
     636             :             default: /* advance to next char */
     637     1313341 :                 curpos++;
     638     1313341 :                 break;
     639             :             }
     640     1334031 :             if (state != LINE_STATE_BEGIN)
     641             :             {
     642             :                 /* skip the newline char */
     643       20690 :                 curpos++;
     644             : 
     645             :                 /* flush buffer to sequence */
     646             :                 result = writeSequence_Impl (
     647       20690 :                     ppSequence, &dstpos, &(m_buffer[bufpos]), curpos - bufpos - 1);
     648       20690 :                 if (result != osl_File_E_None)
     649           0 :                     return (result);
     650       20690 :                 *pBytesRead += curpos - bufpos, nOffset += curpos - bufpos;
     651             :             }
     652     1334031 :             break;
     653             :         }
     654             :     }
     655             : 
     656       22002 :     result = writeSequence_Impl (ppSequence, &dstpos, 0, 0);
     657       22002 :     if (result != osl_File_E_None)
     658           0 :         return (result);
     659       22002 :     if (0 < dstpos)
     660       20738 :         return osl_File_E_None;
     661        1264 :     if (bufpos >= m_buflen)
     662        1264 :         return osl_File_E_AGAIN;
     663           0 :     return osl_File_E_None;
     664             : }
     665             : 
     666       42785 : oslFileError FileHandle_Impl::writeSequence_Impl (
     667             :     sal_Sequence ** ppSequence,
     668             :     size_t *        pnOffset,
     669             :     const void *    pBuffer,
     670             :     size_t          nBytes)
     671             : {
     672       42785 :     sal_Int32 nElements = *pnOffset + nBytes;
     673       42785 :     if (!*ppSequence)
     674             :     {
     675             :         /* construct sequence */
     676           0 :         rtl_byte_sequence_constructNoDefault(ppSequence, nElements);
     677             :     }
     678       42785 :     else if (nElements != (*ppSequence)->nElements)
     679             :     {
     680             :         /* resize sequence */
     681       20366 :         rtl_byte_sequence_realloc(ppSequence, nElements);
     682             :     }
     683       42785 :     if (*ppSequence != 0)
     684             :     {
     685             :         /* fill sequence */
     686       42785 :         memcpy(&((*ppSequence)->elements[*pnOffset]), pBuffer, nBytes), *pnOffset += nBytes;
     687             :     }
     688       42785 :     return (*ppSequence != 0) ? osl_File_E_None : osl_File_E_NOMEM;
     689             : }
     690             : 
     691      305018 : oslFileError FileHandle_Impl::syncFile()
     692             : {
     693      305018 :     oslFileError result = osl_File_E_None;
     694      305018 :     if (m_state & STATE_MODIFIED)
     695             :     {
     696       49705 :         sal_uInt64 uDone = 0;
     697       49705 :         result = writeAt (m_bufptr, m_buffer, m_buflen, &uDone);
     698       49705 :         if (result != osl_File_E_None)
     699           0 :             return (result);
     700       49705 :         if (uDone != m_buflen)
     701           0 :             return osl_File_E_IO;
     702       49705 :         m_state &= ~STATE_MODIFIED;
     703             :     }
     704      305018 :     return (result);
     705             : }
     706             : 
     707         175 : oslFileHandle osl::detail::createFileHandleFromFD( int fd )
     708             : {
     709         175 :     if (-1 == fd)
     710           0 :         return 0; // EINVAL
     711             : 
     712             :     struct stat aFileStat;
     713         175 :     if (-1 == fstat (fd, &aFileStat))
     714           0 :         return 0; // EBADF
     715             : 
     716         175 :     FileHandle_Impl * pImpl = new FileHandle_Impl (fd);
     717         175 :     if (0 == pImpl)
     718           0 :         return 0; // ENOMEM
     719             : 
     720             :     // assume writeable
     721         175 :     pImpl->m_state |= FileHandle_Impl::STATE_WRITEABLE;
     722         175 :     if (!S_ISREG(aFileStat.st_mode))
     723             :     {
     724             :         /* not a regular file, mark not seekable */
     725         175 :         pImpl->m_state &= ~FileHandle_Impl::STATE_SEEKABLE;
     726             :     }
     727             :     else
     728             :     {
     729             :         /* regular file, init current size */
     730           0 :         pImpl->m_size = sal::static_int_cast< sal_uInt64 >(aFileStat.st_size);
     731             :     }
     732             : 
     733             :     SAL_INFO("sal.file", "osl::detail::createFileHandleFromFD(" << pImpl->m_fd << ", writeable) => " << pImpl->m_strFilePath);
     734         175 :     return (oslFileHandle)(pImpl);
     735             : }
     736             : 
     737       72648 : static int osl_file_adjustLockFlags (const char * path, int flags)
     738             : {
     739             : #ifdef MACOSX
     740             :     /*
     741             :      * The AFP implementation of MacOS X 10.4 treats O_EXLOCK in a way
     742             :      * that makes it impossible for OOo to create a backup copy of the
     743             :      * file it keeps opened. OTOH O_SHLOCK for AFP behaves as desired by
     744             :      * the OOo file handling, so we need to check the path of the file
     745             :      * for the filesystem name.
     746             :      */
     747             :     struct statfs s;
     748             :     if( 0 <= statfs( path, &s ) )
     749             :     {
     750             :         if( 0 == strncmp("afpfs", s.f_fstypename, 5) )
     751             :         {
     752             :             flags &= ~O_EXLOCK;
     753             :             flags |=  O_SHLOCK;
     754             :         }
     755             :         else
     756             :         {
     757             :             /* Needed flags to allow opening a webdav file */
     758             :             flags &= ~(O_EXLOCK | O_SHLOCK | O_NONBLOCK);
     759             :         }
     760             :     }
     761             : #endif /* MACOSX */
     762             : 
     763             :     (void) path;
     764       72648 :     return flags;
     765             : }
     766             : 
     767       97939 : static bool osl_file_queryLocking (sal_uInt32 uFlags)
     768             : {
     769             : #if !defined HAVE_O_EXLOCK
     770       97939 :     if (!(uFlags & osl_File_OpenFlag_NoLock)
     771       64222 :         && ((uFlags & osl_File_OpenFlag_Write)
     772       19899 :             || (uFlags & osl_File_OpenFlag_Create)))
     773             :     {
     774       44815 :         static bool enabled = getenv("SAL_ENABLE_FILE_LOCKING") != 0;
     775             :             // getenv is not thread safe, so minimize use of result
     776       44815 :         return enabled;
     777             :     }
     778             : #endif
     779             :     (void) uFlags;
     780       53124 :     return false;
     781             : }
     782             : 
     783             : #ifdef UNX
     784             : 
     785             : static oslFileError
     786           0 : osl_openMemoryAsFile( void *address, size_t size, oslFileHandle *pHandle, const char *path )
     787             : {
     788             :     oslFileError eRet;
     789           0 :     FileHandle_Impl * pImpl = new FileHandle_Impl (-1, FileHandle_Impl::KIND_MEM, path);
     790           0 :     if (!pImpl)
     791             :     {
     792           0 :         eRet = oslTranslateFileError (OSL_FET_ERROR, ENOMEM);
     793           0 :         return eRet;
     794             :     }
     795           0 :     pImpl->m_size = sal::static_int_cast< sal_uInt64 >(size);
     796             : 
     797           0 :     *pHandle = (oslFileHandle)(pImpl);
     798             : 
     799           0 :     pImpl->m_bufptr = 0;
     800           0 :     pImpl->m_buflen = size;
     801             : 
     802           0 :     pImpl->m_bufsiz = size;
     803           0 :     pImpl->m_buffer = (sal_uInt8*) address;
     804             : 
     805           0 :     return osl_File_E_None;
     806             : }
     807             : 
     808             : oslFileError
     809           0 : SAL_CALL osl_openMemoryAsFile( void *address, size_t size, oslFileHandle *pHandle )
     810             : {
     811           0 :     return osl_openMemoryAsFile( address, size, pHandle, "<anon>" );
     812             : }
     813             : 
     814             : #endif
     815             : 
     816             : #ifdef HAVE_O_EXLOCK
     817             : #define OPEN_WRITE_FLAGS ( O_RDWR | O_EXLOCK | O_NONBLOCK )
     818             : #define OPEN_CREATE_FLAGS ( O_CREAT | O_RDWR | O_EXLOCK | O_NONBLOCK )
     819             : #else
     820             : #define OPEN_WRITE_FLAGS ( O_RDWR )
     821             : #define OPEN_CREATE_FLAGS ( O_CREAT | O_RDWR )
     822             : #endif
     823             : 
     824             : oslFileError
     825      107832 : SAL_CALL osl_openFilePath( const char *cpFilePath, oslFileHandle* pHandle, sal_uInt32 uFlags )
     826             : {
     827             :     oslFileError eRet;
     828             : 
     829             : #ifdef ANDROID
     830             :     /* Opening a file from /assets read-only means
     831             :      * we should mmap it from the .apk file
     832             :      */
     833             :     if (strncmp (cpFilePath, "/assets/", sizeof ("/assets/") - 1) == 0)
     834             :     {
     835             :         if (uFlags & osl_File_OpenFlag_Write)
     836             :         {
     837             :             // It seems to work better to silently "open" it read-only
     838             :             // and let write attempts, if any, fail later. Otherwise
     839             :             // loading a document from /assets fails with that idiotic
     840             :             // "General Error" dialog...
     841             :         }
     842             :         void *address;
     843             :         size_t size;
     844             :         address = lo_apkentry(cpFilePath, &size);
     845             :         SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ") => " << address);
     846             :         if (address == NULL)
     847             :         {
     848             :             errno = ENOENT;
     849             :             return osl_File_E_NOENT;
     850             :         }
     851             :         return osl_openMemoryAsFile(address, size, pHandle, cpFilePath);
     852             :     }
     853             : #endif
     854             : 
     855             :     /* set mode and flags */
     856      107832 :     int mode  = S_IRUSR | S_IRGRP | S_IROTH;
     857      107832 :     int flags = O_RDONLY;
     858      107832 :     if (uFlags & osl_File_OpenFlag_Write)
     859             :     {
     860       46070 :         mode |= S_IWUSR | S_IWGRP | S_IWOTH;
     861       46070 :         flags = OPEN_WRITE_FLAGS;
     862             :     }
     863      107832 :     if (uFlags & osl_File_OpenFlag_Create)
     864             :     {
     865       42150 :         mode |= S_IWUSR | S_IWGRP | S_IWOTH;
     866       42150 :         flags = OPEN_CREATE_FLAGS;
     867             :     }
     868             : 
     869             :     /* Check for flags passed in from SvFileStream::Open() */
     870      107832 :     if (uFlags & osl_File_OpenFlag_Trunc)
     871         141 :         flags |= O_TRUNC;
     872      107832 :     if (!(uFlags & osl_File_OpenFlag_NoExcl))
     873       99949 :         flags |= O_EXCL;
     874             : 
     875      107832 :     if (uFlags & osl_File_OpenFlag_NoLock)
     876             :     {
     877             : #ifdef HAVE_O_EXLOCK
     878             :         flags &= ~(O_EXLOCK | O_SHLOCK | O_NONBLOCK);
     879             : #endif /* HAVE_O_EXLOCK */
     880             :     }
     881             :     else
     882             :     {
     883       72648 :         flags = osl_file_adjustLockFlags (cpFilePath, flags);
     884             :     }
     885             : 
     886             :     /* open the file */
     887      107832 :     int fd = open_c( cpFilePath, flags, mode );
     888             : 
     889             : #ifdef IOS
     890             :     /* Horrible hack: If opening for RDWR and getting EPERM, just try
     891             :      * again for RDONLY. Quicker this way than to figure out why
     892             :      * we get that oh so useful General Error when trying to open a
     893             :      * read-only document.
     894             :      */
     895             :     if (-1 == fd && (flags & O_RDWR) && EPERM == errno)
     896             :     {
     897             :         int rdonly_flags = (flags & ~O_ACCMODE) | O_RDONLY;
     898             :         fd = open_c( cpFilePath, rdonly_flags, mode );
     899             :     }
     900             : #endif
     901      107832 :     if (-1 == fd)
     902             :     {
     903        9893 :         int saved_errno = errno;
     904             :         SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << (flags & O_RDWR ? "writeable":"readonly") << ") failed: " << strerror(saved_errno));
     905        9893 :         return oslTranslateFileError (OSL_FET_ERROR, saved_errno);
     906             :     }
     907             : 
     908             : #if !HAVE_FEATURE_MACOSX_SANDBOX
     909             :     /* reset O_NONBLOCK flag */
     910       97939 :     if (flags & O_NONBLOCK)
     911             :     {
     912           0 :         int f = fcntl (fd, F_GETFL, 0);
     913           0 :         if (-1 == f)
     914             :         {
     915           0 :             int saved_errno = errno;
     916             :             SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << (flags & O_RDWR ? "writeable":"readonly") << "): fcntl(" << fd << ", F_GETFL) failed: " << strerror(saved_errno));
     917           0 :             eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno);
     918           0 :             (void) close(fd);
     919           0 :             return eRet;
     920             :         }
     921           0 :         if (-1 == fcntl (fd, F_SETFL, (f & ~O_NONBLOCK)))
     922             :         {
     923           0 :             int saved_errno = errno;
     924             :              SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << (flags & O_RDWR ? "writeable":"readonly") << "): fcntl(" << fd << ", F_SETFL) failed: " << strerror(saved_errno));
     925           0 :             eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno);
     926           0 :             (void) close(fd);
     927           0 :             return eRet;
     928             :         }
     929             :     }
     930             : #endif
     931             :     /* get file status (mode, size) */
     932             :     struct stat aFileStat;
     933       97939 :     if (-1 == fstat (fd, &aFileStat))
     934             :     {
     935           0 :         int saved_errno = errno;
     936             :         SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << (flags & O_RDWR ? "writeable":"readonly") << "): fstat(" << fd << ") failed: " << strerror(saved_errno));
     937           0 :         eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno);
     938           0 :         (void) close(fd);
     939           0 :         return eRet;
     940             :     }
     941       97939 :     if (!S_ISREG(aFileStat.st_mode))
     942             :     {
     943             :         /* we only open regular files here */
     944             :         SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << "): not a regular file");
     945           0 :         (void) close(fd);
     946           0 :         return osl_File_E_INVAL;
     947             :     }
     948             : 
     949       97939 :     if (osl_file_queryLocking (uFlags))
     950             :     {
     951             : #ifdef MACOSX
     952             :         if (-1 == flock (fd, LOCK_EX | LOCK_NB))
     953             :         {
     954             :             /* Mac OSX returns ENOTSUP for webdav drives. We should try read lock */
     955             :             if ((errno != ENOTSUP) || ((-1 == flock (fd, LOCK_SH | LOCK_NB)) && (errno != ENOTSUP)))
     956             :             {
     957             :                 eRet = oslTranslateFileError (OSL_FET_ERROR, errno);
     958             :                 (void) close(fd);
     959             :                 return eRet;
     960             :             }
     961             :         }
     962             : #else   /* F_SETLK */
     963             :         {
     964             :             struct flock aflock;
     965             : 
     966       44815 :             aflock.l_type = F_WRLCK;
     967       44815 :             aflock.l_whence = SEEK_SET;
     968       44815 :             aflock.l_start = 0;
     969       44815 :             aflock.l_len = 0;
     970             : 
     971       44815 :             if (-1 == fcntl (fd, F_SETLK, &aflock))
     972             :             {
     973           0 :                 int saved_errno = errno;
     974             :                 SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << (flags & O_RDWR ? "writeable":"readonly") << "): fcntl(" << fd << ", F_SETLK) failed: " << strerror(saved_errno));
     975           0 :                 eRet = oslTranslateFileError (OSL_FET_ERROR, saved_errno);
     976           0 :                 (void) close(fd);
     977           0 :                 return eRet;
     978             :             }
     979             :         }
     980             : #endif  /* F_SETLK */
     981             :     }
     982             : 
     983             :     /* allocate memory for impl structure */
     984       97939 :     FileHandle_Impl * pImpl = new FileHandle_Impl (fd, FileHandle_Impl::KIND_FD, cpFilePath);
     985       97939 :     if (!pImpl)
     986             :     {
     987           0 :         eRet = oslTranslateFileError (OSL_FET_ERROR, ENOMEM);
     988           0 :         (void) close(fd);
     989           0 :         return eRet;
     990             :     }
     991       97939 :     if (flags & O_RDWR)
     992       63526 :         pImpl->m_state |= FileHandle_Impl::STATE_WRITEABLE;
     993       97939 :     pImpl->m_size = sal::static_int_cast< sal_uInt64 >(aFileStat.st_size);
     994             : 
     995             :     SAL_INFO("sal.file", "osl_openFile(" << cpFilePath << ", " << (flags & O_RDWR ? "writeable":"readonly") << ") => " << pImpl->m_fd);
     996             : 
     997       97939 :     *pHandle = (oslFileHandle)(pImpl);
     998       97939 :     return osl_File_E_None;
     999             : }
    1000             : 
    1001             : oslFileError
    1002      104487 : SAL_CALL osl_openFile( rtl_uString* ustrFileURL, oslFileHandle* pHandle, sal_uInt32 uFlags )
    1003             : {
    1004             :     oslFileError eRet;
    1005             : 
    1006      104487 :     if ((ustrFileURL == 0) || (ustrFileURL->length == 0) || (pHandle == 0))
    1007           0 :         return osl_File_E_INVAL;
    1008             : 
    1009             :     /* convert file URL to system path */
    1010             :     char buffer[PATH_MAX];
    1011      104487 :     eRet = FileURLToPath (buffer, sizeof(buffer), ustrFileURL);
    1012      104487 :     if (eRet != osl_File_E_None)
    1013           0 :         return eRet;
    1014             : 
    1015             : #ifdef MACOSX
    1016             :     if (macxp_resolveAlias (buffer, sizeof(buffer)) != 0)
    1017             :         return oslTranslateFileError (OSL_FET_ERROR, errno);
    1018             : #endif /* MACOSX */
    1019             : 
    1020      104487 :     return osl_openFilePath (buffer, pHandle, uFlags);
    1021             : }
    1022             : 
    1023             : oslFileError
    1024       97285 : SAL_CALL osl_closeFile( oslFileHandle Handle )
    1025             : {
    1026       97285 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1027             : 
    1028       97285 :     if (pImpl == 0)
    1029           0 :         return osl_File_E_INVAL;
    1030             : 
    1031             :     SAL_INFO("sal.file", "osl_closeFile(" << rtl::OString(pImpl->m_strFilePath) << ":" << pImpl->m_fd << ")");
    1032             : 
    1033       97285 :     if (pImpl->m_kind == FileHandle_Impl::KIND_MEM)
    1034             :     {
    1035           0 :         delete pImpl;
    1036           0 :         return osl_File_E_None;
    1037             :     }
    1038             : 
    1039       97285 :     if (pImpl->m_fd < 0)
    1040           0 :         return osl_File_E_INVAL;
    1041             : 
    1042       97285 :     (void) pthread_mutex_lock (&(pImpl->m_mutex));
    1043             : 
    1044             :     /* close(2) implicitly (and unconditionally) unlocks */
    1045       97285 :     oslFileError result = pImpl->syncFile();
    1046       97285 :     if (result != osl_File_E_None)
    1047             :     {
    1048             :         /* close, ignoring double failure */
    1049           0 :         (void) close (pImpl->m_fd);
    1050             :     }
    1051       97285 :     else if (-1 == close (pImpl->m_fd))
    1052             :     {
    1053             :         /* translate error code */
    1054           0 :         result = oslTranslateFileError (OSL_FET_ERROR, errno);
    1055             :     }
    1056             : 
    1057       97285 :     (void) pthread_mutex_unlock (&(pImpl->m_mutex));
    1058       97285 :     delete pImpl;
    1059       97285 :     return (result);
    1060             : }
    1061             : 
    1062             : oslFileError
    1063         591 : SAL_CALL osl_syncFile(oslFileHandle Handle)
    1064             : {
    1065         591 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1066             : 
    1067         591 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)))
    1068           1 :         return osl_File_E_INVAL;
    1069             : 
    1070         590 :     if (pImpl->m_kind == FileHandle_Impl::KIND_MEM)
    1071           0 :         return osl_File_E_None;
    1072             : 
    1073         590 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1074             : 
    1075             :     SAL_INFO("sal.file", "osl_syncFile(" << pImpl->m_fd << ")");
    1076         590 :     oslFileError result = pImpl->syncFile();
    1077         590 :     if (result != osl_File_E_None)
    1078           0 :         return (result);
    1079         590 :     if (-1 == fsync (pImpl->m_fd))
    1080           0 :         return oslTranslateFileError (OSL_FET_ERROR, errno);
    1081             : 
    1082         590 :     return osl_File_E_None;
    1083             : }
    1084             : 
    1085             : oslFileError
    1086           0 : SAL_CALL osl_getFileOSHandle(
    1087             :     oslFileHandle Handle,
    1088             :     sal_IntPtr *piFileHandle )
    1089             : {
    1090           0 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1091             : 
    1092           0 :     if (0 == pImpl || pImpl->m_kind != FileHandle_Impl::KIND_FD || -1 == pImpl->m_fd)
    1093           0 :         return osl_File_E_INVAL;
    1094             : 
    1095           0 :     *piFileHandle = pImpl->m_fd;
    1096             : 
    1097           0 :     return osl_File_E_None;
    1098             : }
    1099             : 
    1100             : oslFileError
    1101       13277 : SAL_CALL osl_mapFile (
    1102             :     oslFileHandle Handle,
    1103             :     void**        ppAddr,
    1104             :     sal_uInt64    uLength,
    1105             :     sal_uInt64    uOffset,
    1106             :     sal_uInt32    uFlags
    1107             : )
    1108             : {
    1109       13277 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1110             : 
    1111       13277 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)) || (0 == ppAddr))
    1112           0 :         return osl_File_E_INVAL;
    1113       13277 :     *ppAddr = 0;
    1114             : 
    1115             :     static sal_uInt64 const g_limit_size_t = std::numeric_limits< size_t >::max();
    1116       13277 :     if (g_limit_size_t < uLength)
    1117           0 :         return osl_File_E_OVERFLOW;
    1118       13277 :     size_t const nLength = sal::static_int_cast< size_t >(uLength);
    1119             : 
    1120             :     static sal_uInt64 const g_limit_off_t = std::numeric_limits< off_t >::max();
    1121       13277 :     if (g_limit_off_t < uOffset)
    1122           0 :         return osl_File_E_OVERFLOW;
    1123             : 
    1124       13277 :     if (pImpl->m_kind == FileHandle_Impl::KIND_MEM)
    1125             :     {
    1126           0 :         *ppAddr = pImpl->m_buffer + uOffset;
    1127           0 :         return osl_File_E_None;
    1128             :     }
    1129             : 
    1130       13277 :     off_t const nOffset = sal::static_int_cast< off_t >(uOffset);
    1131             : 
    1132       13277 :     void* p = mmap(NULL, nLength, PROT_READ, MAP_SHARED, pImpl->m_fd, nOffset);
    1133       13277 :     if (MAP_FAILED == p)
    1134           4 :         return oslTranslateFileError(OSL_FET_ERROR, errno);
    1135       13273 :     *ppAddr = p;
    1136             : 
    1137       13273 :     if (uFlags & osl_File_MapFlag_RandomAccess)
    1138             :     {
    1139             :         // Determine memory pagesize.
    1140        6522 :         size_t const nPageSize = FileHandle_Impl::getpagesize();
    1141        6522 :         if (size_t(-1) != nPageSize)
    1142             :         {
    1143             :             /*
    1144             :              * Pagein, touching first byte of every memory page.
    1145             :              * Note: volatile disables optimizing the loop away.
    1146             :              */
    1147        6522 :             sal_uInt8 * pData (reinterpret_cast<sal_uInt8*>(*ppAddr));
    1148        6522 :             size_t      nSize (nLength);
    1149             : 
    1150        6522 :             volatile sal_uInt8 c = 0;
    1151       59779 :             while (nSize > nPageSize)
    1152             :             {
    1153       46735 :                 c ^= pData[0];
    1154       46735 :                 pData += nPageSize;
    1155       46735 :                 nSize -= nPageSize;
    1156             :             }
    1157        6522 :             if (nSize > 0)
    1158             :             {
    1159        6522 :                 c^= pData[0];
    1160             :             }
    1161             :         }
    1162             :     }
    1163       13273 :     if (uFlags & osl_File_MapFlag_WillNeed)
    1164             :     {
    1165             :         // On Linux, madvise(..., MADV_WILLNEED) appears to have the undesirable
    1166             :         // effect of not returning until the data has actually been paged in, so
    1167             :         // that its net effect would typically be to slow down the process
    1168             :         // (which could start processing at the beginning of the data while the
    1169             :         // OS simultaneously pages in the rest); on other platforms, it remains
    1170             :         // to be evaluated whether madvise or equivalent is available and
    1171             :         // actually useful:
    1172             : #if defined MACOSX
    1173             :         int e = posix_madvise(p, nLength, POSIX_MADV_WILLNEED);
    1174             :         if (e != 0)
    1175             :         {
    1176             :             SAL_INFO("sal.file", "posix_madvise(..., POSIX_MADV_WILLNEED) failed with " << e);
    1177             :         }
    1178             : #elif defined SOLARIS
    1179             :         if (madvise(static_cast< caddr_t >(p), nLength, MADV_WILLNEED) != 0)
    1180             :         {
    1181             :             SAL_INFO("sal.file", "madvise(..., MADV_WILLNEED) failed with " << strerror(errno));
    1182             :         }
    1183             : #endif
    1184             :     }
    1185       13273 :     return osl_File_E_None;
    1186             : }
    1187             : 
    1188             : static
    1189             : oslFileError
    1190       12964 : unmapFile (void* pAddr, sal_uInt64 uLength)
    1191             : {
    1192       12964 :     if (0 == pAddr)
    1193           0 :         return osl_File_E_INVAL;
    1194             : 
    1195             :     static sal_uInt64 const g_limit_size_t = std::numeric_limits< size_t >::max();
    1196       12964 :     if (g_limit_size_t < uLength)
    1197           0 :         return osl_File_E_OVERFLOW;
    1198       12964 :     size_t const nLength = sal::static_int_cast< size_t >(uLength);
    1199             : 
    1200       12964 :     if (-1 == munmap(static_cast<char*>(pAddr), nLength))
    1201           0 :         return oslTranslateFileError(OSL_FET_ERROR, errno);
    1202             : 
    1203       12964 :     return osl_File_E_None;
    1204             : }
    1205             : 
    1206             : #ifndef ANDROID
    1207             : 
    1208             : // Note that osl_unmapFile() just won't work on Android in general
    1209             : // where for (uncompressed) files inside the .apk, in the /assets
    1210             : // folder osl_mapFile just returns a pointer to the file inside the
    1211             : // already mmapped .apk archive.
    1212             : 
    1213             : oslFileError
    1214           0 : SAL_CALL osl_unmapFile (void* pAddr, sal_uInt64 uLength)
    1215             : {
    1216           0 :     return unmapFile (pAddr, uLength);
    1217             : }
    1218             : 
    1219             : #endif
    1220             : 
    1221             : oslFileError
    1222       12964 : SAL_CALL osl_unmapMappedFile (oslFileHandle Handle, void* pAddr, sal_uInt64 uLength)
    1223             : {
    1224       12964 :     FileHandle_Impl * pImpl = static_cast<FileHandle_Impl*>(Handle);
    1225             : 
    1226       12964 :     if (pImpl == 0)
    1227           0 :         return osl_File_E_INVAL;
    1228             : 
    1229       12964 :     if (pImpl->m_kind == FileHandle_Impl::KIND_FD)
    1230       12964 :         return unmapFile (pAddr, uLength);
    1231             : 
    1232             :     // For parts of already mmapped "parent" files, whose mapping we
    1233             :     // can't change, not much we can or should do...
    1234           0 :     return osl_File_E_None;
    1235             : }
    1236             : 
    1237             : oslFileError
    1238       22002 : SAL_CALL osl_readLine (
    1239             :     oslFileHandle   Handle,
    1240             :     sal_Sequence ** ppSequence)
    1241             : {
    1242       22002 :     FileHandle_Impl * pImpl = static_cast<FileHandle_Impl*>(Handle);
    1243             : 
    1244       22002 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)) || (0 == ppSequence))
    1245           0 :         return osl_File_E_INVAL;
    1246       22002 :     sal_uInt64 uBytesRead = 0;
    1247             : 
    1248             :     // read at current fileptr; fileptr += uBytesRead;
    1249       22002 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1250             :     oslFileError result = pImpl->readLineAt (
    1251       22002 :         pImpl->m_fileptr, ppSequence, &uBytesRead);
    1252       22002 :     if (result == osl_File_E_None)
    1253       20738 :         pImpl->m_fileptr += uBytesRead;
    1254       22002 :     return (result);
    1255             : }
    1256             : 
    1257             : oslFileError
    1258      789472 : SAL_CALL osl_readFile (
    1259             :     oslFileHandle Handle,
    1260             :     void *        pBuffer,
    1261             :     sal_uInt64    uBytesRequested,
    1262             :     sal_uInt64 *  pBytesRead)
    1263             : {
    1264      789472 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1265             : 
    1266      789472 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)) || (0 == pBuffer) || (0 == pBytesRead))
    1267           0 :         return osl_File_E_INVAL;
    1268             : 
    1269             :     static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max();
    1270      789472 :     if (g_limit_ssize_t < uBytesRequested)
    1271           0 :         return osl_File_E_OVERFLOW;
    1272      789472 :     size_t const nBytesRequested = sal::static_int_cast< size_t >(uBytesRequested);
    1273             : 
    1274             :     // read at current fileptr; fileptr += *pBytesRead;
    1275      789472 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1276             :     oslFileError result = pImpl->readFileAt (
    1277      789472 :         pImpl->m_fileptr, pBuffer, nBytesRequested, pBytesRead);
    1278      789472 :     if (result == osl_File_E_None)
    1279      789472 :         pImpl->m_fileptr += *pBytesRead;
    1280      789472 :     return (result);
    1281             : }
    1282             : 
    1283             : oslFileError
    1284     6296026 : SAL_CALL osl_writeFile (
    1285             :     oslFileHandle Handle,
    1286             :     const void *  pBuffer,
    1287             :     sal_uInt64    uBytesToWrite,
    1288             :     sal_uInt64 *  pBytesWritten)
    1289             : {
    1290     6296026 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1291             : 
    1292     6296026 :     if ((0 == pImpl) || (-1 == pImpl->m_fd) || (0 == pBuffer) || (0 == pBytesWritten))
    1293           1 :         return osl_File_E_INVAL;
    1294     6296025 :     if (0 == (pImpl->m_state & FileHandle_Impl::STATE_WRITEABLE))
    1295           0 :         return osl_File_E_BADF;
    1296             : 
    1297             :     static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max();
    1298     6296025 :     if (g_limit_ssize_t < uBytesToWrite)
    1299           0 :         return osl_File_E_OVERFLOW;
    1300     6296025 :     size_t const nBytesToWrite = sal::static_int_cast< size_t >(uBytesToWrite);
    1301             : 
    1302             :     // write at current fileptr; fileptr += *pBytesWritten;
    1303     6296025 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1304             :     oslFileError result = pImpl->writeFileAt (
    1305     6296025 :         pImpl->m_fileptr, pBuffer, nBytesToWrite, pBytesWritten);
    1306     6296025 :     if (result == osl_File_E_None)
    1307     6296025 :         pImpl->m_fileptr += *pBytesWritten;
    1308     6296025 :     return (result);
    1309             : }
    1310             : 
    1311             : oslFileError
    1312         158 : SAL_CALL osl_readFileAt (
    1313             :     oslFileHandle Handle,
    1314             :     sal_uInt64    uOffset,
    1315             :     void*         pBuffer,
    1316             :     sal_uInt64    uBytesRequested,
    1317             :     sal_uInt64*   pBytesRead)
    1318             : {
    1319         158 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1320             : 
    1321         158 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)) || (0 == pBuffer) || (0 == pBytesRead))
    1322           0 :         return osl_File_E_INVAL;
    1323         158 :     if (0 == (pImpl->m_state & FileHandle_Impl::STATE_SEEKABLE))
    1324           0 :         return osl_File_E_SPIPE;
    1325             : 
    1326             :     static sal_uInt64 const g_limit_off_t = std::numeric_limits< off_t >::max();
    1327         158 :     if (g_limit_off_t < uOffset)
    1328           0 :         return osl_File_E_OVERFLOW;
    1329         158 :     off_t const nOffset = sal::static_int_cast< off_t >(uOffset);
    1330             : 
    1331             :     static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max();
    1332         158 :     if (g_limit_ssize_t < uBytesRequested)
    1333           0 :         return osl_File_E_OVERFLOW;
    1334         158 :     size_t const nBytesRequested = sal::static_int_cast< size_t >(uBytesRequested);
    1335             : 
    1336             :     // read at specified fileptr
    1337         158 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1338         158 :     return pImpl->readFileAt (nOffset, pBuffer, nBytesRequested, pBytesRead);
    1339             : }
    1340             : 
    1341             : oslFileError
    1342        2870 : SAL_CALL osl_writeFileAt (
    1343             :     oslFileHandle Handle,
    1344             :     sal_uInt64    uOffset,
    1345             :     const void*   pBuffer,
    1346             :     sal_uInt64    uBytesToWrite,
    1347             :     sal_uInt64*   pBytesWritten)
    1348             : {
    1349        2870 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1350             : 
    1351        2870 :     if ((0 == pImpl) || (-1 == pImpl->m_fd) || (0 == pBuffer) || (0 == pBytesWritten))
    1352           0 :         return osl_File_E_INVAL;
    1353        2870 :     if (0 == (pImpl->m_state & FileHandle_Impl::STATE_SEEKABLE))
    1354           0 :         return osl_File_E_SPIPE;
    1355        2870 :     if (0 == (pImpl->m_state & FileHandle_Impl::STATE_WRITEABLE))
    1356           0 :         return osl_File_E_BADF;
    1357             : 
    1358             :     static sal_uInt64 const g_limit_off_t = std::numeric_limits< off_t >::max();
    1359        2870 :     if (g_limit_off_t < uOffset)
    1360           0 :         return osl_File_E_OVERFLOW;
    1361        2870 :     off_t const nOffset = sal::static_int_cast< off_t >(uOffset);
    1362             : 
    1363             :     static sal_uInt64 const g_limit_ssize_t = std::numeric_limits< ssize_t >::max();
    1364        2870 :     if (g_limit_ssize_t < uBytesToWrite)
    1365           0 :         return osl_File_E_OVERFLOW;
    1366        2870 :     size_t const nBytesToWrite = sal::static_int_cast< size_t >(uBytesToWrite);
    1367             : 
    1368             :     // write at specified fileptr
    1369        2870 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1370        2870 :     return pImpl->writeFileAt (nOffset, pBuffer, nBytesToWrite, pBytesWritten);
    1371             : }
    1372             : 
    1373             : oslFileError
    1374          10 : SAL_CALL osl_isEndOfFile( oslFileHandle Handle, sal_Bool *pIsEOF )
    1375             : {
    1376          10 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1377             : 
    1378          10 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)) || (0 == pIsEOF))
    1379           0 :         return osl_File_E_INVAL;
    1380             : 
    1381          10 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1382          10 :     *pIsEOF = (pImpl->getPos() == pImpl->getSize());
    1383          10 :     return osl_File_E_None;
    1384             : }
    1385             : 
    1386             : oslFileError
    1387     4901965 : SAL_CALL osl_getFilePos( oslFileHandle Handle, sal_uInt64* pPos )
    1388             : {
    1389     4901965 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1390             : 
    1391     4901965 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)) || (0 == pPos))
    1392           0 :         return osl_File_E_INVAL;
    1393             : 
    1394     4901965 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1395     4901965 :     *pPos = pImpl->getPos();
    1396     4901965 :     return osl_File_E_None;
    1397             : }
    1398             : 
    1399             : oslFileError
    1400      392638 : SAL_CALL osl_setFilePos (oslFileHandle Handle, sal_uInt32 uHow, sal_Int64 uOffset)
    1401             : {
    1402      392638 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1403             : 
    1404      392638 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)))
    1405           0 :         return osl_File_E_INVAL;
    1406             : 
    1407             :     static sal_Int64 const g_limit_off_t = std::numeric_limits< off_t >::max();
    1408             :     if (g_limit_off_t < uOffset)
    1409             :         return osl_File_E_OVERFLOW;
    1410      392638 :     off_t nPos = 0, nOffset = sal::static_int_cast< off_t >(uOffset);
    1411             : 
    1412      392638 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1413      392638 :     switch(uHow)
    1414             :     {
    1415             :         case osl_Pos_Absolut:
    1416      390042 :             if (0 > nOffset)
    1417           0 :                 return osl_File_E_INVAL;
    1418      390042 :             break;
    1419             : 
    1420             :         case osl_Pos_Current:
    1421          26 :             nPos = sal::static_int_cast< off_t >(pImpl->getPos());
    1422          26 :             if ((0 > nOffset) && (-1*nOffset > nPos))
    1423           0 :                 return osl_File_E_INVAL;
    1424             :             if (g_limit_off_t < (sal_Int64) nPos + nOffset)
    1425             :                 return osl_File_E_OVERFLOW;
    1426          26 :             break;
    1427             : 
    1428             :         case osl_Pos_End:
    1429        2570 :             nPos = sal::static_int_cast< off_t >(pImpl->getSize());
    1430        2570 :             if ((0 > nOffset) && (-1*nOffset > nPos))
    1431           0 :                 return osl_File_E_INVAL;
    1432             :             if (g_limit_off_t < (sal_Int64) nPos + nOffset)
    1433             :                 return osl_File_E_OVERFLOW;
    1434        2570 :             break;
    1435             : 
    1436             :         default:
    1437           0 :             return osl_File_E_INVAL;
    1438             :     }
    1439             : 
    1440      392638 :     return pImpl->setPos (nPos + nOffset);
    1441             : }
    1442             : 
    1443             : oslFileError
    1444      109614 : SAL_CALL osl_getFileSize( oslFileHandle Handle, sal_uInt64* pSize )
    1445             : {
    1446      109614 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1447             : 
    1448      109614 :     if ((0 == pImpl) || ((pImpl->m_kind == FileHandle_Impl::KIND_FD) && (-1 == pImpl->m_fd)) || (0 == pSize))
    1449           0 :         return osl_File_E_INVAL;
    1450             : 
    1451      109614 :     FileHandle_Impl::Guard lock (&(pImpl->m_mutex));
    1452      109614 :     *pSize = pImpl->getSize();
    1453      109614 :     return osl_File_E_None;
    1454             : }
    1455             : 
    1456             : oslFileError
    1457        1129 : SAL_CALL osl_setFileSize( oslFileHandle Handle, sal_uInt64 uSize )
    1458             : {
    1459        1129 :     FileHandle_Impl* pImpl = static_cast<FileHandle_Impl*>(Handle);
    1460             : 
    1461        1129 :     if ((0 == pImpl) || (-1 == pImpl->m_fd))
    1462           0 :         return osl_File_E_INVAL;
    1463        1129 :     if (0 == (pImpl->m_state & FileHandle_Impl::STATE_WRITEABLE))
    1464           0 :         return osl_File_E_BADF;
    1465             : 
    1466             :     static sal_uInt64 const g_limit_off_t = std::numeric_limits< off_t >::max();
    1467        1129 :     if (g_limit_off_t < uSize)
    1468           0 :         return osl_File_E_OVERFLOW;
    1469             : 
    1470        1129 :     oslFileError result = pImpl->syncFile();
    1471        1129 :     if (result != osl_File_E_None)
    1472           0 :         return (result);
    1473        1129 :     pImpl->m_bufptr = -1, pImpl->m_buflen = 0;
    1474             : 
    1475        1129 :     return pImpl->setSize (uSize);
    1476             : }
    1477             : 
    1478             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10