Skip to content

Commit 02e7d41

Browse files
committed
Make trace-dir selection more backwards compatible and more XDG compatible.
If XDG_DATA_HOME is set, but ~/.rr already exists, use ~/.rr. If XDG_DATA_HOME is not set and ~/.rr doesn't already exist (i.e. new rr user), use ~/.local/share/rr.
1 parent 2c9e68c commit 02e7d41

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/TraceStream.cc

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,44 @@ static TraceStream::Substream operator++(TraceStream::Substream& s) {
5151
return s;
5252
}
5353

54+
static bool dir_exists(const string& dir) {
55+
struct stat dummy;
56+
return !dir.empty() && stat(dir.c_str(), &dummy) == 0;
57+
}
58+
5459
static string default_rr_trace_dir() {
55-
const char* xdg_data_home = getenv("XDG_DATA_HOME");
56-
if (xdg_data_home) {
57-
return string(xdg_data_home) + "/rr";
60+
static string cached_dir;
61+
62+
if (!cached_dir.empty()) {
63+
return cached_dir;
5864
}
5965

66+
string dot_dir;
6067
const char* home = getenv("HOME");
6168
if (home) {
62-
return string(home) + "/.rr";
69+
dot_dir = string(home) + "/.rr";
70+
}
71+
string xdg_dir;
72+
const char* xdg_data_home = getenv("XDG_DATA_HOME");
73+
if (xdg_data_home) {
74+
xdg_dir = string(xdg_data_home) + "/rr";
75+
} else if (home) {
76+
xdg_dir = string(home) + "/.local/share/rr";
77+
}
78+
79+
// If XDG dir does not exist but ~/.rr does, prefer ~/.rr for backwards
80+
// compatibility.
81+
if (dir_exists(xdg_dir)) {
82+
cached_dir = xdg_dir;
83+
} else if (dir_exists(dot_dir)) {
84+
cached_dir = dot_dir;
85+
} else if (!xdg_dir.empty()) {
86+
cached_dir = xdg_dir;
87+
} else {
88+
cached_dir = "/tmp/rr";
6389
}
6490

65-
return string("/tmp/.rr");
91+
return cached_dir;
6692
}
6793

6894
static string trace_save_dir() {

0 commit comments

Comments
 (0)