diff --git a/dub-lock.json b/dub-lock.json index 97205eb..a145180 100644 --- a/dub-lock.json +++ b/dub-lock.json @@ -1,8 +1,8 @@ { "dependencies": { "ae": { - "version": "0.0.3701", - "sha256": "0vf458cvr8mmvjr87g2i6yhn7yyz1cg4lbixf62b9qnszd88j2q2" + "version": "0.0.3870", + "sha256": "0p9gnygb0lq07wday1mmsh1b8ag43h73arjgzjfjxg0y3y1mrk8c" }, "btrfs": { "version": "0.0.21", diff --git a/dub.sdl b/dub.sdl index cd2d31e..347af2a 100644 --- a/dub.sdl +++ b/dub.sdl @@ -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" diff --git a/dub.selections.json b/dub.selections.json index 054b640..67e0dad 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -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" } } diff --git a/source/btdu/impexp.d b/source/btdu/impexp.d index cebef85..43a680a 100644 --- a/source/btdu/impexp.d +++ b/source/btdu/impexp.d @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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); } // ============================================================================ diff --git a/source/btdu/paths.d b/source/btdu/paths.d index 6cebd4c..ca291cb 100644 --- a/source/btdu/paths.d +++ b/source/btdu/paths.d @@ -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; @@ -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)