Rename, ready for a super big change about OPFS
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "global.h"
|
||||
#include "jsBridge.h"
|
||||
|
||||
pthread_t dstThrd{pthread_self()};
|
||||
bool OPFSOk{};
|
||||
@@ -17,9 +17,7 @@ void fireEv(int index, const char* content, const char* type) {
|
||||
}
|
||||
reusableThrd::reusableThrd() {
|
||||
std::thread thrd{[this](){
|
||||
while(!done.test(std::memory_order_relaxed)) {
|
||||
blocker.wait(queue.empty() || done.test(std::memory_order_relaxed), std::memory_order_relaxed);
|
||||
blocker.clear(std::memory_order_relaxed);
|
||||
while(!done) {
|
||||
while(!queue.empty()) {
|
||||
emscripten_console_log("==========Executing task===========");
|
||||
queue.front()();
|
||||
@@ -31,12 +29,7 @@ reusableThrd::reusableThrd() {
|
||||
}
|
||||
void reusableThrd::addTask(std::function<void()>&& task) {
|
||||
queue.emplace(task);
|
||||
// The following line will make -O3 magically work. Yes, I know, it is a god dang PRINT STATEMENT.
|
||||
//emscripten_console_log("");
|
||||
blocker.test_and_set(std::memory_order_relaxed);
|
||||
blocker.notify_one();
|
||||
}
|
||||
reusableThrd::~reusableThrd() {
|
||||
done.test_and_set(std::memory_order_relaxed);
|
||||
done.notify_one();
|
||||
done = true;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "global.h"
|
||||
#include "jsBridge.h"
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
@@ -11,7 +11,6 @@
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
struct genericModel {
|
||||
bool normalMdl;
|
||||
bool recognizerUsedThrd{};
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
|
||||
#include <emscripten/wasmfs.h>
|
||||
#include <emscripten/console.h>
|
||||
@@ -18,8 +19,8 @@ extern ProxyingQueue glbQ;
|
||||
void fireEv(int index, const char* content, const char* type = nullptr);
|
||||
struct reusableThrd {
|
||||
std::queue<std::function<void()>> queue{};
|
||||
std::atomic_flag blocker{};
|
||||
std::atomic_flag done{};
|
||||
bool done{};
|
||||
|
||||
void addTask(std::function<void()>&& task);
|
||||
reusableThrd();
|
||||
~reusableThrd();
|
||||
@@ -46,7 +46,7 @@ void recognizer:: finishConstruction(genericModel* model, genericModel* spkModel
|
||||
}};
|
||||
if(!model->recognizerUsedThrd) {
|
||||
model->recognizerUsedThrd = true;
|
||||
model->thrd.addTask(main);
|
||||
model->thrd.addTask(main);
|
||||
emscripten_console_log("Adding task to model thread...");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "genericModel.h"
|
||||
#include "global.h"
|
||||
#include "jsBridge.h"
|
||||
|
||||
struct recognizer {
|
||||
std::atomic_flag done{};
|
||||
|
||||
135
src/untar.cc
Normal file
135
src/untar.cc
Normal file
@@ -0,0 +1,135 @@
|
||||
// Taken from https://github.com/libarchive/libarchive/blob/master/contrib/untar.c
|
||||
// Stripped all the way down for smallest size
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <filesystem>
|
||||
#include <emscripten/console.h>
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
#define TO_STRING(x) #x
|
||||
#define STR(x) TO_STRING(x)
|
||||
|
||||
/* Parse an octal number, ignoring leading and trailing nonsense. */
|
||||
int parseoct(const char *p, size_t n)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while ((*p < '0' || *p > '7') && n > 0) {
|
||||
++p;
|
||||
--n;
|
||||
}
|
||||
while (*p >= '0' && *p <= '7' && n > 0) {
|
||||
i *= 8;
|
||||
i += *p - '0';
|
||||
++p;
|
||||
--n;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Returns true if this is 512 zero bytes. */
|
||||
bool is_end_of_archive(const char *p)
|
||||
{
|
||||
int n;
|
||||
for (n = 511; n >= 0; --n)
|
||||
if (p[n] != '\0')
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Create a file, including parent directory as necessary. */
|
||||
FILE * create_file(char *pathname) {
|
||||
FILE *f;
|
||||
if (f == NULL) {
|
||||
/* Try creating parent dir and then creating file. */
|
||||
char *p = strrchr(pathname, '/');
|
||||
if (p != NULL) {
|
||||
*p = '\0';
|
||||
fs::create_directories(pathname);
|
||||
*p = '/';
|
||||
f = fopen(pathname, "wb+");
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
/* Verify the tar checksum. */
|
||||
int verify_checksum(const char *p)
|
||||
{
|
||||
int n, u = 0;
|
||||
for (n = 0; n < 512; ++n) {
|
||||
if (n < 148 || n > 155)
|
||||
/* Standard tar checksum adds unsigned bytes. */
|
||||
u += ((unsigned char *)p)[n];
|
||||
else
|
||||
u += 0x20;
|
||||
|
||||
}
|
||||
return (u == parseoct(p + 148, 8));
|
||||
}
|
||||
|
||||
/* Extract a tar archive. */
|
||||
bool untar(FILE *a, const char *path)
|
||||
{
|
||||
char buff[512];
|
||||
FILE *f = NULL;
|
||||
size_t bytes_read;
|
||||
off_t filesize;
|
||||
|
||||
emscripten_console_logf("Extracting from %s", path);
|
||||
for (;;) {
|
||||
bytes_read = fread(buff, 1, 512, a);
|
||||
if (bytes_read < 512) {
|
||||
emscripten_console_logf("Short read on %s", path);
|
||||
return false;
|
||||
}
|
||||
if (is_end_of_archive(buff)) {
|
||||
emscripten_console_logf("End of %s", path);
|
||||
return true;
|
||||
}
|
||||
if (!verify_checksum(buff)) {
|
||||
emscripten_console_log("Checksum failure");
|
||||
return false;
|
||||
}
|
||||
filesize = parseoct(buff + 124, 12);
|
||||
switch (buff[156]) {
|
||||
// Directory
|
||||
case '5':
|
||||
emscripten_console_logf(" Extracting dir %s", buff);
|
||||
fs::create_directories(buff);
|
||||
filesize = 0;
|
||||
break;
|
||||
// Normal file
|
||||
case '0':
|
||||
case '\0':
|
||||
emscripten_console_logf(" Extracting file %s", buff);
|
||||
f = create_file(buff);
|
||||
}
|
||||
while (filesize > 0) {
|
||||
bytes_read = fread(buff, 1, 512, a);
|
||||
if (bytes_read < 512) {
|
||||
emscripten_console_logf("Short read on %s", path);
|
||||
return false;
|
||||
}
|
||||
if (filesize < 512)
|
||||
bytes_read = filesize;
|
||||
if (f != NULL) {
|
||||
if (fwrite(buff, 1, bytes_read, f)
|
||||
!= bytes_read)
|
||||
{
|
||||
emscripten_console_log("Failed write");
|
||||
fclose(f);
|
||||
f = NULL;
|
||||
}
|
||||
}
|
||||
filesize -= (off_t)bytes_read;
|
||||
}
|
||||
if (f != NULL) {
|
||||
fclose(f);
|
||||
f = NULL;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user