diff --git a/.cirrus.yml b/.cirrus.yml deleted file mode 100644 index 50f8a25b1..000000000 --- a/.cirrus.yml +++ /dev/null @@ -1,21 +0,0 @@ -env: - # Temporary workaround for error `error: sysinfo not supported on - # this platform` seen on FreeBSD platforms, affecting Rustup - # - # References: https://github.com/rust-lang/rustup/issues/2774 - RUSTUP_IO_THREADS: 1 - -task: - name: stable x86_64-unknown-freebsd-12 - freebsd_instance: - image: freebsd-12-2-release-amd64 - setup_script: - - pkg install -y curl gmake - - curl https://sh.rustup.rs -sSf --output rustup.sh - - sh rustup.sh -y --profile=minimal - build_script: - - . $HOME/.cargo/env - - cargo build - test_script: - - . $HOME/.cargo/env - - cargo test -p uucore -p coreutils diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index b2d77a5a4..0372336a7 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -13,8 +13,7 @@ env: PROJECT_NAME: coreutils PROJECT_DESC: "Core universal (cross-platform) utilities" PROJECT_AUTH: "uutils" - RUST_MIN_SRV: "1.43.1" ## v1.43.0 - RUST_COV_SRV: "2020-08-01" ## (~v1.47.0) supported rust version for code coverage; (date required/used by 'coverage') ## !maint: refactor when code coverage support is included in the stable channel + RUST_MIN_SRV: "1.47.0" ## MSRV v1.47.0 on: [push, pull_request] @@ -102,11 +101,17 @@ jobs: fail-fast: false matrix: job: - - { os: ubuntu-latest , features: feat_os_unix } + - { os: ubuntu-latest } - { os: macos-latest , features: feat_os_macos } - { os: windows-latest , features: feat_os_windows } steps: - uses: actions/checkout@v2 + - name: Install/setup prerequisites + shell: bash + run: | + case '${{ matrix.job.os }}' in + macos-latest) brew install coreutils ;; # needed for show-utils.sh + esac - name: Initialize workflow variables id: vars shell: bash @@ -115,9 +120,14 @@ jobs: outputs() { step_id="vars"; for var in "$@" ; do echo steps.${step_id}.outputs.${var}="${!var}"; echo ::set-output name=${var}::${!var}; done; } # target-specific options # * CARGO_FEATURES_OPTION - CARGO_FEATURES_OPTION='' ; - if [ -n "${{ matrix.job.features }}" ]; then CARGO_FEATURES_OPTION='--features "${{ matrix.job.features }}"' ; fi + CARGO_FEATURES_OPTION='--all-features' ; + if [ -n "${{ matrix.job.features }}" ]; then CARGO_FEATURES_OPTION='--features ${{ matrix.job.features }}' ; fi outputs CARGO_FEATURES_OPTION + # * determine sub-crate utility list + UTILITY_LIST="$(./util/show-utils.sh ${CARGO_FEATURES_OPTION})" + echo UTILITY_LIST=${UTILITY_LIST} + CARGO_UTILITY_LIST_OPTIONS="$(for u in ${UTILITY_LIST}; do echo "-puu_${u}"; done;)" + outputs CARGO_UTILITY_LIST_OPTIONS - name: Install `rust` toolchain uses: actions-rs/toolchain@v1 with: @@ -130,7 +140,7 @@ jobs: run: | ## `clippy` lint testing # * convert any warnings to GHA UI annotations; ref: - S=$(cargo +nightly clippy --all-targets ${{ matrix.job.cargo-options }} ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} -- -D warnings 2>&1) && printf "%s\n" "$S" || { printf "%s\n" "$S" ; printf "%s" "$S" | sed -E -n -e '/^error:/{' -e "N; s/^error:[[:space:]]+(.*)\\n[[:space:]]+-->[[:space:]]+${PWD//\//\\/}\/(.*):([0-9]+):([0-9]+).*$/::error file=\2,line=\3,col=\4::ERROR: \`cargo clippy\`: \1 (file:'\2', line:\3)/p;" -e '}' ; exit 1 ; } + S=$(cargo +nightly clippy --all-targets ${{ steps.vars.outputs.CARGO_UTILITY_LIST_OPTIONS }} ${{ matrix.job.cargo-options }} ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} -- -D warnings 2>&1) && printf "%s\n" "$S" || { printf "%s\n" "$S" ; printf "%s" "$S" | sed -E -n -e '/^error:/{' -e "N; s/^error:[[:space:]]+(.*)\\n[[:space:]]+-->[[:space:]]+${PWD//\//\\/}\/(.*):([0-9]+):([0-9]+).*$/::error file=\2,line=\3,col=\4::ERROR: \`cargo clippy\`: \1 (file:'\2', line:\3)/p;" -e '}' ; exit 1 ; } code_spellcheck: name: Style/spelling @@ -249,6 +259,8 @@ jobs: # { os, target, cargo-options, features, use-cross, toolchain } - { os: ubuntu-latest , target: arm-unknown-linux-gnueabihf , features: feat_os_unix_gnueabihf , use-cross: use-cross } - { os: ubuntu-latest , target: aarch64-unknown-linux-gnu , features: feat_os_unix_gnueabihf , use-cross: use-cross } + - { os: ubuntu-latest , target: x86_64-unknown-linux-gnu , features: feat_os_unix , use-cross: use-cross } + # - { os: ubuntu-latest , target: x86_64-unknown-linux-gnu , features: feat_selinux , use-cross: use-cross } # - { os: ubuntu-18.04 , target: i586-unknown-linux-gnu , features: feat_os_unix , use-cross: use-cross } ## note: older windows platform; not required, dev-FYI only # - { os: ubuntu-18.04 , target: i586-unknown-linux-gnu , features: feat_os_unix , use-cross: use-cross } ## note: older windows platform; not required, dev-FYI only - { os: ubuntu-18.04 , target: i686-unknown-linux-gnu , features: feat_os_unix , use-cross: use-cross } @@ -519,6 +531,52 @@ jobs: n_fails=$(echo "$output" | grep "^FAIL:\s" | wc --lines) if [ $n_fails -gt 0 ] ; then echo "::warning ::${n_fails}+ test failures" ; fi + test_freebsd: + runs-on: macos-10.15 + name: Tests/FreeBSD test suite + env: + mem: 2048 + steps: + - uses: actions/checkout@v2 + - name: Prepare, build and test + id: test + uses: vmactions/freebsd-vm@v0.1.5 + with: + usesh: true + prepare: pkg install -y curl gmake sudo + run: | + # Need to be run in the same block. Otherwise, we are back on the mac host. + set -e + pw adduser -n cuuser -d /root/ -g wheel -c "Coreutils user to build" -w random + chown -R cuuser:wheel /root/ /Users/runner/work/coreutils/ + whoami + + # Needs to be done in a sudo as we are changing users + sudo -i -u cuuser sh << EOF + set -e + whoami + curl https://sh.rustup.rs -sSf --output rustup.sh + sh rustup.sh -y --profile=minimal + ## Info + # environment + echo "## environment" + echo "CI='${CI}'" + # tooling info display + echo "## tooling" + . $HOME/.cargo/env + cargo -V + rustc -V + env + + # where the files are resynced + cd /Users/runner/work/coreutils/coreutils/ + cargo build + cargo test --features feat_os_unix -p uucore -p coreutils + # Clean to avoid to rsync back the files + cargo clean + EOF + + coverage: name: Code Coverage runs-on: ${{ matrix.job.os }} @@ -548,7 +606,7 @@ jobs: ## VARs setup outputs() { step_id="vars"; for var in "$@" ; do echo steps.${step_id}.outputs.${var}="${!var}"; echo ::set-output name=${var}::${!var}; done; } # toolchain - TOOLCHAIN="nightly-${{ env.RUST_COV_SRV }}" ## default to "nightly" toolchain (required for certain required unstable compiler flags) ## !maint: refactor when stable channel has needed support + TOOLCHAIN="nightly" ## default to "nightly" toolchain (required for certain required unstable compiler flags) ## !maint: refactor when stable channel has needed support # * specify gnu-type TOOLCHAIN for windows; `grcov` requires gnu-style code coverage data files case ${{ matrix.job.os }} in windows-*) TOOLCHAIN="$TOOLCHAIN-x86_64-pc-windows-gnu" ;; esac; # * use requested TOOLCHAIN if specified @@ -650,3 +708,35 @@ jobs: flags: ${{ steps.vars.outputs.CODECOV_FLAGS }} name: codecov-umbrella fail_ci_if_error: false + + unused_deps: + name: Unused deps + runs-on: ${{ matrix.job.os }} + strategy: + fail-fast: false + matrix: + job: + - { os: ubuntu-latest , features: feat_os_unix } + - { os: macos-latest , features: feat_os_macos } + - { os: windows-latest , features: feat_os_windows } + steps: + - uses: actions/checkout@v2 + - name: Install `rust` toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + default: true + profile: minimal + - name: Install `cargo-udeps` + uses: actions-rs/install@v0.1 + with: + crate: cargo-udeps + version: latest + use-tool-cache: true + env: + RUSTUP_TOOLCHAIN: stable + - name: Confirms there isn't any unused deps + shell: bash + run: | + cargo +nightly udeps --all-targets &> udeps.log || cat udeps.log + grep "seem to have been used" udeps.log diff --git a/.github/workflows/GnuTests.yml b/.github/workflows/GnuTests.yml index 8bf6c091b..dad53f20c 100644 --- a/.github/workflows/GnuTests.yml +++ b/.github/workflows/GnuTests.yml @@ -90,3 +90,42 @@ jobs: with: name: gnu-result path: gnu-result.json + - name: Download the result + uses: dawidd6/action-download-artifact@v2 + with: + workflow: GnuTests.yml + name: gnu-result + repo: uutils/coreutils + branch: master + path: dl + - name: Download the log + uses: dawidd6/action-download-artifact@v2 + with: + workflow: GnuTests.yml + name: test-report + repo: uutils/coreutils + branch: master + path: dl + - name: Compare failing tests against master + shell: bash + run: | + OLD_FAILING=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" dl/test-suite.log | sort) + NEW_FAILING=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" gnu/tests/test-suite.log | sort) + for LINE in $OLD_FAILING + do + if ! grep -Fxq $LINE<<<"$NEW_FAILING"; then + echo "::warning ::Congrats! The gnu test $LINE is now passing!" + fi + done + for LINE in $NEW_FAILING + do + if ! grep -Fxq $LINE<<<"$OLD_FAILING" + then + echo "::error ::GNU test failed: $LINE. $LINE is passing on 'master'. Maybe you have to rebase?" + fi + done + - name: Compare against master results + shell: bash + run: | + mv dl/gnu-result.json master-gnu-result.json + python uutils/util/compare_gnu_result.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e3ad98ee3..f90466bed 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,7 +11,7 @@ repos: - id: rust-clippy name: Rust clippy description: Run cargo clippy on files included in the commit. - entry: cargo +nightly clippy --all-targets --all-features -- + entry: cargo +nightly clippy --workspace --all-targets --all-features -- pass_filenames: false types: [file, rust] language: system diff --git a/.vscode/cSpell.json b/.vscode/cSpell.json index 9869923a4..498360139 100644 --- a/.vscode/cSpell.json +++ b/.vscode/cSpell.json @@ -11,7 +11,7 @@ { "name": "workspace", "path": "./cspell.dictionaries/workspace.wordlist.txt" } ], // ignorePaths - a list of globs to specify which files are to be ignored - "ignorePaths": ["Cargo.lock", "target/**", "tests/**/fixtures/**"], + "ignorePaths": ["Cargo.lock", "target/**", "tests/**/fixtures/**", "src/uu/dd/test-resources/**"], // ignoreWords - a list of words to be ignored (even if they are in the flagWords) "ignoreWords": [], // words - list of words to be always considered correct diff --git a/.vscode/cspell.dictionaries/jargon.wordlist.txt b/.vscode/cspell.dictionaries/jargon.wordlist.txt index c2e2c29f3..9b1d0a8da 100644 --- a/.vscode/cspell.dictionaries/jargon.wordlist.txt +++ b/.vscode/cspell.dictionaries/jargon.wordlist.txt @@ -1,3 +1,4 @@ +AFAICT arity autogenerate autogenerated @@ -8,10 +9,13 @@ bytewise canonicalization canonicalize canonicalizing +codepoint +codepoints colorizable colorize coprime consts +conv cyclomatic dedup deduplication @@ -23,6 +27,7 @@ dev devs discoverability duplicative +dsync enqueue errored executable @@ -30,17 +35,30 @@ executables exponentiate eval falsey +fileio flamegraph +fullblock +getfacl +gibi gibibytes glob globbing +hardcode +hardcoded +hardcoding hardfloat hardlink hardlinks hasher hashsums +infile +iflag +iflags kibi kibibytes +libacl +lcase +lossily mebi mebibytes mergeable @@ -49,8 +67,21 @@ microbenchmarks microbenchmarking multibyte multicall +noatime +nocache +nocreat +noctty +noerror +nofollow +nolinks +nonblock nonportable nonprinting +notrunc +noxfer +ofile +oflag +oflags peekable performant precompiled @@ -68,11 +99,15 @@ seedable semver semiprime semiprimes +setfacl shortcode shortcodes +siginfo +sigusr subcommand subexpression submodule +sync symlink symlinks syscall @@ -80,12 +115,15 @@ syscalls tokenize toolchain truthy +ucase unbuffered +udeps unescape unintuitive unprefixed unportable unsync +urand whitespace wordlist wordlists diff --git a/.vscode/cspell.dictionaries/people.wordlist.txt b/.vscode/cspell.dictionaries/people.wordlist.txt index d7665585b..405733836 100644 --- a/.vscode/cspell.dictionaries/people.wordlist.txt +++ b/.vscode/cspell.dictionaries/people.wordlist.txt @@ -151,6 +151,9 @@ Sylvestre Ledru T Jameson Little Jameson Little +Thomas Queiroz + Thomas + Queiroz Tobias Bohumir Schottdorf Tobias Bohumir diff --git a/.vscode/cspell.dictionaries/shell.wordlist.txt b/.vscode/cspell.dictionaries/shell.wordlist.txt index d8f297d21..4ed281efb 100644 --- a/.vscode/cspell.dictionaries/shell.wordlist.txt +++ b/.vscode/cspell.dictionaries/shell.wordlist.txt @@ -8,6 +8,7 @@ csh globstar inotify localtime +mksh mountinfo mountpoint mtab @@ -63,6 +64,7 @@ abspath addprefix addsuffix endef +findstring firstword ifeq ifneq @@ -89,5 +91,10 @@ markdownlint rerast rollup sed +selinuxenabled +sestatus wslpath xargs + +# * directories +sbin diff --git a/.vscode/cspell.dictionaries/workspace.wordlist.txt b/.vscode/cspell.dictionaries/workspace.wordlist.txt index 7242199a5..d37a59465 100644 --- a/.vscode/cspell.dictionaries/workspace.wordlist.txt +++ b/.vscode/cspell.dictionaries/workspace.wordlist.txt @@ -9,12 +9,14 @@ aho-corasick backtrace blake2b_simd bstr +bytecount byteorder chacha chrono conv corasick crossterm +exacl filetime formatteriteminfo fsext @@ -66,8 +68,10 @@ structs substr splitn trunc +uninit # * uutils +basenc chcon chgrp chmod @@ -105,11 +109,18 @@ whoami # * vars/errno errno +EACCES +EBADF +EBUSY EEXIST +EINVAL +ENODATA ENOENT ENOSYS -EPERM +ENOTEMPTY EOPNOTSUPP +EPERM +EROFS # * vars/fcntl F_GETFL @@ -118,7 +129,9 @@ fcntl vmsplice # * vars/libc +COMFOLLOW FILENO +FTSENT HOSTSIZE IDSIZE IFBLK @@ -151,12 +164,14 @@ SIGTERM SYS_fdatasync SYS_syncfs USERSIZE +accpath addrinfo addrlen blocksize canonname chroot dlsym +execvp fdatasync freeaddrinfo getaddrinfo @@ -174,15 +189,18 @@ inode inodes isatty lchown +pathlen setgid setgroups settime setuid socktype statfs +statp statvfs strcmp strerror +strlen syncfs umask waitpid @@ -260,6 +278,7 @@ ULONG ULONGLONG UNLEN WCHAR +WSADATA errhandlingapi fileapi handleapi @@ -274,6 +293,13 @@ winerror winnt winsock +# * vars/selinux +freecon +getfilecon +lgetfilecon +lsetfilecon +setfilecon + # * vars/uucore optflag optflagmulti @@ -298,3 +324,6 @@ uucore_procs uumain uutil uutils + +# * function names +getcwd diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 7a73a41bf..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,2 +0,0 @@ -{ -} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 05334055f..4a40fd883 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +[[package]] +name = "ahash" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e" + [[package]] name = "aho-corasick" version = "0.7.18" @@ -17,6 +23,12 @@ dependencies = [ "memchr 2.4.0", ] +[[package]] +name = "aliasable" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "250f629c0161ad8107cf89319e990051fae62832fd343083bea452d93e2205fd" + [[package]] name = "ansi_term" version = "0.11.0" @@ -73,6 +85,29 @@ dependencies = [ "compare", ] +[[package]] +name = "bindgen" +version = "0.59.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453c49e5950bb0eb63bb3df640e31618846c89d5b7faa54040d76e98e0134375" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "clap", + "env_logger 0.8.4", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2", + "quote 1.0.9", + "regex", + "rustc-hash", + "shlex", + "which", +] + [[package]] name = "bit-set" version = "0.5.2" @@ -90,9 +125,21 @@ checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitvec" +version = "0.19.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8942c8d352ae1838c9dda0b0ca2ab657696ef2232a20147cf1b30ae1a9cb4321" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] [[package]] name = "blake2b_simd" @@ -132,6 +179,21 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" +[[package]] +name = "byte-unit" +version = "4.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063197e6eb4b775b64160dedde7a0986bb2836cce140e9492e9e96f28e18bcd8" +dependencies = [ + "utf8-width", +] + +[[package]] +name = "bytecount" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72feb31ffc86498dacdbd0fcebb56138e7177a8cc5cea4516031d15ae85a742e" + [[package]] name = "byteorder" version = "1.4.3" @@ -140,9 +202,18 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "cc" -version = "1.0.68" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" +checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" + +[[package]] +name = "cexpr" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db507a7679252d2276ed0dd8113c6875ec56d3089f9225b2b42c30cc1f8e5c89" +dependencies = [ + "nom", +] [[package]] name = "cfg-if" @@ -169,6 +240,17 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "clang-sys" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "853eda514c284c2287f4bf20ae614f8781f40a81d32ecda6e91449304dfe077c" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "clap" version = "2.33.3" @@ -180,7 +262,7 @@ dependencies = [ "bitflags", "strsim", "term_size", - "textwrap", + "textwrap 0.11.0", "unicode-width", "vec_map", ] @@ -217,14 +299,14 @@ dependencies = [ [[package]] name = "coreutils" -version = "0.0.7" +version = "0.0.8" dependencies = [ "atty", "chrono", "clap", "conv", "filetime", - "glob 0.3.0", + "glob", "lazy_static", "libc", "nix 0.20.0", @@ -232,9 +314,10 @@ dependencies = [ "rand 0.7.3", "regex", "rlimit", + "selinux", "sha1", "tempfile", - "textwrap", + "textwrap 0.14.2", "time", "unindent", "unix_socket", @@ -243,7 +326,9 @@ dependencies = [ "uu_base32", "uu_base64", "uu_basename", + "uu_basenc", "uu_cat", + "uu_chcon", "uu_chgrp", "uu_chmod", "uu_chown", @@ -254,6 +339,7 @@ dependencies = [ "uu_csplit", "uu_cut", "uu_date", + "uu_dd", "uu_df", "uu_dircolors", "uu_dirname", @@ -304,6 +390,7 @@ dependencies = [ "uu_relpath", "uu_rm", "uu_rmdir", + "uu_runcon", "uu_seq", "uu_shred", "uu_shuf", @@ -454,9 +541,9 @@ dependencies = [ [[package]] name = "crossbeam-deque" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" +checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e" dependencies = [ "cfg-if 1.0.0", "crossbeam-epoch", @@ -521,6 +608,16 @@ dependencies = [ "syn", ] +[[package]] +name = "ctrlc" +version = "3.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "232295399409a8b7ae41276757b5a1cc21032848d42bff2352261f958b3ca29a" +dependencies = [ + "nix 0.20.0", + "winapi 0.3.9", +] + [[package]] name = "custom_derive" version = "0.1.7" @@ -529,9 +626,40 @@ checksum = "ef8ae57c4978a2acd8b869ce6b9ca1dfe817bff704c220209fdef2c0b75a01b9" [[package]] name = "data-encoding" -version = "2.1.2" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f47ca1860a761136924ddd2422ba77b2ea54fe8cc75b9040804a0d9d32ad97" +checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57" + +[[package]] +name = "data-encoding-macro" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86927b7cd2fe88fa698b87404b287ab98d1a0063a34071d92e575b72d3029aca" +dependencies = [ + "data-encoding", + "data-encoding-macro-internal", +] + +[[package]] +name = "data-encoding-macro-internal" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5bbed42daaa95e780b60a50546aa345b8413a1e46f9a40a12907d3598f038db" +dependencies = [ + "data-encoding", + "syn", +] + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote 1.0.9", + "syn", +] [[package]] name = "diff" @@ -541,13 +669,22 @@ checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" [[package]] name = "digest" -version = "0.6.2" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5b29bf156f3f4b3c4f610a25ff69370616ae6e0657d416de22645483e72af0a" +checksum = "ecae1c064e29fcabb6c2e9939e53dc7da72ed90234ae36ebfe03a478742efbd1" dependencies = [ "generic-array", ] +[[package]] +name = "dlv-list" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68df3f2b690c1b86e65ef7830956aededf3cb0a16f898f79b9a6f421a7b6211b" +dependencies = [ + "rand 0.8.4", +] + [[package]] name = "dns-lookup" version = "1.0.5" @@ -582,6 +719,34 @@ dependencies = [ "regex", ] +[[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "exacl" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "769bbd173781e84865b957cf83449f0d2869f4c9d2f191cbbffffb3d9751ba2b" +dependencies = [ + "bitflags", + "log", + "nix 0.21.0", + "num_enum", + "scopeguard", + "serde", + "uuid", +] + [[package]] name = "fake-simd" version = "0.1.2" @@ -596,13 +761,13 @@ checksum = "31a7a908b8f32538a2143e59a6e4e2508988832d5d4d6f7c156b3cbc762643a5" [[package]] name = "filetime" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8" +checksum = "975ccf83d8d9d0d84682850a38c8169027be83368805971cc4f238c2b245bc98" dependencies = [ "cfg-if 1.0.0", "libc", - "redox_syscall 0.2.9", + "redox_syscall", "winapi 0.3.9", ] @@ -618,12 +783,34 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" +[[package]] +name = "fts-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d31ec9f1580e270ee49a1fae7b102f54514142d9be2d4aa363c361363d65cac9" +dependencies = [ + "bindgen", + "libc", +] + [[package]] name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +[[package]] +name = "funty" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" + +[[package]] +name = "gcd" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c7cd301bf2ab11ae4e5bdfd79c221d97a25e46c089144a62ee9d09cb32d2b92" + [[package]] name = "generic-array" version = "0.8.4" @@ -665,12 +852,6 @@ dependencies = [ "wasi 0.10.2+wasi-snapshot-preview1", ] -[[package]] -name = "glob" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" - [[package]] name = "glob" version = "0.3.0" @@ -696,6 +877,15 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3" +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" +dependencies = [ + "ahash", +] + [[package]] name = "heck" version = "0.3.3" @@ -731,6 +921,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "if_rust_version" version = "1.0.0" @@ -748,9 +944,9 @@ dependencies = [ [[package]] name = "ioctl-sys" -version = "0.5.2" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e2c4b26352496eaaa8ca7cfa9bd99e93419d3f7983dc6e99c2a35fe9e33504a" +checksum = "1c429fffa658f288669529fc26565f728489a2e39bc7b24a428aaaf51355182e" [[package]] name = "itertools" @@ -787,18 +983,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] -name = "libc" -version = "0.2.85" +name = "lazycell" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] -name = "locale" -version = "0.2.2" +name = "libc" +version = "0.2.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fdbe492a9c0238da900a1165c42fc5067161ce292678a6fe80921f30fe307fd" +checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21" + +[[package]] +name = "libloading" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f84d96438c15fcd6c3f244c8fce01d1e2b9c6b5623e9c711dc9286d8fc92d6a" dependencies = [ - "libc", + "cfg-if 1.0.0", + "winapi 0.3.9", ] [[package]] @@ -861,6 +1064,15 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +[[package]] +name = "memmap2" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4647a11b578fead29cdbb34d4adef8dd3dc35b876c9c6d5240d83f205abfe96e" +dependencies = [ + "libc", +] + [[package]] name = "memoffset" version = "0.6.4" @@ -894,15 +1106,14 @@ dependencies = [ [[package]] name = "nix" -version = "0.13.1" +version = "0.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dbdc256eaac2e3bd236d93ad999d3479ef775c863dbda3068c4006a92eec51b" +checksum = "b2ccba0cfe4fdf15982d1674c69b1fd80bad427d293849982668dfe454bd61f2" dependencies = [ "bitflags", "cc", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "libc", - "void", ] [[package]] @@ -917,12 +1128,37 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c3728fec49d363a50a8828a190b379a446cc5cf085c06259bbbeb34447e4ec7" +dependencies = [ + "bitflags", + "cc", + "cfg-if 1.0.0", + "libc", + "memoffset", +] + [[package]] name = "nodrop" version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "nom" +version = "6.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7413f999671bd4745a7b624bd370a569fb6bc574b23c83a3c5ed2e453f3d5e2" +dependencies = [ + "bitvec", + "funty", + "memchr 2.4.0", + "version_check", +] + [[package]] name = "ntapi" version = "0.3.6" @@ -934,9 +1170,9 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.4.0" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e0d047c1062aa51e256408c560894e5251f08925980e53cf1aa5bd00eec6512" +checksum = "74e768dff5fb39a41b3bcd30bb25cf989706c90d028d1ad71971987aa309d535" dependencies = [ "autocfg", "num-integer", @@ -972,6 +1208,28 @@ dependencies = [ "libc", ] +[[package]] +name = "num_enum" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9bd055fb730c4f8f4f57d45d35cd6b3f0980535b056dc7ff119cee6a66ed6f" +dependencies = [ + "derivative", + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "486ea01961c4a818096de679a8b740b26d9033146ac5291b1c98557658f8cdd9" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote 1.0.9", + "syn", +] + [[package]] name = "number_prefix" version = "0.4.0" @@ -1013,20 +1271,31 @@ dependencies = [ ] [[package]] -name = "ouroboros" -version = "0.9.5" +name = "ordered-multimap" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbeff60e3e37407a80ead3e9458145b456e978c4068cddbfea6afb48572962ca" +checksum = "1c672c7ad9ec066e428c00eb917124a06f08db19e2584de982cc34b1f4c12485" dependencies = [ + "dlv-list", + "hashbrown", +] + +[[package]] +name = "ouroboros" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84236d64f1718c387232287cf036eb6632a5ecff226f4ff9dccb8c2b79ba0bde" +dependencies = [ + "aliasable", "ouroboros_macro", "stable_deref_trait", ] [[package]] name = "ouroboros_macro" -version = "0.9.5" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03f2cb802b5bdfdf52f1ffa0b54ce105e4d346e91990dd571f86c91321ad49e2" +checksum = "f463857a6eb96c0136b1d56e56c718350cef30412ec065b48294799a088bca68" dependencies = [ "Inflector", "proc-macro-error", @@ -1064,7 +1333,7 @@ dependencies = [ "cfg-if 1.0.0", "instant", "libc", - "redox_syscall 0.2.9", + "redox_syscall", "smallvec 1.6.1", "winapi 0.3.9", ] @@ -1088,6 +1357,12 @@ dependencies = [ "proc-macro-hack", ] +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + [[package]] name = "pkg-config" version = "0.3.19" @@ -1122,6 +1397,16 @@ dependencies = [ "output_vt100", ] +[[package]] +name = "proc-macro-crate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fdbd1df62156fbc5945f4762632564d7d038153091c3fcf1067f6aef7cff92" +dependencies = [ + "thiserror", + "toml", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1154,9 +1439,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro2" -version = "1.0.27" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" dependencies = [ "unicode-xid 0.2.2", ] @@ -1179,7 +1464,7 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f" dependencies = [ - "env_logger", + "env_logger 0.7.1", "log", "rand 0.7.3", "rand_core 0.5.1", @@ -1200,6 +1485,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" + [[package]] name = "rand" version = "0.5.6" @@ -1346,15 +1637,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.1.57" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" - -[[package]] -name = "redox_syscall" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" dependencies = [ "bitflags", ] @@ -1365,9 +1650,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f" dependencies = [ - "redox_syscall 0.2.9", + "redox_syscall", ] +[[package]] +name = "reference-counted-singleton" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef445213a92fdddc4bc69d9111156d20ffd50704a86ad82b372aab701a0d3a9a" + [[package]] name = "regex" version = "1.5.4" @@ -1418,9 +1709,19 @@ dependencies = [ [[package]] name = "rust-ini" -version = "0.13.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2" +checksum = "63471c4aa97a1cf8332a5f97709a79a4234698de6a1f5087faf66f2dae810e22" +dependencies = [ + "cfg-if 1.0.0", + "ordered-multimap", +] + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "same-file" @@ -1437,6 +1738,52 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "selinux" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cf704a543fe60d898f3253f1cc37655d0f0e9cdb68ef6230557e0e031b80608" +dependencies = [ + "bitflags", + "libc", + "once_cell", + "reference-counted-singleton", + "selinux-sys", + "thiserror", +] + +[[package]] +name = "selinux-sys" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d842d177120716580c4c6cb56dfe3c5f3a3e3dcec635091f1b2034b6c0be4c6" +dependencies = [ + "bindgen", + "cc", + "dunce", + "walkdir", +] + +[[package]] +name = "serde" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +dependencies = [ + "proc-macro2", + "quote 1.0.9", + "syn", +] + [[package]] name = "sha1" version = "0.6.0" @@ -1468,6 +1815,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "shlex" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42a568c8f2cd051a4d283bd6eb0343ac214c1b0f1ac19f93e1175b2dee38c73d" + [[package]] name = "signal-hook" version = "0.3.9" @@ -1513,6 +1866,12 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +[[package]] +name = "smawk" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043" + [[package]] name = "socket2" version = "0.3.19" @@ -1538,15 +1897,15 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "strum" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c" +checksum = "aaf86bbcfd1fa9670b7a129f64fc0c9fcbbfe4f1bc4210e9e98fe71ffc12cde2" [[package]] name = "strum_macros" -version = "0.20.1" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149" +checksum = "d06aaeeee809dbc59eb4556183dd927df67db1540de5be8d3ec0b6636358a5ec" dependencies = [ "heck", "proc-macro2", @@ -1556,15 +1915,21 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.73" +version = "1.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" +checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" dependencies = [ "proc-macro2", "quote 1.0.9", "unicode-xid 0.2.2", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tempfile" version = "3.2.0" @@ -1574,7 +1939,7 @@ dependencies = [ "cfg-if 1.0.0", "libc", "rand 0.8.4", - "redox_syscall 0.2.9", + "redox_syscall", "remove_dir_all", "winapi 0.3.9", ] @@ -1598,6 +1963,25 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "terminal_size" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +dependencies = [ + "libc", + "winapi 0.3.9", +] + [[package]] name = "termion" version = "1.5.6" @@ -1606,7 +1990,7 @@ checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e" dependencies = [ "libc", "numtoa", - "redox_syscall 0.2.9", + "redox_syscall", "redox_termios", ] @@ -1633,6 +2017,18 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "textwrap" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" +dependencies = [ + "smawk", + "terminal_size", + "unicode-linebreak", + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.26" @@ -1663,12 +2059,30 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "typenum" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" +[[package]] +name = "unicode-linebreak" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a52dcaab0c48d931f7cc8ef826fa51690a08e1ea55117ef26f89864f532383f" +dependencies = [ + "regex", +] + [[package]] name = "unicode-segmentation" version = "1.8.0" @@ -1719,9 +2133,21 @@ dependencies = [ "log", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf8-width" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cf7d77f457ef8dfa11e4cd5933c5ddb5dc52a94664071951219a97710f0a32b" + [[package]] name = "uu_arch" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "platform-info", @@ -1731,7 +2157,7 @@ dependencies = [ [[package]] name = "uu_base32" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -1740,7 +2166,7 @@ dependencies = [ [[package]] name = "uu_base64" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uu_base32", @@ -1750,16 +2176,26 @@ dependencies = [ [[package]] name = "uu_basename" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", "uucore_procs", ] +[[package]] +name = "uu_basenc" +version = "0.0.8" +dependencies = [ + "clap", + "uu_base32", + "uucore", + "uucore_procs", +] + [[package]] name = "uu_cat" -version = "0.0.7" +version = "0.0.8" dependencies = [ "atty", "clap", @@ -1768,21 +2204,34 @@ dependencies = [ "unix_socket", "uucore", "uucore_procs", + "winapi-util", +] + +[[package]] +name = "uu_chcon" +version = "0.0.8" +dependencies = [ + "clap", + "fts-sys", + "libc", + "selinux", + "thiserror", + "uucore", + "uucore_procs", ] [[package]] name = "uu_chgrp" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", "uucore_procs", - "walkdir", ] [[package]] name = "uu_chmod" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -1793,18 +2242,16 @@ dependencies = [ [[package]] name = "uu_chown" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", - "glob 0.3.0", "uucore", "uucore_procs", - "walkdir", ] [[package]] name = "uu_chroot" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -1813,7 +2260,7 @@ dependencies = [ [[package]] name = "uu_cksum" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -1823,7 +2270,7 @@ dependencies = [ [[package]] name = "uu_comm" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -1833,13 +2280,15 @@ dependencies = [ [[package]] name = "uu_cp" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", + "exacl", "filetime", "ioctl-sys", "libc", "quick-error 1.2.3", + "selinux", "uucore", "uucore_procs", "walkdir", @@ -1849,10 +2298,9 @@ dependencies = [ [[package]] name = "uu_csplit" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", - "glob 0.2.11", "regex", "thiserror", "uucore", @@ -1861,7 +2309,7 @@ dependencies = [ [[package]] name = "uu_cut" -version = "0.0.7" +version = "0.0.8" dependencies = [ "atty", "bstr", @@ -1873,7 +2321,7 @@ dependencies = [ [[package]] name = "uu_date" -version = "0.0.7" +version = "0.0.8" dependencies = [ "chrono", "clap", @@ -1883,9 +2331,23 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "uu_dd" +version = "0.0.8" +dependencies = [ + "byte-unit", + "clap", + "gcd", + "libc", + "signal-hook", + "tempfile", + "uucore", + "uucore_procs", +] + [[package]] name = "uu_df" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "number_prefix", @@ -1895,17 +2357,17 @@ dependencies = [ [[package]] name = "uu_dircolors" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", - "glob 0.3.0", + "glob", "uucore", "uucore_procs", ] [[package]] name = "uu_dirname" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -1915,7 +2377,7 @@ dependencies = [ [[package]] name = "uu_du" -version = "0.0.7" +version = "0.0.8" dependencies = [ "chrono", "clap", @@ -1926,7 +2388,7 @@ dependencies = [ [[package]] name = "uu_echo" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -1935,7 +2397,7 @@ dependencies = [ [[package]] name = "uu_env" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -1946,7 +2408,7 @@ dependencies = [ [[package]] name = "uu_expand" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "unicode-width", @@ -1956,7 +2418,7 @@ dependencies = [ [[package]] name = "uu_expr" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -1969,7 +2431,7 @@ dependencies = [ [[package]] name = "uu_factor" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "coz", @@ -1984,7 +2446,7 @@ dependencies = [ [[package]] name = "uu_false" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -1993,7 +2455,7 @@ dependencies = [ [[package]] name = "uu_fmt" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2004,7 +2466,7 @@ dependencies = [ [[package]] name = "uu_fold" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2013,7 +2475,7 @@ dependencies = [ [[package]] name = "uu_groups" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2022,7 +2484,7 @@ dependencies = [ [[package]] name = "uu_hashsum" -version = "0.0.7" +version = "0.0.8" dependencies = [ "blake2b_simd", "clap", @@ -2030,6 +2492,7 @@ dependencies = [ "hex", "libc", "md5", + "memchr 2.4.0", "regex", "regex-syntax", "sha1", @@ -2041,16 +2504,17 @@ dependencies = [ [[package]] name = "uu_head" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", + "memchr 2.4.0", "uucore", "uucore_procs", ] [[package]] name = "uu_hostid" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2060,7 +2524,7 @@ dependencies = [ [[package]] name = "uu_hostname" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "hostname", @@ -2072,16 +2536,17 @@ dependencies = [ [[package]] name = "uu_id" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", + "selinux", "uucore", "uucore_procs", ] [[package]] name = "uu_install" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "file_diff", @@ -2094,7 +2559,7 @@ dependencies = [ [[package]] name = "uu_join" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2103,7 +2568,7 @@ dependencies = [ [[package]] name = "uu_kill" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2113,7 +2578,7 @@ dependencies = [ [[package]] name = "uu_link" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2123,7 +2588,7 @@ dependencies = [ [[package]] name = "uu_ln" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2133,7 +2598,7 @@ dependencies = [ [[package]] name = "uu_logname" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2143,17 +2608,17 @@ dependencies = [ [[package]] name = "uu_ls" -version = "0.0.7" +version = "0.0.8" dependencies = [ "atty", "chrono", "clap", "globset", "lazy_static", - "locale", "lscolors", "number_prefix", "once_cell", + "selinux", "term_grid", "termsize", "unicode-width", @@ -2163,7 +2628,7 @@ dependencies = [ [[package]] name = "uu_mkdir" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2173,7 +2638,7 @@ dependencies = [ [[package]] name = "uu_mkfifo" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2183,7 +2648,7 @@ dependencies = [ [[package]] name = "uu_mknod" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2193,7 +2658,7 @@ dependencies = [ [[package]] name = "uu_mktemp" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "rand 0.5.6", @@ -2204,13 +2669,13 @@ dependencies = [ [[package]] name = "uu_more" -version = "0.0.7" +version = "0.0.8" dependencies = [ "atty", "clap", "crossterm", - "nix 0.13.1", - "redox_syscall 0.1.57", + "nix 0.19.1", + "redox_syscall", "redox_termios", "unicode-segmentation", "unicode-width", @@ -2220,7 +2685,7 @@ dependencies = [ [[package]] name = "uu_mv" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "fs_extra", @@ -2230,18 +2695,18 @@ dependencies = [ [[package]] name = "uu_nice" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", - "nix 0.13.1", + "nix 0.20.0", "uucore", "uucore_procs", ] [[package]] name = "uu_nl" -version = "0.0.7" +version = "0.0.8" dependencies = [ "aho-corasick", "clap", @@ -2255,7 +2720,7 @@ dependencies = [ [[package]] name = "uu_nohup" -version = "0.0.7" +version = "0.0.8" dependencies = [ "atty", "clap", @@ -2266,7 +2731,7 @@ dependencies = [ [[package]] name = "uu_nproc" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2277,7 +2742,7 @@ dependencies = [ [[package]] name = "uu_numfmt" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2286,7 +2751,7 @@ dependencies = [ [[package]] name = "uu_od" -version = "0.0.7" +version = "0.0.8" dependencies = [ "byteorder", "clap", @@ -2298,7 +2763,7 @@ dependencies = [ [[package]] name = "uu_paste" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2307,7 +2772,7 @@ dependencies = [ [[package]] name = "uu_pathchk" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2317,7 +2782,7 @@ dependencies = [ [[package]] name = "uu_pinky" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2326,7 +2791,7 @@ dependencies = [ [[package]] name = "uu_pr" -version = "0.0.7" +version = "0.0.8" dependencies = [ "chrono", "clap", @@ -2334,14 +2799,13 @@ dependencies = [ "itertools 0.10.1", "quick-error 2.0.1", "regex", - "time", "uucore", "uucore_procs", ] [[package]] name = "uu_printenv" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2350,7 +2814,7 @@ dependencies = [ [[package]] name = "uu_printf" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "itertools 0.8.2", @@ -2360,7 +2824,7 @@ dependencies = [ [[package]] name = "uu_ptx" -version = "0.0.7" +version = "0.0.8" dependencies = [ "aho-corasick", "clap", @@ -2374,7 +2838,7 @@ dependencies = [ [[package]] name = "uu_pwd" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2383,7 +2847,7 @@ dependencies = [ [[package]] name = "uu_readlink" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2393,7 +2857,7 @@ dependencies = [ [[package]] name = "uu_realpath" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2402,7 +2866,7 @@ dependencies = [ [[package]] name = "uu_relpath" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2411,7 +2875,7 @@ dependencies = [ [[package]] name = "uu_rm" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "remove_dir_all", @@ -2423,16 +2887,30 @@ dependencies = [ [[package]] name = "uu_rmdir" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", + "libc", + "uucore", + "uucore_procs", +] + +[[package]] +name = "uu_runcon" +version = "0.0.8" +dependencies = [ + "clap", + "fts-sys", + "libc", + "selinux", + "thiserror", "uucore", "uucore_procs", ] [[package]] name = "uu_seq" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "num-bigint", @@ -2443,20 +2921,18 @@ dependencies = [ [[package]] name = "uu_shred" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", - "filetime", "libc", - "rand 0.5.6", - "time", + "rand 0.7.3", "uucore", "uucore_procs", ] [[package]] name = "uu_shuf" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "rand 0.5.6", @@ -2466,7 +2942,7 @@ dependencies = [ [[package]] name = "uu_sleep" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2475,11 +2951,12 @@ dependencies = [ [[package]] name = "uu_sort" -version = "0.0.7" +version = "0.0.8" dependencies = [ "binary-heap-plus", "clap", "compare", + "ctrlc", "fnv", "itertools 0.10.1", "memchr 2.4.0", @@ -2494,7 +2971,7 @@ dependencies = [ [[package]] name = "uu_split" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2503,7 +2980,7 @@ dependencies = [ [[package]] name = "uu_stat" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2512,7 +2989,7 @@ dependencies = [ [[package]] name = "uu_stdbuf" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "tempfile", @@ -2523,7 +3000,7 @@ dependencies = [ [[package]] name = "uu_stdbuf_libstdbuf" -version = "0.0.7" +version = "0.0.8" dependencies = [ "cpp", "cpp_build", @@ -2534,7 +3011,7 @@ dependencies = [ [[package]] name = "uu_sum" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2543,7 +3020,7 @@ dependencies = [ [[package]] name = "uu_sync" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2554,20 +3031,24 @@ dependencies = [ [[package]] name = "uu_tac" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", + "memchr 2.4.0", + "memmap2", + "regex", "uucore", "uucore_procs", ] [[package]] name = "uu_tail" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", - "redox_syscall 0.1.57", + "nix 0.20.0", + "redox_syscall", "uucore", "uucore_procs", "winapi 0.3.9", @@ -2575,7 +3056,7 @@ dependencies = [ [[package]] name = "uu_tee" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2586,18 +3067,18 @@ dependencies = [ [[package]] name = "uu_test" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", - "redox_syscall 0.1.57", + "redox_syscall", "uucore", "uucore_procs", ] [[package]] name = "uu_timeout" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "libc", @@ -2608,7 +3089,7 @@ dependencies = [ [[package]] name = "uu_touch" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "filetime", @@ -2619,7 +3100,7 @@ dependencies = [ [[package]] name = "uu_tr" -version = "0.0.7" +version = "0.0.8" dependencies = [ "bit-set", "clap", @@ -2630,7 +3111,7 @@ dependencies = [ [[package]] name = "uu_true" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2639,7 +3120,7 @@ dependencies = [ [[package]] name = "uu_truncate" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2648,7 +3129,7 @@ dependencies = [ [[package]] name = "uu_tsort" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2657,7 +3138,7 @@ dependencies = [ [[package]] name = "uu_tty" -version = "0.0.7" +version = "0.0.8" dependencies = [ "atty", "clap", @@ -2668,7 +3149,7 @@ dependencies = [ [[package]] name = "uu_uname" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "platform-info", @@ -2678,7 +3159,7 @@ dependencies = [ [[package]] name = "uu_unexpand" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "unicode-width", @@ -2688,7 +3169,7 @@ dependencies = [ [[package]] name = "uu_uniq" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "strum", @@ -2699,17 +3180,16 @@ dependencies = [ [[package]] name = "uu_unlink" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", - "libc", "uucore", "uucore_procs", ] [[package]] name = "uu_uptime" -version = "0.0.7" +version = "0.0.8" dependencies = [ "chrono", "clap", @@ -2719,7 +3199,7 @@ dependencies = [ [[package]] name = "uu_users" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2728,19 +3208,21 @@ dependencies = [ [[package]] name = "uu_wc" -version = "0.0.7" +version = "0.0.8" dependencies = [ + "bytecount", "clap", "libc", "nix 0.20.0", - "thiserror", + "unicode-width", + "utf-8", "uucore", "uucore_procs", ] [[package]] name = "uu_who" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", "uucore", @@ -2749,9 +3231,10 @@ dependencies = [ [[package]] name = "uu_whoami" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", + "libc", "uucore", "uucore_procs", "winapi 0.3.9", @@ -2759,42 +3242,52 @@ dependencies = [ [[package]] name = "uu_yes" -version = "0.0.7" +version = "0.0.8" dependencies = [ "clap", + "nix 0.20.0", "uucore", "uucore_procs", ] [[package]] name = "uucore" -version = "0.0.9" +version = "0.0.10" dependencies = [ "clap", "data-encoding", + "data-encoding-macro", "dns-lookup", "dunce", "getopts", "lazy_static", "libc", - "nix 0.13.1", - "platform-info", + "nix 0.20.0", + "once_cell", "termion", "thiserror", "time", + "walkdir", "wild", "winapi 0.3.9", + "z85", ] [[package]] name = "uucore_procs" -version = "0.0.6" +version = "0.0.7" dependencies = [ "proc-macro2", "quote 1.0.9", "syn", ] +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" + [[package]] name = "vec_map" version = "0.8.2" @@ -2807,12 +3300,6 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - [[package]] name = "walkdir" version = "2.3.2" @@ -2836,13 +3323,22 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "which" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" +dependencies = [ + "libc", +] + [[package]] name = "wild" version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "035793abb854745033f01a07647a79831eba29ec0be377205f2a25b0aa830020" dependencies = [ - "glob 0.3.0", + "glob", ] [[package]] @@ -2888,6 +3384,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "wyz" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" + [[package]] name = "xattr" version = "0.2.2" @@ -2896,3 +3398,9 @@ checksum = "244c3741f4240ef46274860397c7c74e50eb23624996930e484c16679633a54c" dependencies = [ "libc", ] + +[[package]] +name = "z85" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ac8b56e4f9906a4ef5412875e9ce448364023335cec645fd457ecf51d4f2781" diff --git a/Cargo.toml b/Cargo.toml index 46931b205..9ecea79c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,11 @@ # coreutils (uutils) # * see the repository LICENSE, README, and CONTRIBUTING files for more information +# spell-checker:ignore (libs) libselinux + [package] name = "coreutils" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "coreutils ~ GNU coreutils (updated); implemented as universal (cross-platform) utils, written in Rust" @@ -33,6 +35,7 @@ feat_common_core = [ "base32", "base64", "basename", + "basenc", "cat", "cksum", "comm", @@ -43,6 +46,7 @@ feat_common_core = [ "df", "dircolors", "dirname", + "dd", "du", "echo", "env", @@ -91,8 +95,10 @@ feat_common_core = [ "true", "truncate", "tsort", + "touch", "unexpand", "uniq", + "unlink", "wc", "yes", ] @@ -138,6 +144,16 @@ feat_os_unix_musl = [ # "feat_require_unix", ] +# "feat_selinux" == set of utilities providing support for SELinux Security Context if enabled with `--features feat_selinux`. +# NOTE: +# The selinux(-sys) crate requires `libselinux` headers and shared library to be accessible in the C toolchain at compile time. +# Running a uutils compiled with `feat_selinux` requires an SELinux enabled Kernel at run time. +feat_selinux = ["cp/selinux", "id/selinux", "ls/selinux", "selinux", "feat_require_selinux"] +# "feat_acl" == set of utilities providing support for acl (access control lists) if enabled with `--features feat_acl`. +# NOTE: +# On linux, the posix-acl/acl-sys crate requires `libacl` headers and shared library to be accessible in the C toolchain at compile time. +# On FreeBSD and macOS this is not required. +feat_acl = ["cp/feat_acl"] ## feature sets with requirements (restricting cross-platform availability) # # ** NOTE: these `feat_require_...` sets should be minimized as much as possible to encourage cross-platform availability of utilities @@ -167,7 +183,6 @@ feat_require_unix = [ "timeout", "tty", "uname", - "unlink", ] # "feat_require_unix_utmpx" == set of utilities requiring unix utmp/utmpx support # * ref: @@ -177,6 +192,11 @@ feat_require_unix_utmpx = [ "users", "who", ] +# "feat_require_selinux" == set of utilities depending on SELinux. +feat_require_selinux = [ + "chcon", + "runcon", +] ## (alternate/newer/smaller platforms) feature sets # "feat_os_unix_fuchsia" == set of utilities which can be built/run on the "Fuchsia" OS (refs: ; ) feat_os_unix_fuchsia = [ @@ -204,9 +224,8 @@ feat_os_unix_fuchsia = [ feat_os_unix_redox = [ "feat_common_core", # - "uname", "chmod", - "install", + "uname", ] # "feat_os_windows_legacy" == slightly restricted set of utilities which can be built/run on early windows platforms (eg, "WinXP") feat_os_windows_legacy = [ @@ -227,106 +246,111 @@ test = [ "uu_test" ] [dependencies] clap = { version = "2.33", features = ["wrap_help"] } lazy_static = { version="1.3" } -textwrap = { version="=0.11.0", features=["term_size"] } # !maint: [2020-05-10; rivy] unstable crate using undocumented features; pinned currently, will review -uucore = { version=">=0.0.9", package="uucore", path="src/uucore" } +textwrap = { version="0.14", features=["terminal_size"] } +uucore = { version=">=0.0.10", package="uucore", path="src/uucore" } +selinux = { version="0.2.3", optional = true } # * uutils -uu_test = { optional=true, version="0.0.7", package="uu_test", path="src/uu/test" } +uu_test = { optional=true, version="0.0.8", package="uu_test", path="src/uu/test" } # -arch = { optional=true, version="0.0.7", package="uu_arch", path="src/uu/arch" } -base32 = { optional=true, version="0.0.7", package="uu_base32", path="src/uu/base32" } -base64 = { optional=true, version="0.0.7", package="uu_base64", path="src/uu/base64" } -basename = { optional=true, version="0.0.7", package="uu_basename", path="src/uu/basename" } -cat = { optional=true, version="0.0.7", package="uu_cat", path="src/uu/cat" } -chgrp = { optional=true, version="0.0.7", package="uu_chgrp", path="src/uu/chgrp" } -chmod = { optional=true, version="0.0.7", package="uu_chmod", path="src/uu/chmod" } -chown = { optional=true, version="0.0.7", package="uu_chown", path="src/uu/chown" } -chroot = { optional=true, version="0.0.7", package="uu_chroot", path="src/uu/chroot" } -cksum = { optional=true, version="0.0.7", package="uu_cksum", path="src/uu/cksum" } -comm = { optional=true, version="0.0.7", package="uu_comm", path="src/uu/comm" } -cp = { optional=true, version="0.0.7", package="uu_cp", path="src/uu/cp" } -csplit = { optional=true, version="0.0.7", package="uu_csplit", path="src/uu/csplit" } -cut = { optional=true, version="0.0.7", package="uu_cut", path="src/uu/cut" } -date = { optional=true, version="0.0.7", package="uu_date", path="src/uu/date" } -df = { optional=true, version="0.0.7", package="uu_df", path="src/uu/df" } -dircolors= { optional=true, version="0.0.7", package="uu_dircolors", path="src/uu/dircolors" } -dirname = { optional=true, version="0.0.7", package="uu_dirname", path="src/uu/dirname" } -du = { optional=true, version="0.0.7", package="uu_du", path="src/uu/du" } -echo = { optional=true, version="0.0.7", package="uu_echo", path="src/uu/echo" } -env = { optional=true, version="0.0.7", package="uu_env", path="src/uu/env" } -expand = { optional=true, version="0.0.7", package="uu_expand", path="src/uu/expand" } -expr = { optional=true, version="0.0.7", package="uu_expr", path="src/uu/expr" } -factor = { optional=true, version="0.0.7", package="uu_factor", path="src/uu/factor" } -false = { optional=true, version="0.0.7", package="uu_false", path="src/uu/false" } -fmt = { optional=true, version="0.0.7", package="uu_fmt", path="src/uu/fmt" } -fold = { optional=true, version="0.0.7", package="uu_fold", path="src/uu/fold" } -groups = { optional=true, version="0.0.7", package="uu_groups", path="src/uu/groups" } -hashsum = { optional=true, version="0.0.7", package="uu_hashsum", path="src/uu/hashsum" } -head = { optional=true, version="0.0.7", package="uu_head", path="src/uu/head" } -hostid = { optional=true, version="0.0.7", package="uu_hostid", path="src/uu/hostid" } -hostname = { optional=true, version="0.0.7", package="uu_hostname", path="src/uu/hostname" } -id = { optional=true, version="0.0.7", package="uu_id", path="src/uu/id" } -install = { optional=true, version="0.0.7", package="uu_install", path="src/uu/install" } -join = { optional=true, version="0.0.7", package="uu_join", path="src/uu/join" } -kill = { optional=true, version="0.0.7", package="uu_kill", path="src/uu/kill" } -link = { optional=true, version="0.0.7", package="uu_link", path="src/uu/link" } -ln = { optional=true, version="0.0.7", package="uu_ln", path="src/uu/ln" } -ls = { optional=true, version="0.0.7", package="uu_ls", path="src/uu/ls" } -logname = { optional=true, version="0.0.7", package="uu_logname", path="src/uu/logname" } -mkdir = { optional=true, version="0.0.7", package="uu_mkdir", path="src/uu/mkdir" } -mkfifo = { optional=true, version="0.0.7", package="uu_mkfifo", path="src/uu/mkfifo" } -mknod = { optional=true, version="0.0.7", package="uu_mknod", path="src/uu/mknod" } -mktemp = { optional=true, version="0.0.7", package="uu_mktemp", path="src/uu/mktemp" } -more = { optional=true, version="0.0.7", package="uu_more", path="src/uu/more" } -mv = { optional=true, version="0.0.7", package="uu_mv", path="src/uu/mv" } -nice = { optional=true, version="0.0.7", package="uu_nice", path="src/uu/nice" } -nl = { optional=true, version="0.0.7", package="uu_nl", path="src/uu/nl" } -nohup = { optional=true, version="0.0.7", package="uu_nohup", path="src/uu/nohup" } -nproc = { optional=true, version="0.0.7", package="uu_nproc", path="src/uu/nproc" } -numfmt = { optional=true, version="0.0.7", package="uu_numfmt", path="src/uu/numfmt" } -od = { optional=true, version="0.0.7", package="uu_od", path="src/uu/od" } -paste = { optional=true, version="0.0.7", package="uu_paste", path="src/uu/paste" } -pathchk = { optional=true, version="0.0.7", package="uu_pathchk", path="src/uu/pathchk" } -pinky = { optional=true, version="0.0.7", package="uu_pinky", path="src/uu/pinky" } -pr = { optional=true, version="0.0.7", package="uu_pr", path="src/uu/pr" } -printenv = { optional=true, version="0.0.7", package="uu_printenv", path="src/uu/printenv" } -printf = { optional=true, version="0.0.7", package="uu_printf", path="src/uu/printf" } -ptx = { optional=true, version="0.0.7", package="uu_ptx", path="src/uu/ptx" } -pwd = { optional=true, version="0.0.7", package="uu_pwd", path="src/uu/pwd" } -readlink = { optional=true, version="0.0.7", package="uu_readlink", path="src/uu/readlink" } -realpath = { optional=true, version="0.0.7", package="uu_realpath", path="src/uu/realpath" } -relpath = { optional=true, version="0.0.7", package="uu_relpath", path="src/uu/relpath" } -rm = { optional=true, version="0.0.7", package="uu_rm", path="src/uu/rm" } -rmdir = { optional=true, version="0.0.7", package="uu_rmdir", path="src/uu/rmdir" } -seq = { optional=true, version="0.0.7", package="uu_seq", path="src/uu/seq" } -shred = { optional=true, version="0.0.7", package="uu_shred", path="src/uu/shred" } -shuf = { optional=true, version="0.0.7", package="uu_shuf", path="src/uu/shuf" } -sleep = { optional=true, version="0.0.7", package="uu_sleep", path="src/uu/sleep" } -sort = { optional=true, version="0.0.7", package="uu_sort", path="src/uu/sort" } -split = { optional=true, version="0.0.7", package="uu_split", path="src/uu/split" } -stat = { optional=true, version="0.0.7", package="uu_stat", path="src/uu/stat" } -stdbuf = { optional=true, version="0.0.7", package="uu_stdbuf", path="src/uu/stdbuf" } -sum = { optional=true, version="0.0.7", package="uu_sum", path="src/uu/sum" } -sync = { optional=true, version="0.0.7", package="uu_sync", path="src/uu/sync" } -tac = { optional=true, version="0.0.7", package="uu_tac", path="src/uu/tac" } -tail = { optional=true, version="0.0.7", package="uu_tail", path="src/uu/tail" } -tee = { optional=true, version="0.0.7", package="uu_tee", path="src/uu/tee" } -timeout = { optional=true, version="0.0.7", package="uu_timeout", path="src/uu/timeout" } -touch = { optional=true, version="0.0.7", package="uu_touch", path="src/uu/touch" } -tr = { optional=true, version="0.0.7", package="uu_tr", path="src/uu/tr" } -true = { optional=true, version="0.0.7", package="uu_true", path="src/uu/true" } -truncate = { optional=true, version="0.0.7", package="uu_truncate", path="src/uu/truncate" } -tsort = { optional=true, version="0.0.7", package="uu_tsort", path="src/uu/tsort" } -tty = { optional=true, version="0.0.7", package="uu_tty", path="src/uu/tty" } -uname = { optional=true, version="0.0.7", package="uu_uname", path="src/uu/uname" } -unexpand = { optional=true, version="0.0.7", package="uu_unexpand", path="src/uu/unexpand" } -uniq = { optional=true, version="0.0.7", package="uu_uniq", path="src/uu/uniq" } -unlink = { optional=true, version="0.0.7", package="uu_unlink", path="src/uu/unlink" } -uptime = { optional=true, version="0.0.7", package="uu_uptime", path="src/uu/uptime" } -users = { optional=true, version="0.0.7", package="uu_users", path="src/uu/users" } -wc = { optional=true, version="0.0.7", package="uu_wc", path="src/uu/wc" } -who = { optional=true, version="0.0.7", package="uu_who", path="src/uu/who" } -whoami = { optional=true, version="0.0.7", package="uu_whoami", path="src/uu/whoami" } -yes = { optional=true, version="0.0.7", package="uu_yes", path="src/uu/yes" } +arch = { optional=true, version="0.0.8", package="uu_arch", path="src/uu/arch" } +base32 = { optional=true, version="0.0.8", package="uu_base32", path="src/uu/base32" } +base64 = { optional=true, version="0.0.8", package="uu_base64", path="src/uu/base64" } +basename = { optional=true, version="0.0.8", package="uu_basename", path="src/uu/basename" } +basenc = { optional=true, version="0.0.8", package="uu_basenc", path="src/uu/basenc" } +cat = { optional=true, version="0.0.8", package="uu_cat", path="src/uu/cat" } +chcon = { optional=true, version="0.0.8", package="uu_chcon", path="src/uu/chcon" } +chgrp = { optional=true, version="0.0.8", package="uu_chgrp", path="src/uu/chgrp" } +chmod = { optional=true, version="0.0.8", package="uu_chmod", path="src/uu/chmod" } +chown = { optional=true, version="0.0.8", package="uu_chown", path="src/uu/chown" } +chroot = { optional=true, version="0.0.8", package="uu_chroot", path="src/uu/chroot" } +cksum = { optional=true, version="0.0.8", package="uu_cksum", path="src/uu/cksum" } +comm = { optional=true, version="0.0.8", package="uu_comm", path="src/uu/comm" } +cp = { optional=true, version="0.0.8", package="uu_cp", path="src/uu/cp" } +csplit = { optional=true, version="0.0.8", package="uu_csplit", path="src/uu/csplit" } +cut = { optional=true, version="0.0.8", package="uu_cut", path="src/uu/cut" } +date = { optional=true, version="0.0.8", package="uu_date", path="src/uu/date" } +dd = { optional=true, version="0.0.8", package="uu_dd", path="src/uu/dd" } +df = { optional=true, version="0.0.8", package="uu_df", path="src/uu/df" } +dircolors= { optional=true, version="0.0.8", package="uu_dircolors", path="src/uu/dircolors" } +dirname = { optional=true, version="0.0.8", package="uu_dirname", path="src/uu/dirname" } +du = { optional=true, version="0.0.8", package="uu_du", path="src/uu/du" } +echo = { optional=true, version="0.0.8", package="uu_echo", path="src/uu/echo" } +env = { optional=true, version="0.0.8", package="uu_env", path="src/uu/env" } +expand = { optional=true, version="0.0.8", package="uu_expand", path="src/uu/expand" } +expr = { optional=true, version="0.0.8", package="uu_expr", path="src/uu/expr" } +factor = { optional=true, version="0.0.8", package="uu_factor", path="src/uu/factor" } +false = { optional=true, version="0.0.8", package="uu_false", path="src/uu/false" } +fmt = { optional=true, version="0.0.8", package="uu_fmt", path="src/uu/fmt" } +fold = { optional=true, version="0.0.8", package="uu_fold", path="src/uu/fold" } +groups = { optional=true, version="0.0.8", package="uu_groups", path="src/uu/groups" } +hashsum = { optional=true, version="0.0.8", package="uu_hashsum", path="src/uu/hashsum" } +head = { optional=true, version="0.0.8", package="uu_head", path="src/uu/head" } +hostid = { optional=true, version="0.0.8", package="uu_hostid", path="src/uu/hostid" } +hostname = { optional=true, version="0.0.8", package="uu_hostname", path="src/uu/hostname" } +id = { optional=true, version="0.0.8", package="uu_id", path="src/uu/id" } +install = { optional=true, version="0.0.8", package="uu_install", path="src/uu/install" } +join = { optional=true, version="0.0.8", package="uu_join", path="src/uu/join" } +kill = { optional=true, version="0.0.8", package="uu_kill", path="src/uu/kill" } +link = { optional=true, version="0.0.8", package="uu_link", path="src/uu/link" } +ln = { optional=true, version="0.0.8", package="uu_ln", path="src/uu/ln" } +ls = { optional=true, version="0.0.8", package="uu_ls", path="src/uu/ls" } +logname = { optional=true, version="0.0.8", package="uu_logname", path="src/uu/logname" } +mkdir = { optional=true, version="0.0.8", package="uu_mkdir", path="src/uu/mkdir" } +mkfifo = { optional=true, version="0.0.8", package="uu_mkfifo", path="src/uu/mkfifo" } +mknod = { optional=true, version="0.0.8", package="uu_mknod", path="src/uu/mknod" } +mktemp = { optional=true, version="0.0.8", package="uu_mktemp", path="src/uu/mktemp" } +more = { optional=true, version="0.0.8", package="uu_more", path="src/uu/more" } +mv = { optional=true, version="0.0.8", package="uu_mv", path="src/uu/mv" } +nice = { optional=true, version="0.0.8", package="uu_nice", path="src/uu/nice" } +nl = { optional=true, version="0.0.8", package="uu_nl", path="src/uu/nl" } +nohup = { optional=true, version="0.0.8", package="uu_nohup", path="src/uu/nohup" } +nproc = { optional=true, version="0.0.8", package="uu_nproc", path="src/uu/nproc" } +numfmt = { optional=true, version="0.0.8", package="uu_numfmt", path="src/uu/numfmt" } +od = { optional=true, version="0.0.8", package="uu_od", path="src/uu/od" } +paste = { optional=true, version="0.0.8", package="uu_paste", path="src/uu/paste" } +pathchk = { optional=true, version="0.0.8", package="uu_pathchk", path="src/uu/pathchk" } +pinky = { optional=true, version="0.0.8", package="uu_pinky", path="src/uu/pinky" } +pr = { optional=true, version="0.0.8", package="uu_pr", path="src/uu/pr" } +printenv = { optional=true, version="0.0.8", package="uu_printenv", path="src/uu/printenv" } +printf = { optional=true, version="0.0.8", package="uu_printf", path="src/uu/printf" } +ptx = { optional=true, version="0.0.8", package="uu_ptx", path="src/uu/ptx" } +pwd = { optional=true, version="0.0.8", package="uu_pwd", path="src/uu/pwd" } +readlink = { optional=true, version="0.0.8", package="uu_readlink", path="src/uu/readlink" } +realpath = { optional=true, version="0.0.8", package="uu_realpath", path="src/uu/realpath" } +relpath = { optional=true, version="0.0.8", package="uu_relpath", path="src/uu/relpath" } +rm = { optional=true, version="0.0.8", package="uu_rm", path="src/uu/rm" } +rmdir = { optional=true, version="0.0.8", package="uu_rmdir", path="src/uu/rmdir" } +runcon = { optional=true, version="0.0.8", package="uu_runcon", path="src/uu/runcon" } +seq = { optional=true, version="0.0.8", package="uu_seq", path="src/uu/seq" } +shred = { optional=true, version="0.0.8", package="uu_shred", path="src/uu/shred" } +shuf = { optional=true, version="0.0.8", package="uu_shuf", path="src/uu/shuf" } +sleep = { optional=true, version="0.0.8", package="uu_sleep", path="src/uu/sleep" } +sort = { optional=true, version="0.0.8", package="uu_sort", path="src/uu/sort" } +split = { optional=true, version="0.0.8", package="uu_split", path="src/uu/split" } +stat = { optional=true, version="0.0.8", package="uu_stat", path="src/uu/stat" } +stdbuf = { optional=true, version="0.0.8", package="uu_stdbuf", path="src/uu/stdbuf" } +sum = { optional=true, version="0.0.8", package="uu_sum", path="src/uu/sum" } +sync = { optional=true, version="0.0.8", package="uu_sync", path="src/uu/sync" } +tac = { optional=true, version="0.0.8", package="uu_tac", path="src/uu/tac" } +tail = { optional=true, version="0.0.8", package="uu_tail", path="src/uu/tail" } +tee = { optional=true, version="0.0.8", package="uu_tee", path="src/uu/tee" } +timeout = { optional=true, version="0.0.8", package="uu_timeout", path="src/uu/timeout" } +touch = { optional=true, version="0.0.8", package="uu_touch", path="src/uu/touch" } +tr = { optional=true, version="0.0.8", package="uu_tr", path="src/uu/tr" } +true = { optional=true, version="0.0.8", package="uu_true", path="src/uu/true" } +truncate = { optional=true, version="0.0.8", package="uu_truncate", path="src/uu/truncate" } +tsort = { optional=true, version="0.0.8", package="uu_tsort", path="src/uu/tsort" } +tty = { optional=true, version="0.0.8", package="uu_tty", path="src/uu/tty" } +uname = { optional=true, version="0.0.8", package="uu_uname", path="src/uu/uname" } +unexpand = { optional=true, version="0.0.8", package="uu_unexpand", path="src/uu/unexpand" } +uniq = { optional=true, version="0.0.8", package="uu_uniq", path="src/uu/uniq" } +unlink = { optional=true, version="0.0.8", package="uu_unlink", path="src/uu/unlink" } +uptime = { optional=true, version="0.0.8", package="uu_uptime", path="src/uu/uptime" } +users = { optional=true, version="0.0.8", package="uu_users", path="src/uu/users" } +wc = { optional=true, version="0.0.8", package="uu_wc", path="src/uu/wc" } +who = { optional=true, version="0.0.8", package="uu_who", path="src/uu/who" } +whoami = { optional=true, version="0.0.8", package="uu_whoami", path="src/uu/whoami" } +yes = { optional=true, version="0.0.8", package="uu_yes", path="src/uu/yes" } # this breaks clippy linting with: "tests/by-util/test_factor_benches.rs: No such file or directory (os error 2)" # factor_benches = { optional = true, version = "0.0.0", package = "uu_factor_benches", path = "tests/benches/factor" } @@ -342,7 +366,6 @@ conv = "0.3" filetime = "0.2" glob = "0.3.0" libc = "0.2" -nix = "0.20.0" pretty_assertions = "0.7.2" rand = "0.7" regex = "1.0" @@ -350,12 +373,16 @@ sha1 = { version="0.6", features=["std"] } tempfile = "3.2.0" time = "0.1" unindent = "0.1" -uucore = { version=">=0.0.9", package="uucore", path="src/uucore", features=["entries", "process"] } +uucore = { version=">=0.0.10", package="uucore", path="src/uucore", features=["entries", "process"] } walkdir = "2.2" atty = "0.2" -[target.'cfg(unix)'.dev-dependencies] +[target.'cfg(target_os = "linux")'.dev-dependencies] rlimit = "0.4.0" + +[target.'cfg(unix)'.dev-dependencies] +nix = "0.20.0" + rust-users = { version="0.10", package="users" } unix_socket = "0.5.0" diff --git a/DEVELOPER_INSTRUCTIONS.md b/DEVELOPER_INSTRUCTIONS.md index e0a5cf001..027d4dca1 100644 --- a/DEVELOPER_INSTRUCTIONS.md +++ b/DEVELOPER_INSTRUCTIONS.md @@ -1,3 +1,29 @@ +Documentation +------------- + +The source of the documentation is available on: + +https://uutils.github.io/coreutils-docs/coreutils/ + +The documentation is updated everyday on this repository: + +https://github.com/uutils/coreutils-docs + +Running GNU tests +----------------- + + + +- Check out https://github.com/coreutils/coreutils next to your fork as gnu +- Check out https://github.com/coreutils/gnulib next to your fork as gnulib +- Rename the checkout of your fork to uutils + +At the end you should have uutils, gnu and gnulib checked out next to each other. + +- Run `cd uutils && ./util/build-gnu.sh && cd ..` to get everything ready (this may take a while) +- Finally, you can run `tests with bash uutils/util/run-gnu-test.sh `. Instead of `` insert the test you want to run, e.g. `tests/misc/wc-proc`. + + Code Coverage Report Generation --------------------------------- diff --git a/GNUmakefile b/GNUmakefile index 996c568f3..367568ca8 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -46,6 +46,15 @@ BUSYBOX_ROOT := $(BASEDIR)/tmp BUSYBOX_VER := 1.32.1 BUSYBOX_SRC := $(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER) +ifeq ($(SELINUX_ENABLED),) + SELINUX_ENABLED := 0 + ifneq ($(OS),Windows_NT) + ifeq ($(shell /sbin/selinuxenabled 2>/dev/null ; echo $$?),0) + SELINUX_ENABLED := 1 + endif + endif +endif + # Possible programs PROGS := \ base32 \ @@ -147,10 +156,18 @@ UNIX_PROGS := \ users \ who +SELINUX_PROGS := \ + chcon \ + runcon + ifneq ($(OS),Windows_NT) PROGS := $(PROGS) $(UNIX_PROGS) endif +ifeq ($(SELINUX_ENABLED),1) + PROGS := $(PROGS) $(SELINUX_PROGS) +endif + UTILS ?= $(PROGS) # Programs with usable tests @@ -159,6 +176,7 @@ TEST_PROGS := \ base64 \ basename \ cat \ + chcon \ chgrp \ chmod \ chown \ @@ -199,6 +217,7 @@ TEST_PROGS := \ realpath \ rm \ rmdir \ + runcon \ seq \ sort \ split \ @@ -228,6 +247,9 @@ TEST_SPEC_FEATURE := ifneq ($(SPEC),) TEST_NO_FAIL_FAST :=--no-fail-fast TEST_SPEC_FEATURE := test_unimplemented +else ifeq ($(SELINUX_ENABLED),1) +TEST_NO_FAIL_FAST := +TEST_SPEC_FEATURE := feat_selinux endif define TEST_BUSYBOX diff --git a/README.md b/README.md index 083320ac0..7e420bb33 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ to compile anywhere, and this is as good a way as any to try and learn it. ### Rust Version uutils follows Rust's release channels and is tested against stable, beta and nightly. -The current oldest supported version of the Rust compiler is `1.43.1`. +The current oldest supported version of the Rust compiler is `1.47`. On both Windows and Redox, only the nightly version is tested currently. @@ -365,23 +365,25 @@ To contribute to uutils, please see [CONTRIBUTING](CONTRIBUTING.md). | Done | Semi-Done | To Do | |-----------|-----------|--------| -| arch | cp | chcon | -| base32 | date | dd | -| base64 | df | runcon | -| basename | expr | stty | +| arch | cp | stty | +| base32 | date | | +| base64 | dd | | +| basename | df | | +| basenc | expr | | | cat | install | | -| chgrp | join | | -| chmod | ls | | -| chown | more | | -| chroot | numfmt | | -| cksum | od (`--strings` and 128-bit data types missing) | | -| comm | pr | | -| csplit | printf | | -| cut | sort | | -| dircolors | split | | -| dirname | tac | | -| du | tail | | -| echo | test | | +| chcon | join | | +| chgrp | ls | | +| chmod | more | | +| chown | numfmt | | +| chroot | od (`--strings` and 128-bit data types missing) | | +| cksum | pr | | +| comm | printf | | +| csplit | sort | | +| cut | split | | +| dircolors | tac | | +| dirname | tail | | +| du | test | | +| echo | | | | env | | | | expand | | | | factor | | | @@ -424,6 +426,7 @@ To contribute to uutils, please see [CONTRIBUTING](CONTRIBUTING.md). | relpath | | | | rm | | | | rmdir | | | +| runcon | | | | seq | | | | shred | | | | shuf | | | diff --git a/build.rs b/build.rs index e9fe129eb..261eb5d9a 100644 --- a/build.rs +++ b/build.rs @@ -28,8 +28,8 @@ pub fn main() { if val == "1" && key.starts_with(env_feature_prefix) { let krate = key[env_feature_prefix.len()..].to_lowercase(); match krate.as_ref() { - "default" | "macos" | "unix" | "windows" => continue, // common/standard feature names - "nightly" | "test_unimplemented" => continue, // crate-local custom features + "default" | "macos" | "unix" | "windows" | "selinux" => continue, // common/standard feature names + "nightly" | "test_unimplemented" => continue, // crate-local custom features "test" => continue, // over-ridden with 'uu_test' to avoid collision with rust core crate 'test' s if s.starts_with(feature_prefix) => continue, // crate feature sets _ => {} // util feature name @@ -46,6 +46,8 @@ pub fn main() { "type UtilityMap = HashMap<&'static str, (fn(T) -> i32, fn() -> App<'static, 'static>)>;\n\ \n\ fn util_map() -> UtilityMap {\n\ + \t#[allow(unused_mut)]\n\ + \t#[allow(clippy::let_and_return)]\n\ \tlet mut map = UtilityMap::new();\n\ " .as_bytes(), diff --git a/clippy.toml b/clippy.toml index 0a0a69a41..dbabdab50 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1 +1 @@ -msrv = "1.43.1" +msrv = "1.47.0" diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 3e8df57f7..1de1b6354 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -10,10 +10,12 @@ use clap::Arg; use clap::Shell; use std::cmp; use std::collections::hash_map::HashMap; +use std::ffi::OsStr; use std::ffi::OsString; use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::process; +use uucore::display::Quotable; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -70,18 +72,27 @@ fn main() { Some(OsString::from(*util)) } else { // unmatched binary name => regard as multi-binary container and advance argument list + uucore::set_utility_is_second_arg(); args.next() }; // 0th argument equals util name? if let Some(util_os) = util_name { - let util = util_os.as_os_str().to_string_lossy(); + fn not_found(util: &OsStr) -> ! { + println!("{}: function/utility not found", util.maybe_quote()); + process::exit(1); + } + + let util = match util_os.to_str() { + Some(util) => util, + None => not_found(&util_os), + }; if util == "completion" { gen_completions(args, utils); } - match utils.get(&util[..]) { + match utils.get(util) { Some(&(uumain, _)) => { process::exit(uumain((vec![util_os].into_iter()).chain(args))); } @@ -89,9 +100,12 @@ fn main() { if util == "--help" || util == "-h" { // see if they want help on a specific util if let Some(util_os) = args.next() { - let util = util_os.as_os_str().to_string_lossy(); + let util = match util_os.to_str() { + Some(util) => util, + None => not_found(&util_os), + }; - match utils.get(&util[..]) { + match utils.get(util) { Some(&(uumain, _)) => { let code = uumain( (vec![util_os, OsString::from("--help")].into_iter()) @@ -100,17 +114,13 @@ fn main() { io::stdout().flush().expect("could not flush stdout"); process::exit(code); } - None => { - println!("{}: function/utility not found", util); - process::exit(1); - } + None => not_found(&util_os), } } usage(&utils, binary_as_util); process::exit(0); } else { - println!("{}: function/utility not found", util); - process::exit(1); + not_found(&util_os); } } } diff --git a/src/uu/arch/Cargo.toml b/src/uu/arch/Cargo.toml index aa51183d7..c7fc4f9f9 100644 --- a/src/uu/arch/Cargo.toml +++ b/src/uu/arch/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_arch" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "arch ~ (uutils) display machine architecture" @@ -17,8 +17,8 @@ path = "src/arch.rs" [dependencies] platform-info = "0.1" clap = { version = "2.33", features = ["wrap_help"] } -uucore = { version=">=0.0.9", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore" } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "arch" diff --git a/src/uu/arch/src/arch.rs b/src/uu/arch/src/arch.rs index 94ec97e98..d23a11cc8 100644 --- a/src/uu/arch/src/arch.rs +++ b/src/uu/arch/src/arch.rs @@ -6,9 +6,6 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -#[macro_use] -extern crate uucore; - use platform_info::*; use clap::{crate_version, App}; @@ -27,7 +24,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .after_help(SUMMARY) diff --git a/src/uu/base32/Cargo.toml b/src/uu/base32/Cargo.toml index 3f6f79a0b..d5fc40024 100644 --- a/src/uu/base32/Cargo.toml +++ b/src/uu/base32/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_base32" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "base32 ~ (uutils) decode/encode input (base32-encoding)" @@ -16,9 +16,13 @@ path = "src/base32.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features = ["encoding"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features = ["encoding"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "base32" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/base32/src/base32.rs b/src/uu/base32/src/base32.rs index 9a29717ac..f4b4b49de 100644 --- a/src/uu/base32/src/base32.rs +++ b/src/uu/base32/src/base32.rs @@ -5,13 +5,10 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -#[macro_use] -extern crate uucore; - use std::io::{stdin, Read}; use clap::App; -use uucore::encoding::Format; +use uucore::{encoding::Format, error::UResult}; pub mod base_common; @@ -24,27 +21,22 @@ static ABOUT: &str = " to attempt to recover from any other non-alphabet bytes in the encoded stream. "; -static VERSION: &str = env!("CARGO_PKG_VERSION"); -static BASE_CMD_PARSE_ERROR: i32 = 1; - -fn get_usage() -> String { - format!("{0} [OPTION]... [FILE]", executable!()) +fn usage() -> String { + format!("{0} [OPTION]... [FILE]", uucore::execution_phrase()) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let format = Format::Base32; - let usage = get_usage(); - let name = executable!(); + let usage = usage(); - let config_result: Result = - base_common::parse_base_cmd_args(args, name, VERSION, ABOUT, &usage); - let config = config_result.unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s)); + let config: base_common::Config = base_common::parse_base_cmd_args(args, ABOUT, &usage)?; // Create a reference to stdin so we can return a locked stdin from // parse_base_cmd_args let stdin_raw = stdin(); - let mut input: Box = base_common::get_input(&config, &stdin_raw); + let mut input: Box = base_common::get_input(&config, &stdin_raw)?; base_common::handle_input( &mut input, @@ -52,12 +44,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { config.wrap_cols, config.ignore_garbage, config.decode, - name, - ); - - 0 + ) } pub fn uu_app() -> App<'static, 'static> { - base_common::base_app(executable!(), VERSION, ABOUT) + base_common::base_app(ABOUT) } diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index 4fc8b495b..b07203507 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -9,14 +9,18 @@ use std::io::{stdout, Read, Write}; +use uucore::display::Quotable; use uucore::encoding::{wrap_print, Data, Format}; +use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; use uucore::InvalidEncodingHandling; use std::fs::File; use std::io::{BufReader, Stdin}; use std::path::Path; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; + +pub static BASE_CMD_PARSE_ERROR: i32 = 1; // Config. pub struct Config { @@ -34,19 +38,25 @@ pub mod options { } impl Config { - fn from(options: clap::ArgMatches) -> Result { + pub fn from(options: &clap::ArgMatches) -> UResult { let file: Option = match options.values_of(options::FILE) { Some(mut values) => { let name = values.next().unwrap(); - if values.len() != 0 { - return Err(format!("extra operand '{}'", name)); + if let Some(extra_op) = values.next() { + return Err(UUsageError::new( + BASE_CMD_PARSE_ERROR, + format!("extra operand {}", extra_op.quote(),), + )); } if name == "-" { None } else { if !Path::exists(Path::new(name)) { - return Err(format!("{}: No such file or directory", name)); + return Err(USimpleError::new( + BASE_CMD_PARSE_ERROR, + format!("{}: No such file or directory", name.maybe_quote()), + )); } Some(name.to_owned()) } @@ -57,8 +67,12 @@ impl Config { let cols = options .value_of(options::WRAP) .map(|num| { - num.parse::() - .map_err(|e| format!("Invalid wrap size: '{}': {}", num, e)) + num.parse::().map_err(|_| { + USimpleError::new( + BASE_CMD_PARSE_ERROR, + format!("invalid wrap size: {}", num.quote()), + ) + }) }) .transpose()?; @@ -71,23 +85,17 @@ impl Config { } } -pub fn parse_base_cmd_args( - args: impl uucore::Args, - name: &str, - version: &str, - about: &str, - usage: &str, -) -> Result { - let app = base_app(name, version, about).usage(usage); +pub fn parse_base_cmd_args(args: impl uucore::Args, about: &str, usage: &str) -> UResult { + let app = base_app(about).usage(usage); let arg_list = args .collect_str(InvalidEncodingHandling::ConvertLossy) .accept_any(); - Config::from(app.get_matches_from(arg_list)) + Config::from(&app.get_matches_from(arg_list)) } -pub fn base_app<'a>(name: &str, version: &'a str, about: &'a str) -> App<'static, 'a> { - App::new(name) - .version(version) +pub fn base_app<'a>(about: &'a str) -> App<'static, 'a> { + App::new(uucore::util_name()) + .version(crate_version!()) .about(about) // Format arguments. .arg( @@ -116,14 +124,15 @@ pub fn base_app<'a>(name: &str, version: &'a str, about: &'a str) -> App<'static .arg(Arg::with_name(options::FILE).index(1).multiple(true)) } -pub fn get_input<'a>(config: &Config, stdin_ref: &'a Stdin) -> Box { +pub fn get_input<'a>(config: &Config, stdin_ref: &'a Stdin) -> UResult> { match &config.to_read { Some(name) => { - let file_buf = safe_unwrap!(File::open(Path::new(name))); - Box::new(BufReader::new(file_buf)) // as Box + let file_buf = + File::open(Path::new(name)).map_err_context(|| name.maybe_quote().to_string())?; + Ok(Box::new(BufReader::new(file_buf))) // as Box } None => { - Box::new(stdin_ref.lock()) // as Box + Ok(Box::new(stdin_ref.lock())) // as Box } } } @@ -134,29 +143,35 @@ pub fn handle_input( line_wrap: Option, ignore_garbage: bool, decode: bool, - name: &str, -) { +) -> UResult<()> { let mut data = Data::new(input, format).ignore_garbage(ignore_garbage); if let Some(wrap) = line_wrap { data = data.line_wrap(wrap); } if !decode { - let encoded = data.encode(); - wrap_print(&data, encoded); + match data.encode() { + Ok(s) => { + wrap_print(&data, s); + Ok(()) + } + Err(_) => Err(USimpleError::new( + 1, + "error: invalid input (length must be multiple of 4 characters)", + )), + } } else { match data.decode() { Ok(s) => { + // Silent the warning as we want to the error message + #[allow(clippy::question_mark)] if stdout().write_all(&s).is_err() { // on windows console, writing invalid utf8 returns an error - eprintln!("{}: error: Cannot write non-utf8 data", name); - exit!(1) + return Err(USimpleError::new(1, "error: cannot write non-utf8 data")); } + Ok(()) } - Err(_) => { - eprintln!("{}: error: invalid input", name); - exit!(1) - } + Err(_) => Err(USimpleError::new(1, "error: invalid input")), } } } diff --git a/src/uu/base64/Cargo.toml b/src/uu/base64/Cargo.toml index ff5a9aa48..c8d71b85a 100644 --- a/src/uu/base64/Cargo.toml +++ b/src/uu/base64/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_base64" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "base64 ~ (uutils) decode/encode input (base64-encoding)" @@ -16,10 +16,14 @@ path = "src/base64.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features = ["encoding"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } -uu_base32 = { version=">=0.0.6", package="uu_base32", path="../base32"} +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features = ["encoding"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } +uu_base32 = { version=">=0.0.8", package="uu_base32", path="../base32"} [[bin]] name = "base64" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/base64/src/base64.rs b/src/uu/base64/src/base64.rs index 71ed44e6e..c041d6d69 100644 --- a/src/uu/base64/src/base64.rs +++ b/src/uu/base64/src/base64.rs @@ -6,13 +6,10 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -#[macro_use] -extern crate uucore; - use uu_base32::base_common; pub use uu_base32::uu_app; -use uucore::encoding::Format; +use uucore::{encoding::Format, error::UResult}; use std::io::{stdin, Read}; @@ -25,26 +22,22 @@ static ABOUT: &str = " to attempt to recover from any other non-alphabet bytes in the encoded stream. "; -static VERSION: &str = env!("CARGO_PKG_VERSION"); -static BASE_CMD_PARSE_ERROR: i32 = 1; - -fn get_usage() -> String { - format!("{0} [OPTION]... [FILE]", executable!()) +fn usage() -> String { + format!("{0} [OPTION]... [FILE]", uucore::execution_phrase()) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let format = Format::Base64; - let usage = get_usage(); - let name = executable!(); - let config_result: Result = - base_common::parse_base_cmd_args(args, name, VERSION, ABOUT, &usage); - let config = config_result.unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s)); + let usage = usage(); + + let config: base_common::Config = base_common::parse_base_cmd_args(args, ABOUT, &usage)?; // Create a reference to stdin so we can return a locked stdin from // parse_base_cmd_args let stdin_raw = stdin(); - let mut input: Box = base_common::get_input(&config, &stdin_raw); + let mut input: Box = base_common::get_input(&config, &stdin_raw)?; base_common::handle_input( &mut input, @@ -52,8 +45,5 @@ pub fn uumain(args: impl uucore::Args) -> i32 { config.wrap_cols, config.ignore_garbage, config.decode, - name, - ); - - 0 + ) } diff --git a/src/uu/basename/Cargo.toml b/src/uu/basename/Cargo.toml index b5270eba9..b75105790 100644 --- a/src/uu/basename/Cargo.toml +++ b/src/uu/basename/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_basename" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "basename ~ (uutils) display PATHNAME with leading directory components removed" @@ -16,9 +16,13 @@ path = "src/basename.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } -uucore = { version=">=0.0.9", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore" } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "basename" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/basename/src/basename.rs b/src/uu/basename/src/basename.rs index 5450ee3f2..f7f4a3d08 100644 --- a/src/uu/basename/src/basename.rs +++ b/src/uu/basename/src/basename.rs @@ -17,11 +17,11 @@ use uucore::InvalidEncodingHandling; static SUMMARY: &str = "Print NAME with any leading directory components removed If specified, also remove a trailing SUFFIX"; -fn get_usage() -> String { +fn usage() -> String { format!( "{0} NAME [SUFFIX] {0} OPTION... NAME...", - executable!() + uucore::execution_phrase() ) } @@ -36,7 +36,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let args = args .collect_str(InvalidEncodingHandling::ConvertLossy) .accept_any(); - let usage = get_usage(); + let usage = usage(); // // Argument parsing // @@ -47,7 +47,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { crash!( 1, "{1}\nTry '{0} --help' for more information.", - executable!(), + uucore::execution_phrase(), "missing operand" ); } @@ -61,7 +61,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { crash!( 1, "extra operand '{1}'\nTry '{0} --help' for more information.", - executable!(), + uucore::execution_phrase(), matches.values_of(options::NAME).unwrap().nth(2).unwrap() ); } @@ -93,7 +93,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(SUMMARY) .arg( @@ -119,27 +119,27 @@ pub fn uu_app() -> App<'static, 'static> { } fn basename(fullname: &str, suffix: &str) -> String { - // Remove all platform-specific path separators from the end + // Remove all platform-specific path separators from the end. let path = fullname.trim_end_matches(is_separator); + // If the path contained *only* suffix characters (for example, if + // `fullname` were "///" and `suffix` were "/"), then `path` would + // be left with the empty string. In that case, we set `path` to be + // the original `fullname` to avoid returning the empty path. + let path = if path.is_empty() { fullname } else { path }; + // Convert to path buffer and get last path component let pb = PathBuf::from(path); match pb.components().last() { - Some(c) => strip_suffix(c.as_os_str().to_str().unwrap(), suffix), + Some(c) => { + let name = c.as_os_str().to_str().unwrap(); + if name == suffix { + name.to_string() + } else { + name.strip_suffix(suffix).unwrap_or(name).to_string() + } + } + None => "".to_owned(), } } - -// can be replaced with strip_suffix once MSRV is 1.45 -#[allow(clippy::manual_strip)] -fn strip_suffix(name: &str, suffix: &str) -> String { - if name == suffix { - return name.to_owned(); - } - - if name.ends_with(suffix) { - return name[..name.len() - suffix.len()].to_owned(); - } - - name.to_owned() -} diff --git a/src/uu/basenc/Cargo.toml b/src/uu/basenc/Cargo.toml new file mode 100644 index 000000000..165d3d3a0 --- /dev/null +++ b/src/uu/basenc/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "uu_basenc" +version = "0.0.8" +authors = ["uutils developers"] +license = "MIT" +description = "basenc ~ (uutils) decode/encode input" + +homepage = "https://github.com/uutils/coreutils" +repository = "https://github.com/uutils/coreutils/tree/master/src/uu/basenc" +keywords = ["coreutils", "uutils", "cross-platform", "cli", "utility"] +categories = ["command-line-utilities"] +edition = "2018" + +[lib] +path = "src/basenc.rs" + +[dependencies] +clap = { version = "2.33", features = ["wrap_help"] } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features = ["encoding"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } +uu_base32 = { version=">=0.0.8", package="uu_base32", path="../base32"} + +[[bin]] +name = "basenc" +path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/basenc/src/basenc.rs b/src/uu/basenc/src/basenc.rs new file mode 100644 index 000000000..e4b24c351 --- /dev/null +++ b/src/uu/basenc/src/basenc.rs @@ -0,0 +1,86 @@ +// This file is part of the uutils coreutils package. +// +// (c) Jordy Dickinson +// (c) Jian Zeng +// +// For the full copyright and license information, please view the LICENSE file +// that was distributed with this source code. + +//spell-checker:ignore (args) lsbf msbf + +use clap::{App, Arg}; +use uu_base32::base_common::{self, Config, BASE_CMD_PARSE_ERROR}; + +use uucore::{ + encoding::Format, + error::{UResult, UUsageError}, + InvalidEncodingHandling, +}; + +use std::io::{stdin, Read}; + +static ABOUT: &str = " + With no FILE, or when FILE is -, read standard input. + + When decoding, the input may contain newlines in addition to the bytes of + the formal alphabet. Use --ignore-garbage to attempt to recover + from any other non-alphabet bytes in the encoded stream. +"; + +const ENCODINGS: &[(&str, Format)] = &[ + ("base64", Format::Base64), + ("base64url", Format::Base64Url), + ("base32", Format::Base32), + ("base32hex", Format::Base32Hex), + ("base16", Format::Base16), + ("base2lsbf", Format::Base2Lsbf), + ("base2msbf", Format::Base2Msbf), + ("z85", Format::Z85), + // common abbreviations. TODO: once we have clap 3.0 we can use `AppSettings::InferLongArgs` to get all abbreviations automatically + ("base2l", Format::Base2Lsbf), + ("base2m", Format::Base2Msbf), +]; + +fn usage() -> String { + format!("{0} [OPTION]... [FILE]", uucore::execution_phrase()) +} + +pub fn uu_app() -> App<'static, 'static> { + let mut app = base_common::base_app(ABOUT); + for encoding in ENCODINGS { + app = app.arg(Arg::with_name(encoding.0).long(encoding.0)); + } + app +} + +fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> { + let usage = usage(); + let matches = uu_app().usage(&usage[..]).get_matches_from( + args.collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(), + ); + let format = ENCODINGS + .iter() + .find(|encoding| matches.is_present(encoding.0)) + .ok_or_else(|| UUsageError::new(BASE_CMD_PARSE_ERROR, "missing encoding type"))? + .1; + let config = Config::from(&matches)?; + Ok((config, format)) +} + +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { + let (config, format) = parse_cmd_args(args)?; + // Create a reference to stdin so we can return a locked stdin from + // parse_base_cmd_args + let stdin_raw = stdin(); + let mut input: Box = base_common::get_input(&config, &stdin_raw)?; + + base_common::handle_input( + &mut input, + format, + config.wrap_cols, + config.ignore_garbage, + config.decode, + ) +} diff --git a/src/uu/basenc/src/main.rs b/src/uu/basenc/src/main.rs new file mode 100644 index 000000000..9a9a5f4c6 --- /dev/null +++ b/src/uu/basenc/src/main.rs @@ -0,0 +1 @@ +uucore_procs::main!(uu_basenc); diff --git a/src/uu/cat/Cargo.toml b/src/uu/cat/Cargo.toml index b0721cee0..b6b0165ef 100644 --- a/src/uu/cat/Cargo.toml +++ b/src/uu/cat/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cat" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "cat ~ (uutils) concatenate and display input" @@ -18,14 +18,15 @@ path = "src/cat.rs" clap = { version = "2.33", features = ["wrap_help"] } thiserror = "1.0" atty = "0.2" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["fs"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features=["fs", "pipes"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [target.'cfg(unix)'.dependencies] unix_socket = "0.5.0" +nix = "0.20.0" -[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] -nix = "0.20" +[target.'cfg(windows)'.dependencies] +winapi-util = "0.1.5" [[bin]] name = "cat" diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 35a5308ed..af84890db 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -12,20 +12,21 @@ #[cfg(unix)] extern crate unix_socket; -#[macro_use] -extern crate uucore; // last synced with: cat (GNU coreutils) 8.13 use clap::{crate_version, App, Arg}; use std::fs::{metadata, File}; use std::io::{self, Read, Write}; use thiserror::Error; +use uucore::display::Quotable; +use uucore::error::UResult; + +#[cfg(unix)] +use std::os::unix::io::AsRawFd; /// Linux splice support #[cfg(any(target_os = "linux", target_os = "android"))] mod splice; -#[cfg(any(target_os = "linux", target_os = "android"))] -use std::os::unix::io::{AsRawFd, RawFd}; /// Unix domain socket support #[cfg(unix)] @@ -58,6 +59,8 @@ enum CatError { }, #[error("Is a directory")] IsDirectory, + #[error("input file is output file")] + OutputIsInput, } type CatResult = Result; @@ -122,12 +125,26 @@ struct OutputState { /// Whether the output cursor is at the beginning of a new line at_line_start: bool, + + /// Whether we skipped a \r, which still needs to be printed + skipped_carriage_return: bool, + + /// Whether we have already printed a blank line + one_blank_kept: bool, } +#[cfg(unix)] +trait FdReadable: Read + AsRawFd {} +#[cfg(not(unix))] +trait FdReadable: Read {} + +#[cfg(unix)] +impl FdReadable for T where T: Read + AsRawFd {} +#[cfg(not(unix))] +impl FdReadable for T where T: Read {} + /// Represents an open file handle, stream, or other device -struct InputHandle { - #[cfg(any(target_os = "linux", target_os = "android"))] - file_descriptor: RawFd, +struct InputHandle { reader: R, is_interactive: bool, } @@ -164,7 +181,8 @@ mod options { pub static SHOW_NONPRINTING: &str = "show-nonprinting"; } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = args .collect_str(InvalidEncodingHandling::Ignore) .accept_any(); @@ -217,17 +235,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 { show_tabs, squeeze_blank, }; - let success = cat_files(files, &options).is_ok(); - - if success { - 0 - } else { - 1 - } + cat_files(files, &options) } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .name(NAME) .version(crate_version!()) .usage(SYNTAX) @@ -289,7 +301,7 @@ pub fn uu_app() -> App<'static, 'static> { ) } -fn cat_handle( +fn cat_handle( handle: &mut InputHandle, options: &OutputOptions, state: &mut OutputState, @@ -301,12 +313,16 @@ fn cat_handle( } } -fn cat_path(path: &str, options: &OutputOptions, state: &mut OutputState) -> CatResult<()> { +fn cat_path( + path: &str, + options: &OutputOptions, + state: &mut OutputState, + #[cfg(unix)] out_info: &nix::sys::stat::FileStat, + #[cfg(windows)] out_info: &winapi_util::file::Information, +) -> CatResult<()> { if path == "-" { let stdin = io::stdin(); let mut handle = InputHandle { - #[cfg(any(target_os = "linux", target_os = "android"))] - file_descriptor: stdin.as_raw_fd(), reader: stdin, is_interactive: atty::is(atty::Stream::Stdin), }; @@ -319,8 +335,6 @@ fn cat_path(path: &str, options: &OutputOptions, state: &mut OutputState) -> Cat let socket = UnixStream::connect(path)?; socket.shutdown(Shutdown::Write)?; let mut handle = InputHandle { - #[cfg(any(target_os = "linux", target_os = "android"))] - file_descriptor: socket.as_raw_fd(), reader: socket, is_interactive: false, }; @@ -328,9 +342,11 @@ fn cat_path(path: &str, options: &OutputOptions, state: &mut OutputState) -> Cat } _ => { let file = File::open(path)?; + #[cfg(any(windows, unix))] + if same_file(out_info, &file) { + return Err(CatError::OutputIsInput); + } let mut handle = InputHandle { - #[cfg(any(target_os = "linux", target_os = "android"))] - file_descriptor: file.as_raw_fd(), reader: file, is_interactive: false, }; @@ -339,23 +355,52 @@ fn cat_path(path: &str, options: &OutputOptions, state: &mut OutputState) -> Cat } } -fn cat_files(files: Vec, options: &OutputOptions) -> Result<(), u32> { - let mut error_count = 0; +#[cfg(unix)] +fn same_file(a_info: &nix::sys::stat::FileStat, b: &File) -> bool { + let b_info = nix::sys::stat::fstat(b.as_raw_fd()).unwrap(); + b_info.st_size != 0 && b_info.st_dev == a_info.st_dev && b_info.st_ino == a_info.st_ino +} + +#[cfg(windows)] +fn same_file(a_info: &winapi_util::file::Information, b: &File) -> bool { + let b_info = winapi_util::file::information(b).unwrap(); + b_info.file_size() != 0 + && b_info.volume_serial_number() == a_info.volume_serial_number() + && b_info.file_index() == a_info.file_index() +} + +fn cat_files(files: Vec, options: &OutputOptions) -> UResult<()> { + #[cfg(windows)] + let out_info = winapi_util::file::information(&std::io::stdout()).unwrap(); + #[cfg(unix)] + let out_info = nix::sys::stat::fstat(std::io::stdout().as_raw_fd()).unwrap(); + let mut state = OutputState { line_number: 1, at_line_start: true, + skipped_carriage_return: false, + one_blank_kept: false, }; + let mut error_messages: Vec = Vec::new(); for path in &files { - if let Err(err) = cat_path(path, options, &mut state) { - show_error!("{}: {}", path, err); - error_count += 1; + if let Err(err) = cat_path(path, options, &mut state, &out_info) { + error_messages.push(format!("{}: {}", path.maybe_quote(), err)); } } - if error_count == 0 { + if state.skipped_carriage_return { + print!("\r"); + } + if error_messages.is_empty() { Ok(()) } else { - Err(error_count) + // each next line is expected to display "cat: …" + let line_joiner = format!("\n{}: ", uucore::util_name()); + + Err(uucore::error::USimpleError::new( + error_messages.len() as i32, + error_messages.join(&line_joiner), + )) } } @@ -390,14 +435,14 @@ fn get_input_type(path: &str) -> CatResult { /// Writes handle to stdout with no configuration. This allows a /// simple memory copy. -fn write_fast(handle: &mut InputHandle) -> CatResult<()> { +fn write_fast(handle: &mut InputHandle) -> CatResult<()> { let stdout = io::stdout(); let mut stdout_lock = stdout.lock(); #[cfg(any(target_os = "linux", target_os = "android"))] { // If we're on Linux or Android, try to use the splice() system call // for faster writing. If it works, we're done. - if !splice::write_fast_using_splice(handle, stdout_lock.as_raw_fd())? { + if !splice::write_fast_using_splice(handle, &stdout_lock)? { return Ok(()); } } @@ -415,7 +460,7 @@ fn write_fast(handle: &mut InputHandle) -> CatResult<()> { /// Outputs file contents to stdout in a line-by-line fashion, /// propagating any errors that might occur. -fn write_lines( +fn write_lines( handle: &mut InputHandle, options: &OutputOptions, state: &mut OutputState, @@ -423,7 +468,6 @@ fn write_lines( let mut in_buf = [0; 1024 * 31]; let stdout = io::stdout(); let mut writer = stdout.lock(); - let mut one_blank_kept = false; while let Ok(n) = handle.reader.read(&mut in_buf) { if n == 0 { @@ -434,8 +478,13 @@ fn write_lines( while pos < n { // skip empty line_number enumerating them if needed if in_buf[pos] == b'\n' { - if !state.at_line_start || !options.squeeze_blank || !one_blank_kept { - one_blank_kept = true; + // \r followed by \n is printed as ^M when show_ends is enabled, so that \r\n prints as ^M$ + if state.skipped_carriage_return && options.show_ends { + writer.write_all(b"^M")?; + state.skipped_carriage_return = false; + } + if !state.at_line_start || !options.squeeze_blank || !state.one_blank_kept { + state.one_blank_kept = true; if state.at_line_start && options.number == NumberingMode::All { write!(&mut writer, "{0:6}\t", state.line_number)?; state.line_number += 1; @@ -449,7 +498,12 @@ fn write_lines( pos += 1; continue; } - one_blank_kept = false; + if state.skipped_carriage_return { + writer.write_all(b"\r")?; + state.skipped_carriage_return = false; + state.at_line_start = false; + } + state.one_blank_kept = false; if state.at_line_start && options.number != NumberingMode::None { write!(&mut writer, "{0:6}\t", state.line_number)?; state.line_number += 1; @@ -464,17 +518,22 @@ fn write_lines( write_to_end(&in_buf[pos..], &mut writer) }; // end of buffer? - if offset == 0 { + if offset + pos == in_buf.len() { state.at_line_start = false; break; } - // print suitable end of line - writer.write_all(options.end_of_line().as_bytes())?; - if handle.is_interactive { - writer.flush()?; + if in_buf[pos + offset] == b'\r' { + state.skipped_carriage_return = true; + } else { + assert_eq!(in_buf[pos + offset], b'\n'); + // print suitable end of line + writer.write_all(options.end_of_line().as_bytes())?; + if handle.is_interactive { + writer.flush()?; + } + state.at_line_start = true; } - state.at_line_start = true; - pos += offset; + pos += offset + 1; } } @@ -482,17 +541,19 @@ fn write_lines( } // write***_to_end methods -// Write all symbols till end of line or end of buffer is reached -// Return the (number of written symbols + 1) or 0 if the end of buffer is reached +// Write all symbols till \n or \r or end of buffer is reached +// We need to stop at \r because it may be written as ^M depending on the byte after and settings; +// however, write_nonprint_to_end doesn't need to stop at \r because it will always write \r as ^M. +// Return the number of written symbols fn write_to_end(in_buf: &[u8], writer: &mut W) -> usize { - match in_buf.iter().position(|c| *c == b'\n') { + match in_buf.iter().position(|c| *c == b'\n' || *c == b'\r') { Some(p) => { writer.write_all(&in_buf[..p]).unwrap(); - p + 1 + p } None => { writer.write_all(in_buf).unwrap(); - 0 + in_buf.len() } } } @@ -500,20 +561,25 @@ fn write_to_end(in_buf: &[u8], writer: &mut W) -> usize { fn write_tab_to_end(mut in_buf: &[u8], writer: &mut W) -> usize { let mut count = 0; loop { - match in_buf.iter().position(|c| *c == b'\n' || *c == b'\t') { + match in_buf + .iter() + .position(|c| *c == b'\n' || *c == b'\t' || *c == b'\r') + { Some(p) => { writer.write_all(&in_buf[..p]).unwrap(); if in_buf[p] == b'\n' { - return count + p + 1; - } else { + return count + p; + } else if in_buf[p] == b'\t' { writer.write_all(b"^I").unwrap(); in_buf = &in_buf[p + 1..]; count += p + 1; + } else { + return count + p; } } None => { writer.write_all(in_buf).unwrap(); - return 0; + return in_buf.len(); } }; } @@ -522,7 +588,7 @@ fn write_tab_to_end(mut in_buf: &[u8], writer: &mut W) -> usize { fn write_nonprint_to_end(in_buf: &[u8], writer: &mut W, tab: &[u8]) -> usize { let mut count = 0; - for byte in in_buf.iter().map(|c| *c) { + for byte in in_buf.iter().copied() { if byte == b'\n' { break; } @@ -538,11 +604,7 @@ fn write_nonprint_to_end(in_buf: &[u8], writer: &mut W, tab: &[u8]) -> .unwrap(); count += 1; } - if count != in_buf.len() { - count + 1 - } else { - 0 - } + count } #[cfg(test)] diff --git a/src/uu/cat/src/splice.rs b/src/uu/cat/src/splice.rs index 0b46fc662..26802c7e6 100644 --- a/src/uu/cat/src/splice.rs +++ b/src/uu/cat/src/splice.rs @@ -1,10 +1,11 @@ -use super::{CatResult, InputHandle}; +use super::{CatResult, FdReadable, InputHandle}; -use nix::fcntl::{splice, SpliceFFlags}; -use nix::unistd::{self, pipe}; -use std::io::Read; -use std::os::unix::io::RawFd; +use nix::unistd; +use std::os::unix::io::{AsRawFd, RawFd}; +use uucore::pipes::{pipe, splice, splice_exact}; + +const SPLICE_SIZE: usize = 1024 * 128; const BUF_SIZE: usize = 1024 * 16; /// This function is called from `write_fast()` on Linux and Android. The @@ -15,38 +16,25 @@ const BUF_SIZE: usize = 1024 * 16; /// The `bool` in the result value indicates if we need to fall back to normal /// copying or not. False means we don't have to. #[inline] -pub(super) fn write_fast_using_splice( +pub(super) fn write_fast_using_splice( handle: &mut InputHandle, - write_fd: RawFd, + write_fd: &impl AsRawFd, ) -> CatResult { - let (pipe_rd, pipe_wr) = match pipe() { - Ok(r) => r, - Err(_) => { - // It is very rare that creating a pipe fails, but it can happen. - return Ok(true); - } - }; + let (pipe_rd, pipe_wr) = pipe()?; loop { - match splice( - handle.file_descriptor, - None, - pipe_wr, - None, - BUF_SIZE, - SpliceFFlags::empty(), - ) { + match splice(&handle.reader, &pipe_wr, SPLICE_SIZE) { Ok(n) => { if n == 0 { return Ok(false); } - if splice_exact(pipe_rd, write_fd, n).is_err() { + if splice_exact(&pipe_rd, write_fd, n).is_err() { // If the first splice manages to copy to the intermediate // pipe, but the second splice to stdout fails for some reason // we can recover by copying the data that we have from the // intermediate pipe to stdout using normal read/write. Then // we tell the caller to fall back. - copy_exact(pipe_rd, write_fd, n)?; + copy_exact(pipe_rd.as_raw_fd(), write_fd.as_raw_fd(), n)?; return Ok(true); } } @@ -57,35 +45,23 @@ pub(super) fn write_fast_using_splice( } } -/// Splice wrapper which handles short writes. -#[inline] -fn splice_exact(read_fd: RawFd, write_fd: RawFd, num_bytes: usize) -> nix::Result<()> { - let mut left = num_bytes; - loop { - let written = splice(read_fd, None, write_fd, None, left, SpliceFFlags::empty())?; - left -= written; - if left == 0 { - break; - } - } - Ok(()) -} - -/// Caller must ensure that `num_bytes <= BUF_SIZE`, otherwise this function -/// will panic. The way we use this function in `write_fast_using_splice` -/// above is safe because `splice` is set to write at most `BUF_SIZE` to the -/// pipe. -#[inline] +/// Move exactly `num_bytes` bytes from `read_fd` to `write_fd`. +/// +/// Panics if not enough bytes can be read. fn copy_exact(read_fd: RawFd, write_fd: RawFd, num_bytes: usize) -> nix::Result<()> { let mut left = num_bytes; let mut buf = [0; BUF_SIZE]; - loop { - let read = unistd::read(read_fd, &mut buf[..left])?; - let written = unistd::write(write_fd, &buf[..read])?; - left -= written; - if left == 0 { - break; + while left > 0 { + let read = unistd::read(read_fd, &mut buf)?; + assert_ne!(read, 0, "unexpected end of pipe"); + let mut written = 0; + while written < read { + match unistd::write(write_fd, &buf[written..read])? { + 0 => panic!(), + n => written += n, + } } + left -= read; } Ok(()) } diff --git a/src/uu/chcon/Cargo.toml b/src/uu/chcon/Cargo.toml new file mode 100644 index 000000000..a359f142f --- /dev/null +++ b/src/uu/chcon/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "uu_chcon" +version = "0.0.8" +authors = ["uutils developers"] +license = "MIT" +description = "chcon ~ (uutils) change file security context" +homepage = "https://github.com/uutils/coreutils" +repository = "https://github.com/uutils/coreutils/tree/master/src/uu/chcon" +keywords = ["coreutils", "uutils", "cli", "utility"] +categories = ["command-line-utilities"] +edition = "2018" + +[lib] +path = "src/chcon.rs" + +[dependencies] +clap = { version = "2.33", features = ["wrap_help"] } +uucore = { version = ">=0.0.9", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } +uucore_procs = { version = ">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +selinux = { version = "0.2" } +fts-sys = { version = "0.2" } +thiserror = { version = "1.0" } +libc = { version = "0.2" } + +[[bin]] +name = "chcon" +path = "src/main.rs" diff --git a/src/uu/chcon/src/chcon.rs b/src/uu/chcon/src/chcon.rs new file mode 100644 index 000000000..9664f69f5 --- /dev/null +++ b/src/uu/chcon/src/chcon.rs @@ -0,0 +1,745 @@ +// spell-checker:ignore (vars) RFILE + +#![allow(clippy::upper_case_acronyms)] + +use uucore::{display::Quotable, show_error, show_usage_error, show_warning}; + +use clap::{App, Arg}; +use selinux::{OpaqueSecurityContext, SecurityContext}; + +use std::borrow::Cow; +use std::ffi::{CStr, CString, OsStr, OsString}; +use std::os::raw::c_int; +use std::path::{Path, PathBuf}; +use std::{fs, io}; + +mod errors; +mod fts; + +use errors::*; + +static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Change the SELinux security context of each FILE to CONTEXT. \n\ + With --reference, change the security context of each FILE to that of RFILE."; + +pub mod options { + pub static VERBOSE: &str = "verbose"; + + pub static REFERENCE: &str = "reference"; + + pub static USER: &str = "user"; + pub static ROLE: &str = "role"; + pub static TYPE: &str = "type"; + pub static RANGE: &str = "range"; + + pub static RECURSIVE: &str = "recursive"; + + pub mod sym_links { + pub static FOLLOW_ARG_DIR_SYM_LINK: &str = "follow-arg-dir-sym-link"; + pub static FOLLOW_DIR_SYM_LINKS: &str = "follow-dir-sym-links"; + pub static NO_FOLLOW_SYM_LINKS: &str = "no-follow-sym-links"; + } + + pub mod dereference { + pub static DEREFERENCE: &str = "dereference"; + pub static NO_DEREFERENCE: &str = "no-dereference"; + } + + pub mod preserve_root { + pub static PRESERVE_ROOT: &str = "preserve-root"; + pub static NO_PRESERVE_ROOT: &str = "no-preserve-root"; + } +} + +fn get_usage() -> String { + format!( + "{0} [OPTION]... CONTEXT FILE... \n \ + {0} [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE... \n \ + {0} [OPTION]... --reference=RFILE FILE...", + uucore::execution_phrase() + ) +} + +pub fn uumain(args: impl uucore::Args) -> i32 { + let usage = get_usage(); + + let config = uu_app().usage(usage.as_ref()); + + let options = match parse_command_line(config, args) { + Ok(r) => r, + Err(r) => { + if let Error::CommandLine(r) = &r { + match r.kind { + clap::ErrorKind::HelpDisplayed | clap::ErrorKind::VersionDisplayed => { + println!("{}", r); + return libc::EXIT_SUCCESS; + } + _ => {} + } + } + + show_usage_error!("{}.\n", r); + return libc::EXIT_FAILURE; + } + }; + + let context = match &options.mode { + CommandLineMode::ReferenceBased { reference } => { + let result = match SecurityContext::of_path(reference, true, false) { + Ok(Some(context)) => Ok(context), + + Ok(None) => { + let err = io::Error::from_raw_os_error(libc::ENODATA); + Err(Error::from_io1("Getting security context", reference, err)) + } + + Err(r) => Err(Error::from_selinux("Getting security context", r)), + }; + + match result { + Err(r) => { + show_error!("{}.", report_full_error(&r)); + return libc::EXIT_FAILURE; + } + + Ok(file_context) => SELinuxSecurityContext::File(file_context), + } + } + + CommandLineMode::ContextBased { context } => { + let c_context = match os_str_to_c_string(context) { + Ok(context) => context, + + Err(_r) => { + show_error!("Invalid security context {}.", context.quote()); + return libc::EXIT_FAILURE; + } + }; + + if SecurityContext::from_c_str(&c_context, false).check() == Some(false) { + show_error!("Invalid security context {}.", context.quote()); + return libc::EXIT_FAILURE; + } + + SELinuxSecurityContext::String(Some(c_context)) + } + + CommandLineMode::Custom { .. } => SELinuxSecurityContext::String(None), + }; + + let root_dev_ino = if options.preserve_root && options.recursive_mode.is_recursive() { + match get_root_dev_ino() { + Ok(r) => Some(r), + + Err(r) => { + show_error!("{}.", report_full_error(&r)); + return libc::EXIT_FAILURE; + } + } + } else { + None + }; + + let results = process_files(&options, &context, root_dev_ino); + if results.is_empty() { + return libc::EXIT_SUCCESS; + } + + for result in &results { + show_error!("{}.", report_full_error(result)); + } + libc::EXIT_FAILURE +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(uucore::util_name()) + .version(VERSION) + .about(ABOUT) + .arg( + Arg::with_name(options::dereference::DEREFERENCE) + .long(options::dereference::DEREFERENCE) + .conflicts_with(options::dereference::NO_DEREFERENCE) + .help( + "Affect the referent of each symbolic link (this is the default), \ + rather than the symbolic link itself.", + ), + ) + .arg( + Arg::with_name(options::dereference::NO_DEREFERENCE) + .short("h") + .long(options::dereference::NO_DEREFERENCE) + .help("Affect symbolic links instead of any referenced file."), + ) + .arg( + Arg::with_name(options::preserve_root::PRESERVE_ROOT) + .long(options::preserve_root::PRESERVE_ROOT) + .conflicts_with(options::preserve_root::NO_PRESERVE_ROOT) + .help("Fail to operate recursively on '/'."), + ) + .arg( + Arg::with_name(options::preserve_root::NO_PRESERVE_ROOT) + .long(options::preserve_root::NO_PRESERVE_ROOT) + .help("Do not treat '/' specially (the default)."), + ) + .arg( + Arg::with_name(options::REFERENCE) + .long(options::REFERENCE) + .takes_value(true) + .value_name("RFILE") + .conflicts_with_all(&[options::USER, options::ROLE, options::TYPE, options::RANGE]) + .help( + "Use security context of RFILE, rather than specifying \ + a CONTEXT value.", + ), + ) + .arg( + Arg::with_name(options::USER) + .short("u") + .long(options::USER) + .takes_value(true) + .value_name("USER") + .help("Set user USER in the target security context."), + ) + .arg( + Arg::with_name(options::ROLE) + .short("r") + .long(options::ROLE) + .takes_value(true) + .value_name("ROLE") + .help("Set role ROLE in the target security context."), + ) + .arg( + Arg::with_name(options::TYPE) + .short("t") + .long(options::TYPE) + .takes_value(true) + .value_name("TYPE") + .help("Set type TYPE in the target security context."), + ) + .arg( + Arg::with_name(options::RANGE) + .short("l") + .long(options::RANGE) + .takes_value(true) + .value_name("RANGE") + .help("Set range RANGE in the target security context."), + ) + .arg( + Arg::with_name(options::RECURSIVE) + .short("R") + .long(options::RECURSIVE) + .help("Operate on files and directories recursively."), + ) + .arg( + Arg::with_name(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK) + .short("H") + .requires(options::RECURSIVE) + .overrides_with_all(&[ + options::sym_links::FOLLOW_DIR_SYM_LINKS, + options::sym_links::NO_FOLLOW_SYM_LINKS, + ]) + .help( + "If a command line argument is a symbolic link to a directory, \ + traverse it. Only valid when -R is specified.", + ), + ) + .arg( + Arg::with_name(options::sym_links::FOLLOW_DIR_SYM_LINKS) + .short("L") + .requires(options::RECURSIVE) + .overrides_with_all(&[ + options::sym_links::FOLLOW_ARG_DIR_SYM_LINK, + options::sym_links::NO_FOLLOW_SYM_LINKS, + ]) + .help( + "Traverse every symbolic link to a directory encountered. \ + Only valid when -R is specified.", + ), + ) + .arg( + Arg::with_name(options::sym_links::NO_FOLLOW_SYM_LINKS) + .short("P") + .requires(options::RECURSIVE) + .overrides_with_all(&[ + options::sym_links::FOLLOW_ARG_DIR_SYM_LINK, + options::sym_links::FOLLOW_DIR_SYM_LINKS, + ]) + .help( + "Do not traverse any symbolic links (default). \ + Only valid when -R is specified.", + ), + ) + .arg( + Arg::with_name(options::VERBOSE) + .short("v") + .long(options::VERBOSE) + .help("Output a diagnostic for every file processed."), + ) + .arg(Arg::with_name("FILE").multiple(true).min_values(1)) +} + +#[derive(Debug)] +struct Options { + verbose: bool, + preserve_root: bool, + recursive_mode: RecursiveMode, + affect_symlink_referent: bool, + mode: CommandLineMode, + files: Vec, +} + +fn parse_command_line(config: clap::App, args: impl uucore::Args) -> Result { + let matches = config.get_matches_from_safe(args)?; + + let verbose = matches.is_present(options::VERBOSE); + + let (recursive_mode, affect_symlink_referent) = if matches.is_present(options::RECURSIVE) { + if matches.is_present(options::sym_links::FOLLOW_DIR_SYM_LINKS) { + if matches.is_present(options::dereference::NO_DEREFERENCE) { + return Err(Error::ArgumentsMismatch(format!( + "'--{}' with '--{}' require '-P'", + options::RECURSIVE, + options::dereference::NO_DEREFERENCE + ))); + } + + (RecursiveMode::RecursiveAndFollowAllDirSymLinks, true) + } else if matches.is_present(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK) { + if matches.is_present(options::dereference::NO_DEREFERENCE) { + return Err(Error::ArgumentsMismatch(format!( + "'--{}' with '--{}' require '-P'", + options::RECURSIVE, + options::dereference::NO_DEREFERENCE + ))); + } + + (RecursiveMode::RecursiveAndFollowArgDirSymLinks, true) + } else { + if matches.is_present(options::dereference::DEREFERENCE) { + return Err(Error::ArgumentsMismatch(format!( + "'--{}' with '--{}' require either '-H' or '-L'", + options::RECURSIVE, + options::dereference::DEREFERENCE + ))); + } + + (RecursiveMode::RecursiveButDoNotFollowSymLinks, false) + } + } else { + let no_dereference = matches.is_present(options::dereference::NO_DEREFERENCE); + (RecursiveMode::NotRecursive, !no_dereference) + }; + + // By default, do not preserve root. + let preserve_root = matches.is_present(options::preserve_root::PRESERVE_ROOT); + + let mut files = matches.values_of_os("FILE").unwrap_or_default(); + + let mode = if let Some(path) = matches.value_of_os(options::REFERENCE) { + CommandLineMode::ReferenceBased { + reference: PathBuf::from(path), + } + } else if matches.is_present(options::USER) + || matches.is_present(options::ROLE) + || matches.is_present(options::TYPE) + || matches.is_present(options::RANGE) + { + CommandLineMode::Custom { + user: matches.value_of_os(options::USER).map(Into::into), + role: matches.value_of_os(options::ROLE).map(Into::into), + the_type: matches.value_of_os(options::TYPE).map(Into::into), + range: matches.value_of_os(options::RANGE).map(Into::into), + } + } else if let Some(context) = files.next() { + CommandLineMode::ContextBased { + context: context.into(), + } + } else { + return Err(Error::MissingContext); + }; + + let files: Vec<_> = files.map(PathBuf::from).collect(); + if files.is_empty() { + return Err(Error::MissingFiles); + } + + Ok(Options { + verbose, + preserve_root, + recursive_mode, + affect_symlink_referent, + mode, + files, + }) +} + +#[derive(Debug, Copy, Clone)] +enum RecursiveMode { + NotRecursive, + /// Do not traverse any symbolic links. + RecursiveButDoNotFollowSymLinks, + /// Traverse every symbolic link to a directory encountered. + RecursiveAndFollowAllDirSymLinks, + /// If a command line argument is a symbolic link to a directory, traverse it. + RecursiveAndFollowArgDirSymLinks, +} + +impl RecursiveMode { + fn is_recursive(self) -> bool { + match self { + RecursiveMode::NotRecursive => false, + + RecursiveMode::RecursiveButDoNotFollowSymLinks + | RecursiveMode::RecursiveAndFollowAllDirSymLinks + | RecursiveMode::RecursiveAndFollowArgDirSymLinks => true, + } + } + + fn fts_open_options(self) -> c_int { + match self { + RecursiveMode::NotRecursive | RecursiveMode::RecursiveButDoNotFollowSymLinks => { + fts_sys::FTS_PHYSICAL + } + + RecursiveMode::RecursiveAndFollowAllDirSymLinks => fts_sys::FTS_LOGICAL, + + RecursiveMode::RecursiveAndFollowArgDirSymLinks => { + fts_sys::FTS_PHYSICAL | fts_sys::FTS_COMFOLLOW + } + } + } +} + +#[derive(Debug)] +enum CommandLineMode { + ReferenceBased { + reference: PathBuf, + }, + ContextBased { + context: OsString, + }, + Custom { + user: Option, + role: Option, + the_type: Option, + range: Option, + }, +} + +fn process_files( + options: &Options, + context: &SELinuxSecurityContext, + root_dev_ino: Option<(libc::ino_t, libc::dev_t)>, +) -> Vec { + let fts_options = options.recursive_mode.fts_open_options(); + let mut fts = match fts::FTS::new(options.files.iter(), fts_options) { + Ok(fts) => fts, + Err(err) => return vec![err], + }; + + let mut errors = Vec::default(); + loop { + match fts.read_next_entry() { + Ok(true) => { + if let Err(err) = process_file(options, context, &mut fts, root_dev_ino) { + errors.push(err); + } + } + + Ok(false) => break, + + Err(err) => { + errors.push(err); + break; + } + } + } + errors +} + +fn process_file( + options: &Options, + context: &SELinuxSecurityContext, + fts: &mut fts::FTS, + root_dev_ino: Option<(libc::ino_t, libc::dev_t)>, +) -> Result<()> { + let mut entry = fts.last_entry_ref().unwrap(); + + let file_full_name = entry.path().map(PathBuf::from).ok_or_else(|| { + Error::from_io("File name validation", io::ErrorKind::InvalidInput.into()) + })?; + + let fts_access_path = entry.access_path().ok_or_else(|| { + let err = io::ErrorKind::InvalidInput.into(); + Error::from_io1("File name validation", &file_full_name, err) + })?; + + let err = |s, k: io::ErrorKind| Error::from_io1(s, &file_full_name, k.into()); + + let fts_err = |s| { + let r = io::Error::from_raw_os_error(entry.errno()); + Err(Error::from_io1(s, &file_full_name, r)) + }; + + // SAFETY: If `entry.fts_statp` is not null, then is is assumed to be valid. + let file_dev_ino = if let Some(stat) = entry.stat() { + (stat.st_ino, stat.st_dev) + } else { + return Err(err("Getting meta data", io::ErrorKind::InvalidInput)); + }; + + let mut result = Ok(()); + + match entry.flags() { + fts_sys::FTS_D => { + if options.recursive_mode.is_recursive() { + if root_dev_ino_check(root_dev_ino, file_dev_ino) { + // This happens e.g., with "chcon -R --preserve-root ... /" + // and with "chcon -RH --preserve-root ... symlink-to-root". + root_dev_ino_warn(&file_full_name); + + // Tell fts not to traverse into this hierarchy. + let _ignored = fts.set(fts_sys::FTS_SKIP); + + // Ensure that we do not process "/" on the second visit. + let _ignored = fts.read_next_entry(); + + return Err(err("Modifying root path", io::ErrorKind::PermissionDenied)); + } + + return Ok(()); + } + } + + fts_sys::FTS_DP => { + if !options.recursive_mode.is_recursive() { + return Ok(()); + } + } + + fts_sys::FTS_NS => { + // For a top-level file or directory, this FTS_NS (stat failed) indicator is determined + // at the time of the initial fts_open call. With programs like chmod, chown, and chgrp, + // that modify permissions, it is possible that the file in question is accessible when + // control reaches this point. So, if this is the first time we've seen the FTS_NS for + // this file, tell fts_read to stat it "again". + if entry.level() == 0 && entry.number() == 0 { + entry.set_number(1); + let _ignored = fts.set(fts_sys::FTS_AGAIN); + return Ok(()); + } + + result = fts_err("Accessing"); + } + + fts_sys::FTS_ERR => result = fts_err("Accessing"), + + fts_sys::FTS_DNR => result = fts_err("Reading directory"), + + fts_sys::FTS_DC => { + if cycle_warning_required(options.recursive_mode.fts_open_options(), &entry) { + emit_cycle_warning(&file_full_name); + return Err(err("Reading cyclic directory", io::ErrorKind::InvalidData)); + } + } + + _ => {} + } + + if entry.flags() == fts_sys::FTS_DP + && result.is_ok() + && root_dev_ino_check(root_dev_ino, file_dev_ino) + { + root_dev_ino_warn(&file_full_name); + result = Err(err("Modifying root path", io::ErrorKind::PermissionDenied)); + } + + if result.is_ok() { + if options.verbose { + println!( + "{}: Changing security context of: {}", + uucore::util_name(), + file_full_name.quote() + ); + } + + result = change_file_context(options, context, fts_access_path); + } + + if !options.recursive_mode.is_recursive() { + let _ignored = fts.set(fts_sys::FTS_SKIP); + } + result +} + +fn change_file_context( + options: &Options, + context: &SELinuxSecurityContext, + path: &Path, +) -> Result<()> { + match &options.mode { + CommandLineMode::Custom { + user, + role, + the_type, + range, + } => { + let err0 = || -> Result<()> { + // If the file doesn't have a context, and we're not setting all of the context + // components, there isn't really an obvious default. Thus, we just give up. + let op = "Applying partial security context to unlabeled file"; + let err = io::ErrorKind::InvalidInput.into(); + Err(Error::from_io1(op, path, err)) + }; + + let file_context = + match SecurityContext::of_path(path, options.affect_symlink_referent, false) { + Ok(Some(context)) => context, + + Ok(None) => return err0(), + Err(r) => return Err(Error::from_selinux("Getting security context", r)), + }; + + let c_file_context = match file_context.to_c_string() { + Ok(Some(context)) => context, + + Ok(None) => return err0(), + Err(r) => return Err(Error::from_selinux("Getting security context", r)), + }; + + let se_context = + OpaqueSecurityContext::from_c_str(c_file_context.as_ref()).map_err(|_r| { + let err = io::ErrorKind::InvalidInput.into(); + Error::from_io1("Creating security context", path, err) + })?; + + type SetValueProc = fn(&OpaqueSecurityContext, &CStr) -> selinux::errors::Result<()>; + + let list: &[(&Option, SetValueProc)] = &[ + (user, OpaqueSecurityContext::set_user), + (role, OpaqueSecurityContext::set_role), + (the_type, OpaqueSecurityContext::set_type), + (range, OpaqueSecurityContext::set_range), + ]; + + for (new_value, set_value_proc) in list { + if let Some(new_value) = new_value { + let c_new_value = os_str_to_c_string(new_value).map_err(|_r| { + let err = io::ErrorKind::InvalidInput.into(); + Error::from_io1("Creating security context", path, err) + })?; + + set_value_proc(&se_context, &c_new_value) + .map_err(|r| Error::from_selinux("Setting security context user", r))?; + } + } + + let context_string = se_context + .to_c_string() + .map_err(|r| Error::from_selinux("Getting security context", r))?; + + if c_file_context.as_ref().to_bytes() == context_string.as_ref().to_bytes() { + Ok(()) // Nothing to change. + } else { + SecurityContext::from_c_str(&context_string, false) + .set_for_path(path, options.affect_symlink_referent, false) + .map_err(|r| Error::from_selinux("Setting security context", r)) + } + } + + CommandLineMode::ReferenceBased { .. } | CommandLineMode::ContextBased { .. } => { + if let Some(c_context) = context.to_c_string()? { + SecurityContext::from_c_str(c_context.as_ref(), false) + .set_for_path(path, options.affect_symlink_referent, false) + .map_err(|r| Error::from_selinux("Setting security context", r)) + } else { + let err = io::ErrorKind::InvalidInput.into(); + Err(Error::from_io1("Setting security context", path, err)) + } + } + } +} + +#[cfg(unix)] +pub(crate) fn os_str_to_c_string(s: &OsStr) -> Result { + use std::os::unix::ffi::OsStrExt; + + CString::new(s.as_bytes()) + .map_err(|_r| Error::from_io("CString::new()", io::ErrorKind::InvalidInput.into())) +} + +/// Call `lstat()` to get the device and inode numbers for `/`. +#[cfg(unix)] +fn get_root_dev_ino() -> Result<(libc::ino_t, libc::dev_t)> { + use std::os::unix::fs::MetadataExt; + + fs::symlink_metadata("/") + .map(|md| (md.ino(), md.dev())) + .map_err(|r| Error::from_io1("std::fs::symlink_metadata", "/", r)) +} + +fn root_dev_ino_check( + root_dev_ino: Option<(libc::ino_t, libc::dev_t)>, + dir_dev_ino: (libc::ino_t, libc::dev_t), +) -> bool { + root_dev_ino.map_or(false, |root_dev_ino| root_dev_ino == dir_dev_ino) +} + +fn root_dev_ino_warn(dir_name: &Path) { + if dir_name.as_os_str() == "/" { + show_warning!( + "It is dangerous to operate recursively on '/'. \ + Use --{} to override this failsafe.", + options::preserve_root::NO_PRESERVE_ROOT, + ); + } else { + show_warning!( + "It is dangerous to operate recursively on {} (same as '/'). \ + Use --{} to override this failsafe.", + dir_name.quote(), + options::preserve_root::NO_PRESERVE_ROOT, + ); + } +} + +// When fts_read returns FTS_DC to indicate a directory cycle, it may or may not indicate +// a real problem. +// When a program like chgrp performs a recursive traversal that requires traversing symbolic links, +// it is *not* a problem. +// However, when invoked with "-P -R", it deserves a warning. +// The fts_options parameter records the options that control this aspect of fts's behavior, +// so test that. +fn cycle_warning_required(fts_options: c_int, entry: &fts::EntryRef) -> bool { + // When dereferencing no symlinks, or when dereferencing only those listed on the command line + // and we're not processing a command-line argument, then a cycle is a serious problem. + ((fts_options & fts_sys::FTS_PHYSICAL) != 0) + && (((fts_options & fts_sys::FTS_COMFOLLOW) == 0) || entry.level() != 0) +} + +fn emit_cycle_warning(file_name: &Path) { + show_warning!( + "Circular directory structure.\n\ +This almost certainly means that you have a corrupted file system.\n\ +NOTIFY YOUR SYSTEM MANAGER.\n\ +The following directory is part of the cycle {}.", + file_name.quote() + ) +} + +#[derive(Debug)] +enum SELinuxSecurityContext<'t> { + File(SecurityContext<'t>), + String(Option), +} + +impl<'t> SELinuxSecurityContext<'t> { + fn to_c_string(&self) -> Result>> { + match self { + Self::File(context) => context + .to_c_string() + .map_err(|r| Error::from_selinux("SELinuxSecurityContext::to_c_string()", r)), + + Self::String(context) => Ok(context.as_deref().map(Cow::Borrowed)), + } + } +} diff --git a/src/uu/chcon/src/errors.rs b/src/uu/chcon/src/errors.rs new file mode 100644 index 000000000..2d8f72e67 --- /dev/null +++ b/src/uu/chcon/src/errors.rs @@ -0,0 +1,73 @@ +use std::ffi::OsString; +use std::fmt::Write; +use std::io; + +use uucore::display::Quotable; + +pub(crate) type Result = std::result::Result; + +#[derive(thiserror::Error, Debug)] +pub(crate) enum Error { + #[error("No context is specified")] + MissingContext, + + #[error("No files are specified")] + MissingFiles, + + #[error("{0}")] + ArgumentsMismatch(String), + + #[error(transparent)] + CommandLine(#[from] clap::Error), + + #[error("{operation} failed")] + SELinux { + operation: &'static str, + source: selinux::errors::Error, + }, + + #[error("{operation} failed")] + Io { + operation: &'static str, + source: io::Error, + }, + + #[error("{operation} failed on {}", .operand1.quote())] + Io1 { + operation: &'static str, + operand1: OsString, + source: io::Error, + }, +} + +impl Error { + pub(crate) fn from_io(operation: &'static str, source: io::Error) -> Self { + Self::Io { operation, source } + } + + pub(crate) fn from_io1( + operation: &'static str, + operand1: impl Into, + source: io::Error, + ) -> Self { + Self::Io1 { + operation, + operand1: operand1.into(), + source, + } + } + + pub(crate) fn from_selinux(operation: &'static str, source: selinux::errors::Error) -> Self { + Self::SELinux { operation, source } + } +} + +pub(crate) fn report_full_error(mut err: &dyn std::error::Error) -> String { + let mut desc = String::with_capacity(256); + write!(&mut desc, "{}", err).unwrap(); + while let Some(source) = err.source() { + err = source; + write!(&mut desc, ". {}", err).unwrap(); + } + desc +} diff --git a/src/uu/chcon/src/fts.rs b/src/uu/chcon/src/fts.rs new file mode 100644 index 000000000..89dd6184d --- /dev/null +++ b/src/uu/chcon/src/fts.rs @@ -0,0 +1,193 @@ +use std::ffi::{CStr, CString, OsStr}; +use std::marker::PhantomData; +use std::os::raw::{c_int, c_long, c_short}; +use std::path::Path; +use std::ptr::NonNull; +use std::{io, iter, ptr, slice}; + +use crate::errors::{Error, Result}; +use crate::os_str_to_c_string; + +#[derive(Debug)] +pub(crate) struct FTS { + fts: ptr::NonNull, + + entry: Option>, + _phantom_data: PhantomData, +} + +impl FTS { + pub(crate) fn new(paths: I, options: c_int) -> Result + where + I: IntoIterator, + I::Item: AsRef, + { + let files_paths: Vec = paths + .into_iter() + .map(|s| os_str_to_c_string(s.as_ref())) + .collect::>()?; + + if files_paths.is_empty() { + return Err(Error::from_io( + "FTS::new()", + io::ErrorKind::InvalidInput.into(), + )); + } + + let path_argv: Vec<_> = files_paths + .iter() + .map(CString::as_ref) + .map(CStr::as_ptr) + .chain(iter::once(ptr::null())) + .collect(); + + // SAFETY: We assume calling fts_open() is safe: + // - `path_argv` is an array holding at least one path, and null-terminated. + // - `compar` is None. + let fts = unsafe { fts_sys::fts_open(path_argv.as_ptr().cast(), options, None) }; + + let fts = ptr::NonNull::new(fts) + .ok_or_else(|| Error::from_io("fts_open()", io::Error::last_os_error()))?; + + Ok(Self { + fts, + entry: None, + _phantom_data: PhantomData, + }) + } + + pub(crate) fn last_entry_ref(&mut self) -> Option { + self.entry.map(move |entry| EntryRef::new(self, entry)) + } + + pub(crate) fn read_next_entry(&mut self) -> Result { + // SAFETY: We assume calling fts_read() is safe with a non-null `fts` + // pointer assumed to be valid. + let new_entry = unsafe { fts_sys::fts_read(self.fts.as_ptr()) }; + + self.entry = NonNull::new(new_entry); + if self.entry.is_none() { + let r = io::Error::last_os_error(); + if let Some(0) = r.raw_os_error() { + Ok(false) + } else { + Err(Error::from_io("fts_read()", r)) + } + } else { + Ok(true) + } + } + + pub(crate) fn set(&mut self, instr: c_int) -> Result<()> { + let fts = self.fts.as_ptr(); + let entry = self + .entry + .ok_or_else(|| Error::from_io("FTS::set()", io::ErrorKind::UnexpectedEof.into()))?; + + // SAFETY: We assume calling fts_set() is safe with non-null `fts` + // and `entry` pointers assumed to be valid. + if unsafe { fts_sys::fts_set(fts, entry.as_ptr(), instr) } == -1 { + Err(Error::from_io("fts_set()", io::Error::last_os_error())) + } else { + Ok(()) + } + } +} + +impl Drop for FTS { + fn drop(&mut self) { + // SAFETY: We assume calling fts_close() is safe with a non-null `fts` + // pointer assumed to be valid. + unsafe { fts_sys::fts_close(self.fts.as_ptr()) }; + } +} + +#[derive(Debug)] +pub(crate) struct EntryRef<'fts> { + pub(crate) pointer: ptr::NonNull, + + _fts: PhantomData<&'fts FTS>, + _phantom_data: PhantomData, +} + +impl<'fts> EntryRef<'fts> { + fn new(_fts: &'fts FTS, entry: ptr::NonNull) -> Self { + Self { + pointer: entry, + _fts: PhantomData, + _phantom_data: PhantomData, + } + } + + fn as_ref(&self) -> &fts_sys::FTSENT { + // SAFETY: `self.pointer` is a non-null pointer that is assumed to be valid. + unsafe { self.pointer.as_ref() } + } + + fn as_mut(&mut self) -> &mut fts_sys::FTSENT { + // SAFETY: `self.pointer` is a non-null pointer that is assumed to be valid. + unsafe { self.pointer.as_mut() } + } + + pub(crate) fn flags(&self) -> c_int { + c_int::from(self.as_ref().fts_info) + } + + pub(crate) fn errno(&self) -> c_int { + self.as_ref().fts_errno + } + + pub(crate) fn level(&self) -> c_short { + self.as_ref().fts_level + } + + pub(crate) fn number(&self) -> c_long { + self.as_ref().fts_number + } + + pub(crate) fn set_number(&mut self, new_number: c_long) { + self.as_mut().fts_number = new_number; + } + + pub(crate) fn path(&self) -> Option<&Path> { + let entry = self.as_ref(); + if entry.fts_pathlen == 0 { + return None; + } + + NonNull::new(entry.fts_path) + .map(|path_ptr| { + let path_size = usize::from(entry.fts_pathlen).saturating_add(1); + + // SAFETY: `entry.fts_path` is a non-null pointer that is assumed to be valid. + unsafe { slice::from_raw_parts(path_ptr.as_ptr().cast(), path_size) } + }) + .and_then(|bytes| CStr::from_bytes_with_nul(bytes).ok()) + .map(c_str_to_os_str) + .map(Path::new) + } + + pub(crate) fn access_path(&self) -> Option<&Path> { + ptr::NonNull::new(self.as_ref().fts_accpath) + .map(|path_ptr| { + // SAFETY: `entry.fts_accpath` is a non-null pointer that is assumed to be valid. + unsafe { CStr::from_ptr(path_ptr.as_ptr()) } + }) + .map(c_str_to_os_str) + .map(Path::new) + } + + pub(crate) fn stat(&self) -> Option<&libc::stat> { + ptr::NonNull::new(self.as_ref().fts_statp).map(|stat_ptr| { + // SAFETY: `entry.fts_statp` is a non-null pointer that is assumed to be valid. + unsafe { stat_ptr.as_ref() } + }) + } +} + +#[cfg(unix)] +fn c_str_to_os_str(s: &CStr) -> &OsStr { + use std::os::unix::ffi::OsStrExt; + + OsStr::from_bytes(s.to_bytes()) +} diff --git a/src/uu/chcon/src/main.rs b/src/uu/chcon/src/main.rs new file mode 100644 index 000000000..e23b34c5b --- /dev/null +++ b/src/uu/chcon/src/main.rs @@ -0,0 +1 @@ +uucore_procs::main!(uu_chcon); diff --git a/src/uu/chgrp/Cargo.toml b/src/uu/chgrp/Cargo.toml index 5a2591f56..acc03472c 100644 --- a/src/uu/chgrp/Cargo.toml +++ b/src/uu/chgrp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chgrp" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "chgrp ~ (uutils) change the group ownership of FILE" @@ -16,9 +16,8 @@ path = "src/chgrp.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } -walkdir = "2.2" +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "chgrp" diff --git a/src/uu/chgrp/src/chgrp.rs b/src/uu/chgrp/src/chgrp.rs index 489be59eb..c70e5e5c7 100644 --- a/src/uu/chgrp/src/chgrp.rs +++ b/src/uu/chgrp/src/chgrp.rs @@ -5,200 +5,67 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) COMFOLLOW Chgrper RFILE RFILE's derefer dgid nonblank nonprint nonprinting +// spell-checker:ignore (ToDO) COMFOLLOW Chowner RFILE RFILE's derefer dgid nonblank nonprint nonprinting -#[macro_use] -extern crate uucore; +use uucore::display::Quotable; pub use uucore::entries; -use uucore::fs::resolve_relative_path; -use uucore::libc::gid_t; -use uucore::perms::{wrap_chgrp, Verbosity}; +use uucore::error::{FromIo, UResult, USimpleError}; +use uucore::perms::{chown_base, options, IfFrom}; -use clap::{App, Arg}; - -extern crate walkdir; -use walkdir::WalkDir; +use clap::{App, Arg, ArgMatches}; use std::fs; -use std::fs::Metadata; use std::os::unix::fs::MetadataExt; -use std::path::Path; -use uucore::InvalidEncodingHandling; - static ABOUT: &str = "Change the group of each FILE to GROUP."; static VERSION: &str = env!("CARGO_PKG_VERSION"); -pub mod options { - pub mod verbosity { - pub static CHANGES: &str = "changes"; - pub static QUIET: &str = "quiet"; - pub static SILENT: &str = "silent"; - pub static VERBOSE: &str = "verbose"; - } - pub mod preserve_root { - pub static PRESERVE: &str = "preserve-root"; - pub static NO_PRESERVE: &str = "no-preserve-root"; - } - pub mod dereference { - pub static DEREFERENCE: &str = "dereference"; - pub static NO_DEREFERENCE: &str = "no-dereference"; - } - pub static RECURSIVE: &str = "recursive"; - pub mod traverse { - pub static TRAVERSE: &str = "H"; - pub static NO_TRAVERSE: &str = "P"; - pub static EVERY: &str = "L"; - } - pub static REFERENCE: &str = "reference"; - pub static ARG_GROUP: &str = "GROUP"; - pub static ARG_FILES: &str = "FILE"; -} - -const FTS_COMFOLLOW: u8 = 1; -const FTS_PHYSICAL: u8 = 1 << 1; -const FTS_LOGICAL: u8 = 1 << 2; - fn get_usage() -> String { format!( "{0} [OPTION]... GROUP FILE...\n {0} [OPTION]... --reference=RFILE FILE...", - executable!() + uucore::execution_phrase() ) } -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); - - let usage = get_usage(); - - let mut app = uu_app().usage(&usage[..]); - - // we change the positional args based on whether - // --reference was used. - let mut reference = false; - let mut help = false; - // stop processing options on -- - for arg in args.iter().take_while(|s| *s != "--") { - if arg.starts_with("--reference=") || arg == "--reference" { - reference = true; - } else if arg == "--help" { - // we stop processing once we see --help, - // as it doesn't matter if we've seen reference or not - help = true; - break; - } - } - - if help || !reference { - // add both positional arguments - app = app.arg( - Arg::with_name(options::ARG_GROUP) - .value_name(options::ARG_GROUP) - .required(true) - .takes_value(true) - .multiple(false), - ) - } - app = app.arg( - Arg::with_name(options::ARG_FILES) - .value_name(options::ARG_FILES) - .multiple(true) - .takes_value(true) - .required(true) - .min_values(1), - ); - - let matches = app.get_matches_from(args); - - /* Get the list of files */ - let files: Vec = matches - .values_of(options::ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let preserve_root = matches.is_present(options::preserve_root::PRESERVE); - - let mut derefer = if matches.is_present(options::dereference::DEREFERENCE) { - 1 - } else if matches.is_present(options::dereference::NO_DEREFERENCE) { - 0 - } else { - -1 - }; - - let mut bit_flag = if matches.is_present(options::traverse::TRAVERSE) { - FTS_COMFOLLOW | FTS_PHYSICAL - } else if matches.is_present(options::traverse::EVERY) { - FTS_LOGICAL - } else { - FTS_PHYSICAL - }; - - let recursive = matches.is_present(options::RECURSIVE); - if recursive { - if bit_flag == FTS_PHYSICAL { - if derefer == 1 { - show_error!("-R --dereference requires -H or -L"); - return 1; - } - derefer = 0; - } - } else { - bit_flag = FTS_PHYSICAL; - } - - let verbosity = if matches.is_present(options::verbosity::CHANGES) { - Verbosity::Changes - } else if matches.is_present(options::verbosity::SILENT) - || matches.is_present(options::verbosity::QUIET) - { - Verbosity::Silent - } else if matches.is_present(options::verbosity::VERBOSE) { - Verbosity::Verbose - } else { - Verbosity::Normal - }; - - let dest_gid: u32; - if let Some(file) = matches.value_of(options::REFERENCE) { - match fs::metadata(&file) { - Ok(meta) => { - dest_gid = meta.gid(); - } - Err(e) => { - show_error!("failed to get attributes of '{}': {}", file, e); - return 1; - } - } +fn parse_gid_and_uid(matches: &ArgMatches) -> UResult<(Option, Option, IfFrom)> { + let dest_gid = if let Some(file) = matches.value_of(options::REFERENCE) { + fs::metadata(&file) + .map(|meta| Some(meta.gid())) + .map_err_context(|| format!("failed to get attributes of {}", file.quote()))? } else { let group = matches.value_of(options::ARG_GROUP).unwrap_or_default(); - match entries::grp2gid(group) { - Ok(g) => { - dest_gid = g; - } - _ => { - show_error!("invalid group: {}", group); - return 1; + if group.is_empty() { + None + } else { + match entries::grp2gid(group) { + Ok(g) => Some(g), + _ => { + return Err(USimpleError::new( + 1, + format!("invalid group: {}", group.quote()), + )) + } } } - } - - let executor = Chgrper { - bit_flag, - dest_gid, - verbosity, - recursive, - dereference: derefer != 0, - preserve_root, - files, }; - executor.exec() + Ok((dest_gid, None, IfFrom::All)) +} + +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { + let usage = get_usage(); + + chown_base( + uu_app().usage(&usage[..]), + args, + options::ARG_GROUP, + parse_gid_and_uid, + true, + ) } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(VERSION) .about(ABOUT) .arg( @@ -276,170 +143,3 @@ pub fn uu_app() -> App<'static, 'static> { .help("traverse every symbolic link to a directory encountered"), ) } - -struct Chgrper { - dest_gid: gid_t, - bit_flag: u8, - verbosity: Verbosity, - files: Vec, - recursive: bool, - preserve_root: bool, - dereference: bool, -} - -macro_rules! unwrap { - ($m:expr, $e:ident, $err:block) => { - match $m { - Ok(meta) => meta, - Err($e) => $err, - } - }; -} - -impl Chgrper { - fn exec(&self) -> i32 { - let mut ret = 0; - for f in &self.files { - ret |= self.traverse(f); - } - ret - } - - #[cfg(windows)] - fn is_bind_root>(&self, root: P) -> bool { - // TODO: is there an equivalent on Windows? - false - } - - #[cfg(unix)] - fn is_bind_root>(&self, path: P) -> bool { - if let (Ok(given), Ok(root)) = (fs::metadata(path), fs::metadata("/")) { - given.dev() == root.dev() && given.ino() == root.ino() - } else { - // FIXME: not totally sure if it's okay to just ignore an error here - false - } - } - - fn traverse>(&self, root: P) -> i32 { - let follow_arg = self.dereference || self.bit_flag != FTS_PHYSICAL; - let path = root.as_ref(); - let meta = match self.obtain_meta(path, follow_arg) { - Some(m) => m, - _ => return 1, - }; - - // Prohibit only if: - // (--preserve-root and -R present) && - // ( - // (argument is not symlink && resolved to be '/') || - // (argument is symlink && should follow argument && resolved to be '/') - // ) - if self.recursive && self.preserve_root { - let may_exist = if follow_arg { - path.canonicalize().ok() - } else { - let real = resolve_relative_path(path); - if real.is_dir() { - Some(real.canonicalize().expect("failed to get real path")) - } else { - Some(real.into_owned()) - } - }; - - if let Some(p) = may_exist { - if p.parent().is_none() || self.is_bind_root(p) { - show_error!("it is dangerous to operate recursively on '/'"); - show_error!("use --no-preserve-root to override this failsafe"); - return 1; - } - } - } - - let ret = match wrap_chgrp( - path, - &meta, - self.dest_gid, - follow_arg, - self.verbosity.clone(), - ) { - Ok(n) => { - show_error!("{}", n); - 0 - } - Err(e) => { - if self.verbosity != Verbosity::Silent { - show_error!("{}", e); - } - 1 - } - }; - - if !self.recursive { - ret - } else { - ret | self.dive_into(&root) - } - } - - fn dive_into>(&self, root: P) -> i32 { - let mut ret = 0; - let root = root.as_ref(); - let follow = self.dereference || self.bit_flag & FTS_LOGICAL != 0; - for entry in WalkDir::new(root).follow_links(follow).min_depth(1) { - let entry = unwrap!(entry, e, { - ret = 1; - show_error!("{}", e); - continue; - }); - let path = entry.path(); - let meta = match self.obtain_meta(path, follow) { - Some(m) => m, - _ => { - ret = 1; - continue; - } - }; - - ret = match wrap_chgrp(path, &meta, self.dest_gid, follow, self.verbosity.clone()) { - Ok(n) => { - if !n.is_empty() { - show_error!("{}", n); - } - 0 - } - Err(e) => { - if self.verbosity != Verbosity::Silent { - show_error!("{}", e); - } - 1 - } - } - } - - ret - } - - fn obtain_meta>(&self, path: P, follow: bool) -> Option { - use self::Verbosity::*; - let path = path.as_ref(); - let meta = if follow { - unwrap!(path.metadata(), e, { - match self.verbosity { - Silent => (), - _ => show_error!("cannot access '{}': {}", path.display(), e), - } - return None; - }) - } else { - unwrap!(path.symlink_metadata(), e, { - match self.verbosity { - Silent => (), - _ => show_error!("cannot dereference '{}': {}", path.display(), e), - } - return None; - }) - }; - Some(meta) - } -} diff --git a/src/uu/chmod/Cargo.toml b/src/uu/chmod/Cargo.toml index df150e0ed..64961e6dd 100644 --- a/src/uu/chmod/Cargo.toml +++ b/src/uu/chmod/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chmod" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "chmod ~ (uutils) change mode of FILE" @@ -17,8 +17,8 @@ path = "src/chmod.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } libc = "0.2.42" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["fs", "mode"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features=["fs", "mode"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } walkdir = "2.2" [[bin]] diff --git a/src/uu/chmod/src/chmod.rs b/src/uu/chmod/src/chmod.rs index d89827c97..68c55b4cb 100644 --- a/src/uu/chmod/src/chmod.rs +++ b/src/uu/chmod/src/chmod.rs @@ -14,6 +14,7 @@ use clap::{crate_version, App, Arg}; use std::fs; use std::os::unix::fs::{MetadataExt, PermissionsExt}; use std::path::Path; +use uucore::display::Quotable; use uucore::fs::display_permissions_unix; use uucore::libc::mode_t; #[cfg(not(windows))] @@ -36,12 +37,12 @@ mod options { pub const FILE: &str = "FILE"; } -fn get_usage() -> String { +fn usage() -> String { format!( "{0} [OPTION]... MODE[,MODE]... FILE... or: {0} [OPTION]... OCTAL-MODE FILE... or: {0} [OPTION]... --reference=RFILE FILE...", - executable!() + uucore::execution_phrase() ) } @@ -56,9 +57,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { // Before we can parse 'args' with clap (and previously getopts), // a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE"). - let mode_had_minus_prefix = strip_minus_from_mode(&mut args); + let mode_had_minus_prefix = mode::strip_minus_from_mode(&mut args); - let usage = get_usage(); + let usage = usage(); let after_help = get_long_usage(); let matches = uu_app() @@ -75,7 +76,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .value_of(options::REFERENCE) .and_then(|fref| match fs::metadata(fref) { Ok(meta) => Some(meta.mode()), - Err(err) => crash!(1, "cannot stat attributes of '{}': {}", fref, err), + Err(err) => crash!(1, "cannot stat attributes of {}: {}", fref.quote(), err), }); let modes = matches.value_of(options::MODE).unwrap(); // should always be Some because required let cmode = if mode_had_minus_prefix { @@ -98,6 +99,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { Some(cmode) }; + if files.is_empty() { + crash!(1, "missing operand"); + } + let chmoder = Chmoder { changes, quiet, @@ -116,7 +121,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .arg( @@ -175,27 +180,6 @@ pub fn uu_app() -> App<'static, 'static> { ) } -// Iterate 'args' and delete the first occurrence -// of a prefix '-' if it's associated with MODE -// e.g. "chmod -v -xw -R FILE" -> "chmod -v xw -R FILE" -pub fn strip_minus_from_mode(args: &mut Vec) -> bool { - for arg in args { - if arg.starts_with('-') { - if let Some(second) = arg.chars().nth(1) { - match second { - 'r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7' => { - // TODO: use strip_prefix() once minimum rust version reaches 1.45.0 - *arg = arg[1..arg.len()].to_string(); - return true; - } - _ => {} - } - } - } - } - false -} - struct Chmoder { changes: bool, quiet: bool, @@ -216,21 +200,24 @@ impl Chmoder { if !file.exists() { if is_symlink(file) { println!( - "failed to change mode of '{}' from 0000 (---------) to 0000 (---------)", - filename + "failed to change mode of {} from 0000 (---------) to 0000 (---------)", + filename.quote() ); if !self.quiet { - show_error!("cannot operate on dangling symlink '{}'", filename); + show_error!("cannot operate on dangling symlink {}", filename.quote()); } - } else { - show_error!("cannot access '{}': No such file or directory", filename); + } else if !self.quiet { + show_error!( + "cannot access {}: No such file or directory", + filename.quote() + ); } return Err(1); } if self.recursive && self.preserve_root && filename == "/" { show_error!( - "it is dangerous to operate recursively on '{}'\nuse --no-preserve-root to override this failsafe", - filename + "it is dangerous to operate recursively on {}\nuse --no-preserve-root to override this failsafe", + filename.quote() ); return Err(1); } @@ -253,23 +240,27 @@ impl Chmoder { // instead it just sets the readonly attribute on the file Err(0) } - #[cfg(any(unix, target_os = "redox"))] + #[cfg(unix)] fn chmod_file(&self, file: &Path) -> Result<(), i32> { - let mut fperm = match fs::metadata(file) { + use uucore::mode::get_umask; + + let fperm = match fs::metadata(file) { Ok(meta) => meta.mode() & 0o7777, Err(err) => { if is_symlink(file) { if self.verbose { println!( - "neither symbolic link '{}' nor referent has been changed", - file.display() + "neither symbolic link {} nor referent has been changed", + file.quote() ); } return Ok(()); } else if err.kind() == std::io::ErrorKind::PermissionDenied { - show_error!("'{}': Permission denied", file.display()); + // These two filenames would normally be conditionally + // quoted, but GNU's tests expect them to always be quoted + show_error!("{}: Permission denied", file.quote()); } else { - show_error!("'{}': {}", file.display(), err); + show_error!("{}: {}", file.quote(), err); } return Err(1); } @@ -278,18 +269,30 @@ impl Chmoder { Some(mode) => self.change_file(fperm, mode, file)?, None => { let cmode_unwrapped = self.cmode.clone().unwrap(); + let mut new_mode = fperm; + let mut naively_expected_new_mode = new_mode; for mode in cmode_unwrapped.split(',') { // cmode is guaranteed to be Some in this case let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; let result = if mode.contains(arr) { - mode::parse_numeric(fperm, mode) + mode::parse_numeric(new_mode, mode, file.is_dir()).map(|v| (v, v)) } else { - mode::parse_symbolic(fperm, mode, file.is_dir()) + mode::parse_symbolic(new_mode, mode, get_umask(), file.is_dir()).map(|m| { + // calculate the new mode as if umask was 0 + let naive_mode = mode::parse_symbolic( + naively_expected_new_mode, + mode, + 0, + file.is_dir(), + ) + .unwrap(); // we know that mode must be valid, so this cannot fail + (m, naive_mode) + }) }; match result { - Ok(mode) => { - self.change_file(fperm, mode, file)?; - fperm = mode; + Ok((mode, naive_mode)) => { + new_mode = mode; + naively_expected_new_mode = naive_mode; } Err(f) => { if !self.quiet { @@ -299,6 +302,17 @@ impl Chmoder { } } } + self.change_file(fperm, new_mode, file)?; + // if a permission would have been removed if umask was 0, but it wasn't because umask was not 0, print an error and fail + if (new_mode & !naively_expected_new_mode) != 0 { + show_error!( + "{}: new permissions are {}, not {}", + file.maybe_quote(), + display_permissions_unix(new_mode as mode_t, false), + display_permissions_unix(naively_expected_new_mode as mode_t, false) + ); + return Err(1); + } } } @@ -310,8 +324,8 @@ impl Chmoder { if fperm == mode { if self.verbose && !self.changes { println!( - "mode of '{}' retained as {:04o} ({})", - file.display(), + "mode of {} retained as {:04o} ({})", + file.quote(), fperm, display_permissions_unix(fperm as mode_t, false), ); @@ -322,9 +336,9 @@ impl Chmoder { show_error!("{}", err); } if self.verbose { - show_error!( - "failed to change mode of file '{}' from {:o} ({}) to {:o} ({})", - file.display(), + println!( + "failed to change mode of file {} from {:04o} ({}) to {:04o} ({})", + file.quote(), fperm, display_permissions_unix(fperm as mode_t, false), mode, @@ -334,9 +348,9 @@ impl Chmoder { Err(1) } else { if self.verbose || self.changes { - show_error!( - "mode of '{}' changed from {:o} ({}) to {:o} ({})", - file.display(), + println!( + "mode of {} changed from {:04o} ({}) to {:04o} ({})", + file.quote(), fperm, display_permissions_unix(fperm as mode_t, false), mode, diff --git a/src/uu/chown/Cargo.toml b/src/uu/chown/Cargo.toml index 828c214be..20381c660 100644 --- a/src/uu/chown/Cargo.toml +++ b/src/uu/chown/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chown" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "chown ~ (uutils) change the ownership of FILE" @@ -16,10 +16,8 @@ path = "src/chown.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } -glob = "0.3.0" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } -walkdir = "2.2" +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "chown" diff --git a/src/uu/chown/src/chown.rs b/src/uu/chown/src/chown.rs index 081d3a148..f24c4ec89 100644 --- a/src/uu/chown/src/chown.rs +++ b/src/uu/chown/src/chown.rs @@ -5,133 +5,31 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) COMFOLLOW Chowner Passwd RFILE RFILE's derefer dgid duid +// spell-checker:ignore (ToDO) COMFOLLOW Passwd RFILE RFILE's derefer dgid duid groupname -#[macro_use] -extern crate uucore; +use uucore::display::Quotable; pub use uucore::entries::{self, Group, Locate, Passwd}; -use uucore::fs::resolve_relative_path; -use uucore::libc::{gid_t, uid_t}; -use uucore::perms::{wrap_chown, Verbosity}; +use uucore::perms::{chown_base, options, IfFrom}; -use uucore::error::{FromIo, UError, UResult, USimpleError}; +use uucore::error::{FromIo, UResult, USimpleError}; -use clap::{crate_version, App, Arg}; +use clap::{crate_version, App, Arg, ArgMatches}; -use walkdir::WalkDir; - -use std::fs::{self, Metadata}; +use std::fs; use std::os::unix::fs::MetadataExt; -use std::convert::AsRef; -use std::path::Path; -use uucore::InvalidEncodingHandling; - static ABOUT: &str = "change file owner and group"; -pub mod options { - pub mod verbosity { - pub static CHANGES: &str = "changes"; - pub static QUIET: &str = "quiet"; - pub static SILENT: &str = "silent"; - pub static VERBOSE: &str = "verbose"; - } - pub mod preserve_root { - pub static PRESERVE: &str = "preserve-root"; - pub static NO_PRESERVE: &str = "no-preserve-root"; - } - pub mod dereference { - pub static DEREFERENCE: &str = "dereference"; - pub static NO_DEREFERENCE: &str = "no-dereference"; - } - pub static FROM: &str = "from"; - pub static RECURSIVE: &str = "recursive"; - pub mod traverse { - pub static TRAVERSE: &str = "H"; - pub static NO_TRAVERSE: &str = "P"; - pub static EVERY: &str = "L"; - } - pub static REFERENCE: &str = "reference"; -} - -static ARG_OWNER: &str = "owner"; -static ARG_FILES: &str = "files"; - -const FTS_COMFOLLOW: u8 = 1; -const FTS_PHYSICAL: u8 = 1 << 1; -const FTS_LOGICAL: u8 = 1 << 2; - fn get_usage() -> String { format!( "{0} [OPTION]... [OWNER][:[GROUP]] FILE...\n{0} [OPTION]... --reference=RFILE FILE...", - executable!() + uucore::execution_phrase() ) } -#[uucore_procs::gen_uumain] -pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); - - let usage = get_usage(); - - let matches = uu_app().usage(&usage[..]).get_matches_from(args); - - /* First arg is the owner/group */ - let owner = matches.value_of(ARG_OWNER).unwrap(); - - /* Then the list of files */ - let files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let preserve_root = matches.is_present(options::preserve_root::PRESERVE); - - let mut derefer = if matches.is_present(options::dereference::NO_DEREFERENCE) { - 1 - } else { - 0 - }; - - let mut bit_flag = if matches.is_present(options::traverse::TRAVERSE) { - FTS_COMFOLLOW | FTS_PHYSICAL - } else if matches.is_present(options::traverse::EVERY) { - FTS_LOGICAL - } else { - FTS_PHYSICAL - }; - - let recursive = matches.is_present(options::RECURSIVE); - if recursive { - if bit_flag == FTS_PHYSICAL { - if derefer == 1 { - return Err(USimpleError::new( - 1, - "-R --dereference requires -H or -L".to_string(), - )); - } - derefer = 0; - } - } else { - bit_flag = FTS_PHYSICAL; - } - - let verbosity = if matches.is_present(options::verbosity::CHANGES) { - Verbosity::Changes - } else if matches.is_present(options::verbosity::SILENT) - || matches.is_present(options::verbosity::QUIET) - { - Verbosity::Silent - } else if matches.is_present(options::verbosity::VERBOSE) { - Verbosity::Verbose - } else { - Verbosity::Normal - }; - +fn parse_gid_uid_and_filter(matches: &ArgMatches) -> UResult<(Option, Option, IfFrom)> { let filter = if let Some(spec) = matches.value_of(options::FROM) { - match parse_spec(spec)? { + match parse_spec(spec, ':')? { (Some(uid), None) => IfFrom::User(uid), (None, Some(gid)) => IfFrom::Group(gid), (Some(uid), Some(gid)) => IfFrom::UserGroup(uid, gid), @@ -145,30 +43,32 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let dest_gid: Option; if let Some(file) = matches.value_of(options::REFERENCE) { let meta = fs::metadata(&file) - .map_err_context(|| format!("failed to get attributes of '{}'", file))?; + .map_err_context(|| format!("failed to get attributes of {}", file.quote()))?; dest_gid = Some(meta.gid()); dest_uid = Some(meta.uid()); } else { - let (u, g) = parse_spec(owner)?; + let (u, g) = parse_spec(matches.value_of(options::ARG_OWNER).unwrap(), ':')?; dest_uid = u; dest_gid = g; } - let executor = Chowner { - bit_flag, - dest_uid, - dest_gid, - verbosity, - recursive, - dereference: derefer != 0, - filter, - preserve_root, - files, - }; - executor.exec() + Ok((dest_gid, dest_uid, filter)) +} + +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { + let usage = get_usage(); + + chown_base( + uu_app().usage(&usage[..]), + args, + options::ARG_OWNER, + parse_gid_uid_and_filter, + false, + ) } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .arg( @@ -177,22 +77,31 @@ pub fn uu_app() -> App<'static, 'static> { .long(options::verbosity::CHANGES) .help("like verbose but report only when a change is made"), ) - .arg(Arg::with_name(options::dereference::DEREFERENCE).long(options::dereference::DEREFERENCE).help( - "affect the referent of each symbolic link (this is the default), rather than the symbolic link itself", - )) + .arg( + Arg::with_name(options::dereference::DEREFERENCE) + .long(options::dereference::DEREFERENCE) + .help( + "affect the referent of each symbolic link (this is the default), \ + rather than the symbolic link itself", + ), + ) .arg( Arg::with_name(options::dereference::NO_DEREFERENCE) .short("h") .long(options::dereference::NO_DEREFERENCE) .help( - "affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)", + "affect symbolic links instead of any referenced file \ + (useful only on systems that can change the ownership of a symlink)", ), ) .arg( Arg::with_name(options::FROM) .long(options::FROM) .help( - "change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute", + "change the owner and/or group of each file only if its \ + current owner and/or group match those specified here. \ + Either may be omitted, in which case a match is not required \ + for the omitted attribute", ) .value_name("CURRENT_OWNER:CURRENT_GROUP"), ) @@ -224,7 +133,11 @@ pub fn uu_app() -> App<'static, 'static> { .value_name("RFILE") .min_values(1), ) - .arg(Arg::with_name(options::verbosity::SILENT).short("f").long(options::verbosity::SILENT)) + .arg( + Arg::with_name(options::verbosity::SILENT) + .short("f") + .long(options::verbosity::SILENT), + ) .arg( Arg::with_name(options::traverse::TRAVERSE) .short(options::traverse::TRAVERSE) @@ -246,41 +159,55 @@ pub fn uu_app() -> App<'static, 'static> { .arg( Arg::with_name(options::verbosity::VERBOSE) .long(options::verbosity::VERBOSE) + .short("v") .help("output a diagnostic for every file processed"), ) - .arg( - Arg::with_name(ARG_OWNER) - .multiple(false) - .takes_value(true) - .required(true), - ) - .arg( - Arg::with_name(ARG_FILES) - .multiple(true) - .takes_value(true) - .required(true) - .min_values(1), - ) } -fn parse_spec(spec: &str) -> UResult<(Option, Option)> { - let args = spec.split_terminator(':').collect::>(); - let usr_only = args.len() == 1 && !args[0].is_empty(); - let grp_only = args.len() == 2 && args[0].is_empty(); - let usr_grp = args.len() == 2 && !args[0].is_empty() && !args[1].is_empty(); - let uid = if usr_only || usr_grp { - Some( - Passwd::locate(args[0]) - .map_err(|_| USimpleError::new(1, format!("invalid user: '{}'", spec)))? - .uid(), - ) +/// Parse the username and groupname +/// +/// In theory, it should be username:groupname +/// but ... +/// it can user.name:groupname +/// or username.groupname +/// +/// # Arguments +/// +/// * `spec` - The input from the user +/// * `sep` - Should be ':' or '.' +fn parse_spec(spec: &str, sep: char) -> UResult<(Option, Option)> { + assert!(['.', ':'].contains(&sep)); + let mut args = spec.splitn(2, sep); + let user = args.next().unwrap_or(""); + let group = args.next().unwrap_or(""); + + let uid = if !user.is_empty() { + Some(match Passwd::locate(user) { + Ok(u) => u.uid(), // We have been able to get the uid + Err(_) => + // we have NOT been able to find the uid + // but we could be in the case where we have user.group + { + if spec.contains('.') && !spec.contains(':') && sep == ':' { + // but the input contains a '.' but not a ':' + // we might have something like username.groupname + // So, try to parse it this way + return parse_spec(spec, '.'); + } else { + return Err(USimpleError::new( + 1, + format!("invalid user: {}", spec.quote()), + )); + } + } + }) } else { None }; - let gid = if grp_only || usr_grp { + let gid = if !group.is_empty() { Some( - Group::locate(args[1]) - .map_err(|_| USimpleError::new(1, format!("invalid group: '{}'", spec)))? + Group::locate(group) + .map_err(|_| USimpleError::new(1, format!("invalid group: {}", spec.quote())))? .gid(), ) } else { @@ -289,206 +216,16 @@ fn parse_spec(spec: &str) -> UResult<(Option, Option)> { Ok((uid, gid)) } -enum IfFrom { - All, - User(u32), - Group(u32), - UserGroup(u32, u32), -} - -struct Chowner { - dest_uid: Option, - dest_gid: Option, - bit_flag: u8, - verbosity: Verbosity, - filter: IfFrom, - files: Vec, - recursive: bool, - preserve_root: bool, - dereference: bool, -} - -macro_rules! unwrap { - ($m:expr, $e:ident, $err:block) => { - match $m { - Ok(meta) => meta, - Err($e) => $err, - } - }; -} - -impl Chowner { - fn exec(&self) -> UResult<()> { - let mut ret = 0; - for f in &self.files { - ret |= self.traverse(f); - } - if ret != 0 { - return Err(UError::from(ret)); - } - Ok(()) - } - - fn traverse>(&self, root: P) -> i32 { - let follow_arg = self.dereference || self.bit_flag != FTS_PHYSICAL; - let path = root.as_ref(); - let meta = match self.obtain_meta(path, follow_arg) { - Some(m) => m, - _ => return 1, - }; - - // Prohibit only if: - // (--preserve-root and -R present) && - // ( - // (argument is not symlink && resolved to be '/') || - // (argument is symlink && should follow argument && resolved to be '/') - // ) - if self.recursive && self.preserve_root { - let may_exist = if follow_arg { - path.canonicalize().ok() - } else { - let real = resolve_relative_path(path); - if real.is_dir() { - Some(real.canonicalize().expect("failed to get real path")) - } else { - Some(real.into_owned()) - } - }; - - if let Some(p) = may_exist { - if p.parent().is_none() { - show_error!("it is dangerous to operate recursively on '/'"); - show_error!("use --no-preserve-root to override this failsafe"); - return 1; - } - } - } - - let ret = if self.matched(meta.uid(), meta.gid()) { - match wrap_chown( - path, - &meta, - self.dest_uid, - self.dest_gid, - follow_arg, - self.verbosity.clone(), - ) { - Ok(n) => { - if !n.is_empty() { - show_error!("{}", n); - } - 0 - } - Err(e) => { - if self.verbosity != Verbosity::Silent { - show_error!("{}", e); - } - 1 - } - } - } else { - 0 - }; - - if !self.recursive { - ret - } else { - ret | self.dive_into(&root) - } - } - - fn dive_into>(&self, root: P) -> i32 { - let mut ret = 0; - let root = root.as_ref(); - let follow = self.dereference || self.bit_flag & FTS_LOGICAL != 0; - for entry in WalkDir::new(root).follow_links(follow).min_depth(1) { - let entry = unwrap!(entry, e, { - ret = 1; - show_error!("{}", e); - continue; - }); - let path = entry.path(); - let meta = match self.obtain_meta(path, follow) { - Some(m) => m, - _ => { - ret = 1; - continue; - } - }; - - if !self.matched(meta.uid(), meta.gid()) { - continue; - } - - ret = match wrap_chown( - path, - &meta, - self.dest_uid, - self.dest_gid, - follow, - self.verbosity.clone(), - ) { - Ok(n) => { - if !n.is_empty() { - show_error!("{}", n); - } - 0 - } - Err(e) => { - if self.verbosity != Verbosity::Silent { - show_error!("{}", e); - } - 1 - } - } - } - ret - } - - fn obtain_meta>(&self, path: P, follow: bool) -> Option { - use self::Verbosity::*; - let path = path.as_ref(); - let meta = if follow { - unwrap!(path.metadata(), e, { - match self.verbosity { - Silent => (), - _ => show_error!("cannot access '{}': {}", path.display(), e), - } - return None; - }) - } else { - unwrap!(path.symlink_metadata(), e, { - match self.verbosity { - Silent => (), - _ => show_error!("cannot dereference '{}': {}", path.display(), e), - } - return None; - }) - }; - Some(meta) - } - - #[inline] - fn matched(&self, uid: uid_t, gid: gid_t) -> bool { - match self.filter { - IfFrom::All => true, - IfFrom::User(u) => u == uid, - IfFrom::Group(g) => g == gid, - IfFrom::UserGroup(u, g) => u == uid && g == gid, - } - } -} - #[cfg(test)] mod test { use super::*; #[test] fn test_parse_spec() { - assert_eq!(parse_spec(":"), Ok((None, None))); - assert!(parse_spec("::") - .err() - .unwrap() - .starts_with("invalid group: ")); + assert!(matches!(parse_spec(":", ':'), Ok((None, None)))); + assert!(matches!(parse_spec(".", ':'), Ok((None, None)))); + assert!(matches!(parse_spec(".", '.'), Ok((None, None)))); + assert!(format!("{}", parse_spec("::", ':').err().unwrap()).starts_with("invalid group: ")); + assert!(format!("{}", parse_spec("..", ':').err().unwrap()).starts_with("invalid group: ")); } } diff --git a/src/uu/chroot/Cargo.toml b/src/uu/chroot/Cargo.toml index a808a717b..362e43b59 100644 --- a/src/uu/chroot/Cargo.toml +++ b/src/uu/chroot/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chroot" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "chroot ~ (uutils) run COMMAND under a new root directory" @@ -16,8 +16,8 @@ path = "src/chroot.rs" [dependencies] clap= "2.33" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["entries"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features=["entries"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "chroot" diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 2c0f8522c..55097c1bb 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -15,10 +15,10 @@ use std::ffi::CString; use std::io::Error; use std::path::Path; use std::process::Command; +use uucore::display::Quotable; use uucore::libc::{self, chroot, setgid, setgroups, setuid}; use uucore::{entries, InvalidEncodingHandling}; -static NAME: &str = "chroot"; static ABOUT: &str = "Run COMMAND with root directory set to NEWROOT."; static SYNTAX: &str = "[OPTION]... NEWROOT [COMMAND [ARG]...]"; @@ -47,15 +47,15 @@ pub fn uumain(args: impl uucore::Args) -> i32 { None => crash!( 1, "Missing operand: NEWROOT\nTry '{} --help' for more information.", - NAME + uucore::execution_phrase() ), }; if !newroot.is_dir() { crash!( 1, - "cannot change root directory to `{}`: no such directory", - newroot.display() + "cannot change root directory to {}: no such directory", + newroot.quote() ); } @@ -67,7 +67,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { // TODO: refactor the args and command matching // See: https://github.com/uutils/coreutils/pull/2365#discussion_r647849967 let command: Vec<&str> = match commands.len() { - 1 => { + 0 => { let shell: &str = match user_shell { Err(_) => default_shell, Ok(ref s) => s.as_ref(), @@ -77,12 +77,28 @@ pub fn uumain(args: impl uucore::Args) -> i32 { _ => commands, }; + assert!(!command.is_empty()); + let chroot_command = command[0]; + let chroot_args = &command[1..]; + + // NOTE: Tests can only trigger code beyond this point if they're invoked with root permissions set_context(newroot, &matches); - let pstatus = Command::new(command[0]) - .args(&command[1..]) + let pstatus = Command::new(chroot_command) + .args(chroot_args) .status() - .unwrap_or_else(|e| crash!(1, "Cannot exec: {}", e)); + .unwrap_or_else(|e| { + // TODO: Exit status: + // 125 if chroot itself fails + // 126 if command is found but cannot be invoked + // 127 if command cannot be found + crash!( + 1, + "failed to run command {}: {}", + command[0].to_string().quote(), + e + ) + }); if pstatus.success() { 0 @@ -92,7 +108,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .usage(SYNTAX) @@ -150,7 +166,7 @@ fn set_context(root: &Path, options: &clap::ArgMatches) { Some(u) => { let s: Vec<&str> = u.split(':').collect(); if s.len() != 2 || s.iter().any(|&spec| spec.is_empty()) { - crash!(1, "invalid userspec: `{}`", u) + crash!(1, "invalid userspec: {}", u.quote()) }; s } @@ -171,7 +187,6 @@ fn set_context(root: &Path, options: &clap::ArgMatches) { } fn enter_chroot(root: &Path) { - let root_str = root.display(); std::env::set_current_dir(root).unwrap(); let err = unsafe { chroot(CString::new(".").unwrap().as_bytes_with_nul().as_ptr() as *const libc::c_char) @@ -180,7 +195,7 @@ fn enter_chroot(root: &Path) { crash!( 1, "cannot chroot to {}: {}", - root_str, + root.quote(), Error::last_os_error() ) }; @@ -190,7 +205,7 @@ fn set_main_group(group: &str) { if !group.is_empty() { let group_id = match entries::grp2gid(group) { Ok(g) => g, - _ => crash!(1, "no such group: {}", group), + _ => crash!(1, "no such group: {}", group.maybe_quote()), }; let err = unsafe { setgid(group_id) }; if err != 0 { @@ -235,7 +250,12 @@ fn set_user(user: &str) { let user_id = entries::usr2uid(user).unwrap(); let err = unsafe { setuid(user_id as libc::uid_t) }; if err != 0 { - crash!(1, "cannot set user to {}: {}", user, Error::last_os_error()) + crash!( + 1, + "cannot set user to {}: {}", + user.maybe_quote(), + Error::last_os_error() + ) } } } diff --git a/src/uu/cksum/Cargo.toml b/src/uu/cksum/Cargo.toml index b92b680c8..b7f81d74b 100644 --- a/src/uu/cksum/Cargo.toml +++ b/src/uu/cksum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cksum" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "cksum ~ (uutils) display CRC and size of input" @@ -17,9 +17,13 @@ path = "src/cksum.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } libc = "0.2.42" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore" } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "cksum" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index e88cc78b3..92853a3e8 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -14,6 +14,7 @@ use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{self, stdin, BufReader, Read}; use std::path::Path; +use uucore::display::Quotable; use uucore::InvalidEncodingHandling; // NOTE: CRC_TABLE_LEN *must* be <= 256 as we cast 0..CRC_TABLE_LEN to u8 @@ -24,60 +25,15 @@ const NAME: &str = "cksum"; const SYNTAX: &str = "[OPTIONS] [FILE]..."; const SUMMARY: &str = "Print CRC and size for each file"; -// this is basically a hack to get "loops" to work on Rust 1.33. Once we update to Rust 1.46 or -// greater, we can just use while loops -macro_rules! unroll { - (256, |$i:ident| $s:expr) => {{ - unroll!(@ 32, 0 * 32, $i, $s); - unroll!(@ 32, 1 * 32, $i, $s); - unroll!(@ 32, 2 * 32, $i, $s); - unroll!(@ 32, 3 * 32, $i, $s); - unroll!(@ 32, 4 * 32, $i, $s); - unroll!(@ 32, 5 * 32, $i, $s); - unroll!(@ 32, 6 * 32, $i, $s); - unroll!(@ 32, 7 * 32, $i, $s); - }}; - (8, |$i:ident| $s:expr) => {{ - unroll!(@ 8, 0, $i, $s); - }}; - - (@ 32, $start:expr, $i:ident, $s:expr) => {{ - unroll!(@ 8, $start + 0 * 8, $i, $s); - unroll!(@ 8, $start + 1 * 8, $i, $s); - unroll!(@ 8, $start + 2 * 8, $i, $s); - unroll!(@ 8, $start + 3 * 8, $i, $s); - }}; - (@ 8, $start:expr, $i:ident, $s:expr) => {{ - unroll!(@ 4, $start, $i, $s); - unroll!(@ 4, $start + 4, $i, $s); - }}; - (@ 4, $start:expr, $i:ident, $s:expr) => {{ - unroll!(@ 2, $start, $i, $s); - unroll!(@ 2, $start + 2, $i, $s); - }}; - (@ 2, $start:expr, $i:ident, $s:expr) => {{ - unroll!(@ 1, $start, $i, $s); - unroll!(@ 1, $start + 1, $i, $s); - }}; - (@ 1, $start:expr, $i:ident, $s:expr) => {{ - let $i = $start; - let _ = $s; - }}; -} - const fn generate_crc_table() -> [u32; CRC_TABLE_LEN] { let mut table = [0; CRC_TABLE_LEN]; - // NOTE: works on Rust 1.46 - //let mut i = 0; - //while i < CRC_TABLE_LEN { - // table[i] = crc_entry(i as u8) as u32; - // - // i += 1; - //} - unroll!(256, |i| { + let mut i = 0; + while i < CRC_TABLE_LEN { table[i] = crc_entry(i as u8) as u32; - }); + + i += 1; + } table } @@ -85,19 +41,8 @@ const fn generate_crc_table() -> [u32; CRC_TABLE_LEN] { const fn crc_entry(input: u8) -> u32 { let mut crc = (input as u32) << 24; - // NOTE: this does not work on Rust 1.33, but *does* on 1.46 - //let mut i = 0; - //while i < 8 { - // if crc & 0x8000_0000 != 0 { - // crc <<= 1; - // crc ^= 0x04c1_1db7; - // } else { - // crc <<= 1; - // } - // - // i += 1; - //} - unroll!(8, |_i| { + let mut i = 0; + while i < 8 { let if_condition = crc & 0x8000_0000; let if_body = (crc << 1) ^ 0x04c1_1db7; let else_body = crc << 1; @@ -107,7 +52,8 @@ const fn crc_entry(input: u8) -> u32 { let condition_table = [else_body, if_body]; crc = condition_table[(if_condition != 0) as usize]; - }); + i += 1; + } crc } @@ -147,6 +93,8 @@ fn cksum(fname: &str) -> io::Result<(u32, usize)> { "Is a directory", )); }; + // Silent the warning as we want to the error message + #[allow(clippy::question_mark)] if path.metadata().is_err() { return Err(std::io::Error::new( io::ErrorKind::NotFound, @@ -191,7 +139,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { match cksum("-") { Ok((crc, size)) => println!("{} {}", crc, size), Err(err) => { - show_error!("{}", err); + show_error!("-: {}", err); return 2; } } @@ -203,7 +151,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { match cksum(fname.as_ref()) { Ok((crc, size)) => println!("{} {} {}", crc, size, fname), Err(err) => { - show_error!("'{}' {}", fname, err); + show_error!("{}: {}", fname.maybe_quote(), err); exit_code = 2; } } @@ -213,7 +161,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .name(NAME) .version(crate_version!()) .about(SUMMARY) diff --git a/src/uu/comm/Cargo.toml b/src/uu/comm/Cargo.toml index 1deb094e2..abcbff57b 100644 --- a/src/uu/comm/Cargo.toml +++ b/src/uu/comm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_comm" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "comm ~ (uutils) compare sorted inputs" @@ -17,9 +17,13 @@ path = "src/comm.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } libc = "0.2.42" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore" } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "comm" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/comm/src/comm.rs b/src/uu/comm/src/comm.rs index aa10432a2..56af42fd9 100644 --- a/src/uu/comm/src/comm.rs +++ b/src/uu/comm/src/comm.rs @@ -7,9 +7,6 @@ // spell-checker:ignore (ToDO) delim mkdelim -#[macro_use] -extern crate uucore; - use std::cmp::Ordering; use std::fs::File; use std::io::{self, stdin, BufRead, BufReader, Stdin}; @@ -31,8 +28,8 @@ mod options { pub const FILE_2: &str = "FILE2"; } -fn get_usage() -> String { - format!("{} [OPTION]... FILE1 FILE2", executable!()) +fn usage() -> String { + format!("{} [OPTION]... FILE1 FILE2", uucore::execution_phrase()) } fn mkdelim(col: usize, opts: &ArgMatches) -> String { @@ -132,7 +129,7 @@ fn open_file(name: &str) -> io::Result { } pub fn uumain(args: impl uucore::Args) -> i32 { - let usage = get_usage(); + let usage = usage(); let args = args .collect_str(InvalidEncodingHandling::ConvertLossy) .accept_any(); @@ -148,7 +145,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .after_help(LONG_HELP) diff --git a/src/uu/cp/Cargo.toml b/src/uu/cp/Cargo.toml index b7b3809a7..891bf0244 100644 --- a/src/uu/cp/Cargo.toml +++ b/src/uu/cp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cp" -version = "0.0.7" +version = "0.0.8" authors = [ "Jordy Dickinson ", "Joshua S. Miller ", @@ -23,19 +23,29 @@ clap = { version = "2.33", features = ["wrap_help"] } filetime = "0.2" libc = "0.2.85" quick-error = "1.2.3" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["fs"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +selinux = { version="0.2.3", optional=true } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features=["entries", "fs", "perms", "mode"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } walkdir = "2.2" [target.'cfg(target_os = "linux")'.dependencies] -ioctl-sys = "0.5.2" +ioctl-sys = "0.6" [target.'cfg(target_os = "windows")'.dependencies] winapi = { version="0.3", features=["fileapi"] } [target.'cfg(unix)'.dependencies] xattr="0.2.1" +exacl= { version = "0.6.0", optional=true } [[bin]] name = "cp" path = "src/main.rs" + +[features] +feat_selinux = ["selinux"] +feat_acl = ["exacl"] + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 7c67649c2..518a2262c 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -18,6 +18,7 @@ extern crate quick_error; #[macro_use] extern crate uucore; +use uucore::display::Quotable; #[cfg(windows)] use winapi::um::fileapi::CreateFileW; #[cfg(windows)] @@ -48,7 +49,8 @@ use std::path::{Path, PathBuf, StripPrefixError}; use std::str::FromStr; use std::string::ToString; use uucore::backup_control::{self, BackupMode}; -use uucore::fs::{canonicalize, CanonicalizeMode}; +use uucore::error::{set_exit_code, ExitCode, UError, UResult}; +use uucore::fs::{canonicalize, MissingHandling, ResolveMode}; use walkdir::WalkDir; #[cfg(unix)] @@ -67,6 +69,7 @@ quick_error! { IoErrContext(err: io::Error, path: String) { display("{}: {}", path, err) context(path: &'a str, err: io::Error) -> (err, path.to_owned()) + context(context: String, err: io::Error) -> (err, context) cause(err) } @@ -99,7 +102,13 @@ quick_error! { NotImplemented(opt: String) { display("Option '{}' not yet implemented.", opt) } /// Invalid arguments to backup - Backup(description: String) { display("{}\nTry 'cp --help' for more information.", description) } + Backup(description: String) { display("{}\nTry '{} --help' for more information.", description, uucore::execution_phrase()) } + } +} + +impl UError for Error { + fn code(&self) -> i32 { + EXIT_ERR } } @@ -180,12 +189,15 @@ pub enum CopyMode { AttrOnly, } -#[derive(Clone, Eq, PartialEq)] +// The ordering here determines the order in which attributes are (re-)applied. +// In particular, Ownership must be changed first to avoid interfering with mode change. +#[derive(Clone, Eq, PartialEq, Debug, PartialOrd, Ord)] pub enum Attribute { #[cfg(unix)] - Mode, Ownership, + Mode, Timestamps, + #[cfg(feature = "feat_selinux")] Context, Links, Xattr, @@ -215,15 +227,14 @@ pub struct Options { static ABOUT: &str = "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY."; static LONG_HELP: &str = ""; -static EXIT_OK: i32 = 0; static EXIT_ERR: i32 = 1; -fn get_usage() -> String { +fn usage() -> String { format!( "{0} [OPTION]... [-T] SOURCE DEST {0} [OPTION]... SOURCE... DIRECTORY {0} [OPTION]... -t DIRECTORY SOURCE...", - executable!() + uucore::execution_phrase() ) } @@ -231,8 +242,6 @@ fn get_usage() -> String { mod options { pub const ARCHIVE: &str = "archive"; pub const ATTRIBUTES_ONLY: &str = "attributes-only"; - pub const BACKUP: &str = "backup"; - pub const BACKUP_NO_ARG: &str = "b"; pub const CLI_SYMBOLIC_LINKS: &str = "cli-symbolic-links"; pub const CONTEXT: &str = "context"; pub const COPY_CONTENTS: &str = "copy-contents"; @@ -242,7 +251,7 @@ mod options { pub const LINK: &str = "link"; pub const NO_CLOBBER: &str = "no-clobber"; pub const NO_DEREFERENCE: &str = "no-dereference"; - pub const NO_DEREFERENCE_PRESERVE_LINKS: &str = "no-dereference-preserve-linkgs"; + pub const NO_DEREFERENCE_PRESERVE_LINKS: &str = "no-dereference-preserve-links"; pub const NO_PRESERVE: &str = "no-preserve"; pub const NO_TARGET_DIRECTORY: &str = "no-target-directory"; pub const ONE_FILE_SYSTEM: &str = "one-file-system"; @@ -257,7 +266,6 @@ mod options { pub const REMOVE_DESTINATION: &str = "remove-destination"; pub const SPARSE: &str = "sparse"; pub const STRIP_TRAILING_SLASHES: &str = "strip-trailing-slashes"; - pub const SUFFIX: &str = "suffix"; pub const SYMBOLIC_LINK: &str = "symbolic-link"; pub const TARGET_DIRECTORY: &str = "target-directory"; pub const UPDATE: &str = "update"; @@ -269,6 +277,7 @@ static PRESERVABLE_ATTRIBUTES: &[&str] = &[ "mode", "ownership", "timestamps", + #[cfg(feature = "feat_selinux")] "context", "links", "xattr", @@ -276,24 +285,18 @@ static PRESERVABLE_ATTRIBUTES: &[&str] = &[ ]; #[cfg(not(unix))] -static PRESERVABLE_ATTRIBUTES: &[&str] = &[ - "ownership", - "timestamps", - "context", - "links", - "xattr", - "all", -]; +static PRESERVABLE_ATTRIBUTES: &[&str] = + &["mode", "timestamps", "context", "links", "xattr", "all"]; static DEFAULT_ATTRIBUTES: &[Attribute] = &[ - #[cfg(unix)] Attribute::Mode, + #[cfg(unix)] Attribute::Ownership, Attribute::Timestamps, ]; pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .arg(Arg::with_name(options::TARGET_DIRECTORY) @@ -355,24 +358,9 @@ pub fn uu_app() -> App<'static, 'static> { .conflicts_with(options::FORCE) .help("remove each existing destination file before attempting to open it \ (contrast with --force). On Windows, current only works for writeable files.")) - .arg(Arg::with_name(options::BACKUP) - .long(options::BACKUP) - .help("make a backup of each existing destination file") - .takes_value(true) - .require_equals(true) - .min_values(0) - .value_name("CONTROL") - ) - .arg(Arg::with_name(options::BACKUP_NO_ARG) - .short(options::BACKUP_NO_ARG) - .help("like --backup but does not accept an argument") - ) - .arg(Arg::with_name(options::SUFFIX) - .short("S") - .long(options::SUFFIX) - .takes_value(true) - .value_name("SUFFIX") - .help("override the usual backup suffix")) + .arg(backup_control::arguments::backup()) + .arg(backup_control::arguments::backup_no_args()) + .arg(backup_control::arguments::suffix()) .arg(Arg::with_name(options::UPDATE) .short("u") .long(options::UPDATE) @@ -399,13 +387,13 @@ pub fn uu_app() -> App<'static, 'static> { .conflicts_with_all(&[options::PRESERVE_DEFAULT_ATTRIBUTES, options::NO_PRESERVE]) // -d sets this option // --archive sets this option - .help("Preserve the specified attributes (default: mode (unix only), ownership, timestamps), \ + .help("Preserve the specified attributes (default: mode, ownership (unix only), timestamps), \ if possible additional attributes: context, links, xattr, all")) .arg(Arg::with_name(options::PRESERVE_DEFAULT_ATTRIBUTES) .short("-p") .long(options::PRESERVE_DEFAULT_ATTRIBUTES) .conflicts_with_all(&[options::PRESERVE, options::NO_PRESERVE, options::ARCHIVE]) - .help("same as --preserve=mode(unix only),ownership,timestamps")) + .help("same as --preserve=mode,ownership(unix only),timestamps")) .arg(Arg::with_name(options::NO_PRESERVE) .long(options::NO_PRESERVE) .takes_value(true) @@ -464,8 +452,9 @@ pub fn uu_app() -> App<'static, 'static> { .multiple(true)) } -pub fn uumain(args: impl uucore::Args) -> i32 { - let usage = get_usage(); +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { + let usage = usage(); let matches = uu_app() .after_help(&*format!( "{}\n{}", @@ -475,11 +464,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .usage(&usage[..]) .get_matches_from(args); - let options = crash_if_err!(EXIT_ERR, Options::from_matches(&matches)); + let options = Options::from_matches(&matches)?; if options.overwrite == OverwriteMode::NoClobber && options.backup != BackupMode::NoBackup { show_usage_error!("options --backup and --no-clobber are mutually exclusive"); - return 1; + return Err(ExitCode(EXIT_ERR).into()); } let paths: Vec = matches @@ -487,7 +476,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); - let (sources, target) = crash_if_err!(EXIT_ERR, parse_path_args(&paths, &options)); + let (sources, target) = parse_path_args(&paths, &options)?; if let Err(error) = copy(&sources, &target, &options) { match error { @@ -497,10 +486,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { // Else we caught a fatal bubbled-up error, log it to stderr _ => show_error!("{}", error), }; - return EXIT_ERR; + set_exit_code(EXIT_ERR); } - EXIT_OK + Ok(()) } impl ClobberMode { @@ -550,17 +539,18 @@ impl FromStr for Attribute { fn from_str(value: &str) -> CopyResult { Ok(match &*value.to_lowercase() { - #[cfg(unix)] "mode" => Attribute::Mode, + #[cfg(unix)] "ownership" => Attribute::Ownership, "timestamps" => Attribute::Timestamps, + #[cfg(feature = "feat_selinux")] "context" => Attribute::Context, "links" => Attribute::Links, "xattr" => Attribute::Xattr, _ => { return Err(Error::InvalidArgument(format!( - "invalid attribute '{}'", - value + "invalid attribute {}", + value.quote() ))); } }) @@ -570,14 +560,16 @@ impl FromStr for Attribute { fn add_all_attributes() -> Vec { use Attribute::*; - #[cfg(target_os = "windows")] - let attr = vec![Ownership, Timestamps, Context, Xattr, Links]; - - #[cfg(not(target_os = "windows"))] - let mut attr = vec![Ownership, Timestamps, Context, Xattr, Links]; - - #[cfg(unix)] - attr.insert(0, Mode); + let attr = vec![ + #[cfg(unix)] + Ownership, + Mode, + Timestamps, + #[cfg(feature = "feat_selinux")] + Context, + Links, + Xattr, + ]; attr } @@ -604,20 +596,12 @@ impl Options { || matches.is_present(options::RECURSIVE_ALIAS) || matches.is_present(options::ARCHIVE); - let backup_mode = backup_control::determine_backup_mode( - matches.is_present(options::BACKUP_NO_ARG), - matches.is_present(options::BACKUP), - matches.value_of(options::BACKUP), - ); - let backup_mode = match backup_mode { - Err(err) => { - return Err(Error::Backup(err)); - } + let backup_mode = match backup_control::determine_backup_mode(matches) { + Err(e) => return Err(Error::Backup(format!("{}", e))), Ok(mode) => mode, }; - let backup_suffix = - backup_control::determine_backup_suffix(matches.value_of(options::SUFFIX)); + let backup_suffix = backup_control::determine_backup_suffix(matches); let overwrite = OverwriteMode::from_matches(matches); @@ -628,7 +612,7 @@ impl Options { .map(ToString::to_string); // Parse attributes to preserve - let preserve_attributes: Vec = if matches.is_present(options::PRESERVE) { + let mut preserve_attributes: Vec = if matches.is_present(options::PRESERVE) { match matches.values_of(options::PRESERVE) { None => DEFAULT_ATTRIBUTES.to_vec(), Some(attribute_strs) => { @@ -655,6 +639,11 @@ impl Options { vec![] }; + // Make sure ownership is changed before other attributes, + // as chown clears some of the permission and therefore could undo previous changes + // if not executed first. + preserve_attributes.sort_unstable(); + let options = Options { attributes_only: matches.is_present(options::ATTRIBUTES_ONLY), copy_contents: matches.is_present(options::COPY_CONTENTS), @@ -678,8 +667,8 @@ impl Options { "never" => ReflinkMode::Never, value => { return Err(Error::InvalidArgument(format!( - "invalid argument '{}' for \'reflink\'", - value + "invalid argument {} for \'reflink\'", + value.quote() ))); } } @@ -851,7 +840,7 @@ fn copy(sources: &[Source], target: &TargetSlice, options: &Options) -> CopyResu let mut seen_sources = HashSet::with_capacity(sources.len()); for source in sources { if seen_sources.contains(source) { - show_warning!("source '{}' specified more than once", source.display()); + show_warning!("source {} specified more than once", source.quote()); } else { let mut found_hard_link = false; if preserve_hard_links { @@ -892,8 +881,8 @@ fn construct_dest_path( ) -> CopyResult { if options.no_target_dir && target.is_dir() { return Err(format!( - "cannot overwrite directory '{}' with non-directory", - target.display() + "cannot overwrite directory {} with non-directory", + target.quote() ) .into()); } @@ -960,7 +949,7 @@ fn adjust_canonicalization(p: &Path) -> Cow { /// will not cause a short-circuit. fn copy_directory(root: &Path, target: &TargetSlice, options: &Options) -> CopyResult<()> { if !options.recursive { - return Err(format!("omitting directory '{}'", root.display()).into()); + return Err(format!("omitting directory {}", root.quote()).into()); } // if no-dereference is enabled and this is a symlink, copy it as a file @@ -1060,12 +1049,12 @@ impl OverwriteMode { match *self { OverwriteMode::NoClobber => Err(Error::NotAllFilesCopied), OverwriteMode::Interactive(_) => { - if prompt_yes!("{}: overwrite {}? ", executable!(), path.display()) { + if prompt_yes!("{}: overwrite {}? ", uucore::util_name(), path.quote()) { Ok(()) } else { Err(Error::Skipped(format!( "Not overwriting {} at user request", - path.display() + path.quote() ))) } } @@ -1075,27 +1064,66 @@ impl OverwriteMode { } fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResult<()> { - let context = &*format!("'{}' -> '{}'", source.display().to_string(), dest.display()); + let context = &*format!("{} -> {}", source.quote(), dest.quote()); + let source_metadata = fs::symlink_metadata(source).context(context)?; match *attribute { - #[cfg(unix)] Attribute::Mode => { - let mode = fs::metadata(source).context(context)?.permissions().mode(); - let mut dest_metadata = fs::metadata(source).context(context)?.permissions(); - dest_metadata.set_mode(mode); + fs::set_permissions(dest, source_metadata.permissions()).context(context)?; + // FIXME: Implement this for windows as well + #[cfg(feature = "feat_acl")] + exacl::getfacl(source, None) + .and_then(|acl| exacl::setfacl(&[dest], &acl, None)) + .map_err(|err| Error::Error(err.to_string()))?; } + #[cfg(unix)] Attribute::Ownership => { - let metadata = fs::metadata(source).context(context)?; - fs::set_permissions(dest, metadata.permissions()).context(context)?; + use std::os::unix::prelude::MetadataExt; + use uucore::perms::wrap_chown; + use uucore::perms::Verbosity; + use uucore::perms::VerbosityLevel; + + let dest_uid = source_metadata.uid(); + let dest_gid = source_metadata.gid(); + + wrap_chown( + dest, + &dest.symlink_metadata().context(context)?, + Some(dest_uid), + Some(dest_gid), + false, + Verbosity { + groups_only: false, + level: VerbosityLevel::Normal, + }, + ) + .map_err(Error::Error)?; } Attribute::Timestamps => { - let metadata = fs::metadata(source)?; filetime::set_file_times( Path::new(dest), - FileTime::from_last_access_time(&metadata), - FileTime::from_last_modification_time(&metadata), + FileTime::from_last_access_time(&source_metadata), + FileTime::from_last_modification_time(&source_metadata), )?; } - Attribute::Context => {} + #[cfg(feature = "feat_selinux")] + Attribute::Context => { + let context = selinux::SecurityContext::of_path(source, false, false).map_err(|e| { + format!( + "failed to get security context of {}: {}", + source.display(), + e + ) + })?; + if let Some(context) = context { + context.set_for_path(dest, false, false).map_err(|e| { + format!( + "failed to set security context for {}: {}", + dest.display(), + e + ) + })?; + } + } Attribute::Links => {} Attribute::Xattr => { #[cfg(unix)] @@ -1103,7 +1131,7 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu let xattrs = xattr::list(source)?; for attr in xattrs { if let Some(attr_value) = xattr::get(source, attr.clone())? { - crash_if_err!(EXIT_ERR, xattr::set(dest, attr, &attr_value[..])); + xattr::set(dest, attr, &attr_value[..])?; } } } @@ -1132,7 +1160,7 @@ fn symlink_file(source: &Path, dest: &Path, context: &str) -> CopyResult<()> { } fn context_for(src: &Path, dest: &Path) -> String { - format!("'{}' -> '{}'", src.display(), dest.display()) + format!("{} -> {}", src.quote(), dest.quote()) } /// Implements a simple backup copy for the destination file. @@ -1169,8 +1197,8 @@ fn handle_existing_dest(source: &Path, dest: &Path, options: &Options) -> CopyRe Ok(()) } -/// Copy the a file from `source` to `dest`. No path manipulation is -/// done on either `source` or `dest`, the are used as provided. +/// Copy the a file from `source` to `dest`. `source` will be dereferenced if +/// `options.dereference` is set to true. `dest` will always be dereferenced. /// /// Behavior when copying to existing files is contingent on the /// `options.overwrite` mode. If a file is skipped, the return type @@ -1187,41 +1215,66 @@ fn copy_file(source: &Path, dest: &Path, options: &Options) -> CopyResult<()> { println!("{}", context_for(source, dest)); } - #[allow(unused)] - { - // TODO: implement --preserve flag - let mut preserve_context = false; - for attribute in &options.preserve_attributes { - if *attribute == Attribute::Context { - preserve_context = true; - } + // Calculate the context upfront before canonicalizing the path + let context = context_for(source, dest); + let context = context.as_str(); + + // canonicalize dest and source so that later steps can work with the paths directly + let dest = canonicalize(dest, MissingHandling::Missing, ResolveMode::Physical).unwrap(); + let source = if options.dereference { + canonicalize(source, MissingHandling::Missing, ResolveMode::Physical).unwrap() + } else { + source.to_owned() + }; + + let dest_permissions = if dest.exists() { + dest.symlink_metadata().context(context)?.permissions() + } else { + #[allow(unused_mut)] + let mut permissions = source.symlink_metadata().context(context)?.permissions(); + #[cfg(unix)] + { + use uucore::mode::get_umask; + + let mut mode = permissions.mode(); + + // remove sticky bit, suid and gid bit + const SPECIAL_PERMS_MASK: u32 = 0o7000; + mode &= !SPECIAL_PERMS_MASK; + + // apply umask + mode &= !get_umask(); + + permissions.set_mode(mode); } - } + permissions + }; + match options.copy_mode { CopyMode::Link => { - fs::hard_link(source, dest).context(&*context_for(source, dest))?; + fs::hard_link(&source, &dest).context(context)?; } CopyMode::Copy => { - copy_helper(source, dest, options)?; + copy_helper(&source, &dest, options, context)?; } CopyMode::SymLink => { - symlink_file(source, dest, &*context_for(source, dest))?; + symlink_file(&source, &dest, context)?; } CopyMode::Sparse => return Err(Error::NotImplemented(options::SPARSE.to_string())), CopyMode::Update => { if dest.exists() { - let src_metadata = fs::metadata(source)?; - let dest_metadata = fs::metadata(dest)?; + let src_metadata = fs::symlink_metadata(&source)?; + let dest_metadata = fs::symlink_metadata(&dest)?; let src_time = src_metadata.modified()?; let dest_time = dest_metadata.modified()?; if src_time <= dest_time { return Ok(()); } else { - copy_helper(source, dest, options)?; + copy_helper(&source, &dest, options, context)?; } } else { - copy_helper(source, dest, options)?; + copy_helper(&source, &dest, options, context)?; } } CopyMode::AttrOnly => { @@ -1229,53 +1282,51 @@ fn copy_file(source: &Path, dest: &Path, options: &Options) -> CopyResult<()> { .write(true) .truncate(false) .create(true) - .open(dest) + .open(&dest) .unwrap(); } }; + + // TODO: implement something similar to gnu's lchown + if fs::symlink_metadata(&dest) + .map(|meta| !meta.file_type().is_symlink()) + .unwrap_or(false) + { + fs::set_permissions(&dest, dest_permissions).unwrap(); + } for attribute in &options.preserve_attributes { - copy_attribute(source, dest, attribute)?; + copy_attribute(&source, &dest, attribute)?; } Ok(()) } /// Copy the file from `source` to `dest` either using the normal `fs::copy` or a /// copy-on-write scheme if --reflink is specified and the filesystem supports it. -fn copy_helper(source: &Path, dest: &Path, options: &Options) -> CopyResult<()> { +fn copy_helper(source: &Path, dest: &Path, options: &Options, context: &str) -> CopyResult<()> { if options.parents { let parent = dest.parent().unwrap_or(dest); fs::create_dir_all(parent)?; } let is_symlink = fs::symlink_metadata(&source)?.file_type().is_symlink(); - if source.to_string_lossy() == "/dev/null" { + if source.as_os_str() == "/dev/null" { /* workaround a limitation of fs::copy * https://github.com/rust-lang/rust/issues/79390 */ File::create(dest)?; - } else if !options.dereference && is_symlink { + } else if is_symlink { copy_link(source, dest)?; } else if options.reflink_mode != ReflinkMode::Never { #[cfg(not(any(target_os = "linux", target_os = "macos")))] return Err("--reflink is only supported on linux and macOS" .to_string() .into()); - #[cfg(any(target_os = "linux", target_os = "macos"))] - if is_symlink { - assert!(options.dereference); - let real_path = std::fs::read_link(source)?; - #[cfg(target_os = "macos")] - copy_on_write_macos(&real_path, dest, options.reflink_mode)?; - #[cfg(target_os = "linux")] - copy_on_write_linux(&real_path, dest, options.reflink_mode)?; - } else { - #[cfg(target_os = "macos")] - copy_on_write_macos(source, dest, options.reflink_mode)?; - #[cfg(target_os = "linux")] - copy_on_write_linux(source, dest, options.reflink_mode)?; - } + #[cfg(target_os = "macos")] + copy_on_write_macos(source, dest, options.reflink_mode, context)?; + #[cfg(target_os = "linux")] + copy_on_write_linux(source, dest, options.reflink_mode, context)?; } else { - fs::copy(source, dest).context(&*context_for(source, dest))?; + fs::copy(source, dest).context(context)?; } Ok(()) @@ -1289,8 +1340,8 @@ fn copy_link(source: &Path, dest: &Path) -> CopyResult<()> { Some(name) => dest.join(name).into(), None => crash!( EXIT_ERR, - "cannot stat '{}': No such file or directory", - source.display() + "cannot stat {}: No such file or directory", + source.quote() ), } } else { @@ -1306,16 +1357,21 @@ fn copy_link(source: &Path, dest: &Path) -> CopyResult<()> { /// Copies `source` to `dest` using copy-on-write if possible. #[cfg(target_os = "linux")] -fn copy_on_write_linux(source: &Path, dest: &Path, mode: ReflinkMode) -> CopyResult<()> { +fn copy_on_write_linux( + source: &Path, + dest: &Path, + mode: ReflinkMode, + context: &str, +) -> CopyResult<()> { debug_assert!(mode != ReflinkMode::Never); - let src_file = File::open(source).context(&*context_for(source, dest))?; + let src_file = File::open(source).context(context)?; let dst_file = OpenOptions::new() .write(true) .truncate(false) .create(true) .open(dest) - .context(&*context_for(source, dest))?; + .context(context)?; match mode { ReflinkMode::Always => unsafe { let result = ficlone(dst_file.as_raw_fd(), src_file.as_raw_fd() as *const i32); @@ -1334,7 +1390,7 @@ fn copy_on_write_linux(source: &Path, dest: &Path, mode: ReflinkMode) -> CopyRes ReflinkMode::Auto => unsafe { let result = ficlone(dst_file.as_raw_fd(), src_file.as_raw_fd() as *const i32); if result != 0 { - fs::copy(source, dest).context(&*context_for(source, dest))?; + fs::copy(source, dest).context(context)?; } Ok(()) }, @@ -1344,7 +1400,12 @@ fn copy_on_write_linux(source: &Path, dest: &Path, mode: ReflinkMode) -> CopyRes /// Copies `source` to `dest` using copy-on-write if possible. #[cfg(target_os = "macos")] -fn copy_on_write_macos(source: &Path, dest: &Path, mode: ReflinkMode) -> CopyResult<()> { +fn copy_on_write_macos( + source: &Path, + dest: &Path, + mode: ReflinkMode, + context: &str, +) -> CopyResult<()> { debug_assert!(mode != ReflinkMode::Never); // Extract paths in a form suitable to be passed to a syscall. @@ -1389,7 +1450,7 @@ fn copy_on_write_macos(source: &Path, dest: &Path, mode: ReflinkMode) -> CopyRes format!("failed to clone {:?} from {:?}: {}", source, dest, error).into(), ) } - ReflinkMode::Auto => fs::copy(source, dest).context(&*context_for(source, dest))?, + ReflinkMode::Auto => fs::copy(source, dest).context(context)?, ReflinkMode::Never => unreachable!(), }; } @@ -1401,11 +1462,11 @@ fn copy_on_write_macos(source: &Path, dest: &Path, mode: ReflinkMode) -> CopyRes pub fn verify_target_type(target: &Path, target_type: &TargetType) -> CopyResult<()> { match (target_type, target.is_dir()) { (&TargetType::Directory, false) => { - Err(format!("target: '{}' is not a directory", target.display()).into()) + Err(format!("target: {} is not a directory", target.quote()).into()) } (&TargetType::File, true) => Err(format!( - "cannot overwrite directory '{}' with non-directory", - target.display() + "cannot overwrite directory {} with non-directory", + target.quote() ) .into()), _ => Ok(()), @@ -1430,8 +1491,8 @@ pub fn localize_to_target(root: &Path, source: &Path, target: &Path) -> CopyResu pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::Result { // We have to take symlinks and relative paths into account. - let pathbuf1 = canonicalize(p1, CanonicalizeMode::Normal)?; - let pathbuf2 = canonicalize(p2, CanonicalizeMode::Normal)?; + let pathbuf1 = canonicalize(p1, MissingHandling::Normal, ResolveMode::Logical)?; + let pathbuf2 = canonicalize(p2, MissingHandling::Normal, ResolveMode::Logical)?; Ok(pathbuf1 == pathbuf2) } diff --git a/src/uu/csplit/Cargo.toml b/src/uu/csplit/Cargo.toml index 2ddc3b89c..b76e942ee 100644 --- a/src/uu/csplit/Cargo.toml +++ b/src/uu/csplit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_csplit" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "csplit ~ (uutils) Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output" @@ -18,10 +18,13 @@ path = "src/csplit.rs" clap = { version = "2.33", features = ["wrap_help"] } thiserror = "1.0" regex = "1.0.0" -glob = "0.2.11" -uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["entries", "fs"] } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore", features=["entries", "fs"] } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [[bin]] name = "csplit" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 048ec80d8..0d99154df 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -10,6 +10,7 @@ use std::{ fs::{remove_file, File}, io::{BufRead, BufWriter, Write}, }; +use uucore::display::Quotable; mod csplit_error; mod patterns; @@ -34,8 +35,11 @@ mod options { pub const PATTERN: &str = "pattern"; } -fn get_usage() -> String { - format!("{0} [OPTION]... FILE PATTERN...", executable!()) +fn usage() -> String { + format!( + "{0} [OPTION]... FILE PATTERN...", + uucore::execution_phrase() + ) } /// Command line options for csplit. @@ -316,18 +320,19 @@ impl<'a> SplitWriter<'a> { let l = line?; match n.cmp(&(&ln + 1)) { Ordering::Less => { - if input_iter.add_line_to_buffer(ln, l).is_some() { - panic!("the buffer is big enough to contain 1 line"); - } + assert!( + input_iter.add_line_to_buffer(ln, l).is_none(), + "the buffer is big enough to contain 1 line" + ); ret = Ok(()); break; } Ordering::Equal => { - if !self.options.suppress_matched - && input_iter.add_line_to_buffer(ln, l).is_some() - { - panic!("the buffer is big enough to contain 1 line"); - } + assert!( + self.options.suppress_matched + || input_iter.add_line_to_buffer(ln, l).is_none(), + "the buffer is big enough to contain 1 line" + ); ret = Ok(()); break; } @@ -374,9 +379,10 @@ impl<'a> SplitWriter<'a> { match (self.options.suppress_matched, offset) { // no offset, add the line to the next split (false, 0) => { - if input_iter.add_line_to_buffer(ln, l).is_some() { - panic!("the buffer is big enough to contain 1 line"); - } + assert!( + input_iter.add_line_to_buffer(ln, l).is_none(), + "the buffer is big enough to contain 1 line" + ); } // a positive offset, some more lines need to be added to the current split (false, _) => self.writeln(l)?, @@ -421,9 +427,10 @@ impl<'a> SplitWriter<'a> { if !self.options.suppress_matched { // add 1 to the buffer size to make place for the matched line input_iter.set_size_of_buffer(offset_usize + 1); - if input_iter.add_line_to_buffer(ln, l).is_some() { - panic!("should be big enough to hold every lines"); - } + assert!( + input_iter.add_line_to_buffer(ln, l).is_none(), + "should be big enough to hold every lines" + ); } self.finish_split(); if input_iter.buffer_len() < offset_usize { @@ -565,7 +572,7 @@ mod tests { assert_eq!(input_splitter.add_line_to_buffer(0, line), None); assert_eq!(input_splitter.buffer_len(), 1); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -574,7 +581,7 @@ mod tests { assert_eq!(input_splitter.add_line_to_buffer(1, line), None); assert_eq!(input_splitter.buffer_len(), 2); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -586,7 +593,7 @@ mod tests { ); assert_eq!(input_splitter.buffer_len(), 2); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; input_splitter.rewind_buffer(); @@ -596,7 +603,7 @@ mod tests { assert_eq!(line, String::from("bbb")); assert_eq!(input_splitter.buffer_len(), 1); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -604,7 +611,7 @@ mod tests { assert_eq!(line, String::from("ccc")); assert_eq!(input_splitter.buffer_len(), 0); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -612,7 +619,7 @@ mod tests { assert_eq!(line, String::from("ddd")); assert_eq!(input_splitter.buffer_len(), 0); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; assert!(input_splitter.next().is_none()); @@ -637,7 +644,7 @@ mod tests { assert_eq!(input_splitter.add_line_to_buffer(0, line), None); assert_eq!(input_splitter.buffer_len(), 1); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -646,7 +653,7 @@ mod tests { assert_eq!(input_splitter.add_line_to_buffer(1, line), None); assert_eq!(input_splitter.buffer_len(), 2); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -655,7 +662,7 @@ mod tests { assert_eq!(input_splitter.add_line_to_buffer(2, line), None); assert_eq!(input_splitter.buffer_len(), 3); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; input_splitter.rewind_buffer(); @@ -666,7 +673,7 @@ mod tests { assert_eq!(input_splitter.add_line_to_buffer(0, line), None); assert_eq!(input_splitter.buffer_len(), 3); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -674,7 +681,7 @@ mod tests { assert_eq!(line, String::from("aaa")); assert_eq!(input_splitter.buffer_len(), 2); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -682,7 +689,7 @@ mod tests { assert_eq!(line, String::from("bbb")); assert_eq!(input_splitter.buffer_len(), 1); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -690,7 +697,7 @@ mod tests { assert_eq!(line, String::from("ccc")); assert_eq!(input_splitter.buffer_len(), 0); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; match input_splitter.next() { @@ -698,7 +705,7 @@ mod tests { assert_eq!(line, String::from("ddd")); assert_eq!(input_splitter.buffer_len(), 0); } - item @ _ => panic!("wrong item: {:?}", item), + item => panic!("wrong item: {:?}", item), }; assert!(input_splitter.next().is_none()); @@ -706,7 +713,7 @@ mod tests { } pub fn uumain(args: impl uucore::Args) -> i32 { - let usage = get_usage(); + let usage = usage(); let args = args .collect_str(InvalidEncodingHandling::Ignore) .accept_any(); @@ -722,16 +729,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .unwrap() .map(str::to_string) .collect(); - let patterns = return_if_err!(1, patterns::get_patterns(&patterns[..])); + let patterns = crash_if_err!(1, patterns::get_patterns(&patterns[..])); let options = CsplitOptions::new(&matches); if file_name == "-" { let stdin = io::stdin(); crash_if_err!(1, csplit(&options, patterns, stdin.lock())); } else { - let file = return_if_err!(1, File::open(file_name)); - let file_metadata = return_if_err!(1, file.metadata()); + let file = crash_if_err!(1, File::open(file_name)); + let file_metadata = crash_if_err!(1, file.metadata()); if !file_metadata.is_file() { - crash!(1, "'{}' is not a regular file", file_name); + crash!(1, "{} is not a regular file", file_name.quote()); } crash_if_err!(1, csplit(&options, patterns, BufReader::new(file))); }; @@ -739,7 +746,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(SUMMARY) .arg( diff --git a/src/uu/csplit/src/csplit_error.rs b/src/uu/csplit/src/csplit_error.rs index 637cf8890..1d4823ee2 100644 --- a/src/uu/csplit/src/csplit_error.rs +++ b/src/uu/csplit/src/csplit_error.rs @@ -1,26 +1,28 @@ use std::io; use thiserror::Error; +use uucore::display::Quotable; + /// Errors thrown by the csplit command #[derive(Debug, Error)] pub enum CsplitError { #[error("IO error: {}", _0)] IoError(io::Error), - #[error("'{}': line number out of range", _0)] + #[error("{}: line number out of range", ._0.quote())] LineOutOfRange(String), - #[error("'{}': line number out of range on repetition {}", _0, _1)] + #[error("{}: line number out of range on repetition {}", ._0.quote(), _1)] LineOutOfRangeOnRepetition(String, usize), - #[error("'{}': match not found", _0)] + #[error("{}: match not found", ._0.quote())] MatchNotFound(String), - #[error("'{}': match not found on repetition {}", _0, _1)] + #[error("{}: match not found on repetition {}", ._0.quote(), _1)] MatchNotFoundOnRepetition(String, usize), #[error("line number must be greater than zero")] LineNumberIsZero, #[error("line number '{}' is smaller than preceding line number, {}", _0, _1)] LineNumberSmallerThanPrevious(usize, usize), - #[error("invalid pattern: {}", _0)] + #[error("{}: invalid pattern", ._0.quote())] InvalidPattern(String), - #[error("invalid number: '{}'", _0)] + #[error("invalid number: {}", ._0.quote())] InvalidNumber(String), #[error("incorrect conversion specification in suffix")] SuffixFormatIncorrect, diff --git a/src/uu/csplit/src/split_name.rs b/src/uu/csplit/src/split_name.rs index 758216414..44ea2a5af 100644 --- a/src/uu/csplit/src/split_name.rs +++ b/src/uu/csplit/src/split_name.rs @@ -47,7 +47,7 @@ impl SplitName { }), Some(custom) => { let spec = - Regex::new(r"(?P%(?P[0#-])(?P\d+)?(?P[diuoxX]))") + Regex::new(r"(?P%((?P[0#-])(?P\d+)?)?(?P[diuoxX]))") .unwrap(); let mut captures_iter = spec.captures_iter(&custom); let custom_fn: Box String> = match captures_iter.next() { @@ -60,6 +60,21 @@ impl SplitName { Some(m) => m.as_str().parse::().unwrap(), }; match (captures.name("FLAG"), captures.name("TYPE")) { + (None, Some(ref t)) => match t.as_str() { + "d" | "i" | "u" => Box::new(move |n: usize| -> String { + format!("{}{}{}{}", prefix, before, n, after) + }), + "o" => Box::new(move |n: usize| -> String { + format!("{}{}{:o}{}", prefix, before, n, after) + }), + "x" => Box::new(move |n: usize| -> String { + format!("{}{}{:x}{}", prefix, before, n, after) + }), + "X" => Box::new(move |n: usize| -> String { + format!("{}{}{:X}{}", prefix, before, n, after) + }), + _ => return Err(CsplitError::SuffixFormatIncorrect), + }, (Some(ref f), Some(ref t)) => { match (f.as_str(), t.as_str()) { /* @@ -276,6 +291,12 @@ mod tests { assert_eq!(split_name.get(2), "xx00002"); } + #[test] + fn no_padding_decimal() { + let split_name = SplitName::new(None, Some(String::from("cst-%d-")), None).unwrap(); + assert_eq!(split_name.get(2), "xxcst-2-"); + } + #[test] fn zero_padding_decimal1() { let split_name = SplitName::new(None, Some(String::from("cst-%03d-")), None).unwrap(); diff --git a/src/uu/cut/Cargo.toml b/src/uu/cut/Cargo.toml index 6f92b39d1..991e3c449 100644 --- a/src/uu/cut/Cargo.toml +++ b/src/uu/cut/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cut" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "cut ~ (uutils) display byte/field columns of input lines" @@ -16,8 +16,8 @@ path = "src/cut.rs" [dependencies] clap = { version = "2.33", features = ["wrap_help"] } -uucore = { version=">=0.0.9", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore" } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } memchr = "2" bstr = "0.2" atty = "0.2" @@ -25,3 +25,7 @@ atty = "0.2" [[bin]] name = "cut" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/cut/src/cut.rs b/src/uu/cut/src/cut.rs index e33b8a2fe..35d92b83f 100644 --- a/src/uu/cut/src/cut.rs +++ b/src/uu/cut/src/cut.rs @@ -15,6 +15,7 @@ use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write}; use std::path::Path; +use uucore::display::Quotable; use self::searcher::Searcher; use uucore::ranges::Range; @@ -351,19 +352,19 @@ fn cut_files(mut filenames: Vec, mode: Mode) -> i32 { let path = Path::new(&filename[..]); if path.is_dir() { - show_error!("{}: Is a directory", filename); + show_error!("{}: Is a directory", filename.maybe_quote()); continue; } if path.metadata().is_err() { - show_error!("{}: No such file or directory", filename); + show_error!("{}: No such file or directory", filename.maybe_quote()); continue; } let file = match File::open(&path) { Ok(f) => f, Err(e) => { - show_error!("opening '{}': {}", &filename[..], e); + show_error!("opening {}: {}", filename.quote(), e); continue; } }; @@ -548,7 +549,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .name(NAME) .version(crate_version!()) .usage(SYNTAX) diff --git a/src/uu/date/Cargo.toml b/src/uu/date/Cargo.toml index c144d0d81..b1077fbe1 100644 --- a/src/uu/date/Cargo.toml +++ b/src/uu/date/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_date" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "date ~ (uutils) display or set the current time" @@ -17,8 +17,8 @@ path = "src/date.rs" [dependencies] chrono = "0.4.4" clap = { version = "2.33", features = ["wrap_help"] } -uucore = { version=">=0.0.9", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" } +uucore = { version=">=0.0.10", package="uucore", path="../../uucore" } +uucore_procs = { version=">=0.0.7", package="uucore_procs", path="../../uucore_procs" } [target.'cfg(unix)'.dependencies] libc = "0.2" @@ -29,3 +29,7 @@ winapi = { version = "0.3", features = ["minwinbase", "sysinfoapi", "minwindef"] [[bin]] name = "date" path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index 0071b5e8c..adcf77024 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -8,18 +8,17 @@ // spell-checker:ignore (chrono) Datelike Timelike ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes -#[macro_use] -extern crate uucore; - use chrono::{DateTime, FixedOffset, Local, Offset, Utc}; #[cfg(windows)] use chrono::{Datelike, Timelike}; use clap::{crate_version, App, Arg}; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] use libc::{clock_settime, timespec, CLOCK_REALTIME}; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::PathBuf; +use uucore::display::Quotable; +use uucore::show_error; #[cfg(windows)] use winapi::{ shared::minwindef::WORD, @@ -67,10 +66,12 @@ static RFC_3339_HELP_STRING: &str = "output date/time in RFC 3339 format. for date and time to the indicated precision. Example: 2006-08-14 02:34:56-06:00"; -#[cfg(not(target_os = "macos"))] +#[cfg(not(any(target_os = "macos", target_os = "redox")))] static OPT_SET_HELP_STRING: &str = "set time described by STRING"; #[cfg(target_os = "macos")] static OPT_SET_HELP_STRING: &str = "set time described by STRING (not available on mac yet)"; +#[cfg(target_os = "redox")] +static OPT_SET_HELP_STRING: &str = "set time described by STRING (not available on redox yet)"; /// Settings for this program, parsed from the command line struct Settings { @@ -146,7 +147,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let format = if let Some(form) = matches.value_of(OPT_FORMAT) { if !form.starts_with('+') { - eprintln!("date: invalid date '{}'", form); + show_error!("invalid date {}", form.quote()); return 1; } let form = form[1..].to_string(); @@ -175,7 +176,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let set_to = match matches.value_of(OPT_SET).map(parse_date) { None => None, Some(Err((input, _err))) => { - eprintln!("date: invalid date '{}'", input); + show_error!("invalid date {}", input.quote()); return 1; } Some(Ok(date)) => Some(date), @@ -241,7 +242,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { println!("{}", formatted); } Err((input, _err)) => { - println!("date: invalid date '{}'", input); + show_error!("invalid date {}", input.quote()); } } } @@ -251,7 +252,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } pub fn uu_app() -> App<'static, 'static> { - App::new(executable!()) + App::new(uucore::util_name()) .version(crate_version!()) .about(ABOUT) .arg( @@ -353,11 +354,17 @@ fn set_system_datetime(_date: DateTime) -> i32 { #[cfg(target_os = "macos")] fn set_system_datetime(_date: DateTime) -> i32 { - eprintln!("date: setting the date is not supported by macOS"); + show_error!("setting the date is not supported by macOS"); 1 } -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(target_os = "redox")] +fn set_system_datetime(_date: DateTime) -> i32 { + show_error!("setting the date is not supported by Redox"); + 1 +} + +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] /// System call to set date (unix). /// See here for more: /// https://doc.rust-lang.org/libc/i686-unknown-linux-gnu/libc/fn.clock_settime.html @@ -373,7 +380,7 @@ fn set_system_datetime(date: DateTime) -> i32 { if result != 0 { let error = std::io::Error::last_os_error(); - eprintln!("date: cannot set date: {}", error); + show_error!("cannot set date: {}", error); error.raw_os_error().unwrap() } else { 0 @@ -403,7 +410,7 @@ fn set_system_datetime(date: DateTime) -> i32 { if result == 0 { let error = std::io::Error::last_os_error(); - eprintln!("date: cannot set date: {}", error); + show_error!("cannot set date: {}", error); error.raw_os_error().unwrap() } else { 0 diff --git a/src/uu/dd/Cargo.toml b/src/uu/dd/Cargo.toml new file mode 100644 index 000000000..e26c141cb --- /dev/null +++ b/src/uu/dd/Cargo.toml @@ -0,0 +1,37 @@ +[package] +name = "uu_dd" +version = "0.0.8" +authors = ["uutils developers"] +license = "MIT" +description = "dd ~ (uutils) copy and convert files" + +homepage = "https://github.com/uutils/coreutils" +repository = "https://github.com/uutils/coreutils/tree/master/src/uu/dd" +keywords = ["coreutils", "uutils", "cross-platform", "cli", "utility"] +categories = ["command-line-utilities"] +edition = "2018" + +[lib] +path = "src/dd.rs" + +[dependencies] +byte-unit = "4.0" +clap = { version = "2.33", features = [ "wrap_help" ] } +gcd = "2.0" +libc = "0.2" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } +uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } + +[dev-dependencies] +tempfile = "^3" + +[target.'cfg(target_os = "linux")'.dependencies] +signal-hook = "0.3.9" + +[[bin]] +name = "dd" +path = "src/main.rs" + +[package.metadata.cargo-udeps.ignore] +# Necessary for "make all" +normal = ["uucore_procs"] diff --git a/src/uu/dd/src/conversion_tables.rs b/src/uu/dd/src/conversion_tables.rs new file mode 100644 index 000000000..aca2ef9bc --- /dev/null +++ b/src/uu/dd/src/conversion_tables.rs @@ -0,0 +1,221 @@ +// This file is part of the uutils coreutils package. +// +// (c) Tyler Steele +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +// Note: Conversion tables are just lookup tables. +// eg. The ASCII->EBCDIC table stores the EBCDIC code at the index +// obtained by treating the ASCII representation as a number. + +pub type ConversionTable = [u8; 256]; + +pub const ASCII_UCASE_TO_LCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const ASCII_LCASE_TO_UCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const ASCII_TO_EBCDIC: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x9a, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0x5f, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x6a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0x4a, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xa1, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const ASCII_TO_EBCDIC_UCASE_TO_LCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xad, 0xe0, 0xbd, 0x9a, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0x5f, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x6a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0x4a, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xa1, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const ASCII_TO_EBCDIC_LCASE_TO_UCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x9a, 0x6d, + 0x79, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xc0, 0x4f, 0xd0, 0x5f, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x6a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0x4a, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xa1, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const ASCII_TO_IBM: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const ASCII_TO_IBM_UCASE_TO_LCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const ASCII_TO_IBM_LCASE_TO_UCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const EBCDIC_TO_ASCII: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x85, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x0a, 0x17, 0x1b, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xd5, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x7e, + 0x2d, 0x2f, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xcb, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xc3, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, + 0xca, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x5e, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, + 0xd1, 0xe5, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0xd2, 0xd3, 0xd4, 0x5b, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0x5d, 0xe6, 0xe7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, + 0x5c, 0x9f, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const EBCDIC_TO_ASCII_UCASE_TO_LCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; + +pub const EBCDIC_TO_ASCII_LCASE_TO_UCASE: ConversionTable = [ + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]; diff --git a/src/uu/dd/src/datastructures.rs b/src/uu/dd/src/datastructures.rs new file mode 100644 index 000000000..b4410d210 --- /dev/null +++ b/src/uu/dd/src/datastructures.rs @@ -0,0 +1,172 @@ +// This file is part of the uutils coreutils package. +// +// (c) Tyler Steele +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. +// spell-checker:ignore ctable, outfile + +use crate::conversion_tables::*; + +use std::error::Error; +use std::time; + +pub struct ProgUpdate { + pub read_stat: ReadStat, + pub write_stat: WriteStat, + pub duration: time::Duration, +} + +#[derive(Clone, Copy, Default)] +pub struct ReadStat { + pub reads_complete: u64, + pub reads_partial: u64, + pub records_truncated: u32, +} +impl std::ops::AddAssign for ReadStat { + fn add_assign(&mut self, other: Self) { + *self = Self { + reads_complete: self.reads_complete + other.reads_complete, + reads_partial: self.reads_partial + other.reads_partial, + records_truncated: self.records_truncated + other.records_truncated, + } + } +} + +#[derive(Clone, Copy)] +pub struct WriteStat { + pub writes_complete: u64, + pub writes_partial: u64, + pub bytes_total: u128, +} +impl std::ops::AddAssign for WriteStat { + fn add_assign(&mut self, other: Self) { + *self = Self { + writes_complete: self.writes_complete + other.writes_complete, + writes_partial: self.writes_partial + other.writes_partial, + bytes_total: self.bytes_total + other.bytes_total, + } + } +} + +type Cbs = usize; + +/// Stores all Conv Flags that apply to the input +#[derive(Debug, Default, PartialEq)] +pub struct IConvFlags { + pub ctable: Option<&'static ConversionTable>, + pub block: Option, + pub unblock: Option, + pub swab: bool, + pub sync: Option, + pub noerror: bool, +} + +/// Stores all Conv Flags that apply to the output +#[derive(Debug, Default, PartialEq)] +pub struct OConvFlags { + pub sparse: bool, + pub excl: bool, + pub nocreat: bool, + pub notrunc: bool, + pub fdatasync: bool, + pub fsync: bool, +} + +/// Stores all Flags that apply to the input +#[derive(Debug, Default, PartialEq)] +pub struct IFlags { + pub cio: bool, + pub direct: bool, + pub directory: bool, + pub dsync: bool, + pub sync: bool, + pub nocache: bool, + pub nonblock: bool, + pub noatime: bool, + pub noctty: bool, + pub nofollow: bool, + pub nolinks: bool, + pub binary: bool, + pub text: bool, + pub fullblock: bool, + pub count_bytes: bool, + pub skip_bytes: bool, +} + +/// Stores all Flags that apply to the output +#[derive(Debug, Default, PartialEq)] +pub struct OFlags { + pub append: bool, + pub cio: bool, + pub direct: bool, + pub directory: bool, + pub dsync: bool, + pub sync: bool, + pub nocache: bool, + pub nonblock: bool, + pub noatime: bool, + pub noctty: bool, + pub nofollow: bool, + pub nolinks: bool, + pub binary: bool, + pub text: bool, + pub seek_bytes: bool, +} + +/// The value of the status cl-option. +/// Controls printing of transfer stats +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum StatusLevel { + Progress, + Noxfer, + None, +} + +/// The value of count=N +/// Defaults to Reads(N) +/// if iflag=count_bytes +/// then becomes Bytes(N) +#[derive(Debug, PartialEq)] +pub enum CountType { + Reads(usize), + Bytes(usize), +} + +#[derive(Debug)] +pub enum InternalError { + WrongInputType, + WrongOutputType, + InvalidConvBlockUnblockCase, +} + +impl std::fmt::Display for InternalError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::WrongInputType | Self::WrongOutputType => { + write!(f, "Internal dd error: Wrong Input/Output data type") + } + Self::InvalidConvBlockUnblockCase => { + write!(f, "Invalid Conversion, Block, or Unblock data") + } + } + } +} + +impl Error for InternalError {} + +pub mod options { + pub const INFILE: &str = "if"; + pub const OUTFILE: &str = "of"; + pub const IBS: &str = "ibs"; + pub const OBS: &str = "obs"; + pub const BS: &str = "bs"; + pub const CBS: &str = "cbs"; + pub const COUNT: &str = "count"; + pub const SKIP: &str = "skip"; + pub const SEEK: &str = "seek"; + pub const STATUS: &str = "status"; + pub const CONV: &str = "conv"; + pub const IFLAG: &str = "iflag"; + pub const OFLAG: &str = "oflag"; +} diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs new file mode 100644 index 000000000..9f1d28714 --- /dev/null +++ b/src/uu/dd/src/dd.rs @@ -0,0 +1,1144 @@ +// This file is part of the uutils coreutils package. +// +// (c) Tyler Steele +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +// spell-checker:ignore fname, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, btotal, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, outfile, parseargs, rlen, rmax, rposition, rremain, rsofar, rstat, sigusr, sigval, wlen, wstat + +use uucore::InvalidEncodingHandling; + +#[cfg(test)] +mod dd_unit_tests; + +mod datastructures; +use datastructures::*; + +mod parseargs; +use parseargs::Matches; + +mod conversion_tables; +use conversion_tables::*; + +use byte_unit::Byte; +use clap::{self, crate_version}; +use gcd::Gcd; +#[cfg(target_os = "linux")] +use signal_hook::consts::signal; +use std::cmp; +use std::convert::TryInto; +use std::env; +use std::error::Error; +use std::fs::{File, OpenOptions}; +use std::io::{self, Read, Seek, Write}; +#[cfg(target_os = "linux")] +use std::os::unix::fs::OpenOptionsExt; +use std::path::Path; +use std::sync::mpsc; +#[cfg(target_os = "linux")] +use std::sync::{atomic::AtomicUsize, atomic::Ordering, Arc}; +use std::thread; +use std::time; + +const ABOUT: &str = "copy, and optionally convert, a file system resource"; +const BUF_INIT_BYTE: u8 = 0xDD; +const RTN_SUCCESS: i32 = 0; +const RTN_FAILURE: i32 = 1; +const NEWLINE: u8 = b'\n'; +const SPACE: u8 = b' '; + +struct Input { + src: R, + non_ascii: bool, + ibs: usize, + print_level: Option, + count: Option, + cflags: IConvFlags, + iflags: IFlags, +} + +impl Input { + fn new(matches: &Matches) -> Result> { + let ibs = parseargs::parse_ibs(matches)?; + let non_ascii = parseargs::parse_input_non_ascii(matches)?; + let print_level = parseargs::parse_status_level(matches)?; + let cflags = parseargs::parse_conv_flag_input(matches)?; + let iflags = parseargs::parse_iflags(matches)?; + let skip = parseargs::parse_skip_amt(&ibs, &iflags, matches)?; + let count = parseargs::parse_count(&iflags, matches)?; + + let mut i = Input { + src: io::stdin(), + non_ascii, + ibs, + print_level, + count, + cflags, + iflags, + }; + + if let Some(amt) = skip { + let mut buf = vec![BUF_INIT_BYTE; amt]; + + i.force_fill(&mut buf, amt)?; + } + + Ok(i) + } +} + +#[cfg(target_os = "linux")] +fn make_linux_iflags(iflags: &IFlags) -> Option { + let mut flag = 0; + + if iflags.direct { + flag |= libc::O_DIRECT; + } + if iflags.directory { + flag |= libc::O_DIRECTORY; + } + if iflags.dsync { + flag |= libc::O_DSYNC; + } + if iflags.noatime { + flag |= libc::O_NOATIME; + } + if iflags.noctty { + flag |= libc::O_NOCTTY; + } + if iflags.nofollow { + flag |= libc::O_NOFOLLOW; + } + if iflags.nonblock { + flag |= libc::O_NONBLOCK; + } + if iflags.sync { + flag |= libc::O_SYNC; + } + + if flag != 0 { + Some(flag) + } else { + None + } +} + +impl Input { + fn new(matches: &Matches) -> Result> { + let ibs = parseargs::parse_ibs(matches)?; + let non_ascii = parseargs::parse_input_non_ascii(matches)?; + let print_level = parseargs::parse_status_level(matches)?; + let cflags = parseargs::parse_conv_flag_input(matches)?; + let iflags = parseargs::parse_iflags(matches)?; + let skip = parseargs::parse_skip_amt(&ibs, &iflags, matches)?; + let count = parseargs::parse_count(&iflags, matches)?; + + if let Some(fname) = matches.value_of(options::INFILE) { + let mut src = { + let mut opts = OpenOptions::new(); + opts.read(true); + + #[cfg(target_os = "linux")] + if let Some(libc_flags) = make_linux_iflags(&iflags) { + opts.custom_flags(libc_flags); + } + + opts.open(fname)? + }; + + if let Some(amt) = skip { + let amt: u64 = amt.try_into()?; + src.seek(io::SeekFrom::Start(amt))?; + } + + let i = Input { + src, + non_ascii, + ibs, + print_level, + count, + cflags, + iflags, + }; + + Ok(i) + } else { + Err(Box::new(InternalError::WrongInputType)) + } + } +} + +impl Read for Input { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let mut base_idx = 0; + let target_len = buf.len(); + loop { + match self.src.read(&mut buf[base_idx..]) { + Ok(0) => return Ok(base_idx), + Ok(rlen) if self.iflags.fullblock => { + base_idx += rlen; + + if base_idx >= target_len { + return Ok(target_len); + } + } + Ok(len) => return Ok(len), + Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, + Err(_) if self.cflags.noerror => return Ok(base_idx), + Err(e) => return Err(e), + } + } + } +} + +impl Input { + /// Fills a given buffer. + /// Reads in increments of 'self.ibs'. + /// The start of each ibs-sized read follows the previous one. + fn fill_consecutive(&mut self, buf: &mut Vec) -> Result> { + let mut reads_complete = 0; + let mut reads_partial = 0; + let mut bytes_total = 0; + + for chunk in buf.chunks_mut(self.ibs) { + match self.read(chunk)? { + rlen if rlen == self.ibs => { + bytes_total += rlen; + reads_complete += 1; + } + rlen if rlen > 0 => { + bytes_total += rlen; + reads_partial += 1; + } + _ => break, + } + } + + buf.truncate(bytes_total); + Ok(ReadStat { + reads_complete, + reads_partial, + // Records are not truncated when filling. + records_truncated: 0, + }) + } + + /// Fills a given buffer. + /// Reads in increments of 'self.ibs'. + /// The start of each ibs-sized read is aligned to multiples of ibs; remaining space is filled with the 'pad' byte. + fn fill_blocks(&mut self, buf: &mut Vec, pad: u8) -> Result> { + let mut reads_complete = 0; + let mut reads_partial = 0; + let mut base_idx = 0; + + while base_idx < buf.len() { + let next_blk = cmp::min(base_idx + self.ibs, buf.len()); + let target_len = next_blk - base_idx; + + match self.read(&mut buf[base_idx..next_blk])? { + 0 => break, + rlen if rlen < target_len => { + reads_partial += 1; + let padding = vec![pad; target_len - rlen]; + buf.splice(base_idx + rlen..next_blk, padding.into_iter()); + } + _ => { + reads_complete += 1; + } + } + + base_idx += self.ibs; + } + + buf.truncate(base_idx); + Ok(ReadStat { + reads_complete, + reads_partial, + records_truncated: 0, + }) + } + + /// Force-fills a buffer, ignoring zero-length reads which would otherwise be + /// interpreted as EOF. + /// Note: This will not return unless the source (eventually) produces + /// enough bytes to meet target_len. + fn force_fill(&mut self, buf: &mut [u8], target_len: usize) -> Result> { + let mut base_idx = 0; + while base_idx < target_len { + base_idx += self.read(&mut buf[base_idx..target_len])?; + } + + Ok(base_idx) + } +} + +trait OutputTrait: Sized + Write { + fn new(matches: &Matches) -> Result>; + fn fsync(&mut self) -> io::Result<()>; + fn fdatasync(&mut self) -> io::Result<()>; +} + +struct Output { + dst: W, + obs: usize, + cflags: OConvFlags, +} + +impl OutputTrait for Output { + fn new(matches: &Matches) -> Result> { + let obs = parseargs::parse_obs(matches)?; + let cflags = parseargs::parse_conv_flag_output(matches)?; + + let dst = io::stdout(); + + Ok(Output { dst, obs, cflags }) + } + + fn fsync(&mut self) -> io::Result<()> { + self.dst.flush() + } + + fn fdatasync(&mut self) -> io::Result<()> { + self.dst.flush() + } +} + +impl Output +where + Self: OutputTrait, +{ + fn write_blocks(&mut self, buf: Vec) -> io::Result { + let mut writes_complete = 0; + let mut writes_partial = 0; + let mut bytes_total = 0; + + for chunk in buf.chunks(self.obs) { + match self.write(chunk)? { + wlen if wlen < chunk.len() => { + writes_partial += 1; + bytes_total += wlen; + } + wlen => { + writes_complete += 1; + bytes_total += wlen; + } + } + } + + Ok(WriteStat { + writes_complete, + writes_partial, + bytes_total: bytes_total.try_into().unwrap_or(0u128), + }) + } + + fn dd_out(mut self, mut i: Input) -> Result<(), Box> { + let mut rstat = ReadStat { + reads_complete: 0, + reads_partial: 0, + records_truncated: 0, + }; + let mut wstat = WriteStat { + writes_complete: 0, + writes_partial: 0, + bytes_total: 0, + }; + let start = time::Instant::now(); + let bsize = calc_bsize(i.ibs, self.obs); + + let prog_tx = { + let (tx, rx) = mpsc::channel(); + thread::spawn(gen_prog_updater(rx, i.print_level)); + tx + }; + + while below_count_limit(&i.count, &rstat, &wstat) { + // Read/Write + let loop_bsize = calc_loop_bsize(&i.count, &rstat, &wstat, i.ibs, bsize); + match read_helper(&mut i, loop_bsize)? { + ( + ReadStat { + reads_complete: 0, + reads_partial: 0, + .. + }, + _, + ) => break, + (rstat_update, buf) => { + let wstat_update = self.write_blocks(buf)?; + + rstat += rstat_update; + wstat += wstat_update; + } + }; + // Update Prog + prog_tx.send(ProgUpdate { + read_stat: rstat, + write_stat: wstat, + duration: start.elapsed(), + })?; + } + + if self.cflags.fsync { + self.fsync()?; + } else if self.cflags.fdatasync { + self.fdatasync()?; + } + + match i.print_level { + Some(StatusLevel::Noxfer) | Some(StatusLevel::None) => {} + _ => print_transfer_stats(&ProgUpdate { + read_stat: rstat, + write_stat: wstat, + duration: start.elapsed(), + }), + } + Ok(()) + } +} + +#[cfg(target_os = "linux")] +fn make_linux_oflags(oflags: &OFlags) -> Option { + let mut flag = 0; + + // oflag=FLAG + if oflags.append { + flag |= libc::O_APPEND; + } + if oflags.direct { + flag |= libc::O_DIRECT; + } + if oflags.directory { + flag |= libc::O_DIRECTORY; + } + if oflags.dsync { + flag |= libc::O_DSYNC; + } + if oflags.noatime { + flag |= libc::O_NOATIME; + } + if oflags.noctty { + flag |= libc::O_NOCTTY; + } + if oflags.nofollow { + flag |= libc::O_NOFOLLOW; + } + if oflags.nonblock { + flag |= libc::O_NONBLOCK; + } + if oflags.sync { + flag |= libc::O_SYNC; + } + + if flag != 0 { + Some(flag) + } else { + None + } +} + +impl OutputTrait for Output { + fn new(matches: &Matches) -> Result> { + fn open_dst(path: &Path, cflags: &OConvFlags, oflags: &OFlags) -> Result { + let mut opts = OpenOptions::new(); + opts.write(true) + .create(!cflags.nocreat) + .truncate(!cflags.notrunc) + .create_new(cflags.excl) + .append(oflags.append); + + #[cfg(target_os = "linux")] + if let Some(libc_flags) = make_linux_oflags(oflags) { + opts.custom_flags(libc_flags); + } + + opts.open(path) + } + let obs = parseargs::parse_obs(matches)?; + let cflags = parseargs::parse_conv_flag_output(matches)?; + let oflags = parseargs::parse_oflags(matches)?; + let seek = parseargs::parse_seek_amt(&obs, &oflags, matches)?; + + if let Some(fname) = matches.value_of(options::OUTFILE) { + let mut dst = open_dst(Path::new(&fname), &cflags, &oflags)?; + + if let Some(amt) = seek { + let amt: u64 = amt.try_into()?; + dst.seek(io::SeekFrom::Start(amt))?; + } + + Ok(Output { dst, obs, cflags }) + } else { + // The following error should only occur if someone + // mistakenly calls Output::::new() without checking + // if 'of' has been provided. In this case, + // Output::::new() is probably intended. + Err(Box::new(InternalError::WrongOutputType)) + } + } + + fn fsync(&mut self) -> io::Result<()> { + self.dst.flush()?; + self.dst.sync_all() + } + + fn fdatasync(&mut self) -> io::Result<()> { + self.dst.flush()?; + self.dst.sync_data() + } +} + +impl Seek for Output { + fn seek(&mut self, pos: io::SeekFrom) -> io::Result { + self.dst.seek(pos) + } +} + +impl Write for Output { + fn write(&mut self, buf: &[u8]) -> io::Result { + fn is_sparse(buf: &[u8]) -> bool { + buf.iter().all(|&e| e == 0u8) + } + // ----------------------------- + if self.cflags.sparse && is_sparse(buf) { + let seek_amt: i64 = buf + .len() + .try_into() + .expect("Internal dd Error: Seek amount greater than signed 64-bit integer"); + self.dst.seek(io::SeekFrom::Current(seek_amt))?; + Ok(buf.len()) + } else { + self.dst.write(buf) + } + } + + fn flush(&mut self) -> io::Result<()> { + self.dst.flush() + } +} + +impl Write for Output { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.dst.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.dst.flush() + } +} + +/// Splits the content of buf into cbs-length blocks +/// Appends padding as specified by conv=block and cbs=N +/// Expects ascii encoded data +fn block(buf: Vec, cbs: usize, rstat: &mut ReadStat) -> Vec> { + let mut blocks = buf + .split(|&e| e == NEWLINE) + .map(|split| split.to_vec()) + .fold(Vec::new(), |mut blocks, mut split| { + if split.len() > cbs { + rstat.records_truncated += 1; + } + split.resize(cbs, SPACE); + blocks.push(split); + + blocks + }); + + if let Some(last) = blocks.last() { + if last.iter().all(|&e| e == SPACE) { + blocks.pop(); + } + } + + blocks +} + +/// Trims padding from each cbs-length partition of buf +/// as specified by conv=unblock and cbs=N +/// Expects ascii encoded data +fn unblock(buf: Vec, cbs: usize) -> Vec { + buf.chunks(cbs).fold(Vec::new(), |mut acc, block| { + if let Some(last_char_idx) = block.iter().rposition(|&e| e != SPACE) { + // Include text up to last space. + acc.extend(&block[..=last_char_idx]); + } + + acc.push(NEWLINE); + acc + }) +} + +/// A helper for teasing out which options must be applied and in which order. +/// Some user options, such as the presence of conversion tables, will determine whether the input is assumed to be ascii. The parser sets the Input::non_ascii flag accordingly. +/// Examples: +/// - If conv=ebcdic or conv=ibm is specified then block, unblock or swab must be performed before the conversion happens since the source will start in ascii. +/// - If conv=ascii is specified then block, unblock or swab must be performed after the conversion since the source starts in ebcdic. +/// - If no conversion is specified then the source is assumed to be in ascii. +/// For more info see `info dd` +fn conv_block_unblock_helper( + mut buf: Vec, + i: &mut Input, + rstat: &mut ReadStat, +) -> Result, Box> { + // Local Predicate Fns ------------------------------------------------- + fn should_block_then_conv(i: &Input) -> bool { + !i.non_ascii && i.cflags.block.is_some() + } + fn should_conv_then_block(i: &Input) -> bool { + i.non_ascii && i.cflags.block.is_some() + } + fn should_unblock_then_conv(i: &Input) -> bool { + !i.non_ascii && i.cflags.unblock.is_some() + } + fn should_conv_then_unblock(i: &Input) -> bool { + i.non_ascii && i.cflags.unblock.is_some() + } + fn conv_only(i: &Input) -> bool { + i.cflags.ctable.is_some() && i.cflags.block.is_none() && i.cflags.unblock.is_none() + } + // Local Helper Fns ---------------------------------------------------- + fn apply_conversion(buf: &mut [u8], ct: &ConversionTable) { + for idx in 0..buf.len() { + buf[idx] = ct[buf[idx] as usize]; + } + } + // -------------------------------------------------------------------- + if conv_only(i) { + // no block/unblock + let ct = i.cflags.ctable.unwrap(); + apply_conversion(&mut buf, ct); + + Ok(buf) + } else if should_block_then_conv(i) { + // ascii input so perform the block first + let cbs = i.cflags.block.unwrap(); + + let mut blocks = block(buf, cbs, rstat); + + if let Some(ct) = i.cflags.ctable { + for buf in blocks.iter_mut() { + apply_conversion(buf, ct); + } + } + + let blocks = blocks.into_iter().flatten().collect(); + + Ok(blocks) + } else if should_conv_then_block(i) { + // Non-ascii so perform the conversion first + let cbs = i.cflags.block.unwrap(); + + if let Some(ct) = i.cflags.ctable { + apply_conversion(&mut buf, ct); + } + + let blocks = block(buf, cbs, rstat).into_iter().flatten().collect(); + + Ok(blocks) + } else if should_unblock_then_conv(i) { + // ascii input so perform the unblock first + let cbs = i.cflags.unblock.unwrap(); + + let mut buf = unblock(buf, cbs); + + if let Some(ct) = i.cflags.ctable { + apply_conversion(&mut buf, ct); + } + + Ok(buf) + } else if should_conv_then_unblock(i) { + // Non-ascii input so perform the conversion first + let cbs = i.cflags.unblock.unwrap(); + + if let Some(ct) = i.cflags.ctable { + apply_conversion(&mut buf, ct); + } + + let buf = unblock(buf, cbs); + + Ok(buf) + } else { + // The following error should not happen, as it results from + // insufficient command line data. This case should be caught + // by the parser before making it this far. + // Producing this error is an alternative to risking an unwrap call + // on 'cbs' if the required data is not provided. + Err(Box::new(InternalError::InvalidConvBlockUnblockCase)) + } +} + +/// Read helper performs read operations common to all dd reads, and dispatches the buffer to relevant helper functions as dictated by the operations requested by the user. +fn read_helper( + i: &mut Input, + bsize: usize, +) -> Result<(ReadStat, Vec), Box> { + // Local Predicate Fns ----------------------------------------------- + fn is_conv(i: &Input) -> bool { + i.cflags.ctable.is_some() + } + fn is_block(i: &Input) -> bool { + i.cflags.block.is_some() + } + fn is_unblock(i: &Input) -> bool { + i.cflags.unblock.is_some() + } + // Local Helper Fns ------------------------------------------------- + fn perform_swab(buf: &mut [u8]) { + for base in (1..buf.len()).step_by(2) { + buf.swap(base, base - 1); + } + } + // ------------------------------------------------------------------ + // Read + let mut buf = vec![BUF_INIT_BYTE; bsize]; + let mut rstat = match i.cflags.sync { + Some(ch) => i.fill_blocks(&mut buf, ch)?, + _ => i.fill_consecutive(&mut buf)?, + }; + // Return early if no data + if rstat.reads_complete == 0 && rstat.reads_partial == 0 { + return Ok((rstat, buf)); + } + + // Perform any conv=x[,x...] options + if i.cflags.swab { + perform_swab(&mut buf); + } + if is_conv(i) || is_block(i) || is_unblock(i) { + let buf = conv_block_unblock_helper(buf, i, &mut rstat)?; + Ok((rstat, buf)) + } else { + Ok((rstat, buf)) + } +} + +// Print io lines of a status update: +// + records in +// + records out +fn print_io_lines(update: &ProgUpdate) { + eprintln!( + "{}+{} records in", + update.read_stat.reads_complete, update.read_stat.reads_partial + ); + if update.read_stat.records_truncated > 0 { + eprintln!("{} truncated records", update.read_stat.records_truncated); + } + eprintln!( + "{}+{} records out", + update.write_stat.writes_complete, update.write_stat.writes_partial + ); +} +// Print the progress line of a status update: +// bytes (, ) copied,