Skip to content

Commit

Permalink
Fix crash caused by missing crashdump directory
Browse files Browse the repository at this point in the history
  • Loading branch information
r888800009 committed Sep 19, 2024
1 parent 81b752c commit 617c85d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions source/creator/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ bool incOpenProject(string mainPath, string backupPath) {
string report;

try {
mkdirCrashDumpDir();
string path = genCrashDumpPath("inochi-creator-runtime-error");
write(path, genCrashDump(ex));
report = _("Please report this file to the developers:\n\n%s").format(path);
Expand Down
24 changes: 23 additions & 1 deletion source/creator/utils/crashdump.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import std.file : write;
//import i18n;
import std.stdio;
import std.path;
import std.process : environment;
import std.traits;
import std.array;
import i18n;
Expand Down Expand Up @@ -46,10 +47,15 @@ version(Windows) {
}
}

string linuxStateHome() {
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
return environment.get("XDG_STATE_HOME", buildPath(environment["HOME"], ".local", "state"));
}

string getCrashDumpDir() {
version(Windows) return getDesktopDir();
else version(OSX) return expandTilde("~/Library/Logs/");
else version(linux) return expandTilde("$XDG_STATE_HOME/"); // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
else version(linux) return expandTilde(linuxStateHome() ~ "/");
else return expandTilde("~");
}

Expand All @@ -58,9 +64,25 @@ string genCrashDumpPath(string filename) {
return buildPath(getCrashDumpDir(), filename ~ "-" ~ Clock.currTime.toISOString() ~ ".txt");
}

void mkdirCrashDumpDir() {
import std.file : mkdirRecurse, exists, setAttributes;
auto dir = getCrashDumpDir();
if (exists(dir))
return;

mkdirRecurse(dir);
version(linux) {
import std.conv : octal;
// https://specifications.freedesktop.org/basedir-spec/latest/#referencing
// TODO: Should we set permissions recursively?
setAttributes(dir, octal!700);
}
}

void crashdump(T...)(Throwable throwable, T state) {
// Write crash dump to disk
try {
mkdirCrashDumpDir();
write(genCrashDumpPath("inochi-creator-crashdump"), genCrashDump(throwable, state));
} catch (Exception ex) {
writeln("Failed to write crash dump" ~ ex.msg);
Expand Down

0 comments on commit 617c85d

Please sign in to comment.