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

fix: Ensure nameless FSEntry cannot be constructed #2989

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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
16 changes: 14 additions & 2 deletions source/dub/internal/io/mockfs.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ public final class MockFS : Filesystem {
import std.algorithm.iteration : reduce;

const abs = path.absolute();
auto segments = path.bySegment;
// `library-nonet` (using vibe.d) has an empty front for absolute path,
// while our built-in module (in vibecompat) does not.
if (abs && segments.front.name.length == 0) segments.popFront();
reduce!((FSEntry dir, segment) => dir.mkdir(segment.name))(
(abs ? this.root : this.cwd), path.bySegment);
(abs ? this.root : this.cwd), segments);
}

/// Ditto
Expand Down Expand Up @@ -276,11 +280,15 @@ public final class MockFS : Filesystem {
import std.algorithm.iteration : reduce;

const abs = path.absolute();
auto segments = path.bySegment;
// `library-nonet` (using vibe.d) has an empty front for absolute path,
// while our built-in module (in vibecompat) does not.
if (abs && segments.front.name.length == 0) segments.popFront();
// Casting away constness because no good way to do this with `inout`,
// but `FSEntry.lookup` is `inout` too.
return cast(inout(FSEntry)) reduce!(
(FSEntry dir, segment) => dir ? dir.lookup(segment.name) : null)
(cast() (abs ? this.root : this.cwd), path.bySegment);
(cast() (abs ? this.root : this.cwd), segments);
}
}

Expand Down Expand Up @@ -333,6 +341,10 @@ public class FSEntry
import std.datetime.date;
SysTime DefaultTime = SysTime(DateTime(2020, 01, 01));

assert(n.length > 0,
"FSentry.this(%s, %s, %s) called with empty name"
.format(p.path(), t, n));

this.attributes.type = t;
this.parent = p;
this.name = n;
Expand Down
Loading