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

Log file password change on DB rebuild added to prevent inconsistent state appearing. #1934

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
74 changes: 56 additions & 18 deletions LiteDB/Engine/Disk/DiskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ internal class DiskService : IDisposable
private readonly Lazy<DiskWriterQueue> _queue;

private IStreamFactory _dataFactory;
private readonly IStreamFactory _logFactory;
private IStreamFactory _logFactory;

private StreamPool _dataPool;
private readonly StreamPool _logPool;
private StreamPool _logPool;

private long _dataLength;
private long _logLength;
Expand Down Expand Up @@ -59,18 +59,8 @@ public DiskService(EngineSettings settings, int[] memorySegmentSizes)
var dummy = _dataPool.Writer.CanRead;
}

// get initial data file length
_dataLength = _dataFactory.GetLength() - PAGE_SIZE;

// get initial log file length
if (_logFactory.Exists())
{
_logLength = _logFactory.GetLength() - PAGE_SIZE;
}
else
{
_logLength = -PAGE_SIZE;
}
GetInitialDataFileLength();
GetInitialLogFileLength();
}

/// <summary>
Expand Down Expand Up @@ -209,9 +199,9 @@ public long GetLength(FileOrigin origin)
/// <summary>
/// Clear data file, close any data stream pool, change password and re-create data factory
/// </summary>
public void ChangePassword(string password, EngineSettings settings)
public bool ChangeDataFilePassword(string password, EngineSettings settings)
{
if (settings.Password == password) return;
if (settings.Password == password) return false;

// empty data file
this.SetLength(0, FileOrigin.Data);
Expand All @@ -230,8 +220,56 @@ public void ChangePassword(string password, EngineSettings settings)
// create stream pool
_dataPool = new StreamPool(_dataFactory, false);

// get initial data file length
_dataLength = -PAGE_SIZE;
GetInitialDataFileLength();

return true;
}

/// <summary>
/// Clear log file, close any data stream pool and re-create data factory with new password
/// </summary>
public void ChangeLogFilePassword(string password, EngineSettings settings)
{
// empty log file
this.SetLength(0, FileOrigin.Log);

// close all streams
_logPool.Dispose();

// delete data file
_logFactory.Delete();

// new logfile will be created with new password
_logFactory = settings.CreateLogFactory();

// create stream pool
_logPool = new StreamPool(_logFactory, true);

GetInitialLogFileLength();
}

private void GetInitialDataFileLength()
{
if (_dataFactory.Exists())
{
_dataLength = _dataFactory.GetLength() - PAGE_SIZE;
}
else
{
_dataLength = -PAGE_SIZE;
}
}

private void GetInitialLogFileLength()
{
if (_logFactory.Exists())
{
_logLength = _logFactory.GetLength() - PAGE_SIZE;
}
else
{
_logLength = -PAGE_SIZE;
}
}

#region Sync Read/Write operations
Expand Down
8 changes: 7 additions & 1 deletion LiteDB/Engine/Engine/Rebuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ public long Rebuild(RebuildOptions options)
this.RebuildContent(reader);

// change password (can be a problem if any error occurs after here)
bool isPasswordChanged = false;
if (options != null)
{
_disk.ChangePassword(options.Password, _settings);
isPasswordChanged = _disk.ChangeDataFilePassword(options.Password, _settings);
}

// do checkpoint
_walIndex.Checkpoint();

if (isPasswordChanged)
{
_disk.ChangeLogFilePassword(options.Password, _settings);
}

// override header page
_disk.Write(new[] { _header.UpdateBuffer() }, FileOrigin.Data);

Expand Down