feat: base project
This commit is contained in:
141
src/core/provider/local.zig
Normal file
141
src/core/provider/local.zig
Normal file
@@ -0,0 +1,141 @@
|
||||
const std = @import("std");
|
||||
const models = @import("../models.zig");
|
||||
const Provider = @import("provider.zig").Provider;
|
||||
const ProviderVTable = @import("provider.zig").VTable;
|
||||
const GetItemsError = @import("provider.zig").GetItemsError;
|
||||
const DeinitTracker = @import("../deinit.zig").DeinitTracker;
|
||||
|
||||
const FullName = models.FullName;
|
||||
const Item = models.Item;
|
||||
const ItemEnum = models.ItemEnum;
|
||||
const Element = models.Element;
|
||||
const Container = models.Container;
|
||||
|
||||
fn loadChildren(container: *Container, deinitTracker: *DeinitTracker) void {
|
||||
defer {
|
||||
if (!deinitTracker.deinited)
|
||||
container.childrenLoading = false;
|
||||
}
|
||||
|
||||
deinitTracker.use() catch {
|
||||
std.debug.print("already deinitialized", .{});
|
||||
return;
|
||||
};
|
||||
defer deinitTracker.unuse();
|
||||
|
||||
var dir = std.fs.cwd().openDir(container.item.nativePath.path, .{ .iterate = true }) catch {
|
||||
if (deinitTracker.deinited) return;
|
||||
|
||||
const errorContent = std.fmt.allocPrint(container.item.allocator, "Could not open directory '{s}'.", .{container.item.nativePath.path}) catch return;
|
||||
container.item.errors.append(.{ .content = errorContent }) catch return;
|
||||
return;
|
||||
};
|
||||
defer dir.close();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
const VTable: ProviderVTable = .{
|
||||
.getItemByFullName = getItemByFullNameGeneric,
|
||||
};
|
||||
|
||||
pub fn getItemByFullNameGeneric(
|
||||
context: *anyopaque,
|
||||
fullName: FullName,
|
||||
allocator: std.mem.Allocator,
|
||||
globalAllocator: std.mem.Allocator,
|
||||
) GetItemsError!Item {
|
||||
const self: *LocalContentProvider = @ptrCast(@alignCast(context));
|
||||
return self.getItemByFullName(fullName, allocator, globalAllocator);
|
||||
}
|
||||
|
||||
pub const LocalContentProvider = struct {
|
||||
threadPool: *std.Thread.Pool,
|
||||
|
||||
pub fn getItemByFullName(
|
||||
self: *LocalContentProvider,
|
||||
fullName: FullName,
|
||||
allocator: std.mem.Allocator,
|
||||
globalAllocator: std.mem.Allocator,
|
||||
) GetItemsError!Item {
|
||||
const stat = std.fs.cwd().statFile(fullName.path) catch return GetItemsError.NotExists;
|
||||
var deinitTracker = try globalAllocator.create(DeinitTracker);
|
||||
deinitTracker.init(globalAllocator);
|
||||
|
||||
const basename = std.fs.path.basename(fullName.path);
|
||||
|
||||
const name = try allocator.alloc(u8, basename.len);
|
||||
@memcpy(name, basename);
|
||||
|
||||
const displayName = try allocator.alloc(u8, basename.len);
|
||||
@memcpy(displayName, basename);
|
||||
|
||||
const fullName2 = try allocator.alloc(u8, fullName.path.len);
|
||||
@memcpy(fullName2, fullName.path);
|
||||
|
||||
const nativePath = try allocator.alloc(u8, fullName.path.len);
|
||||
@memcpy(nativePath, fullName.path);
|
||||
|
||||
var item = Item{
|
||||
.allocator = allocator,
|
||||
.provider = self.provider(),
|
||||
.deinitTracker = deinitTracker,
|
||||
.name = name,
|
||||
.displayName = displayName,
|
||||
.fullName = .{ .path = fullName2 },
|
||||
.nativePath = models.NativePath{ .path = nativePath },
|
||||
.parent = null,
|
||||
.item = undefined,
|
||||
.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 {
|
||||
return Provider{
|
||||
.object = self,
|
||||
.vtable = &VTable,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user