Do not assert on SIGTERM and backup modified buffers on signal induced termination

This commit is contained in:
Maxime Coste 2014-10-13 13:38:28 +01:00
parent fa85f0fc32
commit 2adaa7bdfa
5 changed files with 35 additions and 2 deletions

View File

@ -105,6 +105,15 @@ void BufferManager::set_last_used_buffer(Buffer& buffer)
m_buffers.emplace(m_buffers.begin(), &buffer);
}
void BufferManager::backup_modified_buffers()
{
for (auto& buf : m_buffers)
{
if ((buf->flags() & Buffer::Flags::File) and buf->is_modified())
write_buffer_to_backup_file(*buf);
}
}
CandidateList BufferManager::complete_buffer_name(StringView prefix,
ByteCount cursor_pos)
{

View File

@ -34,6 +34,8 @@ public:
Buffer& get_buffer(StringView name);
void set_last_used_buffer(Buffer& buffer);
void backup_modified_buffers();
CandidateList complete_buffer_name(StringView prefix,
ByteCount cursor_pos = -1);

View File

@ -209,6 +209,19 @@ void write_buffer_to_file(Buffer& buffer, StringView filename)
buffer.run_hook_in_own_context("BufWritePost", buffer.name());
}
void write_buffer_to_backup_file(Buffer& buffer)
{
char pattern[PATH_MAX+1];
snprintf(pattern, PATH_MAX+1, ".%s.kak.XXXXXX",
real_path(buffer.name()).c_str());
int fd = mkstemp(pattern);
if (fd >= 0)
{
write_buffer_to_fd(buffer, fd);
close(fd);
}
}
String find_file(StringView filename, memoryview<String> paths)
{
struct stat buf;

View File

@ -36,6 +36,7 @@ Buffer* create_buffer_from_file(String filename);
void write_buffer_to_file(Buffer& buffer, StringView filename);
void write_buffer_to_fd(Buffer& buffer, int fd);
void write_buffer_to_backup_file(Buffer& buffer);
String find_file(StringView filename, memoryview<String> paths);

View File

@ -252,10 +252,18 @@ void signal_handler(int signal)
case SIGQUIT: text = "SIGQUIT"; break;
case SIGTERM: text = "SIGTERM"; break;
}
on_assert_failed(text);
if (signal != SIGTERM)
on_assert_failed(text);
if (Server::has_instance())
Server::instance().close_session();
abort();
if (BufferManager::has_instance())
BufferManager::instance().backup_modified_buffers();
if (signal == SIGTERM)
exit(-1);
else
abort();
}
int run_client(StringView session, StringView init_command)