Skip to content

Commit

Permalink
Added built-in mkdir on missing dirs instead of crash.
Browse files Browse the repository at this point in the history
████ ███  To request new features or in case this commit breaks something for you,
████ ███  please, create a new github issue with all possible information for me,
▓███▀█▄   but never share your API Keys!
▒▓██ ███
░▒▓█ ███  Signed-off-by: Carles Tubio <ctubio@users.noreply.github.com>
 _________________________________________
/ Hello, WORLD!                           \
|                                         |
\ pssst.. 1.00000000 BTC = 58950.21 EUR.  /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
  • Loading branch information
ctubio committed Sep 29, 2024
1 parent e3ae6af commit dd98d8b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ K ?= K.sh
MAJOR = 0
MINOR = 7
PATCH = 0
BUILD = 20
BUILD = 21

OBLIGATORY = DISCLAIMER: This is strict non-violent software: \n$\
if you hurt other living creatures, please stop; \n$\
Expand Down Expand Up @@ -117,7 +117,6 @@ hlep hepl help:
# make download - download K src precompiled #
# make clean - remove external src files #
# KALL=1 make clean - remove external src files #
# make cleandb - remove databases #
# make uninstall - remove /usr/local/bin/K-* #
# #

Expand Down Expand Up @@ -193,17 +192,13 @@ download:
@$(MAKE) system_install -s
@test -n "`ls *.sh 2>/dev/null`" || (cp etc/K.sh.dist K.sh && chmod +x K.sh && echo && echo NEW CONFIG FILE created at: && LS_COLORS="ex=40;92" CLICOLOR="Yes" ls $(shell ls --color > /dev/null 2>&1 && echo --color) -lah K.sh && echo)

cleandb:
rm -vrf $(KHOME)/db/K*

packages:
@test -n "`command -v apt-get`" && sudo apt-get -y install g++ build-essential automake autoconf libtool libxml2 libxml2-dev zlib1g-dev python curl gzip screen doxygen graphviz \
|| (test -n "`command -v yum`" && sudo yum -y install gcc-c++ automake autoconf libtool libxml2 libxml2-devel python curl gzip screen) \
|| (test -n "`command -v brew`" && (xcode-select --install || :) && (brew install automake autoconf libxml2 zlib python curl gzip screen proctools doxygen graphviz || brew upgrade || :)) \
|| (test -n "`command -v pacman`" && $(SUDO) pacman --noconfirm -S --needed base-devel libxml2 zlib curl python gzip)

uninstall:
rm -vrf $(KHOME)/cache $(KHOME)/node_modules
@$(foreach bin,$(addprefix /usr/local/bin/,$(notdir $(wildcard $(KBUILD)/bin/K-*))), $(SUDO) rm -v $(bin);)

system_install:
Expand All @@ -217,9 +212,6 @@ system_install:
@echo
@$(SUDO) mkdir -p $(KHOME)
@$(SUDO) chown $(shell id -u) $(KHOME)
@mkdir -p $(KHOME)/db
@mkdir -p $(KHOME)/cache
@rm -f $(KHOME)/cache/handshake.*

install:
@seq `expr $${COLUMNS:-21} / 2` | sed 's/.*/=/' | xargs echo \
Expand Down Expand Up @@ -348,4 +340,4 @@ md5: src
asandwich:
@test "`whoami`" = "root" && echo OK || echo make it yourself!

.PHONY: all K $(SOURCE) hlep hepl help doc test src client client.o clean check lib download cleandb screen-help list screen start stop restart startall stopall restartall packages system_install uninstall install docker reinstall diff upgrade changelog test-c push MAJOR MINOR PATCH BUILD release md5 asandwich
.PHONY: all K $(SOURCE) hlep hepl help doc test src client client.o clean check lib download screen-help list screen start stop restart startall stopall restartall packages system_install uninstall install docker reinstall diff upgrade changelog test-c push MAJOR MINOR PATCH BUILD release md5 asandwich
4 changes: 3 additions & 1 deletion src/lib/Krypto.ninja-apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ namespace ₿ {
+ '.' + base
+ '.' + quote
+ '.' + "json";
if (!nocache and !Files::mkdirs(cache))
print("Error while writing into " + cache + ", please create the parent directory or change the permissions manually before try again.");
fstream file;
struct stat st;
if (!nocache
Expand All @@ -339,7 +341,7 @@ namespace ₿ {
decimal.price.precision(tickPrice);
decimal.amount.precision(tickSize);
decimal.percent.precision(1e-2);
if (!file.is_open()
if (!nocache and !file.is_open() and Files::mkdirs(cache)
and tickPrice and tickSize and minSize
and !base.empty() and !quote.empty()
) {
Expand Down
9 changes: 7 additions & 2 deletions src/lib/Krypto.ninja-bots.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,19 +812,24 @@ namespace ₿ {
protected:
void backups(const Option *const K) {
if (blackhole()) return;
if (K->arg<string>("database") != ":memory:")
if (K->arg<string>("database") != ":memory:") {
if (!Files::mkdirs(K->arg<string>("database")))
error("CF", "Can't write into " + K->arg<string>("database") + ", please create the parent directory or change the permissions manually before try again.");
dbSize = [K](){
struct stat st;
return stat(K->arg<string>("database").data(), &st) ? 0 : st.st_size;
};
}
sqlite3 *_db = nullptr;
if (sqlite3_open(K->arg<string>("database").data(), &_db))
error("DB", sqlite3_errmsg(_db));
db.reset(_db);
K->log("DB", "loaded OK from", K->arg<string>("database"));
if (!K->arg<string>("diskdata").empty()) {
if (!Files::mkdirs(K->arg<string>("diskdata")))
error("CF", "Can't write into " + K->arg<string>("diskdata") + ", please create the parent directory or change the permissions manually before try again.");
exec("ATTACH '" + K->arg<string>("diskdata") + "' AS " + (disk = "disk") + ";");
K->log("DB", "loaded OK from", K->arg<string>("diskdata"));
K->log("DB", "loaded OK from", K->arg<string>("diskdata"));
}
exec("PRAGMA " + disk + ".journal_mode = WAL;"
"PRAGMA " + disk + ".synchronous = NORMAL;");
Expand Down
20 changes: 20 additions & 0 deletions src/lib/Krypto.ninja-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -1378,4 +1378,24 @@ namespace ₿ {
return stream.str();
};
};

class Files {
public:
static bool mkdirs(const string &file) {
string::size_type has_dir = file.find_last_of("/\\");
if (has_dir != string::npos) {
string dir = file.substr(0, has_dir);
if (access(dir.data(), R_OK) == -1) {
mkdir(dir.data()
#ifndef _WIN32
, 0775
#endif
);
if (access(dir.data(), R_OK) == -1)
return false;
}
}
return true;
}
};
}

0 comments on commit dd98d8b

Please sign in to comment.