From 71e362e2f96392cff4779200e687cd1dc541a7c8 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 23 Aug 2015 14:13:46 +0100 Subject: [PATCH] Move session socket to /tmp/kakoune// --- src/file.cc | 18 ++++++++++++++++++ src/file.hh | 2 ++ src/remote.cc | 9 +++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/file.cc b/src/file.cc index bea46dbb..2c8afeb5 100644 --- a/src/file.cc +++ b/src/file.cc @@ -291,6 +291,24 @@ String find_file(StringView filename, ConstArrayView paths) return ""; } +void make_directory(StringView dir) +{ + auto it = dir.begin(), end = dir.end(); + while(it != end) + { + it = std::find(it+1, end, '/'); + struct stat st; + StringView dirname{dir.begin(), it}; + if (stat(dirname.zstr(), &st) == 0) + { + if (not S_ISDIR(st.st_mode)) + throw runtime_error(format("Cannot make directory, '{}' exists but is not a directory", dirname)); + } + else if (mkdir(dirname.zstr(), S_IRWXU) != 0) + throw runtime_error(format("mkdir failed for directory '{}' errno {}", dirname, errno)); + } +} + template Vector list_files(StringView prefix, StringView dirname, Filter filter) diff --git a/src/file.hh b/src/file.hh index fa9fa698..5cbdc778 100644 --- a/src/file.hh +++ b/src/file.hh @@ -48,6 +48,8 @@ void write_buffer_to_backup_file(Buffer& buffer); String find_file(StringView filename, ConstArrayView paths); +void make_directory(StringView dir); + time_t get_fs_timestamp(StringView filename); CandidateList complete_filename(StringView prefix, const Regex& ignore_regex, diff --git a/src/remote.cc b/src/remote.cc index ba8ff05c..f24e83ce 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -6,6 +6,7 @@ #include "command_manager.hh" #include "display_buffer.hh" #include "event_manager.hh" +#include "file.hh" #include #include @@ -432,7 +433,7 @@ static sockaddr_un session_addr(StringView session) { sockaddr_un addr; addr.sun_family = AF_UNIX; - format_to(addr.sun_path, "/tmp/kak-{}", session); + format_to(addr.sun_path, "/tmp/kakoune/{}/{}", getlogin(), session); return addr; } @@ -640,6 +641,8 @@ Server::Server(String session_name) fcntl(listen_sock, F_SETFD, FD_CLOEXEC); sockaddr_un addr = session_addr(m_session); + make_directory(split_path(addr.sun_path).first); + if (bind(listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1) throw runtime_error(format("unable to bind listen socket '{}'", addr.sun_path)); @@ -662,7 +665,9 @@ Server::Server(String session_name) void Server::close_session() { - unlink(format("/tmp/kak-{}", m_session).c_str()); + char socket_file[128]; + format_to(socket_file, "/tmp/kakoune/{}/{}", getlogin(), m_session); + unlink(socket_file); m_listener->close_fd(); m_listener.reset(); }