Skip to content

Commit

Permalink
Merge pull request CleverRaven#70688 from akrieger/windoze
Browse files Browse the repository at this point in the history
Fix flexbuffer cache post-c++-17 migration
  • Loading branch information
Maleclypse authored Jan 6, 2024
2 parents 9464405 + f0e26cd commit ab85743
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/flexbuffer_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ void try_find_and_throw_json_error( TextJsonValue &jv )
}
}

fs::file_time_type get_file_mtime_millis( const fs::path &path, std::error_code &ec )
{
fs::file_time_type ret = fs::last_write_time( path, ec );
if( ec ) {
return ret;
}
// Truncate to nearest millisecond.
ret = fs::file_time_type( std::chrono::milliseconds(
std::chrono::duration_cast<std::chrono::milliseconds>( ret.time_since_epoch() ).count() ) );
return ret;
}

std::vector<uint8_t> parse_json_to_flexbuffer_(
const char *buffer,
const char *source_filename_opt ) noexcept( false )
Expand Down Expand Up @@ -162,7 +174,7 @@ struct file_flexbuffer : parsed_flexbuffer {

bool is_stale() const override {
std::error_code ec;
fs::file_time_type mtime = fs::last_write_time( source_file_path_, ec );
fs::file_time_type mtime = get_file_mtime_millis( source_file_path_, ec );
if( ec ) {
// Assume yes out of date.
return true;
Expand Down Expand Up @@ -296,7 +308,11 @@ class flexbuffer_disk_cache
return storage;
}

fs::file_time_type source_mtime = fs::last_write_time( lexically_normal_json_source_path );
std::error_code ec;
fs::file_time_type source_mtime = get_file_mtime_millis( lexically_normal_json_source_path, ec );
if( ec ) {
return storage;
}

// Does the source file's mtime match what we cached previously
if( source_mtime != disk_entry->second.mtime ) {
Expand All @@ -322,13 +338,14 @@ class flexbuffer_disk_cache
const std::vector<uint8_t> &flexbuffer_binary ) {
std::error_code ec;
std::string json_source_path_string = lexically_normal_json_source_path.u8string();
fs::file_time_type mtime = fs::last_write_time( lexically_normal_json_source_path );
int64_t mtime_ms = std::chrono::duration_cast<std::chrono::milliseconds>
( mtime.time_since_epoch() ).count();
fs::file_time_type mtime = get_file_mtime_millis( lexically_normal_json_source_path, ec );
if( ec ) {
return false;
}

int64_t mtime_ms = std::chrono::duration_cast<std::chrono::milliseconds>
( mtime.time_since_epoch() ).count();

// Doing some variable reuse to avoid copies.
fs::path flexbuffer_path = ( cache_path_ / lexically_normal_json_source_path.lexically_relative(
root_path_ ) ).remove_filename();
Expand Down Expand Up @@ -396,7 +413,8 @@ std::shared_ptr<parsed_flexbuffer> flexbuffer_cache::parse( fs::path json_source

std::error_code ec;
// If we got this far we can get the mtime.
fs::file_time_type mtime = fs::last_write_time( json_source_path, ec );
fs::file_time_type mtime = get_file_mtime_millis( json_source_path, ec );
( void )ec;

return std::make_shared<file_flexbuffer>(
std::move( storage ),
Expand All @@ -415,10 +433,8 @@ std::shared_ptr<parsed_flexbuffer> flexbuffer_cache::parse_and_cache(
lexically_normal_json_source_path );
if( cached_storage ) {
std::error_code ec;
fs::file_time_type mtime = fs::last_write_time( lexically_normal_json_source_path, ec );
if( ec ) {
// Whatever.
}
fs::file_time_type mtime = get_file_mtime_millis( lexically_normal_json_source_path, ec );
( void )ec;

return std::make_shared<file_flexbuffer>( std::move( cached_storage ),
std::move( lexically_normal_json_source_path ), mtime, offset );
Expand Down

0 comments on commit ab85743

Please sign in to comment.