io/files/internal/TemporaryNames.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
1525135d9adb215c4be4135525195d0c8f9d72aa
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 temporary-name admission and construction of `std:io/files`.
//! Pure source except for the one explicit Entropy request: no Filesystem or
//! other authority is reachable from here and every input is explicit.
//!
//! A temporary name is `prefix`, then exactly 32 lowercase hexadecimal
//! characters encoding 16 random octets, then `suffix`. The complete name
//! must be exactly one portable Path component; fragments are never trimmed,
//! normalized, interpreted or split.

Paths: import("std:io/paths")

randomOctets: 16
alphabet: "0123456789abcdef"
placeholder: "00000000000000000000000000000000"

invalid: () => {
    failure: InvalidIOArgument {}
    failure.signal()
}

failed: () => {
    failure: IOError {}
    failure.signal()
}

// The one-component Path of `name`, or a fresh InvalidIOArgument. Portable
// parsing already refuses slash, backslash, colon, NUL, `.` and `..`.
componentPath: (name) => {
    parsed: Error.handle(
        () => Paths.parse(name),
        (ignored) => null
    )
    if(parsed === null) {
        invalid()
    }
    if(Paths.components(parsed).size() != 1) {
        invalid()
    }
    parsed
}

/// Admits both naming fragments by validating the complete name shape with a
/// 32-character hexadecimal placeholder, before any Entropy is requested.
this.require: (prefix, suffix) => {
    (String.recognizes(prefix) && String.recognizes(suffix)).ifFalse(() => {
        invalid()
    })
    componentPath(prefix + placeholder + suffix)
    null
}

/// Requests exactly 16 octets from the explicit `entropy` and answers the
/// Path of `parent` followed by the one generated name. Every octet becomes
/// exactly two lowercase hexadecimal characters, so leading zeroes are kept.
this.child: (parent, entropy, prefix, suffix) => {
    octets: entropy.bytes(randomOctets).value()
    if(octets.size() != randomOctets) {
        failed()
    }
    hex: ""
    index: 0
    while(index < randomOctets) {
        octet: octets[index]
        hex = hex + alphabet[octet.div(16)] + alphabet[octet % 16]
        index = index + 1
    }
    Paths.join(parent, componentPath(prefix + hex + suffix))
}