diff --git a/src/file.cc b/src/file.cc index bea46dbb4..2c8afeb57 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 fa9fa6983..5cbdc778f 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 ba8ff05cf..f24e83ce3 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(); }