This commit is contained in:
Kelvin Sherlock 2016-08-03 14:20:51 -04:00
parent 7f24d8f588
commit 6019cac1fa
2 changed files with 100 additions and 3 deletions

View File

@ -10,6 +10,8 @@
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sysexits.h>
@ -102,7 +104,7 @@ void unfork(const char *in, const char *out) {
if (outname.empty()) throw std::runtime_error("No filename");
int fd = open(outname.c_str(), O_CREAT | O_TRUNC | O_WRONLY);
int fd = open(outname.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (fd < 1) throw_errno();
defer close_fd([fd](){ close(fd); });
@ -119,7 +121,7 @@ void unfork(const char *in, const char *out) {
break;
}
case AS_RESOURCE: {
int fd = fopenattr(fd, "com.apple.ResourceFork", O_CREAT | O_TRUNC | O_WRONLY);
int fd = openat(fd, "com.apple.ResourceFork", O_XATTR | O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (fd < 0) throw_errno("com.apple.ResourceFork");
defer close_fd([fd](){ close(fd); });
@ -133,7 +135,7 @@ void unfork(const char *in, const char *out) {
fputs("Warning: Invalid Finder Info size.\n", stderr);
break;
}
int fd = fopenattr(fd, "com.apple.FinderInfo", O_CREAT | O_TRUNC | O_WRONLY);
int fd = openat(fd, "com.apple.FinderInfo", O_XATTR | O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (fd < 0) throw_errno("com.apple.ResourceFork");
defer close_fd([fd](){ close(fd); });

95
unique_resource.h Normal file
View File

@ -0,0 +1,95 @@
#ifndef __unique_resource_h__
#define __unique_resource_h__
#include <utility>
template <class T, class D>
class unique_resource {
public:
typedef T element_type;
typedef D deleter_type;
unique_resource() = default;
unique_resource(const unique_resource &) = delete;
unique_resource(unique_resource &&rhs) {
swap(rhs);
}
unique_resource(T t, D d): _pair(t,d), _active(true)
{}
~unique_resource() {
reset();
}
unique_resource &operator=(const unique_resource &) = delete;
unique_resource &operator=(unique_resource &&rhs) {
if (this != std::addressof(rhs)) {
reset();
swap(rhs);
}
return *this;
}
void swap(unique_resource & rhs) {
if (this != std::addressof(rhs)) {
std::swap(_active, rhs._active);
std::swap(_pair, rhs._pair);
}
}
void reset(T t) {
reset();
_active = true;
_pair.first = t;
}
void reset(T t, D d) {
reset();
_active = true;
_pair = std::make_pair(t, d);
}
void reset() {
if (_active) {
(*_pair.second)(_pair.first);
_active = false;
}
}
T release() {
_active = false;
return _pair.first;;
}
T get() {
return _pair.first;
}
operator bool() const {
return _active;
}
D& get_deleter() {
return _pair.second;
}
const D& get_deleter() const {
return _pair.second;
}
private:
std::pair<T, D> _pair;
bool _active = false;
};
#define MAKE_UNIQUE_RESOURCE(T, D) \
unique_resource<decltype(T), decltype(D) *>(T, D)
template<class T, class D>
unique_resource<T, D> make_unique_resource(T t, D d) {
return unique_resource<T, D>(t, d);
}
#endif