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-03-23 15:43:35 +01:00
|
|
|
#include "lib/sysdep/filesystem.h" // O_*, S_*
|
2009-04-18 18:14:48 +02:00
|
|
|
#include "lib/file/common/file_stats.h"
|
|
|
|
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
STATUS_DEFINE(ERR, FILE_ACCESS, L"Insufficient access rights to open file", EACCES);
|
|
|
|
STATUS_DEFINE(ERR, FILE_NOT_FOUND, L"No such file or directory", ENOENT);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status FileOpen(const OsPath& pathname, int opcode, int& fd)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2010-11-08 11:39:52 +01:00
|
|
|
int oflag = 0;
|
2011-04-29 21:10:34 +02:00
|
|
|
switch(opcode)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
case LIO_READ:
|
2009-08-24 23:04:01 +02:00
|
|
|
oflag = O_RDONLY;
|
|
|
|
break;
|
2011-04-29 21:10:34 +02:00
|
|
|
case LIO_WRITE:
|
2009-08-24 23:04:01 +02:00
|
|
|
oflag = O_WRONLY|O_CREAT|O_TRUNC;
|
|
|
|
break;
|
|
|
|
default:
|
2011-05-04 14:10:17 +02:00
|
|
|
DEBUG_WARN_ERR(ERR::LOGIC);
|
2011-04-29 21:10:34 +02:00
|
|
|
break;
|
2009-08-24 23:04:01 +02:00
|
|
|
}
|
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-05-03 14:38:42 +02:00
|
|
|
return StatusFromErrno(); // NOWARN
|
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
|
|
|
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
void FileClose(int& fd)
|
2009-08-24 23:04:01 +02:00
|
|
|
{
|
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
|
|
|
}
|