NEVER FORGET TO SPECIFY THE 3RD ARG WHEN USING O_CREAT. NEVER EVER AGAIN! (2 months wasted). Worked for mode 0
This commit is contained in:
@@ -1,12 +1,7 @@
|
||||
#include "genericModel.h"
|
||||
|
||||
genericModel::genericModel(std::string storepath, std::string id, int index, bool normalMdl) : index(index), normalMdl(normalMdl) {
|
||||
this->storepath = new char[storepath.size()];
|
||||
this->id = new char[id.size()];
|
||||
strcpy(this->storepath, storepath.c_str());
|
||||
strcpy(this->id, id.c_str());
|
||||
}
|
||||
void genericModel::load(bool newTask) {
|
||||
genericModel::genericModel(std::string storepath, std::string id, int index, bool normalMdl) : index(index), normalMdl(normalMdl), storepath(std::move(storepath)), id(std::move(id)) {}
|
||||
void genericModel::load() {
|
||||
auto main{[this](){
|
||||
if(normalMdl) {
|
||||
VoskModel* temp {vosk_model_new(".")};
|
||||
@@ -26,14 +21,10 @@ void genericModel::load(bool newTask) {
|
||||
}
|
||||
fireEv("_continue", nullptr, index);
|
||||
}};
|
||||
if(!newTask) {
|
||||
main();
|
||||
return;
|
||||
}
|
||||
thrd.addTask(main);
|
||||
}
|
||||
bool genericModel::checkFiles() {
|
||||
if(std::holds_alternative<VoskModel*>(mdl)) {
|
||||
if(normalMdl) {
|
||||
return fs::exists("am/final.mdl", tank) &&
|
||||
fs::exists("conf/mfcc.conf", tank) &&
|
||||
fs::exists("conf/model.conf", tank) &&
|
||||
@@ -54,13 +45,11 @@ bool genericModel::checkFiles() {
|
||||
fs::exists("transform.mat", tank);
|
||||
}
|
||||
genericModel::~genericModel() {
|
||||
if(std::holds_alternative<VoskModel*>(mdl)) {
|
||||
if(normalMdl) {
|
||||
vosk_model_free(std::get<0>(mdl));
|
||||
return;
|
||||
}
|
||||
vosk_spk_model_free(std::get<1>(mdl));
|
||||
delete[] storepath;
|
||||
delete[] id;
|
||||
}
|
||||
void genericModel::check() {
|
||||
thrd.addTask([this](){
|
||||
@@ -68,10 +57,10 @@ void genericModel::check() {
|
||||
fireEv("_checkMdl", "OPFS isn't available", index);
|
||||
return;
|
||||
}
|
||||
if(!OPFSTried){
|
||||
if(!OPFSTried){
|
||||
OPFSTried = true;
|
||||
OPFSOk = wasmfs_create_directory("/opfs", 0777, wasmfs_create_opfs_backend()) == 0;
|
||||
}
|
||||
};
|
||||
if(!OPFSOk) {
|
||||
fireEv("_checkMdl", "OPFS initialization failed", index);
|
||||
return;
|
||||
@@ -98,11 +87,13 @@ void genericModel::check() {
|
||||
fireEv("_checkMdl", "Couldn't open id file", index);
|
||||
return;
|
||||
}
|
||||
emscripten_console_log("1");
|
||||
if(fseek(idFile, 0, SEEK_END) != 0) {
|
||||
fireEv("_checkMdl", "Id file end seeking fail", index);
|
||||
fclose(idFile);
|
||||
return;
|
||||
};
|
||||
emscripten_console_log("1");
|
||||
long long oldsize{ftell(idFile)};
|
||||
char* oldid {new char[oldsize]};
|
||||
if(fseek(idFile, 0L, SEEK_SET) != 0) {
|
||||
@@ -110,11 +101,13 @@ void genericModel::check() {
|
||||
fclose(idFile);
|
||||
return;
|
||||
};
|
||||
emscripten_console_log("1");
|
||||
fread(oldid, 1, oldsize, idFile);
|
||||
fclose(idFile);
|
||||
if(strcmp(oldid, id) != 0) fireEv("_checkMdl", "fetch", index);
|
||||
if(strcmp(oldid, id.c_str()) != 0) fireEv("_checkMdl", "fetch", index);
|
||||
else fireEv("_checkMdl", nullptr, index);
|
||||
delete[] oldid;
|
||||
emscripten_console_log("1");
|
||||
});
|
||||
}
|
||||
void genericModel::afterFetch() {
|
||||
@@ -126,13 +119,11 @@ void genericModel::afterFetch() {
|
||||
fireEv("_continue", "Unable to extract model", index);
|
||||
return;
|
||||
}
|
||||
fs::remove("/opfs/m0dEl.tar",tank);
|
||||
fs::remove("README",tank);
|
||||
if(!checkFiles()) {
|
||||
fireEv("_continue", "URL points to invalid model files", index);
|
||||
return;
|
||||
}
|
||||
int idFd {open("id", O_WRONLY | O_TRUNC | O_CREAT)};
|
||||
int idFd {creat("id",0777)};
|
||||
if(idFd == -1) {
|
||||
fireEv("_continue", "Unable create ID file", index);
|
||||
fs::remove("/opfs/m0dEl.tar",tank);
|
||||
@@ -140,7 +131,7 @@ void genericModel::afterFetch() {
|
||||
fs::remove_all(storepath, tank);
|
||||
return;
|
||||
}
|
||||
if(write(idFd, id, strlen(id)) == -1) {
|
||||
if(write(idFd, id.c_str(), id.size()) == -1) {
|
||||
fireEv("_continue", "Unable to write new ID", index);
|
||||
fs::remove("/opfs/m0dEl.tar",tank);
|
||||
fs::current_path("/opfs", tank);
|
||||
@@ -149,30 +140,35 @@ void genericModel::afterFetch() {
|
||||
return;
|
||||
};
|
||||
close(idFd);
|
||||
load(false);
|
||||
});
|
||||
load();
|
||||
}
|
||||
bool genericModel::extract() {
|
||||
std::string path{};
|
||||
archive* src {archive_read_new()};
|
||||
archive_entry* entry{};
|
||||
int fd{};
|
||||
static fs::path path{};
|
||||
static int fd{};
|
||||
static archive* src {archive_read_new()};
|
||||
archive_entry* entry{archive_entry_new()};
|
||||
archive_read_support_format_tar(src);
|
||||
archive_read_open_filename(src, "/opfs/m0dEl.tar", 10240);
|
||||
if(archive_errno(src) != 0) return false;
|
||||
while(archive_read_next_header2(src, entry) == ARCHIVE_OK) {
|
||||
if(archive_errno(src) != 0) return false;
|
||||
path = archive_entry_pathname(entry);
|
||||
// Strip 1st component, keep relative path
|
||||
path = "." + path.substr(path.find("/"));
|
||||
if(!fs::path(path).has_extension()) {
|
||||
path = "." + path.generic_string().substr(path.generic_string().find("/"));
|
||||
emscripten_console_log(path.c_str());
|
||||
if(!path.has_extension()) {
|
||||
fs::create_directory(path);
|
||||
continue;
|
||||
}
|
||||
fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC);
|
||||
fd = creat(path.c_str(),0777);
|
||||
if(fd == -1) return false;
|
||||
archive_read_data_into_fd(src, fd);
|
||||
close(fd);
|
||||
if(archive_errno(src) != 0) return false;
|
||||
close(fd);
|
||||
}
|
||||
fs::remove("README",tank);
|
||||
fs::remove("/opfs/m0dEl.tar",tank);
|
||||
archive_read_free(src);
|
||||
return true;
|
||||
}
|
||||
@@ -16,12 +16,12 @@ struct genericModel {
|
||||
bool normalMdl{};
|
||||
bool recognizerUsedThrd{};
|
||||
int index{};
|
||||
char* storepath{};
|
||||
char* id{};
|
||||
std::string storepath;
|
||||
std::string id;
|
||||
std::variant<VoskModel*, VoskSpkModel*> mdl{};
|
||||
reusableThrd thrd{};
|
||||
static bool extract();
|
||||
void load(bool newTask);
|
||||
void load();
|
||||
void check();
|
||||
bool checkFiles();
|
||||
void afterFetch();
|
||||
|
||||
@@ -11,11 +11,8 @@ void fireEv(const char *type, const char *content, int index) {
|
||||
objs[$0].dispatchEvent(new CustomEvent(UTF8ToString($1), {"detail" : UTF8ToString($2)}));
|
||||
},index, type, content);
|
||||
}};
|
||||
if(dstThrd == pthread_self()) {
|
||||
proxy();
|
||||
return;
|
||||
}
|
||||
glbQ.proxySync(dstThrd, proxy);
|
||||
if(dstThrd == pthread_self()) proxy();
|
||||
else glbQ.proxySync(dstThrd, proxy);
|
||||
}
|
||||
void reusableThrd::addTask(std::function<void()>&& task) {
|
||||
static std::thread thrd{[this](){
|
||||
|
||||
Reference in New Issue
Block a user