Skip to content
Closed
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
4 changes: 2 additions & 2 deletions dub-lock.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"dependencies": {
"ae": {
"version": "0.0.3701",
"sha256": "0vf458cvr8mmvjr87g2i6yhn7yyz1cg4lbixf62b9qnszd88j2q2"
"version": "0.0.3870",
"sha256": "0p9gnygb0lq07wday1mmsh1b8ag43h73arjgzjfjxg0y3y1mrk8c"
},
"btrfs": {
"version": "0.0.21",
Expand Down
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ targetType "executable"

toolchainRequirements frontend=">=2.097"

dependency "ae" version="==0.0.3701"
dependency "ae" version="==0.0.3870"
dependency "btrfs" version="~>0.0.21"
dependency "ncurses" version="~>1.0.0"
dependency "emsi_containers" version="==0.9.0"
Expand Down
6 changes: 3 additions & 3 deletions dub.selections.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"fileVersion": 1,
"versions": {
"ae": "0.0.3701",
"ae": "0.0.3870",
"btrfs": "0.0.21",
"ncurses": "1.0.0",
"emsi_containers": "0.9.0"
"emsi_containers": "0.9.0",
"ncurses": "1.0.0"
}
}
68 changes: 52 additions & 16 deletions source/btdu/impexp.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import core.lifetime : move;

import ae.sys.data;
import ae.sys.datamm;
import ae.utils.json;
import ae.utils.serialization.json;

import btrfs.c.ioctl : btrfs_ioctl_fs_info_args;

Expand Down Expand Up @@ -91,8 +91,19 @@ void importData(string path)
// JSON format
// ============================================================================

/// Serialized
struct SerializedState
/// Serialized import state
struct ImportedSerializedState
{
bool expert;
@JSONOptional bool physical;
string fsPath;
@JSONOptional string fsid; /// UUID formatted as "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
ulong totalSize;
BrowserPath.SerializedForm root;
}

/// Serialized export state
struct ExportedSerializedState
{
bool expert;
@JSONOptional bool physical;
Expand Down Expand Up @@ -121,20 +132,39 @@ private typeof(btrfs_ioctl_fs_info_args.fsid) parseFsid(string s)
/// Load and parse an exported JSON file.
/// Returns the parsed state structure.
/// The memory-mapped file is stored in the output parameter to keep it alive.
private SerializedState loadExportFile(string path, out Data mmapData)
private ImportedSerializedState loadExportFile(string path, out Data mmapData)
{
mmapData = mapFile(path, MmMode.read);
auto json = cast(string)mmapData.unsafeContents;

debug importing = true;
scope(exit) debug importing = false;
return json.jsonParse!SerializedState();
return json.jsonParse!ImportedSerializedState();
}

/// Keep memory-mapped files alive, as directory names may reference them
private __gshared Data importMmapData;
private __gshared Data compareMmapData;

private struct JsonOutputAdapter(Output)
{
Output output;

void put(T...)(T args)
{
static foreach (arg; args)
output.put(arg);
}

static if (__traits(compiles, { Output output; output.get(); }))
{
auto get()
{
return output.get();
}
}
}

bool isJsonFormat(string path)
{
import std.file : read;
Expand All @@ -158,7 +188,10 @@ void importJson(string path)
fsPath = s.fsPath;
fsid = parseFsid(s.fsid);
totalSize = s.totalSize;
move(*s.root, browserRoot);
debug importing = true;
scope(exit) debug importing = false;
auto importedRoot = BrowserPath.fromJSON(s.root);
move(importedRoot, browserRoot);

browserRoot.resetParents();
imported = true;
Expand Down Expand Up @@ -194,7 +227,10 @@ private void importCompareJson(string path)
compareTotalSize = s.totalSize;
compareFsid = parseFsid(s.fsid);
// Note: we don't set fsPath from compare - keep current fsPath
move(*s.root, compareRoot);
debug importing = true;
scope(exit) debug importing = false;
auto importedCompareRoot = BrowserPath.fromJSON(s.root);
move(importedCompareRoot, compareRoot);

compareRoot.resetParents();
compareMode = true;
Expand All @@ -221,7 +257,7 @@ void exportData(string path, ExportFormat fmt = ExportFormat.json)

private void exportJson(string path)
{
SerializedState s;
ExportedSerializedState s;
s.expert = expert;
s.physical = physical;
s.fsPath = fsPath;
Expand All @@ -230,14 +266,14 @@ private void exportJson(string path)
s.root = browserRootPtr;

alias LockingBinaryWriter = typeof(File.lockingBinaryWriter());
alias JsonFileSerializer = CustomJsonSerializer!(JsonWriter!LockingBinaryWriter);

{
JsonFileSerializer j;
auto file = path is null ? stdout : File(path, "wb");
j.writer.output = file.lockingBinaryWriter;
j.put(s);
}
alias OutputAdapter = JsonOutputAdapter!LockingBinaryWriter;

auto file = path is null ? stdout : File(path, "wb");
OutputAdapter output;
output.output = file.lockingBinaryWriter;
JsonWriter!OutputAdapter writer;
writer.output = output;
JsonCustomSerializer.Impl!Object.read(&writer, s);
}

// ============================================================================
Expand Down
20 changes: 15 additions & 5 deletions source/btdu/paths.d
Original file line number Diff line number Diff line change
Expand Up @@ -1286,15 +1286,24 @@ struct BrowserPath
@JSONOptional Nullable!bool mark;
@JSONOptional ulong[string] seenAs; // Map: path -> sample count

SerializedForm[] children;
}

struct ExportSerializedForm
{
string name;
SerializedForm.SerializedData data;
@JSONOptional Nullable!bool mark;
@JSONOptional ulong[string] seenAs; // Map: path -> sample count
BrowserPath*[] children;
}

SerializedForm toJSON()
ExportSerializedForm toJSON()
{
import std.conv : to;
import btdu.state : exportSeenAs;

SerializedForm s;
ExportSerializedForm s;
s.name = this.name[];
for (auto p = firstChild; p; p = p.nextSibling)
s.children ~= p;
Expand Down Expand Up @@ -1323,10 +1332,11 @@ struct BrowserPath
import std.conv : to;

auto p = BrowserPath(null, NameString(s.name));
foreach_reverse (child; s.children)
foreach_reverse (ref child; s.children)
{
child.nextSibling = p.firstChild;
p.firstChild = child;
auto parsedChild = growAllocator.make!BrowserPath(BrowserPath.fromJSON(child));
parsedChild.nextSibling = p.firstChild;
p.firstChild = parsedChild;
}
auto aggData = p.ensureAggregateData();
static foreach (sampleType; EnumMembers!SampleType)
Expand Down