More robust Buffer filesystem timestamp handling

This commit is contained in:
Maxime Coste 2013-10-17 18:47:09 +01:00
parent c3bafea2cd
commit 44281c8fed
4 changed files with 15 additions and 12 deletions

View File

@ -12,11 +12,13 @@
namespace Kakoune 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_name(std::move(name)), m_flags(flags | Flags::NoUndo),
m_history(), m_history_cursor(m_history.begin()), m_history(), m_history_cursor(m_history.begin()),
m_last_save_undo_index(0), m_last_save_undo_index(0),
m_timestamp(0), m_timestamp(0),
m_fs_timestamp(fs_timestamp),
m_hooks(GlobalHooks::instance()), m_hooks(GlobalHooks::instance()),
m_options(GlobalOptions::instance()) m_options(GlobalOptions::instance())
{ {
@ -44,7 +46,10 @@ Buffer::Buffer(String name, Flags flags, std::vector<String> lines)
if (flags & Flags::New) if (flags & Flags::New)
m_hooks.run_hook("BufNew", m_name, context); m_hooks.run_hook("BufNew", m_name, context);
else else
{
kak_assert(m_fs_timestamp != InvalidTime);
m_hooks.run_hook("BufOpen", m_name, context); m_hooks.run_hook("BufOpen", m_name, context);
}
} }
m_hooks.run_hook("BufCreate", m_name, context); m_hooks.run_hook("BufCreate", m_name, context);
@ -607,6 +612,7 @@ void Buffer::notify_saved()
++m_timestamp; ++m_timestamp;
m_last_save_undo_index = history_cursor_index; m_last_save_undo_index = history_cursor_index;
} }
m_fs_timestamp = get_fs_timestamp(m_name);
} }
BufferCoord Buffer::advance(BufferCoord coord, ByteCount count) const BufferCoord Buffer::advance(BufferCoord coord, ByteCount count) const

View File

@ -17,6 +17,8 @@ namespace Kakoune
class Buffer; class Buffer;
constexpr time_t InvalidTime = 0;
struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount> struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount>
{ {
constexpr BufferCoord(LineCount line = 0, ByteCount column = 0) constexpr BufferCoord(LineCount line = 0, ByteCount column = 0)
@ -90,7 +92,8 @@ public:
NoUndo = 8, 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(const Buffer&) = delete;
Buffer& operator= (const Buffer&) = delete; Buffer& operator= (const Buffer&) = delete;
~Buffer(); ~Buffer();

View File

@ -147,9 +147,6 @@ void write_buffer(CommandParameters params, Context& context)
: parse_filename(params[0]); : parse_filename(params[0]);
write_buffer_to_file(buffer, filename); write_buffer_to_file(buffer, filename);
if (filename == buffer.name())
buffer.notify_saved();
} }
void write_all_buffers(CommandParameters params, Context& context) 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()) for (auto& buffer : BufferManager::instance())
{ {
if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified()) if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified())
{
write_buffer_to_file(*buffer, buffer->name()); write_buffer_to_file(*buffer, buffer->name());
buffer->notify_saved();
}
} }
} }

View File

@ -175,8 +175,8 @@ Buffer* create_buffer_from_file(String filename)
else else
pos = line_end + 1; pos = line_end + 1;
} }
Buffer* buffer = new Buffer(filename, Buffer::Flags::File, std::move(lines)); Buffer* buffer = new Buffer{filename, Buffer::Flags::File,
buffer->set_fs_timestamp(st.st_mtime); std::move(lines), st.st_mtime};
OptionManager& options = buffer->options(); OptionManager& options = buffer->options();
options.get_local_option("eolformat").set<String>(crlf ? "crlf" : "lf"); 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); write(fd, eoldata, filename);
} }
if ((buffer.flags() & Buffer::Flags::File) and filename == buffer.name()) 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) String find_file(const String& filename, memoryview<String> paths)
@ -312,7 +312,7 @@ time_t get_fs_timestamp(const String& filename)
{ {
struct stat st; struct stat st;
if (stat(filename.c_str(), &st) != 0) if (stat(filename.c_str(), &st) != 0)
throw runtime_error("stat failed on " + filename); return InvalidTime;
return st.st_mtime; return st.st_mtime;
} }