mpw/native/Linux.cpp

255 lines
6.6 KiB
C++
Raw Normal View History

2016-07-31 18:10:05 +00:00
/*
* Copyright (c) 2016, Kelvin W Sherlock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 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.
*
*/
2016-11-04 16:31:22 +00:00
#define XATTR_FINDERINFO_NAME "user.apple.FinderInfo"
#define XATTR_RESOURCEFORK_NAME "user.apple.ResourceFork"
#define XATTR_FILETYPE_NAME "user.prodos.FileType"
#define XATTR_AUXTYPE_NAME "user.prodos.AuxType"
2016-11-04 20:14:51 +00:00
#include "native_internal.h"
2016-11-04 16:31:22 +00:00
#include <unistd.h>
#include <fcntl.h>
#include <sys/xattr.h>
2016-11-04 20:21:50 +00:00
#include <sys/stat.h>
2016-11-04 16:31:22 +00:00
#include <vector>
2016-11-04 20:06:18 +00:00
#include <algorithm>
2016-11-04 16:31:22 +00:00
#include <macos/errors.h>
2016-11-04 20:14:51 +00:00
2016-11-04 16:31:22 +00:00
using MacOS::tool_return;
2016-11-04 20:06:18 +00:00
using MacOS::macos_error;
2016-11-04 16:31:22 +00:00
using MacOS::macos_error_from_errno;
namespace {
2016-11-04 20:06:18 +00:00
class xattr_file final : public native::file {
2016-11-04 16:31:22 +00:00
public:
2016-11-04 20:21:50 +00:00
xattr_file(const std::string &path, int fd, bool readonly): file(path), _fd(fd), _readonly(readonly)
{}
2016-11-04 16:31:22 +00:00
~xattr_file();
2016-11-04 20:14:51 +00:00
virtual tool_return<size_t> read(void *out_buffer, size_t count) override;
2016-11-04 20:21:50 +00:00
virtual tool_return<size_t> write(const void *in_buffer, size_t count) override;
2016-11-04 20:14:51 +00:00
virtual tool_return<size_t> get_mark() override;
virtual tool_return<size_t> set_mark(ssize_t new_mark) override;
virtual tool_return<size_t> get_eof() override;
virtual tool_return<size_t> set_eof(ssize_t new_eof) override;
2016-11-04 16:31:22 +00:00
private:
ssize_t read_rfork(std::vector<uint8_t> &buffer);
int _fd = -1;
2016-11-04 20:06:18 +00:00
size_t _displacement = 0;
bool _readonly = false;
2016-11-04 16:31:22 +00:00
};
xattr_file::~xattr_file() {
close(_fd);
}
ssize_t xattr_file::read_rfork(std::vector<uint8_t> &buffer) {
ssize_t eof = -1;
buffer.clear();
do {
eof = fgetxattr(_fd, XATTR_RESOURCEFORK_NAME, NULL, 0);
if (eof < 0) break;
buffer.resize(eof);
eof = fgetxattr(_fd, XATTR_RESOURCEFORK_NAME, buffer.data(), eof);
} while (eof < 0 && errno == ERANGE);
return eof;
}
tool_return<size_t> xattr_file::read(void *out_buffer, size_t count) {
std::vector<uint8_t> buffer;
ssize_t size;
size = read_rfork(buffer);
if (size < 0) size = 0;
if (_displacement >= buffer.size()) return 0;
count = std::min(count, buffer.size() - _displacement);
std::copy_n(buffer.begin() + _displacement, count, (uint8_t *)out_buffer);
_displacement += count;
return count;
}
tool_return<size_t> xattr_file::write(const void *in_buffer, size_t count) {
if (_readonly) return MacOS::wrPermErr;
2016-11-04 16:31:22 +00:00
std::vector<uint8_t> buffer;
ssize_t size;
size = read_rfork(buffer);
if (size < 0) size = 0;
if (_displacement >= buffer.size()) {
buffer.resize(_displacement, 0);
}
if (_displacement + count >= buffer.size()) {
buffer.resize(_displacement + count, 0);
}
std::copy_n((const uint8_t *)in_buffer, count, buffer.begin() + _displacement);
int ok = fsetxattr(_fd, XATTR_RESOURCEFORK_NAME, buffer.data(), buffer.size(), 0);
if (ok < 0) return macos_error_from_errno();
_displacement += count;
return count;
}
tool_return<size_t> xattr_file::get_mark() {
return _displacement;
}
2016-11-04 20:27:41 +00:00
tool_return<size_t> xattr_file::set_mark(ssize_t new_mark) {
2016-11-04 20:06:18 +00:00
if (new_mark < 0) return MacOS::paramErr;
2016-11-04 16:31:22 +00:00
_displacement = new_mark;
2016-11-04 20:14:51 +00:00
return new_mark;
2016-11-04 16:31:22 +00:00
}
tool_return<size_t> xattr_file::get_eof() {
ssize_t eof = fgetxattr(_fd, XATTR_RESOURCEFORK_NAME, NULL, 0);
if (eof < 0) eof = 0;
return eof;
}
2016-11-04 20:27:41 +00:00
tool_return<size_t> xattr_file::set_eof(ssize_t new_eof) {
2016-11-04 16:31:22 +00:00
if (_readonly) return MacOS::wrPermErr;
2016-11-04 20:06:18 +00:00
if (new_eof < 0) return MacOS::paramErr;
2016-11-04 16:31:22 +00:00
std::vector<uint8_t> buffer;
ssize_t eof = read_rfork(buffer);
if (eof < 0) eof = 0;
2016-11-04 20:06:18 +00:00
if (eof == new_eof) return MacOS::noErr;
2016-11-04 16:31:22 +00:00
buffer.resize(new_eof, 0);
2016-11-04 20:28:55 +00:00
int ok = fsetxattr(_fd, XATTR_RESOURCEFORK_NAME, buffer.data(), buffer.size(), 0);
2016-11-04 16:31:22 +00:00
if (ok < 0) return macos_error_from_errno();
2016-11-04 20:14:51 +00:00
return new_eof;
2016-11-04 16:31:22 +00:00
}
2016-11-04 20:27:41 +00:00
uint32_t rforksize(const std::string &path_name)
{
ssize_t ok = getxattr(path_name.c_str(), XATTR_RESOURCEFORK_NAME, NULL, 0);
if (ok < 0) ok = 0;
return ok;
}
2016-11-04 16:31:22 +00:00
}
namespace native {
tool_return<file_ptr> open_resource_fork(const std::string &path_name, int oflag) {
/* under HFS, every file has a resource fork.
* Therefore, create it if opening for O_RDWR or O_WRONLY
*/
int parent;
if (oflag & O_CREAT) {
int excl = oflag & O_EXCL;
parent = open(path_name.c_str(), O_RDONLY | O_CREAT | excl, 0666);
} else {
parent = open(path_name.c_str(), O_RDONLY);
}
if (parent < 0) return macos_error_from_errno();
int mode = oflag & O_ACCMODE;
2016-11-04 16:31:22 +00:00
2016-11-04 20:06:18 +00:00
auto tmp = new xattr_file(path_name, parent, mode == O_RDONLY);
tmp->resource = true;
return file_ptr(tmp);
2016-11-04 16:31:22 +00:00
}
macos_error get_file_info(const std::string &path_name, file_info &fi)
{
struct stat st;
2016-11-04 20:21:50 +00:00
if (::stat(path_name.c_str(), &st) < 0)
2016-11-04 16:31:22 +00:00
return macos_error_from_errno();
2016-11-04 20:21:50 +00:00
fi.create_date = unix_to_mac(st.st_ctime);
2016-11-04 16:31:22 +00:00
fi.modify_date = unix_to_mac(st.st_mtime);
fi.backup_date = 0;
fi.attributes = 0;
if (S_ISDIR(st.st_mode)) {
fi.type = file_info::directory;
fi.attributes = 1 << 4;
int links = st.st_nlink - 2;
if (links < 0) links = 0;
if (links > 65535) links = 65535;
fi.entry_count = links;
2016-11-04 20:14:51 +00:00
return MacOS::noErr;
2016-11-04 16:31:22 +00:00
}
// todo -- get actual block size instead of assuming 512. oh well!
fi.type = file_info::file;
fi.data_logical_size = st.st_size;
fi.data_physical_size = (st.st_size + 511) & ~511;
fi.resource_physical_size = 0;
fi.resource_logical_size = 0;
get_finder_info(path_name, fi.finder_info);
ssize_t rsize = rforksize(path_name);
if (rsize > 0) {
fi.resource_physical_size = rsize;
fi.resource_logical_size = (rsize + 511) & ~511;
}
2016-11-04 20:14:51 +00:00
return MacOS::noErr;
2016-11-04 16:31:22 +00:00
}
}