From 6019cac1fa8505c8a2db619981a5b953ecea8242 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Wed, 3 Aug 2016 14:20:51 -0400 Subject: [PATCH] updates! --- unfork.cpp | 8 ++-- unique_resource.h | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 unique_resource.h diff --git a/unfork.cpp b/unfork.cpp index b733331..d373407 100644 --- a/unfork.cpp +++ b/unfork.cpp @@ -10,6 +10,8 @@ #include #include +#include + #include #include @@ -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); }); diff --git a/unique_resource.h b/unique_resource.h new file mode 100644 index 0000000..9ec9de0 --- /dev/null +++ b/unique_resource.h @@ -0,0 +1,95 @@ +#ifndef __unique_resource_h__ +#define __unique_resource_h__ + +#include + +template +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 _pair; + bool _active = false; +}; + +#define MAKE_UNIQUE_RESOURCE(T, D) \ + unique_resource(T, D) + +template +unique_resource make_unique_resource(T t, D d) { + return unique_resource(t, d); +} + +#endif