2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* simple POSIX file wrapper.
|
2009-04-18 18:14:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/file/file.h"
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-04-03 21:07:35 +02:00
|
|
|
#include "lib/config2.h"
|
2011-03-23 15:43:35 +01:00
|
|
|
#include "lib/sysdep/filesystem.h" // O_*, S_*
|
2011-03-21 18:53:13 +01:00
|
|
|
#include "lib/posix/posix_aio.h"
|
2009-04-18 18:14:48 +02:00
|
|
|
#include "lib/file/common/file_stats.h"
|
|
|
|
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
ERROR_ASSOCIATE(ERR::FILE_ACCESS, L"Insufficient access rights to open file", EACCES);
|
|
|
|
ERROR_ASSOCIATE(ERR::IO, L"Error during IO", EIO);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
namespace FileImpl {
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
LibError Open(const OsPath& pathname, wchar_t accessType, int& fd)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2010-11-08 11:39:52 +01:00
|
|
|
int oflag = 0;
|
|
|
|
switch(accessType)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
oflag = O_RDONLY;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
oflag = O_WRONLY|O_CREAT|O_TRUNC;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
oflag = O_RDWR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug_assert(0);
|
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
#if OS_WIN
|
2009-08-24 23:04:01 +02:00
|
|
|
oflag |= O_BINARY_NP;
|
2009-04-18 18:14:48 +02:00
|
|
|
#endif
|
2010-11-08 16:02:05 +01:00
|
|
|
// prevent exploits by disallowing writes to our files by other users.
|
|
|
|
// note that the system-wide installed cache is read-only.
|
|
|
|
const mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH; // 0644
|
2011-03-23 14:36:20 +01:00
|
|
|
fd = wopen(pathname, oflag, mode);
|
2009-08-24 23:04:01 +02:00
|
|
|
if(fd < 0)
|
2011-01-28 14:40:07 +01:00
|
|
|
return LibError_from_errno(false);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
stats_open();
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
void Close(int& fd)
|
|
|
|
{
|
2011-01-28 14:40:07 +01:00
|
|
|
if(fd >= 0)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2010-01-05 20:44:30 +01:00
|
|
|
wclose(fd);
|
2011-01-28 14:40:07 +01:00
|
|
|
fd = -1;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
|
2010-11-08 11:39:52 +01:00
|
|
|
LibError IO(int fd, wchar_t accessType, off_t ofs, u8* buf, size_t size)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2010-11-08 11:39:52 +01:00
|
|
|
debug_assert(accessType == 'r' || accessType == 'w');
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
ScopedIoMonitor monitor;
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
lseek(fd, ofs, SEEK_SET);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
errno = 0;
|
2010-11-08 11:39:52 +01:00
|
|
|
const ssize_t ret = (accessType == 'w')? write(fd, buf, size) : read(fd, buf, size);
|
2009-08-24 23:04:01 +02:00
|
|
|
if(ret < 0)
|
|
|
|
return LibError_from_errno();
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
const size_t totalTransferred = (size_t)ret;
|
2011-04-03 21:07:35 +02:00
|
|
|
#if CONFIG2_FILE_ENABLE_AIO
|
|
|
|
// we won't be called from Issue, i.e. size is always the exact
|
|
|
|
// value without padding and can be checked.
|
2009-08-24 23:04:01 +02:00
|
|
|
if(totalTransferred != size)
|
|
|
|
WARN_RETURN(ERR::IO);
|
2011-04-03 21:07:35 +02:00
|
|
|
#endif
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2010-11-08 11:39:52 +01:00
|
|
|
monitor.NotifyOfSuccess(FI_LOWIO, accessType, totalTransferred);
|
2009-08-24 23:04:01 +02:00
|
|
|
return INFO::OK;
|
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
|
2010-11-08 11:39:52 +01:00
|
|
|
LibError Issue(aiocb& req, int fd, wchar_t accessType, off_t alignedOfs, u8* alignedBuf, size_t alignedSize)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
|
|
|
memset(&req, 0, sizeof(req));
|
2010-11-08 11:39:52 +01:00
|
|
|
req.aio_lio_opcode = (accessType == 'w')? LIO_WRITE : LIO_READ;
|
2009-08-24 23:04:01 +02:00
|
|
|
req.aio_buf = (volatile void*)alignedBuf;
|
|
|
|
req.aio_fildes = fd;
|
|
|
|
req.aio_offset = alignedOfs;
|
|
|
|
req.aio_nbytes = alignedSize;
|
2011-04-03 21:07:35 +02:00
|
|
|
#if CONFIG2_FILE_ENABLE_AIO
|
2009-08-24 23:04:01 +02:00
|
|
|
struct sigevent* sig = 0; // no notification signal
|
|
|
|
aiocb* const reqs = &req;
|
|
|
|
if(lio_listio(LIO_NOWAIT, &reqs, 1, sig) != 0)
|
|
|
|
return LibError_from_errno();
|
|
|
|
return INFO::OK;
|
2011-04-03 21:07:35 +02:00
|
|
|
#else
|
|
|
|
// quick and dirty workaround (see CONFIG2_FILE_ENABLE_AIO)
|
|
|
|
return IO(fd, accessType, alignedOfs, alignedBuf, alignedSize);
|
|
|
|
#endif
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
LibError WaitUntilComplete(aiocb& req, u8*& alignedBuf, size_t& alignedSize)
|
|
|
|
{
|
2011-04-03 21:07:35 +02:00
|
|
|
#if CONFIG2_FILE_ENABLE_AIO
|
2011-04-03 22:07:55 +02:00
|
|
|
const int err = aio_error(&req);
|
|
|
|
if(err == EINPROGRESS)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-04-03 22:07:55 +02:00
|
|
|
SUSPEND_AGAIN:
|
2009-08-24 23:04:01 +02:00
|
|
|
aiocb* const reqs = &req;
|
2011-04-03 22:07:55 +02:00
|
|
|
errno = 0;
|
|
|
|
const int ret = aio_suspend(&reqs, 1, (timespec*)0); // no timeout
|
|
|
|
if(ret != 0)
|
|
|
|
{
|
|
|
|
if(errno == EINTR) // interrupted by signal
|
|
|
|
goto SUSPEND_AGAIN;
|
|
|
|
return LibError_from_errno();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(err != 0)
|
|
|
|
{
|
|
|
|
errno = err;
|
|
|
|
return LibError_from_errno();
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
const ssize_t bytesTransferred = aio_return(&req);
|
|
|
|
if(bytesTransferred == -1) // transfer failed
|
|
|
|
WARN_RETURN(ERR::IO);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2009-08-24 23:04:01 +02:00
|
|
|
alignedSize = bytesTransferred;
|
2011-04-03 21:07:35 +02:00
|
|
|
#else
|
|
|
|
alignedSize = req.aio_nbytes;
|
|
|
|
#endif
|
|
|
|
alignedBuf = (u8*)req.aio_buf; // cast from volatile void*
|
2009-08-24 23:04:01 +02:00
|
|
|
return INFO::OK;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
2009-08-24 23:04:01 +02:00
|
|
|
|
|
|
|
} // namespace FileImpl
|