This commit is contained in:
Kelvin Sherlock 2016-08-04 20:28:49 -04:00
parent a7382d7f86
commit 7280f8106e
6 changed files with 54 additions and 54 deletions

View File

@ -1,10 +1,10 @@
CXX = clang++ CXX = clang++
LINK.o = $(LINK.cc) LINK.o = $(LINK.cc)
CXXFLAGS += -std=c++11 -g CXXFLAGS += -std=c++11 -g -Wall
dot_clean: dot_clean.o mapped_file.o dot_clean: dot_clean.o mapped_file.o applefile.h defer.h
mapped_file.o : mapped_file.cpp mapped_file.h mapped_file.o : mapped_file.cpp mapped_file.h unique_resource.h
dot_clean.o : dot_clean.cpp mapped_file.h applefile.h dot_clean.o : dot_clean.cpp mapped_file.h applefile.h

View File

@ -155,7 +155,6 @@ typedef struct ASEntry ASEntry;
#define AS_MSDOSINFO 12 /* MS-DOS file info, attributes, etc */ #define AS_MSDOSINFO 12 /* MS-DOS file info, attributes, etc */
#define AS_AFPNAME 13 /* Short name on AFP server */ #define AS_AFPNAME 13 /* Short name on AFP server */
#define AS_AFPINFO 14 /* AFP file info, attrib., etc */ #define AS_AFPINFO 14 /* AFP file info, attrib., etc */
#define AS_AFPDIRID 15 /* AFP directory ID */ #define AS_AFPDIRID 15 /* AFP directory ID */
/* matrix of entry types and their usage: /* matrix of entry types and their usage:

26
defer.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef __defer_h__
#define __defer_h__
#include <utility>
#include <functional>
class defer {
public:
typedef std::function<void()> FX;
defer() = default;
defer(FX &&fx) : _fx(std::forward<FX>(fx)) {}
defer(const defer &) = delete;
defer(defer &&) = default;
defer & operator=(const defer &) = delete;
defer & operator=(defer &&) = default;
void cancel() { _fx = nullptr; }
~defer() { if (_fx) _fx(); }
private:
FX _fx;
};
#endif

View File

@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <system_error> #include <system_error>
#include <utility>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -23,6 +24,7 @@
#include "applefile.h" #include "applefile.h"
#include "mapped_file.h" #include "mapped_file.h"
#include "defer.h"
std::vector<std::string> unlink_list; std::vector<std::string> unlink_list;
@ -63,25 +65,6 @@ void throw_errno(const std::string &what) {
} }
class defer {
public:
typedef std::function<void()> FX;
defer() = default;
defer(FX fx) : _fx(fx) {}
defer(const defer &) = delete;
defer(defer &&) = default;
defer & operator=(const defer &) = delete;
defer & operator=(defer &&) = default;
void cancel() { _fx = nullptr; }
~defer() { if (_fx) _fx(); }
private:
FX _fx;
};
void one_file(const std::string &data, const std::string &rsrc) noexcept try { void one_file(const std::string &data, const std::string &rsrc) noexcept try {
struct stat rsrc_st; struct stat rsrc_st;
@ -119,9 +102,15 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try {
if (header->magicNum != APPLEDOUBLE_MAGIC || header->versionNum != 0x00020000) if (header->magicNum != APPLEDOUBLE_MAGIC)
throw_not_apple_double(); throw_not_apple_double();
// v 2 is a super set of v1. v1 had type 7 for os-specific info, since split into
// separate entries.
if (header->versionNum != 0x00010000 && header->versionNum != 0x00020000)
throw_not_apple_double();
if (header->numEntries * sizeof(ASEntry) + sizeof(ASHeader) > mf.size()) throw_eof(); if (header->numEntries * sizeof(ASEntry) + sizeof(ASHeader) > mf.size()) throw_eof();
ASEntry *begin = (ASEntry *)(mf.data() + sizeof(ASHeader)); ASEntry *begin = (ASEntry *)(mf.data() + sizeof(ASHeader));
@ -139,11 +128,9 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try {
}); });
std::for_each(begin, end, [&mf,fd](ASEntry &e){
for (auto iter = begin; iter != end; ++iter) { if (e.entryLength == 0) return;
const auto &e = *iter;
if (e.entryLength == 0) continue;
switch(e.entryID) { switch(e.entryID) {
#if 0 #if 0
@ -185,8 +172,7 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try {
break; break;
} }
} }
});
}
if (!_p) unlink_list.push_back(rsrc); if (!_p) unlink_list.push_back(rsrc);

View File

@ -6,22 +6,6 @@
#include "unique_resource.h" #include "unique_resource.h"
namespace { namespace {
class defer {
public:
typedef std::function<void()> FX;
defer() = default;
defer(FX fx) : _fx(fx) {}
defer(const defer &) = delete;
defer(defer &&) = default;
defer & operator=(const defer &) = delete;
defer & operator=(defer &&) = default;
void cancel() { _fx = nullptr; }
~defer() { if (_fx) _fx(); }
private:
FX _fx;
};
void throw_error(int error) { void throw_error(int error) {
throw std::system_error(error, std::system_category()); throw std::system_error(error, std::system_category());
@ -32,8 +16,6 @@ namespace {
throw std::system_error(error, std::system_category(), what); throw std::system_error(error, std::system_category(), what);
} }
} }
#ifdef _WIN32 #ifdef _WIN32

View File

@ -18,6 +18,7 @@ public:
unique_resource(T t, D d): _pair(t,d), _active(true) unique_resource(T t, D d): _pair(t,d), _active(true)
{} {}
~unique_resource() { ~unique_resource() {
reset(); reset();
} }
@ -40,14 +41,14 @@ public:
void reset(T t) { void reset(T t) {
reset(); reset();
_active = true;
_pair.first = t; _pair.first = t;
_active = true;
} }
void reset(T t, D d) { void reset(T t, D d) {
reset(); reset();
_active = true;
_pair = std::make_pair(t, d); _pair = std::make_pair(t, d);
_active = true;
} }
void reset() { void reset() {
@ -59,10 +60,6 @@ public:
T release() { T release() {
_active = false; _active = false;
return _pair.first;;
}
T get() {
return _pair.first; return _pair.first;
} }
@ -70,6 +67,16 @@ public:
return _active; return _active;
} }
T& get() {
return _pair.first;
}
const T& get() const {
return _pair.first;
}
D& get_deleter() { D& get_deleter() {
return _pair.second; return _pair.second;
} }