2013-07-30 05:06:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Kelvin W Sherlock
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2015-02-18 14:32:01 +00:00
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2013-07-30 05:06:19 +00:00
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
2015-02-18 14:32:01 +00:00
|
|
|
* list of conditions and the following disclaimer.
|
2013-07-30 05:06:19 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
2015-02-18 14:32:01 +00:00
|
|
|
* and/or other materials provided with the distribution.
|
2013-07-30 05:06:19 +00:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-02-25 22:47:58 +00:00
|
|
|
#include "os_internal.h"
|
2013-03-04 03:07:25 +00:00
|
|
|
#include "os.h"
|
2013-02-28 23:58:17 +00:00
|
|
|
#include "toolbox.h"
|
|
|
|
|
2013-05-19 01:24:49 +00:00
|
|
|
#include <macos/errors.h>
|
|
|
|
|
2013-02-26 01:16:55 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <cerrno>
|
2015-02-17 00:23:12 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2013-02-26 01:16:55 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2013-03-27 02:18:44 +00:00
|
|
|
#include <sys/xattr.h>
|
2014-12-26 21:31:58 +00:00
|
|
|
#include <sys/attr.h>
|
2013-05-19 01:24:49 +00:00
|
|
|
#include <sys/paths.h>
|
2013-03-27 02:18:44 +00:00
|
|
|
|
2016-07-31 23:56:01 +00:00
|
|
|
#include <native/native.h>
|
|
|
|
|
2013-05-16 04:11:05 +00:00
|
|
|
#include <machine/endian.h>
|
2013-02-28 23:58:17 +00:00
|
|
|
|
2013-05-19 01:24:49 +00:00
|
|
|
using ToolBox::Log;
|
2014-12-21 00:32:54 +00:00
|
|
|
using MacOS::macos_error_from_errno;
|
2013-05-19 01:24:49 +00:00
|
|
|
|
2013-02-25 22:47:58 +00:00
|
|
|
namespace OS { namespace Internal {
|
|
|
|
|
2013-02-28 23:58:17 +00:00
|
|
|
|
|
|
|
|
2013-03-04 03:07:25 +00:00
|
|
|
int32_t mac_seek(uint16_t refNum, uint16_t mode, int32_t offset)
|
|
|
|
{
|
|
|
|
off_t rv;
|
|
|
|
switch (mode & 0x03)
|
|
|
|
{
|
|
|
|
case OS::fsAtMark:
|
|
|
|
mode = SEEK_CUR;
|
|
|
|
offset = 0;
|
|
|
|
break;
|
|
|
|
case OS::fsFromStart:
|
|
|
|
mode = SEEK_SET;
|
|
|
|
break;
|
|
|
|
case OS::fsFromLEOF:
|
|
|
|
mode = SEEK_END;
|
|
|
|
break;
|
|
|
|
case OS::fsFromMark:
|
|
|
|
mode = SEEK_CUR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ::lseek(refNum, offset, mode);
|
2014-12-21 00:32:54 +00:00
|
|
|
if (rv < 0) return macos_error_from_errno();
|
2013-03-04 03:07:25 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2013-02-28 23:58:17 +00:00
|
|
|
|
2013-02-26 01:16:55 +00:00
|
|
|
//std::deque<FDEntry> FDTable;
|
|
|
|
|
|
|
|
std::deque<FDEntry> FDEntry::FDTable;
|
|
|
|
FDEntry& FDEntry::allocate(int fd)
|
|
|
|
{
|
|
|
|
std::string noname;
|
|
|
|
|
|
|
|
return allocate(fd, noname);
|
|
|
|
}
|
|
|
|
|
|
|
|
FDEntry& FDEntry::allocate(int fd, std::string &&filename)
|
|
|
|
{
|
|
|
|
if (fd < 0) throw std::out_of_range("Invalid FD");
|
|
|
|
|
|
|
|
if (FDTable.size() <= fd)
|
|
|
|
FDTable.resize(fd + 1);
|
|
|
|
|
|
|
|
auto &e = FDTable[fd];
|
|
|
|
e.refcount = 1;
|
|
|
|
e.text = false;
|
|
|
|
e.resource = false;
|
|
|
|
e.filename = std::move(filename);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
FDEntry& FDEntry::allocate(int fd, const std::string &filename)
|
|
|
|
{
|
|
|
|
if (fd < 0) throw std::out_of_range("Invalid FD");
|
|
|
|
|
|
|
|
if (FDTable.size() <= fd)
|
|
|
|
FDTable.resize(fd + 1);
|
|
|
|
|
|
|
|
auto &e = FDTable[fd];
|
|
|
|
e.refcount = 1;
|
|
|
|
e.text = false;
|
|
|
|
e.resource = false;
|
|
|
|
e.filename = filename;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-26 01:40:28 +00:00
|
|
|
int FDEntry::close(int fd, bool force)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= FDTable.size())
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
auto &e = FDTable[fd];
|
|
|
|
if (!e.refcount)
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--e.refcount == 0 || force)
|
|
|
|
{
|
|
|
|
e.refcount = 0;
|
|
|
|
return ::close(fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-26 01:16:55 +00:00
|
|
|
ssize_t FDEntry::read(int fd, void *buffer, size_t count)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= FDTable.size())
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const &e = FDTable[fd];
|
|
|
|
if (!e.refcount)
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hmm... keep a current seek position?
|
|
|
|
|
|
|
|
ssize_t size;
|
|
|
|
if (e.text)
|
|
|
|
{
|
|
|
|
std::unique_ptr<uint8_t[]> trbuffer(new uint8_t[count]);
|
|
|
|
|
|
|
|
size = ::read(fd, trbuffer.get(), count);
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
{
|
2015-02-18 14:32:01 +00:00
|
|
|
std::transform(trbuffer.get(), trbuffer.get() + size, (uint8_t *)buffer,
|
2013-02-26 01:16:55 +00:00
|
|
|
[](uint8_t c) { return c == '\n' ? '\r' : c; }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = ::read(fd, buffer, count);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t FDEntry::write(int fd, const void *buffer, size_t count)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= FDTable.size())
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const &e = FDTable[fd];
|
|
|
|
if (!e.refcount)
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hmm... keep a current seek position?
|
|
|
|
|
|
|
|
ssize_t size;
|
|
|
|
if (e.text)
|
|
|
|
{
|
|
|
|
std::unique_ptr<uint8_t[]> trbuffer(new uint8_t[count]);
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2015-02-18 14:32:01 +00:00
|
|
|
std::transform((const uint8_t *)buffer, (const uint8_t *)buffer + count, trbuffer.get(),
|
2013-02-26 01:16:55 +00:00
|
|
|
[](uint8_t c) { return c == '\r' ? '\n' : c; }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
size = ::write(fd, trbuffer.get(), count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = ::write(fd, buffer, count);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-02-25 22:47:58 +00:00
|
|
|
|
2013-05-19 01:24:49 +00:00
|
|
|
int FDEntry::open(const std::string &filename, int ioPermission, int fork)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (filename.empty()) return MacOS::bdNamErr;
|
|
|
|
|
|
|
|
int access = 0;
|
2015-02-18 14:32:01 +00:00
|
|
|
|
2014-12-23 16:30:07 +00:00
|
|
|
// ignore the deny bits for now.
|
|
|
|
switch(ioPermission & 0x0f)
|
2013-05-19 01:24:49 +00:00
|
|
|
{
|
|
|
|
case fsWrPerm:
|
|
|
|
case fsRdWrPerm:
|
|
|
|
case fsRdWrShPerm:
|
|
|
|
case fsCurPerm:
|
|
|
|
access = O_RDWR;
|
|
|
|
break;
|
|
|
|
case fsRdPerm:
|
|
|
|
access = O_RDONLY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return MacOS::paramErr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string xname = filename;
|
2016-07-31 23:56:01 +00:00
|
|
|
|
|
|
|
Log(" open(%s, %04x, %04x)\n", xname.c_str(), access, fork);
|
|
|
|
|
2013-05-19 01:24:49 +00:00
|
|
|
|
2016-07-31 23:56:01 +00:00
|
|
|
fd = native::open_fork(xname, fork, access);
|
2013-05-19 01:24:49 +00:00
|
|
|
|
|
|
|
if (fd < 0 && ioPermission == fsCurPerm && errno == EACCES)
|
2016-07-31 23:56:01 +00:00
|
|
|
fd = native::open_fork(xname, fork, O_RDONLY);
|
2013-05-19 01:24:49 +00:00
|
|
|
|
|
|
|
if (fd < 0)
|
2014-12-21 00:32:54 +00:00
|
|
|
return macos_error_from_errno();
|
2013-05-19 01:24:49 +00:00
|
|
|
|
|
|
|
// allocate the fd entry
|
|
|
|
|
|
|
|
auto &e = OS::Internal::FDEntry::allocate(fd, filename);
|
|
|
|
e.resource = fork;
|
2016-08-01 13:32:27 +00:00
|
|
|
e.text = fork ? false : native::is_text_file(filename);
|
2013-05-19 01:24:49 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 22:47:58 +00:00
|
|
|
} }
|