in Sources/TSFCASFileTree/Internal/FileSegmenter.swift [113:161]
init(importPath: AbsolutePath, _ path: AbsolutePath, segmentSize: Int, minMmapSize: Int, allowInconsistency: Bool) throws {
assert(segmentSize >= 1, "Too small segment size to split files")
assert(minMmapSize >= 4096, "Too small minimum size for mmap(2)")
let segmentSize = max(segmentSize, 1)
let minMmapSize = max(minMmapSize, 1)
var fd = try LLBFutureFileSystem.openImpl(path.pathString, flags: O_RDONLY | O_NOFOLLOW)
defer { if fd >= 0 { close(fd) } }
var sb = stat()
guard fstat(fd, &sb) != -1 else {
throw FileSystemError(errno: errno, path)
}
guard (sb.st_mode & S_IFMT) == S_IFREG else {
throw FileSystemError(.ioError(code: 0), path)
}
self.importPath = importPath
self.path = path
self.statInfo = sb
self.segmentSize = segmentSize
self.allowInconsistency = allowInconsistency
let reportedSize = Int(sb.st_size)
// For relatively small size, just read them in memory.
guard reportedSize >= minMmapSize else {
if reportedSize == 0 {
// Do not stash fds for empty files.
self.reuseFD = .init(value: -1)
} else {
self.reuseFD = .init(value: fd)
fd = -1
}
self.mappedAt = nil
return
}
// This is a large file, mmap it and retain the mapping until EOL.
let mmapReturnValue = mmap(nil, reportedSize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0)
guard let basePointer = mmapReturnValue, basePointer != MAP_FAILED else {
throw FileSystemError(errno: errno, path)
}
posix_madvise(basePointer, reportedSize, POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED)
self.reuseFD = .init(value: -1)
self.mappedAt = basePointer
}