fix: Project file creation issues

This commit is contained in:
WerWolv
2023-01-01 21:46:41 +01:00
parent 78e52a0fe3
commit a20d6aa2b2
3 changed files with 16 additions and 55 deletions

View File

@@ -71,7 +71,6 @@ struct mtar_t {
const char *mtar_strerror(int err);
int mtar_open(mtar_t *tar, const char *filename, const char *mode);
int mtar_wopen(mtar_t *tar, const wchar_t *filename, const wchar_t *mode);
int mtar_close(mtar_t *tar);
int mtar_seek(mtar_t *tar, unsigned pos);

View File

@@ -207,41 +207,6 @@ int mtar_open(mtar_t *tar, const char *filename, const char *mode) {
return MTAR_ESUCCESS;
}
#if defined (_WIN32)
int mtar_wopen(mtar_t *tar, const wchar_t *filename, const wchar_t *mode) {
int err;
mtar_header_t h;
/* Init tar struct and functions */
memset(tar, 0, sizeof(*tar));
tar->write = file_write;
tar->read = file_read;
tar->seek = file_seek;
tar->close = file_close;
/* Assure mode is always binary */
if ( filename[0] == L'r' ) mode = L"rb";
if ( filename[0] == L'w' ) mode = L"wb";
if ( filename[0] == L'a' ) mode = L"ab";
/* Open file */
tar->stream = _wfopen(filename, mode);
if (!tar->stream) {
return MTAR_EOPENFAIL;
}
/* Read first header to check it is valid if mode is `r` */
if (*mode == L'r') {
err = mtar_read_header(tar, &h);
if (err != MTAR_ESUCCESS) {
mtar_close(tar);
return err;
}
}
/* Return ok */
return MTAR_ESUCCESS;
}
#endif
int mtar_close(mtar_t *tar) {
return tar->close(tar);

View File

@@ -1,6 +1,7 @@
#include <hex/helpers/tar.hpp>
#include <hex/helpers/literals.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/fs.hpp>
namespace hex {
@@ -9,25 +10,21 @@ namespace hex {
Tar::Tar(const std::fs::path &path, Mode mode) {
int error = MTAR_ESUCCESS;
#if defined (OS_WINDOWS)
if (mode == Tar::Mode::Read)
error = mtar_wopen(&this->m_ctx, path.c_str(), L"r");
else if (mode == Tar::Mode::Write)
error = mtar_wopen(&this->m_ctx, path.c_str(), L"a");
else if (mode == Tar::Mode::Create)
error = mtar_wopen(&this->m_ctx, path.c_str(), L"w");
else
error = MTAR_EFAILURE;
#else
if (mode == Tar::Mode::Read)
error = mtar_open(&this->m_ctx, path.c_str(), "r");
else if (mode == Tar::Mode::Write)
error = mtar_open(&this->m_ctx, path.c_str(), "a");
else if (mode == Tar::Mode::Create)
error = mtar_open(&this->m_ctx, path.c_str(), "w");
else
error = MTAR_EFAILURE;
#endif
// Explicitly create file so a short path gets generated
if (mode == Mode::Create) {
fs::File file(path, fs::File::Mode::Create);
file.flush();
}
auto shortPath = hex::fs::toShortPath(path);
if (mode == Tar::Mode::Read)
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "r");
else if (mode == Tar::Mode::Write)
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "a");
else if (mode == Tar::Mode::Create)
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "w");
else
error = MTAR_EFAILURE;
this->m_valid = (error == MTAR_ESUCCESS);
}