io/files/internal/Traversal.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
073ad67206d075be3e6b15fcc31111f65d4275db- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
//! Private bounded traversal mechanics of `std:io/files`. Pure source: every
//! Filesystem observation or effect is an explicit closure argument supplied
//! by the library module, so nothing here reaches authority by itself.
//!
//! Traversal is iterative over an explicit linked stack with exact Integer
//! counters. The root has depth zero and every encountered entry, the root
//! included, is counted; an entry deeper than `maxDepth` or a count above
//! `maxEntries` signals a fresh IOError instead of truncating. Only entries
//! reported as directories are descended into, through a fresh confined
//! `entries` observation of their exact Path; links and other entries are
//! never followed. Child Paths are built from exact listed component names.
//! Concurrent namespace changes may affect later observations: no snapshot or
//! stable identity is claimed.
failed: () => {
failure: IOError {}
failure.signal()
}
invalid: () => {
failure: InvalidIOArgument {}
failure.signal()
}
// Answers a fresh Array of `size` null placeholders in O(size).
sized: null
sized = (size) => {
result: Array()
if(size > 0) {
half: sized(size.div(2))
result = Array(...half, ...half)
if((size % 2) == 1) {
result = Array(...result, null)
}
}
result
}
// Copies the `count` values of a reversed linked list into an Array in
// insertion order.
listArray: (last, count) => {
result: sized(count)
node: last
index: count - 1
while(index >= 0) {
result[index] = node.value
node = node.next
index = index - 1
}
result
}
// Answers one budget admission closure over fresh exact counters.
budget: (maxDepth, maxEntries) => {
count: 0
(depth) => {
count = count + 1
if(count > maxEntries) {
failed()
}
if(depth > maxDepth) {
failed()
}
}
}
/// Preorder walk from `root`, root included. `visit(path, kind)` runs in the
/// calling Task and must answer exactly true (continue) or false (stop at
/// once, successfully); any other result, a Future included, signals a fresh
/// InvalidIOArgument. A missing root signals IOError.
this.walk: (metadataOf, entriesOf, filesystem, root, visit, maxDepth, maxEntries) => {
admit: budget(maxDepth, maxEntries)
rootMetadata: metadataOf(filesystem, root)
if(rootMetadata === null) {
failed()
}
admit(0)
stack: {
path: root
kind: rootMetadata.kind
depth: 0
next: null
}
running: true
while(running && (stack !== null)) {
node: stack
stack = node.next
kind: node.kind
if(node.depth != 0) {
observed: metadataOf(filesystem, node.path)
if(observed === null) {
failed()
}
kind = observed.kind
}
answer: visit(node.path, kind)
if(answer === false) {
running = false
} else {
if(answer !== true) {
invalid()
}
if(kind == "directory") {
children: entriesOf(filesystem, node.path)
childDepth: node.depth + 1
// Push in reverse so the first listed child is visited first.
index: children.size() - 1
while(index >= 0) {
entry: children[index]
admit(childDepth)
stack = {
path: node.path.child(entry.name)
kind: entry.kind
depth: childDepth
next: stack
}
index = index - 1
}
}
}
}
null
}
/// Answers the bounded copy plan of the directory `root`: a fresh Array of
/// frozen `{ relative, kind }` records, parents before children, root
/// excluded. `relative` is the exact component sequence below `root`. Only
/// directories and regular files are admitted; any other kind signals IOError.
this.plan: (entriesOf, filesystem, root, maxDepth, maxEntries) => {
admit: budget(maxDepth, maxEntries)
admit(0)
stack: {
path: root
relative: Path.relative()
depth: 0
next: null
}
planned: null
plannedCount: 0
while(stack !== null) {
node: stack
stack = node.next
childDepth: node.depth + 1
for(entry in entriesOf(filesystem, node.path)) {
admit(childDepth)
kind: entry.kind
if((kind != "directory") && (kind != "regular")) {
failed()
}
relative: node.relative.child(entry.name)
item: {
relative: relative
kind: kind
}
planned = {
value: item.freeze()
next: planned
}
plannedCount = plannedCount + 1
if(kind == "directory") {
stack = {
path: node.path.child(entry.name)
relative: relative
depth: childDepth
next: stack
}
}
}
}
listArray(planned, plannedCount)
}
/// Removes `root` and everything below it in postorder through `removeOf`,
/// one exact entry at a time. Links and other entries are removed as
/// themselves and never descended into. A missing root signals IOError;
/// completed removals remain after a failure.
this.removeTree: (metadataOf, entriesOf, removeOf, filesystem, root, maxDepth, maxEntries) => {
admit: budget(maxDepth, maxEntries)
rootMetadata: metadataOf(filesystem, root)
if(rootMetadata === null) {
failed()
}
admit(0)
stack: {
path: root
kind: rootMetadata.kind
depth: 0
expanded: false
next: null
}
while(stack !== null) {
node: stack
stack = node.next
if((node.kind == "directory") && (node.expanded === false)) {
// Revisit this directory after all of its children were removed.
stack = {
path: node.path
kind: node.kind
depth: node.depth
expanded: true
next: stack
}
childDepth: node.depth + 1
for(entry in entriesOf(filesystem, node.path)) {
admit(childDepth)
stack = {
path: node.path.child(entry.name)
kind: entry.kind
depth: childDepth
expanded: false
next: stack
}
}
} else {
removeOf(filesystem, node.path)
}
}
null
}
/// Answers a fresh Array of every descendant Path of the directory `root`
/// matched by the compiled pattern, each exactly once, root excluded. A
/// directory is descended into only while some pattern position can still
/// match one of its descendants.
this.glob: (Glob, entriesOf, filesystem, root, compiled, maxDepth, maxEntries) => {
admit: budget(maxDepth, maxEntries)
admit(0)
stack: {
path: root
marks: Glob.start(compiled)
depth: 0
next: null
}
results: null
resultCount: 0
while(stack !== null) {
node: stack
stack = node.next
childDepth: node.depth + 1
for(entry in entriesOf(filesystem, node.path)) {
admit(childDepth)
marks: Glob.step(compiled, node.marks, entry.name)
childPath: node.path.child(entry.name)
if(Glob.matches(compiled, marks)) {
results = {
value: childPath
next: results
}
resultCount = resultCount + 1
}
if((entry.kind == "directory") && Glob.viable(compiled, marks)) {
stack = {
path: childPath
marks: marks
depth: childDepth
next: stack
}
}
}
}
listArray(results, resultCount)
}