message: Shrink file if logical size is less than file size
diff: @@ -766,14 +766,16 @@ ref_type SlabAlloc::attach_file(const std::string& file_path, Config& cfg)
  auto header = reinterpret_cast<const Header*>(map_header.get_addr());
  auto footer = reinterpret_cast<const StreamingFooter*>(map_footer.get_addr() + footer_offset);
  top_ref = validate_header(header, footer, size, path); // Throws
  +
  m_attach_mode = cfg.is_shared ? attach_SharedFile : attach_UnsharedFile;
  m_data = map_header.get_addr(); // <-- needed below
  + if (cfg.session_initiator) {
  // Make sure the database is not on streaming format. If we did not do this,
  // a later commit would have to do it. That would require coordination with
  // anybody concurrently joining the session, so it seems easier to do it at
  // session initialization, even if it means writing the database during open.
  - if (cfg.session_initiator && is_file_on_streaming_form(*header)) {
  + if (is_file_on_streaming_form(*header)) {
  // Don't compare file format version fields as they are allowed to differ.
  // Also don't compare reserved fields.
  REALM_ASSERT_EX(header->m_flags == 0, header->m_flags, get_file_path_for_assertions());
  @@ -806,6 +808,24 @@ ref_type SlabAlloc::attach_file(const std::string& file_path, Config& cfg)
  realm::util::encryption_read_barrier(map_header, 0, sizeof(Header));
  }
  }
  + else if (top_ref) {
  + // Check if file size can be reduced
  + constexpr size_t file_size_ndx = 2;
  + constexpr size_t max_top_size = (file_size_ndx + 1) * 8 + sizeof(Header);
  + size_t top_page_base = top_ref & ~(page_size() - 1);
  + size_t top_offset = top_ref - top_page_base;
  + File::Map<char> map_top(m_file, top_page_base, File::access_ReadOnly, max_top_size + top_offset, 0);
  + realm::util::encryption_read_barrier(map_top, top_offset, max_top_size);
  + auto top_header = map_top.get_addr() + top_offset;
  + auto top_data = NodeHeader::get_data_from_header(top_header);
  + auto w = NodeHeader::get_width_from_header(top_header);
  + auto logical_size = size_t(get_direct(top_data, w, file_size_ndx)) >> 1;
  + if (logical_size < size) {
  + m_file.resize(logical_size);
  + size = logical_size;
  + }
  + }
  + }
  }
  catch (const DecryptionFailed&) {
  note_reader_end(this);
  