More robust Buffer filesystem timestamp handling
This commit is contained in:
parent
c3bafea2cd
commit
44281c8fed
|
@ -12,11 +12,13 @@
|
|||
namespace Kakoune
|
||||
{
|
||||
|
||||
Buffer::Buffer(String name, Flags flags, std::vector<String> lines)
|
||||
Buffer::Buffer(String name, Flags flags, std::vector<String> lines,
|
||||
time_t fs_timestamp)
|
||||
: m_name(std::move(name)), m_flags(flags | Flags::NoUndo),
|
||||
m_history(), m_history_cursor(m_history.begin()),
|
||||
m_last_save_undo_index(0),
|
||||
m_timestamp(0),
|
||||
m_fs_timestamp(fs_timestamp),
|
||||
m_hooks(GlobalHooks::instance()),
|
||||
m_options(GlobalOptions::instance())
|
||||
{
|
||||
|
@ -44,7 +46,10 @@ Buffer::Buffer(String name, Flags flags, std::vector<String> lines)
|
|||
if (flags & Flags::New)
|
||||
m_hooks.run_hook("BufNew", m_name, context);
|
||||
else
|
||||
{
|
||||
kak_assert(m_fs_timestamp != InvalidTime);
|
||||
m_hooks.run_hook("BufOpen", m_name, context);
|
||||
}
|
||||
}
|
||||
|
||||
m_hooks.run_hook("BufCreate", m_name, context);
|
||||
|
@ -607,6 +612,7 @@ void Buffer::notify_saved()
|
|||
++m_timestamp;
|
||||
m_last_save_undo_index = history_cursor_index;
|
||||
}
|
||||
m_fs_timestamp = get_fs_timestamp(m_name);
|
||||
}
|
||||
|
||||
BufferCoord Buffer::advance(BufferCoord coord, ByteCount count) const
|
||||
|
|
|
@ -17,6 +17,8 @@ namespace Kakoune
|
|||
|
||||
class Buffer;
|
||||
|
||||
constexpr time_t InvalidTime = 0;
|
||||
|
||||
struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount>
|
||||
{
|
||||
constexpr BufferCoord(LineCount line = 0, ByteCount column = 0)
|
||||
|
@ -90,7 +92,8 @@ public:
|
|||
NoUndo = 8,
|
||||
};
|
||||
|
||||
Buffer(String name, Flags flags, std::vector<String> lines = { "\n" });
|
||||
Buffer(String name, Flags flags, std::vector<String> lines = { "\n" },
|
||||
time_t fs_timestamp = InvalidTime);
|
||||
Buffer(const Buffer&) = delete;
|
||||
Buffer& operator= (const Buffer&) = delete;
|
||||
~Buffer();
|
||||
|
|
|
@ -147,9 +147,6 @@ void write_buffer(CommandParameters params, Context& context)
|
|||
: parse_filename(params[0]);
|
||||
|
||||
write_buffer_to_file(buffer, filename);
|
||||
|
||||
if (filename == buffer.name())
|
||||
buffer.notify_saved();
|
||||
}
|
||||
|
||||
void write_all_buffers(CommandParameters params, Context& context)
|
||||
|
@ -160,10 +157,7 @@ void write_all_buffers(CommandParameters params, Context& context)
|
|||
for (auto& buffer : BufferManager::instance())
|
||||
{
|
||||
if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified())
|
||||
{
|
||||
write_buffer_to_file(*buffer, buffer->name());
|
||||
buffer->notify_saved();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -175,8 +175,8 @@ Buffer* create_buffer_from_file(String filename)
|
|||
else
|
||||
pos = line_end + 1;
|
||||
}
|
||||
Buffer* buffer = new Buffer(filename, Buffer::Flags::File, std::move(lines));
|
||||
buffer->set_fs_timestamp(st.st_mtime);
|
||||
Buffer* buffer = new Buffer{filename, Buffer::Flags::File,
|
||||
std::move(lines), st.st_mtime};
|
||||
|
||||
OptionManager& options = buffer->options();
|
||||
options.get_local_option("eolformat").set<String>(crlf ? "crlf" : "lf");
|
||||
|
@ -228,7 +228,7 @@ void write_buffer_to_file(Buffer& buffer, const String& filename)
|
|||
write(fd, eoldata, filename);
|
||||
}
|
||||
if ((buffer.flags() & Buffer::Flags::File) and filename == buffer.name())
|
||||
buffer.set_fs_timestamp(get_fs_timestamp(filename));
|
||||
buffer.notify_saved();
|
||||
}
|
||||
|
||||
String find_file(const String& filename, memoryview<String> paths)
|
||||
|
@ -312,7 +312,7 @@ time_t get_fs_timestamp(const String& filename)
|
|||
{
|
||||
struct stat st;
|
||||
if (stat(filename.c_str(), &st) != 0)
|
||||
throw runtime_error("stat failed on " + filename);
|
||||
return InvalidTime;
|
||||
return st.st_mtime;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user