Move session socket to /tmp/kakoune/<user name>/<session>

This commit is contained in:
Maxime Coste 2015-08-23 14:13:46 +01:00
parent ae839fc471
commit 71e362e2f9
3 changed files with 27 additions and 2 deletions

View File

@ -291,6 +291,24 @@ String find_file(StringView filename, ConstArrayView<String> 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<typename Filter>
Vector<String> list_files(StringView prefix, StringView dirname,
Filter filter)

View File

@ -48,6 +48,8 @@ void write_buffer_to_backup_file(Buffer& buffer);
String find_file(StringView filename, ConstArrayView<String> paths);
void make_directory(StringView dir);
time_t get_fs_timestamp(StringView filename);
CandidateList complete_filename(StringView prefix, const Regex& ignore_regex,

View File

@ -6,6 +6,7 @@
#include "command_manager.hh"
#include "display_buffer.hh"
#include "event_manager.hh"
#include "file.hh"
#include <sys/types.h>
#include <sys/socket.h>
@ -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();
}