Merge remote-tracking branch 'eraserhd/pr/master/fix-kak-list-sessions'

This commit is contained in:
Maxime Coste 2019-08-28 21:27:30 +10:00
commit eeec8ade51
3 changed files with 14 additions and 18 deletions

View File

@ -1018,21 +1018,13 @@ int main(int argc, char* argv[])
const bool clear_sessions = (bool)parser.get_switch("clear"); const bool clear_sessions = (bool)parser.get_switch("clear");
if (list_sessions or clear_sessions) if (list_sessions or clear_sessions)
{ {
const String username = get_user_name(); for (auto& session : list_files(session_directory()))
const StringView tmp_dir = tmpdir();
for (auto& session : list_files(format("{}/kakoune/{}/", tmp_dir,
username)))
{ {
const bool valid = check_session(session); const bool valid = check_session(session);
if (list_sessions) if (list_sessions)
write_stdout(format("{}{}\n", session, valid ? "" : " (dead)")); write_stdout(format("{}{}\n", session, valid ? "" : " (dead)"));
if (not valid and clear_sessions) if (not valid and clear_sessions)
{ unlink(session_path(session).c_str());
char socket_file[128];
format_to(socket_file, "{}/kakoune/{}/{}", tmp_dir,
username, session);
unlink(socket_file);
}
} }
return 0; return 0;
} }

View File

@ -537,6 +537,15 @@ String get_user_name()
return getenv("USER"); return getenv("USER");
} }
String session_directory()
{
StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
if (xdg_runtime_dir.empty())
return format("{}/kakoune/{}", tmpdir(), get_user_name());
else
return format("{}/kakoune", xdg_runtime_dir);
}
void make_session_directory() void make_session_directory()
{ {
StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"); StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
@ -544,21 +553,15 @@ void make_session_directory()
{ {
// set sticky bit on the shared kakoune directory // set sticky bit on the shared kakoune directory
make_directory(format("{}/kakoune", tmpdir()), 01777); make_directory(format("{}/kakoune", tmpdir()), 01777);
make_directory(format("{}/kakoune/{}", tmpdir(), get_user_name()), 0711);
} }
else make_directory(session_directory(), 0711);
make_directory(format("{}/kakoune", xdg_runtime_dir), 0711);
} }
String session_path(StringView session) String session_path(StringView session)
{ {
if (contains(session, '/')) if (contains(session, '/'))
throw runtime_error{"session names cannot have slashes"}; throw runtime_error{"session names cannot have slashes"};
StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"); return format("{}/{}", session_directory(), session);
if (xdg_runtime_dir.empty())
return format("{}/kakoune/{}/{}", tmpdir(), get_user_name(), session);
else
return format("{}/kakoune/{}", xdg_runtime_dir, session);
} }
static sockaddr_un session_addr(StringView session) static sockaddr_un session_addr(StringView session)

View File

@ -45,6 +45,7 @@ private:
void send_command(StringView session, StringView command); void send_command(StringView session, StringView command);
String get_user_name(); String get_user_name();
String session_directory();
String session_path(StringView session); String session_path(StringView session);
struct Server : public Singleton<Server> struct Server : public Singleton<Server>