Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses issue of upgrading from v4.x databases in LiteDB versions 5.0.18-5.0.20 #2499

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions LiteDB.Tests/Issues/Issue2494_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xunit;

namespace LiteDB.Tests.Issues;

public class Issue2494_Tests
{
[Fact]
public static void Test()
{
var original = "../../../Resources/Issue_2494_EncryptedV4.db";
using var filename = new TempFile(original);

var connectionString = new ConnectionString(filename)
{
Password = "pass123",
Upgrade = true,
};

using (var db = new LiteDatabase(connectionString)) // <= throws as of version 5.0.18
{
var col = db.GetCollection<PlayerDto>();
col.FindAll();
}
}

public class PlayerDto
{
[BsonId]
public Guid Id { get; set; }

public string Name { get; set; }

public PlayerDto(Guid id, string name)
{
Id = id;
Name = name;
}

public PlayerDto()
{
}
}
}
Binary file not shown.
6 changes: 4 additions & 2 deletions LiteDB/Engine/Disk/StreamFactory/FileStreamFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ internal class FileStreamFactory : IStreamFactory
private readonly string _password;
private readonly bool _readonly;
private readonly bool _hidden;
private readonly bool _useAesStream;

public FileStreamFactory(string filename, string password, bool readOnly, bool hidden)
public FileStreamFactory(string filename, string password, bool readOnly, bool hidden, bool useAesStream = true)
{
_filename = filename;
_password = password;
_readonly = readOnly;
_hidden = hidden;
_useAesStream = useAesStream;
}

/// <summary>
Expand Down Expand Up @@ -57,7 +59,7 @@ public Stream GetStream(bool canWrite, bool sequencial)
File.SetAttributes(_filename, FileAttributes.Hidden);
}

return _password == null ? (Stream)stream : new AesStream(_password, stream);
return _password == null || !_useAesStream ? (Stream)stream : new AesStream(_password, stream);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/EngineSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class EngineSettings
/// <summary>
/// Create new IStreamFactory for datafile
/// </summary>
internal IStreamFactory CreateDataFactory()
internal IStreamFactory CreateDataFactory(bool useAesStream = true)
{
if (this.DataStream != null)
{
Expand All @@ -85,7 +85,7 @@ internal IStreamFactory CreateDataFactory()
}
else if (!string.IsNullOrEmpty(this.Filename))
{
return new FileStreamFactory(this.Filename, this.Password, this.ReadOnly, false);
return new FileStreamFactory(this.Filename, this.Password, this.ReadOnly, false, useAesStream);
}

throw new ArgumentException("EngineSettings must have Filename or DataStream as data source");
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/FileReader/FileReaderV7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public FileReaderV7(EngineSettings settings)

public void Open()
{
var streamFactory = _settings.CreateDataFactory();
var streamFactory = _settings.CreateDataFactory(false);

// open datafile from stream factory
_stream = streamFactory.GetStream(true, true);
Expand Down Expand Up @@ -437,7 +437,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_stream.Dispose();
_stream?.Dispose();
_aes?.Dispose();
}

Expand Down
17 changes: 11 additions & 6 deletions LiteDB/Engine/Services/RebuildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ public RebuildService(EngineSettings settings)
{
_settings = settings;

// test for prior version
var bufferV7 = this.ReadFirstBytes(false);
if (FileReaderV7.IsVersion(bufferV7))
{
_fileVersion = 7;
return;
}

// open, read first 16kb, and close data file
var buffer = this.ReadFirstBytes();

// test for valid reader to use
_fileVersion =
FileReaderV7.IsVersion(buffer) ? 7 :
FileReaderV8.IsVersion(buffer) ? 8 : throw LiteException.InvalidDatabase();

_fileVersion = FileReaderV8.IsVersion(buffer) ? 8 : throw LiteException.InvalidDatabase();
}

public long Rebuild(RebuildOptions options)
Expand Down Expand Up @@ -109,10 +114,10 @@ public long Rebuild(RebuildOptions options)
/// <summary>
/// Read first 16kb (2 PAGES) in bytes
/// </summary>
private byte[] ReadFirstBytes()
private byte[] ReadFirstBytes(bool useAesStream = true)
{
var buffer = new byte[PAGE_SIZE * 2];
var factory = _settings.CreateDataFactory();
var factory = _settings.CreateDataFactory(useAesStream);

using (var stream = factory.GetStream(false, true))
{
Expand Down