From dc26edee3f2ecac6faae466390dd19064862f117 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 20 Nov 2016 16:20:23 -0500 Subject: [PATCH] win32 updates. --- .gitignore | 2 ++ dot_clean.cpp | 33 ++++++++++++++++++++++++--------- mapped_file.cpp | 27 +++++++++++++-------------- win.h | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 .gitignore create mode 100644 win.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25a7384 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.exe diff --git a/dot_clean.cpp b/dot_clean.cpp index dbb3b9f..d96c736 100644 --- a/dot_clean.cpp +++ b/dot_clean.cpp @@ -12,15 +12,24 @@ #include #include #include +#include +#include + +#ifdef _WIN32 +#include "win.h" +#else + #include +#include +#include + +#endif #include #include #include -#include -#include #include "applefile.h" #include "mapped_file.h" @@ -42,7 +51,7 @@ struct AFP_Info { uint32_t prodos_aux_type; uint8_t reserved[6]; }; -#pragma pack pop +#pragma pack(pop) void init_afp_info(AFP_Info &info) { static_assert(sizeof(AFP_Info) == 60, "Incorrect AFP_Info size"); @@ -53,6 +62,11 @@ void init_afp_info(AFP_Info &info) { #endif +#ifndef O_BINARY +#define O_BINARY 0 +#endif + + std::vector unlink_list; @@ -97,19 +111,19 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { if (_v) fprintf(stdout, "Merging %s & %s\n", rsrc.c_str(), data.c_str()); - int fd = open(data.c_str(), O_RDONLY); + int fd = open(data.c_str(), O_RDONLY | O_BINARY); if (fd < 0) { if (errno == ENOENT) { if (_n) unlink_list.push_back(rsrc); return; } - throw_errno(); + throw_errno("open"); } defer close_fd([fd]{close(fd); }); - if (stat(rsrc.c_str(), &rsrc_st) < 0) throw_errno(); + if (stat(rsrc.c_str(), &rsrc_st) < 0) throw_errno("stat"); if (rsrc_st.st_size == 0) { // mmapping a zero-length file throws EINVAL. if (!_p) unlink_list.push_back(rsrc); @@ -154,7 +168,7 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { }); - std::for_each(begin, end, [&mf,fd](ASEntry &e){ + std::for_each(begin, end, [&](ASEntry &e){ if (e.entryLength == 0) return; switch(e.entryID) { @@ -181,7 +195,7 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { #ifdef _WIN32 std::string tmp = data + ":AFP_Resource"; - int rfd = open(tmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666); + int rfd = open(tmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); if (rfd < 0) throw_errno("com.apple.ResourceFork"); defer close_fd([rfd](){ close(rfd); }); int ok = write(rfd, mf.data() + e.entryOffset, e.entryLength); @@ -217,7 +231,7 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { memcpy(info.finder_info, mf.data() + e.entryOffset, 32); std::string tmp = data + ":AFP_AfpInfo"; - int rfd = openat(tmp.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666); + int rfd = open(tmp.c_str(), O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666); if (rfd < 0) throw_errno("AFP_AfpInfo"); defer close_fd([rfd](){ close(rfd); }); @@ -232,6 +246,7 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { break; + } case AS_PRODOSINFO: { if (e.entryLength != 8) { diff --git a/mapped_file.cpp b/mapped_file.cpp index 61683b4..90c87da 100644 --- a/mapped_file.cpp +++ b/mapped_file.cpp @@ -65,13 +65,12 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si nullptr ); if (fh == INVALID_HANDLE_VALUE) { - throw_error(); + throw_error("CreateFile"); } auto fh_close = make_unique_resource(fh, CloseHandle); - - if (length == -1){ + if (length == -1) { LARGE_INTEGER file_size; GetFileSizeEx(fh, &file_size); length = file_size.QuadPart; @@ -90,13 +89,13 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si break; case priv: protect = PAGE_WRITECOPY; - access = FILE_MAP_WRITE; + access = FILE_MAP_COPY; break; } mh = CreateFileMapping(fh, nullptr, protect, 0, 0, 0); if (mh == INVALID_HANDLE_VALUE) { - throw_error(); + throw_error("CreateFileMapping"); } auto mh_close = make_unique_resource(mh, CloseHandle); @@ -109,7 +108,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si length, nullptr); if (!_data) { - throw_error(); + throw_error("MapViewOfFileEx"); } @@ -146,7 +145,7 @@ void mapped_file_base::create(const path_type& p, size_t length) { nullptr ); if (fh == INVALID_HANDLE_VALUE) { - throw_error(); + throw_error("CreateFile"); } auto fh_close = make_unique_resource(fh, CloseHandle); @@ -154,11 +153,11 @@ void mapped_file_base::create(const path_type& p, size_t length) { file_size.QuadPart = length; if (!SetFilePointerEx(fh, file_size, nullptr, FILE_BEGIN)); - if (!SetEndOfFile(fh)) throw_error(); + if (!SetEndOfFile(fh)) throw_error("SetEndOfFile"); mh = CreateFileMapping(fh, nullptr, protect, 0, 0, 0); if (mh == INVALID_HANDLE_VALUE) { - throw_error(); + throw_error("CreateFileMapping"); } auto mh_close = make_unique_resource(mh, CloseHandle); @@ -171,7 +170,7 @@ void mapped_file_base::create(const path_type& p, size_t length) { nullptr); if (!_data) { - throw_error(); + throw_error("MapViewOfFileEx"); } _file_handle = fh_close.release(); @@ -256,7 +255,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si if (_data == MAP_FAILED) { _data = nullptr; - throw_error(errno); + throw_error(errno, "mmap"); } _fd = close_fd.release(); @@ -277,7 +276,7 @@ void mapped_file_base::create(const path_type& p, size_t length) { fd = ::open(p.c_str(), O_RDWR | O_CREAT | O_TRUNC); if (fd < 0) { - throw_error(errno); + throw_error(errno, "open"); } //defer([fd](){::close(fd); }); @@ -286,7 +285,7 @@ void mapped_file_base::create(const path_type& p, size_t length) { if (::ftruncate(fd, length) < 0) { - throw_error(errno); + throw_error(errno, "ftruncate"); } @@ -297,7 +296,7 @@ void mapped_file_base::create(const path_type& p, size_t length) { if (_data == MAP_FAILED) { _data = nullptr; - throw_error(errno); + throw_error(errno, "mmap"); } _fd = close_fd.release(); diff --git a/win.h b/win.h new file mode 100644 index 0000000..91dba3f --- /dev/null +++ b/win.h @@ -0,0 +1,38 @@ +#ifdef __GNUC__ +#define ntohl __builtin_bswap32 +#define ntohs __builtin_bswap16 + +#else +#define ntohl _byteswap_ulong +#define ntohs _byteswap_ushort +#endif + +#define EX_OK 0 /* successful termination */ + +#define EX__BASE 64 /* base value for error messages */ + +#define EX_USAGE 64 /* command line usage error */ +#define EX_DATAERR 65 /* data format error */ +#define EX_NOINPUT 66 /* cannot open input */ +#define EX_NOUSER 67 /* addressee unknown */ +#define EX_NOHOST 68 /* host name unknown */ +#define EX_UNAVAILABLE 69 /* service unavailable */ +#define EX_SOFTWARE 70 /* internal software error */ +#define EX_OSERR 71 /* system error (e.g., can't fork) */ +#define EX_OSFILE 72 /* critical OS file missing */ +#define EX_CANTCREAT 73 /* can't create (user) output file */ +#define EX_IOERR 74 /* input/output error */ +#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ +#define EX_PROTOCOL 76 /* remote error in protocol */ +#define EX_NOPERM 77 /* permission denied */ +#define EX_CONFIG 78 /* configuration error */ + +#define EX__MAX 78 /* maximum listed value */ + +#define warn(...) do { \ + fputs("dot_clean: ", stderr); \ + fprintf(stderr, __VA_ARGS__); \ + perror(NULL); \ +} while(0) + +