feat(core): fix segfault

This commit is contained in:
2025-04-23 17:41:51 +02:00
parent 490425bfd9
commit 8a39c36aa8
3 changed files with 71 additions and 50 deletions

View File

@@ -35,6 +35,7 @@ fn loadChildren(container: *Container, deinitTracker: *DeinitTracker) void {
var it = dir.iterate();
while (it.next() catch return) |entry| {
const child = container.item.fullName.getChild(entry.name, container.item.allocator) catch return;
if (deinitTracker.deinited) return;
container.children.append(child) catch return;
}
@@ -64,6 +65,67 @@ pub const LocalContentProvider = struct {
globalAllocator: std.mem.Allocator,
) GetItemsError!Item {
const stat = std.fs.cwd().statFile(fullName.path) catch return GetItemsError.NotExists;
return switch (stat.kind) {
.directory => blk: {
const container = try allocator.create(Container);
container.* = Container{
.children = std.ArrayList(FullName).init(allocator),
.childrenLoading = true,
.item = undefined,
};
const val: ItemEnum = .{
.container = container,
};
try initItem(
self,
&container.item,
val,
fullName,
allocator,
globalAllocator,
);
try self.threadPool.spawn(loadChildren, .{ container, container.item.deinitTracker });
break :blk container.item;
},
.file => blk: {
const element = try allocator.create(Element);
element.* = Element{
.item = undefined,
};
const val: ItemEnum = .{
.element = element,
};
try initItem(
self,
&element.item,
val,
fullName,
allocator,
globalAllocator,
);
break :blk element.item;
},
else => @panic(
"Unsupported file type\n",
),
};
}
fn initItem(
contentProvider: *LocalContentProvider,
item: *Item,
innerItem: ItemEnum,
fullName: FullName,
allocator: std.mem.Allocator,
globalAllocator: std.mem.Allocator,
) !void {
var deinitTracker = try globalAllocator.create(DeinitTracker);
deinitTracker.init(globalAllocator);
@@ -81,55 +143,18 @@ pub const LocalContentProvider = struct {
const nativePath = try allocator.alloc(u8, fullName.path.len);
@memcpy(nativePath, fullName.path);
var item = Item{
item.* = Item{
.allocator = allocator,
.provider = self.provider(),
.provider = contentProvider.provider(),
.deinitTracker = deinitTracker,
.name = name,
.displayName = displayName,
.fullName = .{ .path = fullName2 },
.nativePath = models.NativePath{ .path = nativePath },
.parent = null,
.item = undefined,
.item = innerItem,
.errors = std.ArrayList(models.Error).init(allocator),
};
const innerItem: ItemEnum = switch (stat.kind) {
.directory => blk: {
const container = try allocator.create(Container);
container.* = Container{
.children = std.ArrayList(FullName).init(allocator),
.childrenLoading = true,
.item = item,
};
const val: ItemEnum = .{
.container = container,
};
try self.threadPool.spawn(loadChildren, .{ container, deinitTracker });
break :blk val;
},
.file => blk: {
const element = try allocator.create(Element);
element.* = Element{
.item = item,
};
const val: ItemEnum = .{
.element = element,
};
break :blk val;
},
else => @panic(
"Unsupported file type\n",
),
};
item.item = innerItem;
return item;
}
pub fn provider(self: *LocalContentProvider) Provider {