clircle-0.6.1/.cargo_vcs_info.json0000644000000001360000000000100124550ustar { "git": { "sha1": "8a9ed6717f2363014b152b35c1ee3785a346bf2e" }, "path_in_vcs": "" }clircle-0.6.1/.github/workflows/ci.yml000064400000000000000000000044621046102023000157660ustar 00000000000000name: CI on: push: branches: - main pull_request: jobs: build: name: Build, test, and lint strategy: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] rust: ["1.69.0", stable] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} components: clippy - run: cargo build - run: cargo test -F test-close-again -- --test-threads 1 - run: cargo clippy build_extra: name: Build on extra platforms runs-on: ubuntu-latest strategy: fail-fast: false matrix: target: - aarch64-linux-android - aarch64-unknown-linux-gnu - arm-linux-androideabi - arm-unknown-linux-gnueabihf - i586-unknown-linux-gnu - i686-linux-android - i686-unknown-linux-gnu - x86_64-linux-android steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable with: targets: ${{ matrix.target }} - run: cargo build --target ${{ matrix.target }} minimal_versions: # Adapted from https://github.com/jonhoo/rust-ci-conf/blob/a13691528b3f1918594bfc53db8cf5ad0240c59e/.github/workflows/test.yml#L46 name: Build with minimal dependency versions strategy: matrix: os: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - uses: dtolnay/rust-toolchain@nightly - run: cargo +nightly update -Zminimal-versions - run: cargo +stable build --locked --all-features --all-targets semver: name: Check for semver breakage strategy: fail-fast: false matrix: os: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - uses: obi1kenobi/cargo-semver-checks-action@v2 with: verbose: true rustfmt: name: Check formatting runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable with: components: rustfmt - run: cargo fmt --all -- --check clircle-0.6.1/.gitignore000064400000000000000000000000231046102023000132300ustar 00000000000000/target Cargo.lock clircle-0.6.1/Cargo.lock0000644000000263750000000000100104450ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "bitflags" version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6776fc96284a0bb647b615056fc496d1fe1644a7ab01829818a6d91cae888b84" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cfg_aliases" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77e53693616d3075149f4ead59bdeecd204ac6b8192d8969757601b74bddf00f" [[package]] name = "clircle" version = "0.6.1" dependencies = [ "cfg-if", "nix", "serde", "serde_derive", "tempfile", "windows", ] [[package]] name = "errno" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" dependencies = [ "errno-dragonfly", "libc", "winapi", ] [[package]] name = "errno-dragonfly" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14ca354e36190500e1e1fb267c647932382b54053c50b14970856c0b00a35067" dependencies = [ "gcc", "libc", ] [[package]] name = "fastrand" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "779d043b6a0b90cc4c0ed7ee380a6504394cee7efd7db050e3774eee387324b2" dependencies = [ "instant", ] [[package]] name = "gcc" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e33f45ff9bef4a33df0e34df4d68ee016762d11f24e8d536e5e294096cc2b96" [[package]] name = "instant" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fba4f4144401d448848bda978e2a037f002e58438d5b89195352a773e8d874b" [[package]] name = "io-lifetimes" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7d367024b3f3414d8e01f437f704f41a9f64ab36f9067fa73e526ad4c763c87" [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "linux-raw-sys" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb68f22743a3fb35785f1e7f844ca5a3de2dde5bd0c0ef5b372065814699b121" [[package]] name = "nix" version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" dependencies = [ "bitflags 2.3.1", "cfg-if", "cfg_aliases", "libc", ] [[package]] name = "proc-macro2" version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] [[package]] name = "redox_syscall" version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" dependencies = [ "bitflags 1.2.1", ] [[package]] name = "rustix" version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce726fa480ae3aa26c84a43e524fee49ba760c3d40596e162319507152b445a0" dependencies = [ "bitflags 1.2.1", "errno", "io-lifetimes", "libc", "linux-raw-sys", "windows-sys", ] [[package]] name = "serde" version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" [[package]] name = "serde_derive" version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" dependencies = [ "proc-macro2", "quote", "syn 1.0.33", ] [[package]] name = "syn" version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd" dependencies = [ "proc-macro2", "quote", "unicode-xid", ] [[package]] name = "syn" version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tempfile" version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af18f7ae1acd354b992402e9ec5864359d693cd8a79dcbef59f76891701c1e95" dependencies = [ "cfg-if", "fastrand", "redox_syscall", "rustix", "windows-sys", ] [[package]] name = "unicode-ident" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" [[package]] name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows" version = "0.56.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1de69df01bdf1ead2f4ac895dc77c9351aefff65b2f3db429a343f9cbf05e132" dependencies = [ "windows-core", "windows-targets", ] [[package]] name = "windows-core" version = "0.56.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4698e52ed2d08f8658ab0c39512a7c00ee5fe2688c65f8c0a4f06750d729f2a6" dependencies = [ "windows-implement", "windows-interface", "windows-result", "windows-targets", ] [[package]] name = "windows-implement" version = "0.56.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b" dependencies = [ "proc-macro2", "quote", "syn 2.0.12", ] [[package]] name = "windows-interface" version = "0.56.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc" dependencies = [ "proc-macro2", "quote", "syn 2.0.12", ] [[package]] name = "windows-result" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" dependencies = [ "windows-targets", ] [[package]] name = "windows-sys" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ "windows_aarch64_gnullvm 0.42.0", "windows_aarch64_msvc 0.42.0", "windows_i686_gnu 0.42.0", "windows_i686_msvc 0.42.0", "windows_x86_64_gnu 0.42.0", "windows_x86_64_gnullvm 0.42.0", "windows_x86_64_msvc 0.42.0", ] [[package]] name = "windows-targets" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm 0.52.6", "windows_aarch64_msvc 0.52.6", "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", "windows_i686_msvc 0.52.6", "windows_x86_64_gnu 0.52.6", "windows_x86_64_gnullvm 0.52.6", "windows_x86_64_msvc 0.52.6", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" clircle-0.6.1/Cargo.toml0000644000000033520000000000100104560ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.69" name = "clircle" version = "0.6.1" authors = ["Niklas Mohrin "] build = false autobins = false autoexamples = false autotests = false autobenches = false description = "Detect IO circles in your CLI apps arguments." homepage = "https://github.com/niklasmohrin/clircle" documentation = "https://docs.rs/clircle" readme = "README.md" keywords = [ "cycle", "arguments", "argv", "io", ] categories = [ "command-line-interface", "filesystem", "os", ] license = "MIT OR Apache-2.0" repository = "https://github.com/niklasmohrin/clircle" [lib] name = "clircle" path = "src/lib.rs" [[example]] name = "return_code" path = "examples/return_code.rs" [dependencies.cfg-if] version = "1.0.0" [dependencies.serde] version = "1.0.117" optional = true [dependencies.serde_derive] version = "1.0.117" optional = true [dev-dependencies.tempfile] version = "3.4.0" [features] default = ["serde"] serde = [ "dep:serde", "dep:serde_derive", ] test-close-again = [] [target."cfg(unix)".dev-dependencies.nix] version = "0.29" features = ["term"] default-features = false [target."cfg(windows)".dependencies.windows] version = "0.56.0" features = [ "Win32_Storage_FileSystem", "Win32_System_Console", ] clircle-0.6.1/Cargo.toml.orig000064400000000000000000000020031046102023000141270ustar 00000000000000[package] name = "clircle" version = "0.6.1" authors = ["Niklas Mohrin "] license = "MIT OR Apache-2.0" rust-version = "1.69" edition = "2021" description = "Detect IO circles in your CLI apps arguments." homepage = "https://github.com/niklasmohrin/clircle" repository = "https://github.com/niklasmohrin/clircle" documentation = "https://docs.rs/clircle" readme = "README.md" categories = ["command-line-interface", "filesystem", "os"] keywords = ["cycle", "arguments", "argv", "io"] [features] default = ["serde"] serde = ["dep:serde", "dep:serde_derive"] test-close-again = [] [dependencies] serde = { version = "1.0.117", optional = true } serde_derive = { version = "1.0.117", optional = true } cfg-if = "1.0.0" [target.'cfg(windows)'.dependencies] windows = { version = "0.56.0", features = ["Win32_Storage_FileSystem", "Win32_System_Console"] } [dev-dependencies] tempfile = "3.4.0" [target.'cfg(unix)'.dev-dependencies] nix = { version = "0.29", default-features = false, features = ["term"] } clircle-0.6.1/LICENSE-APACHE000064400000000000000000000227731046102023000132040ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS clircle-0.6.1/LICENSE-MIT000064400000000000000000000017771046102023000127150ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. clircle-0.6.1/README.md000064400000000000000000000046271046102023000125350ustar 00000000000000# Clircle [![CI](https://github.com/niklasmohrin/clircle/actions/workflows/ci.yml/badge.svg?branch=main&event=push)](https://github.com/niklasmohrin/clircle/actions/workflows/ci.yml) [![crates.io version](https://img.shields.io/crates/v/clircle)](https://crates.io/crates/clircle) [![MSRV](https://img.shields.io/badge/MSRV-1.69.0-blue)](https://blog.rust-lang.org/2023/04/20/Rust-1.69.0.html) Clircle provides a cross-platform API to detect read / write cycles from your user-supplied arguments. You can get the important identifiers of a file (from a path) and for all three stdio streams, if they are piped from or to a file as well. ## Why? Imagine you want to read data from a couple of files and output something according to the contents of these files. If the user redirects the output of your program to one of the input files, you might end up in an infinite circle of reading and writing. The crate provides the struct `Identifier` which is a platform dependent type alias, so that you can use it on all platforms and do not need to introduce any conditional compilation yourself. On both Unix and Windows systems, `Identifier` holds information to identify a file on a disk. The `Clircle` trait is implemented on both of these structs and requires `TryFrom` for the `clircle::Stdio` enum and for `&Path`, so that all possible inputs can be represented as an `Identifier`. Finally, `Clircle` is a subtrait of `Eq`, so that the identifiers can be conveniently compared and circles can be detected. The `clircle` crate also provides some convenience functions around the comparison of `Clircle` implementors. ## Why should I use this and not just `fs::Metadata`? The `clircle` crate seamlessly works on Linux **and** Windows through a single API, so no conditional compilation is needed at all. Furthermore, `MetadataExt` is not stable on Windows yet, meaning you would have to dig into the Windows APIs yourself to get the information needed to identify a file. ## Where did this crate come from? This crate originated in a pull request to the [`bat`](https://github.com/sharkdp/bat) project. The `bat` tool strives to be a drop-in replacement for the unix tool `cat`. Since `cat` detects these cycles, `bat` has to do so too, which is where most of this code came into play. However, it was decided, that the new logic was - useful for other projects and - too platform specific for `bat`s scope. So now, you can use `clircle` too! clircle-0.6.1/examples/return_code.rs000064400000000000000000000016041046102023000157430ustar 00000000000000//! This example collects all given arguments, interprets them as paths, gathers information //! about them and checks if stdout is being redirected to one of the given files. //! If that's the case, the return code will be set to 1. In both cases, there is an according //! output to stderr. use clircle::{stdout_among_inputs, Identifier}; use std::convert::TryFrom; use std::fs::File; fn main() { let inputs: Vec<_> = std::env::args().collect(); let inputs: Result, _> = inputs .iter() .map(File::open) .map(Result::unwrap) .map(Identifier::try_from) .collect(); let inputs = inputs.expect("There was an argument that could not be converted to an Idenfier!"); if stdout_among_inputs(&inputs) { eprintln!("Cycle detected!"); std::process::exit(1); } else { eprintln!("No cycle detected.") } } clircle-0.6.1/src/clircle_unix.rs000064400000000000000000000146601046102023000150710ustar 00000000000000use crate::{Clircle, Stdio}; use std::convert::TryFrom; use std::fs::File; use std::io::{self, Seek}; use std::os::fd::AsRawFd; use std::os::unix::fs::MetadataExt; use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd}; use std::{cmp, hash, ops}; /// Implementation of `Clircle` for Unix. #[derive(Debug)] pub(crate) struct Identifier { device: u64, inode: u64, size: u64, is_regular_file: bool, file: Option, owns_fd: bool, } impl Identifier { fn file(&self) -> &File { self.file.as_ref().expect("Called file() on an identifier that has already been destroyed, this should never happen! Please file a bug!") } fn current_file_offset(&self) -> io::Result { self.file().stream_position() } fn has_content_left_to_read(&self) -> io::Result { Ok(self.current_file_offset()? < self.size) } /// Creates a `UnixIdentifier` from a raw file descriptor. The preferred way to create a /// `UnixIdentifier` is through one of the `TryFrom` implementations. /// /// # Safety /// /// The `owns_fd` argument should only be true, if the given file descriptor owns the resource /// it points to (for example a file). /// If it is true, a `File` can be obtained back with `Clircle::into_inner`, or it will be /// closed when the `UnixIdentifier` is dropped. /// /// # Errors /// /// The underlying call to `File::metadata` fails. unsafe fn try_from_raw_fd(fd: RawFd, owns_fd: bool) -> io::Result { Self::try_from(File::from_raw_fd(fd)).map(|mut ident| { ident.owns_fd = owns_fd; ident }) } } impl Clircle for Identifier { #[must_use] fn into_inner(mut self) -> Option { if self.owns_fd { self.owns_fd = false; self.file.take() } else { None } } /// This method implements the conflict check that is used in the GNU coreutils program `cat`. #[must_use] fn surely_conflicts_with(&self, other: &Self) -> bool { PartialEq::eq(self, other) && self.is_regular_file && other.has_content_left_to_read().unwrap_or(true) } } impl TryFrom for Identifier { type Error = >::Error; fn try_from(stdio: Stdio) -> Result { let fd = match stdio { Stdio::Stdin => io::stdin().as_raw_fd(), Stdio::Stdout => io::stdout().as_raw_fd(), Stdio::Stderr => io::stderr().as_raw_fd(), }; // Safety: It is okay to create the file, because it won't be dropped later since the // `owns_fd` field is not set. unsafe { Self::try_from_raw_fd(fd, false) } } } impl ops::Drop for Identifier { fn drop(&mut self) { if !self.owns_fd { let _ = self.file.take().map(IntoRawFd::into_raw_fd); } } } impl TryFrom for Identifier { type Error = io::Error; fn try_from(file: File) -> Result { file.metadata().map(|metadata| Self { device: metadata.dev(), inode: metadata.ino(), size: metadata.size(), is_regular_file: metadata.file_type().is_file(), file: Some(file), owns_fd: true, }) } } impl cmp::PartialEq for Identifier { #[must_use] fn eq(&self, other: &Self) -> bool { self.device == other.device && self.inode == other.inode } } impl Eq for Identifier {} impl hash::Hash for Identifier { fn hash(&self, state: &mut H) { self.device.hash(state); self.inode.hash(state); } } #[cfg(test)] mod tests { use super::*; use std::io::Write; use std::os::fd::OwnedFd; use nix::pty::{openpty, OpenptyResult}; use nix::unistd::close; #[test] fn test_into_inner() { let file = tempfile::tempfile().expect("failed to create tempfile"); file.metadata().expect("can stat file"); let ident = Identifier::try_from(file).expect("failed to create identifier"); let mut file = ident .into_inner() .expect("failed to convert identifier to file"); file.write_all(b"some test content") .expect("failed to write test content to file"); } #[test] fn test_borrowed_fd() { let file = tempfile::tempfile().expect("failed to create tempfile"); let fd: OwnedFd = file.into(); let ident = unsafe { Identifier::try_from_raw_fd(fd.as_raw_fd(), false) } .expect("failed to create identifier"); drop(ident); let fd = fd.into_raw_fd(); close(fd).expect("error closing fd"); #[cfg(feature = "test-close-again")] close(fd).expect_err("closing again should fail"); } #[test] fn test_owned_fd() { let file = tempfile::tempfile().expect("failed to create tempfile"); let fd: OwnedFd = file.into(); let ident = unsafe { Identifier::try_from_raw_fd(fd.as_raw_fd(), true) } .expect("failed to create identifier"); drop(ident); #[cfg(feature = "test-close-again")] close(fd.into_raw_fd()) .expect_err("the fd should have already been closed by dropping the identifier"); } #[test] fn test_pty_equal_but_not_conflicting() { let OpenptyResult { master: parent, slave: child, } = openpty(None, None).expect("failed to open pty"); let parent_ident = unsafe { Identifier::try_from_raw_fd(parent.as_raw_fd(), false) } .expect("failed to create parent identifier"); assert_eq!(parent_ident, parent_ident); assert!(!parent_ident.surely_conflicts_with(&parent_ident)); let child_ident = unsafe { Identifier::try_from_raw_fd(child.as_raw_fd(), false) } .expect("failed to create child identifier"); assert_ne!(parent_ident, child_ident); assert!(!parent_ident.surely_conflicts_with(&child_ident)); drop(child_ident); drop(parent_ident); let child = child.into_raw_fd(); close(child).expect("failed to close child"); #[cfg(feature = "test-close-again")] close(child).expect_err("closing child again should fail"); let parent = parent.into_raw_fd(); close(parent).expect("failed to close parent"); #[cfg(feature = "test-close-again")] close(parent).expect_err("closing parent again should fail"); } } clircle-0.6.1/src/clircle_windows.rs000064400000000000000000000077311046102023000156010ustar 00000000000000use crate::{Clircle, Stdio}; use windows::Win32::{ Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE}, Storage::FileSystem::{ GetFileInformationByHandle, GetFileType, BY_HANDLE_FILE_INFORMATION, FILE_TYPE_DISK, }, System::Console::{GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE}, }; use std::convert::TryFrom; use std::fs::File; use std::mem::MaybeUninit; use std::os::windows::io::{FromRawHandle, IntoRawHandle}; use std::{cmp, hash, io, mem, ops}; /// Implementation of `Clircle` for Windows. #[derive(Debug)] pub(crate) struct Identifier { volume_serial: u32, file_index: u64, handle: HANDLE, owns_handle: bool, } impl Identifier { unsafe fn try_from_raw_handle(handle: HANDLE, owns_handle: bool) -> Result { if handle.is_invalid() { return Err(io::Error::new( io::ErrorKind::InvalidInput, "Tried to convert handle to Identifier that was invalid or null.", )); } // SAFETY: This function can be called with any valid handle. // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfiletype if GetFileType(handle) != FILE_TYPE_DISK { return Err(io::Error::new( io::ErrorKind::InvalidInput, "Tried to convert handle to Identifier that was not a file handle.", )); } let mut fi = MaybeUninit::::uninit(); // SAFETY: This function is safe to call, if the handle is valid and a handle to a file. // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle GetFileInformationByHandle(handle, fi.as_mut_ptr())?; // SAFETY: GetFileInformationByHandle returned successfully. let fi = fi.assume_init(); Ok(Self { volume_serial: fi.dwVolumeSerialNumber, file_index: u64::from(fi.nFileIndexHigh) << 32 | u64::from(fi.nFileIndexLow), handle, owns_handle, }) } unsafe fn take_handle(&mut self) -> Option { if self.owns_handle { self.owns_handle = false; Some(mem::replace(&mut self.handle, INVALID_HANDLE_VALUE)) } else { None } } } impl Clircle for Identifier { #[must_use] fn into_inner(mut self) -> Option { Some(unsafe { File::from_raw_handle(self.take_handle()?.0 as _) }) } } impl TryFrom for Identifier { type Error = io::Error; fn try_from(stdio: Stdio) -> Result { let std_handle_id = match stdio { Stdio::Stdin => STD_INPUT_HANDLE, Stdio::Stdout => STD_OUTPUT_HANDLE, Stdio::Stderr => STD_ERROR_HANDLE, }; // SAFETY: This method can safely be called with one of the above constants. // https://docs.microsoft.com/en-us/windows/console/getstdhandle let handle = unsafe { GetStdHandle(std_handle_id) }?; if handle.is_invalid() { return Err(io::Error::last_os_error()); } unsafe { Self::try_from_raw_handle(handle, false) } } } impl TryFrom for Identifier { type Error = io::Error; fn try_from(file: File) -> Result { unsafe { Self::try_from_raw_handle(HANDLE(file.into_raw_handle() as _), true) } } } impl ops::Drop for Identifier { fn drop(&mut self) { unsafe { if let Some(handle) = self.take_handle() { let _ = CloseHandle(handle); } } } } impl cmp::PartialEq for Identifier { #[must_use] fn eq(&self, other: &Self) -> bool { self.volume_serial == other.volume_serial && self.file_index == other.file_index } } impl Eq for Identifier {} impl hash::Hash for Identifier { fn hash(&self, state: &mut H) { self.volume_serial.hash(state); self.file_index.hash(state); } } clircle-0.6.1/src/lib.rs000064400000000000000000000144021046102023000131510ustar 00000000000000//! The `clircle` crate helps you detect IO circles in your CLI applications. //! //! Imagine you want to read data from a couple of files and output something according to the //! contents of these files. If the user redirects the output of your program to one of the input //! files, you might end up in an infinite circle of reading and writing. //! //! The crate provides the struct `Identifier` which is a platform dependent type alias, so that //! you can use it on all platforms and do not need to introduce any conditional compilation //! yourself. `Identifier` implements the `Clircle` trait, which is where you should look for the //! public functionality. //! //! The `Clircle` trait is implemented on both of these structs and requires `TryFrom` for the //! `clircle::Stdio` enum and for `File`, so that all possible inputs can be represented as an //! `Identifier`. Additionally, there are `unsafe` methods for each specific implementation, but //! they are not recommended to use. //! Finally, `Clircle` is a subtrait of `Eq`, which allows checking if two `Identifier`s point to //! the same file, even if they don't conflict. If you only need this last feature, you should //! use [`same-file`](https://crates.io/crates/same-file) instead of this crate. //! //! ## Examples //! //! To check if two `Identifier`s conflict, use //! `Clircle::surely_conflicts_with`: //! //! ```rust,no_run //! # fn example() -> Option<()> { //! # use clircle::{Identifier, Clircle, Stdio::{Stdin, Stdout}}; //! # use std::convert::TryFrom; //! let stdin = Identifier::stdin()?; //! let stdout = Identifier::stdout()?; //! //! if stdin.surely_conflicts_with(&stdout) { //! eprintln!("stdin and stdout are conflicting!"); //! } //! # Some(()) //! # } //! ``` //! //! On Linux, the above snippet could be used to detect `cat < x > x`, while allowing just //! `cat`, although stdin and stdout are pointing to the same pty in both cases. On Windows, this //! code will not print anything, because the same operation is safe there. #![deny(clippy::all)] #![deny(missing_docs)] #![warn(clippy::pedantic)] cfg_if::cfg_if! { if #[cfg(unix)] { mod clircle_unix; use clircle_unix as imp; } else if #[cfg(windows)] { mod clircle_windows; use clircle_windows as imp; } else { compile_error!("Neither cfg(unix) nor cfg(windows) was true, aborting."); } } #[cfg(feature = "serde")] use serde_derive::{Deserialize, Serialize}; use std::convert::TryFrom; use std::fs::File; use std::io; /// The `Clircle` trait describes the public interface of the crate. /// It contains all the platform-independent functionality. /// Additionally, an implementation of `Eq` is required, that gives a simple way to check for /// conflicts, if using the more elaborate `surely_conflicts_with` method is not wanted. /// This trait is implemented for the structs `UnixIdentifier` and `WindowsIdentifier`. pub trait Clircle: Eq + TryFrom + TryFrom { /// Returns the `File` that was used for `From`. If the instance was created otherwise, /// this may also return `None`. fn into_inner(self) -> Option; /// Checks whether the two values will without doubt conflict. By default, this always returns /// `false`, but implementors can override this method. Currently, only the Unix implementation /// overrides `surely_conflicts_with`. fn surely_conflicts_with(&self, _other: &Self) -> bool { false } /// Shorthand for `try_from(Stdio::Stdin)`. #[must_use] fn stdin() -> Option { Self::try_from(Stdio::Stdin).ok() } #[must_use] /// Shorthand for `try_from(Stdio::Stdout)`. fn stdout() -> Option { Self::try_from(Stdio::Stdout).ok() } #[must_use] /// Shorthand for `try_from(Stdio::Stderr)`. fn stderr() -> Option { Self::try_from(Stdio::Stderr).ok() } } /// The three stdio streams. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[allow(missing_docs)] pub enum Stdio { Stdin, Stdout, Stderr, } /// Finds a common `Identifier` in the two given slices. pub fn output_among_inputs<'o, T>(outputs: &'o [T], inputs: &[T]) -> Option<&'o T> where T: Clircle, { outputs.iter().find(|output| inputs.contains(output)) } /// Checks if `Stdio::Stdout` is in the given slice. pub fn stdout_among_inputs(inputs: &[T]) -> bool where T: Clircle, { T::stdout().map_or(false, |stdout| inputs.contains(&stdout)) } /// Identifies a file. The type forwards all methods to the platform implementation. #[derive(Debug, PartialEq, Eq, Hash)] pub struct Identifier(imp::Identifier); impl Clircle for Identifier { #[must_use] fn into_inner(self) -> Option { self.0.into_inner() } fn surely_conflicts_with(&self, other: &Self) -> bool { self.0.surely_conflicts_with(&other.0) } } impl TryFrom for Identifier { type Error = io::Error; fn try_from(stdio: Stdio) -> Result { imp::Identifier::try_from(stdio).map(Self) } } impl TryFrom for Identifier { type Error = io::Error; fn try_from(file: File) -> Result { imp::Identifier::try_from(file).map(Self) } } #[cfg(test)] mod tests { use super::*; use std::collections::HashSet; use std::hash::Hash; fn contains_duplicates(items: Vec) -> bool where T: Eq + Hash, { let mut set = HashSet::new(); items.into_iter().any(|item| !set.insert(item)) } #[test] fn test_basic_comparisons() -> Result<(), &'static str> { let dir = tempfile::tempdir().expect("Couldn't create tempdir."); let dir_path = dir.path().to_path_buf(); let filenames = ["a", "b", "c", "d"]; let paths: Vec<_> = filenames .iter() .map(|filename| dir_path.join(filename)) .collect(); let identifiers = paths .iter() .map(File::create) .map(Result::unwrap) .map(Identifier::try_from) .map(Result::unwrap) .collect::>(); if contains_duplicates(identifiers) { return Err("Duplicate identifier found for set of unique paths."); } Ok(()) } }