LCOV - code coverage report
Current view: top level - source/lib/file/io - io.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 10 11 90.9 %
Date: 2026-12-03 01:19:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : #include "precompiled.h"
      24             : 
      25             : #include "io.h"
      26             : 
      27             : #include "lib/config2.h"
      28             : 
      29             : #include <cerrno>
      30             : 
      31             : static const StatusDefinition ioStatusDefinitions[] = {
      32             :     { ERR::IO, L"Error during IO", EIO }
      33             : };
      34             : STATUS_ADD_DEFINITIONS(ioStatusDefinitions);
      35             : 
      36             : namespace io {
      37             : 
      38             : // this is just a thin wrapper on top of lowio and POSIX aio.
      39             : // note that the Windows aio implementation requires buffers, sizes and
      40             : // offsets to be sector-aligned.
      41             : 
      42       16205 : Status Issue(aiocb& cb, [[maybe_unused]] size_t queueDepth)
      43             : {
      44             : #if CONFIG2_FILE_ENABLE_AIO
      45             :     if(queueDepth > 1)
      46             :     {
      47             :         const int ret = (cb.aio_lio_opcode == LIO_WRITE)? aio_write(&cb): aio_read(&cb);
      48             :         if(ret != 0)
      49             :             WARN_RETURN(StatusFromErrno());
      50             :     }
      51             :     else
      52             : #endif
      53       16205 :     {
      54             : #if OS_WIN
      55             :         // sizeof(long) == 4 on Windows so we can't use lseek.
      56             :         ENSURE(_lseeki64(cb.aio_fildes, cb.aio_offset, SEEK_SET) == cb.aio_offset);
      57             : #else
      58       16205 :         ENSURE(lseek(cb.aio_fildes, cb.aio_offset, SEEK_SET) == cb.aio_offset);
      59             : #endif
      60             : 
      61       16205 :         void* buf = (void*)cb.aio_buf;  // cast from volatile void*
      62       16205 :         const ssize_t bytesTransferred = (cb.aio_lio_opcode == LIO_WRITE)? write(cb.aio_fildes, buf, cb.aio_nbytes) : read(cb.aio_fildes, buf, cb.aio_nbytes);
      63       16205 :         if(bytesTransferred < 0)
      64           0 :             WARN_RETURN(StatusFromErrno());
      65             : 
      66       16205 :         cb.aio_nbytes = (size_t)bytesTransferred;
      67             :     }
      68             : 
      69       16205 :     return INFO::OK;
      70             : }
      71             : 
      72             : 
      73       16205 : Status WaitUntilComplete([[maybe_unused]] aiocb& cb, [[maybe_unused]] size_t queueDepth)
      74             : {
      75             : #if CONFIG2_FILE_ENABLE_AIO
      76             :     if(queueDepth > 1)
      77             :     {
      78             :         aiocb* const cbs = &cb;
      79             :         timespec* const timeout = 0;    // infinite
      80             : SUSPEND_AGAIN:
      81             :         errno = 0;
      82             :         const int ret = aio_suspend(&cbs, 1, timeout);
      83             :         if(ret != 0)
      84             :         {
      85             :             if(errno == EINTR) // interrupted by signal
      86             :                 goto SUSPEND_AGAIN;
      87             :             WARN_RETURN(StatusFromErrno());
      88             :         }
      89             : 
      90             :         const int err = aio_error(&cb);
      91             :         ENSURE(err != EINPROGRESS); // else aio_return is undefined
      92             :         ssize_t bytesTransferred = aio_return(&cb);
      93             :         if(bytesTransferred == -1)  // transfer failed
      94             :         {
      95             :             errno = err;
      96             :             WARN_RETURN(StatusFromErrno());
      97             :         }
      98             :         cb.aio_nbytes = (size_t)bytesTransferred;
      99             :     }
     100             : #endif
     101             : 
     102       16205 :     return INFO::OK;
     103             : }
     104             : 
     105             : }   // namespace io

Generated by: LCOV version 1.16