linux compile fixup

This commit is contained in:
Kelvin Sherlock 2017-07-24 16:19:13 -04:00
parent f398ac94f0
commit 240cc298b6
2 changed files with 51 additions and 134 deletions

View File

@ -1,37 +1,28 @@
#include "finder_info.h"
#include "xattr.h"
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <string>
#if defined (_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define XATTR_FINDERINFO_NAME "AFP_AfpInfo"
#else
#include <unistd.h>
#include <fcntl.h>
#endif
#if defined(__APPLE__)
#include <sys/xattr.h>
#endif
#if defined(__linux__)
#include <sys/xattr.h>
#define XATTR_FINDERINFO_NAME "user.com.apple.FinderInfo"
#define XATTR_RESOURCEFORK_NAME "user.com.apple.ResourceFork"
#endif
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/extattr.h>
#endif
#if defined(_AIX)
#include <sys/ea.h>
#endif
#if defined (_WIN32)
#include <windows.h>
#define XATTR_FINDERINFO_NAME "AFP_AfpInfo"
#endif
#ifndef XATTR_FINDERINFO_NAME
#define XATTR_FINDERINFO_NAME "com.apple.FinderInfo"
@ -213,87 +204,6 @@ namespace {
return t;
}
/*
* extended attributes functions.
*/
#if defined(__APPLE__)
ssize_t size_xattr(int fd, const char *xattr) {
return fgetxattr(fd, xattr, NULL, 0, 0, 0);
}
ssize_t read_xattr(int fd, const char *xattr, void *buffer, size_t size) {
return fgetxattr(fd, xattr, buffer, size, 0, 0);
}
ssize_t write_xattr(int fd, const char *xattr, const void *buffer, size_t size) {
if (fsetxattr(fd, xattr, buffer, size, 0, 0) < 0) return -1;
return size;
}
int remove_xattr(int fd, const char *xattr) {
return fremovexattr(fd, xattr, 0);
}
#elif defined(__linux__)
ssize_t size_xattr(int fd, const char *xattr) {
return fgetxattr(fd, xattr, NULL, 0);
}
ssize_t read_xattr(int fd, const char *xattr, void *buffer, size_t size) {
return fgetxattr(fd, xattr, buffer, size);
}
ssize_t write_xattr(int fd, const char *xattr, const void *buffer, size_t size) {
if (fsetxattr(fd, xattr, buffer, size, 0) < 0) return -1;
return size;
}
int remove_xattr(int fd, const char *xattr) {
return fremovexattr(fd, xattr);
}
#elif defined(__FreeBSD__)
ssize_t size_xattr(int fd, const char *xattr) {
return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, xattr, NULL, 0);
}
ssize_t read_xattr(int fd, const char *xattr, void *buffer, size_t size) {
return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, xattr, buffer, size);
}
ssize_t write_xattr(int fd, const char *xattr, const void *buffer, size_t size) {
return extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, xattr, buffer, size);
}
int remove_xattr(int fd, const char *xattr) {
return extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, xattr);
}
#elif defined(_AIX)
ssize_t size_xattr(int fd, const char *xattr) {
/*
struct stat64x st;
if (fstatea(fd, xattr, &st) < 0) return -1;
return st.st_size;
*/
return fgetea(fd, xattr, NULL, 0);
}
ssize_t read_xattr(int fd, const char *xattr, void *buffer, size_t size) {
return fgetea(fd, xattr, buffer, size);
}
ssize_t write_xattr(int fd, const char *xattr, const void *buffer, size_t size) {
if (fsetea(fd, xattr, buffer, size, 0) < 0) return -1;
return size;
}
int remove_xattr(int fd, const char *xattr) {
return fremoveea(fd, xattr);
}
#endif
void set_or_throw_error(std::error_code *ec, int error, const std::string &what) {
if (ec) *ec = std::error_code(error, std::system_category());
@ -423,6 +333,11 @@ namespace {
#else
void set_or_throw_error(std::error_code *ec, const std::string &what) {
auto e = errno;
set_or_throw_error(ec, e, what);
}
void fi_close(int fd) {
close(fd);
}
@ -538,7 +453,7 @@ void finder_info::close() {
if (_fd != INVALID_HANDLE_VALUE) CloseHandle(_fd);
_fd = INVALID_HANDLE_VALUE;
#else
if (_fd >= 0) close(_fd);
if (_fd >= 0) ::close(_fd);
_fd = -1;
#endif

View File

@ -1,6 +1,8 @@
#include "resource_fork.h"
#include "xattr.h"
#include <cstring>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
@ -97,12 +99,12 @@ namespace {
}
#else
void set_or_throw_error(std::error_code *ec, const char *what) {
auto e = errno
auto e = errno;
set_or_throw_error(ec, e, what);
}
void set_or_throw_error(std::error_code *ec, const std::string &what) {
auto e = errno
auto e = errno;
set_or_throw_error(ec, e, what);
}
@ -237,9 +239,9 @@ namespace afp {
return true;
}
#else
resource_fork::close() {
close(_fd);
_fd = -;1
void resource_fork::close() {
::close(_fd);
_fd = -1;
}
#endif
@ -256,9 +258,9 @@ namespace afp {
case write_only umode = O_WRONLY | O_CREAT; break;
case read_write: umode = O_RDWR | O_CREAT; break;
}
_fd = attropen(path.c_str(), XATTR_RESOURCEFORK_NAME, umode, 0666);
_fd = ::attropen(path.c_str(), XATTR_RESOURCEFORK_NAME, umode, 0666);
if (_fd < 0) {
set_or_throw_error(ec, "attropen");
set_or_throw_error(&ec, "attropen");
return false;
}
return true;
@ -283,9 +285,9 @@ namespace afp {
case read_write: umode = O_RDWR | O_CREAT; break;
}
_fd = open(s.c_str(), umode, 0666);
_fd = ::open(s.c_str(), umode, 0666);
if (_fd < 0) {
set_or_throw_error(ec, "open");
set_or_throw_error(&ec, "open");
return false;
}
return true;
@ -296,9 +298,9 @@ namespace afp {
#ifdef FD_RESOURCE_FORK
size_t resource_fork::read(void *buffer, size_t n, std::error_code &) {
ec.clear();
auto rv = read(_fd, buffer, n);
auto rv = ::read(_fd, buffer, n);
if (rv < 0) {
set_or_throw_error(ec, "read");
set_or_throw_error(&ec, "read");
return 0;
}
return rv;
@ -306,9 +308,9 @@ namespace afp {
size_t resource_fork::write(const void *buffer, size_t n, std::error_code &) {
ec.clear();
auto rv = write(_fd, buffer, n);
auto rv = ::write(_fd, buffer, n);
if (rv < 0) {
set_or_throw_error(ec, "write");
set_or_throw_error(&ec, "write");
return 0;
}
return rv;
@ -316,8 +318,8 @@ namespace afp {
bool resource_fork::truncate(size_t pos, std::error_code &ec) {
ec.clear();
if (ftruncate(_fd, pos) < 0) {
set_or_throw_error(ec, "ftruncate");
if (::ftruncate(_fd, pos) < 0) {
set_or_throw_error(&ec, "ftruncate");
return false;
}
return true;
@ -325,8 +327,8 @@ namespace afp {
bool resource_fork::seek(size_t pos, std::error_code &ec) {
ec.clear();
if (lseek(_fd, pos, SEEK_SET) < 0) {
set_or_throw_error(ec, "lseek");
if (::lseek(_fd, pos, SEEK_SET) < 0) {
set_or_throw_error(&ec, "lseek");
return false;
}
return true;
@ -336,7 +338,7 @@ namespace afp {
ec.clear();
struct stat st;
if (fstat(_fd, &st) < 0) {
set_or_throw_error(ec, "fstat");
set_or_throw_error(&ec, "fstat");
return 0;
}
return st.st_size;
@ -363,7 +365,7 @@ namespace afp {
if (errno == ENOATTR) {
return rv;
}
set_or_throw_error(ec, "size_xattr");
set_or_throw_error(&ec, "size_xattr");
return rv;
}
break;
@ -373,7 +375,7 @@ namespace afp {
rv.resize(size);
for(;;) {
tsize = read_xattr(_fd, XATTR_RESOURCEFORK_NAME, tmp.data(), size);
tsize = read_xattr(_fd, XATTR_RESOURCEFORK_NAME, rv.data(), size);
if (tsize < 0) {
if (errno == EINTR) continue;
if (errno == ERANGE) break;
@ -381,7 +383,7 @@ namespace afp {
rv.clear();
return rv;
}
set_or_throw_error(ec, "read_xattr");
set_or_throw_error(&ec, "read_xattr");
rv.clear();
return rv;
}
@ -394,9 +396,9 @@ namespace afp {
bool resource_fork::open(const std::string &s, open_mode mode, std::error_code &ec) {
close();
ec.clear();
_fd = open(s.c_str(), O_RDONLY);
_fd = ::open(s.c_str(), O_RDONLY);
if (_fd < 0) {
set_or_throw_error(ec, "open");
set_or_throw_error(&ec, "open");
return false;
}
_mode = mode;
@ -409,7 +411,7 @@ namespace afp {
ec.clear();
auto rv = size_xattr(_fd, XATTR_RESOURCEFORK_NAME);
if (rv < 0) {
set_or_throw_error(ec, "size_xattr");
set_or_throw_error(&ec, "size_xattr");
return 0;
}
return rv;
@ -418,11 +420,11 @@ namespace afp {
size_t resource_fork::read(void *buffer, size_t n, std::error_code &ec) {
ec.clear();
if (_fd < 0) {
set_or_throw_error(ec, EBADF, "read");
set_or_throw_error(&ec, EBADF, "read");
return 0;
}
if (_mode == write_only) {
set_or_throw_error(ec, EPERM, "read");
set_or_throw_error(&ec, EPERM, "read");
return 0;
}
@ -443,27 +445,27 @@ namespace afp {
size_t resource_fork::write(const void *buffer, size_t n, std::error_code &ec) {
ec.clear();
if (_fd < 0) {
set_or_throw_error(ec, EBADF, "write");
set_or_throw_error(&ec, EBADF, "write");
return 0;
}
if (_mode == read_only) {
set_or_throw_error(ec, EPERM, "write");
set_or_throw_error(&ec, EPERM, "write");
return 0;
}
if (n == 0) return 0;
auto tmp = read_rfork(_fd, ec);
if (ec) return ec;
if (ec) return 0;
if (_offset > tmp.size()) {
tmp.resize(_offset);
}
tmp.append((const uint8_t *)buffer, (const uint8_t *buffer) + n);
tmp.insert(tmp.end(), (const uint8_t *)buffer, (const uint8_t *)buffer + n);
auto rv = write_xattr(_fd, XATTR_RESOURCEFORK_NAME, tmp.data(), tmp.size());
if (rv < 0) {
set_or_throw_error(ec, "write_xattr");
set_or_throw_error(&ec, "write_xattr");
return 0;
}
return n;
@ -473,11 +475,11 @@ namespace afp {
ec.clear();
if (_fd < 0) {
set_or_throw_error(ec, EBADF, "resource_fork::truncate");
set_or_throw_error(&ec, EBADF, "resource_fork::truncate");
return 0;
}
if (_mode == read_only) {
set_or_throw_error(ec, EPERM, "resource_fork::truncate");
set_or_throw_error(&ec, EPERM, "resource_fork::truncate");
return 0;
}
@ -485,7 +487,7 @@ namespace afp {
if (pos == 0) {
auto rv = remove_xattr(_fd, XATTR_RESOURCEFORK_NAME);
if (rv < 0) {
set_or_throw_error(ec, "remove_xattr");
set_or_throw_error(&ec, "remove_xattr");
return false;
}
return true;
@ -496,7 +498,7 @@ namespace afp {
tmp.resize(pos);
auto rv = write_xattr(_fd, XATTR_RESOURCEFORK_NAME, tmp.data(), pos);
if (rv < 0) {
set_or_throw_error(ec, "write_xattr");
set_or_throw_error(&ec, "write_xattr");
return false;
}
_offset = pos;
@ -505,7 +507,7 @@ namespace afp {
bool resource_fork::seek(size_t pos, std::error_code &ec) {
ec.clear();
if (_fd < 0) {
set_or_throw_error(ec, EBADF, "truncate");
set_or_throw_error(&ec, EBADF, "truncate");
return 0;
}