diff --git a/.cirrus.yml b/.cirrus.yml index 3a34a933a..50f8a25b1 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -1,7 +1,14 @@ +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-1-release-amd64 + image: freebsd-12-2-release-amd64 setup_script: - pkg install -y curl gmake - curl https://sh.rustup.rs -sSf --output rustup.sh @@ -11,4 +18,4 @@ task: - cargo build test_script: - . $HOME/.cargo/env - - cargo test + - cargo test -p uucore -p coreutils diff --git a/.codespell.rc b/.codespell.rc new file mode 100644 index 000000000..914ca2951 --- /dev/null +++ b/.codespell.rc @@ -0,0 +1,3 @@ +[codespell] +ignore-words-list = crate +skip = ./.git/**,./.vscode/cspell.dictionaries/**,./target/**,./tests/fixtures/** diff --git a/.editorconfig b/.editorconfig index 95dfec676..d93fa7c0e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,22 +3,38 @@ # * top-most EditorConfig file root = true -# Unix-style newlines with a newline ending every file [*] +# default ~ utf-8, unix-style newlines with a newline ending every file, 4 space indentation charset = utf-8 end_of_line = lf indent_size = 4 indent_style = space insert_final_newline = true +max_line_length = 100 trim_trailing_whitespace = true -[*.{bat,cmd,[Bb][Aa][Tt],[Cc][Mm][Dd]}] -# DOS/Win requires BAT/CMD files to have CRLF EOLNs -end_of_line = crlf - -[[Mm]akefile{,.*}] -# TAB-style indentation +[[Mm]akefile{,.*}, *.{mk,[Mm][Kk]}] +# makefiles ~ TAB-style indentation indent_style = tab -[*.{yml,[Yy][Mm][Ll]}] +[*.{bat,cmd,[Bb][Aa][Tt],[Cc][Mm][Dd]}] +# BAT/CMD ~ DOS/Win requires BAT/CMD files to have CRLF EOLNs +end_of_line = crlf + +[*.go] +# go ~ TAB-style indentation (SPACE-style alignment); ref: @@ +indent_style = tab + +[*.{cjs,js,json,mjs,ts}] +# js/ts indent_size = 2 + +[*.{markdown,md,mkd,[Mm][Dd],[Mm][Kk][Dd],[Mm][Dd][Oo][Ww][Nn],[Mm][Kk][Dd][Oo][Ww][Nn],[Mm][Aa][Rr][Kk][Dd][Oo][Ww][Nn]}] +# markdown +indent_size = 2 +indent_style = space + +[*.{yaml,yml,[Yy][Mm][Ll],[Yy][Aa][Mm][Ll]}] +# YAML +indent_size = 2 +indent_style = space diff --git a/.github/stale.yml b/.github/stale.yml new file mode 100644 index 000000000..b614226dc --- /dev/null +++ b/.github/stale.yml @@ -0,0 +1,19 @@ +# spell-checker:ignore (labels) wontfix + # Number of days of inactivity before an issue/PR becomes stale +daysUntilStale: 365 +# Number of days of inactivity before a stale issue/PR is closed +daysUntilClose: 365 +# Issues with these labels will never be considered stale +exemptLabels: + - pinned + - security + - "Good first bug" +# Label to use when marking an issue as stale +staleLabel: wontfix +# Comment to post when marking an issue as stale. Set to `false` to disable +markComment: > + This issue has been automatically marked as stale because it has not had + recent activity. It will be closed if no further activity occurs. Thank you + for your contributions. +# Comment to post when closing a stale issue. Set to `false` to disable +closeComment: false diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 97369a8e7..b2d77a5a4 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -5,18 +5,54 @@ name: CICD # spell-checker:ignore (jargon) SHAs deps softprops toolchain # spell-checker:ignore (names) CodeCOV MacOS MinGW Peltoche rivy # spell-checker:ignore (shell/tools) choco clippy dmake dpkg esac fakeroot gmake grcov halium lcov libssl mkdir popd printf pushd rustc rustfmt rustup shopt xargs -# spell-checker:ignore (misc) aarch alnum armhf coreutils gnueabihf issuecomment maint nullglob onexitbegin onexitend tempfile uutils +# spell-checker:ignore (misc) aarch alnum armhf bindir busytest coreutils gnueabihf issuecomment maint nullglob onexitbegin onexitend runtest tempfile testsuite uutils + +# ToDO: [2021-06; rivy] change from `cargo-tree` to `cargo tree` once MSRV is >= 1.45 env: PROJECT_NAME: coreutils PROJECT_DESC: "Core universal (cross-platform) utilities" PROJECT_AUTH: "uutils" - RUST_MIN_SRV: "1.33.0" ## v1.33.0 - minimum version for tempfile 3.1.0 and libc needed for aarch64 + 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 on: [push, pull_request] jobs: + code_deps: + name: Style/dependencies + runs-on: ${{ matrix.job.os }} + strategy: + fail-fast: false + matrix: + job: + - { os: ubuntu-latest , features: feat_os_unix } + steps: + - uses: actions/checkout@v2 + - name: Initialize workflow variables + id: vars + shell: bash + run: | + ## VARs setup + 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 + outputs CARGO_FEATURES_OPTION + - name: Install `rust` toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + profile: minimal # minimal component installation (ie, no documentation) + - name: "`cargo update` testing" + shell: bash + run: | + ## `cargo update` testing + # * convert any warnings to GHA UI annotations; ref: + cargo fetch --locked --quiet || { echo "::error file=Cargo.lock::'Cargo.lock' file requires update (use \`cargo +${{ env.RUST_MIN_SRV }} update\`)" ; exit 1 ; } + code_format: name: Style/format runs-on: ${{ matrix.job.os }} @@ -26,18 +62,18 @@ jobs: job: - { os: ubuntu-latest , features: feat_os_unix } steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Initialize workflow variables id: vars shell: bash run: | ## VARs setup + 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 - echo set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} - echo ::set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} + outputs CARGO_FEATURES_OPTION - name: Install `rust` toolchain uses: actions-rs/toolchain@v1 with: @@ -48,18 +84,19 @@ jobs: - name: "`fmt` testing" shell: bash run: | - # `fmt` testing + ## `fmt` testing # * convert any warnings to GHA UI annotations; ref: - S=$(cargo fmt -- --check) && printf "%s\n" "$S" || { printf "%s\n" "$S" | sed -E -n -e "s/^Diff[[:space:]]+in[[:space:]]+${PWD//\//\\/}\/(.*)[[:space:]]+at[[:space:]]+[^0-9]+([0-9]+).*$/::warning file=\1,line=\2::WARNING: \`cargo fmt\`: style violation/p" ; } + S=$(cargo fmt -- --check) && printf "%s\n" "$S" || { printf "%s\n" "$S" ; printf "%s\n" "$S" | sed -E -n -e "s/^Diff[[:space:]]+in[[:space:]]+${PWD//\//\\/}\/(.*)[[:space:]]+at[[:space:]]+[^0-9]+([0-9]+).*$/::error file=\1,line=\2::ERROR: \`cargo fmt\`: style violation (file:'\1', line:\2; use \`cargo fmt \"\1\"\`)/p" ; exit 1 ; } - name: "`fmt` testing of tests" + if: success() || failure() # run regardless of prior step success/failure shell: bash run: | - # `fmt` testing of tests + ## `fmt` testing of tests # * convert any warnings to GHA UI annotations; ref: - S=$(find tests -name "*.rs" -print0 | xargs -0 cargo fmt -- --check) && printf "%s\n" "$S" || { printf "%s\n" "$S" | sed -E -n "s/^Diff[[:space:]]+in[[:space:]]+${PWD//\//\\/}\/(.*)[[:space:]]+at[[:space:]]+[^0-9]+([0-9]+).*$/::warning file=\1,line=\2::WARNING: \`cargo fmt\`: style violation/p" ; } + S=$(find tests -name "*.rs" -print0 | xargs -0 cargo fmt -- --check) && printf "%s\n" "$S" || { printf "%s\n" "$S" ; printf "%s\n" "$S" | sed -E -n "s/^Diff[[:space:]]+in[[:space:]]+${PWD//\//\\/}\/(.*)[[:space:]]+at[[:space:]]+[^0-9]+([0-9]+).*$/::error file=\1,line=\2::ERROR: \`cargo fmt\`: style violation (file:'\1', line:\2; use \`cargo fmt \"\1\"\`)/p" ; exit 1 ; } - code_warnings: - name: Style/warnings + code_lint: + name: Style/lint runs-on: ${{ matrix.job.os }} strategy: fail-fast: false @@ -69,32 +106,51 @@ jobs: - { os: macos-latest , features: feat_os_macos } - { os: windows-latest , features: feat_os_windows } steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Initialize workflow variables id: vars shell: bash run: | ## VARs setup + 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 - echo set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} - echo ::set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} + outputs CARGO_FEATURES_OPTION - name: Install `rust` toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: nightly default: true profile: minimal # minimal component installation (ie, no documentation) components: clippy - - name: "`clippy` testing" - if: success() || failure() # run regardless of prior step success/failure + - name: "`clippy` lint testing" shell: bash run: | - # `clippy` testing + ## `clippy` lint testing # * convert any warnings to GHA UI annotations; ref: - S=$(cargo clippy ${{ 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:]]+(.*):([0-9]+):([0-9]+).*$/::warning file=\2,line=\3,col=\4::WARNING: \`cargo clippy\`: \1/p;" -e '}' ; } + 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 ; } + + code_spellcheck: + name: Style/spelling + runs-on: ${{ matrix.job.os }} + strategy: + matrix: + job: + - { os: ubuntu-latest } + steps: + - uses: actions/checkout@v2 + - name: Install/setup prerequisites + shell: bash + run: | + ## Install/setup prerequisites + sudo apt-get -y update ; sudo apt-get -y install npm ; sudo npm install cspell -g ; + - name: Run `cspell` + shell: bash + run: | + ## Run `cspell` + cspell --config .vscode/cSpell.json --no-summary --no-progress "**/*" | sed -E -n "s/${PWD//\//\\/}\/(.*):(.*):(.*) - (.*)/::error file=\1,line=\2,col=\3::ERROR: \4 (file:'\1', line:\2)/p" min_version: name: MinRustV # Minimum supported rust version @@ -104,7 +160,7 @@ jobs: job: - { os: ubuntu-latest , features: feat_os_unix } steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Install `rust` toolchain (v${{ env.RUST_MIN_SRV }}) uses: actions-rs/toolchain@v1 with: @@ -119,43 +175,42 @@ jobs: use-tool-cache: true env: RUSTUP_TOOLCHAIN: stable - - name: Confirm compatible 'Cargo.lock' + - name: Confirm MinSRV compatible 'Cargo.lock' shell: bash run: | - # Confirm compatible 'Cargo.lock' + ## Confirm MinSRV compatible 'Cargo.lock' # * 'Cargo.lock' is required to be in a format that `cargo` of MinSRV can interpret (eg, v1-format for MinSRV < v1.38) - cargo fetch --locked --quiet || { echo "::error file=Cargo.lock::Incompatible 'Cargo.lock' format; try \`cargo +${{ env.RUST_MIN_SRV }} update\`" ; exit 1 ; } + cargo fetch --locked --quiet || { echo "::error file=Cargo.lock::Incompatible (or out-of-date) 'Cargo.lock' file; update using \`cargo +${{ env.RUST_MIN_SRV }} update\`" ; exit 1 ; } - name: Info shell: bash run: | - # Info - ## environment + ## Info + # environment echo "## environment" echo "CI='${CI}'" - ## tooling info display + # tooling info display echo "## tooling" which gcc >/dev/null 2>&1 && (gcc --version | head -1) || true - rustup -V + rustup -V 2>/dev/null rustup show active-toolchain cargo -V rustc -V cargo-tree tree -V - ## dependencies + # dependencies echo "## dependency list" cargo fetch --locked --quiet ## * using the 'stable' toolchain is necessary to avoid "unexpected '--filter-platform'" errors - RUSTUP_TOOLCHAIN=stable cargo-tree tree --frozen --all --no-dev-dependencies --no-indent --features ${{ matrix.job.features }} | grep -vE "$PWD" | sort --unique - + RUSTUP_TOOLCHAIN=stable cargo-tree tree --locked --all --no-dev-dependencies --no-indent --features ${{ matrix.job.features }} | grep -vE "$PWD" | sort --unique - name: Test uses: actions-rs/cargo@v1 with: command: test - args: --features "feat_os_unix" + args: --features "feat_os_unix" -p uucore -p coreutils env: RUSTFLAGS: '-Awarnings' - busybox_test: - name: Busybox test suite + build_makefile: + name: Build/Makefile runs-on: ${{ matrix.job.os }} strategy: fail-fast: false @@ -163,23 +218,26 @@ jobs: job: - { os: ubuntu-latest } steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Install `rust` toolchain uses: actions-rs/toolchain@v1 with: toolchain: stable default: true profile: minimal # minimal component installation (ie, no documentation) - - name: "prepare busytest" + - name: Install/setup prerequisites shell: bash run: | - make prepare-busytest - - name: "run busybox testsuite" + ## Install/setup prerequisites + sudo apt-get -y update ; sudo apt-get -y install python3-sphinx ; + - name: "`make build`" shell: bash run: | - bindir=$(pwd)/target/debug - cd tmp/busybox-*/testsuite - S=$(bindir=$bindir ./runtest) && printf "%s\n" "$S" || { printf "%s\n" "$S" | grep "FAIL:" | sed -e "s/FAIL: /::warning ::Test failure:/g" ; } + make build + - name: "`make test`" + shell: bash + run: | + make test build: name: Build @@ -191,7 +249,6 @@ 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-16.04 , target: x86_64-unknown-linux-gnu , features: feat_os_unix , 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 } @@ -204,36 +261,37 @@ jobs: - { os: windows-latest , target: x86_64-pc-windows-gnu , features: feat_os_windows } ## note: requires rust >= 1.43.0 to link correctly - { os: windows-latest , target: x86_64-pc-windows-msvc , features: feat_os_windows } steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Install/setup prerequisites shell: bash run: | - ## install/setup prerequisites + ## Install/setup prerequisites case '${{ matrix.job.target }}' in arm-unknown-linux-gnueabihf) sudo apt-get -y update ; sudo apt-get -y install gcc-arm-linux-gnueabihf ;; aarch64-unknown-linux-gnu) sudo apt-get -y update ; sudo apt-get -y install gcc-aarch64-linux-gnu ;; esac + case '${{ matrix.job.os }}' in + macos-latest) brew install coreutils ;; # needed for testing + esac - name: Initialize workflow variables id: vars shell: bash run: | ## 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="stable" ## default to "stable" toolchain # * specify alternate/non-default TOOLCHAIN for *-pc-windows-gnu targets; gnu targets on Windows are broken for the standard *-pc-windows-msvc toolchain (refs: GH:rust-lang/rust#47048, GH:rust-lang/rust#53454, GH:rust-lang/cargo#6754) case ${{ matrix.job.target }} in *-pc-windows-gnu) TOOLCHAIN="stable-${{ matrix.job.target }}" ;; esac; # * use requested TOOLCHAIN if specified if [ -n "${{ matrix.job.toolchain }}" ]; then TOOLCHAIN="${{ matrix.job.toolchain }}" ; fi - echo set-output name=TOOLCHAIN::${TOOLCHAIN:-/false} - echo ::set-output name=TOOLCHAIN::${TOOLCHAIN} + outputs TOOLCHAIN # staging directory STAGING='_staging' - echo set-output name=STAGING::${STAGING} - echo ::set-output name=STAGING::${STAGING} + outputs STAGING # determine EXE suffix EXE_suffix="" ; case '${{ matrix.job.target }}' in *-pc-windows-*) EXE_suffix=".exe" ;; esac; - echo set-output name=EXE_suffix::${EXE_suffix} - echo ::set-output name=EXE_suffix::${EXE_suffix} + outputs EXE_suffix # parse commit reference info echo GITHUB_REF=${GITHUB_REF} echo GITHUB_SHA=${GITHUB_SHA} @@ -241,14 +299,7 @@ jobs: unset REF_BRANCH ; case "${GITHUB_REF}" in refs/heads/*) REF_BRANCH=${GITHUB_REF#refs/heads/} ;; esac; unset REF_TAG ; case "${GITHUB_REF}" in refs/tags/*) REF_TAG=${GITHUB_REF#refs/tags/} ;; esac; REF_SHAS=${GITHUB_SHA:0:8} - echo set-output name=REF_NAME::${REF_NAME} - echo set-output name=REF_BRANCH::${REF_BRANCH} - echo set-output name=REF_TAG::${REF_TAG} - echo set-output name=REF_SHAS::${REF_SHAS} - echo ::set-output name=REF_NAME::${REF_NAME} - echo ::set-output name=REF_BRANCH::${REF_BRANCH} - echo ::set-output name=REF_TAG::${REF_TAG} - echo ::set-output name=REF_SHAS::${REF_SHAS} + outputs REF_NAME REF_BRANCH REF_TAG REF_SHAS # parse target unset TARGET_ARCH case '${{ matrix.job.target }}' in @@ -258,68 +309,50 @@ jobs: i686-*) TARGET_ARCH=i686 ;; x86_64-*) TARGET_ARCH=x86_64 ;; esac; - echo set-output name=TARGET_ARCH::${TARGET_ARCH} - echo ::set-output name=TARGET_ARCH::${TARGET_ARCH} unset TARGET_OS ; case '${{ matrix.job.target }}' in *-linux-*) TARGET_OS=linux ;; *-apple-*) TARGET_OS=macos ;; *-windows-*) TARGET_OS=windows ;; esac; - echo set-output name=TARGET_OS::${TARGET_OS} - echo ::set-output name=TARGET_OS::${TARGET_OS} + outputs TARGET_ARCH TARGET_OS # package name PKG_suffix=".tar.gz" ; case '${{ matrix.job.target }}' in *-pc-windows-*) PKG_suffix=".zip" ;; esac; PKG_BASENAME=${PROJECT_NAME}-${REF_TAG:-$REF_SHAS}-${{ matrix.job.target }} PKG_NAME=${PKG_BASENAME}${PKG_suffix} - echo set-output name=PKG_suffix::${PKG_suffix} - echo set-output name=PKG_BASENAME::${PKG_BASENAME} - echo set-output name=PKG_NAME::${PKG_NAME} - echo ::set-output name=PKG_suffix::${PKG_suffix} - echo ::set-output name=PKG_BASENAME::${PKG_BASENAME} - echo ::set-output name=PKG_NAME::${PKG_NAME} + outputs PKG_suffix PKG_BASENAME PKG_NAME # deployable tag? (ie, leading "vM" or "M"; M == version number) unset DEPLOY ; if [[ $REF_TAG =~ ^[vV]?[0-9].* ]]; then DEPLOY='true' ; fi - echo set-output name=DEPLOY::${DEPLOY:-/false} - echo ::set-output name=DEPLOY::${DEPLOY} + outputs DEPLOY # DPKG architecture? unset DPKG_ARCH case ${{ matrix.job.target }} in x86_64-*-linux-*) DPKG_ARCH=amd64 ;; *-linux-*) DPKG_ARCH=${TARGET_ARCH} ;; esac - echo set-output name=DPKG_ARCH::${DPKG_ARCH} - echo ::set-output name=DPKG_ARCH::${DPKG_ARCH} + outputs DPKG_ARCH # DPKG version? unset DPKG_VERSION ; if [[ $REF_TAG =~ ^[vV]?[0-9].* ]]; then DPKG_VERSION=${REF_TAG/#[vV]/} ; fi - echo set-output name=DPKG_VERSION::${DPKG_VERSION} - echo ::set-output name=DPKG_VERSION::${DPKG_VERSION} + outputs DPKG_VERSION # DPKG base name/conflicts? DPKG_BASENAME=${PROJECT_NAME} DPKG_CONFLICTS=${PROJECT_NAME}-musl case ${{ matrix.job.target }} in *-musl) DPKG_BASENAME=${PROJECT_NAME}-musl ; DPKG_CONFLICTS=${PROJECT_NAME} ;; esac; - echo set-output name=DPKG_BASENAME::${DPKG_BASENAME} - echo set-output name=DPKG_CONFLICTS::${DPKG_CONFLICTS} - echo ::set-output name=DPKG_BASENAME::${DPKG_BASENAME} - echo ::set-output name=DPKG_CONFLICTS::${DPKG_CONFLICTS} + outputs DPKG_BASENAME DPKG_CONFLICTS # DPKG name unset DPKG_NAME; if [[ -n $DPKG_ARCH && -n $DPKG_VERSION ]]; then DPKG_NAME="${DPKG_BASENAME}_${DPKG_VERSION}_${DPKG_ARCH}.deb" ; fi - echo set-output name=DPKG_NAME::${DPKG_NAME} - echo ::set-output name=DPKG_NAME::${DPKG_NAME} + outputs DPKG_NAME # target-specific options # * CARGO_FEATURES_OPTION CARGO_FEATURES_OPTION='' ; if [ -n "${{ matrix.job.features }}" ]; then CARGO_FEATURES_OPTION='--features "${{ matrix.job.features }}"' ; fi - echo set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} - echo ::set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} + outputs CARGO_FEATURES_OPTION # * CARGO_USE_CROSS (truthy) CARGO_USE_CROSS='true' ; case '${{ matrix.job.use-cross }}' in ''|0|f|false|n|no) unset CARGO_USE_CROSS ;; esac; - echo set-output name=CARGO_USE_CROSS::${CARGO_USE_CROSS:-/false} - echo ::set-output name=CARGO_USE_CROSS::${CARGO_USE_CROSS} + outputs CARGO_USE_CROSS # ** pass needed environment into `cross` container (iff `cross` not already configured via "Cross.toml") if [ -n "${CARGO_USE_CROSS}" ] && [ ! -e "Cross.toml" ] ; then printf "[build.env]\npassthrough = [\"CI\"]\n" > Cross.toml fi # * test only library and/or binaries for arm-type targets unset CARGO_TEST_OPTIONS ; case '${{ matrix.job.target }}' in aarch64-* | arm-*) CARGO_TEST_OPTIONS="--bins" ;; esac; - echo set-output name=CARGO_TEST_OPTIONS::${CARGO_TEST_OPTIONS} - echo ::set-output name=CARGO_TEST_OPTIONS::${CARGO_TEST_OPTIONS} + outputs CARGO_TEST_OPTIONS # * executable for `strip`? STRIP="strip" case ${{ matrix.job.target }} in @@ -327,17 +360,20 @@ jobs: arm-*-linux-gnueabihf) STRIP="arm-linux-gnueabihf-strip" ;; *-pc-windows-msvc) STRIP="" ;; esac; - echo set-output name=STRIP::${STRIP:-/false} - echo ::set-output name=STRIP::${STRIP} + outputs STRIP - name: Create all needed build/work directories shell: bash run: | - ## create build/work space + ## Create build/work space mkdir -p '${{ steps.vars.outputs.STAGING }}' mkdir -p '${{ steps.vars.outputs.STAGING }}/${{ steps.vars.outputs.PKG_BASENAME }}' mkdir -p '${{ steps.vars.outputs.STAGING }}/dpkg' - name: rust toolchain ~ install uses: actions-rs/toolchain@v1 + env: + # Override auto-detection of RAM for Rustc install. + # https://github.com/rust-lang/rustup/issues/2229#issuecomment-585855925 + RUSTUP_UNPACK_RAM: "21474836480" with: toolchain: ${{ steps.vars.outputs.TOOLCHAIN }} target: ${{ matrix.job.target }} @@ -348,11 +384,12 @@ jobs: shell: bash run: | ## Dependent VARs setup + outputs() { step_id="dep_vars"; for var in "$@" ; do echo steps.${step_id}.outputs.${var}="${!var}"; echo ::set-output name=${var}::${!var}; done; } # * 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;)" - echo set-output name=UTILITY_LIST::${UTILITY_LIST} - echo ::set-output name=CARGO_UTILITY_LIST_OPTIONS::${CARGO_UTILITY_LIST_OPTIONS} + outputs CARGO_UTILITY_LIST_OPTIONS - name: Install `cargo-tree` # for dependency information uses: actions-rs/install@v0.1 with: @@ -364,26 +401,26 @@ jobs: - name: Info shell: bash run: | - # Info - ## commit info + ## Info + # commit info echo "## commit" echo GITHUB_REF=${GITHUB_REF} echo GITHUB_SHA=${GITHUB_SHA} - ## environment + # environment echo "## environment" echo "CI='${CI}'" - ## tooling info display + # tooling info display echo "## tooling" which gcc >/dev/null 2>&1 && (gcc --version | head -1) || true - rustup -V + rustup -V 2>/dev/null rustup show active-toolchain cargo -V rustc -V cargo-tree tree -V - ## dependencies + # dependencies echo "## dependency list" cargo fetch --locked --quiet - cargo-tree tree --target=${{ matrix.job.target }} ${{ matrix.job.cargo-options }} ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} --all --no-dev-dependencies --no-indent | grep -vE "$PWD" | sort --unique + cargo-tree tree --locked --target=${{ matrix.job.target }} ${{ matrix.job.cargo-options }} ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} --all --no-dev-dependencies --no-indent | grep -vE "$PWD" | sort --unique - name: Build uses: actions-rs/cargo@v1 with: @@ -410,7 +447,7 @@ jobs: - name: Package shell: bash run: | - ## package artifact(s) + ## Package artifact(s) # binary cp 'target/${{ matrix.job.target }}/release/${{ env.PROJECT_NAME }}${{ steps.vars.outputs.EXE_suffix }}' '${{ steps.vars.outputs.STAGING }}/${{ steps.vars.outputs.PKG_BASENAME }}/' # `strip` binary (if needed) @@ -451,6 +488,37 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + test_busybox: + name: Tests/BusyBox test suite + runs-on: ${{ matrix.job.os }} + strategy: + fail-fast: false + matrix: + job: + - { os: ubuntu-latest } + steps: + - uses: actions/checkout@v2 + - name: Install `rust` toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + profile: minimal # minimal component installation (ie, no documentation) + - name: Install/setup prerequisites + shell: bash + run: | + make prepare-busytest + - name: "Run BusyBox test suite" + shell: bash + run: | + ## Run BusyBox test suite + bindir=$(pwd)/target/debug + cd tmp/busybox-*/testsuite + output=$(bindir=$bindir ./runtest 2>&1 || true) + printf "%s\n" "${output}" + n_fails=$(echo "$output" | grep "^FAIL:\s" | wc --lines) + if [ $n_fails -gt 0 ] ; then echo "::warning ::${n_fails}+ test failures" ; fi + coverage: name: Code Coverage runs-on: ${{ matrix.job.os }} @@ -463,41 +531,45 @@ jobs: - { os: macos-latest , features: macos } - { os: windows-latest , features: windows } steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 + - name: Install/setup prerequisites + shell: bash + run: | + ## Install/setup prerequisites + case '${{ matrix.job.os }}' in + macos-latest) brew install coreutils ;; # needed for testing + esac # - name: Reattach HEAD ## may be needed for accurate code coverage info # run: git checkout ${{ github.head_ref }} - name: Initialize workflow variables id: vars shell: bash run: | + ## 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 # * 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 if [ -n "${{ matrix.job.toolchain }}" ]; then TOOLCHAIN="${{ matrix.job.toolchain }}" ; fi - echo set-output name=TOOLCHAIN::${TOOLCHAIN} - echo ::set-output name=TOOLCHAIN::${TOOLCHAIN} + outputs TOOLCHAIN # staging directory STAGING='_staging' - echo set-output name=STAGING::${STAGING} - echo ::set-output name=STAGING::${STAGING} + outputs STAGING ## # check for CODECOV_TOKEN availability (work-around for inaccessible 'secrets' object for 'if'; see ) ## # note: CODECOV_TOKEN / HAS_CODECOV_TOKEN is not needed for public repositories when using AppVeyor, Azure Pipelines, CircleCI, GitHub Actions, Travis (see ) ## unset HAS_CODECOV_TOKEN ## if [ -n $CODECOV_TOKEN ]; then HAS_CODECOV_TOKEN='true' ; fi - ## echo set-output name=HAS_CODECOV_TOKEN::${HAS_CODECOV_TOKEN} - ## echo ::set-output name=HAS_CODECOV_TOKEN::${HAS_CODECOV_TOKEN} + ## outputs HAS_CODECOV_TOKEN # target-specific options # * CARGO_FEATURES_OPTION CARGO_FEATURES_OPTION='--all-features' ; ## default to '--all-features' for code coverage if [ -n "${{ matrix.job.features }}" ]; then CARGO_FEATURES_OPTION='--features "${{ matrix.job.features }}"' ; fi - echo set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} - echo ::set-output name=CARGO_FEATURES_OPTION::${CARGO_FEATURES_OPTION} + outputs CARGO_FEATURES_OPTION # * CODECOV_FLAGS CODECOV_FLAGS=$( echo "${{ matrix.job.os }}" | sed 's/[^[:alnum:]]/_/g' ) - echo set-output name=CODECOV_FLAGS::${CODECOV_FLAGS} - echo ::set-output name=CODECOV_FLAGS::${CODECOV_FLAGS} + outputs CODECOV_FLAGS - name: rust toolchain ~ install uses: actions-rs/toolchain@v1 with: @@ -509,11 +581,22 @@ jobs: shell: bash run: | ## Dependent VARs setup + outputs() { step_id="dep_vars"; for var in "$@" ; do echo steps.${step_id}.outputs.${var}="${!var}"; echo ::set-output name=${var}::${!var}; done; } # * determine sub-crate utility list UTILITY_LIST="$(./util/show-utils.sh ${CARGO_FEATURES_OPTION})" CARGO_UTILITY_LIST_OPTIONS="$(for u in ${UTILITY_LIST}; do echo "-puu_${u}"; done;)" - echo set-output name=UTILITY_LIST::${UTILITY_LIST} - echo ::set-output name=CARGO_UTILITY_LIST_OPTIONS::${CARGO_UTILITY_LIST_OPTIONS} + outputs CARGO_UTILITY_LIST_OPTIONS + - name: Test uucore + uses: actions-rs/cargo@v1 + with: + command: test + args: ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} --no-fail-fast -p uucore + env: + CARGO_INCREMENTAL: '0' + RUSTC_WRAPPER: '' + RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort' + RUSTDOCFLAGS: '-Cpanic=abort' + # RUSTUP_TOOLCHAIN: ${{ steps.vars.outputs.TOOLCHAIN }} - name: Test uses: actions-rs/cargo@v1 with: @@ -541,12 +624,12 @@ jobs: with: crate: grcov version: latest - use-tool-cache: true + use-tool-cache: false - name: Generate coverage data (via `grcov`) id: coverage shell: bash run: | - # generate coverage data + ## Generate coverage data COVERAGE_REPORT_DIR="target/debug" COVERAGE_REPORT_FILE="${COVERAGE_REPORT_DIR}/lcov.info" # GRCOV_IGNORE_OPTION='--ignore build.rs --ignore "/*" --ignore "[a-zA-Z]:/*"' ## `grcov` ignores these params when passed as an environment variable (why?) diff --git a/.github/workflows/FixPR.yml b/.github/workflows/FixPR.yml new file mode 100644 index 000000000..d3f8a86b8 --- /dev/null +++ b/.github/workflows/FixPR.yml @@ -0,0 +1,135 @@ +name: FixPR + +# Trigger automated fixes for PRs being merged (with associated commits) + +# ToDO: [2021-06; rivy] change from `cargo-tree` to `cargo tree` once MSRV is >= 1.45 + +env: + BRANCH_TARGET: master + +on: + # * only trigger on pull request closed to specific branches + # ref: https://github.community/t/trigger-workflow-only-on-pull-request-merge/17359/9 + pull_request: + branches: + - master # == env.BRANCH_TARGET ## unfortunately, env context variables are only available in jobs/steps (see ) + types: [ closed ] + +jobs: + code_deps: + # Refresh dependencies (ie, 'Cargo.lock') and show updated dependency tree + if: github.event.pull_request.merged == true ## only for PR merges + name: Update/dependencies + runs-on: ${{ matrix.job.os }} + strategy: + matrix: + job: + - { os: ubuntu-latest , features: feat_os_unix } + steps: + - uses: actions/checkout@v2 + - name: Initialize job variables + id: vars + shell: bash + run: | + ## VARs setup + outputs() { step_id="vars"; for var in "$@" ; do echo steps.${step_id}.outputs.${var}="${!var}"; echo ::set-output name=${var}::${!var}; done; } + # surface MSRV from CICD workflow + RUST_MIN_SRV=$(grep -P "^\s+RUST_MIN_SRV:" .github/workflows/CICD.yml | grep -Po "(?<=\x22)\d+[.]\d+(?:[.]\d+)?(?=\x22)" ) + outputs RUST_MIN_SRV + - name: Install `rust` toolchain (v${{ steps.vars.outputs.RUST_MIN_SRV }}) + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ steps.vars.outputs.RUST_MIN_SRV }} + default: true + profile: minimal # minimal component installation (ie, no documentation) + - name: Install `cargo-tree` # for dependency information + uses: actions-rs/install@v0.1 + with: + crate: cargo-tree + version: latest + use-tool-cache: true + env: + RUSTUP_TOOLCHAIN: stable + - name: Ensure updated 'Cargo.lock' + shell: bash + run: | + # Ensure updated 'Cargo.lock' + # * 'Cargo.lock' is required to be in a format that `cargo` of MinSRV can interpret (eg, v1-format for MinSRV < v1.38) + cargo fetch --locked --quiet || cargo +${{ steps.vars.outputs.RUST_MIN_SRV }} update + - name: Info + shell: bash + run: | + # Info + ## environment + echo "## environment" + echo "CI='${CI}'" + ## tooling info display + echo "## tooling" + which gcc >/dev/null 2>&1 && (gcc --version | head -1) || true + rustup -V 2>/dev/null + rustup show active-toolchain + cargo -V + rustc -V + cargo-tree tree -V + ## dependencies + echo "## dependency list" + cargo fetch --locked --quiet + ## * using the 'stable' toolchain is necessary to avoid "unexpected '--filter-platform'" errors + RUSTUP_TOOLCHAIN=stable cargo-tree tree --locked --all --no-dev-dependencies --no-indent --features ${{ matrix.job.features }} | grep -vE "$PWD" | sort --unique + - name: Commit any changes (to '${{ env.BRANCH_TARGET }}') + uses: EndBug/add-and-commit@v7 + with: + branch: ${{ env.BRANCH_TARGET }} + default_author: github_actions + message: "maint ~ refresh 'Cargo.lock'" + add: Cargo.lock + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + code_format: + # Recheck/refresh code formatting + if: github.event.pull_request.merged == true ## only for PR merges + name: Update/format + runs-on: ${{ matrix.job.os }} + strategy: + fail-fast: false + matrix: + job: + - { os: ubuntu-latest , features: feat_os_unix } + steps: + - uses: actions/checkout@v2 + - name: Initialize job variables + id: vars + shell: bash + run: | + ## VARs setup + 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 + outputs CARGO_FEATURES_OPTION + - name: Install `rust` toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + profile: minimal # minimal component installation (ie, no documentation) + components: rustfmt + - name: "`cargo fmt`" + shell: bash + run: | + cargo fmt + - name: "`cargo fmt` tests" + shell: bash + run: | + # `cargo fmt` of tests + find tests -name "*.rs" -print0 | xargs -0 cargo fmt -- + - name: Commit any changes (to '${{ env.BRANCH_TARGET }}') + uses: EndBug/add-and-commit@v7 + with: + branch: ${{ env.BRANCH_TARGET }} + default_author: github_actions + message: "maint ~ rustfmt (`cargo fmt`)" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/GNU.yml b/.github/workflows/GNU.yml deleted file mode 100644 index b1cbc1502..000000000 --- a/.github/workflows/GNU.yml +++ /dev/null @@ -1,101 +0,0 @@ -name: GNU - -on: [push, pull_request] - -jobs: - gnu: - name: Run GNU tests - runs-on: ubuntu-latest - steps: - # Checks out a copy of your repository on the ubuntu-latest machine - - name: Checkout code uutil - uses: actions/checkout@v2 - with: - path: 'uutils' - - name: Chechout GNU coreutils - uses: actions/checkout@v2 - with: - repository: 'coreutils/coreutils' - path: 'gnu' - - name: Chechout GNU corelib - uses: actions/checkout@v2 - with: - repository: 'coreutils/gnulib' - path: 'gnulib' - fetch-depth: 0 # gnu gets upset if gnulib is a shallow checkout - - name: Install `rust` toolchain - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - default: true - profile: minimal # minimal component installation (ie, no documentation) - components: rustfmt - - name: Build binaries - shell: bash - run: | - sudo apt-get update - sudo apt-get install autoconf autopoint bison texinfo gperf gcc g++ gdb python-pyinotify expect python3-sphinx - pushd uutils - make PROFILE=release - BUILDDIR="$PWD/target/release/" - popd - GNULIB_SRCDIR="$PWD/gnulib" - pushd gnu/ - ./bootstrap --gnulib-srcdir="$GNULIB_SRCDIR" - ./configure --quiet --disable-gcc-warnings - # Change the PATH in the Makefile to test the uutils coreutils instead of the GNU coreutils - sed -i "s/^[[:blank:]]*PATH=.*/ PATH='${BUILDDIR//\//\\/}\$(PATH_SEPARATOR)'\"\$\$PATH\" \\\/" Makefile - sed -i 's| tr | /usr/bin/tr |' tests/init.sh - make - # Generate the factor tests, so they can be fixed - for i in $(seq -w 1 36) - do - make tests/factor/t${i}.sh - done - grep -rl 'path_prepend_' tests/* | xargs sed -i 's|path_prepend_ ./src||' - sed -i -e 's|^seq |/usr/bin/seq |' tests/factor/t*sh - sed -i -e '/tests\/misc\/cat-self.sh/ D' Makefile # issue #1707 - sed -i -e '/tests\/misc\/numfmt.pl/ D' Makefile # issue #1708 - sed -i -e '/tests\/chown\/preserve-root.sh/ D' Makefile # issue #1709 - sed -i -e '/tests\/cp\/file-perm-race.sh/ D' Makefile # issue #1710 - sed -i -e '/tests\/cp\/special-f.sh/ D' Makefile # issue #1710 - sed -i -e '/tests\/mv\/mv-special-1.sh/ D' Makefile # issue #1710 - sed -i -e '/tests\/dd\/stats.sh/ D' Makefile # issue #1710 - sed -i -e '/tests\/cp\/existing-perm-race.sh/ D' Makefile # hangs, cp doesn't implement --copy-contents - # Remove tests that rely on seq with large numbers. See issue #1703 - sed -i -e '/tests\/misc\/seq.pl/ D' \ - -e '/tests\/misc\/seq-precision.sh/D' \ - Makefile - # Remove tests checking for --version & --help - # Not really interesting for us and logs are too big - sed -i -e '/tests\/misc\/invalid-opt.pl/ D' \ - -e '/tests\/misc\/help-version.sh/ D' \ - -e '/tests\/misc\/help-version-getopt.sh/ D' \ - Makefile - sed -i -e '/tests\/tail-2\/pid.sh/ D' Makefile # hangs on github, tail doesn't support -f - sed -i -e'/incompat4/ D' -e"/options '-co' are incompatible/ d" tests/misc/sort.pl # Sort doesn't correctly check for incompatible options, waits for input - - test -f "${BUILDDIR}/getlimits" || cp src/getlimits "${BUILDDIR}" - - name: Run GNU tests - shell: bash - run: | - BUILDDIR="${PWD}/uutils/target/release" - GNULIB_DIR="${PWD}/gnulib" - pushd gnu - - unbuffer timeout -sKILL 3600 make -j "$(nproc)" check SUBDIRS=. RUN_EXPENSIVE_TESTS=yes RUN_VERY_EXPENSIVE_TESTS=yes VERBOSE=no || : - - name: Extract tests info - shell: bash - run: | - TOTAL=$( grep "# TOTAL:" gnu/tests/test-suite.log|cut -d' ' -f2-) - PASS=$( grep "# PASS:" gnu/tests/test-suite.log|cut -d' ' -f2-) - SKIP=$( grep "# SKIP:" gnu/tests/test-suite.log|cut -d' ' -f2-) - FAIL=$( grep "# FAIL:" gnu/tests/test-suite.log|cut -d' ' -f2-) - XPASS=$( grep "# XPASS:" gnu/tests/test-suite.log|cut -d' ' -f2-) - ERROR=$( grep "# ERROR:" gnu/tests/test-suite.log|cut -d' ' -f2-) - echo "::warning ::GNU testsuite = $TOTAL / $PASS / $FAIL / $ERROR" - - - uses: actions/upload-artifact@v2 - with: - name: test-report - path: gnu/tests/**/*.log diff --git a/.github/workflows/GnuTests.yml b/.github/workflows/GnuTests.yml new file mode 100644 index 000000000..8bf6c091b --- /dev/null +++ b/.github/workflows/GnuTests.yml @@ -0,0 +1,92 @@ +name: GnuTests + +# spell-checker:ignore (names) gnulib ; (utils) autopoint gperf pyinotify texinfo ; (vars) XPASS + +on: [push, pull_request] + +jobs: + gnu: + name: Run GNU tests + runs-on: ubuntu-latest + steps: + - name: Checkout code uutil + uses: actions/checkout@v2 + with: + path: 'uutils' + - name: Checkout GNU coreutils + uses: actions/checkout@v2 + with: + repository: 'coreutils/coreutils' + path: 'gnu' + ref: v8.32 + - name: Checkout GNU coreutils library (gnulib) + uses: actions/checkout@v2 + with: + repository: 'coreutils/gnulib' + path: 'gnulib' + ref: 8e99f24c0931a38880c6ee9b8287c7da80b0036b + fetch-depth: 0 # gnu gets upset if gnulib is a shallow checkout + - name: Install `rust` toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + profile: minimal # minimal component installation (ie, no documentation) + components: rustfmt + - name: Install dependencies + shell: bash + run: | + ## Install dependencies + sudo apt-get update + sudo apt-get install autoconf autopoint bison texinfo gperf gcc g++ gdb python-pyinotify python3-sphinx jq + - name: Build binaries + shell: bash + run: | + ## Build binaries + cd uutils + bash util/build-gnu.sh + - name: Run GNU tests + shell: bash + run: | + bash uutils/util/run-gnu-test.sh + - name: Extract testing info + shell: bash + run: | + ## Extract testing info + LOG_FILE=gnu/tests/test-suite.log + if test -f "$LOG_FILE" + then + TOTAL=$(sed -n "s/.*# TOTAL: \(.*\)/\1/p" "$LOG_FILE"|tr -d '\r'|head -n1) + PASS=$(sed -n "s/.*# PASS: \(.*\)/\1/p" "$LOG_FILE"|tr -d '\r'|head -n1) + SKIP=$(sed -n "s/.*# SKIP: \(.*\)/\1/p" "$LOG_FILE"|tr -d '\r'|head -n1) + FAIL=$(sed -n "s/.*# FAIL: \(.*\)/\1/p" "$LOG_FILE"|tr -d '\r'|head -n1) + XPASS=$(sed -n "s/.*# XPASS: \(.*\)/\1/p" "$LOG_FILE"|tr -d '\r'|head -n1) + ERROR=$(sed -n "s/.*# ERROR: \(.*\)/\1/p" "$LOG_FILE"|tr -d '\r'|head -n1) + if [[ "$TOTAL" -eq 0 || "$TOTAL" -eq 1 ]]; then + echo "Error in the execution, failing early" + exit 1 + fi + output="GNU tests summary = TOTAL: $TOTAL / PASS: $PASS / FAIL: $FAIL / ERROR: $ERROR" + echo "${output}" + if [[ "$FAIL" -gt 0 || "$ERROR" -gt 0 ]]; then echo "::warning ::${output}" ; fi + jq -n \ + --arg date "$(date --rfc-email)" \ + --arg sha "$GITHUB_SHA" \ + --arg total "$TOTAL" \ + --arg pass "$PASS" \ + --arg skip "$SKIP" \ + --arg fail "$FAIL" \ + --arg xpass "$XPASS" \ + --arg error "$ERROR" \ + '{($date): { sha: $sha, total: $total, pass: $pass, skip: $skip, fail: $fail, xpass: $xpass, error: $error, }}' > gnu-result.json + else + echo "::error ::Failed to get summary of test results" + fi + - uses: actions/upload-artifact@v2 + with: + name: test-report + path: gnu/tests/**/*.log + - uses: actions/upload-artifact@v2 + with: + name: gnu-result + path: gnu-result.json diff --git a/.gitignore b/.gitignore index b1ac52506..77e8f717e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ target/ Cargo.lock lib*.a /docs/_build +*.iml +### macOS ### +.DS_Store diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..e3ad98ee3 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +repos: +- repo: local + hooks: + - id: rust-linting + name: Rust linting + description: Run cargo fmt on files included in the commit. + entry: cargo +nightly fmt -- + pass_filenames: true + types: [file, rust] + language: system + - id: rust-clippy + name: Rust clippy + description: Run cargo clippy on files included in the commit. + entry: cargo +nightly clippy --all-targets --all-features -- + pass_filenames: false + types: [file, rust] + language: system diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 3cd7db130..000000000 --- a/.travis.yml +++ /dev/null @@ -1,72 +0,0 @@ -language: rust - -rust: - - stable - - beta - -os: - - linux - # - osx - -env: - # sphinx v1.8.0 is bugged & fails for linux builds; so, force specific `sphinx` version - global: FEATURES='' TEST_INSTALL='' SPHINX_VERSIONED='sphinx==1.7.8' - -matrix: - allow_failures: - - rust: beta - - rust: nightly - fast_finish: true - include: - - rust: 1.33.0 - env: FEATURES=unix - # - rust: stable - # os: linux - # env: FEATURES=unix TEST_INSTALL=true - # - rust: stable - # os: osx - # env: FEATURES=macos TEST_INSTALL=true - - rust: nightly - os: linux - env: FEATURES=nightly,unix TEST_INSTALL=true - - rust: nightly - os: osx - env: FEATURES=nightly,macos TEST_INSTALL=true - - rust: nightly - os: linux - env: FEATURES=nightly,feat_os_unix_redox CC=x86_64-unknown-redox-gcc CARGO_ARGS='--no-default-features --target=x86_64-unknown-redox' REDOX=1 - -cache: - directories: - - $HOME/.cargo - -sudo: true - -before_install: - - if [ $REDOX ]; then ./.travis/redox-toolchain.sh; fi - -install: - - if [ $TRAVIS_OS_NAME = linux ]; then sudo apt-get install python-pip && sudo pip install $SPHINX_VERSIONED; fi - - | - if [ $TRAVIS_OS_NAME = osx ]; then - brew update - brew upgrade python - pip3 install $SPHINX_VERSIONED - fi - -script: - - cargo build $CARGO_ARGS --features "$FEATURES" - - if [ ! $REDOX ]; then cargo test $CARGO_ARGS --features "$FEATURES" --no-fail-fast; fi - - if [ -n "$TEST_INSTALL" ]; then mkdir installdir_test; DESTDIR=installdir_test make install; [ `ls installdir_test/usr/local/bin | wc -l` -gt 0 ]; fi - -addons: - apt: - packages: - - libssl-dev - -after_success: | - if [ "$TRAVIS_OS_NAME" = linux -a "$TRAVIS_RUST_VERSION" = stable ]; then - bash <(curl https://raw.githubusercontent.com/xd009642/tarpaulin/master/travis-install.sh) - cargo tarpaulin --out Xml - bash <(curl -s https://codecov.io/bash) - fi diff --git a/.vscode/cSpell.json b/.vscode/cSpell.json index 8561d69ad..9869923a4 100644 --- a/.vscode/cSpell.json +++ b/.vscode/cSpell.json @@ -1,352 +1,19 @@ // `cspell` settings { - "version": "0.1", // Version of the setting file. Always 0.1 - "language": "en", // language - current active spelling language - // ignoreWords - "ignoreWords": [ - // abbrev/acronyms - "Cygwin", - "FreeBSD", - "Gmail", - "GNUEABI", - "GNUEABIhf", - "Irix", - "MacOS", - "MinGW", - "Minix", - "MS-DOS", - "MSDOS", - "NetBSD", - "Novell", - "OpenBSD", - "POSIX", - "SELinux", - "Solaris", - "Xenix", - "flac", - "lzma", - // cargo - "cdylib", - "rlib", - // crates - "advapi", - "advapi32-sys", - "aho-corasick", - "backtrace", - "byteorder", - "chacha", - "chrono", - "conv", - "corasick", - "filetime", - "formatteriteminfo", - "getopts", - "itertools", - "memchr", - "multifilereader", - "onig", - "peekreader", - "quickcheck", - "rand_chacha", - "smallvec", - "tempfile", - "termion", - "termios", - "termsize", - "termwidth", - "textwrap", - "walkdir", - "winapi", - "xattr", - // jargon - "AST", // abstract syntax tree - "CPU", - "CPUs", - "FIFO", - "FIFOs", - "FQDN", // fully qualified domain name - "GID", // group ID - "GIDs", - "POSIXLY", - "RNG", // random number generator - "RNGs", - "UID", // user ID - "UIDs", - "UUID", // universally unique identifier - "arity", - "bitmask", - "canonicalization", - "canonicalize", - "colorizable", - "colorize", - "consts", - "dedup", - "demangle", - "deque", - "dequeue", - "enqueue", - "executable", - "executables", - "gibibytes", - "hardfloat", - "hardlink", - "hardlinks", - "hashsums", - "kibibytes", - "mebibytes", - "mergeable", - "multibyte", - "nonportable", - "peekable", - "precompiled", - "precompute", - "preload", - "prepend", - "prepended", - "primality", - "pseudoprime", - "pseudoprimes", - "procs", - "readonly", - "seedable", - "semver", - "shortcode", - "shortcodes", - "symlink", - "symlinks", - "syscall", - "toekenize", - "unbuffered", - "unportable", - "whitespace", - // names - "Akira Hayakawa", "Akira", "Hayakawa", - "Alan Andrade", "Alan", "Andrade", - "Alex Lyon", "Alex", "Lyon", - "Alexander Batischev", "Alexander", "Batischev", - "Aleksander Bielawski", "Aleksander", "Bielawski", - "Alexander Fomin", "Alexander", "Fomin", - "Anthony Deschamps", "Anthony", "Deschamps", - "Ben Eills", "Ben", "Eills", - "Ben Hirsch", "Ben", "Hirsch", - "Benoit Benedetti", "Benoit", "Benedetti", - "Boden Garman", "Boden", "Garman", - "Chirag B Jadwani", "Chirag", "Jadwani", - "Derek Chiang", "Derek", "Chiang", - "Dorota Kapturkiewicz", "Dorota", "Kapturkiewicz", - "Evgeniy Klyuchikov", "Evgeniy", "Klyuchikov", - "Fangxu Hu", "Fangxu", "Hu", - "Gil Cottle", "Gil", "Cottle", - "Haitao Li", "Haitao", "Li", - "Inokentiy Babushkin", "Inokentiy", "Babushkin", - "Joao Oliveira", "Joao", "Oliveira", - "Jeremiah Peschka", "Jeremiah", "Peschka", - "Jian Zeng", "Jian", "Zeng", - "Jimmy Lu", "Jimmy", "Lu", - "Jordi Boggiano", "Jordi", "Boggiano", - "Jordy Dickinson", "Jordy", "Dickinson", - "Joseph Crail", "Joseph", "Crail", - "Joshua S Miller", "Joshua", "Miller", - "KokaKiwi", - "Konstantin Pospelov", "Konstantin", "Pospelov", - "Mahkoh", - "Maciej Dziardziel", "Maciej", "Dziardziel", - "Michael Gehring", "Michael", "Gehring", - "Martin Kysel", "Martin", "Kysel", - "Morten Olsen Lysgaard", "Morten", "Olsen", "Lysgaard", - "Nicholas Juszczak", "Nicholas", "Juszczak", - "Nick Platt", "Nick", "Platt", - "Orvar Segerström", "Orvar", "Segerström", - "Peter Atashian", "Peter", "Atashian", - "Rolf Morel", "Rolf", "Morel", - "Roman Gafiyatullin", "Roman", "Gafiyatullin", - "Roy Ivy III", "Roy", "Ivy", "III", - "Sergey 'Shnatsel' Davidoff", "Sergey", "Shnatsel", "Davidoff", - "Sokovikov Evgeniy", "Sokovikov", "Evgeniy", - "Sunrin SHIMURA", "Sunrin", "SHIMURA", - "Smigle00", "Smigle", - "Sylvestre Ledru", "Sylvestre", "Ledru", - "T Jameson Little", "Jameson", "Little", - "Tobias Bohumir Schottdorf", "Tobias", "Bohumir", "Schottdorf", - "Virgile Andreani", "Virgile", "Andreani", - "Vsevolod Velichko", "Vsevolod", "Velichko", - "Wiktor Kuropatwa", "Wiktor", "Kuropatwa", - "Yury Krivopalov", "Yury", "Krivopalov", - "anonymousknight", - "kwantam", - "nicoo", - "rivy", - // rust - "clippy", - "concat", - "fract", - "powi", - "println", - "repr", - "rfind", - "rustc", - "rustfmt", - "struct", - "structs", - "substr", - "splitn", - "trunc", - // shell - "passwd", - "pipefail", - "tcsh", - // tags - "Maint", - // uutils - "chcon", - "chgrp", - "chmod", - "chown", - "chroot", - "cksum", - "csplit", - "dircolors", - "hashsum", - "hostid", - "logname", - "mkdir", - "mkfifo", - "mknod", - "mktemp", - "nohup", - "nproc", - "numfmt", - "pathchk", - "printenv", - "printf", - "readlink", - "realpath", - "relpath", - "rmdir", - "runcon", - "shuf", - "stdbuf", - "stty", - "tsort", - "uname", - "unexpand", - "whoami", - // vars/errno - "errno", - "EOPNOTSUPP", - // vars/fcntl - "F_GETFL", - "GETFL", - "fcntl", - "vmsplice", - // vars/libc - "FILENO", - "HOSTSIZE", - "IDSIZE", - "IFIFO", - "IFREG", - "IRGRP", - "IROTH", - "IRUSR", - "ISGID", - "ISUID", - "ISVTX", - "IWGRP", - "IWOTH", - "IWUSR", - "IXGRP", - "IXOTH", - "IXUSR", - "LINESIZE", - "NAMESIZE", - "USERSIZE", - "addrinfo", - "addrlen", - "canonname", - "chroot", - "freeaddrinfo", - "getaddrinfo", - "getegid", - "geteuid", - "getgid", - "getgrgid", - "getgrnam", - "getgrouplist", - "getgroups", - "getpwnam", - "getpwuid", - "getuid", - "inode", - "isatty", - "lchown", - "setgid", - "setgroups", - "setuid", - "socktype", - "umask", - "waitpid", - // vars/nix - "iovec", - "unistd", - // vars/signals - "SIGPIPE", - // vars/sync - "Condvar", - // vars/stat - "fstat", - "stat", - // vars/time - "Timespec", - "nsec", - "nsecs", - "strftime", - "usec", - "usecs", - // vars/utmpx - "endutxent", - "getutxent", - "getutxid", - "getutxline", - "pututxline", - "setutxent", - "utmp", - "utmpx", - "utmpxname", - // vars/winapi - "errhandlingapi", - "fileapi", - "handleapi", - "lmcons", - "minwindef", - "processthreadsapi", - "synchapi", - "sysinfoapi", - "winbase", - "winerror", - "winnt", - "winsock", - "DWORD", - "LPWSTR", - "WCHAR", - // uucore - "optflag", - "optflagmulti", - "optflagopt", - "optmulti", - "optopt", - // uutils - "coreopts", - "coreutils", - "libc", - "libstdbuf", - "musl", - "ucmd", - "utmpx", - "uucore", - "uucore_procs", - "uumain", - "uutils" - ], - // words - list of words to be always considered correct - "words": [] + "version": "0.1", // Version of the setting file. Always 0.1 + "language": "en", // language - current active spelling language + "dictionaries": ["acronyms+names", "jargon", "people", "shell", "workspace"], + "dictionaryDefinitions": [ + { "name": "acronyms+names", "path": "./cspell.dictionaries/acronyms+names.wordlist.txt" }, + { "name": "jargon", "path": "./cspell.dictionaries/jargon.wordlist.txt" }, + { "name": "people", "path": "./cspell.dictionaries/people.wordlist.txt" }, + { "name": "shell", "path": "./cspell.dictionaries/shell.wordlist.txt" }, + { "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/**"], + // 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 + "words": [] } diff --git a/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt b/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt new file mode 100644 index 000000000..a46448a32 --- /dev/null +++ b/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt @@ -0,0 +1,66 @@ +# * abbreviations / acronyms +AIX +ASLR # address space layout randomization +AST # abstract syntax tree +CICD # continuous integration/deployment +CPU +CPUs +DevOps +Ext3 +FIFO +FIFOs +FQDN # fully qualified domain name +GID # group ID +GIDs +GNU +GNUEABI +GNUEABIhf +JFS +MSRV # minimum supported rust version +MSVC +NixOS +POSIX +POSIXLY +RISC +RISCV +RNG # random number generator +RNGs +ReiserFS +Solaris +UID # user ID +UIDs +UUID # universally unique identifier +WASI +WASM +XFS +aarch +flac +lzma + +# * names +BusyBox +BusyTest +Codacy +Cygwin +Deno +EditorConfig +FreeBSD +Gmail +GNU +Irix +MS-DOS +MSDOS +MacOS +MinGW +Minix +NetBSD +Novell +OpenBSD +POSIX +PowerPC +SELinux +SkyPack +Solaris +SysV +Xenix +Yargs diff --git a/.vscode/cspell.dictionaries/jargon.wordlist.txt b/.vscode/cspell.dictionaries/jargon.wordlist.txt new file mode 100644 index 000000000..c2e2c29f3 --- /dev/null +++ b/.vscode/cspell.dictionaries/jargon.wordlist.txt @@ -0,0 +1,111 @@ +arity +autogenerate +autogenerated +autogenerates +bitmask +bitwise +bytewise +canonicalization +canonicalize +canonicalizing +colorizable +colorize +coprime +consts +cyclomatic +dedup +deduplication +demangle +denoland +deque +dequeue +dev +devs +discoverability +duplicative +enqueue +errored +executable +executables +exponentiate +eval +falsey +flamegraph +gibibytes +glob +globbing +hardfloat +hardlink +hardlinks +hasher +hashsums +kibi +kibibytes +mebi +mebibytes +mergeable +microbenchmark +microbenchmarks +microbenchmarking +multibyte +multicall +nonportable +nonprinting +peekable +performant +precompiled +precompute +preload +prepend +prepended +primality +pseudoprime +pseudoprimes +quantiles +readonly +reparse +seedable +semver +semiprime +semiprimes +shortcode +shortcodes +subcommand +subexpression +submodule +symlink +symlinks +syscall +syscalls +tokenize +toolchain +truthy +unbuffered +unescape +unintuitive +unprefixed +unportable +unsync +whitespace +wordlist +wordlists + +# * abbreviations +consts +deps +dev +maint +proc +procs + +# * constants +xffff + +# * variables +delim +errno +progname +retval +subdir +val +vals diff --git a/.vscode/cspell.dictionaries/people.wordlist.txt b/.vscode/cspell.dictionaries/people.wordlist.txt new file mode 100644 index 000000000..d7665585b --- /dev/null +++ b/.vscode/cspell.dictionaries/people.wordlist.txt @@ -0,0 +1,178 @@ +Akira Hayakawa + Akira + Hayakawa +Alan Andrade + Alan + Andrade +Aleksander Bielawski + Aleksander + Bielawski +Alex Lyon + Alex + Lyon +Alexander Batischev + Alexander + Batischev +Alexander Fomin + Alexander + Fomin +Anthony Deschamps + Anthony + Deschamps +Árni Dagur + Árni + Dagur +Ben Eills + Ben + Eills +Ben Hirsch + Ben + Hirsch +Benoit Benedetti + Benoit + Benedetti +Boden Garman + Boden + Garman +Chirag B Jadwani + Chirag + Jadwani +Derek Chiang + Derek + Chiang +Dorota Kapturkiewicz + Dorota + Kapturkiewicz +Evgeniy Klyuchikov + Evgeniy + Klyuchikov +Fangxu Hu + Fangxu + Hu +Gil Cottle + Gil + Cottle +Haitao Li + Haitao + Li +Inokentiy Babushkin + Inokentiy + Babushkin +Jan Scheer * jhscheer + Jan + Scheer + jhscheer +Jeremiah Peschka + Jeremiah + Peschka +Jian Zeng + Jian + Zeng +Jimmy Lu + Jimmy + Lu +Joao Oliveira + Joao + Oliveira +Jordi Boggiano + Jordi + Boggiano +Jordy Dickinson + Jordy + Dickinson +Joseph Crail + Joseph + Crail +Joshua S Miller + Joshua + Miller +Konstantin Pospelov + Konstantin + Pospelov +Maciej Dziardziel + Maciej + Dziardziel +Martin Kysel + Martin + Kysel +Michael Debertol + Michael + Debertol +Michael Gehring + Michael + Gehring +Mitchell Mebane + Mitchell + Mebane +Morten Olsen Lysgaard + Morten + Olsen + Lysgaard +Nicholas Juszczak + Nicholas + Juszczak +Nick Platt + Nick + Platt +Orvar Segerström + Orvar + Segerström +Peter Atashian + Peter + Atashian +Robert Swinford + Robert + Swinford +Rolf Morel + Rolf + Morel +Roman Gafiyatullin + Roman + Gafiyatullin +Roy Ivy III * rivy + Roy + Ivy + III + rivy +Sergey "Shnatsel" Davidoff + Sergey Shnatsel Davidoff + Sergey + Shnatsel + Davidoff +Sokovikov Evgeniy + Sokovikov + Evgeniy +Sunrin SHIMURA + Sunrin + SHIMURA +Sylvestre Ledru + Sylvestre + Ledru +T Jameson Little + Jameson + Little +Tobias Bohumir Schottdorf + Tobias + Bohumir + Schottdorf +Virgile Andreani + Virgile + Andreani +Vsevolod Velichko + Vsevolod + Velichko +Wiktor Kuropatwa + Wiktor + Kuropatwa +Yury Krivopalov + Yury + Krivopalov + +KokaKiwi +Mahkoh +Smigle00 + Smigle00 + Smigle +anonymousknight +kwantam +nicoo diff --git a/.vscode/cspell.dictionaries/shell.wordlist.txt b/.vscode/cspell.dictionaries/shell.wordlist.txt new file mode 100644 index 000000000..d8f297d21 --- /dev/null +++ b/.vscode/cspell.dictionaries/shell.wordlist.txt @@ -0,0 +1,93 @@ +# * Mac +clonefile + +# * POSIX +TMPDIR +adduser +csh +globstar +inotify +localtime +mountinfo +mountpoint +mtab +nullglob +passwd +pipefail +popd +ptmx +pushd +setarch +sh +sudo +sudoedit +tcsh +tzselect +urandom +wtmp +zsh + +# * Windows +APPDATA +COMSPEC +HKCU +HKLM +HOMEDRIVE +HOMEPATH +LOCALAPPDATA +PATHEXT +PATHEXT +SYSTEMROOT +USERDOMAIN +USERNAME +USERPROFILE +procmon + +# * `git` +gitattributes +gitignore + +# * `make` (`gmake`) +CURDIR +GNUMAKEFLAGS +GNUMakefile +LIBPATTERNS +MAKECMDGOALS +MAKEFILES +MAKEFLAGS +MAKELEVEL +MAKESHELL +SHELLSTATUS +VPATH +abspath +addprefix +addsuffix +endef +firstword +ifeq +ifneq +lastword +notdir +patsubst + + +# * `npm` +preversion + +# * utilities +cachegrind +chglog +codespell +commitlint +dprint +dtrace +gcov +gmake +grcov +grep +markdownlint +rerast +rollup +sed +wslpath +xargs diff --git a/.vscode/cspell.dictionaries/workspace.wordlist.txt b/.vscode/cspell.dictionaries/workspace.wordlist.txt new file mode 100644 index 000000000..7242199a5 --- /dev/null +++ b/.vscode/cspell.dictionaries/workspace.wordlist.txt @@ -0,0 +1,300 @@ +# * cargo +cdylib +rlib + +# * crates +advapi +advapi32-sys +aho-corasick +backtrace +blake2b_simd +bstr +byteorder +chacha +chrono +conv +corasick +crossterm +filetime +formatteriteminfo +fsext +getopts +getrandom +globset +itertools +lscolors +memchr +multifilereader +onig +ouroboros +peekreader +quickcheck +rand_chacha +ringbuffer +rlimit +smallvec +tempdir +tempfile +termion +termios +termsize +termwidth +textwrap +thiserror +walkdir +winapi +xattr + +# * rust/rustc +RUSTDOCFLAGS +RUSTFLAGS +clippy +rustc +rustfmt +rustup +# +bitor # BitOr trait function +bitxor # BitXor trait function +concat +fract +powi +println +repr +rfind +struct +structs +substr +splitn +trunc + +# * uutils +chcon +chgrp +chmod +chown +chroot +cksum +csplit +dircolors +hashsum +hostid +logname +mkdir +mkfifo +mknod +mktemp +nohup +nproc +numfmt +pathchk +printenv +printf +readlink +realpath +relpath +rmdir +runcon +shuf +sprintf +stdbuf +stty +tsort +uname +unexpand +whoami + +# * vars/errno +errno +EEXIST +ENOENT +ENOSYS +EPERM +EOPNOTSUPP + +# * vars/fcntl +F_GETFL + GETFL +fcntl +vmsplice + +# * vars/libc +FILENO +HOSTSIZE +IDSIZE +IFBLK +IFCHR +IFDIR +IFIFO +IFLNK +IFMT +IFREG +IFSOCK +IRGRP +IROTH +IRUSR +ISGID +ISUID +ISVTX +IWGRP +IWOTH +IWUSR +IXGRP +IXOTH +IXUSR +LINESIZE +NAMESIZE +RTLD_NEXT + RTLD +SIGINT +SIGKILL +SIGTERM +SYS_fdatasync +SYS_syncfs +USERSIZE +addrinfo +addrlen +blocksize +canonname +chroot +dlsym +fdatasync +freeaddrinfo +getaddrinfo +getegid +geteuid +getgid +getgrgid +getgrnam +getgrouplist +getgroups +getpwnam +getpwuid +getuid +inode +inodes +isatty +lchown +setgid +setgroups +settime +setuid +socktype +statfs +statvfs +strcmp +strerror +syncfs +umask +waitpid +wcslen + +# * vars/nix +iovec +unistd + +# * vars/signals +SIGPIPE + +# * vars/std +CString +pathbuf + +# * vars/stat +bavail +bfree +bsize +ffree +frsize +fsid +fstat +fstype +namelen +# unix::fs::MetadataExt +atime # access time +blksize # blocksize for file system I/O +blocks # number of blocks allocated to file +ctime # creation time +dev # ID of device containing the file +gid # group ID of file owner +ino # inode number +mode # permissions +mtime # modification time +nlink # number of hard links to file +rdev # device ID if file is a character/block special file +size # total size of file in bytes +uid # user ID of file owner +nsec # nanosecond measurement scale +# freebsd::MetadataExt +iosize + +# * vars/time +Timespec +isdst +nanos +nsec +nsecs +strftime +strptime +subsec +usec +usecs +utcoff + +# * vars/utmpx +endutxent +getutxent +getutxid +getutxline +pututxline +setutxent +utmp +utmpx +utmpxname + +# * vars/winapi +DWORD +SYSTEMTIME +LPVOID +LPWSTR +ULONG +ULONGLONG +UNLEN +WCHAR +errhandlingapi +fileapi +handleapi +lmcons +minwinbase +minwindef +processthreadsapi +synchapi +sysinfoapi +winbase +winerror +winnt +winsock + +# * vars/uucore +optflag +optflagmulti +optflagopt +optmulti +optopt + +# * uutils +ccmd +coreopts +coreutils +keepenv +libc +libstdbuf +musl +tmpd +ucmd +ucommand +utmpx +uucore +uucore_procs +uumain +uutil +uutils diff --git a/.vscode/extensions.json b/.vscode/extensions.json index cb28d8883..46b105d37 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,10 +1,12 @@ { - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format + // spell-checker:ignore (misc) matklad + // see for the documentation about the extensions.json format "recommendations": [ // Rust language support. "rust-lang.rust", // Provides support for rust-analyzer: novel LSP server for the Rust programming language. - "matklad.rust-analyzer" + "matklad.rust-analyzer", + // `cspell` spell-checker support + "streetsidesoftware.code-spell-checker" ] } diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..d196c6e95 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socioeconomic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +sylvestre@debian.org. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3c40e5dfd..47793977e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,10 +18,12 @@ search the issues to make sure no one else is working on it. ## Best practices 1. Follow what GNU is doing in term of options and behavior. +1. If possible, look at the GNU test suite execution in the CI and make the test work if failing. 1. Use clap for argument management. 1. Make sure that the code coverage is covering all of the cases, including errors. 1. The code must be clippy-warning-free and rustfmt-compliant. 1. Don't hesitate to move common functions into uucore if they can be reused by other binaries. +1. Unsafe code should be documented with Safety comments. ## Commit messages @@ -69,10 +71,6 @@ lines for non-utility modules include: README: add help ``` -``` -travis: fix build -``` - ``` uucore: add new modules ``` diff --git a/Cargo.lock b/Cargo.lock index 314c7559e..1d7634aa2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,22 +1,18 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "advapi32-sys" -version = "0.2.0" +name = "Inflector" +version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e06588080cb19d0acb6739808aafa5f26bfb2ca015b2b6370028b44cf7cb8a9a" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" [[package]] name = "aho-corasick" -version = "0.7.15" +version = "0.7.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" dependencies = [ - "memchr 2.3.4", + "memchr 2.4.0", ] [[package]] @@ -29,14 +25,26 @@ dependencies = [ ] [[package]] -name = "arrayvec" -version = "0.4.12" +name = "ansi_term" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ - "nodrop", + "winapi 0.3.9", ] +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "atty" version = "0.2.14" @@ -54,6 +62,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "binary-heap-plus" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f068638f8ff9e118a9361e66a411eff410e7fb3ecaa23bf9272324f8fc606d7" +dependencies = [ + "compare", +] + [[package]] name = "bit-set" version = "0.5.2" @@ -69,12 +86,6 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" -[[package]] -name = "bitflags" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" - [[package]] name = "bitflags" version = "1.2.1" @@ -82,11 +93,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] -name = "blake2-rfc" -version = "0.2.18" +name = "blake2b_simd" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" +checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587" dependencies = [ + "arrayref", "arrayvec", "constant_time_eq", ] @@ -103,22 +115,15 @@ dependencies = [ [[package]] name = "bstr" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a40b47ad93e1a5404e6c18dec46b628214fee441c70f4ab5d6942142cc268a3d" +checksum = "90682c8d613ad3373e66de8c6411e0ae2ab2571e879d2efbf73558cc66f21279" dependencies = [ "lazy_static", - "memchr 2.3.4", + "memchr 2.4.0", "regex-automata", - "serde", ] -[[package]] -name = "bumpalo" -version = "3.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" - [[package]] name = "byte-tools" version = "0.2.0" @@ -136,24 +141,15 @@ dependencies = [ [[package]] name = "byteorder" -version = "1.3.4" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" - -[[package]] -name = "cast" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0" -dependencies = [ - "rustc_version", -] +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "cc" -version = "1.0.61" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed67cbde08356238e75fc4656be4749481eeffb09e19f320a25237d5221c985d" +checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" [[package]] name = "cfg-if" @@ -169,13 +165,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.11" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" dependencies = [ + "libc", "num-integer", "num-traits", "time", + "winapi 0.3.9", ] [[package]] @@ -184,9 +182,9 @@ version = "2.33.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" dependencies = [ - "ansi_term", + "ansi_term 0.11.0", "atty", - "bitflags 1.2.1", + "bitflags", "strsim", "textwrap", "unicode-width", @@ -199,9 +197,15 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" dependencies = [ - "bitflags 1.2.1", + "bitflags", ] +[[package]] +name = "compare" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "120133d4db2ec47efe2e26502ee984747630c67f51974fca0b6c1340cf2368d3" + [[package]] name = "constant_time_eq" version = "0.1.5" @@ -219,23 +223,24 @@ dependencies = [ [[package]] name = "coreutils" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "byteorder", - "cc", + "atty", + "chrono", + "clap", "conv", "filetime", "glob 0.3.0", "lazy_static", "libc", + "nix 0.20.0", + "pretty_assertions", "rand 0.7.3", "regex", - "rustc-demangle", - "same-file", + "rlimit", "sha1", "tempfile", "textwrap", - "thread_local", "time", "unindent", "unix_socket", @@ -296,6 +301,7 @@ dependencies = [ "uu_paste", "uu_pathchk", "uu_pinky", + "uu_pr", "uu_printenv", "uu_printf", "uu_ptx", @@ -337,7 +343,17 @@ dependencies = [ "uu_whoami", "uu_yes", "uucore", - "winapi-util", + "walkdir", +] + +[[package]] +name = "coz" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cef55b3fe2f5477d59e12bc792e8b3c95a25bd099eadcfae006ecea136de76e2" +dependencies = [ + "libc", + "once_cell", ] [[package]] @@ -433,47 +449,11 @@ dependencies = [ "unicode-xid 0.0.4", ] -[[package]] -name = "criterion" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab327ed7354547cc2ef43cbe20ef68b988e70b4b593cbd66a2a61733123a3d23" -dependencies = [ - "atty", - "cast", - "clap", - "criterion-plot", - "csv", - "itertools 0.10.0", - "lazy_static", - "num-traits", - "oorandom", - "plotters", - "rayon", - "regex", - "serde", - "serde_cbor", - "serde_derive", - "serde_json", - "tinytemplate", - "walkdir", -] - -[[package]] -name = "criterion-plot" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e022feadec601fba1649cfa83586381a4ad31c6bf3a9ab7d408118b05dd9889d" -dependencies = [ - "cast", - "itertools 0.9.0", -] - [[package]] name = "crossbeam-channel" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" +checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4" dependencies = [ "cfg-if 1.0.0", "crossbeam-utils", @@ -492,9 +472,9 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12" +checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd" dependencies = [ "cfg-if 1.0.0", "crossbeam-utils", @@ -505,35 +485,47 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.3" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" +checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db" dependencies = [ - "autocfg", "cfg-if 1.0.0", "lazy_static", ] [[package]] -name = "csv" -version = "1.1.6" +name = "crossterm" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" +checksum = "c0ebde6a9dd5e331cd6c6f48253254d117642c31653baa475e394657c59c1f7d" dependencies = [ - "bstr", - "csv-core", - "itoa", - "ryu", - "serde", + "bitflags", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi 0.3.9", ] [[package]] -name = "csv-core" -version = "0.1.10" +name = "crossterm_winapi" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +checksum = "3a6966607622438301997d3dac0d2f6e9a90c68bb6bc1785ea98456ab93c0507" dependencies = [ - "memchr 2.3.4", + "winapi 0.3.9", +] + +[[package]] +name = "ctor" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e98e2ad1a782e33928b96fc3948e7c355e5af34ba4de7670fe8bac2a3b2006d" +dependencies = [ + "quote 1.0.9", + "syn", ] [[package]] @@ -554,6 +546,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f215f9b7224f49fb73256115331f677d868b34d18b65dbe4db392e6021eea90" +[[package]] +name = "diff" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" + [[package]] name = "digest" version = "0.6.2" @@ -564,10 +562,22 @@ dependencies = [ ] [[package]] -name = "dunce" -version = "1.0.1" +name = "dns-lookup" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2641c4a7c0c4101df53ea572bffdc561c146f6c2eb09e4df02bc4811e3feeb4" +checksum = "093d88961fd18c4ecacb8c80cd0b356463ba941ba11e0e01f9cf5271380b79dc" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "socket2", + "winapi 0.3.9", +] + +[[package]] +name = "dunce" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453440c271cf5577fd2a40e4942540cb7d0d2f85e27c8d07dd0023c925a67541" [[package]] name = "either" @@ -591,6 +601,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "file_diff" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31a7a908b8f32538a2143e59a6e4e2508988832d5d4d6f7c156b3cbc762643a5" + [[package]] name = "filetime" version = "0.2.14" @@ -599,7 +615,7 @@ checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8" dependencies = [ "cfg-if 1.0.0", "libc", - "redox_syscall 0.2.5", + "redox_syscall 0.2.9", "winapi 0.3.9", ] @@ -680,6 +696,19 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "globset" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10463d9ff00a2a068db14231982f5132edebad0d7660cd956a1c30292dbcbfbd" +dependencies = [ + "aho-corasick", + "bstr", + "fnv", + "log", + "regex", +] + [[package]] name = "half" version = "1.7.1" @@ -687,10 +716,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3" [[package]] -name = "hermit-abi" -version = "0.1.18" +name = "heck" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] @@ -718,6 +756,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46dbcb333e86939721589d25a3557e180b52778cb33c7fdfe9e0158ff790d5ec" +[[package]] +name = "instant" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "ioctl-sys" version = "0.5.2" @@ -735,37 +782,13 @@ dependencies = [ [[package]] name = "itertools" -version = "0.9.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" +checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" dependencies = [ "either", ] -[[package]] -name = "itertools" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319" -dependencies = [ - "either", -] - -[[package]] -name = "itoa" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" - -[[package]] -name = "js-sys" -version = "0.3.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc9f84f9b115ce7843d60706df1422a916680bfdfcbdb0447c5614ff9d7e4d78" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "kernel32-sys" version = "0.2.2" @@ -788,6 +811,24 @@ version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" +[[package]] +name = "locale" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fdbe492a9c0238da900a1165c42fc5067161ce292678a6fe80921f30fe307fd" +dependencies = [ + "libc", +] + +[[package]] +name = "lock_api" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.14" @@ -797,6 +838,15 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "lscolors" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d24b894c45c9da468621cdd615a5a79ee5e5523dd4f75c76ebc03d458940c16e" +dependencies = [ + "ansi_term 0.12.1", +] + [[package]] name = "match_cfg" version = "0.1.0" @@ -826,29 +876,39 @@ dependencies = [ [[package]] name = "memchr" -version = "2.3.4" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" [[package]] name = "memoffset" -version = "0.6.1" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" +checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" dependencies = [ "autocfg", ] [[package]] -name = "nix" -version = "0.8.1" +name = "mio" +version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47e49f6982987135c5e9620ab317623e723bd06738fd85377e8d55f57c8b6487" +checksum = "e50ae3f04d169fcc9bde0b547d1c205219b7157e07ded9c5aff03e0637cb3ed7" dependencies = [ - "bitflags 0.7.0", - "cfg-if 0.1.10", "libc", - "void", + "log", + "miow", + "ntapi", + "winapi 0.3.9", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi 0.3.9", ] [[package]] @@ -857,19 +917,51 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dbdc256eaac2e3bd236d93ad999d3479ef775c863dbda3068c4006a92eec51b" dependencies = [ - "bitflags 1.2.1", + "bitflags", "cc", "cfg-if 0.1.10", "libc", "void", ] +[[package]] +name = "nix" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa9b4819da1bc61c0ea48b63b7bc8604064dd43013e7cc325df098d49cd7c18a" +dependencies = [ + "bitflags", + "cc", + "cfg-if 1.0.0", + "libc", +] + [[package]] name = "nodrop" version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi 0.3.9", +] + +[[package]] +name = "num-bigint" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d047c1062aa51e256408c560894e5251f08925980e53cf1aa5bd00eec6512" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -911,13 +1003,19 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" +[[package]] +name = "once_cell" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" + [[package]] name = "onig" version = "4.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8518fcb2b1b8c2f45f0ad499df4fda6087fc3475ca69a185c173b8315d2fb383" dependencies = [ - "bitflags 1.2.1", + "bitflags", "lazy_static", "libc", "onig_sys", @@ -934,10 +1032,61 @@ dependencies = [ ] [[package]] -name = "oorandom" -version = "11.1.3" +name = "ouroboros" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +checksum = "fbeff60e3e37407a80ead3e9458145b456e978c4068cddbfea6afb48572962ca" +dependencies = [ + "ouroboros_macro", + "stable_deref_trait", +] + +[[package]] +name = "ouroboros_macro" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03f2cb802b5bdfdf52f1ffa0b54ce105e4d346e91990dd571f86c91321ad49e2" +dependencies = [ + "Inflector", + "proc-macro-error", + "proc-macro2", + "quote 1.0.9", + "syn", +] + +[[package]] +name = "output_vt100" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" +dependencies = [ + "winapi 0.3.9", +] + +[[package]] +name = "parking_lot" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +dependencies = [ + "cfg-if 1.0.0", + "instant", + "libc", + "redox_syscall 0.2.9", + "smallvec 1.6.1", + "winapi 0.3.9", +] [[package]] name = "paste" @@ -974,40 +1123,48 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "plotters" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45ca0ae5f169d0917a7c7f5a9c1a3d3d9598f18f529dd2b8373ed988efea307a" -dependencies = [ - "num-traits", - "plotters-backend", - "plotters-svg", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "plotters-backend" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b07fffcddc1cb3a1de753caa4e4df03b79922ba43cf882acc1bdd7e8df9f4590" - -[[package]] -name = "plotters-svg" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b38a02e23bd9604b842a812063aec4ef702b57989c37b655254bb61c471ad211" -dependencies = [ - "plotters-backend", -] - [[package]] name = "ppv-lite86" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "pretty_assertions" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cab0e7c02cf376875e9335e0ba1da535775beb5450d21e1dffca068818ed98b" +dependencies = [ + "ansi_term 0.12.1", + "ctor", + "diff", + "output_vt100", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote 1.0.9", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote 1.0.9", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -1016,11 +1173,11 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" dependencies = [ - "unicode-xid 0.2.1", + "unicode-xid 0.2.2", ] [[package]] @@ -1029,6 +1186,12 @@ version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + [[package]] name = "quickcheck" version = "0.9.2" @@ -1177,9 +1340,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" +checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" dependencies = [ "autocfg", "crossbeam-deque", @@ -1189,9 +1352,9 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.9.0" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" +checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" dependencies = [ "crossbeam-channel", "crossbeam-deque", @@ -1208,11 +1371,11 @@ checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" [[package]] name = "redox_syscall" -version = "0.2.5" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9" +checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" dependencies = [ - "bitflags 1.2.1", + "bitflags", ] [[package]] @@ -1221,34 +1384,31 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f" dependencies = [ - "redox_syscall 0.2.5", + "redox_syscall 0.2.9", ] [[package]] name = "regex" -version = "1.4.4" +version = "1.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54fd1046a3107eb58f42de31d656fee6853e5d276c455fd943742dce89fc3dd3" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" dependencies = [ "aho-corasick", - "memchr 2.3.4", + "memchr 2.4.0", "regex-syntax", ] [[package]] name = "regex-automata" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" -dependencies = [ - "byteorder", -] +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" [[package]] name = "regex-syntax" -version = "0.6.23" +version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" [[package]] name = "remove_dir_all" @@ -1259,38 +1419,33 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "retain_mut" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9c17925a9027d298a4603d286befe3f9dc0e8ed02523141914eb628798d6e5b" + +[[package]] +name = "rlimit" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49b02d62c38353a6fce45c25ca19783e25dd5f495ca681c674a4ee15aa4c1536" +dependencies = [ + "cfg-if 0.1.10", + "libc", +] + [[package]] name = "rust-ini" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2" -[[package]] -name = "rustc-demangle" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" - -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver", -] - -[[package]] -name = "ryu" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" - [[package]] name = "same-file" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "585e8ddcedc187886a30fa705c47985c3fa88d06624095856b36ca0b82ff4421" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] @@ -1316,44 +1471,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -[[package]] -name = "serde" -version = "1.0.124" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" - -[[package]] -name = "serde_cbor" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" -dependencies = [ - "half", - "serde", -] - -[[package]] -name = "serde_derive" -version = "1.0.124" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" -dependencies = [ - "itoa", - "ryu", - "serde", -] - [[package]] name = "sha1" version = "0.6.0" @@ -1395,6 +1512,17 @@ dependencies = [ "signal-hook-registry", ] +[[package]] +name = "signal-hook-mio" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29fd5867f1c4f2c5be079aee7a2adf1152ebb04a4bc4d341f504b7dece607ed4" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -1413,6 +1541,29 @@ dependencies = [ "maybe-uninit", ] +[[package]] +name = "smallvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" + +[[package]] +name = "socket2" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "strsim" version = "0.8.0" @@ -1420,26 +1571,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] -name = "syn" -version = "1.0.64" +name = "strum" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fd9d1e9976102a03c542daa2eff1b43f9d72306342f3f8b3ed5fb8908195d6f" +checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c" + +[[package]] +name = "strum_macros" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149" +dependencies = [ + "heck", + "proc-macro2", + "quote 1.0.9", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" dependencies = [ "proc-macro2", "quote 1.0.9", - "unicode-xid 0.2.1", + "unicode-xid 0.2.2", ] [[package]] name = "tempfile" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "libc", - "rand 0.7.3", - "redox_syscall 0.1.57", + "rand 0.8.4", + "redox_syscall 0.2.9", "remove_dir_all", "winapi 0.3.9", ] @@ -1471,7 +1640,7 @@ checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e" dependencies = [ "libc", "numtoa", - "redox_syscall 0.2.5", + "redox_syscall 0.2.9", "redox_termios", ] @@ -1500,60 +1669,46 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.24" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" +checksum = "fa6f76457f59514c7eeb4e59d891395fab0b2fd1d40723ae737d64153392e9c6" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.24" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" +checksum = "8a36768c0fbf1bb15eca10defa29526bda730a2376c2ab4393ccfa16fb1a318d" dependencies = [ "proc-macro2", "quote 1.0.9", "syn", ] -[[package]] -name = "thread_local" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb9bc092d0d51e76b2b19d9d85534ffc9ec2db959a2523cdae0697e2972cd447" -dependencies = [ - "lazy_static", -] - [[package]] name = "time" -version = "0.1.42" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" dependencies = [ "libc", - "redox_syscall 0.1.57", "winapi 0.3.9", ] -[[package]] -name = "tinytemplate" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" -dependencies = [ - "serde", - "serde_json", -] - [[package]] name = "typenum" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" +[[package]] +name = "unicode-segmentation" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" + [[package]] name = "unicode-width" version = "0.1.8" @@ -1568,9 +1723,9 @@ checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" [[package]] name = "unicode-xid" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "unindent" @@ -1606,8 +1761,9 @@ checksum = "7cf7d77f457ef8dfa11e4cd5933c5ddb5dc52a94664071951219a97710f0a32b" [[package]] name = "uu_arch" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "platform-info", "uucore", "uucore_procs", @@ -1615,33 +1771,40 @@ dependencies = [ [[package]] name = "uu_base32" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_base64" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", + "uu_base32", "uucore", "uucore_procs", ] [[package]] name = "uu_basename" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_cat" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "quick-error", + "atty", + "clap", + "nix 0.20.0", + "thiserror", "unix_socket", "uucore", "uucore_procs", @@ -1649,8 +1812,9 @@ dependencies = [ [[package]] name = "uu_chgrp" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", "walkdir", @@ -1658,8 +1822,9 @@ dependencies = [ [[package]] name = "uu_chmod" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "uucore", "uucore_procs", @@ -1668,7 +1833,7 @@ dependencies = [ [[package]] name = "uu_chown" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "glob 0.3.0", @@ -1679,17 +1844,18 @@ dependencies = [ [[package]] name = "uu_chroot" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_cksum" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "uucore", "uucore_procs", @@ -1697,9 +1863,9 @@ dependencies = [ [[package]] name = "uu_comm" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", "uucore", "uucore_procs", @@ -1707,13 +1873,13 @@ dependencies = [ [[package]] name = "uu_cp" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "filetime", "ioctl-sys", "libc", - "quick-error", + "quick-error 1.2.3", "uucore", "uucore_procs", "walkdir", @@ -1723,9 +1889,9 @@ dependencies = [ [[package]] name = "uu_csplit" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "glob 0.2.11", "regex", "thiserror", @@ -1735,20 +1901,26 @@ dependencies = [ [[package]] name = "uu_cut" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "atty", + "bstr", + "clap", + "memchr 2.4.0", "uucore", "uucore_procs", ] [[package]] name = "uu_date" -version = "0.0.4" +version = "0.0.6" dependencies = [ "chrono", "clap", + "libc", "uucore", "uucore_procs", + "winapi 0.3.9", ] [[package]] @@ -1769,20 +1941,19 @@ dependencies = [ [[package]] name = "uu_df" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", - "libc", "number_prefix", "uucore", "uucore_procs", - "winapi 0.3.9", ] [[package]] name = "uu_dircolors" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "glob 0.3.0", "uucore", "uucore_procs", @@ -1790,8 +1961,9 @@ dependencies = [ [[package]] name = "uu_dirname" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "uucore", "uucore_procs", @@ -1799,24 +1971,27 @@ dependencies = [ [[package]] name = "uu_du" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "time", + "chrono", + "clap", "uucore", "uucore_procs", + "winapi 0.3.9", ] [[package]] name = "uu_echo" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_env" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -1827,9 +2002,9 @@ dependencies = [ [[package]] name = "uu_expand" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "unicode-width", "uucore", "uucore_procs", @@ -1837,9 +2012,12 @@ dependencies = [ [[package]] name = "uu_expr" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", + "num-bigint", + "num-traits", "onig", "uucore", "uucore_procs", @@ -1847,30 +2025,31 @@ dependencies = [ [[package]] name = "uu_factor" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "criterion", + "clap", + "coz", "num-traits", "paste", "quickcheck", "rand 0.7.3", - "rand_chacha 0.2.2", - "smallvec", + "smallvec 0.6.14", "uucore", "uucore_procs", ] [[package]] name = "uu_false" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_fmt" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -1881,15 +2060,16 @@ dependencies = [ [[package]] name = "uu_fold" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_groups" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -1898,9 +2078,9 @@ dependencies = [ [[package]] name = "uu_hashsum" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "blake2-rfc", + "blake2b_simd", "clap", "digest", "hex", @@ -1917,17 +2097,18 @@ dependencies = [ [[package]] name = "uu_head" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "libc", + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_hostid" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "uucore", "uucore_procs", @@ -1935,7 +2116,7 @@ dependencies = [ [[package]] name = "uu_hostname" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "hostname", @@ -1947,7 +2128,7 @@ dependencies = [ [[package]] name = "uu_id" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -1956,9 +2137,11 @@ dependencies = [ [[package]] name = "uu_install" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", + "file_diff", + "filetime", "libc", "time", "uucore", @@ -1967,7 +2150,7 @@ dependencies = [ [[package]] name = "uu_join" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -1976,8 +2159,9 @@ dependencies = [ [[package]] name = "uu_kill" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "uucore", "uucore_procs", @@ -1985,8 +2169,9 @@ dependencies = [ [[package]] name = "uu_link" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "uucore", "uucore_procs", @@ -1994,7 +2179,7 @@ dependencies = [ [[package]] name = "uu_ln" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -2004,8 +2189,9 @@ dependencies = [ [[package]] name = "uu_logname" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "uucore", "uucore_procs", @@ -2013,15 +2199,19 @@ dependencies = [ [[package]] name = "uu_ls" -version = "0.0.4" +version = "0.0.6" dependencies = [ "atty", - "getopts", + "chrono", + "clap", + "globset", "lazy_static", + "locale", + "lscolors", "number_prefix", + "once_cell", "term_grid", "termsize", - "time", "unicode-width", "uucore", "uucore_procs", @@ -2029,7 +2219,7 @@ dependencies = [ [[package]] name = "uu_mkdir" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -2039,9 +2229,9 @@ dependencies = [ [[package]] name = "uu_mkfifo" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", "uucore", "uucore_procs", @@ -2049,9 +2239,9 @@ dependencies = [ [[package]] name = "uu_mknod" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", "uucore", "uucore_procs", @@ -2059,7 +2249,7 @@ dependencies = [ [[package]] name = "uu_mktemp" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "rand 0.5.6", @@ -2070,19 +2260,23 @@ dependencies = [ [[package]] name = "uu_more" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", - "nix 0.8.1", + "atty", + "clap", + "crossterm", + "nix 0.13.1", "redox_syscall 0.1.57", "redox_termios", + "unicode-segmentation", + "unicode-width", "uucore", "uucore_procs", ] [[package]] name = "uu_mv" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "fs_extra", @@ -2092,22 +2286,23 @@ dependencies = [ [[package]] name = "uu_nice" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", + "nix 0.13.1", "uucore", "uucore_procs", ] [[package]] name = "uu_nl" -version = "0.0.4" +version = "0.0.6" dependencies = [ "aho-corasick", - "getopts", + "clap", "libc", - "memchr 2.3.4", + "memchr 2.4.0", "regex", "regex-syntax", "uucore", @@ -2116,9 +2311,10 @@ dependencies = [ [[package]] name = "uu_nohup" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "atty", + "clap", "libc", "uucore", "uucore_procs", @@ -2126,7 +2322,7 @@ dependencies = [ [[package]] name = "uu_nproc" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -2137,7 +2333,7 @@ dependencies = [ [[package]] name = "uu_numfmt" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2146,10 +2342,10 @@ dependencies = [ [[package]] name = "uu_od" -version = "0.0.4" +version = "0.0.6" dependencies = [ "byteorder", - "getopts", + "clap", "half", "libc", "uucore", @@ -2158,7 +2354,7 @@ dependencies = [ [[package]] name = "uu_paste" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2167,9 +2363,9 @@ dependencies = [ [[package]] name = "uu_pathchk" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", "uucore", "uucore_procs", @@ -2177,15 +2373,31 @@ dependencies = [ [[package]] name = "uu_pinky" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", + "uucore", + "uucore_procs", +] + +[[package]] +name = "uu_pr" +version = "0.0.6" +dependencies = [ + "chrono", + "clap", + "getopts", + "itertools 0.10.1", + "quick-error 2.0.1", + "regex", + "time", "uucore", "uucore_procs", ] [[package]] name = "uu_printenv" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2194,8 +2406,9 @@ dependencies = [ [[package]] name = "uu_printf" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "itertools 0.8.2", "uucore", "uucore_procs", @@ -2203,12 +2416,12 @@ dependencies = [ [[package]] name = "uu_ptx" -version = "0.0.4" +version = "0.0.6" dependencies = [ "aho-corasick", - "getopts", + "clap", "libc", - "memchr 2.3.4", + "memchr 2.4.0", "regex", "regex-syntax", "uucore", @@ -2217,7 +2430,7 @@ dependencies = [ [[package]] name = "uu_pwd" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2226,7 +2439,7 @@ dependencies = [ [[package]] name = "uu_readlink" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -2236,7 +2449,7 @@ dependencies = [ [[package]] name = "uu_realpath" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2245,27 +2458,28 @@ dependencies = [ [[package]] name = "uu_relpath" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_rm" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "remove_dir_all", "uucore", "uucore_procs", "walkdir", + "winapi 0.3.9", ] [[package]] name = "uu_rmdir" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2274,19 +2488,21 @@ dependencies = [ [[package]] name = "uu_seq" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", + "num-bigint", + "num-traits", "uucore", "uucore_procs", ] [[package]] name = "uu_shred" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "filetime", - "getopts", "libc", "rand 0.5.6", "time", @@ -2296,9 +2512,9 @@ dependencies = [ [[package]] name = "uu_shuf" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "rand 0.5.6", "uucore", "uucore_procs", @@ -2306,7 +2522,7 @@ dependencies = [ [[package]] name = "uu_sleep" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2315,18 +2531,27 @@ dependencies = [ [[package]] name = "uu_sort" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "binary-heap-plus", "clap", - "itertools 0.8.2", + "compare", + "fnv", + "itertools 0.10.1", + "memchr 2.4.0", + "ouroboros", + "rand 0.7.3", + "rayon", "semver", + "tempfile", + "unicode-width", "uucore", "uucore_procs", ] [[package]] name = "uu_split" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2335,19 +2560,18 @@ dependencies = [ [[package]] name = "uu_stat" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", - "time", "uucore", "uucore_procs", ] [[package]] name = "uu_stdbuf" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "tempfile", "uu_stdbuf_libstdbuf", "uucore", @@ -2356,7 +2580,7 @@ dependencies = [ [[package]] name = "uu_stdbuf_libstdbuf" -version = "0.0.4" +version = "0.0.6" dependencies = [ "cpp", "cpp_build", @@ -2367,16 +2591,16 @@ dependencies = [ [[package]] name = "uu_sum" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_sync" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -2387,16 +2611,16 @@ dependencies = [ [[package]] name = "uu_tac" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_tail" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "libc", @@ -2408,18 +2632,20 @@ dependencies = [ [[package]] name = "uu_tee" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", + "retain_mut", "uucore", "uucore_procs", ] [[package]] name = "uu_test" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "libc", "redox_syscall 0.1.57", "uucore", @@ -2428,17 +2654,18 @@ dependencies = [ [[package]] name = "uu_timeout" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", + "nix 0.20.0", "uucore", "uucore_procs", ] [[package]] name = "uu_touch" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "filetime", @@ -2449,26 +2676,27 @@ dependencies = [ [[package]] name = "uu_tr" -version = "0.0.4" +version = "0.0.6" dependencies = [ "bit-set", + "clap", "fnv", - "getopts", "uucore", "uucore_procs", ] [[package]] name = "uu_true" -version = "0.0.4" +version = "0.0.6" dependencies = [ + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_truncate" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2477,18 +2705,19 @@ dependencies = [ [[package]] name = "uu_tsort" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "uucore", "uucore_procs", ] [[package]] name = "uu_tty" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "atty", + "clap", "libc", "uucore", "uucore_procs", @@ -2496,7 +2725,7 @@ dependencies = [ [[package]] name = "uu_uname" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "platform-info", @@ -2506,9 +2735,9 @@ dependencies = [ [[package]] name = "uu_unexpand" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "unicode-width", "uucore", "uucore_procs", @@ -2516,18 +2745,20 @@ dependencies = [ [[package]] name = "uu_uniq" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", + "strum", + "strum_macros", "uucore", "uucore_procs", ] [[package]] name = "uu_unlink" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "getopts", + "clap", "libc", "uucore", "uucore_procs", @@ -2535,7 +2766,7 @@ dependencies = [ [[package]] name = "uu_uptime" -version = "0.0.4" +version = "0.0.6" dependencies = [ "chrono", "clap", @@ -2545,7 +2776,7 @@ dependencies = [ [[package]] name = "uu_users" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2554,26 +2785,29 @@ dependencies = [ [[package]] name = "uu_wc" -version = "0.0.4" +version = "0.0.6" +dependencies = [ + "clap", + "libc", + "nix 0.20.0", + "thiserror", + "uucore", + "uucore_procs", +] + +[[package]] +name = "uu_who" +version = "0.0.6" dependencies = [ "clap", "uucore", "uucore_procs", ] -[[package]] -name = "uu_who" -version = "0.0.4" -dependencies = [ - "uucore", - "uucore_procs", -] - [[package]] name = "uu_whoami" -version = "0.0.4" +version = "0.0.6" dependencies = [ - "advapi32-sys", "clap", "uucore", "uucore_procs", @@ -2582,7 +2816,7 @@ dependencies = [ [[package]] name = "uu_yes" -version = "0.0.4" +version = "0.0.6" dependencies = [ "clap", "uucore", @@ -2591,9 +2825,10 @@ dependencies = [ [[package]] name = "uucore" -version = "0.0.7" +version = "0.0.8" dependencies = [ "data-encoding", + "dns-lookup", "dunce", "getopts", "lazy_static", @@ -2604,6 +2839,7 @@ dependencies = [ "thiserror", "time", "wild", + "winapi 0.3.9", ] [[package]] @@ -2621,6 +2857,12 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + [[package]] name = "void" version = "1.0.2" @@ -2629,9 +2871,9 @@ checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" [[package]] name = "walkdir" -version = "2.3.1" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", "winapi 0.3.9", @@ -2650,70 +2892,6 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" -[[package]] -name = "wasm-bindgen" -version = "0.2.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ee1280240b7c461d6a0071313e08f34a60b0365f14260362e5a2b17d1d31aa7" -dependencies = [ - "cfg-if 1.0.0", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b7d8b6942b8bb3a9b0e73fc79b98095a27de6fa247615e59d096754a3bc2aa8" -dependencies = [ - "bumpalo", - "lazy_static", - "log", - "proc-macro2", - "quote 1.0.9", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ac38da8ef716661f0f36c0d8320b89028efe10c7c0afde65baffb496ce0d3b" -dependencies = [ - "quote 1.0.9", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc053ec74d454df287b9374ee8abb36ffd5acb95ba87da3ba5b7d3fe20eb401e" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d6f8ec44822dd71f5f221a5847fb34acd9060535c1211b70a05844c0f6383b1" - -[[package]] -name = "web-sys" -version = "0.3.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec600b26223b2948cedfde2a0aa6756dcf1fef616f43d7b3097aaf53a6c4d92b" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - [[package]] name = "wild" version = "2.0.4" @@ -2753,9 +2931,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.2" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ "winapi 0.3.9", ] diff --git a/Cargo.toml b/Cargo.toml index 786c66fe3..b3522f454 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "coreutils" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "coreutils ~ GNU coreutils (updated); implemented as universal (cross-platform) utils, written in Rust" @@ -43,6 +43,7 @@ feat_common_core = [ "df", "dircolors", "dirname", + "du", "echo", "env", "expand", @@ -62,8 +63,10 @@ feat_common_core = [ "more", "mv", "nl", + "numfmt", "od", "paste", + "pr", "printenv", "printf", "ptx", @@ -149,7 +152,6 @@ feat_require_unix = [ "chmod", "chown", "chroot", - "du", "dd", "groups", "hostid", @@ -160,7 +162,6 @@ feat_require_unix = [ "mkfifo", "mknod", "nice", - "numfmt", "nohup", "pathchk", "stat", @@ -225,135 +226,142 @@ test = [ "uu_test" ] [workspace] [dependencies] +clap = "2.33.3" 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.7", package="uucore", path="src/uucore" } +uucore = { version=">=0.0.8", package="uucore", path="src/uucore" } # * uutils -uu_test = { optional=true, version="0.0.4", package="uu_test", path="src/uu/test" } +uu_test = { optional=true, version="0.0.6", package="uu_test", path="src/uu/test" } # -arch = { optional=true, version="0.0.4", package="uu_arch", path="src/uu/arch" } -base32 = { optional=true, version="0.0.4", package="uu_base32", path="src/uu/base32" } -base64 = { optional=true, version="0.0.4", package="uu_base64", path="src/uu/base64" } -basename = { optional=true, version="0.0.4", package="uu_basename", path="src/uu/basename" } -cat = { optional=true, version="0.0.4", package="uu_cat", path="src/uu/cat" } -chgrp = { optional=true, version="0.0.4", package="uu_chgrp", path="src/uu/chgrp" } -chmod = { optional=true, version="0.0.4", package="uu_chmod", path="src/uu/chmod" } -chown = { optional=true, version="0.0.4", package="uu_chown", path="src/uu/chown" } -chroot = { optional=true, version="0.0.4", package="uu_chroot", path="src/uu/chroot" } -cksum = { optional=true, version="0.0.4", package="uu_cksum", path="src/uu/cksum" } -comm = { optional=true, version="0.0.4", package="uu_comm", path="src/uu/comm" } -cp = { optional=true, version="0.0.4", package="uu_cp", path="src/uu/cp" } -csplit = { optional=true, version="0.0.4", package="uu_csplit", path="src/uu/csplit" } -cut = { optional=true, version="0.0.4", package="uu_cut", path="src/uu/cut" } -date = { optional=true, version="0.0.4", package="uu_date", path="src/uu/date" } -df = { optional=true, version="0.0.4", package="uu_df", path="src/uu/df" } -dircolors= { optional=true, version="0.0.4", package="uu_dircolors", path="src/uu/dircolors" } -dirname = { optional=true, version="0.0.4", package="uu_dirname", path="src/uu/dirname" } -du = { optional=true, version="0.0.4", package="uu_du", path="src/uu/du" } +arch = { optional=true, version="0.0.6", package="uu_arch", path="src/uu/arch" } +base32 = { optional=true, version="0.0.6", package="uu_base32", path="src/uu/base32" } +base64 = { optional=true, version="0.0.6", package="uu_base64", path="src/uu/base64" } +basename = { optional=true, version="0.0.6", package="uu_basename", path="src/uu/basename" } +cat = { optional=true, version="0.0.6", package="uu_cat", path="src/uu/cat" } +chgrp = { optional=true, version="0.0.6", package="uu_chgrp", path="src/uu/chgrp" } +chmod = { optional=true, version="0.0.6", package="uu_chmod", path="src/uu/chmod" } +chown = { optional=true, version="0.0.6", package="uu_chown", path="src/uu/chown" } +chroot = { optional=true, version="0.0.6", package="uu_chroot", path="src/uu/chroot" } +cksum = { optional=true, version="0.0.6", package="uu_cksum", path="src/uu/cksum" } +comm = { optional=true, version="0.0.6", package="uu_comm", path="src/uu/comm" } +cp = { optional=true, version="0.0.6", package="uu_cp", path="src/uu/cp" } +csplit = { optional=true, version="0.0.6", package="uu_csplit", path="src/uu/csplit" } +cut = { optional=true, version="0.0.6", package="uu_cut", path="src/uu/cut" } +date = { optional=true, version="0.0.6", package="uu_date", path="src/uu/date" } dd = { optional=true, version="0.0.4", package="uu_dd", path="src/uu/dd" } -echo = { optional=true, version="0.0.4", package="uu_echo", path="src/uu/echo" } -env = { optional=true, version="0.0.4", package="uu_env", path="src/uu/env" } -expand = { optional=true, version="0.0.4", package="uu_expand", path="src/uu/expand" } -expr = { optional=true, version="0.0.4", package="uu_expr", path="src/uu/expr" } -factor = { optional=true, version="0.0.4", package="uu_factor", path="src/uu/factor" } -false = { optional=true, version="0.0.4", package="uu_false", path="src/uu/false" } -fmt = { optional=true, version="0.0.4", package="uu_fmt", path="src/uu/fmt" } -fold = { optional=true, version="0.0.4", package="uu_fold", path="src/uu/fold" } -groups = { optional=true, version="0.0.4", package="uu_groups", path="src/uu/groups" } -hashsum = { optional=true, version="0.0.4", package="uu_hashsum", path="src/uu/hashsum" } -head = { optional=true, version="0.0.4", package="uu_head", path="src/uu/head" } -hostid = { optional=true, version="0.0.4", package="uu_hostid", path="src/uu/hostid" } -hostname = { optional=true, version="0.0.4", package="uu_hostname", path="src/uu/hostname" } -id = { optional=true, version="0.0.4", package="uu_id", path="src/uu/id" } -install = { optional=true, version="0.0.4", package="uu_install", path="src/uu/install" } -join = { optional=true, version="0.0.4", package="uu_join", path="src/uu/join" } -kill = { optional=true, version="0.0.4", package="uu_kill", path="src/uu/kill" } -link = { optional=true, version="0.0.4", package="uu_link", path="src/uu/link" } -ln = { optional=true, version="0.0.4", package="uu_ln", path="src/uu/ln" } -ls = { optional=true, version="0.0.4", package="uu_ls", path="src/uu/ls" } -logname = { optional=true, version="0.0.4", package="uu_logname", path="src/uu/logname" } -mkdir = { optional=true, version="0.0.4", package="uu_mkdir", path="src/uu/mkdir" } -mkfifo = { optional=true, version="0.0.4", package="uu_mkfifo", path="src/uu/mkfifo" } -mknod = { optional=true, version="0.0.4", package="uu_mknod", path="src/uu/mknod" } -mktemp = { optional=true, version="0.0.4", package="uu_mktemp", path="src/uu/mktemp" } -more = { optional=true, version="0.0.4", package="uu_more", path="src/uu/more" } -mv = { optional=true, version="0.0.4", package="uu_mv", path="src/uu/mv" } -nice = { optional=true, version="0.0.4", package="uu_nice", path="src/uu/nice" } -nl = { optional=true, version="0.0.4", package="uu_nl", path="src/uu/nl" } -nohup = { optional=true, version="0.0.4", package="uu_nohup", path="src/uu/nohup" } -nproc = { optional=true, version="0.0.4", package="uu_nproc", path="src/uu/nproc" } -numfmt = { optional=true, version="0.0.4", package="uu_numfmt", path="src/uu/numfmt" } -od = { optional=true, version="0.0.4", package="uu_od", path="src/uu/od" } -paste = { optional=true, version="0.0.4", package="uu_paste", path="src/uu/paste" } -pathchk = { optional=true, version="0.0.4", package="uu_pathchk", path="src/uu/pathchk" } -pinky = { optional=true, version="0.0.4", package="uu_pinky", path="src/uu/pinky" } -printenv = { optional=true, version="0.0.4", package="uu_printenv", path="src/uu/printenv" } -printf = { optional=true, version="0.0.4", package="uu_printf", path="src/uu/printf" } -ptx = { optional=true, version="0.0.4", package="uu_ptx", path="src/uu/ptx" } -pwd = { optional=true, version="0.0.4", package="uu_pwd", path="src/uu/pwd" } -readlink = { optional=true, version="0.0.4", package="uu_readlink", path="src/uu/readlink" } -realpath = { optional=true, version="0.0.4", package="uu_realpath", path="src/uu/realpath" } -relpath = { optional=true, version="0.0.4", package="uu_relpath", path="src/uu/relpath" } -rm = { optional=true, version="0.0.4", package="uu_rm", path="src/uu/rm" } -rmdir = { optional=true, version="0.0.4", package="uu_rmdir", path="src/uu/rmdir" } -seq = { optional=true, version="0.0.4", package="uu_seq", path="src/uu/seq" } -shred = { optional=true, version="0.0.4", package="uu_shred", path="src/uu/shred" } -shuf = { optional=true, version="0.0.4", package="uu_shuf", path="src/uu/shuf" } -sleep = { optional=true, version="0.0.4", package="uu_sleep", path="src/uu/sleep" } -sort = { optional=true, version="0.0.4", package="uu_sort", path="src/uu/sort" } -split = { optional=true, version="0.0.4", package="uu_split", path="src/uu/split" } -stat = { optional=true, version="0.0.4", package="uu_stat", path="src/uu/stat" } -stdbuf = { optional=true, version="0.0.4", package="uu_stdbuf", path="src/uu/stdbuf" } -sum = { optional=true, version="0.0.4", package="uu_sum", path="src/uu/sum" } -sync = { optional=true, version="0.0.4", package="uu_sync", path="src/uu/sync" } -tac = { optional=true, version="0.0.4", package="uu_tac", path="src/uu/tac" } -tail = { optional=true, version="0.0.4", package="uu_tail", path="src/uu/tail" } -tee = { optional=true, version="0.0.4", package="uu_tee", path="src/uu/tee" } -timeout = { optional=true, version="0.0.4", package="uu_timeout", path="src/uu/timeout" } -touch = { optional=true, version="0.0.4", package="uu_touch", path="src/uu/touch" } -tr = { optional=true, version="0.0.4", package="uu_tr", path="src/uu/tr" } -true = { optional=true, version="0.0.4", package="uu_true", path="src/uu/true" } -truncate = { optional=true, version="0.0.4", package="uu_truncate", path="src/uu/truncate" } -tsort = { optional=true, version="0.0.4", package="uu_tsort", path="src/uu/tsort" } -tty = { optional=true, version="0.0.4", package="uu_tty", path="src/uu/tty" } -uname = { optional=true, version="0.0.4", package="uu_uname", path="src/uu/uname" } -unexpand = { optional=true, version="0.0.4", package="uu_unexpand", path="src/uu/unexpand" } -uniq = { optional=true, version="0.0.4", package="uu_uniq", path="src/uu/uniq" } -unlink = { optional=true, version="0.0.4", package="uu_unlink", path="src/uu/unlink" } -uptime = { optional=true, version="0.0.4", package="uu_uptime", path="src/uu/uptime" } -users = { optional=true, version="0.0.4", package="uu_users", path="src/uu/users" } -wc = { optional=true, version="0.0.4", package="uu_wc", path="src/uu/wc" } -who = { optional=true, version="0.0.4", package="uu_who", path="src/uu/who" } -whoami = { optional=true, version="0.0.4", package="uu_whoami", path="src/uu/whoami" } -yes = { optional=true, version="0.0.4", package="uu_yes", path="src/uu/yes" } +df = { optional=true, version="0.0.6", package="uu_df", path="src/uu/df" } +dircolors= { optional=true, version="0.0.6", package="uu_dircolors", path="src/uu/dircolors" } +dirname = { optional=true, version="0.0.6", package="uu_dirname", path="src/uu/dirname" } +du = { optional=true, version="0.0.6", package="uu_du", path="src/uu/du" } +echo = { optional=true, version="0.0.6", package="uu_echo", path="src/uu/echo" } +env = { optional=true, version="0.0.6", package="uu_env", path="src/uu/env" } +expand = { optional=true, version="0.0.6", package="uu_expand", path="src/uu/expand" } +expr = { optional=true, version="0.0.6", package="uu_expr", path="src/uu/expr" } +factor = { optional=true, version="0.0.6", package="uu_factor", path="src/uu/factor" } +false = { optional=true, version="0.0.6", package="uu_false", path="src/uu/false" } +fmt = { optional=true, version="0.0.6", package="uu_fmt", path="src/uu/fmt" } +fold = { optional=true, version="0.0.6", package="uu_fold", path="src/uu/fold" } +groups = { optional=true, version="0.0.6", package="uu_groups", path="src/uu/groups" } +hashsum = { optional=true, version="0.0.6", package="uu_hashsum", path="src/uu/hashsum" } +head = { optional=true, version="0.0.6", package="uu_head", path="src/uu/head" } +hostid = { optional=true, version="0.0.6", package="uu_hostid", path="src/uu/hostid" } +hostname = { optional=true, version="0.0.6", package="uu_hostname", path="src/uu/hostname" } +id = { optional=true, version="0.0.6", package="uu_id", path="src/uu/id" } +install = { optional=true, version="0.0.6", package="uu_install", path="src/uu/install" } +join = { optional=true, version="0.0.6", package="uu_join", path="src/uu/join" } +kill = { optional=true, version="0.0.6", package="uu_kill", path="src/uu/kill" } +link = { optional=true, version="0.0.6", package="uu_link", path="src/uu/link" } +ln = { optional=true, version="0.0.6", package="uu_ln", path="src/uu/ln" } +ls = { optional=true, version="0.0.6", package="uu_ls", path="src/uu/ls" } +logname = { optional=true, version="0.0.6", package="uu_logname", path="src/uu/logname" } +mkdir = { optional=true, version="0.0.6", package="uu_mkdir", path="src/uu/mkdir" } +mkfifo = { optional=true, version="0.0.6", package="uu_mkfifo", path="src/uu/mkfifo" } +mknod = { optional=true, version="0.0.6", package="uu_mknod", path="src/uu/mknod" } +mktemp = { optional=true, version="0.0.6", package="uu_mktemp", path="src/uu/mktemp" } +more = { optional=true, version="0.0.6", package="uu_more", path="src/uu/more" } +mv = { optional=true, version="0.0.6", package="uu_mv", path="src/uu/mv" } +nice = { optional=true, version="0.0.6", package="uu_nice", path="src/uu/nice" } +nl = { optional=true, version="0.0.6", package="uu_nl", path="src/uu/nl" } +nohup = { optional=true, version="0.0.6", package="uu_nohup", path="src/uu/nohup" } +nproc = { optional=true, version="0.0.6", package="uu_nproc", path="src/uu/nproc" } +numfmt = { optional=true, version="0.0.6", package="uu_numfmt", path="src/uu/numfmt" } +od = { optional=true, version="0.0.6", package="uu_od", path="src/uu/od" } +paste = { optional=true, version="0.0.6", package="uu_paste", path="src/uu/paste" } +pathchk = { optional=true, version="0.0.6", package="uu_pathchk", path="src/uu/pathchk" } +pinky = { optional=true, version="0.0.6", package="uu_pinky", path="src/uu/pinky" } +pr = { optional=true, version="0.0.6", package="uu_pr", path="src/uu/pr" } +printenv = { optional=true, version="0.0.6", package="uu_printenv", path="src/uu/printenv" } +printf = { optional=true, version="0.0.6", package="uu_printf", path="src/uu/printf" } +ptx = { optional=true, version="0.0.6", package="uu_ptx", path="src/uu/ptx" } +pwd = { optional=true, version="0.0.6", package="uu_pwd", path="src/uu/pwd" } +readlink = { optional=true, version="0.0.6", package="uu_readlink", path="src/uu/readlink" } +realpath = { optional=true, version="0.0.6", package="uu_realpath", path="src/uu/realpath" } +relpath = { optional=true, version="0.0.6", package="uu_relpath", path="src/uu/relpath" } +rm = { optional=true, version="0.0.6", package="uu_rm", path="src/uu/rm" } +rmdir = { optional=true, version="0.0.6", package="uu_rmdir", path="src/uu/rmdir" } +seq = { optional=true, version="0.0.6", package="uu_seq", path="src/uu/seq" } +shred = { optional=true, version="0.0.6", package="uu_shred", path="src/uu/shred" } +shuf = { optional=true, version="0.0.6", package="uu_shuf", path="src/uu/shuf" } +sleep = { optional=true, version="0.0.6", package="uu_sleep", path="src/uu/sleep" } +sort = { optional=true, version="0.0.6", package="uu_sort", path="src/uu/sort" } +split = { optional=true, version="0.0.6", package="uu_split", path="src/uu/split" } +stat = { optional=true, version="0.0.6", package="uu_stat", path="src/uu/stat" } +stdbuf = { optional=true, version="0.0.6", package="uu_stdbuf", path="src/uu/stdbuf" } +sum = { optional=true, version="0.0.6", package="uu_sum", path="src/uu/sum" } +sync = { optional=true, version="0.0.6", package="uu_sync", path="src/uu/sync" } +tac = { optional=true, version="0.0.6", package="uu_tac", path="src/uu/tac" } +tail = { optional=true, version="0.0.6", package="uu_tail", path="src/uu/tail" } +tee = { optional=true, version="0.0.6", package="uu_tee", path="src/uu/tee" } +timeout = { optional=true, version="0.0.6", package="uu_timeout", path="src/uu/timeout" } +touch = { optional=true, version="0.0.6", package="uu_touch", path="src/uu/touch" } +tr = { optional=true, version="0.0.6", package="uu_tr", path="src/uu/tr" } +true = { optional=true, version="0.0.6", package="uu_true", path="src/uu/true" } +truncate = { optional=true, version="0.0.6", package="uu_truncate", path="src/uu/truncate" } +tsort = { optional=true, version="0.0.6", package="uu_tsort", path="src/uu/tsort" } +tty = { optional=true, version="0.0.6", package="uu_tty", path="src/uu/tty" } +uname = { optional=true, version="0.0.6", package="uu_uname", path="src/uu/uname" } +unexpand = { optional=true, version="0.0.6", package="uu_unexpand", path="src/uu/unexpand" } +uniq = { optional=true, version="0.0.6", package="uu_uniq", path="src/uu/uniq" } +unlink = { optional=true, version="0.0.6", package="uu_unlink", path="src/uu/unlink" } +uptime = { optional=true, version="0.0.6", package="uu_uptime", path="src/uu/uptime" } +users = { optional=true, version="0.0.6", package="uu_users", path="src/uu/users" } +wc = { optional=true, version="0.0.6", package="uu_wc", path="src/uu/wc" } +who = { optional=true, version="0.0.6", package="uu_who", path="src/uu/who" } +whoami = { optional=true, version="0.0.6", package="uu_whoami", path="src/uu/whoami" } +yes = { optional=true, version="0.0.6", 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" } + # # * pinned transitive dependencies -pin_cc = { version="1.0.61, < 1.0.62", package="cc" } ## cc v1.0.62 has compiler errors for MinRustV v1.32.0, requires 1.34 (for `std::str::split_ascii_whitespace()`) -pin_rustc-demangle = { version="0.1.16, < 0.1.17", package="rustc-demangle" } ## rust-demangle v0.1.17 has compiler errors for MinRustV v1.32.0, expects 1.33 -pin_same-file = { version="1.0.4, < 1.0.6", package="same-file" } ## same-file v1.0.6 has compiler errors for MinRustV v1.32.0, expects 1.34 -pin_winapi-util = { version="0.1.2, < 0.1.3", package="winapi-util" } ## winapi-util v0.1.3 has compiler errors for MinRustV v1.32.0, expects 1.34 -pin_byteorder = { version="1.3.4, < 1.4.0", package="byteorder" } ## byteorder v1.4 has compiler errors for MinRustV v1.32.0, requires 1.3 (for `use of unstable library feature 'try_from' (see issue #33417)`) -pin_thread_local = { version="1.1.0, < 1.1.1", package="thread_local" } ## thread_local v1.1.2 has compiler errors for MinRustV v1.32.0, requires 1.36 (for `use of unstable library feature 'maybe_uninit'`) +# Not needed for now. Keep as examples: +#pin_cc = { version="1.0.61, < 1.0.62", package="cc" } ## cc v1.0.62 has compiler errors for MinRustV v1.32.0, requires 1.34 (for `std::str::split_ascii_whitespace()`) [dev-dependencies] +chrono = "0.4.11" 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" sha1 = { version="0.6", features=["std"] } -## tempfile 3.2 depends on recent version of rand which depends on getrandom v0.2 which has compiler errors for MinRustV v1.32.0 -## min dep for tempfile = Rustc 1.40 -tempfile = "= 3.1.0" +tempfile = "3.2.0" time = "0.1" unindent = "0.1" -uucore = { version=">=0.0.7", package="uucore", path="src/uucore", features=["entries"] } +uucore = { version=">=0.0.8", package="uucore", path="src/uucore", features=["entries", "process"] } +walkdir = "2.2" +atty = "0.2" [target.'cfg(unix)'.dev-dependencies] +rlimit = "0.4.0" rust-users = { version="0.10", package="users" } unix_socket = "0.5.0" + [[bin]] name = "coreutils" path = "src/bin/coreutils.rs" diff --git a/DEVELOPER_INSTRUCTIONS.md b/DEVELOPER_INSTRUCTIONS.md new file mode 100644 index 000000000..e0a5cf001 --- /dev/null +++ b/DEVELOPER_INSTRUCTIONS.md @@ -0,0 +1,44 @@ +Code Coverage Report Generation +--------------------------------- + + + +Code coverage report can be generated using [grcov](https://github.com/mozilla/grcov). + +### Using Nightly Rust + +To generate [gcov-based](https://github.com/mozilla/grcov#example-how-to-generate-gcda-files-for-cc) coverage report + +```bash +$ export CARGO_INCREMENTAL=0 +$ export RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort" +$ export RUSTDOCFLAGS="-Cpanic=abort" +$ cargo build # e.g., --features feat_os_unix +$ cargo test # e.g., --features feat_os_unix test_pathchk +$ grcov . -s . --binary-path ./target/debug/ -t html --branch --ignore-not-existing --ignore build.rs --excl-br-line "^\s*((debug_)?assert(_eq|_ne)?\#\[derive\()" -o ./target/debug/coverage/ +$ # open target/debug/coverage/index.html in browser +``` + +if changes are not reflected in the report then run `cargo clean` and run the above commands. + +### Using Stable Rust + +If you are using stable version of Rust that doesn't enable code coverage instrumentation by default +then add `-Z-Zinstrument-coverage` flag to `RUSTFLAGS` env variable specified above. + + +pre-commit hooks +---------------- + +A configuration for `pre-commit` is provided in the repository. It allows automatically checking every git commit you make to ensure it compiles, and passes `clippy` and `rustfmt` without warnings. + +To use the provided hook: + +1. [Install `pre-commit`](https://pre-commit.com/#install) +2. Run `pre-commit install` while in the repository directory + +Your git commits will then automatically be checked. If a check fails, an error message will explain why, and your commit will be canceled. You can then make the suggested changes, and run `git commit ...` again. + +### Using Clippy + +The `msrv` key in the clippy configuration file `clippy.toml` is used to disable lints pertaining to newer features by specifying the minimum supported Rust version (MSRV). However, this key is only supported on `nightly`. To invoke clippy without errors, use `cargo +nightly clippy`. In order to also check tests and non-default crate features, use `cargo +nightly clippy --all-targets --all-features`. diff --git a/GNUmakefile b/GNUmakefile index c4c9cbe8b..89a4dca80 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,3 +1,5 @@ +# spell-checker:ignore (misc) testsuite runtest (targets) busytest distclean manpages pkgs ; (vars/env) BINDIR BUILDDIR CARGOFLAGS DESTDIR DOCSDIR INSTALLDIR INSTALLEES MANDIR MULTICALL + # Config options PROFILE ?= debug MULTICALL ?= n @@ -30,7 +32,7 @@ ifneq ($(.SHELLSTATUS),0) override INSTALLDIR_MAN=$(DESTDIR)$(PREFIX)$(MANDIR) endif -#prefix to apply to uutils binary and all tool binaries +#prefix to apply to coreutils binary and all tool binaries PROG_PREFIX ?= # This won't support any directory with spaces in its name, but you can just @@ -82,6 +84,7 @@ PROGS := \ nproc \ od \ paste \ + pr \ printenv \ printf \ ptx \ @@ -188,6 +191,7 @@ TEST_PROGS := \ paste \ pathchk \ pinky \ + pr \ printf \ ptx \ pwd \ @@ -237,7 +241,7 @@ EXES := \ INSTALLEES := ${EXES} ifeq (${MULTICALL}, y) -INSTALLEES := ${INSTALLEES} uutils +INSTALLEES := ${INSTALLEES} coreutils endif all: build @@ -250,13 +254,13 @@ ifneq (${MULTICALL}, y) ${CARGO} build ${CARGOFLAGS} ${PROFILE_CMD} $(foreach pkg,$(EXES),-p uu_$(pkg)) endif -build-uutils: +build-coreutils: ${CARGO} build ${CARGOFLAGS} --features "${EXES}" ${PROFILE_CMD} --no-default-features build-manpages: cd $(DOCSDIR) && $(MAKE) man -build: build-uutils build-pkgs build-manpages +build: build-coreutils build-pkgs build-manpages $(foreach test,$(filter-out $(SKIP_UTILS),$(PROGS)),$(eval $(call TEST_BUSYBOX,$(test)))) @@ -264,22 +268,24 @@ test: ${CARGO} test ${CARGOFLAGS} --features "$(TESTS) $(TEST_SPEC_FEATURE)" --no-default-features $(TEST_NO_FAIL_FAST) busybox-src: - if [ ! -e $(BUSYBOX_SRC) ]; then \ - mkdir -p $(BUSYBOX_ROOT); \ - wget https://busybox.net/downloads/busybox-$(BUSYBOX_VER).tar.bz2 -P $(BUSYBOX_ROOT); \ - tar -C $(BUSYBOX_ROOT) -xf $(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER).tar.bz2; \ - fi; \ + if [ ! -e "$(BUSYBOX_SRC)" ] ; then \ + mkdir -p "$(BUSYBOX_ROOT)" ; \ + wget "https://busybox.net/downloads/busybox-$(BUSYBOX_VER).tar.bz2" -P "$(BUSYBOX_ROOT)" ; \ + tar -C "$(BUSYBOX_ROOT)" -xf "$(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER).tar.bz2" ; \ + fi ; # This is a busybox-specific config file their test suite wants to parse. $(BUILDDIR)/.config: $(BASEDIR)/.busybox-config cp $< $@ -# Test under the busybox testsuite -$(BUILDDIR)/busybox: busybox-src build-uutils $(BUILDDIR)/.config - cp $(BUILDDIR)/coreutils $(BUILDDIR)/busybox; \ - chmod +x $@; +# Test under the busybox test suite +$(BUILDDIR)/busybox: busybox-src build-coreutils $(BUILDDIR)/.config + cp "$(BUILDDIR)/coreutils" "$(BUILDDIR)/busybox" + chmod +x $@ prepare-busytest: $(BUILDDIR)/busybox + # disable inapplicable tests + -( cd "$(BUSYBOX_SRC)/testsuite" ; if [ -e "busybox.tests" ] ; then mv busybox.tests busybox.tests- ; fi ; ) ifeq ($(EXES),) busytest: @@ -298,23 +304,31 @@ install: build mkdir -p $(INSTALLDIR_BIN) mkdir -p $(INSTALLDIR_MAN) ifeq (${MULTICALL}, y) - $(INSTALL) $(BUILDDIR)/uutils $(INSTALLDIR_BIN)/$(PROG_PREFIX)uutils - cd $(INSTALLDIR_BIN) && $(foreach prog, $(filter-out uutils, $(INSTALLEES)), \ - ln -fs $(PROG_PREFIX)uutils $(PROG_PREFIX)$(prog) &&) : - cat $(DOCSDIR)/_build/man/uutils.1 | gzip > $(INSTALLDIR_MAN)/$(PROG_PREFIX)uutils.1.gz + $(INSTALL) $(BUILDDIR)/coreutils $(INSTALLDIR_BIN)/$(PROG_PREFIX)coreutils + cd $(INSTALLDIR_BIN) && $(foreach prog, $(filter-out coreutils, $(INSTALLEES)), \ + ln -fs $(PROG_PREFIX)coreutils $(PROG_PREFIX)$(prog) &&) : + cat $(DOCSDIR)/_build/man/coreutils.1 | gzip > $(INSTALLDIR_MAN)/$(PROG_PREFIX)coreutils.1.gz else $(foreach prog, $(INSTALLEES), \ $(INSTALL) $(BUILDDIR)/$(prog) $(INSTALLDIR_BIN)/$(PROG_PREFIX)$(prog);) endif $(foreach man, $(filter $(INSTALLEES), $(basename $(notdir $(wildcard $(DOCSDIR)/_build/man/*)))), \ cat $(DOCSDIR)/_build/man/$(man).1 | gzip > $(INSTALLDIR_MAN)/$(PROG_PREFIX)$(man).1.gz &&) : + $(foreach prog, $(INSTALLEES), \ + $(BUILDDIR)/coreutils completion $(prog) zsh > $(DESTDIR)$(PREFIX)/share/zsh/site-functions/_$(PROG_PREFIX)$(prog); \ + $(BUILDDIR)/coreutils completion $(prog) bash > $(DESTDIR)$(PREFIX)/share/bash-completion/completions/$(PROG_PREFIX)$(prog); \ + $(BUILDDIR)/coreutils completion $(prog) fish > $(DESTDIR)$(PREFIX)/share/fish/vendor_completions.d/$(PROG_PREFIX)$(prog).fish; \ + ) uninstall: ifeq (${MULTICALL}, y) - rm -f $(addprefix $(INSTALLDIR_BIN)/,$(PROG_PREFIX)uutils) + rm -f $(addprefix $(INSTALLDIR_BIN)/,$(PROG_PREFIX)coreutils) endif - rm -f $(addprefix $(INSTALLDIR_MAN)/,$(PROG_PREFIX)uutils.1.gz) + rm -f $(addprefix $(INSTALLDIR_MAN)/,$(PROG_PREFIX)coreutils.1.gz) rm -f $(addprefix $(INSTALLDIR_BIN)/$(PROG_PREFIX),$(PROGS)) + rm -f $(addprefix $(DESTDIR)$(PREFIX)/share/zsh/site-functions/_$(PROG_PREFIX),$(PROGS)) + rm -f $(addprefix $(DESTDIR)$(PREFIX)/share/bash-completion/completions/$(PROG_PREFIX),$(PROGS)) + rm -f $(addprefix $(DESTDIR)$(PREFIX)/share/fish/vendor_completions.d/$(PROG_PREFIX),$(addsuffix .fish,$(PROGS))) rm -f $(addprefix $(INSTALLDIR_MAN)/$(PROG_PREFIX),$(addsuffix .1.gz,$(PROGS))) -.PHONY: all build build-uutils build-pkgs build-docs test distclean clean busytest install uninstall +.PHONY: all build build-coreutils build-pkgs build-docs test distclean clean busytest install uninstall diff --git a/Makefile b/Makefile index 044aaa770..f56df90fb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -USEGNU=gmake $* +UseGNU=gmake $* all: - @$(USEGNU) + @$(UseGNU) .DEFAULT: - @$(USEGNU) + @$(UseGNU) diff --git a/README.md b/README.md index 9d25a1120..083320ac0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -uutils coreutils -================ +# uutils coreutils [![Crates.io](https://img.shields.io/crates/v/coreutils.svg)](https://crates.io/crates/coreutils) [![Discord](https://img.shields.io/badge/discord-join-7289DA.svg?logo=discord&longCache=true&style=flat)](https://discord.gg/wQVJbvJ) @@ -7,303 +6,382 @@ uutils coreutils [![LOC](https://tokei.rs/b1/github/uutils/coreutils?category=code)](https://github.com/Aaronepower/tokei) [![dependency status](https://deps.rs/repo/github/uutils/coreutils/status.svg)](https://deps.rs/repo/github/uutils/coreutils) -[![Build Status](https://api.travis-ci.org/uutils/coreutils.svg?branch=master)](https://travis-ci.org/uutils/coreutils) [![Build Status (FreeBSD)](https://api.cirrus-ci.com/github/uutils/coreutils.svg)](https://cirrus-ci.com/github/uutils/coreutils/master) -[![codecov](https://codecov.io/gh/uutils/coreutils/branch/master/graph/badge.svg)](https://codecov.io/gh/uutils/coreutils) +[![CodeCov](https://codecov.io/gh/uutils/coreutils/branch/master/graph/badge.svg)](https://codecov.io/gh/uutils/coreutils) ----------------------------------------------- + + + uutils is an attempt at writing universal (as in cross-platform) CLI utilities in [Rust](http://www.rust-lang.org). This repository is intended to aggregate GNU coreutils rewrites. -Why? ----- +## Why? Many GNU, Linux and other utilities are useful, and obviously [some](http://gnuwin32.sourceforge.net) [effort](http://unxutils.sourceforge.net) has been spent in the past to port them to Windows. However, those projects -are either old and abandoned, are hosted on CVS (which makes it more difficult -for new contributors to contribute to them), are written in platform-specific C, or -suffer from other issues. +are written in platform-specific C, a language considered unsafe compared to Rust, and +have other issues. Rust provides a good, platform-agnostic way of writing systems utilities that are easy to compile anywhere, and this is as good a way as any to try and learn it. -Requirements ------------- +## Requirements * Rust (`cargo`, `rustc`) * GNU Make (required to build documentation) * [Sphinx](http://www.sphinx-doc.org/) (for documentation) * gzip (for installing documentation) -### Rust Version ### +### 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.33.0`. +The current oldest supported version of the Rust compiler is `1.43.1`. On both Windows and Redox, only the nightly version is tested currently. -Build Instructions ------------------- +## Build Instructions -There are currently two methods to build uutils: GNU Make and Cargo. However, -while there may be two methods, both systems are required to build on Unix -(only Cargo is required on Windows). +There are currently two methods to build the uutils binaries: either Cargo +or GNU Make. + +> Building the full package, including all documentation, requires both Cargo +> and Gnu Make on a Unix platform. + +For either method, we first need to fetch the repository: -First, for both methods, we need to fetch the repository: ```bash $ git clone https://github.com/uutils/coreutils $ cd coreutils ``` -### Cargo ### +### Cargo Building uutils using Cargo is easy because the process is the same as for every other Rust program: + ```bash -# to keep debug information, compile without --release $ cargo build --release ``` -Because the above command attempts to build utilities that only work on -Unix-like platforms at the moment, to build on Windows, you must do the -following: +This command builds the most portable common core set of uutils into a multicall +(BusyBox-type) binary, named 'coreutils', on most Rust-supported platforms. + +Additional platform-specific uutils are often available. Building these +expanded sets of uutils for a platform (on that platform) is as simple as +specifying it as a feature: + ```bash -# to keep debug information, compile without --release -$ cargo build --release --no-default-features --features windows +$ cargo build --release --features macos +# or ... +$ cargo build --release --features windows +# or ... +$ cargo build --release --features unix ``` If you don't want to build every utility available on your platform into the -multicall binary (the Busybox-esque binary), you can also specify which ones -you want to build manually. For example: +final binary, you can also specify which ones you want to build manually. +For example: + ```bash $ cargo build --features "base32 cat echo rm" --no-default-features ``` -If you don't even want to build the multicall binary and would prefer to just -build the utilities as individual binaries, that is possible too. For example: +If you don't want to build the multicall binary and would prefer to build +the utilities as individual binaries, that is also possible. Each utility +is contained in its own package within the main repository, named +"uu_UTILNAME". To build individual utilities, use cargo to build just the +specific packages (using the `--package` [aka `-p`] option). For example: + ```bash $ cargo build -p uu_base32 -p uu_cat -p uu_echo -p uu_rm ``` -### GNU Make ### +### GNU Make Building using `make` is a simple process as well. To simply build all available utilities: + ```bash $ make ``` To build all but a few of the available utilities: + ```bash $ make SKIP_UTILS='UTILITY_1 UTILITY_2' ``` To build only a few of the available utilities: + ```bash $ make UTILS='UTILITY_1 UTILITY_2' ``` -Installation Instructions -------------------------- +## Installation Instructions -### Cargo ### +### Cargo Likewise, installing can simply be done using: + ```bash $ cargo install --path . ``` This command will install uutils into Cargo's *bin* folder (*e.g.* `$HOME/.cargo/bin`). -### GNU Make ### +This does not install files necessary for shell completion. For shell completion to work, +use `GNU Make` or see `Manually install shell completions`. + +### GNU Make To install all available utilities: + ```bash $ make install ``` To install using `sudo` switch `-E` must be used: + ```bash $ sudo -E make install ``` To install all but a few of the available utilities: + ```bash $ make SKIP_UTILS='UTILITY_1 UTILITY_2' install ``` To install only a few of the available utilities: + ```bash $ make UTILS='UTILITY_1 UTILITY_2' install ``` To install every program with a prefix (e.g. uu-echo uu-cat): + ```bash $ make PROG_PREFIX=PREFIX_GOES_HERE install ``` To install the multicall binary: + ```bash $ make MULTICALL=y install ``` Set install parent directory (default value is /usr/local): + ```bash # DESTDIR is also supported $ make PREFIX=/my/path install ``` -### NixOS ### +Installing with `make` installs shell completions for all installed utilities +for `bash`, `fish` and `zsh`. Completions for `elvish` and `powershell` can also +be generated; See `Manually install shell completions`. + +### NixOS The [standard package set](https://nixos.org/nixpkgs/manual/) of [NixOS](https://nixos.org/) provides this package out of the box since 18.03: -``` -nix-env -iA nixos.uutils-coreutils +```shell +$ nix-env -iA nixos.uutils-coreutils ``` -Uninstallation Instructions ---------------------------- +### Manually install shell completions -Uninstallation differs depending on how you have installed uutils. If you used +The `coreutils` binary can generate completions for the `bash`, `elvish`, `fish`, `powershell` +and `zsh` shells. It prints the result to stdout. + +The syntax is: +```bash +cargo run completion +``` + +So, to install completions for `ls` on `bash` to `/usr/local/share/bash-completion/completions/ls`, +run: + +```bash +cargo run completion ls bash > /usr/local/share/bash-completion/completions/ls +``` + +## Un-installation Instructions + +Un-installation differs depending on how you have installed uutils. If you used Cargo to install, use Cargo to uninstall. If you used GNU Make to install, use Make to uninstall. -### Cargo ### +### Cargo To uninstall uutils: + ```bash $ cargo uninstall uutils ``` -### GNU Make ### +### GNU Make To uninstall all utilities: + ```bash $ make uninstall ``` To uninstall every program with a set prefix: + ```bash $ make PROG_PREFIX=PREFIX_GOES_HERE uninstall ``` To uninstall the multicall binary: + ```bash $ make MULTICALL=y uninstall ``` To uninstall from a custom parent directory: + ```bash # DESTDIR is also supported $ make PREFIX=/my/path uninstall ``` -Test Instructions ------------------ +## Test Instructions Testing can be done using either Cargo or `make`. -### Cargo ### +### Cargo Just like with building, we follow the standard procedure for testing using Cargo: + ```bash $ cargo test ``` By default, `cargo test` only runs the common programs. To run also platform specific tests, run: + ```bash $ cargo test --features unix ``` If you would prefer to test a select few utilities: + ```bash $ cargo test --features "chmod mv tail" --no-default-features ``` +If you also want to test the core utilities: + +```bash +$ cargo test -p uucore -p coreutils +``` + To debug: + ```bash $ gdb --args target/debug/coreutils ls (gdb) b ls.rs:79 (gdb) run ``` -### GNU Make ### +### GNU Make To simply test all available utilities: + ```bash $ make test ``` To test all but a few of the available utilities: + ```bash $ make SKIP_UTILS='UTILITY_1 UTILITY_2' test ``` To test only a few of the available utilities: + ```bash $ make UTILS='UTILITY_1 UTILITY_2' test ``` To include tests for unimplemented behavior: + ```bash $ make UTILS='UTILITY_1 UTILITY_2' SPEC=y test ``` -Run Busybox Tests ------------------ +## Run Busybox Tests This testing functionality is only available on *nix operating systems and requires `make`. -To run busybox's tests for all utilities for which busybox has tests +To run busybox tests for all utilities for which busybox has tests + ```bash $ make busytest ``` -To run busybox's tests for a few of the available utilities +To run busybox tests for a few of the available utilities + ```bash $ make UTILS='UTILITY_1 UTILITY_2' busytest ``` To pass an argument like "-v" to the busybox test runtime + ```bash $ make UTILS='UTILITY_1 UTILITY_2' RUNTEST_ARGS='-v' busytest ``` -Contribute ----------- +## Comparing with GNU + +![Evolution over time](https://github.com/uutils/coreutils-tracking/blob/main/gnu-results.png?raw=true) + +To run locally: + +```bash +$ bash util/build-gnu.sh +$ bash util/run-gnu-test.sh +# To run a single test: +$ bash util/run-gnu-test.sh tests/touch/not-owner.sh # for example +``` + +Note that it relies on individual utilities (not the multicall binary). + +## Contribute To contribute to uutils, please see [CONTRIBUTING](CONTRIBUTING.md). -Utilities ---------- +## Utilities | Done | Semi-Done | To Do | |-----------|-----------|--------| | arch | cp | chcon | -| base32 | expr | dd | -| base64 | install | numfmt | -| basename | ls | pr | -| cat | more | runcon | -| chgrp | od (`--strings` and 128-bit data types missing) | stty | -| chmod | printf | | -| chown | sort | | -| chroot | split | | -| cksum | tail | | -| comm | test | | -| csplit | date | | -| cut | join | | -| dircolors | df | | -| dirname | | | -| du | | | -| echo | | | +| base32 | date | dd | +| base64 | df | runcon | +| basename | expr | stty | +| 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 | | | env | | | | expand | | | | factor | | | @@ -320,12 +398,12 @@ Utilities | link | | | | ln | | | | logname | | | -| ~~md5sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | -| ~~sha1sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | -| ~~sha224sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | -| ~~sha256sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | -| ~~sha384sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | -| ~~sha512sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | +| ~~md5sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | | +| ~~sha1sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | | +| ~~sha224sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | | +| ~~sha256sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | | +| ~~sha384sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | | +| ~~sha512sum~~ (replaced by [hashsum](https://github.com/uutils/coreutils/blob/master/src/uu/hashsum/src/hashsum.rs)) | | | | mkdir | | | | mkfifo | | | | mknod | | | @@ -354,7 +432,6 @@ Utilities | stdbuf | | | | sum | | | | sync | | | -| tac | | | | tee | | | | timeout | | | | touch | | | @@ -374,8 +451,35 @@ Utilities | whoami | | | | yes | | | -License -------- +## Targets that compile + +This is an auto-generated table showing which binaries compile for each target-triple. Note that this **does not** indicate that they are fully implemented, or that the tests pass. + +|######OS######|###ARCH####|arch|base32|base64|basename|cat|chgrp|chmod|chown|chroot|cksum|comm|cp|csplit|cut|date|df|dircolors|dirname|du|echo|env|expand|expr|factor|false|fmt|fold|groups|hashsum|head|hostid|hostname|id|install|join|kill|link|ln|logname|ls|mkdir|mkfifo|mknod|mktemp|more|mv|nice|nl|nohup|nproc|numfmt|od|paste|pathchk|pinky|printenv|printf|ptx|pwd|readlink|realpath|relpath|rm|rmdir|seq|shred|shuf|sleep|sort|split|stat|stdbuf|sum|sync|tac|tail|tee|test|timeout|touch|tr|true|truncate|tsort|tty|uname|unexpand|uniq|unlink|uptime|users|wc|who|whoami|yes| +|--------------|-----------|----|------|------|--------|---|-----|-----|-----|------|-----|----|--|------|---|----|--|---------|-------|--|----|---|------|----|------|-----|---|----|------|-------|----|------|--------|--|-------|----|----|----|--|-------|--|-----|------|-----|------|----|--|----|--|-----|-----|------|--|-----|-------|-----|--------|------|---|---|--------|--------|-------|--|-----|---|-----|----|-----|----|-----|----|------|---|----|---|----|---|----|-------|-----|--|----|--------|-----|---|-----|--------|----|------|------|-----|--|---|------|---| +|linux-gnu|aarch64|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| +|linux-gnu|i686|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| +|linux-gnu|powerpc64|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| +|linux-gnu|riscv64gc| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +|linux-gnu|x86_64|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| +|windows-msvc|aarch64|y|y|y|y|y| | | | |y|y|y|y|y|y|y|y|y| |y|y|y| |y|y|y|y| |y|y|y|y| | |y| |y|y|y| |y| | |y|y|y| |y| |y|y|y|y| | |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| | |y|y|y|y|y|y| |y|y|y|y|y| |y|y|y| |y| |y| | |y| +|windows-gnu|i686|y|y|y|y|y| | | | |y|y|y|y|y|y|y|y|y| |y|y|y| |y|y|y|y| |y|y|y|y| | |y| |y|y|y|y|y| | |y|y|y| |y| |y|y|y|y| | |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| | |y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y| |y| |y| |y|y| +|windows-msvc|i686|y|y|y|y|y| | | | |y|y|y|y|y|y|y|y|y| |y|y|y| |y|y|y|y| |y|y|y|y| | |y| |y|y|y|y|y| | |y|y|y| |y| |y|y|y|y| | |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| | |y|y|y|y|y|y| |y|y|y|y|y| |y|y|y| |y| |y| |y|y| +|windows-gnu|x86_64|y|y|y|y|y| | | | |y|y|y|y|y|y|y|y|y| |y|y|y| |y|y|y|y| |y|y|y|y| | |y| |y|y|y|y|y| | |y|y|y| |y| |y|y|y|y| | |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| | |y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y| |y| |y| |y|y| +|windows-msvc|x86_64|y|y|y|y|y| | | | |y|y|y|y|y|y|y|y|y| |y|y|y| |y|y|y|y| |y|y|y|y| | |y| |y|y|y|y|y| | |y|y|y| |y| |y|y|y|y| | |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| | |y|y|y|y|y|y| |y|y|y|y|y| |y|y|y| |y| |y| |y|y| +|apple MacOS|aarch64|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| +|apple MacOS|x86_64|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| +|freebsd|x86_64|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| +|netbsd|x86_64|y|y|y|y|y|y|y|y| |y|y|y|y|y|y| |y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y| |y|y| |y|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y|y| |y|y|y| | |y| |y|y| +|android|aarch64|y|y|y|y|y|y|y|y| |y|y|y|y|y|y| |y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y| |y|y| |y|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| |y|y| |y|y|y|y| |y|y|y|y|y|y| |y|y|y| | |y| |y|y| +|android|x86_64|y|y|y|y|y|y|y|y| |y|y|y|y|y|y| |y|y|y|y|y|y| |y|y|y|y|y|y|y|y|y| |y|y| |y|y|y|y|y|y|y|y|y|y|y|y| |y|y|y|y|y| |y|y|y|y|y|y|y|y|y|y|y|y|y|y|y| |y|y| |y|y|y|y| |y|y|y|y|y|y| |y|y|y| | |y| |y|y| +|solaris|x86_64| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +|wasi|wasm32| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +|redox|x86_64| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +|fuchsia|aarch64| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +|fuchsia|x86_64| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +## License uutils is licensed under the MIT License - see the `LICENSE` file for details diff --git a/build.rs b/build.rs index 625a3ccc8..e9fe129eb 100644 --- a/build.rs +++ b/build.rs @@ -43,21 +43,44 @@ pub fn main() { let mut tf = File::create(Path::new(&out_dir).join("test_modules.rs")).unwrap(); mf.write_all( - "type UtilityMap = HashMap<&'static str, fn(T) -> i32>;\n\ - \n\ - fn util_map() -> UtilityMap {\n\ - \tlet mut map = UtilityMap::new();\n\ - " + "type UtilityMap = HashMap<&'static str, (fn(T) -> i32, fn() -> App<'static, 'static>)>;\n\ + \n\ + fn util_map() -> UtilityMap {\n\ + \tlet mut map = UtilityMap::new();\n\ + " .as_bytes(), ) .unwrap(); for krate in crates { match krate.as_ref() { + // 'test' is named uu_test to avoid collision with rust core crate 'test'. + // It can also be invoked by name '[' for the '[ expr ] syntax'. + "uu_test" => { + mf.write_all( + format!( + "\ + \tmap.insert(\"test\", ({krate}::uumain, {krate}::uu_app));\n\ + \t\tmap.insert(\"[\", ({krate}::uumain, {krate}::uu_app));\n\ + ", + krate = krate + ) + .as_bytes(), + ) + .unwrap(); + tf.write_all( + format!( + "#[path=\"{dir}/test_test.rs\"]\nmod test_test;\n", + dir = util_tests_dir, + ) + .as_bytes(), + ) + .unwrap() + } k if k.starts_with(override_prefix) => { mf.write_all( format!( - "\tmap.insert(\"{k}\", {krate}::uumain);\n", + "\tmap.insert(\"{k}\", ({krate}::uumain, {krate}::uu_app));\n", k = krate[override_prefix.len()..].to_string(), krate = krate ) @@ -77,7 +100,7 @@ pub fn main() { "false" | "true" => { mf.write_all( format!( - "\tmap.insert(\"{krate}\", r#{krate}::uumain);\n", + "\tmap.insert(\"{krate}\", (r#{krate}::uumain, r#{krate}::uu_app));\n", krate = krate ) .as_bytes(), @@ -97,21 +120,21 @@ pub fn main() { mf.write_all( format!( "\ - \tmap.insert(\"{krate}\", {krate}::uumain);\n\ - \t\tmap.insert(\"md5sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha1sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha224sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha256sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha384sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha512sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha3sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha3-224sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha3-256sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha3-384sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"sha3-512sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"shake128sum\", {krate}::uumain);\n\ - \t\tmap.insert(\"shake256sum\", {krate}::uumain);\n\ - ", + \tmap.insert(\"{krate}\", ({krate}::uumain, {krate}::uu_app_custom));\n\ + \t\tmap.insert(\"md5sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha1sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha224sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha256sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha384sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha512sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha3sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha3-224sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha3-256sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha3-384sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"sha3-512sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"shake128sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + \t\tmap.insert(\"shake256sum\", ({krate}::uumain, {krate}::uu_app_common));\n\ + ", krate = krate ) .as_bytes(), @@ -130,7 +153,7 @@ pub fn main() { _ => { mf.write_all( format!( - "\tmap.insert(\"{krate}\", {krate}::uumain);\n", + "\tmap.insert(\"{krate}\", ({krate}::uumain, {krate}::uu_app));\n", krate = krate ) .as_bytes(), diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 000000000..0a0a69a41 --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +msrv = "1.43.1" diff --git a/docs/GNUmakefile b/docs/GNUmakefile index 894a53be0..49d827da8 100644 --- a/docs/GNUmakefile +++ b/docs/GNUmakefile @@ -1,5 +1,6 @@ +# spell-checker:ignore (vars/env) SPHINXOPTS SPHINXBUILD SPHINXPROJ SOURCEDIR BUILDDIR + # Minimal makefile for Sphinx documentation -# # You can set these variables from the command line. SPHINXOPTS = diff --git a/docs/Makefile b/docs/Makefile index 044aaa770..f56df90fb 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -1,5 +1,5 @@ -USEGNU=gmake $* +UseGNU=gmake $* all: - @$(USEGNU) + @$(UseGNU) .DEFAULT: - @$(USEGNU) + @$(UseGNU) diff --git a/docs/compiles_table.csv b/docs/compiles_table.csv new file mode 100644 index 000000000..6da110132 --- /dev/null +++ b/docs/compiles_table.csv @@ -0,0 +1,21 @@ +target,arch,base32,base64,basename,cat,chgrp,chmod,chown,chroot,cksum,comm,cp,csplit,cut,date,df,dircolors,dirname,du,echo,env,expand,expr,factor,false,fmt,fold,groups,hashsum,head,hostid,hostname,id,install,join,kill,link,ln,logname,ls,mkdir,mkfifo,mknod,mktemp,more,mv,nice,nl,nohup,nproc,numfmt,od,paste,pathchk,pinky,printenv,printf,ptx,pwd,readlink,realpath,relpath,rm,rmdir,seq,shred,shuf,sleep,sort,split,stat,stdbuf,sum,sync,tac,tail,tee,test,timeout,touch,tr,true,truncate,tsort,tty,uname,unexpand,uniq,unlink,uptime,users,wc,who,whoami,yes +aarch64-unknown-linux-gnu,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +i686-unknown-linux-gnu,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +powerpc64-unknown-linux-gnu,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +riscv64gc-unknown-linux-gnu,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101 +x86_64-unknown-linux-gnu,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +aarch64-pc-windows-msvc,0,0,0,0,0,101,101,101,101,0,0,0,0,0,0,0,0,0,101,0,0,0,101,0,0,0,0,101,0,0,0,0,101,101,0,101,0,0,0,101,0,101,101,0,0,0,101,0,101,0,0,0,0,101,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,101,0,0,0,0,0,0,101,0,0,0,0,0,101,0,0,0,101,0,101,0,101,101,0 +i686-pc-windows-gnu,0,0,0,0,0,101,101,101,101,0,0,0,0,0,0,0,0,0,101,0,0,0,101,0,0,0,0,101,0,0,0,0,101,101,0,101,0,0,0,0,0,101,101,0,0,0,101,0,101,0,0,0,0,101,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,101,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,101,0,101,0,101,0,0 +i686-pc-windows-msvc,0,0,0,0,0,101,101,101,101,0,0,0,0,0,0,0,0,0,101,0,0,0,101,0,0,0,0,101,0,0,0,0,101,101,0,101,0,0,0,0,0,101,101,0,0,0,101,0,101,0,0,0,0,101,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,101,0,0,0,0,0,0,101,0,0,0,0,0,101,0,0,0,101,0,101,0,101,0,0 +x86_64-pc-windows-gnu,0,0,0,0,0,101,101,101,101,0,0,0,0,0,0,0,0,0,101,0,0,0,101,0,0,0,0,101,0,0,0,0,101,101,0,101,0,0,0,0,0,101,101,0,0,0,101,0,101,0,0,0,0,101,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,101,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,101,0,101,0,101,0,0 +x86_64-pc-windows-msvc,0,0,0,0,0,101,101,101,101,0,0,0,0,0,0,0,0,0,101,0,0,0,101,0,0,0,0,101,0,0,0,0,101,101,0,101,0,0,0,0,0,101,101,0,0,0,101,0,101,0,0,0,0,101,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,101,0,0,0,0,0,0,101,0,0,0,0,0,101,0,0,0,101,0,101,0,101,0,0 +x86_64-apple-darwin,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +x86_64-unknown-freebsd,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +x86_64-unknown-netbsd,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,101,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,101,101,0,101,0,0 +aarch64-linux-android,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,101,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,101,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,101,101,0,101,0,0 +x86_64-linux-android,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,101,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,101,0,0,0,0,101,0,0,0,0,0,0,101,0,0,0,101,101,0,101,0,0 +x86_64-sun-solaris,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101 +wasm32-wasi,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101 +x86_64-unknown-redox,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101 +aarch64-fuchsia,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101 +x86_64-fuchsia,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101 diff --git a/docs/compiles_table.py b/docs/compiles_table.py new file mode 100644 index 000000000..aa1b8703c --- /dev/null +++ b/docs/compiles_table.py @@ -0,0 +1,237 @@ +#!/usr/bin/env python3 +import multiprocessing +import subprocess +import argparse +import csv +import sys +from collections import defaultdict +from pathlib import Path + +# third party dependencies +from tqdm import tqdm + +# spell-checker:ignore (libs) tqdm imap ; (shell/mac) xcrun ; (vars) nargs + +BINS_PATH=Path("../src/uu") +CACHE_PATH=Path("compiles_table.csv") +TARGETS = [ + # Linux - GNU + "aarch64-unknown-linux-gnu", + "i686-unknown-linux-gnu", + "powerpc64-unknown-linux-gnu", + "riscv64gc-unknown-linux-gnu", + "x86_64-unknown-linux-gnu", + # Windows + "aarch64-pc-windows-msvc", + "i686-pc-windows-gnu", + "i686-pc-windows-msvc", + "x86_64-pc-windows-gnu", + "x86_64-pc-windows-msvc", + # Apple + "aarch64-apple-darwin", + "x86_64-apple-darwin", + "aarch64-apple-ios", + "x86_64-apple-ios", + # BSDs + "x86_64-unknown-freebsd", + "x86_64-unknown-netbsd", + # Android + "aarch64-linux-android", + "x86_64-linux-android", + # Solaris + "x86_64-sun-solaris", + # WASM + "wasm32-wasi", + # Redox + "x86_64-unknown-redox", + # Fuchsia + "aarch64-fuchsia", + "x86_64-fuchsia", +] + +class Target(str): + def __new__(cls, content): + obj = super().__new__(cls, content) + obj.arch, obj.platform, obj.os = Target.parse(content) + return obj + + @staticmethod + def parse(s): + elem = s.split("-") + if len(elem) == 2: + arch, platform, os = elem[0], "n/a", elem[1] + else: + arch, platform, os = elem[0], elem[1], "-".join(elem[2:]) + if os == "ios": + os = "apple IOS" + if os == "darwin": + os = "apple MacOS" + return (arch, platform, os) + + @staticmethod + def get_heading(): + return ["OS", "ARCH"] + + def get_row_heading(self): + return [self.os, self.arch] + + def requires_nightly(self): + return "redox" in self + + # Perform the 'it-compiles' check + def check(self, binary): + if self.requires_nightly(): + args = [ "cargo", "+nightly", "check", + "-p", f"uu_{binary}", "--bin", binary, + f"--target={self}"] + else: + args = ["cargo", "check", + "-p", f"uu_{binary}", "--bin", binary, + f"--target={self}"] + + res = subprocess.run(args, capture_output=True) + return res.returncode + + # Validate that the dependencies for running this target are met + def is_installed(self): + # check IOS sdk is installed, raise exception otherwise + if "ios" in self: + res = subprocess.run(["which", "xcrun"], capture_output=True) + if len(res.stdout) == 0: + raise Exception("Error: IOS sdk does not seem to be installed. Please do that manually") + if not self.requires_nightly(): + # check std toolchains are installed + toolchains = subprocess.run(["rustup", "target", "list"], capture_output=True) + toolchains = toolchains.stdout.decode('utf-8').split("\n") + if "installed" not in next(filter(lambda x: self in x, toolchains)): + raise Exception(f"Error: the {t} target is not installed. Please do that manually") + else: + # check nightly toolchains are installed + toolchains = subprocess.run(["rustup", "+nightly", "target", "list"], capture_output=True) + toolchains = toolchains.stdout.decode('utf-8').split("\n") + if "installed" not in next(filter(lambda x: self in x, toolchains)): + raise Exception(f"Error: the {t} nightly target is not installed. Please do that manually") + return True + +def install_targets(): + cmd = ["rustup", "target", "add"] + TARGETS + print(" ".join(cmd)) + ret = subprocess.run(cmd) + assert(ret.returncode == 0) + +def get_all_bins(): + bins = map(lambda x: x.name, BINS_PATH.iterdir()) + return sorted(list(bins)) + +def get_targets(selection): + if "all" in selection: + return list(map(Target, TARGETS)) + else: + # preserve the same order as in TARGETS + return list(map(Target, filter(lambda x: x in selection, TARGETS))) + +def test_helper(tup): + bin, target = tup + retcode = target.check(bin) + return (target, bin, retcode) + +def test_all_targets(targets, bins): + pool = multiprocessing.Pool() + inputs = [(b, t) for b in bins for t in targets] + + outputs = list(tqdm(pool.imap(test_helper, inputs), total=len(inputs))) + + table = defaultdict(dict) + for (t, b, r) in outputs: + table[t][b] = r + return table + +def save_csv(file, table): + targets = get_targets(table.keys()) # preserve order in CSV + bins = list(list(table.values())[0].keys()) + with open(file, "w") as csvfile: + header = ["target"] + bins + writer = csv.DictWriter(csvfile, fieldnames=header) + writer.writeheader() + for t in targets: + d = {"target" : t} + d.update(table[t]) + writer.writerow(d) + +def load_csv(file): + table = {} + cols = [] + rows = [] + with open(file, "r") as csvfile: + reader = csv.DictReader(csvfile) + cols = list(filter(lambda x: x!="target", reader.fieldnames)) + for row in reader: + t = Target(row["target"]) + rows += [t] + del row["target"] + table[t] = dict([k, int(v)] for k, v in row.items()) + return (table, rows, cols) + +def merge_tables(old, new): + from copy import deepcopy + tmp = deepcopy(old) + tmp.update(deepcopy(new)) + return tmp + +def render_md(fd, table, headings: str, row_headings: Target): + def print_row(lst, lens=[]): + lens = lens + [0] * (len(lst) - len(lens)) + for e, l in zip(lst, lens): + fmt = '|{}' if l == 0 else '|{:>%s}' % len(header[0]) + fd.write(fmt.format(e)) + fd.write("|\n") + def cell_render(target, bin): + return "y" if table[target][bin] == 0 else " " + + # add some 'hard' padding to specific columns + lens = [ + max(map(lambda x: len(x.os), row_headings)) + 2, + max(map(lambda x: len(x.arch), row_headings)) + 2 + ] + header = Target.get_heading() + header[0] = ("{:#^%d}" % lens[0]).format(header[0]) + header[1] = ("{:#^%d}" % lens[1]).format(header[1]) + + header += headings + print_row(header) + lines = list(map(lambda x: '-'*len(x), header)) + print_row(lines) + + for t in row_headings: + row = list(map(lambda b: cell_render(t, b), headings)) + row = t.get_row_heading() + row + print_row(row) + +if __name__ == "__main__": + # create the top-level parser + parser = argparse.ArgumentParser(prog='compiles_table.py') + subparsers = parser.add_subparsers(help='sub-command to execute', required=True, dest="cmd") + # create the parser for the "check" command + parser_a = subparsers.add_parser('check', help='run cargo check on specified targets and update csv cache') + parser_a.add_argument("targets", metavar="TARGET", type=str, nargs='+', choices=["all"]+TARGETS, + help="target-triple to check, as shown by 'rustup target list'") + # create the parser for the "render" command + parser_b = subparsers.add_parser('render', help='print a markdown table to stdout') + parser_b.add_argument("--equidistant", action="store_true", + help="NOT IMPLEMENTED: render each column with an equal width (in plaintext)") + args = parser.parse_args() + + if args.cmd == "render": + table, targets, bins = load_csv(CACHE_PATH) + render_md(sys.stdout, table, bins, targets) + + if args.cmd == "check": + targets = get_targets(args.targets) + bins = get_all_bins() + + assert(all(map(Target.is_installed, targets))) + table = test_all_targets(targets, bins) + + prev_table, _, _ = load_csv(CACHE_PATH) + new_table = merge_tables(prev_table, table) + save_csv(CACHE_PATH, new_table) diff --git a/docs/conf.py b/docs/conf.py index 74e5e1fb8..5a1213627 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -21,6 +21,8 @@ # import sys # sys.path.insert(0, os.path.abspath('.')) +# spell-checker:ignore (words) howto htbp imgmath toctree todos uutilsdoc + import glob import os import re @@ -180,6 +182,6 @@ for name in glob.glob('*.rst'): # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'uutils', 'uutils Documentation', - author, 'uutils', 'A cross-platform reimplementation of GNU coreutils in Rust.', + author, 'uutils', 'A cross-platform implementation of GNU coreutils, written in Rust.', 'Miscellaneous'), ] diff --git a/docs/index.rst b/docs/index.rst index 4d70d4368..7f782b12a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -3,8 +3,11 @@ You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. -Welcome to uutils's documentation! -================================== +.. + spell-checker:ignore (directives) genindex maxdepth modindex toctree ; (misc) quickstart + +Welcome to uutils' documentation! +================================= .. toctree:: :maxdepth: 2 diff --git a/docs/make.bat b/docs/make.bat index 1e8ea10a0..6e63e3baa 100644 --- a/docs/make.bat +++ b/docs/make.bat @@ -1,5 +1,8 @@ +@setLocal @ECHO OFF +rem spell-checker:ignore (vars/env) BUILDDIR SOURCEDIR SPHINXBUILD SPHINXOPTS SPHINXPROJ + pushd %~dp0 REM Command file for Sphinx documentation @@ -14,7 +17,7 @@ set SPHINXPROJ=uutils if "%1" == "" goto help %SPHINXBUILD% >NUL 2>NUL -if errorlevel 9009 ( +if ErrorLevel 9009 ( echo. echo.The 'sphinx-build' command was not found. Make sure you have Sphinx echo.installed, then set the SPHINXBUILD environment variable to point diff --git a/docs/uutils.rst b/docs/uutils.rst index 19af87fef..e3b8c6a1a 100644 --- a/docs/uutils.rst +++ b/docs/uutils.rst @@ -16,7 +16,7 @@ Synopsis Description ----------- -``uutils`` is a program that contains that other coreutils commands, somewhat +``uutils`` is a program that contains other coreutils commands, somewhat similar to Busybox. --help, -h print a help menu for PROGRAM displaying accepted options and diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 7fc494020..270f5153a 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -5,6 +5,8 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +use clap::App; +use clap::Shell; use std::cmp; use std::collections::hash_map::HashMap; use std::ffi::OsString; @@ -52,13 +54,13 @@ fn main() { let binary_as_util = name(&binary); // binary name equals util name? - if let Some(&uumain) = utils.get(binary_as_util) { + if let Some(&(uumain, _)) = utils.get(binary_as_util) { process::exit(uumain((vec![binary.into()].into_iter()).chain(args))); } // binary name equals prefixed util name? // * prefix/stem may be any string ending in a non-alphanumeric character - let utilname = if let Some(util) = utils.keys().find(|util| { + let util_name = if let Some(util) = utils.keys().find(|util| { binary_as_util.ends_with(*util) && !(&binary_as_util[..binary_as_util.len() - (*util).len()]) .ends_with(char::is_alphanumeric) @@ -71,11 +73,15 @@ fn main() { }; // 0th argument equals util name? - if let Some(util_os) = utilname { + if let Some(util_os) = util_name { let util = util_os.as_os_str().to_string_lossy(); + if util == "completion" { + gen_completions(args, utils); + } + match utils.get(&util[..]) { - Some(&uumain) => { + Some(&(uumain, _)) => { process::exit(uumain((vec![util_os].into_iter()).chain(args))); } None => { @@ -85,7 +91,7 @@ fn main() { let util = util_os.as_os_str().to_string_lossy(); match utils.get(&util[..]) { - Some(&uumain) => { + Some(&(uumain, _)) => { let code = uumain( (vec![util_os, OsString::from("--help")].into_iter()) .chain(args), @@ -113,3 +119,43 @@ fn main() { process::exit(0); } } + +/// Prints completions for the utility in the first parameter for the shell in the second parameter to stdout +fn gen_completions( + mut args: impl Iterator, + util_map: UtilityMap, +) -> ! { + let utility = args + .next() + .expect("expected utility as the first parameter") + .to_str() + .expect("utility name was not valid utf-8") + .to_owned(); + let shell = args + .next() + .expect("expected shell as the second parameter") + .to_str() + .expect("shell name was not valid utf-8") + .to_owned(); + let mut app = if utility == "coreutils" { + gen_coreutils_app(util_map) + } else if let Some((_, app)) = util_map.get(utility.as_str()) { + app() + } else { + eprintln!("{} is not a valid utility", utility); + process::exit(1) + }; + let shell: Shell = shell.parse().unwrap(); + let bin_name = std::env::var("PROG_PREFIX").unwrap_or_default() + &utility; + app.gen_completions_to(bin_name, shell, &mut io::stdout()); + io::stdout().flush().unwrap(); + process::exit(0); +} + +fn gen_coreutils_app(util_map: UtilityMap) -> App<'static, 'static> { + let mut app = App::new("coreutils"); + for (_, (_, sub_app)) in util_map { + app = app.subcommand(sub_app()); + } + app +} diff --git a/src/uu/arch/Cargo.toml b/src/uu/arch/Cargo.toml index 4ca0fbda7..b3fe1f8cb 100644 --- a/src/uu/arch/Cargo.toml +++ b/src/uu/arch/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_arch" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "arch ~ (uutils) display machine architecture" @@ -16,7 +16,8 @@ path = "src/arch.rs" [dependencies] platform-info = "0.1" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/arch/src/arch.rs b/src/uu/arch/src/arch.rs index 20392b11f..94ec97e98 100644 --- a/src/uu/arch/src/arch.rs +++ b/src/uu/arch/src/arch.rs @@ -11,13 +11,24 @@ extern crate uucore; use platform_info::*; -static SYNTAX: &str = "Display machine architecture"; -static SUMMARY: &str = "Determine architecture name for current machine."; -static LONG_HELP: &str = ""; +use clap::{crate_version, App}; +use uucore::error::{FromIo, UResult}; -pub fn uumain(args: impl uucore::Args) -> i32 { - app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); - let uts = return_if_err!(1, PlatformInfo::new()); +static ABOUT: &str = "Display machine architecture"; +static SUMMARY: &str = "Determine architecture name for current machine."; + +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { + uu_app().get_matches_from(args); + + let uts = PlatformInfo::new().map_err_context(|| "cannot get system name".to_string())?; println!("{}", uts.machine().trim()); - 0 + Ok(()) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .after_help(SUMMARY) } diff --git a/src/uu/arch/src/main.rs b/src/uu/arch/src/main.rs index 43e0af5bf..e2668864c 100644 --- a/src/uu/arch/src/main.rs +++ b/src/uu/arch/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_arch); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_arch); diff --git a/src/uu/base32/Cargo.toml b/src/uu/base32/Cargo.toml index 0abb718c8..1b448af0a 100644 --- a/src/uu/base32/Cargo.toml +++ b/src/uu/base32/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_base32" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "base32 ~ (uutils) decode/encode input (base32-encoding)" @@ -15,7 +15,8 @@ edition = "2018" path = "src/base32.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features = ["encoding"] } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features = ["encoding"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/base32/src/base32.rs b/src/uu/base32/src/base32.rs index b47f4d4cc..9a29717ac 100644 --- a/src/uu/base32/src/base32.rs +++ b/src/uu/base32/src/base32.rs @@ -7,13 +7,15 @@ #[macro_use] extern crate uucore; + +use std::io::{stdin, Read}; + +use clap::App; use uucore::encoding::Format; -mod base_common; +pub mod base_common; -static SYNTAX: &str = "[OPTION]... [FILE]"; -static SUMMARY: &str = "Base32 encode or decode FILE, or standard input, to standard output."; -static LONG_HELP: &str = " +static ABOUT: &str = " With no FILE, or when FILE is -, read standard input. The data are encoded as described for the base32 alphabet in RFC @@ -22,13 +24,40 @@ static LONG_HELP: &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!()) +} pub fn uumain(args: impl uucore::Args) -> i32 { - base_common::execute( - args.collect_str(), - SYNTAX, - SUMMARY, - LONG_HELP, - Format::Base32, - ) + let format = Format::Base32; + 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)); + + // 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, + name, + ); + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + base_common::base_app(executable!(), VERSION, ABOUT) } diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index 3f1436fb2..4fc8b495b 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -7,73 +7,134 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -use std::fs::File; -use std::io::{stdin, stdout, BufReader, Read, Write}; -use std::path::Path; +use std::io::{stdout, Read, Write}; use uucore::encoding::{wrap_print, Data, Format}; +use uucore::InvalidEncodingHandling; -pub fn execute( - args: Vec, - syntax: &str, - summary: &str, - long_help: &str, - format: Format, -) -> i32 { - let matches = app!(syntax, summary, long_help) - .optflag("d", "decode", "decode data") - .optflag( - "i", - "ignore-garbage", - "when decoding, ignore non-alphabetic characters", - ) - .optopt( - "w", - "wrap", - "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", - "COLS", - ) - .parse(args); +use std::fs::File; +use std::io::{BufReader, Stdin}; +use std::path::Path; - let line_wrap = matches.opt_str("wrap").map(|s| match s.parse() { - Ok(n) => n, - Err(e) => { - crash!(1, "invalid wrap size: ‘{}’: {}", s, e); - } - }); - let ignore_garbage = matches.opt_present("ignore-garbage"); - let decode = matches.opt_present("decode"); +use clap::{App, Arg}; - if matches.free.len() > 1 { - show_usage_error!("extra operand ‘{}’", matches.free[0]); - return 1; - } - - if matches.free.is_empty() || &matches.free[0][..] == "-" { - let stdin_raw = stdin(); - handle_input( - &mut stdin_raw.lock(), - format, - line_wrap, - ignore_garbage, - decode, - ); - } else { - let path = Path::new(matches.free[0].as_str()); - let file_buf = safe_unwrap!(File::open(&path)); - let mut input = BufReader::new(file_buf); - handle_input(&mut input, format, line_wrap, ignore_garbage, decode); - }; - - 0 +// Config. +pub struct Config { + pub decode: bool, + pub ignore_garbage: bool, + pub wrap_cols: Option, + pub to_read: Option, } -fn handle_input( +pub mod options { + pub static DECODE: &str = "decode"; + pub static WRAP: &str = "wrap"; + pub static IGNORE_GARBAGE: &str = "ignore-garbage"; + pub static FILE: &str = "file"; +} + +impl Config { + fn from(options: clap::ArgMatches) -> Result { + 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 name == "-" { + None + } else { + if !Path::exists(Path::new(name)) { + return Err(format!("{}: No such file or directory", name)); + } + Some(name.to_owned()) + } + } + None => None, + }; + + let cols = options + .value_of(options::WRAP) + .map(|num| { + num.parse::() + .map_err(|e| format!("Invalid wrap size: '{}': {}", num, e)) + }) + .transpose()?; + + Ok(Config { + decode: options.is_present(options::DECODE), + ignore_garbage: options.is_present(options::IGNORE_GARBAGE), + wrap_cols: cols, + to_read: file, + }) + } +} + +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); + let arg_list = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); + 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) + .about(about) + // Format arguments. + .arg( + Arg::with_name(options::DECODE) + .short("d") + .long(options::DECODE) + .help("decode data"), + ) + .arg( + Arg::with_name(options::IGNORE_GARBAGE) + .short("i") + .long(options::IGNORE_GARBAGE) + .help("when decoding, ignore non-alphabetic characters"), + ) + .arg( + Arg::with_name(options::WRAP) + .short("w") + .long(options::WRAP) + .takes_value(true) + .help( + "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", + ), + ) + // "multiple" arguments are used to check whether there is more than one + // file passed in. + .arg(Arg::with_name(options::FILE).index(1).multiple(true)) +} + +pub fn get_input<'a>(config: &Config, stdin_ref: &'a Stdin) -> Box { + match &config.to_read { + Some(name) => { + let file_buf = safe_unwrap!(File::open(Path::new(name))); + Box::new(BufReader::new(file_buf)) // as Box + } + None => { + Box::new(stdin_ref.lock()) // as Box + } + } +} + +pub fn handle_input( input: &mut R, format: Format, line_wrap: Option, ignore_garbage: bool, decode: bool, + name: &str, ) { let mut data = Data::new(input, format).ignore_garbage(ignore_garbage); if let Some(wrap) = line_wrap { @@ -88,10 +149,14 @@ fn handle_input( Ok(s) => { if stdout().write_all(&s).is_err() { // on windows console, writing invalid utf8 returns an error - crash!(1, "Cannot write non-utf8 data"); + eprintln!("{}: error: Cannot write non-utf8 data", name); + exit!(1) } } - Err(_) => crash!(1, "invalid input"), + Err(_) => { + eprintln!("{}: error: invalid input", name); + exit!(1) + } } } } diff --git a/src/uu/base32/src/main.rs b/src/uu/base32/src/main.rs index b59cb89f0..83a0b6607 100644 --- a/src/uu/base32/src/main.rs +++ b/src/uu/base32/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_base32); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_base32); diff --git a/src/uu/base64/Cargo.toml b/src/uu/base64/Cargo.toml index eef9b1ec3..d4ee69f03 100644 --- a/src/uu/base64/Cargo.toml +++ b/src/uu/base64/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_base64" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "base64 ~ (uutils) decode/encode input (base64-encoding)" @@ -15,8 +15,10 @@ edition = "2018" path = "src/base64.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features = ["encoding"] } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features = ["encoding"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +uu_base32 = { version=">=0.0.6", package="uu_base32", path="../base32"} [[bin]] name = "base64" diff --git a/src/uu/base64/src/base64.rs b/src/uu/base64/src/base64.rs index ee60f3de5..71ed44e6e 100644 --- a/src/uu/base64/src/base64.rs +++ b/src/uu/base64/src/base64.rs @@ -8,13 +8,15 @@ #[macro_use] extern crate uucore; + +use uu_base32::base_common; +pub use uu_base32::uu_app; + use uucore::encoding::Format; -mod base_common; +use std::io::{stdin, Read}; -static SYNTAX: &str = "[OPTION]... [FILE]"; -static SUMMARY: &str = "Base64 encode or decode FILE, or standard input, to standard output."; -static LONG_HELP: &str = " +static ABOUT: &str = " With no FILE, or when FILE is -, read standard input. The data are encoded as described for the base64 alphabet in RFC @@ -23,13 +25,35 @@ static LONG_HELP: &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!()) +} pub fn uumain(args: impl uucore::Args) -> i32 { - base_common::execute( - args.collect_str(), - SYNTAX, - SUMMARY, - LONG_HELP, - Format::Base64, - ) + 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)); + + // 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, + name, + ); + + 0 } diff --git a/src/uu/base64/src/base_common.rs b/src/uu/base64/src/base_common.rs deleted file mode 100644 index 3f1436fb2..000000000 --- a/src/uu/base64/src/base_common.rs +++ /dev/null @@ -1,97 +0,0 @@ -// This file is part of the uutils coreutils package. -// -// (c) Jordy Dickinson -// (c) Jian Zeng -// (c) Alex Lyon -// -// For the full copyright and license information, please view the LICENSE file -// that was distributed with this source code. - -use std::fs::File; -use std::io::{stdin, stdout, BufReader, Read, Write}; -use std::path::Path; - -use uucore::encoding::{wrap_print, Data, Format}; - -pub fn execute( - args: Vec, - syntax: &str, - summary: &str, - long_help: &str, - format: Format, -) -> i32 { - let matches = app!(syntax, summary, long_help) - .optflag("d", "decode", "decode data") - .optflag( - "i", - "ignore-garbage", - "when decoding, ignore non-alphabetic characters", - ) - .optopt( - "w", - "wrap", - "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", - "COLS", - ) - .parse(args); - - let line_wrap = matches.opt_str("wrap").map(|s| match s.parse() { - Ok(n) => n, - Err(e) => { - crash!(1, "invalid wrap size: ‘{}’: {}", s, e); - } - }); - let ignore_garbage = matches.opt_present("ignore-garbage"); - let decode = matches.opt_present("decode"); - - if matches.free.len() > 1 { - show_usage_error!("extra operand ‘{}’", matches.free[0]); - return 1; - } - - if matches.free.is_empty() || &matches.free[0][..] == "-" { - let stdin_raw = stdin(); - handle_input( - &mut stdin_raw.lock(), - format, - line_wrap, - ignore_garbage, - decode, - ); - } else { - let path = Path::new(matches.free[0].as_str()); - let file_buf = safe_unwrap!(File::open(&path)); - let mut input = BufReader::new(file_buf); - handle_input(&mut input, format, line_wrap, ignore_garbage, decode); - }; - - 0 -} - -fn handle_input( - input: &mut R, - format: Format, - line_wrap: Option, - ignore_garbage: bool, - decode: bool, -) { - 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); - } else { - match data.decode() { - Ok(s) => { - if stdout().write_all(&s).is_err() { - // on windows console, writing invalid utf8 returns an error - crash!(1, "Cannot write non-utf8 data"); - } - } - Err(_) => crash!(1, "invalid input"), - } - } -} diff --git a/src/uu/base64/src/main.rs b/src/uu/base64/src/main.rs index 07ed34256..cae6cb3c4 100644 --- a/src/uu/base64/src/main.rs +++ b/src/uu/base64/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_base64); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_base64); diff --git a/src/uu/basename/Cargo.toml b/src/uu/basename/Cargo.toml index 218d9eb08..0072619b7 100644 --- a/src/uu/basename/Cargo.toml +++ b/src/uu/basename/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_basename" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "basename ~ (uutils) display PATHNAME with leading directory components removed" @@ -15,7 +15,8 @@ edition = "2018" path = "src/basename.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33.2" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/basename/src/basename.rs b/src/uu/basename/src/basename.rs index 84521bdd1..5450ee3f2 100644 --- a/src/uu/basename/src/basename.rs +++ b/src/uu/basename/src/basename.rs @@ -10,98 +10,117 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::path::{is_separator, PathBuf}; +use uucore::InvalidEncodingHandling; -static NAME: &str = "basename"; -static SYNTAX: &str = "NAME [SUFFIX]"; static SUMMARY: &str = "Print NAME with any leading directory components removed - If specified, also remove a trailing SUFFIX"; -static LONG_HELP: &str = ""; +If specified, also remove a trailing SUFFIX"; + +fn get_usage() -> String { + format!( + "{0} NAME [SUFFIX] + {0} OPTION... NAME...", + executable!() + ) +} + +pub mod options { + pub static MULTIPLE: &str = "multiple"; + pub static NAME: &str = "name"; + pub static SUFFIX: &str = "suffix"; + pub static ZERO: &str = "zero"; +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); + let usage = get_usage(); // // Argument parsing // - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag( - "a", - "multiple", - "Support more than one argument. Treat every argument as a name.", - ) - .optopt( - "s", - "suffix", - "Remove a trailing suffix. This option implies the -a option.", - "SUFFIX", - ) - .optflag( - "z", - "zero", - "Output a zero byte (ASCII NUL) at the end of each line, rather than a newline.", - ) - .parse(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); // too few arguments - if matches.free.is_empty() { + if !matches.is_present(options::NAME) { crash!( 1, - "{0}: {1}\nTry '{0} --help' for more information.", - NAME, + "{1}\nTry '{0} --help' for more information.", + executable!(), "missing operand" ); } - let opt_s = matches.opt_present("s"); - let opt_a = matches.opt_present("a"); - let opt_z = matches.opt_present("z"); - let multiple_paths = opt_s || opt_a; + + let opt_suffix = matches.is_present(options::SUFFIX); + let opt_multiple = matches.is_present(options::MULTIPLE); + let opt_zero = matches.is_present(options::ZERO); + let multiple_paths = opt_suffix || opt_multiple; // too many arguments - if !multiple_paths && matches.free.len() > 2 { + if !multiple_paths && matches.occurrences_of(options::NAME) > 2 { crash!( 1, - "{0}: extra operand '{1}'\nTry '{0} --help' for more information.", - NAME, - matches.free[2] + "extra operand '{1}'\nTry '{0} --help' for more information.", + executable!(), + matches.values_of(options::NAME).unwrap().nth(2).unwrap() ); } - let suffix = if opt_s { - matches.opt_str("s").unwrap() - } else if !opt_a && matches.free.len() > 1 { - matches.free[1].clone() + let suffix = if opt_suffix { + matches.value_of(options::SUFFIX).unwrap() + } else if !opt_multiple && matches.occurrences_of(options::NAME) > 1 { + matches.values_of(options::NAME).unwrap().nth(1).unwrap() } else { - "".to_owned() + "" }; // // Main Program Processing // - let paths = if multiple_paths { - &matches.free[..] + let paths: Vec<_> = if multiple_paths { + matches.values_of(options::NAME).unwrap().collect() } else { - &matches.free[0..1] + matches.values_of(options::NAME).unwrap().take(1).collect() }; - let line_ending = if opt_z { "\0" } else { "\n" }; + let line_ending = if opt_zero { "\0" } else { "\n" }; for path in paths { - print!("{}{}", basename(&path, &suffix), line_ending); + print!("{}{}", basename(path, suffix), line_ending); } 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(SUMMARY) + .arg( + Arg::with_name(options::MULTIPLE) + .short("a") + .long(options::MULTIPLE) + .help("support multiple arguments and treat each as a NAME"), + ) + .arg(Arg::with_name(options::NAME).multiple(true).hidden(true)) + .arg( + Arg::with_name(options::SUFFIX) + .short("s") + .long(options::SUFFIX) + .value_name("SUFFIX") + .help("remove a trailing SUFFIX; implies -a"), + ) + .arg( + Arg::with_name(options::ZERO) + .short("z") + .long(options::ZERO) + .help("end each output line with NUL, not newline"), + ) +} + fn basename(fullname: &str, suffix: &str) -> String { // Remove all platform-specific path separators from the end - let mut path: String = fullname - .chars() - .rev() - .skip_while(|&ch| is_separator(ch)) - .collect(); - - // Undo reverse - path = path.chars().rev().collect(); + let path = fullname.trim_end_matches(is_separator); // Convert to path buffer and get last path component let pb = PathBuf::from(path); @@ -111,6 +130,8 @@ fn basename(fullname: &str, suffix: &str) -> String { } } +// 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(); diff --git a/src/uu/basename/src/main.rs b/src/uu/basename/src/main.rs index d1aa19f2b..aa452f750 100644 --- a/src/uu/basename/src/main.rs +++ b/src/uu/basename/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_basename); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_basename); diff --git a/src/uu/cat/Cargo.toml b/src/uu/cat/Cargo.toml index 415477588..9218e84fe 100644 --- a/src/uu/cat/Cargo.toml +++ b/src/uu/cat/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cat" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "cat ~ (uutils) concatenate and display input" @@ -15,13 +15,18 @@ edition = "2018" path = "src/cat.rs" [dependencies] -quick-error = "1.2.3" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +clap = "2.33" +thiserror = "1.0" +atty = "0.2" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [target.'cfg(unix)'.dependencies] unix_socket = "0.5.0" +[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] +nix = "0.20" + [[bin]] name = "cat" path = "src/main.rs" diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 0a35e243b..35a5308ed 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -3,24 +3,29 @@ // (c) Jordi Boggiano // (c) Evgeniy Klyuchikov // (c) Joshua S. Miller +// (c) Árni Dagur // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. // spell-checker:ignore (ToDO) nonprint nonblank nonprinting -#[macro_use] -extern crate quick_error; #[cfg(unix)] extern crate unix_socket; #[macro_use] extern crate uucore; // last synced with: cat (GNU coreutils) 8.13 -use quick_error::ResultExt; +use clap::{crate_version, App, Arg}; use std::fs::{metadata, File}; -use std::io::{self, stderr, stdin, stdout, BufWriter, Read, Write}; -use uucore::fs::is_stdin_interactive; +use std::io::{self, Read, Write}; +use thiserror::Error; + +/// 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)] @@ -29,11 +34,33 @@ use std::net::Shutdown; use std::os::unix::fs::FileTypeExt; #[cfg(unix)] use unix_socket::UnixStream; +use uucore::InvalidEncodingHandling; +static NAME: &str = "cat"; static SYNTAX: &str = "[OPTION]... [FILE]..."; static SUMMARY: &str = "Concatenate FILE(s), or standard input, to standard output With no FILE, or when FILE is -, read standard input."; -static LONG_HELP: &str = ""; + +#[derive(Error, Debug)] +enum CatError { + /// Wrapper around `io::Error` + #[error("{0}")] + Io(#[from] io::Error), + /// Wrapper around `nix::Error` + #[cfg(any(target_os = "linux", target_os = "android"))] + #[error("{0}")] + Nix(#[from] nix::Error), + /// Unknown file type; it's not a regular file, socket, etc. + #[error("unknown filetype: {}", ft_debug)] + UnknownFiletype { + /// A debug print of the file type + ft_debug: String, + }, + #[error("Is a directory")] + IsDirectory, +} + +type CatResult = Result; #[derive(PartialEq)] enum NumberingMode { @@ -42,39 +69,6 @@ enum NumberingMode { All, } -quick_error! { - #[derive(Debug)] - enum CatError { - /// Wrapper for io::Error with path context - Input(err: io::Error, path: String) { - display("cat: {0}: {1}", path, err) - context(path: &'a str, err: io::Error) -> (err, path.to_owned()) - cause(err) - } - - /// Wrapper for io::Error with no context - Output(err: io::Error) { - display("cat: {0}", err) from() - cause(err) - } - - /// Unknown Filetype classification - UnknownFiletype(path: String) { - display("cat: {0}: unknown filetype", path) - } - - /// At least one error was encountered in reading or writing - EncounteredErrors(count: usize) { - display("cat: encountered {0} errors", count) - } - - /// Denotes an error caused by trying to `cat` a directory - IsDirectory(path: String) { - display("cat: {0}: Is a directory", path) - } - } -} - struct OutputOptions { /// Line numbering mode number: NumberingMode, @@ -85,21 +79,56 @@ struct OutputOptions { /// display TAB characters as `tab` show_tabs: bool, - /// If `show_tabs == true`, this string will be printed in the - /// place of tabs - tab: String, - - /// Can be set to show characters other than '\n' a the end of - /// each line, e.g. $ - end_of_line: String, + /// Show end of lines + show_ends: bool, /// use ^ and M- notation, except for LF (\\n) and TAB (\\t) show_nonprint: bool, } +impl OutputOptions { + fn tab(&self) -> &'static str { + if self.show_tabs { + "^I" + } else { + "\t" + } + } + + fn end_of_line(&self) -> &'static str { + if self.show_ends { + "$\n" + } else { + "\n" + } + } + + /// We can write fast if we can simply copy the contents of the file to + /// stdout, without augmenting the output with e.g. line numbers. + fn can_write_fast(&self) -> bool { + !(self.show_tabs + || self.show_nonprint + || self.show_ends + || self.squeeze_blank + || self.number != NumberingMode::None) + } +} + +/// State that persists between output of each file. This struct is only used +/// when we can't write fast. +struct OutputState { + /// The current line number + line_number: usize, + + /// Whether the output cursor is at the beginning of a new line + at_line_start: bool, +} + /// Represents an open file handle, stream, or other device -struct InputHandle { - reader: Box, +struct InputHandle { + #[cfg(any(target_os = "linux", target_os = "android"))] + file_descriptor: RawFd, + reader: R, is_interactive: bool, } @@ -122,78 +151,74 @@ enum InputType { Socket, } -type CatResult = Result; +mod options { + pub static FILE: &str = "file"; + pub static SHOW_ALL: &str = "show-all"; + pub static NUMBER_NONBLANK: &str = "number-nonblank"; + pub static SHOW_NONPRINTING_ENDS: &str = "e"; + pub static SHOW_ENDS: &str = "show-ends"; + pub static NUMBER: &str = "number"; + pub static SQUEEZE_BLANK: &str = "squeeze-blank"; + pub static SHOW_NONPRINTING_TABS: &str = "t"; + pub static SHOW_TABS: &str = "show-tabs"; + pub static SHOW_NONPRINTING: &str = "show-nonprinting"; +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag("A", "show-all", "equivalent to -vET") - .optflag( - "b", - "number-nonblank", - "number nonempty output lines, overrides -n", - ) - .optflag("e", "", "equivalent to -vE") - .optflag("E", "show-ends", "display $ at end of each line") - .optflag("n", "number", "number all output lines") - .optflag("s", "squeeze-blank", "suppress repeated empty output lines") - .optflag("t", "", "equivalent to -vT") - .optflag("T", "show-tabs", "display TAB characters as ^I") - .optflag( - "v", - "show-nonprinting", - "use ^ and M- notation, except for LF (\\n) and TAB (\\t)", - ) - .parse(args); + let matches = uu_app().get_matches_from(args); - let number_mode = if matches.opt_present("b") { + let number_mode = if matches.is_present(options::NUMBER_NONBLANK) { NumberingMode::NonEmpty - } else if matches.opt_present("n") { + } else if matches.is_present(options::NUMBER) { NumberingMode::All } else { NumberingMode::None }; - let show_nonprint = matches.opts_present(&[ - "A".to_owned(), - "e".to_owned(), - "t".to_owned(), - "v".to_owned(), - ]); - let show_ends = matches.opts_present(&["E".to_owned(), "A".to_owned(), "e".to_owned()]); - let show_tabs = matches.opts_present(&["A".to_owned(), "T".to_owned(), "t".to_owned()]); - let squeeze_blank = matches.opt_present("s"); - let mut files = matches.free; - if files.is_empty() { - files.push("-".to_owned()); - } + let show_nonprint = vec![ + options::SHOW_ALL.to_owned(), + options::SHOW_NONPRINTING_ENDS.to_owned(), + options::SHOW_NONPRINTING_TABS.to_owned(), + options::SHOW_NONPRINTING.to_owned(), + ] + .iter() + .any(|v| matches.is_present(v)); - let can_write_fast = !(show_tabs - || show_nonprint - || show_ends - || squeeze_blank - || number_mode != NumberingMode::None); + let show_ends = vec![ + options::SHOW_ENDS.to_owned(), + options::SHOW_ALL.to_owned(), + options::SHOW_NONPRINTING_ENDS.to_owned(), + ] + .iter() + .any(|v| matches.is_present(v)); - let success = if can_write_fast { - write_fast(files).is_ok() - } else { - let tab = if show_tabs { "^I" } else { "\t" }.to_owned(); + let show_tabs = vec![ + options::SHOW_ALL.to_owned(), + options::SHOW_TABS.to_owned(), + options::SHOW_NONPRINTING_TABS.to_owned(), + ] + .iter() + .any(|v| matches.is_present(v)); - let end_of_line = if show_ends { "$\n" } else { "\n" }.to_owned(); - - let options = OutputOptions { - end_of_line, - number: number_mode, - show_nonprint, - show_tabs, - squeeze_blank, - tab, - }; - - write_lines(files, &options).is_ok() + let squeeze_blank = matches.is_present(options::SQUEEZE_BLANK); + let files: Vec = match matches.values_of(options::FILE) { + Some(v) => v.clone().map(|v| v.to_owned()).collect(), + None => vec!["-".to_owned()], }; + let options = OutputOptions { + show_ends, + number: number_mode, + show_nonprint, + show_tabs, + squeeze_blank, + }; + let success = cat_files(files, &options).is_ok(); + if success { 0 } else { @@ -201,6 +226,139 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(SYNTAX) + .about(SUMMARY) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) + .arg( + Arg::with_name(options::SHOW_ALL) + .short("A") + .long(options::SHOW_ALL) + .help("equivalent to -vET"), + ) + .arg( + Arg::with_name(options::NUMBER_NONBLANK) + .short("b") + .long(options::NUMBER_NONBLANK) + .help("number nonempty output lines, overrides -n") + .overrides_with(options::NUMBER), + ) + .arg( + Arg::with_name(options::SHOW_NONPRINTING_ENDS) + .short("e") + .help("equivalent to -vE"), + ) + .arg( + Arg::with_name(options::SHOW_ENDS) + .short("E") + .long(options::SHOW_ENDS) + .help("display $ at end of each line"), + ) + .arg( + Arg::with_name(options::NUMBER) + .short("n") + .long(options::NUMBER) + .help("number all output lines"), + ) + .arg( + Arg::with_name(options::SQUEEZE_BLANK) + .short("s") + .long(options::SQUEEZE_BLANK) + .help("suppress repeated empty output lines"), + ) + .arg( + Arg::with_name(options::SHOW_NONPRINTING_TABS) + .short("t") + .long(options::SHOW_NONPRINTING_TABS) + .help("equivalent to -vT"), + ) + .arg( + Arg::with_name(options::SHOW_TABS) + .short("T") + .long(options::SHOW_TABS) + .help("display TAB characters at ^I"), + ) + .arg( + Arg::with_name(options::SHOW_NONPRINTING) + .short("v") + .long(options::SHOW_NONPRINTING) + .help("use ^ and M- notation, except for LF (\\n) and TAB (\\t)"), + ) +} + +fn cat_handle( + handle: &mut InputHandle, + options: &OutputOptions, + state: &mut OutputState, +) -> CatResult<()> { + if options.can_write_fast() { + write_fast(handle) + } else { + write_lines(handle, options, state) + } +} + +fn cat_path(path: &str, options: &OutputOptions, state: &mut OutputState) -> 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), + }; + return cat_handle(&mut handle, options, state); + } + match get_input_type(path)? { + InputType::Directory => Err(CatError::IsDirectory), + #[cfg(unix)] + InputType::Socket => { + 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, + }; + cat_handle(&mut handle, options, state) + } + _ => { + let file = File::open(path)?; + let mut handle = InputHandle { + #[cfg(any(target_os = "linux", target_os = "android"))] + file_descriptor: file.as_raw_fd(), + reader: file, + is_interactive: false, + }; + cat_handle(&mut handle, options, state) + } + } +} + +fn cat_files(files: Vec, options: &OutputOptions) -> Result<(), u32> { + let mut error_count = 0; + let mut state = OutputState { + line_number: 1, + at_line_start: true, + }; + + for path in &files { + if let Err(err) = cat_path(path, options, &mut state) { + show_error!("{}: {}", path, err); + error_count += 1; + } + } + if error_count == 0 { + Ok(()) + } else { + Err(error_count) + } +} + /// Classifies the `InputType` of file at `path` if possible /// /// # Arguments @@ -211,7 +369,8 @@ fn get_input_type(path: &str) -> CatResult { return Ok(InputType::StdIn); } - match metadata(path).context(path)?.file_type() { + let ft = metadata(path)?.file_type(); + match ft { #[cfg(unix)] ft if ft.is_block_device() => Ok(InputType::BlockDevice), #[cfg(unix)] @@ -223,125 +382,47 @@ fn get_input_type(path: &str) -> CatResult { ft if ft.is_dir() => Ok(InputType::Directory), ft if ft.is_file() => Ok(InputType::File), ft if ft.is_symlink() => Ok(InputType::SymLink), - _ => Err(CatError::UnknownFiletype(path.to_owned())), + _ => Err(CatError::UnknownFiletype { + ft_debug: format!("{:?}", ft), + }), } } -/// Returns an InputHandle from which a Reader can be accessed or an -/// error -/// -/// # Arguments -/// -/// * `path` - `InputHandler` will wrap a reader from this file path -fn open(path: &str) -> CatResult { - if path == "-" { - let stdin = stdin(); - return Ok(InputHandle { - reader: Box::new(stdin) as Box, - is_interactive: is_stdin_interactive(), - }); - } - - match get_input_type(path)? { - InputType::Directory => Err(CatError::IsDirectory(path.to_owned())), - #[cfg(unix)] - InputType::Socket => { - let socket = UnixStream::connect(path).context(path)?; - socket.shutdown(Shutdown::Write).context(path)?; - Ok(InputHandle { - reader: Box::new(socket) as Box, - is_interactive: false, - }) - } - _ => { - let file = File::open(path).context(path)?; - Ok(InputHandle { - reader: Box::new(file) as Box, - is_interactive: false, - }) +/// Writes handle to stdout with no configuration. This allows a +/// simple memory copy. +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())? { + return Ok(()); } } -} - -/// Writes files to stdout with no configuration. This allows a -/// simple memory copy. Returns `Ok(())` if no errors were -/// encountered, or an error with the number of errors encountered. -/// -/// # Arguments -/// -/// * `files` - There is no short circuit when encountering an error -/// reading a file in this vector -fn write_fast(files: Vec) -> CatResult<()> { - let mut writer = stdout(); - let mut in_buf = [0; 1024 * 64]; - let mut error_count = 0; - - for file in files { - match open(&file[..]) { - Ok(mut handle) => { - while let Ok(n) = handle.reader.read(&mut in_buf) { - if n == 0 { - break; - } - writer.write_all(&in_buf[..n]).context(&file[..])?; - } - } - Err(error) => { - writeln!(&mut stderr(), "{}", error)?; - error_count += 1; - } + // If we're not on Linux or Android, or the splice() call failed, + // fall back on slower writing. + let mut buf = [0; 1024 * 64]; + while let Ok(n) = handle.reader.read(&mut buf) { + if n == 0 { + break; } + stdout_lock.write_all(&buf[..n])?; } - - match error_count { - 0 => Ok(()), - _ => Err(CatError::EncounteredErrors(error_count)), - } -} - -/// State that persists between output of each file -struct OutputState { - /// The current line number - line_number: usize, - - /// Whether the output cursor is at the beginning of a new line - at_line_start: bool, -} - -/// Writes files to stdout with `options` as configuration. Returns -/// `Ok(())` if no errors were encountered, or an error with the -/// number of errors encountered. -/// -/// # Arguments -/// -/// * `files` - There is no short circuit when encountering an error -/// reading a file in this vector -fn write_lines(files: Vec, options: &OutputOptions) -> CatResult<()> { - let mut error_count = 0; - let mut state = OutputState { - line_number: 1, - at_line_start: true, - }; - - for file in files { - if let Err(error) = write_file_lines(&file, options, &mut state) { - writeln!(&mut stderr(), "{}", error).context(&file[..])?; - error_count += 1; - } - } - - match error_count { - 0 => Ok(()), - _ => Err(CatError::EncounteredErrors(error_count)), - } + Ok(()) } /// Outputs file contents to stdout in a line-by-line fashion, /// propagating any errors that might occur. -fn write_file_lines(file: &str, options: &OutputOptions, state: &mut OutputState) -> CatResult<()> { - let mut handle = open(file)?; +fn write_lines( + handle: &mut InputHandle, + options: &OutputOptions, + state: &mut OutputState, +) -> CatResult<()> { let mut in_buf = [0; 1024 * 31]; - let mut writer = BufWriter::with_capacity(1024 * 64, stdout()); + 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) { @@ -359,9 +440,9 @@ fn write_file_lines(file: &str, options: &OutputOptions, state: &mut OutputState write!(&mut writer, "{0:6}\t", state.line_number)?; state.line_number += 1; } - writer.write_all(options.end_of_line.as_bytes())?; + writer.write_all(options.end_of_line().as_bytes())?; if handle.is_interactive { - writer.flush().context(&file[..])?; + writer.flush()?; } } state.at_line_start = true; @@ -376,7 +457,7 @@ fn write_file_lines(file: &str, options: &OutputOptions, state: &mut OutputState // print to end of line or end of buffer let offset = if options.show_nonprint { - write_nonprint_to_end(&in_buf[pos..], &mut writer, options.tab.as_bytes()) + write_nonprint_to_end(&in_buf[pos..], &mut writer, options.tab().as_bytes()) } else if options.show_tabs { write_tab_to_end(&in_buf[pos..], &mut writer) } else { @@ -388,7 +469,7 @@ fn write_file_lines(file: &str, options: &OutputOptions, state: &mut OutputState break; } // print suitable end of line - writer.write_all(options.end_of_line.as_bytes())?; + writer.write_all(options.end_of_line().as_bytes())?; if handle.is_interactive { writer.flush()?; } diff --git a/src/uu/cat/src/main.rs b/src/uu/cat/src/main.rs index 2b7969473..1adab666b 100644 --- a/src/uu/cat/src/main.rs +++ b/src/uu/cat/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_cat); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_cat); diff --git a/src/uu/cat/src/splice.rs b/src/uu/cat/src/splice.rs new file mode 100644 index 000000000..0b46fc662 --- /dev/null +++ b/src/uu/cat/src/splice.rs @@ -0,0 +1,91 @@ +use super::{CatResult, InputHandle}; + +use nix::fcntl::{splice, SpliceFFlags}; +use nix::unistd::{self, pipe}; +use std::io::Read; +use std::os::unix::io::RawFd; + +const BUF_SIZE: usize = 1024 * 16; + +/// This function is called from `write_fast()` on Linux and Android. The +/// function `splice()` is used to move data between two file descriptors +/// without copying between kernel and user spaces. This results in a large +/// speedup. +/// +/// 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( + handle: &mut InputHandle, + write_fd: RawFd, +) -> 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); + } + }; + + loop { + match splice( + handle.file_descriptor, + None, + pipe_wr, + None, + BUF_SIZE, + SpliceFFlags::empty(), + ) { + Ok(n) => { + if n == 0 { + return Ok(false); + } + 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)?; + return Ok(true); + } + } + Err(_) => { + return Ok(true); + } + } + } +} + +/// 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] +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; + } + } + Ok(()) +} diff --git a/src/uu/chgrp/Cargo.toml b/src/uu/chgrp/Cargo.toml index 60f9d6370..0e43f7c02 100644 --- a/src/uu/chgrp/Cargo.toml +++ b/src/uu/chgrp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chgrp" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "chgrp ~ (uutils) change the group ownership of FILE" @@ -15,7 +15,8 @@ edition = "2018" path = "src/chgrp.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } walkdir = "2.2" diff --git a/src/uu/chgrp/src/chgrp.rs b/src/uu/chgrp/src/chgrp.rs index b4c3360c5..489be59eb 100644 --- a/src/uu/chgrp/src/chgrp.rs +++ b/src/uu/chgrp/src/chgrp.rs @@ -14,6 +14,8 @@ use uucore::fs::resolve_relative_path; use uucore::libc::gid_t; use uucore::perms::{wrap_chgrp, Verbosity}; +use clap::{App, Arg}; + extern crate walkdir; use walkdir::WalkDir; @@ -22,79 +24,123 @@ use std::fs::Metadata; use std::os::unix::fs::MetadataExt; use std::path::Path; +use uucore::InvalidEncodingHandling; -static SYNTAX: &str = - "chgrp [OPTION]... GROUP FILE...\n or : chgrp [OPTION]... --reference=RFILE FILE..."; -static SUMMARY: &str = "Change the group of each FILE to GROUP."; +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!() + ) +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = app!(SYNTAX, SUMMARY, ""); - opts.optflag("c", - "changes", - "like verbose but report only when a change is made") - .optflag("f", "silent", "") - .optflag("", "quiet", "suppress most error messages") - .optflag("v", - "verbose", - "output a diagnostic for every file processed") - .optflag("", "dereference", "affect the referent of each symbolic link (this is the default), rather than the symbolic link itself") - .optflag("h", "no-dereference", "affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)") - .optflag("", - "no-preserve-root", - "do not treat '/' specially (the default)") - .optflag("", "preserve-root", "fail to operate recursively on '/'") - .optopt("", - "reference", - "use RFILE's owner and group rather than specifying OWNER:GROUP values", - "RFILE") - .optflag("R", - "recursive", - "operate on files and directories recursively") - .optflag("H", - "", - "if a command line argument is a symbolic link to a directory, traverse it") - .optflag("L", - "", - "traverse every symbolic link to a directory encountered") - .optflag("P", "", "do not traverse any symbolic links (default)"); + let usage = get_usage(); - let mut bit_flag = FTS_PHYSICAL; - let mut preserve_root = false; - let mut derefer = -1; - let flags: &[char] = &['H', 'L', 'P']; - for opt in &args { - match opt.as_str() { - // If more than one is specified, only the final one takes effect. - s if s.contains(flags) => { - if let Some(idx) = s.rfind(flags) { - match s.chars().nth(idx).unwrap() { - 'H' => bit_flag = FTS_COMFOLLOW | FTS_PHYSICAL, - 'L' => bit_flag = FTS_LOGICAL, - 'P' => bit_flag = FTS_PHYSICAL, - _ => (), - } - } - } - "--no-preserve-root" => preserve_root = false, - "--preserve-root" => preserve_root = true, - "--dereference" => derefer = 1, - "--no-dereference" => derefer = 0, - _ => (), + 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; } } - let matches = opts.parse(args); - let recursive = matches.opt_present("recursive"); + 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_info!("-R --dereference requires -H or -L"); + show_error!("-R --dereference requires -H or -L"); return 1; } derefer = 0; @@ -103,49 +149,40 @@ pub fn uumain(args: impl uucore::Args) -> i32 { bit_flag = FTS_PHYSICAL; } - let verbosity = if matches.opt_present("changes") { + let verbosity = if matches.is_present(options::verbosity::CHANGES) { Verbosity::Changes - } else if matches.opt_present("silent") || matches.opt_present("quiet") { + } else if matches.is_present(options::verbosity::SILENT) + || matches.is_present(options::verbosity::QUIET) + { Verbosity::Silent - } else if matches.opt_present("verbose") { + } else if matches.is_present(options::verbosity::VERBOSE) { Verbosity::Verbose } else { Verbosity::Normal }; - if matches.free.is_empty() { - show_usage_error!("missing operand"); - return 1; - } else if matches.free.len() < 2 && !matches.opt_present("reference") { - show_usage_error!("missing operand after ‘{}’", matches.free[0]); - return 1; - } - - let dest_gid: gid_t; - let mut files; - if let Some(file) = matches.opt_str("reference") { + 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_info!("failed to get attributes of '{}': {}", file, e); + show_error!("failed to get attributes of '{}': {}", file, e); return 1; } } - files = matches.free; } else { - match entries::grp2gid(&matches.free[0]) { + let group = matches.value_of(options::ARG_GROUP).unwrap_or_default(); + match entries::grp2gid(group) { Ok(g) => { dest_gid = g; } _ => { - show_info!("invalid group: {}", matches.free[0].as_str()); + show_error!("invalid group: {}", group); return 1; } } - files = matches.free; - files.remove(0); } let executor = Chgrper { @@ -160,6 +197,86 @@ pub fn uumain(args: impl uucore::Args) -> i32 { executor.exec() } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(VERSION) + .about(ABOUT) + .arg( + Arg::with_name(options::verbosity::CHANGES) + .short("c") + .long(options::verbosity::CHANGES) + .help("like verbose but report only when a change is made"), + ) + .arg( + Arg::with_name(options::verbosity::SILENT) + .short("f") + .long(options::verbosity::SILENT), + ) + .arg( + Arg::with_name(options::verbosity::QUIET) + .long(options::verbosity::QUIET) + .help("suppress most error messages"), + ) + .arg( + Arg::with_name(options::verbosity::VERBOSE) + .short("v") + .long(options::verbosity::VERBOSE) + .help("output a diagnostic for every file processed"), + ) + .arg( + Arg::with_name(options::dereference::DEREFERENCE) + .long(options::dereference::DEREFERENCE), + ) + .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)", + ), + ) + .arg( + Arg::with_name(options::preserve_root::PRESERVE) + .long(options::preserve_root::PRESERVE) + .help("fail to operate recursively on '/'"), + ) + .arg( + Arg::with_name(options::preserve_root::NO_PRESERVE) + .long(options::preserve_root::NO_PRESERVE) + .help("do not treat '/' specially (the default)"), + ) + .arg( + Arg::with_name(options::REFERENCE) + .long(options::REFERENCE) + .value_name("RFILE") + .help("use RFILE's group rather than specifying GROUP values") + .takes_value(true) + .multiple(false), + ) + .arg( + Arg::with_name(options::RECURSIVE) + .short("R") + .long(options::RECURSIVE) + .help("operate on files and directories recursively"), + ) + .arg( + Arg::with_name(options::traverse::TRAVERSE) + .short(options::traverse::TRAVERSE) + .help("if a command line argument is a symbolic link to a directory, traverse it"), + ) + .arg( + Arg::with_name(options::traverse::NO_TRAVERSE) + .short(options::traverse::NO_TRAVERSE) + .help("do not traverse any symbolic links (default)") + .overrides_with_all(&[options::traverse::TRAVERSE, options::traverse::EVERY]), + ) + .arg( + Arg::with_name(options::traverse::EVERY) + .short(options::traverse::EVERY) + .help("traverse every symbolic link to a directory encountered"), + ) +} + struct Chgrper { dest_gid: gid_t, bit_flag: u8, @@ -232,8 +349,8 @@ impl Chgrper { if let Some(p) = may_exist { if p.parent().is_none() || self.is_bind_root(p) { - show_info!("it is dangerous to operate recursively on '/'"); - show_info!("use --no-preserve-root to override this failsafe"); + show_error!("it is dangerous to operate recursively on '/'"); + show_error!("use --no-preserve-root to override this failsafe"); return 1; } } @@ -247,12 +364,12 @@ impl Chgrper { self.verbosity.clone(), ) { Ok(n) => { - show_info!("{}", n); + show_error!("{}", n); 0 } Err(e) => { if self.verbosity != Verbosity::Silent { - show_info!("{}", e); + show_error!("{}", e); } 1 } @@ -272,7 +389,7 @@ impl Chgrper { for entry in WalkDir::new(root).follow_links(follow).min_depth(1) { let entry = unwrap!(entry, e, { ret = 1; - show_info!("{}", e); + show_error!("{}", e); continue; }); let path = entry.path(); @@ -286,14 +403,14 @@ impl Chgrper { ret = match wrap_chgrp(path, &meta, self.dest_gid, follow, self.verbosity.clone()) { Ok(n) => { - if n != "" { - show_info!("{}", n); + if !n.is_empty() { + show_error!("{}", n); } 0 } Err(e) => { if self.verbosity != Verbosity::Silent { - show_info!("{}", e); + show_error!("{}", e); } 1 } @@ -310,7 +427,7 @@ impl Chgrper { unwrap!(path.metadata(), e, { match self.verbosity { Silent => (), - _ => show_info!("cannot access '{}': {}", path.display(), e), + _ => show_error!("cannot access '{}': {}", path.display(), e), } return None; }) @@ -318,7 +435,7 @@ impl Chgrper { unwrap!(path.symlink_metadata(), e, { match self.verbosity { Silent => (), - _ => show_info!("cannot dereference '{}': {}", path.display(), e), + _ => show_error!("cannot dereference '{}': {}", path.display(), e), } return None; }) diff --git a/src/uu/chgrp/src/main.rs b/src/uu/chgrp/src/main.rs index 2faba7ba4..ee6f70a8b 100644 --- a/src/uu/chgrp/src/main.rs +++ b/src/uu/chgrp/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_chgrp); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_chgrp); diff --git a/src/uu/chmod/Cargo.toml b/src/uu/chmod/Cargo.toml index 41b73d8a6..ac7030b62 100644 --- a/src/uu/chmod/Cargo.toml +++ b/src/uu/chmod/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chmod" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "chmod ~ (uutils) change mode of FILE" @@ -15,8 +15,9 @@ edition = "2018" path = "src/chmod.rs" [dependencies] +clap = "2.33.3" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs", "mode"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs", "mode"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } walkdir = "2.2" diff --git a/src/uu/chmod/src/chmod.rs b/src/uu/chmod/src/chmod.rs index 61c56dd77..d89827c97 100644 --- a/src/uu/chmod/src/chmod.rs +++ b/src/uu/chmod/src/chmod.rs @@ -10,123 +10,190 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::fs; use std::os::unix::fs::{MetadataExt, PermissionsExt}; use std::path::Path; use uucore::fs::display_permissions_unix; +use uucore::libc::mode_t; #[cfg(not(windows))] use uucore::mode; +use uucore::InvalidEncodingHandling; use walkdir::WalkDir; -const NAME: &str = "chmod"; -static SUMMARY: &str = "Change the mode of each FILE to MODE. +static ABOUT: &str = "Change the mode of each FILE to MODE. With --reference, change the mode of each FILE to that of RFILE."; -static LONG_HELP: &str = " - Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=]?[0-7]+'. -"; + +mod options { + pub const CHANGES: &str = "changes"; + pub const QUIET: &str = "quiet"; // visible_alias("silent") + pub const VERBOSE: &str = "verbose"; + pub const NO_PRESERVE_ROOT: &str = "no-preserve-root"; + pub const PRESERVE_ROOT: &str = "preserve-root"; + pub const REFERENCE: &str = "RFILE"; + pub const RECURSIVE: &str = "recursive"; + pub const MODE: &str = "MODE"; + pub const FILE: &str = "FILE"; +} + +fn get_usage() -> String { + format!( + "{0} [OPTION]... MODE[,MODE]... FILE... +or: {0} [OPTION]... OCTAL-MODE FILE... +or: {0} [OPTION]... --reference=RFILE FILE...", + executable!() + ) +} + +fn get_long_usage() -> String { + String::from("Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=]?[0-7]+'.") +} pub fn uumain(args: impl uucore::Args) -> i32 { - let mut args = args.collect_str(); + let mut args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let syntax = format!( - "[OPTION]... MODE[,MODE]... FILE... - {0} [OPTION]... OCTAL-MODE FILE... - {0} [OPTION]... --reference=RFILE FILE...", - NAME - ); - let mut opts = app!(&syntax, SUMMARY, LONG_HELP); - opts.optflag( - "c", - "changes", - "like verbose but report only when a change is made", - ) - // TODO: support --silent (can be done using clap) - .optflag("f", "quiet", "suppress most error messages") - .optflag( - "v", - "verbose", - "output a diagnostic for every file processed", - ) - .optflag( - "", - "no-preserve-root", - "do not treat '/' specially (the default)", - ) - .optflag("", "preserve-root", "fail to operate recursively on '/'") - .optopt( - "", - "reference", - "use RFILE's mode instead of MODE values", - "RFILE", - ) - .optflag("R", "recursive", "change files and directories recursively"); + // 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); - // sanitize input for - at beginning (e.g. chmod -x test_file). Remove - // the option and save it for later, after parsing is finished. - let negative_option = sanitize_input(&mut args); + let usage = get_usage(); + let after_help = get_long_usage(); - let mut matches = opts.parse(args); - if matches.free.is_empty() { - show_error!("missing an argument"); - show_error!("for help, try '{} --help'", NAME); - return 1; + let matches = uu_app() + .usage(&usage[..]) + .after_help(&after_help[..]) + .get_matches_from(args); + + let changes = matches.is_present(options::CHANGES); + let quiet = matches.is_present(options::QUIET); + let verbose = matches.is_present(options::VERBOSE); + let preserve_root = matches.is_present(options::PRESERVE_ROOT); + let recursive = matches.is_present(options::RECURSIVE); + let fmode = matches + .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), + }); + let modes = matches.value_of(options::MODE).unwrap(); // should always be Some because required + let cmode = if mode_had_minus_prefix { + // clap parsing is finished, now put prefix back + format!("-{}", modes) } else { - let changes = matches.opt_present("changes"); - let quiet = matches.opt_present("quiet"); - let verbose = matches.opt_present("verbose"); - let preserve_root = matches.opt_present("preserve-root"); - let recursive = matches.opt_present("recursive"); - let fmode = matches - .opt_str("reference") - .and_then(|ref fref| match fs::metadata(fref) { - Ok(meta) => Some(meta.mode()), - Err(err) => crash!(1, "cannot stat attributes of '{}': {}", fref, err), - }); - let cmode = if fmode.is_none() { - // If there was a negative option, now it's a good time to - // use it. - if negative_option.is_some() { - negative_option - } else { - Some(matches.free.remove(0)) - } - } else { - None - }; - let chmoder = Chmoder { - changes, - quiet, - verbose, - preserve_root, - recursive, - fmode, - cmode, - }; - match chmoder.chmod(matches.free) { - Ok(()) => {} - Err(e) => return e, - } + modes.to_string() + }; + let mut files: Vec = matches + .values_of(options::FILE) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + let cmode = if fmode.is_some() { + // "--reference" and MODE are mutually exclusive + // if "--reference" was used MODE needs to be interpreted as another FILE + // it wasn't possible to implement this behavior directly with clap + files.push(cmode); + None + } else { + Some(cmode) + }; + + let chmoder = Chmoder { + changes, + quiet, + verbose, + preserve_root, + recursive, + fmode, + cmode, + }; + match chmoder.chmod(files) { + Ok(()) => {} + Err(e) => return e, } 0 } -fn sanitize_input(args: &mut Vec) -> Option { - for i in 0..args.len() { - let first = args[i].chars().next().unwrap(); - if first != '-' { - continue; - } - if let Some(second) = args[i].chars().nth(1) { - match second { - 'r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7' => { - return Some(args.remove(i)); +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::CHANGES) + .long(options::CHANGES) + .short("c") + .help("like verbose but report only when a change is made"), + ) + .arg( + Arg::with_name(options::QUIET) + .long(options::QUIET) + .visible_alias("silent") + .short("f") + .help("suppress most error messages"), + ) + .arg( + Arg::with_name(options::VERBOSE) + .long(options::VERBOSE) + .short("v") + .help("output a diagnostic for every file processed"), + ) + .arg( + Arg::with_name(options::NO_PRESERVE_ROOT) + .long(options::NO_PRESERVE_ROOT) + .help("do not treat '/' specially (the default)"), + ) + .arg( + Arg::with_name(options::PRESERVE_ROOT) + .long(options::PRESERVE_ROOT) + .help("fail to operate recursively on '/'"), + ) + .arg( + Arg::with_name(options::RECURSIVE) + .long(options::RECURSIVE) + .short("R") + .help("change files and directories recursively"), + ) + .arg( + Arg::with_name(options::REFERENCE) + .long("reference") + .takes_value(true) + .help("use RFILE's mode instead of MODE values"), + ) + .arg( + Arg::with_name(options::MODE) + .required_unless(options::REFERENCE) + .takes_value(true), + // It would be nice if clap could parse with delimiter, e.g. "g-x,u+x", + // however .multiple(true) cannot be used here because FILE already needs that. + // Only one positional argument with .multiple(true) set is allowed per command + ) + .arg( + Arg::with_name(options::FILE) + .required_unless(options::MODE) + .multiple(true), + ) +} + +// 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; + } + _ => {} } - _ => {} } } } - None + false } struct Chmoder { @@ -147,7 +214,17 @@ impl Chmoder { let filename = &filename[..]; let file = Path::new(filename); if !file.exists() { - show_error!("no such file or directory '{}'", filename); + if is_symlink(file) { + println!( + "failed to change mode of '{}' from 0000 (---------) to 0000 (---------)", + filename + ); + if !self.quiet { + show_error!("cannot operate on dangling symlink '{}'", filename); + } + } else { + show_error!("cannot access '{}': No such file or directory", filename); + } return Err(1); } if self.recursive && self.preserve_root && filename == "/" { @@ -158,11 +235,11 @@ impl Chmoder { return Err(1); } if !self.recursive { - r = self.chmod_file(&file).and(r); + r = self.chmod_file(file).and(r); } else { for entry in WalkDir::new(&filename).into_iter().filter_map(|e| e.ok()) { let file = entry.path(); - r = self.chmod_file(&file).and(r); + r = self.chmod_file(file).and(r); } } } @@ -181,18 +258,18 @@ impl Chmoder { let mut fperm = match fs::metadata(file) { Ok(meta) => meta.mode() & 0o7777, Err(err) => { - if !self.quiet { - if is_symlink(file) { - if self.verbose { - show_info!( - "neither symbolic link '{}' nor referent has been changed", - file.display() - ); - } - return Ok(()); - } else { - show_error!("{}: '{}'", err, file.display()); + if is_symlink(file) { + if self.verbose { + println!( + "neither symbolic link '{}' nor referent has been changed", + file.display() + ); } + return Ok(()); + } else if err.kind() == std::io::ErrorKind::PermissionDenied { + show_error!("'{}': Permission denied", file.display()); + } else { + show_error!("'{}': {}", file.display(), err); } return Err(1); } @@ -232,11 +309,11 @@ impl Chmoder { fn change_file(&self, fperm: u32, mode: u32, file: &Path) -> Result<(), i32> { if fperm == mode { if self.verbose && !self.changes { - show_info!( - "mode of '{}' retained as {:o} ({})", + println!( + "mode of '{}' retained as {:04o} ({})", file.display(), fperm, - display_permissions_unix(fperm) + display_permissions_unix(fperm as mode_t, false), ); } Ok(()) @@ -245,25 +322,25 @@ impl Chmoder { show_error!("{}", err); } if self.verbose { - show_info!( + show_error!( "failed to change mode of file '{}' from {:o} ({}) to {:o} ({})", file.display(), fperm, - display_permissions_unix(fperm), + display_permissions_unix(fperm as mode_t, false), mode, - display_permissions_unix(mode) + display_permissions_unix(mode as mode_t, false) ); } Err(1) } else { if self.verbose || self.changes { - show_info!( + show_error!( "mode of '{}' changed from {:o} ({}) to {:o} ({})", file.display(), fperm, - display_permissions_unix(fperm), + display_permissions_unix(fperm as mode_t, false), mode, - display_permissions_unix(mode) + display_permissions_unix(mode as mode_t, false) ); } Ok(()) diff --git a/src/uu/chmod/src/main.rs b/src/uu/chmod/src/main.rs index 604bd9a7d..adaf887f8 100644 --- a/src/uu/chmod/src/main.rs +++ b/src/uu/chmod/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_chmod); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_chmod); diff --git a/src/uu/chown/Cargo.toml b/src/uu/chown/Cargo.toml index f2d21ef88..74533af04 100644 --- a/src/uu/chown/Cargo.toml +++ b/src/uu/chown/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chown" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "chown ~ (uutils) change the ownership of FILE" @@ -17,7 +17,7 @@ path = "src/chown.rs" [dependencies] clap = "2.33" glob = "0.3.0" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } walkdir = "2.2" diff --git a/src/uu/chown/src/chown.rs b/src/uu/chown/src/chown.rs index 42010de03..e1d3ff22b 100644 --- a/src/uu/chown/src/chown.rs +++ b/src/uu/chown/src/chown.rs @@ -14,7 +14,7 @@ use uucore::fs::resolve_relative_path; use uucore::libc::{gid_t, uid_t}; use uucore::perms::{wrap_chown, Verbosity}; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use walkdir::WalkDir; @@ -23,9 +23,9 @@ 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"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod options { pub mod verbosity { @@ -67,14 +67,122 @@ fn get_usage() -> String { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + 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 { + 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 filter = if let Some(spec) = matches.value_of(options::FROM) { + match parse_spec(spec) { + Ok((Some(uid), None)) => IfFrom::User(uid), + Ok((None, Some(gid))) => IfFrom::Group(gid), + Ok((Some(uid), Some(gid))) => IfFrom::UserGroup(uid, gid), + Ok((None, None)) => IfFrom::All, + Err(e) => { + show_error!("{}", e); + return 1; + } + } + } else { + IfFrom::All + }; + + let dest_uid: Option; + let dest_gid: Option; + if let Some(file) = matches.value_of(options::REFERENCE) { + match fs::metadata(&file) { + Ok(meta) => { + dest_gid = Some(meta.gid()); + dest_uid = Some(meta.uid()); + } + Err(e) => { + show_error!("failed to get attributes of '{}': {}", file, e); + return 1; + } + } + } else { + match parse_spec(owner) { + Ok((u, g)) => { + dest_uid = u; + dest_gid = g; + } + Err(e) => { + show_error!("{}", e); + return 1; + } + } + } + let executor = Chowner { + bit_flag, + dest_uid, + dest_gid, + verbosity, + recursive, + dereference: derefer != 0, + filter, + preserve_root, + files, + }; + executor.exec() +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(options::verbosity::CHANGES) .short("c") @@ -165,148 +273,32 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .required(true) .min_values(1), ) - .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 { - show_info!("-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 filter = if let Some(spec) = matches.value_of(options::FROM) { - match parse_spec(&spec) { - Ok((Some(uid), None)) => IfFrom::User(uid), - Ok((None, Some(gid))) => IfFrom::Group(gid), - Ok((Some(uid), Some(gid))) => IfFrom::UserGroup(uid, gid), - Ok((None, None)) => IfFrom::All, - Err(e) => { - show_info!("{}", e); - return 1; - } - } - } else { - IfFrom::All - }; - - let dest_uid: Option; - let dest_gid: Option; - if let Some(file) = matches.value_of(options::REFERENCE) { - match fs::metadata(&file) { - Ok(meta) => { - dest_gid = Some(meta.gid()); - dest_uid = Some(meta.uid()); - } - Err(e) => { - show_info!("failed to get attributes of '{}': {}", file, e); - return 1; - } - } - } else { - match parse_spec(&owner) { - Ok((u, g)) => { - dest_uid = u; - dest_gid = g; - } - Err(e) => { - show_info!("{}", e); - return 1; - } - } - } - let executor = Chowner { - bit_flag, - dest_uid, - dest_gid, - verbosity, - recursive, - dereference: derefer != 0, - filter, - preserve_root, - files, - }; - executor.exec() } fn parse_spec(spec: &str) -> Result<(Option, Option), String> { - let args = spec.split(':').collect::>(); - let usr_only = args.len() == 1; - let grp_only = args.len() == 2 && args[0].is_empty() && !args[1].is_empty(); + 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(); - - if usr_only { - Ok(( - Some(match Passwd::locate(args[0]) { - Ok(v) => v.uid(), - _ => return Err(format!("invalid user: '{}'", spec)), - }), - None, - )) - } else if grp_only { - Ok(( - None, - Some(match Group::locate(args[1]) { - Ok(v) => v.gid(), - _ => return Err(format!("invalid group: '{}'", spec)), - }), - )) - } else if usr_grp { - Ok(( - Some(match Passwd::locate(args[0]) { - Ok(v) => v.uid(), - _ => return Err(format!("invalid user: '{}'", spec)), - }), - Some(match Group::locate(args[1]) { - Ok(v) => v.gid(), - _ => return Err(format!("invalid group: '{}'", spec)), - }), - )) + let uid = if usr_only || usr_grp { + Some( + Passwd::locate(args[0]) + .map_err(|_| format!("invalid user: '{}'", spec))? + .uid(), + ) } else { - Ok((None, None)) - } + None + }; + let gid = if grp_only || usr_grp { + Some( + Group::locate(args[1]) + .map_err(|_| format!("invalid group: '{}'", spec))? + .gid(), + ) + } else { + None + }; + Ok((uid, gid)) } enum IfFrom { @@ -374,8 +366,8 @@ impl Chowner { if let Some(p) = may_exist { if p.parent().is_none() { - show_info!("it is dangerous to operate recursively on '/'"); - show_info!("use --no-preserve-root to override this failsafe"); + show_error!("it is dangerous to operate recursively on '/'"); + show_error!("use --no-preserve-root to override this failsafe"); return 1; } } @@ -391,14 +383,14 @@ impl Chowner { self.verbosity.clone(), ) { Ok(n) => { - if n != "" { - show_info!("{}", n); + if !n.is_empty() { + show_error!("{}", n); } 0 } Err(e) => { if self.verbosity != Verbosity::Silent { - show_info!("{}", e); + show_error!("{}", e); } 1 } @@ -421,7 +413,7 @@ impl Chowner { for entry in WalkDir::new(root).follow_links(follow).min_depth(1) { let entry = unwrap!(entry, e, { ret = 1; - show_info!("{}", e); + show_error!("{}", e); continue; }); let path = entry.path(); @@ -446,14 +438,14 @@ impl Chowner { self.verbosity.clone(), ) { Ok(n) => { - if n != "" { - show_info!("{}", n); + if !n.is_empty() { + show_error!("{}", n); } 0 } Err(e) => { if self.verbosity != Verbosity::Silent { - show_info!("{}", e); + show_error!("{}", e); } 1 } @@ -469,7 +461,7 @@ impl Chowner { unwrap!(path.metadata(), e, { match self.verbosity { Silent => (), - _ => show_info!("cannot access '{}': {}", path.display(), e), + _ => show_error!("cannot access '{}': {}", path.display(), e), } return None; }) @@ -477,7 +469,7 @@ impl Chowner { unwrap!(path.symlink_metadata(), e, { match self.verbosity { Silent => (), - _ => show_info!("cannot dereference '{}': {}", path.display(), e), + _ => show_error!("cannot dereference '{}': {}", path.display(), e), } return None; }) @@ -495,3 +487,17 @@ impl Chowner { } } } + +#[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: ")); + } +} diff --git a/src/uu/chown/src/main.rs b/src/uu/chown/src/main.rs index ee5aeeba0..b3ed39970 100644 --- a/src/uu/chown/src/main.rs +++ b/src/uu/chown/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_chown); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_chown); diff --git a/src/uu/chroot/Cargo.toml b/src/uu/chroot/Cargo.toml index 7ad2f0908..bf1e0ef59 100644 --- a/src/uu/chroot/Cargo.toml +++ b/src/uu/chroot/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_chroot" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "chroot ~ (uutils) run COMMAND under a new root directory" @@ -15,8 +15,8 @@ edition = "2018" path = "src/chroot.rs" [dependencies] -getopts = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries"] } +clap= "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 773207363..2c0f8522c 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -10,60 +10,47 @@ #[macro_use] extern crate uucore; -use uucore::entries; -use uucore::libc::{self, chroot, setgid, setgroups, setuid}; - +use clap::{crate_version, App, Arg}; use std::ffi::CString; use std::io::Error; use std::path::Path; use std::process::Command; +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]...]"; -static SUMMARY: &str = "Run COMMAND with root directory set to NEWROOT."; -static LONG_HELP: &str = " - If COMMAND is not specified, it defaults to '$(SHELL) -i'. - If $(SHELL) is not set, /bin/sh is used. -"; + +mod options { + pub const NEWROOT: &str = "newroot"; + pub const USER: &str = "user"; + pub const GROUP: &str = "group"; + pub const GROUPS: &str = "groups"; + pub const USERSPEC: &str = "userspec"; + pub const COMMAND: &str = "command"; +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optopt( - "u", - "user", - "User (ID or name) to switch before running the program", - "USER", - ) - .optopt("g", "group", "Group (ID or name) to switch to", "GROUP") - .optopt( - "G", - "groups", - "Comma-separated list of groups to switch to", - "GROUP1,GROUP2...", - ) - .optopt( - "", - "userspec", - "Colon-separated user and group to switch to. \ - Same as -u USER -g GROUP. \ - Userspec has higher preference than -u and/or -g", - "USER:GROUP", - ) - .parse(args); - - if matches.free.is_empty() { - println!("Missing operand: NEWROOT"); - println!("Try `{} --help` for more information.", NAME); - return 1; - } + let matches = uu_app().get_matches_from(args); let default_shell: &'static str = "/bin/sh"; let default_option: &'static str = "-i"; let user_shell = std::env::var("SHELL"); - let newroot = Path::new(&matches.free[0][..]); + let newroot: &Path = match matches.value_of(options::NEWROOT) { + Some(v) => Path::new(v), + None => crash!( + 1, + "Missing operand: NEWROOT\nTry '{} --help' for more information.", + NAME + ), + }; + if !newroot.is_dir() { crash!( 1, @@ -72,7 +59,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 { ); } - let command: Vec<&str> = match matches.free.len() { + let commands = match matches.values_of(options::COMMAND) { + Some(v) => v.collect(), + None => vec![], + }; + + // 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 => { let shell: &str = match user_shell { Err(_) => default_shell, @@ -80,10 +74,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { }; vec![shell, default_option] } - _ => matches.free[1..].iter().map(|x| &x[..]).collect(), + _ => commands, }; - set_context(&newroot, &matches); + set_context(newroot, &matches); let pstatus = Command::new(command[0]) .args(&command[1..]) @@ -97,37 +91,83 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } -fn set_context(root: &Path, options: &getopts::Matches) { - let userspec_str = options.opt_str("userspec"); - let user_str = options.opt_str("user").unwrap_or_default(); - let group_str = options.opt_str("group").unwrap_or_default(); - let groups_str = options.opt_str("groups").unwrap_or_default(); +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .usage(SYNTAX) + .arg( + Arg::with_name(options::NEWROOT) + .hidden(true) + .required(true) + .index(1), + ) + .arg( + Arg::with_name(options::USER) + .short("u") + .long(options::USER) + .help("User (ID or name) to switch before running the program") + .value_name("USER"), + ) + .arg( + Arg::with_name(options::GROUP) + .short("g") + .long(options::GROUP) + .help("Group (ID or name) to switch to") + .value_name("GROUP"), + ) + .arg( + Arg::with_name(options::GROUPS) + .short("G") + .long(options::GROUPS) + .help("Comma-separated list of groups to switch to") + .value_name("GROUP1,GROUP2..."), + ) + .arg( + Arg::with_name(options::USERSPEC) + .long(options::USERSPEC) + .help( + "Colon-separated user and group to switch to. \ + Same as -u USER -g GROUP. \ + Userspec has higher preference than -u and/or -g", + ) + .value_name("USER:GROUP"), + ) + .arg( + Arg::with_name(options::COMMAND) + .hidden(true) + .multiple(true) + .index(2), + ) +} + +fn set_context(root: &Path, options: &clap::ArgMatches) { + let userspec_str = options.value_of(options::USERSPEC); + let user_str = options.value_of(options::USER).unwrap_or_default(); + let group_str = options.value_of(options::GROUP).unwrap_or_default(); + let groups_str = options.value_of(options::GROUPS).unwrap_or_default(); let userspec = match userspec_str { - Some(ref u) => { + Some(u) => { let s: Vec<&str> = u.split(':').collect(); - if s.len() != 2 { + if s.len() != 2 || s.iter().any(|&spec| spec.is_empty()) { crash!(1, "invalid userspec: `{}`", u) }; s } None => Vec::new(), }; - let user = if userspec.is_empty() { - &user_str[..] + + let (user, group) = if userspec.is_empty() { + (user_str, group_str) } else { - &userspec[0][..] - }; - let group = if userspec.is_empty() { - &group_str[..] - } else { - &userspec[1][..] + (userspec[0], userspec[1]) }; enter_chroot(root); - set_groups_from_str(&groups_str[..]); - set_main_group(&group[..]); - set_user(&user[..]); + set_groups_from_str(groups_str); + set_main_group(group); + set_user(user); } fn enter_chroot(root: &Path) { @@ -164,7 +204,7 @@ fn set_main_group(group: &str) { } } -#[cfg(any(target_os = "macos", target_os = "freebsd"))] +#[cfg(any(target_vendor = "apple", target_os = "freebsd"))] fn set_groups(groups: Vec) -> libc::c_int { unsafe { setgroups(groups.len() as libc::c_int, groups.as_ptr()) } } diff --git a/src/uu/chroot/src/main.rs b/src/uu/chroot/src/main.rs index a177c9301..0ca88cfaf 100644 --- a/src/uu/chroot/src/main.rs +++ b/src/uu/chroot/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_chroot); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_chroot); diff --git a/src/uu/cksum/Cargo.toml b/src/uu/cksum/Cargo.toml index ef3ec8b46..0332efbf8 100644 --- a/src/uu/cksum/Cargo.toml +++ b/src/uu/cksum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cksum" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "cksum ~ (uutils) display CRC and size of input" @@ -15,8 +15,9 @@ edition = "2018" path = "src/cksum.rs" [dependencies] +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/cksum/build.rs b/src/uu/cksum/build.rs deleted file mode 100644 index a9edd0d59..000000000 --- a/src/uu/cksum/build.rs +++ /dev/null @@ -1,46 +0,0 @@ -// This file is part of the uutils coreutils package. -// -// (c) Alex Lyon -// (c) Michael Gehring -// -// For the full copyright and license information, please view the LICENSE -// file that was distributed with this source code. - -use std::env; -use std::fs::File; -use std::io::Write; -use std::path::Path; - -const CRC_TABLE_LEN: usize = 256; - -fn main() { - let out_dir = env::var("OUT_DIR").unwrap(); - - let mut table = Vec::with_capacity(CRC_TABLE_LEN); - for num in 0..CRC_TABLE_LEN { - table.push(crc_entry(num as u8) as u32); - } - let file = File::create(&Path::new(&out_dir).join("crc_table.rs")).unwrap(); - write!( - &file, - "#[allow(clippy::unreadable_literal)]\nconst CRC_TABLE: [u32; {}] = {:?};", - CRC_TABLE_LEN, table - ) - .unwrap(); -} - -#[inline] -fn crc_entry(input: u8) -> u32 { - let mut crc = (input as u32) << 24; - - for _ in 0..8 { - if crc & 0x8000_0000 != 0 { - crc <<= 1; - crc ^= 0x04c1_1db7; - } else { - crc <<= 1; - } - } - - crc -} diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index f589e6f81..e88cc78b3 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -10,15 +10,107 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{self, stdin, BufReader, Read}; use std::path::Path; +use uucore::InvalidEncodingHandling; -include!(concat!(env!("OUT_DIR"), "/crc_table.rs")); +// NOTE: CRC_TABLE_LEN *must* be <= 256 as we cast 0..CRC_TABLE_LEN to u8 +const CRC_TABLE_LEN: usize = 256; +const CRC_TABLE: [u32; CRC_TABLE_LEN] = generate_crc_table(); -static SYNTAX: &str = "[OPTIONS] [FILE]..."; -static SUMMARY: &str = "Print CRC and size for each file"; -static LONG_HELP: &str = ""; +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| { + table[i] = crc_entry(i as u8) as u32; + }); + + table +} + +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 if_condition = crc & 0x8000_0000; + let if_body = (crc << 1) ^ 0x04c1_1db7; + let else_body = crc << 1; + + // NOTE: i feel like this is easier to understand than emulating an if statement in bitwise + // ops + let condition_table = [else_body, if_body]; + + crc = condition_table[(if_condition != 0) as usize]; + }); + + crc +} #[inline] fn crc_update(crc: u32, input: u8) -> u32 { @@ -48,33 +140,52 @@ fn cksum(fname: &str) -> io::Result<(u32, usize)> { let mut rd: Box = match fname { "-" => Box::new(stdin()), _ => { - file = File::open(&Path::new(fname))?; + let path = &Path::new(fname); + if path.is_dir() { + return Err(std::io::Error::new( + io::ErrorKind::InvalidInput, + "Is a directory", + )); + }; + if path.metadata().is_err() { + return Err(std::io::Error::new( + io::ErrorKind::NotFound, + "No such file or directory", + )); + }; + file = File::open(&path)?; Box::new(BufReader::new(file)) } }; let mut bytes = init_byte_array(); loop { - match rd.read(&mut bytes) { - Ok(num_bytes) => { - if num_bytes == 0 { - return Ok((crc_final(crc, size), size)); - } - for &b in bytes[..num_bytes].iter() { - crc = crc_update(crc, b); - } - size += num_bytes; - } - Err(err) => return Err(err), + let num_bytes = rd.read(&mut bytes)?; + if num_bytes == 0 { + return Ok((crc_final(crc, size), size)); } + for &b in bytes[..num_bytes].iter() { + crc = crc_update(crc, b); + } + size += num_bytes; } - //Ok((0 as u32,0 as usize)) +} + +mod options { + pub static FILE: &str = "file"; } pub fn uumain(args: impl uucore::Args) -> i32 { - let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let files = matches.free; + let matches = uu_app().get_matches_from(args); + + let files: Vec = match matches.values_of(options::FILE) { + Some(v) => v.clone().map(|v| v.to_owned()).collect(), + None => vec![], + }; if files.is_empty() { match cksum("-") { @@ -100,3 +211,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 { exit_code } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .about(SUMMARY) + .usage(SYNTAX) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) +} diff --git a/src/uu/cksum/src/main.rs b/src/uu/cksum/src/main.rs index 50f424f41..b8a8f6b33 100644 --- a/src/uu/cksum/src/main.rs +++ b/src/uu/cksum/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_cksum); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_cksum); diff --git a/src/uu/comm/Cargo.toml b/src/uu/comm/Cargo.toml index ddfbb6a47..f02217790 100644 --- a/src/uu/comm/Cargo.toml +++ b/src/uu/comm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_comm" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "comm ~ (uutils) compare sorted inputs" @@ -15,9 +15,9 @@ edition = "2018" path = "src/comm.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/comm/src/comm.rs b/src/uu/comm/src/comm.rs index 3fb1ef395..aa10432a2 100644 --- a/src/uu/comm/src/comm.rs +++ b/src/uu/comm/src/comm.rs @@ -14,22 +14,35 @@ use std::cmp::Ordering; use std::fs::File; use std::io::{self, stdin, BufRead, BufReader, Stdin}; use std::path::Path; +use uucore::InvalidEncodingHandling; -static SYNTAX: &str = "[OPTIONS] FILE1 FILE2"; -static SUMMARY: &str = "Compare sorted files line by line"; +use clap::{crate_version, App, Arg, ArgMatches}; + +static ABOUT: &str = "compare two sorted files line by line"; static LONG_HELP: &str = ""; -fn mkdelim(col: usize, opts: &getopts::Matches) -> String { - let mut s = String::new(); - let delim = match opts.opt_str("output-delimiter") { - Some(d) => d, - None => "\t".to_owned(), - }; +mod options { + pub const COLUMN_1: &str = "1"; + pub const COLUMN_2: &str = "2"; + pub const COLUMN_3: &str = "3"; + pub const DELIMITER: &str = "output-delimiter"; + pub const DELIMITER_DEFAULT: &str = "\t"; + pub const FILE_1: &str = "FILE1"; + pub const FILE_2: &str = "FILE2"; +} - if col > 1 && !opts.opt_present("1") { +fn get_usage() -> String { + format!("{} [OPTION]... FILE1 FILE2", executable!()) +} + +fn mkdelim(col: usize, opts: &ArgMatches) -> String { + let mut s = String::new(); + let delim = opts.value_of(options::DELIMITER).unwrap(); + + if col > 1 && !opts.is_present(options::COLUMN_1) { s.push_str(delim.as_ref()); } - if col > 2 && !opts.opt_present("2") { + if col > 2 && !opts.is_present(options::COLUMN_2) { s.push_str(delim.as_ref()); } @@ -37,9 +50,8 @@ fn mkdelim(col: usize, opts: &getopts::Matches) -> String { } fn ensure_nl(line: &mut String) { - match line.chars().last() { - Some('\n') => (), - _ => line.push('\n'), + if !line.ends_with('\n') { + line.push('\n'); } } @@ -57,7 +69,7 @@ impl LineReader { } } -fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { +fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) { let delim: Vec = (0..4).map(|col| mkdelim(col, opts)).collect(); let ra = &mut String::new(); @@ -80,7 +92,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { match ord { Ordering::Less => { - if !opts.opt_present("1") { + if !opts.is_present(options::COLUMN_1) { ensure_nl(ra); print!("{}{}", delim[1], ra); } @@ -88,7 +100,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { na = a.read_line(ra); } Ordering::Greater => { - if !opts.opt_present("2") { + if !opts.is_present(options::COLUMN_2) { ensure_nl(rb); print!("{}{}", delim[2], rb); } @@ -96,7 +108,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { nb = b.read_line(rb); } Ordering::Equal => { - if !opts.opt_present("3") { + if !opts.is_present(options::COLUMN_3) { ensure_nl(ra); print!("{}{}", delim[3], ra); } @@ -120,23 +132,49 @@ fn open_file(name: &str) -> io::Result { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let usage = get_usage(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag("1", "", "suppress column 1 (lines uniq to FILE1)") - .optflag("2", "", "suppress column 2 (lines uniq to FILE2)") - .optflag( - "3", - "", - "suppress column 3 (lines that appear in both files)", - ) - .optopt("", "output-delimiter", "separate columns with STR", "STR") - .parse(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - let mut f1 = open_file(matches.free[0].as_ref()).unwrap(); - let mut f2 = open_file(matches.free[1].as_ref()).unwrap(); + let mut f1 = open_file(matches.value_of(options::FILE_1).unwrap()).unwrap(); + let mut f2 = open_file(matches.value_of(options::FILE_2).unwrap()).unwrap(); comm(&mut f1, &mut f2, &matches); 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .after_help(LONG_HELP) + .arg( + Arg::with_name(options::COLUMN_1) + .short(options::COLUMN_1) + .help("suppress column 1 (lines unique to FILE1)"), + ) + .arg( + Arg::with_name(options::COLUMN_2) + .short(options::COLUMN_2) + .help("suppress column 2 (lines unique to FILE2)"), + ) + .arg( + Arg::with_name(options::COLUMN_3) + .short(options::COLUMN_3) + .help("suppress column 3 (lines that appear in both files)"), + ) + .arg( + Arg::with_name(options::DELIMITER) + .long(options::DELIMITER) + .help("separate columns with STR") + .value_name("STR") + .default_value(options::DELIMITER_DEFAULT) + .hide_default_value(true), + ) + .arg(Arg::with_name(options::FILE_1).required(true)) + .arg(Arg::with_name(options::FILE_2).required(true)) +} diff --git a/src/uu/comm/src/main.rs b/src/uu/comm/src/main.rs index 149098c3c..07ac07544 100644 --- a/src/uu/comm/src/main.rs +++ b/src/uu/comm/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_comm); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_comm); diff --git a/src/uu/cp/Cargo.toml b/src/uu/cp/Cargo.toml index a0253433f..9d582adae 100644 --- a/src/uu/cp/Cargo.toml +++ b/src/uu/cp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cp" -version = "0.0.4" +version = "0.0.6" authors = [ "Jordy Dickinson ", "Joshua S. Miller ", @@ -23,7 +23,7 @@ clap = "2.33" filetime = "0.2" libc = "0.2.85" quick-error = "1.2.3" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } walkdir = "2.2" diff --git a/src/uu/cp/README.md b/src/uu/cp/README.md index 91753eb66..13399c3b1 100644 --- a/src/uu/cp/README.md +++ b/src/uu/cp/README.md @@ -7,37 +7,37 @@ ### To Do -- [ ] archive -- [ ] attributes-only -- [ ] copy-contents -- [ ] no-dereference-preserve-linkgs -- [ ] dereference -- [ ] no-dereference -- [ ] preserve-default-attributes -- [ ] preserve -- [ ] no-preserve -- [ ] parents -- [ ] reflink -- [ ] sparse -- [ ] strip-trailing-slashes -- [ ] update -- [ ] one-file-system -- [ ] context - [ ] cli-symbolic-links +- [ ] context +- [ ] copy-contents +- [ ] sparse ### Completed +- [x] archive +- [x] attributes-only - [x] backup +- [x] dereference - [x] force (Not implemented on Windows) - [x] interactive - [x] link - [x] no-clobber +- [x] no-dereference +- [x] no-dereference-preserve-links +- [x] no-preserve - [x] no-target-directory +- [x] one-file-system +- [x] parents - [x] paths +- [x] preserve +- [x] preserve-default-attributes - [x] recursive +- [x] reflink - [x] remove-destination (On Windows, current only works for writeable files) +- [x] strip-trailing-slashes - [x] suffix - [x] symbolic-link - [x] target-directory +- [x] update - [x] verbose - [x] version diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 0922af241..4deaefa98 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -25,7 +25,7 @@ use winapi::um::fileapi::GetFileInformationByHandle; use std::borrow::Cow; -use clap::{App, Arg, ArgMatches}; +use clap::{crate_version, App, Arg, ArgMatches}; use filetime::FileTime; use quick_error::ResultExt; use std::collections::HashSet; @@ -41,13 +41,13 @@ use std::io; use std::io::{stdin, stdout, Write}; use std::mem; #[cfg(target_os = "linux")] -use std::os::unix::io::IntoRawFd; +use std::os::unix::io::AsRawFd; #[cfg(windows)] use std::os::windows::ffi::OsStrExt; use std::path::{Path, PathBuf, StripPrefixError}; use std::str::FromStr; use std::string::ToString; -use uucore::fs::resolve_relative_path; +use uucore::backup_control::{self, BackupMode}; use uucore::fs::{canonicalize, CanonicalizeMode}; use walkdir::WalkDir; @@ -112,7 +112,7 @@ macro_rules! or_continue( }) ); -/// Prompts the user yes/no and returns `true` they if successfully +/// Prompts the user yes/no and returns `true` if they successfully /// answered yes. macro_rules! prompt_yes( ($($args:tt)+) => ({ @@ -132,7 +132,9 @@ macro_rules! prompt_yes( pub type CopyResult = Result; pub type Source = PathBuf; +pub type SourceSlice = Path; pub type Target = PathBuf; +pub type TargetSlice = Path; /// Specifies whether when overwrite files #[derive(Clone, Eq, PartialEq)] @@ -153,7 +155,8 @@ pub enum OverwriteMode { NoClobber, } -#[derive(Clone, Eq, PartialEq)] +/// Possible arguments for `--reflink`. +#[derive(Copy, Clone, Eq, PartialEq)] pub enum ReflinkMode { Always, Auto, @@ -166,14 +169,6 @@ pub enum TargetType { File, } -#[derive(Clone, Eq, PartialEq)] -pub enum BackupMode { - ExistingBackup, - NoBackup, - NumberedBackup, - SimpleBackup, -} - pub enum CopyMode { Link, SymLink, @@ -198,17 +193,15 @@ pub enum Attribute { #[allow(dead_code)] pub struct Options { attributes_only: bool, - backup: bool, + backup: BackupMode, copy_contents: bool, copy_mode: CopyMode, dereference: bool, - no_dereference: bool, no_target_dir: bool, one_file_system: bool, overwrite: OverwriteMode, parents: bool, strip_trailing_slashes: bool, - reflink: bool, reflink_mode: ReflinkMode, preserve_attributes: Vec, recursive: bool, @@ -218,8 +211,8 @@ pub struct Options { verbose: bool, } -static VERSION: &str = env!("CARGO_PKG_VERSION"); 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; @@ -233,38 +226,41 @@ fn get_usage() -> String { } // Argument constants -static OPT_ARCHIVE: &str = "archive"; -static OPT_ATTRIBUTES_ONLY: &str = "attributes-only"; -static OPT_BACKUP: &str = "backup"; -static OPT_CLI_SYMBOLIC_LINKS: &str = "cli-symbolic-links"; -static OPT_CONTEXT: &str = "context"; -static OPT_COPY_CONTENTS: &str = "copy-contents"; -static OPT_DEREFERENCE: &str = "dereference"; -static OPT_FORCE: &str = "force"; -static OPT_INTERACTIVE: &str = "interactive"; -static OPT_LINK: &str = "link"; -static OPT_NO_CLOBBER: &str = "no-clobber"; -static OPT_NO_DEREFERENCE: &str = "no-dereference"; -static OPT_NO_DEREFERENCE_PRESERVE_LINKS: &str = "no-dereference-preserve-linkgs"; -static OPT_NO_PRESERVE: &str = "no-preserve"; -static OPT_NO_TARGET_DIRECTORY: &str = "no-target-directory"; -static OPT_ONE_FILE_SYSTEM: &str = "one-file-system"; -static OPT_PARENT: &str = "parent"; -static OPT_PARENTS: &str = "parents"; -static OPT_PATHS: &str = "paths"; -static OPT_PRESERVE: &str = "preserve"; -static OPT_PRESERVE_DEFAULT_ATTRIBUTES: &str = "preserve-default-attributes"; -static OPT_RECURSIVE: &str = "recursive"; -static OPT_RECURSIVE_ALIAS: &str = "recursive_alias"; -static OPT_REFLINK: &str = "reflink"; -static OPT_REMOVE_DESTINATION: &str = "remove-destination"; -static OPT_SPARSE: &str = "sparse"; -static OPT_STRIP_TRAILING_SLASHES: &str = "strip-trailing-slashes"; -static OPT_SUFFIX: &str = "suffix"; -static OPT_SYMBOLIC_LINK: &str = "symbolic-link"; -static OPT_TARGET_DIRECTORY: &str = "target-directory"; -static OPT_UPDATE: &str = "update"; -static OPT_VERBOSE: &str = "verbose"; +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"; + pub const DEREFERENCE: &str = "dereference"; + pub const FORCE: &str = "force"; + pub const INTERACTIVE: &str = "interactive"; + 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_PRESERVE: &str = "no-preserve"; + pub const NO_TARGET_DIRECTORY: &str = "no-target-directory"; + pub const ONE_FILE_SYSTEM: &str = "one-file-system"; + pub const PARENT: &str = "parent"; + pub const PARENTS: &str = "parents"; + pub const PATHS: &str = "paths"; + pub const PRESERVE: &str = "preserve"; + pub const PRESERVE_DEFAULT_ATTRIBUTES: &str = "preserve-default-attributes"; + pub const RECURSIVE: &str = "recursive"; + pub const RECURSIVE_ALIAS: &str = "recursive_alias"; + pub const REFLINK: &str = "reflink"; + 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"; + pub const VERBOSE: &str = "verbose"; +} #[cfg(unix)] static PRESERVABLE_ATTRIBUTES: &[&str] = &[ @@ -294,175 +290,199 @@ static DEFAULT_ATTRIBUTES: &[Attribute] = &[ Attribute::Timestamps, ]; -pub fn uumain(args: impl uucore::Args) -> i32 { - let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) - .arg(Arg::with_name(OPT_TARGET_DIRECTORY) + .arg(Arg::with_name(options::TARGET_DIRECTORY) .short("t") - .conflicts_with(OPT_NO_TARGET_DIRECTORY) - .long(OPT_TARGET_DIRECTORY) - .value_name(OPT_TARGET_DIRECTORY) + .conflicts_with(options::NO_TARGET_DIRECTORY) + .long(options::TARGET_DIRECTORY) + .value_name(options::TARGET_DIRECTORY) .takes_value(true) .help("copy all SOURCE arguments into target-directory")) - .arg(Arg::with_name(OPT_NO_TARGET_DIRECTORY) + .arg(Arg::with_name(options::NO_TARGET_DIRECTORY) .short("T") - .long(OPT_NO_TARGET_DIRECTORY) - .conflicts_with(OPT_TARGET_DIRECTORY) + .long(options::NO_TARGET_DIRECTORY) + .conflicts_with(options::TARGET_DIRECTORY) .help("Treat DEST as a regular file and not a directory")) - .arg(Arg::with_name(OPT_INTERACTIVE) + .arg(Arg::with_name(options::INTERACTIVE) .short("i") - .long(OPT_INTERACTIVE) - .conflicts_with(OPT_NO_CLOBBER) + .long(options::INTERACTIVE) + .conflicts_with(options::NO_CLOBBER) .help("ask before overwriting files")) - .arg(Arg::with_name(OPT_LINK) + .arg(Arg::with_name(options::LINK) .short("l") - .long(OPT_LINK) - .overrides_with(OPT_REFLINK) + .long(options::LINK) + .overrides_with(options::REFLINK) .help("hard-link files instead of copying")) - .arg(Arg::with_name(OPT_NO_CLOBBER) + .arg(Arg::with_name(options::NO_CLOBBER) .short("n") - .long(OPT_NO_CLOBBER) - .conflicts_with(OPT_INTERACTIVE) + .long(options::NO_CLOBBER) + .conflicts_with(options::INTERACTIVE) .help("don't overwrite a file that already exists")) - .arg(Arg::with_name(OPT_RECURSIVE) + .arg(Arg::with_name(options::RECURSIVE) .short("r") - .long(OPT_RECURSIVE) + .long(options::RECURSIVE) // --archive sets this option .help("copy directories recursively")) - .arg(Arg::with_name(OPT_RECURSIVE_ALIAS) + .arg(Arg::with_name(options::RECURSIVE_ALIAS) .short("R") .help("same as -r")) - .arg(Arg::with_name(OPT_STRIP_TRAILING_SLASHES) - .long(OPT_STRIP_TRAILING_SLASHES) + .arg(Arg::with_name(options::STRIP_TRAILING_SLASHES) + .long(options::STRIP_TRAILING_SLASHES) .help("remove any trailing slashes from each SOURCE argument")) - .arg(Arg::with_name(OPT_VERBOSE) + .arg(Arg::with_name(options::VERBOSE) .short("v") - .long(OPT_VERBOSE) + .long(options::VERBOSE) .help("explicitly state what is being done")) - .arg(Arg::with_name(OPT_SYMBOLIC_LINK) + .arg(Arg::with_name(options::SYMBOLIC_LINK) .short("s") - .long(OPT_SYMBOLIC_LINK) - .conflicts_with(OPT_LINK) - .overrides_with(OPT_REFLINK) + .long(options::SYMBOLIC_LINK) + .conflicts_with(options::LINK) + .overrides_with(options::REFLINK) .help("make symbolic links instead of copying")) - .arg(Arg::with_name(OPT_FORCE) + .arg(Arg::with_name(options::FORCE) .short("f") - .long(OPT_FORCE) + .long(options::FORCE) .help("if an existing destination file cannot be opened, remove it and \ try again (this option is ignored when the -n option is also used). \ Currently not implemented for Windows.")) - .arg(Arg::with_name(OPT_REMOVE_DESTINATION) - .long(OPT_REMOVE_DESTINATION) - .conflicts_with(OPT_FORCE) + .arg(Arg::with_name(options::REMOVE_DESTINATION) + .long(options::REMOVE_DESTINATION) + .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(OPT_BACKUP) - .short("b") - .long(OPT_BACKUP) - .help("make a backup of each existing destination file")) - .arg(Arg::with_name(OPT_SUFFIX) - .short("S") - .long(OPT_SUFFIX) + .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) + .possible_values(backup_control::BACKUP_CONTROL_VALUES) + .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) - .default_value("~") .value_name("SUFFIX") .help("override the usual backup suffix")) - .arg(Arg::with_name(OPT_UPDATE) + .arg(Arg::with_name(options::UPDATE) .short("u") - .long(OPT_UPDATE) - .help("copy only when the SOURCE file is newer than the destination file\ + .long(options::UPDATE) + .help("copy only when the SOURCE file is newer than the destination file \ or when the destination file is missing")) - .arg(Arg::with_name(OPT_REFLINK) - .long(OPT_REFLINK) + .arg(Arg::with_name(options::REFLINK) + .long(options::REFLINK) .takes_value(true) .value_name("WHEN") .help("control clone/CoW copies. See below")) - .arg(Arg::with_name(OPT_ATTRIBUTES_ONLY) - .long(OPT_ATTRIBUTES_ONLY) - .conflicts_with(OPT_COPY_CONTENTS) - .overrides_with(OPT_REFLINK) + .arg(Arg::with_name(options::ATTRIBUTES_ONLY) + .long(options::ATTRIBUTES_ONLY) + .conflicts_with(options::COPY_CONTENTS) + .overrides_with(options::REFLINK) .help("Don't copy the file data, just the attributes")) - .arg(Arg::with_name(OPT_PRESERVE) - .long(OPT_PRESERVE) + .arg(Arg::with_name(options::PRESERVE) + .long(options::PRESERVE) .takes_value(true) .multiple(true) .use_delimiter(true) .possible_values(PRESERVABLE_ATTRIBUTES) + .min_values(0) .value_name("ATTR_LIST") - .conflicts_with_all(&[OPT_PRESERVE_DEFAULT_ATTRIBUTES, OPT_NO_PRESERVE]) + .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 (unix only), ownership, timestamps), \ if possible additional attributes: context, links, xattr, all")) - .arg(Arg::with_name(OPT_PRESERVE_DEFAULT_ATTRIBUTES) + .arg(Arg::with_name(options::PRESERVE_DEFAULT_ATTRIBUTES) .short("-p") - .long(OPT_PRESERVE_DEFAULT_ATTRIBUTES) - .conflicts_with_all(&[OPT_PRESERVE, OPT_NO_PRESERVE, OPT_ARCHIVE]) + .long(options::PRESERVE_DEFAULT_ATTRIBUTES) + .conflicts_with_all(&[options::PRESERVE, options::NO_PRESERVE, options::ARCHIVE]) .help("same as --preserve=mode(unix only),ownership,timestamps")) - .arg(Arg::with_name(OPT_NO_PRESERVE) - .long(OPT_NO_PRESERVE) + .arg(Arg::with_name(options::NO_PRESERVE) + .long(options::NO_PRESERVE) .takes_value(true) .value_name("ATTR_LIST") - .conflicts_with_all(&[OPT_PRESERVE_DEFAULT_ATTRIBUTES, OPT_PRESERVE, OPT_ARCHIVE]) + .conflicts_with_all(&[options::PRESERVE_DEFAULT_ATTRIBUTES, options::PRESERVE, options::ARCHIVE]) .help("don't preserve the specified attributes")) - .arg(Arg::with_name(OPT_PARENTS) - .long(OPT_PARENTS) - .alias(OPT_PARENT) + .arg(Arg::with_name(options::PARENTS) + .long(options::PARENTS) + .alias(options::PARENT) .help("use full source file name under DIRECTORY")) - .arg(Arg::with_name(OPT_NO_DEREFERENCE) + .arg(Arg::with_name(options::NO_DEREFERENCE) .short("-P") - .long(OPT_NO_DEREFERENCE) - .conflicts_with(OPT_DEREFERENCE) + .long(options::NO_DEREFERENCE) + .conflicts_with(options::DEREFERENCE) // -d sets this option .help("never follow symbolic links in SOURCE")) - .arg(Arg::with_name(OPT_DEREFERENCE) + .arg(Arg::with_name(options::DEREFERENCE) .short("L") - .long(OPT_DEREFERENCE) - .conflicts_with(OPT_NO_DEREFERENCE) + .long(options::DEREFERENCE) + .conflicts_with(options::NO_DEREFERENCE) .help("always follow symbolic links in SOURCE")) - .arg(Arg::with_name(OPT_ARCHIVE) + .arg(Arg::with_name(options::ARCHIVE) .short("a") - .long(OPT_ARCHIVE) - .conflicts_with_all(&[OPT_PRESERVE_DEFAULT_ATTRIBUTES, OPT_PRESERVE, OPT_NO_PRESERVE]) + .long(options::ARCHIVE) + .conflicts_with_all(&[options::PRESERVE_DEFAULT_ATTRIBUTES, options::PRESERVE, options::NO_PRESERVE]) .help("Same as -dR --preserve=all")) - .arg(Arg::with_name(OPT_NO_DEREFERENCE_PRESERVE_LINKS) + .arg(Arg::with_name(options::NO_DEREFERENCE_PRESERVE_LINKS) .short("d") .help("same as --no-dereference --preserve=links")) + .arg(Arg::with_name(options::ONE_FILE_SYSTEM) + .short("x") + .long(options::ONE_FILE_SYSTEM) + .help("stay on this file system")) // TODO: implement the following args - .arg(Arg::with_name(OPT_COPY_CONTENTS) - .long(OPT_COPY_CONTENTS) - .conflicts_with(OPT_ATTRIBUTES_ONLY) + .arg(Arg::with_name(options::COPY_CONTENTS) + .long(options::COPY_CONTENTS) + .conflicts_with(options::ATTRIBUTES_ONLY) .help("NotImplemented: copy contents of special files when recursive")) - .arg(Arg::with_name(OPT_SPARSE) - .long(OPT_SPARSE) + .arg(Arg::with_name(options::SPARSE) + .long(options::SPARSE) .takes_value(true) .value_name("WHEN") .help("NotImplemented: control creation of sparse files. See below")) - .arg(Arg::with_name(OPT_ONE_FILE_SYSTEM) - .short("x") - .long(OPT_ONE_FILE_SYSTEM) - .help("NotImplemented: stay on this file system")) - .arg(Arg::with_name(OPT_CONTEXT) - .long(OPT_CONTEXT) + .arg(Arg::with_name(options::CONTEXT) + .long(options::CONTEXT) .takes_value(true) .value_name("CTX") .help("NotImplemented: set SELinux security context of destination file to default type")) - .arg(Arg::with_name(OPT_CLI_SYMBOLIC_LINKS) + .arg(Arg::with_name(options::CLI_SYMBOLIC_LINKS) .short("H") .help("NotImplemented: follow command-line symbolic links in SOURCE")) // END TODO - .arg(Arg::with_name(OPT_PATHS) + .arg(Arg::with_name(options::PATHS) .multiple(true)) +} + +pub fn uumain(args: impl uucore::Args) -> i32 { + let usage = get_usage(); + let matches = uu_app() + .after_help(&*format!( + "{}\n{}", + LONG_HELP, + backup_control::BACKUP_CONTROL_LONG_HELP + )) + .usage(&usage[..]) .get_matches_from(args); let options = crash_if_err!(EXIT_ERR, 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; + } + let paths: Vec = matches - .values_of(OPT_PATHS) + .values_of(options::PATHS) .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); @@ -484,9 +504,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { impl ClobberMode { fn from_matches(matches: &ArgMatches) -> ClobberMode { - if matches.is_present(OPT_FORCE) { + if matches.is_present(options::FORCE) { ClobberMode::Force - } else if matches.is_present(OPT_REMOVE_DESTINATION) { + } else if matches.is_present(options::REMOVE_DESTINATION) { ClobberMode::RemoveDestination } else { ClobberMode::Standard @@ -496,9 +516,9 @@ impl ClobberMode { impl OverwriteMode { fn from_matches(matches: &ArgMatches) -> OverwriteMode { - if matches.is_present(OPT_INTERACTIVE) { + if matches.is_present(options::INTERACTIVE) { OverwriteMode::Interactive(ClobberMode::from_matches(matches)) - } else if matches.is_present(OPT_NO_CLOBBER) { + } else if matches.is_present(options::NO_CLOBBER) { OverwriteMode::NoClobber } else { OverwriteMode::Clobber(ClobberMode::from_matches(matches)) @@ -508,15 +528,15 @@ impl OverwriteMode { impl CopyMode { fn from_matches(matches: &ArgMatches) -> CopyMode { - if matches.is_present(OPT_LINK) { + if matches.is_present(options::LINK) { CopyMode::Link - } else if matches.is_present(OPT_SYMBOLIC_LINK) { + } else if matches.is_present(options::SYMBOLIC_LINK) { CopyMode::SymLink - } else if matches.is_present(OPT_SPARSE) { + } else if matches.is_present(options::SPARSE) { CopyMode::Sparse - } else if matches.is_present(OPT_UPDATE) { + } else if matches.is_present(options::UPDATE) { CopyMode::Update - } else if matches.is_present(OPT_ATTRIBUTES_ONLY) { + } else if matches.is_present(options::ATTRIBUTES_ONLY) { CopyMode::AttrOnly } else { CopyMode::Copy @@ -547,26 +567,30 @@ impl FromStr for Attribute { } fn add_all_attributes() -> Vec { - let mut attr = Vec::new(); + 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.push(Attribute::Mode); - attr.push(Attribute::Ownership); - attr.push(Attribute::Timestamps); - attr.push(Attribute::Context); - attr.push(Attribute::Xattr); - attr.push(Attribute::Links); + attr.insert(0, Mode); + attr } impl Options { fn from_matches(matches: &ArgMatches) -> CopyResult { let not_implemented_opts = vec![ - OPT_COPY_CONTENTS, - OPT_SPARSE, - OPT_ONE_FILE_SYSTEM, - OPT_CONTEXT, + options::COPY_CONTENTS, + options::SPARSE, + #[cfg(not(any(windows, unix)))] + options::ONE_FILE_SYSTEM, + options::CONTEXT, #[cfg(windows)] - OPT_FORCE, + options::FORCE, ]; for not_implemented_opt in not_implemented_opts { @@ -575,21 +599,28 @@ impl Options { } } - let recursive = matches.is_present(OPT_RECURSIVE) - || matches.is_present(OPT_RECURSIVE_ALIAS) - || matches.is_present(OPT_ARCHIVE); + let recursive = matches.is_present(options::RECURSIVE) + || matches.is_present(options::RECURSIVE_ALIAS) + || matches.is_present(options::ARCHIVE); - let backup = matches.is_present(OPT_BACKUP) || (matches.occurrences_of(OPT_SUFFIX) > 0); + 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_suffix = + backup_control::determine_backup_suffix(matches.value_of(options::SUFFIX)); + + let overwrite = OverwriteMode::from_matches(matches); // Parse target directory options - let no_target_dir = matches.is_present(OPT_NO_TARGET_DIRECTORY); + let no_target_dir = matches.is_present(options::NO_TARGET_DIRECTORY); let target_dir = matches - .value_of(OPT_TARGET_DIRECTORY) + .value_of(options::TARGET_DIRECTORY) .map(ToString::to_string); // Parse attributes to preserve - let preserve_attributes: Vec = if matches.is_present(OPT_PRESERVE) { - match matches.values_of(OPT_PRESERVE) { + let preserve_attributes: Vec = if matches.is_present(options::PRESERVE) { + match matches.values_of(options::PRESERVE) { None => DEFAULT_ATTRIBUTES.to_vec(), Some(attribute_strs) => { let mut attributes = Vec::new(); @@ -604,39 +635,38 @@ impl Options { attributes } } - } else if matches.is_present(OPT_ARCHIVE) { + } else if matches.is_present(options::ARCHIVE) { // --archive is used. Same as --preserve=all add_all_attributes() - } else if matches.is_present(OPT_NO_DEREFERENCE_PRESERVE_LINKS) { + } else if matches.is_present(options::NO_DEREFERENCE_PRESERVE_LINKS) { vec![Attribute::Links] - } else if matches.is_present(OPT_PRESERVE_DEFAULT_ATTRIBUTES) { + } else if matches.is_present(options::PRESERVE_DEFAULT_ATTRIBUTES) { DEFAULT_ATTRIBUTES.to_vec() } else { vec![] }; let options = Options { - attributes_only: matches.is_present(OPT_ATTRIBUTES_ONLY), - copy_contents: matches.is_present(OPT_COPY_CONTENTS), + attributes_only: matches.is_present(options::ATTRIBUTES_ONLY), + copy_contents: matches.is_present(options::COPY_CONTENTS), copy_mode: CopyMode::from_matches(matches), - dereference: matches.is_present(OPT_DEREFERENCE), // No dereference is set with -p, -d and --archive - no_dereference: matches.is_present(OPT_NO_DEREFERENCE) - || matches.is_present(OPT_NO_DEREFERENCE_PRESERVE_LINKS) - || matches.is_present(OPT_ARCHIVE), - one_file_system: matches.is_present(OPT_ONE_FILE_SYSTEM), - overwrite: OverwriteMode::from_matches(matches), - parents: matches.is_present(OPT_PARENTS), - backup_suffix: matches.value_of(OPT_SUFFIX).unwrap().to_string(), - update: matches.is_present(OPT_UPDATE), - verbose: matches.is_present(OPT_VERBOSE), - strip_trailing_slashes: matches.is_present(OPT_STRIP_TRAILING_SLASHES), - reflink: matches.is_present(OPT_REFLINK), + dereference: !(matches.is_present(options::NO_DEREFERENCE) + || matches.is_present(options::NO_DEREFERENCE_PRESERVE_LINKS) + || matches.is_present(options::ARCHIVE) + || recursive) + || matches.is_present(options::DEREFERENCE), + one_file_system: matches.is_present(options::ONE_FILE_SYSTEM), + parents: matches.is_present(options::PARENTS), + update: matches.is_present(options::UPDATE), + verbose: matches.is_present(options::VERBOSE), + strip_trailing_slashes: matches.is_present(options::STRIP_TRAILING_SLASHES), reflink_mode: { - if let Some(reflink) = matches.value_of(OPT_REFLINK) { + if let Some(reflink) = matches.value_of(options::REFLINK) { match reflink { "always" => ReflinkMode::Always, "auto" => ReflinkMode::Auto, + "never" => ReflinkMode::Never, value => { return Err(Error::InvalidArgument(format!( "invalid argument '{}' for \'reflink\'", @@ -645,10 +675,19 @@ impl Options { } } } else { - ReflinkMode::Never + #[cfg(any(target_os = "linux", target_os = "macos"))] + { + ReflinkMode::Auto + } + #[cfg(not(any(target_os = "linux", target_os = "macos")))] + { + ReflinkMode::Never + } } }, - backup, + backup: backup_mode, + backup_suffix, + overwrite, no_target_dir, preserve_attributes, recursive, @@ -664,7 +703,7 @@ impl TargetType { /// /// Treat target as a dir if we have multiple sources or the target /// exists and already is a directory - fn determine(sources: &[Source], target: &Target) -> TargetType { + fn determine(sources: &[Source], target: &TargetSlice) -> TargetType { if sources.len() > 1 || target.is_dir() { TargetType::Directory } else { @@ -688,32 +727,31 @@ fn parse_path_args(path_args: &[String], options: &Options) -> CopyResult<(Vec { // All path args are sources, and the target dir was // specified separately - (paths, PathBuf::from(target)) + PathBuf::from(target) } None => { // If there was no explicit target-dir, then use the last // path_arg - let target = paths.pop().unwrap(); - (paths, target) + paths.pop().unwrap() } }; if options.strip_trailing_slashes { - for source in sources.iter_mut() { + for source in paths.iter_mut() { *source = source.components().as_path().to_owned() } } - Ok((sources, target)) + Ok((paths, target)) } fn preserve_hardlinks( hard_links: &mut Vec<(String, u64)>, - source: &std::path::PathBuf, + source: &std::path::Path, dest: std::path::PathBuf, found_hard_link: &mut bool, ) -> CopyResult<()> { @@ -787,7 +825,7 @@ fn preserve_hardlinks( /// Behavior depends on `options`, see [`Options`] for details. /// /// [`Options`]: ./struct.Options.html -fn copy(sources: &[Source], target: &Target, options: &Options) -> CopyResult<()> { +fn copy(sources: &[Source], target: &TargetSlice, options: &Options) -> CopyResult<()> { let target_type = TargetType::determine(sources, target); verify_target_type(target, &target_type)?; @@ -839,7 +877,7 @@ fn copy(sources: &[Source], target: &Target, options: &Options) -> CopyResult<() fn construct_dest_path( source_path: &Path, - target: &Target, + target: &TargetSlice, target_type: &TargetType, options: &Options, ) -> CopyResult { @@ -869,8 +907,8 @@ fn construct_dest_path( } fn copy_source( - source: &Source, - target: &Target, + source: &SourceSlice, + target: &TargetSlice, target_type: &TargetType, options: &Options, ) -> CopyResult<()> { @@ -911,12 +949,20 @@ fn adjust_canonicalization(p: &Path) -> Cow { /// /// Any errors encountered copying files in the tree will be logged but /// will not cause a short-circuit. -fn copy_directory(root: &Path, target: &Target, options: &Options) -> CopyResult<()> { +fn copy_directory(root: &Path, target: &TargetSlice, options: &Options) -> CopyResult<()> { if !options.recursive { return Err(format!("omitting directory '{}'", root.display()).into()); } - let root_path = Path::new(&root).canonicalize()?; + // if no-dereference is enabled and this is a symlink, copy it as a file + if !options.dereference && fs::symlink_metadata(root).unwrap().file_type().is_symlink() { + return copy_file(root, target, options); + } + + let current_dir = + env::current_dir().unwrap_or_else(|e| crash!(1, "failed to get current directory {}", e)); + + let root_path = current_dir.join(root); let root_parent = if target.exists() { root_path.parent() @@ -937,18 +983,13 @@ fn copy_directory(root: &Path, target: &Target, options: &Options) -> CopyResult #[cfg(any(windows, target_os = "redox"))] let mut hard_links: Vec<(String, u64)> = vec![]; - for path in WalkDir::new(root) { + for path in WalkDir::new(root) + .same_file_system(options.one_file_system) + .follow_links(options.dereference) + { let p = or_continue!(path); let is_symlink = fs::symlink_metadata(p.path())?.file_type().is_symlink(); - let path = if (options.no_dereference || options.dereference) && is_symlink { - // we are dealing with a symlink. Don't follow it - match env::current_dir() { - Ok(cwd) => cwd.join(resolve_relative_path(p.path())), - Err(e) => crash!(1, "failed to get current directory {}", e), - } - } else { - or_continue!(p.path().canonicalize()) - }; + let path = current_dir.join(&p.path()); let local_to_root_parent = match root_parent { Some(parent) => { @@ -971,9 +1012,10 @@ fn copy_directory(root: &Path, target: &Target, options: &Options) -> CopyResult }; let local_to_target = target.join(&local_to_root_parent); - - if path.is_dir() && !local_to_target.exists() { - or_continue!(fs::create_dir_all(local_to_target.clone())); + if is_symlink && !options.dereference { + copy_link(&path, &local_to_target)?; + } else if path.is_dir() && !local_to_target.exists() { + or_continue!(fs::create_dir_all(local_to_target)); } else if !path.is_dir() { if preserve_hard_links { let mut found_hard_link = false; @@ -1067,6 +1109,7 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu } #[cfg(not(windows))] +#[allow(clippy::unnecessary_wraps)] // needed for windows version fn symlink_file(source: &Path, dest: &Path, context: &str) -> CopyResult<()> { match std::os::unix::fs::symlink(source, dest).context(context) { Ok(_) => Ok(()), @@ -1083,14 +1126,10 @@ fn context_for(src: &Path, dest: &Path) -> String { format!("'{}' -> '{}'", src.display(), dest.display()) } -/// Implements a relatively naive backup that is not as full featured -/// as GNU cp. No CONTROL version control method argument is taken -/// for backups. -/// TODO: Add version control methods -fn backup_file(path: &Path, suffix: &str) -> CopyResult { - let mut backup_path = path.to_path_buf().into_os_string(); - backup_path.push(suffix); - fs::copy(path, &backup_path)?; +/// Implements a simple backup copy for the destination file. +/// TODO: for the backup, should this function be replaced by `copy_file(...)`? +fn backup_dest(dest: &Path, backup_path: &Path) -> CopyResult { + fs::copy(dest, &backup_path)?; Ok(backup_path.into()) } @@ -1101,8 +1140,9 @@ fn handle_existing_dest(source: &Path, dest: &Path, options: &Options) -> CopyRe options.overwrite.verify(dest)?; - if options.backup { - backup_file(dest, &options.backup_suffix)?; + let backup_path = backup_control::get_backup_path(options.backup, dest, &options.backup_suffix); + if let Some(backup_path) = backup_path { + backup_dest(dest, &backup_path)?; } match options.overwrite { @@ -1158,7 +1198,7 @@ fn copy_file(source: &Path, dest: &Path, options: &Options) -> CopyResult<()> { CopyMode::SymLink => { symlink_file(source, dest, &*context_for(source, dest))?; } - CopyMode::Sparse => return Err(Error::NotImplemented(OPT_SPARSE.to_string())), + CopyMode::Sparse => return Err(Error::NotImplemented(options::SPARSE.to_string())), CopyMode::Update => { if dest.exists() { let src_metadata = fs::metadata(source)?; @@ -1190,79 +1230,164 @@ fn copy_file(source: &Path, dest: &Path, options: &Options) -> CopyResult<()> { Ok(()) } -///Copy the file from `source` to `dest` either using the normal `fs::copy` or the -///`FICLONE` ioctl if --reflink is specified and the filesystem supports it. +/// 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<()> { - if options.reflink { - #[cfg(not(target_os = "linux"))] - return Err("--reflink is only supported on linux".to_string().into()); - - #[cfg(target_os = "linux")] - { - let src_file = File::open(source).unwrap().into_raw_fd(); - let dst_file = OpenOptions::new() - .write(true) - .truncate(false) - .create(true) - .open(dest) - .unwrap() - .into_raw_fd(); - match options.reflink_mode { - ReflinkMode::Always => unsafe { - let result = ficlone(dst_file, src_file as *const i32); - if result != 0 { - return Err(format!( - "failed to clone {:?} from {:?}: {}", - source, - dest, - std::io::Error::last_os_error() - ) - .into()); - } else { - return Ok(()); - } - }, - ReflinkMode::Auto => unsafe { - let result = ficlone(dst_file, src_file as *const i32); - if result != 0 { - fs::copy(source, dest).context(&*context_for(source, dest))?; - } - }, - ReflinkMode::Never => {} - } - } - } else if options.no_dereference && fs::symlink_metadata(&source)?.file_type().is_symlink() { - // Here, we will copy the symlink itself (actually, just recreate it) - let link = fs::read_link(&source)?; - let dest: Cow<'_, Path> = if dest.is_dir() { - match source.file_name() { - Some(name) => dest.join(name).into(), - None => crash!( - EXIT_ERR, - "cannot stat ‘{}’: No such file or directory", - source.display() - ), - } - } else { - dest.into() - }; - symlink_file(&link, &dest, &*context_for(&link, &dest))?; - } else if source.to_string_lossy() == "/dev/null" { + 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" { /* workaround a limitation of fs::copy * https://github.com/rust-lang/rust/issues/79390 */ File::create(dest)?; - } else { - if options.parents { - let parent = dest.parent().unwrap_or(dest); - fs::create_dir_all(parent)?; + } else if !options.dereference && 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)?; } + } else { fs::copy(source, dest).context(&*context_for(source, dest))?; } Ok(()) } +fn copy_link(source: &Path, dest: &Path) -> CopyResult<()> { + // Here, we will copy the symlink itself (actually, just recreate it) + let link = fs::read_link(&source)?; + let dest: Cow<'_, Path> = if dest.is_dir() { + match source.file_name() { + Some(name) => dest.join(name).into(), + None => crash!( + EXIT_ERR, + "cannot stat '{}': No such file or directory", + source.display() + ), + } + } else { + // we always need to remove the file to be able to create a symlink, + // even if it is writeable. + if dest.exists() { + fs::remove_file(dest)?; + } + dest.into() + }; + symlink_file(&link, &dest, &*context_for(&link, &dest)) +} + +/// 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<()> { + debug_assert!(mode != ReflinkMode::Never); + + let src_file = File::open(source).context(&*context_for(source, dest))?; + let dst_file = OpenOptions::new() + .write(true) + .truncate(false) + .create(true) + .open(dest) + .context(&*context_for(source, dest))?; + match mode { + ReflinkMode::Always => unsafe { + let result = ficlone(dst_file.as_raw_fd(), src_file.as_raw_fd() as *const i32); + if result != 0 { + Err(format!( + "failed to clone {:?} from {:?}: {}", + source, + dest, + std::io::Error::last_os_error() + ) + .into()) + } else { + Ok(()) + } + }, + 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))?; + } + Ok(()) + }, + ReflinkMode::Never => unreachable!(), + } +} + +/// 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<()> { + debug_assert!(mode != ReflinkMode::Never); + + // Extract paths in a form suitable to be passed to a syscall. + // The unwrap() is safe because they come from the command-line and so contain non nul + // character. + use std::os::unix::ffi::OsStrExt; + let src = CString::new(source.as_os_str().as_bytes()).unwrap(); + let dst = CString::new(dest.as_os_str().as_bytes()).unwrap(); + + // clonefile(2) was introduced in macOS 10.12 so we cannot statically link against it + // for backward compatibility. + let clonefile = CString::new("clonefile").unwrap(); + let raw_pfn = unsafe { libc::dlsym(libc::RTLD_NEXT, clonefile.as_ptr()) }; + + let mut error = 0; + if !raw_pfn.is_null() { + // Call clonefile(2). + // Safety: Casting a C function pointer to a rust function value is one of the few + // blessed uses of `transmute()`. + unsafe { + let pfn: extern "C" fn( + src: *const libc::c_char, + dst: *const libc::c_char, + flags: u32, + ) -> libc::c_int = std::mem::transmute(raw_pfn); + error = pfn(src.as_ptr(), dst.as_ptr(), 0); + if std::io::Error::last_os_error().kind() == std::io::ErrorKind::AlreadyExists { + // clonefile(2) fails if the destination exists. Remove it and try again. Do not + // bother to check if removal worked because we're going to try to clone again. + let _ = fs::remove_file(dest); + error = pfn(src.as_ptr(), dst.as_ptr(), 0); + } + } + } + + if raw_pfn.is_null() || error != 0 { + // clonefile(2) is either not supported or it errored out (possibly because the FS does not + // support COW). + match mode { + ReflinkMode::Always => { + return Err( + format!("failed to clone {:?} from {:?}: {}", source, dest, error).into(), + ) + } + ReflinkMode::Auto => fs::copy(source, dest).context(&*context_for(source, dest))?, + ReflinkMode::Never => unreachable!(), + }; + } + + Ok(()) +} + /// Generate an error message if `target` is not the correct `target_type` pub fn verify_target_type(target: &Path, target_type: &TargetType) -> CopyResult<()> { match (target_type, target.is_dir()) { @@ -1306,9 +1431,9 @@ pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::Result { fn test_cp_localize_to_target() { assert!( localize_to_target( - &Path::new("a/source/"), - &Path::new("a/source/c.txt"), - &Path::new("target/") + Path::new("a/source/"), + Path::new("a/source/c.txt"), + Path::new("target/") ) .unwrap() == Path::new("target/c.txt") diff --git a/src/uu/cp/src/main.rs b/src/uu/cp/src/main.rs index 425d490e7..acfcfd1b2 100644 --- a/src/uu/cp/src/main.rs +++ b/src/uu/cp/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_cp); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_cp); diff --git a/src/uu/csplit/Cargo.toml b/src/uu/csplit/Cargo.toml index 68c6e2322..7687991b0 100644 --- a/src/uu/csplit/Cargo.toml +++ b/src/uu/csplit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_csplit" -version = "0.0.4" +version = "0.0.6" 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" @@ -15,11 +15,11 @@ edition = "2018" path = "src/csplit.rs" [dependencies] -getopts = "0.2.17" +clap = "2.33" thiserror = "1.0" regex = "1.0.0" glob = "0.2.11" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries", "fs"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 7eb287522..048ec80d8 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -2,7 +2,7 @@ #[macro_use] extern crate uucore; -use getopts::Matches; +use clap::{crate_version, App, Arg, ArgMatches}; use regex::Regex; use std::cmp::Ordering; use std::io::{self, BufReader}; @@ -13,22 +13,30 @@ use std::{ mod csplit_error; mod patterns; -mod splitname; +mod split_name; use crate::csplit_error::CsplitError; -use crate::splitname::SplitName; +use crate::split_name::SplitName; +use uucore::InvalidEncodingHandling; -static SYNTAX: &str = "[OPTION]... FILE PATTERN..."; static SUMMARY: &str = "split a file into sections determined by context lines"; static LONG_HELP: &str = "Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output."; -static SUFFIX_FORMAT_OPT: &str = "suffix-format"; -static SUPPRESS_MATCHED_OPT: &str = "suppress-matched"; -static DIGITS_OPT: &str = "digits"; -static PREFIX_OPT: &str = "prefix"; -static KEEP_FILES_OPT: &str = "keep-files"; -static QUIET_OPT: &str = "quiet"; -static ELIDE_EMPTY_FILES_OPT: &str = "elide-empty-files"; +mod options { + pub const SUFFIX_FORMAT: &str = "suffix-format"; + pub const SUPPRESS_MATCHED: &str = "suppress-matched"; + pub const DIGITS: &str = "digits"; + pub const PREFIX: &str = "prefix"; + pub const KEEP_FILES: &str = "keep-files"; + pub const QUIET: &str = "quiet"; + pub const ELIDE_EMPTY_FILES: &str = "elide-empty-files"; + pub const FILE: &str = "file"; + pub const PATTERN: &str = "pattern"; +} + +fn get_usage() -> String { + format!("{0} [OPTION]... FILE PATTERN...", executable!()) +} /// Command line options for csplit. pub struct CsplitOptions { @@ -40,19 +48,19 @@ pub struct CsplitOptions { } impl CsplitOptions { - fn new(matches: &Matches) -> CsplitOptions { - let keep_files = matches.opt_present(KEEP_FILES_OPT); - let quiet = matches.opt_present(QUIET_OPT); - let elide_empty_files = matches.opt_present(ELIDE_EMPTY_FILES_OPT); - let suppress_matched = matches.opt_present(SUPPRESS_MATCHED_OPT); + fn new(matches: &ArgMatches) -> CsplitOptions { + let keep_files = matches.is_present(options::KEEP_FILES); + let quiet = matches.is_present(options::QUIET); + let elide_empty_files = matches.is_present(options::ELIDE_EMPTY_FILES); + let suppress_matched = matches.is_present(options::SUPPRESS_MATCHED); CsplitOptions { split_name: crash_if_err!( 1, SplitName::new( - matches.opt_str(PREFIX_OPT), - matches.opt_str(SUFFIX_FORMAT_OPT), - matches.opt_str(DIGITS_OPT) + matches.value_of(options::PREFIX).map(str::to_string), + matches.value_of(options::SUFFIX_FORMAT).map(str::to_string), + matches.value_of(options::DIGITS).map(str::to_string) ) ), keep_files, @@ -68,7 +76,7 @@ impl CsplitOptions { /// # Errors /// /// - [`io::Error`] if there is some problem reading/writing from/to a file. -/// - [`::CsplitError::LineOutOfRange`] if the linenum pattern is larger than the number of input +/// - [`::CsplitError::LineOutOfRange`] if the line number pattern is larger than the number of input /// lines. /// - [`::CsplitError::LineOutOfRangeOnRepetition`], like previous but after applying the pattern /// more than once. @@ -84,7 +92,7 @@ where T: BufRead, { let mut input_iter = InputSplitter::new(input.lines().enumerate()); - let mut split_writer = SplitWriter::new(&options); + let mut split_writer = SplitWriter::new(options); let ret = do_csplit(&mut split_writer, patterns, &mut input_iter); // consume the rest @@ -115,12 +123,7 @@ where // split the file based on patterns for pattern in patterns.into_iter() { let pattern_as_str = pattern.to_string(); - #[allow(clippy::match_like_matches_macro)] - let is_skip = if let patterns::Pattern::SkipToMatch(_, _, _) = pattern { - true - } else { - false - }; + let is_skip = matches!(pattern, patterns::Pattern::SkipToMatch(_, _, _)); match pattern { patterns::Pattern::UpToLine(n, ex) => { let mut up_to_line = n; @@ -479,10 +482,11 @@ where /// Shrink the buffer so that its length is equal to the set size, returning an iterator for /// the elements that were too much. fn shrink_buffer_to_size(&mut self) -> impl Iterator + '_ { - let mut shrink_offset = 0; - if self.buffer.len() > self.size { - shrink_offset = self.buffer.len() - self.size; - } + let shrink_offset = if self.buffer.len() > self.size { + self.buffer.len() - self.size + } else { + 0 + }; self.buffer .drain(..shrink_offset) .map(|(_, line)| line.unwrap()) @@ -702,45 +706,23 @@ mod tests { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let usage = get_usage(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optopt( - "b", - SUFFIX_FORMAT_OPT, - "use sprintf FORMAT instead of %02d", - "FORMAT", - ) - .optopt("f", PREFIX_OPT, "use PREFIX instead of 'xx'", "PREFIX") - .optflag("k", KEEP_FILES_OPT, "do not remove output files on errors") - .optflag( - "", - SUPPRESS_MATCHED_OPT, - "suppress the lines matching PATTERN", - ) - .optopt( - "n", - DIGITS_OPT, - "use specified number of digits instead of 2", - "DIGITS", - ) - .optflag("s", QUIET_OPT, "do not print counts of output file sizes") - .optflag("z", ELIDE_EMPTY_FILES_OPT, "remove empty output files") - .parse(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - // check for mandatory arguments - if matches.free.is_empty() { - show_error!("missing operand"); - exit!(1); - } - if matches.free.len() == 1 { - show_error!("missing operand after '{}'", matches.free[0]); - exit!(1); - } - // get the patterns to split on - let patterns = return_if_err!(1, patterns::get_patterns(&matches.free[1..])); // get the file to split - let file_name: &str = &matches.free[0]; + let file_name = matches.value_of(options::FILE).unwrap(); + + // get the patterns to split on + let patterns: Vec = matches + .values_of(options::PATTERN) + .unwrap() + .map(str::to_string) + .collect(); + let patterns = return_if_err!(1, patterns::get_patterns(&patterns[..])); let options = CsplitOptions::new(&matches); if file_name == "-" { let stdin = io::stdin(); @@ -755,3 +737,62 @@ pub fn uumain(args: impl uucore::Args) -> i32 { }; 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(SUMMARY) + .arg( + Arg::with_name(options::SUFFIX_FORMAT) + .short("b") + .long(options::SUFFIX_FORMAT) + .value_name("FORMAT") + .help("use sprintf FORMAT instead of %02d"), + ) + .arg( + Arg::with_name(options::PREFIX) + .short("f") + .long(options::PREFIX) + .value_name("PREFIX") + .help("use PREFIX instead of 'xx'"), + ) + .arg( + Arg::with_name(options::KEEP_FILES) + .short("k") + .long(options::KEEP_FILES) + .help("do not remove output files on errors"), + ) + .arg( + Arg::with_name(options::SUPPRESS_MATCHED) + .long(options::SUPPRESS_MATCHED) + .help("suppress the lines matching PATTERN"), + ) + .arg( + Arg::with_name(options::DIGITS) + .short("n") + .long(options::DIGITS) + .value_name("DIGITS") + .help("use specified number of digits instead of 2"), + ) + .arg( + Arg::with_name(options::QUIET) + .short("s") + .long(options::QUIET) + .visible_alias("silent") + .help("do not print counts of output file sizes"), + ) + .arg( + Arg::with_name(options::ELIDE_EMPTY_FILES) + .short("z") + .long(options::ELIDE_EMPTY_FILES) + .help("remove empty output files"), + ) + .arg(Arg::with_name(options::FILE).hidden(true).required(true)) + .arg( + Arg::with_name(options::PATTERN) + .hidden(true) + .multiple(true) + .required(true), + ) + .after_help(LONG_HELP) +} diff --git a/src/uu/csplit/src/main.rs b/src/uu/csplit/src/main.rs index 97aeb3821..b0b144e8c 100644 --- a/src/uu/csplit/src/main.rs +++ b/src/uu/csplit/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_csplit); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_csplit); diff --git a/src/uu/csplit/src/patterns.rs b/src/uu/csplit/src/patterns.rs index d2f14578a..4ab7862ac 100644 --- a/src/uu/csplit/src/patterns.rs +++ b/src/uu/csplit/src/patterns.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (regex) SKIPTO UPTO ; (vars) ntimes + use crate::csplit_error::CsplitError; use regex::Regex; @@ -131,20 +133,12 @@ fn extract_patterns(args: &[String]) -> Result, CsplitError> { Some(m) => m.as_str().parse().unwrap(), }; if let Some(up_to_match) = captures.name("UPTO") { - let pattern = match Regex::new(up_to_match.as_str()) { - Err(_) => { - return Err(CsplitError::InvalidPattern(arg.to_string())); - } - Ok(reg) => reg, - }; + let pattern = Regex::new(up_to_match.as_str()) + .map_err(|_| CsplitError::InvalidPattern(arg.to_string()))?; patterns.push(Pattern::UpToMatch(pattern, offset, execute_ntimes)); } else if let Some(skip_to_match) = captures.name("SKIPTO") { - let pattern = match Regex::new(skip_to_match.as_str()) { - Err(_) => { - return Err(CsplitError::InvalidPattern(arg.to_string())); - } - Ok(reg) => reg, - }; + let pattern = Regex::new(skip_to_match.as_str()) + .map_err(|_| CsplitError::InvalidPattern(arg.to_string()))?; patterns.push(Pattern::SkipToMatch(pattern, offset, execute_ntimes)); } } else if let Ok(line_number) = arg.parse::() { @@ -167,7 +161,7 @@ fn validate_line_numbers(patterns: &[Pattern]) -> Result<(), CsplitError> { .try_fold(0, |prev_ln, ¤t_ln| match (prev_ln, current_ln) { // a line number cannot be zero (_, 0) => Err(CsplitError::LineNumberIsZero), - // two consecutifs numbers should not be equal + // two consecutive numbers should not be equal (n, m) if n == m => { show_warning!("line number '{}' is the same as preceding line number", n); Ok(n) diff --git a/src/uu/csplit/src/splitname.rs b/src/uu/csplit/src/split_name.rs similarity index 96% rename from src/uu/csplit/src/splitname.rs rename to src/uu/csplit/src/split_name.rs index 66b17ba67..758216414 100644 --- a/src/uu/csplit/src/splitname.rs +++ b/src/uu/csplit/src/split_name.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (regex) diuox + use regex::Regex; use crate::csplit_error::CsplitError; @@ -31,13 +33,13 @@ impl SplitName { // get the prefix let prefix = prefix_opt.unwrap_or_else(|| "xx".to_string()); // the width for the split offset - let n_digits = match n_digits_opt { - None => 2, - Some(opt) => match opt.parse::() { - Ok(digits) => digits, - Err(_) => return Err(CsplitError::InvalidNumber(opt)), - }, - }; + let n_digits = n_digits_opt + .map(|opt| { + opt.parse::() + .map_err(|_| CsplitError::InvalidNumber(opt)) + }) + .transpose()? + .unwrap_or(2); // translate the custom format into a function let fn_split_name: Box String> = match format_opt { None => Box::new(move |n: usize| -> String { @@ -225,6 +227,8 @@ impl SplitName { #[cfg(test)] mod tests { + // spell-checker:ignore (path) xxcst + use super::*; #[test] @@ -319,13 +323,13 @@ mod tests { } #[test] - fn zero_padding_lower_hexa() { + fn zero_padding_lower_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%03x-")), None).unwrap(); assert_eq!(split_name.get(42), "xxcst-02a-"); } #[test] - fn zero_padding_upper_hexa() { + fn zero_padding_upper_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%03X-")), None).unwrap(); assert_eq!(split_name.get(42), "xxcst-02A-"); } @@ -337,13 +341,13 @@ mod tests { } #[test] - fn alternate_form_lower_hexa() { + fn alternate_form_lower_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%#10x-")), None).unwrap(); assert_eq!(split_name.get(42), "xxcst- 0x2a-"); } #[test] - fn alternate_form_upper_hexa() { + fn alternate_form_upper_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%#10X-")), None).unwrap(); assert_eq!(split_name.get(42), "xxcst- 0x2A-"); } @@ -373,13 +377,13 @@ mod tests { } #[test] - fn left_adjusted_lower_hexa() { + fn left_adjusted_lower_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%-10x-")), None).unwrap(); assert_eq!(split_name.get(42), "xxcst-0x2a -"); } #[test] - fn left_adjusted_upper_hexa() { + fn left_adjusted_upper_hex() { let split_name = SplitName::new(None, Some(String::from("cst-%-10X-")), None).unwrap(); assert_eq!(split_name.get(42), "xxcst-0x2A -"); } diff --git a/src/uu/cut/BENCHMARKING.md b/src/uu/cut/BENCHMARKING.md new file mode 100644 index 000000000..bd94cf93c --- /dev/null +++ b/src/uu/cut/BENCHMARKING.md @@ -0,0 +1,46 @@ +## Benchmarking cut + +### Performance profile + +In normal use cases a significant amount of the total execution time of `cut` +is spent performing I/O. When invoked with the `-f` option (cut fields) some +CPU time is spent on detecting fields (in `Searcher::next`). Other than that +some small amount of CPU time is spent on breaking the input stream into lines. + + +### How to + +When fixing bugs or adding features you might want to compare +performance before and after your code changes. + +- `hyperfine` can be used to accurately measure and compare the total + execution time of one or more commands. + + ``` + $ cargo build --release --package uu_cut + + $ hyperfine -w3 "./target/release/cut -f2-4,8 -d' ' input.txt" "cut -f2-4,8 -d' ' input.txt" + ``` + You can put those two commands in a shell script to be sure that you don't + forget to build after making any changes. + +When optimizing or fixing performance regressions seeing the number of times a +function is called, and the amount of time it takes can be useful. + +- `cargo flamegraph` generates flame graphs from function level metrics it records using `perf` or `dtrace` + + ``` + $ cargo flamegraph --bin cut --package uu_cut -- -f1,3-4 input.txt > /dev/null + ``` + + +### What to benchmark + +There are four different performance paths in `cut` to benchmark. + +- Byte ranges `-c`/`--characters` or `-b`/`--bytes` e.g. `cut -c 2,4,6-` +- Byte ranges with output delimiters e.g. `cut -c 4- --output-delimiter=/` +- Fields e.g. `cut -f -4` +- Fields with output delimiters e.g. `cut -f 7-10 --output-delimiter=:` + +Choose a test input file with large number of lines so that program startup time does not significantly affect the benchmark. diff --git a/src/uu/cut/Cargo.toml b/src/uu/cut/Cargo.toml index 71f7c3f6a..47b8223c5 100644 --- a/src/uu/cut/Cargo.toml +++ b/src/uu/cut/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_cut" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "cut ~ (uutils) display byte/field columns of input lines" @@ -15,8 +15,12 @@ edition = "2018" path = "src/cut.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +memchr = "2" +bstr = "0.2" +atty = "0.2" [[bin]] name = "cut" diff --git a/src/uu/cut/src/buffer.rs b/src/uu/cut/src/buffer.rs deleted file mode 100644 index 6c3238be1..000000000 --- a/src/uu/cut/src/buffer.rs +++ /dev/null @@ -1,152 +0,0 @@ -// This file is part of the uutils coreutils package. -// -// (c) Rolf Morel -// (c) kwantam -// * substantial rewrite to use the `std::io::BufReader` trait -// -// For the full copyright and license information, please view the LICENSE -// file that was distributed with this source code. - -// spell-checker:ignore (ToDO) SRes Newl - -use std::io::Result as IoResult; -use std::io::{BufRead, BufReader, Read, Write}; - -#[allow(non_snake_case)] -pub mod Bytes { - use std::io::Write; - - pub trait Select { - fn select(&mut self, bytes: usize, out: Option<&mut W>) -> Selected; - } - - #[derive(PartialEq, Eq, Debug)] - pub enum Selected { - NewlineFound, - Complete(usize), - Partial(usize), - EndOfFile, - } -} - -#[derive(Debug)] -pub struct ByteReader -where - R: Read, -{ - inner: BufReader, - newline_char: u8, -} - -impl ByteReader { - pub fn new(read: R, newline_char: u8) -> ByteReader { - ByteReader { - inner: BufReader::with_capacity(4096, read), - newline_char, - } - } -} - -impl Read for ByteReader { - fn read(&mut self, buf: &mut [u8]) -> IoResult { - self.inner.read(buf) - } -} - -impl BufRead for ByteReader { - fn fill_buf(&mut self) -> IoResult<&[u8]> { - self.inner.fill_buf() - } - - fn consume(&mut self, amt: usize) { - self.inner.consume(amt) - } -} - -impl ByteReader { - pub fn consume_line(&mut self) -> usize { - let mut bytes_consumed = 0; - let mut consume_val; - let newline_char = self.newline_char; - - loop { - { - // need filled_buf to go out of scope - let filled_buf = match self.fill_buf() { - Ok(b) => { - if b.is_empty() { - return bytes_consumed; - } else { - b - } - } - Err(e) => crash!(1, "read error: {}", e), - }; - - if let Some(idx) = filled_buf.iter().position(|byte| *byte == newline_char) { - consume_val = idx + 1; - bytes_consumed += consume_val; - break; - } - - consume_val = filled_buf.len(); - } - - bytes_consumed += consume_val; - self.consume(consume_val); - } - - self.consume(consume_val); - bytes_consumed - } -} - -impl self::Bytes::Select for ByteReader { - fn select(&mut self, bytes: usize, out: Option<&mut W>) -> Bytes::Selected { - enum SRes { - Comp, - Part, - Newl, - } - - use self::Bytes::Selected::*; - - let newline_char = self.newline_char; - let (res, consume_val) = { - let buffer = match self.fill_buf() { - Err(e) => crash!(1, "read error: {}", e), - Ok(b) => b, - }; - - let (res, consume_val) = match buffer.len() { - 0 => return EndOfFile, - buf_used if bytes < buf_used => { - // because the output delimiter should only be placed between - // segments check if the byte after bytes is a newline - let buf_slice = &buffer[0..=bytes]; - - match buf_slice.iter().position(|byte| *byte == newline_char) { - Some(idx) => (SRes::Newl, idx + 1), - None => (SRes::Comp, bytes), - } - } - _ => match buffer.iter().position(|byte| *byte == newline_char) { - Some(idx) => (SRes::Newl, idx + 1), - None => (SRes::Part, buffer.len()), - }, - }; - - if let Some(out) = out { - crash_if_err!(1, out.write_all(&buffer[0..consume_val])); - } - (res, consume_val) - }; - - self.consume(consume_val); - match res { - SRes::Comp => Complete(consume_val), - SRes::Part => Partial(consume_val), - SRes::Newl => NewlineFound, - } - } -} diff --git a/src/uu/cut/src/cut.rs b/src/uu/cut/src/cut.rs index 95411e3fb..e33b8a2fe 100644 --- a/src/uu/cut/src/cut.rs +++ b/src/uu/cut/src/cut.rs @@ -10,16 +10,19 @@ #[macro_use] extern crate uucore; +use bstr::io::BufReadExt; +use clap::{crate_version, App, Arg}; use std::fs::File; -use std::io::{stdin, stdout, BufRead, BufReader, Read, Stdout, Write}; +use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write}; use std::path::Path; use self::searcher::Searcher; use uucore::ranges::Range; +use uucore::InvalidEncodingHandling; -mod buffer; mod searcher; +static NAME: &str = "cut"; static SYNTAX: &str = "[-d] [-s] [-z] [--output-delimiter] ((-f|-b|-c) {{sequence}}) {{sourcefile}}+"; static SUMMARY: &str = @@ -122,6 +125,14 @@ enum Mode { Fields(Vec, FieldOptions), } +fn stdout_writer() -> Box { + if atty::is(atty::Stream::Stdout) { + Box::new(stdout()) + } else { + Box::new(BufWriter::new(stdout())) as Box + } +} + fn list_to_ranges(list: &str, complement: bool) -> Result, String> { if complement { Range::from_list(list).map(|r| uucore::ranges::complement(&r)) @@ -131,72 +142,35 @@ fn list_to_ranges(list: &str, complement: bool) -> Result, String> { } fn cut_bytes(reader: R, ranges: &[Range], opts: &Options) -> i32 { - use self::buffer::Bytes::Select; - use self::buffer::Bytes::Selected::*; - let newline_char = if opts.zero_terminated { b'\0' } else { b'\n' }; - let mut buf_read = buffer::ByteReader::new(reader, newline_char); - let mut out = stdout(); + let buf_in = BufReader::new(reader); + let mut out = stdout_writer(); + let delim = opts + .out_delim + .as_ref() + .map_or("", String::as_str) + .as_bytes(); - 'newline: loop { - let mut cur_pos = 1; + let res = buf_in.for_byte_record(newline_char, |line| { let mut print_delim = false; - - for &Range { low, high } in ranges.iter() { - // skip up to low - let orig_pos = cur_pos; - loop { - match buf_read.select(low - cur_pos, None::<&mut Stdout>) { - NewlineFound => { - crash_if_err!(1, out.write_all(&[newline_char])); - continue 'newline; - } - Complete(len) => { - cur_pos += len; - break; - } - Partial(len) => cur_pos += len, - EndOfFile => { - if orig_pos != cur_pos { - crash_if_err!(1, out.write_all(&[newline_char])); - } - - break 'newline; - } - } + for &Range { low, high } in ranges { + if low > line.len() { + break; } - - if let Some(ref delim) = opts.out_delim { - if print_delim { - crash_if_err!(1, out.write_all(delim.as_bytes())); - } + if print_delim { + out.write_all(delim)?; + } else if opts.out_delim.is_some() { print_delim = true; } - - // write out from low to high - loop { - match buf_read.select(high - cur_pos + 1, Some(&mut out)) { - NewlineFound => continue 'newline, - Partial(len) => cur_pos += len, - Complete(_) => { - cur_pos = high + 1; - break; - } - EndOfFile => { - if cur_pos != low || low == high { - crash_if_err!(1, out.write_all(&[newline_char])); - } - - break 'newline; - } - } - } + // change `low` from 1-indexed value to 0-index value + let low = low - 1; + let high = high.min(line.len()); + out.write_all(&line[low..high])?; } - - buf_read.consume_line(); - crash_if_err!(1, out.write_all(&[newline_char])); - } - + out.write_all(&[newline_char])?; + Ok(true) + }); + crash_if_err!(1, res); 0 } @@ -209,23 +183,11 @@ fn cut_fields_delimiter( newline_char: u8, out_delim: &str, ) -> i32 { - let mut buf_in = BufReader::new(reader); - let mut out = stdout(); - let mut buffer = Vec::new(); + let buf_in = BufReader::new(reader); + let mut out = stdout_writer(); + let input_delim_len = delim.len(); - 'newline: loop { - buffer.clear(); - match buf_in.read_until(newline_char, &mut buffer) { - Ok(n) if n == 0 => break, - Err(e) => { - if buffer.is_empty() { - crash!(1, "read error: {}", e); - } - } - _ => (), - } - - let line = &buffer[..]; + let result = buf_in.for_byte_record_with_terminator(newline_char, |line| { let mut fields_pos = 1; let mut low_idx = 0; let mut delim_search = Searcher::new(line, delim.as_bytes()).peekable(); @@ -233,46 +195,46 @@ fn cut_fields_delimiter( if delim_search.peek().is_none() { if !only_delimited { - crash_if_err!(1, out.write_all(line)); + out.write_all(line)?; if line[line.len() - 1] != newline_char { - crash_if_err!(1, out.write_all(&[newline_char])); + out.write_all(&[newline_char])?; } } - continue; + return Ok(true); } - for &Range { low, high } in ranges.iter() { + for &Range { low, high } in ranges { if low - fields_pos > 0 { low_idx = match delim_search.nth(low - fields_pos - 1) { - Some((_, beyond_delim)) => beyond_delim, + Some(index) => index + input_delim_len, None => break, }; } for _ in 0..=high - low { if print_delim { - crash_if_err!(1, out.write_all(out_delim.as_bytes())); + out.write_all(out_delim.as_bytes())?; + } else { + print_delim = true; } match delim_search.next() { - Some((high_idx, next_low_idx)) => { + Some(high_idx) => { let segment = &line[low_idx..high_idx]; - crash_if_err!(1, out.write_all(segment)); + out.write_all(segment)?; - print_delim = true; - - low_idx = next_low_idx; + low_idx = high_idx + input_delim_len; fields_pos = high + 1; } None => { let segment = &line[low_idx..]; - crash_if_err!(1, out.write_all(segment)); + out.write_all(segment)?; if line[line.len() - 1] == newline_char { - continue 'newline; + return Ok(true); } break; } @@ -280,9 +242,10 @@ fn cut_fields_delimiter( } } - crash_if_err!(1, out.write_all(&[newline_char])); - } - + out.write_all(&[newline_char])?; + Ok(true) + }); + crash_if_err!(1, result); 0 } @@ -300,23 +263,11 @@ fn cut_fields(reader: R, ranges: &[Range], opts: &FieldOptions) -> i32 ); } - let mut buf_in = BufReader::new(reader); - let mut out = stdout(); - let mut buffer = Vec::new(); + let buf_in = BufReader::new(reader); + let mut out = stdout_writer(); + let delim_len = opts.delimiter.len(); - 'newline: loop { - buffer.clear(); - match buf_in.read_until(newline_char, &mut buffer) { - Ok(n) if n == 0 => break, - Err(e) => { - if buffer.is_empty() { - crash!(1, "read error: {}", e); - } - } - _ => (), - } - - let line = &buffer[..]; + let result = buf_in.for_byte_record_with_terminator(newline_char, |line| { let mut fields_pos = 1; let mut low_idx = 0; let mut delim_search = Searcher::new(line, opts.delimiter.as_bytes()).peekable(); @@ -324,53 +275,54 @@ fn cut_fields(reader: R, ranges: &[Range], opts: &FieldOptions) -> i32 if delim_search.peek().is_none() { if !opts.only_delimited { - crash_if_err!(1, out.write_all(line)); + out.write_all(line)?; if line[line.len() - 1] != newline_char { - crash_if_err!(1, out.write_all(&[newline_char])); + out.write_all(&[newline_char])?; } } - continue; + return Ok(true); } - for &Range { low, high } in ranges.iter() { + for &Range { low, high } in ranges { if low - fields_pos > 0 { - low_idx = match delim_search.nth(low - fields_pos - 1) { - Some((_, beyond_delim)) => beyond_delim, - None => break, - }; - } - - if print_delim && low_idx >= opts.delimiter.as_bytes().len() { - low_idx -= opts.delimiter.as_bytes().len(); + if let Some(delim_pos) = delim_search.nth(low - fields_pos - 1) { + low_idx = if print_delim { + delim_pos + } else { + delim_pos + delim_len + } + } else { + break; + } } match delim_search.nth(high - low) { - Some((high_idx, next_low_idx)) => { + Some(high_idx) => { let segment = &line[low_idx..high_idx]; - crash_if_err!(1, out.write_all(segment)); + out.write_all(segment)?; print_delim = true; - low_idx = next_low_idx; + low_idx = high_idx; fields_pos = high + 1; } None => { let segment = &line[low_idx..line.len()]; - crash_if_err!(1, out.write_all(segment)); + out.write_all(segment)?; if line[line.len() - 1] == newline_char { - continue 'newline; + return Ok(true); } break; } } } - - crash_if_err!(1, out.write_all(&[newline_char])); - } - + out.write_all(&[newline_char])?; + Ok(true) + }); + crash_if_err!(1, result); 0 } @@ -398,8 +350,13 @@ fn cut_files(mut filenames: Vec, mode: Mode) -> i32 { } else { let path = Path::new(&filename[..]); - if !path.exists() { - show_error!("{}", msg_args_nonexistent_file!(filename)); + if path.is_dir() { + show_error!("{}: Is a directory", filename); + continue; + } + + if path.metadata().is_err() { + show_error!("{}: No such file or directory", filename); continue; } @@ -422,67 +379,87 @@ fn cut_files(mut filenames: Vec, mode: Mode) -> i32 { exit_code } -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); +mod options { + pub const BYTES: &str = "bytes"; + pub const CHARACTERS: &str = "characters"; + pub const DELIMITER: &str = "delimiter"; + pub const FIELDS: &str = "fields"; + pub const ZERO_TERMINATED: &str = "zero-terminated"; + pub const ONLY_DELIMITED: &str = "only-delimited"; + pub const OUTPUT_DELIMITER: &str = "output-delimiter"; + pub const COMPLEMENT: &str = "complement"; + pub const FILE: &str = "file"; +} - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optopt("b", "bytes", "filter byte columns from the input source", "sequence") - .optopt("c", "characters", "alias for character mode", "sequence") - .optopt("d", "delimiter", "specify the delimiter character that separates fields in the input source. Defaults to Tab.", "delimiter") - .optopt("f", "fields", "filter field columns from the input source", "sequence") - .optflag("n", "", "legacy option - has no effect.") - .optflag("", "complement", "invert the filter - instead of displaying only the filtered columns, display all but those columns") - .optflag("s", "only-delimited", "in field mode, only print lines which contain the delimiter") - .optflag("z", "zero-terminated", "instead of filtering columns based on line, filter columns based on \\0 (NULL character)") - .optopt("", "output-delimiter", "in field mode, replace the delimiter in output lines with this option's argument", "new delimiter") - .parse(args); - let complement = matches.opt_present("complement"); +pub fn uumain(args: impl uucore::Args) -> i32 { + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); + + let matches = uu_app().get_matches_from(args); + + let complement = matches.is_present(options::COMPLEMENT); let mode_parse = match ( - matches.opt_str("bytes"), - matches.opt_str("characters"), - matches.opt_str("fields"), + matches.value_of(options::BYTES), + matches.value_of(options::CHARACTERS), + matches.value_of(options::FIELDS), ) { - (Some(byte_ranges), None, None) => { - list_to_ranges(&byte_ranges[..], complement).map(|ranges| { - Mode::Bytes( - ranges, - Options { - out_delim: matches.opt_str("output-delimiter"), - zero_terminated: matches.opt_present("zero-terminated"), - }, - ) - }) - } - (None, Some(char_ranges), None) => { - list_to_ranges(&char_ranges[..], complement).map(|ranges| { - Mode::Characters( - ranges, - Options { - out_delim: matches.opt_str("output-delimiter"), - zero_terminated: matches.opt_present("zero-terminated"), - }, - ) - }) - } + (Some(byte_ranges), None, None) => list_to_ranges(byte_ranges, complement).map(|ranges| { + Mode::Bytes( + ranges, + Options { + out_delim: Some( + matches + .value_of(options::OUTPUT_DELIMITER) + .unwrap_or_default() + .to_owned(), + ), + zero_terminated: matches.is_present(options::ZERO_TERMINATED), + }, + ) + }), + (None, Some(char_ranges), None) => list_to_ranges(char_ranges, complement).map(|ranges| { + Mode::Characters( + ranges, + Options { + out_delim: Some( + matches + .value_of(options::OUTPUT_DELIMITER) + .unwrap_or_default() + .to_owned(), + ), + zero_terminated: matches.is_present(options::ZERO_TERMINATED), + }, + ) + }), (None, None, Some(field_ranges)) => { - list_to_ranges(&field_ranges[..], complement).and_then(|ranges| { - let out_delim = match matches.opt_str("output-delimiter") { + list_to_ranges(field_ranges, complement).and_then(|ranges| { + let out_delim = match matches.value_of(options::OUTPUT_DELIMITER) { Some(s) => { if s.is_empty() { Some("\0".to_owned()) } else { - Some(s) + Some(s.to_owned()) } } None => None, }; - let only_delimited = matches.opt_present("only-delimited"); - let zero_terminated = matches.opt_present("zero-terminated"); + let only_delimited = matches.is_present(options::ONLY_DELIMITED); + let zero_terminated = matches.is_present(options::ZERO_TERMINATED); - match matches.opt_str("delimiter") { - Some(delim) => { + match matches.value_of(options::DELIMITER) { + Some(mut delim) => { + // GNU's `cut` supports `-d=` to set the delimiter to `=`. + // Clap parsing is limited in this situation, see: + // https://github.com/uutils/coreutils/issues/2424#issuecomment-863825242 + // Since clap parsing handles `-d=` as delimiter explicitly set to "" and + // an empty delimiter is not accepted by GNU's `cut` (and makes no sense), + // we can use this as basis for a simple workaround: + if delim.is_empty() { + delim = "="; + } if delim.chars().count() > 1 { Err(msg_opt_invalid_should_be!( "empty or 1 character long", @@ -494,7 +471,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let delim = if delim.is_empty() { "\0".to_owned() } else { - delim + delim.to_owned() }; Ok(Mode::Fields( @@ -533,10 +510,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let mode_parse = match mode_parse { Err(_) => mode_parse, Ok(mode) => match mode { - Mode::Bytes(_, _) | Mode::Characters(_, _) if matches.opt_present("delimiter") => Err( - msg_opt_only_usable_if!("printing a sequence of fields", "--delimiter", "-d"), - ), - Mode::Bytes(_, _) | Mode::Characters(_, _) if matches.opt_present("only-delimited") => { + Mode::Bytes(_, _) | Mode::Characters(_, _) + if matches.is_present(options::DELIMITER) => + { + Err(msg_opt_only_usable_if!( + "printing a sequence of fields", + "--delimiter", + "-d" + )) + } + Mode::Bytes(_, _) | Mode::Characters(_, _) + if matches.is_present(options::ONLY_DELIMITED) => + { Err(msg_opt_only_usable_if!( "printing a sequence of fields", "--only-delimited", @@ -547,11 +532,101 @@ pub fn uumain(args: impl uucore::Args) -> i32 { }, }; + let files: Vec = matches + .values_of(options::FILE) + .unwrap_or_default() + .map(str::to_owned) + .collect(); + match mode_parse { - Ok(mode) => cut_files(matches.free, mode), + Ok(mode) => cut_files(files, mode), Err(err_msg) => { show_error!("{}", err_msg); 1 } } } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(SYNTAX) + .about(SUMMARY) + .after_help(LONG_HELP) + .arg( + Arg::with_name(options::BYTES) + .short("b") + .long(options::BYTES) + .takes_value(true) + .help("filter byte columns from the input source") + .allow_hyphen_values(true) + .value_name("LIST") + .display_order(1), + ) + .arg( + Arg::with_name(options::CHARACTERS) + .short("c") + .long(options::CHARACTERS) + .help("alias for character mode") + .takes_value(true) + .allow_hyphen_values(true) + .value_name("LIST") + .display_order(2), + ) + .arg( + Arg::with_name(options::DELIMITER) + .short("d") + .long(options::DELIMITER) + .help("specify the delimiter character that separates fields in the input source. Defaults to Tab.") + .takes_value(true) + .value_name("DELIM") + .display_order(3), + ) + .arg( + Arg::with_name(options::FIELDS) + .short("f") + .long(options::FIELDS) + .help("filter field columns from the input source") + .takes_value(true) + .allow_hyphen_values(true) + .value_name("LIST") + .display_order(4), + ) + .arg( + Arg::with_name(options::COMPLEMENT) + .long(options::COMPLEMENT) + .help("invert the filter - instead of displaying only the filtered columns, display all but those columns") + .takes_value(false) + .display_order(5), + ) + .arg( + Arg::with_name(options::ONLY_DELIMITED) + .short("s") + .long(options::ONLY_DELIMITED) + .help("in field mode, only print lines which contain the delimiter") + .takes_value(false) + .display_order(6), + ) + .arg( + Arg::with_name(options::ZERO_TERMINATED) + .short("z") + .long(options::ZERO_TERMINATED) + .help("instead of filtering columns based on line, filter columns based on \\0 (NULL character)") + .takes_value(false) + .display_order(8), + ) + .arg( + Arg::with_name(options::OUTPUT_DELIMITER) + .long(options::OUTPUT_DELIMITER) + .help("in field mode, replace the delimiter in output lines with this option's argument") + .takes_value(true) + .value_name("NEW_DELIM") + .display_order(7), + ) + .arg( + Arg::with_name(options::FILE) + .hidden(true) + .multiple(true) + ) +} diff --git a/src/uu/cut/src/main.rs b/src/uu/cut/src/main.rs index 065a01f40..8822335f6 100644 --- a/src/uu/cut/src/main.rs +++ b/src/uu/cut/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_cut); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_cut); diff --git a/src/uu/cut/src/searcher.rs b/src/uu/cut/src/searcher.rs index f0821ff3a..5fe4a723b 100644 --- a/src/uu/cut/src/searcher.rs +++ b/src/uu/cut/src/searcher.rs @@ -5,7 +5,8 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -#[derive(Clone)] +use memchr::memchr; + pub struct Searcher<'a> { haystack: &'a [u8], needle: &'a [u8], @@ -14,6 +15,7 @@ pub struct Searcher<'a> { impl<'a> Searcher<'a> { pub fn new(haystack: &'a [u8], needle: &'a [u8]) -> Searcher<'a> { + assert!(!needle.is_empty()); Searcher { haystack, needle, @@ -23,30 +25,81 @@ impl<'a> Searcher<'a> { } impl<'a> Iterator for Searcher<'a> { - type Item = (usize, usize); + type Item = usize; - fn next(&mut self) -> Option<(usize, usize)> { - if self.needle.len() == 1 { - for offset in self.position..self.haystack.len() { - if self.haystack[offset] == self.needle[0] { - self.position = offset + 1; - return Some((offset, offset + 1)); + fn next(&mut self) -> Option { + loop { + if let Some(match_idx) = memchr(self.needle[0], self.haystack) { + if self.needle.len() == 1 + || self.haystack[match_idx + 1..].starts_with(&self.needle[1..]) + { + let match_pos = self.position + match_idx; + let skip = match_idx + self.needle.len(); + self.haystack = &self.haystack[skip..]; + self.position += skip; + return Some(match_pos); + } else { + let skip = match_idx + 1; + self.haystack = &self.haystack[skip..]; + self.position += skip; + // continue } - } - - self.position = self.haystack.len(); - return None; - } - - while self.position + self.needle.len() <= self.haystack.len() { - if &self.haystack[self.position..self.position + self.needle.len()] == self.needle { - let match_pos = self.position; - self.position += self.needle.len(); - return Some((match_pos, match_pos + self.needle.len())); } else { - self.position += 1; + return None; } } - None + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + const NEEDLE: &[u8] = "ab".as_bytes(); + + #[test] + fn test_normal() { + let iter = Searcher::new("a.a.a".as_bytes(), "a".as_bytes()); + let items: Vec = iter.collect(); + assert_eq!(vec![0, 2, 4], items); + } + + #[test] + fn test_empty() { + let iter = Searcher::new("".as_bytes(), "a".as_bytes()); + let items: Vec = iter.collect(); + assert_eq!(vec![] as Vec, items); + } + + fn test_multibyte(line: &[u8], expected: Vec) { + let iter = Searcher::new(line, NEEDLE); + let items: Vec = iter.collect(); + assert_eq!(expected, items); + } + + #[test] + fn test_multibyte_normal() { + test_multibyte("...ab...ab...".as_bytes(), vec![3, 8]); + } + + #[test] + fn test_multibyte_needle_head_at_end() { + test_multibyte("a".as_bytes(), vec![]); + } + + #[test] + fn test_multibyte_starting_needle() { + test_multibyte("ab...ab...".as_bytes(), vec![0, 5]); + } + + #[test] + fn test_multibyte_trailing_needle() { + test_multibyte("...ab...ab".as_bytes(), vec![3, 8]); + } + + #[test] + fn test_multibyte_first_byte_false_match() { + test_multibyte("aA..aCaC..ab..aD".as_bytes(), vec![10]); } } diff --git a/src/uu/date/Cargo.toml b/src/uu/date/Cargo.toml index 4e3227f02..db6c077bd 100644 --- a/src/uu/date/Cargo.toml +++ b/src/uu/date/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_date" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "date ~ (uutils) display or set the current time" @@ -17,9 +17,15 @@ path = "src/date.rs" [dependencies] chrono = "0.4.4" clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +[target.'cfg(unix)'.dependencies] +libc = "0.2" + +[target.'cfg(windows)'.dependencies] +winapi = { version = "0.3", features = ["minwinbase", "sysinfoapi", "minwindef"] } + [[bin]] name = "date" path = "src/main.rs" diff --git a/src/uu/date/usage.txt b/src/uu/date/date-usage.mkd similarity index 97% rename from src/uu/date/usage.txt rename to src/uu/date/date-usage.mkd index 12df1a03c..829001095 100644 --- a/src/uu/date/usage.txt +++ b/src/uu/date/date-usage.mkd @@ -1,3 +1,8 @@ +# `date` usage + + + +``` text FORMAT controls the output. Interpreted sequences are: %% a literal % @@ -70,3 +75,4 @@ Show the time on the west coast of the US (use tzselect(1) to find TZ) Show the local time for 9AM next Friday on the west coast of the US $ date --date='TZ="America/Los_Angeles" 09:00 next Fri' +``` diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index 5ee4b1610..0071b5e8c 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -6,19 +6,25 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (format) MMDDhhmm -// spell-checker:ignore (ToDO) DATEFILE +// spell-checker:ignore (chrono) Datelike Timelike ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes #[macro_use] extern crate uucore; -use clap::{App, Arg}; - -use chrono::offset::Utc; -use chrono::{DateTime, FixedOffset, Local, Offset}; +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")))] +use libc::{clock_settime, timespec, CLOCK_REALTIME}; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::PathBuf; +#[cfg(windows)] +use winapi::{ + shared::minwindef::WORD, + um::{minwinbase::SYSTEMTIME, sysinfoapi::SetSystemTime}, +}; // Options const DATE: &str = "date"; @@ -31,7 +37,6 @@ const SECOND: &str = "second"; const NS: &str = "ns"; const NAME: &str = "date"; -const VERSION: &str = env!("CARGO_PKG_VERSION"); const ABOUT: &str = "print or set the system date and time"; const OPT_DATE: &str = "date"; @@ -62,6 +67,11 @@ 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"))] +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)"; + /// Settings for this program, parsed from the command line struct Settings { utc: bool, @@ -132,10 +142,118 @@ pub fn uumain(args: impl uucore::Args) -> i32 { {0} [OPTION]... [MMDDhhmm[[CC]YY][.ss]]", NAME ); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&syntax[..]).get_matches_from(args); + + let format = if let Some(form) = matches.value_of(OPT_FORMAT) { + if !form.starts_with('+') { + eprintln!("date: invalid date '{}'", form); + return 1; + } + let form = form[1..].to_string(); + Format::Custom(form) + } else if let Some(fmt) = matches + .values_of(OPT_ISO_8601) + .map(|mut iter| iter.next().unwrap_or(DATE).into()) + { + Format::Iso8601(fmt) + } else if matches.is_present(OPT_RFC_EMAIL) { + Format::Rfc5322 + } else if let Some(fmt) = matches.value_of(OPT_RFC_3339).map(Into::into) { + Format::Rfc3339(fmt) + } else { + Format::Default + }; + + let date_source = if let Some(date) = matches.value_of(OPT_DATE) { + DateSource::Custom(date.into()) + } else if let Some(file) = matches.value_of(OPT_FILE) { + DateSource::File(file.into()) + } else { + DateSource::Now + }; + + let set_to = match matches.value_of(OPT_SET).map(parse_date) { + None => None, + Some(Err((input, _err))) => { + eprintln!("date: invalid date '{}'", input); + return 1; + } + Some(Ok(date)) => Some(date), + }; + + let settings = Settings { + utc: matches.is_present(OPT_UNIVERSAL), + format, + date_source, + set_to, + }; + + if let Some(date) = settings.set_to { + // All set time functions expect UTC datetimes. + let date: DateTime = if settings.utc { + date.with_timezone(&Utc) + } else { + date.into() + }; + + return set_system_datetime(date); + } else { + // Declare a file here because it needs to outlive the `dates` iterator. + let file: File; + + // Get the current time, either in the local time zone or UTC. + let now: DateTime = if settings.utc { + let now = Utc::now(); + now.with_timezone(&now.offset().fix()) + } else { + let now = Local::now(); + now.with_timezone(now.offset()) + }; + + // Iterate over all dates - whether it's a single date or a file. + let dates: Box> = match settings.date_source { + DateSource::Custom(ref input) => { + let date = parse_date(input.clone()); + let iter = std::iter::once(date); + Box::new(iter) + } + DateSource::File(ref path) => { + file = File::open(path).unwrap(); + let lines = BufReader::new(file).lines(); + let iter = lines.filter_map(Result::ok).map(parse_date); + Box::new(iter) + } + DateSource::Now => { + let iter = std::iter::once(Ok(now)); + Box::new(iter) + } + }; + + let format_string = make_format_string(&settings); + + // Format all the dates + for date in dates { + match date { + Ok(date) => { + // GNU `date` uses `%N` for nano seconds, however crate::chrono uses `%f` + let format_string = &format_string.replace("%N", "%f"); + let formatted = date.format(format_string).to_string().replace("%f", "%N"); + println!("{}", formatted); + } + Err((input, _err)) => { + println!("date: invalid date '{}'", input); + } + } + } + } + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&syntax[..]) .arg( Arg::with_name(OPT_DATE) .short("d") @@ -186,7 +304,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .short("s") .long(OPT_SET) .takes_value(true) - .help("set time described by STRING"), + .help(OPT_SET_HELP_STRING), ) .arg( Arg::with_name(OPT_UNIVERSAL) @@ -195,103 +313,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .alias(OPT_UNIVERSAL_2) .help("print or set Coordinated Universal Time (UTC)"), ) - .arg(Arg::with_name(OPT_FORMAT).multiple(true)) - .get_matches_from(args); - - let format = if let Some(form) = matches.value_of(OPT_FORMAT) { - let form = form[1..].into(); - Format::Custom(form) - } else if let Some(fmt) = matches - .values_of(OPT_ISO_8601) - .map(|mut iter| iter.next().unwrap_or(DATE).into()) - { - Format::Iso8601(fmt) - } else if matches.is_present(OPT_RFC_EMAIL) { - Format::Rfc5322 - } else if let Some(fmt) = matches.value_of(OPT_RFC_3339).map(Into::into) { - Format::Rfc3339(fmt) - } else { - Format::Default - }; - - let date_source = if let Some(date) = matches.value_of(OPT_DATE) { - DateSource::Custom(date.into()) - } else if let Some(file) = matches.value_of(OPT_FILE) { - DateSource::File(file.into()) - } else { - DateSource::Now - }; - - let settings = Settings { - utc: matches.is_present(OPT_UNIVERSAL), - format, - date_source, - // TODO: Handle this option: - set_to: None, - }; - - if let Some(_time) = settings.set_to { - unimplemented!(); - // Probably need to use this syscall: - // https://doc.rust-lang.org/libc/i686-unknown-linux-gnu/libc/fn.clock_settime.html - } else { - // Declare a file here because it needs to outlive the `dates` iterator. - let file: File; - - // Get the current time, either in the local time zone or UTC. - let now: DateTime = if settings.utc { - let now = Utc::now(); - now.with_timezone(&now.offset().fix()) - } else { - let now = Local::now(); - now.with_timezone(now.offset()) - }; - - /// Parse a `String` into a `DateTime`. - /// If it fails, return a tuple of the `String` along with its `ParseError`. - fn parse_date( - s: String, - ) -> Result, (String, chrono::format::ParseError)> { - // TODO: The GNU date command can parse a wide variety of inputs. - s.parse().map_err(|e| (s, e)) - } - - // Iterate over all dates - whether it's a single date or a file. - let dates: Box> = match settings.date_source { - DateSource::Custom(ref input) => { - let date = parse_date(input.clone()); - let iter = std::iter::once(date); - Box::new(iter) - } - DateSource::File(ref path) => { - file = File::open(path).unwrap(); - let lines = BufReader::new(file).lines(); - let iter = lines.filter_map(Result::ok).map(parse_date); - Box::new(iter) - } - DateSource::Now => { - let iter = std::iter::once(Ok(now)); - Box::new(iter) - } - }; - - let format_string = make_format_string(&settings); - - // Format all the dates - for date in dates { - match date { - Ok(date) => { - let formatted = date.format(format_string); - println!("{}", formatted); - } - Err((input, _err)) => { - println!("date: invalid date '{}'", input); - } - } - } - } - - 0 + .arg(Arg::with_name(OPT_FORMAT).multiple(false)) } /// Return the appropriate format string for the given settings. @@ -314,3 +336,76 @@ fn make_format_string(settings: &Settings) -> &str { Format::Default => "%c", } } + +/// Parse a `String` into a `DateTime`. +/// If it fails, return a tuple of the `String` along with its `ParseError`. +fn parse_date + Clone>( + s: S, +) -> Result, (String, chrono::format::ParseError)> { + // TODO: The GNU date command can parse a wide variety of inputs. + s.as_ref().parse().map_err(|e| (s.as_ref().into(), e)) +} + +#[cfg(not(any(unix, windows)))] +fn set_system_datetime(_date: DateTime) -> i32 { + unimplemented!("setting date not implemented (unsupported target)"); +} + +#[cfg(target_os = "macos")] +fn set_system_datetime(_date: DateTime) -> i32 { + eprintln!("date: setting the date is not supported by macOS"); + 1 +} + +#[cfg(all(unix, not(target_os = "macos")))] +/// 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 +/// https://linux.die.net/man/3/clock_settime +/// https://www.gnu.org/software/libc/manual/html_node/Time-Types.html +fn set_system_datetime(date: DateTime) -> i32 { + let timespec = timespec { + tv_sec: date.timestamp() as _, + tv_nsec: date.timestamp_subsec_nanos() as _, + }; + + let result = unsafe { clock_settime(CLOCK_REALTIME, ×pec) }; + + if result != 0 { + let error = std::io::Error::last_os_error(); + eprintln!("date: cannot set date: {}", error); + error.raw_os_error().unwrap() + } else { + 0 + } +} + +#[cfg(windows)] +/// System call to set date (Windows). +/// See here for more: +/// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-setsystemtime +/// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime +fn set_system_datetime(date: DateTime) -> i32 { + let system_time = SYSTEMTIME { + wYear: date.year() as WORD, + wMonth: date.month() as WORD, + // Ignored + wDayOfWeek: 0, + wDay: date.day() as WORD, + wHour: date.hour() as WORD, + wMinute: date.minute() as WORD, + wSecond: date.second() as WORD, + // TODO: be careful of leap seconds - valid range is [0, 999] - how to handle? + wMilliseconds: ((date.nanosecond() / 1_000_000) % 1000) as WORD, + }; + + let result = unsafe { SetSystemTime(&system_time) }; + + if result == 0 { + let error = std::io::Error::last_os_error(); + eprintln!("date: cannot set date: {}", error); + error.raw_os_error().unwrap() + } else { + 0 + } +} diff --git a/src/uu/date/src/main.rs b/src/uu/date/src/main.rs index 13edc0fba..9064c7f67 100644 --- a/src/uu/date/src/main.rs +++ b/src/uu/date/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_date); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_date); diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index 74ee24c46..c81d8da2f 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -9,6 +9,7 @@ #[macro_use] extern crate uucore; +use uucore::InvalidEncodingHandling; #[cfg(test)] mod dd_unit_tests; @@ -19,7 +20,6 @@ mod conversion_tables; use conversion_tables::*; use byte_unit::Byte; -// #[macro_use] use debug_print::debug_println; use gcd::Gcd; use getopts; @@ -46,7 +46,7 @@ use std::thread; use std::time; const SYNTAX: &str = "dd [OPERAND]...\ndd OPTION"; -const SUMMARY: &str = "convert, and optionally copy, a file"; +const SUMMARY: &str = "copy, and optionally convert, a file system resource"; const LONG_HELP: &str = ""; const BUF_INIT_BYTE: u8 = 0xDD; const RTN_SUCCESS: i32 = 0; @@ -518,6 +518,13 @@ impl Output { 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(amt) = seek + { + let amt: u64 = amt.try_into()?; + dst.seek(io::SeekFrom::Start(amt))?; + } Ok(Output { dst: io::stdout(), @@ -695,11 +702,37 @@ impl Write for Output } } +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 { - self.dst.write(buf) + #[inline] + 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<()> @@ -1375,124 +1408,6 @@ fn dd_fileout(mut i: Input, mut o: Output) -> Result<(), Box - { - app!(SYNTAX, SUMMARY, LONG_HELP) - .optopt( - "", - "skip", - "Skip N ‘ibs’-byte blocks in the input file before copying. If ‘iflag=skip_bytes’ is specified, N is interpreted as a byte count rather than a block count.", - "N" - ) - .optopt( - "", - "seek", - "Skip N ‘obs’-byte blocks in the input file before copying. If ‘oflag=skip_bytes’ is specified, N is interpreted as a byte count rather than a block count.", - "N" - ) - .optopt( - "", - "count", - "Copy N ‘ibs’-byte blocks from the input file, instead of everything until the end of the file. if ‘iflag=count_bytes’ is specified, N is interpreted as a byte count rather than a block count. Note if the input may return short reads as could be the case when reading - from a pipe for example, ‘iflag=fullblock’ will ensure that ‘count=’ corresponds to complete input blocks rather than the traditional POSIX specified behavior of counting input read operations.", - "BYTES" - ) - .optopt( - "", - "bs", - "Set both input and output block sizes to BYTES. This makes ‘dd’ read and write BYTES per block, overriding any ‘ibs’ and ‘obs’ settings. In addition, if no data-transforming ‘conv’ option is specified, input is copied to the output as soon as it’s read, even - if it is smaller than the block size.", - "BYTES" - ) - .optopt( - "", - "iflag", - "read as per the comma separated symbol list of flags", - "FLAG" - ) - .optopt( - "", - "oflag", - "write as per the comma separated symbol list of flags", - "FLAG" - ) - .optopt( - "", - "if", - "Read from FILE instead of standard input.", - "FILE" - ) - .optopt( - "", - "ibs", - "Set the input block size to BYTES. This makes ‘dd’ read BYTES per block. The default is 512 bytes.", - "BYTES" - ) - .optopt( - "", - "of", - "Write to FILE instead of standard output. Unless ‘conv=notrunc’ is given, ‘dd’ truncates FILE to zero bytes (or the size specified with ‘seek=’).", - "FILE" - ) - .optopt( - "", - "obs", - "Set the output block size to BYTES. This makes ‘dd’ write BYTES per block. The default is 512 bytes.", - "BYTES" - ) - .optopt( - "", - "conv", - "Convert the file as specified by the CONVERSION argument(s). (No spaces around any comma(s).)", - "OPT[,OPT]..." - ) - .optopt( - "", - "cbs", - "Set the conversion block size to BYTES. When converting variable-length records to fixed-length ones (‘conv=block’) or the reverse (‘conv=unblock’), use BYTES as the fixed record length.", - "BYTES" - ) - .optopt( - "", - "status", - "Specify the amount of information printed. If this operand is - given multiple times, the last one takes precedence. The LEVEL - value can be one of the following: - - ‘none’ - Do not print any informational or warning messages to stderr. - Error messages are output as normal. - - ‘noxfer’ - Do not print the final transfer rate and volume statistics - that normally make up the last status line. - - ‘progress’ - Print the transfer rate and volume statistics on stderr, when - processing each input block. Statistics are output on a - single line at most once every second, but updates can be - delayed when waiting on I/O. - - Transfer information is normally output to stderr upon receipt of - the ‘INFO’ signal or when ‘dd’ exits, and defaults to the following - form in the C locale: - - 7287+1 records in - 116608+0 records out - 59703296 bytes (60 MB, 57 MiB) copied, 0.0427974 s, 1.4 GB/s - - The notation ‘W+P’ stands for W whole blocks and P partial blocks. - A partial block occurs when a read or write operation succeeds but - transfers less data than the block size. An additional line like - ‘1 truncated record’ or ‘10 truncated records’ is output after the - ‘records out’ line if ‘conv=block’ processing truncated one or more - input records.", - "LEVEL" - ) - } -); fn append_dashes_if_not_present(mut acc: Vec, s: &String) -> Vec { @@ -1527,7 +1442,8 @@ macro_rules! unpack_or_rtn ( pub fn uumain(args: impl uucore::Args) -> i32 { - let dashed_args = args.collect_str() + let dashed_args = args.collect_str(InvalidEncodingHandling::Ignore) + .accept_any() .iter() .fold(Vec::new(), append_dashes_if_not_present); let matches = build_app!().parse(dashed_args); @@ -1573,3 +1489,175 @@ pub fn uumain(args: impl uucore::Args) -> i32 } } +pub fn uu_app() -> App<'static, 'static> +{ + build_app!() +} + +#[macro_export] +macro_rules! build_app ( + () => + { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::INFILE) + .long(options::INFILE) + .takes_value(true) + .help("if=FILE (alternatively --if FILE) specifies the file used for input. When not specified, stdin is used instead") + ) + .arg( + Arg::with_name(options::OUTFILE) + .long(options::OUTFILE) + .takes_value(true) + .help("of=FILE (alternatively --of FILE) specifies the file used for output. When not specified, stdout is used instead") + ) + .arg( + Arg::with_name(options::IBS) + .long(options::IBS) + .takes_value(true) + .help("ibs=N (alternatively --ibs N) specifies the size of buffer used for reads (default: 512). Multiplier strings permitted.") + ) + .arg( + Arg::with_name(options::OBS) + .long(options::OBS) + .takes_value(true) + .help("obs=N (alternatively --obs N) specifies the size of buffer used for writes (default: 512). Multiplier strings permitted.") + ) + .arg( + Arg::with_name(options::BS) + .long(options::BS) + .takes_value(true) + .help("bs=N (alternatively --bs N) specifies ibs=N and obs=N (default: 512). If ibs or obs are also specified, bs=N takes presedence. Multiplier strings permitted.") + ) + .arg( + Arg::with_name(options::CBS) + .long(options::CBS) + .takes_value(true) + .help("cbs=BYTES (alternatively --cbs BYTES) specifies the 'conversion block size' in bytes. Applies to the conv=block, and conv=unblock operations. Multiplier strings permitted.") + ) + .arg( + Arg::with_name(options::SKIP) + .long(options::SKIP) + .takes_value(true) + .help("skip=N (alternatively --skip N) causes N ibs-sized records of input to be skipped before beginning copy/convert operations. See iflag=count_bytes if skipping N bytes is prefered. Multiplier strings permitted.") + ) + .arg( + Arg::with_name(options::SEEK) + .long(options::SEEK) + .takes_value(true) + .help("seek=N (alternatively --seek N) seeks N obs-sized records into output before beginning copy/convert operations. See oflag=seek_bytes if seeking N bytes is prefered. Multiplier strings permitted.") + ) + .arg( + Arg::with_name(options::COUNT) + .long(options::COUNT) + .takes_value(true) + .help("count=N (alternatively --count N) stop reading input after N ibs-sized read operations rather than proceeding until EOF. See iflag=count_bytes if stopping after N bytes is prefered. Multiplier strings permitted.") + ) + .arg( + Arg::with_name(options::STATUS) + .long(options::STATUS) + .takes_value(true) + .help("status=LEVEL (alternatively --status LEVEL) controls whether volume and performace stats are written to stderr. + +When unspecified, dd will print stats upon completion. An example is below. +\t6+0 records in +\t16+0 records out +\t8192 bytes (8.2 kB, 8.0 KiB) copied, 0.00057009 s, 14.4 MB/s +The first two lines are the 'volume' stats and the final line is the 'performance' stats. +The volume stats indicate the number of complete and partial ibs-sized reads, or obs-sized writes that took place during the copy. The format of the volume stats is +. If records have been truncated (see conv=block), the volume stats will contain the number of truncated records. + +Permissable LEVEL values are: +\t- progress: Print periodic performance stats as the copy proceedes. +\t- noxfer: Print final volume stats, but not performance stats. +\t- none: Do not print any stats. + +Printing performance stats is also triggered by the INFO signal (where supported), or the USR1 signal. Setting the POSIXLY_CORRECT evnironment variable to any value (including an empty value) will cause the USR1 signal to be ignored. +") + ) + .arg( + Arg::with_name(options::CONV) + .long(options::CONV) + .takes_value(true) + .help("conv=CONV[,CONV] (alternatively --conv CONV[,CONV]) specifies a comma-separated list of conversion options or (for legacy reasons) file-flags. Conversion options and file flags may be intermixed. + +Conversion options: +\t- One of {ascii, ebcdic, ibm} will perform an encoding conversion. +\t\t- 'ascii' converts from EBCDIC to ASCII. This is the inverse of the 'ebcdic' option. +\t\t- 'ebcdic' converts from ASCII to EBCDIC. This is the inverse of the 'ascii' option. +\t\t- 'ibm' converts from ASCII to EBCDIC, appling the conventions for '[', ']' and '~' specified in POSIX. + +\t- One of {ucase, lcase} will perform a case conversion. Works in conjuction with option {ascii, ebcdic, ibm} to infer input encoding. If no other conversion option is specified, input is assumed to be ascii. +\t\t- 'ucase' converts from lower-case to upper-case +\t\t- 'lcase' converts from upper-case to lower-case. + +\t- One of {block, unblock}. Convert between lines terminated by newline characters, and fixed-width lines padded by spaces (without any newlines). Both the 'block' and 'unblock' options require cbs=BYTES be specified. +\t\t- 'block' for each newline less than the size indicated by cbs=BYTES, remove the newline and pad with spaces up to cbs. Lines longer than cbs are truncated. +\t\t- 'unblock' for each block of input of the size indicated by cbs=BYTES, remove right-trailing spaces and replace with a newline character. + +\t 'sparse' attempts to seek the output when an obs-sized block consists of only zeros. +\t 'swab' swaps each adjacent pair of bytes. If an odd number of bytes is present, the final byte is omitted. +\t 'sync' pad each ibs-sided block with zeros. If 'block' or 'unblock' is specified, pad with spaces instead. + +Flags: +\t- One of {excl, nocreat} +\t\t- 'excl' the output file must be created. Fail if the output file is already present. +\t\t- 'nocreat' the output file will not be created. Fail if the output file in not already present. +\t- 'notrunc' the output file will not be truncated. If this option is not present, output will be truncated when opened. +\t- 'noerror' all read errors will be ignored. If this option is not present, dd will only ignore Error::Interrupted. +\t- 'fdatasync' data will be written before finishing. +\t- 'fsync' data and metadata will be written before finishing. +") + ) + .arg( + Arg::with_name(options::IFLAG) + .long(options::IFLAG) + .takes_value(true) + .help("iflag=FLAG[,FLAG] (alternatively --iflag FLAG[,FLAG]) a comma separated list of input flags which specify how the input source is treated. FLAG may be any of the input-flags or general-flags specified below. + +Input-Flags +\t- 'count_bytes' a value to count=N will be interpreted as bytes. +\t- 'skip_bytes' a value to skip=N will be interpreted as bytes. +\t- 'fullblock' wait for ibs bytes from each read. zero-length reads are still considered EOF. + +General-Flags +\t- 'direct' +\t- 'directory' +\t- 'dsync' +\t- 'sync' +\t- 'nonblock' +\t- 'noatime' +\t- 'nocache' +\t- 'noctty' +\t- 'nofollow' + +Output-Flags +\t- 'append' open file in append mode. Consider setting conv=notrunc as well. +\t- 'seek_bytes' a value to seek=N will be interpreted as bytes. +") + ) + .arg( + Arg::with_name(options::OFLAG) + .long(options::OFLAG) + .takes_value(true) + .help("oflag=FLAG[,FLAG] (alternatively --oflag FLAG[,FLAG]) a comma separated list of output flags which specify how the output source is treated. FLAG may be any of the output-flags or general-flags specified below. + +Output-Flags +\t- 'append' open file in append mode. Consider setting conv=notrunc as well. +\t- 'seek_bytes' a value to seek=N will be interpreted as bytes. + +General-Flags +\t- 'direct' +\t- 'directory' +\t- 'dsync' +\t- 'sync' +\t- 'nonblock' +\t- 'noatime' +\t- 'nocache' +\t- 'noctty' +\t- 'nofollow' +") + ) + }; +); diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index 24fce763b..0e65fdb32 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_df" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "df ~ (uutils) display file system information" @@ -16,14 +16,10 @@ path = "src/df.rs" [dependencies] clap = "2.33" -libc = "0.2" number_prefix = "0.4" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["libc", "fsext"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } -[target.'cfg(target_os = "windows")'.dependencies] -winapi = { version = "0.3", features = ["errhandlingapi", "fileapi", "handleapi", "winerror"] } - [[bin]] name = "df" path = "src/main.rs" diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index ed2865728..1092938df 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -6,21 +6,13 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -// spell-checker:ignore (ToDO) mountinfo mtab BLOCKSIZE getmntinfo fobj mptr noatime Iused overmounted -// spell-checker:ignore (libc/fs) asyncreads asyncwrites autofs bavail bfree bsize charspare cifs debugfs devfs devpts ffree frsize fsid fstypename fusectl inode inodes iosize kernfs mntbufp mntfromname mntonname mqueue namemax pipefs smbfs statfs statvfs subfs syncreads syncwrites sysfs wcslen - #[macro_use] extern crate uucore; +#[cfg(unix)] +use uucore::fsext::statfs_fn; +use uucore::fsext::{read_fs_list, FsUsage, MountInfo}; -use clap::{App, Arg}; - -#[cfg(windows)] -use winapi::um::errhandlingapi::GetLastError; -#[cfg(windows)] -use winapi::um::fileapi::{ - FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, GetDriveTypeW, GetVolumeInformationW, - GetVolumePathNamesForVolumeNameW, QueryDosDeviceW, -}; +use clap::{crate_version, App, Arg}; use number_prefix::NumberPrefix; use std::cell::Cell; @@ -32,57 +24,18 @@ use std::ffi::CString; #[cfg(unix)] use std::mem; -#[cfg(any(target_os = "macos", target_os = "freebsd"))] -use libc::c_int; -#[cfg(target_os = "macos")] -use libc::statfs; -#[cfg(any(target_os = "macos", target_os = "freebsd"))] -use std::ffi::CStr; -#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "windows"))] -use std::ptr; -#[cfg(any(target_os = "macos", target_os = "freebsd"))] -use std::slice; - #[cfg(target_os = "freebsd")] -use libc::{c_char, fsid_t, uid_t}; +use uucore::libc::{c_char, fsid_t, uid_t}; -#[cfg(target_os = "linux")] -use std::fs::File; -#[cfg(target_os = "linux")] -use std::io::{BufRead, BufReader}; - -#[cfg(windows)] -use std::ffi::OsString; -#[cfg(windows)] -use std::os::windows::ffi::OsStrExt; -#[cfg(windows)] -use std::os::windows::ffi::OsStringExt; #[cfg(windows)] use std::path::Path; -#[cfg(windows)] -use winapi::shared::minwindef::DWORD; -#[cfg(windows)] -use winapi::um::fileapi::GetDiskFreeSpaceW; -#[cfg(windows)] -use winapi::um::handleapi::INVALID_HANDLE_VALUE; -#[cfg(windows)] -use winapi::um::winbase::DRIVE_REMOTE; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Show information about the file system on which each FILE resides,\n\ or all file systems by default."; static EXIT_OK: i32 = 0; static EXIT_ERR: i32 = 1; -#[cfg(windows)] -const MAX_PATH: usize = 266; - -#[cfg(target_os = "linux")] -static LINUX_MOUNTINFO: &str = "/proc/self/mountinfo"; -#[cfg(target_os = "linux")] -static LINUX_MTAB: &str = "/etc/mtab"; - static OPT_ALL: &str = "all"; static OPT_BLOCKSIZE: &str = "blocksize"; static OPT_DIRECT: &str = "direct"; @@ -101,8 +54,6 @@ static OPT_TYPE: &str = "type"; static OPT_PRINT_TYPE: &str = "print-type"; static OPT_EXCLUDE_TYPE: &str = "exclude-type"; -static MOUNT_OPT_BIND: &str = "bind"; - /// Store names of file systems as a selector. /// Note: `exclude` takes priority over `include`. struct FsSelector { @@ -116,142 +67,21 @@ struct Options { show_listed_fs: bool, show_fs_type: bool, show_inode_instead: bool, - print_grand_total: bool, // block_size: usize, human_readable_base: i64, fs_selector: FsSelector, } -#[derive(Debug, Clone)] -struct MountInfo { - // it stores `volume_name` in windows platform and `dev_id` in unix platform - dev_id: String, - dev_name: String, - fs_type: String, - mount_dir: String, - mount_option: String, // we only care "bind" option - mount_root: String, - remote: bool, - dummy: bool, -} - -#[cfg(all( - target_os = "freebsd", - not(all(target_os = "macos", target_arch = "x86_64")) -))] -#[repr(C)] -#[derive(Copy, Clone)] -#[allow(non_camel_case_types)] -struct statfs { - f_version: u32, - f_type: u32, - f_flags: u64, - f_bsize: u64, - f_iosize: u64, - f_blocks: u64, - f_bfree: u64, - f_bavail: i64, - f_files: u64, - f_ffree: i64, - f_syncwrites: u64, - f_asyncwrites: u64, - f_syncreads: u64, - f_asyncreads: u64, - f_spare: [u64; 10usize], - f_namemax: u32, - f_owner: uid_t, - f_fsid: fsid_t, - f_charspare: [c_char; 80usize], - f_fstypename: [c_char; 16usize], - f_mntfromname: [c_char; 88usize], - f_mntonname: [c_char; 88usize], -} - -#[derive(Debug, Clone)] -struct FsUsage { - blocksize: u64, - blocks: u64, - bfree: u64, - bavail: u64, - bavail_top_bit_set: bool, - files: u64, - ffree: u64, -} - #[derive(Debug, Clone)] struct Filesystem { - mountinfo: MountInfo, + mount_info: MountInfo, usage: FsUsage, } -#[cfg(windows)] -macro_rules! String2LPWSTR { - ($str: expr) => { - OsString::from($str.clone()) - .as_os_str() - .encode_wide() - .chain(Some(0)) - .collect::>() - .as_ptr() - }; -} - -#[cfg(windows)] -#[allow(non_snake_case)] -fn LPWSTR2String(buf: &[u16]) -> String { - let len = unsafe { libc::wcslen(buf.as_ptr()) }; - OsString::from_wide(&buf[..len as usize]) - .into_string() - .unwrap() -} - fn get_usage() -> String { format!("{0} [OPTION]... [FILE]...", executable!()) } -#[cfg(any(target_os = "freebsd", target_os = "macos"))] -extern "C" { - #[cfg(all(target_os = "macos", target_arch = "x86_64"))] - #[link_name = "getmntinfo$INODE64"] - fn getmntinfo(mntbufp: *mut *mut statfs, flags: c_int) -> c_int; - - #[cfg(any( - all(target_os = "freebsd"), - all(target_os = "macos", target_arch = "aarch64") - ))] - fn getmntinfo(mntbufp: *mut *mut statfs, flags: c_int) -> c_int; -} - -#[cfg(any(target_os = "freebsd", target_os = "macos"))] -impl From for MountInfo { - fn from(statfs: statfs) -> Self { - let mut info = MountInfo { - dev_id: "".to_string(), - dev_name: unsafe { - CStr::from_ptr(&statfs.f_mntfromname[0]) - .to_string_lossy() - .into_owned() - }, - fs_type: unsafe { - CStr::from_ptr(&statfs.f_fstypename[0]) - .to_string_lossy() - .into_owned() - }, - mount_dir: unsafe { - CStr::from_ptr(&statfs.f_mntonname[0]) - .to_string_lossy() - .into_owned() - }, - mount_root: "".to_string(), - mount_option: "".to_string(), - remote: false, - dummy: false, - }; - info.set_missing_fields(); - info - } -} - impl FsSelector { fn new() -> FsSelector { FsSelector { @@ -286,7 +116,6 @@ impl Options { show_listed_fs: false, show_fs_type: false, show_inode_instead: false, - print_grand_total: false, // block_size: match env::var("BLOCKSIZE") { // Ok(size) => size.parse().unwrap(), // Err(_) => 512, @@ -297,350 +126,43 @@ impl Options { } } -impl MountInfo { - fn set_missing_fields(&mut self) { - #[cfg(unix)] - { - // We want to keep the dev_id on Windows - // but set dev_id - let path = CString::new(self.mount_dir.clone()).unwrap(); - unsafe { - let mut stat = mem::zeroed(); - if libc::stat(path.as_ptr(), &mut stat) == 0 { - self.dev_id = (stat.st_dev as i32).to_string(); - } else { - self.dev_id = "".to_string(); - } - } - } - // set MountInfo::dummy - match self.fs_type.as_ref() { - "autofs" | "proc" | "subfs" - /* for Linux 2.6/3.x */ - | "debugfs" | "devpts" | "fusectl" | "mqueue" | "rpc_pipefs" | "sysfs" - /* FreeBSD, Linux 2.4 */ - | "devfs" - /* for NetBSD 3.0 */ - | "kernfs" - /* for Irix 6.5 */ - | "ignore" => self.dummy = true, - _ => self.dummy = self.fs_type == "none" - && self.mount_option.find(MOUNT_OPT_BIND).is_none(), - } - // set MountInfo::remote - #[cfg(windows)] - { - self.remote = DRIVE_REMOTE == unsafe { GetDriveTypeW(String2LPWSTR!(self.mount_root)) }; - } - #[cfg(unix)] - { - if self.dev_name.find(':').is_some() - || (self.dev_name.starts_with("//") && self.fs_type == "smbfs" - || self.fs_type == "cifs") - || self.dev_name == "-hosts" - { - self.remote = true; - } else { - self.remote = false; - } - } - } - - #[cfg(target_os = "linux")] - fn new(file_name: &str, raw: Vec<&str>) -> Option { - match file_name { - // Format: 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue - // "man proc" for more details - "/proc/self/mountinfo" => { - let mut m = MountInfo { - dev_id: "".to_string(), - dev_name: raw[9].to_string(), - fs_type: raw[8].to_string(), - mount_root: raw[3].to_string(), - mount_dir: raw[4].to_string(), - mount_option: raw[5].to_string(), - remote: false, - dummy: false, - }; - m.set_missing_fields(); - Some(m) - } - "/etc/mtab" => { - let mut m = MountInfo { - dev_id: "".to_string(), - dev_name: raw[0].to_string(), - fs_type: raw[2].to_string(), - mount_root: "".to_string(), - mount_dir: raw[1].to_string(), - mount_option: raw[3].to_string(), - remote: false, - dummy: false, - }; - m.set_missing_fields(); - Some(m) - } - _ => None, - } - } - #[cfg(windows)] - fn new(mut volume_name: String) -> Option { - let mut dev_name_buf = [0u16; MAX_PATH]; - volume_name.pop(); - unsafe { - QueryDosDeviceW( - OsString::from(volume_name.clone()) - .as_os_str() - .encode_wide() - .chain(Some(0)) - .skip(4) - .collect::>() - .as_ptr(), - dev_name_buf.as_mut_ptr(), - dev_name_buf.len() as DWORD, - ) - }; - volume_name.push('\\'); - let dev_name = LPWSTR2String(&dev_name_buf); - - let mut mount_root_buf = [0u16; MAX_PATH]; - let success = unsafe { - GetVolumePathNamesForVolumeNameW( - String2LPWSTR!(volume_name), - mount_root_buf.as_mut_ptr(), - mount_root_buf.len() as DWORD, - ptr::null_mut(), - ) - }; - if 0 == success { - // TODO: support the case when `GetLastError()` returns `ERROR_MORE_DATA` - return None; - } - let mount_root = LPWSTR2String(&mount_root_buf); - - let mut fs_type_buf = [0u16; MAX_PATH]; - let success = unsafe { - GetVolumeInformationW( - String2LPWSTR!(mount_root), - ptr::null_mut(), - 0 as DWORD, - ptr::null_mut(), - ptr::null_mut(), - ptr::null_mut(), - fs_type_buf.as_mut_ptr(), - fs_type_buf.len() as DWORD, - ) - }; - let fs_type = if 0 != success { - Some(LPWSTR2String(&fs_type_buf)) - } else { - None - }; - let mut mn_info = MountInfo { - dev_id: volume_name, - dev_name, - fs_type: fs_type.unwrap_or_else(|| "".to_string()), - mount_root, - mount_dir: "".to_string(), - mount_option: "".to_string(), - remote: false, - dummy: false, - }; - mn_info.set_missing_fields(); - Some(mn_info) - } -} - -impl FsUsage { - #[cfg(unix)] - fn new(statvfs: libc::statvfs) -> FsUsage { - { - FsUsage { - blocksize: if statvfs.f_frsize != 0 { - statvfs.f_frsize as u64 - } else { - statvfs.f_bsize as u64 - }, - blocks: statvfs.f_blocks as u64, - bfree: statvfs.f_bfree as u64, - bavail: statvfs.f_bavail as u64, - bavail_top_bit_set: ((statvfs.f_bavail as u64) & (1u64.rotate_right(1))) != 0, - files: statvfs.f_files as u64, - ffree: statvfs.f_ffree as u64, - } - } - } - #[cfg(not(unix))] - fn new(path: &Path) -> FsUsage { - let mut root_path = [0u16; MAX_PATH]; - let success = unsafe { - GetVolumePathNamesForVolumeNameW( - //path_utf8.as_ptr(), - String2LPWSTR!(path.as_os_str()), - root_path.as_mut_ptr(), - root_path.len() as DWORD, - ptr::null_mut(), - ) - }; - if 0 == success { - crash!( - EXIT_ERR, - "GetVolumePathNamesForVolumeNameW failed: {}", - unsafe { GetLastError() } - ); - } - - let mut sectors_per_cluster = 0; - let mut bytes_per_sector = 0; - let mut number_of_free_clusters = 0; - let mut total_number_of_clusters = 0; - - let success = unsafe { - GetDiskFreeSpaceW( - String2LPWSTR!(path.as_os_str()), - &mut sectors_per_cluster, - &mut bytes_per_sector, - &mut number_of_free_clusters, - &mut total_number_of_clusters, - ) - }; - if 0 == success { - // Fails in case of CD for example - //crash!(EXIT_ERR, "GetDiskFreeSpaceW failed: {}", unsafe { - //GetLastError() - //}); - } - - let bytes_per_cluster = sectors_per_cluster as u64 * bytes_per_sector as u64; - FsUsage { - // f_bsize File system block size. - blocksize: bytes_per_cluster as u64, - // f_blocks - Total number of blocks on the file system, in units of f_frsize. - // frsize = Fundamental file system block size (fragment size). - blocks: total_number_of_clusters as u64, - // Total number of free blocks. - bfree: number_of_free_clusters as u64, - // Total number of free blocks available to non-privileged processes. - bavail: 0 as u64, - bavail_top_bit_set: ((bytes_per_sector as u64) & (1u64.rotate_right(1))) != 0, - // Total number of file nodes (inodes) on the file system. - files: 0 as u64, // Not available on windows - // Total number of free file nodes (inodes). - ffree: 4096 as u64, // Meaningless on Windows - } - } -} - impl Filesystem { - // TODO: resolve uuid in `mountinfo.dev_name` if exists - fn new(mountinfo: MountInfo) -> Option { - let _stat_path = if !mountinfo.mount_dir.is_empty() { - mountinfo.mount_dir.clone() + // TODO: resolve uuid in `mount_info.dev_name` if exists + fn new(mount_info: MountInfo) -> Option { + let _stat_path = if !mount_info.mount_dir.is_empty() { + mount_info.mount_dir.clone() } else { #[cfg(unix)] { - mountinfo.dev_name.clone() + mount_info.dev_name.clone() } #[cfg(windows)] { // On windows, we expect the volume id - mountinfo.dev_id.clone() + mount_info.dev_id.clone() } }; #[cfg(unix)] unsafe { let path = CString::new(_stat_path).unwrap(); let mut statvfs = mem::zeroed(); - if libc::statvfs(path.as_ptr(), &mut statvfs) < 0 { + if statfs_fn(path.as_ptr(), &mut statvfs) < 0 { None } else { Some(Filesystem { - mountinfo, + mount_info, usage: FsUsage::new(statvfs), }) } } #[cfg(windows)] Some(Filesystem { - mountinfo, + mount_info, usage: FsUsage::new(Path::new(&_stat_path)), }) } } -/// Read file system list. -fn read_fs_list() -> Vec { - #[cfg(target_os = "linux")] - { - let (file_name, fobj) = File::open(LINUX_MOUNTINFO) - .map(|f| (LINUX_MOUNTINFO, f)) - .or_else(|_| File::open(LINUX_MTAB).map(|f| (LINUX_MTAB, f))) - .expect("failed to find mount list files"); - let reader = BufReader::new(fobj); - reader - .lines() - .filter_map(|line| line.ok()) - .filter_map(|line| { - let raw_data = line.split_whitespace().collect::>(); - MountInfo::new(file_name, raw_data) - }) - .collect::>() - } - #[cfg(any(target_os = "freebsd", target_os = "macos"))] - { - let mut mptr: *mut statfs = ptr::null_mut(); - let len = unsafe { getmntinfo(&mut mptr, 1 as c_int) }; - if len < 0 { - crash!(EXIT_ERR, "getmntinfo failed"); - } - let mounts = unsafe { slice::from_raw_parts(mptr, len as usize) }; - mounts - .iter() - .map(|m| MountInfo::from(*m)) - .collect::>() - } - #[cfg(windows)] - { - let mut volume_name_buf = [0u16; MAX_PATH]; - // As recommended in the MS documentation, retrieve the first volume before the others - let find_handle = unsafe { - FindFirstVolumeW(volume_name_buf.as_mut_ptr(), volume_name_buf.len() as DWORD) - }; - if INVALID_HANDLE_VALUE == find_handle { - crash!(EXIT_ERR, "FindFirstVolumeW failed: {}", unsafe { - GetLastError() - }); - } - let mut mounts = Vec::::new(); - loop { - let volume_name = LPWSTR2String(&volume_name_buf); - if !volume_name.starts_with("\\\\?\\") || !volume_name.ends_with('\\') { - show_warning!("A bad path was skipped: {}", volume_name); - continue; - } - if let Some(m) = MountInfo::new(volume_name) { - mounts.push(m); - } - if 0 == unsafe { - FindNextVolumeW( - find_handle, - volume_name_buf.as_mut_ptr(), - volume_name_buf.len() as DWORD, - ) - } { - let err = unsafe { GetLastError() }; - if err != winapi::shared::winerror::ERROR_NO_MORE_FILES { - crash!(EXIT_ERR, "FindNextVolumeW failed: {}", err); - } - break; - } - } - unsafe { - FindVolumeClose(find_handle); - } - mounts - } -} - fn filter_mount_list(vmi: Vec, paths: &[String], opt: &Options) -> Vec { vmi.into_iter() .filter_map(|mi| { @@ -679,7 +201,7 @@ fn filter_mount_list(vmi: Vec, paths: &[String], opt: &Options) -> Ve if (!mi.dev_name.starts_with('/') || seen.dev_name.starts_with('/')) // let points towards the root of the device win. && (!target_nearer_root || source_below_root) - // let an entry overmounted on a new device win... + // let an entry over-mounted on a new device win... && (seen.dev_name == mi.dev_name /* ... but only when matching an existing mnt point, to avoid problematic replacement when given @@ -736,10 +258,151 @@ fn use_size(free_size: u64, total_size: u64) -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let paths: Vec = matches + .values_of(OPT_PATHS) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + #[cfg(windows)] + { + if matches.is_present(OPT_INODES) { + println!("{}: doesn't support -i option", executable!()); + return EXIT_OK; + } + } + + let mut opt = Options::new(); + if matches.is_present(OPT_LOCAL) { + opt.show_local_fs = true; + } + if matches.is_present(OPT_ALL) { + opt.show_all_fs = true; + } + if matches.is_present(OPT_INODES) { + opt.show_inode_instead = true; + } + if matches.is_present(OPT_PRINT_TYPE) { + opt.show_fs_type = true; + } + if matches.is_present(OPT_HUMAN_READABLE) { + opt.human_readable_base = 1024; + } + if matches.is_present(OPT_HUMAN_READABLE_2) { + opt.human_readable_base = 1000; + } + for fs_type in matches.values_of_lossy(OPT_TYPE).unwrap_or_default() { + opt.fs_selector.include(fs_type.to_owned()); + } + for fs_type in matches + .values_of_lossy(OPT_EXCLUDE_TYPE) + .unwrap_or_default() + { + opt.fs_selector.exclude(fs_type.to_owned()); + } + + let fs_list = filter_mount_list(read_fs_list(), &paths, &opt) + .into_iter() + .filter_map(Filesystem::new) + .filter(|fs| fs.usage.blocks != 0 || opt.show_all_fs || opt.show_listed_fs) + .collect::>(); + + // set headers + let mut header = vec!["Filesystem"]; + if opt.show_fs_type { + header.push("Type"); + } + header.extend_from_slice(&if opt.show_inode_instead { + // spell-checker:disable-next-line + ["Inodes", "Iused", "IFree", "IUses%"] + } else { + [ + if opt.human_readable_base == -1 { + "1k-blocks" + } else { + "Size" + }, + "Used", + "Available", + "Use%", + ] + }); + if cfg!(target_os = "macos") && !opt.show_inode_instead { + header.insert(header.len() - 1, "Capacity"); + } + header.push("Mounted on"); + + for (idx, title) in header.iter().enumerate() { + if idx == 0 || idx == header.len() - 1 { + print!("{0: <16} ", title); + } else if opt.show_fs_type && idx == 1 { + print!("{0: <5} ", title); + } else if idx == header.len() - 2 { + print!("{0: >5} ", title); + } else { + print!("{0: >12} ", title); + } + } + println!(); + for fs in fs_list.iter() { + print!("{0: <16} ", fs.mount_info.dev_name); + if opt.show_fs_type { + print!("{0: <5} ", fs.mount_info.fs_type); + } + if opt.show_inode_instead { + print!( + "{0: >12} ", + human_readable(fs.usage.files, opt.human_readable_base) + ); + print!( + "{0: >12} ", + human_readable(fs.usage.files - fs.usage.ffree, opt.human_readable_base) + ); + print!( + "{0: >12} ", + human_readable(fs.usage.ffree, opt.human_readable_base) + ); + print!( + "{0: >5} ", + format!( + "{0:.1}%", + 100f64 - 100f64 * (fs.usage.ffree as f64 / fs.usage.files as f64) + ) + ); + } else { + let total_size = fs.usage.blocksize * fs.usage.blocks; + let free_size = fs.usage.blocksize * fs.usage.bfree; + print!( + "{0: >12} ", + human_readable(total_size, opt.human_readable_base) + ); + print!( + "{0: >12} ", + human_readable(total_size - free_size, opt.human_readable_base) + ); + print!( + "{0: >12} ", + human_readable(free_size, opt.human_readable_base) + ); + if cfg!(target_os = "macos") { + let used = fs.usage.blocks - fs.usage.bfree; + let blocks = used + fs.usage.bavail; + print!("{0: >12} ", use_size(used, blocks)); + } + print!("{0: >5} ", use_size(free_size, total_size)); + } + print!("{0: <16}", fs.mount_info.mount_dir); + println!(); + } + + EXIT_OK +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(OPT_ALL) .short("a") @@ -849,137 +512,4 @@ pub fn uumain(args: impl uucore::Args) -> i32 { ) .arg(Arg::with_name(OPT_PATHS).multiple(true)) .help("Filesystem(s) to list") - .get_matches_from(args); - - let paths: Vec = matches - .values_of(OPT_PATHS) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - #[cfg(windows)] - { - if matches.is_present(OPT_INODES) { - println!("{}: doesn't support -i option", executable!()); - return EXIT_OK; - } - } - - let mut opt = Options::new(); - if matches.is_present(OPT_LOCAL) { - opt.show_local_fs = true; - } - if matches.is_present(OPT_ALL) { - opt.show_all_fs = true; - } - if matches.is_present(OPT_TOTAL) { - opt.print_grand_total = true; - } - if matches.is_present(OPT_INODES) { - opt.show_inode_instead = true; - } - if matches.is_present(OPT_PRINT_TYPE) { - opt.show_fs_type = true; - } - if matches.is_present(OPT_HUMAN_READABLE) { - opt.human_readable_base = 1024; - } - if matches.is_present(OPT_HUMAN_READABLE_2) { - opt.human_readable_base = 1000; - } - for fs_type in matches.values_of_lossy(OPT_TYPE).unwrap_or_default() { - opt.fs_selector.include(fs_type.to_owned()); - } - for fs_type in matches - .values_of_lossy(OPT_EXCLUDE_TYPE) - .unwrap_or_default() - { - opt.fs_selector.exclude(fs_type.to_owned()); - } - - let fs_list = filter_mount_list(read_fs_list(), &paths, &opt) - .into_iter() - .filter_map(Filesystem::new) - .filter(|fs| fs.usage.blocks != 0 || opt.show_all_fs || opt.show_listed_fs) - .collect::>(); - - // set headers - let mut header = vec!["Filesystem"]; - if opt.show_fs_type { - header.push("Type"); - } - header.extend_from_slice(&if opt.show_inode_instead { - ["Inodes", "Iused", "IFree", "IUses%"] - } else { - [ - if opt.human_readable_base == -1 { - "1k-blocks" - } else { - "Size" - }, - "Used", - "Available", - "Use%", - ] - }); - header.push("Mounted on"); - - for (idx, title) in header.iter().enumerate() { - if idx == 0 || idx == header.len() - 1 { - print!("{0: <16} ", title); - } else if opt.show_fs_type && idx == 1 { - print!("{0: <5} ", title); - } else if idx == header.len() - 2 { - print!("{0: >5} ", title); - } else { - print!("{0: >12} ", title); - } - } - println!(); - for fs in fs_list.iter() { - print!("{0: <16} ", fs.mountinfo.dev_name); - if opt.show_fs_type { - print!("{0: <5} ", fs.mountinfo.fs_type); - } - if opt.show_inode_instead { - print!( - "{0: >12} ", - human_readable(fs.usage.files, opt.human_readable_base) - ); - print!( - "{0: >12} ", - human_readable(fs.usage.files - fs.usage.ffree, opt.human_readable_base) - ); - print!( - "{0: >12} ", - human_readable(fs.usage.ffree, opt.human_readable_base) - ); - print!( - "{0: >5} ", - format!( - "{0:.1}%", - 100f64 - 100f64 * (fs.usage.ffree as f64 / fs.usage.files as f64) - ) - ); - } else { - let total_size = fs.usage.blocksize * fs.usage.blocks; - let free_size = fs.usage.blocksize * fs.usage.bfree; - print!( - "{0: >12} ", - human_readable(total_size, opt.human_readable_base) - ); - print!( - "{0: >12} ", - human_readable(total_size - free_size, opt.human_readable_base) - ); - print!( - "{0: >12} ", - human_readable(free_size, opt.human_readable_base) - ); - print!("{0: >5} ", use_size(free_size, total_size)); - } - print!("{0: <16}", fs.mountinfo.mount_dir); - println!(); - } - - EXIT_OK } diff --git a/src/uu/df/src/main.rs b/src/uu/df/src/main.rs index 6062603db..a6d403782 100644 --- a/src/uu/df/src/main.rs +++ b/src/uu/df/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_df); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_df); diff --git a/src/uu/dircolors/Cargo.toml b/src/uu/dircolors/Cargo.toml index 667fc3b70..7d47fa5c4 100644 --- a/src/uu/dircolors/Cargo.toml +++ b/src/uu/dircolors/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_dircolors" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "dircolors ~ (uutils) display commands to set LS_COLORS" @@ -15,8 +15,9 @@ edition = "2018" path = "src/dircolors.rs" [dependencies] +clap = "2.33" glob = "0.3.0" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 6cb5f9e1b..70b609e31 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -1,6 +1,7 @@ // This file is part of the uutils coreutils package. // // (c) Jian Zeng +// (c) Mitchell Mebane // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. @@ -15,6 +16,15 @@ use std::env; use std::fs::File; use std::io::{BufRead, BufReader}; +use clap::{crate_version, App, Arg}; + +mod options { + pub const BOURNE_SHELL: &str = "bourne-shell"; + pub const C_SHELL: &str = "c-shell"; + pub const PRINT_DATABASE: &str = "print-database"; + pub const FILE: &str = "FILE"; +} + static SYNTAX: &str = "[OPTION]... [FILE]"; static SUMMARY: &str = "Output commands to set the LS_COLORS environment variable."; static LONG_HELP: &str = " @@ -52,26 +62,27 @@ pub fn guess_syntax() -> OutputFmt { } } +fn get_usage() -> String { + format!("{0} {1}", executable!(), SYNTAX) +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag("b", "sh", "output Bourne shell code to set LS_COLORS") - .optflag( - "", - "bourne-shell", - "output Bourne shell code to set LS_COLORS", - ) - .optflag("c", "csh", "output C shell code to set LS_COLORS") - .optflag("", "c-shell", "output C shell code to set LS_COLORS") - .optflag("p", "print-database", "print the byte counts") - .parse(args); + let usage = get_usage(); - if (matches.opt_present("csh") - || matches.opt_present("c-shell") - || matches.opt_present("sh") - || matches.opt_present("bourne-shell")) - && matches.opt_present("print-database") + let matches = uu_app().usage(&usage[..]).get_matches_from(&args); + + let files = matches + .values_of(options::FILE) + .map_or(vec![], |file_values| file_values.collect()); + + // clap provides .conflicts_with / .conflicts_with_all, but we want to + // manually handle conflicts so we can match the output of GNU coreutils + if (matches.is_present(options::C_SHELL) || matches.is_present(options::BOURNE_SHELL)) + && matches.is_present(options::PRINT_DATABASE) { show_usage_error!( "the options to output dircolors' internal database and\nto select a shell \ @@ -80,12 +91,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 { return 1; } - if matches.opt_present("print-database") { - if !matches.free.is_empty() { + if matches.is_present(options::PRINT_DATABASE) { + if !files.is_empty() { show_usage_error!( - "extra operand ‘{}’\nfile operands cannot be combined with \ + "extra operand '{}'\nfile operands cannot be combined with \ --print-database (-p)", - matches.free[0] + files[0] ); return 1; } @@ -94,16 +105,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } let mut out_format = OutputFmt::Unknown; - if matches.opt_present("csh") || matches.opt_present("c-shell") { + if matches.is_present(options::C_SHELL) { out_format = OutputFmt::CShell; - } else if matches.opt_present("sh") || matches.opt_present("bourne-shell") { + } else if matches.is_present(options::BOURNE_SHELL) { out_format = OutputFmt::Shell; } if out_format == OutputFmt::Unknown { match guess_syntax() { OutputFmt::Unknown => { - show_info!("no SHELL environment variable, and no shell type option given"); + show_error!("no SHELL environment variable, and no shell type option given"); return 1; } fmt => out_format = fmt, @@ -111,24 +122,20 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } let result; - if matches.free.is_empty() { + if files.is_empty() { result = parse(INTERNAL_DB.lines(), out_format, "") } else { - if matches.free.len() > 1 { - show_usage_error!("extra operand ‘{}’", matches.free[1]); + if files.len() > 1 { + show_usage_error!("extra operand '{}'", files[1]); return 1; } - match File::open(matches.free[0].as_str()) { + match File::open(files[0]) { Ok(f) => { let fin = BufReader::new(f); - result = parse( - fin.lines().filter_map(Result::ok), - out_format, - matches.free[0].as_str(), - ) + result = parse(fin.lines().filter_map(Result::ok), out_format, files[0]) } Err(e) => { - show_info!("{}: {}", matches.free[0], e); + show_error!("{}: {}", files[0], e); return 1; } } @@ -139,12 +146,43 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } Err(s) => { - show_info!("{}", s); + show_error!("{}", s); 1 } } } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(SUMMARY) + .after_help(LONG_HELP) + .arg( + Arg::with_name(options::BOURNE_SHELL) + .long("sh") + .short("b") + .visible_alias("bourne-shell") + .help("output Bourne shell code to set LS_COLORS") + .display_order(1), + ) + .arg( + Arg::with_name(options::C_SHELL) + .long("csh") + .short("c") + .visible_alias("c-shell") + .help("output C shell code to set LS_COLORS") + .display_order(2), + ) + .arg( + Arg::with_name(options::PRINT_DATABASE) + .long("print-database") + .short("p") + .help("print the byte counts") + .display_order(3), + ) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) +} + pub trait StrUtils { /// Remove comments and trim whitespace fn purify(&self) -> &Self; @@ -156,21 +194,25 @@ pub trait StrUtils { impl StrUtils for str { fn purify(&self) -> &Self { let mut line = self; - for (n, c) in self.chars().enumerate() { - if c != '#' { - continue; - } - - // Ignore if '#' is at the beginning of line - if n == 0 { - line = &self[..0]; - break; - } - + for (n, _) in self + .as_bytes() + .iter() + .enumerate() + .filter(|(_, c)| **c == b'#') + { // Ignore the content after '#' // only if it is preceded by at least one whitespace - if self.chars().nth(n - 1).unwrap().is_whitespace() { - line = &self[..n]; + match self[..n].chars().last() { + Some(c) if c.is_whitespace() => { + line = &self[..n - c.len_utf8()]; + break; + } + None => { + // n == 0 + line = &self[..0]; + break; + } + _ => (), } } line.trim() @@ -202,6 +244,8 @@ enum ParseState { Pass, } use std::collections::HashMap; +use uucore::InvalidEncodingHandling; + fn parse(lines: T, fmt: OutputFmt, fp: &str) -> Result where T: IntoIterator, diff --git a/src/uu/dircolors/src/main.rs b/src/uu/dircolors/src/main.rs index 10c96ecd9..a6a820bd9 100644 --- a/src/uu/dircolors/src/main.rs +++ b/src/uu/dircolors/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_dircolors); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_dircolors); diff --git a/src/uu/dirname/Cargo.toml b/src/uu/dirname/Cargo.toml index 9c565a3e6..0975f33bb 100644 --- a/src/uu/dirname/Cargo.toml +++ b/src/uu/dirname/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_dirname" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "dirname ~ (uutils) display parent directory of PATHNAME" @@ -15,8 +15,9 @@ edition = "2018" path = "src/dirname.rs" [dependencies] +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/dirname/src/dirname.rs b/src/uu/dirname/src/dirname.rs index 59f47ff01..356f2e6b1 100644 --- a/src/uu/dirname/src/dirname.rs +++ b/src/uu/dirname/src/dirname.rs @@ -8,32 +8,55 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::path::Path; +use uucore::InvalidEncodingHandling; -static NAME: &str = "dirname"; -static SYNTAX: &str = "[OPTION] NAME..."; -static SUMMARY: &str = "strip last component from file name"; -static LONG_HELP: &str = " - Output each NAME with its last non-slash component and trailing slashes - removed; if NAME contains no /'s, output '.' (meaning the current - directory). -"; +static ABOUT: &str = "strip last component from file name"; + +mod options { + pub const ZERO: &str = "zero"; + pub const DIR: &str = "dir"; +} + +fn get_usage() -> String { + format!("{0} [OPTION] NAME...", executable!()) +} + +fn get_long_usage() -> String { + String::from( + "Output each NAME with its last non-slash component and trailing slashes + removed; if NAME contains no /'s, output '.' (meaning the current directory).", + ) +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag("z", "zero", "separate output with NUL rather than newline") - .parse(args); + let usage = get_usage(); + let after_help = get_long_usage(); - let separator = if matches.opt_present("zero") { + let matches = uu_app() + .usage(&usage[..]) + .after_help(&after_help[..]) + .get_matches_from(args); + + let separator = if matches.is_present(options::ZERO) { "\0" } else { "\n" }; - if !matches.free.is_empty() { - for path in &matches.free { + let dirnames: Vec = matches + .values_of(options::DIR) + .unwrap_or_default() + .map(str::to_owned) + .collect(); + + if !dirnames.is_empty() { + for path in dirnames.iter() { let p = Path::new(path); match p.parent() { Some(d) => { @@ -54,10 +77,22 @@ pub fn uumain(args: impl uucore::Args) -> i32 { print!("{}", separator); } } else { - println!("{0}: missing operand", NAME); - println!("Try '{0} --help' for more information.", NAME); + show_usage_error!("missing operand"); return 1; } 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .about(ABOUT) + .version(crate_version!()) + .arg( + Arg::with_name(options::ZERO) + .long(options::ZERO) + .short("z") + .help("separate output with NUL rather than newline"), + ) + .arg(Arg::with_name(options::DIR).hidden(true).multiple(true)) +} diff --git a/src/uu/dirname/src/main.rs b/src/uu/dirname/src/main.rs index e4be3372a..bf923e86a 100644 --- a/src/uu/dirname/src/main.rs +++ b/src/uu/dirname/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_dirname); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_dirname); diff --git a/src/uu/du/Cargo.toml b/src/uu/du/Cargo.toml index 912eef17e..dcd1f720e 100644 --- a/src/uu/du/Cargo.toml +++ b/src/uu/du/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_du" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "du ~ (uutils) display disk usage" @@ -15,10 +15,14 @@ edition = "2018" path = "src/du.rs" [dependencies] -time = "0.1.40" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +chrono = "0.4" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +[target.'cfg(target_os = "windows")'.dependencies] +winapi = { version="0.3", features=[] } + [[bin]] name = "du" path = "src/main.rs" diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 4ed80f18b..e5f0e6efc 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -1,35 +1,81 @@ -// This file is part of the uutils coreutils package. -// -// (c) Derek Chiang -// -// For the full copyright and license information, please view the LICENSE -// file that was distributed with this source code. - -// spell-checker:ignore (ToDO) BLOCKSIZE inode inodes ment strs +// * This file is part of the uutils coreutils package. +// * +// * (c) Derek Chiang +// * +// * 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 chrono::prelude::DateTime; +use chrono::Local; +use clap::{crate_version, App, Arg}; use std::collections::HashSet; +use std::convert::TryFrom; use std::env; use std::fs; -use std::io::{stderr, Result, Write}; +#[cfg(not(windows))] +use std::fs::Metadata; +use std::io::{stderr, ErrorKind, Result, Write}; use std::iter; +#[cfg(not(windows))] use std::os::unix::fs::MetadataExt; +#[cfg(windows)] +use std::os::windows::fs::MetadataExt; +#[cfg(windows)] +use std::os::windows::io::AsRawHandle; +#[cfg(windows)] +use std::path::Path; use std::path::PathBuf; -use time::Timespec; +use std::str::FromStr; +use std::time::{Duration, UNIX_EPOCH}; +use uucore::parse_size::{parse_size, ParseSizeError}; +use uucore::InvalidEncodingHandling; +#[cfg(windows)] +use winapi::shared::minwindef::{DWORD, LPVOID}; +#[cfg(windows)] +use winapi::um::fileapi::{FILE_ID_INFO, FILE_STANDARD_INFO}; +#[cfg(windows)] +use winapi::um::minwinbase::{FileIdInfo, FileStandardInfo}; +#[cfg(windows)] +use winapi::um::winbase::GetFileInformationByHandleEx; +#[cfg(windows)] +use winapi::um::winnt::{FILE_ID_128, ULONGLONG}; + +mod options { + pub const NULL: &str = "0"; + pub const ALL: &str = "all"; + pub const APPARENT_SIZE: &str = "apparent-size"; + pub const BLOCK_SIZE: &str = "block-size"; + pub const BYTES: &str = "b"; + pub const TOTAL: &str = "c"; + pub const MAX_DEPTH: &str = "d"; + pub const HUMAN_READABLE: &str = "h"; + pub const BLOCK_SIZE_1K: &str = "k"; + pub const COUNT_LINKS: &str = "l"; + pub const BLOCK_SIZE_1M: &str = "m"; + pub const SEPARATE_DIRS: &str = "S"; + pub const SUMMARIZE: &str = "s"; + pub const THRESHOLD: &str = "threshold"; + pub const SI: &str = "si"; + pub const TIME: &str = "time"; + pub const TIME_STYLE: &str = "time-style"; + pub const ONE_FILE_SYSTEM: &str = "one-file-system"; + pub const DEREFERENCE: &str = "dereference"; + pub const FILE: &str = "FILE"; +} const NAME: &str = "du"; const SUMMARY: &str = "estimate file space usage"; const LONG_HELP: &str = " - Display values are in units of the first available SIZE from - --block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environ‐ - ment variables. Otherwise, units default to 1024 bytes (or 512 if - POSIXLY_CORRECT is set). +Display values are in units of the first available SIZE from --block-size, +and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables. +Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set). - SIZE is an integer and optional unit (example: 10M is 10*1024*1024). - Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (pow‐ - ers of 1000). +SIZE is an integer and optional unit (example: 10M is 10*1024*1024). +Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB,... (powers +of 1000). "; // TODO: Support Z & Y (currently limited by size of u64) @@ -41,6 +87,14 @@ struct Options { max_depth: Option, total: bool, separate_dirs: bool, + one_file_system: bool, + dereference: bool, +} + +#[derive(PartialEq, Eq, Hash, Clone, Copy)] +struct FileInfo { + file_id: u128, + dev_id: u64, } struct Stat { @@ -48,85 +102,157 @@ struct Stat { is_dir: bool, size: u64, blocks: u64, - inode: u64, - created: u64, + inode: Option, + created: Option, accessed: u64, modified: u64, } impl Stat { - fn new(path: PathBuf) -> Result { - let metadata = fs::symlink_metadata(&path)?; - Ok(Stat { + fn new(path: PathBuf, options: &Options) -> Result { + let metadata = if options.dereference { + fs::metadata(&path)? + } else { + fs::symlink_metadata(&path)? + }; + + #[cfg(not(windows))] + let file_info = FileInfo { + file_id: metadata.ino() as u128, + dev_id: metadata.dev(), + }; + #[cfg(not(windows))] + return Ok(Stat { path, is_dir: metadata.is_dir(), size: metadata.len(), blocks: metadata.blocks() as u64, - inode: metadata.ino() as u64, - created: metadata.mtime() as u64, + inode: Some(file_info), + created: birth_u64(&metadata), accessed: metadata.atime() as u64, modified: metadata.mtime() as u64, + }); + + #[cfg(windows)] + let size_on_disk = get_size_on_disk(&path); + #[cfg(windows)] + let file_info = get_file_info(&path); + #[cfg(windows)] + Ok(Stat { + path, + is_dir: metadata.is_dir(), + size: metadata.len(), + blocks: size_on_disk / 1024 * 2, + inode: file_info, + created: windows_creation_time_to_unix_time(metadata.creation_time()), + accessed: windows_time_to_unix_time(metadata.last_access_time()), + modified: windows_time_to_unix_time(metadata.last_write_time()), }) } } -fn unit_string_to_number(s: &str) -> Option { - let mut offset = 0; - let mut s_chars = s.chars().rev(); +#[cfg(windows)] +// https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.last_access_time +// "The returned 64-bit value [...] which represents the number of 100-nanosecond intervals since January 1, 1601 (UTC)." +// "If the underlying filesystem does not support last access time, the returned value is 0." +fn windows_time_to_unix_time(win_time: u64) -> u64 { + (win_time / 10_000_000).saturating_sub(11_644_473_600) +} - let (mut ch, multiple) = match s_chars.next() { - Some('B') | Some('b') => ('B', 1000u64), - Some(ch) => (ch, 1024u64), - None => return None, +#[cfg(windows)] +fn windows_creation_time_to_unix_time(win_time: u64) -> Option { + (win_time / 10_000_000).checked_sub(11_644_473_600) +} + +#[cfg(not(windows))] +fn birth_u64(meta: &Metadata) -> Option { + meta.created() + .ok() + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|e| e.as_secs() as u64) +} + +#[cfg(windows)] +fn get_size_on_disk(path: &Path) -> u64 { + let mut size_on_disk = 0; + + // bind file so it stays in scope until end of function + // if it goes out of scope the handle below becomes invalid + let file = match fs::File::open(path) { + Ok(file) => file, + Err(_) => return size_on_disk, // opening directories will fail }; - if ch == 'B' { - ch = s_chars.next()?; - offset += 1; + + let handle = file.as_raw_handle(); + + unsafe { + let mut file_info: FILE_STANDARD_INFO = core::mem::zeroed(); + let file_info_ptr: *mut FILE_STANDARD_INFO = &mut file_info; + + let success = GetFileInformationByHandleEx( + handle, + FileStandardInfo, + file_info_ptr as LPVOID, + std::mem::size_of::() as DWORD, + ); + + if success != 0 { + size_on_disk = *file_info.AllocationSize.QuadPart() as u64; + } } - ch = ch.to_ascii_uppercase(); - let unit = UNITS - .iter() - .rev() - .find(|&&(unit_ch, _)| unit_ch == ch) - .map(|&(_, val)| { - // we found a match, so increment offset - offset += 1; - val - }) - .or_else(|| if multiple == 1024 { Some(0) } else { None })?; - - let number = s[..s.len() - offset].parse::().ok()?; - - Some(number * multiple.pow(unit)) + size_on_disk } -fn translate_to_pure_number(s: &Option) -> Option { - match *s { - Some(ref s) => unit_string_to_number(s), - None => None, +#[cfg(windows)] +fn get_file_info(path: &Path) -> Option { + let mut result = None; + + let file = match fs::File::open(path) { + Ok(file) => file, + Err(_) => return result, + }; + + let handle = file.as_raw_handle(); + + unsafe { + let mut file_info: FILE_ID_INFO = core::mem::zeroed(); + let file_info_ptr: *mut FILE_ID_INFO = &mut file_info; + + let success = GetFileInformationByHandleEx( + handle, + FileIdInfo, + file_info_ptr as LPVOID, + std::mem::size_of::() as DWORD, + ); + + if success != 0 { + result = Some(FileInfo { + file_id: std::mem::transmute::(file_info.FileId), + dev_id: std::mem::transmute::(file_info.VolumeSerialNumber), + }); + } } + + result } -fn read_block_size(s: Option) -> u64 { - match translate_to_pure_number(&s) { - Some(v) => v, - None => { - if let Some(value) = s { - show_error!("invalid --block-size argument '{}'", value); - }; - - for env_var in &["DU_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] { - if let Some(quantity) = translate_to_pure_number(&env::var(env_var).ok()) { - return quantity; +fn read_block_size(s: Option<&str>) -> usize { + if let Some(s) = s { + parse_size(s) + .unwrap_or_else(|e| crash!(1, "{}", format_error_message(e, s, options::BLOCK_SIZE))) + } else { + for env_var in &["DU_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] { + if let Ok(env_size) = env::var(env_var) { + if let Ok(v) = parse_size(&env_size) { + return v; } } - - if env::var("POSIXLY_CORRECT").is_ok() { - 512 - } else { - 1024 - } + } + if env::var("POSIXLY_CORRECT").is_ok() { + 512 + } else { + 1024 } } } @@ -137,7 +263,7 @@ fn du( mut my_stat: Stat, options: &Options, depth: usize, - inodes: &mut HashSet, + inodes: &mut HashSet, ) -> Box> { let mut stats = vec![]; let mut futures = vec![]; @@ -148,7 +274,7 @@ fn du( Err(e) => { safe_writeln!( stderr(), - "{}: cannot read directory ‘{}‘: {}", + "{}: cannot read directory '{}': {}", options.program_name, my_stat.path.display(), e @@ -159,15 +285,26 @@ fn du( for f in read { match f { - Ok(entry) => match Stat::new(entry.path()) { + Ok(entry) => match Stat::new(entry.path(), options) { Ok(this_stat) => { - if this_stat.is_dir { - futures.push(du(this_stat, options, depth + 1, inodes)); - } else { - if inodes.contains(&this_stat.inode) { + if let Some(inode) = this_stat.inode { + if inodes.contains(&inode) { continue; } - inodes.insert(this_stat.inode); + inodes.insert(inode); + } + if this_stat.is_dir { + if options.one_file_system { + if let (Some(this_inode), Some(my_inode)) = + (this_stat.inode, my_stat.inode) + { + if this_inode.dev_id != my_inode.dev_id { + continue; + } + } + } + futures.push(du(this_stat, options, depth + 1, inodes)); + } else { my_stat.size += this_stat.size; my_stat.blocks += this_stat.blocks; if options.all { @@ -175,19 +312,28 @@ fn du( } } } - Err(error) => show_error!("{}", error), + Err(error) => match error.kind() { + ErrorKind::PermissionDenied => { + let description = format!("cannot access '{}'", entry.path().display()); + let error_message = "Permission denied"; + show_error_custom_description!(description, "{}", error_message) + } + _ => show_error!("cannot access '{}': {}", entry.path().display(), error), + }, }, Err(error) => show_error!("{}", error), } } } - stats.extend(futures.into_iter().flatten().rev().filter(|stat| { + stats.extend(futures.into_iter().flatten().filter(|stat| { if !options.separate_dirs && stat.path.parent().unwrap() == my_stat.path { my_stat.size += stat.size; my_stat.blocks += stat.blocks; } - options.max_depth == None || depth < options.max_depth.unwrap() + options + .max_depth + .map_or(true, |max_depth| depth < max_depth) })); stats.push(my_stat); Box::new(stats.into_iter()) @@ -200,6 +346,9 @@ fn convert_size_human(size: u64, multiplier: u64, _block_size: u64) -> String { return format!("{:.1}{}", (size as f64) / (limit as f64), unit); } } + if size == 0 { + return "0".to_string(); + } format!("{}B", size) } @@ -222,166 +371,75 @@ fn convert_size_other(size: u64, _multiplier: u64, block_size: u64) -> String { format!("{}", ((size as f64) / (block_size as f64)).ceil()) } +fn get_usage() -> String { + format!( + "{0} [OPTION]... [FILE]... + {0} [OPTION]... --files0-from=F", + executable!() + ) +} + #[allow(clippy::cognitive_complexity)] pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let syntax = format!( - "[OPTION]... [FILE]... - {0} [OPTION]... --files0-from=F", - NAME - ); - let matches = app!(&syntax, SUMMARY, LONG_HELP) - // In task - .optflag( - "a", - "all", - " write counts for all files, not just directories", - ) - // In main - .optflag( - "", - "apparent-size", - "print apparent sizes, rather than disk usage - although the apparent size is usually smaller, it may be larger due to holes - in ('sparse') files, internal fragmentation, indirect blocks, and the like", - ) - // In main - .optopt( - "B", - "block-size", - "scale sizes by SIZE before printing them. - E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", - "SIZE", - ) - // In main - .optflag( - "b", - "bytes", - "equivalent to '--apparent-size --block-size=1'", - ) - // In main - .optflag("c", "total", "produce a grand total") - // In task - // opts.optflag("D", "dereference-args", "dereference only symlinks that are listed - // on the command line"), - // In main - // opts.optopt("", "files0-from", "summarize disk usage of the NUL-terminated file - // names specified in file F; - // If F is - then read names from standard input", "F"), - // // In task - // opts.optflag("H", "", "equivalent to --dereference-args (-D)"), - // In main - .optflag( - "h", - "human-readable", - "print sizes in human readable format (e.g., 1K 234M 2G)", - ) - // In main - .optflag("", "si", "like -h, but use powers of 1000 not 1024") - // In main - .optflag("k", "", "like --block-size=1K") - // In task - .optflag("l", "count-links", "count sizes many times if hard linked") - // // In main - .optflag("m", "", "like --block-size=1M") - // // In task - // opts.optflag("L", "dereference", "dereference all symbolic links"), - // // In task - // opts.optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"), - // // In main - .optflag( - "0", - "null", - "end each output line with 0 byte rather than newline", - ) - // In main - .optflag( - "S", - "separate-dirs", - "do not include size of subdirectories", - ) - // In main - .optflag("s", "summarize", "display only a total for each argument") - // // In task - // opts.optflag("x", "one-file-system", "skip directories on different file systems"), - // // In task - // opts.optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"), - // // In task - // opts.optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"), - // In main - .optopt( - "d", - "max-depth", - "print the total for a directory (or file, with --all) - only if it is N or fewer levels below the command - line argument; --max-depth=0 is the same as --summarize", - "N", - ) - // In main - .optflagopt( - "", - "time", - "show time of the last modification of any file in the - directory, or any of its subdirectories. If WORD is given, show time as WORD instead - of modification time: atime, access, use, ctime or status", - "WORD", - ) - // In main - .optopt( - "", - "time-style", - "show times using style STYLE: - full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", - "STYLE", - ) - .parse(args); + let usage = get_usage(); - let summarize = matches.opt_present("summarize"); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - let max_depth_str = matches.opt_str("max-depth"); + let summarize = matches.is_present(options::SUMMARIZE); + + let max_depth_str = matches.value_of(options::MAX_DEPTH); let max_depth = max_depth_str.as_ref().and_then(|s| s.parse::().ok()); match (max_depth_str, max_depth) { - (Some(ref s), _) if summarize => { - show_error!("summarizing conflicts with --max-depth={}", *s); + (Some(s), _) if summarize => { + show_error!("summarizing conflicts with --max-depth={}", s); return 1; } - (Some(ref s), None) => { - show_error!("invalid maximum depth '{}'", *s); + (Some(s), None) => { + show_error!("invalid maximum depth '{}'", s); return 1; } (Some(_), Some(_)) | (None, _) => { /* valid */ } } let options = Options { - all: matches.opt_present("all"), + all: matches.is_present(options::ALL), program_name: NAME.to_owned(), max_depth, - total: matches.opt_present("total"), - separate_dirs: matches.opt_present("S"), + total: matches.is_present(options::TOTAL), + separate_dirs: matches.is_present(options::SEPARATE_DIRS), + one_file_system: matches.is_present(options::ONE_FILE_SYSTEM), + dereference: matches.is_present(options::DEREFERENCE), }; - let strs = if matches.free.is_empty() { - vec!["./".to_owned()] - } else { - matches.free.clone() + let files = match matches.value_of(options::FILE) { + Some(_) => matches.values_of(options::FILE).unwrap().collect(), + None => vec!["."], }; - let block_size = read_block_size(matches.opt_str("block-size")); + let block_size = u64::try_from(read_block_size(matches.value_of(options::BLOCK_SIZE))).unwrap(); - let multiplier: u64 = if matches.opt_present("si") { + let threshold = matches.value_of(options::THRESHOLD).map(|s| { + Threshold::from_str(s) + .unwrap_or_else(|e| crash!(1, "{}", format_error_message(e, s, options::THRESHOLD))) + }); + + let multiplier: u64 = if matches.is_present(options::SI) { 1000 } else { 1024 }; let convert_size_fn = { - if matches.opt_present("human-readable") || matches.opt_present("si") { + if matches.is_present(options::HUMAN_READABLE) || matches.is_present(options::SI) { convert_size_human - } else if matches.opt_present("b") { + } else if matches.is_present(options::BYTES) { convert_size_b - } else if matches.opt_present("k") { + } else if matches.is_present(options::BLOCK_SIZE_1K) { convert_size_k - } else if matches.opt_present("m") { + } else if matches.is_present(options::BLOCK_SIZE_1M) { convert_size_m } else { convert_size_other @@ -389,8 +447,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 { }; let convert_size = |size| convert_size_fn(size, multiplier, block_size); - let time_format_str = match matches.opt_str("time-style") { - Some(s) => match &s[..] { + let time_format_str = match matches.value_of("time-style") { + Some(s) => match s { "full-iso" => "%Y-%m-%d %H:%M:%S.%f %z", "long-iso" => "%Y-%m-%d %H:%M", "iso" => "%Y-%m-%d", @@ -411,53 +469,68 @@ Try '{} --help' for more information.", None => "%Y-%m-%d %H:%M", }; - let line_separator = if matches.opt_present("0") { "\0" } else { "\n" }; + let line_separator = if matches.is_present(options::NULL) { + "\0" + } else { + "\n" + }; let mut grand_total = 0; - for path_str in strs { - let path = PathBuf::from(&path_str); - match Stat::new(path) { + for path_string in files { + let path = PathBuf::from(&path_string); + match Stat::new(path, &options) { Ok(stat) => { - let mut inodes: HashSet = HashSet::new(); - + let mut inodes: HashSet = HashSet::new(); + if let Some(inode) = stat.inode { + inodes.insert(inode); + } let iter = du(stat, &options, 0, &mut inodes); let (_, len) = iter.size_hint(); let len = len.unwrap(); for (index, stat) in iter.enumerate() { - let size = if matches.opt_present("apparent-size") || matches.opt_present("b") { + let size = if matches.is_present(options::APPARENT_SIZE) + || matches.is_present(options::BYTES) + { stat.size } else { // C's stat is such that each block is assume to be 512 bytes // See: http://linux.die.net/man/2/stat stat.blocks * 512 }; - if matches.opt_present("time") { + + if threshold.map_or(false, |threshold| threshold.should_exclude(size)) { + continue; + } + + if matches.is_present(options::TIME) { let tm = { - let (secs, nsecs) = { - let time = match matches.opt_str("time") { - Some(s) => match &s[..] { - "accessed" => stat.accessed, - "created" => stat.created, - "modified" => stat.modified, - _ => { - show_error!( - "invalid argument 'modified' for '--time' - Valid arguments are: - - 'accessed', 'created', 'modified' - Try '{} --help' for more information.", - NAME - ); - return 1; + let secs = { + match matches.value_of(options::TIME) { + Some(s) => match s { + "ctime" | "status" => stat.modified, + "access" | "atime" | "use" => stat.accessed, + "birth" | "creation" => { + if let Some(time) = stat.created { + time + } else { + show_error!( + "Invalid argument '{}' for --time. +'birth' and 'creation' arguments are not supported on this platform.", + s + ); + return 1; + } } + // below should never happen as clap already restricts the values. + _ => unreachable!("Invalid field for --time"), }, None => stat.modified, - }; - ((time / 1000) as i64, (time % 1000 * 1_000_000) as i32) + } }; - time::at(Timespec::new(secs, nsecs)) + DateTime::::from(UNIX_EPOCH + Duration::from_secs(secs)) }; if !summarize || index == len - 1 { - let time_str = tm.strftime(time_format_str).unwrap(); + let time_str = tm.format(time_format_str).to_string(); print!( "{}\t{}\t{}{}", convert_size(size), @@ -476,13 +549,13 @@ Try '{} --help' for more information.", } if options.total && index == (len - 1) { // The last element will be the total size of the the path under - // path_str. We add it to the grand total. + // path_string. We add it to the grand total. grand_total += size; } } } Err(_) => { - show_error!("{}: {}", path_str, "No such file or directory"); + show_error!("{}: {}", path_string, "No such file or directory"); } } } @@ -495,34 +568,238 @@ Try '{} --help' for more information.", 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(SUMMARY) + .after_help(LONG_HELP) + .arg( + Arg::with_name(options::ALL) + .short("a") + .long(options::ALL) + .help("write counts for all files, not just directories"), + ) + .arg( + Arg::with_name(options::APPARENT_SIZE) + .long(options::APPARENT_SIZE) + .help( + "print apparent sizes, rather than disk usage \ + although the apparent size is usually smaller, it may be larger due to holes \ + in ('sparse') files, internal fragmentation, indirect blocks, and the like" + ) + .alias("app") // The GNU test suite uses this alias + ) + .arg( + Arg::with_name(options::BLOCK_SIZE) + .short("B") + .long(options::BLOCK_SIZE) + .value_name("SIZE") + .help( + "scale sizes by SIZE before printing them. \ + E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below." + ) + ) + .arg( + Arg::with_name(options::BYTES) + .short("b") + .long("bytes") + .help("equivalent to '--apparent-size --block-size=1'") + ) + .arg( + Arg::with_name(options::TOTAL) + .long("total") + .short("c") + .help("produce a grand total") + ) + .arg( + Arg::with_name(options::MAX_DEPTH) + .short("d") + .long("max-depth") + .value_name("N") + .help( + "print the total for a directory (or file, with --all) \ + only if it is N or fewer levels below the command \ + line argument; --max-depth=0 is the same as --summarize" + ) + ) + .arg( + Arg::with_name(options::HUMAN_READABLE) + .long("human-readable") + .short("h") + .help("print sizes in human readable format (e.g., 1K 234M 2G)") + ) + .arg( + Arg::with_name("inodes") + .long("inodes") + .help( + "list inode usage information instead of block usage like --block-size=1K" + ) + ) + .arg( + Arg::with_name(options::BLOCK_SIZE_1K) + .short("k") + .help("like --block-size=1K") + ) + .arg( + Arg::with_name(options::COUNT_LINKS) + .short("l") + .long("count-links") + .help("count sizes many times if hard linked") + ) + .arg( + Arg::with_name(options::DEREFERENCE) + .short("L") + .long(options::DEREFERENCE) + .help("dereference all symbolic links") + ) + // .arg( + // Arg::with_name("no-dereference") + // .short("P") + // .long("no-dereference") + // .help("don't follow any symbolic links (this is the default)") + // ) + .arg( + Arg::with_name(options::BLOCK_SIZE_1M) + .short("m") + .help("like --block-size=1M") + ) + .arg( + Arg::with_name(options::NULL) + .short("0") + .long("null") + .help("end each output line with 0 byte rather than newline") + ) + .arg( + Arg::with_name(options::SEPARATE_DIRS) + .short("S") + .long("separate-dirs") + .help("do not include size of subdirectories") + ) + .arg( + Arg::with_name(options::SUMMARIZE) + .short("s") + .long("summarize") + .help("display only a total for each argument") + ) + .arg( + Arg::with_name(options::SI) + .long(options::SI) + .help("like -h, but use powers of 1000 not 1024") + ) + .arg( + Arg::with_name(options::ONE_FILE_SYSTEM) + .short("x") + .long(options::ONE_FILE_SYSTEM) + .help("skip directories on different file systems") + ) + .arg( + Arg::with_name(options::THRESHOLD) + .short("t") + .long(options::THRESHOLD) + .alias("th") + .value_name("SIZE") + .number_of_values(1) + .allow_hyphen_values(true) + .help("exclude entries smaller than SIZE if positive, \ + or entries greater than SIZE if negative") + ) + // .arg( + // Arg::with_name("") + // .short("x") + // .long("exclude-from") + // .value_name("FILE") + // .help("exclude files that match any pattern in FILE") + // ) + // .arg( + // Arg::with_name("exclude") + // .long("exclude") + // .value_name("PATTERN") + // .help("exclude files that match PATTERN") + // ) + .arg( + Arg::with_name(options::TIME) + .long(options::TIME) + .value_name("WORD") + .require_equals(true) + .min_values(0) + .possible_values(&["atime", "access", "use", "ctime", "status", "birth", "creation"]) + .help( + "show time of the last modification of any file in the \ + directory, or any of its subdirectories. If WORD is given, show time as WORD instead \ + of modification time: atime, access, use, ctime, status, birth or creation" + ) + ) + .arg( + Arg::with_name(options::TIME_STYLE) + .long(options::TIME_STYLE) + .value_name("STYLE") + .help( + "show times using style STYLE: \ + full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'" + ) + ) + .arg( + Arg::with_name(options::FILE) + .hidden(true) + .multiple(true) + ) +} + +#[derive(Clone, Copy)] +enum Threshold { + Lower(u64), + Upper(u64), +} + +impl FromStr for Threshold { + type Err = ParseSizeError; + + fn from_str(s: &str) -> std::result::Result { + let offset = if s.starts_with(&['-', '+'][..]) { 1 } else { 0 }; + + let size = u64::try_from(parse_size(&s[offset..])?).unwrap(); + + if s.starts_with('-') { + Ok(Threshold::Upper(size)) + } else { + Ok(Threshold::Lower(size)) + } + } +} + +impl Threshold { + fn should_exclude(&self, size: u64) -> bool { + match *self { + Threshold::Upper(threshold) => size > threshold, + Threshold::Lower(threshold) => size < threshold, + } + } +} + +fn format_error_message(error: ParseSizeError, s: &str, option: &str) -> String { + // NOTE: + // GNU's du echos affected flag, -B or --block-size (-t or --threshold), depending user's selection + // GNU's du does distinguish between "invalid (suffix in) argument" + match error { + ParseSizeError::ParseFailure(_) => format!("invalid --{} argument '{}'", option, s), + ParseSizeError::SizeTooBig(_) => format!("--{} argument '{}' too large", option, s), + } +} + #[cfg(test)] mod test_du { #[allow(unused_imports)] use super::*; - #[test] - fn test_translate_to_pure_number() { - let test_data = [ - (Some("10".to_string()), Some(10)), - (Some("10K".to_string()), Some(10 * 1024)), - (Some("5M".to_string()), Some(5 * 1024 * 1024)), - (Some("900KB".to_string()), Some(900 * 1000)), - (Some("BAD_STRING".to_string()), None), - ]; - for it in test_data.into_iter() { - assert_eq!(translate_to_pure_number(&it.0), it.1); - } - } - #[test] fn test_read_block_size() { let test_data = [ - (Some("10".to_string()), 10), + (Some("1024".to_string()), 1024), + (Some("K".to_string()), 1024), (None, 1024), - (Some("BAD_STRING".to_string()), 1024), ]; - for it in test_data.into_iter() { - assert_eq!(read_block_size(it.0.clone()), it.1); + for it in test_data.iter() { + assert_eq!(read_block_size(it.0.as_deref()), it.1); } } } diff --git a/src/uu/du/src/main.rs b/src/uu/du/src/main.rs index de1967bc8..de9bfc1a5 100644 --- a/src/uu/du/src/main.rs +++ b/src/uu/du/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_du); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_du); diff --git a/src/uu/echo/Cargo.toml b/src/uu/echo/Cargo.toml index a8742b68f..15f189030 100644 --- a/src/uu/echo/Cargo.toml +++ b/src/uu/echo/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_echo" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "echo ~ (uutils) display TEXT" @@ -15,7 +15,8 @@ edition = "2018" path = "src/echo.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 93c395391..8c976c2b4 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -9,14 +9,18 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::io::{self, Write}; use std::iter::Peekable; use std::str::Chars; +use uucore::InvalidEncodingHandling; -const SYNTAX: &str = "[OPTIONS]... [STRING]..."; +const NAME: &str = "echo"; const SUMMARY: &str = "display a line of text"; -const HELP: &str = r#" +const USAGE: &str = "[OPTIONS]... [STRING]..."; +const AFTER_HELP: &str = r#" Echo the STRING(s) to standard output. + If -e is in effect, the following sequences are recognized: \\\\ backslash @@ -33,6 +37,13 @@ const HELP: &str = r#" \\xHH byte with hexadecimal value HH (1 to 2 digits) "#; +mod options { + pub const STRING: &str = "STRING"; + pub const NO_NEWLINE: &str = "no_newline"; + pub const ENABLE_BACKSLASH_ESCAPE: &str = "enable_backslash_escape"; + pub const DISABLE_BACKSLASH_ESCAPE: &str = "disable_backslash_escape"; +} + fn parse_code( input: &mut Peekable, base: u32, @@ -103,22 +114,19 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); + let matches = uu_app().get_matches_from(args); - let matches = app!(SYNTAX, SUMMARY, HELP) - .optflag("n", "", "do not output the trailing newline") - .optflag("e", "", "enable interpretation of backslash escapes") - .optflag( - "E", - "", - "disable interpretation of backslash escapes (default)", - ) - .parse(args); + let no_newline = matches.is_present(options::NO_NEWLINE); + let escaped = matches.is_present(options::ENABLE_BACKSLASH_ESCAPE); + let values: Vec = match matches.values_of(options::STRING) { + Some(s) => s.map(|s| s.to_string()).collect(), + None => vec!["".to_string()], + }; - let no_newline = matches.opt_present("n"); - let escaped = matches.opt_present("e"); - - match execute(no_newline, escaped, matches.free) { + match execute(no_newline, escaped, values) { Ok(_) => 0, Err(f) => { show_error!("{}", f); @@ -127,6 +135,46 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + // TrailingVarArg specifies the final positional argument is a VarArg + // and it doesn't attempts the parse any further args. + // Final argument must have multiple(true) or the usage string equivalent. + .setting(clap::AppSettings::TrailingVarArg) + .setting(clap::AppSettings::AllowLeadingHyphen) + .version(crate_version!()) + .about(SUMMARY) + .after_help(AFTER_HELP) + .usage(USAGE) + .arg( + Arg::with_name(options::NO_NEWLINE) + .short("n") + .help("do not output the trailing newline") + .takes_value(false) + .display_order(1), + ) + .arg( + Arg::with_name(options::ENABLE_BACKSLASH_ESCAPE) + .short("e") + .help("enable interpretation of backslash escapes") + .takes_value(false) + .display_order(2), + ) + .arg( + Arg::with_name(options::DISABLE_BACKSLASH_ESCAPE) + .short("E") + .help("disable interpretation of backslash escapes (default)") + .takes_value(false) + .display_order(3), + ) + .arg( + Arg::with_name(options::STRING) + .multiple(true) + .allow_hyphen_values(true), + ) +} + fn execute(no_newline: bool, escaped: bool, free: Vec) -> io::Result<()> { let stdout = io::stdout(); let mut output = stdout.lock(); @@ -136,7 +184,7 @@ fn execute(no_newline: bool, escaped: bool, free: Vec) -> io::Result<()> write!(output, " ")?; } if escaped { - let should_stop = print_escaped(&input, &mut output)?; + let should_stop = print_escaped(input, &mut output)?; if should_stop { break; } diff --git a/src/uu/echo/src/main.rs b/src/uu/echo/src/main.rs index 00b84e983..b8d3b4b32 100644 --- a/src/uu/echo/src/main.rs +++ b/src/uu/echo/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_echo); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_echo); diff --git a/src/uu/env/Cargo.toml b/src/uu/env/Cargo.toml index 864ecd1b2..ef0017e02 100644 --- a/src/uu/env/Cargo.toml +++ b/src/uu/env/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_env" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "env ~ (uutils) set each NAME to VALUE in the environment and run COMMAND" @@ -18,7 +18,7 @@ path = "src/env.rs" clap = "2.33" libc = "0.2.42" rust-ini = "0.13.0" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 83927b160..51ff92801 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -7,7 +7,7 @@ /* last synced with: env (GNU coreutils) 8.13 */ -// spell-checker:ignore (ToDO) execvp progname subcommand subcommands unsets +// spell-checker:ignore (ToDO) chdir execvp progname subcommand subcommands unsets #[macro_use] extern crate clap; @@ -82,13 +82,10 @@ fn load_config_file(opts: &mut Options) -> Result<(), i32> { Ini::load_from_file(file) }; - let conf = match conf { - Ok(config) => config, - Err(error) => { - eprintln!("env: error: \"{}\": {}", file, error); - return Err(1); - } - }; + let conf = conf.map_err(|error| { + eprintln!("env: error: \"{}\": {}", file, error); + 1 + })?; for (_, prop) in &conf { // ignore all INI section lines (treat them as comments) @@ -117,7 +114,7 @@ fn build_command<'a, 'b>(args: &'a mut Vec<&'b str>) -> (Cow<'b, str>, &'a [&'b (progname, &args[..]) } -fn create_app() -> App<'static, 'static> { +pub fn uu_app() -> App<'static, 'static> { App::new(crate_name!()) .version(crate_version!()) .author(crate_authors!()) @@ -161,7 +158,7 @@ fn create_app() -> App<'static, 'static> { } fn run_env(args: impl uucore::Args) -> Result<(), i32> { - let app = create_app(); + let app = uu_app(); let matches = app.get_matches_from(args); let ignore_env = matches.is_present("ignore-environment"); @@ -245,7 +242,7 @@ fn run_env(args: impl uucore::Args) -> Result<(), i32> { } // set specified env vars - for &(ref name, ref val) in &opts.sets { + for &(name, val) in &opts.sets { // FIXME: set_var() panics if name is an empty string env::set_var(name, val); } @@ -256,13 +253,10 @@ fn run_env(args: impl uucore::Args) -> Result<(), i32> { // FIXME: this should just use execvp() (no fork()) on Unix-like systems match Command::new(&*prog).args(args).status() { - Ok(exit) => { - if !exit.success() { - return Err(exit.code().unwrap()); - } - } + Ok(exit) if !exit.success() => return Err(exit.code().unwrap()), Err(ref err) if err.kind() == io::ErrorKind::NotFound => return Err(127), Err(_) => return Err(126), + Ok(_) => (), } } else { // no program provided, so just dump all env vars to stdout diff --git a/src/uu/env/src/main.rs b/src/uu/env/src/main.rs index 8b654eb00..9c19a3ab4 100644 --- a/src/uu/env/src/main.rs +++ b/src/uu/env/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_env); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_env); diff --git a/src/uu/expand/Cargo.toml b/src/uu/expand/Cargo.toml index d9861a0c7..4931cf53c 100644 --- a/src/uu/expand/Cargo.toml +++ b/src/uu/expand/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_expand" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "expand ~ (uutils) convert input tabs to spaces" @@ -15,9 +15,9 @@ edition = "2018" path = "src/expand.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" unicode-width = "0.1.5" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index 353c3e6bc..66c3eb259 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -12,19 +12,30 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg, ArgMatches}; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; -use std::iter::repeat; use std::str::from_utf8; use unicode_width::UnicodeWidthChar; -static SYNTAX: &str = "[OPTION]... [FILE]..."; -static SUMMARY: &str = "Convert tabs in each FILE to spaces, writing to standard output. +static ABOUT: &str = "Convert tabs in each FILE to spaces, writing to standard output. With no FILE, or when FILE is -, read standard input."; + +pub mod options { + pub static TABS: &str = "tabs"; + pub static INITIAL: &str = "initial"; + pub static NO_UTF8: &str = "no-utf8"; + pub static FILES: &str = "FILES"; +} + static LONG_HELP: &str = ""; static DEFAULT_TABSTOP: usize = 8; +fn get_usage() -> String { + format!("{0} [OPTION]... [FILE]...", executable!()) +} + fn tabstops_parse(s: String) -> Vec { let words = s.split(','); @@ -58,14 +69,14 @@ struct Options { } impl Options { - fn new(matches: getopts::Matches) -> Options { - let tabstops = match matches.opt_str("t") { + fn new(matches: &ArgMatches) -> Options { + let tabstops = match matches.value_of(options::TABS) { + Some(s) => tabstops_parse(s.to_string()), None => vec![DEFAULT_TABSTOP], - Some(s) => tabstops_parse(s), }; - let iflag = matches.opt_present("i"); - let uflag = !matches.opt_present("U"); + let iflag = matches.is_present(options::INITIAL); + let uflag = !matches.is_present(options::NO_UTF8); // avoid allocations when dumping out long sequences of spaces // by precomputing the longest string of spaces we will ever need @@ -78,12 +89,11 @@ impl Options { }) .max() .unwrap(); // length of tabstops is guaranteed >= 1 - let tspaces = repeat(' ').take(nspaces).collect(); + let tspaces = " ".repeat(nspaces); - let files = if matches.free.is_empty() { - vec!["-".to_owned()] - } else { - matches.free + let files: Vec = match matches.values_of(options::FILES) { + Some(s) => s.map(|v| v.to_string()).collect(), + None => vec!["-".to_owned()], }; Options { @@ -97,34 +107,45 @@ impl Options { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag("i", "initial", "do not convert tabs after non blanks") - .optopt( - "t", - "tabs", - "have tabs NUMBER characters apart, not 8", - "NUMBER", - ) - .optopt( - "t", - "tabs", - "use comma separated list of explicit tab positions", - "LIST", - ) - .optflag( - "U", - "no-utf8", - "interpret input file as 8-bit ASCII rather than UTF-8", - ) - .parse(args); - - expand(Options::new(matches)); + let usage = get_usage(); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + expand(Options::new(&matches)); 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .after_help(LONG_HELP) + .arg( + Arg::with_name(options::INITIAL) + .long(options::INITIAL) + .short("i") + .help("do not convert tabs after non blanks"), + ) + .arg( + Arg::with_name(options::TABS) + .long(options::TABS) + .short("t") + .value_name("N, LIST") + .takes_value(true) + .help("have tabs N characters apart, not 8 or use comma separated list of explicit tab positions"), + ) + .arg( + Arg::with_name(options::NO_UTF8) + .long(options::NO_UTF8) + .short("U") + .help("interpret input file as 8-bit ASCII rather than UTF-8"), + ).arg( + Arg::with_name(options::FILES) + .multiple(true) + .hidden(true) + .takes_value(true) + ) +} + fn open(path: String) -> BufReader> { let file_buf; if path == "-" { @@ -216,7 +237,7 @@ fn expand(options: Options) { // now dump out either spaces if we're expanding, or a literal tab if we're not if init || !options.iflag { - safe_unwrap!(output.write_all(&options.tspaces[..nts].as_bytes())); + safe_unwrap!(output.write_all(options.tspaces[..nts].as_bytes())); } else { safe_unwrap!(output.write_all(&buf[byte..byte + nbytes])); } diff --git a/src/uu/expand/src/main.rs b/src/uu/expand/src/main.rs index d80b6090f..a154b36be 100644 --- a/src/uu/expand/src/main.rs +++ b/src/uu/expand/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_expand); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_expand); diff --git a/src/uu/expr/Cargo.toml b/src/uu/expr/Cargo.toml index 1246ff873..0906856d1 100644 --- a/src/uu/expr/Cargo.toml +++ b/src/uu/expr/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_expr" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "expr ~ (uutils) display the value of EXPRESSION" @@ -15,9 +15,12 @@ edition = "2018" path = "src/expr.rs" [dependencies] +clap = "2.33.3" libc = "0.2.42" +num-bigint = "0.4.0" +num-traits = "0.2.14" onig = "~4.3.2" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/expr/src/expr.rs b/src/uu/expr/src/expr.rs index fee85dfe1..92c15565d 100644 --- a/src/uu/expr/src/expr.rs +++ b/src/uu/expr/src/expr.rs @@ -8,14 +8,25 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; +use uucore::InvalidEncodingHandling; + mod syntax_tree; mod tokens; -static NAME: &str = "expr"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +const VERSION: &str = "version"; +const HELP: &str = "help"; + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .arg(Arg::with_name(VERSION).long(VERSION)) + .arg(Arg::with_name(HELP).long(HELP)) +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); // For expr utility we do not want getopts. // The following usage should work without escaping hyphens: `expr -15 = 1 + 2 \* \( 3 - -4 \)` @@ -33,7 +44,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } fn process_expr(token_strings: &[String]) -> Result { - let maybe_tokens = tokens::strings_to_tokens(&token_strings); + let maybe_tokens = tokens::strings_to_tokens(token_strings); let maybe_ast = syntax_tree::tokens_to_ast(maybe_tokens); evaluate_ast(maybe_ast) } @@ -51,12 +62,8 @@ fn print_expr_error(expr_error: &str) -> ! { crash!(2, "{}", expr_error) } -fn evaluate_ast(maybe_ast: Result, String>) -> Result { - if maybe_ast.is_err() { - Err(maybe_ast.err().unwrap()) - } else { - maybe_ast.ok().unwrap().evaluate() - } +fn evaluate_ast(maybe_ast: Result, String>) -> Result { + maybe_ast.and_then(|ast| ast.evaluate()) } fn maybe_handle_help_or_version(args: &[String]) -> bool { @@ -133,5 +140,5 @@ Environment variables: } fn print_version() { - println!("{} {}", NAME, VERSION); + println!("{} {}", executable!(), crate_version!()); } diff --git a/src/uu/expr/src/main.rs b/src/uu/expr/src/main.rs index 4268865df..6fdd6be14 100644 --- a/src/uu/expr/src/main.rs +++ b/src/uu/expr/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_expr); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_expr); diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index d56bab4fc..ff49ea57e 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -12,15 +12,17 @@ // spell-checker:ignore (ToDO) binop binops ints paren prec +use num_bigint::BigInt; +use num_traits::{One, Zero}; use onig::{Regex, RegexOptions, Syntax}; use crate::tokens::Token; type TokenStack = Vec<(usize, Token)>; -pub type OperandsList = Vec>; +pub type OperandsList = Vec>; #[derive(Debug)] -pub enum ASTNode { +pub enum AstNode { Leaf { token_idx: usize, value: String, @@ -31,7 +33,7 @@ pub enum ASTNode { operands: OperandsList, }, } -impl ASTNode { +impl AstNode { fn debug_dump(&self) { self.debug_dump_impl(1); } @@ -39,20 +41,17 @@ impl ASTNode { for _ in 0..depth { print!("\t",); } - match *self { - ASTNode::Leaf { - ref token_idx, - ref value, - } => println!( + match self { + AstNode::Leaf { token_idx, value } => println!( "Leaf( {} ) at #{} ( evaluate -> {:?} )", value, token_idx, self.evaluate() ), - ASTNode::Node { - ref token_idx, - ref op_type, - ref operands, + AstNode::Node { + token_idx, + op_type, + operands, } => { println!( "Node( {} ) at #{} (evaluate -> {:?})", @@ -67,50 +66,47 @@ impl ASTNode { } } - fn new_node(token_idx: usize, op_type: &str, operands: OperandsList) -> Box { - Box::new(ASTNode::Node { + fn new_node(token_idx: usize, op_type: &str, operands: OperandsList) -> Box { + Box::new(AstNode::Node { token_idx, op_type: op_type.into(), operands, }) } - fn new_leaf(token_idx: usize, value: &str) -> Box { - Box::new(ASTNode::Leaf { + fn new_leaf(token_idx: usize, value: &str) -> Box { + Box::new(AstNode::Leaf { token_idx, value: value.into(), }) } pub fn evaluate(&self) -> Result { - match *self { - ASTNode::Leaf { ref value, .. } => Ok(value.clone()), - ASTNode::Node { ref op_type, .. } => match self.operand_values() { + match self { + AstNode::Leaf { value, .. } => Ok(value.clone()), + AstNode::Node { op_type, .. } => match self.operand_values() { Err(reason) => Err(reason), Ok(operand_values) => match op_type.as_ref() { - "+" => infix_operator_two_ints( - |a: i64, b: i64| checked_binop(|| a.checked_add(b), "+"), - &operand_values, - ), - "-" => infix_operator_two_ints( - |a: i64, b: i64| checked_binop(|| a.checked_sub(b), "-"), - &operand_values, - ), - "*" => infix_operator_two_ints( - |a: i64, b: i64| checked_binop(|| a.checked_mul(b), "*"), - &operand_values, - ), + "+" => { + infix_operator_two_ints(|a: BigInt, b: BigInt| Ok(a + b), &operand_values) + } + "-" => { + infix_operator_two_ints(|a: BigInt, b: BigInt| Ok(a - b), &operand_values) + } + "*" => { + infix_operator_two_ints(|a: BigInt, b: BigInt| Ok(a * b), &operand_values) + } "/" => infix_operator_two_ints( - |a: i64, b: i64| { - if b == 0 { + |a: BigInt, b: BigInt| { + if b.is_zero() { Err("division by zero".to_owned()) } else { - checked_binop(|| a.checked_div(b), "/") + Ok(a / b) } }, &operand_values, ), "%" => infix_operator_two_ints( - |a: i64, b: i64| { - if b == 0 { + |a: BigInt, b: BigInt| { + if b.is_zero() { Err("division by zero".to_owned()) } else { Ok(a % b) @@ -119,41 +115,41 @@ impl ASTNode { &operand_values, ), "=" => infix_operator_two_ints_or_two_strings( - |a: i64, b: i64| Ok(bool_as_int(a == b)), + |a: BigInt, b: BigInt| Ok(bool_as_int(a == b)), |a: &String, b: &String| Ok(bool_as_string(a == b)), &operand_values, ), "!=" => infix_operator_two_ints_or_two_strings( - |a: i64, b: i64| Ok(bool_as_int(a != b)), + |a: BigInt, b: BigInt| Ok(bool_as_int(a != b)), |a: &String, b: &String| Ok(bool_as_string(a != b)), &operand_values, ), "<" => infix_operator_two_ints_or_two_strings( - |a: i64, b: i64| Ok(bool_as_int(a < b)), + |a: BigInt, b: BigInt| Ok(bool_as_int(a < b)), |a: &String, b: &String| Ok(bool_as_string(a < b)), &operand_values, ), ">" => infix_operator_two_ints_or_two_strings( - |a: i64, b: i64| Ok(bool_as_int(a > b)), + |a: BigInt, b: BigInt| Ok(bool_as_int(a > b)), |a: &String, b: &String| Ok(bool_as_string(a > b)), &operand_values, ), "<=" => infix_operator_two_ints_or_two_strings( - |a: i64, b: i64| Ok(bool_as_int(a <= b)), + |a: BigInt, b: BigInt| Ok(bool_as_int(a <= b)), |a: &String, b: &String| Ok(bool_as_string(a <= b)), &operand_values, ), ">=" => infix_operator_two_ints_or_two_strings( - |a: i64, b: i64| Ok(bool_as_int(a >= b)), + |a: BigInt, b: BigInt| Ok(bool_as_int(a >= b)), |a: &String, b: &String| Ok(bool_as_string(a >= b)), &operand_values, ), - "|" => infix_operator_or(&operand_values), - "&" => infix_operator_and(&operand_values), + "|" => Ok(infix_operator_or(&operand_values)), + "&" => Ok(infix_operator_and(&operand_values)), ":" | "match" => operator_match(&operand_values), - "length" => prefix_operator_length(&operand_values), - "index" => prefix_operator_index(&operand_values), - "substr" => prefix_operator_substr(&operand_values), + "length" => Ok(prefix_operator_length(&operand_values)), + "index" => Ok(prefix_operator_index(&operand_values)), + "substr" => Ok(prefix_operator_substr(&operand_values)), _ => Err(format!("operation not implemented: {}", op_type)), }, @@ -161,13 +157,11 @@ impl ASTNode { } } pub fn operand_values(&self) -> Result, String> { - if let ASTNode::Node { ref operands, .. } = *self { + if let AstNode::Node { operands, .. } = self { let mut out = Vec::with_capacity(operands.len()); for operand in operands { - match operand.evaluate() { - Ok(value) => out.push(value), - Err(reason) => return Err(reason), - } + let value = operand.evaluate()?; + out.push(value); } Ok(out) } else { @@ -178,24 +172,15 @@ impl ASTNode { pub fn tokens_to_ast( maybe_tokens: Result, String>, -) -> Result, String> { - if maybe_tokens.is_err() { - Err(maybe_tokens.err().unwrap()) - } else { - let tokens = maybe_tokens.ok().unwrap(); +) -> Result, String> { + maybe_tokens.and_then(|tokens| { let mut out_stack: TokenStack = Vec::new(); let mut op_stack: TokenStack = Vec::new(); for (token_idx, token) in tokens { - if let Err(reason) = - push_token_to_either_stack(token_idx, &token, &mut out_stack, &mut op_stack) - { - return Err(reason); - } - } - if let Err(reason) = move_rest_of_ops_to_out(&mut out_stack, &mut op_stack) { - return Err(reason); + push_token_to_either_stack(token_idx, &token, &mut out_stack, &mut op_stack)?; } + move_rest_of_ops_to_out(&mut out_stack, &mut op_stack)?; assert!(op_stack.is_empty()); maybe_dump_rpn(&out_stack); @@ -209,17 +194,17 @@ pub fn tokens_to_ast( maybe_dump_ast(&result); result } - } + }) } -fn maybe_dump_ast(result: &Result, String>) { +fn maybe_dump_ast(result: &Result, String>) { use std::env; if let Ok(debug_var) = env::var("EXPR_DEBUG_AST") { if debug_var == "1" { println!("EXPR_DEBUG_AST"); - match *result { - Ok(ref ast) => ast.debug_dump(), - Err(ref reason) => println!("\terr: {:?}", reason), + match result { + Ok(ast) => ast.debug_dump(), + Err(reason) => println!("\terr: {:?}", reason), } } } @@ -238,11 +223,11 @@ fn maybe_dump_rpn(rpn: &TokenStack) { } } -fn ast_from_rpn(rpn: &mut TokenStack) -> Result, String> { +fn ast_from_rpn(rpn: &mut TokenStack) -> Result, String> { match rpn.pop() { None => Err("syntax error (premature end of expression)".to_owned()), - Some((token_idx, Token::Value { value })) => Ok(ASTNode::new_leaf(token_idx, &value)), + Some((token_idx, Token::Value { value })) => Ok(AstNode::new_leaf(token_idx, &value)), Some((token_idx, Token::InfixOp { value, .. })) => { maybe_ast_node(token_idx, &value, 2, rpn) @@ -262,16 +247,14 @@ fn maybe_ast_node( op_type: &str, arity: usize, rpn: &mut TokenStack, -) -> Result, String> { +) -> Result, String> { let mut operands = Vec::with_capacity(arity); for _ in 0..arity { - match ast_from_rpn(rpn) { - Err(reason) => return Err(reason), - Ok(operand) => operands.push(operand), - } + let operand = ast_from_rpn(rpn)?; + operands.push(operand); } operands.reverse(); - Ok(ASTNode::new_node(token_idx, op_type, operands)) + Ok(AstNode::new_node(token_idx, op_type, operands)) } fn move_rest_of_ops_to_out( @@ -304,7 +287,7 @@ fn push_token_to_either_stack( out_stack: &mut TokenStack, op_stack: &mut TokenStack, ) -> Result<(), String> { - let result = match *token { + let result = match token { Token::Value { .. } => { out_stack.push((token_idx, token.clone())); Ok(()) @@ -412,32 +395,24 @@ fn move_till_match_paren( op_stack: &mut TokenStack, ) -> Result<(), String> { loop { - match op_stack.pop() { - None => return Err("syntax error (Mismatched close-parenthesis)".to_string()), - Some((_, Token::ParOpen)) => return Ok(()), - Some(other) => out_stack.push(other), + let op = op_stack + .pop() + .ok_or_else(|| "syntax error (Mismatched close-parenthesis)".to_string())?; + match op { + (_, Token::ParOpen) => return Ok(()), + other => out_stack.push(other), } } } -fn checked_binop Option, T>(cb: F, op: &str) -> Result { - match cb() { - Some(v) => Ok(v), - None => Err(format!("{}: Numerical result out of range", op)), - } -} - fn infix_operator_two_ints(f: F, values: &[String]) -> Result where - F: Fn(i64, i64) -> Result, + F: Fn(BigInt, BigInt) -> Result, { assert!(values.len() == 2); - if let Ok(left) = values[0].parse::() { - if let Ok(right) = values[1].parse::() { - return match f(left, right) { - Ok(result) => Ok(result.to_string()), - Err(reason) => Err(reason), - }; + if let Ok(left) = values[0].parse::() { + if let Ok(right) = values[1].parse::() { + return f(left, right).map(|big_int| big_int.to_string()); } } Err("Expected an integer operand".to_string()) @@ -449,13 +424,14 @@ fn infix_operator_two_ints_or_two_strings( values: &[String], ) -> Result where - FI: Fn(i64, i64) -> Result, + FI: Fn(BigInt, BigInt) -> Result, FS: Fn(&String, &String) -> Result, { assert!(values.len() == 2); - if let (Some(a_int), Some(b_int)) = - (values[0].parse::().ok(), values[1].parse::().ok()) - { + if let (Some(a_int), Some(b_int)) = ( + values[0].parse::().ok(), + values[1].parse::().ok(), + ) { match fi(a_int, b_int) { Ok(result) => Ok(result.to_string()), Err(reason) => Err(reason), @@ -465,49 +441,44 @@ where } } -fn infix_operator_or(values: &[String]) -> Result { +fn infix_operator_or(values: &[String]) -> String { assert!(values.len() == 2); if value_as_bool(&values[0]) { - Ok(values[0].clone()) + values[0].clone() } else { - Ok(values[1].clone()) + values[1].clone() } } -fn infix_operator_and(values: &[String]) -> Result { +fn infix_operator_and(values: &[String]) -> String { if value_as_bool(&values[0]) && value_as_bool(&values[1]) { - Ok(values[0].clone()) + values[0].clone() } else { - Ok(0.to_string()) + 0.to_string() } } fn operator_match(values: &[String]) -> Result { assert!(values.len() == 2); - let re = match Regex::with_options(&values[1], RegexOptions::REGEX_OPTION_NONE, Syntax::grep()) - { - Ok(m) => m, - Err(err) => return Err(err.description().to_string()), - }; - if re.captures_len() > 0 { - Ok(match re.captures(&values[0]) { - Some(captures) => captures.at(1).unwrap().to_string(), - None => "".to_string(), - }) + let re = Regex::with_options(&values[1], RegexOptions::REGEX_OPTION_NONE, Syntax::grep()) + .map_err(|err| err.description().to_string())?; + Ok(if re.captures_len() > 0 { + re.captures(&values[0]) + .map(|captures| captures.at(1).unwrap()) + .unwrap_or("") + .to_string() } else { - Ok(match re.find(&values[0]) { - Some((start, end)) => (end - start).to_string(), - None => "0".to_string(), - }) - } + re.find(&values[0]) + .map_or("0".to_string(), |(start, end)| (end - start).to_string()) + }) } -fn prefix_operator_length(values: &[String]) -> Result { +fn prefix_operator_length(values: &[String]) -> String { assert!(values.len() == 1); - Ok(values[0].len().to_string()) + values[0].len().to_string() } -fn prefix_operator_index(values: &[String]) -> Result { +fn prefix_operator_index(values: &[String]) -> String { assert!(values.len() == 2); let haystack = &values[0]; let needles = &values[1]; @@ -515,45 +486,33 @@ fn prefix_operator_index(values: &[String]) -> Result { for (current_idx, ch_h) in haystack.chars().enumerate() { for ch_n in needles.chars() { if ch_n == ch_h { - return Ok(current_idx.to_string()); + return current_idx.to_string(); } } } - Ok("0".to_string()) + "0".to_string() } -fn prefix_operator_substr(values: &[String]) -> Result { +fn prefix_operator_substr(values: &[String]) -> String { assert!(values.len() == 3); let subj = &values[0]; - let mut idx = match values[1].parse::() { - Ok(i) => i, - Err(_) => return Err("expected integer as POS arg to 'substr'".to_string()), + let idx = match values[1] + .parse::() + .ok() + .and_then(|v| v.checked_sub(1)) + { + Some(i) => i, + None => return String::new(), }; - let mut len = match values[2].parse::() { + let len = match values[2].parse::() { Ok(i) => i, - Err(_) => return Err("expected integer as LENGTH arg to 'substr'".to_string()), + Err(_) => return String::new(), }; - if idx <= 0 || len <= 0 { - return Ok("".to_string()); - } - - let mut out_str = String::new(); - for ch in subj.chars() { - idx -= 1; - if idx <= 0 { - if len <= 0 { - break; - } - len -= 1; - - out_str.push(ch); - } - } - Ok(out_str) + subj.chars().skip(idx).take(len).collect() } -fn bool_as_int(b: bool) -> i64 { +fn bool_as_int(b: bool) -> u8 { if b { 1 } else { @@ -571,8 +530,8 @@ fn value_as_bool(s: &str) -> bool { if s.is_empty() { return false; } - match s.parse::() { - Ok(n) => n != 0, + match s.parse::() { + Ok(n) => n.is_one(), Err(_) => true, } } diff --git a/src/uu/expr/src/tokens.rs b/src/uu/expr/src/tokens.rs index b65b0d482..748960bc3 100644 --- a/src/uu/expr/src/tokens.rs +++ b/src/uu/expr/src/tokens.rs @@ -18,6 +18,8 @@ // spell-checker:ignore (ToDO) paren +use num_bigint::BigInt; + #[derive(Debug, Clone)] pub enum Token { Value { @@ -51,24 +53,19 @@ impl Token { } fn is_infix_plus(&self) -> bool { - match *self { - Token::InfixOp { ref value, .. } => value == "+", + match self { + Token::InfixOp { value, .. } => value == "+", _ => false, } } fn is_a_number(&self) -> bool { - match *self { - Token::Value { ref value, .. } => value.parse::().is_ok(), + match self { + Token::Value { value, .. } => value.parse::().is_ok(), _ => false, } } fn is_a_close_paren(&self) -> bool { - #[allow(clippy::match_like_matches_macro)] - // `matches!(...)` macro not stabilized until rust v1.42 - match *self { - Token::ParClose => true, - _ => false, - } + matches!(*self, Token::ParClose) } } @@ -81,27 +78,27 @@ pub fn strings_to_tokens(strings: &[String]) -> Result, Stri "(" => Token::ParOpen, ")" => Token::ParClose, - "^" => Token::new_infix_op(&s, false, 7), + "^" => Token::new_infix_op(s, false, 7), - ":" => Token::new_infix_op(&s, true, 6), + ":" => Token::new_infix_op(s, true, 6), - "*" => Token::new_infix_op(&s, true, 5), - "/" => Token::new_infix_op(&s, true, 5), - "%" => Token::new_infix_op(&s, true, 5), + "*" => Token::new_infix_op(s, true, 5), + "/" => Token::new_infix_op(s, true, 5), + "%" => Token::new_infix_op(s, true, 5), - "+" => Token::new_infix_op(&s, true, 4), - "-" => Token::new_infix_op(&s, true, 4), + "+" => Token::new_infix_op(s, true, 4), + "-" => Token::new_infix_op(s, true, 4), - "=" => Token::new_infix_op(&s, true, 3), - "!=" => Token::new_infix_op(&s, true, 3), - "<" => Token::new_infix_op(&s, true, 3), - ">" => Token::new_infix_op(&s, true, 3), - "<=" => Token::new_infix_op(&s, true, 3), - ">=" => Token::new_infix_op(&s, true, 3), + "=" => Token::new_infix_op(s, true, 3), + "!=" => Token::new_infix_op(s, true, 3), + "<" => Token::new_infix_op(s, true, 3), + ">" => Token::new_infix_op(s, true, 3), + "<=" => Token::new_infix_op(s, true, 3), + ">=" => Token::new_infix_op(s, true, 3), - "&" => Token::new_infix_op(&s, true, 2), + "&" => Token::new_infix_op(s, true, 2), - "|" => Token::new_infix_op(&s, true, 1), + "|" => Token::new_infix_op(s, true, 1), "match" => Token::PrefixOp { arity: 2, @@ -120,9 +117,9 @@ pub fn strings_to_tokens(strings: &[String]) -> Result, Stri value: s.clone(), }, - _ => Token::new_value(&s), + _ => Token::new_value(s), }; - push_token_if_not_escaped(&mut tokens_acc, tok_idx, token_if_not_escaped, &s); + push_token_if_not_escaped(&mut tokens_acc, tok_idx, token_if_not_escaped, s); tok_idx += 1; } maybe_dump_tokens_acc(&tokens_acc); @@ -147,7 +144,7 @@ fn push_token_if_not_escaped(acc: &mut Vec<(usize, Token)>, tok_idx: usize, toke // Smells heuristics... :( let prev_is_plus = match acc.last() { None => false, - Some(ref t) => t.1.is_infix_plus(), + Some(t) => t.1.is_infix_plus(), }; let should_use_as_escaped = if prev_is_plus && acc.len() >= 2 { let pre_prev = &acc[acc.len() - 2]; diff --git a/src/uu/factor/BENCHMARKING.md b/src/uu/factor/BENCHMARKING.md new file mode 100644 index 000000000..6bf9cbf90 --- /dev/null +++ b/src/uu/factor/BENCHMARKING.md @@ -0,0 +1,112 @@ +# Benchmarking `factor` + + + +The benchmarks for `factor` are located under `tests/benches/factor` +and can be invoked with `cargo bench` in that directory. + +They are located outside the `uu_factor` crate, as they do not comply +with the project's minimum supported Rust version, *i.e.* may require +a newer version of `rustc`. + +## Microbenchmarking deterministic functions + +We currently use [`criterion`] to benchmark deterministic functions, +such as `gcd` and `table::factor`. + +However, microbenchmarks are by nature unstable: not only are they specific to +the hardware, operating system version, etc., but they are noisy and affected +by other tasks on the system (browser, compile jobs, etc.), which can cause +`criterion` to report spurious performance improvements and regressions. + +This can be mitigated by getting as close to [idealized conditions][lemire] +as possible: + +- minimize the amount of computation and I/O running concurrently to the + benchmark, *i.e.* close your browser and IM clients, don't compile at the + same time, etc. ; +- ensure the CPU's [frequency stays constant] during the benchmark ; +- [isolate a **physical** core], set it to `nohz_full`, and pin the benchmark + to it, so it won't be preempted in the middle of a measurement ; +- disable ASLR by running `setarch -R cargo bench`, so we can compare results + across multiple executions. + +[`criterion`]: https://bheisler.github.io/criterion.rs/book/index.html +[lemire]: https://lemire.me/blog/2018/01/16/microbenchmarking-calls-for-idealized-conditions/ +[isolate a **physical** core]: https://pyperf.readthedocs.io/en/latest/system.html#isolate-cpus-on-linux +[frequency stays constant]: ... + +### Guidance for designing microbenchmarks + +*Note:* this guidance is specific to `factor` and takes its application domain +into account; do not expect it to generalize to other projects. It is based +on Daniel Lemire's [*Microbenchmarking calls for idealized conditions*][lemire], +which I recommend reading if you want to add benchmarks to `factor`. + +1. Select a small, self-contained, deterministic component + `gcd` and `table::factor` are good example of such: + - no I/O or access to external data structures ; + - no call into other components ; + - behavior is deterministic: no RNG, no concurrency, ... ; + - the test's body is *fast* (~100ns for `gcd`, ~10µs for `factor::table`), + so each sample takes a very short time, minimizing variability and + maximizing the numbers of samples we can take in a given time. + +2. Benchmarks are immutable (once merged in `uutils`) + Modifying a benchmark means previously-collected values cannot meaningfully + be compared, silently giving nonsensical results. If you must modify an + existing benchmark, rename it. + +3. Test common cases + We are interested in overall performance, rather than specific edge-cases; + use **reproducibly-randomized inputs**, sampling from either all possible + input values or some subset of interest. + +4. Use [`criterion`], `criterion::black_box`, ... + `criterion` isn't perfect, but it is also much better than ad-hoc + solutions in each benchmark. + +## Wishlist + +### Configurable statistical estimators + +`criterion` always uses the arithmetic average as estimator; in microbenchmarks, +where the code under test is fully deterministic and the measurements are +subject to additive, positive noise, [the minimum is more appropriate][lemire]. + +### CI & reproducible performance testing + +Measuring performance on real hardware is important, as it relates directly +to what users of `factor` experience; however, such measurements are subject +to the constraints of the real-world, and aren't perfectly reproducible. +Moreover, the mitigation for it (described above) isn't achievable in +virtualized, multi-tenant environments such as CI. + +Instead, we could run the microbenchmarks in a simulated CPU with [`cachegrind`], +measure execution “time” in that model (in CI), and use it to detect and report +performance improvements and regressions. + +[`iai`] is an implementation of this idea for Rust. + +[`cachegrind`]: https://www.valgrind.org/docs/manual/cg-manual.html +[`iai`]: https://bheisler.github.io/criterion.rs/book/iai/iai.html + +### Comparing randomized implementations across multiple inputs + +`factor` is a challenging target for system benchmarks as it combines two +characteristics: + +1. integer factoring algorithms are randomized, with large variance in + execution time ; + +2. various inputs also have large differences in factoring time, that + corresponds to no natural, linear ordering of the inputs. + +If (1) was untrue (i.e. if execution time wasn't random), we could faithfully +compare 2 implementations (2 successive versions, or `uutils` and GNU) using +a scatter plot, where each axis corresponds to the perf. of one implementation. + +Similarly, without (2) we could plot numbers on the X axis and their factoring +time on the Y axis, using multiple lines for various quantiles. The large +differences in factoring times for successive numbers, mean that such a plot +would be unreadable. diff --git a/src/uu/factor/Cargo.toml b/src/uu/factor/Cargo.toml index d1160c493..eb977760f 100644 --- a/src/uu/factor/Cargo.toml +++ b/src/uu/factor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_factor" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "factor ~ (uutils) display the prime factors of each NUMBER" @@ -14,23 +14,19 @@ edition = "2018" [build-dependencies] num-traits = "0.2.13" # used in src/numerics.rs, which is included by build.rs - [dependencies] +coz = { version = "0.1.3", optional = true } num-traits = "0.2.13" # Needs at least version 0.2.13 for "OverflowingAdd" -rand = { version="0.7", features=["small_rng"] } -smallvec = { version="0.6.14, < 1.0" } -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +rand = { version = "0.7", features = ["small_rng"] } +smallvec = { version = "0.6.14, < 1.0" } +uucore = { version = ">=0.0.8", package = "uucore", path = "../../uucore" } +uucore_procs = { version = ">=0.0.5", package = "uucore_procs", path = "../../uucore_procs" } +clap = "2.33" [dev-dependencies] -criterion = "0.3" paste = "0.1.18" quickcheck = "0.9.2" -rand_chacha = "0.2.2" -[[bench]] -name = "gcd" -harness = false [[bin]] name = "factor" diff --git a/src/uu/factor/build.rs b/src/uu/factor/build.rs index eff21ae2f..32640d61a 100644 --- a/src/uu/factor/build.rs +++ b/src/uu/factor/build.rs @@ -13,8 +13,6 @@ //! 2 has no multiplicative inverse mode 2^64 because 2 | 2^64, //! and in any case divisibility by two is trivial by checking the LSB. -// spell-checker:ignore (ToDO) invs newr newrp newtp outstr - #![cfg_attr(test, allow(dead_code))] use std::env::{self, args}; @@ -60,13 +58,13 @@ fn main() { let mut x = primes.next().unwrap(); for next in primes { // format the table - let outstr = format!("({}, {}, {}),", x, modular_inverse(x), std::u64::MAX / x); - if cols + outstr.len() > MAX_WIDTH { - write!(file, "\n {}", outstr).unwrap(); - cols = 4 + outstr.len(); + let output = format!("({}, {}, {}),", x, modular_inverse(x), std::u64::MAX / x); + if cols + output.len() > MAX_WIDTH { + write!(file, "\n {}", output).unwrap(); + cols = 4 + output.len(); } else { - write!(file, " {}", outstr).unwrap(); - cols += 1 + outstr.len(); + write!(file, " {}", output).unwrap(); + cols += 1 + output.len(); } x = next; @@ -81,7 +79,7 @@ fn main() { } #[test] -fn test_generator_isprime() { +fn test_generator_is_prime() { assert_eq!(Sieve::odd_primes.take(10_000).all(is_prime)); } @@ -106,5 +104,5 @@ const PREAMBLE: &str = r##"/* // re-run src/factor/gen_tables.rs. #[allow(clippy::unreadable_literal)] -pub const P_INVS_U64: &[(u64, u64, u64)] = &[ +pub const PRIME_INVERSIONS_U64: &[(u64, u64, u64)] = &[ "##; diff --git a/src/uu/factor/sieve.rs b/src/uu/factor/sieve.rs index c10321a7f..492c8159f 100644 --- a/src/uu/factor/sieve.rs +++ b/src/uu/factor/sieve.rs @@ -7,7 +7,7 @@ // spell-checker:ignore (ToDO) filts, minidx, minkey paridx -use std::iter::{Chain, Cycle, Map}; +use std::iter::{Chain, Copied, Cycle}; use std::slice::Iter; /// A lazy Sieve of Eratosthenes. @@ -30,7 +30,7 @@ impl Iterator for Sieve { #[inline] fn next(&mut self) -> Option { - while let Some(n) = self.inner.next() { + for n in &mut self.inner { let mut prime = true; while let Some((next, inc)) = self.filts.peek() { // need to keep checking the min element of the heap @@ -68,27 +68,17 @@ impl Sieve { #[allow(dead_code)] #[inline] pub fn primes() -> PrimeSieve { - #[allow(clippy::trivially_copy_pass_by_ref)] - fn deref(x: &u64) -> u64 { - *x - } - let deref = deref as fn(&u64) -> u64; - INIT_PRIMES.iter().map(deref).chain(Sieve::new()) + INIT_PRIMES.iter().copied().chain(Sieve::new()) } #[allow(dead_code)] #[inline] pub fn odd_primes() -> PrimeSieve { - #[allow(clippy::trivially_copy_pass_by_ref)] - fn deref(x: &u64) -> u64 { - *x - } - let deref = deref as fn(&u64) -> u64; - (&INIT_PRIMES[1..]).iter().map(deref).chain(Sieve::new()) + (&INIT_PRIMES[1..]).iter().copied().chain(Sieve::new()) } } -pub type PrimeSieve = Chain, fn(&u64) -> u64>, Sieve>; +pub type PrimeSieve = Chain>, Sieve>; /// An iterator that generates an infinite list of numbers that are /// not divisible by any of 2, 3, 5, or 7. diff --git a/src/uu/factor/src/cli.rs b/src/uu/factor/src/cli.rs index 34bcf6a2d..0f5d21362 100644 --- a/src/uu/factor/src/cli.rs +++ b/src/uu/factor/src/cli.rs @@ -13,17 +13,20 @@ use std::error::Error; use std::io::{self, stdin, stdout, BufRead, Write}; mod factor; -pub(crate) use factor::*; +use clap::{crate_version, App, Arg}; +pub use factor::*; mod miller_rabin; pub mod numeric; mod rho; -mod table; +pub mod table; -static SYNTAX: &str = "[OPTION] [NUMBER]..."; -static SUMMARY: &str = "Print the prime factors of the given number(s). - If none are specified, read from standard input."; -static LONG_HELP: &str = ""; +static SUMMARY: &str = "Print the prime factors of the given NUMBER(s). +If none are specified, read from standard input."; + +mod options { + pub static NUMBER: &str = "NUMBER"; +} fn print_factors_str(num_str: &str, w: &mut impl io::Write) -> Result<(), Box> { num_str @@ -33,11 +36,17 @@ fn print_factors_str(num_str: &str, w: &mut impl io::Write) -> Result<(), Box i32 { - let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); + let matches = uu_app().get_matches_from(args); let stdout = stdout(); let mut w = io::BufWriter::new(stdout.lock()); - if matches.free.is_empty() { + if let Some(values) = matches.values_of(options::NUMBER) { + for number in values { + if let Err(e) = print_factors_str(number, &mut w) { + show_warning!("{}: {}", number, e); + } + } + } else { let stdin = stdin(); for line in stdin.lock().lines() { @@ -47,12 +56,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } } - } else { - for number in &matches.free { - if let Err(e) = print_factors_str(number, &mut w) { - show_warning!("{}: {}", number, e); - } - } } if let Err(e) = w.flush() { @@ -61,3 +64,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(SUMMARY) + .arg(Arg::with_name(options::NUMBER).multiple(true)) +} diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index 7beb6a043..54ad0bdd4 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -9,7 +9,7 @@ use smallvec::SmallVec; use std::cell::RefCell; use std::fmt; -use crate::numeric::{gcd, Arithmetic, Montgomery}; +use crate::numeric::{Arithmetic, Montgomery}; use crate::{miller_rabin, rho, table}; type Exponent = u8; @@ -17,6 +17,7 @@ type Exponent = u8; #[derive(Clone, Debug)] struct Decomposition(SmallVec<[(u64, Exponent); NUM_FACTORS_INLINE]>); +// spell-checker:ignore (names) Erdős–Kac * Erdős Kac // The number of factors to inline directly into a `Decomposition` object. // As a consequence of the Erdős–Kac theorem, the average number of prime factors // of integers < 10²⁵ ≃ 2⁸³ is 4, so we can use a slightly higher value. @@ -29,20 +30,15 @@ impl Decomposition { fn add(&mut self, factor: u64, exp: Exponent) { debug_assert!(exp > 0); - // Assert the factor doesn't already exist in the Decomposition object - debug_assert_eq!(self.0.iter_mut().find(|(f, _)| *f == factor), None); - self.0.push((factor, exp)) - } - - fn is_one(&self) -> bool { - self.0.is_empty() - } - - fn pop(&mut self) -> Option<(u64, Exponent)> { - self.0.pop() + if let Some((_, e)) = self.0.iter_mut().find(|(f, _)| *f == factor) { + *e += exp; + } else { + self.0.push((factor, exp)) + } } + #[cfg(test)] fn product(&self) -> u64 { self.0 .iter() @@ -86,11 +82,11 @@ impl Factors { self.0.borrow_mut().add(prime, exp) } - #[cfg(test)] pub fn push(&mut self, prime: u64) { self.add(prime, 1) } + #[cfg(test)] fn product(&self) -> u64 { self.0.borrow().product() } @@ -111,134 +107,91 @@ impl fmt::Display for Factors { } } -fn _find_factor(num: u64) -> Option { +fn _factor(num: u64, f: Factors) -> Factors { use miller_rabin::Result::*; + // Shadow the name, so the recursion automatically goes from “Big” arithmetic to small. + let _factor = |n, f| { + if n < (1 << 32) { + _factor::>(n, f) + } else { + _factor::(n, f) + } + }; + + if num == 1 { + return f; + } + let n = A::new(num); - match miller_rabin::test::(n) { - Prime => None, - Composite(d) => Some(d), - Pseudoprime => Some(rho::find_divisor::(n)), - } + let divisor = match miller_rabin::test::(n) { + Prime => { + #[cfg(feature = "coz")] + coz::progress!("factor found"); + let mut r = f; + r.push(num); + return r; + } + + Composite(d) => d, + Pseudoprime => rho::find_divisor::(n), + }; + + let f = _factor(divisor, f); + _factor(num / divisor, f) } -fn find_factor(num: u64) -> Option { - if num < (1 << 32) { - _find_factor::>(num) - } else { - _find_factor::>(num) - } -} - -pub fn factor(num: u64) -> Factors { +pub fn factor(mut n: u64) -> Factors { + #[cfg(feature = "coz")] + coz::begin!("factorization"); let mut factors = Factors::one(); - if num < 2 { + if n < 2 { return factors; } - let mut n = num; - let n_zeros = num.trailing_zeros(); + let n_zeros = n.trailing_zeros(); if n_zeros > 0 { factors.add(2, n_zeros as Exponent); n >>= n_zeros; } - debug_assert_eq!(num, n * factors.product()); if n == 1 { + #[cfg(feature = "coz")] + coz::end!("factorization"); return factors; } table::factor(&mut n, &mut factors); - debug_assert_eq!(num, n * factors.product()); - if n == 1 { - return factors; - } + #[allow(clippy::let_and_return)] + let r = if n < (1 << 32) { + _factor::>(n, factors) + } else { + _factor::>(n, factors) + }; - let mut dec = Decomposition::one(); - dec.add(n, 1); + #[cfg(feature = "coz")] + coz::end!("factorization"); - while !dec.is_one() { - // Check correctness invariant - debug_assert_eq!(num, factors.product() * dec.product()); - - let (factor, exp) = dec.pop().unwrap(); - - if let Some(divisor) = find_factor(factor) { - let mut gcd_queue = Decomposition::one(); - - let quotient = factor / divisor; - let mut trivial_gcd = quotient == divisor; - if trivial_gcd { - gcd_queue.add(divisor, exp + 1); - } else { - gcd_queue.add(divisor, exp); - gcd_queue.add(quotient, exp); - } - - while !trivial_gcd { - debug_assert_eq!(factor, gcd_queue.product()); - - let mut tmp = Decomposition::one(); - trivial_gcd = true; - for i in 0..gcd_queue.0.len() - 1 { - let (mut a, exp_a) = gcd_queue.0[i]; - let (mut b, exp_b) = gcd_queue.0[i + 1]; - - if a == 1 { - continue; - } - - let g = gcd(a, b); - if g != 1 { - trivial_gcd = false; - a /= g; - b /= g; - } - if a != 1 { - tmp.add(a, exp_a); - } - if g != 1 { - tmp.add(g, exp_a + exp_b); - } - - if i + 1 != gcd_queue.0.len() - 1 { - gcd_queue.0[i + 1].0 = b; - } else if b != 1 { - tmp.add(b, exp_b); - } - } - gcd_queue = tmp; - } - - debug_assert_eq!(factor, gcd_queue.product()); - dec.0.extend(gcd_queue.0); - } else { - // factor is prime - factors.add(factor, exp); - } - } - - factors + r } #[cfg(test)] mod tests { - use super::{factor, Factors}; + use super::{factor, Decomposition, Exponent, Factors}; use quickcheck::quickcheck; + use smallvec::smallvec; + use std::cell::RefCell; #[test] - fn factor_correctly_recombines_prior_test_failures() { - let prior_failures = [ - // * integers with duplicate factors (ie, N.pow(M)) - 4566769_u64, // == 2137.pow(2) - 2044854919485649_u64, - 18446739546814299361_u64, - 18446738440860217487_u64, - 18446736729316206481_u64, - ]; - assert!(prior_failures.iter().all(|i| factor(*i).product() == *i)); + fn factor_2044854919485649() { + let f = Factors(RefCell::new(Decomposition(smallvec![ + (503, 1), + (2423, 1), + (40961, 2) + ]))); + assert_eq!(factor(f.product()), f); } #[test] @@ -248,15 +201,6 @@ mod tests { .all(|i| factor(i).product() == i)); } - #[test] - fn factor_recombines_small_squares() { - // factor(18446736729316206481) == 4294966441 ** 2 ; causes debug_assert fault for repeated decomposition factor in add() - // ToDO: explain/combine with factor_18446736729316206481 and factor_18446739546814299361 tests - assert!((1..10_000) - .map(|i| (2 * i + 1) * (2 * i + 1)) - .all(|i| factor(i).product() == i)); - } - #[test] fn factor_recombines_overflowing() { assert!((0..250) @@ -282,28 +226,39 @@ mod tests { i == 0 || factor(i).product() == i } - fn recombines_factors(f: Factors) -> bool { + fn recombines_factors(f: Factors) -> () { assert_eq!(factor(f.product()), f); - true + } + + fn exponentiate_factors(f: Factors, e: Exponent) -> () { + if e == 0 { return; } + if let Some(fe) = f.product().checked_pow(e.into()) { + assert_eq!(factor(fe), f ^ e); + } } } } #[cfg(test)] -impl quickcheck::Arbitrary for Factors { - fn arbitrary(gen: &mut G) -> Self { - use rand::Rng; +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; +#[cfg(test)] +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Factors { let mut f = Factors::one(); let mut g = 1u64; let mut n = u64::MAX; + // spell-checker:ignore (names) Adam Kalai * Kalai's // Adam Kalai's algorithm for generating uniformly-distributed // integers and their factorization. // // See Generating Random Factored Numbers, Easily, J. Cryptology (2003) 'attempt: loop { while n > 1 { - n = gen.gen_range(1, n); + n = rng.gen_range(1, n); if miller_rabin::is_prime(n) { if let Some(h) = g.checked_mul(n) { f.push(n); @@ -319,3 +274,26 @@ impl quickcheck::Arbitrary for Factors { } } } + +#[cfg(test)] +impl quickcheck::Arbitrary for Factors { + fn arbitrary(g: &mut G) -> Self { + g.gen() + } +} + +#[cfg(test)] +impl std::ops::BitXor for Factors { + type Output = Self; + + fn bitxor(self, rhs: Exponent) -> Factors { + debug_assert_ne!(rhs, 0); + let mut r = Factors::one(); + for (p, e) in self.0.borrow().0.iter() { + r.add(*p, rhs * e); + } + + debug_assert_eq!(r.product(), self.product().pow(rhs.into())); + r + } +} diff --git a/src/uu/factor/src/main.rs b/src/uu/factor/src/main.rs index b251716b5..4d8b281b6 100644 --- a/src/uu/factor/src/main.rs +++ b/src/uu/factor/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_factor); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_factor); diff --git a/src/uu/factor/src/miller_rabin.rs b/src/uu/factor/src/miller_rabin.rs index 2a3b7bdf1..ec6d81c0f 100644 --- a/src/uu/factor/src/miller_rabin.rs +++ b/src/uu/factor/src/miller_rabin.rs @@ -21,6 +21,7 @@ impl Basis for Montgomery { } impl Basis for Montgomery { + // spell-checker:ignore (names) Steve Worley // Small set of bases for the Miller-Rabin prime test, valid for all 32b integers; // discovered by Steve Worley on 2013-05-27, see miller-rabin.appspot.com #[allow(clippy::unreadable_literal)] @@ -121,8 +122,8 @@ mod tests { } fn odd_primes() -> impl Iterator { - use crate::table::{NEXT_PRIME, P_INVS_U64}; - P_INVS_U64 + use crate::table::{NEXT_PRIME, PRIME_INVERSIONS_U64}; + PRIME_INVERSIONS_U64 .iter() .map(|(p, _, _)| *p) .chain(iter::once(NEXT_PRIME)) diff --git a/src/uu/factor/src/numeric/gcd.rs b/src/uu/factor/src/numeric/gcd.rs index 36ad833a6..004ef1515 100644 --- a/src/uu/factor/src/numeric/gcd.rs +++ b/src/uu/factor/src/numeric/gcd.rs @@ -93,7 +93,7 @@ mod tests { gcd(a, gcd(b, c)) == gcd(gcd(a, b), c) } - fn scalar_mult(a: u64, b: u64, k: u64) -> bool { + fn scalar_multiplication(a: u64, b: u64, k: u64) -> bool { gcd(k * a, k * b) == k * gcd(a, b) } diff --git a/src/uu/factor/src/numeric/modular_inverse.rs b/src/uu/factor/src/numeric/modular_inverse.rs index 763ee90a0..e4827a3ee 100644 --- a/src/uu/factor/src/numeric/modular_inverse.rs +++ b/src/uu/factor/src/numeric/modular_inverse.rs @@ -16,11 +16,11 @@ pub(crate) fn modular_inverse(a: T) -> T { debug_assert!(a % (one + one) == one, "{:?} is not odd", a); let mut t = zero; - let mut newt = one; + let mut new_t = one; let mut r = zero; - let mut newr = a; + let mut new_r = a; - while newr != zero { + while new_r != zero { let quot = if r == zero { // special case when we're just starting out // This works because we know that @@ -28,15 +28,15 @@ pub(crate) fn modular_inverse(a: T) -> T { T::max_value() } else { r - } / newr; + } / new_r; - let newtp = t.wrapping_sub(".wrapping_mul(&newt)); - t = newt; - newt = newtp; + let new_tp = t.wrapping_sub(".wrapping_mul(&new_t)); + t = new_t; + new_t = new_tp; - let newrp = r.wrapping_sub(".wrapping_mul(&newr)); - r = newr; - newr = newrp; + let new_rp = r.wrapping_sub(".wrapping_mul(&new_r)); + r = new_r; + new_r = new_rp; } debug_assert_eq!(r, one); diff --git a/src/uu/factor/src/numeric/montgomery.rs b/src/uu/factor/src/numeric/montgomery.rs index 8c38464bc..485025d87 100644 --- a/src/uu/factor/src/numeric/montgomery.rs +++ b/src/uu/factor/src/numeric/montgomery.rs @@ -69,7 +69,7 @@ impl Montgomery { let t_bits = T::zero().count_zeros() as usize; debug_assert!(x < (self.n.as_double_width()) << t_bits); - // TODO: optimiiiiiiise + // TODO: optimize let Montgomery { a, n } = self; let m = T::from_double_width(x).wrapping_mul(a); let nm = (n.as_double_width()) * (m.as_double_width()); @@ -138,7 +138,7 @@ impl Arithmetic for Montgomery { r + self.n.wrapping_neg() }; - // Normalise to [0; n[ + // Normalize to [0; n[ let r = if r < self.n { r } else { r - self.n }; // Check that r (reduced back to the usual representation) equals @@ -200,7 +200,7 @@ mod tests { } parametrized_check!(test_add); - fn test_mult() { + fn test_multiplication() { for n in 0..100 { let n = 2 * n + 1; let m = Montgomery::::new(n); @@ -213,7 +213,7 @@ mod tests { } } } - parametrized_check!(test_mult); + parametrized_check!(test_multiplication); fn test_roundtrip() { for n in 0..100 { diff --git a/src/uu/factor/src/table.rs b/src/uu/factor/src/table.rs index b62e801cb..6d4047e49 100644 --- a/src/uu/factor/src/table.rs +++ b/src/uu/factor/src/table.rs @@ -8,16 +8,13 @@ // spell-checker: ignore (ToDO) INVS -use std::num::Wrapping; - use crate::Factors; include!(concat!(env!("OUT_DIR"), "/prime_table.rs")); -pub(crate) fn factor(n: &mut u64, factors: &mut Factors) { - let mut num = *n; - for &(prime, inv, ceil) in P_INVS_U64 { - if num == 1 { +pub fn factor(num: &mut u64, factors: &mut Factors) { + for &(prime, inv, ceil) in PRIME_INVERSIONS_U64 { + if *num == 1 { break; } @@ -28,12 +25,14 @@ pub(crate) fn factor(n: &mut u64, factors: &mut Factors) { // for a nice explanation. let mut k = 0; loop { - let Wrapping(x) = Wrapping(num) * Wrapping(inv); + let x = num.wrapping_mul(inv); // While prime divides num if x <= ceil { - num = x; + *num = x; k += 1; + #[cfg(feature = "coz")] + coz::progress!("factor found"); } else { if k > 0 { factors.add(prime, k); @@ -42,6 +41,61 @@ pub(crate) fn factor(n: &mut u64, factors: &mut Factors) { } } } - - *n = num; +} + +pub const CHUNK_SIZE: usize = 8; +pub fn factor_chunk(n_s: &mut [u64; CHUNK_SIZE], f_s: &mut [Factors; CHUNK_SIZE]) { + for &(prime, inv, ceil) in PRIME_INVERSIONS_U64 { + if n_s[0] == 1 && n_s[1] == 1 && n_s[2] == 1 && n_s[3] == 1 { + break; + } + + for (num, factors) in n_s.iter_mut().zip(f_s.iter_mut()) { + if *num == 1 { + continue; + } + let mut k = 0; + loop { + let x = num.wrapping_mul(inv); + + // While prime divides num + if x <= ceil { + *num = x; + k += 1; + } else { + if k > 0 { + factors.add(prime, k); + } + break; + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Factors; + use quickcheck::quickcheck; + use rand::{rngs::SmallRng, Rng, SeedableRng}; + + quickcheck! { + fn chunk_vs_iter(seed: u64) -> () { + let mut rng = SmallRng::seed_from_u64(seed); + let mut n_c: [u64; CHUNK_SIZE] = rng.gen(); + let mut f_c: [Factors; CHUNK_SIZE] = rng.gen(); + + let mut n_i = n_c.clone(); + let mut f_i = f_c.clone(); + for (n, f) in n_i.iter_mut().zip(f_i.iter_mut()) { + factor(n, f); + } + + factor_chunk(&mut n_c, &mut f_c); + + assert_eq!(n_i, n_c); + assert_eq!(f_i, f_c); + } + } } diff --git a/src/uu/false/Cargo.toml b/src/uu/false/Cargo.toml index 413cbc990..644051d59 100644 --- a/src/uu/false/Cargo.toml +++ b/src/uu/false/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_false" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "false ~ (uutils) do nothing and fail" @@ -15,7 +15,8 @@ edition = "2018" path = "src/false.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33.3" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/false/src/false.rs b/src/uu/false/src/false.rs index 917c43fa0..17c681129 100644 --- a/src/uu/false/src/false.rs +++ b/src/uu/false/src/false.rs @@ -5,6 +5,14 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -pub fn uumain(_: impl uucore::Args) -> i32 { +use clap::App; +use uucore::executable; + +pub fn uumain(args: impl uucore::Args) -> i32 { + uu_app().get_matches_from(args); 1 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) +} diff --git a/src/uu/false/src/main.rs b/src/uu/false/src/main.rs index 0cede3b5e..382a16fc7 100644 --- a/src/uu/false/src/main.rs +++ b/src/uu/false/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_false); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_false); diff --git a/src/uu/fmt/Cargo.toml b/src/uu/fmt/Cargo.toml index e150b2962..24ee13b35 100644 --- a/src/uu/fmt/Cargo.toml +++ b/src/uu/fmt/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_fmt" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "fmt ~ (uutils) reformat each paragraph of input" @@ -18,7 +18,7 @@ path = "src/fmt.rs" clap = "2.33" libc = "0.2.42" unicode-width = "0.1.5" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index f10f4cf7f..8c2c8d9d9 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -10,7 +10,7 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::cmp; use std::fs::File; use std::io::{stdin, stdout, Write}; @@ -32,7 +32,6 @@ mod linebreak; mod parasplit; static ABOUT: &str = "Reformat paragraphs from input files (or stdin) to stdout."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static MAX_WIDTH: usize = 2500; static OPT_CROWN_MARGIN: &str = "crown-margin"; @@ -78,129 +77,7 @@ pub struct FmtOptions { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(OPT_CROWN_MARGIN) - .short("c") - .long(OPT_CROWN_MARGIN) - .help( - "First and second line of paragraph - may have different indentations, in which - case the first line's indentation is preserved, - and each subsequent line's indentation matches the second line.", - ), - ) - .arg( - Arg::with_name(OPT_TAGGED_PARAGRAPH) - .short("t") - .long("tagged-paragraph") - .help( - "Like -c, except that the first and second line of a paragraph *must* - have different indentation or they are treated as separate paragraphs.", - ), - ) - .arg( - Arg::with_name(OPT_PRESERVE_HEADERS) - .short("m") - .long("preserve-headers") - .help( - "Attempt to detect and preserve mail headers in the input. - Be careful when combining this flag with -p.", - ), - ) - .arg( - Arg::with_name(OPT_SPLIT_ONLY) - .short("s") - .long("split-only") - .help("Split lines only, do not reflow."), - ) - .arg( - Arg::with_name(OPT_UNIFORM_SPACING) - .short("u") - .long("uniform-spacing") - .help( - "Insert exactly one - space between words, and two between sentences. - Sentence breaks in the input are detected as [?!.] - followed by two spaces or a newline; other punctuation - is not interpreted as a sentence break.", - ), - ) - .arg( - Arg::with_name(OPT_PREFIX) - .short("p") - .long("prefix") - .help( - "Reformat only lines - beginning with PREFIX, reattaching PREFIX to reformatted lines. - Unless -x is specified, leading whitespace will be ignored - when matching PREFIX.", - ) - .value_name("PREFIX"), - ) - .arg( - Arg::with_name(OPT_SKIP_PREFIX) - .short("P") - .long("skip-prefix") - .help( - "Do not reformat lines - beginning with PSKIP. Unless -X is specified, leading whitespace - will be ignored when matching PSKIP", - ) - .value_name("PSKIP"), - ) - .arg( - Arg::with_name(OPT_EXACT_PREFIX) - .short("x") - .long("exact-prefix") - .help( - "PREFIX must match at the - beginning of the line with no preceding whitespace.", - ), - ) - .arg( - Arg::with_name(OPT_EXACT_SKIP_PREFIX) - .short("X") - .long("exact-skip-prefix") - .help( - "PSKIP must match at the - beginning of the line with no preceding whitespace.", - ), - ) - .arg( - Arg::with_name(OPT_WIDTH) - .short("w") - .long("width") - .help("Fill output lines up to a maximum of WIDTH columns, default 79.") - .value_name("WIDTH"), - ) - .arg( - Arg::with_name(OPT_GOAL) - .short("g") - .long("goal") - .help("Goal width, default ~0.94*WIDTH. Must be less than WIDTH.") - .value_name("GOAL"), - ) - .arg(Arg::with_name(OPT_QUICK).short("q").long("quick").help( - "Break lines more quickly at the - expense of a potentially more ragged appearance.", - )) - .arg( - Arg::with_name(OPT_TAB_WIDTH) - .short("T") - .long("tab-width") - .help( - "Treat tabs as TABWIDTH spaces for - determining line length, default 8. Note that this is used only for - calculating line lengths; tabs are preserved in the output.", - ) - .value_name("TABWIDTH"), - ) - .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); let mut files: Vec = matches .values_of(ARG_FILES) @@ -332,3 +209,127 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(OPT_CROWN_MARGIN) + .short("c") + .long(OPT_CROWN_MARGIN) + .help( + "First and second line of paragraph \ + may have different indentations, in which \ + case the first line's indentation is preserved, \ + and each subsequent line's indentation matches the second line.", + ), + ) + .arg( + Arg::with_name(OPT_TAGGED_PARAGRAPH) + .short("t") + .long("tagged-paragraph") + .help( + "Like -c, except that the first and second line of a paragraph *must* \ + have different indentation or they are treated as separate paragraphs.", + ), + ) + .arg( + Arg::with_name(OPT_PRESERVE_HEADERS) + .short("m") + .long("preserve-headers") + .help( + "Attempt to detect and preserve mail headers in the input. \ + Be careful when combining this flag with -p.", + ), + ) + .arg( + Arg::with_name(OPT_SPLIT_ONLY) + .short("s") + .long("split-only") + .help("Split lines only, do not reflow."), + ) + .arg( + Arg::with_name(OPT_UNIFORM_SPACING) + .short("u") + .long("uniform-spacing") + .help( + "Insert exactly one \ + space between words, and two between sentences. \ + Sentence breaks in the input are detected as [?!.] \ + followed by two spaces or a newline; other punctuation \ + is not interpreted as a sentence break.", + ), + ) + .arg( + Arg::with_name(OPT_PREFIX) + .short("p") + .long("prefix") + .help( + "Reformat only lines \ + beginning with PREFIX, reattaching PREFIX to reformatted lines. \ + Unless -x is specified, leading whitespace will be ignored \ + when matching PREFIX.", + ) + .value_name("PREFIX"), + ) + .arg( + Arg::with_name(OPT_SKIP_PREFIX) + .short("P") + .long("skip-prefix") + .help( + "Do not reformat lines \ + beginning with PSKIP. Unless -X is specified, leading whitespace \ + will be ignored when matching PSKIP", + ) + .value_name("PSKIP"), + ) + .arg( + Arg::with_name(OPT_EXACT_PREFIX) + .short("x") + .long("exact-prefix") + .help( + "PREFIX must match at the \ + beginning of the line with no preceding whitespace.", + ), + ) + .arg( + Arg::with_name(OPT_EXACT_SKIP_PREFIX) + .short("X") + .long("exact-skip-prefix") + .help( + "PSKIP must match at the \ + beginning of the line with no preceding whitespace.", + ), + ) + .arg( + Arg::with_name(OPT_WIDTH) + .short("w") + .long("width") + .help("Fill output lines up to a maximum of WIDTH columns, default 79.") + .value_name("WIDTH"), + ) + .arg( + Arg::with_name(OPT_GOAL) + .short("g") + .long("goal") + .help("Goal width, default ~0.94*WIDTH. Must be less than WIDTH.") + .value_name("GOAL"), + ) + .arg(Arg::with_name(OPT_QUICK).short("q").long("quick").help( + "Break lines more quickly at the \ + expense of a potentially more ragged appearance.", + )) + .arg( + Arg::with_name(OPT_TAB_WIDTH) + .short("T") + .long("tab-width") + .help( + "Treat tabs as TABWIDTH spaces for \ + determining line length, default 8. Note that this is used only for \ + calculating line lengths; tabs are preserved in the output.", + ) + .value_name("TABWIDTH"), + ) + .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) +} diff --git a/src/uu/fmt/src/linebreak.rs b/src/uu/fmt/src/linebreak.rs index c41fd41bd..fe9f8568e 100644 --- a/src/uu/fmt/src/linebreak.rs +++ b/src/uu/fmt/src/linebreak.rs @@ -81,7 +81,7 @@ pub fn break_lines(para: &Paragraph, opts: &FmtOptions, ostream: &mut BufWriter< let mut break_args = BreakArgs { opts, init_len: p_init_len, - indent_str: &p_indent[..], + indent_str: p_indent, indent_len: p_indent_len, uniform, ostream, @@ -296,7 +296,7 @@ fn find_kp_breakpoints<'a, T: Iterator>>( (0, 0.0) } else { compute_demerits( - (args.opts.goal - tlen) as isize, + args.opts.goal as isize - tlen as isize, stretch, w.word_nchars as isize, active.prev_rat, diff --git a/src/uu/fmt/src/main.rs b/src/uu/fmt/src/main.rs index d7e883ba7..35531a8b4 100644 --- a/src/uu/fmt/src/main.rs +++ b/src/uu/fmt/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_fmt); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_fmt); diff --git a/src/uu/fmt/src/parasplit.rs b/src/uu/fmt/src/parasplit.rs index f74a25413..71b5f62ec 100644 --- a/src/uu/fmt/src/parasplit.rs +++ b/src/uu/fmt/src/parasplit.rs @@ -264,12 +264,9 @@ impl<'a> ParagraphStream<'a> { return false; } - #[allow(clippy::match_like_matches_macro)] - // `matches!(...)` macro not stabilized until rust v1.42 - l_slice[..colon_posn].chars().all(|x| match x as usize { - y if y < 33 || y > 126 => false, - _ => true, - }) + l_slice[..colon_posn] + .chars() + .all(|x| !matches!(x as usize, y if !(33..=126).contains(&y))) } } } @@ -541,12 +538,7 @@ impl<'a> WordSplit<'a> { } fn is_punctuation(c: char) -> bool { - #[allow(clippy::match_like_matches_macro)] - // `matches!(...)` macro not stabilized until rust v1.42 - match c { - '!' | '.' | '?' => true, - _ => false, - } + matches!(c, '!' | '.' | '?') } } diff --git a/src/uu/fold/Cargo.toml b/src/uu/fold/Cargo.toml index 23dadc8eb..c5578384e 100644 --- a/src/uu/fold/Cargo.toml +++ b/src/uu/fold/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_fold" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "fold ~ (uutils) wrap each line of input" @@ -15,7 +15,8 @@ edition = "2018" path = "src/fold.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index 05f0725ef..1dbc8cdc7 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -10,46 +10,41 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{stdin, BufRead, BufReader, Read}; use std::path::Path; +use uucore::InvalidEncodingHandling; +const TAB_WIDTH: usize = 8; + +static NAME: &str = "fold"; static SYNTAX: &str = "[OPTION]... [FILE]..."; static SUMMARY: &str = "Writes each file (or standard input if no files are given) to standard output whilst breaking long lines"; -static LONG_HELP: &str = ""; + +mod options { + pub const BYTES: &str = "bytes"; + pub const SPACES: &str = "spaces"; + pub const WIDTH: &str = "width"; + pub const FILE: &str = "file"; +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); let (args, obs_width) = handle_obsolete(&args[..]); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optflag( - "b", - "bytes", - "count using bytes rather than columns (meaning control characters \ - such as newline are not treated specially)", - ) - .optflag( - "s", - "spaces", - "break lines at word boundaries rather than a hard cut-off", - ) - .optopt( - "w", - "width", - "set WIDTH as the maximum line width rather than 80", - "WIDTH", - ) - .parse(args); + let matches = uu_app().get_matches_from(args); - let bytes = matches.opt_present("b"); - let spaces = matches.opt_present("s"); - let poss_width = if matches.opt_present("w") { - matches.opt_str("w") - } else { - obs_width + let bytes = matches.is_present(options::BYTES); + let spaces = matches.is_present(options::SPACES); + let poss_width = match matches.value_of(options::WIDTH) { + Some(v) => Some(v.to_owned()), + None => obs_width, }; + let width = match poss_width { Some(inp_width) => match inp_width.parse::() { Ok(width) => width, @@ -57,20 +52,56 @@ pub fn uumain(args: impl uucore::Args) -> i32 { }, None => 80, }; - let files = if matches.free.is_empty() { - vec!["-".to_owned()] - } else { - matches.free + + let files = match matches.values_of(options::FILE) { + Some(v) => v.map(|v| v.to_owned()).collect(), + None => vec!["-".to_owned()], }; + fold(files, bytes, spaces, width); 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(SYNTAX) + .about(SUMMARY) + .arg( + Arg::with_name(options::BYTES) + .long(options::BYTES) + .short("b") + .help( + "count using bytes rather than columns (meaning control characters \ + such as newline are not treated specially)", + ) + .takes_value(false), + ) + .arg( + Arg::with_name(options::SPACES) + .long(options::SPACES) + .short("s") + .help("break lines at word boundaries rather than a hard cut-off") + .takes_value(false), + ) + .arg( + Arg::with_name(options::WIDTH) + .long(options::WIDTH) + .short("w") + .help("set WIDTH as the maximum line width rather than 80") + .value_name("WIDTH") + .allow_hyphen_values(true) + .takes_value(true), + ) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) +} + fn handle_obsolete(args: &[String]) -> (Vec, Option) { for (i, arg) in args.iter().enumerate() { let slice = &arg; - if slice.starts_with('-') && slice.len() > 1 && slice.chars().nth(1).unwrap().is_digit(10) { + if slice.starts_with('-') && slice.chars().nth(1).map_or(false, |c| c.is_digit(10)) { let mut v = args.to_vec(); v.remove(i); return (v, Some(slice[1..].to_owned())); @@ -79,10 +110,9 @@ fn handle_obsolete(args: &[String]) -> (Vec, Option) { (args.to_vec(), None) } -#[inline] fn fold(filenames: Vec, bytes: bool, spaces: bool, width: usize) { for filename in &filenames { - let filename: &str = &filename; + let filename: &str = filename; let mut stdin_buf; let mut file_buf; let buffer = BufReader::new(if filename == "-" { @@ -92,120 +122,163 @@ fn fold(filenames: Vec, bytes: bool, spaces: bool, width: usize) { file_buf = safe_unwrap!(File::open(Path::new(filename))); &mut file_buf as &mut dyn Read }); - fold_file(buffer, bytes, spaces, width); - } -} -#[inline] -fn fold_file(file: BufReader, bytes: bool, spaces: bool, width: usize) { - for line_result in file.lines() { - let mut line = safe_unwrap!(line_result); if bytes { - let len = line.len(); - let mut i = 0; - while i < len { - let width = if len - i >= width { width } else { len - i }; - let slice = { - let slice = &line[i..i + width]; - if spaces && i + width < len { - match slice.rfind(char::is_whitespace) { - Some(m) => &slice[..=m], - None => slice, - } - } else { - slice - } - }; - print!("{}", slice); - i += slice.len(); - } + fold_file_bytewise(buffer, spaces, width); } else { - let mut len = line.chars().count(); - let newline = line.ends_with('\n'); - if newline { - if len == 1 { - println!(); - continue; - } - len -= 1; - line.truncate(len); - } - let mut output = String::new(); - let mut count = 0; - for (i, ch) in line.chars().enumerate() { - if count >= width { - let (val, ncount) = { - let slice = &output[..]; - let (out, val, ncount) = if spaces && i + 1 < len { - match rfind_whitespace(slice) { - Some(m) => { - let routput = &slice[m + 1..slice.chars().count()]; - let ncount = routput.chars().fold(0, |out, ch: char| { - out + match ch { - '\t' => 8, - '\x08' => { - if out > 0 { - !0 - } else { - 0 - } - } - '\r' => return 0, - _ => 1, - } - }); - (&slice[0..=m], routput, ncount) - } - None => (slice, "", 0), - } - } else { - (slice, "", 0) - }; - println!("{}", out); - (val.to_owned(), ncount) - }; - output = val; - count = ncount; - } - match ch { - '\t' => { - count += 8; - if count > width { - println!("{}", output); - output.truncate(0); - count = 8; - } - } - '\x08' => { - if count > 0 { - count -= 1; - let len = output.len() - 1; - output.truncate(len); - } - continue; - } - '\r' => { - output.truncate(0); - count = 0; - continue; - } - _ => count += 1, - }; - output.push(ch); - } - if count > 0 { - println!("{}", output); - } + fold_file(buffer, spaces, width); } } } -#[inline] -fn rfind_whitespace(slice: &str) -> Option { - for (i, ch) in slice.chars().rev().enumerate() { - if ch.is_whitespace() { - return Some(slice.chars().count() - (i + 1)); +/// Fold `file` to fit `width` (number of columns), counting all characters as +/// one column. +/// +/// This function handles folding for the `-b`/`--bytes` option, counting +/// tab, backspace, and carriage return as occupying one column, identically +/// to all other characters in the stream. +/// +/// If `spaces` is `true`, attempt to break lines at whitespace boundaries. +fn fold_file_bytewise(mut file: BufReader, spaces: bool, width: usize) { + let mut line = String::new(); + + loop { + if let Ok(0) = file.read_line(&mut line) { + break; } + + if line == "\n" { + println!(); + line.truncate(0); + continue; + } + + let len = line.len(); + let mut i = 0; + + while i < len { + let width = if len - i >= width { width } else { len - i }; + let slice = { + let slice = &line[i..i + width]; + if spaces && i + width < len { + match slice.rfind(|c: char| c.is_whitespace() && c != '\r') { + Some(m) => &slice[..=m], + None => slice, + } + } else { + slice + } + }; + + // Don't duplicate trailing newlines: if the slice is "\n", the + // previous iteration folded just before the end of the line and + // has already printed this newline. + if slice == "\n" { + break; + } + + i += slice.len(); + + let at_eol = i >= len; + + if at_eol { + print!("{}", slice); + } else { + println!("{}", slice); + } + } + + line.truncate(0); + } +} + +/// Fold `file` to fit `width` (number of columns). +/// +/// By default `fold` treats tab, backspace, and carriage return specially: +/// tab characters count as 8 columns, backspace decreases the +/// column count, and carriage return resets the column count to 0. +/// +/// If `spaces` is `true`, attempt to break lines at whitespace boundaries. +#[allow(unused_assignments)] +fn fold_file(mut file: BufReader, spaces: bool, width: usize) { + let mut line = String::new(); + let mut output = String::new(); + let mut col_count = 0; + let mut last_space = None; + + /// Print the output line, resetting the column and character counts. + /// + /// If `spaces` is `true`, print the output line up to the last + /// encountered whitespace character (inclusive) and set the remaining + /// characters as the start of the next line. + macro_rules! emit_output { + () => { + let consume = match last_space { + Some(i) => i + 1, + None => output.len(), + }; + + println!("{}", &output[..consume]); + output.replace_range(..consume, ""); + + // we know there are no tabs left in output, so each char counts + // as 1 column + col_count = output.len(); + + last_space = None; + }; + } + + loop { + if let Ok(0) = file.read_line(&mut line) { + break; + } + + for ch in line.chars() { + if ch == '\n' { + // make sure to _not_ split output at whitespace, since we + // know the entire output will fit + last_space = None; + emit_output!(); + break; + } + + if col_count >= width { + emit_output!(); + } + + match ch { + '\r' => col_count = 0, + '\t' => { + let next_tab_stop = col_count + TAB_WIDTH - col_count % TAB_WIDTH; + + if next_tab_stop > width && !output.is_empty() { + emit_output!(); + } + + col_count = next_tab_stop; + last_space = if spaces { Some(output.len()) } else { None }; + } + '\x08' => { + if col_count > 0 { + col_count -= 1; + } + } + _ if spaces && ch.is_whitespace() => { + last_space = Some(output.len()); + col_count += 1; + } + _ => col_count += 1, + }; + + output.push(ch); + } + + if !output.is_empty() { + print!("{}", output); + output.truncate(0); + } + + line.truncate(0); } - None } diff --git a/src/uu/fold/src/main.rs b/src/uu/fold/src/main.rs index abdf80211..1802f2cf8 100644 --- a/src/uu/fold/src/main.rs +++ b/src/uu/fold/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_fold); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_fold); diff --git a/src/uu/groups/Cargo.toml b/src/uu/groups/Cargo.toml index 455e2d224..4a5a537e5 100644 --- a/src/uu/groups/Cargo.toml +++ b/src/uu/groups/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_groups" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "groups ~ (uutils) display group memberships for USERNAME" @@ -15,7 +15,7 @@ edition = "2018" path = "src/groups.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "process"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } clap = "2.33" diff --git a/src/uu/groups/src/groups.rs b/src/uu/groups/src/groups.rs index 2ce5fe70e..a40d1a490 100644 --- a/src/uu/groups/src/groups.rs +++ b/src/uu/groups/src/groups.rs @@ -5,60 +5,93 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +// +// ============================================================================ +// Test suite summary for GNU coreutils 8.32.162-4eda +// ============================================================================ +// PASS: tests/misc/groups-dash.sh +// PASS: tests/misc/groups-process-all.sh +// PASS: tests/misc/groups-version.sh // spell-checker:ignore (ToDO) passwd #[macro_use] extern crate uucore; -use uucore::entries::{get_groups, gid2grp, Locate, Passwd}; +use uucore::entries::{get_groups_gnu, gid2grp, Locate, Passwd}; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; -static VERSION: &str = env!("CARGO_PKG_VERSION"); -static ABOUT: &str = "display current group names"; -static OPT_USER: &str = "user"; +mod options { + pub const USERS: &str = "USERNAME"; +} +static ABOUT: &str = "Print group memberships for each USERNAME or, \ + if no USERNAME is specified, for\nthe current process \ + (which may differ if the groups data‐base has changed)."; fn get_usage() -> String { - format!("{0} [USERNAME]", executable!()) + format!("{0} [OPTION]... [USERNAME]...", executable!()) } pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg(Arg::with_name(OPT_USER)) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - match matches.value_of(OPT_USER) { - None => { + let users: Vec = matches + .values_of(options::USERS) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + let mut exit_code = 0; + + if users.is_empty() { + println!( + "{}", + get_groups_gnu(None) + .unwrap() + .iter() + .map(|&gid| gid2grp(gid).unwrap_or_else(|_| { + show_error!("cannot find name for group ID {}", gid); + exit_code = 1; + gid.to_string() + })) + .collect::>() + .join(" ") + ); + return exit_code; + } + + for user in users { + if let Ok(p) = Passwd::locate(user.as_str()) { println!( - "{}", - get_groups() - .unwrap() + "{} : {}", + user, + p.belongs_to() .iter() - .map(|&g| gid2grp(g).unwrap()) + .map(|&gid| gid2grp(gid).unwrap_or_else(|_| { + show_error!("cannot find name for group ID {}", gid); + exit_code = 1; + gid.to_string() + })) .collect::>() .join(" ") ); - 0 - } - Some(user) => { - if let Ok(p) = Passwd::locate(user) { - println!( - "{}", - p.belongs_to() - .iter() - .map(|&g| gid2grp(g).unwrap()) - .collect::>() - .join(" ") - ); - 0 - } else { - crash!(1, "unknown user {}", user); - } + } else { + show_error!("'{}': no such user", user); + exit_code = 1; } } + exit_code +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::USERS) + .multiple(true) + .takes_value(true) + .value_name(options::USERS), + ) } diff --git a/src/uu/groups/src/main.rs b/src/uu/groups/src/main.rs index 0a37ec7f4..6efe64b54 100644 --- a/src/uu/groups/src/main.rs +++ b/src/uu/groups/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_groups); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_groups); diff --git a/src/uu/hashsum/BENCHMARKING.md b/src/uu/hashsum/BENCHMARKING.md new file mode 100644 index 000000000..cef710a19 --- /dev/null +++ b/src/uu/hashsum/BENCHMARKING.md @@ -0,0 +1,9 @@ +## Benchmarking hashsum + +### To bench blake2 + +Taken from: https://github.com/uutils/coreutils/pull/2296 + +With a large file: +$ hyperfine "./target/release/coreutils hashsum --b2sum large-file" "b2sum large-file" + diff --git a/src/uu/hashsum/Cargo.toml b/src/uu/hashsum/Cargo.toml index c9bb4da9f..11388ebf8 100644 --- a/src/uu/hashsum/Cargo.toml +++ b/src/uu/hashsum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_hashsum" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "hashsum ~ (uutils) display or check input digests" @@ -25,8 +25,8 @@ regex-syntax = "0.6.7" sha1 = "0.6.0" sha2 = "0.6.0" sha3 = "0.6.0" -blake2-rfc = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +blake2b_simd = "0.5.11" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/hashsum/src/digest.rs b/src/uu/hashsum/src/digest.rs index 218de0a36..9093d94a7 100644 --- a/src/uu/hashsum/src/digest.rs +++ b/src/uu/hashsum/src/digest.rs @@ -1,4 +1,3 @@ -extern crate blake2_rfc; extern crate digest; extern crate md5; extern crate sha1; @@ -49,9 +48,9 @@ impl Digest for md5::Context { } } -impl Digest for blake2_rfc::blake2b::Blake2b { +impl Digest for blake2b_simd::State { fn new() -> Self { - blake2_rfc::blake2b::Blake2b::new(64) + Self::new() } fn input(&mut self, input: &[u8]) { @@ -59,12 +58,12 @@ impl Digest for blake2_rfc::blake2b::Blake2b { } fn result(&mut self, out: &mut [u8]) { - let hash_result = &self.clone().finalize(); - out.copy_from_slice(&hash_result.as_bytes()); + let hash_result = &self.finalize(); + out.copy_from_slice(hash_result.as_bytes()); } fn reset(&mut self) { - *self = blake2_rfc::blake2b::Blake2b::new(64); + *self = Self::new(); } fn output_bits(&self) -> usize { diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index ee7d2a0f7..d9feb6648 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -19,7 +19,6 @@ mod digest; use self::digest::Digest; -use blake2_rfc::blake2b::Blake2b; use clap::{App, Arg, ArgMatches}; use hex::ToHex; use md5::Context as Md5; @@ -51,14 +50,23 @@ struct Options { } fn is_custom_binary(program: &str) -> bool { - #[allow(clippy::match_like_matches_macro)] - // `matches!(...)` macro not stabilized until rust v1.42 - match program { - "md5sum" | "sha1sum" | "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum" - | "sha3sum" | "sha3-224sum" | "sha3-256sum" | "sha3-384sum" | "sha3-512sum" - | "shake128sum" | "shake256sum" | "b2sum" => true, - _ => false, - } + matches!( + program, + "md5sum" + | "sha1sum" + | "sha224sum" + | "sha256sum" + | "sha384sum" + | "sha512sum" + | "sha3sum" + | "sha3-224sum" + | "sha3-256sum" + | "sha3-384sum" + | "sha3-512sum" + | "shake128sum" + | "shake256sum" + | "b2sum" + ) } #[allow(clippy::cognitive_complexity)] @@ -76,9 +84,13 @@ fn detect_algo<'a>( "sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box, 256), "sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box, 384), "sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box, 512), - "b2sum" => ("BLAKE2", Box::new(Blake2b::new(64)) as Box, 512), + "b2sum" => ( + "BLAKE2", + Box::new(blake2b_simd::State::new()) as Box, + 512, + ), "sha3sum" => match matches.value_of("bits") { - Some(bits_str) => match usize::from_str_radix(&bits_str, 10) { + Some(bits_str) => match (bits_str).parse::() { Ok(224) => ( "SHA3-224", Box::new(Sha3_224::new()) as Box, @@ -128,7 +140,7 @@ fn detect_algo<'a>( 512, ), "shake128sum" => match matches.value_of("bits") { - Some(bits_str) => match usize::from_str_radix(&bits_str, 10) { + Some(bits_str) => match (bits_str).parse::() { Ok(bits) => ( "SHAKE128", Box::new(Shake128::new()) as Box, @@ -139,7 +151,7 @@ fn detect_algo<'a>( None => crash!(1, "--bits required for SHAKE-128"), }, "shake256sum" => match matches.value_of("bits") { - Some(bits_str) => match usize::from_str_radix(&bits_str, 10) { + Some(bits_str) => match (bits_str).parse::() { Ok(bits) => ( "SHAKE256", Box::new(Shake256::new()) as Box, @@ -178,11 +190,11 @@ fn detect_algo<'a>( set_or_crash("SHA512", Box::new(Sha512::new()), 512) } if matches.is_present("b2sum") { - set_or_crash("BLAKE2", Box::new(Blake2b::new(64)), 512) + set_or_crash("BLAKE2", Box::new(blake2b_simd::State::new()), 512) } if matches.is_present("sha3") { match matches.value_of("bits") { - Some(bits_str) => match usize::from_str_radix(&bits_str, 10) { + Some(bits_str) => match (bits_str).parse::() { Ok(224) => set_or_crash( "SHA3-224", Box::new(Sha3_224::new()) as Box, @@ -226,7 +238,7 @@ fn detect_algo<'a>( } if matches.is_present("shake128") { match matches.value_of("bits") { - Some(bits_str) => match usize::from_str_radix(&bits_str, 10) { + Some(bits_str) => match (bits_str).parse::() { Ok(bits) => set_or_crash("SHAKE128", Box::new(Shake128::new()), bits), Err(err) => crash!(1, "{}", err), }, @@ -235,7 +247,7 @@ fn detect_algo<'a>( } if matches.is_present("shake256") { match matches.value_of("bits") { - Some(bits_str) => match usize::from_str_radix(&bits_str, 10) { + Some(bits_str) => match (bits_str).parse::() { Ok(bits) => set_or_crash("SHAKE256", Box::new(Shake256::new()), bits), Err(err) => crash!(1, "{}", err), }, @@ -243,17 +255,15 @@ fn detect_algo<'a>( } } } - if alg.is_none() { - crash!(1, "You must specify hash algorithm!") - }; - (name, alg.unwrap(), output_bits) + let alg = alg.unwrap_or_else(|| crash!(1, "You must specify hash algorithm!")); + (name, alg, output_bits) } } } // TODO: return custom error type fn parse_bit_num(arg: &str) -> Result { - usize::from_str_radix(arg, 10) + arg.parse() } fn is_valid_bit_num(arg: String) -> Result<(), String> { @@ -275,119 +285,7 @@ pub fn uumain(mut args: impl uucore::Args) -> i32 { // Default binary in Windows, text mode otherwise let binary_flag_default = cfg!(windows); - let binary_help = format!( - "read in binary mode{}", - if binary_flag_default { - " (default)" - } else { - "" - } - ); - - let text_help = format!( - "read in text mode{}", - if binary_flag_default { - "" - } else { - " (default)" - } - ); - - let mut app = App::new(executable!()) - .version(crate_version!()) - .about("Compute and check message digests.") - .arg( - Arg::with_name("binary") - .short("b") - .long("binary") - .help(&binary_help), - ) - .arg( - Arg::with_name("check") - .short("c") - .long("check") - .help("read hashsums from the FILEs and check them"), - ) - .arg( - Arg::with_name("tag") - .long("tag") - .help("create a BSD-style checksum"), - ) - .arg( - Arg::with_name("text") - .short("t") - .long("text") - .help(&text_help) - .conflicts_with("binary"), - ) - .arg( - Arg::with_name("quiet") - .short("q") - .long("quiet") - .help("don't print OK for each successfully verified file"), - ) - .arg( - Arg::with_name("status") - .short("s") - .long("status") - .help("don't output anything, status code shows success"), - ) - .arg( - Arg::with_name("strict") - .long("strict") - .help("exit non-zero for improperly formatted checksum lines"), - ) - .arg( - Arg::with_name("warn") - .short("w") - .long("warn") - .help("warn about improperly formatted checksum lines"), - ) - // Needed for variable-length output sums (e.g. SHAKE) - .arg( - Arg::with_name("bits") - .long("bits") - .help("set the size of the output (only for SHAKE)") - .takes_value(true) - .value_name("BITS") - // XXX: should we actually use validators? they're not particularly efficient - .validator(is_valid_bit_num), - ) - .arg( - Arg::with_name("FILE") - .index(1) - .multiple(true) - .value_name("FILE"), - ); - - if !is_custom_binary(&binary_name) { - let algos = &[ - ("md5", "work with MD5"), - ("sha1", "work with SHA1"), - ("sha224", "work with SHA224"), - ("sha256", "work with SHA256"), - ("sha384", "work with SHA384"), - ("sha512", "work with SHA512"), - ("sha3", "work with SHA3"), - ("sha3-224", "work with SHA3-224"), - ("sha3-256", "work with SHA3-256"), - ("sha3-384", "work with SHA3-384"), - ("sha3-512", "work with SHA3-512"), - ( - "shake128", - "work with SHAKE128 using BITS for the output size", - ), - ( - "shake256", - "work with SHAKE256 using BITS for the output size", - ), - ("b2sum", "work with BLAKE2"), - ]; - - for (name, desc) in algos { - app = app.arg(Arg::with_name(name).long(name).help(desc)); - } - } + let app = uu_app(&binary_name); // FIXME: this should use get_matches_from_safe() and crash!(), but at the moment that just // causes "error: " to be printed twice (once from crash!() and once from clap). With @@ -435,6 +333,124 @@ pub fn uumain(mut args: impl uucore::Args) -> i32 { } } +pub fn uu_app_common() -> App<'static, 'static> { + #[cfg(windows)] + const BINARY_HELP: &str = "read in binary mode (default)"; + #[cfg(not(windows))] + const BINARY_HELP: &str = "read in binary mode"; + #[cfg(windows)] + const TEXT_HELP: &str = "read in text mode"; + #[cfg(not(windows))] + const TEXT_HELP: &str = "read in text mode (default)"; + App::new(executable!()) + .version(crate_version!()) + .about("Compute and check message digests.") + .arg( + Arg::with_name("binary") + .short("b") + .long("binary") + .help(BINARY_HELP), + ) + .arg( + Arg::with_name("check") + .short("c") + .long("check") + .help("read hashsums from the FILEs and check them"), + ) + .arg( + Arg::with_name("tag") + .long("tag") + .help("create a BSD-style checksum"), + ) + .arg( + Arg::with_name("text") + .short("t") + .long("text") + .help(TEXT_HELP) + .conflicts_with("binary"), + ) + .arg( + Arg::with_name("quiet") + .short("q") + .long("quiet") + .help("don't print OK for each successfully verified file"), + ) + .arg( + Arg::with_name("status") + .short("s") + .long("status") + .help("don't output anything, status code shows success"), + ) + .arg( + Arg::with_name("strict") + .long("strict") + .help("exit non-zero for improperly formatted checksum lines"), + ) + .arg( + Arg::with_name("warn") + .short("w") + .long("warn") + .help("warn about improperly formatted checksum lines"), + ) + // Needed for variable-length output sums (e.g. SHAKE) + .arg( + Arg::with_name("bits") + .long("bits") + .help("set the size of the output (only for SHAKE)") + .takes_value(true) + .value_name("BITS") + // XXX: should we actually use validators? they're not particularly efficient + .validator(is_valid_bit_num), + ) + .arg( + Arg::with_name("FILE") + .index(1) + .multiple(true) + .value_name("FILE"), + ) +} + +pub fn uu_app_custom() -> App<'static, 'static> { + let mut app = uu_app_common(); + let algorithms = &[ + ("md5", "work with MD5"), + ("sha1", "work with SHA1"), + ("sha224", "work with SHA224"), + ("sha256", "work with SHA256"), + ("sha384", "work with SHA384"), + ("sha512", "work with SHA512"), + ("sha3", "work with SHA3"), + ("sha3-224", "work with SHA3-224"), + ("sha3-256", "work with SHA3-256"), + ("sha3-384", "work with SHA3-384"), + ("sha3-512", "work with SHA3-512"), + ( + "shake128", + "work with SHAKE128 using BITS for the output size", + ), + ( + "shake256", + "work with SHAKE256 using BITS for the output size", + ), + ("b2sum", "work with BLAKE2"), + ]; + + for (name, desc) in algorithms { + app = app.arg(Arg::with_name(name).long(name).help(desc)); + } + app +} + +// hashsum is handled differently in build.rs, therefore this is not the same +// as in other utilities. +fn uu_app(binary_name: &str) -> App<'static, 'static> { + if !is_custom_binary(binary_name) { + uu_app_custom() + } else { + uu_app_common() + } +} + #[allow(clippy::cognitive_complexity)] fn hashsum<'a, I>(mut options: Options, files: I) -> Result<(), i32> where diff --git a/src/uu/hashsum/src/main.rs b/src/uu/hashsum/src/main.rs index 12bd3b393..bc4e2f3be 100644 --- a/src/uu/hashsum/src/main.rs +++ b/src/uu/hashsum/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_hashsum); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_hashsum); diff --git a/src/uu/head/Cargo.toml b/src/uu/head/Cargo.toml index adcce2726..661052f58 100644 --- a/src/uu/head/Cargo.toml +++ b/src/uu/head/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_head" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "head ~ (uutils) display the first lines of input" @@ -15,8 +15,8 @@ edition = "2018" path = "src/head.rs" [dependencies] -libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["ringbuffer"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/head/src/head.rs b/src/uu/head/src/head.rs index 56d7e8452..e17e17034 100644 --- a/src/uu/head/src/head.rs +++ b/src/uu/head/src/head.rs @@ -1,223 +1,605 @@ // * This file is part of the uutils coreutils package. // * -// * (c) Alan Andrade -// * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// * -// * Synced with: https://raw.github.com/avsm/src/master/usr.bin/head/head.c -#[macro_use] -extern crate uucore; +// spell-checker:ignore (vars) zlines -use std::collections::VecDeque; -use std::fs::File; -use std::io::{stdin, BufRead, BufReader, Read}; -use std::path::Path; -use std::str::from_utf8; +use clap::{crate_version, App, Arg}; +use std::convert::TryFrom; +use std::ffi::OsString; +use std::io::{self, ErrorKind, Read, Seek, SeekFrom, Write}; +use uucore::{crash, executable, show_error, show_error_custom_description}; -static SYNTAX: &str = ""; -static SUMMARY: &str = ""; -static LONG_HELP: &str = ""; +const EXIT_FAILURE: i32 = 1; +const EXIT_SUCCESS: i32 = 0; +const BUF_SIZE: usize = 65536; -enum FilterMode { - Bytes(usize), +const ABOUT: &str = "\ + Print the first 10 lines of each FILE to standard output.\n\ + With more than one FILE, precede each with a header giving the file name.\n\ + \n\ + With no FILE, or when FILE is -, read standard input.\n\ + \n\ + Mandatory arguments to long flags are mandatory for short flags too.\ + "; +const USAGE: &str = "head [FLAG]... [FILE]..."; + +mod options { + pub const BYTES_NAME: &str = "BYTES"; + pub const LINES_NAME: &str = "LINES"; + pub const QUIET_NAME: &str = "QUIET"; + pub const VERBOSE_NAME: &str = "VERBOSE"; + pub const ZERO_NAME: &str = "ZERO"; + pub const FILES_NAME: &str = "FILE"; +} +mod lines; +mod parse; +mod split; +mod take; +use lines::zlines; +use take::take_all_but; + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .usage(USAGE) + .arg( + Arg::with_name(options::BYTES_NAME) + .short("c") + .long("bytes") + .value_name("[-]NUM") + .takes_value(true) + .help( + "\ + print the first NUM bytes of each file;\n\ + with the leading '-', print all but the last\n\ + NUM bytes of each file\ + ", + ) + .overrides_with_all(&[options::BYTES_NAME, options::LINES_NAME]) + .allow_hyphen_values(true), + ) + .arg( + Arg::with_name(options::LINES_NAME) + .short("n") + .long("lines") + .value_name("[-]NUM") + .takes_value(true) + .help( + "\ + print the first NUM lines instead of the first 10;\n\ + with the leading '-', print all but the last\n\ + NUM lines of each file\ + ", + ) + .overrides_with_all(&[options::LINES_NAME, options::BYTES_NAME]) + .allow_hyphen_values(true), + ) + .arg( + Arg::with_name(options::QUIET_NAME) + .short("q") + .long("quiet") + .visible_alias("silent") + .help("never print headers giving file names") + .overrides_with_all(&[options::VERBOSE_NAME, options::QUIET_NAME]), + ) + .arg( + Arg::with_name(options::VERBOSE_NAME) + .short("v") + .long("verbose") + .help("always print headers giving file names") + .overrides_with_all(&[options::QUIET_NAME, options::VERBOSE_NAME]), + ) + .arg( + Arg::with_name(options::ZERO_NAME) + .short("z") + .long("zero-terminated") + .help("line delimiter is NUL, not newline") + .overrides_with(options::ZERO_NAME), + ) + .arg(Arg::with_name(options::FILES_NAME).multiple(true)) +} +#[derive(PartialEq, Debug, Clone, Copy)] +enum Modes { Lines(usize), - NLines(usize), + Bytes(usize), } -struct Settings { - mode: FilterMode, - verbose: bool, +fn parse_mode(src: &str, closure: F) -> Result<(Modes, bool), String> +where + F: FnOnce(usize) -> Modes, +{ + match parse::parse_num(src) { + Ok((n, last)) => Ok((closure(n), last)), + Err(e) => Err(e.to_string()), + } } -impl Default for Settings { - fn default() -> Settings { - Settings { - mode: FilterMode::Lines(10), - verbose: false, +fn arg_iterate<'a>( + mut args: impl uucore::Args + 'a, +) -> Result + 'a>, String> { + // argv[0] is always present + let first = args.next().unwrap(); + if let Some(second) = args.next() { + if let Some(s) = second.to_str() { + match parse::parse_obsolete(s) { + Some(Ok(iter)) => Ok(Box::new(vec![first].into_iter().chain(iter).chain(args))), + Some(Err(e)) => match e { + parse::ParseError::Syntax => Err(format!("bad argument format: '{}'", s)), + parse::ParseError::Overflow => Err(format!( + "invalid argument: '{}' Value too large for defined datatype", + s + )), + }, + None => Ok(Box::new(vec![first, second].into_iter().chain(args))), + } + } else { + Err("bad argument encoding".to_owned()) } + } else { + Ok(Box::new(vec![first].into_iter())) + } +} + +#[derive(Debug, PartialEq)] +struct HeadOptions { + pub quiet: bool, + pub verbose: bool, + pub zeroed: bool, + pub all_but_last: bool, + pub mode: Modes, + pub files: Vec, +} + +impl HeadOptions { + pub fn new() -> HeadOptions { + HeadOptions { + quiet: false, + verbose: false, + zeroed: false, + all_but_last: false, + mode: Modes::Lines(10), + files: Vec::new(), + } + } + + ///Construct options from matches + pub fn get_from(args: impl uucore::Args) -> Result { + let matches = uu_app().get_matches_from(arg_iterate(args)?); + + let mut options = HeadOptions::new(); + + options.quiet = matches.is_present(options::QUIET_NAME); + options.verbose = matches.is_present(options::VERBOSE_NAME); + options.zeroed = matches.is_present(options::ZERO_NAME); + + let mode_and_from_end = if let Some(v) = matches.value_of(options::BYTES_NAME) { + parse_mode(v, Modes::Bytes) + .map_err(|err| format!("invalid number of bytes: {}", err))? + } else if let Some(v) = matches.value_of(options::LINES_NAME) { + parse_mode(v, Modes::Lines) + .map_err(|err| format!("invalid number of lines: {}", err))? + } else { + (Modes::Lines(10), false) + }; + + options.mode = mode_and_from_end.0; + options.all_but_last = mode_and_from_end.1; + + options.files = match matches.values_of(options::FILES_NAME) { + Some(v) => v.map(|s| s.to_owned()).collect(), + None => vec!["-".to_owned()], + }; + //println!("{:#?}", options); + Ok(options) + } +} +// to make clippy shut up +impl Default for HeadOptions { + fn default() -> Self { + Self::new() + } +} + +fn read_n_bytes(input: R, n: usize) -> std::io::Result<()> +where + R: Read, +{ + // Read the first `n` bytes from the `input` reader. + let mut reader = input.take(n as u64); + + // Write those bytes to `stdout`. + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + + io::copy(&mut reader, &mut stdout)?; + + Ok(()) +} + +fn read_n_lines(input: &mut impl std::io::BufRead, n: usize, zero: bool) -> std::io::Result<()> { + if n == 0 { + return Ok(()); + } + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + let mut lines = 0usize; + split::walk_lines(input, zero, |e| match e { + split::Event::Data(dat) => { + stdout.write_all(dat)?; + Ok(true) + } + split::Event::Line => { + lines += 1; + if lines == n { + Ok(false) + } else { + Ok(true) + } + } + }) +} + +fn read_but_last_n_bytes(input: &mut impl std::io::BufRead, n: usize) -> std::io::Result<()> { + if n == 0 { + //prints everything + return read_n_bytes(input, std::usize::MAX); + } + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + + let mut ring_buffer = vec![0u8; n]; + + // first we fill the ring buffer + if let Err(e) = input.read_exact(&mut ring_buffer) { + if e.kind() == ErrorKind::UnexpectedEof { + return Ok(()); + } else { + return Err(e); + } + } + let mut buffer = [0u8; BUF_SIZE]; + loop { + let read = loop { + match input.read(&mut buffer) { + Ok(n) => break n, + Err(e) => match e.kind() { + ErrorKind::Interrupted => {} + _ => return Err(e), + }, + } + }; + if read == 0 { + return Ok(()); + } else if read >= n { + stdout.write_all(&ring_buffer)?; + stdout.write_all(&buffer[..read - n])?; + for i in 0..n { + ring_buffer[i] = buffer[read - n + i]; + } + } else { + stdout.write_all(&ring_buffer[..read])?; + for i in 0..n - read { + ring_buffer[i] = ring_buffer[read + i]; + } + ring_buffer[n - read..].copy_from_slice(&buffer[..read]); + } + } +} + +fn read_but_last_n_lines( + input: impl std::io::BufRead, + n: usize, + zero: bool, +) -> std::io::Result<()> { + if zero { + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + for bytes in take_all_but(zlines(input), n) { + stdout.write_all(&bytes?)?; + } + } else { + for line in take_all_but(input.lines(), n) { + println!("{}", line?); + } + } + Ok(()) +} + +fn head_backwards_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Result<()> { + assert!(options.all_but_last); + let size = input.seek(SeekFrom::End(0))?; + let size = usize::try_from(size).unwrap(); + match options.mode { + Modes::Bytes(n) => { + if n >= size { + return Ok(()); + } else { + input.seek(SeekFrom::Start(0))?; + read_n_bytes( + &mut std::io::BufReader::with_capacity(BUF_SIZE, input), + size - n, + )?; + } + } + Modes::Lines(n) => { + let mut buffer = [0u8; BUF_SIZE]; + let buffer = &mut buffer[..BUF_SIZE.min(size)]; + let mut i = 0usize; + let mut lines = 0usize; + + let found = 'o: loop { + // the casts here are ok, `buffer.len()` should never be above a few k + input.seek(SeekFrom::Current( + -((buffer.len() as i64).min((size - i) as i64)), + ))?; + input.read_exact(buffer)?; + for byte in buffer.iter().rev() { + match byte { + b'\n' if !options.zeroed => { + lines += 1; + } + 0u8 if options.zeroed => { + lines += 1; + } + _ => {} + } + // if it were just `n`, + if lines == n + 1 { + break 'o i; + } + i += 1; + } + if size - i == 0 { + return Ok(()); + } + }; + input.seek(SeekFrom::Start(0))?; + read_n_bytes( + &mut std::io::BufReader::with_capacity(BUF_SIZE, input), + size - found, + )?; + } + } + Ok(()) +} + +fn head_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Result<()> { + if options.all_but_last { + head_backwards_file(input, options) + } else { + match options.mode { + Modes::Bytes(n) => { + read_n_bytes(&mut std::io::BufReader::with_capacity(BUF_SIZE, input), n) + } + Modes::Lines(n) => read_n_lines( + &mut std::io::BufReader::with_capacity(BUF_SIZE, input), + n, + options.zeroed, + ), + } + } +} + +fn uu_head(options: &HeadOptions) -> Result<(), u32> { + let mut error_count = 0; + let mut first = true; + for file in &options.files { + let res = match file.as_str() { + "-" => { + if (options.files.len() > 1 && !options.quiet) || options.verbose { + if !first { + println!(); + } + println!("==> standard input <==") + } + let stdin = std::io::stdin(); + let mut stdin = stdin.lock(); + match options.mode { + Modes::Bytes(n) => { + if options.all_but_last { + read_but_last_n_bytes(&mut stdin, n) + } else { + read_n_bytes(&mut stdin, n) + } + } + Modes::Lines(n) => { + if options.all_but_last { + read_but_last_n_lines(&mut stdin, n, options.zeroed) + } else { + read_n_lines(&mut stdin, n, options.zeroed) + } + } + } + } + name => { + let mut file = match std::fs::File::open(name) { + Ok(f) => f, + Err(err) => { + let prefix = format!("cannot open '{}' for reading", name); + match err.kind() { + ErrorKind::NotFound => { + show_error_custom_description!(prefix, "No such file or directory"); + } + ErrorKind::PermissionDenied => { + show_error_custom_description!(prefix, "Permission denied"); + } + _ => { + show_error_custom_description!(prefix, "{}", err); + } + } + error_count += 1; + continue; + } + }; + if (options.files.len() > 1 && !options.quiet) || options.verbose { + if !first { + println!(); + } + println!("==> {} <==", name) + } + head_file(&mut file, options) + } + }; + if res.is_err() { + let name = if file.as_str() == "-" { + "standard input" + } else { + file + }; + let prefix = format!("error reading {}", name); + show_error_custom_description!(prefix, "Input/output error"); + error_count += 1; + } + first = false; + } + if error_count > 0 { + Err(error_count) + } else { + Ok(()) } } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - - let mut settings: Settings = Default::default(); - - // handle obsolete -number syntax - let new_args = match obsolete(&args[0..]) { - (args, Some(n)) => { - settings.mode = FilterMode::Lines(n); - args - } - (args, None) => args, - }; - - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optopt( - "c", - "bytes", - "Print the first K bytes. With the leading '-', print all but the last K bytes", - "[-]K", - ) - .optopt( - "n", - "lines", - "Print the first K lines. With the leading '-', print all but the last K lines", - "[-]K", - ) - .optflag("q", "quiet", "never print headers giving file names") - .optflag("v", "verbose", "always print headers giving file names") - .optflag("h", "help", "display this help and exit") - .optflag("V", "version", "output version information and exit") - .parse(new_args); - - let use_bytes = matches.opt_present("c"); - // TODO: suffixes (e.g. b, kB, etc.) - match matches.opt_str("n") { - Some(n) => { - if use_bytes { - show_error!("cannot specify both --bytes and --lines."); - return 1; - } - - match n.parse::() { - Ok(m) => { - settings.mode = if m < 0 { - let m: usize = m.abs() as usize; - FilterMode::NLines(m) - } else { - let m: usize = m.abs() as usize; - FilterMode::Lines(m) - } - } - Err(e) => { - show_error!("invalid line count '{}': {}", n, e); - return 1; - } - } - } - None => { - if let Some(count) = matches.opt_str("c") { - match count.parse::() { - Ok(m) => settings.mode = FilterMode::Bytes(m), - Err(e) => { - show_error!("invalid byte count '{}': {}", count, e); - return 1; - } - } - } + let args = match HeadOptions::get_from(args) { + Ok(o) => o, + Err(s) => { + crash!(EXIT_FAILURE, "{}", s); } }; - - let quiet = matches.opt_present("q"); - let verbose = matches.opt_present("v"); - let files = matches.free; - - // GNU implementation allows multiple declarations of "-q" and "-v" with the - // last flag winning. This can't be simulated with the getopts cargo unless - // we manually parse the arguments. Given the declaration of both flags, - // verbose mode always wins. This is a potential future improvement. - if files.len() > 1 && !quiet && !verbose { - settings.verbose = true; + match uu_head(&args) { + Ok(_) => EXIT_SUCCESS, + Err(_) => EXIT_FAILURE, } - if quiet { - settings.verbose = false; - } - if verbose { - settings.verbose = true; - } - - if files.is_empty() { - let mut buffer = BufReader::new(stdin()); - head(&mut buffer, &settings); - } else { - let mut first_time = true; - - for file in &files { - if settings.verbose { - if !first_time { - println!(); - } - println!("==> {} <==", file); - } - first_time = false; - - let path = Path::new(file); - let reader = File::open(&path).unwrap(); - let mut buffer = BufReader::new(reader); - if !head(&mut buffer, &settings) { - break; - } - } - } - - 0 } -// It searches for an option in the form of -123123 -// -// In case is found, the options vector will get rid of that object so that -// getopts works correctly. -fn obsolete(options: &[String]) -> (Vec, Option) { - let mut options: Vec = options.to_vec(); - let mut a = 1; - let b = options.len(); +#[cfg(test)] +mod tests { + use std::ffi::OsString; - while a < b { - let previous = options[a - 1].clone(); - let current = options[a].clone(); - let current = current.as_bytes(); - - if previous != "-n" && current.len() > 1 && current[0] == b'-' { - let len = current.len(); - for pos in 1..len { - // Ensure that the argument is only made out of digits - if !(current[pos] as char).is_numeric() { - break; - } - - // If this is the last number - if pos == len - 1 { - options.remove(a); - let number: Option = - from_utf8(¤t[1..len]).unwrap().parse::().ok(); - return (options, Some(number.unwrap())); - } - } - } - - a += 1; + use super::*; + fn options(args: &str) -> Result { + let combined = "head ".to_owned() + args; + let args = combined.split_whitespace(); + HeadOptions::get_from(args.map(|s| OsString::from(s))) } + #[test] + fn test_args_modes() { + let args = options("-n -10M -vz").unwrap(); + assert!(args.zeroed); + assert!(args.verbose); + assert!(args.all_but_last); + assert_eq!(args.mode, Modes::Lines(10 * 1024 * 1024)); + } + #[test] + fn test_gnu_compatibility() { + let args = options("-n 1 -c 1 -n 5 -c kiB -vqvqv").unwrap(); // spell-checker:disable-line + assert!(args.mode == Modes::Bytes(1024)); + assert!(args.verbose); + assert_eq!(options("-5").unwrap().mode, Modes::Lines(5)); + assert_eq!(options("-2b").unwrap().mode, Modes::Bytes(1024)); + assert_eq!(options("-5 -c 1").unwrap().mode, Modes::Bytes(1)); + } + #[test] + fn all_args_test() { + assert!(options("--silent").unwrap().quiet); + assert!(options("--quiet").unwrap().quiet); + assert!(options("-q").unwrap().quiet); + assert!(options("--verbose").unwrap().verbose); + assert!(options("-v").unwrap().verbose); + assert!(options("--zero-terminated").unwrap().zeroed); + assert!(options("-z").unwrap().zeroed); + assert_eq!(options("--lines 15").unwrap().mode, Modes::Lines(15)); + assert_eq!(options("-n 15").unwrap().mode, Modes::Lines(15)); + assert_eq!(options("--bytes 15").unwrap().mode, Modes::Bytes(15)); + assert_eq!(options("-c 15").unwrap().mode, Modes::Bytes(15)); + } + #[test] + fn test_options_errors() { + assert!(options("-n IsThisTheRealLife?").is_err()); + assert!(options("-c IsThisJustFantasy").is_err()); + } + #[test] + fn test_options_correct_defaults() { + let opts = HeadOptions::new(); + let opts2: HeadOptions = Default::default(); - (options, None) -} + assert_eq!(opts, opts2); -// TODO: handle errors on read -fn head(reader: &mut BufReader, settings: &Settings) -> bool { - match settings.mode { - FilterMode::Bytes(count) => { - for byte in reader.bytes().take(count) { - print!("{}", byte.unwrap() as char); - } - } - FilterMode::Lines(count) => { - for line in reader.lines().take(count) { - println!("{}", line.unwrap()); - } - } - FilterMode::NLines(count) => { - let mut vector: VecDeque = VecDeque::new(); - - for line in reader.lines() { - vector.push_back(line.unwrap()); - if vector.len() <= count { - continue; - } - println!("{}", vector.pop_front().unwrap()); + assert!(opts.verbose == false); + assert!(opts.quiet == false); + assert!(opts.zeroed == false); + assert!(opts.all_but_last == false); + assert_eq!(opts.mode, Modes::Lines(10)); + assert!(opts.files.is_empty()); + } + #[test] + fn test_parse_mode() { + assert_eq!( + parse_mode("123", Modes::Lines), + Ok((Modes::Lines(123), false)) + ); + assert_eq!( + parse_mode("-456", Modes::Bytes), + Ok((Modes::Bytes(456), true)) + ); + assert!(parse_mode("Nonsensical Nonsense", Modes::Bytes).is_err()); + #[cfg(target_pointer_width = "64")] + assert!(parse_mode("1Y", Modes::Lines).is_err()); + #[cfg(target_pointer_width = "32")] + assert!(parse_mode("1T", Modes::Bytes).is_err()); + } + fn arg_outputs(src: &str) -> Result { + let split = src.split_whitespace().map(|x| OsString::from(x)); + match arg_iterate(split) { + Ok(args) => { + let vec = args + .map(|s| s.to_str().unwrap().to_owned()) + .collect::>(); + Ok(vec.join(" ")) } + Err(e) => Err(e), } } - true + #[test] + fn test_arg_iterate() { + // test that normal args remain unchanged + assert_eq!( + arg_outputs("head -n -5 -zv"), + Ok("head -n -5 -zv".to_owned()) + ); + // tests that nonsensical args are unchanged + assert_eq!( + arg_outputs("head -to_be_or_not_to_be,..."), + Ok("head -to_be_or_not_to_be,...".to_owned()) + ); + //test that the obsolete syntax is unrolled + assert_eq!( + arg_outputs("head -123qvqvqzc"), // spell-checker:disable-line + Ok("head -q -z -c 123".to_owned()) + ); + //test that bad obsoletes are an error + assert!(arg_outputs("head -123FooBar").is_err()); + //test overflow + assert!(arg_outputs("head -100000000000000000000000000000000000000000").is_err()); + //test that empty args remain unchanged + assert_eq!(arg_outputs("head"), Ok("head".to_owned())); + } + #[test] + #[cfg(target_os = "linux")] + fn test_arg_iterate_bad_encoding() { + let invalid = unsafe { std::str::from_utf8_unchecked(b"\x80\x81") }; + // this arises from a conversion from OsString to &str + assert!( + arg_iterate(vec![OsString::from("head"), OsString::from(invalid)].into_iter()).is_err() + ); + } + #[test] + fn read_early_exit() { + let mut empty = std::io::BufReader::new(std::io::Cursor::new(Vec::new())); + assert!(read_n_bytes(&mut empty, 0).is_ok()); + assert!(read_n_lines(&mut empty, 0, false).is_ok()); + } } diff --git a/src/uu/head/src/lines.rs b/src/uu/head/src/lines.rs new file mode 100644 index 000000000..118aba17f --- /dev/null +++ b/src/uu/head/src/lines.rs @@ -0,0 +1,75 @@ +// spell-checker:ignore (vars) zline zlines + +//! Iterate over zero-terminated lines. +use std::io::BufRead; + +/// The zero byte, representing the null character. +const ZERO: u8 = 0; + +/// Returns an iterator over the lines of the given reader. +/// +/// The iterator returned from this function will yield instances of +/// [`io::Result`]<[`Vec`]<[`u8`]>>, representing the bytes of the line +/// *including* the null character (with the possible exception of the +/// last line, which may not have one). +/// +/// # Examples +/// +/// ```rust,ignore +/// use std::io::Cursor; +/// +/// let cursor = Cursor::new(b"x\0y\0z\0"); +/// let mut iter = zlines(cursor).map(|l| l.unwrap()); +/// assert_eq!(iter.next(), Some(b"x\0".to_vec())); +/// assert_eq!(iter.next(), Some(b"y\0".to_vec())); +/// assert_eq!(iter.next(), Some(b"z\0".to_vec())); +/// assert_eq!(iter.next(), None); +/// ``` +pub fn zlines(buf: B) -> ZLines { + ZLines { buf } +} + +/// An iterator over the zero-terminated lines of an instance of `BufRead`. +pub struct ZLines { + buf: B, +} + +impl Iterator for ZLines { + type Item = std::io::Result>; + + fn next(&mut self) -> Option>> { + let mut buf = Vec::new(); + match self.buf.read_until(ZERO, &mut buf) { + Ok(0) => None, + Ok(_) => Some(Ok(buf)), + Err(e) => Some(Err(e)), + } + } +} + +#[cfg(test)] +mod tests { + + use crate::lines::zlines; + use std::io::Cursor; + + #[test] + fn test_null_terminated() { + let cursor = Cursor::new(b"x\0y\0z\0"); + let mut iter = zlines(cursor).map(|l| l.unwrap()); + assert_eq!(iter.next(), Some(b"x\0".to_vec())); + assert_eq!(iter.next(), Some(b"y\0".to_vec())); + assert_eq!(iter.next(), Some(b"z\0".to_vec())); + assert_eq!(iter.next(), None); + } + + #[test] + fn test_not_null_terminated() { + let cursor = Cursor::new(b"x\0y\0z"); + let mut iter = zlines(cursor).map(|l| l.unwrap()); + assert_eq!(iter.next(), Some(b"x\0".to_vec())); + assert_eq!(iter.next(), Some(b"y\0".to_vec())); + assert_eq!(iter.next(), Some(b"z".to_vec())); + assert_eq!(iter.next(), None); + } +} diff --git a/src/uu/head/src/main.rs b/src/uu/head/src/main.rs index fbeb3381e..3e66a50d0 100644 --- a/src/uu/head/src/main.rs +++ b/src/uu/head/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_head); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_head); diff --git a/src/uu/head/src/parse.rs b/src/uu/head/src/parse.rs new file mode 100644 index 000000000..f6f291814 --- /dev/null +++ b/src/uu/head/src/parse.rs @@ -0,0 +1,182 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +use std::ffi::OsString; +use uucore::parse_size::{parse_size, ParseSizeError}; + +#[derive(PartialEq, Debug)] +pub enum ParseError { + Syntax, + Overflow, +} +/// Parses obsolete syntax +/// head -NUM[kmzv] // spell-checker:disable-line +pub fn parse_obsolete(src: &str) -> Option, ParseError>> { + let mut chars = src.char_indices(); + if let Some((_, '-')) = chars.next() { + let mut num_end = 0usize; + let mut has_num = false; + let mut last_char = 0 as char; + for (n, c) in &mut chars { + if c.is_numeric() { + has_num = true; + num_end = n; + } else { + last_char = c; + break; + } + } + if has_num { + match src[1..=num_end].parse::() { + Ok(num) => { + let mut quiet = false; + let mut verbose = false; + let mut zero_terminated = false; + let mut multiplier = None; + let mut c = last_char; + loop { + // not that here, we only match lower case 'k', 'c', and 'm' + match c { + // we want to preserve order + // this also saves us 1 heap allocation + 'q' => { + quiet = true; + verbose = false + } + 'v' => { + verbose = true; + quiet = false + } + 'z' => zero_terminated = true, + 'c' => multiplier = Some(1), + 'b' => multiplier = Some(512), + 'k' => multiplier = Some(1024), + 'm' => multiplier = Some(1024 * 1024), + '\0' => {} + _ => return Some(Err(ParseError::Syntax)), + } + if let Some((_, next)) = chars.next() { + c = next + } else { + break; + } + } + let mut options = Vec::new(); + if quiet { + options.push(OsString::from("-q")) + } + if verbose { + options.push(OsString::from("-v")) + } + if zero_terminated { + options.push(OsString::from("-z")) + } + if let Some(n) = multiplier { + options.push(OsString::from("-c")); + let num = match num.checked_mul(n) { + Some(n) => n, + None => return Some(Err(ParseError::Overflow)), + }; + options.push(OsString::from(format!("{}", num))); + } else { + options.push(OsString::from("-n")); + options.push(OsString::from(format!("{}", num))); + } + Some(Ok(options.into_iter())) + } + Err(_) => Some(Err(ParseError::Overflow)), + } + } else { + None + } + } else { + None + } +} +/// Parses an -c or -n argument, +/// the bool specifies whether to read from the end +pub fn parse_num(src: &str) -> Result<(usize, bool), ParseSizeError> { + let mut size_string = src.trim(); + let mut all_but_last = false; + + if let Some(c) = size_string.chars().next() { + if c == '+' || c == '-' { + // head: '+' is not documented (8.32 man pages) + size_string = &size_string[1..]; + if c == '-' { + all_but_last = true; + } + } + } else { + return Err(ParseSizeError::ParseFailure(src.to_string())); + } + + parse_size(size_string).map(|n| (n, all_but_last)) +} + +#[cfg(test)] +mod tests { + use super::*; + fn obsolete(src: &str) -> Option, ParseError>> { + let r = parse_obsolete(src); + match r { + Some(s) => match s { + Ok(v) => Some(Ok(v.map(|s| s.to_str().unwrap().to_owned()).collect())), + Err(e) => Some(Err(e)), + }, + None => None, + } + } + fn obsolete_result(src: &[&str]) -> Option, ParseError>> { + Some(Ok(src.iter().map(|s| s.to_string()).collect())) + } + #[test] + fn test_parse_numbers_obsolete() { + assert_eq!(obsolete("-5"), obsolete_result(&["-n", "5"])); + assert_eq!(obsolete("-100"), obsolete_result(&["-n", "100"])); + assert_eq!(obsolete("-5m"), obsolete_result(&["-c", "5242880"])); + assert_eq!(obsolete("-1k"), obsolete_result(&["-c", "1024"])); + assert_eq!(obsolete("-2b"), obsolete_result(&["-c", "1024"])); + assert_eq!(obsolete("-1mmk"), obsolete_result(&["-c", "1024"])); + assert_eq!(obsolete("-1vz"), obsolete_result(&["-v", "-z", "-n", "1"])); + assert_eq!( + obsolete("-1vzqvq"), // spell-checker:disable-line + obsolete_result(&["-q", "-z", "-n", "1"]) + ); + assert_eq!(obsolete("-1vzc"), obsolete_result(&["-v", "-z", "-c", "1"])); + assert_eq!( + obsolete("-105kzm"), + obsolete_result(&["-z", "-c", "110100480"]) + ); + } + #[test] + fn test_parse_errors_obsolete() { + assert_eq!(obsolete("-5n"), Some(Err(ParseError::Syntax))); + assert_eq!(obsolete("-5c5"), Some(Err(ParseError::Syntax))); + } + #[test] + fn test_parse_obsolete_no_match() { + assert_eq!(obsolete("-k"), None); + assert_eq!(obsolete("asd"), None); + } + #[test] + #[cfg(target_pointer_width = "64")] + fn test_parse_obsolete_overflow_x64() { + assert_eq!( + obsolete("-1000000000000000m"), + Some(Err(ParseError::Overflow)) + ); + assert_eq!( + obsolete("-10000000000000000000000"), + Some(Err(ParseError::Overflow)) + ); + } + #[test] + #[cfg(target_pointer_width = "32")] + fn test_parse_obsolete_overflow_x32() { + assert_eq!(obsolete("-42949672960"), Some(Err(ParseError::Overflow))); + assert_eq!(obsolete("-42949672k"), Some(Err(ParseError::Overflow))); + } +} diff --git a/src/uu/head/src/split.rs b/src/uu/head/src/split.rs new file mode 100644 index 000000000..9e9a0c685 --- /dev/null +++ b/src/uu/head/src/split.rs @@ -0,0 +1,60 @@ +#[derive(Debug)] +pub enum Event<'a> { + Data(&'a [u8]), + Line, +} +/// Loops over the lines read from a BufRead. +/// # Arguments +/// * `input` the ReadBuf to read from +/// * `zero` whether to use 0u8 as a line delimiter +/// * `on_event` a closure receiving some bytes read in a slice, or +/// event signalling a line was just read. +/// this is guaranteed to be signalled *directly* after the +/// slice containing the (CR on win)LF / 0 is passed +/// +/// Return whether to continue +pub fn walk_lines( + input: &mut impl std::io::BufRead, + zero: bool, + mut on_event: F, +) -> std::io::Result<()> +where + F: FnMut(Event) -> std::io::Result, +{ + let mut buffer = [0u8; super::BUF_SIZE]; + loop { + let read = loop { + match input.read(&mut buffer) { + Ok(n) => break n, + Err(e) => match e.kind() { + std::io::ErrorKind::Interrupted => {} + _ => return Err(e), + }, + } + }; + if read == 0 { + return Ok(()); + } + let mut base = 0usize; + for (i, byte) in buffer[..read].iter().enumerate() { + match byte { + b'\n' if !zero => { + on_event(Event::Data(&buffer[base..=i]))?; + base = i + 1; + if !on_event(Event::Line)? { + return Ok(()); + } + } + 0u8 if zero => { + on_event(Event::Data(&buffer[base..=i]))?; + base = i + 1; + if !on_event(Event::Line)? { + return Ok(()); + } + } + _ => {} + } + } + on_event(Event::Data(&buffer[base..read]))?; + } +} diff --git a/src/uu/head/src/take.rs b/src/uu/head/src/take.rs new file mode 100644 index 000000000..94fa012be --- /dev/null +++ b/src/uu/head/src/take.rs @@ -0,0 +1,93 @@ +//! Take all but the last elements of an iterator. +use uucore::ringbuffer::RingBuffer; + +/// Create an iterator over all but the last `n` elements of `iter`. +/// +/// # Examples +/// +/// ```rust,ignore +/// let data = [1, 2, 3, 4, 5]; +/// let n = 2; +/// let mut iter = take_all_but(data.iter(), n); +/// assert_eq!(Some(4), iter.next()); +/// assert_eq!(Some(5), iter.next()); +/// assert_eq!(None, iter.next()); +/// ``` +pub fn take_all_but(iter: I, n: usize) -> TakeAllBut { + TakeAllBut::new(iter, n) +} + +/// An iterator that only iterates over the last elements of another iterator. +pub struct TakeAllBut { + iter: I, + buf: RingBuffer<::Item>, +} + +impl TakeAllBut { + pub fn new(mut iter: I, n: usize) -> TakeAllBut { + // Create a new ring buffer and fill it up. + // + // If there are fewer than `n` elements in `iter`, then we + // exhaust the iterator so that whenever `TakeAllBut::next()` is + // called, it will return `None`, as expected. + let mut buf = RingBuffer::new(n); + for _ in 0..n { + let value = match iter.next() { + None => { + break; + } + Some(x) => x, + }; + buf.push_back(value); + } + TakeAllBut { iter, buf } + } +} + +impl Iterator for TakeAllBut +where + I: Iterator, +{ + type Item = ::Item; + + fn next(&mut self) -> Option<::Item> { + match self.iter.next() { + Some(value) => self.buf.push_back(value), + None => None, + } + } +} + +#[cfg(test)] +mod tests { + + use crate::take::take_all_but; + + #[test] + fn test_fewer_elements() { + let mut iter = take_all_but([0, 1, 2].iter(), 2); + assert_eq!(Some(&0), iter.next()); + assert_eq!(None, iter.next()); + } + + #[test] + fn test_same_number_of_elements() { + let mut iter = take_all_but([0, 1].iter(), 2); + assert_eq!(None, iter.next()); + } + + #[test] + fn test_more_elements() { + let mut iter = take_all_but([0].iter(), 2); + assert_eq!(None, iter.next()); + } + + #[test] + fn test_zero_elements() { + let mut iter = take_all_but([0, 1, 2].iter(), 0); + assert_eq!(Some(&0), iter.next()); + assert_eq!(Some(&1), iter.next()); + assert_eq!(Some(&2), iter.next()); + assert_eq!(None, iter.next()); + } +} diff --git a/src/uu/hostid/Cargo.toml b/src/uu/hostid/Cargo.toml index b3870652e..ab8b43f05 100644 --- a/src/uu/hostid/Cargo.toml +++ b/src/uu/hostid/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_hostid" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "hostid ~ (uutils) display the numeric identifier of the current host" @@ -15,8 +15,9 @@ edition = "2018" path = "src/hostid.rs" [dependencies] +clap = "2.33.3" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/hostid/src/hostid.rs b/src/uu/hostid/src/hostid.rs index 5a4909c62..e9fc08379 100644 --- a/src/uu/hostid/src/hostid.rs +++ b/src/uu/hostid/src/hostid.rs @@ -10,11 +10,10 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App}; use libc::c_long; static SYNTAX: &str = "[options]"; -static SUMMARY: &str = ""; -static LONG_HELP: &str = ""; // currently rust libc interface doesn't include gethostid extern "C" { @@ -22,11 +21,17 @@ extern "C" { } pub fn uumain(args: impl uucore::Args) -> i32 { - app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); + uu_app().get_matches_from(args); hostid(); 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .usage(SYNTAX) +} + fn hostid() { /* * POSIX says gethostid returns a "32-bit identifier" but is silent diff --git a/src/uu/hostid/src/main.rs b/src/uu/hostid/src/main.rs index 12b1178ec..9645ed4a6 100644 --- a/src/uu/hostid/src/main.rs +++ b/src/uu/hostid/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_hostid); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_hostid); diff --git a/src/uu/hostname/Cargo.toml b/src/uu/hostname/Cargo.toml index 75b9b5f9a..fb1d00682 100644 --- a/src/uu/hostname/Cargo.toml +++ b/src/uu/hostname/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_hostname" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "hostname ~ (uutils) display or set the host name of the current host" @@ -18,7 +18,7 @@ path = "src/hostname.rs" clap = "2.33" libc = "0.2.42" hostname = { version = "0.3", features = ["set"] } -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["wide"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["wide"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } winapi = { version="0.3", features=["sysinfoapi", "winsock2"] } diff --git a/src/uu/hostname/src/hostname.rs b/src/uu/hostname/src/hostname.rs index f37b6a74d..fe477d7b5 100644 --- a/src/uu/hostname/src/hostname.rs +++ b/src/uu/hostname/src/hostname.rs @@ -10,7 +10,7 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg, ArgMatches}; +use clap::{crate_version, App, Arg, ArgMatches}; use std::collections::hash_set::HashSet; use std::net::ToSocketAddrs; use std::str; @@ -21,7 +21,6 @@ use winapi::shared::minwindef::MAKEWORD; use winapi::um::winsock2::{WSACleanup, WSAStartup}; static ABOUT: &str = "Display or set the system's host name."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static OPT_DOMAIN: &str = "domain"; static OPT_IP_ADDRESS: &str = "ip-address"; @@ -53,10 +52,25 @@ fn get_usage() -> String { } fn execute(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + match matches.value_of(OPT_HOST) { + None => display_hostname(&matches), + Some(host) => { + if let Err(err) = hostname::set(host) { + show_error!("{}", err); + 1 + } else { + 0 + } + } + } +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(OPT_DOMAIN) .short("d") @@ -78,22 +92,9 @@ fn execute(args: impl uucore::Args) -> i32 { ) .arg(Arg::with_name(OPT_SHORT).short("s").long("short").help( "Display the short hostname (the portion before the first dot) if \ - possible", + possible", )) .arg(Arg::with_name(OPT_HOST)) - .get_matches_from(args); - - match matches.value_of(OPT_HOST) { - None => display_hostname(&matches), - Some(host) => { - if let Err(err) = hostname::set(host) { - show_error!("{}", err); - 1 - } else { - 0 - } - } - } } fn display_hostname(matches: &ArgMatches) -> i32 { diff --git a/src/uu/hostname/src/main.rs b/src/uu/hostname/src/main.rs index a483a8b1a..1d6e6733e 100644 --- a/src/uu/hostname/src/main.rs +++ b/src/uu/hostname/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_hostname); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_hostname); diff --git a/src/uu/id/Cargo.toml b/src/uu/id/Cargo.toml index 46e5cb578..308d6089d 100644 --- a/src/uu/id/Cargo.toml +++ b/src/uu/id/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_id" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "id ~ (uutils) display user and group information for USER" @@ -16,7 +16,7 @@ path = "src/id.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries", "process"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "process"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/id/src/id.rs b/src/uu/id/src/id.rs index f07a850fa..d5acc97f3 100644 --- a/src/uu/id/src/id.rs +++ b/src/uu/id/src/id.rs @@ -6,11 +6,27 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. // -// Synced with: +// This was originally based on BSD's `id` +// (noticeable in functionality, usage text, options text, etc.) +// and synced with: // http://ftp-archive.freebsd.org/mirror/FreeBSD-Archive/old-releases/i386/1.0-RELEASE/ports/shellutils/src/id.c // http://www.opensource.apple.com/source/shell_cmds/shell_cmds-118/id/id.c +// +// * This was partially rewritten in order for stdout/stderr/exit_code +// to be conform with GNU coreutils (8.32) test suite for `id`. +// +// * This supports multiple users (a feature that was introduced in coreutils 8.31) +// +// * This passes GNU's coreutils Test suite (8.32) +// for "tests/id/uid.sh" and "tests/id/zero/sh". +// +// * Option '--zero' does not exist for BSD's `id`, therefore '--zero' is only +// allowed together with other options that are available on GNU's `id`. +// +// * Help text based on BSD's `id` manpage and GNU's `id` manpage. +// -// spell-checker:ignore (ToDO) asid auditid auditinfo auid cstr egid emod euid getaudit getlogin gflag nflag pline rflag termid uflag +// spell-checker:ignore (ToDO) asid auditid auditinfo auid cstr egid emod euid getaudit getlogin gflag nflag pline rflag termid uflag gsflag zflag #![allow(non_camel_case_types)] #![allow(dead_code)] @@ -18,7 +34,7 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::ffi::CStr; use uucore::entries::{self, Group, Locate, Passwd}; pub use uucore::libc; @@ -31,211 +47,346 @@ macro_rules! cstr2cow { }; } -#[cfg(not(target_os = "linux"))] -mod audit { - use super::libc::{c_int, c_uint, dev_t, pid_t, uid_t}; +static ABOUT: &str = "Print user and group information for each specified USER, +or (when USER omitted) for the current user."; - pub type au_id_t = uid_t; - pub type au_asid_t = pid_t; - pub type au_event_t = c_uint; - pub type au_emod_t = c_uint; - pub type au_class_t = c_int; - pub type au_flag_t = u64; - - #[repr(C)] - pub struct au_mask { - pub am_success: c_uint, - pub am_failure: c_uint, - } - pub type au_mask_t = au_mask; - - #[repr(C)] - pub struct au_tid_addr { - pub port: dev_t, - } - pub type au_tid_addr_t = au_tid_addr; - - #[repr(C)] - pub struct c_auditinfo_addr { - pub ai_auid: au_id_t, // Audit user ID - pub ai_mask: au_mask_t, // Audit masks. - pub ai_termid: au_tid_addr_t, // Terminal ID. - pub ai_asid: au_asid_t, // Audit session ID. - pub ai_flags: au_flag_t, // Audit session flags - } - pub type c_auditinfo_addr_t = c_auditinfo_addr; - - extern "C" { - pub fn getaudit(auditinfo_addr: *mut c_auditinfo_addr_t) -> c_int; - } +mod options { + pub const OPT_AUDIT: &str = "audit"; // GNU's id does not have this + pub const OPT_CONTEXT: &str = "context"; + pub const OPT_EFFECTIVE_USER: &str = "user"; + pub const OPT_GROUP: &str = "group"; + pub const OPT_GROUPS: &str = "groups"; + pub const OPT_HUMAN_READABLE: &str = "human-readable"; // GNU's id does not have this + pub const OPT_NAME: &str = "name"; + pub const OPT_PASSWORD: &str = "password"; // GNU's id does not have this + pub const OPT_REAL_ID: &str = "real"; + pub const OPT_ZERO: &str = "zero"; // BSD's id does not have this + pub const ARG_USERS: &str = "USER"; } -static ABOUT: &str = "Display user and group information for the specified USER,\n or (when USER omitted) for the current user."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); - -static OPT_AUDIT: &str = "audit"; -static OPT_EFFECTIVE_USER: &str = "effective-user"; -static OPT_GROUP: &str = "group"; -static OPT_GROUPS: &str = "groups"; -static OPT_HUMAN_READABLE: &str = "human-readable"; -static OPT_NAME: &str = "name"; -static OPT_PASSWORD: &str = "password"; -static OPT_REAL_ID: &str = "real-id"; - -static ARG_USERS: &str = "users"; - fn get_usage() -> String { - format!("{0} [OPTION]... [USER]", executable!()) + format!("{0} [OPTION]... [USER]...", executable!()) +} + +fn get_description() -> String { + String::from( + "The id utility displays the user and group names and numeric IDs, of the \ + calling process, to the standard output. If the real and effective IDs are \ + different, both are displayed, otherwise only the real ID is displayed.\n\n\ + If a user (login name or user ID) is specified, the user and group IDs of \ + that user are displayed. In this case, the real and effective IDs are \ + assumed to be the same.", + ) +} + +struct Ids { + uid: u32, // user id + gid: u32, // group id + euid: u32, // effective uid + egid: u32, // effective gid +} + +struct State { + nflag: bool, // --name + uflag: bool, // --user + gflag: bool, // --group + gsflag: bool, // --groups + rflag: bool, // --real + zflag: bool, // --zero + ids: Option, + // The behavior for calling GNU's `id` and calling GNU's `id $USER` is similar but different. + // * The SELinux context is only displayed without a specified user. + // * The `getgroups` system call is only used without a specified user, this causes + // the order of the displayed groups to be different between `id` and `id $USER`. + // + // Example: + // $ strace -e getgroups id -G $USER + // 1000 10 975 968 + // +++ exited with 0 +++ + // $ strace -e getgroups id -G + // getgroups(0, NULL) = 4 + // getgroups(4, [10, 968, 975, 1000]) = 4 + // 1000 10 968 975 + // +++ exited with 0 +++ + user_specified: bool, } pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); + let after_help = get_description(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() .usage(&usage[..]) - .arg( - Arg::with_name(OPT_AUDIT) - .short("A") - .help("Display the process audit (not available on Linux)"), - ) - .arg( - Arg::with_name(OPT_EFFECTIVE_USER) - .short("u") - .long("user") - .help("Display the effective user ID as a number"), - ) - .arg( - Arg::with_name(OPT_GROUP) - .short("g") - .long(OPT_GROUP) - .help("Display the effective group ID as a number"), - ) - .arg( - Arg::with_name(OPT_GROUPS) - .short("G") - .long(OPT_GROUPS) - .help("Display the different group IDs"), - ) - .arg( - Arg::with_name(OPT_HUMAN_READABLE) - .short("p") - .help("Make the output human-readable"), - ) - .arg( - Arg::with_name(OPT_NAME) - .short("n") - .help("Display the name of the user or group ID for the -G, -g and -u options"), - ) - .arg( - Arg::with_name(OPT_PASSWORD) - .short("P") - .help("Display the id as a password file entry"), - ) - .arg( - Arg::with_name(OPT_REAL_ID) - .short("r") - .help("Display the real ID for the -g and -u options"), - ) - .arg(Arg::with_name(ARG_USERS).multiple(true).takes_value(true)) + .after_help(&after_help[..]) .get_matches_from(args); let users: Vec = matches - .values_of(ARG_USERS) + .values_of(options::ARG_USERS) .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); - if matches.is_present(OPT_AUDIT) { - auditid(); - return 0; + let mut state = State { + nflag: matches.is_present(options::OPT_NAME), + uflag: matches.is_present(options::OPT_EFFECTIVE_USER), + gflag: matches.is_present(options::OPT_GROUP), + gsflag: matches.is_present(options::OPT_GROUPS), + rflag: matches.is_present(options::OPT_REAL_ID), + zflag: matches.is_present(options::OPT_ZERO), + user_specified: !users.is_empty(), + ids: None, + }; + + let default_format = { + // "default format" is when none of '-ugG' was used + !(state.uflag || state.gflag || state.gsflag) + }; + + if (state.nflag || state.rflag) && default_format { + crash!(1, "cannot print only names or real IDs in default format"); + } + if (state.zflag) && default_format { + // NOTE: GNU test suite "id/zero.sh" needs this stderr output: + crash!(1, "option --zero not permitted in default format"); } - let possible_pw = if users.is_empty() { - None - } else { - match Passwd::locate(users[0].as_str()) { - Ok(p) => Some(p), - Err(_) => crash!(1, "No such user/group: {}", users[0]), + let delimiter = { + if state.zflag { + "\0".to_string() + } else { + " ".to_string() } }; - - let nflag = matches.is_present(OPT_NAME); - let uflag = matches.is_present(OPT_EFFECTIVE_USER); - let gflag = matches.is_present(OPT_GROUP); - let rflag = matches.is_present(OPT_REAL_ID); - - if gflag { - let id = possible_pw - .map(|p| p.gid()) - .unwrap_or(if rflag { getgid() } else { getegid() }); - println!( - "{}", - if nflag { - entries::gid2grp(id).unwrap_or_else(|_| id.to_string()) - } else { - id.to_string() - } - ); - return 0; - } - - if uflag { - let id = possible_pw - .map(|p| p.uid()) - .unwrap_or(if rflag { getuid() } else { geteuid() }); - println!( - "{}", - if nflag { - entries::uid2usr(id).unwrap_or_else(|_| id.to_string()) - } else { - id.to_string() - } - ); - return 0; - } - - if matches.is_present(OPT_GROUPS) { - println!( - "{}", - if nflag { - possible_pw - .map(|p| p.belongs_to()) - .unwrap_or_else(|| entries::get_groups().unwrap()) - .iter() - .map(|&id| entries::gid2grp(id).unwrap()) - .collect::>() - .join(" ") - } else { - possible_pw - .map(|p| p.belongs_to()) - .unwrap_or_else(|| entries::get_groups().unwrap()) - .iter() - .map(|&id| id.to_string()) - .collect::>() - .join(" ") - } - ); - return 0; - } - - if matches.is_present(OPT_PASSWORD) { - pline(possible_pw.map(|v| v.uid())); - return 0; + let line_ending = { + if state.zflag { + '\0' + } else { + '\n' + } }; + let mut exit_code = 0; - if matches.is_present(OPT_HUMAN_READABLE) { - pretty(possible_pw); - return 0; + for i in 0..=users.len() { + let possible_pw = if !state.user_specified { + None + } else { + match Passwd::locate(users[i].as_str()) { + Ok(p) => Some(p), + Err(_) => { + show_error!("'{}': no such user", users[i]); + exit_code = 1; + if i + 1 >= users.len() { + break; + } else { + continue; + } + } + } + }; + + // GNU's `id` does not support the flags: -p/-P/-A. + if matches.is_present(options::OPT_PASSWORD) { + // BSD's `id` ignores all but the first specified user + pline(possible_pw.map(|v| v.uid())); + return exit_code; + }; + if matches.is_present(options::OPT_HUMAN_READABLE) { + // BSD's `id` ignores all but the first specified user + pretty(possible_pw); + return exit_code; + } + if matches.is_present(options::OPT_AUDIT) { + // BSD's `id` ignores specified users + auditid(); + return exit_code; + } + + let (uid, gid) = possible_pw.map(|p| (p.uid(), p.gid())).unwrap_or(( + if state.rflag { getuid() } else { geteuid() }, + if state.rflag { getgid() } else { getegid() }, + )); + state.ids = Some(Ids { + uid, + gid, + euid: geteuid(), + egid: getegid(), + }); + + if state.gflag { + print!( + "{}", + if state.nflag { + entries::gid2grp(gid).unwrap_or_else(|_| { + show_error!("cannot find name for group ID {}", gid); + exit_code = 1; + gid.to_string() + }) + } else { + gid.to_string() + } + ); + } + + if state.uflag { + print!( + "{}", + if state.nflag { + entries::uid2usr(uid).unwrap_or_else(|_| { + show_error!("cannot find name for user ID {}", uid); + exit_code = 1; + uid.to_string() + }) + } else { + uid.to_string() + } + ); + } + + let groups = entries::get_groups_gnu(Some(gid)).unwrap(); + let groups = if state.user_specified { + possible_pw.map(|p| p.belongs_to()).unwrap() + } else { + groups.clone() + }; + + if state.gsflag { + print!( + "{}{}", + groups + .iter() + .map(|&id| { + if state.nflag { + entries::gid2grp(id).unwrap_or_else(|_| { + show_error!("cannot find name for group ID {}", id); + exit_code = 1; + id.to_string() + }) + } else { + id.to_string() + } + }) + .collect::>() + .join(&delimiter), + // NOTE: this is necessary to pass GNU's "tests/id/zero.sh": + if state.zflag && state.user_specified && users.len() > 1 { + "\0" + } else { + "" + } + ); + } + + if default_format { + id_print(&state, groups); + } + print!("{}", line_ending); + + if i + 1 >= users.len() { + break; + } } - if possible_pw.is_some() { - id_print(possible_pw, false, false) - } else { - id_print(possible_pw, true, true) - } + exit_code +} - 0 +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::OPT_AUDIT) + .short("A") + .conflicts_with_all(&[ + options::OPT_GROUP, + options::OPT_EFFECTIVE_USER, + options::OPT_HUMAN_READABLE, + options::OPT_PASSWORD, + options::OPT_GROUPS, + options::OPT_ZERO, + ]) + .help( + "Display the process audit user ID and other process audit properties,\n\ + which requires privilege (not available on Linux).", + ), + ) + .arg( + Arg::with_name(options::OPT_EFFECTIVE_USER) + .short("u") + .long(options::OPT_EFFECTIVE_USER) + .conflicts_with(options::OPT_GROUP) + .help("Display only the effective user ID as a number."), + ) + .arg( + Arg::with_name(options::OPT_GROUP) + .short("g") + .long(options::OPT_GROUP) + .help("Display only the effective group ID as a number"), + ) + .arg( + Arg::with_name(options::OPT_GROUPS) + .short("G") + .long(options::OPT_GROUPS) + .conflicts_with_all(&[ + options::OPT_GROUP, + options::OPT_EFFECTIVE_USER, + options::OPT_HUMAN_READABLE, + options::OPT_PASSWORD, + options::OPT_AUDIT, + ]) + .help( + "Display only the different group IDs as white-space separated numbers, \ + in no particular order.", + ), + ) + .arg( + Arg::with_name(options::OPT_HUMAN_READABLE) + .short("p") + .help("Make the output human-readable. Each display is on a separate line."), + ) + .arg( + Arg::with_name(options::OPT_NAME) + .short("n") + .long(options::OPT_NAME) + .help( + "Display the name of the user or group ID for the -G, -g and -u options \ + instead of the number.\nIf any of the ID numbers cannot be mapped into \ + names, the number will be displayed as usual.", + ), + ) + .arg( + Arg::with_name(options::OPT_PASSWORD) + .short("P") + .help("Display the id as a password file entry."), + ) + .arg( + Arg::with_name(options::OPT_REAL_ID) + .short("r") + .long(options::OPT_REAL_ID) + .help( + "Display the real ID for the -G, -g and -u options instead of \ + the effective ID.", + ), + ) + .arg( + Arg::with_name(options::OPT_ZERO) + .short("z") + .long(options::OPT_ZERO) + .help( + "delimit entries with NUL characters, not whitespace;\n\ + not permitted in default format", + ), + ) + .arg( + Arg::with_name(options::OPT_CONTEXT) + .short("Z") + .long(options::OPT_CONTEXT) + .help("NotImplemented: print only the security context of the process"), + ) + .arg( + Arg::with_name(options::ARG_USERS) + .multiple(true) + .takes_value(true) + .value_name(options::ARG_USERS), + ) } fn pretty(possible_pw: Option) { @@ -281,7 +432,7 @@ fn pretty(possible_pw: Option) { println!( "groups\t{}", - entries::get_groups() + entries::get_groups_gnu(None) .unwrap() .iter() .map(|&gr| entries::gid2grp(gr).unwrap()) @@ -291,7 +442,7 @@ fn pretty(possible_pw: Option) { } } -#[cfg(any(target_os = "macos", target_os = "freebsd"))] +#[cfg(any(target_vendor = "apple", target_os = "freebsd"))] fn pline(possible_uid: Option) { let uid = possible_uid.unwrap_or_else(getuid); let pw = Passwd::locate(uid).unwrap(); @@ -348,30 +499,21 @@ fn auditid() { println!("asid={}", auditinfo.ai_asid); } -fn id_print(possible_pw: Option, p_euid: bool, p_egid: bool) { - let (uid, gid) = possible_pw - .map(|p| (p.uid(), p.gid())) - .unwrap_or((getuid(), getgid())); - - let groups = match Passwd::locate(uid) { - Ok(p) => p.belongs_to(), - Err(e) => crash!(1, "Could not find uid {}: {}", uid, e), - }; +fn id_print(state: &State, groups: Vec) { + let uid = state.ids.as_ref().unwrap().uid; + let gid = state.ids.as_ref().unwrap().gid; + let euid = state.ids.as_ref().unwrap().euid; + let egid = state.ids.as_ref().unwrap().egid; print!("uid={}({})", uid, entries::uid2usr(uid).unwrap()); print!(" gid={}({})", gid, entries::gid2grp(gid).unwrap()); - - let euid = geteuid(); - if p_euid && (euid != uid) { + if !state.user_specified && (euid != uid) { print!(" euid={}({})", euid, entries::uid2usr(euid).unwrap()); } - - let egid = getegid(); - if p_egid && (egid != gid) { + if !state.user_specified && (egid != gid) { print!(" egid={}({})", euid, entries::gid2grp(egid).unwrap()); } - - println!( + print!( " groups={}", groups .iter() @@ -379,4 +521,49 @@ fn id_print(possible_pw: Option, p_euid: bool, p_egid: bool) { .collect::>() .join(",") ); + + // NOTE: (SELinux NotImplemented) placeholder: + // if !state.user_specified { + // // print SElinux context (does not depend on "-Z") + // print!(" context={}", get_selinux_contexts().join(":")); + // } +} + +#[cfg(not(target_os = "linux"))] +mod audit { + use super::libc::{c_int, c_uint, dev_t, pid_t, uid_t}; + + pub type au_id_t = uid_t; + pub type au_asid_t = pid_t; + pub type au_event_t = c_uint; + pub type au_emod_t = c_uint; + pub type au_class_t = c_int; + pub type au_flag_t = u64; + + #[repr(C)] + pub struct au_mask { + pub am_success: c_uint, + pub am_failure: c_uint, + } + pub type au_mask_t = au_mask; + + #[repr(C)] + pub struct au_tid_addr { + pub port: dev_t, + } + pub type au_tid_addr_t = au_tid_addr; + + #[repr(C)] + pub struct c_auditinfo_addr { + pub ai_auid: au_id_t, // Audit user ID + pub ai_mask: au_mask_t, // Audit masks. + pub ai_termid: au_tid_addr_t, // Terminal ID. + pub ai_asid: au_asid_t, // Audit session ID. + pub ai_flags: au_flag_t, // Audit session flags + } + pub type c_auditinfo_addr_t = c_auditinfo_addr; + + extern "C" { + pub fn getaudit(auditinfo_addr: *mut c_auditinfo_addr_t) -> c_int; + } } diff --git a/src/uu/id/src/main.rs b/src/uu/id/src/main.rs index 389e2deff..c8f6fe6aa 100644 --- a/src/uu/id/src/main.rs +++ b/src/uu/id/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_id); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_id); diff --git a/src/uu/install/Cargo.toml b/src/uu/install/Cargo.toml index 5280184fe..91463199a 100644 --- a/src/uu/install/Cargo.toml +++ b/src/uu/install/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_install" -version = "0.0.4" +version = "0.0.6" authors = [ "Ben Eills ", "uutils developers", @@ -19,8 +19,10 @@ path = "src/install.rs" [dependencies] clap = "2.33" +filetime = "0.2" +file_diff = "1.0.0" libc = ">= 0.2" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["mode", "perms", "entries"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["mode", "perms", "entries"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [dev-dependencies] diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index b41b16ef6..e45797750 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -12,17 +12,22 @@ mod mode; #[macro_use] extern crate uucore; -use clap::{App, Arg, ArgMatches}; +use clap::{crate_version, App, Arg, ArgMatches}; +use file_diff::diff; +use filetime::{set_file_times, FileTime}; use uucore::entries::{grp2gid, usr2uid}; use uucore::perms::{wrap_chgrp, wrap_chown, Verbosity}; +use libc::{getegid, geteuid}; use std::fs; use std::fs::File; use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; +use std::process::Command; use std::result::Result; const DEFAULT_MODE: u32 = 0o755; +const DEFAULT_STRIP_PROGRAM: &str = "strip"; #[allow(dead_code)] pub struct Behavior { @@ -32,6 +37,12 @@ pub struct Behavior { owner: String, group: String, verbose: bool, + preserve_timestamps: bool, + compare: bool, + strip: bool, + strip_program: String, + create_leading: bool, + target_dir: Option, } #[derive(Clone, Eq, PartialEq)] @@ -54,14 +65,13 @@ impl Behavior { static ABOUT: &str = "Copy SOURCE to DEST or multiple SOURCE(s) to the existing DIRECTORY, while setting permission modes and owner/group"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static OPT_COMPARE: &str = "compare"; static OPT_BACKUP: &str = "backup"; static OPT_BACKUP_2: &str = "backup2"; static OPT_DIRECTORY: &str = "directory"; static OPT_IGNORED: &str = "ignored"; -static OPT_CREATED: &str = "created"; +static OPT_CREATE_LEADING: &str = "create-leading"; static OPT_GROUP: &str = "group"; static OPT_MODE: &str = "mode"; static OPT_OWNER: &str = "owner"; @@ -88,10 +98,35 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let paths: Vec = matches + .values_of(ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + if let Err(s) = check_unimplemented(&matches) { + show_error!("Unimplemented feature: {}", s); + return 2; + } + + let behavior = match behavior(&matches) { + Ok(x) => x, + Err(ret) => { + return ret; + } + }; + + match behavior.main_function { + MainFunction::Directory => directory(paths, behavior), + MainFunction::Standard => standard(paths, behavior), + } +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(OPT_BACKUP) .long(OPT_BACKUP) @@ -110,11 +145,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .help("ignored") ) .arg( - // TODO implement flag Arg::with_name(OPT_COMPARE) .short("C") .long(OPT_COMPARE) - .help("(unimplemented) compare each pair of source and destination files, and in some cases, do not modify the destination at all") + .help("compare each pair of source and destination files, and in some cases, do not modify the destination at all") ) .arg( Arg::with_name(OPT_DIRECTORY) @@ -125,9 +159,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .arg( // TODO implement flag - Arg::with_name(OPT_CREATED) + Arg::with_name(OPT_CREATE_LEADING) .short("D") - .help("(unimplemented) create all leading components of DEST except the last, then copy SOURCE to DEST") + .help("create all leading components of DEST except the last, then copy SOURCE to DEST") ) .arg( Arg::with_name(OPT_GROUP) @@ -154,24 +188,21 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .takes_value(true) ) .arg( - // TODO implement flag Arg::with_name(OPT_PRESERVE_TIMESTAMPS) .short("p") .long(OPT_PRESERVE_TIMESTAMPS) - .help("(unimplemented) apply access/modification times of SOURCE files to corresponding destination files") + .help("apply access/modification times of SOURCE files to corresponding destination files") ) .arg( - // TODO implement flag Arg::with_name(OPT_STRIP) .short("s") .long(OPT_STRIP) - .help("(unimplemented) strip symbol tables") + .help("strip symbol tables (no action Windows)") ) .arg( - // TODO implement flag Arg::with_name(OPT_STRIP_PROGRAM) .long(OPT_STRIP_PROGRAM) - .help("(unimplemented) program used to strip binaries") + .help("program used to strip binaries (no action Windows)") .value_name("PROGRAM") ) .arg( @@ -189,7 +220,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { Arg::with_name(OPT_TARGET_DIRECTORY) .short("t") .long(OPT_TARGET_DIRECTORY) - .help("(unimplemented) move all SOURCE arguments into DIRECTORY") + .help("move all SOURCE arguments into DIRECTORY") .value_name("DIRECTORY") ) .arg( @@ -222,29 +253,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .value_name("CONTEXT") ) .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true).min_values(1)) - .get_matches_from(args); - - let paths: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - if let Err(s) = check_unimplemented(&matches) { - show_error!("Unimplemented feature: {}", s); - return 2; - } - - let behavior = match behavior(&matches) { - Ok(x) => x, - Err(ret) => { - return ret; - } - }; - - match behavior.main_function { - MainFunction::Directory => directory(paths, behavior), - MainFunction::Standard => standard(paths, behavior), - } } /// Check for unimplemented command line arguments. @@ -261,20 +269,8 @@ fn check_unimplemented<'a>(matches: &ArgMatches) -> Result<(), &'a str> { Err("--backup") } else if matches.is_present(OPT_BACKUP_2) { Err("-b") - } else if matches.is_present(OPT_COMPARE) { - Err("--compare, -C") - } else if matches.is_present(OPT_CREATED) { - Err("-D") - } else if matches.is_present(OPT_PRESERVE_TIMESTAMPS) { - Err("--preserve-timestamps, -p") - } else if matches.is_present(OPT_STRIP) { - Err("--strip, -s") - } else if matches.is_present(OPT_STRIP_PROGRAM) { - Err("--strip-program") } else if matches.is_present(OPT_SUFFIX) { Err("--suffix, -S") - } else if matches.is_present(OPT_TARGET_DIRECTORY) { - Err("--target-directory, -t") } else if matches.is_present(OPT_NO_TARGET_DIRECTORY) { Err("--no-target-directory, -T") } else if matches.is_present(OPT_PRESERVE_CONTEXT) { @@ -304,33 +300,23 @@ fn behavior(matches: &ArgMatches) -> Result { let considering_dir: bool = MainFunction::Directory == main_function; let specified_mode: Option = if matches.is_present(OPT_MODE) { - match matches.value_of(OPT_MODE) { - Some(x) => match mode::parse(&x[..], considering_dir) { - Ok(y) => Some(y), - Err(err) => { - show_error!("Invalid mode string: {}", err); - return Err(1); - } - }, - None => { - return Err(1); - } - } + let x = matches.value_of(OPT_MODE).ok_or(1)?; + Some(mode::parse(x, considering_dir).map_err(|err| { + show_error!("Invalid mode string: {}", err); + 1 + })?) } else { None }; let backup_suffix = if matches.is_present(OPT_SUFFIX) { - match matches.value_of(OPT_SUFFIX) { - Some(x) => x, - None => { - return Err(1); - } - } + matches.value_of(OPT_SUFFIX).ok_or(1)? } else { "~" }; + let target_dir = matches.value_of(OPT_TARGET_DIRECTORY).map(|d| d.to_owned()); + Ok(Behavior { main_function, specified_mode, @@ -338,6 +324,16 @@ fn behavior(matches: &ArgMatches) -> Result { owner: matches.value_of(OPT_OWNER).unwrap_or("").to_string(), group: matches.value_of(OPT_GROUP).unwrap_or("").to_string(), verbose: matches.is_present(OPT_VERBOSE), + preserve_timestamps: matches.is_present(OPT_PRESERVE_TIMESTAMPS), + compare: matches.is_present(OPT_COMPARE), + strip: matches.is_present(OPT_STRIP), + strip_program: String::from( + matches + .value_of(OPT_STRIP_PROGRAM) + .unwrap_or(DEFAULT_STRIP_PROGRAM), + ), + create_leading: matches.is_present(OPT_CREATE_LEADING), + target_dir, }) } @@ -355,23 +351,29 @@ fn directory(paths: Vec, b: Behavior) -> i32 { } else { let mut all_successful = true; - for directory in paths.iter() { - let path = Path::new(directory); - + for path in paths.iter().map(Path::new) { // if the path already exist, don't try to create it again if !path.exists() { - if let Err(e) = fs::create_dir(directory) { - show_info!("{}: {}", path.display(), e.to_string()); + // Differently than the primary functionality (MainFunction::Standard), the directory + // functionality should create all ancestors (or components) of a directory regardless + // of the presence of the "-D" flag. + // NOTE: the GNU "install" sets the expected mode only for the target directory. All + // created ancestor directories will have the default mode. Hence it is safe to use + // fs::create_dir_all and then only modify the target's dir mode. + if let Err(e) = fs::create_dir_all(path) { + show_error!("{}: {}", path.display(), e); all_successful = false; + continue; + } + + if b.verbose { + show_error!("creating directory '{}'", path.display()); } } - if mode::chmod(&path, b.mode()).is_err() { + if mode::chmod(path, b.mode()).is_err() { all_successful = false; - } - - if b.verbose { - show_info!("created directory '{}'", path.display()); + continue; } } if all_successful { @@ -394,17 +396,41 @@ fn is_new_file_path(path: &Path) -> bool { /// /// Returns an integer intended as a program return code. /// -fn standard(paths: Vec, b: Behavior) -> i32 { - let sources = &paths[0..paths.len() - 1] - .iter() - .map(PathBuf::from) - .collect::>(); - let target = Path::new(paths.last().unwrap()); +fn standard(mut paths: Vec, b: Behavior) -> i32 { + let target: PathBuf = b + .target_dir + .clone() + .unwrap_or_else(|| paths.pop().unwrap()) + .into(); - if (target.is_file() || is_new_file_path(target)) && sources.len() == 1 { - copy_file_to_file(&sources[0], &target.to_path_buf(), &b) + let sources = &paths.iter().map(PathBuf::from).collect::>(); + + if sources.len() > 1 || (target.exists() && target.is_dir()) { + copy_files_into_dir(sources, &target, &b) } else { - copy_files_into_dir(sources, &target.to_path_buf(), &b) + if let Some(parent) = target.parent() { + if !parent.exists() && b.create_leading { + if let Err(e) = fs::create_dir_all(parent) { + show_error!("failed to create {}: {}", parent.display(), e); + return 1; + } + + if mode::chmod(parent, b.mode()).is_err() { + show_error!("failed to chmod {}", parent.display()); + return 1; + } + } + } + + if target.is_file() || is_new_file_path(&target) { + copy_file_to_file(&sources[0], &target, &b) + } else { + show_error!( + "invalid target {}: No such file or directory", + target.display() + ); + 1 + } } } @@ -418,7 +444,7 @@ fn standard(paths: Vec, b: Behavior) -> i32 { /// _files_ must all exist as non-directories. /// _target_dir_ must be a directory. /// -fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> i32 { +fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> i32 { if !target_dir.is_dir() { show_error!("target '{}' is not a directory", target_dir.display()); return 1; @@ -426,18 +452,25 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> let mut all_successful = true; for sourcepath in files.iter() { - let targetpath = match sourcepath.as_os_str().to_str() { - Some(name) => target_dir.join(name), - None => { - show_error!( - "cannot stat '{}': No such file or directory", - sourcepath.display() - ); + if !sourcepath.exists() { + show_error!( + "cannot stat '{}': No such file or directory", + sourcepath.display() + ); - all_successful = false; - continue; - } - }; + all_successful = false; + continue; + } + + if sourcepath.is_dir() { + show_error!("omitting directory '{}'", sourcepath.display()); + all_successful = false; + continue; + } + + let mut targetpath = target_dir.to_path_buf(); + let filename = sourcepath.components().last().unwrap(); + targetpath.push(filename); if copy(sourcepath, &targetpath, b).is_err() { all_successful = false; @@ -460,8 +493,8 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> /// _file_ must exist as a non-directory. /// _target_ must be a non-directory /// -fn copy_file_to_file(file: &PathBuf, target: &PathBuf, b: &Behavior) -> i32 { - if copy(file, &target, b).is_err() { +fn copy_file_to_file(file: &Path, target: &Path, b: &Behavior) -> i32 { + if copy(file, target, b).is_err() { 1 } else { 0 @@ -479,7 +512,12 @@ fn copy_file_to_file(file: &PathBuf, target: &PathBuf, b: &Behavior) -> i32 { /// /// If the copy system call fails, we print a verbose error and return an empty error value. /// -fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { +#[allow(clippy::cognitive_complexity)] +fn copy(from: &Path, to: &Path, b: &Behavior) -> Result<(), ()> { + if b.compare && !need_copy(from, to, b) { + return Ok(()); + } + if from.to_string_lossy() == "/dev/null" { /* workaround a limitation of fs::copy * https://github.com/rust-lang/rust/issues/79390 @@ -503,7 +541,22 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { return Err(()); } - if mode::chmod(&to, b.mode()).is_err() { + if b.strip && cfg!(not(windows)) { + match Command::new(&b.strip_program).arg(to).output() { + Ok(o) => { + if !o.status.success() { + crash!( + 1, + "strip program failed: {}", + String::from_utf8(o.stderr).unwrap_or_default() + ); + } + } + Err(e) => crash!(1, "strip program execution failed: {}", e), + } + } + + if mode::chmod(to, b.mode()).is_err() { return Err(()); } @@ -519,7 +572,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { }; let gid = meta.gid(); match wrap_chown( - to.as_path(), + to, &meta, Some(owner_id), Some(gid), @@ -528,10 +581,10 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { ) { Ok(n) => { if !n.is_empty() { - show_info!("{}", n); + show_error!("{}", n); } } - Err(e) => show_info!("{}", e), + Err(e) => show_error!("{}", e), } } @@ -545,19 +598,112 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { Ok(g) => g, _ => crash!(1, "no such group: {}", b.group), }; - match wrap_chgrp(to.as_path(), &meta, group_id, false, Verbosity::Normal) { + match wrap_chgrp(to, &meta, group_id, false, Verbosity::Normal) { Ok(n) => { if !n.is_empty() { - show_info!("{}", n); + show_error!("{}", n); } } - Err(e) => show_info!("{}", e), + Err(e) => show_error!("{}", e), + } + } + + if b.preserve_timestamps { + let meta = match fs::metadata(from) { + Ok(meta) => meta, + Err(f) => crash!(1, "{}", f.to_string()), + }; + + let modified_time = FileTime::from_last_modification_time(&meta); + let accessed_time = FileTime::from_last_access_time(&meta); + + match set_file_times(to, accessed_time, modified_time) { + Ok(_) => {} + Err(e) => show_error!("{}", e), } } if b.verbose { - show_info!("'{}' -> '{}'", from.display(), to.display()); + show_error!("'{}' -> '{}'", from.display(), to.display()); } Ok(()) } + +/// Return true if a file is necessary to copy. This is the case when: +/// - _from_ or _to_ is nonexistent; +/// - either file has a sticky bit or set[ug]id bit, or the user specified one; +/// - either file isn't a regular file; +/// - the sizes of _from_ and _to_ differ; +/// - _to_'s owner differs from intended; or +/// - the contents of _from_ and _to_ differ. +/// +/// # Parameters +/// +/// _from_ and _to_, if existent, must be non-directories. +/// +/// # Errors +/// +/// Crashes the program if a nonexistent owner or group is specified in _b_. +/// +fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool { + let from_meta = match fs::metadata(from) { + Ok(meta) => meta, + Err(_) => return true, + }; + let to_meta = match fs::metadata(to) { + Ok(meta) => meta, + Err(_) => return true, + }; + + // setuid || setgid || sticky + let extra_mode: u32 = 0o7000; + + if b.specified_mode.unwrap_or(0) & extra_mode != 0 + || from_meta.mode() & extra_mode != 0 + || to_meta.mode() & extra_mode != 0 + { + return true; + } + + if !from_meta.is_file() || !to_meta.is_file() { + return true; + } + + if from_meta.len() != to_meta.len() { + return true; + } + + // TODO: if -P (#1809) and from/to contexts mismatch, return true. + + if !b.owner.is_empty() { + let owner_id = match usr2uid(&b.owner) { + Ok(id) => id, + _ => crash!(1, "no such user: {}", b.owner), + }; + if owner_id != to_meta.uid() { + return true; + } + } else if !b.group.is_empty() { + let group_id = match grp2gid(&b.group) { + Ok(id) => id, + _ => crash!(1, "no such group: {}", b.group), + }; + if group_id != to_meta.gid() { + return true; + } + } else { + #[cfg(not(target_os = "windows"))] + unsafe { + if to_meta.uid() != geteuid() || to_meta.gid() != getegid() { + return true; + } + } + } + + if !diff(from.to_str().unwrap(), to.to_str().unwrap()) { + return true; + } + + false +} diff --git a/src/uu/install/src/main.rs b/src/uu/install/src/main.rs index 289079daf..d296ec4a2 100644 --- a/src/uu/install/src/main.rs +++ b/src/uu/install/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_install); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_install); diff --git a/src/uu/install/src/mode.rs b/src/uu/install/src/mode.rs index a3de40c68..b8d5cd839 100644 --- a/src/uu/install/src/mode.rs +++ b/src/uu/install/src/mode.rs @@ -23,7 +23,7 @@ pub fn parse(mode_string: &str, considering_dir: bool) -> Result { pub fn chmod(path: &Path, mode: u32) -> Result<(), ()> { use std::os::unix::fs::PermissionsExt; fs::set_permissions(path, fs::Permissions::from_mode(mode)).map_err(|err| { - show_info!("{}: chmod failed with error {}", path.display(), err); + show_error!("{}: chmod failed with error {}", path.display(), err); }) } diff --git a/src/uu/join/Cargo.toml b/src/uu/join/Cargo.toml index d02703a62..9371b7601 100644 --- a/src/uu/join/Cargo.toml +++ b/src/uu/join/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_join" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "join ~ (uutils) merge lines from inputs with matching join fields" @@ -16,7 +16,7 @@ path = "src/join.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/join/src/join.rs b/src/uu/join/src/join.rs index d02a54eb3..60721f212 100644 --- a/src/uu/join/src/join.rs +++ b/src/uu/join/src/join.rs @@ -10,13 +10,12 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::cmp::{min, Ordering}; use std::fs::File; use std::io::{stdin, BufRead, BufReader, Lines, Stdin}; static NAME: &str = "join"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); #[derive(Copy, Clone, PartialEq)] enum FileNum { @@ -329,8 +328,8 @@ impl<'a> State<'a> { }); } else { repr.print_field(key); - repr.print_fields(&line1, self.key, self.max_fields); - repr.print_fields(&line2, other.key, other.max_fields); + repr.print_fields(line1, self.key, self.max_fields); + repr.print_fields(line2, other.key, other.max_fields); } println!(); @@ -443,8 +442,73 @@ impl<'a> State<'a> { } pub fn uumain(args: impl uucore::Args) -> i32 { - let matches = App::new(NAME) - .version(VERSION) + let matches = uu_app().get_matches_from(args); + + let keys = parse_field_number_option(matches.value_of("j")); + let key1 = parse_field_number_option(matches.value_of("1")); + let key2 = parse_field_number_option(matches.value_of("2")); + + let mut settings: Settings = Default::default(); + + if let Some(value) = matches.value_of("v") { + settings.print_unpaired = parse_file_number(value); + settings.print_joined = false; + } else if let Some(value) = matches.value_of("a") { + settings.print_unpaired = parse_file_number(value); + } + + settings.ignore_case = matches.is_present("i"); + settings.key1 = get_field_number(keys, key1); + settings.key2 = get_field_number(keys, key2); + + if let Some(value) = matches.value_of("t") { + settings.separator = match value.len() { + 0 => Sep::Line, + 1 => Sep::Char(value.chars().next().unwrap()), + _ => crash!(1, "multi-character tab {}", value), + }; + } + + if let Some(format) = matches.value_of("o") { + if format == "auto" { + settings.autoformat = true; + } else { + settings.format = format + .split(|c| c == ' ' || c == ',' || c == '\t') + .map(Spec::parse) + .collect(); + } + } + + if let Some(empty) = matches.value_of("e") { + settings.empty = empty.to_string(); + } + + if matches.is_present("nocheck-order") { + settings.check_order = CheckOrder::Disabled; + } + + if matches.is_present("check-order") { + settings.check_order = CheckOrder::Enabled; + } + + if matches.is_present("header") { + settings.headers = true; + } + + let file1 = matches.value_of("file1").unwrap(); + let file2 = matches.value_of("file2").unwrap(); + + if file1 == "-" && file2 == "-" { + crash!(1, "both files cannot be standard input"); + } + + exec(file1, file2, &settings) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(NAME) + .version(crate_version!()) .about( "For each pair of input lines with identical join fields, write a line to standard output. The default join field is the first, delimited by blanks. @@ -543,68 +607,6 @@ FILENUM is 1 or 2, corresponding to FILE1 or FILE2", .value_name("FILE2") .hidden(true), ) - .get_matches_from(args); - - let keys = parse_field_number_option(matches.value_of("j")); - let key1 = parse_field_number_option(matches.value_of("1")); - let key2 = parse_field_number_option(matches.value_of("2")); - - let mut settings: Settings = Default::default(); - - if let Some(value) = matches.value_of("v") { - settings.print_unpaired = parse_file_number(value); - settings.print_joined = false; - } else if let Some(value) = matches.value_of("a") { - settings.print_unpaired = parse_file_number(value); - } - - settings.ignore_case = matches.is_present("i"); - settings.key1 = get_field_number(keys, key1); - settings.key2 = get_field_number(keys, key2); - - if let Some(value) = matches.value_of("t") { - settings.separator = match value.len() { - 0 => Sep::Line, - 1 => Sep::Char(value.chars().next().unwrap()), - _ => crash!(1, "multi-character tab {}", value), - }; - } - - if let Some(format) = matches.value_of("o") { - if format == "auto" { - settings.autoformat = true; - } else { - settings.format = format - .split(|c| c == ' ' || c == ',' || c == '\t') - .map(Spec::parse) - .collect(); - } - } - - if let Some(empty) = matches.value_of("e") { - settings.empty = empty.to_string(); - } - - if matches.is_present("nocheck-order") { - settings.check_order = CheckOrder::Disabled; - } - - if matches.is_present("check-order") { - settings.check_order = CheckOrder::Enabled; - } - - if matches.is_present("header") { - settings.headers = true; - } - - let file1 = matches.value_of("file1").unwrap(); - let file2 = matches.value_of("file2").unwrap(); - - if file1 == "-" && file2 == "-" { - crash!(1, "both files cannot be standard input"); - } - - exec(file1, file2, &settings) } fn exec(file1: &str, file2: &str, settings: &Settings) -> i32 { @@ -612,7 +614,7 @@ fn exec(file1: &str, file2: &str, settings: &Settings) -> i32 { let mut state1 = State::new( FileNum::File1, - &file1, + file1, &stdin, settings.key1, settings.print_unpaired, @@ -620,7 +622,7 @@ fn exec(file1: &str, file2: &str, settings: &Settings) -> i32 { let mut state2 = State::new( FileNum::File2, - &file2, + file2, &stdin, settings.key2, settings.print_unpaired, diff --git a/src/uu/join/src/main.rs b/src/uu/join/src/main.rs index 75be18875..5114252cd 100644 --- a/src/uu/join/src/main.rs +++ b/src/uu/join/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_join); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_join); diff --git a/src/uu/kill/Cargo.toml b/src/uu/kill/Cargo.toml index dd69cd35d..e33411c70 100644 --- a/src/uu/kill/Cargo.toml +++ b/src/uu/kill/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_kill" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "kill ~ (uutils) send a signal to a process" @@ -15,8 +15,9 @@ edition = "2018" path = "src/kill.rs" [dependencies] +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["signals"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["signals"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index 9af9c74f7..92868efdb 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -10,17 +10,25 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use libc::{c_int, pid_t}; use std::io::Error; use uucore::signals::ALL_SIGNALS; +use uucore::InvalidEncodingHandling; -static SYNTAX: &str = "[options] [...]"; -static SUMMARY: &str = ""; -static LONG_HELP: &str = ""; +static ABOUT: &str = "Send signal to processes or list information about signals."; static EXIT_OK: i32 = 0; static EXIT_ERR: i32 = 1; +pub mod options { + pub static PIDS_OR_SIGNALS: &str = "pids_of_signals"; + pub static LIST: &str = "list"; + pub static TABLE: &str = "table"; + pub static TABLE_OLD: &str = "table_old"; + pub static SIGNAL: &str = "signal"; +} + #[derive(Clone, Copy)] pub enum Mode { Kill, @@ -29,42 +37,75 @@ pub enum Mode { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); let (args, obs_signal) = handle_obsolete(args); - let matches = app!(SYNTAX, SUMMARY, LONG_HELP) - .optopt("s", "signal", "specify the to be sent", "SIGNAL") - .optflagopt( - "l", - "list", - "list all signal names, or convert one to a name", - "LIST", - ) - .optflag("L", "table", "list all signal names in a nice table") - .parse(args); - let mode = if matches.opt_present("table") { + let usage = format!("{} [OPTIONS]... PID...", executable!()); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let mode = if matches.is_present(options::TABLE) || matches.is_present(options::TABLE_OLD) { Mode::Table - } else if matches.opt_present("list") { + } else if matches.is_present(options::LIST) { Mode::List } else { Mode::Kill }; + let pids_or_signals: Vec = matches + .values_of(options::PIDS_OR_SIGNALS) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + match mode { Mode::Kill => { - return kill( - &matches - .opt_str("signal") - .unwrap_or_else(|| obs_signal.unwrap_or_else(|| "9".to_owned())), - matches.free, - ) + let sig = match (obs_signal, matches.value_of(options::SIGNAL)) { + (Some(s), Some(_)) => s, // -s takes precedence + (Some(s), None) => s, + (None, Some(s)) => s.to_owned(), + (None, None) => "TERM".to_owned(), + }; + return kill(&sig, &pids_or_signals); } Mode::Table => table(), - Mode::List => list(matches.opt_str("list")), + Mode::List => list(pids_or_signals.get(0).cloned()), } - 0 + EXIT_OK +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::LIST) + .short("l") + .long(options::LIST) + .help("Lists signals") + .conflicts_with(options::TABLE) + .conflicts_with(options::TABLE_OLD), + ) + .arg( + Arg::with_name(options::TABLE) + .short("t") + .long(options::TABLE) + .help("Lists table of signals"), + ) + .arg(Arg::with_name(options::TABLE_OLD).short("L").hidden(true)) + .arg( + Arg::with_name(options::SIGNAL) + .short("s") + .long(options::SIGNAL) + .help("Sends given signal") + .takes_value(true), + ) + .arg( + Arg::with_name(options::PIDS_OR_SIGNALS) + .hidden(true) + .multiple(true), + ) } fn handle_obsolete(mut args: Vec) -> (Vec, Option) { @@ -72,7 +113,7 @@ fn handle_obsolete(mut args: Vec) -> (Vec, Option) { while i < args.len() { // this is safe because slice is valid when it is referenced let slice = &args[i].clone(); - if slice.starts_with('-') && slice.len() > 1 && slice.chars().nth(1).unwrap().is_digit(10) { + if slice.starts_with('-') && slice.chars().nth(1).map_or(false, |c| c.is_digit(10)) { let val = &slice[1..]; match val.parse() { Ok(num) => { @@ -90,33 +131,24 @@ fn handle_obsolete(mut args: Vec) -> (Vec, Option) { } fn table() { - let mut name_width = 0; - /* Compute the maximum width of a signal name. */ - for s in &ALL_SIGNALS { - if s.name.len() > name_width { - name_width = s.name.len() - } - } + let name_width = ALL_SIGNALS.iter().map(|n| n.len()).max().unwrap(); for (idx, signal) in ALL_SIGNALS.iter().enumerate() { - print!("{0: >#2} {1: <#8}", idx + 1, signal.name); - //TODO: obtain max signal width here - + print!("{0: >#2} {1: <#2$}", idx, signal, name_width + 2); if (idx + 1) % 7 == 0 { println!(); } } + println!() } fn print_signal(signal_name_or_value: &str) { - for signal in &ALL_SIGNALS { - if signal.name == signal_name_or_value - || (format!("SIG{}", signal.name)) == signal_name_or_value - { - println!("{}", signal.value); + for (value, &signal) in ALL_SIGNALS.iter().enumerate() { + if signal == signal_name_or_value || (format!("SIG{}", signal)) == signal_name_or_value { + println!("{}", value); exit!(EXIT_OK as i32) - } else if signal_name_or_value == signal.value.to_string() { - println!("{}", signal.name); + } else if signal_name_or_value == value.to_string() { + println!("{}", signal); exit!(EXIT_OK as i32) } } @@ -126,8 +158,8 @@ fn print_signal(signal_name_or_value: &str) { fn print_signals() { let mut pos = 0; for (idx, signal) in ALL_SIGNALS.iter().enumerate() { - pos += signal.name.len(); - print!("{}", signal.name); + pos += signal.len(); + print!("{}", signal); if idx > 0 && pos > 73 { println!(); pos = 0; @@ -145,14 +177,14 @@ fn list(arg: Option) { }; } -fn kill(signalname: &str, pids: std::vec::Vec) -> i32 { +fn kill(signalname: &str, pids: &[String]) -> i32 { let mut status = 0; let optional_signal_value = uucore::signals::signal_by_name_or_value(signalname); let signal_value = match optional_signal_value { Some(x) => x, None => crash!(EXIT_ERR, "unknown signal name {}", signalname), }; - for pid in &pids { + for pid in pids { match pid.parse::() { Ok(x) => { if unsafe { libc::kill(x as pid_t, signal_value as c_int) } != 0 { diff --git a/src/uu/kill/src/main.rs b/src/uu/kill/src/main.rs index c9f639967..91d0a28f0 100644 --- a/src/uu/kill/src/main.rs +++ b/src/uu/kill/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_kill); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_kill); diff --git a/src/uu/link/Cargo.toml b/src/uu/link/Cargo.toml index aaa183981..14a6ac7c9 100644 --- a/src/uu/link/Cargo.toml +++ b/src/uu/link/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_link" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "link ~ (uutils) create a hard (file system) link to FILE" @@ -16,8 +16,9 @@ path = "src/link.rs" [dependencies] libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +clap = "2.33" [[bin]] name = "link" diff --git a/src/uu/link/src/link.rs b/src/uu/link/src/link.rs index 51c7acb5f..ad7702044 100644 --- a/src/uu/link/src/link.rs +++ b/src/uu/link/src/link.rs @@ -8,13 +8,20 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::fs::hard_link; use std::io::Error; use std::path::Path; -static SYNTAX: &str = "[OPTIONS] FILE1 FILE2"; -static SUMMARY: &str = "Create a link named FILE2 to FILE1"; -static LONG_HELP: &str = ""; +static ABOUT: &str = "Call the link function to create a link named FILE2 to an existing FILE1."; + +pub mod options { + pub static FILES: &str = "FILES"; +} + +fn get_usage() -> String { + format!("{0} FILE1 FILE2", executable!()) +} pub fn normalize_error_message(e: Error) -> String { match e.raw_os_error() { @@ -24,13 +31,15 @@ pub fn normalize_error_message(e: Error) -> String { } pub fn uumain(args: impl uucore::Args) -> i32 { - let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); - if matches.free.len() != 2 { - crash!(1, "{}", msg_wrong_number_of_arguments!(2)); - } + let usage = get_usage(); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - let old = Path::new(&matches.free[0]); - let new = Path::new(&matches.free[1]); + let files: Vec<_> = matches + .values_of_os(options::FILES) + .unwrap_or_default() + .collect(); + let old = Path::new(files[0]); + let new = Path::new(files[1]); match hard_link(old, new) { Ok(_) => 0, @@ -40,3 +49,17 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::FILES) + .hidden(true) + .required(true) + .min_values(2) + .max_values(2) + .takes_value(true), + ) +} diff --git a/src/uu/link/src/main.rs b/src/uu/link/src/main.rs index d837b4278..ccc565fed 100644 --- a/src/uu/link/src/main.rs +++ b/src/uu/link/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_link); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_link); diff --git a/src/uu/ln/Cargo.toml b/src/uu/ln/Cargo.toml index 2b97f025a..c19d8fb52 100644 --- a/src/uu/ln/Cargo.toml +++ b/src/uu/ln/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_ln" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "ln ~ (uutils) create a (file system) link to TARGET" @@ -17,7 +17,7 @@ path = "src/ln.rs" [dependencies] clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/ln/src/ln.rs b/src/uu/ln/src/ln.rs index ab07a2b08..b08eba97a 100644 --- a/src/uu/ln/src/ln.rs +++ b/src/uu/ln/src/ln.rs @@ -10,7 +10,7 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::borrow::Cow; use std::ffi::OsStr; @@ -27,7 +27,6 @@ use uucore::fs::{canonicalize, CanonicalizeMode}; pub struct Settings { overwrite: OverwriteMode, backup: BackupMode, - force: bool, suffix: String, symbolic: bool, relative: bool, @@ -54,7 +53,7 @@ pub enum BackupMode { fn get_usage() -> String { format!( - "{0} [OPTION]... [-T] TARGET LINK_executable!() (1st form) + "{0} [OPTION]... [-T] TARGET LINK_NAME (1st form) {0} [OPTION]... TARGET (2nd form) {0} [OPTION]... TARGET... DIRECTORY (3rd form) {0} [OPTION]... -t DIRECTORY TARGET... (4th form)", @@ -64,7 +63,7 @@ fn get_usage() -> String { fn get_long_usage() -> String { String::from( - " In the 1st form, create a link to TARGET with the name LINK_executable!(). + " In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in DIRECTORY. Create hard links by default, symbolic links with --symbolic. @@ -77,19 +76,20 @@ fn get_long_usage() -> String { } static ABOUT: &str = "change file owner and group"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); -static OPT_B: &str = "b"; -static OPT_BACKUP: &str = "backup"; -static OPT_FORCE: &str = "force"; -static OPT_INTERACTIVE: &str = "interactive"; -static OPT_NO_DEREFERENCE: &str = "no-dereference"; -static OPT_SYMBOLIC: &str = "symbolic"; -static OPT_SUFFIX: &str = "suffix"; -static OPT_TARGET_DIRECTORY: &str = "target-directory"; -static OPT_NO_TARGET_DIRECTORY: &str = "no-target-directory"; -static OPT_RELATIVE: &str = "relative"; -static OPT_VERBOSE: &str = "verbose"; +mod options { + pub const B: &str = "b"; + pub const BACKUP: &str = "backup"; + pub const FORCE: &str = "force"; + pub const INTERACTIVE: &str = "interactive"; + pub const NO_DEREFERENCE: &str = "no-dereference"; + pub const SYMBOLIC: &str = "symbolic"; + pub const SUFFIX: &str = "suffix"; + pub const TARGET_DIRECTORY: &str = "target-directory"; + pub const NO_TARGET_DIRECTORY: &str = "no-target-directory"; + pub const RELATIVE: &str = "relative"; + pub const VERBOSE: &str = "verbose"; +} static ARG_FILES: &str = "files"; @@ -97,109 +97,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let long_usage = get_long_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() .usage(&usage[..]) .after_help(&long_usage[..]) - .arg(Arg::with_name(OPT_B).short(OPT_B).help( - "make a backup of each file that would otherwise be overwritten or \ - removed", - )) - .arg( - Arg::with_name(OPT_BACKUP) - .long(OPT_BACKUP) - .help( - "make a backup of each file that would otherwise be overwritten \ - or removed", - ) - .takes_value(true) - .possible_value("simple") - .possible_value("never") - .possible_value("numbered") - .possible_value("t") - .possible_value("existing") - .possible_value("nil") - .possible_value("none") - .possible_value("off") - .value_name("METHOD"), - ) - // TODO: opts.arg( - // Arg::with_name(("d", "directory", "allow users with appropriate privileges to attempt \ - // to make hard links to directories"); - .arg( - Arg::with_name(OPT_FORCE) - .short("f") - .long(OPT_FORCE) - .help("remove existing destination files"), - ) - .arg( - Arg::with_name(OPT_INTERACTIVE) - .short("i") - .long(OPT_INTERACTIVE) - .help("prompt whether to remove existing destination files"), - ) - .arg( - Arg::with_name(OPT_NO_DEREFERENCE) - .short("n") - .long(OPT_NO_DEREFERENCE) - .help( - "treat LINK_executable!() as a normal file if it is a \ - symbolic link to a directory", - ), - ) - // TODO: opts.arg( - // Arg::with_name(("L", "logical", "dereference TARGETs that are symbolic links"); - // - // TODO: opts.arg( - // Arg::with_name(("P", "physical", "make hard links directly to symbolic links"); - .arg( - Arg::with_name(OPT_SYMBOLIC) - .short("s") - .long("symbolic") - .help("make symbolic links instead of hard links"), - ) - .arg( - Arg::with_name(OPT_SUFFIX) - .short("S") - .long(OPT_SUFFIX) - .help("override the usual backup suffix") - .value_name("SUFFIX") - .takes_value(true), - ) - .arg( - Arg::with_name(OPT_TARGET_DIRECTORY) - .short("t") - .long(OPT_TARGET_DIRECTORY) - .help("specify the DIRECTORY in which to create the links") - .value_name("DIRECTORY") - .conflicts_with(OPT_NO_TARGET_DIRECTORY), - ) - .arg( - Arg::with_name(OPT_NO_TARGET_DIRECTORY) - .short("T") - .long(OPT_NO_TARGET_DIRECTORY) - .help("treat LINK_executable!() as a normal file always"), - ) - .arg( - Arg::with_name(OPT_RELATIVE) - .short("r") - .long(OPT_RELATIVE) - .help("create symbolic links relative to link location"), - ) - .arg( - Arg::with_name(OPT_VERBOSE) - .short("v") - .long(OPT_VERBOSE) - .help("print name of each linked file"), - ) - .arg( - Arg::with_name(ARG_FILES) - .multiple(true) - .takes_value(true) - .required(true) - .min_values(1), - ) .get_matches_from(args); /* the list of files */ @@ -210,20 +110,20 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .map(PathBuf::from) .collect(); - let overwrite_mode = if matches.is_present(OPT_FORCE) { + let overwrite_mode = if matches.is_present(options::FORCE) { OverwriteMode::Force - } else if matches.is_present(OPT_INTERACTIVE) { + } else if matches.is_present(options::INTERACTIVE) { OverwriteMode::Interactive } else { OverwriteMode::NoClobber }; - let backup_mode = if matches.is_present(OPT_B) { + let backup_mode = if matches.is_present(options::B) { BackupMode::ExistingBackup - } else if matches.is_present(OPT_BACKUP) { - match matches.value_of(OPT_BACKUP) { + } else if matches.is_present(options::BACKUP) { + match matches.value_of(options::BACKUP) { None => BackupMode::ExistingBackup, - Some(mode) => match &mode[..] { + Some(mode) => match mode { "simple" | "never" => BackupMode::SimpleBackup, "numbered" | "t" => BackupMode::NumberedBackup, "existing" | "nil" => BackupMode::ExistingBackup, @@ -235,8 +135,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 { BackupMode::NoBackup }; - let backup_suffix = if matches.is_present(OPT_SUFFIX) { - matches.value_of(OPT_SUFFIX).unwrap() + let backup_suffix = if matches.is_present(options::SUFFIX) { + matches.value_of(options::SUFFIX).unwrap() } else { "~" }; @@ -244,34 +144,137 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let settings = Settings { overwrite: overwrite_mode, backup: backup_mode, - force: matches.is_present(OPT_FORCE), suffix: backup_suffix.to_string(), - symbolic: matches.is_present(OPT_SYMBOLIC), - relative: matches.is_present(OPT_RELATIVE), - target_dir: matches.value_of(OPT_TARGET_DIRECTORY).map(String::from), - no_target_dir: matches.is_present(OPT_NO_TARGET_DIRECTORY), - no_dereference: matches.is_present(OPT_NO_DEREFERENCE), - verbose: matches.is_present(OPT_VERBOSE), + symbolic: matches.is_present(options::SYMBOLIC), + relative: matches.is_present(options::RELATIVE), + target_dir: matches + .value_of(options::TARGET_DIRECTORY) + .map(String::from), + no_target_dir: matches.is_present(options::NO_TARGET_DIRECTORY), + no_dereference: matches.is_present(options::NO_DEREFERENCE), + verbose: matches.is_present(options::VERBOSE), }; exec(&paths[..], &settings) } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg(Arg::with_name(options::B).short(options::B).help( + "make a backup of each file that would otherwise be overwritten or \ + removed", + )) + .arg( + Arg::with_name(options::BACKUP) + .long(options::BACKUP) + .help( + "make a backup of each file that would otherwise be overwritten \ + or removed", + ) + .takes_value(true) + .possible_values(&[ + "simple", "never", "numbered", "t", "existing", "nil", "none", "off", + ]) + .value_name("METHOD"), + ) + // TODO: opts.arg( + // Arg::with_name(("d", "directory", "allow users with appropriate privileges to attempt \ + // to make hard links to directories"); + .arg( + Arg::with_name(options::FORCE) + .short("f") + .long(options::FORCE) + .help("remove existing destination files"), + ) + .arg( + Arg::with_name(options::INTERACTIVE) + .short("i") + .long(options::INTERACTIVE) + .help("prompt whether to remove existing destination files"), + ) + .arg( + Arg::with_name(options::NO_DEREFERENCE) + .short("n") + .long(options::NO_DEREFERENCE) + .help( + "treat LINK_NAME as a normal file if it is a \ + symbolic link to a directory", + ), + ) + // TODO: opts.arg( + // Arg::with_name(("L", "logical", "dereference TARGETs that are symbolic links"); + // + // TODO: opts.arg( + // Arg::with_name(("P", "physical", "make hard links directly to symbolic links"); + .arg( + Arg::with_name(options::SYMBOLIC) + .short("s") + .long("symbolic") + .help("make symbolic links instead of hard links") + // override added for https://github.com/uutils/coreutils/issues/2359 + .overrides_with(options::SYMBOLIC), + ) + .arg( + Arg::with_name(options::SUFFIX) + .short("S") + .long(options::SUFFIX) + .help("override the usual backup suffix") + .value_name("SUFFIX") + .takes_value(true), + ) + .arg( + Arg::with_name(options::TARGET_DIRECTORY) + .short("t") + .long(options::TARGET_DIRECTORY) + .help("specify the DIRECTORY in which to create the links") + .value_name("DIRECTORY") + .conflicts_with(options::NO_TARGET_DIRECTORY), + ) + .arg( + Arg::with_name(options::NO_TARGET_DIRECTORY) + .short("T") + .long(options::NO_TARGET_DIRECTORY) + .help("treat LINK_NAME as a normal file always"), + ) + .arg( + Arg::with_name(options::RELATIVE) + .short("r") + .long(options::RELATIVE) + .help("create symbolic links relative to link location") + .requires(options::SYMBOLIC), + ) + .arg( + Arg::with_name(options::VERBOSE) + .short("v") + .long(options::VERBOSE) + .help("print name of each linked file"), + ) + .arg( + Arg::with_name(ARG_FILES) + .multiple(true) + .takes_value(true) + .required(true) + .min_values(1), + ) +} + fn exec(files: &[PathBuf], settings: &Settings) -> i32 { // Handle cases where we create links in a directory first. if let Some(ref name) = settings.target_dir { // 4th form: a directory is specified by -t. - return link_files_in_dir(files, &PathBuf::from(name), &settings); + return link_files_in_dir(files, &PathBuf::from(name), settings); } if !settings.no_target_dir { if files.len() == 1 { // 2nd form: the target directory is the current directory. - return link_files_in_dir(files, &PathBuf::from("."), &settings); + return link_files_in_dir(files, &PathBuf::from("."), settings); } let last_file = &PathBuf::from(files.last().unwrap()); if files.len() > 2 || last_file.is_dir() { // 3rd form: create links in the last argument. - return link_files_in_dir(&files[0..files.len() - 1], last_file, &settings); + return link_files_in_dir(&files[0..files.len() - 1], last_file, settings); } } @@ -303,7 +306,7 @@ fn exec(files: &[PathBuf], settings: &Settings) -> i32 { } } -fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Settings) -> i32 { +fn link_files_in_dir(files: &[PathBuf], target_dir: &Path, settings: &Settings) -> i32 { if !target_dir.is_dir() { show_error!("target '{}' is not a directory", target_dir.display()); return 1; @@ -311,47 +314,48 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Setting let mut all_successful = true; for srcpath in files.iter() { - let targetpath = if settings.no_dereference && settings.force { - // In that case, we don't want to do link resolution - // We need to clean the target - if is_symlink(target_dir) { - if target_dir.is_file() { - if let Err(e) = fs::remove_file(target_dir) { - show_error!("Could not update {}: {}", target_dir.display(), e) - }; - } - if target_dir.is_dir() { - // Not sure why but on Windows, the symlink can be - // considered as a dir - // See test_ln::test_symlink_no_deref_dir - if let Err(e) = fs::remove_dir(target_dir) { - show_error!("Could not update {}: {}", target_dir.display(), e) - }; - } - } - target_dir.clone() - } else { - match srcpath.as_os_str().to_str() { - Some(name) => { - match Path::new(name).file_name() { - Some(basename) => target_dir.join(basename), - // This can be None only for "." or "..". Trying - // to create a link with such name will fail with - // EEXIST, which agrees with the behavior of GNU - // coreutils. - None => target_dir.join(name), + let targetpath = + if settings.no_dereference && matches!(settings.overwrite, OverwriteMode::Force) { + // In that case, we don't want to do link resolution + // We need to clean the target + if is_symlink(target_dir) { + if target_dir.is_file() { + if let Err(e) = fs::remove_file(target_dir) { + show_error!("Could not update {}: {}", target_dir.display(), e) + }; + } + if target_dir.is_dir() { + // Not sure why but on Windows, the symlink can be + // considered as a dir + // See test_ln::test_symlink_no_deref_dir + if let Err(e) = fs::remove_dir(target_dir) { + show_error!("Could not update {}: {}", target_dir.display(), e) + }; } } - None => { - show_error!( - "cannot stat '{}': No such file or directory", - srcpath.display() - ); - all_successful = false; - continue; + target_dir.to_path_buf() + } else { + match srcpath.as_os_str().to_str() { + Some(name) => { + match Path::new(name).file_name() { + Some(basename) => target_dir.join(basename), + // This can be None only for "." or "..". Trying + // to create a link with such name will fail with + // EEXIST, which agrees with the behavior of GNU + // coreutils. + None => target_dir.join(name), + } + } + None => { + show_error!( + "cannot stat '{}': No such file or directory", + srcpath.display() + ); + all_successful = false; + continue; + } } - } - }; + }; if let Err(e) = link(srcpath, &targetpath, settings) { show_error!( @@ -370,30 +374,34 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Setting } } -fn relative_path<'a>(src: &PathBuf, dst: &PathBuf) -> Result> { - let abssrc = canonicalize(src, CanonicalizeMode::Normal)?; - let absdst = canonicalize(dst, CanonicalizeMode::Normal)?; - let suffix_pos = abssrc +fn relative_path<'a>(src: &Path, dst: &Path) -> Result> { + let src_abs = canonicalize(src, CanonicalizeMode::Normal)?; + let mut dst_abs = canonicalize(dst.parent().unwrap(), CanonicalizeMode::Normal)?; + dst_abs.push(dst.components().last().unwrap()); + let suffix_pos = src_abs .components() - .zip(absdst.components()) + .zip(dst_abs.components()) .take_while(|(s, d)| s == d) .count(); - let srciter = abssrc.components().skip(suffix_pos).map(|x| x.as_os_str()); + let src_iter = src_abs.components().skip(suffix_pos).map(|x| x.as_os_str()); - let result: PathBuf = absdst + let mut result: PathBuf = dst_abs .components() .skip(suffix_pos + 1) .map(|_| OsStr::new("..")) - .chain(srciter) + .chain(src_iter) .collect(); + if result.as_os_str().is_empty() { + result.push("."); + } Ok(result.into()) } -fn link(src: &PathBuf, dst: &PathBuf, settings: &Settings) -> Result<()> { +fn link(src: &Path, dst: &Path, settings: &Settings) -> Result<()> { let mut backup_path = None; let source: Cow<'_, Path> = if settings.relative { - relative_path(&src, dst)? + relative_path(src, dst)? } else { src.into() }; @@ -422,10 +430,6 @@ fn link(src: &PathBuf, dst: &PathBuf, settings: &Settings) -> Result<()> { } } - if settings.no_dereference && settings.force && dst.exists() { - fs::remove_file(dst)?; - } - if settings.symbolic { symlink(&source, dst)?; } else { @@ -453,13 +457,13 @@ fn read_yes() -> bool { } } -fn simple_backup_path(path: &PathBuf, suffix: &str) -> PathBuf { +fn simple_backup_path(path: &Path, suffix: &str) -> PathBuf { let mut p = path.as_os_str().to_str().unwrap().to_owned(); p.push_str(suffix); PathBuf::from(p) } -fn numbered_backup_path(path: &PathBuf) -> PathBuf { +fn numbered_backup_path(path: &Path) -> PathBuf { let mut i: u64 = 1; loop { let new_path = simple_backup_path(path, &format!(".~{}~", i)); @@ -470,7 +474,7 @@ fn numbered_backup_path(path: &PathBuf) -> PathBuf { } } -fn existing_backup_path(path: &PathBuf, suffix: &str) -> PathBuf { +fn existing_backup_path(path: &Path, suffix: &str) -> PathBuf { let test_path = simple_backup_path(path, &".~1~".to_owned()); if test_path.exists() { return numbered_backup_path(path); diff --git a/src/uu/ln/src/main.rs b/src/uu/ln/src/main.rs index de14d10bd..060001972 100644 --- a/src/uu/ln/src/main.rs +++ b/src/uu/ln/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_ln); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_ln); diff --git a/src/uu/logname/Cargo.toml b/src/uu/logname/Cargo.toml index d58576c77..4aa4d68f4 100644 --- a/src/uu/logname/Cargo.toml +++ b/src/uu/logname/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_logname" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "logname ~ (uutils) display the login name of the current user" @@ -16,7 +16,8 @@ path = "src/logname.rs" [dependencies] libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/logname/src/logname.rs b/src/uu/logname/src/logname.rs index c1f0c31aa..4a6f43418 100644 --- a/src/uu/logname/src/logname.rs +++ b/src/uu/logname/src/logname.rs @@ -13,6 +13,9 @@ extern crate uucore; use std::ffi::CStr; +use uucore::InvalidEncodingHandling; + +use clap::{crate_version, App}; extern "C" { // POSIX requires using getlogin (or equivalent code) @@ -30,12 +33,19 @@ fn get_userlogin() -> Option { } } -static SYNTAX: &str = ""; static SUMMARY: &str = "Print user's login name"; -static LONG_HELP: &str = ""; + +fn get_usage() -> String { + String::from(executable!()) +} pub fn uumain(args: impl uucore::Args) -> i32 { - app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); + + let usage = get_usage(); + let _ = uu_app().usage(&usage[..]).get_matches_from(args); match get_userlogin() { Some(userlogin) => println!("{}", userlogin), @@ -44,3 +54,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(SUMMARY) +} diff --git a/src/uu/logname/src/main.rs b/src/uu/logname/src/main.rs index 48a4063f1..f9cf6160e 100644 --- a/src/uu/logname/src/main.rs +++ b/src/uu/logname/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_logname); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_logname); diff --git a/src/uu/ls/BENCHMARKING.md b/src/uu/ls/BENCHMARKING.md new file mode 100644 index 000000000..6bc157805 --- /dev/null +++ b/src/uu/ls/BENCHMARKING.md @@ -0,0 +1,59 @@ +# Benchmarking ls + +ls majorly involves fetching a lot of details (depending upon what details are requested, eg. time/date, inode details, etc) for each path using system calls. Ideally, any system call should be done only once for each of the paths - not adhering to this principle leads to a lot of system call overhead multiplying and bubbling up, especially for recursive ls, therefore it is important to always benchmark multiple scenarios. +This is an overview over what was benchmarked, and if you make changes to `ls`, you are encouraged to check +how performance was affected for the workloads listed below. Feel free to add other workloads to the +list that we should improve / make sure not to regress. + +Run `cargo build --release` before benchmarking after you make a change! + +## Simple recursive ls + +- Get a large tree, for example linux kernel source tree. +- Benchmark simple recursive ls with hyperfine: `hyperfine --warmup 2 "target/release/coreutils ls -R tree > /dev/null"`. + +## Recursive ls with all and long options + +- Same tree as above +- Benchmark recursive ls with -al -R options with hyperfine: `hyperfine --warmup 2 "target/release/coreutils ls -al -R tree > /dev/null"`. + +## Comparing with GNU ls + +Hyperfine accepts multiple commands to run and will compare them. To compare performance with GNU ls +duplicate the string you passed to hyperfine but remove the `target/release/coreutils` bit from it. + +Example: `hyperfine --warmup 2 "target/release/coreutils ls -al -R tree > /dev/null"` becomes +`hyperfine --warmup 2 "target/release/coreutils ls -al -R tree > /dev/null" "ls -al -R tree > /dev/null"` +(This assumes GNU ls is installed as `ls`) + +This can also be used to compare with version of ls built before your changes to ensure your change does not regress this. + +Here is a `bash` script for doing this comparison: +```bash +#!/bin/bash +cargo build --no-default-features --features ls --release +args="$@" +hyperfine "ls $args" "target/release/coreutils ls $args" +``` + +**Note**: No localization is currently implemented. This means that the comparison above is not really fair. We can fix this by setting `LC_ALL=C`, so GNU `ls` can ignore localization. + +## Checking system call count + +- Another thing to look at would be system calls count using strace (on linux) or equivalent on other operating systems. +- Example: `strace -c target/release/coreutils ls -al -R tree` + +## Cargo Flamegraph + +With Cargo Flamegraph you can easily make a flamegraph of `ls`: +```bash +cargo flamegraph --cmd coreutils -- ls [additional parameters] +``` + +However, if the `-R` option is given, the output becomes pretty much useless due to recursion. We can fix this by merging all the direct recursive calls with `uniq`, below is a `bash` script that does this. +```bash +#!/bin/bash +cargo build --release --no-default-features --features ls +perf record target/release/coreutils ls "$@" +perf script | uniq | inferno-collapse-perf | inferno-flamegraph > flamegraph.svg +``` diff --git a/src/uu/ls/Cargo.toml b/src/uu/ls/Cargo.toml index 10d7cc2da..ab58a7300 100644 --- a/src/uu/ls/Cargo.toml +++ b/src/uu/ls/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_ls" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "ls ~ (uutils) display directory contents" @@ -15,18 +15,22 @@ edition = "2018" path = "src/ls.rs" [dependencies] -getopts = "0.2.18" -lazy_static = "1.0.1" +locale = "0.2.2" +chrono = "0.4.19" +clap = "2.33" +unicode-width = "0.1.8" number_prefix = "0.4" term_grid = "0.1.5" termsize = "0.1.6" -time = "0.1.40" -unicode-width = "0.1.5" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries", "fs"] } -uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +globset = "0.4.6" +lscolors = { version = "0.7.1", features = ["ansi_term"] } +uucore = { version = ">=0.0.8", package = "uucore", path = "../../uucore", features = ["entries", "fs"] } +uucore_procs = { version = ">=0.0.5", package = "uucore_procs", path = "../../uucore_procs" } +once_cell = "1.7.2" +atty = "0.2" [target.'cfg(unix)'.dependencies] -atty = "0.2" +lazy_static = "1.4.0" [[bin]] name = "ls" diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index b8defa397..059981e28 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -7,333 +7,1364 @@ // spell-checker:ignore (ToDO) cpio svgz webm somegroup nlink rmvb xspf +#[macro_use] +extern crate uucore; #[cfg(unix)] #[macro_use] extern crate lazy_static; -#[macro_use] -extern crate uucore; +mod quoting_style; +mod version_cmp; + +use clap::{crate_version, App, Arg}; +use globset::{self, Glob, GlobSet, GlobSetBuilder}; +use lscolors::LsColors; use number_prefix::NumberPrefix; -use std::cmp::Reverse; -#[cfg(unix)] -use std::collections::HashMap; -use std::fs; -use std::fs::{DirEntry, FileType, Metadata}; -#[cfg(unix)] -use std::os::unix::fs::FileTypeExt; -#[cfg(any(unix, target_os = "redox"))] -use std::os::unix::fs::MetadataExt; +use once_cell::unsync::OnceCell; +use quoting_style::{escape_name, QuotingStyle}; #[cfg(windows)] use std::os::windows::fs::MetadataExt; -use std::path::{Path, PathBuf}; -use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; -use time::{strftime, Timespec}; +use std::{ + cmp::Reverse, + error::Error, + fmt::Display, + fs::{self, DirEntry, FileType, Metadata}, + io::{stdout, BufWriter, Stdout, Write}, + path::{Path, PathBuf}, + time::{SystemTime, UNIX_EPOCH}, +}; #[cfg(unix)] +use std::{ + collections::HashMap, + os::unix::fs::{FileTypeExt, MetadataExt}, + time::Duration, +}; +use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; +use uucore::error::{set_exit_code, FromIo, UCustomError, UResult}; + use unicode_width::UnicodeWidthStr; #[cfg(unix)] -use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH, S_IXGRP, S_IXOTH, S_IXUSR}; +use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR}; -static NAME: &str = "ls"; -static SUMMARY: &str = ""; -static LONG_HELP: &str = " +static ABOUT: &str = " By default, ls will list the files and contents of any directories on the command line, expect that it will ignore files and directories whose names start with '.' "; +static AFTER_HELP: &str = "The TIME_STYLE argument can be full-iso, long-iso, iso. +Also the TIME_STYLE environment variable sets the default style to use."; -#[cfg(unix)] -static DEFAULT_COLORS: &str = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:"; - -#[cfg(unix)] -lazy_static! { - static ref LS_COLORS: String = - std::env::var("LS_COLORS").unwrap_or_else(|_| DEFAULT_COLORS.to_string()); - static ref COLOR_MAP: HashMap<&'static str, &'static str> = { - let codes = LS_COLORS.split(':'); - let mut map = HashMap::new(); - for c in codes { - let p: Vec<_> = c.split('=').collect(); - if p.len() == 2 { - map.insert(p[0], p[1]); - } - } - map - }; - static ref RESET_CODE: &'static str = COLOR_MAP.get("rs").unwrap_or(&"0"); - static ref LEFT_CODE: &'static str = COLOR_MAP.get("lc").unwrap_or(&"\x1b["); - static ref RIGHT_CODE: &'static str = COLOR_MAP.get("rc").unwrap_or(&"m"); - static ref END_CODE: &'static str = COLOR_MAP.get("ec").unwrap_or(&""); +fn get_usage() -> String { + format!("{0} [OPTION]... [FILE]...", executable!()) } -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - - let syntax = format!( - "[OPTION]... DIRECTORY - {0} [OPTION]... [FILE]...", - NAME - ); - let matches = app!(&syntax, SUMMARY, LONG_HELP) - .optflag("1", "", "list one file per line.") - .optflag( - "a", - "all", - "Do not ignore hidden files (files with names that start with '.').", - ) - .optflag( - "A", - "almost-all", - "In a directory, do not ignore all file names that start with '.', only ignore \ - '.' and '..'.", - ) - .optflag("B", "ignore-backups", "Ignore entries which end with ~.") - .optflag( - "c", - "", - "If the long listing format (e.g., -l, -o) is being used, print the status \ - change time (the ‘ctime’ in the inode) instead of the modification time. When \ - explicitly sorting by time (--sort=time or -t) or when not using a long listing \ - format, sort according to the status change time.", - ) - .optflag( - "d", - "directory", - "Only list the names of directories, rather than listing directory contents. \ - This will not follow symbolic links unless one of `--dereference-command-line \ - (-H)`, `--dereference (-L)`, or `--dereference-command-line-symlink-to-dir` is \ - specified.", - ) - .optflag( - "F", - "classify", - "Append a character to each file name indicating the file type. Also, for \ - regular files that are executable, append '*'. The file type indicators are \ - '/' for directories, '@' for symbolic links, '|' for FIFOs, '=' for sockets, \ - '>' for doors, and nothing for regular files.", - ) - .optflag( - "h", - "human-readable", - "Print human readable file sizes (e.g. 1K 234M 56G).", - ) - .optflag("i", "inode", "print the index number of each file") - .optflag( - "L", - "dereference", - "When showing file information for a symbolic link, show information for the \ - file the link references rather than the link itself.", - ) - .optflag("l", "long", "Display detailed information.") - .optflag("n", "numeric-uid-gid", "-l with numeric UIDs and GIDs.") - .optflag( - "r", - "reverse", - "Reverse whatever the sorting method is--e.g., list files in reverse \ - alphabetical order, youngest first, smallest first, or whatever.", - ) - .optflag( - "R", - "recursive", - "List the contents of all directories recursively.", - ) - .optflag("S", "", "Sort by file size, largest first.") - .optflag( - "t", - "", - "Sort by modification time (the 'mtime' in the inode), newest first.", - ) - .optflag( - "U", - "", - "Do not sort; list the files in whatever order they are stored in the \ - directory. This is especially useful when listing very large directories, \ - since not doing any sorting can be noticeably faster.", - ) - .optflagopt( - "", - "color", - "Color output based on file type.", - "always|auto|never", - ) - .parse(args); - - list(matches) +pub mod options { + pub mod format { + pub static ONE_LINE: &str = "1"; + pub static LONG: &str = "long"; + pub static COLUMNS: &str = "C"; + pub static ACROSS: &str = "x"; + pub static COMMAS: &str = "m"; + pub static LONG_NO_OWNER: &str = "g"; + pub static LONG_NO_GROUP: &str = "o"; + pub static LONG_NUMERIC_UID_GID: &str = "numeric-uid-gid"; + } + pub mod files { + pub static ALL: &str = "all"; + pub static ALMOST_ALL: &str = "almost-all"; + } + pub mod sort { + pub static SIZE: &str = "S"; + pub static TIME: &str = "t"; + pub static NONE: &str = "U"; + pub static VERSION: &str = "v"; + pub static EXTENSION: &str = "X"; + } + pub mod time { + pub static ACCESS: &str = "u"; + pub static CHANGE: &str = "c"; + } + pub mod size { + pub static HUMAN_READABLE: &str = "human-readable"; + pub static SI: &str = "si"; + } + pub mod quoting { + pub static ESCAPE: &str = "escape"; + pub static LITERAL: &str = "literal"; + pub static C: &str = "quote-name"; + } + pub static QUOTING_STYLE: &str = "quoting-style"; + pub mod indicator_style { + pub static SLASH: &str = "p"; + pub static FILE_TYPE: &str = "file-type"; + pub static CLASSIFY: &str = "classify"; + } + pub mod dereference { + pub static ALL: &str = "dereference"; + pub static ARGS: &str = "dereference-command-line"; + pub static DIR_ARGS: &str = "dereference-command-line-symlink-to-dir"; + } + pub static HIDE_CONTROL_CHARS: &str = "hide-control-chars"; + pub static SHOW_CONTROL_CHARS: &str = "show-control-chars"; + pub static WIDTH: &str = "width"; + pub static AUTHOR: &str = "author"; + pub static NO_GROUP: &str = "no-group"; + pub static FORMAT: &str = "format"; + pub static SORT: &str = "sort"; + pub static TIME: &str = "time"; + pub static IGNORE_BACKUPS: &str = "ignore-backups"; + pub static DIRECTORY: &str = "directory"; + pub static INODE: &str = "inode"; + pub static REVERSE: &str = "reverse"; + pub static RECURSIVE: &str = "recursive"; + pub static COLOR: &str = "color"; + pub static PATHS: &str = "paths"; + pub static INDICATOR_STYLE: &str = "indicator-style"; + pub static TIME_STYLE: &str = "time-style"; + pub static FULL_TIME: &str = "full-time"; + pub static HIDE: &str = "hide"; + pub static IGNORE: &str = "ignore"; } -fn list(options: getopts::Matches) -> i32 { - let locs: Vec = if options.free.is_empty() { - vec![String::from(".")] - } else { - options.free.to_vec() - }; +#[derive(Debug)] +enum LsError { + InvalidLineWidth(String), + NoMetadata(PathBuf), +} - let mut files = Vec::::new(); - let mut dirs = Vec::::new(); - let mut has_failed = false; - for loc in locs { - let p = PathBuf::from(&loc); - if !p.exists() { - show_error!("'{}': {}", &loc, "No such file or directory"); - // We found an error, the return code of ls should not be 0 - // And no need to continue the execution - has_failed = true; - continue; +impl UCustomError for LsError { + fn code(&self) -> i32 { + match self { + LsError::InvalidLineWidth(_) => 2, + LsError::NoMetadata(_) => 1, } - let mut dir = false; + } +} - if p.is_dir() && !options.opt_present("d") { - dir = true; - if options.opt_present("l") && !(options.opt_present("L")) { - if let Ok(md) = p.symlink_metadata() { - if md.file_type().is_symlink() && !p.ends_with("/") { - dir = false; - } +impl Error for LsError {} + +impl Display for LsError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + LsError::InvalidLineWidth(s) => write!(f, "invalid line width: '{}'", s), + LsError::NoMetadata(p) => write!(f, "could not open file: '{}'", p.display()), + } + } +} + +#[derive(PartialEq, Eq)] +enum Format { + Columns, + Long, + OneLine, + Across, + Commas, +} + +enum Sort { + None, + Name, + Size, + Time, + Version, + Extension, +} + +enum SizeFormat { + Bytes, + Binary, // Powers of 1024, --human-readable, -h + Decimal, // Powers of 1000, --si +} + +#[derive(PartialEq, Eq)] +enum Files { + All, + AlmostAll, + Normal, +} + +enum Time { + Modification, + Access, + Change, + Birth, +} + +#[derive(Debug)] +enum TimeStyle { + FullIso, + LongIso, + Iso, + Locale, +} + +enum Dereference { + None, + DirArgs, + Args, + All, +} + +#[derive(PartialEq, Eq)] +enum IndicatorStyle { + None, + Slash, + FileType, + Classify, +} + +struct Config { + format: Format, + files: Files, + sort: Sort, + recursive: bool, + reverse: bool, + dereference: Dereference, + ignore_patterns: GlobSet, + size_format: SizeFormat, + directory: bool, + time: Time, + #[cfg(unix)] + inode: bool, + color: Option, + long: LongFormat, + width: Option, + quoting_style: QuotingStyle, + indicator_style: IndicatorStyle, + time_style: TimeStyle, +} + +// Fields that can be removed or added to the long format +struct LongFormat { + author: bool, + group: bool, + owner: bool, + #[cfg(unix)] + numeric_uid_gid: bool, +} + +impl Config { + #[allow(clippy::cognitive_complexity)] + fn from(options: clap::ArgMatches) -> UResult { + let (mut format, opt) = if let Some(format_) = options.value_of(options::FORMAT) { + ( + match format_ { + "long" | "verbose" => Format::Long, + "single-column" => Format::OneLine, + "columns" | "vertical" => Format::Columns, + "across" | "horizontal" => Format::Across, + "commas" => Format::Commas, + // below should never happen as clap already restricts the values. + _ => unreachable!("Invalid field for --format"), + }, + options::FORMAT, + ) + } else if options.is_present(options::format::LONG) { + (Format::Long, options::format::LONG) + } else if options.is_present(options::format::ACROSS) { + (Format::Across, options::format::ACROSS) + } else if options.is_present(options::format::COMMAS) { + (Format::Commas, options::format::COMMAS) + } else { + (Format::Columns, options::format::COLUMNS) + }; + + // The -o, -n and -g options are tricky. They cannot override with each + // other because it's possible to combine them. For example, the option + // -og should hide both owner and group. Furthermore, they are not + // reset if -l or --format=long is used. So these should just show the + // group: -gl or "-g --format=long". Finally, they are also not reset + // when switching to a different format option in-between like this: + // -ogCl or "-og --format=vertical --format=long". + // + // -1 has a similar issue: it does nothing if the format is long. This + // actually makes it distinct from the --format=singe-column option, + // which always applies. + // + // The idea here is to not let these options override with the other + // options, but manually whether they have an index that's greater than + // the other format options. If so, we set the appropriate format. + if format != Format::Long { + let idx = options + .indices_of(opt) + .map(|x| x.max().unwrap()) + .unwrap_or(0); + if [ + options::format::LONG_NO_OWNER, + options::format::LONG_NO_GROUP, + options::format::LONG_NUMERIC_UID_GID, + options::FULL_TIME, + ] + .iter() + .flat_map(|opt| options.indices_of(opt)) + .flatten() + .any(|i| i >= idx) + { + format = Format::Long; + } else if let Some(mut indices) = options.indices_of(options::format::ONE_LINE) { + if indices.any(|i| i > idx) { + format = Format::OneLine; } } } - if dir { - dirs.push(p); - } else { - files.push(p); - } - } - sort_entries(&mut files, &options); - display_items(&files, None, &options); - sort_entries(&mut dirs, &options); - for dir in dirs { - if options.free.len() > 1 { - println!("\n{}:", dir.to_string_lossy()); + let files = if options.is_present(options::files::ALL) { + Files::All + } else if options.is_present(options::files::ALMOST_ALL) { + Files::AlmostAll + } else { + Files::Normal + }; + + let sort = if let Some(field) = options.value_of(options::SORT) { + match field { + "none" => Sort::None, + "name" => Sort::Name, + "time" => Sort::Time, + "size" => Sort::Size, + "version" => Sort::Version, + "extension" => Sort::Extension, + // below should never happen as clap already restricts the values. + _ => unreachable!("Invalid field for --sort"), + } + } else if options.is_present(options::sort::TIME) { + Sort::Time + } else if options.is_present(options::sort::SIZE) { + Sort::Size + } else if options.is_present(options::sort::NONE) { + Sort::None + } else if options.is_present(options::sort::VERSION) { + Sort::Version + } else if options.is_present(options::sort::EXTENSION) { + Sort::Extension + } else { + Sort::Name + }; + + let time = if let Some(field) = options.value_of(options::TIME) { + match field { + "ctime" | "status" => Time::Change, + "access" | "atime" | "use" => Time::Access, + "birth" | "creation" => Time::Birth, + // below should never happen as clap already restricts the values. + _ => unreachable!("Invalid field for --time"), + } + } else if options.is_present(options::time::ACCESS) { + Time::Access + } else if options.is_present(options::time::CHANGE) { + Time::Change + } else { + Time::Modification + }; + + let needs_color = match options.value_of(options::COLOR) { + None => options.is_present(options::COLOR), + Some(val) => match val { + "" | "always" | "yes" | "force" => true, + "auto" | "tty" | "if-tty" => atty::is(atty::Stream::Stdout), + /* "never" | "no" | "none" | */ _ => false, + }, + }; + + let color = if needs_color { + Some(LsColors::from_env().unwrap_or_default()) + } else { + None + }; + + let size_format = if options.is_present(options::size::HUMAN_READABLE) { + SizeFormat::Binary + } else if options.is_present(options::size::SI) { + SizeFormat::Decimal + } else { + SizeFormat::Bytes + }; + + let long = { + let author = options.is_present(options::AUTHOR); + let group = !options.is_present(options::NO_GROUP) + && !options.is_present(options::format::LONG_NO_GROUP); + let owner = !options.is_present(options::format::LONG_NO_OWNER); + #[cfg(unix)] + let numeric_uid_gid = options.is_present(options::format::LONG_NUMERIC_UID_GID); + LongFormat { + author, + group, + owner, + #[cfg(unix)] + numeric_uid_gid, + } + }; + + let width = match options.value_of(options::WIDTH) { + Some(x) => match x.parse::() { + Ok(u) => Some(u), + Err(_) => return Err(LsError::InvalidLineWidth(x.into()).into()), + }, + None => termsize::get().map(|s| s.cols), + }; + + #[allow(clippy::needless_bool)] + let show_control = if options.is_present(options::HIDE_CONTROL_CHARS) { + false + } else if options.is_present(options::SHOW_CONTROL_CHARS) || atty::is(atty::Stream::Stdout) + { + true + } else { + false + }; + + let quoting_style = if let Some(style) = options.value_of(options::QUOTING_STYLE) { + match style { + "literal" => QuotingStyle::Literal { show_control }, + "shell" => QuotingStyle::Shell { + escape: false, + always_quote: false, + show_control, + }, + "shell-always" => QuotingStyle::Shell { + escape: false, + always_quote: true, + show_control, + }, + "shell-escape" => QuotingStyle::Shell { + escape: true, + always_quote: false, + show_control, + }, + "shell-escape-always" => QuotingStyle::Shell { + escape: true, + always_quote: true, + show_control, + }, + "c" => QuotingStyle::C { + quotes: quoting_style::Quotes::Double, + }, + "escape" => QuotingStyle::C { + quotes: quoting_style::Quotes::None, + }, + _ => unreachable!("Should have been caught by Clap"), + } + } else if options.is_present(options::quoting::LITERAL) { + QuotingStyle::Literal { show_control } + } else if options.is_present(options::quoting::ESCAPE) { + QuotingStyle::C { + quotes: quoting_style::Quotes::None, + } + } else if options.is_present(options::quoting::C) { + QuotingStyle::C { + quotes: quoting_style::Quotes::Double, + } + } else { + // TODO: use environment variable if available + QuotingStyle::Shell { + escape: true, + always_quote: false, + show_control, + } + }; + + let indicator_style = if let Some(field) = options.value_of(options::INDICATOR_STYLE) { + match field { + "none" => IndicatorStyle::None, + "file-type" => IndicatorStyle::FileType, + "classify" => IndicatorStyle::Classify, + "slash" => IndicatorStyle::Slash, + &_ => IndicatorStyle::None, + } + } else if options.is_present(options::indicator_style::CLASSIFY) { + IndicatorStyle::Classify + } else if options.is_present(options::indicator_style::SLASH) { + IndicatorStyle::Slash + } else if options.is_present(options::indicator_style::FILE_TYPE) { + IndicatorStyle::FileType + } else { + IndicatorStyle::None + }; + + let time_style = if let Some(field) = options.value_of(options::TIME_STYLE) { + //If both FULL_TIME and TIME_STYLE are present + //The one added last is dominant + if options.is_present(options::FULL_TIME) + && options.indices_of(options::FULL_TIME).unwrap().last() + > options.indices_of(options::TIME_STYLE).unwrap().last() + { + TimeStyle::FullIso + } else { + //Clap handles the env variable "TIME_STYLE" + match field { + "full-iso" => TimeStyle::FullIso, + "long-iso" => TimeStyle::LongIso, + "iso" => TimeStyle::Iso, + "locale" => TimeStyle::Locale, + // below should never happen as clap already restricts the values. + _ => unreachable!("Invalid field for --time-style"), + } + } + } else if options.is_present(options::FULL_TIME) { + TimeStyle::FullIso + } else { + TimeStyle::Locale + }; + let mut ignore_patterns = GlobSetBuilder::new(); + if options.is_present(options::IGNORE_BACKUPS) { + ignore_patterns.add(Glob::new("*~").unwrap()); + ignore_patterns.add(Glob::new(".*~").unwrap()); } - enter_directory(&dir, &options); - } - if has_failed { - 1 - } else { - 0 + + for pattern in options.values_of(options::IGNORE).into_iter().flatten() { + match Glob::new(pattern) { + Ok(p) => { + ignore_patterns.add(p); + } + Err(_) => show_warning!("Invalid pattern for ignore: '{}'", pattern), + } + } + + if files == Files::Normal { + for pattern in options.values_of(options::HIDE).into_iter().flatten() { + match Glob::new(pattern) { + Ok(p) => { + ignore_patterns.add(p); + } + Err(_) => show_warning!("Invalid pattern for hide: '{}'", pattern), + } + } + } + + if files == Files::Normal { + ignore_patterns.add(Glob::new(".*").unwrap()); + } + + let ignore_patterns = ignore_patterns.build().unwrap(); + + let dereference = if options.is_present(options::dereference::ALL) { + Dereference::All + } else if options.is_present(options::dereference::ARGS) { + Dereference::Args + } else if options.is_present(options::dereference::DIR_ARGS) { + Dereference::DirArgs + } else if options.is_present(options::DIRECTORY) + || indicator_style == IndicatorStyle::Classify + || format == Format::Long + { + Dereference::None + } else { + Dereference::DirArgs + }; + + Ok(Config { + format, + files, + sort, + recursive: options.is_present(options::RECURSIVE), + reverse: options.is_present(options::REVERSE), + dereference, + ignore_patterns, + size_format, + directory: options.is_present(options::DIRECTORY), + time, + color, + #[cfg(unix)] + inode: options.is_present(options::INODE), + long, + width, + quoting_style, + indicator_style, + time_style, + }) } } -#[cfg(any(unix, target_os = "redox"))] -fn sort_entries(entries: &mut Vec, options: &getopts::Matches) { - let mut reverse = options.opt_present("r"); - if options.opt_present("t") { - if options.opt_present("c") { - entries.sort_by_key(|k| { - Reverse(get_metadata(k, options).map(|md| md.ctime()).unwrap_or(0)) - }); - } else { - entries.sort_by_key(|k| { - // Newest first - Reverse( - get_metadata(k, options) - .and_then(|md| md.modified()) - .unwrap_or(std::time::UNIX_EPOCH), +#[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 app = uu_app().usage(&usage[..]); + + let matches = app.get_matches_from(args); + + let locs = matches + .values_of(options::PATHS) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_else(|| vec![String::from(".")]); + + list(locs, Config::from(matches)?) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + + // Format arguments + .arg( + Arg::with_name(options::FORMAT) + .long(options::FORMAT) + .help("Set the display format.") + .takes_value(true) + .possible_values(&["long", "verbose", "single-column", "columns", "vertical", "across", "horizontal", "commas"]) + .hide_possible_values(true) + .require_equals(true) + .overrides_with_all(&[ + options::FORMAT, + options::format::COLUMNS, + options::format::LONG, + options::format::ACROSS, + options::format::COLUMNS, + ]), + ) + .arg( + Arg::with_name(options::format::COLUMNS) + .short(options::format::COLUMNS) + .help("Display the files in columns.") + .overrides_with_all(&[ + options::FORMAT, + options::format::COLUMNS, + options::format::LONG, + options::format::ACROSS, + options::format::COLUMNS, + ]), + ) + .arg( + Arg::with_name(options::format::LONG) + .short("l") + .long(options::format::LONG) + .help("Display detailed information.") + .overrides_with_all(&[ + options::FORMAT, + options::format::COLUMNS, + options::format::LONG, + options::format::ACROSS, + options::format::COLUMNS, + ]), + ) + .arg( + Arg::with_name(options::format::ACROSS) + .short(options::format::ACROSS) + .help("List entries in rows instead of in columns.") + .overrides_with_all(&[ + options::FORMAT, + options::format::COLUMNS, + options::format::LONG, + options::format::ACROSS, + options::format::COLUMNS, + ]), + ) + .arg( + Arg::with_name(options::format::COMMAS) + .short(options::format::COMMAS) + .help("List entries separated by commas.") + .overrides_with_all(&[ + options::FORMAT, + options::format::COLUMNS, + options::format::LONG, + options::format::ACROSS, + options::format::COLUMNS, + ]), + ) + // The next four arguments do not override with the other format + // options, see the comment in Config::from for the reason. + // Ideally, they would use Arg::override_with, with their own name + // but that doesn't seem to work in all cases. Example: + // ls -1g1 + // even though `ls -11` and `ls -1 -g -1` work. + .arg( + Arg::with_name(options::format::ONE_LINE) + .short(options::format::ONE_LINE) + .help("List one file per line.") + .multiple(true) + ) + .arg( + Arg::with_name(options::format::LONG_NO_GROUP) + .short(options::format::LONG_NO_GROUP) + .help("Long format without group information. Identical to --format=long with --no-group.") + .multiple(true) + ) + .arg( + Arg::with_name(options::format::LONG_NO_OWNER) + .short(options::format::LONG_NO_OWNER) + .help("Long format without owner information.") + .multiple(true) + ) + .arg( + Arg::with_name(options::format::LONG_NUMERIC_UID_GID) + .short("n") + .long(options::format::LONG_NUMERIC_UID_GID) + .help("-l with numeric UIDs and GIDs.") + .multiple(true) + ) + + // Quoting style + .arg( + Arg::with_name(options::QUOTING_STYLE) + .long(options::QUOTING_STYLE) + .takes_value(true) + .help("Set quoting style.") + .possible_values(&["literal", "shell", "shell-always", "shell-escape", "shell-escape-always", "c", "escape"]) + .overrides_with_all(&[ + options::QUOTING_STYLE, + options::quoting::LITERAL, + options::quoting::ESCAPE, + options::quoting::C, + ]) + ) + .arg( + Arg::with_name(options::quoting::LITERAL) + .short("N") + .long(options::quoting::LITERAL) + .help("Use literal quoting style. Equivalent to `--quoting-style=literal`") + .overrides_with_all(&[ + options::QUOTING_STYLE, + options::quoting::LITERAL, + options::quoting::ESCAPE, + options::quoting::C, + ]) + ) + .arg( + Arg::with_name(options::quoting::ESCAPE) + .short("b") + .long(options::quoting::ESCAPE) + .help("Use escape quoting style. Equivalent to `--quoting-style=escape`") + .overrides_with_all(&[ + options::QUOTING_STYLE, + options::quoting::LITERAL, + options::quoting::ESCAPE, + options::quoting::C, + ]) + ) + .arg( + Arg::with_name(options::quoting::C) + .short("Q") + .long(options::quoting::C) + .help("Use C quoting style. Equivalent to `--quoting-style=c`") + .overrides_with_all(&[ + options::QUOTING_STYLE, + options::quoting::LITERAL, + options::quoting::ESCAPE, + options::quoting::C, + ]) + ) + + // Control characters + .arg( + Arg::with_name(options::HIDE_CONTROL_CHARS) + .short("q") + .long(options::HIDE_CONTROL_CHARS) + .help("Replace control characters with '?' if they are not escaped.") + .overrides_with_all(&[ + options::HIDE_CONTROL_CHARS, + options::SHOW_CONTROL_CHARS, + ]) + ) + .arg( + Arg::with_name(options::SHOW_CONTROL_CHARS) + .long(options::SHOW_CONTROL_CHARS) + .help("Show control characters 'as is' if they are not escaped.") + .overrides_with_all(&[ + options::HIDE_CONTROL_CHARS, + options::SHOW_CONTROL_CHARS, + ]) + ) + + // Time arguments + .arg( + Arg::with_name(options::TIME) + .long(options::TIME) + .help("Show time in :\n\ + \taccess time (-u): atime, access, use;\n\ + \tchange time (-t): ctime, status.\n\ + \tbirth time: birth, creation;") + .value_name("field") + .takes_value(true) + .possible_values(&["atime", "access", "use", "ctime", "status", "birth", "creation"]) + .hide_possible_values(true) + .require_equals(true) + .overrides_with_all(&[ + options::TIME, + options::time::ACCESS, + options::time::CHANGE, + ]) + ) + .arg( + Arg::with_name(options::time::CHANGE) + .short(options::time::CHANGE) + .help("If the long listing format (e.g., -l, -o) is being used, print the status \ + change time (the 'ctime' in the inode) instead of the modification time. When \ + explicitly sorting by time (--sort=time or -t) or when not using a long listing \ + format, sort according to the status change time.") + .overrides_with_all(&[ + options::TIME, + options::time::ACCESS, + options::time::CHANGE, + ]) + ) + .arg( + Arg::with_name(options::time::ACCESS) + .short(options::time::ACCESS) + .help("If the long listing format (e.g., -l, -o) is being used, print the status \ + access time instead of the modification time. When explicitly sorting by time \ + (--sort=time or -t) or when not using a long listing format, sort according to the \ + access time.") + .overrides_with_all(&[ + options::TIME, + options::time::ACCESS, + options::time::CHANGE, + ]) + ) + + // Hide and ignore + .arg( + Arg::with_name(options::HIDE) + .long(options::HIDE) + .takes_value(true) + .multiple(true) + .value_name("PATTERN") + .help("do not list implied entries matching shell PATTERN (overridden by -a or -A)") + ) + .arg( + Arg::with_name(options::IGNORE) + .short("I") + .long(options::IGNORE) + .takes_value(true) + .multiple(true) + .value_name("PATTERN") + .help("do not list implied entries matching shell PATTERN") + ) + .arg( + Arg::with_name(options::IGNORE_BACKUPS) + .short("B") + .long(options::IGNORE_BACKUPS) + .help("Ignore entries which end with ~."), + ) + + // Sort arguments + .arg( + Arg::with_name(options::SORT) + .long(options::SORT) + .help("Sort by : name, none (-U), time (-t), size (-S) or extension (-X)") + .value_name("field") + .takes_value(true) + .possible_values(&["name", "none", "time", "size", "version", "extension"]) + .require_equals(true) + .overrides_with_all(&[ + options::SORT, + options::sort::SIZE, + options::sort::TIME, + options::sort::NONE, + options::sort::VERSION, + options::sort::EXTENSION, + ]) + ) + .arg( + Arg::with_name(options::sort::SIZE) + .short(options::sort::SIZE) + .help("Sort by file size, largest first.") + .overrides_with_all(&[ + options::SORT, + options::sort::SIZE, + options::sort::TIME, + options::sort::NONE, + options::sort::VERSION, + options::sort::EXTENSION, + ]) + ) + .arg( + Arg::with_name(options::sort::TIME) + .short(options::sort::TIME) + .help("Sort by modification time (the 'mtime' in the inode), newest first.") + .overrides_with_all(&[ + options::SORT, + options::sort::SIZE, + options::sort::TIME, + options::sort::NONE, + options::sort::VERSION, + options::sort::EXTENSION, + ]) + ) + .arg( + Arg::with_name(options::sort::VERSION) + .short(options::sort::VERSION) + .help("Natural sort of (version) numbers in the filenames.") + .overrides_with_all(&[ + options::SORT, + options::sort::SIZE, + options::sort::TIME, + options::sort::NONE, + options::sort::VERSION, + options::sort::EXTENSION, + ]) + ) + .arg( + Arg::with_name(options::sort::EXTENSION) + .short(options::sort::EXTENSION) + .help("Sort alphabetically by entry extension.") + .overrides_with_all(&[ + options::SORT, + options::sort::SIZE, + options::sort::TIME, + options::sort::NONE, + options::sort::VERSION, + options::sort::EXTENSION, + ]) + ) + .arg( + Arg::with_name(options::sort::NONE) + .short(options::sort::NONE) + .help("Do not sort; list the files in whatever order they are stored in the \ + directory. This is especially useful when listing very large directories, \ + since not doing any sorting can be noticeably faster.") + .overrides_with_all(&[ + options::SORT, + options::sort::SIZE, + options::sort::TIME, + options::sort::NONE, + options::sort::VERSION, + options::sort::EXTENSION, + ]) + ) + + // Dereferencing + .arg( + Arg::with_name(options::dereference::ALL) + .short("L") + .long(options::dereference::ALL) + .help( + "When showing file information for a symbolic link, show information for the \ + file the link references rather than the link itself.", ) - }); + .overrides_with_all(&[ + options::dereference::ALL, + options::dereference::DIR_ARGS, + options::dereference::ARGS, + ]) + ) + .arg( + Arg::with_name(options::dereference::DIR_ARGS) + .long(options::dereference::DIR_ARGS) + .help( + "Do not dereference symlinks except when they link to directories and are \ + given as command line arguments.", + ) + .overrides_with_all(&[ + options::dereference::ALL, + options::dereference::DIR_ARGS, + options::dereference::ARGS, + ]) + ) + .arg( + Arg::with_name(options::dereference::ARGS) + .short("H") + .long(options::dereference::ARGS) + .help( + "Do not dereference symlinks except when given as command line arguments.", + ) + .overrides_with_all(&[ + options::dereference::ALL, + options::dereference::DIR_ARGS, + options::dereference::ARGS, + ]) + ) + + // Long format options + .arg( + Arg::with_name(options::NO_GROUP) + .long(options::NO_GROUP) + .short("-G") + .help("Do not show group in long format.") + ) + .arg( + Arg::with_name(options::AUTHOR) + .long(options::AUTHOR) + .help("Show author in long format. On the supported platforms, the author \ + always matches the file owner.") + ) + // Other Flags + .arg( + Arg::with_name(options::files::ALL) + .short("a") + .long(options::files::ALL) + .help("Do not ignore hidden files (files with names that start with '.')."), + ) + .arg( + Arg::with_name(options::files::ALMOST_ALL) + .short("A") + .long(options::files::ALMOST_ALL) + .help( + "In a directory, do not ignore all file names that start with '.', only ignore \ + '.' and '..'.", + ), + ) + .arg( + Arg::with_name(options::DIRECTORY) + .short("d") + .long(options::DIRECTORY) + .help( + "Only list the names of directories, rather than listing directory contents. \ + This will not follow symbolic links unless one of `--dereference-command-line \ + (-H)`, `--dereference (-L)`, or `--dereference-command-line-symlink-to-dir` is \ + specified.", + ), + ) + .arg( + Arg::with_name(options::size::HUMAN_READABLE) + .short("h") + .long(options::size::HUMAN_READABLE) + .help("Print human readable file sizes (e.g. 1K 234M 56G).") + .overrides_with(options::size::SI), + ) + .arg( + Arg::with_name(options::size::SI) + .long(options::size::SI) + .help("Print human readable file sizes using powers of 1000 instead of 1024.") + ) + .arg( + Arg::with_name(options::INODE) + .short("i") + .long(options::INODE) + .help("print the index number of each file"), + ) + .arg( + Arg::with_name(options::REVERSE) + .short("r") + .long(options::REVERSE) + .help("Reverse whatever the sorting method is--e.g., list files in reverse \ + alphabetical order, youngest first, smallest first, or whatever.", + )) + .arg( + Arg::with_name(options::RECURSIVE) + .short("R") + .long(options::RECURSIVE) + .help("List the contents of all directories recursively."), + ) + .arg( + Arg::with_name(options::WIDTH) + .long(options::WIDTH) + .short("w") + .help("Assume that the terminal is COLS columns wide.") + .value_name("COLS") + .takes_value(true) + ) + .arg( + Arg::with_name(options::COLOR) + .long(options::COLOR) + .help("Color output based on file type.") + .takes_value(true) + .require_equals(true) + .min_values(0), + ) + .arg( + Arg::with_name(options::INDICATOR_STYLE) + .long(options::INDICATOR_STYLE) + .help(" append indicator with style WORD to entry names: none (default), slash\ + (-p), file-type (--file-type), classify (-F)") + .takes_value(true) + .possible_values(&["none", "slash", "file-type", "classify"]) + .overrides_with_all(&[ + options::indicator_style::FILE_TYPE, + options::indicator_style::SLASH, + options::indicator_style::CLASSIFY, + options::INDICATOR_STYLE, + ])) + .arg( + Arg::with_name(options::indicator_style::CLASSIFY) + .short("F") + .long(options::indicator_style::CLASSIFY) + .help("Append a character to each file name indicating the file type. Also, for \ + regular files that are executable, append '*'. The file type indicators are \ + '/' for directories, '@' for symbolic links, '|' for FIFOs, '=' for sockets, \ + '>' for doors, and nothing for regular files.") + .overrides_with_all(&[ + options::indicator_style::FILE_TYPE, + options::indicator_style::SLASH, + options::indicator_style::CLASSIFY, + options::INDICATOR_STYLE, + ]) + ) + .arg( + Arg::with_name(options::indicator_style::FILE_TYPE) + .long(options::indicator_style::FILE_TYPE) + .help("Same as --classify, but do not append '*'") + .overrides_with_all(&[ + options::indicator_style::FILE_TYPE, + options::indicator_style::SLASH, + options::indicator_style::CLASSIFY, + options::INDICATOR_STYLE, + ])) + .arg( + Arg::with_name(options::indicator_style::SLASH) + .short(options::indicator_style::SLASH) + .help("Append / indicator to directories." + ) + .overrides_with_all(&[ + options::indicator_style::FILE_TYPE, + options::indicator_style::SLASH, + options::indicator_style::CLASSIFY, + options::INDICATOR_STYLE, + ])) + .arg( + //This still needs support for posix-*, +FORMAT + Arg::with_name(options::TIME_STYLE) + .long(options::TIME_STYLE) + .help("time/date format with -l; see TIME_STYLE below") + .value_name("TIME_STYLE") + .env("TIME_STYLE") + .possible_values(&[ + "full-iso", + "long-iso", + "iso", + "locale", + ]) + .overrides_with_all(&[ + options::TIME_STYLE + ]) + ) + .arg( + Arg::with_name(options::FULL_TIME) + .long(options::FULL_TIME) + .overrides_with(options::FULL_TIME) + .help("like -l --time-style=full-iso") + ) + + // Positional arguments + .arg(Arg::with_name(options::PATHS).multiple(true).takes_value(true)) + + .after_help(AFTER_HELP) +} + +/// Represents a Path along with it's associated data +/// Any data that will be reused several times makes sense to be added to this structure +/// Caching data here helps eliminate redundant syscalls to fetch same information +struct PathData { + // Result got from symlink_metadata() or metadata() based on config + md: OnceCell>, + ft: OnceCell>, + // Name of the file - will be empty for . or .. + display_name: String, + // PathBuf that all above data corresponds to + p_buf: PathBuf, + must_dereference: bool, +} + +impl PathData { + fn new( + p_buf: PathBuf, + file_type: Option>, + file_name: Option, + config: &Config, + command_line: bool, + ) -> Self { + // We cannot use `Path::ends_with` or `Path::Components`, because they remove occurrences of '.' + // For '..', the filename is None + let display_name = if let Some(name) = file_name { + name + } else { + let display_os_str = if command_line { + p_buf.as_os_str() + } else { + p_buf + .file_name() + .unwrap_or_else(|| p_buf.iter().next_back().unwrap()) + }; + + display_os_str.to_string_lossy().into_owned() + }; + let must_dereference = match &config.dereference { + Dereference::All => true, + Dereference::Args => command_line, + Dereference::DirArgs => { + if command_line { + if let Ok(md) = p_buf.metadata() { + md.is_dir() + } else { + false + } + } else { + false + } + } + Dereference::None => false, + }; + let ft = match file_type { + Some(ft) => OnceCell::from(ft.ok()), + None => OnceCell::new(), + }; + + Self { + md: OnceCell::new(), + ft, + display_name, + p_buf, + must_dereference, } - } else if options.opt_present("S") { - entries.sort_by_key(|k| get_metadata(k, options).map(|md| md.size()).unwrap_or(0)); - reverse = !reverse; - } else if !options.opt_present("U") { - entries.sort(); } - if reverse { - entries.reverse(); + fn md(&self) -> Option<&Metadata> { + self.md + .get_or_init(|| get_metadata(&self.p_buf, self.must_dereference).ok()) + .as_ref() + } + + fn file_type(&self) -> Option<&FileType> { + self.ft + .get_or_init(|| self.md().map(|md| md.file_type())) + .as_ref() } } -#[cfg(windows)] -fn is_hidden(file_path: &DirEntry) -> std::io::Result { - let metadata = fs::metadata(file_path.path())?; - let attr = metadata.file_attributes(); - Ok(((attr & 0x2) > 0) || file_path.file_name().to_string_lossy().starts_with('.')) +fn list(locs: Vec, config: Config) -> UResult<()> { + let mut files = Vec::::new(); + let mut dirs = Vec::::new(); + + let mut out = BufWriter::new(stdout()); + + for loc in &locs { + let p = PathBuf::from(&loc); + let path_data = PathData::new(p, None, None, &config, true); + + if path_data.md().is_none() { + show!(std::io::ErrorKind::NotFound + .map_err_context(|| format!("cannot access '{}'", path_data.p_buf.display()))); + // We found an error, no need to continue the execution + continue; + } + + let show_dir_contents = match path_data.file_type() { + Some(ft) => !config.directory && ft.is_dir(), + None => { + set_exit_code(1); + false + } + }; + + if show_dir_contents { + dirs.push(path_data); + } else { + files.push(path_data); + } + } + sort_entries(&mut files, &config); + display_items(&files, &config, &mut out); + + sort_entries(&mut dirs, &config); + for dir in dirs { + if locs.len() > 1 || config.recursive { + let _ = writeln!(out, "\n{}:", dir.p_buf.display()); + } + enter_directory(&dir, &config, &mut out); + } + + Ok(()) } -#[cfg(unix)] -fn is_hidden(file_path: &DirEntry) -> std::io::Result { - Ok(file_path.file_name().to_string_lossy().starts_with('.')) -} - -#[cfg(windows)] -fn sort_entries(entries: &mut Vec, options: &getopts::Matches) { - let mut reverse = options.opt_present("r"); - if options.opt_present("t") { - entries.sort_by_key(|k| { - // Newest first +fn sort_entries(entries: &mut Vec, config: &Config) { + match config.sort { + Sort::Time => entries.sort_by_key(|k| { Reverse( - get_metadata(k, options) - .and_then(|md| md.modified()) - .unwrap_or(std::time::UNIX_EPOCH), + k.md() + .and_then(|md| get_system_time(md, config)) + .unwrap_or(UNIX_EPOCH), ) - }); - } else if options.opt_present("S") { - entries.sort_by_key(|k| { - get_metadata(k, options) - .map(|md| md.file_size()) - .unwrap_or(0) - }); - reverse = !reverse; - } else if !options.opt_present("U") { - entries.sort(); + }), + Sort::Size => { + entries.sort_by_key(|k| Reverse(k.md().as_ref().map(|md| md.len()).unwrap_or(0))) + } + // The default sort in GNU ls is case insensitive + Sort::Name => entries.sort_by(|a, b| a.display_name.cmp(&b.display_name)), + Sort::Version => entries.sort_by(|a, b| version_cmp::version_cmp(&a.p_buf, &b.p_buf)), + Sort::Extension => entries.sort_by(|a, b| { + a.p_buf + .extension() + .cmp(&b.p_buf.extension()) + .then(a.p_buf.file_stem().cmp(&b.p_buf.file_stem())) + }), + Sort::None => {} } - if reverse { + if config.reverse { entries.reverse(); } } -fn should_display(entry: &DirEntry, options: &getopts::Matches) -> bool { - let ffi_name = entry.file_name(); - let name = ffi_name.to_string_lossy(); - if !options.opt_present("a") && !options.opt_present("A") && is_hidden(entry).unwrap() { - return false; - } - if options.opt_present("B") && name.ends_with('~') { - return false; - } - true +#[cfg(windows)] +fn is_hidden(file_path: &DirEntry) -> bool { + let path = file_path.path(); + let metadata = fs::metadata(&path).unwrap_or_else(|_| fs::symlink_metadata(&path).unwrap()); + let attr = metadata.file_attributes(); + (attr & 0x2) > 0 } -fn enter_directory(dir: &PathBuf, options: &getopts::Matches) { - let mut entries: Vec<_> = safe_unwrap!(fs::read_dir(dir).and_then(Iterator::collect)); +fn should_display(entry: &DirEntry, config: &Config) -> bool { + let ffi_name = entry.file_name(); - entries.retain(|e| should_display(e, options)); - - let mut entries: Vec<_> = entries.iter().map(DirEntry::path).collect(); - sort_entries(&mut entries, options); - - if options.opt_present("a") { - let mut display_entries = entries.clone(); - display_entries.insert(0, dir.join("..")); - display_entries.insert(0, dir.join(".")); - display_items(&display_entries, Some(dir), options); - } else { - display_items(&entries, Some(dir), options); + // For unix, the hidden files are already included in the ignore pattern + #[cfg(windows)] + { + if config.files == Files::Normal && is_hidden(entry) { + return false; + } } - if options.opt_present("R") { - for e in entries.iter().filter(|p| p.is_dir()) { - println!("\n{}:", e.to_string_lossy()); - enter_directory(&e, options); + !config.ignore_patterns.is_match(&ffi_name) +} + +fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter) { + let mut entries: Vec<_> = if config.files == Files::All { + vec![ + PathData::new( + dir.p_buf.clone(), + Some(Ok(*dir.file_type().unwrap())), + Some(".".into()), + config, + false, + ), + PathData::new(dir.p_buf.join(".."), None, Some("..".into()), config, false), + ] + } else { + vec![] + }; + + let mut temp: Vec<_> = safe_unwrap!(fs::read_dir(&dir.p_buf)) + .map(|res| safe_unwrap!(res)) + .filter(|e| should_display(e, config)) + .map(|e| PathData::new(DirEntry::path(&e), Some(e.file_type()), None, config, false)) + .collect(); + + sort_entries(&mut temp, config); + + entries.append(&mut temp); + + display_items(&entries, config, out); + + if config.recursive { + for e in entries + .iter() + .skip(if config.files == Files::All { 2 } else { 0 }) + .filter(|p| p.file_type().map(|ft| ft.is_dir()).unwrap_or(false)) + { + let _ = writeln!(out, "\n{}:", e.p_buf.display()); + enter_directory(e, config, out); } } } -fn get_metadata(entry: &PathBuf, options: &getopts::Matches) -> std::io::Result { - if options.opt_present("L") { - entry.metadata().or_else(|_| entry.symlink_metadata()) +fn get_metadata(entry: &Path, dereference: bool) -> std::io::Result { + if dereference { + entry.metadata() } else { entry.symlink_metadata() } } -fn display_dir_entry_size(entry: &PathBuf, options: &getopts::Matches) -> (usize, usize) { - if let Ok(md) = get_metadata(entry, options) { +fn display_dir_entry_size(entry: &PathData, config: &Config) -> (usize, usize) { + if let Some(md) = entry.md() { ( - display_symlink_count(&md).len(), - display_file_size(&md, options).len(), + display_symlink_count(md).len(), + display_size_or_rdev(md, config).len(), ) } else { (0, 0) @@ -341,64 +1372,116 @@ fn display_dir_entry_size(entry: &PathBuf, options: &getopts::Matches) -> (usize } fn pad_left(string: String, count: usize) -> String { - if count > string.len() { - let pad = count - string.len(); - let pad = String::from_utf8(vec![b' '; pad]).unwrap(); - format!("{}{}", pad, string) + format!("{:>width$}", string, width = count) +} + +fn display_items(items: &[PathData], config: &Config, out: &mut BufWriter) { + if config.format == Format::Long { + let (mut max_links, mut max_width) = (1, 1); + let mut total_size = 0; + + for item in items { + let (links, width) = display_dir_entry_size(item, config); + max_links = links.max(max_links); + max_width = width.max(max_width); + total_size += item.md().map_or(0, |md| get_block_size(md, config)); + } + + if total_size > 0 { + let _ = writeln!(out, "total {}", display_size(total_size, config)); + } + + for item in items { + display_item_long(item, max_links, max_width, config, out); + } } else { - string + let names = items.iter().filter_map(|i| display_file_name(i, config)); + + match (&config.format, config.width) { + (Format::Columns, Some(width)) => { + display_grid(names, width, Direction::TopToBottom, out) + } + (Format::Across, Some(width)) => { + display_grid(names, width, Direction::LeftToRight, out) + } + (Format::Commas, width_opt) => { + let term_width = width_opt.unwrap_or(1); + let mut current_col = 0; + let mut names = names; + if let Some(name) = names.next() { + let _ = write!(out, "{}", name.contents); + current_col = name.width as u16 + 2; + } + for name in names { + let name_width = name.width as u16; + if current_col + name_width + 1 > term_width { + current_col = name_width + 2; + let _ = write!(out, ",\n{}", name.contents); + } else { + current_col += name_width + 2; + let _ = write!(out, ", {}", name.contents); + } + } + // Current col is never zero again if names have been printed. + // So we print a newline. + if current_col > 0 { + let _ = writeln!(out,); + } + } + _ => { + for name in names { + let _ = writeln!(out, "{}", name.contents); + } + } + } } } -fn display_items(items: &[PathBuf], strip: Option<&Path>, options: &getopts::Matches) { - if options.opt_present("long") || options.opt_present("numeric-uid-gid") { - let (mut max_links, mut max_size) = (1, 1); - for item in items { - let (links, size) = display_dir_entry_size(item, options); - max_links = links.max(max_links); - max_size = size.max(max_size); +fn get_block_size(md: &Metadata, config: &Config) -> u64 { + /* GNU ls will display sizes in terms of block size + md.len() will differ from this value when the file has some holes + */ + #[cfg(unix)] + { + // hard-coded for now - enabling setting this remains a TODO + let ls_block_size = 1024; + match config.size_format { + SizeFormat::Binary => md.blocks() * 512, + SizeFormat::Decimal => md.blocks() * 512, + SizeFormat::Bytes => md.blocks() * 512 / ls_block_size, } - for item in items { - display_item_long(item, strip, max_links, max_size, options); + } + + #[cfg(not(unix))] + { + let _ = config; + // no way to get block size for windows, fall-back to file size + md.len() + } +} + +fn display_grid( + names: impl Iterator, + width: u16, + direction: Direction, + out: &mut BufWriter, +) { + let mut grid = Grid::new(GridOptions { + filling: Filling::Spaces(2), + direction, + }); + + for name in names { + grid.add(name); + } + + match grid.fit_into_width(width as usize) { + Some(output) => { + let _ = write!(out, "{}", output); } - } else { - if !options.opt_present("1") { - let names = items.iter().filter_map(|i| { - let md = get_metadata(i, options); - match md { - Err(e) => { - let filename = get_file_name(i, strip); - show_error!("'{}': {}", filename, e); - None - } - Ok(md) => Some(display_file_name(&i, strip, &md, options)), - } - }); - - if let Some(size) = termsize::get() { - let mut grid = Grid::new(GridOptions { - filling: Filling::Spaces(2), - direction: Direction::TopToBottom, - }); - - for name in names { - grid.add(name); - } - - if let Some(output) = grid.fit_into_width(size.cols as usize) { - print!("{}", output); - return; - } - } - } - - // Couldn't display a grid, either because we don't know - // the terminal width or because fit_into_width failed - for i in items { - let md = get_metadata(i, options); - if let Ok(md) = md { - println!("{}", display_file_name(&i, strip, &md, options).contents); - } + // Width is too small for the grid, so we fit it in one column + None => { + let _ = write!(out, "{}", grid.fit_into_columns(1)); } } } @@ -406,312 +1489,339 @@ fn display_items(items: &[PathBuf], strip: Option<&Path>, options: &getopts::Mat use uucore::fs::display_permissions; fn display_item_long( - item: &PathBuf, - strip: Option<&Path>, + item: &PathData, max_links: usize, max_size: usize, - options: &getopts::Matches, + config: &Config, + out: &mut BufWriter, ) { - let md = match get_metadata(item, options) { - Err(e) => { - let filename = get_file_name(&item, strip); - show_error!("{}: {}", filename, e); + let md = match item.md() { + None => { + show!(LsError::NoMetadata(item.p_buf.clone())); return; } - Ok(md) => md, + Some(md) => md, }; - println!( - "{}{}{} {} {} {} {} {} {}", - get_inode(&md, options), - display_file_type(md.file_type()), - display_permissions(&md), - pad_left(display_symlink_count(&md), max_links), - display_uname(&md, options), - display_group(&md, options), - pad_left(display_file_size(&md, options), max_size), - display_date(&md, options), - display_file_name(&item, strip, &md, options).contents + #[cfg(unix)] + { + if config.inode { + let _ = write!(out, "{} ", get_inode(md)); + } + } + + let _ = write!( + out, + "{} {}", + display_permissions(md, true), + pad_left(display_symlink_count(md), max_links), + ); + + if config.long.owner { + let _ = write!(out, " {}", display_uname(md, config)); + } + + if config.long.group { + let _ = write!(out, " {}", display_group(md, config)); + } + + // Author is only different from owner on GNU/Hurd, so we reuse + // the owner, since GNU/Hurd is not currently supported by Rust. + if config.long.author { + let _ = write!(out, " {}", display_uname(md, config)); + } + + let _ = writeln!( + out, + " {} {} {}", + pad_left(display_size_or_rdev(md, config), max_size), + display_date(md, config), + // unwrap is fine because it fails when metadata is not available + // but we already know that it is because it's checked at the + // start of the function. + display_file_name(item, config).unwrap().contents, ); } #[cfg(unix)] -fn get_inode(metadata: &Metadata, options: &getopts::Matches) -> String { - if options.opt_present("inode") { - format!("{:8} ", metadata.ino()) - } else { - "".to_string() - } -} - -#[cfg(not(unix))] -fn get_inode(_metadata: &Metadata, _options: &getopts::Matches) -> String { - "".to_string() +fn get_inode(metadata: &Metadata) -> String { + format!("{:8}", metadata.ino()) } // Currently getpwuid is `linux` target only. If it's broken out into // a posix-compliant attribute this can be updated... #[cfg(unix)] +use std::sync::Mutex; +#[cfg(unix)] use uucore::entries; +use uucore::InvalidEncodingHandling; #[cfg(unix)] -fn display_uname(metadata: &Metadata, options: &getopts::Matches) -> String { - if options.opt_present("numeric-uid-gid") { +fn cached_uid2usr(uid: u32) -> String { + lazy_static! { + static ref UID_CACHE: Mutex> = Mutex::new(HashMap::new()); + } + + let mut uid_cache = UID_CACHE.lock().unwrap(); + uid_cache + .entry(uid) + .or_insert_with(|| entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string())) + .clone() +} + +#[cfg(unix)] +fn display_uname(metadata: &Metadata, config: &Config) -> String { + if config.long.numeric_uid_gid { metadata.uid().to_string() } else { - entries::uid2usr(metadata.uid()).unwrap_or_else(|_| metadata.uid().to_string()) + cached_uid2usr(metadata.uid()) } } #[cfg(unix)] -fn display_group(metadata: &Metadata, options: &getopts::Matches) -> String { - if options.opt_present("numeric-uid-gid") { +fn cached_gid2grp(gid: u32) -> String { + lazy_static! { + static ref GID_CACHE: Mutex> = Mutex::new(HashMap::new()); + } + + let mut gid_cache = GID_CACHE.lock().unwrap(); + gid_cache + .entry(gid) + .or_insert_with(|| entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string())) + .clone() +} + +#[cfg(unix)] +fn display_group(metadata: &Metadata, config: &Config) -> String { + if config.long.numeric_uid_gid { metadata.gid().to_string() } else { - entries::gid2grp(metadata.gid()).unwrap_or_else(|_| metadata.gid().to_string()) + cached_gid2grp(metadata.gid()) } } #[cfg(not(unix))] -#[allow(unused_variables)] -fn display_uname(metadata: &Metadata, _options: &getopts::Matches) -> String { +fn display_uname(_metadata: &Metadata, _config: &Config) -> String { "somebody".to_string() } #[cfg(not(unix))] -#[allow(unused_variables)] -fn display_group(metadata: &Metadata, _options: &getopts::Matches) -> String { +fn display_group(_metadata: &Metadata, _config: &Config) -> String { "somegroup".to_string() } +// The implementations for get_time are separated because some options, such +// as ctime will not be available #[cfg(unix)] -fn display_date(metadata: &Metadata, options: &getopts::Matches) -> String { - let secs = if options.opt_present("c") { - metadata.ctime() - } else { - metadata.mtime() - }; - let time = time::at(Timespec::new(secs, 0)); - strftime("%F %R", &time).unwrap() -} - -#[cfg(not(unix))] -#[allow(unused_variables)] -fn display_date(metadata: &Metadata, options: &getopts::Matches) -> String { - if let Ok(mtime) = metadata.modified() { - let time = time::at(Timespec::new( - mtime - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_secs() as i64, - 0, - )); - strftime("%F %R", &time).unwrap() - } else { - "???".to_string() +fn get_system_time(md: &Metadata, config: &Config) -> Option { + match config.time { + Time::Change => Some(UNIX_EPOCH + Duration::new(md.ctime() as u64, md.ctime_nsec() as u32)), + Time::Modification => md.modified().ok(), + Time::Access => md.accessed().ok(), + Time::Birth => md.created().ok(), } } -fn display_file_size(metadata: &Metadata, options: &getopts::Matches) -> String { - if options.opt_present("human-readable") { - match NumberPrefix::decimal(metadata.len() as f64) { - NumberPrefix::Standalone(bytes) => bytes.to_string(), - NumberPrefix::Prefixed(prefix, bytes) => { - format!("{:.2}{}", bytes, prefix).to_uppercase() +#[cfg(not(unix))] +fn get_system_time(md: &Metadata, config: &Config) -> Option { + match config.time { + Time::Modification => md.modified().ok(), + Time::Access => md.accessed().ok(), + Time::Birth => md.created().ok(), + _ => None, + } +} + +fn get_time(md: &Metadata, config: &Config) -> Option> { + let time = get_system_time(md, config)?; + Some(time.into()) +} + +fn display_date(metadata: &Metadata, config: &Config) -> String { + match get_time(metadata, config) { + Some(time) => { + //Date is recent if from past 6 months + //According to GNU a Gregorian year has 365.2425 * 24 * 60 * 60 == 31556952 seconds on the average. + let recent = time + chrono::Duration::seconds(31_556_952 / 2) > chrono::Local::now(); + + match config.time_style { + TimeStyle::FullIso => time.format("%Y-%m-%d %H:%M:%S.%f %z"), + TimeStyle::LongIso => time.format("%Y-%m-%d %H:%M"), + TimeStyle::Iso => time.format(if recent { "%m-%d %H:%M" } else { "%Y-%m-%d " }), + TimeStyle::Locale => { + let fmt = if recent { "%b %e %H:%M" } else { "%b %e %Y" }; + + // spell-checker:ignore (word) datetime + //In this version of chrono translating can be done + //The function is chrono::datetime::DateTime::format_localized + //However it's currently still hard to get the current pure-rust-locale + //So it's not yet implemented + + time.format(fmt) + } } + .to_string() } - } else { - metadata.len().to_string() + None => "???".into(), } } -fn display_file_type(file_type: FileType) -> String { - if file_type.is_dir() { - "d".to_string() - } else if file_type.is_symlink() { - "l".to_string() - } else { - "-".to_string() - } -} +// There are a few peculiarities to how GNU formats the sizes: +// 1. One decimal place is given if and only if the size is smaller than 10 +// 2. It rounds sizes up. +// 3. The human-readable format uses powers for 1024, but does not display the "i" +// that is commonly used to denote Kibi, Mebi, etc. +// 4. Kibi and Kilo are denoted differently ("k" and "K", respectively) +fn format_prefixed(prefixed: NumberPrefix) -> String { + match prefixed { + NumberPrefix::Standalone(bytes) => bytes.to_string(), + NumberPrefix::Prefixed(prefix, bytes) => { + // Remove the "i" from "Ki", "Mi", etc. if present + let prefix_str = prefix.symbol().trim_end_matches('i'); -fn get_file_name(name: &Path, strip: Option<&Path>) -> String { - let mut name = match strip { - Some(prefix) => name.strip_prefix(prefix).unwrap_or(name), - None => name, - }; - if name.as_os_str().is_empty() { - name = Path::new("."); - } - name.to_string_lossy().into_owned() -} - -#[cfg(not(unix))] -fn display_file_name( - path: &Path, - strip: Option<&Path>, - metadata: &Metadata, - options: &getopts::Matches, -) -> Cell { - let mut name = get_file_name(path, strip); - - if !options.opt_present("long") { - name = get_inode(metadata, options) + &name; - } - - if options.opt_present("classify") { - let file_type = metadata.file_type(); - if file_type.is_dir() { - name.push('/'); - } else if file_type.is_symlink() { - name.push('@'); - } - } - - if options.opt_present("long") && metadata.file_type().is_symlink() { - if let Ok(target) = path.read_link() { - // We don't bother updating width here because it's not used for long listings - let target_name = target.to_string_lossy().to_string(); - name.push_str(" -> "); - name.push_str(&target_name); - } - } - - name.into() -} - -#[cfg(unix)] -fn color_name(name: String, typ: &str) -> String { - let mut typ = typ; - if !COLOR_MAP.contains_key(typ) { - if typ == "or" { - typ = "ln"; - } else if typ == "mi" { - typ = "fi"; - } - }; - if let Some(code) = COLOR_MAP.get(typ) { - format!( - "{}{}{}{}{}{}{}{}", - *LEFT_CODE, code, *RIGHT_CODE, name, *END_CODE, *LEFT_CODE, *RESET_CODE, *RIGHT_CODE, - ) - } else { - name - } -} - -#[cfg(unix)] -macro_rules! has { - ($mode:expr, $perm:expr) => { - $mode & ($perm as mode_t) != 0 - }; -} - -#[cfg(unix)] -#[allow(clippy::cognitive_complexity)] -fn display_file_name( - path: &Path, - strip: Option<&Path>, - metadata: &Metadata, - options: &getopts::Matches, -) -> Cell { - let mut name = get_file_name(path, strip); - if !options.opt_present("long") { - name = get_inode(metadata, options) + &name; - } - let mut width = UnicodeWidthStr::width(&*name); - - let color = match options.opt_str("color") { - None => atty::is(atty::Stream::Stdout), - Some(val) => match val.as_ref() { - "always" | "yes" | "force" => true, - "auto" | "tty" | "if-tty" => atty::is(atty::Stream::Stdout), - /* "never" | "no" | "none" | */ _ => false, - }, - }; - let classify = options.opt_present("classify"); - let ext; - - if color || classify { - let file_type = metadata.file_type(); - - let (code, sym) = if file_type.is_dir() { - ("di", Some('/')) - } else if file_type.is_symlink() { - if path.exists() { - ("ln", Some('@')) + // Check whether we get more than 10 if we round up to the first decimal + // because we want do display 9.81 as "9.9", not as "10". + if (10.0 * bytes).ceil() >= 100.0 { + format!("{:.0}{}", bytes.ceil(), prefix_str) } else { - ("or", Some('@')) + format!("{:.1}{}", (10.0 * bytes).ceil() / 10.0, prefix_str) } - } else if file_type.is_socket() { - ("so", Some('=')) - } else if file_type.is_fifo() { - ("pi", Some('|')) - } else if file_type.is_block_device() { - ("bd", None) - } else if file_type.is_char_device() { - ("cd", None) - } else if file_type.is_file() { - let mode = metadata.mode() as mode_t; - let sym = if has!(mode, S_IXUSR | S_IXGRP | S_IXOTH) { + } + } +} + +fn display_size_or_rdev(metadata: &Metadata, config: &Config) -> String { + #[cfg(unix)] + { + let ft = metadata.file_type(); + if ft.is_char_device() || ft.is_block_device() { + let dev: u64 = metadata.rdev(); + let major = (dev >> 8) as u8; + let minor = dev as u8; + return format!("{}, {}", major, minor); + } + } + + display_size(metadata.len(), config) +} + +fn display_size(size: u64, config: &Config) -> String { + // NOTE: The human-readable behavior deviates from the GNU ls. + // The GNU ls uses binary prefixes by default. + match config.size_format { + SizeFormat::Binary => format_prefixed(NumberPrefix::binary(size as f64)), + SizeFormat::Decimal => format_prefixed(NumberPrefix::decimal(size as f64)), + SizeFormat::Bytes => size.to_string(), + } +} + +#[cfg(unix)] +fn file_is_executable(md: &Metadata) -> bool { + // Mode always returns u32, but the flags might not be, based on the platform + // e.g. linux has u32, mac has u16. + // S_IXUSR -> user has execute permission + // S_IXGRP -> group has execute permission + // S_IXOTH -> other users have execute permission + md.mode() & ((S_IXUSR | S_IXGRP | S_IXOTH) as u32) != 0 +} + +fn classify_file(path: &PathData) -> Option { + let file_type = path.file_type()?; + + if file_type.is_dir() { + Some('/') + } else if file_type.is_symlink() { + Some('@') + } else { + #[cfg(unix)] + { + if file_type.is_socket() { + Some('=') + } else if file_type.is_fifo() { + Some('|') + } else if file_type.is_file() && file_is_executable(path.md()?) { Some('*') } else { None - }; - if has!(mode, S_ISUID) { - ("su", sym) - } else if has!(mode, S_ISGID) { - ("sg", sym) - } else if has!(mode, S_ISVTX) && has!(mode, S_IWOTH) { - ("tw", sym) - } else if has!(mode, S_ISVTX) { - ("st", sym) - } else if has!(mode, S_IWOTH) { - ("ow", sym) - } else if has!(mode, S_IXUSR | S_IXGRP | S_IXOTH) { - ("ex", sym) - } else if metadata.nlink() > 1 { - ("mh", sym) - } else if let Some(e) = path.extension() { - ext = format!("*.{}", e.to_string_lossy()); - (ext.as_str(), None) - } else { - ("fi", None) } - } else { - ("", None) + } + #[cfg(not(unix))] + None + } +} + +fn display_file_name(path: &PathData, config: &Config) -> Option { + let mut name = escape_name(&path.display_name, &config.quoting_style); + + #[cfg(unix)] + { + if config.format != Format::Long && config.inode { + name = path + .md() + .map_or_else(|| "?".to_string(), |md| get_inode(md)) + + " " + + &name; + } + } + + // We need to keep track of the width ourselves instead of letting term_grid + // infer it because the color codes mess up term_grid's width calculation. + let mut width = name.width(); + + if let Some(ls_colors) = &config.color { + name = color_name(ls_colors, &path.p_buf, name, path.md()?); + } + + if config.indicator_style != IndicatorStyle::None { + let sym = classify_file(path); + + let char_opt = match config.indicator_style { + IndicatorStyle::Classify => sym, + IndicatorStyle::FileType => { + // Don't append an asterisk. + match sym { + Some('*') => None, + _ => sym, + } + } + IndicatorStyle::Slash => { + // Append only a slash. + match sym { + Some('/') => Some('/'), + _ => None, + } + } + IndicatorStyle::None => None, }; - if color { - name = color_name(name, code); - } - if classify { - if let Some(s) = sym { - name.push(s); - width += 1; - } + if let Some(c) = char_opt { + name.push(c); + width += 1; } } - if options.opt_present("long") && metadata.file_type().is_symlink() { - if let Ok(target) = path.read_link() { - // We don't bother updating width here because it's not used for long listings - let code = if target.exists() { "fi" } else { "mi" }; - let target_name = color_name(target.to_string_lossy().to_string(), code); + if config.format == Format::Long && path.file_type()?.is_symlink() { + if let Ok(target) = path.p_buf.read_link() { name.push_str(" -> "); - name.push_str(&target_name); + name.push_str(&target.to_string_lossy()); } } - Cell { + Some(Cell { contents: name, width, + }) +} + +fn color_name(ls_colors: &LsColors, path: &Path, name: String, md: &Metadata) -> String { + match ls_colors.style_for_path_with_metadata(path, Some(md)) { + Some(style) => style.to_ansi_term_style().paint(name).to_string(), + None => name, } } #[cfg(not(unix))] -#[allow(unused_variables)] -fn display_symlink_count(metadata: &Metadata) -> String { +fn display_symlink_count(_metadata: &Metadata) -> String { // Currently not sure of how to get this on Windows, so I'm punting. // Git Bash looks like it may do the same thing. String::from("1") diff --git a/src/uu/ls/src/main.rs b/src/uu/ls/src/main.rs index f45314577..d867c3843 100644 --- a/src/uu/ls/src/main.rs +++ b/src/uu/ls/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_ls); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_ls); diff --git a/src/uu/ls/src/quoting_style.rs b/src/uu/ls/src/quoting_style.rs new file mode 100644 index 000000000..f9ba55f7b --- /dev/null +++ b/src/uu/ls/src/quoting_style.rs @@ -0,0 +1,662 @@ +use std::char::from_digit; + +const SPECIAL_SHELL_CHARS: &str = "~`#$&*()|[]{};\\'\"<>?! "; + +pub(super) enum QuotingStyle { + Shell { + escape: bool, + always_quote: bool, + show_control: bool, + }, + C { + quotes: Quotes, + }, + Literal { + show_control: bool, + }, +} + +#[derive(Clone, Copy)] +pub(super) enum Quotes { + None, + Single, + Double, + // TODO: Locale +} + +// This implementation is heavily inspired by the std::char::EscapeDefault implementation +// in the Rust standard library. This custom implementation is needed because the +// characters \a, \b, \e, \f & \v are not recognized by Rust. +struct EscapedChar { + state: EscapeState, +} + +enum EscapeState { + Done, + Char(char), + Backslash(char), + ForceQuote(char), + Octal(EscapeOctal), +} + +struct EscapeOctal { + c: char, + state: EscapeOctalState, + idx: usize, +} + +enum EscapeOctalState { + Done, + Backslash, + Value, +} + +impl Iterator for EscapeOctal { + type Item = char; + + fn next(&mut self) -> Option { + match self.state { + EscapeOctalState::Done => None, + EscapeOctalState::Backslash => { + self.state = EscapeOctalState::Value; + Some('\\') + } + EscapeOctalState::Value => { + let octal_digit = ((self.c as u32) >> (self.idx * 3)) & 0o7; + if self.idx == 0 { + self.state = EscapeOctalState::Done; + } else { + self.idx -= 1; + } + Some(from_digit(octal_digit, 8).unwrap()) + } + } + } +} + +impl EscapeOctal { + fn from(c: char) -> EscapeOctal { + EscapeOctal { + c, + idx: 2, + state: EscapeOctalState::Backslash, + } + } +} + +impl EscapedChar { + fn new_literal(c: char) -> Self { + Self { + state: EscapeState::Char(c), + } + } + + fn new_c(c: char, quotes: Quotes) -> Self { + use EscapeState::*; + let init_state = match c { + '\x07' => Backslash('a'), + '\x08' => Backslash('b'), + '\t' => Backslash('t'), + '\n' => Backslash('n'), + '\x0B' => Backslash('v'), + '\x0C' => Backslash('f'), + '\r' => Backslash('r'), + '\\' => Backslash('\\'), + '\'' => match quotes { + Quotes::Single => Backslash('\''), + _ => Char('\''), + }, + '"' => match quotes { + Quotes::Double => Backslash('"'), + _ => Char('"'), + }, + ' ' => match quotes { + Quotes::None => Backslash(' '), + _ => Char(' '), + }, + _ if c.is_ascii_control() => Octal(EscapeOctal::from(c)), + _ => Char(c), + }; + Self { state: init_state } + } + + fn new_shell(c: char, escape: bool, quotes: Quotes) -> Self { + use EscapeState::*; + let init_state = match c { + _ if !escape && c.is_control() => Char(c), + '\x07' => Backslash('a'), + '\x08' => Backslash('b'), + '\t' => Backslash('t'), + '\n' => Backslash('n'), + '\x0B' => Backslash('v'), + '\x0C' => Backslash('f'), + '\r' => Backslash('r'), + '\x00'..='\x1F' | '\x7F' => Octal(EscapeOctal::from(c)), + '\'' => match quotes { + Quotes::Single => Backslash('\''), + _ => Char('\''), + }, + _ if SPECIAL_SHELL_CHARS.contains(c) => ForceQuote(c), + _ => Char(c), + }; + Self { state: init_state } + } + + fn hide_control(self) -> Self { + match self.state { + EscapeState::Char(c) if c.is_control() => Self { + state: EscapeState::Char('?'), + }, + _ => self, + } + } +} + +impl Iterator for EscapedChar { + type Item = char; + + fn next(&mut self) -> Option { + match self.state { + EscapeState::Backslash(c) => { + self.state = EscapeState::Char(c); + Some('\\') + } + EscapeState::Char(c) | EscapeState::ForceQuote(c) => { + self.state = EscapeState::Done; + Some(c) + } + EscapeState::Done => None, + EscapeState::Octal(ref mut iter) => iter.next(), + } + } +} + +fn shell_without_escape(name: &str, quotes: Quotes, show_control_chars: bool) -> (String, bool) { + let mut must_quote = false; + let mut escaped_str = String::with_capacity(name.len()); + + for c in name.chars() { + let escaped = { + let ec = EscapedChar::new_shell(c, false, quotes); + if show_control_chars { + ec + } else { + ec.hide_control() + } + }; + + match escaped.state { + EscapeState::Backslash('\'') => escaped_str.push_str("'\\''"), + EscapeState::ForceQuote(x) => { + must_quote = true; + escaped_str.push(x); + } + _ => { + for char in escaped { + escaped_str.push(char); + } + } + } + } + (escaped_str, must_quote) +} + +fn shell_with_escape(name: &str, quotes: Quotes) -> (String, bool) { + // We need to keep track of whether we are in a dollar expression + // because e.g. \b\n is escaped as $'\b\n' and not like $'b'$'n' + let mut in_dollar = false; + let mut must_quote = false; + let mut escaped_str = String::with_capacity(name.len()); + + for c in name.chars() { + let escaped = EscapedChar::new_shell(c, true, quotes); + match escaped.state { + EscapeState::Char(x) => { + if in_dollar { + escaped_str.push_str("''"); + in_dollar = false; + } + escaped_str.push(x); + } + EscapeState::ForceQuote(x) => { + if in_dollar { + escaped_str.push_str("''"); + in_dollar = false; + } + must_quote = true; + escaped_str.push(x); + } + // Single quotes are not put in dollar expressions, but are escaped + // if the string also contains double quotes. In that case, they must + // be handled separately. + EscapeState::Backslash('\'') => { + must_quote = true; + in_dollar = false; + escaped_str.push_str("'\\''"); + } + _ => { + if !in_dollar { + escaped_str.push_str("'$'"); + in_dollar = true; + } + must_quote = true; + for char in escaped { + escaped_str.push(char); + } + } + } + } + (escaped_str, must_quote) +} + +pub(super) fn escape_name(name: &str, style: &QuotingStyle) -> String { + match style { + QuotingStyle::Literal { show_control } => { + if !show_control { + name.chars() + .flat_map(|c| EscapedChar::new_literal(c).hide_control()) + .collect() + } else { + name.into() + } + } + QuotingStyle::C { quotes } => { + let escaped_str: String = name + .chars() + .flat_map(|c| EscapedChar::new_c(c, *quotes)) + .collect(); + + match quotes { + Quotes::Single => format!("'{}'", escaped_str), + Quotes::Double => format!("\"{}\"", escaped_str), + _ => escaped_str, + } + } + QuotingStyle::Shell { + escape, + always_quote, + show_control, + } => { + let (quotes, must_quote) = if name.contains('"') { + (Quotes::Single, true) + } else if name.contains('\'') { + (Quotes::Double, true) + } else if *always_quote { + (Quotes::Single, true) + } else { + (Quotes::Single, false) + }; + + let (escaped_str, contains_quote_chars) = if *escape { + shell_with_escape(name, quotes) + } else { + shell_without_escape(name, quotes, *show_control) + }; + + match (must_quote | contains_quote_chars, quotes) { + (true, Quotes::Single) => format!("'{}'", escaped_str), + (true, Quotes::Double) => format!("\"{}\"", escaped_str), + _ => escaped_str, + } + } + } +} + +#[cfg(test)] +mod tests { + // spell-checker:ignore (tests/words) one\'two one'two + + use crate::quoting_style::{escape_name, Quotes, QuotingStyle}; + fn get_style(s: &str) -> QuotingStyle { + match s { + "literal" => QuotingStyle::Literal { + show_control: false, + }, + "literal-show" => QuotingStyle::Literal { show_control: true }, + "escape" => QuotingStyle::C { + quotes: Quotes::None, + }, + "c" => QuotingStyle::C { + quotes: Quotes::Double, + }, + "shell" => QuotingStyle::Shell { + escape: false, + always_quote: false, + show_control: false, + }, + "shell-show" => QuotingStyle::Shell { + escape: false, + always_quote: false, + show_control: true, + }, + "shell-always" => QuotingStyle::Shell { + escape: false, + always_quote: true, + show_control: false, + }, + "shell-always-show" => QuotingStyle::Shell { + escape: false, + always_quote: true, + show_control: true, + }, + "shell-escape" => QuotingStyle::Shell { + escape: true, + always_quote: false, + show_control: false, + }, + "shell-escape-always" => QuotingStyle::Shell { + escape: true, + always_quote: true, + show_control: false, + }, + _ => panic!("Invalid name!"), + } + } + + fn check_names(name: &str, map: Vec<(&str, &str)>) { + assert_eq!( + map.iter() + .map(|(_, style)| escape_name(name, &get_style(style))) + .collect::>(), + map.iter() + .map(|(correct, _)| correct.to_string()) + .collect::>() + ); + } + + #[test] + fn test_simple_names() { + check_names( + "one_two", + vec![ + ("one_two", "literal"), + ("one_two", "literal-show"), + ("one_two", "escape"), + ("\"one_two\"", "c"), + ("one_two", "shell"), + ("one_two", "shell-show"), + ("\'one_two\'", "shell-always"), + ("\'one_two\'", "shell-always-show"), + ("one_two", "shell-escape"), + ("\'one_two\'", "shell-escape-always"), + ], + ); + } + + #[test] + fn test_spaces() { + check_names( + "one two", + vec![ + ("one two", "literal"), + ("one two", "literal-show"), + ("one\\ two", "escape"), + ("\"one two\"", "c"), + ("\'one two\'", "shell"), + ("\'one two\'", "shell-show"), + ("\'one two\'", "shell-always"), + ("\'one two\'", "shell-always-show"), + ("\'one two\'", "shell-escape"), + ("\'one two\'", "shell-escape-always"), + ], + ); + + check_names( + " one", + vec![ + (" one", "literal"), + (" one", "literal-show"), + ("\\ one", "escape"), + ("\" one\"", "c"), + ("' one'", "shell"), + ("' one'", "shell-show"), + ("' one'", "shell-always"), + ("' one'", "shell-always-show"), + ("' one'", "shell-escape"), + ("' one'", "shell-escape-always"), + ], + ); + } + + #[test] + fn test_quotes() { + // One double quote + check_names( + "one\"two", + vec![ + ("one\"two", "literal"), + ("one\"two", "literal-show"), + ("one\"two", "escape"), + ("\"one\\\"two\"", "c"), + ("'one\"two'", "shell"), + ("'one\"two'", "shell-show"), + ("'one\"two'", "shell-always"), + ("'one\"two'", "shell-always-show"), + ("'one\"two'", "shell-escape"), + ("'one\"two'", "shell-escape-always"), + ], + ); + + // One single quote + check_names( + "one\'two", + vec![ + ("one'two", "literal"), + ("one'two", "literal-show"), + ("one'two", "escape"), + ("\"one'two\"", "c"), + ("\"one'two\"", "shell"), + ("\"one'two\"", "shell-show"), + ("\"one'two\"", "shell-always"), + ("\"one'two\"", "shell-always-show"), + ("\"one'two\"", "shell-escape"), + ("\"one'two\"", "shell-escape-always"), + ], + ); + + // One single quote and one double quote + check_names( + "one'two\"three", + vec![ + ("one'two\"three", "literal"), + ("one'two\"three", "literal-show"), + ("one'two\"three", "escape"), + ("\"one'two\\\"three\"", "c"), + ("'one'\\''two\"three'", "shell"), + ("'one'\\''two\"three'", "shell-show"), + ("'one'\\''two\"three'", "shell-always"), + ("'one'\\''two\"three'", "shell-always-show"), + ("'one'\\''two\"three'", "shell-escape"), + ("'one'\\''two\"three'", "shell-escape-always"), + ], + ); + + // Consecutive quotes + check_names( + "one''two\"\"three", + vec![ + ("one''two\"\"three", "literal"), + ("one''two\"\"three", "literal-show"), + ("one''two\"\"three", "escape"), + ("\"one''two\\\"\\\"three\"", "c"), + ("'one'\\'''\\''two\"\"three'", "shell"), + ("'one'\\'''\\''two\"\"three'", "shell-show"), + ("'one'\\'''\\''two\"\"three'", "shell-always"), + ("'one'\\'''\\''two\"\"three'", "shell-always-show"), + ("'one'\\'''\\''two\"\"three'", "shell-escape"), + ("'one'\\'''\\''two\"\"three'", "shell-escape-always"), + ], + ); + } + + #[test] + fn test_control_chars() { + // A simple newline + check_names( + "one\ntwo", + vec![ + ("one?two", "literal"), + ("one\ntwo", "literal-show"), + ("one\\ntwo", "escape"), + ("\"one\\ntwo\"", "c"), + ("one?two", "shell"), + ("one\ntwo", "shell-show"), + ("'one?two'", "shell-always"), + ("'one\ntwo'", "shell-always-show"), + ("'one'$'\\n''two'", "shell-escape"), + ("'one'$'\\n''two'", "shell-escape-always"), + ], + ); + + // A control character followed by a special shell character + check_names( + "one\n&two", + vec![ + ("one?&two", "literal"), + ("one\n&two", "literal-show"), + ("one\\n&two", "escape"), + ("\"one\\n&two\"", "c"), + ("'one?&two'", "shell"), + ("'one\n&two'", "shell-show"), + ("'one?&two'", "shell-always"), + ("'one\n&two'", "shell-always-show"), + ("'one'$'\\n''&two'", "shell-escape"), + ("'one'$'\\n''&two'", "shell-escape-always"), + ], + ); + + // The first 16 control characters. NUL is also included, even though it is of + // no importance for file names. + check_names( + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", + vec![ + ("????????????????", "literal"), + ( + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", + "literal-show", + ), + ( + "\\000\\001\\002\\003\\004\\005\\006\\a\\b\\t\\n\\v\\f\\r\\016\\017", + "escape", + ), + ( + "\"\\000\\001\\002\\003\\004\\005\\006\\a\\b\\t\\n\\v\\f\\r\\016\\017\"", + "c", + ), + ("????????????????", "shell"), + ( + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", + "shell-show", + ), + ("'????????????????'", "shell-always"), + ( + "'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F'", + "shell-always-show", + ), + ( + "''$'\\000\\001\\002\\003\\004\\005\\006\\a\\b\\t\\n\\v\\f\\r\\016\\017'", + "shell-escape", + ), + ( + "''$'\\000\\001\\002\\003\\004\\005\\006\\a\\b\\t\\n\\v\\f\\r\\016\\017'", + "shell-escape-always", + ), + ], + ); + + // The last 16 control characters. + check_names( + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", + vec![ + ("????????????????", "literal"), + ( + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", + "literal-show", + ), + ( + "\\020\\021\\022\\023\\024\\025\\026\\027\\030\\031\\032\\033\\034\\035\\036\\037", + "escape", + ), + ( + "\"\\020\\021\\022\\023\\024\\025\\026\\027\\030\\031\\032\\033\\034\\035\\036\\037\"", + "c", + ), + ("????????????????", "shell"), + ( + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", + "shell-show", + ), + ("'????????????????'", "shell-always"), + ( + "'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F'", + "shell-always-show", + ), + ( + "''$'\\020\\021\\022\\023\\024\\025\\026\\027\\030\\031\\032\\033\\034\\035\\036\\037'", + "shell-escape", + ), + ( + "''$'\\020\\021\\022\\023\\024\\025\\026\\027\\030\\031\\032\\033\\034\\035\\036\\037'", + "shell-escape-always", + ), + ], + ); + + // DEL + check_names( + "\x7F", + vec![ + ("?", "literal"), + ("\x7F", "literal-show"), + ("\\177", "escape"), + ("\"\\177\"", "c"), + ("?", "shell"), + ("\x7F", "shell-show"), + ("'?'", "shell-always"), + ("'\x7F'", "shell-always-show"), + ("''$'\\177'", "shell-escape"), + ("''$'\\177'", "shell-escape-always"), + ], + ); + } + + #[test] + fn test_question_mark() { + // A question mark must force quotes in shell and shell-always, unless + // it is in place of a control character (that case is already covered + // in other tests) + check_names( + "one?two", + vec![ + ("one?two", "literal"), + ("one?two", "literal-show"), + ("one?two", "escape"), + ("\"one?two\"", "c"), + ("'one?two'", "shell"), + ("'one?two'", "shell-show"), + ("'one?two'", "shell-always"), + ("'one?two'", "shell-always-show"), + ("'one?two'", "shell-escape"), + ("'one?two'", "shell-escape-always"), + ], + ); + } + + #[test] + fn test_backslash() { + // Escaped in C-style, but not in Shell-style escaping + check_names( + "one\\two", + vec![ + ("one\\two", "literal"), + ("one\\two", "literal-show"), + ("one\\\\two", "escape"), + ("\"one\\\\two\"", "c"), + ("'one\\two'", "shell"), + ("\'one\\two\'", "shell-always"), + ("'one\\two'", "shell-escape"), + ("'one\\two'", "shell-escape-always"), + ], + ); + } +} diff --git a/src/uu/ls/src/version_cmp.rs b/src/uu/ls/src/version_cmp.rs new file mode 100644 index 000000000..e3f7e29e3 --- /dev/null +++ b/src/uu/ls/src/version_cmp.rs @@ -0,0 +1,306 @@ +use std::cmp::Ordering; +use std::path::Path; + +/// Compare paths in a way that matches the GNU version sort, meaning that +/// numbers get sorted in a natural way. +pub(crate) fn version_cmp(a: &Path, b: &Path) -> Ordering { + let a_string = a.to_string_lossy(); + let b_string = b.to_string_lossy(); + let mut a = a_string.chars().peekable(); + let mut b = b_string.chars().peekable(); + + // The order determined from the number of leading zeroes. + // This is used if the filenames are equivalent up to leading zeroes. + let mut leading_zeroes = Ordering::Equal; + + loop { + match (a.next(), b.next()) { + // If the characters are both numerical. We collect the rest of the number + // and parse them to u64's and compare them. + (Some(a_char @ '0'..='9'), Some(b_char @ '0'..='9')) => { + let mut a_leading_zeroes = 0; + if a_char == '0' { + a_leading_zeroes = 1; + while let Some('0') = a.peek() { + a_leading_zeroes += 1; + a.next(); + } + } + + let mut b_leading_zeroes = 0; + if b_char == '0' { + b_leading_zeroes = 1; + while let Some('0') = b.peek() { + b_leading_zeroes += 1; + b.next(); + } + } + // The first different number of leading zeros determines the order + // so if it's already been determined by a previous number, we leave + // it as that ordering. + // It's b.cmp(&a), because the *largest* number of leading zeros + // should go first + if leading_zeroes == Ordering::Equal { + leading_zeroes = b_leading_zeroes.cmp(&a_leading_zeroes); + } + + let mut a_str = String::new(); + let mut b_str = String::new(); + if a_char != '0' { + a_str.push(a_char); + } + if b_char != '0' { + b_str.push(b_char); + } + + // Unwrapping here is fine because we only call next if peek returns + // Some(_), so next should also return Some(_). + while let Some('0'..='9') = a.peek() { + a_str.push(a.next().unwrap()); + } + + while let Some('0'..='9') = b.peek() { + b_str.push(b.next().unwrap()); + } + + // Since the leading zeroes are stripped, the length can be + // used to compare the numbers. + match a_str.len().cmp(&b_str.len()) { + Ordering::Equal => {} + x => return x, + } + + // At this point, leading zeroes are stripped and the lengths + // are equal, meaning that the strings can be compared using + // the standard compare function. + match a_str.cmp(&b_str) { + Ordering::Equal => {} + x => return x, + } + } + // If there are two characters we just compare the characters + (Some(a_char), Some(b_char)) => match a_char.cmp(&b_char) { + Ordering::Equal => {} + x => return x, + }, + // Otherwise, we compare the options (because None < Some(_)) + (a_opt, b_opt) => match a_opt.cmp(&b_opt) { + // If they are completely equal except for leading zeroes, we use the leading zeroes. + Ordering::Equal => return leading_zeroes, + x => return x, + }, + } + } +} + +#[cfg(test)] +mod tests { + use crate::version_cmp::version_cmp; + use std::cmp::Ordering; + use std::path::PathBuf; + #[test] + fn test_version_cmp() { + // Identical strings + assert_eq!( + version_cmp(&PathBuf::from("hello"), &PathBuf::from("hello")), + Ordering::Equal + ); + + assert_eq!( + version_cmp(&PathBuf::from("file12"), &PathBuf::from("file12")), + Ordering::Equal + ); + + assert_eq!( + version_cmp( + &PathBuf::from("file12-suffix"), + &PathBuf::from("file12-suffix") + ), + Ordering::Equal + ); + + assert_eq!( + version_cmp( + &PathBuf::from("file12-suffix24"), + &PathBuf::from("file12-suffix24") + ), + Ordering::Equal + ); + + // Shortened names + assert_eq!( + version_cmp(&PathBuf::from("world"), &PathBuf::from("wo")), + Ordering::Greater, + ); + + assert_eq!( + version_cmp(&PathBuf::from("hello10wo"), &PathBuf::from("hello10world")), + Ordering::Less, + ); + + // Simple names + assert_eq!( + version_cmp(&PathBuf::from("world"), &PathBuf::from("hello")), + Ordering::Greater, + ); + + assert_eq!( + version_cmp(&PathBuf::from("hello"), &PathBuf::from("world")), + Ordering::Less + ); + + assert_eq!( + version_cmp(&PathBuf::from("apple"), &PathBuf::from("ant")), + Ordering::Greater + ); + + assert_eq!( + version_cmp(&PathBuf::from("ant"), &PathBuf::from("apple")), + Ordering::Less + ); + + // Uppercase letters + assert_eq!( + version_cmp(&PathBuf::from("Beef"), &PathBuf::from("apple")), + Ordering::Less, + "Uppercase letters are sorted before all lowercase letters" + ); + + assert_eq!( + version_cmp(&PathBuf::from("Apple"), &PathBuf::from("apple")), + Ordering::Less + ); + + assert_eq!( + version_cmp(&PathBuf::from("apple"), &PathBuf::from("aPple")), + Ordering::Greater + ); + + // Numbers + assert_eq!( + version_cmp(&PathBuf::from("100"), &PathBuf::from("20")), + Ordering::Greater, + "Greater numbers are greater even if they start with a smaller digit", + ); + + assert_eq!( + version_cmp(&PathBuf::from("20"), &PathBuf::from("20")), + Ordering::Equal, + "Equal numbers are equal" + ); + + assert_eq!( + version_cmp(&PathBuf::from("15"), &PathBuf::from("200")), + Ordering::Less, + "Small numbers are smaller" + ); + + // Comparing numbers with other characters + assert_eq!( + version_cmp(&PathBuf::from("1000"), &PathBuf::from("apple")), + Ordering::Less, + "Numbers are sorted before other characters" + ); + + assert_eq!( + // spell-checker:disable-next-line + version_cmp(&PathBuf::from("file1000"), &PathBuf::from("fileapple")), + Ordering::Less, + "Numbers in the middle of the name are sorted before other characters" + ); + + // Leading zeroes + assert_eq!( + version_cmp(&PathBuf::from("012"), &PathBuf::from("12")), + Ordering::Less, + "A single leading zero can make a difference" + ); + + assert_eq!( + version_cmp(&PathBuf::from("000800"), &PathBuf::from("0000800")), + Ordering::Greater, + "Leading number of zeroes is used even if both non-zero number of zeros" + ); + + // Numbers and other characters combined + assert_eq!( + version_cmp(&PathBuf::from("ab10"), &PathBuf::from("aa11")), + Ordering::Greater + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa10"), &PathBuf::from("aa11")), + Ordering::Less, + "Numbers after other characters are handled correctly." + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa2"), &PathBuf::from("aa100")), + Ordering::Less, + "Numbers after alphabetical characters are handled correctly." + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa10bb"), &PathBuf::from("aa11aa")), + Ordering::Less, + "Number is used even if alphabetical characters after it differ." + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa10aa0010"), &PathBuf::from("aa11aa1")), + Ordering::Less, + "Second number is ignored if the first number differs." + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa10aa0010"), &PathBuf::from("aa10aa1")), + Ordering::Greater, + "Second number is used if the rest is equal." + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa10aa0010"), &PathBuf::from("aa00010aa1")), + Ordering::Greater, + "Second number is used if the rest is equal up to leading zeroes of the first number." + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa10aa0022"), &PathBuf::from("aa010aa022")), + Ordering::Greater, + "The leading zeroes of the first number has priority." + ); + + assert_eq!( + version_cmp(&PathBuf::from("aa10aa0022"), &PathBuf::from("aa10aa022")), + Ordering::Less, + "The leading zeroes of other numbers than the first are used." + ); + + assert_eq!( + version_cmp(&PathBuf::from("file-1.4"), &PathBuf::from("file-1.13")), + Ordering::Less, + "Periods are handled as normal text, not as a decimal point." + ); + + // Greater than u64::Max + // u64 == 18446744073709551615 so this should be plenty: + // 20000000000000000000000 + assert_eq!( + version_cmp( + &PathBuf::from("aa2000000000000000000000bb"), + &PathBuf::from("aa002000000000000000000001bb") + ), + Ordering::Less, + "Numbers larger than u64::MAX are handled correctly without crashing" + ); + + assert_eq!( + version_cmp( + &PathBuf::from("aa2000000000000000000000bb"), + &PathBuf::from("aa002000000000000000000000bb") + ), + Ordering::Greater, + "Leading zeroes for numbers larger than u64::MAX are handled correctly without crashing" + ); + } +} diff --git a/src/uu/mkdir/Cargo.toml b/src/uu/mkdir/Cargo.toml index 9cedb5c03..a8d374bf9 100644 --- a/src/uu/mkdir/Cargo.toml +++ b/src/uu/mkdir/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_mkdir" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "mkdir ~ (uutils) create DIRECTORY" @@ -17,7 +17,7 @@ path = "src/mkdir.rs" [dependencies] clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs", "mode"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs", "mode"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/mkdir/src/main.rs b/src/uu/mkdir/src/main.rs index 3850113b9..fa6855c93 100644 --- a/src/uu/mkdir/src/main.rs +++ b/src/uu/mkdir/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_mkdir); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_mkdir); diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index 6b9fd68ea..a99867570 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -8,149 +8,120 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::OsValues; +use clap::{crate_version, App, Arg}; use std::fs; use std::path::Path; +use uucore::error::{FromIo, UResult, USimpleError}; static ABOUT: &str = "Create the given DIRECTORY(ies) if they do not exist"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); -static OPT_MODE: &str = "mode"; -static OPT_PARENTS: &str = "parents"; -static OPT_VERBOSE: &str = "verbose"; - -static ARG_DIRS: &str = "dirs"; +mod options { + pub const MODE: &str = "mode"; + pub const PARENTS: &str = "parents"; + pub const VERBOSE: &str = "verbose"; + pub const DIRS: &str = "dirs"; +} fn get_usage() -> String { format!("{0} [OPTION]... [USER]", executable!()) } -/** - * Handles option parsing - */ -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let usage = get_usage(); // Linux-specific options, not implemented // opts.optflag("Z", "context", "set SELinux security context" + // " of each created directory to CTX"), - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(OPT_MODE) - .short("m") - .long(OPT_MODE) - .help("set file mode") - .default_value("755"), - ) - .arg( - Arg::with_name(OPT_PARENTS) - .short("p") - .long(OPT_PARENTS) - .alias("parent") - .help("make parent directories as needed"), - ) - .arg( - Arg::with_name(OPT_VERBOSE) - .short("v") - .long(OPT_VERBOSE) - .help("print a message for each printed directory"), - ) - .arg( - Arg::with_name(ARG_DIRS) - .multiple(true) - .takes_value(true) - .min_values(1), - ) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - let dirs: Vec = matches - .values_of(ARG_DIRS) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let verbose = matches.is_present(OPT_VERBOSE); - let recursive = matches.is_present(OPT_PARENTS); + let dirs = matches.values_of_os(options::DIRS).unwrap_or_default(); + let verbose = matches.is_present(options::VERBOSE); + let recursive = matches.is_present(options::PARENTS); // Translate a ~str in octal form to u16, default to 755 // Not tested on Windows - let mode_match = matches.value_of(OPT_MODE); - let mode: u16 = match mode_match { - Some(m) => { - let res: Option = u16::from_str_radix(&m, 8).ok(); - match res { - Some(r) => r, - _ => crash!(1, "no mode given"), - } - } - _ => 0o755_u16, + let mode: u16 = match matches.value_of(options::MODE) { + Some(m) => u16::from_str_radix(m, 8) + .map_err(|_| USimpleError::new(1, format!("invalid mode '{}'", m)))?, + None => 0o755_u16, }; exec(dirs, recursive, mode, verbose) } -/** - * Create the list of new directories - */ -fn exec(dirs: Vec, recursive: bool, mode: u16, verbose: bool) -> i32 { - let mut status = 0; - let empty = Path::new(""); - for dir in &dirs { - let path = Path::new(dir); - if !recursive { - if let Some(parent) = path.parent() { - if parent != empty && !parent.exists() { - show_info!( - "cannot create directory '{}': No such file or directory", - path.display() - ); - status = 1; - continue; - } - } - } - status |= mkdir(path, recursive, mode, verbose); - } - status +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::MODE) + .short("m") + .long(options::MODE) + .help("set file mode (not implemented on windows)") + .default_value("755"), + ) + .arg( + Arg::with_name(options::PARENTS) + .short("p") + .long(options::PARENTS) + .alias("parent") + .help("make parent directories as needed"), + ) + .arg( + Arg::with_name(options::VERBOSE) + .short("v") + .long(options::VERBOSE) + .help("print a message for each printed directory"), + ) + .arg( + Arg::with_name(options::DIRS) + .multiple(true) + .takes_value(true) + .min_values(1), + ) } /** - * Wrapper to catch errors, return 1 if failed + * Create the list of new directories */ -fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 { +fn exec(dirs: OsValues, recursive: bool, mode: u16, verbose: bool) -> UResult<()> { + for dir in dirs { + let path = Path::new(dir); + show_if_err!(mkdir(path, recursive, mode, verbose)); + } + Ok(()) +} + +fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> UResult<()> { let create_dir = if recursive { fs::create_dir_all } else { fs::create_dir }; - if let Err(e) = create_dir(path) { - show_info!("{}: {}", path.display(), e.to_string()); - return 1; - } + + create_dir(path).map_err_context(|| format!("cannot create directory '{}'", path.display()))?; if verbose { println!("{}: created directory '{}'", executable!(), path.display()); } - #[cfg(any(unix, target_os = "redox"))] - fn chmod(path: &Path, mode: u16) -> i32 { - use std::fs::{set_permissions, Permissions}; - use std::os::unix::fs::PermissionsExt; - - let mode = Permissions::from_mode(u32::from(mode)); - - if let Err(err) = set_permissions(path, mode) { - show_error!("{}: {}", path.display(), err); - return 1; - } - 0 - } - #[cfg(windows)] - #[allow(unused_variables)] - fn chmod(path: &Path, mode: u16) -> i32 { - // chmod on Windows only sets the readonly flag, which isn't even honored on directories - 0 - } chmod(path, mode) } + +#[cfg(any(unix, target_os = "redox"))] +fn chmod(path: &Path, mode: u16) -> UResult<()> { + use std::fs::{set_permissions, Permissions}; + use std::os::unix::fs::PermissionsExt; + + let mode = Permissions::from_mode(u32::from(mode)); + + set_permissions(path, mode) + .map_err_context(|| format!("cannot set permissions '{}'", path.display())) +} + +#[cfg(windows)] +fn chmod(_path: &Path, _mode: u16) -> UResult<()> { + // chmod on Windows only sets the readonly flag, which isn't even honored on directories + Ok(()) +} diff --git a/src/uu/mkfifo/Cargo.toml b/src/uu/mkfifo/Cargo.toml index 627d4a721..d66003b10 100644 --- a/src/uu/mkfifo/Cargo.toml +++ b/src/uu/mkfifo/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_mkfifo" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "mkfifo ~ (uutils) create FIFOs (named pipes)" @@ -15,9 +15,9 @@ edition = "2018" path = "src/mkfifo.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/mkfifo/src/main.rs b/src/uu/mkfifo/src/main.rs index 489dc8ffd..3ad5a3bed 100644 --- a/src/uu/mkfifo/src/main.rs +++ b/src/uu/mkfifo/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_mkfifo); // spell-checker:ignore procs uucore mkfifo +uucore_procs::main!(uu_mkfifo); diff --git a/src/uu/mkfifo/src/mkfifo.rs b/src/uu/mkfifo/src/mkfifo.rs index 41582b14a..ea0906567 100644 --- a/src/uu/mkfifo/src/mkfifo.rs +++ b/src/uu/mkfifo/src/mkfifo.rs @@ -8,57 +8,38 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use libc::mkfifo; use std::ffi::CString; -use std::io::Error; +use uucore::InvalidEncodingHandling; static NAME: &str = "mkfifo"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static USAGE: &str = "mkfifo [OPTION]... NAME..."; +static SUMMARY: &str = "Create a FIFO with the given name."; + +mod options { + pub static MODE: &str = "mode"; + pub static SE_LINUX_SECURITY_CONTEXT: &str = "Z"; + pub static CONTEXT: &str = "context"; + pub static FIFO: &str = "fifo"; +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let mut opts = getopts::Options::new(); + let matches = uu_app().get_matches_from(args); - opts.optopt( - "m", - "mode", - "file permissions for the fifo", - "(default 0666)", - ); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(err) => panic!("{}", err), - }; - - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; + if matches.is_present(options::CONTEXT) { + crash!(1, "--context is not implemented"); + } + if matches.is_present(options::SE_LINUX_SECURITY_CONTEXT) { + crash!(1, "-Z is not implemented"); } - if matches.opt_present("help") || matches.free.is_empty() { - let msg = format!( - "{0} {1} - -Usage: - {0} [OPTIONS] NAME... - -Create a FIFO with the given name.", - NAME, VERSION - ); - - print!("{}", opts.usage(&msg)); - if matches.free.is_empty() { - return 1; - } - return 0; - } - - let mode = match matches.opt_str("m") { - Some(m) => match usize::from_str_radix(&m, 8) { + let mode = match matches.value_of(options::MODE) { + Some(m) => match usize::from_str_radix(m, 8) { Ok(m) => m, Err(e) => { show_error!("invalid mode: {}", e); @@ -68,21 +49,53 @@ Create a FIFO with the given name.", None => 0o666, }; - let mut exit_status = 0; - for f in &matches.free { + let fifos: Vec = match matches.values_of(options::FIFO) { + Some(v) => v.clone().map(|s| s.to_owned()).collect(), + None => crash!(1, "missing operand"), + }; + + let mut exit_code = 0; + for f in fifos { let err = unsafe { let name = CString::new(f.as_bytes()).unwrap(); mkfifo(name.as_ptr(), mode as libc::mode_t) }; if err == -1 { - show_error!( - "creating '{}': {}", - f, - Error::last_os_error().raw_os_error().unwrap() - ); - exit_status = 1; + show_error!("cannot create fifo '{}': File exists", f); + exit_code = 1; } } - exit_status + exit_code +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(USAGE) + .about(SUMMARY) + .arg( + Arg::with_name(options::MODE) + .short("m") + .long(options::MODE) + .help("file permissions for the fifo") + .default_value("0666") + .value_name("0666"), + ) + .arg( + Arg::with_name(options::SE_LINUX_SECURITY_CONTEXT) + .short(options::SE_LINUX_SECURITY_CONTEXT) + .help("set the SELinux security context to default type"), + ) + .arg( + Arg::with_name(options::CONTEXT) + .long(options::CONTEXT) + .value_name("CTX") + .help( + "like -Z, or if CTX is specified then set the SELinux \ + or SMACK security context to CTX", + ), + ) + .arg(Arg::with_name(options::FIFO).hidden(true).multiple(true)) } diff --git a/src/uu/mknod/Cargo.toml b/src/uu/mknod/Cargo.toml index 426534e75..1320e3546 100644 --- a/src/uu/mknod/Cargo.toml +++ b/src/uu/mknod/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_mknod" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "mknod ~ (uutils) create special file NAME of TYPE" @@ -16,9 +16,9 @@ name = "uu_mknod" path = "src/mknod.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "^0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["mode"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["mode"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/mknod/src/main.rs b/src/uu/mknod/src/main.rs index f3878199b..b65a20cd4 100644 --- a/src/uu/mknod/src/main.rs +++ b/src/uu/mknod/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_mknod); // spell-checker:ignore procs uucore mknod +uucore_procs::main!(uu_mknod); diff --git a/src/uu/mknod/src/mknod.rs b/src/uu/mknod/src/mknod.rs index 1343501bb..8cc7db908 100644 --- a/src/uu/mknod/src/mknod.rs +++ b/src/uu/mknod/src/mknod.rs @@ -5,22 +5,40 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) parsemode makedev sysmacros makenod newmode perror IFBLK IFCHR IFIFO - -mod parsemode; +// spell-checker:ignore (ToDO) parsemode makedev sysmacros perror IFBLK IFCHR IFIFO #[macro_use] extern crate uucore; +use std::ffi::CString; + +use clap::{crate_version, App, Arg, ArgMatches}; use libc::{dev_t, mode_t}; use libc::{S_IFBLK, S_IFCHR, S_IFIFO, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR}; -use getopts::Options; - -use std::ffi::CString; +use uucore::InvalidEncodingHandling; static NAME: &str = "mknod"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Create the special file NAME of the given TYPE."; +static USAGE: &str = "mknod [OPTION]... NAME TYPE [MAJOR MINOR]"; +static LONG_HELP: &str = "Mandatory arguments to long options are mandatory for short options too. +-m, --mode=MODE set file permission bits to MODE, not a=rw - umask +--help display this help and exit +--version output version information and exit + +Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they +must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X, +it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal; +otherwise, as decimal. TYPE may be: + +b create a block (buffered) special file +c, u create a character (unbuffered) special file +p create a FIFO + +NOTE: your shell may have its own version of mknod, which usually supersedes +the version described here. Please refer to your shell's documentation +for details about the options it supports. +"; const MODE_RW_UGO: mode_t = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; @@ -31,168 +49,175 @@ fn makedev(maj: u64, min: u64) -> dev_t { } #[cfg(windows)] -fn _makenod(path: CString, mode: mode_t, dev: dev_t) -> i32 { +fn _mknod(file_name: &str, mode: mode_t, dev: dev_t) -> i32 { panic!("Unsupported for windows platform") } #[cfg(unix)] -fn _makenod(path: CString, mode: mode_t, dev: dev_t) -> i32 { - unsafe { libc::mknod(path.as_ptr(), mode, dev) } +fn _mknod(file_name: &str, mode: mode_t, dev: dev_t) -> i32 { + let c_str = CString::new(file_name).expect("Failed to convert to CString"); + + // the user supplied a mode + let set_umask = mode & MODE_RW_UGO != MODE_RW_UGO; + + unsafe { + // store prev umask + let last_umask = if set_umask { libc::umask(0) } else { 0 }; + + let errno = libc::mknod(c_str.as_ptr(), mode, dev); + + // set umask back to original value + if set_umask { + libc::umask(last_umask); + } + + if errno == -1 { + let c_str = CString::new(NAME).expect("Failed to convert to CString"); + // shows the error from the mknod syscall + libc::perror(c_str.as_ptr()); + } + errno + } } #[allow(clippy::cognitive_complexity)] pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - - let mut opts = Options::new(); - + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); // Linux-specific options, not implemented // opts.optflag("Z", "", "set the SELinux security context to default type"); // opts.optopt("", "context", "like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX"); - opts.optopt( - "m", - "mode", - "set file permission bits to MODE, not a=rw - umask", - "MODE", - ); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("", "version", "output version information and exit"); + let matches = uu_app().get_matches_from(args); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "{}\nTry '{} --help' for more information.", f, NAME), + let mode = match get_mode(&matches) { + Ok(mode) => mode, + Err(err) => { + show_error!("{}", err); + return 1; + } }; - if matches.opt_present("help") { - println!( - "Usage: {0} [OPTION]... NAME TYPE [MAJOR MINOR] + let file_name = matches.value_of("name").expect("Missing argument 'NAME'"); -Mandatory arguments to long options are mandatory for short options too. - -m, --mode=MODE set file permission bits to MODE, not a=rw - umask - --help display this help and exit - --version output version information and exit + // Only check the first character, to allow mnemonic usage like + // 'mknod /dev/rst0 character 18 0'. + let ch = matches + .value_of("type") + .expect("Missing argument 'TYPE'") + .chars() + .next() + .expect("Failed to get the first char"); -Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they -must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X, -it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal; -otherwise, as decimal. TYPE may be: - - b create a block (buffered) special file - c, u create a character (unbuffered) special file - p create a FIFO - -NOTE: your shell may have its own version of mknod, which usually supersedes -the version described here. Please refer to your shell's documentation -for details about the options it supports.", - NAME - ); - return 0; - } - - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; - } - - let mut last_umask: mode_t = 0; - let mut newmode: mode_t = MODE_RW_UGO; - if matches.opt_present("mode") { - match parsemode::parse_mode(matches.opt_str("mode")) { - Ok(parsed) => { - if parsed > 0o777 { - show_info!("mode must specify only file permission bits"); - return 1; - } - newmode = parsed; - } - Err(e) => { - show_info!("{}", e); - return 1; - } + if ch == 'p' { + if matches.is_present("major") || matches.is_present("minor") { + eprintln!("Fifos do not have major and minor device numbers."); + eprintln!("Try '{} --help' for more information.", NAME); + 1 + } else { + _mknod(file_name, S_IFIFO | mode, 0) } - unsafe { - last_umask = libc::umask(0); - } - } + } else { + match (matches.value_of("major"), matches.value_of("minor")) { + (None, None) | (_, None) | (None, _) => { + eprintln!("Special files require major and minor device numbers."); + eprintln!("Try '{} --help' for more information.", NAME); + 1 + } + (Some(major), Some(minor)) => { + let major = major.parse::().expect("validated by clap"); + let minor = minor.parse::().expect("validated by clap"); - let mut ret = 0i32; - match matches.free.len() { - 0 => show_usage_error!("missing operand"), - 1 => show_usage_error!("missing operand after ‘{}’", matches.free[0]), - _ => { - let args = &matches.free; - let c_str = CString::new(args[0].as_str()).expect("Failed to convert to CString"); - - // Only check the first character, to allow mnemonic usage like - // 'mknod /dev/rst0 character 18 0'. - let ch = args[1] - .chars() - .next() - .expect("Failed to get the first char"); - - if ch == 'p' { - if args.len() > 2 { - show_info!("{}: extra operand ‘{}’", NAME, args[2]); - if args.len() == 4 { - eprintln!("Fifos do not have major and minor device numbers."); - } - eprintln!("Try '{} --help' for more information.", NAME); - return 1; - } - - ret = _makenod(c_str, S_IFIFO | newmode, 0); - } else { - if args.len() < 4 { - show_info!("missing operand after ‘{}’", args[args.len() - 1]); - if args.len() == 2 { - eprintln!("Special files require major and minor device numbers."); - } - eprintln!("Try '{} --help' for more information.", NAME); - return 1; - } else if args.len() > 4 { - show_usage_error!("extra operand ‘{}’", args[4]); - return 1; - } else if !"bcu".contains(ch) { - show_usage_error!("invalid device type ‘{}’", args[1]); - return 1; - } - - let maj = args[2].parse::(); - let min = args[3].parse::(); - if maj.is_err() { - show_info!("invalid major device number ‘{}’", args[2]); - return 1; - } else if min.is_err() { - show_info!("invalid minor device number ‘{}’", args[3]); - return 1; - } - - let (maj, min) = (maj.unwrap(), min.unwrap()); - let dev = makedev(maj, min); + let dev = makedev(major, minor); if ch == 'b' { // block special file - ret = _makenod(c_str, S_IFBLK | newmode, dev); - } else { + _mknod(file_name, S_IFBLK | mode, dev) + } else if ch == 'c' || ch == 'u' { // char special file - ret = _makenod(c_str, S_IFCHR | newmode, dev); + _mknod(file_name, S_IFCHR | mode, dev) + } else { + unreachable!("{} was validated to be only b, c or u", ch); } } } } - - if last_umask != 0 { - unsafe { - libc::umask(last_umask); - } - } - if ret == -1 { - let c_str = CString::new(format!("{}: {}", NAME, matches.free[0]).as_str()) - .expect("Failed to convert to CString"); - unsafe { - libc::perror(c_str.as_ptr()); - } - } - - ret +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .usage(USAGE) + .after_help(LONG_HELP) + .about(ABOUT) + .arg( + Arg::with_name("mode") + .short("m") + .long("mode") + .value_name("MODE") + .help("set file permission bits to MODE, not a=rw - umask"), + ) + .arg( + Arg::with_name("name") + .value_name("NAME") + .help("name of the new file") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("type") + .value_name("TYPE") + .help("type of the new file (b, c, u or p)") + .required(true) + .validator(valid_type) + .index(2), + ) + .arg( + Arg::with_name("major") + .value_name("MAJOR") + .help("major file type") + .validator(valid_u64) + .index(3), + ) + .arg( + Arg::with_name("minor") + .value_name("MINOR") + .help("minor file type") + .validator(valid_u64) + .index(4), + ) +} + +fn get_mode(matches: &ArgMatches) -> Result { + match matches.value_of("mode") { + None => Ok(MODE_RW_UGO), + Some(str_mode) => uucore::mode::parse_mode(str_mode) + .map_err(|e| format!("invalid mode ({})", e)) + .and_then(|mode| { + if mode > 0o777 { + Err("mode must specify only file permission bits".to_string()) + } else { + Ok(mode) + } + }), + } +} + +fn valid_type(tpe: String) -> Result<(), String> { + // Only check the first character, to allow mnemonic usage like + // 'mknod /dev/rst0 character 18 0'. + tpe.chars() + .next() + .ok_or_else(|| "missing device type".to_string()) + .and_then(|first_char| { + if vec!['b', 'c', 'u', 'p'].contains(&first_char) { + Ok(()) + } else { + Err(format!("invalid device type '{}'", tpe)) + } + }) +} + +fn valid_u64(num: String) -> Result<(), String> { + num.parse::().map(|_| ()).map_err(|_| num) } diff --git a/src/uu/mknod/src/parsemode.rs b/src/uu/mknod/src/parsemode.rs index 8f8f9af61..2767cb303 100644 --- a/src/uu/mknod/src/parsemode.rs +++ b/src/uu/mknod/src/parsemode.rs @@ -1,22 +1,19 @@ -// spell-checker:ignore (ToDO) fperm +// spell-checker:ignore (path) osrelease use libc::{mode_t, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR}; use uucore::mode; -pub fn parse_mode(mode: Option) -> Result { - let fperm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; - if let Some(mode) = mode { - let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; - let result = if mode.contains(arr) { - mode::parse_numeric(fperm as u32, mode.as_str()) - } else { - mode::parse_symbolic(fperm as u32, mode.as_str(), true) - }; - result.map(|mode| mode as mode_t) +pub const MODE_RW_UGO: mode_t = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + +pub fn parse_mode(mode: &str) -> Result { + let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + let result = if mode.contains(arr) { + mode::parse_numeric(MODE_RW_UGO as u32, mode) } else { - Ok(fperm) - } + mode::parse_symbolic(MODE_RW_UGO as u32, mode, true) + }; + result.map(|mode| mode as mode_t) } #[cfg(test)] @@ -39,20 +36,19 @@ mod test { #[test] fn symbolic_modes() { - assert_eq!(super::parse_mode(Some("u+x".to_owned())).unwrap(), 0o766); + assert_eq!(super::parse_mode("u+x").unwrap(), 0o766); assert_eq!( - super::parse_mode(Some("+x".to_owned())).unwrap(), + super::parse_mode("+x").unwrap(), if !is_wsl() { 0o777 } else { 0o776 } ); - assert_eq!(super::parse_mode(Some("a-w".to_owned())).unwrap(), 0o444); - assert_eq!(super::parse_mode(Some("g-r".to_owned())).unwrap(), 0o626); + assert_eq!(super::parse_mode("a-w").unwrap(), 0o444); + assert_eq!(super::parse_mode("g-r").unwrap(), 0o626); } #[test] fn numeric_modes() { - assert_eq!(super::parse_mode(Some("644".to_owned())).unwrap(), 0o644); - assert_eq!(super::parse_mode(Some("+100".to_owned())).unwrap(), 0o766); - assert_eq!(super::parse_mode(Some("-4".to_owned())).unwrap(), 0o662); - assert_eq!(super::parse_mode(None).unwrap(), 0o666); + assert_eq!(super::parse_mode("644").unwrap(), 0o644); + assert_eq!(super::parse_mode("+100").unwrap(), 0o766); + assert_eq!(super::parse_mode("-4").unwrap(), 0o662); } } diff --git a/src/uu/mktemp/Cargo.toml b/src/uu/mktemp/Cargo.toml index c48306a40..c669f0acc 100644 --- a/src/uu/mktemp/Cargo.toml +++ b/src/uu/mktemp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_mktemp" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "mktemp ~ (uutils) create and display a temporary file or directory from TEMPLATE" @@ -18,7 +18,7 @@ path = "src/mktemp.rs" clap = "2.33" rand = "0.5" tempfile = "3.1" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/mktemp/src/main.rs b/src/uu/mktemp/src/main.rs index 217f09372..020284655 100644 --- a/src/uu/mktemp/src/main.rs +++ b/src/uu/mktemp/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_mktemp); // spell-checker:ignore procs uucore mktemp +uucore_procs::main!(uu_mktemp); diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 194b6625f..8a4b472aa 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -6,25 +6,24 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) tempfile tempdir SUFF TMPDIR tmpname +// spell-checker:ignore (paths) GPGHome #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; +use uucore::error::{FromIo, UCustomError, UResult}; use std::env; +use std::error::Error; +use std::fmt::Display; use std::iter; -use std::mem::forget; use std::path::{is_separator, PathBuf}; use rand::Rng; use tempfile::Builder; -mod tempdir; - static ABOUT: &str = "create a temporary file or directory."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static DEFAULT_TEMPLATE: &str = "tmp.XXXXXXXXXX"; @@ -41,64 +40,43 @@ fn get_usage() -> String { format!("{0} [OPTION]... [TEMPLATE]", executable!()) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[derive(Debug)] +enum MkTempError { + PersistError(PathBuf), + MustEndInX(String), + TooFewXs(String), + ContainsDirSeparator(String), + InvalidTemplate(String), +} + +impl UCustomError for MkTempError {} + +impl Error for MkTempError {} + +impl Display for MkTempError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use MkTempError::*; + match self { + PersistError(p) => write!(f, "could not persist file '{}'", p.display()), + MustEndInX(s) => write!(f, "with --suffix, template '{}' must end in X", s), + TooFewXs(s) => write!(f, "too few X's in template '{}'", s), + ContainsDirSeparator(s) => { + write!(f, "invalid suffix '{}', contains directory separator", s) + } + InvalidTemplate(s) => write!( + f, + "invalid template, '{}'; with --tmpdir, it may not be absolute", + s + ), + } + } +} + +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(OPT_DIRECTORY) - .short("d") - .long(OPT_DIRECTORY) - .help("Make a directory instead of a file"), - ) - .arg( - Arg::with_name(OPT_DRY_RUN) - .short("u") - .long(OPT_DRY_RUN) - .help("do not create anything; merely print a name (unsafe)"), - ) - .arg( - Arg::with_name(OPT_QUIET) - .short("q") - .long("quiet") - .help("Fail silently if an error occurs."), - ) - .arg( - Arg::with_name(OPT_SUFFIX) - .long(OPT_SUFFIX) - .help( - "append SUFF to TEMPLATE; SUFF must not contain a path separator. \ - This option is implied if TEMPLATE does not end with X.", - ) - .value_name("SUFF"), - ) - .arg( - Arg::with_name(OPT_TMPDIR) - .short("p") - .long(OPT_TMPDIR) - .help( - "interpret TEMPLATE relative to DIR; if DIR is not specified, use \ - $TMPDIR if set, else /tmp. With this option, TEMPLATE must not \ - be an absolute name; unlike with -t, TEMPLATE may contain \ - slashes, but mktemp creates only the final component", - ) - .value_name("DIR"), - ) - .arg(Arg::with_name(OPT_T).short(OPT_T).help( - "Generate a template (using the supplied prefix and TMPDIR if set) \ - to create a filename template [deprecated]", - )) - .arg( - Arg::with_name(ARG_TEMPLATE) - .multiple(false) - .takes_value(true) - .max_values(1) - .default_value(DEFAULT_TEMPLATE), - ) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); let template = matches.value_of(ARG_TEMPLATE).unwrap(); let tmpdir = matches.value_of(OPT_TMPDIR).unwrap_or_default(); @@ -131,63 +109,120 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let dry_run = matches.is_present(OPT_DRY_RUN); let suppress_file_err = matches.is_present(OPT_QUIET); - let (prefix, rand, suffix) = match parse_template(template) { - Some((p, r, s)) => match matches.value_of(OPT_SUFFIX) { - Some(suf) => { - if s.is_empty() { - (p, r, suf) - } else { - crash!( - 1, - "Template should end with 'X' when you specify suffix option." - ) - } - } - None => (p, r, s), - }, - None => ("", 0, ""), - }; - - if rand < 3 { - crash!(1, "Too few 'X's in template") - } - - if suffix.chars().any(is_separator) { - crash!(1, "suffix cannot contain any path separators"); - } + let (prefix, rand, suffix) = parse_template(template, matches.value_of(OPT_SUFFIX))?; if matches.is_present(OPT_TMPDIR) && PathBuf::from(prefix).is_absolute() { - show_info!( - "invalid template, ‘{}’; with --tmpdir, it may not be absolute", - template - ); - return 1; - }; + return Err(MkTempError::InvalidTemplate(template.into()).into()); + } if matches.is_present(OPT_T) { tmpdir = env::temp_dir() + } + + let res = if dry_run { + dry_exec(tmpdir, prefix, rand, suffix) + } else { + exec(tmpdir, prefix, rand, suffix, make_dir) }; - if dry_run { - dry_exec(tmpdir, prefix, rand, &suffix) + if suppress_file_err { + // Mapping all UErrors to ExitCodes prevents the errors from being printed + res.map_err(|e| e.code().into()) } else { - exec(tmpdir, prefix, rand, &suffix, make_dir, suppress_file_err) + res } } -fn parse_template(temp: &str) -> Option<(&str, usize, &str)> { +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(OPT_DIRECTORY) + .short("d") + .long(OPT_DIRECTORY) + .help("Make a directory instead of a file"), + ) + .arg( + Arg::with_name(OPT_DRY_RUN) + .short("u") + .long(OPT_DRY_RUN) + .help("do not create anything; merely print a name (unsafe)"), + ) + .arg( + Arg::with_name(OPT_QUIET) + .short("q") + .long("quiet") + .help("Fail silently if an error occurs."), + ) + .arg( + Arg::with_name(OPT_SUFFIX) + .long(OPT_SUFFIX) + .help( + "append SUFFIX to TEMPLATE; SUFFIX must not contain a path separator. \ + This option is implied if TEMPLATE does not end with X.", + ) + .value_name("SUFFIX"), + ) + .arg( + Arg::with_name(OPT_TMPDIR) + .short("p") + .long(OPT_TMPDIR) + .help( + "interpret TEMPLATE relative to DIR; if DIR is not specified, use \ + $TMPDIR ($TMP on windows) if set, else /tmp. With this option, TEMPLATE must not \ + be an absolute name; unlike with -t, TEMPLATE may contain \ + slashes, but mktemp creates only the final component", + ) + .value_name("DIR"), + ) + .arg(Arg::with_name(OPT_T).short(OPT_T).help( + "Generate a template (using the supplied prefix and TMPDIR (TMP on windows) if set) \ + to create a filename template [deprecated]", + )) + .arg( + Arg::with_name(ARG_TEMPLATE) + .multiple(false) + .takes_value(true) + .max_values(1) + .default_value(DEFAULT_TEMPLATE), + ) +} + +fn parse_template<'a>( + temp: &'a str, + suffix: Option<&'a str>, +) -> UResult<(&'a str, usize, &'a str)> { let right = match temp.rfind('X') { Some(r) => r + 1, - None => return None, + None => return Err(MkTempError::TooFewXs(temp.into()).into()), }; let left = temp[..right].rfind(|c| c != 'X').map_or(0, |i| i + 1); let prefix = &temp[..left]; let rand = right - left; - let suffix = &temp[right..]; - Some((prefix, rand, suffix)) + + if rand < 3 { + return Err(MkTempError::TooFewXs(temp.into()).into()); + } + + let mut suf = &temp[right..]; + + if let Some(s) = suffix { + if suf.is_empty() { + suf = s; + } else { + return Err(MkTempError::MustEndInX(temp.into()).into()); + } + }; + + if suf.chars().any(is_separator) { + return Err(MkTempError::ContainsDirSeparator(suf.into()).into()); + } + + Ok((prefix, rand, suf)) } -pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) -> i32 { +pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) -> UResult<()> { let len = prefix.len() + suffix.len() + rand; let mut buf = String::with_capacity(len); buf.push_str(prefix); @@ -210,53 +245,35 @@ pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) -> } tmpdir.push(buf); println!("{}", tmpdir.display()); - 0 + Ok(()) } -fn exec( - tmpdir: PathBuf, - prefix: &str, - rand: usize, - suffix: &str, - make_dir: bool, - quiet: bool, -) -> i32 { - if make_dir { - match tempdir::new_in(&tmpdir, prefix, rand, suffix) { - Ok(ref f) => { - println!("{}", f); - return 0; - } - Err(e) => { - if !quiet { - show_info!("{}: {}", e, tmpdir.display()); - } - return 1; - } - } - } - let tmpfile = Builder::new() - .prefix(prefix) - .rand_bytes(rand) - .suffix(suffix) - .tempfile_in(tmpdir); - let tmpfile = match tmpfile { - Ok(f) => f, - Err(e) => { - if !quiet { - show_info!("failed to create tempfile: {}", e); - } - return 1; - } +fn exec(dir: PathBuf, prefix: &str, rand: usize, suffix: &str, make_dir: bool) -> UResult<()> { + let context = || { + format!( + "failed to create file via template '{}{}{}'", + prefix, + "X".repeat(rand), + suffix + ) }; - let tmpname = tmpfile.path().to_string_lossy().to_string(); + let mut builder = Builder::new(); + builder.prefix(prefix).rand_bytes(rand).suffix(suffix); - println!("{}", tmpname); - - // CAUTION: Not to call `drop` of tmpfile, which removes the tempfile, - // I call a dangerous function `forget`. - forget(tmpfile); - - 0 + let path = if make_dir { + builder + .tempdir_in(&dir) + .map_err_context(context)? + .into_path() // `into_path` consumes the TempDir without removing it + } else { + builder + .tempfile_in(&dir) + .map_err_context(context)? + .keep() // `keep` ensures that the file is not deleted + .map_err(|e| MkTempError::PersistError(e.file.path().to_path_buf()))? + .1 + }; + println!("{}", path.display()); + Ok(()) } diff --git a/src/uu/mktemp/src/tempdir.rs b/src/uu/mktemp/src/tempdir.rs deleted file mode 100644 index 1b6c9d7b3..000000000 --- a/src/uu/mktemp/src/tempdir.rs +++ /dev/null @@ -1,51 +0,0 @@ -// spell-checker:ignore (ToDO) tempdir tmpdir - -// Mainly taken from crate `tempdir` - -use rand::distributions::Alphanumeric; -use rand::{thread_rng, Rng}; - -use std::io::Result as IOResult; -use std::io::{Error, ErrorKind}; -use std::path::Path; - -// How many times should we (re)try finding an unused random name? It should be -// enough that an attacker will run out of luck before we run out of patience. -const NUM_RETRIES: u32 = 1 << 31; - -#[cfg(any(unix, target_os = "redox"))] -fn create_dir>(path: P) -> IOResult<()> { - use std::fs::DirBuilder; - use std::os::unix::fs::DirBuilderExt; - - DirBuilder::new().mode(0o700).create(path) -} - -#[cfg(windows)] -fn create_dir>(path: P) -> IOResult<()> { - ::std::fs::create_dir(path) -} - -pub fn new_in>( - tmpdir: P, - prefix: &str, - rand: usize, - suffix: &str, -) -> IOResult { - let mut rng = thread_rng(); - for _ in 0..NUM_RETRIES { - let rand_chars: String = rng.sample_iter(&Alphanumeric).take(rand).collect(); - let leaf = format!("{}{}{}", prefix, rand_chars, suffix); - let path = tmpdir.as_ref().join(&leaf); - match create_dir(&path) { - Ok(_) => return Ok(path.to_string_lossy().into_owned()), - Err(ref e) if e.kind() == ErrorKind::AlreadyExists => {} - Err(e) => return Err(e), - } - } - - Err(Error::new( - ErrorKind::AlreadyExists, - "too many temporary directories already exist", - )) -} diff --git a/src/uu/more/Cargo.toml b/src/uu/more/Cargo.toml index 3c1746bd4..af6781876 100644 --- a/src/uu/more/Cargo.toml +++ b/src/uu/more/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_more" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "more ~ (uutils) input perusal filter" @@ -15,16 +15,20 @@ edition = "2018" path = "src/more.rs" [dependencies] -getopts = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } -uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +clap = "2.33" +uucore = { version = ">=0.0.7", package = "uucore", path = "../../uucore" } +uucore_procs = { version = ">=0.0.5", package = "uucore_procs", path = "../../uucore_procs" } +crossterm = ">=0.19" +atty = "0.2" +unicode-width = "0.1.7" +unicode-segmentation = "1.7.1" [target.'cfg(target_os = "redox")'.dependencies] redox_termios = "0.1" redox_syscall = "0.1" [target.'cfg(all(unix, not(target_os = "fuchsia")))'.dependencies] -nix = "0.8.1" +nix = "<=0.13" [[bin]] name = "more" diff --git a/src/uu/more/src/main.rs b/src/uu/more/src/main.rs index 1f3137265..15fbf51f9 100644 --- a/src/uu/more/src/main.rs +++ b/src/uu/more/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_more); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_more); diff --git a/src/uu/more/src/more.rs b/src/uu/more/src/more.rs index de4e10187..8f25cd7e4 100644 --- a/src/uu/more/src/more.rs +++ b/src/uu/more/src/more.rs @@ -5,168 +5,458 @@ // * For the full copyright and license information, please view the LICENSE file // * that was distributed with this source code. -// spell-checker:ignore (ToDO) lflag ICANON tcgetattr tcsetattr TCSADRAIN +// spell-checker:ignore (methods) isnt #[macro_use] extern crate uucore; -use getopts::Options; -use std::fs::File; -use std::io::{stdout, Read, Write}; +use std::{ + fs::File, + io::{stdin, stdout, BufReader, Read, Stdout, Write}, + path::Path, + time::Duration, +}; #[cfg(all(unix, not(target_os = "fuchsia")))] extern crate nix; -#[cfg(all(unix, not(target_os = "fuchsia")))] -use nix::sys::termios; -#[cfg(target_os = "redox")] -extern crate redox_termios; -#[cfg(target_os = "redox")] -extern crate syscall; +use clap::{crate_version, App, Arg}; +use crossterm::{ + event::{self, Event, KeyCode, KeyEvent, KeyModifiers}, + execute, queue, + style::Attribute, + terminal, +}; -#[derive(Clone, Eq, PartialEq)] -pub enum Mode { - More, - Help, - Version, +use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; + +const BELL: &str = "\x07"; + +pub mod options { + pub const SILENT: &str = "silent"; + pub const LOGICAL: &str = "logical"; + pub const NO_PAUSE: &str = "no-pause"; + pub const PRINT_OVER: &str = "print-over"; + pub const CLEAN_PRINT: &str = "clean-print"; + pub const SQUEEZE: &str = "squeeze"; + pub const PLAIN: &str = "plain"; + pub const LINES: &str = "lines"; + pub const NUMBER: &str = "number"; + pub const PATTERN: &str = "pattern"; + pub const FROM_LINE: &str = "from-line"; + pub const FILES: &str = "files"; } -static NAME: &str = "more"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +const MULTI_FILE_TOP_PROMPT: &str = "::::::::::::::\n{}\n::::::::::::::\n"; pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let matches = uu_app().get_matches_from(args); - let mut opts = Options::new(); + let mut buff = String::new(); + let silent = matches.is_present(options::SILENT); + if let Some(files) = matches.values_of(options::FILES) { + let mut stdout = setup_term(); + let length = files.len(); - // FixME: fail without panic for now; but `more` should work with no arguments (ie, for piped input) - if args.len() < 2 { - println!("{}: incorrect usage", args[0]); - return 1; - } - - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("v", "version", "output version information and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(e) => { - show_error!("{}", e); - panic!() + let mut files_iter = files.peekable(); + while let (Some(file), next_file) = (files_iter.next(), files_iter.peek()) { + let file = Path::new(file); + if file.is_dir() { + terminal::disable_raw_mode().unwrap(); + show_usage_error!("'{}' is a directory.", file.display()); + return 1; + } + if !file.exists() { + terminal::disable_raw_mode().unwrap(); + show_error!("cannot open {}: No such file or directory", file.display()); + return 1; + } + if length > 1 { + buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", file.to_str().unwrap())); + } + let mut reader = BufReader::new(File::open(file).unwrap()); + reader.read_to_string(&mut buff).unwrap(); + more(&buff, &mut stdout, next_file.copied(), silent); + buff.clear(); } - }; - let usage = opts.usage("more TARGET."); - let mode = if matches.opt_present("version") { - Mode::Version - } else if matches.opt_present("help") { - Mode::Help + reset_term(&mut stdout); + } else if atty::isnt(atty::Stream::Stdin) { + stdin().read_to_string(&mut buff).unwrap(); + let mut stdout = setup_term(); + more(&buff, &mut stdout, None, silent); + reset_term(&mut stdout); } else { - Mode::More - }; - - match mode { - Mode::More => more(matches), - Mode::Help => help(&usage), - Mode::Version => version(), + show_usage_error!("bad usage"); } - 0 } -fn version() { - println!("{} {}", NAME, VERSION); +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .about("A file perusal filter for CRT viewing.") + .version(crate_version!()) + .arg( + Arg::with_name(options::SILENT) + .short("d") + .long(options::SILENT) + .help("Display help instead of ringing bell"), + ) + // The commented arguments below are unimplemented: + /* + .arg( + Arg::with_name(options::LOGICAL) + .short("f") + .long(options::LOGICAL) + .help("Count logical rather than screen lines"), + ) + .arg( + Arg::with_name(options::NO_PAUSE) + .short("l") + .long(options::NO_PAUSE) + .help("Suppress pause after form feed"), + ) + .arg( + Arg::with_name(options::PRINT_OVER) + .short("c") + .long(options::PRINT_OVER) + .help("Do not scroll, display text and clean line ends"), + ) + .arg( + Arg::with_name(options::CLEAN_PRINT) + .short("p") + .long(options::CLEAN_PRINT) + .help("Do not scroll, clean screen and display text"), + ) + .arg( + Arg::with_name(options::SQUEEZE) + .short("s") + .long(options::SQUEEZE) + .help("Squeeze multiple blank lines into one"), + ) + .arg( + Arg::with_name(options::PLAIN) + .short("u") + .long(options::PLAIN) + .help("Suppress underlining and bold"), + ) + .arg( + Arg::with_name(options::LINES) + .short("n") + .long(options::LINES) + .value_name("number") + .takes_value(true) + .help("The number of lines per screen full"), + ) + .arg( + Arg::with_name(options::NUMBER) + .allow_hyphen_values(true) + .long(options::NUMBER) + .required(false) + .takes_value(true) + .help("Same as --lines"), + ) + .arg( + Arg::with_name(options::FROM_LINE) + .short("F") + .allow_hyphen_values(true) + .required(false) + .takes_value(true) + .value_name("number") + .help("Display file beginning from line number"), + ) + .arg( + Arg::with_name(options::PATTERN) + .short("P") + .allow_hyphen_values(true) + .required(false) + .takes_value(true) + .help("Display file beginning from pattern match"), + ) + */ + .arg( + Arg::with_name(options::FILES) + .required(false) + .multiple(true) + .help("Path to the files to be read"), + ) } -fn help(usage: &str) { - let msg = format!( - "{0} {1}\n\n\ - Usage: {0} TARGET\n \ - \n\ - {2}", - NAME, VERSION, usage - ); - println!("{}", msg); +#[cfg(not(target_os = "fuchsia"))] +fn setup_term() -> std::io::Stdout { + let stdout = stdout(); + terminal::enable_raw_mode().unwrap(); + stdout } -#[cfg(all(unix, not(target_os = "fuchsia")))] -fn setup_term() -> termios::Termios { - let mut term = termios::tcgetattr(0).unwrap(); - // Unset canonical mode, so we get characters immediately - term.c_lflag.remove(termios::ICANON); - // Disable local echo - term.c_lflag.remove(termios::ECHO); - termios::tcsetattr(0, termios::TCSADRAIN, &term).unwrap(); - term -} - -#[cfg(any(windows, target_os = "fuchsia"))] +#[cfg(target_os = "fuchsia")] #[inline(always)] fn setup_term() -> usize { 0 } -#[cfg(target_os = "redox")] -fn setup_term() -> redox_termios::Termios { - let mut term = redox_termios::Termios::default(); - let fd = syscall::dup(0, b"termios").unwrap(); - syscall::read(fd, &mut term).unwrap(); - term.c_lflag &= !redox_termios::ICANON; - term.c_lflag &= !redox_termios::ECHO; - syscall::write(fd, &term).unwrap(); - let _ = syscall::close(fd); - term +#[cfg(not(target_os = "fuchsia"))] +fn reset_term(stdout: &mut std::io::Stdout) { + terminal::disable_raw_mode().unwrap(); + // Clear the prompt + queue!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap(); + // Move cursor to the beginning without printing new line + print!("\r"); + stdout.flush().unwrap(); } -#[cfg(all(unix, not(target_os = "fuchsia")))] -fn reset_term(term: &mut termios::Termios) { - term.c_lflag.insert(termios::ICANON); - term.c_lflag.insert(termios::ECHO); - termios::tcsetattr(0, termios::TCSADRAIN, &term).unwrap(); -} - -#[cfg(any(windows, target_os = "fuchsia"))] +#[cfg(target_os = "fuchsia")] #[inline(always)] fn reset_term(_: &mut usize) {} -#[cfg(any(target_os = "redox"))] -fn reset_term(term: &mut redox_termios::Termios) { - let fd = syscall::dup(0, b"termios").unwrap(); - syscall::read(fd, term).unwrap(); - term.c_lflag |= redox_termios::ICANON; - term.c_lflag |= redox_termios::ECHO; - syscall::write(fd, &term).unwrap(); - let _ = syscall::close(fd); +fn more(buff: &str, mut stdout: &mut Stdout, next_file: Option<&str>, silent: bool) { + let (cols, rows) = terminal::size().unwrap(); + let lines = break_buff(buff, usize::from(cols)); + + let mut pager = Pager::new(rows, lines, next_file, silent); + pager.draw(stdout, None); + if pager.should_close() { + return; + } + + loop { + let mut wrong_key = None; + if event::poll(Duration::from_millis(10)).unwrap() { + match event::read().unwrap() { + Event::Key(KeyEvent { + code: KeyCode::Char('q'), + modifiers: KeyModifiers::NONE, + }) + | Event::Key(KeyEvent { + code: KeyCode::Char('c'), + modifiers: KeyModifiers::CONTROL, + }) => { + reset_term(&mut stdout); + std::process::exit(0); + } + Event::Key(KeyEvent { + code: KeyCode::Down, + modifiers: KeyModifiers::NONE, + }) + | Event::Key(KeyEvent { + code: KeyCode::Char(' '), + modifiers: KeyModifiers::NONE, + }) => { + if pager.should_close() { + return; + } else { + pager.page_down(); + } + } + Event::Key(KeyEvent { + code: KeyCode::Up, + modifiers: KeyModifiers::NONE, + }) => { + pager.page_up(); + } + Event::Resize(col, row) => { + pager.page_resize(col, row); + } + Event::Key(KeyEvent { + code: KeyCode::Char(k), + .. + }) => wrong_key = Some(k), + _ => continue, + } + + pager.draw(stdout, wrong_key); + } + } } -fn more(matches: getopts::Matches) { - let files = matches.free; - let mut f = File::open(files.first().unwrap()).unwrap(); - let mut buffer = [0; 1024]; +struct Pager<'a> { + // The current line at the top of the screen + upper_mark: usize, + // The number of rows that fit on the screen + content_rows: u16, + lines: Vec, + next_file: Option<&'a str>, + line_count: usize, + silent: bool, +} - let mut term = setup_term(); - - let mut end = false; - while let Ok(sz) = f.read(&mut buffer) { - if sz == 0 { - break; - } - stdout().write_all(&buffer[0..sz]).unwrap(); - for byte in std::io::stdin().bytes() { - match byte.unwrap() { - b' ' => break, - b'q' | 27 => { - end = true; - break; - } - _ => (), - } - } - - if end { - break; +impl<'a> Pager<'a> { + fn new(rows: u16, lines: Vec, next_file: Option<&'a str>, silent: bool) -> Self { + let line_count = lines.len(); + Self { + upper_mark: 0, + content_rows: rows.saturating_sub(1), + lines, + next_file, + line_count, + silent, } } - reset_term(&mut term); - println!(); + fn should_close(&mut self) -> bool { + self.upper_mark + .saturating_add(self.content_rows.into()) + .ge(&self.line_count) + } + + fn page_down(&mut self) { + self.upper_mark = self.upper_mark.saturating_add(self.content_rows.into()); + } + + fn page_up(&mut self) { + self.upper_mark = self.upper_mark.saturating_sub(self.content_rows.into()); + } + + // TODO: Deal with column size changes. + fn page_resize(&mut self, _: u16, row: u16) { + self.content_rows = row.saturating_sub(1); + } + + fn draw(&self, stdout: &mut std::io::Stdout, wrong_key: Option) { + let lower_mark = self + .line_count + .min(self.upper_mark.saturating_add(self.content_rows.into())); + self.draw_lines(stdout); + self.draw_prompt(stdout, lower_mark, wrong_key); + stdout.flush().unwrap(); + } + + fn draw_lines(&self, stdout: &mut std::io::Stdout) { + execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap(); + let displayed_lines = self + .lines + .iter() + .skip(self.upper_mark) + .take(self.content_rows.into()); + + for line in displayed_lines { + stdout + .write_all(format!("\r{}\n", line).as_bytes()) + .unwrap(); + } + } + + fn draw_prompt(&self, stdout: &mut Stdout, lower_mark: usize, wrong_key: Option) { + let status_inner = if lower_mark == self.line_count { + format!("Next file: {}", self.next_file.unwrap_or_default()) + } else { + format!( + "{}%", + (lower_mark as f64 / self.line_count as f64 * 100.0).round() as u16 + ) + }; + + let status = format!("--More--({})", status_inner); + + let banner = match (self.silent, wrong_key) { + (true, Some(key)) => { + format!( + "{} [Unknown key: '{}'. Press 'h' for instructions. (unimplemented)]", + status, key + ) + } + (true, None) => format!("{}[Press space to continue, 'q' to quit.]", status), + (false, Some(_)) => format!("{}{}", status, BELL), + (false, None) => status, + }; + + write!( + stdout, + "\r{}{}{}", + Attribute::Reverse, + banner, + Attribute::Reset + ) + .unwrap(); + } +} + +// Break the lines on the cols of the terminal +fn break_buff(buff: &str, cols: usize) -> Vec { + let mut lines = Vec::with_capacity(buff.lines().count()); + + for l in buff.lines() { + lines.append(&mut break_line(l, cols)); + } + lines +} + +fn break_line(line: &str, cols: usize) -> Vec { + let width = UnicodeWidthStr::width(line); + let mut lines = Vec::new(); + if width < cols { + lines.push(line.to_string()); + return lines; + } + + let gr_idx = UnicodeSegmentation::grapheme_indices(line, true); + let mut last_index = 0; + let mut total_width = 0; + for (index, grapheme) in gr_idx { + let width = UnicodeWidthStr::width(grapheme); + total_width += width; + + if total_width > cols { + lines.push(line[last_index..index].to_string()); + last_index = index; + total_width = width; + } + } + + if last_index != line.len() { + lines.push(line[last_index..].to_string()); + } + lines +} + +#[cfg(test)] +mod tests { + use super::break_line; + use unicode_width::UnicodeWidthStr; + + #[test] + fn test_break_lines_long() { + let mut test_string = String::with_capacity(100); + for _ in 0..200 { + test_string.push('#'); + } + + let lines = break_line(&test_string, 80); + let widths: Vec = lines + .iter() + .map(|s| UnicodeWidthStr::width(&s[..])) + .collect(); + + assert_eq!((80, 80, 40), (widths[0], widths[1], widths[2])); + } + + #[test] + fn test_break_lines_short() { + let mut test_string = String::with_capacity(100); + for _ in 0..20 { + test_string.push('#'); + } + + let lines = break_line(&test_string, 80); + + assert_eq!(20, lines[0].len()); + } + + #[test] + fn test_break_line_zwj() { + let mut test_string = String::with_capacity(1100); + for _ in 0..20 { + test_string.push_str("👩🏻‍🔬"); + } + + let lines = break_line(&test_string, 80); + + let widths: Vec = lines + .iter() + .map(|s| UnicodeWidthStr::width(&s[..])) + .collect(); + + // Each 👩🏻‍🔬 is 6 character width it break line to the closest number to 80 => 6 * 13 = 78 + assert_eq!((78, 42), (widths[0], widths[1])); + } } diff --git a/src/uu/mv/Cargo.toml b/src/uu/mv/Cargo.toml index 1a4f90801..8f1e7b9ee 100644 --- a/src/uu/mv/Cargo.toml +++ b/src/uu/mv/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_mv" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "mv ~ (uutils) move (rename) SOURCE to DESTINATION" @@ -17,7 +17,7 @@ path = "src/mv.rs" [dependencies] clap = "2.33" fs_extra = "1.1.0" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/mv/src/main.rs b/src/uu/mv/src/main.rs index c7e321234..49f7956e8 100644 --- a/src/uu/mv/src/main.rs +++ b/src/uu/mv/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_mv); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_mv); diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index 6575ad37a..4a761861f 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -11,7 +11,7 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg, ArgMatches}; +use clap::{crate_version, App, Arg, ArgMatches}; use std::env; use std::fs; use std::io::{self, stdin}; @@ -20,6 +20,7 @@ use std::os::unix; #[cfg(windows)] use std::os::windows; use std::path::{Path, PathBuf}; +use uucore::backup_control::{self, BackupMode}; use fs_extra::dir::{move_dir, CopyOptions as DirCopyOptions}; @@ -40,16 +41,8 @@ pub enum OverwriteMode { Force, } -#[derive(Clone, Copy, Eq, PartialEq)] -pub enum BackupMode { - NoBackup, - SimpleBackup, - NumberedBackup, - ExistingBackup, -} - static ABOUT: &str = "Move SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static LONG_HELP: &str = ""; static OPT_BACKUP: &str = "backup"; static OPT_BACKUP_NO_ARG: &str = "b"; @@ -77,23 +70,72 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() + .after_help(&*format!( + "{}\n{}", + LONG_HELP, + backup_control::BACKUP_CONTROL_LONG_HELP + )) .usage(&usage[..]) + .get_matches_from(args); + + let files: Vec = matches + .values_of(ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + let overwrite_mode = determine_overwrite_mode(&matches); + let backup_mode = backup_control::determine_backup_mode( + matches.is_present(OPT_BACKUP_NO_ARG) || matches.is_present(OPT_BACKUP), + matches.value_of(OPT_BACKUP), + ); + + if overwrite_mode == OverwriteMode::NoClobber && backup_mode != BackupMode::NoBackup { + show_usage_error!("options --backup and --no-clobber are mutually exclusive"); + return 1; + } + + let backup_suffix = backup_control::determine_backup_suffix(matches.value_of(OPT_SUFFIX)); + + let behavior = Behavior { + overwrite: overwrite_mode, + backup: backup_mode, + suffix: backup_suffix, + update: matches.is_present(OPT_UPDATE), + target_dir: matches.value_of(OPT_TARGET_DIRECTORY).map(String::from), + no_target_dir: matches.is_present(OPT_NO_TARGET_DIRECTORY), + verbose: matches.is_present(OPT_VERBOSE), + }; + + let paths: Vec = { + fn strip_slashes(p: &Path) -> &Path { + p.components().as_path() + } + let to_owned = |p: &Path| p.to_owned(); + let paths = files.iter().map(Path::new); + + if matches.is_present(OPT_STRIP_TRAILING_SLASHES) { + paths.map(strip_slashes).map(to_owned).collect() + } else { + paths.map(to_owned).collect() + } + }; + + exec(&paths[..], behavior) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) .arg( Arg::with_name(OPT_BACKUP) .long(OPT_BACKUP) .help("make a backup of each existing destination file") .takes_value(true) - .possible_value("simple") - .possible_value("never") - .possible_value("numbered") - .possible_value("t") - .possible_value("existing") - .possible_value("nil") - .possible_value("none") - .possible_value("off") + .require_equals(true) + .min_values(0) + .possible_values(backup_control::BACKUP_CONTROL_VALUES) .value_name("CONTROL") ) .arg( @@ -164,52 +206,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .min_values(2) .required(true) ) - .get_matches_from(args); - - let files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let overwrite_mode = determine_overwrite_mode(&matches); - let backup_mode = determine_backup_mode(&matches); - - if overwrite_mode == OverwriteMode::NoClobber && backup_mode != BackupMode::NoBackup { - show_error!( - "options --backup and --no-clobber are mutually exclusive\n\ - Try '{} --help' for more information.", - executable!() - ); - return 1; - } - - let backup_suffix = determine_backup_suffix(backup_mode, &matches); - - let behavior = Behavior { - overwrite: overwrite_mode, - backup: backup_mode, - suffix: backup_suffix, - update: matches.is_present(OPT_UPDATE), - target_dir: matches.value_of(OPT_TARGET_DIRECTORY).map(String::from), - no_target_dir: matches.is_present(OPT_NO_TARGET_DIRECTORY), - verbose: matches.is_present(OPT_VERBOSE), - }; - - let paths: Vec = { - fn strip_slashes(p: &Path) -> &Path { - p.components().as_path() - } - let to_owned = |p: &Path| p.to_owned(); - let paths = files.iter().map(Path::new); - - if matches.is_present(OPT_STRIP_TRAILING_SLASHES) { - paths.map(strip_slashes).map(to_owned).collect() - } else { - paths.map(to_owned).collect() - } - }; - - exec(&paths[..], behavior) } fn determine_overwrite_mode(matches: &ArgMatches) -> OverwriteMode { @@ -227,37 +223,6 @@ fn determine_overwrite_mode(matches: &ArgMatches) -> OverwriteMode { } } -fn determine_backup_mode(matches: &ArgMatches) -> BackupMode { - if matches.is_present(OPT_BACKUP_NO_ARG) { - BackupMode::SimpleBackup - } else if matches.is_present(OPT_BACKUP) { - match matches.value_of(OPT_BACKUP).map(String::from) { - None => BackupMode::SimpleBackup, - Some(mode) => match &mode[..] { - "simple" | "never" => BackupMode::SimpleBackup, - "numbered" | "t" => BackupMode::NumberedBackup, - "existing" | "nil" => BackupMode::ExistingBackup, - "none" | "off" => BackupMode::NoBackup, - _ => panic!(), // cannot happen as it is managed by clap - }, - } - } else { - BackupMode::NoBackup - } -} - -fn determine_backup_suffix(backup_mode: BackupMode, matches: &ArgMatches) -> String { - if matches.is_present(OPT_SUFFIX) { - matches.value_of(OPT_SUFFIX).map(String::from).unwrap() - } else if let (Ok(s), BackupMode::SimpleBackup) = - (env::var("SIMPLE_BACKUP_SUFFIX"), backup_mode) - { - s - } else { - "~".to_owned() - } -} - fn exec(files: &[PathBuf], b: Behavior) -> i32 { if let Some(ref name) = b.target_dir { return move_files_into_dir(files, &PathBuf::from(name), &b); @@ -273,7 +238,7 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 { // lacks permission to access metadata. if source.symlink_metadata().is_err() { show_error!( - "cannot stat ‘{}’: No such file or directory", + "cannot stat '{}': No such file or directory", source.display() ); return 1; @@ -283,7 +248,7 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 { if b.no_target_dir { if !source.is_dir() { show_error!( - "cannot overwrite directory ‘{}’ with non-directory", + "cannot overwrite directory '{}' with non-directory", target.display() ); return 1; @@ -292,10 +257,10 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 { return match rename(source, target, &b) { Err(e) => { show_error!( - "cannot move ‘{}’ to ‘{}’: {}", + "cannot move '{}' to '{}': {}", source.display(), target.display(), - e + e.to_string() ); 1 } @@ -306,7 +271,7 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 { return move_files_into_dir(&[source.clone()], target, &b); } else if target.exists() && source.is_dir() { show_error!( - "cannot overwrite non-directory ‘{}’ with directory ‘{}’", + "cannot overwrite non-directory '{}' with directory '{}'", target.display(), source.display() ); @@ -321,7 +286,7 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 { _ => { if b.no_target_dir { show_error!( - "mv: extra operand ‘{}’\n\ + "mv: extra operand '{}'\n\ Try '{} --help' for more information.", files[2].display(), executable!() @@ -335,9 +300,9 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 { 0 } -fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> i32 { +fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> i32 { if !target_dir.is_dir() { - show_error!("target ‘{}’ is not a directory", target_dir.display()); + show_error!("target '{}' is not a directory", target_dir.display()); return 1; } @@ -347,7 +312,7 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> Some(name) => target_dir.join(name), None => { show_error!( - "cannot stat ‘{}’: No such file or directory", + "cannot stat '{}': No such file or directory", sourcepath.display() ); @@ -358,14 +323,15 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> if let Err(e) = rename(sourcepath, &targetpath, b) { show_error!( - "mv: cannot move ‘{}’ to ‘{}’: {}", + "cannot move '{}' to '{}': {}", sourcepath.display(), targetpath.display(), - e + e.to_string() ); all_successful = false; } } + if all_successful { 0 } else { @@ -373,14 +339,14 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> } } -fn rename(from: &PathBuf, to: &PathBuf, b: &Behavior) -> io::Result<()> { +fn rename(from: &Path, to: &Path, b: &Behavior) -> io::Result<()> { let mut backup_path = None; if to.exists() { match b.overwrite { OverwriteMode::NoClobber => return Ok(()), OverwriteMode::Interactive => { - print!("{}: overwrite ‘{}’? ", executable!(), to.display()); + println!("{}: overwrite '{}'? ", executable!(), to.display()); if !read_yes() { return Ok(()); } @@ -388,12 +354,7 @@ fn rename(from: &PathBuf, to: &PathBuf, b: &Behavior) -> io::Result<()> { OverwriteMode::Force => {} }; - backup_path = match b.backup { - BackupMode::NoBackup => None, - BackupMode::SimpleBackup => Some(simple_backup_path(to, &b.suffix)), - BackupMode::NumberedBackup => Some(numbered_backup_path(to)), - BackupMode::ExistingBackup => Some(existing_backup_path(to, &b.suffix)), - }; + backup_path = backup_control::get_backup_path(b.backup, to, &b.suffix); if let Some(ref backup_path) = backup_path { rename_with_fallback(to, backup_path)?; } @@ -418,9 +379,9 @@ fn rename(from: &PathBuf, to: &PathBuf, b: &Behavior) -> io::Result<()> { rename_with_fallback(from, to)?; if b.verbose { - print!("‘{}’ -> ‘{}’", from.display(), to.display()); + print!("'{}' -> '{}'", from.display(), to.display()); match backup_path { - Some(path) => println!(" (backup: ‘{}’)", path.display()), + Some(path) => println!(" (backup: '{}')", path.display()), None => println!(), } } @@ -429,14 +390,14 @@ fn rename(from: &PathBuf, to: &PathBuf, b: &Behavior) -> io::Result<()> { /// A wrapper around `fs::rename`, so that if it fails, we try falling back on /// copying and removing. -fn rename_with_fallback(from: &PathBuf, to: &PathBuf) -> io::Result<()> { +fn rename_with_fallback(from: &Path, to: &Path) -> io::Result<()> { if fs::rename(from, to).is_err() { // Get metadata without following symlinks let metadata = from.symlink_metadata()?; let file_type = metadata.file_type(); if file_type.is_symlink() { - rename_symlink_fallback(&from, &to)?; + rename_symlink_fallback(from, to)?; } else if file_type.is_dir() { // We remove the destination directory if it exists to match the // behavior of `fs::rename`. As far as I can tell, `fs_extra`'s @@ -452,7 +413,13 @@ fn rename_with_fallback(from: &PathBuf, to: &PathBuf) -> io::Result<()> { ..DirCopyOptions::new() }; if let Err(err) = move_dir(from, to, &options) { - return Err(io::Error::new(io::ErrorKind::Other, format!("{:?}", err))); + return match err.kind { + fs_extra::error::ErrorKind::PermissionDenied => Err(io::Error::new( + io::ErrorKind::PermissionDenied, + "Permission denied", + )), + _ => Err(io::Error::new(io::ErrorKind::Other, format!("{:?}", err))), + }; } } else { fs::copy(from, to).and_then(|_| fs::remove_file(from))?; @@ -464,7 +431,7 @@ fn rename_with_fallback(from: &PathBuf, to: &PathBuf) -> io::Result<()> { /// Move the given symlink to the given destination. On Windows, dangling /// symlinks return an error. #[inline] -fn rename_symlink_fallback(from: &PathBuf, to: &PathBuf) -> io::Result<()> { +fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> { let path_symlink_points_to = fs::read_link(from)?; #[cfg(unix)] { @@ -507,29 +474,7 @@ fn read_yes() -> bool { } } -fn simple_backup_path(path: &PathBuf, suffix: &str) -> PathBuf { - let mut p = path.to_string_lossy().into_owned(); - p.push_str(suffix); - PathBuf::from(p) -} - -fn numbered_backup_path(path: &PathBuf) -> PathBuf { - (1_u64..) - .map(|i| path.with_extension(format!("~{}~", i))) - .find(|p| !p.exists()) - .expect("cannot create backup") -} - -fn existing_backup_path(path: &PathBuf, suffix: &str) -> PathBuf { - let test_path = path.with_extension("~1~"); - if test_path.exists() { - numbered_backup_path(path) - } else { - simple_backup_path(path, suffix) - } -} - -fn is_empty_dir(path: &PathBuf) -> bool { +fn is_empty_dir(path: &Path) -> bool { match fs::read_dir(path) { Ok(contents) => contents.peekable().peek().is_none(), Err(_e) => false, diff --git a/src/uu/nice/Cargo.toml b/src/uu/nice/Cargo.toml index e7d184c96..279e79ae3 100644 --- a/src/uu/nice/Cargo.toml +++ b/src/uu/nice/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_nice" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "nice ~ (uutils) run PROGRAM with modified scheduling priority" @@ -15,9 +15,10 @@ edition = "2018" path = "src/nice.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +nix = { version="<=0.13" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/nice/src/main.rs b/src/uu/nice/src/main.rs index 25da035aa..039f40d9d 100644 --- a/src/uu/nice/src/main.rs +++ b/src/uu/nice/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_nice); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_nice); diff --git a/src/uu/nice/src/nice.rs b/src/uu/nice/src/nice.rs index 1f79ea09b..d5a4094d1 100644 --- a/src/uu/nice/src/nice.rs +++ b/src/uu/nice/src/nice.rs @@ -15,8 +15,7 @@ use std::ffi::CString; use std::io::Error; use std::ptr; -const NAME: &str = "nice"; -const VERSION: &str = env!("CARGO_PKG_VERSION"); +use clap::{crate_version, App, AppSettings, Arg}; // XXX: PRIO_PROCESS is 0 on at least FreeBSD and Linux. Don't know about Mac OS X. const PRIO_PROCESS: c_int = 0; @@ -26,64 +25,44 @@ extern "C" { fn setpriority(which: c_int, who: c_int, prio: c_int) -> c_int; } -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); +pub mod options { + pub static ADJUSTMENT: &str = "adjustment"; + pub static COMMAND: &str = "COMMAND"; +} - let mut opts = getopts::Options::new(); - - opts.optopt( - "n", - "adjustment", - "add N to the niceness (default is 10)", - "N", - ); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(err) => { - show_error!("{}", err); - return 125; - } - }; - - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; - } - - if matches.opt_present("help") { - let msg = format!( - "{0} {1} - -Usage: +fn get_usage() -> String { + format!( + " {0} [OPTIONS] [COMMAND [ARGS]] Run COMMAND with an adjusted niceness, which affects process scheduling. With no COMMAND, print the current niceness. Niceness values range from at least -20 (most favorable to the process) to 19 (least favorable to the process).", - NAME, VERSION - ); + executable!() + ) +} - print!("{}", opts.usage(&msg)); - return 0; - } +pub fn uumain(args: impl uucore::Args) -> i32 { + let usage = get_usage(); - let mut niceness = unsafe { getpriority(PRIO_PROCESS, 0) }; + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let mut niceness = unsafe { + nix::errno::Errno::clear(); + getpriority(PRIO_PROCESS, 0) + }; if Error::last_os_error().raw_os_error().unwrap() != 0 { - show_error!("{}", Error::last_os_error()); + show_error!("getpriority: {}", Error::last_os_error()); return 125; } - let adjustment = match matches.opt_str("adjustment") { + let adjustment = match matches.value_of(options::ADJUSTMENT) { Some(nstr) => { - if matches.free.is_empty() { + if !matches.is_present(options::COMMAND) { show_error!( - "A command must be given with an adjustment. - Try \"{} --help\" for more information.", - args[0] + "A command must be given with an adjustment.\nTry \"{} --help\" for more information.", + executable!() ); return 125; } @@ -96,7 +75,7 @@ process).", } } None => { - if matches.free.is_empty() { + if !matches.is_present(options::COMMAND) { println!("{}", niceness); return 0; } @@ -105,28 +84,41 @@ process).", }; niceness += adjustment; - unsafe { - setpriority(PRIO_PROCESS, 0, niceness); - } - if Error::last_os_error().raw_os_error().unwrap() != 0 { - show_warning!("{}", Error::last_os_error()); + if unsafe { setpriority(PRIO_PROCESS, 0, niceness) } == -1 { + show_warning!("setpriority: {}", Error::last_os_error()); } let cstrs: Vec = matches - .free - .iter() + .values_of(options::COMMAND) + .unwrap() .map(|x| CString::new(x.as_bytes()).unwrap()) .collect(); + let mut args: Vec<*const c_char> = cstrs.iter().map(|s| s.as_ptr()).collect(); args.push(ptr::null::()); unsafe { execvp(args[0], args.as_mut_ptr()); } - show_error!("{}", Error::last_os_error()); + show_error!("execvp: {}", Error::last_os_error()); if Error::last_os_error().raw_os_error().unwrap() as c_int == libc::ENOENT { 127 } else { 126 } } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .setting(AppSettings::TrailingVarArg) + .version(crate_version!()) + .arg( + Arg::with_name(options::ADJUSTMENT) + .short("n") + .long(options::ADJUSTMENT) + .help("add N to the niceness (default is 10)") + .takes_value(true) + .allow_hyphen_values(true), + ) + .arg(Arg::with_name(options::COMMAND).multiple(true)) +} diff --git a/src/uu/nl/Cargo.toml b/src/uu/nl/Cargo.toml index d5cddbde2..a51a2555e 100644 --- a/src/uu/nl/Cargo.toml +++ b/src/uu/nl/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_nl" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "nl ~ (uutils) display input with added line numbers" @@ -15,13 +15,13 @@ edition = "2018" path = "src/nl.rs" [dependencies] +clap = "2.33.3" aho-corasick = "0.7.3" -getopts = "0.2.18" libc = "0.2.42" memchr = "2.2.0" regex = "1.0.1" regex-syntax = "0.6.7" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/nl/src/helper.rs b/src/uu/nl/src/helper.rs index 9b98129f1..562cdeb93 100644 --- a/src/uu/nl/src/helper.rs +++ b/src/uu/nl/src/helper.rs @@ -1,5 +1,7 @@ // spell-checker:ignore (ToDO) conv +use crate::options; + // parse_style parses a style string into a NumberingStyle. fn parse_style(chars: &[char]) -> Result { if chars.len() == 1 && chars[0] == 'a' { @@ -23,19 +25,19 @@ fn parse_style(chars: &[char]) -> Result { // parse_options loads the options into the settings, returning an array of // error messages. -pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> Vec { +pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) -> Vec { // This vector holds error messages encountered. let mut errs: Vec = vec![]; - settings.renumber = !opts.opt_present("p"); - match opts.opt_str("s") { + settings.renumber = !opts.is_present(options::NO_RENUMBER); + match opts.value_of(options::NUMBER_SEPARATOR) { None => {} Some(val) => { - settings.number_separator = val; + settings.number_separator = val.to_owned(); } } - match opts.opt_str("n") { + match opts.value_of(options::NUMBER_FORMAT) { None => {} - Some(val) => match val.as_ref() { + Some(val) => match val { "ln" => { settings.number_format = crate::NumberFormat::Left; } @@ -50,7 +52,7 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> } }, } - match opts.opt_str("b") { + match opts.value_of(options::BODY_NUMBERING) { None => {} Some(val) => { let chars: Vec = val.chars().collect(); @@ -64,7 +66,7 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> } } } - match opts.opt_str("f") { + match opts.value_of(options::FOOTER_NUMBERING) { None => {} Some(val) => { let chars: Vec = val.chars().collect(); @@ -78,7 +80,7 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> } } } - match opts.opt_str("h") { + match opts.value_of(options::HEADER_NUMBERING) { None => {} Some(val) => { let chars: Vec = val.chars().collect(); @@ -92,7 +94,7 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> } } } - match opts.opt_str("i") { + match opts.value_of(options::LINE_INCREMENT) { None => {} Some(val) => { let conv: Option = val.parse().ok(); @@ -104,7 +106,7 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> } } } - match opts.opt_str("w") { + match opts.value_of(options::NUMBER_WIDTH) { None => {} Some(val) => { let conv: Option = val.parse().ok(); @@ -116,7 +118,7 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> } } } - match opts.opt_str("v") { + match opts.value_of(options::STARTING_LINE_NUMBER) { None => {} Some(val) => { let conv: Option = val.parse().ok(); @@ -128,7 +130,7 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &getopts::Matches) -> } } } - match opts.opt_str("l") { + match opts.value_of(options::JOIN_BLANK_LINES) { None => {} Some(val) => { let conv: Option = val.parse().ok(); diff --git a/src/uu/nl/src/main.rs b/src/uu/nl/src/main.rs index fac355069..072fad504 100644 --- a/src/uu/nl/src/main.rs +++ b/src/uu/nl/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_nl); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_nl); diff --git a/src/uu/nl/src/nl.rs b/src/uu/nl/src/nl.rs index 47b6c3ae9..81e76aa26 100644 --- a/src/uu/nl/src/nl.rs +++ b/src/uu/nl/src/nl.rs @@ -11,15 +11,16 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{stdin, BufRead, BufReader, Read}; use std::iter::repeat; use std::path::Path; +use uucore::InvalidEncodingHandling; mod helper; static NAME: &str = "nl"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static USAGE: &str = "nl [OPTION]... [FILE]..."; // A regular expression matching everything. @@ -67,78 +68,27 @@ enum NumberFormat { RightZero, } +pub mod options { + pub const FILE: &str = "file"; + pub const BODY_NUMBERING: &str = "body-numbering"; + pub const SECTION_DELIMITER: &str = "section-delimiter"; + pub const FOOTER_NUMBERING: &str = "footer-numbering"; + pub const HEADER_NUMBERING: &str = "header-numbering"; + pub const LINE_INCREMENT: &str = "line-increment"; + pub const JOIN_BLANK_LINES: &str = "join-blank-lines"; + pub const NUMBER_FORMAT: &str = "number-format"; + pub const NO_RENUMBER: &str = "no-renumber"; + pub const NUMBER_SEPARATOR: &str = "number-separator"; + pub const STARTING_LINE_NUMBER: &str = "starting-line-number"; + pub const NUMBER_WIDTH: &str = "number-width"; +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = getopts::Options::new(); - - opts.optopt( - "b", - "body-numbering", - "use STYLE for numbering body lines", - "STYLE", - ); - opts.optopt( - "d", - "section-delimiter", - "use CC for separating logical pages", - "CC", - ); - opts.optopt( - "f", - "footer-numbering", - "use STYLE for numbering footer lines", - "STYLE", - ); - opts.optopt( - "h", - "header-numbering", - "use STYLE for numbering header lines", - "STYLE", - ); - opts.optopt( - "i", - "line-increment", - "line number increment at each line", - "", - ); - opts.optopt( - "l", - "join-blank-lines", - "group of NUMBER empty lines counted as one", - "NUMBER", - ); - opts.optopt( - "n", - "number-format", - "insert line numbers according to FORMAT", - "FORMAT", - ); - opts.optflag( - "p", - "no-renumber", - "do not reset line numbers at logical pages", - ); - opts.optopt( - "s", - "number-separator", - "add STRING after (possible) line number", - "STRING", - ); - opts.optopt( - "v", - "starting-line-number", - "first line number on each logical page", - "NUMBER", - ); - opts.optopt( - "w", - "number-width", - "use NUMBER columns for line numbers", - "NUMBER", - ); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("V", "version", "version"); + let matches = uu_app().get_matches_from(args); // A mutable settings object, initialized with the defaults. let mut settings = Settings { @@ -155,27 +105,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { number_separator: String::from("\t"), }; - let given_options = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => { - show_error!("{}", f); - print_usage(&opts); - return 1; - } - }; - - if given_options.opt_present("help") { - print_usage(&opts); - return 0; - } - if given_options.opt_present("version") { - version(); - return 0; - } - // Update the settings from the command line options, and terminate the // program if some options could not successfully be parsed. - let parse_errors = helper::parse_options(&mut settings, &given_options); + let parse_errors = helper::parse_options(&mut settings, &matches); if !parse_errors.is_empty() { show_error!("Invalid arguments supplied."); for message in &parse_errors { @@ -184,8 +116,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 { return 1; } - let files = given_options.free; - let mut read_stdin = files.is_empty(); + let mut read_stdin = false; + let files: Vec = match matches.values_of(options::FILE) { + Some(v) => v.clone().map(|v| v.to_owned()).collect(), + None => vec!["-".to_owned()], + }; for file in &files { if file == "-" { @@ -207,6 +142,90 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(USAGE) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) + .arg( + Arg::with_name(options::BODY_NUMBERING) + .short("b") + .long(options::BODY_NUMBERING) + .help("use STYLE for numbering body lines") + .value_name("SYNTAX"), + ) + .arg( + Arg::with_name(options::SECTION_DELIMITER) + .short("d") + .long(options::SECTION_DELIMITER) + .help("use CC for separating logical pages") + .value_name("CC"), + ) + .arg( + Arg::with_name(options::FOOTER_NUMBERING) + .short("f") + .long(options::FOOTER_NUMBERING) + .help("use STYLE for numbering footer lines") + .value_name("STYLE"), + ) + .arg( + Arg::with_name(options::HEADER_NUMBERING) + .short("h") + .long(options::HEADER_NUMBERING) + .help("use STYLE for numbering header lines") + .value_name("STYLE"), + ) + .arg( + Arg::with_name(options::LINE_INCREMENT) + .short("i") + .long(options::LINE_INCREMENT) + .help("line number increment at each line") + .value_name("NUMBER"), + ) + .arg( + Arg::with_name(options::JOIN_BLANK_LINES) + .short("l") + .long(options::JOIN_BLANK_LINES) + .help("group of NUMBER empty lines counted as one") + .value_name("NUMBER"), + ) + .arg( + Arg::with_name(options::NUMBER_FORMAT) + .short("n") + .long(options::NUMBER_FORMAT) + .help("insert line numbers according to FORMAT") + .value_name("FORMAT"), + ) + .arg( + Arg::with_name(options::NO_RENUMBER) + .short("p") + .long(options::NO_RENUMBER) + .help("do not reset line numbers at logical pages"), + ) + .arg( + Arg::with_name(options::NUMBER_SEPARATOR) + .short("s") + .long(options::NUMBER_SEPARATOR) + .help("add STRING after (possible) line number") + .value_name("STRING"), + ) + .arg( + Arg::with_name(options::STARTING_LINE_NUMBER) + .short("v") + .long(options::STARTING_LINE_NUMBER) + .help("first line number on each logical page") + .value_name("NUMBER"), + ) + .arg( + Arg::with_name(options::NUMBER_WIDTH) + .short("w") + .long(options::NUMBER_WIDTH) + .help("use NUMBER columns for line numbers") + .value_name("NUMBER"), + ) +} + // nl implements the main functionality for an individual buffer. fn nl(reader: &mut BufReader, settings: &Settings) { let regexp: regex::Regex = regex::Regex::new(r".?").unwrap(); @@ -231,7 +250,7 @@ fn nl(reader: &mut BufReader, settings: &Settings) { let mut line_filter: fn(&str, ®ex::Regex) -> bool = pass_regex; for mut l in reader.lines().map(|r| r.unwrap()) { // Sanitize the string. We want to print the newline ourselves. - if !l.is_empty() && l.chars().rev().next().unwrap() == '\n' { + if l.ends_with('\n') { l.pop(); } // Next we iterate through the individual chars to see if this @@ -370,11 +389,3 @@ fn pass_none(_: &str, _: ®ex::Regex) -> bool { fn pass_all(_: &str, _: ®ex::Regex) -> bool { true } - -fn print_usage(opts: &getopts::Options) { - println!("{}", opts.usage(USAGE)); -} - -fn version() { - println!("{} {}", NAME, VERSION); -} diff --git a/src/uu/nohup/Cargo.toml b/src/uu/nohup/Cargo.toml index 0c5709a65..839219a84 100644 --- a/src/uu/nohup/Cargo.toml +++ b/src/uu/nohup/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_nohup" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "nohup ~ (uutils) run COMMAND, ignoring hangup signals" @@ -15,9 +15,10 @@ edition = "2018" path = "src/nohup.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +atty = "0.2" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/nohup/src/main.rs b/src/uu/nohup/src/main.rs index d46ceb7cb..2007711f6 100644 --- a/src/uu/nohup/src/main.rs +++ b/src/uu/nohup/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_nohup); // spell-checker:ignore procs uucore nohup +uucore_procs::main!(uu_nohup); diff --git a/src/uu/nohup/src/nohup.rs b/src/uu/nohup/src/nohup.rs index 67e281e38..acc101e4e 100644 --- a/src/uu/nohup/src/nohup.rs +++ b/src/uu/nohup/src/nohup.rs @@ -10,6 +10,7 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, AppSettings, Arg}; use libc::{c_char, dup2, execvp, signal}; use libc::{SIGHUP, SIG_IGN}; use std::env; @@ -18,52 +19,34 @@ use std::fs::{File, OpenOptions}; use std::io::Error; use std::os::unix::prelude::*; use std::path::{Path, PathBuf}; -use uucore::fs::{is_stderr_interactive, is_stdin_interactive, is_stdout_interactive}; +use uucore::InvalidEncodingHandling; -static NAME: &str = "nohup"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Run COMMAND ignoring hangup signals."; +static LONG_HELP: &str = " +If standard input is terminal, it'll be replaced with /dev/null. +If standard output is terminal, it'll be appended to nohup.out instead, +or $HOME/nohup.out, if nohup.out open failed. +If standard error is terminal, it'll be redirected to stdout. +"; +static NOHUP_OUT: &str = "nohup.out"; +// exit codes that match the GNU implementation +static EXIT_CANCELED: i32 = 125; +static EXIT_CANNOT_INVOKE: i32 = 126; +static EXIT_ENOENT: i32 = 127; +static POSIX_NOHUP_FAILURE: i32 = 127; -#[cfg(target_os = "macos")] -extern "C" { - fn _vprocmgr_detach_from_console(flags: u32) -> *const libc::c_int; -} - -#[cfg(any(target_os = "linux", target_os = "freebsd"))] -unsafe fn _vprocmgr_detach_from_console(_: u32) -> *const libc::c_int { - std::ptr::null() +mod options { + pub const CMD: &str = "cmd"; } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let usage = get_usage(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = getopts::Options::new(); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - opts.optflag("h", "help", "Show help and exit"); - opts.optflag("V", "version", "Show version and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => { - show_error!("{}", f); - show_usage(&opts); - return 1; - } - }; - - if matches.opt_present("V") { - println!("{} {}", NAME, VERSION); - return 0; - } - if matches.opt_present("h") { - show_usage(&opts); - return 0; - } - - if matches.free.is_empty() { - show_error!("Missing operand: COMMAND"); - println!("Try `{} --help` for more information.", NAME); - return 1; - } replace_fds(); unsafe { signal(SIGHUP, SIG_IGN) }; @@ -73,17 +56,36 @@ pub fn uumain(args: impl uucore::Args) -> i32 { }; let cstrs: Vec = matches - .free - .iter() + .values_of(options::CMD) + .unwrap() .map(|x| CString::new(x.as_bytes()).unwrap()) .collect(); let mut args: Vec<*const c_char> = cstrs.iter().map(|s| s.as_ptr()).collect(); args.push(std::ptr::null()); - unsafe { execvp(args[0], args.as_mut_ptr()) } + + let ret = unsafe { execvp(args[0], args.as_mut_ptr()) }; + match ret { + libc::ENOENT => EXIT_ENOENT, + _ => EXIT_CANNOT_INVOKE, + } +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .after_help(LONG_HELP) + .arg( + Arg::with_name(options::CMD) + .hidden(true) + .required(true) + .multiple(true), + ) + .setting(AppSettings::TrailingVarArg) } fn replace_fds() { - if is_stdin_interactive() { + if atty::is(atty::Stream::Stdin) { let new_stdin = match File::open(Path::new("/dev/null")) { Ok(t) => t, Err(e) => crash!(2, "Cannot replace STDIN: {}", e), @@ -93,7 +95,7 @@ fn replace_fds() { } } - if is_stdout_interactive() { + if atty::is(atty::Stream::Stdout) { let new_stdout = find_stdout(); let fd = new_stdout.as_raw_fd(); @@ -102,29 +104,38 @@ fn replace_fds() { } } - if is_stderr_interactive() && unsafe { dup2(1, 2) } != 2 { + if atty::is(atty::Stream::Stderr) && unsafe { dup2(1, 2) } != 2 { crash!(2, "Cannot replace STDERR: {}", Error::last_os_error()) } } fn find_stdout() -> File { + let internal_failure_code = match std::env::var("POSIXLY_CORRECT") { + Ok(_) => POSIX_NOHUP_FAILURE, + Err(_) => EXIT_CANCELED, + }; + match OpenOptions::new() .write(true) .create(true) .append(true) - .open(Path::new("nohup.out")) + .open(Path::new(NOHUP_OUT)) { Ok(t) => { - show_warning!("Output is redirected to: nohup.out"); + show_error!("ignoring input and appending output to '{}'", NOHUP_OUT); t } - Err(e) => { + Err(e1) => { let home = match env::var("HOME") { - Err(_) => crash!(2, "Cannot replace STDOUT: {}", e), + Err(_) => { + show_error!("failed to open '{}': {}", NOHUP_OUT, e1); + exit!(internal_failure_code) + } Ok(h) => h, }; let mut homeout = PathBuf::from(home); - homeout.push("nohup.out"); + homeout.push(NOHUP_OUT); + let homeout_str = homeout.to_str().unwrap(); match OpenOptions::new() .write(true) .create(true) @@ -132,30 +143,29 @@ fn find_stdout() -> File { .open(&homeout) { Ok(t) => { - show_warning!("Output is redirected to: {:?}", homeout); + show_error!("ignoring input and appending output to '{}'", homeout_str); t } - Err(e) => crash!(2, "Cannot replace STDOUT: {}", e), + Err(e2) => { + show_error!("failed to open '{}': {}", NOHUP_OUT, e1); + show_error!("failed to open '{}': {}", homeout_str, e2); + exit!(internal_failure_code) + } } } } } -fn show_usage(opts: &getopts::Options) { - let msg = format!( - "{0} {1} - -Usage: - {0} COMMAND [ARG]... - {0} OPTION - -Run COMMAND ignoring hangup signals. -If standard input is terminal, it'll be replaced with /dev/null. -If standard output is terminal, it'll be appended to nohup.out instead, -or $HOME/nohup.out, if nohup.out open failed. -If standard error is terminal, it'll be redirected to stdout.", - NAME, VERSION - ); - - print!("{}", opts.usage(&msg)); +fn get_usage() -> String { + format!("{0} COMMAND [ARG]...\n {0} FLAG", executable!()) +} + +#[cfg(target_vendor = "apple")] +extern "C" { + fn _vprocmgr_detach_from_console(flags: u32) -> *const libc::c_int; +} + +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +unsafe fn _vprocmgr_detach_from_console(_: u32) -> *const libc::c_int { + std::ptr::null() } diff --git a/src/uu/nproc/Cargo.toml b/src/uu/nproc/Cargo.toml index 98f9a4afa..be9d8f2e3 100644 --- a/src/uu/nproc/Cargo.toml +++ b/src/uu/nproc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_nproc" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "nproc ~ (uutils) display the number of processing units available" @@ -18,7 +18,7 @@ path = "src/nproc.rs" libc = "0.2.42" num_cpus = "1.10" clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/nproc/src/main.rs b/src/uu/nproc/src/main.rs index 7650cce09..356c2101f 100644 --- a/src/uu/nproc/src/main.rs +++ b/src/uu/nproc/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_nproc); // spell-checker:ignore procs uucore nproc +uucore_procs::main!(uu_nproc); diff --git a/src/uu/nproc/src/nproc.rs b/src/uu/nproc/src/nproc.rs index 4eb538618..1f284685b 100644 --- a/src/uu/nproc/src/nproc.rs +++ b/src/uu/nproc/src/nproc.rs @@ -10,12 +10,12 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::env; #[cfg(target_os = "linux")] pub const _SC_NPROCESSORS_CONF: libc::c_int = 83; -#[cfg(target_os = "macos")] +#[cfg(target_vendor = "apple")] pub const _SC_NPROCESSORS_CONF: libc::c_int = libc::_SC_NPROCESSORS_CONF; #[cfg(target_os = "freebsd")] pub const _SC_NPROCESSORS_CONF: libc::c_int = 57; @@ -25,7 +25,6 @@ pub const _SC_NPROCESSORS_CONF: libc::c_int = 1001; static OPT_ALL: &str = "all"; static OPT_IGNORE: &str = "ignore"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Print the number of cores available to the current process."; fn get_usage() -> String { @@ -34,24 +33,7 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(OPT_ALL) - .short("") - .long(OPT_ALL) - .help("print the number of cores available to the system"), - ) - .arg( - Arg::with_name(OPT_IGNORE) - .short("") - .long(OPT_IGNORE) - .takes_value(true) - .help("ignore up to N cores"), - ) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); let mut ignore = match matches.value_of(OPT_IGNORE) { Some(numstr) => match numstr.parse() { @@ -87,9 +69,28 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(OPT_ALL) + .short("") + .long(OPT_ALL) + .help("print the number of cores available to the system"), + ) + .arg( + Arg::with_name(OPT_IGNORE) + .short("") + .long(OPT_IGNORE) + .takes_value(true) + .help("ignore up to N cores"), + ) +} + #[cfg(any( target_os = "linux", - target_os = "macos", + target_vendor = "apple", target_os = "freebsd", target_os = "netbsd" ))] @@ -109,7 +110,7 @@ fn num_cpus_all() -> usize { // Other platforms (e.g., windows), num_cpus::get() directly. #[cfg(not(any( target_os = "linux", - target_os = "macos", + target_vendor = "apple", target_os = "freebsd", target_os = "netbsd" )))] diff --git a/src/uu/numfmt/Cargo.toml b/src/uu/numfmt/Cargo.toml index b8e54656c..ac5266d68 100644 --- a/src/uu/numfmt/Cargo.toml +++ b/src/uu/numfmt/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_numfmt" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "numfmt ~ (uutils) reformat NUMBER" @@ -16,7 +16,7 @@ path = "src/numfmt.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs new file mode 100644 index 000000000..e44446818 --- /dev/null +++ b/src/uu/numfmt/src/format.rs @@ -0,0 +1,301 @@ +use crate::options::{NumfmtOptions, RoundMethod}; +use crate::units::{DisplayableSuffix, RawSuffix, Result, Suffix, Unit, IEC_BASES, SI_BASES}; + +/// Iterate over a line's fields, where each field is a contiguous sequence of +/// non-whitespace, optionally prefixed with one or more characters of leading +/// whitespace. Fields are returned as tuples of `(prefix, field)`. +/// +/// # Examples: +/// +/// ``` +/// let mut fields = uu_numfmt::format::WhitespaceSplitter { s: Some(" 1234 5") }; +/// +/// assert_eq!(Some((" ", "1234")), fields.next()); +/// assert_eq!(Some((" ", "5")), fields.next()); +/// assert_eq!(None, fields.next()); +/// ``` +/// +/// Delimiters are included in the results; `prefix` will be empty only for +/// the first field of the line (including the case where the input line is +/// empty): +/// +/// ``` +/// let mut fields = uu_numfmt::format::WhitespaceSplitter { s: Some("first second") }; +/// +/// assert_eq!(Some(("", "first")), fields.next()); +/// assert_eq!(Some((" ", "second")), fields.next()); +/// +/// let mut fields = uu_numfmt::format::WhitespaceSplitter { s: Some("") }; +/// +/// assert_eq!(Some(("", "")), fields.next()); +/// ``` +pub struct WhitespaceSplitter<'a> { + pub s: Option<&'a str>, +} + +impl<'a> Iterator for WhitespaceSplitter<'a> { + type Item = (&'a str, &'a str); + + /// Yield the next field in the input string as a tuple `(prefix, field)`. + fn next(&mut self) -> Option { + let haystack = self.s?; + + let (prefix, field) = haystack.split_at( + haystack + .find(|c: char| !c.is_whitespace()) + .unwrap_or_else(|| haystack.len()), + ); + + let (field, rest) = field.split_at( + field + .find(|c: char| c.is_whitespace()) + .unwrap_or_else(|| field.len()), + ); + + self.s = if !rest.is_empty() { Some(rest) } else { None }; + + Some((prefix, field)) + } +} + +fn parse_suffix(s: &str) -> Result<(f64, Option)> { + if s.is_empty() { + return Err("invalid number: ''".to_string()); + } + + let with_i = s.ends_with('i'); + let mut iter = s.chars(); + if with_i { + iter.next_back(); + } + let suffix = match iter.next_back() { + Some('K') => Some((RawSuffix::K, with_i)), + Some('M') => Some((RawSuffix::M, with_i)), + Some('G') => Some((RawSuffix::G, with_i)), + Some('T') => Some((RawSuffix::T, with_i)), + Some('P') => Some((RawSuffix::P, with_i)), + Some('E') => Some((RawSuffix::E, with_i)), + Some('Z') => Some((RawSuffix::Z, with_i)), + Some('Y') => Some((RawSuffix::Y, with_i)), + Some('0'..='9') => None, + _ => return Err(format!("invalid suffix in input: '{}'", s)), + }; + + let suffix_len = match suffix { + None => 0, + Some((_, false)) => 1, + Some((_, true)) => 2, + }; + + let number = s[..s.len() - suffix_len] + .parse::() + .map_err(|_| format!("invalid number: '{}'", s))?; + + Ok((number, suffix)) +} + +fn remove_suffix(i: f64, s: Option, u: &Unit) -> Result { + match (s, u) { + (None, _) => Ok(i), + (Some((raw_suffix, false)), &Unit::Auto) | (Some((raw_suffix, false)), &Unit::Si) => { + match raw_suffix { + RawSuffix::K => Ok(i * 1e3), + RawSuffix::M => Ok(i * 1e6), + RawSuffix::G => Ok(i * 1e9), + RawSuffix::T => Ok(i * 1e12), + RawSuffix::P => Ok(i * 1e15), + RawSuffix::E => Ok(i * 1e18), + RawSuffix::Z => Ok(i * 1e21), + RawSuffix::Y => Ok(i * 1e24), + } + } + (Some((raw_suffix, false)), &Unit::Iec(false)) + | (Some((raw_suffix, true)), &Unit::Auto) + | (Some((raw_suffix, true)), &Unit::Iec(true)) => match raw_suffix { + RawSuffix::K => Ok(i * IEC_BASES[1]), + RawSuffix::M => Ok(i * IEC_BASES[2]), + RawSuffix::G => Ok(i * IEC_BASES[3]), + RawSuffix::T => Ok(i * IEC_BASES[4]), + RawSuffix::P => Ok(i * IEC_BASES[5]), + RawSuffix::E => Ok(i * IEC_BASES[6]), + RawSuffix::Z => Ok(i * IEC_BASES[7]), + RawSuffix::Y => Ok(i * IEC_BASES[8]), + }, + (_, _) => Err("This suffix is unsupported for specified unit".to_owned()), + } +} + +fn transform_from(s: &str, opts: &Unit) -> Result { + let (i, suffix) = parse_suffix(s)?; + + remove_suffix(i, suffix, opts).map(|n| if n < 0.0 { -n.abs().ceil() } else { n.ceil() }) +} + +/// Divide numerator by denominator, with rounding. +/// +/// If the result of the division is less than 10.0, round to one decimal point. +/// +/// Otherwise, round to an integer. +/// +/// # Examples: +/// +/// ``` +/// use uu_numfmt::format::div_round; +/// use uu_numfmt::options::RoundMethod; +/// +/// // Rounding methods: +/// assert_eq!(div_round(1.01, 1.0, RoundMethod::FromZero), 1.1); +/// assert_eq!(div_round(1.01, 1.0, RoundMethod::TowardsZero), 1.0); +/// assert_eq!(div_round(1.01, 1.0, RoundMethod::Up), 1.1); +/// assert_eq!(div_round(1.01, 1.0, RoundMethod::Down), 1.0); +/// assert_eq!(div_round(1.01, 1.0, RoundMethod::Nearest), 1.0); +/// +/// // Division: +/// assert_eq!(div_round(999.1, 1000.0, RoundMethod::FromZero), 1.0); +/// assert_eq!(div_round(1001., 10., RoundMethod::FromZero), 101.); +/// assert_eq!(div_round(9991., 10., RoundMethod::FromZero), 1000.); +/// assert_eq!(div_round(-12.34, 1.0, RoundMethod::FromZero), -13.0); +/// assert_eq!(div_round(1000.0, -3.14, RoundMethod::FromZero), -319.0); +/// assert_eq!(div_round(-271828.0, -271.0, RoundMethod::FromZero), 1004.0); +/// ``` +pub fn div_round(n: f64, d: f64, method: RoundMethod) -> f64 { + let v = n / d; + + if v.abs() < 10.0 { + method.round(10.0 * v) / 10.0 + } else { + method.round(v) + } +} + +fn consider_suffix(n: f64, u: &Unit, round_method: RoundMethod) -> Result<(f64, Option)> { + use crate::units::RawSuffix::*; + + let abs_n = n.abs(); + let suffixes = [K, M, G, T, P, E, Z, Y]; + + let (bases, with_i) = match *u { + Unit::Si => (&SI_BASES, false), + Unit::Iec(with_i) => (&IEC_BASES, with_i), + Unit::Auto => return Err("Unit 'auto' isn't supported with --to options".to_owned()), + Unit::None => return Ok((n, None)), + }; + + let i = match abs_n { + _ if abs_n <= bases[1] - 1.0 => return Ok((n, None)), + _ if abs_n < bases[2] => 1, + _ if abs_n < bases[3] => 2, + _ if abs_n < bases[4] => 3, + _ if abs_n < bases[5] => 4, + _ if abs_n < bases[6] => 5, + _ if abs_n < bases[7] => 6, + _ if abs_n < bases[8] => 7, + _ if abs_n < bases[9] => 8, + _ => return Err("Number is too big and unsupported".to_string()), + }; + + let v = div_round(n, bases[i], round_method); + + // check if rounding pushed us into the next base + if v.abs() >= bases[1] { + Ok((v / bases[1], Some((suffixes[i], with_i)))) + } else { + Ok((v, Some((suffixes[i - 1], with_i)))) + } +} + +fn transform_to(s: f64, opts: &Unit, round_method: RoundMethod) -> Result { + let (i2, s) = consider_suffix(s, opts, round_method)?; + Ok(match s { + None => format!("{}", i2), + Some(s) if i2.abs() < 10.0 => format!("{:.1}{}", i2, DisplayableSuffix(s)), + Some(s) => format!("{:.0}{}", i2, DisplayableSuffix(s)), + }) +} + +fn format_string( + source: &str, + options: &NumfmtOptions, + implicit_padding: Option, +) -> Result { + let number = transform_to( + transform_from(source, &options.transform.from)?, + &options.transform.to, + options.round, + )?; + + Ok(match implicit_padding.unwrap_or(options.padding) { + 0 => number, + p if p > 0 => format!("{:>padding$}", number, padding = p as usize), + p => format!("{: Result<()> { + let delimiter = options.delimiter.as_ref().unwrap(); + + for (n, field) in (1..).zip(s.split(delimiter)) { + let field_selected = uucore::ranges::contain(&options.fields, n); + + // print delimiter before second and subsequent fields + if n > 1 { + print!("{}", delimiter); + } + + if field_selected { + print!("{}", format_string(field.trim_start(), options, None)?); + } else { + // print unselected field without conversion + print!("{}", field); + } + } + + println!(); + + Ok(()) +} + +fn format_and_print_whitespace(s: &str, options: &NumfmtOptions) -> Result<()> { + for (n, (prefix, field)) in (1..).zip(WhitespaceSplitter { s: Some(s) }) { + let field_selected = uucore::ranges::contain(&options.fields, n); + + if field_selected { + let empty_prefix = prefix.is_empty(); + + // print delimiter before second and subsequent fields + let prefix = if n > 1 { + print!(" "); + &prefix[1..] + } else { + &prefix + }; + + let implicit_padding = if !empty_prefix && options.padding == 0 { + Some((prefix.len() + field.len()) as isize) + } else { + None + }; + + print!("{}", format_string(field, options, implicit_padding)?); + } else { + // print unselected field without conversion + print!("{}{}", prefix, field); + } + } + + println!(); + + Ok(()) +} + +/// Format a line of text according to the selected options. +/// +/// Given a line of text `s`, split the line into fields, transform and format +/// any selected numeric fields, and print the result to stdout. Fields not +/// selected for conversion are passed through unmodified. +pub fn format_and_print(s: &str, options: &NumfmtOptions) -> Result<()> { + match &options.delimiter { + Some(_) => format_and_print_delimited(s, options), + None => format_and_print_whitespace(s, options), + } +} diff --git a/src/uu/numfmt/src/main.rs b/src/uu/numfmt/src/main.rs index 084d494f2..f4d991727 100644 --- a/src/uu/numfmt/src/main.rs +++ b/src/uu/numfmt/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_numfmt); // spell-checker:ignore procs uucore numfmt +uucore_procs::main!(uu_numfmt); diff --git a/src/uu/numfmt/src/numfmt.rs b/src/uu/numfmt/src/numfmt.rs index ea750c024..01f12c51b 100644 --- a/src/uu/numfmt/src/numfmt.rs +++ b/src/uu/numfmt/src/numfmt.rs @@ -5,15 +5,22 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. +// spell-checker:ignore N'th M'th + #[macro_use] extern crate uucore; -use clap::{App, AppSettings, Arg, ArgMatches}; -use std::fmt; +use crate::format::format_and_print; +use crate::options::*; +use crate::units::{Result, Unit}; +use clap::{crate_version, App, AppSettings, Arg, ArgMatches}; use std::io::{BufRead, Write}; use uucore::ranges::Range; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +pub mod format; +pub mod options; +mod units; + static ABOUT: &str = "Convert numbers from/to human-readable strings"; static LONG_HELP: &str = "UNIT options: none no auto-scaling is done; suffixes will trigger an error @@ -43,415 +50,10 @@ FIELDS supports cut(1) style field ranges: Multiple fields/ranges can be separated with commas "; -mod options { - pub const FIELD: &str = "field"; - pub const FIELD_DEFAULT: &str = "1"; - pub const FROM: &str = "from"; - pub const FROM_DEFAULT: &str = "none"; - pub const HEADER: &str = "header"; - pub const HEADER_DEFAULT: &str = "1"; - pub const NUMBER: &str = "NUMBER"; - pub const PADDING: &str = "padding"; - pub const TO: &str = "to"; - pub const TO_DEFAULT: &str = "none"; -} - fn get_usage() -> String { format!("{0} [OPTION]... [NUMBER]...", executable!()) } -const SI_BASES: [f64; 10] = [1., 1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24, 1e27]; - -const IEC_BASES: [f64; 10] = [ - 1., - 1_024., - 1_048_576., - 1_073_741_824., - 1_099_511_627_776., - 1_125_899_906_842_624., - 1_152_921_504_606_846_976., - 1_180_591_620_717_411_303_424., - 1_208_925_819_614_629_174_706_176., - 1_237_940_039_285_380_274_899_124_224., -]; - -type Result = std::result::Result; - -type WithI = bool; - -enum Unit { - Auto, - Si, - Iec(WithI), - None, -} - -#[derive(Clone, Copy, Debug)] -enum RawSuffix { - K, - M, - G, - T, - P, - E, - Z, - Y, -} - -type Suffix = (RawSuffix, WithI); - -struct DisplayableSuffix(Suffix); - -impl fmt::Display for DisplayableSuffix { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let DisplayableSuffix((ref raw_suffix, ref with_i)) = *self; - match raw_suffix { - RawSuffix::K => write!(f, "K"), - RawSuffix::M => write!(f, "M"), - RawSuffix::G => write!(f, "G"), - RawSuffix::T => write!(f, "T"), - RawSuffix::P => write!(f, "P"), - RawSuffix::E => write!(f, "E"), - RawSuffix::Z => write!(f, "Z"), - RawSuffix::Y => write!(f, "Y"), - } - .and_then(|()| match with_i { - true => write!(f, "i"), - false => Ok(()), - }) - } -} - -fn parse_suffix(s: &str) -> Result<(f64, Option)> { - if s.is_empty() { - return Err("invalid number: ‘’".to_string()); - } - - let with_i = s.ends_with('i'); - let mut iter = s.chars(); - if with_i { - iter.next_back(); - } - let suffix: Option = match iter.next_back() { - Some('K') => Ok(Some((RawSuffix::K, with_i))), - Some('M') => Ok(Some((RawSuffix::M, with_i))), - Some('G') => Ok(Some((RawSuffix::G, with_i))), - Some('T') => Ok(Some((RawSuffix::T, with_i))), - Some('P') => Ok(Some((RawSuffix::P, with_i))), - Some('E') => Ok(Some((RawSuffix::E, with_i))), - Some('Z') => Ok(Some((RawSuffix::Z, with_i))), - Some('Y') => Ok(Some((RawSuffix::Y, with_i))), - Some('0'..='9') => Ok(None), - _ => Err(format!("invalid suffix in input: ‘{}’", s)), - }?; - - let suffix_len = match suffix { - None => 0, - Some((_, false)) => 1, - Some((_, true)) => 2, - }; - - let number = s[..s.len() - suffix_len] - .parse::() - .map_err(|_| format!("invalid number: ‘{}’", s))?; - - Ok((number, suffix)) -} - -fn parse_unit(s: &str) -> Result { - match s { - "auto" => Ok(Unit::Auto), - "si" => Ok(Unit::Si), - "iec" => Ok(Unit::Iec(false)), - "iec-i" => Ok(Unit::Iec(true)), - "none" => Ok(Unit::None), - _ => Err("Unsupported unit is specified".to_owned()), - } -} - -struct TransformOptions { - from: Transform, - to: Transform, -} - -struct Transform { - unit: Unit, -} - -struct NumfmtOptions { - transform: TransformOptions, - padding: isize, - header: usize, - fields: Vec, -} - -/// Iterate over a line's fields, where each field is a contiguous sequence of -/// non-whitespace, optionally prefixed with one or more characters of leading -/// whitespace. Fields are returned as tuples of `(prefix, field)`. -/// -/// # Examples: -/// -/// ``` -/// let mut fields = uu_numfmt::WhitespaceSplitter { s: Some(" 1234 5") }; -/// -/// assert_eq!(Some((" ", "1234")), fields.next()); -/// assert_eq!(Some((" ", "5")), fields.next()); -/// assert_eq!(None, fields.next()); -/// ``` -/// -/// Delimiters are included in the results; `prefix` will be empty only for -/// the first field of the line (including the case where the input line is -/// empty): -/// -/// ``` -/// let mut fields = uu_numfmt::WhitespaceSplitter { s: Some("first second") }; -/// -/// assert_eq!(Some(("", "first")), fields.next()); -/// assert_eq!(Some((" ", "second")), fields.next()); -/// -/// let mut fields = uu_numfmt::WhitespaceSplitter { s: Some("") }; -/// -/// assert_eq!(Some(("", "")), fields.next()); -/// ``` -pub struct WhitespaceSplitter<'a> { - pub s: Option<&'a str>, -} - -impl<'a> Iterator for WhitespaceSplitter<'a> { - type Item = (&'a str, &'a str); - - /// Yield the next field in the input string as a tuple `(prefix, field)`. - fn next(&mut self) -> Option { - let haystack = self.s?; - - let (prefix, field) = haystack.split_at( - haystack - .find(|c: char| !c.is_whitespace()) - .unwrap_or_else(|| haystack.len()), - ); - - let (field, rest) = field.split_at( - field - .find(|c: char| c.is_whitespace()) - .unwrap_or_else(|| field.len()), - ); - - self.s = if !rest.is_empty() { Some(rest) } else { None }; - - Some((prefix, field)) - } -} - -fn remove_suffix(i: f64, s: Option, u: &Unit) -> Result { - match (s, u) { - (None, _) => Ok(i), - (Some((raw_suffix, false)), &Unit::Auto) | (Some((raw_suffix, false)), &Unit::Si) => { - match raw_suffix { - RawSuffix::K => Ok(i * 1e3), - RawSuffix::M => Ok(i * 1e6), - RawSuffix::G => Ok(i * 1e9), - RawSuffix::T => Ok(i * 1e12), - RawSuffix::P => Ok(i * 1e15), - RawSuffix::E => Ok(i * 1e18), - RawSuffix::Z => Ok(i * 1e21), - RawSuffix::Y => Ok(i * 1e24), - } - } - (Some((raw_suffix, false)), &Unit::Iec(false)) - | (Some((raw_suffix, true)), &Unit::Auto) - | (Some((raw_suffix, true)), &Unit::Iec(true)) => match raw_suffix { - RawSuffix::K => Ok(i * IEC_BASES[1]), - RawSuffix::M => Ok(i * IEC_BASES[2]), - RawSuffix::G => Ok(i * IEC_BASES[3]), - RawSuffix::T => Ok(i * IEC_BASES[4]), - RawSuffix::P => Ok(i * IEC_BASES[5]), - RawSuffix::E => Ok(i * IEC_BASES[6]), - RawSuffix::Z => Ok(i * IEC_BASES[7]), - RawSuffix::Y => Ok(i * IEC_BASES[8]), - }, - (_, _) => Err("This suffix is unsupported for specified unit".to_owned()), - } -} - -fn transform_from(s: &str, opts: &Transform) -> Result { - let (i, suffix) = parse_suffix(s)?; - - remove_suffix(i, suffix, &opts.unit).map(|n| if n < 0.0 { -n.abs().ceil() } else { n.ceil() }) -} - -/// Divide numerator by denominator, with ceiling. -/// -/// If the result of the division is less than 10.0, truncate the result -/// to the next highest tenth. -/// -/// Otherwise, truncate the result to the next highest whole number. -/// -/// # Examples: -/// -/// ``` -/// use uu_numfmt::div_ceil; -/// -/// assert_eq!(div_ceil(1.01, 1.0), 1.1); -/// assert_eq!(div_ceil(999.1, 1000.), 1.0); -/// assert_eq!(div_ceil(1001., 10.), 101.); -/// assert_eq!(div_ceil(9991., 10.), 1000.); -/// assert_eq!(div_ceil(-12.34, 1.0), -13.0); -/// assert_eq!(div_ceil(1000.0, -3.14), -319.0); -/// assert_eq!(div_ceil(-271828.0, -271.0), 1004.0); -/// ``` -pub fn div_ceil(n: f64, d: f64) -> f64 { - let v = n / (d / 10.0); - let (v, sign) = if v < 0.0 { (v.abs(), -1.0) } else { (v, 1.0) }; - - if v < 100.0 { - v.ceil() / 10.0 * sign - } else { - (v / 10.0).ceil() * sign - } -} - -fn consider_suffix(n: f64, u: &Unit) -> Result<(f64, Option)> { - use RawSuffix::*; - - let abs_n = n.abs(); - let suffixes = [K, M, G, T, P, E, Z, Y]; - - let (bases, with_i) = match *u { - Unit::Si => (&SI_BASES, false), - Unit::Iec(with_i) => (&IEC_BASES, with_i), - Unit::Auto => return Err("Unit 'auto' isn't supported with --to options".to_owned()), - Unit::None => return Ok((n, None)), - }; - - let i = match abs_n { - _ if abs_n <= bases[1] - 1.0 => return Ok((n, None)), - _ if abs_n < bases[2] => 1, - _ if abs_n < bases[3] => 2, - _ if abs_n < bases[4] => 3, - _ if abs_n < bases[5] => 4, - _ if abs_n < bases[6] => 5, - _ if abs_n < bases[7] => 6, - _ if abs_n < bases[8] => 7, - _ if abs_n < bases[9] => 8, - _ => return Err("Number is too big and unsupported".to_string()), - }; - - let v = div_ceil(n, bases[i]); - - // check if rounding pushed us into the next base - if v.abs() >= bases[1] { - Ok((v / bases[1], Some((suffixes[i], with_i)))) - } else { - Ok((v, Some((suffixes[i - 1], with_i)))) - } -} - -fn transform_to(s: f64, opts: &Transform) -> Result { - let (i2, s) = consider_suffix(s, &opts.unit)?; - Ok(match s { - None => format!("{}", i2), - Some(s) if i2.abs() < 10.0 => format!("{:.1}{}", i2, DisplayableSuffix(s)), - Some(s) => format!("{:.0}{}", i2, DisplayableSuffix(s)), - }) -} - -fn format_string( - source: &str, - options: &NumfmtOptions, - implicit_padding: Option, -) -> Result { - let number = transform_to( - transform_from(source, &options.transform.from)?, - &options.transform.to, - )?; - - Ok(match implicit_padding.unwrap_or(options.padding) { - p if p == 0 => number, - p if p > 0 => format!("{:>padding$}", number, padding = p as usize), - p => format!("{: Result<()> { - for (n, (prefix, field)) in (1..).zip(WhitespaceSplitter { s: Some(s) }) { - let field_selected = uucore::ranges::contain(&options.fields, n); - - if field_selected { - let empty_prefix = prefix.is_empty(); - - // print delimiter before second and subsequent fields - let prefix = if n > 1 { - print!(" "); - &prefix[1..] - } else { - &prefix - }; - - let implicit_padding = if !empty_prefix && options.padding == 0 { - Some((prefix.len() + field.len()) as isize) - } else { - None - }; - - print!("{}", format_string(&field, options, implicit_padding)?); - } else { - // print unselected field without conversion - print!("{}{}", prefix, field); - } - } - - println!(); - - Ok(()) -} - -fn parse_options(args: &ArgMatches) -> Result { - let from = parse_unit(args.value_of(options::FROM).unwrap())?; - let to = parse_unit(args.value_of(options::TO).unwrap())?; - - let transform = TransformOptions { - from: Transform { unit: from }, - to: Transform { unit: to }, - }; - - let padding = match args.value_of(options::PADDING) { - Some(s) => s.parse::().map_err(|err| err.to_string()), - None => Ok(0), - }?; - - let header = match args.occurrences_of(options::HEADER) { - 0 => Ok(0), - _ => { - let value = args.value_of(options::HEADER).unwrap(); - - value - .parse::() - .map_err(|_| value) - .and_then(|n| match n { - 0 => Err(value), - _ => Ok(n), - }) - .map_err(|value| format!("invalid header value ‘{}’", value)) - } - }?; - - let fields = match args.value_of(options::FIELD) { - Some("-") => vec![Range { - low: 1, - high: std::usize::MAX, - }], - Some(v) => Range::from_list(v)?, - None => unreachable!(), - }; - - Ok(NumfmtOptions { - transform, - padding, - header, - fields, - }) -} - fn handle_args<'a>(args: impl Iterator, options: NumfmtOptions) -> Result<()> { for l in args { format_and_print(l, &options)?; @@ -477,15 +79,114 @@ fn handle_stdin(options: NumfmtOptions) -> Result<()> { Ok(()) } +fn parse_unit(s: &str) -> Result { + match s { + "auto" => Ok(Unit::Auto), + "si" => Ok(Unit::Si), + "iec" => Ok(Unit::Iec(false)), + "iec-i" => Ok(Unit::Iec(true)), + "none" => Ok(Unit::None), + _ => Err("Unsupported unit is specified".to_owned()), + } +} + +fn parse_options(args: &ArgMatches) -> Result { + let from = parse_unit(args.value_of(options::FROM).unwrap())?; + let to = parse_unit(args.value_of(options::TO).unwrap())?; + + let transform = TransformOptions { from, to }; + + let padding = match args.value_of(options::PADDING) { + Some(s) => s.parse::().map_err(|err| err.to_string()), + None => Ok(0), + }?; + + let header = match args.occurrences_of(options::HEADER) { + 0 => Ok(0), + _ => { + let value = args.value_of(options::HEADER).unwrap(); + + value + .parse::() + .map_err(|_| value) + .and_then(|n| match n { + 0 => Err(value), + _ => Ok(n), + }) + .map_err(|value| format!("invalid header value '{}'", value)) + } + }?; + + let fields = match args.value_of(options::FIELD).unwrap() { + "-" => vec![Range { + low: 1, + high: std::usize::MAX, + }], + v => Range::from_list(v)?, + }; + + let delimiter = args.value_of(options::DELIMITER).map_or(Ok(None), |arg| { + if arg.len() == 1 { + Ok(Some(arg.to_string())) + } else { + Err("the delimiter must be a single character".to_string()) + } + })?; + + // unwrap is fine because the argument has a default value + let round = match args.value_of(options::ROUND).unwrap() { + "up" => RoundMethod::Up, + "down" => RoundMethod::Down, + "from-zero" => RoundMethod::FromZero, + "towards-zero" => RoundMethod::TowardsZero, + "nearest" => RoundMethod::Nearest, + _ => unreachable!("Should be restricted by clap"), + }; + + Ok(NumfmtOptions { + transform, + padding, + header, + fields, + delimiter, + round, + }) +} + pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let result = + parse_options(&matches).and_then(|options| match matches.values_of(options::NUMBER) { + Some(values) => handle_args(values, options), + None => handle_stdin(options), + }); + + match result { + Err(e) => { + std::io::stdout().flush().expect("error flushing stdout"); + show_error!("{}", e); + 1 + } + _ => 0, + } +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .after_help(LONG_HELP) .setting(AppSettings::AllowNegativeNumbers) + .arg( + Arg::with_name(options::DELIMITER) + .short("d") + .long(options::DELIMITER) + .value_name("X") + .help("use X instead of whitespace for field delimiter"), + ) .arg( Arg::with_name(options::FIELD) .long(options::FIELD) @@ -512,9 +213,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .long(options::PADDING) .help( "pad the output to N characters; positive N will \ - right-align; negative N will left-align; padding is \ - ignored if the output is wider than N; the default is \ - to automatically pad if a whitespace is found", + right-align; negative N will left-align; padding is \ + ignored if the output is wider than N; the default is \ + to automatically pad if a whitespace is found", ) .value_name("N"), ) @@ -523,27 +224,22 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .long(options::HEADER) .help( "print (without converting) the first N header lines; \ - N defaults to 1 if not specified", + N defaults to 1 if not specified", ) .value_name("N") .default_value(options::HEADER_DEFAULT) .hide_default_value(true), ) + .arg( + Arg::with_name(options::ROUND) + .long(options::ROUND) + .help( + "use METHOD for rounding when scaling; METHOD can be: up,\ + down, from-zero (default), towards-zero, nearest", + ) + .value_name("METHOD") + .default_value("from-zero") + .possible_values(&["up", "down", "from-zero", "towards-zero", "nearest"]), + ) .arg(Arg::with_name(options::NUMBER).hidden(true).multiple(true)) - .get_matches_from(args); - - let result = - parse_options(&matches).and_then(|options| match matches.values_of(options::NUMBER) { - Some(values) => handle_args(values, options), - None => handle_stdin(options), - }); - - match result { - Err(e) => { - std::io::stdout().flush().expect("error flushing stdout"); - show_info!("{}", e); - 1 - } - _ => 0, - } } diff --git a/src/uu/numfmt/src/options.rs b/src/uu/numfmt/src/options.rs new file mode 100644 index 000000000..59bf9d8d3 --- /dev/null +++ b/src/uu/numfmt/src/options.rs @@ -0,0 +1,62 @@ +use crate::units::Unit; +use uucore::ranges::Range; + +pub const DELIMITER: &str = "delimiter"; +pub const FIELD: &str = "field"; +pub const FIELD_DEFAULT: &str = "1"; +pub const FROM: &str = "from"; +pub const FROM_DEFAULT: &str = "none"; +pub const HEADER: &str = "header"; +pub const HEADER_DEFAULT: &str = "1"; +pub const NUMBER: &str = "NUMBER"; +pub const PADDING: &str = "padding"; +pub const ROUND: &str = "round"; +pub const TO: &str = "to"; +pub const TO_DEFAULT: &str = "none"; + +pub struct TransformOptions { + pub from: Unit, + pub to: Unit, +} + +pub struct NumfmtOptions { + pub transform: TransformOptions, + pub padding: isize, + pub header: usize, + pub fields: Vec, + pub delimiter: Option, + pub round: RoundMethod, +} + +#[derive(Clone, Copy)] +pub enum RoundMethod { + Up, + Down, + FromZero, + TowardsZero, + Nearest, +} + +impl RoundMethod { + pub fn round(&self, f: f64) -> f64 { + match self { + RoundMethod::Up => f.ceil(), + RoundMethod::Down => f.floor(), + RoundMethod::FromZero => { + if f < 0.0 { + f.floor() + } else { + f.ceil() + } + } + RoundMethod::TowardsZero => { + if f < 0.0 { + f.ceil() + } else { + f.floor() + } + } + RoundMethod::Nearest => f.round(), + } + } +} diff --git a/src/uu/numfmt/src/units.rs b/src/uu/numfmt/src/units.rs new file mode 100644 index 000000000..8a2895ab7 --- /dev/null +++ b/src/uu/numfmt/src/units.rs @@ -0,0 +1,63 @@ +use std::fmt; + +pub const SI_BASES: [f64; 10] = [1., 1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24, 1e27]; + +pub const IEC_BASES: [f64; 10] = [ + 1., + 1_024., + 1_048_576., + 1_073_741_824., + 1_099_511_627_776., + 1_125_899_906_842_624., + 1_152_921_504_606_846_976., + 1_180_591_620_717_411_303_424., + 1_208_925_819_614_629_174_706_176., + 1_237_940_039_285_380_274_899_124_224., +]; + +pub type WithI = bool; + +pub enum Unit { + Auto, + Si, + Iec(WithI), + None, +} + +pub type Result = std::result::Result; + +#[derive(Clone, Copy, Debug)] +pub enum RawSuffix { + K, + M, + G, + T, + P, + E, + Z, + Y, +} + +pub type Suffix = (RawSuffix, WithI); + +pub struct DisplayableSuffix(pub Suffix); + +impl fmt::Display for DisplayableSuffix { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let DisplayableSuffix((ref raw_suffix, ref with_i)) = *self; + match raw_suffix { + RawSuffix::K => write!(f, "K"), + RawSuffix::M => write!(f, "M"), + RawSuffix::G => write!(f, "G"), + RawSuffix::T => write!(f, "T"), + RawSuffix::P => write!(f, "P"), + RawSuffix::E => write!(f, "E"), + RawSuffix::Z => write!(f, "Z"), + RawSuffix::Y => write!(f, "Y"), + } + .and_then(|()| match with_i { + true => write!(f, "i"), + false => Ok(()), + }) + } +} diff --git a/src/uu/od/Cargo.toml b/src/uu/od/Cargo.toml index 14aea59a7..6f9a75318 100644 --- a/src/uu/od/Cargo.toml +++ b/src/uu/od/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_od" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "od ~ (uutils) display formatted representation of input" @@ -16,10 +16,10 @@ path = "src/od.rs" [dependencies] byteorder = "1.3.2" -getopts = "0.2.18" +clap = "2.33" half = "1.6" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/od/src/formatteriteminfo.rs b/src/uu/od/src/formatteriteminfo.rs index d44d97a92..13cf62246 100644 --- a/src/uu/od/src/formatteriteminfo.rs +++ b/src/uu/od/src/formatteriteminfo.rs @@ -2,6 +2,7 @@ use std::fmt; +#[allow(clippy::enum_variant_names)] #[derive(Copy)] pub enum FormatWriter { IntWriter(fn(u64) -> String), diff --git a/src/uu/od/src/inputdecoder.rs b/src/uu/od/src/inputdecoder.rs index 3b36c28fb..606495461 100644 --- a/src/uu/od/src/inputdecoder.rs +++ b/src/uu/od/src/inputdecoder.rs @@ -115,7 +115,7 @@ impl<'a> MemoryDecoder<'a> { /// Creates a clone of the internal buffer. The clone only contain the valid data. pub fn clone_buffer(&self, other: &mut Vec) { - other.clone_from(&self.data); + other.clone_from(self.data); other.resize(self.used_normal_length, 0); } @@ -166,39 +166,30 @@ mod tests { let mut input = PeekReader::new(Cursor::new(&data)); let mut sut = InputDecoder::new(&mut input, 8, 2, ByteOrder::Little); - match sut.peek_read() { - Ok(mut mem) => { - assert_eq!(8, mem.length()); + // Peek normal length + let mut mem = sut.peek_read().unwrap(); - assert_eq!(-2.0, mem.read_float(0, 8)); - assert_eq!(-2.0, mem.read_float(4, 4)); - assert_eq!(0xc000000000000000, mem.read_uint(0, 8)); - assert_eq!(0xc0000000, mem.read_uint(4, 4)); - assert_eq!(0xc000, mem.read_uint(6, 2)); - assert_eq!(0xc0, mem.read_uint(7, 1)); - assert_eq!(&[0, 0xc0], mem.get_buffer(6)); - assert_eq!(&[0, 0xc0, 0xff, 0xff], mem.get_full_buffer(6)); + assert_eq!(8, mem.length()); - let mut copy: Vec = Vec::new(); - mem.clone_buffer(&mut copy); - assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0], copy); + assert_eq!(-2.0, mem.read_float(0, 8)); + assert_eq!(-2.0, mem.read_float(4, 4)); + assert_eq!(0xc000000000000000, mem.read_uint(0, 8)); + assert_eq!(0xc0000000, mem.read_uint(4, 4)); + assert_eq!(0xc000, mem.read_uint(6, 2)); + assert_eq!(0xc0, mem.read_uint(7, 1)); + assert_eq!(&[0, 0xc0], mem.get_buffer(6)); + assert_eq!(&[0, 0xc0, 0xff, 0xff], mem.get_full_buffer(6)); - mem.zero_out_buffer(7, 8); - assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(6)); - } - Err(e) => { - assert!(false, e); - } - } + let mut copy: Vec = Vec::new(); + mem.clone_buffer(&mut copy); + assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0], copy); - match sut.peek_read() { - Ok(mem) => { - assert_eq!(2, mem.length()); - assert_eq!(0xffff, mem.read_uint(0, 2)); - } - Err(e) => { - assert!(false, e); - } - } + mem.zero_out_buffer(7, 8); + assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(6)); + + // Peek tail + let mem = sut.peek_read().unwrap(); + assert_eq!(2, mem.length()); + assert_eq!(0xffff, mem.read_uint(0, 2)); } } diff --git a/src/uu/od/src/main.rs b/src/uu/od/src/main.rs index d5e96180c..3f30d15e8 100644 --- a/src/uu/od/src/main.rs +++ b/src/uu/od/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_od); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_od); diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 47d3c29f8..ec5bb595a 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -5,6 +5,7 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. +// spell-checker:ignore (clap) DontDelimitTrailingValues // spell-checker:ignore (ToDO) formatteriteminfo inputdecoder inputoffset mockstream nrofbytes partialreader odfunc multifile exitcode #[macro_use] @@ -41,15 +42,19 @@ use crate::parse_nrofbytes::parse_number_of_bytes; use crate::partialreader::*; use crate::peekreader::*; use crate::prn_char::format_ascii_dump; +use clap::{self, crate_version, AppSettings, Arg, ArgMatches}; +use uucore::parse_size::ParseSizeError; +use uucore::InvalidEncodingHandling; -static VERSION: &str = env!("CARGO_PKG_VERSION"); const PEEK_BUFFER_SIZE: usize = 4; // utf-8 can be 4 bytes +static ABOUT: &str = "dump files in octal and other formats"; -static USAGE: &str = r#"Usage: +static USAGE: &str = r#" od [OPTION]... [--] [FILENAME]... od [-abcdDefFhHiIlLoOsxX] [FILENAME] [[+][0x]OFFSET[.][b]] - od --traditional [OPTION]... [FILENAME] [[+][0x]OFFSET[.][b] [[+][0x]LABEL[.][b]]] + od --traditional [OPTION]... [FILENAME] [[+][0x]OFFSET[.][b] [[+][0x]LABEL[.][b]]]"#; +static LONG_HELP: &str = r#" Displays data in various human-readable formats. If multiple formats are specified, the output will contain all formats in the order they appear on the command line. Each format will be printed on a new line. Only the line @@ -88,85 +93,17 @@ Any type specification can have a "z" suffix, which will add a ASCII dump at If an error occurred, a diagnostic message will be printed to stderr, and the exitcode will be non-zero."#; -fn create_getopts_options() -> getopts::Options { - let mut opts = getopts::Options::new(); - - opts.optopt( - "A", - "address-radix", - "Select the base in which file offsets are printed.", - "RADIX", - ); - opts.optopt( - "j", - "skip-bytes", - "Skip bytes input bytes before formatting and writing.", - "BYTES", - ); - opts.optopt( - "N", - "read-bytes", - "limit dump to BYTES input bytes", - "BYTES", - ); - opts.optopt( - "", - "endian", - "byte order to use for multi-byte formats", - "big|little", - ); - opts.optopt( - "S", - "strings", - "output strings of at least BYTES graphic chars. 3 is assumed when \ - BYTES is not specified.", - "BYTES", - ); - opts.optflagmulti("a", "", "named characters, ignoring high-order bit"); - opts.optflagmulti("b", "", "octal bytes"); - opts.optflagmulti("c", "", "ASCII characters or backslash escapes"); - opts.optflagmulti("d", "", "unsigned decimal 2-byte units"); - opts.optflagmulti("D", "", "unsigned decimal 4-byte units"); - opts.optflagmulti("o", "", "octal 2-byte units"); - - opts.optflagmulti("I", "", "decimal 8-byte units"); - opts.optflagmulti("L", "", "decimal 8-byte units"); - opts.optflagmulti("i", "", "decimal 4-byte units"); - opts.optflagmulti("l", "", "decimal 8-byte units"); - opts.optflagmulti("x", "", "hexadecimal 2-byte units"); - opts.optflagmulti("h", "", "hexadecimal 2-byte units"); - - opts.optflagmulti("O", "", "octal 4-byte units"); - opts.optflagmulti("s", "", "decimal 2-byte units"); - opts.optflagmulti("X", "", "hexadecimal 4-byte units"); - opts.optflagmulti("H", "", "hexadecimal 4-byte units"); - - opts.optflagmulti("e", "", "floating point double precision (64-bit) units"); - opts.optflagmulti("f", "", "floating point single precision (32-bit) units"); - opts.optflagmulti("F", "", "floating point double precision (64-bit) units"); - - opts.optmulti("t", "format", "select output format or formats", "TYPE"); - opts.optflag( - "v", - "output-duplicates", - "do not use * to mark line suppression", - ); - opts.optflagopt( - "w", - "width", - "output BYTES bytes per output line. 32 is implied when BYTES is not \ - specified.", - "BYTES", - ); - opts.optflag("", "help", "display this help and exit."); - opts.optflag("", "version", "output version information and exit."); - opts.optflag( - "", - "traditional", - "compatibility mode with one input, offset and label.", - ); - - opts +pub(crate) mod options { + pub const ADDRESS_RADIX: &str = "address-radix"; + pub const SKIP_BYTES: &str = "skip-bytes"; + pub const READ_BYTES: &str = "read-bytes"; + pub const ENDIAN: &str = "endian"; + pub const STRINGS: &str = "strings"; + pub const FORMAT: &str = "format"; + pub const OUTPUT_DUPLICATES: &str = "output-duplicates"; + pub const TRADITIONAL: &str = "traditional"; + pub const WIDTH: &str = "width"; + pub const FILENAME: &str = "FILENAME"; } struct OdOptions { @@ -182,8 +119,8 @@ struct OdOptions { } impl OdOptions { - fn new(matches: getopts::Matches, args: Vec) -> Result { - let byte_order = match matches.opt_str("endian").as_ref().map(String::as_ref) { + fn new(matches: ArgMatches, args: Vec) -> Result { + let byte_order = match matches.value_of(options::ENDIAN) { None => ByteOrder::Native, Some("little") => ByteOrder::Little, Some("big") => ByteOrder::Big, @@ -192,41 +129,34 @@ impl OdOptions { } }; - let mut skip_bytes = match matches.opt_default("skip-bytes", "0") { - None => 0, - Some(s) => match parse_number_of_bytes(&s) { - Ok(i) => i, - Err(_) => { - return Err(format!("Invalid argument --skip-bytes={}", s)); - } - }, - }; + let mut skip_bytes = matches.value_of(options::SKIP_BYTES).map_or(0, |s| { + parse_number_of_bytes(s).unwrap_or_else(|e| { + crash!(1, "{}", format_error_message(e, s, options::SKIP_BYTES)) + }) + }); let mut label: Option = None; - let input_strings = match parse_inputs(&matches) { - Ok(CommandLineInputs::FileNames(v)) => v, - Ok(CommandLineInputs::FileAndOffset((f, s, l))) => { + let parsed_input = parse_inputs(&matches).map_err(|e| format!("Invalid inputs: {}", e))?; + let input_strings = match parsed_input { + CommandLineInputs::FileNames(v) => v, + CommandLineInputs::FileAndOffset((f, s, l)) => { skip_bytes = s; label = l; vec![f] } - Err(e) => { - return Err(format!("Invalid inputs: {}", e)); - } }; - let formats = match parse_format_flags(&args) { - Ok(f) => f, - Err(e) => { - return Err(e); - } - }; + let formats = parse_format_flags(&args)?; + + let mut line_bytes = matches.value_of(options::WIDTH).map_or(16, |s| { + if matches.occurrences_of(options::WIDTH) == 0 { + return 16; + }; + parse_number_of_bytes(s) + .unwrap_or_else(|e| crash!(1, "{}", format_error_message(e, s, options::WIDTH))) + }); - let mut line_bytes = match matches.opt_default("w", "32") { - None => 16, - Some(s) => s.parse::().unwrap_or(0), - }; let min_bytes = formats.iter().fold(1, |max, next| { cmp::max(max, next.formatter_item_info.byte_size) }); @@ -235,22 +165,18 @@ impl OdOptions { line_bytes = min_bytes; } - let output_duplicates = matches.opt_present("v"); + let output_duplicates = matches.is_present(options::OUTPUT_DUPLICATES); - let read_bytes = match matches.opt_str("read-bytes") { - None => None, - Some(s) => match parse_number_of_bytes(&s) { - Ok(i) => Some(i), - Err(_) => { - return Err(format!("Invalid argument --read-bytes={}", s)); - } - }, - }; + let read_bytes = matches.value_of(options::READ_BYTES).map(|s| { + parse_number_of_bytes(s).unwrap_or_else(|e| { + crash!(1, "{}", format_error_message(e, s, options::READ_BYTES)) + }) + }); - let radix = match matches.opt_str("A") { + let radix = match matches.value_of(options::ADDRESS_RADIX) { None => Radix::Octal, Some(s) => { - let st = s.into_bytes(); + let st = s.as_bytes(); if st.len() != 1 { return Err("Radix must be one of [d, o, n, x]".to_string()); } else { @@ -284,31 +210,19 @@ impl OdOptions { /// parses and validates command line parameters, prepares data structures, /// opens the input and calls `odfunc` to process the input. pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let opts = create_getopts_options(); + let clap_opts = uu_app(); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => { - show_usage_error!("{}", f); - return 1; - } - }; + let clap_matches = clap_opts + .clone() // Clone to reuse clap_opts to print help + .get_matches_from(args.clone()); - if matches.opt_present("help") { - println!("{}", opts.usage(&USAGE)); - return 0; - } - if matches.opt_present("version") { - println!("{} {}", executable!(), VERSION); - return 0; - } - - let od_options = match OdOptions::new(matches, args) { + let od_options = match OdOptions::new(clap_matches, args) { Err(s) => { - show_usage_error!("{}", s); - return 1; + crash!(1, "{}", s); } Ok(o) => o, }; @@ -337,6 +251,230 @@ pub fn uumain(args: impl uucore::Args) -> i32 { odfunc(&mut input_offset, &mut input_decoder, &output_info) } +pub fn uu_app() -> clap::App<'static, 'static> { + clap::App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .usage(USAGE) + .after_help(LONG_HELP) + .arg( + Arg::with_name(options::ADDRESS_RADIX) + .short("A") + .long(options::ADDRESS_RADIX) + .help("Select the base in which file offsets are printed.") + .value_name("RADIX"), + ) + .arg( + Arg::with_name(options::SKIP_BYTES) + .short("j") + .long(options::SKIP_BYTES) + .help("Skip bytes input bytes before formatting and writing.") + .value_name("BYTES"), + ) + .arg( + Arg::with_name(options::READ_BYTES) + .short("N") + .long(options::READ_BYTES) + .help("limit dump to BYTES input bytes") + .value_name("BYTES"), + ) + .arg( + Arg::with_name(options::ENDIAN) + .long(options::ENDIAN) + .help("byte order to use for multi-byte formats") + .possible_values(&["big", "little"]) + .value_name("big|little"), + ) + .arg( + Arg::with_name(options::STRINGS) + .short("S") + .long(options::STRINGS) + .help( + "NotImplemented: output strings of at least BYTES graphic chars. 3 is assumed when \ + BYTES is not specified.", + ) + .default_value("3") + .value_name("BYTES"), + ) + .arg( + Arg::with_name("a") + .short("a") + .help("named characters, ignoring high-order bit") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("b") + .short("b") + .help("octal bytes") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("c") + .short("c") + .help("ASCII characters or backslash escapes") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("d") + .short("d") + .help("unsigned decimal 2-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("D") + .short("D") + .help("unsigned decimal 4-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("o") + .short("o") + .help("octal 2-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("I") + .short("I") + .help("decimal 8-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("L") + .short("L") + .help("decimal 8-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("i") + .short("i") + .help("decimal 4-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("l") + .short("l") + .help("decimal 8-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("x") + .short("x") + .help("hexadecimal 2-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("h") + .short("h") + .help("hexadecimal 2-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("O") + .short("O") + .help("octal 4-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("s") + .short("s") + .help("decimal 2-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("X") + .short("X") + .help("hexadecimal 4-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("H") + .short("H") + .help("hexadecimal 4-byte units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("e") + .short("e") + .help("floating point double precision (64-bit) units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("f") + .short("f") + .help("floating point double precision (32-bit) units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name("F") + .short("F") + .help("floating point double precision (64-bit) units") + .multiple(true) + .takes_value(false), + ) + .arg( + Arg::with_name(options::FORMAT) + .short("t") + .long(options::FORMAT) + .help("select output format or formats") + .multiple(true) + .value_name("TYPE"), + ) + .arg( + Arg::with_name(options::OUTPUT_DUPLICATES) + .short("v") + .long(options::OUTPUT_DUPLICATES) + .help("do not use * to mark line suppression") + .takes_value(false) + .possible_values(&["big", "little"]), + ) + .arg( + Arg::with_name(options::WIDTH) + .short("w") + .long(options::WIDTH) + .help( + "output BYTES bytes per output line. 32 is implied when BYTES is not \ + specified.", + ) + .default_value("32") + .value_name("BYTES"), + ) + .arg( + Arg::with_name(options::TRADITIONAL) + .long(options::TRADITIONAL) + .help("compatibility mode with one input, offset and label.") + .takes_value(false), + ) + .arg( + Arg::with_name(options::FILENAME) + .hidden(true) + .multiple(true), + ) + .settings(&[ + AppSettings::TrailingVarArg, + AppSettings::DontDelimitTrailingValues, + AppSettings::DisableVersion, + AppSettings::DeriveDisplayOrder, + ]) +} + /// Loops through the input line by line, calling print_bytes to take care of the output. fn odfunc( input_offset: &mut InputOffset, @@ -391,7 +529,7 @@ where print_bytes( &input_offset.format_byte_offset(), &memory_decoder, - &output_info, + output_info, ); } @@ -490,3 +628,13 @@ fn open_input_peek_reader( let pr = PartialReader::new(mf, skip_bytes, read_bytes); PeekReader::new(pr) } + +fn format_error_message(error: ParseSizeError, s: &str, option: &str) -> String { + // NOTE: + // GNU's od echos affected flag, -N or --read-bytes (-j or --skip-bytes, etc.), depending user's selection + // GNU's od does distinguish between "invalid (suffix in) argument" + match error { + ParseSizeError::ParseFailure(_) => format!("invalid --{} argument '{}'", option, s), + ParseSizeError::SizeTooBig(_) => format!("--{} argument '{}' too large", option, s), + } +} diff --git a/src/uu/od/src/output_info.rs b/src/uu/od/src/output_info.rs index a204fa36e..49c2a09a2 100644 --- a/src/uu/od/src/output_info.rs +++ b/src/uu/od/src/output_info.rs @@ -68,7 +68,7 @@ impl OutputInfo { let print_width_line = print_width_block * (line_bytes / byte_size_block); let spaced_formatters = - OutputInfo::create_spaced_formatter_info(&formats, byte_size_block, print_width_block); + OutputInfo::create_spaced_formatter_info(formats, byte_size_block, print_width_block); OutputInfo { byte_size_line: line_bytes, diff --git a/src/uu/od/src/parse_formats.rs b/src/uu/od/src/parse_formats.rs index 8b32d648c..f5b150d61 100644 --- a/src/uu/od/src/parse_formats.rs +++ b/src/uu/od/src/parse_formats.rs @@ -85,12 +85,7 @@ fn od_format_type(type_char: FormatType, byte_size: u8) -> Option bool { - #[allow(clippy::match_like_matches_macro)] - // `matches!(...)` macro not stabilized until rust v1.42 - match ch { - 'A' | 'j' | 'N' | 'S' | 'w' => true, - _ => false, - } + matches!(ch, 'A' | 'j' | 'N' | 'S' | 'w') } /// Parses format flags from command line @@ -113,10 +108,8 @@ pub fn parse_format_flags(args: &[String]) -> Result formats.extend(v.into_iter()), - Err(e) => return Err(e), - } + let v = parse_type_string(arg)?; + formats.extend(v.into_iter()); expect_type_string = false; } else if arg.starts_with("--") { if arg.len() == 2 { @@ -124,10 +117,8 @@ pub fn parse_format_flags(args: &[String]) -> Result Result formats.extend(v.into_iter()), - Err(e) => return Err(e), - } + let v = parse_type_string(&format_spec)?; + formats.extend(v.into_iter()); expect_type_string = false; } } @@ -280,17 +269,13 @@ fn parse_type_string(params: &str) -> Result, Strin let mut chars = params.chars(); let mut ch = chars.next(); - while ch.is_some() { - let type_char = ch.unwrap(); - let type_char = match format_type(type_char) { - Some(t) => t, - None => { - return Err(format!( - "unexpected char '{}' in format specification '{}'", - type_char, params - )); - } - }; + while let Some(type_char) = ch { + let type_char = format_type(type_char).ok_or_else(|| { + format!( + "unexpected char '{}' in format specification '{}'", + type_char, params + ) + })?; let type_cat = format_type_category(type_char); @@ -306,30 +291,25 @@ fn parse_type_string(params: &str) -> Result, Strin ch = chars.next(); } if !decimal_size.is_empty() { - byte_size = match decimal_size.parse() { - Err(_) => { - return Err(format!( - "invalid number '{}' in format specification '{}'", - decimal_size, params - )) - } - Ok(n) => n, - } + byte_size = decimal_size.parse().map_err(|_| { + format!( + "invalid number '{}' in format specification '{}'", + decimal_size, params + ) + })?; } } if is_format_dump_char(ch, &mut show_ascii_dump) { ch = chars.next(); } - match od_format_type(type_char, byte_size) { - Some(ft) => formats.push(ParsedFormatterItemInfo::new(ft, show_ascii_dump)), - None => { - return Err(format!( - "invalid size '{}' in format specification '{}'", - byte_size, params - )) - } - } + let ft = od_format_type(type_char, byte_size).ok_or_else(|| { + format!( + "invalid size '{}' in format specification '{}'", + byte_size, params + ) + })?; + formats.push(ParsedFormatterItemInfo::new(ft, show_ascii_dump)); } Ok(formats) @@ -340,16 +320,13 @@ pub fn parse_format_flags_str( args_str: &Vec<&'static str>, ) -> Result, String> { let args: Vec = args_str.iter().map(|s| s.to_string()).collect(); - match parse_format_flags(&args) { - Err(e) => Err(e), - Ok(v) => { - // tests using this function assume add_ascii_dump is not set - Ok(v.into_iter() - .inspect(|f| assert!(!f.add_ascii_dump)) - .map(|f| f.formatter_item_info) - .collect()) - } - } + parse_format_flags(&args).map(|v| { + // tests using this function assume add_ascii_dump is not set + v.into_iter() + .inspect(|f| assert!(!f.add_ascii_dump)) + .map(|f| f.formatter_item_info) + .collect() + }) } #[test] diff --git a/src/uu/od/src/parse_inputs.rs b/src/uu/od/src/parse_inputs.rs index 89a833d94..419b7173d 100644 --- a/src/uu/od/src/parse_inputs.rs +++ b/src/uu/od/src/parse_inputs.rs @@ -1,20 +1,24 @@ -use getopts::Matches; +use super::options; +use clap::ArgMatches; /// Abstraction for getopts pub trait CommandLineOpts { /// returns all command line parameters which do not belong to an option. - fn inputs(&self) -> Vec; + fn inputs(&self) -> Vec<&str>; /// tests if any of the specified options is present. fn opts_present(&self, _: &[&str]) -> bool; } /// Implementation for `getopts` -impl CommandLineOpts for Matches { - fn inputs(&self) -> Vec { - self.free.clone() +impl<'a> CommandLineOpts for ArgMatches<'a> { + fn inputs(&self) -> Vec<&str> { + self.values_of(options::FILENAME) + .map(|values| values.collect()) + .unwrap_or_default() } + fn opts_present(&self, opts: &[&str]) -> bool { - self.opts_present(&opts.iter().map(|s| (*s).to_string()).collect::>()) + opts.iter().any(|opt| self.is_present(opt)) } } @@ -39,7 +43,7 @@ pub enum CommandLineInputs { /// '-' is used as filename if stdin is meant. This is also returned if /// there is no input, as stdin is the default input. pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result { - let mut input_strings: Vec = matches.inputs(); + let mut input_strings = matches.inputs(); if matches.opts_present(&["traditional"]) { return parse_inputs_traditional(input_strings); @@ -51,7 +55,7 @@ pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result Result Result) -> Result { +pub fn parse_inputs_traditional(input_strings: Vec<&str>) -> Result { match input_strings.len() { 0 => Ok(CommandLineInputs::FileNames(vec!["-".to_string()])), 1 => { - let offset0 = parse_offset_operand(&input_strings[0]); + let offset0 = parse_offset_operand(input_strings[0]); Ok(match offset0 { Ok(n) => CommandLineInputs::FileAndOffset(("-".to_string(), n, None)), - _ => CommandLineInputs::FileNames(input_strings), + _ => CommandLineInputs::FileNames( + input_strings.iter().map(|&s| s.to_string()).collect(), + ), }) } 2 => { - let offset0 = parse_offset_operand(&input_strings[0]); - let offset1 = parse_offset_operand(&input_strings[1]); + let offset0 = parse_offset_operand(input_strings[0]); + let offset1 = parse_offset_operand(input_strings[1]); match (offset0, offset1) { (Ok(n), Ok(m)) => Ok(CommandLineInputs::FileAndOffset(( "-".to_string(), @@ -98,7 +106,7 @@ pub fn parse_inputs_traditional(input_strings: Vec) -> Result Ok(CommandLineInputs::FileAndOffset(( - input_strings[0].clone(), + input_strings[0].to_string(), m, None, ))), @@ -106,11 +114,11 @@ pub fn parse_inputs_traditional(input_strings: Vec) -> Result { - let offset = parse_offset_operand(&input_strings[1]); - let label = parse_offset_operand(&input_strings[2]); + let offset = parse_offset_operand(input_strings[1]); + let label = parse_offset_operand(input_strings[2]); match (offset, label) { (Ok(n), Ok(m)) => Ok(CommandLineInputs::FileAndOffset(( - input_strings[0].clone(), + input_strings[0].to_string(), n, Some(m), ))), @@ -171,15 +179,15 @@ mod tests { impl<'a> MockOptions<'a> { fn new(inputs: Vec<&'a str>, option_names: Vec<&'a str>) -> MockOptions<'a> { MockOptions { - inputs: inputs.iter().map(|s| s.to_string()).collect::>(), + inputs: inputs.iter().map(|&s| s.to_string()).collect::>(), option_names, } } } impl<'a> CommandLineOpts for MockOptions<'a> { - fn inputs(&self) -> Vec { - self.inputs.clone() + fn inputs(&self) -> Vec<&str> { + self.inputs.iter().map(|s| s.as_str()).collect() } fn opts_present(&self, opts: &[&str]) -> bool { for expected in opts.iter() { diff --git a/src/uu/od/src/parse_nrofbytes.rs b/src/uu/od/src/parse_nrofbytes.rs index d2ba1527b..d6329c60a 100644 --- a/src/uu/od/src/parse_nrofbytes.rs +++ b/src/uu/od/src/parse_nrofbytes.rs @@ -1,14 +1,17 @@ -pub fn parse_number_of_bytes(s: &str) -> Result { +use uucore::parse_size::{parse_size, ParseSizeError}; + +pub fn parse_number_of_bytes(s: &str) -> Result { let mut start = 0; let mut len = s.len(); - let mut radix = 10; + let mut radix = 16; let mut multiply = 1; if s.starts_with("0x") || s.starts_with("0X") { start = 2; - radix = 16; } else if s.starts_with('0') { radix = 8; + } else { + return parse_size(&s[start..]); } let mut ends_with = s.chars().rev(); @@ -56,78 +59,33 @@ pub fn parse_number_of_bytes(s: &str) -> Result { Some('P') => 1000 * 1000 * 1000 * 1000 * 1000, #[cfg(target_pointer_width = "64")] Some('E') => 1000 * 1000 * 1000 * 1000 * 1000 * 1000, - _ => return Err("parse failed"), + _ => return Err(ParseSizeError::ParseFailure(s.to_string())), } } _ => {} } - match usize::from_str_radix(&s[start..len], radix) { - Ok(i) => Ok(i * multiply), - Err(_) => Err("parse failed"), - } -} - -#[allow(dead_code)] -fn parse_number_of_bytes_str(s: &str) -> Result { - parse_number_of_bytes(&String::from(s)) + let factor = match usize::from_str_radix(&s[start..len], radix) { + Ok(f) => f, + Err(e) => return Err(ParseSizeError::ParseFailure(e.to_string())), + }; + factor + .checked_mul(multiply) + .ok_or_else(|| ParseSizeError::SizeTooBig(s.to_string())) } #[test] fn test_parse_number_of_bytes() { - // normal decimal numbers - assert_eq!(0, parse_number_of_bytes_str("0").unwrap()); - assert_eq!(5, parse_number_of_bytes_str("5").unwrap()); - assert_eq!(999, parse_number_of_bytes_str("999").unwrap()); - assert_eq!(2 * 512, parse_number_of_bytes_str("2b").unwrap()); - assert_eq!(2 * 1024, parse_number_of_bytes_str("2k").unwrap()); - assert_eq!(4 * 1024, parse_number_of_bytes_str("4K").unwrap()); - assert_eq!(2 * 1048576, parse_number_of_bytes_str("2m").unwrap()); - assert_eq!(4 * 1048576, parse_number_of_bytes_str("4M").unwrap()); - assert_eq!(1073741824, parse_number_of_bytes_str("1G").unwrap()); - assert_eq!(2000, parse_number_of_bytes_str("2kB").unwrap()); - assert_eq!(4000, parse_number_of_bytes_str("4KB").unwrap()); - assert_eq!(2000000, parse_number_of_bytes_str("2mB").unwrap()); - assert_eq!(4000000, parse_number_of_bytes_str("4MB").unwrap()); - assert_eq!(2000000000, parse_number_of_bytes_str("2GB").unwrap()); - // octal input - assert_eq!(8, parse_number_of_bytes_str("010").unwrap()); - assert_eq!(8 * 512, parse_number_of_bytes_str("010b").unwrap()); - assert_eq!(8 * 1024, parse_number_of_bytes_str("010k").unwrap()); - assert_eq!(8 * 1048576, parse_number_of_bytes_str("010m").unwrap()); + assert_eq!(8, parse_number_of_bytes("010").unwrap()); + assert_eq!(8 * 512, parse_number_of_bytes("010b").unwrap()); + assert_eq!(8 * 1024, parse_number_of_bytes("010k").unwrap()); + assert_eq!(8 * 1_048_576, parse_number_of_bytes("010m").unwrap()); // hex input - assert_eq!(15, parse_number_of_bytes_str("0xf").unwrap()); - assert_eq!(15, parse_number_of_bytes_str("0XF").unwrap()); - assert_eq!(27, parse_number_of_bytes_str("0x1b").unwrap()); - assert_eq!(16 * 1024, parse_number_of_bytes_str("0x10k").unwrap()); - assert_eq!(16 * 1048576, parse_number_of_bytes_str("0x10m").unwrap()); - - // invalid input - parse_number_of_bytes_str("").unwrap_err(); - parse_number_of_bytes_str("-1").unwrap_err(); - parse_number_of_bytes_str("1e2").unwrap_err(); - parse_number_of_bytes_str("xyz").unwrap_err(); - parse_number_of_bytes_str("b").unwrap_err(); - parse_number_of_bytes_str("1Y").unwrap_err(); - parse_number_of_bytes_str("∞").unwrap_err(); -} - -#[test] -#[cfg(target_pointer_width = "64")] -fn test_parse_number_of_bytes_64bits() { - assert_eq!(1099511627776, parse_number_of_bytes_str("1T").unwrap()); - assert_eq!(1125899906842624, parse_number_of_bytes_str("1P").unwrap()); - assert_eq!( - 1152921504606846976, - parse_number_of_bytes_str("1E").unwrap() - ); - - assert_eq!(2000000000000, parse_number_of_bytes_str("2TB").unwrap()); - assert_eq!(2000000000000000, parse_number_of_bytes_str("2PB").unwrap()); - assert_eq!( - 2000000000000000000, - parse_number_of_bytes_str("2EB").unwrap() - ); + assert_eq!(15, parse_number_of_bytes("0xf").unwrap()); + assert_eq!(15, parse_number_of_bytes("0XF").unwrap()); + assert_eq!(27, parse_number_of_bytes("0x1b").unwrap()); + assert_eq!(16 * 1024, parse_number_of_bytes("0x10k").unwrap()); + assert_eq!(16 * 1_048_576, parse_number_of_bytes("0x10m").unwrap()); } diff --git a/src/uu/od/src/partialreader.rs b/src/uu/od/src/partialreader.rs index ee3588830..f155a7bd2 100644 --- a/src/uu/od/src/partialreader.rs +++ b/src/uu/od/src/partialreader.rs @@ -36,16 +36,15 @@ impl Read for PartialReader { while self.skip > 0 { let skip_count = cmp::min(self.skip, MAX_SKIP_BUFFER); - match self.inner.read(&mut bytes[..skip_count]) { - Ok(0) => { + match self.inner.read(&mut bytes[..skip_count])? { + 0 => { // this is an error as we still have more to skip return Err(io::Error::new( io::ErrorKind::UnexpectedEof, "tried to skip past end of input", )); } - Ok(n) => self.skip -= n, - Err(e) => return Err(e), + n => self.skip -= n, } } } diff --git a/src/uu/od/src/prn_char.rs b/src/uu/od/src/prn_char.rs index cbbb370ce..742229dd7 100644 --- a/src/uu/od/src/prn_char.rs +++ b/src/uu/od/src/prn_char.rs @@ -1,5 +1,3 @@ -// spell-checker:ignore (ToDO) CHRS itembytes MUTF - use std::str::from_utf8; use crate::formatteriteminfo::*; @@ -16,7 +14,7 @@ pub static FORMAT_ITEM_C: FormatterItemInfo = FormatterItemInfo { formatter: FormatWriter::MultibyteWriter(format_item_c), }; -static A_CHRS: [&str; 128] = [ +static A_CHARS: [&str; 128] = [ "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", "bs", "ht", "nl", "vt", "ff", "cr", "so", "si", "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", "can", "em", "sub", "esc", "fs", "gs", "rs", "us", "sp", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", @@ -28,12 +26,12 @@ static A_CHRS: [&str; 128] = [ ]; fn format_item_a(p: u64) -> String { - // itembytes == 1 + // item-bytes == 1 let b = (p & 0x7f) as u8; - format!("{:>4}", A_CHRS.get(b as usize).unwrap_or(&"??")) + format!("{:>4}", A_CHARS.get(b as usize).unwrap_or(&"??")) } -static C_CHRS: [&str; 128] = [ +static C_CHARS: [&str; 128] = [ "\\0", "001", "002", "003", "004", "005", "006", "\\a", "\\b", "\\t", "\\n", "\\v", "\\f", "\\r", "016", "017", "020", "021", "022", "023", "024", "025", "026", "027", "030", "031", "032", "033", "034", "035", "036", "037", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", @@ -45,11 +43,11 @@ static C_CHRS: [&str; 128] = [ ]; fn format_item_c(bytes: &[u8]) -> String { - // itembytes == 1 + // item-bytes == 1 let b = bytes[0]; if b & 0x80 == 0x00 { - match C_CHRS.get(b as usize) { + match C_CHARS.get(b as usize) { Some(s) => format!("{:>4}", s), None => format!("{:>4}", b), } @@ -86,7 +84,7 @@ pub fn format_ascii_dump(bytes: &[u8]) -> String { result.push('>'); for c in bytes.iter() { if *c >= 0x20 && *c <= 0x7e { - result.push_str(C_CHRS[*c as usize]); + result.push_str(C_CHARS[*c as usize]); } else { result.push('.'); } @@ -136,12 +134,12 @@ fn test_format_item_c() { format_item_c(&[0xf0, 0x9f, 0x92, 0x96, 0x21]) ); - assert_eq!(" 300", format_item_c(&[0xc0, 0x80])); // invalid utf-8 (MUTF-8 null) + assert_eq!(" 300", format_item_c(&[0xc0, 0x80])); // invalid utf-8 (UTF-8 null) assert_eq!(" 301", format_item_c(&[0xc1, 0xa1])); // invalid utf-8 assert_eq!(" 303", format_item_c(&[0xc3, 0xc3])); // invalid utf-8 assert_eq!(" 360", format_item_c(&[0xf0, 0x82, 0x82, 0xac])); // invalid utf-8 (overlong) assert_eq!(" 360", format_item_c(&[0xf0, 0x9f, 0x92])); // invalid utf-8 (missing octet) - assert_eq!(" \u{10FFFD}", format_item_c(&[0xf4, 0x8f, 0xbf, 0xbd])); // largest valid utf-8 + assert_eq!(" \u{10FFFD}", format_item_c(&[0xf4, 0x8f, 0xbf, 0xbd])); // largest valid utf-8 // spell-checker:ignore 10FFFD FFFD assert_eq!(" 364", format_item_c(&[0xf4, 0x90, 0x00, 0x00])); // invalid utf-8 assert_eq!(" 365", format_item_c(&[0xf5, 0x80, 0x80, 0x80])); // invalid utf-8 assert_eq!(" 377", format_item_c(&[0xff])); // invalid utf-8 diff --git a/src/uu/od/src/prn_int.rs b/src/uu/od/src/prn_int.rs index b8397b01b..0946824c1 100644 --- a/src/uu/od/src/prn_int.rs +++ b/src/uu/od/src/prn_int.rs @@ -1,5 +1,3 @@ -// spell-checker:ignore (ToDO) itembytes - use crate::formatteriteminfo::*; /// format string to print octal using `int_writer_unsigned` @@ -60,9 +58,9 @@ macro_rules! int_writer_signed { }; } -/// Extends a signed number in `item` of `itembytes` bytes into a (signed) i64 -fn sign_extend(item: u64, itembytes: usize) -> i64 { - let shift = 64 - itembytes * 8; +/// Extends a signed number in `item` of `item_bytes` bytes into a (signed) i64 +fn sign_extend(item: u64, item_bytes: usize) -> i64 { + let shift = 64 - item_bytes * 8; (item << shift) as i64 >> shift } @@ -89,46 +87,46 @@ int_writer_signed!(FORMAT_ITEM_DEC64S, 8, 21, format_item_dec_s64, DEC!()); // m #[test] fn test_sign_extend() { assert_eq!( - 0xffffffffffffff80u64 as i64, - sign_extend(0x0000000000000080, 1) + 0xffff_ffff_ffff_ff80u64 as i64, + sign_extend(0x0000_0000_0000_0080, 1) ); assert_eq!( - 0xffffffffffff8000u64 as i64, - sign_extend(0x0000000000008000, 2) + 0xffff_ffff_ffff_8000u64 as i64, + sign_extend(0x0000_0000_0000_8000, 2) ); assert_eq!( - 0xffffffffff800000u64 as i64, - sign_extend(0x0000000000800000, 3) + 0xffff_ffff_ff80_0000u64 as i64, + sign_extend(0x0000_0000_0080_0000, 3) ); assert_eq!( - 0xffffffff80000000u64 as i64, - sign_extend(0x0000000080000000, 4) + 0xffff_ffff_8000_0000u64 as i64, + sign_extend(0x0000_0000_8000_0000, 4) ); assert_eq!( - 0xffffff8000000000u64 as i64, - sign_extend(0x0000008000000000, 5) + 0xffff_ff80_0000_0000u64 as i64, + sign_extend(0x0000_0080_0000_0000, 5) ); assert_eq!( - 0xffff800000000000u64 as i64, - sign_extend(0x0000800000000000, 6) + 0xffff_8000_0000_0000u64 as i64, + sign_extend(0x0000_8000_0000_0000, 6) ); assert_eq!( - 0xff80000000000000u64 as i64, - sign_extend(0x0080000000000000, 7) + 0xff80_0000_0000_0000u64 as i64, + sign_extend(0x0080_0000_0000_0000, 7) ); assert_eq!( - 0x8000000000000000u64 as i64, - sign_extend(0x8000000000000000, 8) + 0x8000_0000_0000_0000u64 as i64, + sign_extend(0x8000_0000_0000_0000, 8) ); - assert_eq!(0x000000000000007f, sign_extend(0x000000000000007f, 1)); - assert_eq!(0x0000000000007fff, sign_extend(0x0000000000007fff, 2)); - assert_eq!(0x00000000007fffff, sign_extend(0x00000000007fffff, 3)); - assert_eq!(0x000000007fffffff, sign_extend(0x000000007fffffff, 4)); - assert_eq!(0x0000007fffffffff, sign_extend(0x0000007fffffffff, 5)); - assert_eq!(0x00007fffffffffff, sign_extend(0x00007fffffffffff, 6)); - assert_eq!(0x007fffffffffffff, sign_extend(0x007fffffffffffff, 7)); - assert_eq!(0x7fffffffffffffff, sign_extend(0x7fffffffffffffff, 8)); + assert_eq!(0x0000_0000_0000_007f, sign_extend(0x0000_0000_0000_007f, 1)); + assert_eq!(0x0000_0000_0000_7fff, sign_extend(0x0000_0000_0000_7fff, 2)); + assert_eq!(0x0000_0000_007f_ffff, sign_extend(0x0000_0000_007f_ffff, 3)); + assert_eq!(0x0000_0000_7fff_ffff, sign_extend(0x0000_0000_7fff_ffff, 4)); + assert_eq!(0x0000_007f_ffff_ffff, sign_extend(0x0000_007f_ffff_ffff, 5)); + assert_eq!(0x0000_7fff_ffff_ffff, sign_extend(0x0000_7fff_ffff_ffff, 6)); + assert_eq!(0x007f_ffff_ffff_ffff, sign_extend(0x007f_ffff_ffff_ffff, 7)); + assert_eq!(0x7fff_ffff_ffff_ffff, sign_extend(0x7fff_ffff_ffff_ffff, 8)); } #[test] @@ -138,11 +136,11 @@ fn test_format_item_oct() { assert_eq!(" 000000", format_item_oct16(0)); assert_eq!(" 177777", format_item_oct16(0xffff)); assert_eq!(" 00000000000", format_item_oct32(0)); - assert_eq!(" 37777777777", format_item_oct32(0xffffffff)); + assert_eq!(" 37777777777", format_item_oct32(0xffff_ffff)); assert_eq!(" 0000000000000000000000", format_item_oct64(0)); assert_eq!( " 1777777777777777777777", - format_item_oct64(0xffffffffffffffff) + format_item_oct64(0xffff_ffff_ffff_ffff) ); } @@ -153,9 +151,12 @@ fn test_format_item_hex() { assert_eq!(" 0000", format_item_hex16(0)); assert_eq!(" ffff", format_item_hex16(0xffff)); assert_eq!(" 00000000", format_item_hex32(0)); - assert_eq!(" ffffffff", format_item_hex32(0xffffffff)); + assert_eq!(" ffffffff", format_item_hex32(0xffff_ffff)); assert_eq!(" 0000000000000000", format_item_hex64(0)); - assert_eq!(" ffffffffffffffff", format_item_hex64(0xffffffffffffffff)); + assert_eq!( + " ffffffffffffffff", + format_item_hex64(0xffff_ffff_ffff_ffff) + ); } #[test] @@ -165,11 +166,11 @@ fn test_format_item_dec_u() { assert_eq!(" 0", format_item_dec_u16(0)); assert_eq!(" 65535", format_item_dec_u16(0xffff)); assert_eq!(" 0", format_item_dec_u32(0)); - assert_eq!(" 4294967295", format_item_dec_u32(0xffffffff)); + assert_eq!(" 4294967295", format_item_dec_u32(0xffff_ffff)); assert_eq!(" 0", format_item_dec_u64(0)); assert_eq!( " 18446744073709551615", - format_item_dec_u64(0xffffffffffffffff) + format_item_dec_u64(0xffff_ffff_ffff_ffff) ); } @@ -182,15 +183,15 @@ fn test_format_item_dec_s() { assert_eq!(" 32767", format_item_dec_s16(0x7fff)); assert_eq!(" -32768", format_item_dec_s16(0x8000)); assert_eq!(" 0", format_item_dec_s32(0)); - assert_eq!(" 2147483647", format_item_dec_s32(0x7fffffff)); - assert_eq!(" -2147483648", format_item_dec_s32(0x80000000)); + assert_eq!(" 2147483647", format_item_dec_s32(0x7fff_ffff)); + assert_eq!(" -2147483648", format_item_dec_s32(0x8000_0000)); assert_eq!(" 0", format_item_dec_s64(0)); assert_eq!( " 9223372036854775807", - format_item_dec_s64(0x7fffffffffffffff) + format_item_dec_s64(0x7fff_ffff_ffff_ffff) ); assert_eq!( " -9223372036854775808", - format_item_dec_s64(0x8000000000000000) + format_item_dec_s64(0x8000_0000_0000_0000) ); } diff --git a/src/uu/paste/Cargo.toml b/src/uu/paste/Cargo.toml index 3787ed270..4e9971368 100644 --- a/src/uu/paste/Cargo.toml +++ b/src/uu/paste/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_paste" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "paste ~ (uutils) merge lines from inputs" @@ -16,7 +16,7 @@ path = "src/paste.rs" [dependencies] clap = "2.33.3" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/paste/src/main.rs b/src/uu/paste/src/main.rs index bb9b25a68..1d4458b9e 100644 --- a/src/uu/paste/src/main.rs +++ b/src/uu/paste/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_paste); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_paste); diff --git a/src/uu/paste/src/paste.rs b/src/uu/paste/src/paste.rs index 751cc0a04..7f7969687 100644 --- a/src/uu/paste/src/paste.rs +++ b/src/uu/paste/src/paste.rs @@ -10,13 +10,12 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{stdin, BufRead, BufReader, Read}; use std::iter::repeat; use std::path::Path; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Write lines consisting of the sequentially corresponding lines from each FILE, separated by TABs, to standard output."; @@ -38,8 +37,23 @@ fn read_line( } pub fn uumain(args: impl uucore::Args) -> i32 { - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().get_matches_from(args); + + let serial = matches.is_present(options::SERIAL); + let delimiters = matches.value_of(options::DELIMITER).unwrap().to_owned(); + let files = matches + .values_of(options::FILE) + .unwrap() + .map(|s| s.to_owned()) + .collect(); + paste(files, serial, delimiters); + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) .arg( Arg::with_name(options::SERIAL) @@ -62,18 +76,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .multiple(true) .default_value("-"), ) - .get_matches_from(args); - - let serial = matches.is_present(options::SERIAL); - let delimiters = matches.value_of(options::DELIMITER).unwrap().to_owned(); - let files = matches - .values_of(options::FILE) - .unwrap() - .map(|s| s.to_owned()) - .collect(); - paste(files, serial, delimiters); - - 0 } fn paste(filenames: Vec, serial: bool, delimiters: String) { diff --git a/src/uu/pathchk/Cargo.toml b/src/uu/pathchk/Cargo.toml index 452199f4f..8c4e61d2b 100644 --- a/src/uu/pathchk/Cargo.toml +++ b/src/uu/pathchk/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_pathchk" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "pathchk ~ (uutils) diagnose invalid or non-portable PATHNAME" @@ -15,9 +15,9 @@ edition = "2018" path = "src/pathchk.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/pathchk/src/main.rs b/src/uu/pathchk/src/main.rs index e16353196..2b7c3b3ee 100644 --- a/src/uu/pathchk/src/main.rs +++ b/src/uu/pathchk/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_pathchk); // spell-checker:ignore procs uucore pathchk +uucore_procs::main!(uu_pathchk); diff --git a/src/uu/pathchk/src/pathchk.rs b/src/uu/pathchk/src/pathchk.rs index 745d0d36c..335266456 100644 --- a/src/uu/pathchk/src/pathchk.rs +++ b/src/uu/pathchk/src/pathchk.rs @@ -12,9 +12,10 @@ #[macro_use] extern crate uucore; -use getopts::Options; +use clap::{crate_version, App, Arg}; use std::fs; use std::io::{ErrorKind, Write}; +use uucore::InvalidEncodingHandling; // operating mode enum Mode { @@ -22,116 +23,107 @@ enum Mode { Basic, // check basic compatibility with POSIX Extra, // check for leading dashes and empty names Both, // a combination of `Basic` and `Extra` - Help, // show help - Version, // show version information } static NAME: &str = "pathchk"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Check whether file names are valid or portable"; + +mod options { + pub const POSIX: &str = "posix"; + pub const POSIX_SPECIAL: &str = "posix-special"; + pub const PORTABILITY: &str = "portability"; + pub const PATH: &str = "path"; +} // a few global constants as used in the GNU implementation const POSIX_PATH_MAX: usize = 256; const POSIX_NAME_MAX: usize = 14; -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); +fn get_usage() -> String { + format!("{0} [OPTION]... NAME...", executable!()) +} - // add options - let mut opts = Options::new(); - opts.optflag("p", "posix", "check for (most) POSIX systems"); - opts.optflag( - "P", - "posix-special", - "check for empty names and leading \"-\"", - ); - opts.optflag( - "", - "portability", - "check for all POSIX systems (equivalent to -p -P)", - ); - opts.optflag("h", "help", "display this help text and exit"); - opts.optflag("V", "version", "output version information and exit"); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(e) => crash!(1, "{}", e), - }; +pub fn uumain(args: impl uucore::Args) -> i32 { + let usage = get_usage(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); + + let matches = uu_app().usage(&usage[..]).get_matches_from(args); // set working mode - let mode = if matches.opt_present("version") { - Mode::Version - } else if matches.opt_present("help") { - Mode::Help - } else if (matches.opt_present("posix") && matches.opt_present("posix-special")) - || matches.opt_present("portability") - { + let is_posix = matches.values_of(options::POSIX).is_some(); + let is_posix_special = matches.values_of(options::POSIX_SPECIAL).is_some(); + let is_portability = matches.values_of(options::PORTABILITY).is_some(); + + let mode = if (is_posix && is_posix_special) || is_portability { Mode::Both - } else if matches.opt_present("posix") { + } else if is_posix { Mode::Basic - } else if matches.opt_present("posix-special") { + } else if is_posix_special { Mode::Extra } else { Mode::Default }; // take necessary actions - match mode { - Mode::Help => { - help(opts); - 0 - } - Mode::Version => { - version(); - 0 - } - _ => { - let mut res = if matches.free.is_empty() { - show_error!("missing operand\nTry {} --help for more information", NAME); - false - } else { - true - }; - // free strings are path operands - // FIXME: TCS, seems inefficient and overly verbose (?) - for p in matches.free { - let mut path = Vec::new(); - for path_segment in p.split('/') { - path.push(path_segment.to_string()); - } - res &= check_path(&mode, &path); - } - // determine error code - if res { - 0 - } else { - 1 + let paths = matches.values_of(options::PATH); + let mut res = if paths.is_none() { + show_error!("missing operand\nTry {} --help for more information", NAME); + false + } else { + true + }; + + if res { + // free strings are path operands + // FIXME: TCS, seems inefficient and overly verbose (?) + for p in paths.unwrap() { + let mut path = Vec::new(); + for path_segment in p.split('/') { + path.push(path_segment.to_string()); } + res &= check_path(&mode, &path); } } + + // determine error code + if res { + 0 + } else { + 1 + } } -// print help -fn help(opts: Options) { - let msg = format!( - "Usage: {} [OPTION]... NAME...\n\n\ - Diagnose invalid or unportable file names.", - NAME - ); - - print!("{}", opts.usage(&msg)); -} - -// print version information -fn version() { - println!("{} {}", NAME, VERSION); +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::POSIX) + .short("p") + .help("check for most POSIX systems"), + ) + .arg( + Arg::with_name(options::POSIX_SPECIAL) + .short("P") + .help(r#"check for empty names and leading "-""#), + ) + .arg( + Arg::with_name(options::PORTABILITY) + .long(options::PORTABILITY) + .help("check for all POSIX systems (equivalent to -p -P)"), + ) + .arg(Arg::with_name(options::PATH).hidden(true).multiple(true)) } // check a path, given as a slice of it's components and an operating mode fn check_path(mode: &Mode, path: &[String]) -> bool { match *mode { - Mode::Basic => check_basic(&path), - Mode::Extra => check_default(&path) && check_extra(&path), - Mode::Both => check_basic(&path) && check_extra(&path), - _ => check_default(&path), + Mode::Basic => check_basic(path), + Mode::Extra => check_default(path) && check_extra(path), + Mode::Both => check_basic(path) && check_extra(path), + _ => check_default(path), } } @@ -166,7 +158,7 @@ fn check_basic(path: &[String]) -> bool { ); return false; } - if !check_portable_chars(&p) { + if !check_portable_chars(p) { return false; } } @@ -178,7 +170,7 @@ fn check_basic(path: &[String]) -> bool { fn check_extra(path: &[String]) -> bool { // components: leading hyphens for p in path { - if !no_leading_hyphen(&p) { + if !no_leading_hyphen(p) { writeln!( &mut std::io::stderr(), "leading hyphen in file name component '{}'", @@ -251,13 +243,14 @@ fn no_leading_hyphen(path_segment: &str) -> bool { // check whether a path segment contains only valid (read: portable) characters fn check_portable_chars(path_segment: &str) -> bool { - let valid_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-".to_string(); - for ch in path_segment.chars() { - if !valid_str.contains(ch) { + const VALID_CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"; + for (i, ch) in path_segment.as_bytes().iter().enumerate() { + if !VALID_CHARS.contains(ch) { + let invalid = path_segment[i..].chars().next().unwrap(); writeln!( &mut std::io::stderr(), "nonportable character '{}' in file name component '{}'", - ch, + invalid, path_segment ); return false; diff --git a/src/uu/pinky/Cargo.toml b/src/uu/pinky/Cargo.toml index 1618eab57..a3c36259a 100644 --- a/src/uu/pinky/Cargo.toml +++ b/src/uu/pinky/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_pinky" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "pinky ~ (uutils) display user information" @@ -15,8 +15,9 @@ edition = "2018" path = "src/pinky.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["utmpx", "entries"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["utmpx", "entries"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +clap = "2.33.3" [[bin]] name = "pinky" diff --git a/src/uu/pinky/src/main.rs b/src/uu/pinky/src/main.rs index 1bf1e618a..5414c42cc 100644 --- a/src/uu/pinky/src/main.rs +++ b/src/uu/pinky/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_pinky); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_pinky); diff --git a/src/uu/pinky/src/pinky.rs b/src/uu/pinky/src/pinky.rs index 772e311d6..16bcfd3c9 100644 --- a/src/uu/pinky/src/pinky.rs +++ b/src/uu/pinky/src/pinky.rs @@ -15,69 +15,60 @@ use uucore::utmpx::{self, time, Utmpx}; use std::io::prelude::*; use std::io::BufReader; -use std::io::Result as IOResult; use std::fs::File; use std::os::unix::fs::MetadataExt; +use clap::{crate_version, App, Arg}; use std::path::PathBuf; - -static SYNTAX: &str = "[OPTION]... [USER]..."; -static SUMMARY: &str = "A lightweight 'finger' program; print user information."; +use uucore::InvalidEncodingHandling; const BUFSIZE: usize = 1024; -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); +static ABOUT: &str = "pinky - lightweight finger"; - let long_help = &format!( - " - -l produce long format output for the specified USERs - -b omit the user's home directory and shell in long format - -h omit the user's project file in long format - -p omit the user's plan file in long format - -s do short format output, this is the default - -f omit the line of column headings in short format - -w omit the user's full name in short format - -i omit the user's full name and remote host in short format - -q omit the user's full name, remote host and idle time - in short format - --help display this help and exit - --version output version information and exit +mod options { + pub const LONG_FORMAT: &str = "long_format"; + pub const OMIT_HOME_DIR: &str = "omit_home_dir"; + pub const OMIT_PROJECT_FILE: &str = "omit_project_file"; + pub const OMIT_PLAN_FILE: &str = "omit_plan_file"; + pub const SHORT_FORMAT: &str = "short_format"; + pub const OMIT_HEADINGS: &str = "omit_headings"; + pub const OMIT_NAME: &str = "omit_name"; + pub const OMIT_NAME_HOST: &str = "omit_name_host"; + pub const OMIT_NAME_HOST_TIME: &str = "omit_name_host_time"; + pub const USER: &str = "user"; +} -The utmp file will be {}", +fn get_usage() -> String { + format!("{0} [OPTION]... [USER]...", executable!()) +} + +fn get_long_usage() -> String { + format!( + "A lightweight 'finger' program; print user information.\n\ + The utmp file will be {}.", utmpx::DEFAULT_FILE - ); - let mut opts = app!(SYNTAX, SUMMARY, &long_help); - opts.optflag( - "l", - "", - "produce long format output for the specified USERs", - ); - opts.optflag( - "b", - "", - "omit the user's home directory and shell in long format", - ); - opts.optflag("h", "", "omit the user's project file in long format"); - opts.optflag("p", "", "omit the user's plan file in long format"); - opts.optflag("s", "", "do short format output, this is the default"); - opts.optflag("f", "", "omit the line of column headings in short format"); - opts.optflag("w", "", "omit the user's full name in short format"); - opts.optflag( - "i", - "", - "omit the user's full name and remote host in short format", - ); - opts.optflag( - "q", - "", - "omit the user's full name, remote host and idle time in short format", - ); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("", "version", "output version information and exit"); + ) +} - let matches = opts.parse(args); +pub fn uumain(args: impl uucore::Args) -> i32 { + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); + + let usage = get_usage(); + let after_help = get_long_usage(); + + let matches = uu_app() + .usage(&usage[..]) + .after_help(&after_help[..]) + .get_matches_from(args); + + let users: Vec = matches + .values_of(options::USER) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); // If true, display the hours:minutes since each user has touched // the keyboard, or blank if within the last minute, or days followed @@ -85,45 +76,40 @@ The utmp file will be {}", let mut include_idle = true; // If true, display a line at the top describing each field. - let include_heading = !matches.opt_present("f"); + let include_heading = !matches.is_present(options::OMIT_HEADINGS); // if true, display the user's full name from pw_gecos. let mut include_fullname = true; // if true, display the user's ~/.project file when doing long format. - let include_project = !matches.opt_present("h"); + let include_project = !matches.is_present(options::OMIT_PROJECT_FILE); // if true, display the user's ~/.plan file when doing long format. - let include_plan = !matches.opt_present("p"); + let include_plan = !matches.is_present(options::OMIT_PLAN_FILE); // if true, display the user's home directory and shell // when doing long format. - let include_home_and_shell = !matches.opt_present("b"); + let include_home_and_shell = !matches.is_present(options::OMIT_HOME_DIR); // if true, use the "short" output format. - let do_short_format = !matches.opt_present("l"); + let do_short_format = !matches.is_present(options::LONG_FORMAT); /* if true, display the ut_host field. */ let mut include_where = true; - if matches.opt_present("w") { + if matches.is_present(options::OMIT_NAME) { include_fullname = false; } - if matches.opt_present("i") { + if matches.is_present(options::OMIT_NAME_HOST) { include_fullname = false; include_where = false; } - if matches.opt_present("q") { + if matches.is_present(options::OMIT_NAME_HOST_TIME) { include_fullname = false; include_idle = false; include_where = false; } - if !do_short_format && matches.free.is_empty() { - show_usage_error!("no username specified; at least one must be specified when using -l"); - return 1; - } - let pk = Pinky { include_idle, include_heading, @@ -132,21 +118,74 @@ The utmp file will be {}", include_plan, include_home_and_shell, include_where, - names: matches.free, + names: users, }; if do_short_format { - if let Err(e) = pk.short_pinky() { - show_usage_error!("{}", e); - 1 - } else { - 0 - } + pk.short_pinky(); + 0 } else { pk.long_pinky() } } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::LONG_FORMAT) + .short("l") + .requires(options::USER) + .help("produce long format output for the specified USERs"), + ) + .arg( + Arg::with_name(options::OMIT_HOME_DIR) + .short("b") + .help("omit the user's home directory and shell in long format"), + ) + .arg( + Arg::with_name(options::OMIT_PROJECT_FILE) + .short("h") + .help("omit the user's project file in long format"), + ) + .arg( + Arg::with_name(options::OMIT_PLAN_FILE) + .short("p") + .help("omit the user's plan file in long format"), + ) + .arg( + Arg::with_name(options::SHORT_FORMAT) + .short("s") + .help("do short format output, this is the default"), + ) + .arg( + Arg::with_name(options::OMIT_HEADINGS) + .short("f") + .help("omit the line of column headings in short format"), + ) + .arg( + Arg::with_name(options::OMIT_NAME) + .short("w") + .help("omit the user's full name in short format"), + ) + .arg( + Arg::with_name(options::OMIT_NAME_HOST) + .short("i") + .help("omit the user's full name and remote host in short format"), + ) + .arg( + Arg::with_name(options::OMIT_NAME_HOST_TIME) + .short("q") + .help("omit the user's full name, remote host and idle time in short format"), + ) + .arg( + Arg::with_name(options::USER) + .takes_value(true) + .multiple(true), + ) +} + struct Pinky { include_idle: bool, include_heading: bool, @@ -199,7 +238,7 @@ fn idle_string(when: i64) -> String { } fn time_string(ut: &Utmpx) -> String { - time::strftime("%Y-%m-%d %H:%M", &ut.login_time()).unwrap() + time::strftime("%b %e %H:%M", &ut.login_time()).unwrap() // LC_ALL=C } impl Pinky { @@ -248,19 +287,12 @@ impl Pinky { } } - print!(" {}", time_string(&ut)); + print!(" {}", time_string(ut)); - if self.include_where && !ut.host().is_empty() { - let ut_host = ut.host(); - let mut res = ut_host.splitn(2, ':'); - let host = match res.next() { - Some(_) => ut.canon_host().unwrap_or_else(|_| ut_host.clone()), - None => ut_host.clone(), - }; - match res.next() { - Some(d) => print!(" {}:{}", host, d), - None => print!(" {}", host), - } + let mut s = ut.host(); + if self.include_where && !s.is_empty() { + s = safe_unwrap!(ut.canon_host()); + print!(" {}", s); } println!(); @@ -282,7 +314,7 @@ impl Pinky { println!(); } - fn short_pinky(&self) -> IOResult<()> { + fn short_pinky(&self) { if self.include_heading { self.print_heading(); } @@ -295,7 +327,6 @@ impl Pinky { } } } - Ok(()) } fn long_pinky(&self) -> i32 { diff --git a/src/uu/pr/Cargo.toml b/src/uu/pr/Cargo.toml new file mode 100644 index 000000000..de519161a --- /dev/null +++ b/src/uu/pr/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "uu_pr" +version = "0.0.6" +authors = ["uutils developers"] +license = "MIT" +description = "pr ~ (uutils) convert text files for printing" + +homepage = "https://github.com/uutils/coreutils" +repository = "https://github.com/uutils/coreutils/tree/master/src/uu/pr" +keywords = ["coreutils", "uutils", "cross-platform", "cli", "utility"] +categories = ["command-line-utilities"] +edition = "2018" + +[lib] +path = "src/pr.rs" + +[dependencies] +clap = "2.33.3" +uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["utmpx", "entries"] } +uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +getopts = "0.2.21" +time = "0.1.41" +# A higher version would cause a conflict with time +chrono = "0.4.19" +quick-error = "2.0.1" +itertools = "0.10" +regex = "1.0" + +[[bin]] +name = "pr" +path = "src/main.rs" diff --git a/src/uu/pr/src/main.rs b/src/uu/pr/src/main.rs new file mode 100644 index 000000000..893145c3e --- /dev/null +++ b/src/uu/pr/src/main.rs @@ -0,0 +1 @@ +uucore_procs::main!(uu_pr); // spell-checker:ignore procs uucore diff --git a/src/uu/pr/src/pr.rs b/src/uu/pr/src/pr.rs new file mode 100644 index 000000000..d6b9e8ca3 --- /dev/null +++ b/src/uu/pr/src/pr.rs @@ -0,0 +1,1295 @@ +#![crate_name = "uu_pr"] + +// This file is part of the uutils coreutils package. +// +// For the full copyright and license information, please view the LICENSE file +// that was distributed with this source code. +// + +// spell-checker:ignore (ToDO) adFfmprt, kmerge + +#[macro_use] +extern crate quick_error; + +use chrono::offset::Local; +use chrono::DateTime; +use getopts::Matches; +use getopts::{HasArg, Occur}; +use itertools::Itertools; +use quick_error::ResultExt; +use regex::Regex; +use std::convert::From; +use std::fs::{metadata, File}; +use std::io::{stdin, stdout, BufRead, BufReader, Lines, Read, Stdout, Write}; +#[cfg(unix)] +use std::os::unix::fs::FileTypeExt; +use uucore::executable; + +type IOError = std::io::Error; + +const NAME: &str = "pr"; +const VERSION: &str = env!("CARGO_PKG_VERSION"); +const TAB: char = '\t'; +const LINES_PER_PAGE: usize = 66; +const LINES_PER_PAGE_FOR_FORM_FEED: usize = 63; +const HEADER_LINES_PER_PAGE: usize = 5; +const TRAILER_LINES_PER_PAGE: usize = 5; +const FILE_STDIN: &str = "-"; +const READ_BUFFER_SIZE: usize = 1024 * 64; +const DEFAULT_COLUMN_WIDTH: usize = 72; +const DEFAULT_COLUMN_WIDTH_WITH_S_OPTION: usize = 512; +const DEFAULT_COLUMN_SEPARATOR: &char = &TAB; +const FF: u8 = 0x0C_u8; + +mod options { + pub const STRING_HEADER_OPTION: &str = "h"; + pub const DOUBLE_SPACE_OPTION: &str = "d"; + pub const NUMBERING_MODE_OPTION: &str = "n"; + pub const FIRST_LINE_NUMBER_OPTION: &str = "N"; + pub const PAGE_RANGE_OPTION: &str = "pages"; + pub const NO_HEADER_TRAILER_OPTION: &str = "t"; + pub const PAGE_LENGTH_OPTION: &str = "l"; + pub const SUPPRESS_PRINTING_ERROR: &str = "r"; + pub const FORM_FEED_OPTION: &str = "F"; + pub const FORM_FEED_OPTION_SMALL: &str = "f"; + pub const COLUMN_WIDTH_OPTION: &str = "w"; + pub const PAGE_WIDTH_OPTION: &str = "W"; + pub const ACROSS_OPTION: &str = "a"; + pub const COLUMN_OPTION: &str = "column"; + pub const COLUMN_CHAR_SEPARATOR_OPTION: &str = "s"; + pub const COLUMN_STRING_SEPARATOR_OPTION: &str = "S"; + pub const MERGE_FILES_PRINT: &str = "m"; + pub const OFFSET_SPACES_OPTION: &str = "o"; + pub const JOIN_LINES_OPTION: &str = "J"; +} + +struct OutputOptions { + /// Line numbering mode + number: Option, + header: String, + double_space: bool, + line_separator: String, + content_line_separator: String, + last_modified_time: String, + start_page: usize, + end_page: Option, + display_header_and_trailer: bool, + content_lines_per_page: usize, + page_separator_char: String, + column_mode_options: Option, + merge_files_print: Option, + offset_spaces: String, + form_feed_used: bool, + join_lines: bool, + col_sep_for_printing: String, + line_width: Option, +} + +struct FileLine { + file_id: usize, + line_number: usize, + page_number: usize, + group_key: usize, + line_content: Result, + form_feeds_after: usize, +} + +struct ColumnModeOptions { + width: usize, + columns: usize, + column_separator: String, + across_mode: bool, +} + +/// Line numbering mode +struct NumberingMode { + width: usize, + separator: String, + first_number: usize, +} + +impl Default for NumberingMode { + fn default() -> NumberingMode { + NumberingMode { + width: 5, + separator: TAB.to_string(), + first_number: 1, + } + } +} + +impl Default for FileLine { + fn default() -> FileLine { + FileLine { + file_id: 0, + line_number: 0, + page_number: 0, + group_key: 0, + line_content: Ok(String::new()), + form_feeds_after: 0, + } + } +} + +impl From for PrError { + fn from(err: IOError) -> Self { + PrError::EncounteredErrors(err.to_string()) + } +} + +quick_error! { + #[derive(Debug)] + enum PrError { + Input(err: IOError, path: String) { + context(path: &'a str, err: IOError) -> (err, path.to_owned()) + display("pr: Reading from input {0} gave error", path) + source(err) + } + + UnknownFiletype(path: String) { + display("pr: {0}: unknown filetype", path) + } + + EncounteredErrors(msg: String) { + display("pr: {0}", msg) + } + + IsDirectory(path: String) { + display("pr: {0}: Is a directory", path) + } + + IsSocket(path: String) { + display("pr: cannot open {}, Operation not supported on socket", path) + } + + NotExists(path: String) { + display("pr: cannot open {}, No such file or directory", path) + } + } +} + +pub fn uu_app() -> clap::App<'static, 'static> { + // TODO: migrate to clap to get more shell completions + clap::App::new(executable!()) +} + +pub fn uumain(args: impl uucore::Args) -> i32 { + let args = args + .collect_str(uucore::InvalidEncodingHandling::Ignore) + .accept_any(); + let mut opts = getopts::Options::new(); + + opts.opt( + "", + options::PAGE_RANGE_OPTION, + "Begin and stop printing with page FIRST_PAGE[:LAST_PAGE]", + "FIRST_PAGE[:LAST_PAGE]", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::STRING_HEADER_OPTION, + "header", + "Use the string header to replace the file name \ + in the header line.", + "STRING", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::DOUBLE_SPACE_OPTION, + "double-space", + "Produce output that is double spaced. An extra character is output following every + found in the input.", + "", + HasArg::No, + Occur::Optional, + ); + + opts.opt( + options::NUMBERING_MODE_OPTION, + "number-lines", + "Provide width digit line numbering. The default for width, if not specified, is 5. The number occupies + the first width column positions of each text column or each line of -m output. If char (any non-digit + character) is given, it is appended to the line number to separate it from whatever follows. The default + for char is a . Line numbers longer than width columns are truncated.", + "[char][width]", + HasArg::Maybe, + Occur::Optional, + ); + + opts.opt( + options::FIRST_LINE_NUMBER_OPTION, + "first-line-number", + "start counting with NUMBER at 1st line of first page printed", + "NUMBER", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::NO_HEADER_TRAILER_OPTION, + "omit-header", + "Write neither the five-line identifying header nor the five-line trailer usually supplied for each page. Quit + writing after the last line of each file without spacing to the end of the page.", + "", + HasArg::No, + Occur::Optional, + ); + + opts.opt( + options::PAGE_LENGTH_OPTION, + "length", + "Override the 66-line default (default number of lines of text 56, and with -F 63) and reset the page length to lines. If lines is not greater than the sum of both + the header and trailer depths (in lines), the pr utility shall suppress both the header and trailer, as if the + -t option were in effect. ", + "lines", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::SUPPRESS_PRINTING_ERROR, + "no-file-warnings", + "omit warning when a file cannot be opened", + "", + HasArg::No, + Occur::Optional, + ); + + opts.opt( + options::FORM_FEED_OPTION, + "form-feed", + "Use a for new pages, instead of the default behavior that uses a sequence of s.", + "", + HasArg::No, + Occur::Optional, + ); + opts.opt( + options::FORM_FEED_OPTION_SMALL, + "form-feed", + "Same as -F but pause before beginning the first page if standard output is a + terminal.", + "", + HasArg::No, + Occur::Optional, + ); + + opts.opt( + "", + options::COLUMN_OPTION, + "Produce multi-column output that is arranged in column columns (the default shall be 1) and is written down each + column in the order in which the text is received from the input file. This option should not be used with -m. + The options -e and -i shall be assumed for multiple text-column output. Whether or not text columns are produced + with identical vertical lengths is unspecified, but a text column shall never exceed the length of the + page (see the -l option). When used with -t, use the minimum number of lines to write the output.", + "[column]", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::COLUMN_WIDTH_OPTION, + "width", + "Set the width of the line to width column positions for multiple text-column output only. If the -w option is + not specified and the -s option is not specified, the default width shall be 72. If the -w option is not specified + and the -s option is specified, the default width shall be 512.", + "[width]", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::PAGE_WIDTH_OPTION, + "page-width", + "set page width to PAGE_WIDTH (72) characters always, + truncate lines, except -J option is set, no interference + with -S or -s", + "[width]", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::ACROSS_OPTION, + "across", + "Modify the effect of the - column option so that the columns are filled across the page in a round-robin order + (for example, when column is 2, the first input line heads column 1, the second heads column 2, the third is the + second line in column 1, and so on).", + "", + HasArg::No, + Occur::Optional, + ); + + opts.opt( + options::COLUMN_CHAR_SEPARATOR_OPTION, + "separator", + "Separate text columns by the single character char instead of by the appropriate number of s + (default for char is the character).", + "char", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::COLUMN_STRING_SEPARATOR_OPTION, + "sep-string", + "separate columns by STRING, + without -S: Default separator with -J and + otherwise (same as -S\" \"), no effect on column options", + "string", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::MERGE_FILES_PRINT, + "merge", + "Merge files. Standard output shall be formatted so the pr utility writes one line from each file specified by a + file operand, side by side into text columns of equal fixed widths, in terms of the number of column positions. + Implementations shall support merging of at least nine file operands.", + "", + HasArg::No, + Occur::Optional, + ); + + opts.opt( + options::OFFSET_SPACES_OPTION, + "indent", + "Each line of output shall be preceded by offset s. If the -o option is not specified, the default offset + shall be zero. The space taken is in addition to the output line width (see the -w option below).", + "offset", + HasArg::Yes, + Occur::Optional, + ); + + opts.opt( + options::JOIN_LINES_OPTION, + "join-lines", + "merge full lines, turns off -W line truncation, no column + alignment, --sep-string[=STRING] sets separators", + "offset", + HasArg::No, + Occur::Optional, + ); + + opts.optflag("", "help", "display this help and exit"); + opts.optflag("V", "version", "output version information and exit"); + + let opt_args = recreate_arguments(&args); + + let matches = match opts.parse(&opt_args[1..]) { + Ok(m) => m, + Err(e) => panic!("Invalid options\n{}", e), + }; + + if matches.opt_present("version") { + println!("{} {}", NAME, VERSION); + return 0; + } + + let mut files = matches.free.clone(); + if files.is_empty() { + files.insert(0, FILE_STDIN.to_owned()); + } + + if matches.opt_present("help") { + return print_usage(&mut opts, &matches); + } + + let file_groups: Vec<_> = if matches.opt_present(options::MERGE_FILES_PRINT) { + vec![files] + } else { + files.into_iter().map(|i| vec![i]).collect() + }; + + for file_group in file_groups { + let result_options = build_options(&matches, &file_group, args.join(" ")); + let options = match result_options { + Ok(options) => options, + Err(err) => { + print_error(&matches, err); + return 1; + } + }; + + let cmd_result = if let Ok(group) = file_group.iter().exactly_one() { + pr(group, &options) + } else { + mpr(&file_group, &options) + }; + + let status = match cmd_result { + Err(error) => { + print_error(&matches, error); + 1 + } + _ => 0, + }; + if status != 0 { + return status; + } + } + + 0 +} + +/// Returns re-written arguments which are passed to the program. +/// Removes -column and +page option as getopts cannot parse things like -3 etc +/// # Arguments +/// * `args` - Command line arguments +fn recreate_arguments(args: &[String]) -> Vec { + let column_page_option = Regex::new(r"^[-+]\d+.*").unwrap(); + let num_regex = Regex::new(r"(.\d+)|(\d+)|^[^-]$").unwrap(); + //let a_file: Regex = Regex::new(r"^[^-+].*").unwrap(); + let n_regex = Regex::new(r"^-n\s*$").unwrap(); + let mut arguments = args.to_owned(); + let num_option = args.iter().find_position(|x| n_regex.is_match(x.trim())); + if let Some((pos, _value)) = num_option { + if let Some(num_val_opt) = args.get(pos + 1) { + if !num_regex.is_match(num_val_opt) { + let could_be_file = arguments.remove(pos + 1); + arguments.insert(pos + 1, format!("{}", NumberingMode::default().width)); + arguments.insert(pos + 2, could_be_file); + } + } + } + + arguments + .into_iter() + .filter(|i| !column_page_option.is_match(i)) + .collect() +} + +fn print_error(matches: &Matches, err: PrError) { + if !matches.opt_present(options::SUPPRESS_PRINTING_ERROR) { + eprintln!("{}", err); + } +} + +fn print_usage(opts: &mut getopts::Options, matches: &Matches) -> i32 { + println!("{} {} -- print files", NAME, VERSION); + println!(); + println!( + "Usage: {} [+page] [-column] [-adFfmprt] [[-e] [char] [gap]] + [-L locale] [-h header] [[-i] [char] [gap]] + [-l lines] [-o offset] [[-s] [char]] [[-n] [char] + [width]] [-w width] [-] [file ...].", + NAME + ); + println!(); + let usage: &str = "The pr utility is a printing and pagination filter + for text files. When multiple input files are specified, + each is read, formatted, and written to standard + output. By default, the input is separated + into 66-line pages, each with + + o A 5-line header with the page number, date, + time, and the pathname of the file. + + o A 5-line trailer consisting of blank lines. + + If standard output is associated with a terminal, + diagnostic messages are suppressed until the pr + utility has completed processing. + + When multiple column output is specified, text columns + are of equal width. By default text columns + are separated by at least one . Input lines + that do not fit into a text column are truncated. + Lines are not truncated under single column output."; + println!("{}", opts.usage(usage)); + println!(" +page \t\tBegin output at page number page of the formatted input."); + println!( + " -column \t\tProduce multi-column output. Refer --{}", + options::COLUMN_OPTION + ); + if matches.free.is_empty() { + return 1; + } + + 0 +} + +fn parse_usize(matches: &Matches, opt: &str) -> Option> { + let from_parse_error_to_pr_error = |value_to_parse: (String, String)| { + let i = value_to_parse.0; + let option = value_to_parse.1; + i.parse().map_err(|_e| { + PrError::EncounteredErrors(format!("invalid {} argument '{}'", option, i)) + }) + }; + matches + .opt_str(opt) + .map(|i| (i, format!("-{}", opt))) + .map(from_parse_error_to_pr_error) +} + +fn build_options( + matches: &Matches, + paths: &[String], + free_args: String, +) -> Result { + let form_feed_used = matches.opt_present(options::FORM_FEED_OPTION) + || matches.opt_present(options::FORM_FEED_OPTION_SMALL); + + let is_merge_mode = matches.opt_present(options::MERGE_FILES_PRINT); + + if is_merge_mode && matches.opt_present(options::COLUMN_OPTION) { + let err_msg = String::from("cannot specify number of columns when printing in parallel"); + return Err(PrError::EncounteredErrors(err_msg)); + } + + if is_merge_mode && matches.opt_present(options::ACROSS_OPTION) { + let err_msg = String::from("cannot specify both printing across and printing in parallel"); + return Err(PrError::EncounteredErrors(err_msg)); + } + + let merge_files_print = if matches.opt_present(options::MERGE_FILES_PRINT) { + Some(paths.len()) + } else { + None + }; + + let header = matches.opt_str(options::STRING_HEADER_OPTION).unwrap_or( + if is_merge_mode || paths[0] == FILE_STDIN { + String::new() + } else { + paths[0].to_string() + }, + ); + + let default_first_number = NumberingMode::default().first_number; + let first_number = parse_usize(matches, options::FIRST_LINE_NUMBER_OPTION) + .unwrap_or(Ok(default_first_number))?; + + let number = matches + .opt_str(options::NUMBERING_MODE_OPTION) + .map(|i| { + let parse_result = i.parse::(); + + let separator = if parse_result.is_err() { + i[0..1].to_string() + } else { + NumberingMode::default().separator + }; + + let width = match parse_result { + Ok(res) => res, + Err(_) => i[1..] + .parse::() + .unwrap_or(NumberingMode::default().width), + }; + + NumberingMode { + width, + separator, + first_number, + } + }) + .or_else(|| { + if matches.opt_present(options::NUMBERING_MODE_OPTION) { + Some(NumberingMode::default()) + } else { + None + } + }); + + let double_space = matches.opt_present(options::DOUBLE_SPACE_OPTION); + + let content_line_separator = if double_space { + "\n".repeat(2) + } else { + "\n".to_string() + }; + + let line_separator = "\n".to_string(); + + let last_modified_time = if is_merge_mode || paths[0].eq(FILE_STDIN) { + let date_time = Local::now(); + date_time.format("%b %d %H:%M %Y").to_string() + } else { + file_last_modified_time(paths.get(0).unwrap()) + }; + + // +page option is less priority than --pages + let page_plus_re = Regex::new(r"\s*\+(\d+:*\d*)\s*").unwrap(); + let start_page_in_plus_option = match page_plus_re.captures(&free_args).map(|i| { + let unparsed_num = i.get(1).unwrap().as_str().trim(); + let x: Vec<_> = unparsed_num.split(':').collect(); + x[0].to_string().parse::().map_err(|_e| { + PrError::EncounteredErrors(format!("invalid {} argument '{}'", "+", unparsed_num)) + }) + }) { + Some(res) => res?, + None => 1, + }; + + let end_page_in_plus_option = match page_plus_re + .captures(&free_args) + .map(|i| i.get(1).unwrap().as_str().trim()) + .filter(|i| i.contains(':')) + .map(|unparsed_num| { + let x: Vec<_> = unparsed_num.split(':').collect(); + x[1].to_string().parse::().map_err(|_e| { + PrError::EncounteredErrors(format!("invalid {} argument '{}'", "+", unparsed_num)) + }) + }) { + Some(res) => Some(res?), + None => None, + }; + + let invalid_pages_map = |i: String| { + let unparsed_value = matches.opt_str(options::PAGE_RANGE_OPTION).unwrap(); + i.parse::().map_err(|_e| { + PrError::EncounteredErrors(format!("invalid --pages argument '{}'", unparsed_value)) + }) + }; + + let start_page = match matches + .opt_str(options::PAGE_RANGE_OPTION) + .map(|i| { + let x: Vec<_> = i.split(':').collect(); + x[0].to_string() + }) + .map(invalid_pages_map) + { + Some(res) => res?, + None => start_page_in_plus_option, + }; + + let end_page = match matches + .opt_str(options::PAGE_RANGE_OPTION) + .filter(|i| i.contains(':')) + .map(|i| { + let x: Vec<_> = i.split(':').collect(); + x[1].to_string() + }) + .map(invalid_pages_map) + { + Some(res) => Some(res?), + None => end_page_in_plus_option, + }; + + if let Some(end_page) = end_page { + if start_page > end_page { + return Err(PrError::EncounteredErrors(format!( + "invalid --pages argument '{}:{}'", + start_page, end_page + ))); + } + } + + let default_lines_per_page = if form_feed_used { + LINES_PER_PAGE_FOR_FORM_FEED + } else { + LINES_PER_PAGE + }; + + let page_length = + parse_usize(matches, options::PAGE_LENGTH_OPTION).unwrap_or(Ok(default_lines_per_page))?; + + let page_length_le_ht = page_length < (HEADER_LINES_PER_PAGE + TRAILER_LINES_PER_PAGE); + + let display_header_and_trailer = + !(page_length_le_ht) && !matches.opt_present(options::NO_HEADER_TRAILER_OPTION); + + let content_lines_per_page = if page_length_le_ht { + page_length + } else { + page_length - (HEADER_LINES_PER_PAGE + TRAILER_LINES_PER_PAGE) + }; + + let page_separator_char = if matches.opt_present(options::FORM_FEED_OPTION) { + let bytes = vec![FF]; + String::from_utf8(bytes).unwrap() + } else { + "\n".to_string() + }; + + let across_mode = matches.opt_present(options::ACROSS_OPTION); + + let column_separator = match matches.opt_str(options::COLUMN_STRING_SEPARATOR_OPTION) { + Some(x) => Some(x), + None => matches.opt_str(options::COLUMN_CHAR_SEPARATOR_OPTION), + } + .unwrap_or_else(|| DEFAULT_COLUMN_SEPARATOR.to_string()); + + let default_column_width = if matches.opt_present(options::COLUMN_WIDTH_OPTION) + && matches.opt_present(options::COLUMN_CHAR_SEPARATOR_OPTION) + { + DEFAULT_COLUMN_WIDTH_WITH_S_OPTION + } else { + DEFAULT_COLUMN_WIDTH + }; + + let column_width = + parse_usize(matches, options::COLUMN_WIDTH_OPTION).unwrap_or(Ok(default_column_width))?; + + let page_width = if matches.opt_present(options::JOIN_LINES_OPTION) { + None + } else { + match parse_usize(matches, options::PAGE_WIDTH_OPTION) { + Some(res) => Some(res?), + None => None, + } + }; + + let re_col = Regex::new(r"\s*-(\d+)\s*").unwrap(); + + let start_column_option = match re_col.captures(&free_args).map(|i| { + let unparsed_num = i.get(1).unwrap().as_str().trim(); + unparsed_num.parse::().map_err(|_e| { + PrError::EncounteredErrors(format!("invalid {} argument '{}'", "-", unparsed_num)) + }) + }) { + Some(res) => Some(res?), + None => None, + }; + + // --column has more priority than -column + + let column_option_value = match parse_usize(matches, options::COLUMN_OPTION) { + Some(res) => Some(res?), + None => start_column_option, + }; + + let column_mode_options = column_option_value.map(|columns| ColumnModeOptions { + columns, + width: column_width, + column_separator, + across_mode, + }); + + let offset_spaces = + " ".repeat(parse_usize(matches, options::OFFSET_SPACES_OPTION).unwrap_or(Ok(0))?); + let join_lines = matches.opt_present(options::JOIN_LINES_OPTION); + + let col_sep_for_printing = column_mode_options + .as_ref() + .map(|i| i.column_separator.clone()) + .unwrap_or_else(|| { + merge_files_print + .map(|_k| DEFAULT_COLUMN_SEPARATOR.to_string()) + .unwrap_or_default() + }); + + let columns_to_print = merge_files_print + .unwrap_or_else(|| column_mode_options.as_ref().map(|i| i.columns).unwrap_or(1)); + + let line_width = if join_lines { + None + } else if columns_to_print > 1 { + Some( + column_mode_options + .as_ref() + .map(|i| i.width) + .unwrap_or(DEFAULT_COLUMN_WIDTH), + ) + } else { + page_width + }; + + Ok(OutputOptions { + number, + header, + double_space, + line_separator, + content_line_separator, + last_modified_time, + start_page, + end_page, + display_header_and_trailer, + content_lines_per_page, + page_separator_char, + column_mode_options, + merge_files_print, + offset_spaces, + form_feed_used, + join_lines, + col_sep_for_printing, + line_width, + }) +} + +fn open(path: &str) -> Result, PrError> { + if path == FILE_STDIN { + let stdin = stdin(); + return Ok(Box::new(stdin) as Box); + } + + metadata(path) + .map(|i| { + let path_string = path.to_string(); + match i.file_type() { + #[cfg(unix)] + ft if ft.is_block_device() => Err(PrError::UnknownFiletype(path_string)), + #[cfg(unix)] + ft if ft.is_char_device() => Err(PrError::UnknownFiletype(path_string)), + #[cfg(unix)] + ft if ft.is_fifo() => Err(PrError::UnknownFiletype(path_string)), + #[cfg(unix)] + ft if ft.is_socket() => Err(PrError::IsSocket(path_string)), + ft if ft.is_dir() => Err(PrError::IsDirectory(path_string)), + ft if ft.is_file() || ft.is_symlink() => { + Ok(Box::new(File::open(path).context(path)?) as Box) + } + _ => Err(PrError::UnknownFiletype(path_string)), + } + }) + .unwrap_or_else(|_| Err(PrError::NotExists(path.to_string()))) +} + +fn split_lines_if_form_feed(file_content: Result) -> Vec { + file_content + .map(|content| { + let mut lines = Vec::new(); + let mut f_occurred = 0; + let mut chunk = Vec::new(); + for byte in content.as_bytes() { + if byte == &FF { + f_occurred += 1; + } else { + if f_occurred != 0 { + // First time byte occurred in the scan + lines.push(FileLine { + line_content: Ok(String::from_utf8(chunk.clone()).unwrap()), + form_feeds_after: f_occurred, + ..FileLine::default() + }); + chunk.clear(); + } + chunk.push(*byte); + f_occurred = 0; + } + } + + lines.push(FileLine { + line_content: Ok(String::from_utf8(chunk).unwrap()), + form_feeds_after: f_occurred, + ..FileLine::default() + }); + + lines + }) + .unwrap_or_else(|e| { + vec![FileLine { + line_content: Err(e), + ..FileLine::default() + }] + }) +} + +fn pr(path: &str, options: &OutputOptions) -> Result { + let lines = BufReader::with_capacity(READ_BUFFER_SIZE, open(path)?).lines(); + + let pages = read_stream_and_create_pages(options, lines, 0); + + for page_with_page_number in pages { + let page_number = page_with_page_number.0 + 1; + let page = page_with_page_number.1; + print_page(&page, options, page_number)?; + } + + Ok(0) +} + +fn read_stream_and_create_pages( + options: &OutputOptions, + lines: Lines>>, + file_id: usize, +) -> Box)>> { + let start_page = options.start_page; + let start_line_number = get_start_line_number(options); + let last_page = options.end_page; + let lines_needed_per_page = lines_to_read_for_page(options); + + Box::new( + lines + .map(split_lines_if_form_feed) + .flatten() + .enumerate() + .map(move |(i, line)| FileLine { + line_number: i + start_line_number, + file_id, + ..line + }) // Add line number and file_id + .batching(move |it| { + let mut first_page = Vec::new(); + let mut page_with_lines = Vec::new(); + for line in it { + let form_feeds_after = line.form_feeds_after; + first_page.push(line); + + if form_feeds_after > 1 { + // insert empty pages + page_with_lines.push(first_page); + for _i in 1..form_feeds_after { + page_with_lines.push(vec![]); + } + return Some(page_with_lines); + } + + if first_page.len() == lines_needed_per_page || form_feeds_after == 1 { + break; + } + } + + if first_page.is_empty() { + return None; + } + page_with_lines.push(first_page); + Some(page_with_lines) + }) // Create set of pages as form feeds could lead to empty pages + .flatten() // Flatten to pages from page sets + .enumerate() // Assign page number + .skip_while(move |(x, _)| { + // Skip the not needed pages + let current_page = x + 1; + current_page < start_page + }) + .take_while(move |(x, _)| { + // Take only the required pages + let current_page = x + 1; + + current_page >= start_page + && last_page.map_or(true, |last_page| current_page <= last_page) + }), + ) +} + +fn mpr(paths: &[String], options: &OutputOptions) -> Result { + let n_files = paths.len(); + + // Check if files exists + for path in paths { + open(path)?; + } + + let file_line_groups = paths + .iter() + .enumerate() + .map(|(i, path)| { + let lines = BufReader::with_capacity(READ_BUFFER_SIZE, open(path).unwrap()).lines(); + + read_stream_and_create_pages(options, lines, i) + .map(move |(x, line)| { + let file_line = line; + let page_number = x + 1; + file_line + .into_iter() + .map(|fl| FileLine { + page_number, + group_key: page_number * n_files + fl.file_id, + ..fl + }) + .collect::>() + }) + .flatten() + }) + .kmerge_by(|a, b| { + if a.group_key == b.group_key { + a.line_number < b.line_number + } else { + a.group_key < b.group_key + } + }) + .group_by(|file_line| file_line.group_key); + + let start_page = options.start_page; + let mut lines = Vec::new(); + let mut page_counter = start_page; + + for (_key, file_line_group) in file_line_groups.into_iter() { + for file_line in file_line_group { + if let Err(e) = file_line.line_content { + return Err(e.into()); + } + let new_page_number = file_line.page_number; + if page_counter != new_page_number { + print_page(&lines, options, page_counter)?; + lines = Vec::new(); + page_counter = new_page_number; + } + lines.push(file_line); + } + } + + print_page(&lines, options, page_counter)?; + + Ok(0) +} + +fn print_page(lines: &[FileLine], options: &OutputOptions, page: usize) -> Result { + let line_separator = options.line_separator.as_bytes(); + let page_separator = options.page_separator_char.as_bytes(); + + let header = header_content(options, page); + let trailer_content = trailer_content(options); + let out = &mut stdout(); + + out.lock(); + for x in header { + out.write_all(x.as_bytes())?; + out.write_all(line_separator)?; + } + + let lines_written = write_columns(lines, options, out)?; + + for (index, x) in trailer_content.iter().enumerate() { + out.write_all(x.as_bytes())?; + if index + 1 != trailer_content.len() { + out.write_all(line_separator)?; + } + } + out.write_all(page_separator)?; + out.flush()?; + Ok(lines_written) +} + +fn write_columns( + lines: &[FileLine], + options: &OutputOptions, + out: &mut Stdout, +) -> Result { + let line_separator = options.content_line_separator.as_bytes(); + + let content_lines_per_page = if options.double_space { + options.content_lines_per_page / 2 + } else { + options.content_lines_per_page + }; + + let columns = options + .merge_files_print + .unwrap_or_else(|| get_columns(options)); + let line_width = options.line_width; + let mut lines_printed = 0; + let feed_line_present = options.form_feed_used; + let mut not_found_break = false; + + let across_mode = options + .column_mode_options + .as_ref() + .map(|i| i.across_mode) + .unwrap_or(false); + + let mut filled_lines = Vec::new(); + if options.merge_files_print.is_some() { + let mut offset = 0; + for col in 0..columns { + let mut inserted = 0; + for line in &lines[offset..] { + if line.file_id != col { + break; + } + filled_lines.push(Some(line)); + inserted += 1; + } + offset += inserted; + + for _i in inserted..content_lines_per_page { + filled_lines.push(None); + } + } + } + + let table: Vec> = (0..content_lines_per_page) + .map(move |a| { + (0..columns) + .map(|i| { + if across_mode { + lines.get(a * columns + i) + } else if options.merge_files_print.is_some() { + *filled_lines + .get(content_lines_per_page * i + a) + .unwrap_or(&None) + } else { + lines.get(content_lines_per_page * i + a) + } + }) + .collect() + }) + .collect(); + + let blank_line = FileLine::default(); + for row in table { + let indexes = row.len(); + for (i, cell) in row.iter().enumerate() { + if cell.is_none() && options.merge_files_print.is_some() { + out.write_all( + get_line_for_printing(options, &blank_line, columns, i, &line_width, indexes) + .as_bytes(), + )?; + } else if cell.is_none() { + not_found_break = true; + break; + } else if cell.is_some() { + let file_line = cell.unwrap(); + + out.write_all( + get_line_for_printing(options, file_line, columns, i, &line_width, indexes) + .as_bytes(), + )?; + lines_printed += 1; + } + } + if not_found_break && feed_line_present { + break; + } else { + out.write_all(line_separator)?; + } + } + + Ok(lines_printed) +} + +fn get_line_for_printing( + options: &OutputOptions, + file_line: &FileLine, + columns: usize, + index: usize, + line_width: &Option, + indexes: usize, +) -> String { + let blank_line = String::new(); + let formatted_line_number = get_formatted_line_number(options, file_line.line_number, index); + + let mut complete_line = format!( + "{}{}", + formatted_line_number, + file_line.line_content.as_ref().unwrap() + ); + + let offset_spaces = &options.offset_spaces; + + let tab_count = complete_line.chars().filter(|i| i == &TAB).count(); + + let display_length = complete_line.len() + (tab_count * 7); + + let sep = if (index + 1) != indexes && !options.join_lines { + &options.col_sep_for_printing + } else { + &blank_line + }; + + format!( + "{}{}{}", + offset_spaces, + line_width + .map(|i| { + let min_width = (i - (columns - 1)) / columns; + if display_length < min_width { + for _i in 0..(min_width - display_length) { + complete_line.push(' '); + } + } + + complete_line.chars().take(min_width).collect() + }) + .unwrap_or(complete_line), + sep + ) +} + +fn get_formatted_line_number(opts: &OutputOptions, line_number: usize, index: usize) -> String { + let should_show_line_number = + opts.number.is_some() && (opts.merge_files_print.is_none() || index == 0); + if should_show_line_number && line_number != 0 { + let line_str = line_number.to_string(); + let num_opt = opts.number.as_ref().unwrap(); + let width = num_opt.width; + let separator = &num_opt.separator; + if line_str.len() >= width { + format!( + "{:>width$}{}", + &line_str[line_str.len() - width..], + separator, + width = width + ) + } else { + format!("{:>width$}{}", line_str, separator, width = width) + } + } else { + String::new() + } +} + +/// Returns a five line header content if displaying header is not disabled by +/// using `NO_HEADER_TRAILER_OPTION` option. +fn header_content(options: &OutputOptions, page: usize) -> Vec { + if options.display_header_and_trailer { + let first_line = format!( + "{} {} Page {}", + options.last_modified_time, options.header, page + ); + vec![ + String::new(), + String::new(), + first_line, + String::new(), + String::new(), + ] + } else { + Vec::new() + } +} + +fn file_last_modified_time(path: &str) -> String { + metadata(path) + .map(|i| { + i.modified() + .map(|x| { + let date_time: DateTime = x.into(); + date_time.format("%b %d %H:%M %Y").to_string() + }) + .unwrap_or_default() + }) + .unwrap_or_default() +} + +/// Returns five empty lines as trailer content if displaying trailer +/// is not disabled by using `NO_HEADER_TRAILER_OPTION`option. +fn trailer_content(options: &OutputOptions) -> Vec { + if options.display_header_and_trailer && !options.form_feed_used { + vec![ + String::new(), + String::new(), + String::new(), + String::new(), + String::new(), + ] + } else { + Vec::new() + } +} + +/// Returns starting line number for the file to be printed. +/// If -N is specified the first line number changes otherwise +/// default is 1. +fn get_start_line_number(opts: &OutputOptions) -> usize { + opts.number.as_ref().map(|i| i.first_number).unwrap_or(1) +} + +/// Returns number of lines to read from input for constructing one page of pr output. +/// If double space -d is used lines are halved. +/// If columns --columns is used the lines are multiplied by the value. +fn lines_to_read_for_page(opts: &OutputOptions) -> usize { + let content_lines_per_page = opts.content_lines_per_page; + let columns = get_columns(opts); + if opts.double_space { + (content_lines_per_page / 2) * columns + } else { + content_lines_per_page * columns + } +} + +/// Returns number of columns to output +fn get_columns(opts: &OutputOptions) -> usize { + opts.column_mode_options + .as_ref() + .map(|i| i.columns) + .unwrap_or(1) +} diff --git a/src/uu/printenv/Cargo.toml b/src/uu/printenv/Cargo.toml index 5266c1e03..be95b8157 100644 --- a/src/uu/printenv/Cargo.toml +++ b/src/uu/printenv/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_printenv" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "printenv ~ (uutils) display value of environment VAR" @@ -16,7 +16,7 @@ path = "src/printenv.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/printenv/src/main.rs b/src/uu/printenv/src/main.rs index 328c3b485..b61cbe90a 100644 --- a/src/uu/printenv/src/main.rs +++ b/src/uu/printenv/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_printenv); // spell-checker:ignore procs uucore printenv +uucore_procs::main!(uu_printenv); diff --git a/src/uu/printenv/src/printenv.rs b/src/uu/printenv/src/printenv.rs index 34571ddad..6e0ca7157 100644 --- a/src/uu/printenv/src/printenv.rs +++ b/src/uu/printenv/src/printenv.rs @@ -10,11 +10,10 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::env; static ABOUT: &str = "Display the values of the specified environment VARIABLE(s), or (with no VARIABLE) display name and value pairs for them all."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static OPT_NULL: &str = "null"; @@ -27,33 +26,18 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(OPT_NULL) - .short("0") - .long(OPT_NULL) - .help("end each output line with 0 byte rather than newline"), - ) - .arg( - Arg::with_name(ARG_VARIABLES) - .multiple(true) - .takes_value(true) - .min_values(1), - ) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); let variables: Vec = matches .values_of(ARG_VARIABLES) .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); - let mut separator = "\n"; - if matches.is_present(OPT_NULL) { - separator = "\x00"; - } + let separator = if matches.is_present(OPT_NULL) { + "\x00" + } else { + "\n" + }; if variables.is_empty() { for (env_var, value) in env::vars() { @@ -69,3 +53,21 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(OPT_NULL) + .short("0") + .long(OPT_NULL) + .help("end each output line with 0 byte rather than newline"), + ) + .arg( + Arg::with_name(ARG_VARIABLES) + .multiple(true) + .takes_value(true) + .min_values(1), + ) +} diff --git a/src/uu/printf/Cargo.toml b/src/uu/printf/Cargo.toml index c6737d178..13d54fcca 100644 --- a/src/uu/printf/Cargo.toml +++ b/src/uu/printf/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_printf" -version = "0.0.4" +version = "0.0.6" authors = [ "Nathan Ross", "uutils developers", @@ -18,8 +18,9 @@ edition = "2018" path = "src/printf.rs" [dependencies] +clap = "2.33.3" itertools = "0.8.0" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/printf/src/cli.rs b/src/uu/printf/src/cli.rs index 12e80a925..a5e9c9775 100644 --- a/src/uu/printf/src/cli.rs +++ b/src/uu/printf/src/cli.rs @@ -18,18 +18,15 @@ pub fn err_msg(msg: &str) { // by default stdout only flushes // to console when a newline is passed. -#[allow(unused_must_use)] pub fn flush_char(c: char) { print!("{}", c); - stdout().flush(); + let _ = stdout().flush(); } -#[allow(unused_must_use)] pub fn flush_str(s: &str) { print!("{}", s); - stdout().flush(); + let _ = stdout().flush(); } -#[allow(unused_must_use)] pub fn flush_bytes(bslice: &[u8]) { - stdout().write(bslice); - stdout().flush(); + let _ = stdout().write(bslice); + let _ = stdout().flush(); } diff --git a/src/uu/printf/src/main.rs b/src/uu/printf/src/main.rs index aa9a45be5..9def7dafe 100644 --- a/src/uu/printf/src/main.rs +++ b/src/uu/printf/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_printf); // spell-checker:ignore procs uucore printf +uucore_procs::main!(uu_printf); diff --git a/src/uu/printf/src/printf.rs b/src/uu/printf/src/printf.rs index c2952e5a9..efa9aea57 100644 --- a/src/uu/printf/src/printf.rs +++ b/src/uu/printf/src/printf.rs @@ -1,15 +1,19 @@ #![allow(dead_code)] - // spell-checker:ignore (change!) each's // spell-checker:ignore (ToDO) LONGHELP FORMATSTRING templating parameterizing formatstr +#[macro_use] +extern crate uucore; + +use clap::{crate_version, App, Arg}; +use uucore::InvalidEncodingHandling; + mod cli; mod memo; mod tokenize; -static NAME: &str = "printf"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); -static SHORT_USAGE: &str = "printf: usage: printf [-v var] format [arguments]"; +const VERSION: &str = "version"; +const HELP: &str = "help"; static LONGHELP_LEAD: &str = "printf USAGE: printf FORMATSTRING [ARGUMENT]... @@ -273,7 +277,9 @@ COPYRIGHT : "; pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); let location = &args[0]; if args.len() <= 1 { @@ -288,10 +294,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if formatstr == "--help" { print!("{} {}", LONGHELP_LEAD, LONGHELP_BODY); } else if formatstr == "--version" { - println!("{} {}", NAME, VERSION); + println!("{} {}", executable!(), crate_version!()); } else { let printf_args = &args[2..]; memo::Memo::run_all(formatstr, printf_args); } 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .arg(Arg::with_name(VERSION).long(VERSION)) + .arg(Arg::with_name(HELP).long(HELP)) +} diff --git a/src/uu/printf/src/tokenize/num_format/format_field.rs b/src/uu/printf/src/tokenize/num_format/format_field.rs index 68786e815..02998cde5 100644 --- a/src/uu/printf/src/tokenize/num_format/format_field.rs +++ b/src/uu/printf/src/tokenize/num_format/format_field.rs @@ -1,4 +1,4 @@ -// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum fprim interp +// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety //! Primitives used by Sub Tokenizer //! and num_format modules diff --git a/src/uu/printf/src/tokenize/num_format/formatter.rs b/src/uu/printf/src/tokenize/num_format/formatter.rs index 35faff2a9..f5f5d71b1 100644 --- a/src/uu/printf/src/tokenize/num_format/formatter.rs +++ b/src/uu/printf/src/tokenize/num_format/formatter.rs @@ -1,5 +1,3 @@ -// spell-checker:ignore (ToDO) inprefix for conv - //! Primitives used by num_format and sub_modules. //! never dealt with above (e.g. Sub Tokenizer never uses these) @@ -41,7 +39,7 @@ pub enum Base { // information from the beginning of a numeric argument // the precedes the beginning of a numeric value -pub struct InPrefix { +pub struct InitialPrefix { pub radix_in: Base, pub sign: i8, pub offset: usize, @@ -54,7 +52,7 @@ pub trait Formatter { fn get_primitive( &self, field: &FormatField, - inprefix: &InPrefix, + in_prefix: &InitialPrefix, str_in: &str, ) -> Option; // return a string from a FormatPrimitive, diff --git a/src/uu/printf/src/tokenize/num_format/formatters/base_conv/mod.rs b/src/uu/printf/src/tokenize/num_format/formatters/base_conv/mod.rs index 79af9abd5..a20f03a95 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/base_conv/mod.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/base_conv/mod.rs @@ -1,4 +1,4 @@ -// spell-checker:ignore (ToDO) arrnum mult basenum bufferval refd vals arrfloat conv intermed addl +// spell-checker:ignore (ToDO) arrnum arr_num mult basenum bufferval refd vals arrfloat conv intermed addl pub fn arrnum_int_mult(arr_num: &[u8], basenum: u8, base_ten_int_fact: u8) -> Vec { let mut carry: u16 = 0; @@ -28,8 +28,7 @@ pub fn arrnum_int_mult(arr_num: &[u8], basenum: u8, base_ten_int_fact: u8) -> Ve } } } - #[allow(clippy::map_clone)] - let ret: Vec = ret_rev.iter().rev().map(|x| *x).collect(); + let ret: Vec = ret_rev.into_iter().rev().collect(); ret } @@ -102,70 +101,6 @@ pub fn arrnum_int_div_step( remainder: rem_out, } } -// pub struct ArrFloat { -// pub leading_zeros: u8, -// pub values: Vec, -// pub basenum: u8 -// } -// -// pub struct ArrFloatDivOut { -// pub quotient: u8, -// pub remainder: ArrFloat -// } -// -// pub fn arrfloat_int_div( -// arrfloat_in : &ArrFloat, -// base_ten_int_divisor : u8, -// precision : u16 -// ) -> DivOut { -// -// let mut remainder = ArrFloat { -// basenum: arrfloat_in.basenum, -// leading_zeros: arrfloat_in.leading_zeroes, -// values: Vec::new() -// } -// let mut quotient = 0; -// -// let mut bufferval : u16 = 0; -// let base : u16 = arrfloat_in.basenum as u16; -// let divisor : u16 = base_ten_int_divisor as u16; -// -// let mut it_f = arrfloat_in.values.iter(); -// let mut position = 0 + arrfloat_in.leading_zeroes as u16; -// let mut at_end = false; -// while position< precision { -// let next_digit = match it_f.next() { -// Some(c) => {} -// None => { 0 } -// } -// match u_cur { -// Some(u) => { -// bufferval += u.clone() as u16; -// if bufferval > divisor { -// while bufferval >= divisor { -// quotient+=1; -// bufferval -= divisor; -// } -// if bufferval == 0 { -// rem_out.position +=1; -// } else { -// rem_out.replace = Some(bufferval as u8); -// } -// break; -// } else { -// bufferval *= base; -// } -// }, -// None => { -// break; -// } -// } -// u_cur = it_f.next().clone(); -// rem_out.position+=1; -// } -// ArrFloatDivOut { quotient: quotient, remainder: remainder } -// } -// pub fn arrnum_int_add(arrnum: &[u8], basenum: u8, base_ten_int_term: u8) -> Vec { let mut carry: u16 = u16::from(base_ten_int_term); let mut rem: u16; @@ -193,14 +128,12 @@ pub fn arrnum_int_add(arrnum: &[u8], basenum: u8, base_ten_int_term: u8) -> Vec< } } } - #[allow(clippy::map_clone)] - let ret: Vec = ret_rev.iter().rev().map(|x| *x).collect(); + let ret: Vec = ret_rev.into_iter().rev().collect(); ret } pub fn base_conv_vec(src: &[u8], radix_src: u8, radix_dest: u8) -> Vec { - let mut result: Vec = Vec::new(); - result.push(0); + let mut result = vec![0]; for i in src { result = arrnum_int_mult(&result, radix_dest, radix_src); result = arrnum_int_add(&result, radix_dest, *i); @@ -220,14 +153,11 @@ pub fn unsigned_to_arrnum(src: u16) -> Vec { } // temporary needs-improvement-function -#[allow(unused_variables)] -pub fn base_conv_float(src: &[u8], radix_src: u8, radix_dest: u8) -> f64 { +pub fn base_conv_float(src: &[u8], radix_src: u8, _radix_dest: u8) -> f64 { // it would require a lot of addl code // to implement this for arbitrary string input. // until then, the below operates as an outline // of how it would work. - let mut result: Vec = Vec::new(); - result.push(0); let mut factor: f64 = 1_f64; let radix_src_float: f64 = f64::from(radix_src); let mut r: f64 = 0_f64; @@ -269,7 +199,6 @@ pub fn arrnum_to_str(src: &[u8], radix_def_dest: &dyn RadixDef) -> String { str_out } -#[allow(unused_variables)] pub fn base_conv_str( src: &str, radix_def_src: &dyn RadixDef, diff --git a/src/uu/printf/src/tokenize/num_format/formatters/base_conv/tests.rs b/src/uu/printf/src/tokenize/num_format/formatters/base_conv/tests.rs index fcac4392e..7945d41ac 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/base_conv/tests.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/base_conv/tests.rs @@ -1,4 +1,4 @@ -// spell-checker:ignore (ToDO) conv arrnum mult shortcircuit +// spell-checker:ignore (ToDO) arrnum mult #[cfg(test)] use super::*; @@ -29,7 +29,7 @@ fn test_arrnum_int_non_base_10() { } #[test] -fn test_arrnum_int_div_shortcircuit() { +fn test_arrnum_int_div_short_circuit() { // ( let arrnum: Vec = vec![5, 5, 5, 5, 0]; let base_num = 10; diff --git a/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs b/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs index bf21bf8f9..0ca993680 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs @@ -1,8 +1,9 @@ -// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum +// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety +// spell-checker:ignore (ToDO) arrnum //! formatter for %a %F C99 Hex-floating-point subs use super::super::format_field::FormatField; -use super::super::formatter::{FormatPrimitive, Formatter, InPrefix}; +use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix}; use super::base_conv; use super::base_conv::RadixDef; use super::float_common::{primitive_to_str_common, FloatAnalysis}; @@ -20,15 +21,20 @@ impl Formatter for CninetyNineHexFloatf { fn get_primitive( &self, field: &FormatField, - inprefix: &InPrefix, + initial_prefix: &InitialPrefix, str_in: &str, ) -> Option { let second_field = field.second_field.unwrap_or(6) + 1; - let analysis = - FloatAnalysis::analyze(&str_in, inprefix, Some(second_field as usize), None, true); + let analysis = FloatAnalysis::analyze( + str_in, + initial_prefix, + Some(second_field as usize), + None, + true, + ); let f = get_primitive_hex( - inprefix, - &str_in[inprefix.offset..], + initial_prefix, + &str_in[initial_prefix.offset..], &analysis, second_field as usize, *field.field_char == 'A', @@ -43,44 +49,18 @@ impl Formatter for CninetyNineHexFloatf { // c99 hex has unique requirements of all floating point subs in pretty much every part of building a primitive, from prefix and suffix to need for base conversion (in all other cases if you don't have decimal you must have decimal, here it's the other way around) // on the todo list is to have a trait for get_primitive that is implemented by each float formatter and can override a default. when that happens we can take the parts of get_primitive_dec specific to dec and spin them out to their own functions that can be overridden. -#[allow(unused_variables)] -#[allow(unused_assignments)] fn get_primitive_hex( - inprefix: &InPrefix, - str_in: &str, - analysis: &FloatAnalysis, - last_dec_place: usize, + initial_prefix: &InitialPrefix, + _str_in: &str, + _analysis: &FloatAnalysis, + _last_dec_place: usize, capitalized: bool, ) -> FormatPrimitive { - let prefix = Some(String::from(if inprefix.sign == -1 { "-0x" } else { "0x" })); - - // assign the digits before and after the decimal points - // to separate slices. If no digits after decimal point, - // assign 0 - let (mut first_segment_raw, second_segment_raw) = match analysis.decimal_pos { - Some(pos) => (&str_in[..pos], &str_in[pos + 1..]), - None => (&str_in[..], "0"), - }; - if first_segment_raw.is_empty() { - first_segment_raw = "0"; - } - // convert to string, hexifying if input is in dec. - // let (first_segment, second_segment) = - // match inprefix.radix_in { - // Base::Ten => { - // (to_hex(first_segment_raw, true), - // to_hex(second_segment_raw, false)) - // } - // _ => { - // (String::from(first_segment_raw), - // String::from(second_segment_raw)) - // } - // }; - // - // - // f.pre_decimal = Some(first_segment); - // f.post_decimal = Some(second_segment); - // + let prefix = Some(String::from(if initial_prefix.sign == -1 { + "-0x" + } else { + "0x" + })); // TODO actual conversion, make sure to get back mantissa. // for hex to hex, it's really just a matter of moving the @@ -93,7 +73,7 @@ fn get_primitive_hex( // the difficult part of this (arrnum_int_div_step) is already implemented. // the hex float name may be a bit misleading in terms of how to go about the - // conversion. The best way to do it is to just convert the floatnum + // conversion. The best way to do it is to just convert the float number // directly to base 2 and then at the end translate back to hex. let mantissa = 0; let suffix = Some({ @@ -112,15 +92,15 @@ fn get_primitive_hex( } fn to_hex(src: &str, before_decimal: bool) -> String { - let rten = base_conv::RadixTen; - let rhex = base_conv::RadixHex; + let radix_ten = base_conv::RadixTen; + let radix_hex = base_conv::RadixHex; if before_decimal { - base_conv::base_conv_str(src, &rten, &rhex) + base_conv::base_conv_str(src, &radix_ten, &radix_hex) } else { - let as_arrnum_ten = base_conv::str_to_arrnum(src, &rten); + let as_arrnum_ten = base_conv::str_to_arrnum(src, &radix_ten); let s = format!( "{}", - base_conv::base_conv_float(&as_arrnum_ten, rten.get_max(), rhex.get_max()) + base_conv::base_conv_float(&as_arrnum_ten, radix_ten.get_max(), radix_hex.get_max()) ); if s.len() > 2 { String::from(&s[2..]) diff --git a/src/uu/printf/src/tokenize/num_format/formatters/decf.rs b/src/uu/printf/src/tokenize/num_format/formatters/decf.rs index 6b2baa890..3376345e0 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/decf.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/decf.rs @@ -1,87 +1,79 @@ -// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum fprim interp +// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety //! formatter for %g %G decimal subs use super::super::format_field::FormatField; -use super::super::formatter::{FormatPrimitive, Formatter, InPrefix}; +use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix}; use super::float_common::{get_primitive_dec, primitive_to_str_common, FloatAnalysis}; -fn get_len_fprim(fprim: &FormatPrimitive) -> usize { +fn get_len_fmt_primitive(fmt: &FormatPrimitive) -> usize { let mut len = 0; - if let Some(ref s) = fprim.prefix { + if let Some(ref s) = fmt.prefix { len += s.len(); } - if let Some(ref s) = fprim.pre_decimal { + if let Some(ref s) = fmt.pre_decimal { len += s.len(); } - if let Some(ref s) = fprim.post_decimal { + if let Some(ref s) = fmt.post_decimal { len += s.len(); } - if let Some(ref s) = fprim.suffix { + if let Some(ref s) = fmt.suffix { len += s.len(); } len } -pub struct Decf { - as_num: f64, -} +pub struct Decf; + impl Decf { pub fn new() -> Decf { - Decf { as_num: 0.0 } + Decf } } impl Formatter for Decf { fn get_primitive( &self, field: &FormatField, - inprefix: &InPrefix, + initial_prefix: &InitialPrefix, str_in: &str, ) -> Option { let second_field = field.second_field.unwrap_or(6) + 1; - // default to scif interp. so as to not truncate input vals + // default to scif interpretation so as to not truncate input vals // (that would be displayed in scif) based on relation to decimal place let analysis = FloatAnalysis::analyze( str_in, - inprefix, + initial_prefix, Some(second_field as usize + 1), None, false, ); let mut f_sci = get_primitive_dec( - inprefix, - &str_in[inprefix.offset..], + initial_prefix, + &str_in[initial_prefix.offset..], &analysis, second_field as usize, Some(*field.field_char == 'G'), ); // strip trailing zeroes if let Some(ref post_dec) = f_sci.post_decimal { - let mut i = post_dec.len(); - { - let mut it = post_dec.chars(); - while let Some(c) = it.next_back() { - if c != '0' { - break; - } - i -= 1; - } - } - if i != post_dec.len() { - f_sci.post_decimal = Some(String::from(&post_dec[0..i])); + let trimmed = post_dec.trim_end_matches('0'); + if trimmed.len() != post_dec.len() { + f_sci.post_decimal = Some(trimmed.to_owned()); } } let f_fl = get_primitive_dec( - inprefix, - &str_in[inprefix.offset..], + initial_prefix, + &str_in[initial_prefix.offset..], &analysis, second_field as usize, None, ); - Some(if get_len_fprim(&f_fl) >= get_len_fprim(&f_sci) { - f_sci - } else { - f_fl - }) + Some( + if get_len_fmt_primitive(&f_fl) >= get_len_fmt_primitive(&f_sci) { + f_sci + } else { + f_fl + }, + ) } fn primitive_to_str(&self, prim: &FormatPrimitive, field: FormatField) -> String { primitive_to_str_common(prim, &field) diff --git a/src/uu/printf/src/tokenize/num_format/formatters/float_common.rs b/src/uu/printf/src/tokenize/num_format/formatters/float_common.rs index 65fe5b6ea..97009b586 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/float_common.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/float_common.rs @@ -1,7 +1,10 @@ -// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum +// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety +// spell-checker:ignore (ToDO) arrnum use super::super::format_field::FormatField; -use super::super::formatter::{get_it_at, warn_incomplete_conv, Base, FormatPrimitive, InPrefix}; +use super::super::formatter::{ + get_it_at, warn_incomplete_conv, Base, FormatPrimitive, InitialPrefix, +}; use super::base_conv; use super::base_conv::RadixDef; @@ -39,7 +42,7 @@ fn has_enough_digits( impl FloatAnalysis { pub fn analyze( str_in: &str, - inprefix: &InPrefix, + initial_prefix: &InitialPrefix, max_sd_opt: Option, max_after_dec_opt: Option, hex_output: bool, @@ -47,13 +50,13 @@ impl FloatAnalysis { // this fn assumes // the input string // has no leading spaces or 0s - let str_it = get_it_at(inprefix.offset, str_in); + let str_it = get_it_at(initial_prefix.offset, str_in); let mut ret = FloatAnalysis { len_important: 0, decimal_pos: None, follow: None, }; - let hex_input = match inprefix.radix_in { + let hex_input = match initial_prefix.radix_in { Base::Hex => true, Base::Ten => false, Base::Octal => { @@ -126,15 +129,15 @@ impl FloatAnalysis { } fn de_hex(src: &str, before_decimal: bool) -> String { - let rten = base_conv::RadixTen; - let rhex = base_conv::RadixHex; + let radix_ten = base_conv::RadixTen; + let radix_hex = base_conv::RadixHex; if before_decimal { - base_conv::base_conv_str(src, &rhex, &rten) + base_conv::base_conv_str(src, &radix_hex, &radix_ten) } else { - let as_arrnum_hex = base_conv::str_to_arrnum(src, &rhex); + let as_arrnum_hex = base_conv::str_to_arrnum(src, &radix_hex); let s = format!( "{}", - base_conv::base_conv_float(&as_arrnum_hex, rhex.get_max(), rten.get_max()) + base_conv::base_conv_float(&as_arrnum_hex, radix_hex.get_max(), radix_ten.get_max()) ); if s.len() > 2 { String::from(&s[2..]) @@ -200,7 +203,7 @@ fn round_terminal_digit( } pub fn get_primitive_dec( - inprefix: &InPrefix, + initial_prefix: &InitialPrefix, str_in: &str, analysis: &FloatAnalysis, last_dec_place: usize, @@ -209,7 +212,7 @@ pub fn get_primitive_dec( let mut f: FormatPrimitive = Default::default(); // add negative sign section - if inprefix.sign == -1 { + if initial_prefix.sign == -1 { f.prefix = Some(String::from("-")); } @@ -218,13 +221,13 @@ pub fn get_primitive_dec( // assign 0 let (mut first_segment_raw, second_segment_raw) = match analysis.decimal_pos { Some(pos) => (&str_in[..pos], &str_in[pos + 1..]), - None => (&str_in[..], "0"), + None => (str_in, "0"), }; if first_segment_raw.is_empty() { first_segment_raw = "0"; } - // convert to string, de_hexifying if input is in hex. - let (first_segment, second_segment) = match inprefix.radix_in { + // convert to string, de_hexifying if input is in hex // spell-checker:disable-line + let (first_segment, second_segment) = match initial_prefix.radix_in { Base::Hex => ( de_hex(first_segment_raw, true), de_hex(second_segment_raw, false), @@ -244,8 +247,12 @@ pub fn get_primitive_dec( first_segment.len() as isize - 1, ) } else { - match first_segment.chars().next() { - Some('0') => { + match first_segment + .chars() + .next() + .expect("float_common: no chars in first segment.") + { + '0' => { let it = second_segment.chars().enumerate(); let mut m: isize = 0; let mut pre = String::from("0"); @@ -263,10 +270,7 @@ pub fn get_primitive_dec( } (pre, post, m) } - Some(_) => (first_segment, second_segment, 0), - None => { - panic!("float_common: no chars in first segment."); - } + _ => (first_segment, second_segment, 0), } } } else { @@ -295,11 +299,11 @@ pub fn get_primitive_dec( pub fn primitive_to_str_common(prim: &FormatPrimitive, field: &FormatField) -> String { let mut final_str = String::new(); if let Some(ref prefix) = prim.prefix { - final_str.push_str(&prefix); + final_str.push_str(prefix); } match prim.pre_decimal { Some(ref pre_decimal) => { - final_str.push_str(&pre_decimal); + final_str.push_str(pre_decimal); } None => { panic!( diff --git a/src/uu/printf/src/tokenize/num_format/formatters/floatf.rs b/src/uu/printf/src/tokenize/num_format/formatters/floatf.rs index 97ceafe8d..afb2bcf08 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/floatf.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/floatf.rs @@ -1,31 +1,35 @@ -// spell-checker:ignore (ToDO) floatf inprefix +// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety +// spell-checker:ignore (ToDO) arrnum //! formatter for %f %F common-notation floating-point subs use super::super::format_field::FormatField; -use super::super::formatter::{FormatPrimitive, Formatter, InPrefix}; +use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix}; use super::float_common::{get_primitive_dec, primitive_to_str_common, FloatAnalysis}; -pub struct Floatf { - as_num: f64, -} +pub struct Floatf; impl Floatf { pub fn new() -> Floatf { - Floatf { as_num: 0.0 } + Floatf } } impl Formatter for Floatf { fn get_primitive( &self, field: &FormatField, - inprefix: &InPrefix, + initial_prefix: &InitialPrefix, str_in: &str, ) -> Option { let second_field = field.second_field.unwrap_or(6) + 1; - let analysis = - FloatAnalysis::analyze(&str_in, inprefix, None, Some(second_field as usize), false); + let analysis = FloatAnalysis::analyze( + str_in, + initial_prefix, + None, + Some(second_field as usize), + false, + ); let f = get_primitive_dec( - inprefix, - &str_in[inprefix.offset..], + initial_prefix, + &str_in[initial_prefix.offset..], &analysis, second_field as usize, None, diff --git a/src/uu/printf/src/tokenize/num_format/formatters/intf.rs b/src/uu/printf/src/tokenize/num_format/formatters/intf.rs index 9231bd027..b6c18d436 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/intf.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/intf.rs @@ -1,17 +1,18 @@ -// spell-checker:ignore (ToDO) fchar conv decr inprefix intf ints finalstr +// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety +// spell-checker:ignore (ToDO) arrnum //! formatter for unsigned and signed int subs -//! unsigned ints: %X %x (hex u64) %o (octal u64) %u (base ten u64) -//! signed ints: %i %d (both base ten i64) +//! unsigned int: %X %x (hex u64) %o (octal u64) %u (base ten u64) +//! signed int: %i %d (both base ten i64) use super::super::format_field::FormatField; use super::super::formatter::{ - get_it_at, warn_incomplete_conv, Base, FormatPrimitive, Formatter, InPrefix, + get_it_at, warn_incomplete_conv, Base, FormatPrimitive, Formatter, InitialPrefix, }; use std::i64; use std::u64; pub struct Intf { - a: u32, + _a: u32, } // see the Intf::analyze() function below @@ -24,7 +25,7 @@ struct IntAnalysis { impl Intf { pub fn new() -> Intf { - Intf { a: 0 } + Intf { _a: 0 } } // take a ref to argument string, and basic information // about prefix (offset, radix, sign), and analyze string @@ -38,19 +39,19 @@ impl Intf { // is_zero: true if number is zero, false otherwise // len_digits: length of digits used to create the int // important, for example, if we run into a non-valid character - fn analyze(str_in: &str, signed_out: bool, inprefix: &InPrefix) -> IntAnalysis { + fn analyze(str_in: &str, signed_out: bool, initial_prefix: &InitialPrefix) -> IntAnalysis { // the maximum number of digits we could conceivably // have before the decimal point without exceeding the // max - let mut str_it = get_it_at(inprefix.offset, str_in); + let mut str_it = get_it_at(initial_prefix.offset, str_in); let max_sd_in = if signed_out { - match inprefix.radix_in { + match initial_prefix.radix_in { Base::Ten => 19, Base::Octal => 21, Base::Hex => 16, } } else { - match inprefix.radix_in { + match initial_prefix.radix_in { Base::Ten => 20, Base::Octal => 22, Base::Hex => 16, @@ -118,13 +119,13 @@ impl Intf { } // get a FormatPrimitive of the maximum value for the field char // and given sign - fn get_max(fchar: char, sign: i8) -> FormatPrimitive { - let mut fmt_prim: FormatPrimitive = Default::default(); - fmt_prim.pre_decimal = Some(String::from(match fchar { + fn get_max(field_char: char, sign: i8) -> FormatPrimitive { + let mut fmt_primitive: FormatPrimitive = Default::default(); + fmt_primitive.pre_decimal = Some(String::from(match field_char { 'd' | 'i' => match sign { 1 => "9223372036854775807", _ => { - fmt_prim.prefix = Some(String::from("-")); + fmt_primitive.prefix = Some(String::from("-")); "9223372036854775808" } }, @@ -132,7 +133,7 @@ impl Intf { 'o' => "1777777777777777777777", /* 'u' | */ _ => "18446744073709551615", })); - fmt_prim + fmt_primitive } // conv_from_segment contract: // 1. takes @@ -149,8 +150,13 @@ impl Intf { // - if the string falls outside bounds: // for i64 output, the int minimum or int max (depending on sign) // for u64 output, the u64 max in the output radix - fn conv_from_segment(segment: &str, radix_in: Base, fchar: char, sign: i8) -> FormatPrimitive { - match fchar { + fn conv_from_segment( + segment: &str, + radix_in: Base, + field_char: char, + sign: i8, + ) -> FormatPrimitive { + match field_char { 'i' | 'd' => match i64::from_str_radix(segment, radix_in as u32) { Ok(i) => { let mut fmt_prim: FormatPrimitive = Default::default(); @@ -160,13 +166,13 @@ impl Intf { fmt_prim.pre_decimal = Some(format!("{}", i)); fmt_prim } - Err(_) => Intf::get_max(fchar, sign), + Err(_) => Intf::get_max(field_char, sign), }, _ => match u64::from_str_radix(segment, radix_in as u32) { Ok(u) => { let mut fmt_prim: FormatPrimitive = Default::default(); let u_f = if sign == -1 { u64::MAX - (u - 1) } else { u }; - fmt_prim.pre_decimal = Some(match fchar { + fmt_prim.pre_decimal = Some(match field_char { 'X' => format!("{:X}", u_f), 'x' => format!("{:x}", u_f), 'o' => format!("{:o}", u_f), @@ -174,7 +180,7 @@ impl Intf { }); fmt_prim } - Err(_) => Intf::get_max(fchar, sign), + Err(_) => Intf::get_max(field_char, sign), }, } } @@ -183,17 +189,17 @@ impl Formatter for Intf { fn get_primitive( &self, field: &FormatField, - inprefix: &InPrefix, + initial_prefix: &InitialPrefix, str_in: &str, ) -> Option { - let begin = inprefix.offset; + let begin = initial_prefix.offset; // get information about the string. see Intf::Analyze // def above. let convert_hints = Intf::analyze( str_in, *field.field_char == 'i' || *field.field_char == 'd', - inprefix, + initial_prefix, ); // We always will have a format primitive to return Some(if convert_hints.len_digits == 0 || convert_hints.is_zero { @@ -209,22 +215,22 @@ impl Formatter for Intf { 'x' | 'X' => Base::Hex, /* 'o' | */ _ => Base::Octal, }; - let radix_mismatch = !radix_out.eq(&inprefix.radix_in); - let decr_from_max: bool = inprefix.sign == -1 && *field.field_char != 'i'; + let radix_mismatch = !radix_out.eq(&initial_prefix.radix_in); + let decrease_from_max: bool = initial_prefix.sign == -1 && *field.field_char != 'i'; let end = begin + convert_hints.len_digits as usize; // convert to int if any one of these is true: // - number of digits in int indicates it may be past max // - we're subtracting from the max // - we're converting the base - if convert_hints.check_past_max || decr_from_max || radix_mismatch { + if convert_hints.check_past_max || decrease_from_max || radix_mismatch { // radix of in and out is the same. let segment = String::from(&str_in[begin..end]); Intf::conv_from_segment( &segment, - inprefix.radix_in.clone(), + initial_prefix.radix_in.clone(), *field.field_char, - inprefix.sign, + initial_prefix.sign, ) } else { // otherwise just do a straight string copy. @@ -233,20 +239,20 @@ impl Formatter for Intf { // this is here and not earlier because // zero doesn't get a sign, and conv_from_segment // creates its format primitive separately - if inprefix.sign == -1 && *field.field_char == 'i' { + if initial_prefix.sign == -1 && *field.field_char == 'i' { fmt_prim.prefix = Some(String::from("-")); } fmt_prim.pre_decimal = Some(String::from(&str_in[begin..end])); fmt_prim } } else { - Intf::get_max(*field.field_char, inprefix.sign) + Intf::get_max(*field.field_char, initial_prefix.sign) }) } fn primitive_to_str(&self, prim: &FormatPrimitive, field: FormatField) -> String { - let mut finalstr: String = String::new(); + let mut final_str: String = String::new(); if let Some(ref prefix) = prim.prefix { - finalstr.push_str(&prefix); + final_str.push_str(prefix); } // integral second fields is zero-padded minimum-width // which gets handled before general minimum-width @@ -256,11 +262,11 @@ impl Formatter for Intf { let mut i = min; let len = pre_decimal.len() as u32; while i > len { - finalstr.push('0'); + final_str.push('0'); i -= 1; } } - finalstr.push_str(&pre_decimal); + final_str.push_str(pre_decimal); } None => { panic!( @@ -269,6 +275,6 @@ impl Formatter for Intf { ); } } - finalstr + final_str } } diff --git a/src/uu/printf/src/tokenize/num_format/formatters/mod.rs b/src/uu/printf/src/tokenize/num_format/formatters/mod.rs index ccbcdb1e7..e23230071 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/mod.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/mod.rs @@ -1,4 +1,4 @@ -// spell-checker:ignore (ToDO) conv cninetyninehexfloatf floatf intf scif +// spell-checker:ignore (vars) charf cninetyninehexfloatf decf floatf intf scif strf Cninety mod base_conv; pub mod cninetyninehexfloatf; diff --git a/src/uu/printf/src/tokenize/num_format/formatters/scif.rs b/src/uu/printf/src/tokenize/num_format/formatters/scif.rs index 69a703042..c46c7d423 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/scif.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/scif.rs @@ -1,36 +1,35 @@ -// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix +// spell-checker:ignore (vars) charf cninetyninehexfloatf decf floatf intf scif strf Cninety //! formatter for %e %E scientific notation subs use super::super::format_field::FormatField; -use super::super::formatter::{FormatPrimitive, Formatter, InPrefix}; +use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix}; use super::float_common::{get_primitive_dec, primitive_to_str_common, FloatAnalysis}; -pub struct Scif { - as_num: f64, -} +pub struct Scif; + impl Scif { pub fn new() -> Scif { - Scif { as_num: 0.0 } + Scif } } impl Formatter for Scif { fn get_primitive( &self, field: &FormatField, - inprefix: &InPrefix, + initial_prefix: &InitialPrefix, str_in: &str, ) -> Option { let second_field = field.second_field.unwrap_or(6) + 1; let analysis = FloatAnalysis::analyze( str_in, - inprefix, + initial_prefix, Some(second_field as usize + 1), None, false, ); let f = get_primitive_dec( - inprefix, - &str_in[inprefix.offset..], + initial_prefix, + &str_in[initial_prefix.offset..], &analysis, second_field as usize, Some(*field.field_char == 'E'), diff --git a/src/uu/printf/src/tokenize/num_format/num_format.rs b/src/uu/printf/src/tokenize/num_format/num_format.rs index 9a519e95e..b32731f2d 100644 --- a/src/uu/printf/src/tokenize/num_format/num_format.rs +++ b/src/uu/printf/src/tokenize/num_format/num_format.rs @@ -1,12 +1,14 @@ -// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety qchar topchar structs fmtr fchar inprefix devs octals cninetyninehexfloatf +// spell-checker:ignore (vars) charf cninetyninehexfloatf decf floatf intf scif strf Cninety //! handles creating printed output for numeric substitutions +// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety + use std::env; use std::vec::Vec; use super::format_field::{FieldType, FormatField}; -use super::formatter::{Base, FormatPrimitive, Formatter, InPrefix}; +use super::formatter::{Base, FormatPrimitive, Formatter, InitialPrefix}; use super::formatters::cninetyninehexfloatf::CninetyNineHexFloatf; use super::formatters::decf::Decf; use super::formatters::floatf::Floatf; @@ -46,8 +48,8 @@ fn get_provided(str_in_opt: Option<&String>) -> Option { match str_in_opt { Some(str_in) => { let mut byte_it = str_in.bytes(); - if let Some(qchar) = byte_it.next() { - match qchar { + if let Some(ch) = byte_it.next() { + match ch { C_S_QUOTE | C_D_QUOTE => { Some(match byte_it.next() { Some(second_byte) => { @@ -62,7 +64,7 @@ fn get_provided(str_in_opt: Option<&String>) -> Option { } // no byte after quote None => { - let so_far = (qchar as u8 as char).to_string(); + let so_far = (ch as u8 as char).to_string(); warn_expected_numeric(&so_far); 0_u8 } @@ -84,30 +86,30 @@ fn get_provided(str_in_opt: Option<&String>) -> Option { // a base, // and an offset for index after all // initial spacing, sign, base prefix, and leading zeroes -fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix { +fn get_initial_prefix(str_in: &str, field_type: &FieldType) -> InitialPrefix { let mut str_it = str_in.chars(); - let mut ret = InPrefix { + let mut ret = InitialPrefix { radix_in: Base::Ten, sign: 1, offset: 0, }; - let mut topchar = str_it.next(); - // skip spaces and ensure topchar is the first non-space char + let mut top_char = str_it.next(); + // skip spaces and ensure top_char is the first non-space char // (or None if none exists) - while let Some(' ') = topchar { + while let Some(' ') = top_char { ret.offset += 1; - topchar = str_it.next(); + top_char = str_it.next(); } // parse sign - match topchar { + match top_char { Some('+') => { ret.offset += 1; - topchar = str_it.next(); + top_char = str_it.next(); } Some('-') => { ret.sign = -1; ret.offset += 1; - topchar = str_it.next(); + top_char = str_it.next(); } _ => {} } @@ -125,7 +127,7 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix { // final offset. If the zero could be before // a decimal point we don't move past the zero. let mut is_hex = false; - if Some('0') == topchar { + if Some('0') == top_char { if let Some(base) = str_it.next() { // lead zeroes can only exist in // octal and hex base @@ -152,7 +154,7 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix { let mut first = true; for ch_zero in str_it { // see notes on offset above: - // this is why the offset for octals and decimals + // this is why the offset for octal and decimal numbers // that reach this branch is 1 even though // they have already eaten the characters '00' // this is also why when hex encounters its @@ -194,21 +196,21 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix { // if it is a numeric field, passing the field details // and an iterator to the argument pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option { - let fchar = field.field_char; + let field_char = field.field_char; // num format mainly operates by further delegating to one of // several Formatter structs depending on the field // see formatter.rs for more details // to do switch to static dispatch - let fmtr: Box = match *field.field_type { + let formatter: Box = match *field.field_type { FieldType::Intf => Box::new(Intf::new()), FieldType::Floatf => Box::new(Floatf::new()), FieldType::CninetyNineHexFloatf => Box::new(CninetyNineHexFloatf::new()), FieldType::Scif => Box::new(Scif::new()), FieldType::Decf => Box::new(Decf::new()), _ => { - panic!("asked to do num format with non-num fieldtype"); + panic!("asked to do num format with non-num field type"); } }; let prim_opt= @@ -216,7 +218,7 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option { tmp.pre_decimal = Some( format!("{}", provided_num)); @@ -231,11 +233,11 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option { let as_str = format!("{}", provided_num); - let inprefix = get_inprefix( + let initial_prefix = get_initial_prefix( &as_str, - &field.field_type + field.field_type ); - tmp=fmtr.get_primitive(field, &inprefix, &as_str) + tmp=formatter.get_primitive(field, &initial_prefix, &as_str) .expect("err during default provided num"); }, _ => { @@ -254,18 +256,14 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option FieldType::Charf, _ => { // should be unreachable. - println!("Invalid fieldtype"); + println!("Invalid field type"); exit(cli::EXIT_ERR); } }; @@ -130,7 +130,7 @@ impl SubParser { } } fn build_token(parser: SubParser) -> Box { - // not a self method so as to allow move of subparser vals. + // not a self method so as to allow move of sub-parser vals. // return new Sub struct as token let t: Box = Box::new(Sub::new( if parser.min_width_is_asterisk { @@ -275,7 +275,7 @@ impl SubParser { } None => { text_so_far.push('%'); - err_conv(&text_so_far); + err_conv(text_so_far); false } } @@ -354,7 +354,7 @@ impl token::Token for Sub { // field char let pre_min_width_opt: Option = match *field.field_type { // if %s just return arg - // if %b use UnescapedText module's unescaping-fn + // if %b use UnescapedText module's unescape-fn // if %c return first char of arg FieldType::Strf | FieldType::Charf => { match pf_arg { diff --git a/src/uu/printf/src/tokenize/unescaped_text.rs b/src/uu/printf/src/tokenize/unescaped_text.rs index 3b9f0123e..084014ae9 100644 --- a/src/uu/printf/src/tokenize/unescaped_text.rs +++ b/src/uu/printf/src/tokenize/unescaped_text.rs @@ -242,18 +242,16 @@ impl UnescapedText { } } } -#[allow(unused_variables)] impl token::Tokenizer for UnescapedText { fn from_it( it: &mut PutBackN, - args: &mut Peekable>, + _: &mut Peekable>, ) -> Option> { UnescapedText::from_it_core(it, false) } } -#[allow(unused_variables)] impl token::Token for UnescapedText { - fn print(&self, pf_args_it: &mut Peekable>) { + fn print(&self, _: &mut Peekable>) { cli::flush_bytes(&self.0[..]); } } diff --git a/src/uu/ptx/Cargo.toml b/src/uu/ptx/Cargo.toml index 3a91feeb0..eb4413cbd 100644 --- a/src/uu/ptx/Cargo.toml +++ b/src/uu/ptx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_ptx" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "ptx ~ (uutils) display a permuted index of input" @@ -15,13 +15,13 @@ edition = "2018" path = "src/ptx.rs" [dependencies] +clap = "2.33" aho-corasick = "0.7.3" -getopts = "0.2.18" libc = "0.2.42" memchr = "2.2.0" regex = "1.0.1" regex-syntax = "0.6.7" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/ptx/src/main.rs b/src/uu/ptx/src/main.rs index 0b235443a..b627b801f 100644 --- a/src/uu/ptx/src/main.rs +++ b/src/uu/ptx/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_ptx); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_ptx); diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index bfcd6699f..01b14bc4d 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -10,16 +10,22 @@ #[macro_use] extern crate uucore; -use getopts::{Matches, Options}; +use clap::{crate_version, App, Arg}; use regex::Regex; use std::cmp; use std::collections::{BTreeSet, HashMap, HashSet}; use std::default::Default; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; +use uucore::InvalidEncodingHandling; static NAME: &str = "ptx"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static BRIEF: &str = "Usage: ptx [OPTION]... [INPUT]... (without -G) or: \ + ptx -G [OPTION]... [INPUT [OUTPUT]] \n Output a permuted index, \ + including context, of the words in the input files. \n\n Mandatory \ + arguments to long options are mandatory for short options too.\n + With no FILE, or when FILE is -, read standard input. \ + Default is '-F /'."; #[derive(Debug)] enum OutFormat { @@ -61,8 +67,11 @@ impl Default for Config { } } -fn read_word_filter_file(matches: &Matches, option: &str) -> HashSet { - let filename = matches.opt_str(option).expect("parsing options failed!"); +fn read_word_filter_file(matches: &clap::ArgMatches, option: &str) -> HashSet { + let filename = matches + .value_of(option) + .expect("parsing options failed!") + .to_string(); let reader = BufReader::new(crash_if_err!(1, File::open(filename))); let mut words: HashSet = HashSet::new(); for word in reader.lines() { @@ -81,23 +90,32 @@ struct WordFilter { } impl WordFilter { - fn new(matches: &Matches, config: &Config) -> WordFilter { - let (o, oset): (bool, HashSet) = if matches.opt_present("o") { - (true, read_word_filter_file(matches, "o")) + fn new(matches: &clap::ArgMatches, config: &Config) -> WordFilter { + let (o, oset): (bool, HashSet) = if matches.is_present(options::ONLY_FILE) { + (true, read_word_filter_file(matches, options::ONLY_FILE)) } else { (false, HashSet::new()) }; - let (i, iset): (bool, HashSet) = if matches.opt_present("i") { - (true, read_word_filter_file(matches, "i")) + let (i, iset): (bool, HashSet) = if matches.is_present(options::IGNORE_FILE) { + (true, read_word_filter_file(matches, options::IGNORE_FILE)) } else { (false, HashSet::new()) }; - if matches.opt_present("b") { + if matches.is_present(options::BREAK_FILE) { crash!(1, "-b not implemented yet"); } // Ignore empty string regex from cmd-line-args - let arg_reg: Option = if matches.opt_present("W") { - matches.opt_str("W").filter(|reg| !reg.is_empty()) + let arg_reg: Option = if matches.is_present(options::WORD_REGEXP) { + match matches.value_of(options::WORD_REGEXP) { + Some(v) => { + if v.is_empty() { + None + } else { + Some(v.to_string()) + } + } + None => None, + } } else { None }; @@ -131,73 +149,76 @@ struct WordRef { filename: String, } -fn print_version() { - println!("{} {}", NAME, VERSION); -} - -fn print_usage(opts: &Options) { - let brief = "Usage: ptx [OPTION]... [INPUT]... (without -G) or: \ - ptx -G [OPTION]... [INPUT [OUTPUT]] \n Output a permuted index, \ - including context, of the words in the input files. \n\n Mandatory \ - arguments to long options are mandatory for short options too."; - let explanation = "With no FILE, or when FILE is -, read standard input. \ - Default is '-F /'."; - println!("{}\n{}", opts.usage(&brief), explanation); -} - -fn get_config(matches: &Matches) -> Config { +fn get_config(matches: &clap::ArgMatches) -> Config { let mut config: Config = Default::default(); let err_msg = "parsing options failed"; - if matches.opt_present("G") { + if matches.is_present(options::TRADITIONAL) { config.gnu_ext = false; config.format = OutFormat::Roff; config.context_regex = "[^ \t\n]+".to_owned(); } else { crash!(1, "GNU extensions not implemented yet"); } - if matches.opt_present("S") { + if matches.is_present(options::SENTENCE_REGEXP) { crash!(1, "-S not implemented yet"); } - config.auto_ref = matches.opt_present("A"); - config.input_ref = matches.opt_present("r"); - config.right_ref &= matches.opt_present("R"); - config.ignore_case = matches.opt_present("f"); - if matches.opt_present("M") { - config.macro_name = matches.opt_str("M").expect(err_msg); + config.auto_ref = matches.is_present(options::AUTO_REFERENCE); + config.input_ref = matches.is_present(options::REFERENCES); + config.right_ref &= matches.is_present(options::RIGHT_SIDE_REFS); + config.ignore_case = matches.is_present(options::IGNORE_CASE); + if matches.is_present(options::MACRO_NAME) { + config.macro_name = matches + .value_of(options::MACRO_NAME) + .expect(err_msg) + .to_string(); } - if matches.opt_present("F") { - config.trunc_str = matches.opt_str("F").expect(err_msg); + if matches.is_present(options::FLAG_TRUNCATION) { + config.trunc_str = matches + .value_of(options::FLAG_TRUNCATION) + .expect(err_msg) + .to_string(); } - if matches.opt_present("w") { - let width_str = matches.opt_str("w").expect(err_msg); - config.line_width = crash_if_err!(1, usize::from_str_radix(&width_str, 10)); + if matches.is_present(options::WIDTH) { + let width_str = matches.value_of(options::WIDTH).expect(err_msg).to_string(); + config.line_width = crash_if_err!(1, (&width_str).parse::()); } - if matches.opt_present("g") { - let gap_str = matches.opt_str("g").expect(err_msg); - config.gap_size = crash_if_err!(1, usize::from_str_radix(&gap_str, 10)); + if matches.is_present(options::GAP_SIZE) { + let gap_str = matches + .value_of(options::GAP_SIZE) + .expect(err_msg) + .to_string(); + config.gap_size = crash_if_err!(1, (&gap_str).parse::()); } - if matches.opt_present("O") { + if matches.is_present(options::FORMAT_ROFF) { config.format = OutFormat::Roff; } - if matches.opt_present("T") { + if matches.is_present(options::FORMAT_TEX) { config.format = OutFormat::Tex; } config } -fn read_input(input_files: &[String], config: &Config) -> HashMap, usize)> { - let mut file_map: HashMap, usize)> = HashMap::new(); +struct FileContent { + lines: Vec, + chars_lines: Vec>, + offset: usize, +} + +type FileMap = HashMap; + +fn read_input(input_files: &[String], config: &Config) -> FileMap { + let mut file_map: FileMap = HashMap::new(); let mut files = Vec::new(); if input_files.is_empty() { files.push("-"); } else if config.gnu_ext { for file in input_files { - files.push(&file); + files.push(file); } } else { files.push(&input_files[0]); } - let mut lines_so_far: usize = 0; + let mut offset: usize = 0; for filename in files { let reader: BufReader> = BufReader::new(if filename == "-" { Box::new(stdin()) @@ -206,25 +227,33 @@ fn read_input(input_files: &[String], config: &Config) -> HashMap = reader.lines().map(|x| crash_if_err!(1, x)).collect(); + + // Indexing UTF-8 string requires walking from the beginning, which can hurts performance badly when the line is long. + // Since we will be jumping around the line a lot, we dump the content into a Vec, which can be indexed in constant time. + let chars_lines: Vec> = lines.iter().map(|x| x.chars().collect()).collect(); let size = lines.len(); - file_map.insert(filename.to_owned(), (lines, lines_so_far)); - lines_so_far += size + file_map.insert( + filename.to_owned(), + FileContent { + lines, + chars_lines, + offset, + }, + ); + offset += size } file_map } -fn create_word_set( - config: &Config, - filter: &WordFilter, - file_map: &HashMap, usize)>, -) -> BTreeSet { +/// Go through every lines in the input files and record each match occurrence as a `WordRef`. +fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) -> BTreeSet { let reg = Regex::new(&filter.word_regex).unwrap(); let ref_reg = Regex::new(&config.context_regex).unwrap(); let mut word_set: BTreeSet = BTreeSet::new(); for (file, lines) in file_map.iter() { let mut count: usize = 0; - let offs = lines.1; - for line in &lines.0 { + let offs = lines.offset; + for line in &lines.lines { // if -r, exclude reference from word set let (ref_beg, ref_end) = match ref_reg.find(line) { Some(x) => (x.start(), x.end()), @@ -261,12 +290,11 @@ fn create_word_set( word_set } -fn get_reference(config: &Config, word_ref: &WordRef, line: &str) -> String { +fn get_reference(config: &Config, word_ref: &WordRef, line: &str, context_reg: &Regex) -> String { if config.auto_ref { format!("{}:{}", word_ref.filename, word_ref.local_line_nr + 1) } else if config.input_ref { - let reg = Regex::new(&config.context_regex).unwrap(); - let (beg, end) = match reg.find(line) { + let (beg, end) = match context_reg.find(line) { Some(x) => (x.start(), x.end()), None => (0, 0), }; @@ -319,57 +347,107 @@ fn trim_idx(s: &[char], beg: usize, end: usize) -> (usize, usize) { } fn get_output_chunks( - all_before: &str, + all_before: &[char], keyword: &str, - all_after: &str, + all_after: &[char], config: &Config, ) -> (String, String, String, String) { - assert_eq!(all_before.trim(), all_before); - assert_eq!(keyword.trim(), keyword); - assert_eq!(all_after.trim(), all_after); - let mut head = String::new(); - let mut before = String::new(); - let mut after = String::new(); - let mut tail = String::new(); - - let half_line_size = cmp::max( - (config.line_width / 2) as isize - (2 * config.trunc_str.len()) as isize, + // Chunk size logics are mostly copied from the GNU ptx source. + // https://github.com/MaiZure/coreutils-8.3/blob/master/src/ptx.c#L1234 + let half_line_size = (config.line_width / 2) as usize; + let max_before_size = cmp::max(half_line_size as isize - config.gap_size as isize, 0) as usize; + let max_after_size = cmp::max( + half_line_size as isize + - (2 * config.trunc_str.len()) as isize + - keyword.len() as isize + - 1, 0, ) as usize; - let max_after_size = cmp::max(half_line_size as isize - keyword.len() as isize - 1, 0) as usize; - let max_before_size = half_line_size; - let all_before_vec: Vec = all_before.chars().collect(); - let all_after_vec: Vec = all_after.chars().collect(); - // get before - let mut bb_tmp = cmp::max(all_before.len() as isize - max_before_size as isize, 0) as usize; - bb_tmp = trim_broken_word_left(&all_before_vec, bb_tmp, all_before.len()); - let (before_beg, before_end) = trim_idx(&all_before_vec, bb_tmp, all_before.len()); - before.push_str(&all_before[before_beg..before_end]); + // Allocate plenty space for all the chunks. + let mut head = String::with_capacity(half_line_size); + let mut before = String::with_capacity(half_line_size); + let mut after = String::with_capacity(half_line_size); + let mut tail = String::with_capacity(half_line_size); + + // the before chunk + + // trim whitespace away from all_before to get the index where the before chunk should end. + let (_, before_end) = trim_idx(all_before, 0, all_before.len()); + + // the minimum possible begin index of the before_chunk is the end index minus the length. + let before_beg = cmp::max(before_end as isize - max_before_size as isize, 0) as usize; + // in case that falls in the middle of a word, trim away the word. + let before_beg = trim_broken_word_left(all_before, before_beg, before_end); + + // trim away white space. + let (before_beg, before_end) = trim_idx(all_before, before_beg, before_end); + + // and get the string. + let before_str: String = all_before[before_beg..before_end].iter().collect(); + before.push_str(&before_str); assert!(max_before_size >= before.len()); - // get after - let mut ae_tmp = cmp::min(max_after_size, all_after.len()); - ae_tmp = trim_broken_word_right(&all_after_vec, 0, ae_tmp); - let (after_beg, after_end) = trim_idx(&all_after_vec, 0, ae_tmp); - after.push_str(&all_after[after_beg..after_end]); + // the after chunk + + // must be no longer than the minimum between the max size and the total available string. + let after_end = cmp::min(max_after_size, all_after.len()); + // in case that falls in the middle of a word, trim away the word. + let after_end = trim_broken_word_right(all_after, 0, after_end); + + // trim away white space. + let (_, after_end) = trim_idx(all_after, 0, after_end); + + // and get the string + let after_str: String = all_after[0..after_end].iter().collect(); + after.push_str(&after_str); assert!(max_after_size >= after.len()); - // get tail - let max_tail_size = max_before_size - before.len(); - let (tb, _) = trim_idx(&all_after_vec, after_end, all_after.len()); - let mut te_tmp = cmp::min(tb + max_tail_size, all_after.len()); - te_tmp = trim_broken_word_right(&all_after_vec, tb, te_tmp); - let (tail_beg, tail_end) = trim_idx(&all_after_vec, tb, te_tmp); - tail.push_str(&all_after[tail_beg..tail_end]); + // the tail chunk - // get head - let max_head_size = max_after_size - after.len(); - let (_, he) = trim_idx(&all_before_vec, 0, before_beg); - let mut hb_tmp = cmp::max(he as isize - max_head_size as isize, 0) as usize; - hb_tmp = trim_broken_word_left(&all_before_vec, hb_tmp, he); - let (head_beg, head_end) = trim_idx(&all_before_vec, hb_tmp, he); - head.push_str(&all_before[head_beg..head_end]); + // max size of the tail chunk = max size of left half - space taken by before chunk - gap size. + let max_tail_size = cmp::max( + max_before_size as isize - before.len() as isize - config.gap_size as isize, + 0, + ) as usize; + + // the tail chunk takes text starting from where the after chunk ends (with whitespace trimmed). + let (tail_beg, _) = trim_idx(all_after, after_end, all_after.len()); + + // end = begin + max length + let tail_end = cmp::min(all_after.len(), tail_beg + max_tail_size) as usize; + // in case that falls in the middle of a word, trim away the word. + let tail_end = trim_broken_word_right(all_after, tail_beg, tail_end); + + // trim away whitespace again. + let (tail_beg, tail_end) = trim_idx(all_after, tail_beg, tail_end); + + // and get the string + let tail_str: String = all_after[tail_beg..tail_end].iter().collect(); + tail.push_str(&tail_str); + + // the head chunk + + // max size of the head chunk = max size of right half - space taken by after chunk - gap size. + let max_head_size = cmp::max( + max_after_size as isize - after.len() as isize - config.gap_size as isize, + 0, + ) as usize; + + // the head chunk takes text from before the before chunk + let (_, head_end) = trim_idx(all_before, 0, before_beg); + + // begin = end - max length + let head_beg = cmp::max(head_end as isize - max_head_size as isize, 0) as usize; + // in case that falls in the middle of a word, trim away the word. + let head_beg = trim_broken_word_left(all_before, head_beg, head_end); + + // trim away white space again. + let (head_beg, head_end) = trim_idx(all_before, head_beg, head_end); + + // and get the string. + let head_str: String = all_before[head_beg..head_end].iter().collect(); + head.push_str(&head_str); // put right context truncation string if needed if after_end != all_after.len() && tail_beg == tail_end { @@ -385,11 +463,6 @@ fn get_output_chunks( head = format!("{}{}", config.trunc_str, head); } - // add space before "after" if needed - if !after.is_empty() { - after = format!(" {}", after); - } - (tail, before, after, head) } @@ -402,70 +475,95 @@ fn tex_mapper(x: char) -> String { } } -fn adjust_tex_str(context: &str) -> String { - let ws_reg = Regex::new(r"[\t\n\v\f\r ]").unwrap(); - let mut fix: String = ws_reg.replace_all(context, " ").trim().to_owned(); - let mapped_chunks: Vec = fix.chars().map(tex_mapper).collect(); - fix = mapped_chunks.join(""); - fix +/// Escape special characters for TeX. +fn format_tex_field(s: &str) -> String { + let mapped_chunks: Vec = s.chars().map(tex_mapper).collect(); + mapped_chunks.join("") } -fn format_tex_line(config: &Config, word_ref: &WordRef, line: &str, reference: &str) -> String { +fn format_tex_line( + config: &Config, + word_ref: &WordRef, + line: &str, + chars_line: &[char], + reference: &str, +) -> String { let mut output = String::new(); output.push_str(&format!("\\{} ", config.macro_name)); let all_before = if config.input_ref { let before = &line[0..word_ref.position]; - adjust_tex_str(before.trim().trim_start_matches(reference)) + let before_start_trim_offset = + word_ref.position - before.trim_start_matches(reference).trim_start().len(); + let before_end_index = before.len(); + &chars_line[before_start_trim_offset..cmp::max(before_end_index, before_start_trim_offset)] } else { - adjust_tex_str(&line[0..word_ref.position]) + let before_chars_trim_idx = (0, word_ref.position); + &chars_line[before_chars_trim_idx.0..before_chars_trim_idx.1] }; - let keyword = adjust_tex_str(&line[word_ref.position..word_ref.position_end]); - let all_after = adjust_tex_str(&line[word_ref.position_end..line.len()]); - let (tail, before, after, head) = get_output_chunks(&all_before, &keyword, &all_after, &config); + let keyword = &line[word_ref.position..word_ref.position_end]; + let after_chars_trim_idx = (word_ref.position_end, chars_line.len()); + let all_after = &chars_line[after_chars_trim_idx.0..after_chars_trim_idx.1]; + let (tail, before, after, head) = get_output_chunks(all_before, keyword, all_after, config); output.push_str(&format!( "{5}{0}{6}{5}{1}{6}{5}{2}{6}{5}{3}{6}{5}{4}{6}", - tail, before, keyword, after, head, "{", "}" + format_tex_field(&tail), + format_tex_field(&before), + format_tex_field(keyword), + format_tex_field(&after), + format_tex_field(&head), + "{", + "}" )); if config.auto_ref || config.input_ref { - output.push_str(&format!("{}{}{}", "{", adjust_tex_str(&reference), "}")); + output.push_str(&format!("{}{}{}", "{", format_tex_field(reference), "}")); } output } -fn adjust_roff_str(context: &str) -> String { - let ws_reg = Regex::new(r"[\t\n\v\f\r]").unwrap(); - ws_reg - .replace_all(context, " ") - .replace("\"", "\"\"") - .trim() - .to_owned() +fn format_roff_field(s: &str) -> String { + s.replace("\"", "\"\"") } -fn format_roff_line(config: &Config, word_ref: &WordRef, line: &str, reference: &str) -> String { +fn format_roff_line( + config: &Config, + word_ref: &WordRef, + line: &str, + chars_line: &[char], + reference: &str, +) -> String { let mut output = String::new(); output.push_str(&format!(".{}", config.macro_name)); let all_before = if config.input_ref { let before = &line[0..word_ref.position]; - adjust_roff_str(before.trim().trim_start_matches(reference)) + let before_start_trim_offset = + word_ref.position - before.trim_start_matches(reference).trim_start().len(); + let before_end_index = before.len(); + &chars_line[before_start_trim_offset..cmp::max(before_end_index, before_start_trim_offset)] } else { - adjust_roff_str(&line[0..word_ref.position]) + let before_chars_trim_idx = (0, word_ref.position); + &chars_line[before_chars_trim_idx.0..before_chars_trim_idx.1] }; - let keyword = adjust_roff_str(&line[word_ref.position..word_ref.position_end]); - let all_after = adjust_roff_str(&line[word_ref.position_end..line.len()]); - let (tail, before, after, head) = get_output_chunks(&all_before, &keyword, &all_after, &config); + let keyword = &line[word_ref.position..word_ref.position_end]; + let after_chars_trim_idx = (word_ref.position_end, chars_line.len()); + let all_after = &chars_line[after_chars_trim_idx.0..after_chars_trim_idx.1]; + let (tail, before, after, head) = get_output_chunks(all_before, keyword, all_after, config); output.push_str(&format!( " \"{}\" \"{}\" \"{}{}\" \"{}\"", - tail, before, keyword, after, head + format_roff_field(&tail), + format_roff_field(&before), + format_roff_field(keyword), + format_roff_field(&after), + format_roff_field(&head) )); if config.auto_ref || config.input_ref { - output.push_str(&format!(" \"{}\"", adjust_roff_str(&reference))); + output.push_str(&format!(" \"{}\"", format_roff_field(reference))); } output } fn write_traditional_output( config: &Config, - file_map: &HashMap, usize)>, + file_map: &FileMap, words: &BTreeSet, output_filename: &str, ) { @@ -475,124 +573,214 @@ fn write_traditional_output( let file = crash_if_err!(1, File::create(output_filename)); Box::new(file) }); + + let context_reg = Regex::new(&config.context_regex).unwrap(); + for word_ref in words.iter() { - let file_map_value: &(Vec, usize) = file_map + let file_map_value: &FileContent = file_map .get(&(word_ref.filename)) .expect("Missing file in file map"); - let (ref lines, _) = *(file_map_value); - let reference = get_reference(config, word_ref, &lines[word_ref.local_line_nr]); + let FileContent { + ref lines, + ref chars_lines, + offset: _, + } = *(file_map_value); + let reference = get_reference( + config, + word_ref, + &lines[word_ref.local_line_nr], + &context_reg, + ); let output_line: String = match config.format { - OutFormat::Tex => { - format_tex_line(config, word_ref, &lines[word_ref.local_line_nr], &reference) - } - OutFormat::Roff => { - format_roff_line(config, word_ref, &lines[word_ref.local_line_nr], &reference) - } + OutFormat::Tex => format_tex_line( + config, + word_ref, + &lines[word_ref.local_line_nr], + &chars_lines[word_ref.local_line_nr], + &reference, + ), + OutFormat::Roff => format_roff_line( + config, + word_ref, + &lines[word_ref.local_line_nr], + &chars_lines[word_ref.local_line_nr], + &reference, + ), OutFormat::Dumb => crash!(1, "There is no dumb format with GNU extensions disabled"), }; crash_if_err!(1, writeln!(writer, "{}", output_line)); } } +mod options { + pub static FILE: &str = "file"; + pub static AUTO_REFERENCE: &str = "auto-reference"; + pub static TRADITIONAL: &str = "traditional"; + pub static FLAG_TRUNCATION: &str = "flag-truncation"; + pub static MACRO_NAME: &str = "macro-name"; + pub static FORMAT_ROFF: &str = "format=roff"; + pub static RIGHT_SIDE_REFS: &str = "right-side-refs"; + pub static SENTENCE_REGEXP: &str = "sentence-regexp"; + pub static FORMAT_TEX: &str = "format=tex"; + pub static WORD_REGEXP: &str = "word-regexp"; + pub static BREAK_FILE: &str = "break-file"; + pub static IGNORE_CASE: &str = "ignore-case"; + pub static GAP_SIZE: &str = "gap-size"; + pub static IGNORE_FILE: &str = "ignore-file"; + pub static ONLY_FILE: &str = "only-file"; + pub static REFERENCES: &str = "references"; + pub static WIDTH: &str = "width"; +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let mut opts = Options::new(); - opts.optflag( - "A", - "auto-reference", - "output automatically generated references", - ); - opts.optflag("G", "traditional", "behave more like System V 'ptx'"); - opts.optopt( - "F", - "flag-truncation", - "use STRING for flagging line truncations", - "STRING", - ); - opts.optopt( - "M", - "macro-name", - "macro name to use instead of 'xx'", - "STRING", - ); - opts.optflag("O", "format=roff", "generate output as roff directives"); - opts.optflag( - "R", - "right-side-refs", - "put references at right, not counted in -w", - ); - opts.optopt( - "S", - "sentence-regexp", - "for end of lines or end of sentences", - "REGEXP", - ); - opts.optflag("T", "format=tex", "generate output as TeX directives"); - opts.optopt( - "W", - "word-regexp", - "use REGEXP to match each keyword", - "REGEXP", - ); - opts.optopt( - "b", - "break-file", - "word break characters in this FILE", - "FILE", - ); - opts.optflag( - "f", - "ignore-case", - "fold lower case to upper case for sorting", - ); - opts.optopt( - "g", - "gap-size", - "gap size in columns between output fields", - "NUMBER", - ); - opts.optopt( - "i", - "ignore-file", - "read ignore word list from FILE", - "FILE", - ); - opts.optopt( - "o", - "only-file", - "read only word list from this FILE", - "FILE", - ); - opts.optflag("r", "references", "first field of each line is a reference"); - opts.optopt( - "w", - "width", - "output width in columns, reference excluded", - "NUMBER", - ); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("", "version", "output version information and exit"); + // let mut opts = Options::new(); + let matches = uu_app().get_matches_from(args); - let matches = return_if_err!(1, opts.parse(&args[1..])); + let input_files: Vec = match &matches.values_of(options::FILE) { + Some(v) => v.clone().map(|v| v.to_owned()).collect(), + None => vec!["-".to_string()], + }; - if matches.opt_present("help") { - print_usage(&opts); - return 0; - } - if matches.opt_present("version") { - print_version(); - return 0; - } let config = get_config(&matches); let word_filter = WordFilter::new(&matches, &config); - let file_map = read_input(&matches.free, &config); + let file_map = read_input(&input_files, &config); let word_set = create_word_set(&config, &word_filter, &file_map); - let output_file = if !config.gnu_ext && matches.free.len() == 2 { - matches.free[1].clone() + let output_file = if !config.gnu_ext && matches.args.len() == 2 { + matches.value_of(options::FILE).unwrap_or("-").to_string() } else { "-".to_owned() }; write_traditional_output(&config, &file_map, &word_set, &output_file); 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(BRIEF) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) + .arg( + Arg::with_name(options::AUTO_REFERENCE) + .short("A") + .long(options::AUTO_REFERENCE) + .help("output automatically generated references") + .takes_value(false), + ) + .arg( + Arg::with_name(options::TRADITIONAL) + .short("G") + .long(options::TRADITIONAL) + .help("behave more like System V 'ptx'"), + ) + .arg( + Arg::with_name(options::FLAG_TRUNCATION) + .short("F") + .long(options::FLAG_TRUNCATION) + .help("use STRING for flagging line truncations") + .value_name("STRING") + .takes_value(true), + ) + .arg( + Arg::with_name(options::MACRO_NAME) + .short("M") + .long(options::MACRO_NAME) + .help("macro name to use instead of 'xx'") + .value_name("STRING") + .takes_value(true), + ) + .arg( + Arg::with_name(options::FORMAT_ROFF) + .short("O") + .long(options::FORMAT_ROFF) + .help("generate output as roff directives"), + ) + .arg( + Arg::with_name(options::RIGHT_SIDE_REFS) + .short("R") + .long(options::RIGHT_SIDE_REFS) + .help("put references at right, not counted in -w") + .takes_value(false), + ) + .arg( + Arg::with_name(options::SENTENCE_REGEXP) + .short("S") + .long(options::SENTENCE_REGEXP) + .help("for end of lines or end of sentences") + .value_name("REGEXP") + .takes_value(true), + ) + .arg( + Arg::with_name(options::FORMAT_TEX) + .short("T") + .long(options::FORMAT_TEX) + .help("generate output as TeX directives"), + ) + .arg( + Arg::with_name(options::WORD_REGEXP) + .short("W") + .long(options::WORD_REGEXP) + .help("use REGEXP to match each keyword") + .value_name("REGEXP") + .takes_value(true), + ) + .arg( + Arg::with_name(options::BREAK_FILE) + .short("b") + .long(options::BREAK_FILE) + .help("word break characters in this FILE") + .value_name("FILE") + .takes_value(true), + ) + .arg( + Arg::with_name(options::IGNORE_CASE) + .short("f") + .long(options::IGNORE_CASE) + .help("fold lower case to upper case for sorting") + .takes_value(false), + ) + .arg( + Arg::with_name(options::GAP_SIZE) + .short("g") + .long(options::GAP_SIZE) + .help("gap size in columns between output fields") + .value_name("NUMBER") + .takes_value(true), + ) + .arg( + Arg::with_name(options::IGNORE_FILE) + .short("i") + .long(options::IGNORE_FILE) + .help("read ignore word list from FILE") + .value_name("FILE") + .takes_value(true), + ) + .arg( + Arg::with_name(options::ONLY_FILE) + .short("o") + .long(options::ONLY_FILE) + .help("read only word list from this FILE") + .value_name("FILE") + .takes_value(true), + ) + .arg( + Arg::with_name(options::REFERENCES) + .short("r") + .long(options::REFERENCES) + .help("first field of each line is a reference") + .value_name("FILE") + .takes_value(false), + ) + .arg( + Arg::with_name(options::WIDTH) + .short("w") + .long(options::WIDTH) + .help("output width in columns, reference excluded") + .value_name("NUMBER") + .takes_value(true), + ) +} diff --git a/src/uu/pwd/Cargo.toml b/src/uu/pwd/Cargo.toml index e074dc618..f4350d54c 100644 --- a/src/uu/pwd/Cargo.toml +++ b/src/uu/pwd/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_pwd" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "pwd ~ (uutils) display current working directory" @@ -16,7 +16,7 @@ path = "src/pwd.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/pwd/src/main.rs b/src/uu/pwd/src/main.rs index 4445b7891..c5716d2c9 100644 --- a/src/uu/pwd/src/main.rs +++ b/src/uu/pwd/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_pwd); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_pwd); diff --git a/src/uu/pwd/src/pwd.rs b/src/uu/pwd/src/pwd.rs index 1786d33ee..764a63a88 100644 --- a/src/uu/pwd/src/pwd.rs +++ b/src/uu/pwd/src/pwd.rs @@ -8,13 +8,12 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::env; use std::io; use std::path::{Path, PathBuf}; static ABOUT: &str = "Display the full filename of the current working directory."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static OPT_LOGICAL: &str = "logical"; static OPT_PHYSICAL: &str = "physical"; @@ -40,23 +39,7 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(OPT_LOGICAL) - .short("L") - .long(OPT_LOGICAL) - .help("use PWD from environment, even if it contains symlinks"), - ) - .arg( - Arg::with_name(OPT_PHYSICAL) - .short("P") - .long(OPT_PHYSICAL) - .help("avoid all symlinks"), - ) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); match env::current_dir() { Ok(logical_path) => { @@ -74,3 +57,21 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(OPT_LOGICAL) + .short("L") + .long(OPT_LOGICAL) + .help("use PWD from environment, even if it contains symlinks"), + ) + .arg( + Arg::with_name(OPT_PHYSICAL) + .short("P") + .long(OPT_PHYSICAL) + .help("avoid all symlinks"), + ) +} diff --git a/src/uu/readlink/Cargo.toml b/src/uu/readlink/Cargo.toml index 8e31f66f1..6e4be4dd8 100644 --- a/src/uu/readlink/Cargo.toml +++ b/src/uu/readlink/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_readlink" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "readlink ~ (uutils) display resolved path of PATHNAME" @@ -17,7 +17,7 @@ path = "src/readlink.rs" [dependencies] clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/readlink/src/main.rs b/src/uu/readlink/src/main.rs index e5aab3cb6..651fd73ca 100644 --- a/src/uu/readlink/src/main.rs +++ b/src/uu/readlink/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_readlink); // spell-checker:ignore procs uucore readlink +uucore_procs::main!(uu_readlink); diff --git a/src/uu/readlink/src/readlink.rs b/src/uu/readlink/src/readlink.rs index 8bc9a2aeb..826fa0254 100644 --- a/src/uu/readlink/src/readlink.rs +++ b/src/uu/readlink/src/readlink.rs @@ -10,14 +10,13 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::fs; use std::io::{stdout, Write}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use uucore::fs::{canonicalize, CanonicalizeMode}; const NAME: &str = "readlink"; -const VERSION: &str = env!("CARGO_PKG_VERSION"); const ABOUT: &str = "Print value of a symbolic link or canonical file name."; const OPT_CANONICALIZE: &str = "canonicalize"; const OPT_CANONICALIZE_MISSING: &str = "canonicalize-missing"; @@ -36,69 +35,7 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(OPT_CANONICALIZE) - .short("f") - .long(OPT_CANONICALIZE) - .help( - "canonicalize by following every symlink in every component of the \ - given name recursively; all but the last component must exist", - ), - ) - .arg( - Arg::with_name(OPT_CANONICALIZE_EXISTING) - .short("e") - .long("canonicalize-existing") - .help( - "canonicalize by following every symlink in every component of the \ - given name recursively, all components must exist", - ), - ) - .arg( - Arg::with_name(OPT_CANONICALIZE_MISSING) - .short("m") - .long(OPT_CANONICALIZE_MISSING) - .help( - "canonicalize by following every symlink in every component of the \ - given name recursively, without requirements on components existence", - ), - ) - .arg( - Arg::with_name(OPT_NO_NEWLINE) - .short("n") - .long(OPT_NO_NEWLINE) - .help("do not output the trailing delimiter"), - ) - .arg( - Arg::with_name(OPT_QUIET) - .short("q") - .long(OPT_QUIET) - .help("suppress most error messages"), - ) - .arg( - Arg::with_name(OPT_SILENT) - .short("s") - .long(OPT_SILENT) - .help("suppress most error messages"), - ) - .arg( - Arg::with_name(OPT_VERBOSE) - .short("v") - .long(OPT_VERBOSE) - .help("report error message"), - ) - .arg( - Arg::with_name(OPT_ZERO) - .short("z") - .long(OPT_ZERO) - .help("separate output with NUL rather than newline"), - ) - .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); let mut no_newline = matches.is_present(OPT_NO_NEWLINE); let use_zero = matches.is_present(OPT_ZERO); @@ -160,8 +97,72 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } -fn show(path: &PathBuf, no_newline: bool, use_zero: bool) { - let path = path.as_path().to_str().unwrap(); +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(OPT_CANONICALIZE) + .short("f") + .long(OPT_CANONICALIZE) + .help( + "canonicalize by following every symlink in every component of the \ + given name recursively; all but the last component must exist", + ), + ) + .arg( + Arg::with_name(OPT_CANONICALIZE_EXISTING) + .short("e") + .long("canonicalize-existing") + .help( + "canonicalize by following every symlink in every component of the \ + given name recursively, all components must exist", + ), + ) + .arg( + Arg::with_name(OPT_CANONICALIZE_MISSING) + .short("m") + .long(OPT_CANONICALIZE_MISSING) + .help( + "canonicalize by following every symlink in every component of the \ + given name recursively, without requirements on components existence", + ), + ) + .arg( + Arg::with_name(OPT_NO_NEWLINE) + .short("n") + .long(OPT_NO_NEWLINE) + .help("do not output the trailing delimiter"), + ) + .arg( + Arg::with_name(OPT_QUIET) + .short("q") + .long(OPT_QUIET) + .help("suppress most error messages"), + ) + .arg( + Arg::with_name(OPT_SILENT) + .short("s") + .long(OPT_SILENT) + .help("suppress most error messages"), + ) + .arg( + Arg::with_name(OPT_VERBOSE) + .short("v") + .long(OPT_VERBOSE) + .help("report error message"), + ) + .arg( + Arg::with_name(OPT_ZERO) + .short("z") + .long(OPT_ZERO) + .help("separate output with NUL rather than newline"), + ) + .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) +} + +fn show(path: &Path, no_newline: bool, use_zero: bool) { + let path = path.to_str().unwrap(); if use_zero { print!("{}\0", path); } else if no_newline { diff --git a/src/uu/realpath/Cargo.toml b/src/uu/realpath/Cargo.toml index 6f03086ba..327a875f8 100644 --- a/src/uu/realpath/Cargo.toml +++ b/src/uu/realpath/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_realpath" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "realpath ~ (uutils) display resolved absolute path of PATHNAME" @@ -16,7 +16,7 @@ path = "src/realpath.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/realpath/src/main.rs b/src/uu/realpath/src/main.rs index 3a74bc5f6..8b8a8dc5e 100644 --- a/src/uu/realpath/src/main.rs +++ b/src/uu/realpath/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_realpath); // spell-checker:ignore procs uucore realpath +uucore_procs::main!(uu_realpath); diff --git a/src/uu/realpath/src/realpath.rs b/src/uu/realpath/src/realpath.rs index 5cc8f3d9a..fe2ad4ccc 100644 --- a/src/uu/realpath/src/realpath.rs +++ b/src/uu/realpath/src/realpath.rs @@ -10,13 +10,11 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; -use std::fs; -use std::path::PathBuf; +use clap::{crate_version, App, Arg}; +use std::path::{Path, PathBuf}; use uucore::fs::{canonicalize, CanonicalizeMode}; static ABOUT: &str = "print the resolved path"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static OPT_QUIET: &str = "quiet"; static OPT_STRIP: &str = "strip"; @@ -31,10 +29,35 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + /* the list of files */ + + let paths: Vec = matches + .values_of(ARG_FILES) + .unwrap() + .map(PathBuf::from) + .collect(); + + let strip = matches.is_present(OPT_STRIP); + let zero = matches.is_present(OPT_ZERO); + let quiet = matches.is_present(OPT_QUIET); + let mut retcode = 0; + for path in &paths { + if let Err(e) = resolve_path(path, strip, zero) { + if !quiet { + show_error!("{}: {}", e, path.display()); + } + retcode = 1 + }; + } + retcode +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(OPT_QUIET) .short("q") @@ -60,79 +83,27 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .required(true) .min_values(1), ) - .get_matches_from(args); - - /* the list of files */ - - let paths: Vec = matches - .values_of(ARG_FILES) - .unwrap() - .map(PathBuf::from) - .collect(); - - let strip = matches.is_present(OPT_STRIP); - let zero = matches.is_present(OPT_ZERO); - let quiet = matches.is_present(OPT_QUIET); - let mut retcode = 0; - for path in &paths { - if !resolve_path(path, strip, zero, quiet) { - retcode = 1 - }; - } - retcode } -fn resolve_path(p: &PathBuf, strip: bool, zero: bool, quiet: bool) -> bool { - let abs = canonicalize(p, CanonicalizeMode::Normal).unwrap(); - - if strip { - if zero { - print!("{}\0", p.display()); - } else { - println!("{}", p.display()) - } - return true; - } - - let mut result = PathBuf::new(); - let mut links_left = 256; - - for part in abs.components() { - result.push(part.as_os_str()); - loop { - if links_left == 0 { - if !quiet { - show_error!("Too many symbolic links: {}", p.display()) - }; - return false; - } - match fs::metadata(result.as_path()) { - Err(_) => break, - Ok(ref m) if !m.file_type().is_symlink() => break, - Ok(_) => { - links_left -= 1; - match fs::read_link(result.as_path()) { - Ok(x) => { - result.pop(); - result.push(x.as_path()); - } - _ => { - if !quiet { - show_error!("Invalid path: {}", p.display()) - }; - return false; - } - } - } - } - } - } - - if zero { - print!("{}\0", result.display()); +/// Resolve a path to an absolute form and print it. +/// +/// If `strip` is `true`, then this function does not attempt to resolve +/// symbolic links in the path. If `zero` is `true`, then this function +/// prints the path followed by the null byte (`'\0'`) instead of a +/// newline character (`'\n'`). +/// +/// # Errors +/// +/// This function returns an error if there is a problem resolving +/// symbolic links. +fn resolve_path(p: &Path, strip: bool, zero: bool) -> std::io::Result<()> { + let mode = if strip { + CanonicalizeMode::None } else { - println!("{}", result.display()); - } - - true + CanonicalizeMode::Normal + }; + let abs = canonicalize(p, mode)?; + let line_ending = if zero { '\0' } else { '\n' }; + print!("{}{}", abs.display(), line_ending); + Ok(()) } diff --git a/src/uu/relpath/Cargo.toml b/src/uu/relpath/Cargo.toml index 1dc296ec6..7a316c29c 100644 --- a/src/uu/relpath/Cargo.toml +++ b/src/uu/relpath/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_relpath" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "relpath ~ (uutils) display relative path of PATHNAME_TO from PATHNAME_FROM" @@ -15,8 +15,8 @@ edition = "2018" path = "src/relpath.rs" [dependencies] -getopts = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +clap = "2.33.3" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/relpath/src/main.rs b/src/uu/relpath/src/main.rs index a5f866bb2..22aa68d53 100644 --- a/src/uu/relpath/src/main.rs +++ b/src/uu/relpath/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_relpath); // spell-checker:ignore procs uucore relpath +uucore_procs::main!(uu_relpath); diff --git a/src/uu/relpath/src/relpath.rs b/src/uu/relpath/src/relpath.rs index 4e165df1f..cb0fba7cc 100644 --- a/src/uu/relpath/src/relpath.rs +++ b/src/uu/relpath/src/relpath.rs @@ -10,62 +10,43 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::env; use std::path::{Path, PathBuf}; use uucore::fs::{canonicalize, CanonicalizeMode}; +use uucore::InvalidEncodingHandling; -static NAME: &str = "relpath"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Convert TO destination to the relative path from the FROM dir. +If FROM path is omitted, current working dir will be used."; + +mod options { + pub const DIR: &str = "DIR"; + pub const TO: &str = "TO"; + pub const FROM: &str = "FROM"; +} + +fn get_usage() -> String { + format!("{} [-d DIR] TO [FROM]", executable!()) +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); + let usage = get_usage(); - let mut opts = getopts::Options::new(); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - opts.optflag("h", "help", "Show help and exit"); - opts.optflag("V", "version", "Show version and exit"); - opts.optopt( - "d", - "", - "If any of FROM and TO is not subpath of DIR, output absolute path instead of relative", - "DIR", - ); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => { - show_error!("{}", f); - show_usage(&opts); - return 1; - } - }; - - if matches.opt_present("V") { - version(); - return 0; - } - if matches.opt_present("h") { - show_usage(&opts); - return 0; - } - - if matches.free.is_empty() { - show_error!("Missing operand: TO"); - println!("Try `{} --help` for more information.", NAME); - return 1; - } - - let to = Path::new(&matches.free[0]); - let from = if matches.free.len() > 1 { - Path::new(&matches.free[1]).to_path_buf() - } else { - env::current_dir().unwrap() + let to = Path::new(matches.value_of(options::TO).unwrap()).to_path_buf(); // required + let from = match matches.value_of(options::FROM) { + Some(p) => Path::new(p).to_path_buf(), + None => env::current_dir().unwrap(), }; let absto = canonicalize(to, CanonicalizeMode::Normal).unwrap(); let absfrom = canonicalize(from, CanonicalizeMode::Normal).unwrap(); - if matches.opt_present("d") { - let base = Path::new(&matches.opt_str("d").unwrap()).to_path_buf(); + if matches.is_present(options::DIR) { + let base = Path::new(&matches.value_of(options::DIR).unwrap()).to_path_buf(); let absbase = canonicalize(base, CanonicalizeMode::Normal).unwrap(); if !absto.as_path().starts_with(absbase.as_path()) || !absfrom.as_path().starts_with(absbase.as_path()) @@ -100,23 +81,23 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } -fn version() { - println!("{} {}", NAME, VERSION) -} - -fn show_usage(opts: &getopts::Options) { - version(); - println!(); - println!("Usage:"); - println!(" {} [-d DIR] TO [FROM]", NAME); - println!(" {} -V|--version", NAME); - println!(" {} -h|--help", NAME); - println!(); - print!( - "{}", - opts.usage( - "Convert TO destination to the relative path from the FROM dir.\n\ - If FROM path is omitted, current working dir will be used." +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::DIR) + .short("d") + .takes_value(true) + .help("If any of FROM and TO is not subpath of DIR, output absolute path instead of relative"), + ) + .arg( + Arg::with_name(options::TO) + .required(true) + .takes_value(true), + ) + .arg( + Arg::with_name(options::FROM) + .takes_value(true), ) - ); } diff --git a/src/uu/rm/Cargo.toml b/src/uu/rm/Cargo.toml index b916412d8..d84756fd3 100644 --- a/src/uu/rm/Cargo.toml +++ b/src/uu/rm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_rm" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "rm ~ (uutils) remove PATHNAME" @@ -18,9 +18,12 @@ path = "src/rm.rs" clap = "2.33" walkdir = "2.2" remove_dir_all = "0.5.1" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +winapi = { version="0.3", features=[] } + +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } + [[bin]] name = "rm" path = "src/main.rs" diff --git a/src/uu/rm/src/main.rs b/src/uu/rm/src/main.rs index ebb998c39..960867359 100644 --- a/src/uu/rm/src/main.rs +++ b/src/uu/rm/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_rm); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_rm); diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index 4c81c97cc..259d1ab39 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -5,18 +5,18 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore (ToDO) bitor ulong +// spell-checker:ignore (path) eacces #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use remove_dir_all::remove_dir_all; use std::collections::VecDeque; use std::fs; use std::io::{stderr, stdin, BufRead, Write}; use std::ops::BitOr; -use std::path::Path; +use std::path::{Path, PathBuf}; use walkdir::{DirEntry, WalkDir}; #[derive(Eq, PartialEq, Clone, Copy)] @@ -38,7 +38,6 @@ struct Options { } static ABOUT: &str = "Remove (unlink) the FILE(s)"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static OPT_DIR: &str = "dir"; static OPT_INTERACTIVE: &str = "interactive"; static OPT_FORCE: &str = "force"; @@ -48,6 +47,7 @@ static OPT_PRESERVE_ROOT: &str = "preserve-root"; static OPT_PROMPT: &str = "prompt"; static OPT_PROMPT_MORE: &str = "prompt-more"; static OPT_RECURSIVE: &str = "recursive"; +static OPT_RECURSIVE_R: &str = "recursive_R"; static OPT_VERBOSE: &str = "verbose"; static ARG_FILES: &str = "files"; @@ -58,7 +58,7 @@ fn get_usage() -> String { fn get_long_usage() -> String { String::from( - "By default, rm does not remove directories. Use the --recursive (-r) + "By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents To remove a file whose name starts with a '-', for example '-foo', @@ -77,12 +77,72 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let long_usage = get_long_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() .usage(&usage[..]) .after_help(&long_usage[..]) - // TODO: make getopts support -R in addition to -r + .get_matches_from(args); + + let files: Vec = matches + .values_of(ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + let force = matches.is_present(OPT_FORCE); + + if files.is_empty() && !force { + // Still check by hand and not use clap + // Because "rm -f" is a thing + show_error!("missing an argument"); + show_error!("for help, try '{0} --help'", executable!()); + return 1; + } else { + let options = Options { + force, + interactive: { + if matches.is_present(OPT_PROMPT) { + InteractiveMode::Always + } else if matches.is_present(OPT_PROMPT_MORE) { + InteractiveMode::Once + } else if matches.is_present(OPT_INTERACTIVE) { + match matches.value_of(OPT_INTERACTIVE).unwrap() { + "none" => InteractiveMode::None, + "once" => InteractiveMode::Once, + "always" => InteractiveMode::Always, + val => crash!(1, "Invalid argument to interactive ({})", val), + } + } else { + InteractiveMode::None + } + }, + one_fs: matches.is_present(OPT_ONE_FILE_SYSTEM), + preserve_root: !matches.is_present(OPT_NO_PRESERVE_ROOT), + recursive: matches.is_present(OPT_RECURSIVE) || matches.is_present(OPT_RECURSIVE_R), + dir: matches.is_present(OPT_DIR), + verbose: matches.is_present(OPT_VERBOSE), + }; + if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) { + let msg = if options.recursive { + "Remove all arguments recursively? " + } else { + "Remove all arguments? " + }; + if !prompt(msg) { + return 0; + } + } + + if remove(files, options) { + return 1; + } + } + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) .arg( Arg::with_name(OPT_FORCE) @@ -128,6 +188,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .long(OPT_RECURSIVE) .help("remove directories and their contents recursively") ) + .arg( + // To mimic GNU's behavior we also want the '-R' flag. However, using clap's + // alias method 'visible_alias("R")' would result in a long '--R' flag. + Arg::with_name(OPT_RECURSIVE_R).short("R") + .help("Equivalent to -r") + ) .arg( Arg::with_name(OPT_DIR) .short("d") @@ -146,63 +212,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .takes_value(true) .min_values(1) ) - .get_matches_from(args); - - let files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let force = matches.is_present(OPT_FORCE); - - if files.is_empty() && !force { - // Still check by hand and not use clap - // Because "rm -f" is a thing - show_error!("missing an argument"); - show_error!("for help, try '{0} --help'", executable!()); - return 1; - } else { - let options = Options { - force, - interactive: { - if matches.is_present(OPT_PROMPT) { - InteractiveMode::Always - } else if matches.is_present(OPT_PROMPT_MORE) { - InteractiveMode::Once - } else if matches.is_present(OPT_INTERACTIVE) { - match &matches.value_of(OPT_INTERACTIVE).unwrap()[..] { - "none" => InteractiveMode::None, - "once" => InteractiveMode::Once, - "always" => InteractiveMode::Always, - val => crash!(1, "Invalid argument to interactive ({})", val), - } - } else { - InteractiveMode::None - } - }, - one_fs: matches.is_present(OPT_ONE_FILE_SYSTEM), - preserve_root: !matches.is_present(OPT_NO_PRESERVE_ROOT), - recursive: matches.is_present(OPT_RECURSIVE), - dir: matches.is_present(OPT_DIR), - verbose: matches.is_present(OPT_VERBOSE), - }; - if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) { - let msg = if options.recursive { - "Remove all arguments recursively? " - } else { - "Remove all arguments? " - }; - if !prompt(msg) { - return 0; - } - } - - if remove(files, options) { - return 1; - } - } - - 0 } // TODO: implement one-file-system (this may get partially implemented in walkdir) @@ -227,7 +236,7 @@ fn remove(files: Vec, options: Options) -> bool { // (e.g., permission), even rm -f should fail with // outputting the error, but there's no easy eay. if !options.force { - show_error!("no such file or directory '{}'", filename); + show_error!("cannot remove '{}': No such file or directory", filename); true } else { false @@ -245,12 +254,23 @@ fn handle_dir(path: &Path, options: &Options) -> bool { let is_root = path.has_root() && path.parent().is_none(); if options.recursive && (!is_root || !options.preserve_root) { - if options.interactive != InteractiveMode::Always { + if options.interactive != InteractiveMode::Always && !options.verbose { // we need the extra crate because apparently fs::remove_dir_all() does not function // correctly on Windows if let Err(e) = remove_dir_all(path) { had_err = true; - show_error!("could not remove '{}': {}", path.display(), e); + if e.kind() == std::io::ErrorKind::PermissionDenied { + // GNU compatibility (rm/fail-eacces.sh) + // here, GNU doesn't use some kind of remove_dir_all + // It will show directory+file + show_error!( + "cannot remove '{}': {}", + path.display(), + "Permission denied" + ); + } else { + show_error!("cannot remove '{}': {}", path.display(), e); + } } } else { let mut dirs: VecDeque = VecDeque::new(); @@ -283,7 +303,7 @@ fn handle_dir(path: &Path, options: &Options) -> bool { had_err = true; } else { show_error!( - "could not remove directory '{}' (did you mean to pass '-r'?)", + "cannot remove '{}': Is a directory", // GNU's rm error message does not include help path.display() ); had_err = true; @@ -299,16 +319,43 @@ fn remove_dir(path: &Path, options: &Options) -> bool { true }; if response { - match fs::remove_dir(path) { - Ok(_) => { - if options.verbose { - println!("removed '{}'", path.display()); + if let Ok(mut read_dir) = fs::read_dir(path) { + if options.dir || options.recursive { + if read_dir.next().is_none() { + match fs::remove_dir(path) { + Ok(_) => { + if options.verbose { + println!("removed directory '{}'", normalize(path).display()); + } + } + Err(e) => { + if e.kind() == std::io::ErrorKind::PermissionDenied { + // GNU compatibility (rm/fail-eacces.sh) + show_error!( + "cannot remove '{}': {}", + path.display(), + "Permission denied" + ); + } else { + show_error!("cannot remove '{}': {}", path.display(), e); + } + return true; + } + } + } else { + // directory can be read but is not empty + show_error!("cannot remove '{}': Directory not empty", path.display()); + return true; } - } - Err(e) => { - show_error!("removing '{}': {}", path.display(), e); + } else { + // called to remove a symlink_dir (windows) without "-r"/"-R" or "-d" + show_error!("cannot remove '{}': Is a directory", path.display()); return true; } + } else { + // GNU's rm shows this message if directory is empty but not readable + show_error!("cannot remove '{}': Directory not empty", path.display()); + return true; } } @@ -325,11 +372,20 @@ fn remove_file(path: &Path, options: &Options) -> bool { match fs::remove_file(path) { Ok(_) => { if options.verbose { - println!("removed '{}'", path.display()); + println!("removed '{}'", normalize(path).display()); } } Err(e) => { - show_error!("removing '{}': {}", path.display(), e); + if e.kind() == std::io::ErrorKind::PermissionDenied { + // GNU compatibility (rm/fail-eacces.sh) + show_error!( + "cannot remove '{}': {}", + path.display(), + "Permission denied" + ); + } else { + show_error!("cannot remove '{}': {}", path.display(), e); + } return true; } } @@ -346,6 +402,14 @@ fn prompt_file(path: &Path, is_dir: bool) -> bool { } } +fn normalize(path: &Path) -> PathBuf { + // copied from https://github.com/rust-lang/cargo/blob/2e4cfc2b7d43328b207879228a2ca7d427d188bb/src/cargo/util/paths.rs#L65-L90 + // both projects are MIT https://github.com/rust-lang/cargo/blob/master/LICENSE-MIT + // for std impl progress see rfc https://github.com/rust-lang/rfcs/issues/2208 + // TODO: replace this once that lands + uucore::fs::normalize_path(path) +} + fn prompt(msg: &str) -> bool { let _ = stderr().write_all(msg.as_bytes()); let _ = stderr().flush(); @@ -354,13 +418,8 @@ fn prompt(msg: &str) -> bool { let stdin = stdin(); let mut stdin = stdin.lock(); - #[allow(clippy::match_like_matches_macro)] - // `matches!(...)` macro not stabilized until rust v1.42 match stdin.read_until(b'\n', &mut buf) { - Ok(x) if x > 0 => match buf[0] { - b'y' | b'Y' => true, - _ => false, - }, + Ok(x) if x > 0 => matches!(buf[0], b'y' | b'Y'), _ => false, } } @@ -375,9 +434,7 @@ use std::os::windows::prelude::MetadataExt; #[cfg(windows)] fn is_symlink_dir(metadata: &fs::Metadata) -> bool { - use std::os::raw::c_ulong; - pub type DWORD = c_ulong; - pub const FILE_ATTRIBUTE_DIRECTORY: DWORD = 0x10; + use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY; metadata.file_type().is_symlink() && ((metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY) != 0) diff --git a/src/uu/rmdir/Cargo.toml b/src/uu/rmdir/Cargo.toml index 9d6bb03cc..b6e04f71c 100644 --- a/src/uu/rmdir/Cargo.toml +++ b/src/uu/rmdir/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_rmdir" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "rmdir ~ (uutils) remove empty DIRECTORY" @@ -16,7 +16,7 @@ path = "src/rmdir.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/rmdir/src/main.rs b/src/uu/rmdir/src/main.rs index ab1939b4a..92ff22c07 100644 --- a/src/uu/rmdir/src/main.rs +++ b/src/uu/rmdir/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_rmdir); // spell-checker:ignore procs uucore rmdir +uucore_procs::main!(uu_rmdir); diff --git a/src/uu/rmdir/src/rmdir.rs b/src/uu/rmdir/src/rmdir.rs index 7a7e8fc9b..8dbaf79a8 100644 --- a/src/uu/rmdir/src/rmdir.rs +++ b/src/uu/rmdir/src/rmdir.rs @@ -5,14 +5,15 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. +// spell-checker:ignore (ToDO) ENOTDIR + #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::fs; use std::path::Path; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Remove the DIRECTORY(ies), if they are empty."; static OPT_IGNORE_FAIL_NON_EMPTY: &str = "ignore-fail-on-non-empty"; static OPT_PARENTS: &str = "parents"; @@ -20,6 +21,11 @@ static OPT_VERBOSE: &str = "verbose"; static ARG_DIRS: &str = "dirs"; +#[cfg(unix)] +static ENOTDIR: i32 = 20; +#[cfg(windows)] +static ENOTDIR: i32 = 267; + fn get_usage() -> String { format!("{0} [OPTION]... DIRECTORY...", executable!()) } @@ -27,10 +33,29 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let dirs: Vec = matches + .values_of(ARG_DIRS) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + let ignore = matches.is_present(OPT_IGNORE_FAIL_NON_EMPTY); + let parents = matches.is_present(OPT_PARENTS); + let verbose = matches.is_present(OPT_VERBOSE); + + match remove(dirs, ignore, parents, verbose) { + Ok(()) => ( /* pass */ ), + Err(e) => return e, + } + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(OPT_IGNORE_FAIL_NON_EMPTY) .long(OPT_IGNORE_FAIL_NON_EMPTY) @@ -58,23 +83,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .min_values(1) .required(true), ) - .get_matches_from(args); - - let dirs: Vec = matches - .values_of(ARG_DIRS) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let ignore = matches.is_present(OPT_IGNORE_FAIL_NON_EMPTY); - let parents = matches.is_present(OPT_PARENTS); - let verbose = matches.is_present(OPT_VERBOSE); - - match remove(dirs, ignore, parents, verbose) { - Ok(()) => ( /* pass */ ), - Err(e) => return e, - } - - 0 } fn remove(dirs: Vec, ignore: bool, parents: bool, verbose: bool) -> Result<(), i32> { @@ -82,7 +90,7 @@ fn remove(dirs: Vec, ignore: bool, parents: bool, verbose: bool) -> Resu for dir in &dirs { let path = Path::new(&dir[..]); - r = remove_dir(&path, ignore, verbose).and(r); + r = remove_dir(path, ignore, verbose).and(r); if parents { let mut p = path; while let Some(new_p) = p.parent() { @@ -103,13 +111,14 @@ fn remove(dirs: Vec, ignore: bool, parents: bool, verbose: bool) -> Resu } fn remove_dir(path: &Path, ignore: bool, verbose: bool) -> Result<(), i32> { - let mut read_dir = match fs::read_dir(path) { - Ok(m) => m, - Err(e) => { + let mut read_dir = fs::read_dir(path).map_err(|e| { + if e.raw_os_error() == Some(ENOTDIR) { + show_error!("failed to remove '{}': Not a directory", path.display()); + } else { show_error!("reading directory '{}': {}", path.display(), e); - return Err(1); } - }; + 1 + })?; let mut r = Ok(()); diff --git a/src/uu/seq/Cargo.toml b/src/uu/seq/Cargo.toml index d7ee72b68..32f2bbac8 100644 --- a/src/uu/seq/Cargo.toml +++ b/src/uu/seq/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_seq" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "seq ~ (uutils) display a sequence of numbers" @@ -16,7 +16,9 @@ path = "src/seq.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +num-bigint = "0.4.0" +num-traits = "0.2.14" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/seq/src/main.rs b/src/uu/seq/src/main.rs index c984ed61a..266ac5d11 100644 --- a/src/uu/seq/src/main.rs +++ b/src/uu/seq/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_seq); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_seq); diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 671dd7e1c..50a93d3af 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -6,11 +6,15 @@ #[macro_use] extern crate uucore; -use clap::{App, AppSettings, Arg}; +use clap::{crate_version, App, AppSettings, Arg}; +use num_bigint::BigInt; +use num_traits::One; +use num_traits::Zero; +use num_traits::{Num, ToPrimitive}; use std::cmp; use std::io::{stdout, Write}; +use std::str::FromStr; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Display numbers from FIRST to LAST, in steps of INCREMENT."; static OPT_SEPARATOR: &str = "separator"; static OPT_TERMINATOR: &str = "terminator"; @@ -29,34 +33,140 @@ fn get_usage() -> String { #[derive(Clone)] struct SeqOptions { separator: String, - terminator: Option, + terminator: String, widths: bool, } -fn parse_float(mut s: &str) -> Result { - if s.starts_with('+') { - s = &s[1..]; +enum Number { + BigInt(BigInt), + F64(f64), +} + +impl Number { + fn is_zero(&self) -> bool { + match self { + Number::BigInt(n) => n.is_zero(), + Number::F64(n) => n.is_zero(), + } } - match s.parse() { - Ok(n) => Ok(n), - Err(e) => Err(format!( - "seq: invalid floating point argument `{}`: {}", - s, e - )), + + fn into_f64(self) -> f64 { + match self { + // BigInt::to_f64() can not return None. + Number::BigInt(n) => n.to_f64().unwrap(), + Number::F64(n) => n, + } } } -fn escape_sequences(s: &str) -> String { - s.replace("\\n", "\n").replace("\\t", "\t") +impl FromStr for Number { + type Err = String; + fn from_str(mut s: &str) -> Result { + if s.starts_with('+') { + s = &s[1..]; + } + + match s.parse::() { + Ok(n) => Ok(Number::BigInt(n)), + Err(_) => match s.parse::() { + Ok(value) if value.is_nan() => Err(format!( + "invalid 'not-a-number' argument: '{}'\nTry '{} --help' for more information.", + s, + executable!(), + )), + Ok(value) => Ok(Number::F64(value)), + Err(_) => Err(format!( + "invalid floating point argument: '{}'\nTry '{} --help' for more information.", + s, + executable!(), + )), + }, + } + } } pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let numbers = matches.values_of(ARG_NUMBERS).unwrap().collect::>(); + + let options = SeqOptions { + separator: matches.value_of(OPT_SEPARATOR).unwrap_or("\n").to_string(), + terminator: matches.value_of(OPT_TERMINATOR).unwrap_or("\n").to_string(), + widths: matches.is_present(OPT_WIDTHS), + }; + + let mut largest_dec = 0; + let mut padding = 0; + let first = if numbers.len() > 1 { + let slice = numbers[0]; + let len = slice.len(); + let dec = slice.find('.').unwrap_or(len); + largest_dec = len - dec; + padding = dec; + return_if_err!(1, slice.parse()) + } else { + Number::BigInt(BigInt::one()) + }; + let increment = if numbers.len() > 2 { + let slice = numbers[1]; + let len = slice.len(); + let dec = slice.find('.').unwrap_or(len); + largest_dec = cmp::max(largest_dec, len - dec); + padding = cmp::max(padding, dec); + return_if_err!(1, slice.parse()) + } else { + Number::BigInt(BigInt::one()) + }; + if increment.is_zero() { + show_error!( + "invalid Zero increment value: '{}'\nTry '{} --help' for more information.", + numbers[1], + executable!() + ); + return 1; + } + let last = { + let slice = numbers[numbers.len() - 1]; + padding = cmp::max(padding, slice.find('.').unwrap_or_else(|| slice.len())); + return_if_err!(1, slice.parse()) + }; + if largest_dec > 0 { + largest_dec -= 1; + } + + match (first, last, increment) { + (Number::BigInt(first), Number::BigInt(last), Number::BigInt(increment)) => { + print_seq_integers( + first, + increment, + last, + options.separator, + options.terminator, + options.widths, + padding, + ) + } + (first, last, increment) => print_seq( + first.into_f64(), + increment.into_f64(), + last.into_f64(), + largest_dec, + options.separator, + options.terminator, + options.widths, + padding, + ), + } + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) .setting(AppSettings::AllowLeadingHyphen) - .version(VERSION) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(OPT_SEPARATOR) .short("s") @@ -69,7 +179,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { Arg::with_name(OPT_TERMINATOR) .short("t") .long("terminator") - .help("Terminator character (defaults to separator)") + .help("Terminator character (defaults to \\n)") .takes_value(true) .number_of_values(1), ) @@ -84,100 +194,20 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .multiple(true) .takes_value(true) .allow_hyphen_values(true) - .max_values(3), + .max_values(3) + .required(true), ) - .get_matches_from(args); - - let numbers = matches.values_of(ARG_NUMBERS).unwrap().collect::>(); - - let mut options = SeqOptions { - separator: "\n".to_owned(), - terminator: None, - widths: false, - }; - options.separator = matches.value_of(OPT_SEPARATOR).unwrap_or("\n").to_string(); - options.terminator = matches.value_of(OPT_TERMINATOR).map(String::from); - options.widths = matches.is_present(OPT_WIDTHS); - - let mut largest_dec = 0; - let mut padding = 0; - let first = if numbers.len() > 1 { - let slice = &numbers[0][..]; - let len = slice.len(); - let dec = slice.find('.').unwrap_or(len); - largest_dec = len - dec; - padding = dec; - match parse_float(slice) { - Ok(n) => n, - Err(s) => { - show_error!("{}", s); - return 1; - } - } - } else { - 1.0 - }; - let increment = if numbers.len() > 2 { - let slice = &numbers[1][..]; - let len = slice.len(); - let dec = slice.find('.').unwrap_or(len); - largest_dec = cmp::max(largest_dec, len - dec); - padding = cmp::max(padding, dec); - match parse_float(slice) { - Ok(n) => n, - Err(s) => { - show_error!("{}", s); - return 1; - } - } - } else { - 1.0 - }; - if increment == 0.0 { - show_error!("increment value: '{}'", &numbers[1][..]); - return 1; - } - let last = { - let slice = &numbers[numbers.len() - 1][..]; - padding = cmp::max(padding, slice.find('.').unwrap_or_else(|| slice.len())); - match parse_float(slice) { - Ok(n) => n, - Err(s) => { - show_error!("{}", s); - return 1; - } - } - }; - if largest_dec > 0 { - largest_dec -= 1; - } - let separator = escape_sequences(&options.separator[..]); - let terminator = match options.terminator { - Some(term) => escape_sequences(&term[..]), - None => separator.clone(), - }; - print_seq( - first, - increment, - last, - largest_dec, - separator, - terminator, - options.widths, - padding, - ); - - 0 } -fn done_printing(next: f64, increment: f64, last: f64) -> bool { - if increment >= 0f64 { +fn done_printing(next: &T, increment: &T, last: &T) -> bool { + if increment >= &T::zero() { next > last } else { next < last } } +/// Floating point based code path #[allow(clippy::too_many_arguments)] fn print_seq( first: f64, @@ -191,7 +221,7 @@ fn print_seq( ) { let mut i = 0isize; let mut value = first + i as f64 * increment; - while !done_printing(value, increment, last) { + while !done_printing(&value, &increment, &last) { let istr = format!("{:.*}", largest_dec, value); let ilen = istr.len(); let before_dec = istr.find('.').unwrap_or(ilen); @@ -203,7 +233,7 @@ fn print_seq( print!("{}", istr); i += 1; value = first + i as f64 * increment; - if !done_printing(value, increment, last) { + if !done_printing(&value, &increment, &last) { print!("{}", separator); } } @@ -212,3 +242,33 @@ fn print_seq( } crash_if_err!(1, stdout().flush()); } + +/// BigInt based code path +fn print_seq_integers( + first: BigInt, + increment: BigInt, + last: BigInt, + separator: String, + terminator: String, + pad: bool, + padding: usize, +) { + let mut value = first; + let mut is_first_iteration = true; + while !done_printing(&value, &increment, &last) { + if !is_first_iteration { + print!("{}", separator); + } + is_first_iteration = false; + if pad { + print!("{number:>0width$}", number = value, width = padding); + } else { + print!("{}", value); + } + value += &increment; + } + + if !is_first_iteration { + print!("{}", terminator); + } +} diff --git a/src/uu/shred/Cargo.toml b/src/uu/shred/Cargo.toml index 72e6119a8..dda68b45b 100644 --- a/src/uu/shred/Cargo.toml +++ b/src/uu/shred/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_shred" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "shred ~ (uutils) hide former FILE contents with repeated overwrites" @@ -15,12 +15,12 @@ edition = "2018" path = "src/shred.rs" [dependencies] +clap = "2.33" filetime = "0.2.1" -getopts = "0.2.18" libc = "0.2.42" rand = "0.5" time = "0.1.40" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/shred/src/main.rs b/src/uu/shred/src/main.rs index 2880499eb..ea7a42f65 100644 --- a/src/uu/shred/src/main.rs +++ b/src/uu/shred/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_shred); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_shred); diff --git a/src/uu/shred/src/shred.rs b/src/uu/shred/src/shred.rs index 4946ed35d..90336ea95 100644 --- a/src/uu/shred/src/shred.rs +++ b/src/uu/shred/src/shred.rs @@ -6,8 +6,9 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore (ToDO) NAMESET FILESIZE fstab coeff journaling writeback REiser journaled +// spell-checker:ignore (words) writeback wipesync +use clap::{crate_version, App, Arg}; use rand::{Rng, ThreadRng}; use std::cell::{Cell, RefCell}; use std::fs; @@ -16,14 +17,14 @@ use std::io; use std::io::prelude::*; use std::io::SeekFrom; use std::path::{Path, PathBuf}; +use uucore::InvalidEncodingHandling; #[macro_use] extern crate uucore; static NAME: &str = "shred"; -static VERSION_STR: &str = "1.0.0"; const BLOCK_SIZE: usize = 512; -const NAMESET: &str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_."; +const NAME_CHARSET: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_."; // Patterns as shown in the GNU coreutils shred implementation const PATTERNS: [&[u8]; 22] = [ @@ -57,10 +58,10 @@ enum PassType<'a> { Random, } -// Used to generate all possible filenames of a certain length using NAMESET as an alphabet +// Used to generate all possible filenames of a certain length using NAME_CHARSET as an alphabet struct FilenameGenerator { name_len: usize, - nameset_indices: RefCell>, // Store the indices of the letters of our filename in NAMESET + name_charset_indices: RefCell>, // Store the indices of the letters of our filename in NAME_CHARSET exhausted: Cell, } @@ -69,7 +70,7 @@ impl FilenameGenerator { let indices: Vec = vec![0; name_len]; FilenameGenerator { name_len, - nameset_indices: RefCell::new(indices), + name_charset_indices: RefCell::new(indices), exhausted: Cell::new(false), } } @@ -83,25 +84,25 @@ impl Iterator for FilenameGenerator { return None; } - let mut nameset_indices = self.nameset_indices.borrow_mut(); + let mut name_charset_indices = self.name_charset_indices.borrow_mut(); // Make the return value, then increment let mut ret = String::new(); - for i in nameset_indices.iter() { - let c: char = NAMESET.chars().nth(*i).unwrap(); + for i in name_charset_indices.iter() { + let c = char::from(NAME_CHARSET[*i]); ret.push(c); } - if nameset_indices[0] == NAMESET.len() - 1 { + if name_charset_indices[0] == NAME_CHARSET.len() - 1 { self.exhausted.set(true) } // Now increment the least significant index for i in (0..self.name_len).rev() { - if nameset_indices[i] == NAMESET.len() - 1 { - nameset_indices[i] = 0; // Carry the 1 + if name_charset_indices[i] == NAME_CHARSET.len() - 1 { + name_charset_indices[i] = 0; // Carry the 1 continue; } else { - nameset_indices[i] += 1; + name_charset_indices[i] += 1; break; } } @@ -162,16 +163,14 @@ impl<'a> BytesGenerator<'a> { return None; } - let this_block_size = { - if !self.exact { + let this_block_size = if !self.exact { + self.block_size + } else { + let bytes_left = self.total_bytes - self.bytes_generated.get(); + if bytes_left >= self.block_size as u64 { self.block_size } else { - let bytes_left = self.total_bytes - self.bytes_generated.get(); - if bytes_left >= self.block_size as u64 { - self.block_size - } else { - (bytes_left % self.block_size as u64) as usize - } + (bytes_left % self.block_size as u64) as usize } }; @@ -183,12 +182,10 @@ impl<'a> BytesGenerator<'a> { rng.fill(bytes); } PassType::Pattern(pattern) => { - let skip = { - if self.bytes_generated.get() == 0 { - 0 - } else { - (pattern.len() as u64 % self.bytes_generated.get()) as usize - } + let skip = if self.bytes_generated.get() == 0 { + 0 + } else { + (pattern.len() as u64 % self.bytes_generated.get()) as usize }; // Copy the pattern in chunks rather than simply one byte at a time @@ -212,136 +209,182 @@ impl<'a> BytesGenerator<'a> { } } +static ABOUT: &str = "Overwrite the specified FILE(s) repeatedly, in order to make it harder\n\ +for even very expensive hardware probing to recover the data. +"; + +fn get_usage() -> String { + format!("{} [OPTION]... FILE...", executable!()) +} + +static AFTER_HELP: &str = + "Delete FILE(s) if --remove (-u) is specified. The default is not to remove\n\ + the files because it is common to operate on device files like /dev/hda,\n\ + and those files usually should not be removed.\n\ + \n\ + CAUTION: Note that shred relies on a very important assumption:\n\ + that the file system overwrites data in place. This is the traditional\n\ + way to do things, but many modern file system designs do not satisfy this\n\ + assumption. The following are examples of file systems on which shred is\n\ + not effective, or is not guaranteed to be effective in all file system modes:\n\ + \n\ + * log-structured or journal file systems, such as those supplied with\n\ + AIX and Solaris (and JFS, ReiserFS, XFS, Ext3, etc.)\n\ + \n\ + * file systems that write redundant data and carry on even if some writes\n\ + fail, such as RAID-based file systems\n\ + \n\ + * file systems that make snapshots, such as Network Appliance's NFS server\n\ + \n\ + * file systems that cache in temporary locations, such as NFS\n\ + version 3 clients\n\ + \n\ + * compressed file systems\n\ + \n\ + In the case of ext3 file systems, the above disclaimer applies\n\ + and shred is thus of limited effectiveness) only in data=journal mode,\n\ + which journals file data in addition to just metadata. In both the\n\ + data=ordered (default) and data=writeback modes, shred works as usual.\n\ + Ext3 journal modes can be changed by adding the data=something option\n\ + to the mount options for a particular file system in the /etc/fstab file,\n\ + as documented in the mount man page (man mount).\n\ + \n\ + In addition, file system backups and remote mirrors may contain copies\n\ + of the file that cannot be removed, and that will allow a shredded file\n\ + to be recovered later.\n\ + "; + +pub mod options { + pub const FORCE: &str = "force"; + pub const FILE: &str = "file"; + pub const ITERATIONS: &str = "iterations"; + pub const SIZE: &str = "size"; + pub const REMOVE: &str = "remove"; + pub const VERBOSE: &str = "verbose"; + pub const EXACT: &str = "exact"; + pub const ZERO: &str = "zero"; +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let mut opts = getopts::Options::new(); + let usage = get_usage(); - // TODO: Add force option - opts.optopt( - "n", - "iterations", - "overwrite N times instead of the default (3)", - "N", - ); - opts.optopt( - "s", - "size", - "shred this many bytes (suffixes like K, M, G accepted)", - "FILESIZE", - ); - opts.optflag( - "u", - "remove", - "truncate and remove the file after overwriting; See below", - ); - opts.optflag("v", "verbose", "show progress"); - opts.optflag( - "x", - "exact", - "do not round file sizes up to the next full block; \ - this is the default for non-regular files", - ); - opts.optflag( - "z", - "zero", - "add a final overwrite with zeros to hide shredding", - ); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("", "version", "output version information and exit"); + let app = uu_app().usage(&usage[..]); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(e) => panic!("Invalid options\n{}", e), + let matches = app.get_matches_from(args); + + let mut errs: Vec = vec![]; + + if !matches.is_present(options::FILE) { + show_error!("Missing an argument"); + show_error!("For help, try '{} --help'", NAME); + return 0; + } + + let iterations = match matches.value_of(options::ITERATIONS) { + Some(s) => match s.parse::() { + Ok(u) => u, + Err(_) => { + errs.push(format!("invalid number of passes: '{}'", s)); + 0 + } + }, + None => unreachable!(), }; - if matches.opt_present("help") { - show_help(&opts); - return 0; - } else if matches.opt_present("version") { - println!("{} {}", NAME, VERSION_STR); - return 0; - } else if matches.free.is_empty() { - println!("{}: Missing an argument", NAME); - println!("For help, try '{} --help'", NAME); - return 0; - } else { - let iterations = match matches.opt_str("iterations") { - Some(s) => match s.parse::() { - Ok(u) => u, - Err(_) => { - println!("{}: Invalid number of passes", NAME); - return 1; - } - }, - None => 3, - }; - let remove = matches.opt_present("remove"); - let size = get_size(matches.opt_str("size")); - let exact = matches.opt_present("exact") && size.is_none(); // if -s is given, ignore -x - let zero = matches.opt_present("zero"); - let verbose = matches.opt_present("verbose"); - for path_str in matches.free.into_iter() { - wipe_file(&path_str, iterations, remove, size, exact, zero, verbose); + // TODO: implement --remove HOW + // The optional HOW parameter indicates how to remove a directory entry: + // - 'unlink' => use a standard unlink call. + // - 'wipe' => also first obfuscate bytes in the name. + // - 'wipesync' => also sync each obfuscated byte to disk. + // The default mode is 'wipesync', but note it can be expensive. + + // TODO: implement --random-source + + let force = matches.is_present(options::FORCE); + let remove = matches.is_present(options::REMOVE); + let size_arg = matches.value_of(options::SIZE).map(|s| s.to_string()); + let size = get_size(size_arg); + let exact = matches.is_present(options::EXACT) && size.is_none(); // if -s is given, ignore -x + let zero = matches.is_present(options::ZERO); + let verbose = matches.is_present(options::VERBOSE); + + if !errs.is_empty() { + show_error!("Invalid arguments supplied."); + for message in errs { + show_error!("{}", message); } + return 1; + } + + for path_str in matches.values_of(options::FILE).unwrap() { + wipe_file( + path_str, iterations, remove, size, exact, zero, verbose, force, + ); } 0 } -fn show_help(opts: &getopts::Options) { - println!("Usage: {} [OPTION]... FILE...", NAME); - println!( - "Overwrite the specified FILE(s) repeatedly, in order to make it harder \ - for even very expensive hardware probing to recover the data." - ); - println!("{}", opts.usage("")); - println!("Delete FILE(s) if --remove (-u) is specified. The default is not to remove"); - println!("the files because it is common to operate on device files like /dev/hda,"); - println!("and those files usually should not be removed."); - println!(); - println!( - "CAUTION: Note that {} relies on a very important assumption:", - NAME - ); - println!("that the file system overwrites data in place. This is the traditional"); - println!("way to do things, but many modern file system designs do not satisfy this"); - println!( - "assumption. The following are examples of file systems on which {} is", - NAME - ); - println!("not effective, or is not guaranteed to be effective in all file system modes:"); - println!(); - println!("* log-structured or journaled file systems, such as those supplied with"); - println!("AIX and Solaris (and JFS, ReiserFS, XFS, Ext3, etc.)"); - println!(); - println!("* file systems that write redundant data and carry on even if some writes"); - println!("fail, such as RAID-based file systems"); - println!(); - println!("* file systems that make snapshots, such as Network Appliance's NFS server"); - println!(); - println!("* file systems that cache in temporary locations, such as NFS"); - println!("version 3 clients"); - println!(); - println!("* compressed file systems"); - println!(); - println!("In the case of ext3 file systems, the above disclaimer applies"); - println!( - "(and {} is thus of limited effectiveness) only in data=journal mode,", - NAME - ); - println!("which journals file data in addition to just metadata. In both the"); - println!( - "data=ordered (default) and data=writeback modes, {} works as usual.", - NAME - ); - println!("Ext3 journaling modes can be changed by adding the data=something option"); - println!("to the mount options for a particular file system in the /etc/fstab file,"); - println!("as documented in the mount man page (man mount)."); - println!(); - println!("In addition, file system backups and remote mirrors may contain copies"); - println!("of the file that cannot be removed, and that will allow a shredded file"); - println!("to be recovered later."); +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .after_help(AFTER_HELP) + .arg( + Arg::with_name(options::FORCE) + .long(options::FORCE) + .short("f") + .help("change permissions to allow writing if necessary"), + ) + .arg( + Arg::with_name(options::ITERATIONS) + .long(options::ITERATIONS) + .short("n") + .help("overwrite N times instead of the default (3)") + .value_name("NUMBER") + .default_value("3"), + ) + .arg( + Arg::with_name(options::SIZE) + .long(options::SIZE) + .short("s") + .takes_value(true) + .value_name("N") + .help("shred this many bytes (suffixes like K, M, G accepted)"), + ) + .arg( + Arg::with_name(options::REMOVE) + .short("u") + .long(options::REMOVE) + .help("truncate and remove file after overwriting; See below"), + ) + .arg( + Arg::with_name(options::VERBOSE) + .long(options::VERBOSE) + .short("v") + .help("show progress"), + ) + .arg( + Arg::with_name(options::EXACT) + .long(options::EXACT) + .short("x") + .help( + "do not round file sizes up to the next full block;\n\ + this is the default for non-regular files", + ), + ) + .arg( + Arg::with_name(options::ZERO) + .long(options::ZERO) + .short("z") + .help("add a final overwrite with zeros to hide shredding"), + ) + // Positional arguments + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) } // TODO: Add support for all postfixes here up to and including EiB @@ -367,7 +410,7 @@ fn get_size(size_str_opt: Option) -> Option { _ => 1u64, }; - let coeff = match size_str.parse::() { + let coefficient = match size_str.parse::() { Ok(u) => u, Err(_) => { println!("{}: {}: Invalid file size", NAME, size_str_opt.unwrap()); @@ -375,7 +418,7 @@ fn get_size(size_str_opt: Option) -> Option { } }; - Some(coeff * unit) + Some(coefficient * unit) } fn pass_name(pass_type: PassType) -> String { @@ -394,6 +437,7 @@ fn pass_name(pass_type: PassType) -> String { } } +#[allow(clippy::too_many_arguments)] fn wipe_file( path_str: &str, n_passes: usize, @@ -402,18 +446,37 @@ fn wipe_file( exact: bool, zero: bool, verbose: bool, + force: bool, ) { // Get these potential errors out of the way first let path: &Path = Path::new(path_str); if !path.exists() { - println!("{}: {}: No such file or directory", NAME, path.display()); + show_error!("{}: No such file or directory", path.display()); return; } if !path.is_file() { - println!("{}: {}: Not a file", NAME, path.display()); + show_error!("{}: Not a file", path.display()); return; } + // If force is true, set file permissions to not-readonly. + if force { + let metadata = match fs::metadata(path) { + Ok(m) => m, + Err(e) => { + show_error!("{}", e); + return; + } + }; + + let mut perms = metadata.permissions(); + perms.set_readonly(false); + if let Err(e) = fs::set_permissions(path, perms) { + show_error!("{}", e); + return; + } + } + // Fill up our pass sequence let mut pass_sequence: Vec = Vec::new(); @@ -452,11 +515,13 @@ fn wipe_file( { let total_passes: usize = pass_sequence.len(); - let mut file: File = OpenOptions::new() - .write(true) - .truncate(false) - .open(path) - .expect("Failed to open file for writing"); + let mut file: File = match OpenOptions::new().write(true).truncate(false).open(path) { + Ok(f) => f, + Err(e) => { + show_error!("{}: failed to open for writing: {}", path.display(), e); + return; + } + }; // NOTE: it does not really matter what we set for total_bytes and gen_type here, so just // use bogus values @@ -486,14 +551,23 @@ fn wipe_file( } } // size is an optional argument for exactly how many bytes we want to shred - do_pass(&mut file, path, &mut generator, *pass_type, size) - .expect("File write pass failed"); + match do_pass(&mut file, path, &mut generator, *pass_type, size) { + Ok(_) => {} + Err(e) => { + show_error!("{}: File write pass failed: {}", path.display(), e); + } + } // Ignore failed writes; just keep trying } } if remove { - do_remove(path, path_str, verbose).expect("Failed to remove file"); + match do_remove(path, path_str, verbose) { + Ok(_) => {} + Err(e) => { + show_error!("{}: failed to remove file: {}", path.display(), e); + } + } } } @@ -584,7 +658,7 @@ fn do_remove(path: &Path, orig_filename: &str, verbose: bool) -> Result<(), io:: println!("{}: {}: removing", NAME, orig_filename); } - let renamed_path: Option = wipe_name(&path, verbose); + let renamed_path: Option = wipe_name(path, verbose); if let Some(rp) = renamed_path { fs::remove_file(rp)?; } diff --git a/src/uu/shuf/Cargo.toml b/src/uu/shuf/Cargo.toml index b78d4fc04..dbf559454 100644 --- a/src/uu/shuf/Cargo.toml +++ b/src/uu/shuf/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_shuf" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "shuf ~ (uutils) display random permutations of input lines" @@ -15,9 +15,9 @@ edition = "2018" path = "src/shuf.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" rand = "0.5" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/shuf/src/main.rs b/src/uu/shuf/src/main.rs index 1ecff5d21..fc6e2b4ae 100644 --- a/src/uu/shuf/src/main.rs +++ b/src/uu/shuf/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_shuf); // spell-checker:ignore procs uucore shuf +uucore_procs::main!(uu_shuf); diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index 278e872fb..4690d1c6e 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -10,138 +10,175 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use rand::Rng; use std::fs::File; use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write}; -use std::usize::MAX as MAX_USIZE; +use uucore::InvalidEncodingHandling; enum Mode { - Default, - Echo, + Default(String), + Echo(Vec), InputRange((usize, usize)), } static NAME: &str = "shuf"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static USAGE: &str = r#"shuf [OPTION]... [FILE] + or: shuf -e [OPTION]... [ARG]... + or: shuf -i LO-HI [OPTION]... +Write a random permutation of the input lines to standard output. + +With no FILE, or when FILE is -, read standard input. +"#; +static TEMPLATE: &str = "Usage: {usage}\nMandatory arguments to long options are mandatory for short options too.\n{unified}"; + +struct Options { + head_count: usize, + output: Option, + random_source: Option, + repeat: bool, + sep: u8, +} + +mod options { + pub static ECHO: &str = "echo"; + pub static INPUT_RANGE: &str = "input-range"; + pub static HEAD_COUNT: &str = "head-count"; + pub static OUTPUT: &str = "output"; + pub static RANDOM_SOURCE: &str = "random-source"; + pub static REPEAT: &str = "repeat"; + pub static ZERO_TERMINATED: &str = "zero-terminated"; + pub static FILE: &str = "file"; +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = getopts::Options::new(); - opts.optflag("e", "echo", "treat each ARG as an input line"); - opts.optopt( - "i", - "input-range", - "treat each number LO through HI as an input line", - "LO-HI", - ); - opts.optopt("n", "head-count", "output at most COUNT lines", "COUNT"); - opts.optopt( - "o", - "output", - "write result to FILE instead of standard output", - "FILE", - ); - opts.optopt("", "random-source", "get random bytes from FILE", "FILE"); - opts.optflag("r", "repeat", "output lines can be repeated"); - opts.optflag("z", "zero-terminated", "end lines with 0 byte, not newline"); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - let mut matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "{}", f), - }; - if matches.opt_present("help") { - let msg = format!( - "{0} {1} + let matches = uu_app().get_matches_from(args); -Usage: - {0} [OPTION]... [FILE] - {0} -e [OPTION]... [ARG]... - {0} -i LO-HI [OPTION]... - -Write a random permutation of the input lines to standard output. -With no FILE, or when FILE is -, read standard input.", - NAME, VERSION - ); - print!("{}", opts.usage(&msg)); - } else if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); + let mode = if let Some(args) = matches.values_of(options::ECHO) { + Mode::Echo(args.map(String::from).collect()) + } else if let Some(range) = matches.value_of(options::INPUT_RANGE) { + match parse_range(range) { + Ok(m) => Mode::InputRange(m), + Err(msg) => { + crash!(1, "{}", msg); + } + } } else { - let echo = matches.opt_present("echo"); - let mode = match matches.opt_str("input-range") { - Some(range) => { - if echo { - show_error!("cannot specify more than one mode"); - return 1; - } - match parse_range(range) { - Ok(m) => Mode::InputRange(m), - Err(msg) => { - crash!(1, "{}", msg); - } - } - } - None => { - if echo { - Mode::Echo - } else { - if matches.free.is_empty() { - matches.free.push("-".to_owned()); - } else if matches.free.len() > 1 { - show_error!("extra operand '{}'", &matches.free[1][..]); - } - Mode::Default - } - } - }; - let repeat = matches.opt_present("repeat"); - let sep = if matches.opt_present("zero-terminated") { - 0x00_u8 - } else { - 0x0a_u8 - }; - let count = match matches.opt_str("head-count") { - Some(cnt) => match cnt.parse::() { + Mode::Default(matches.value_of(options::FILE).unwrap_or("-").to_string()) + }; + + let options = Options { + head_count: match matches.value_of(options::HEAD_COUNT) { + Some(count) => match count.parse::() { Ok(val) => val, - Err(e) => { - show_error!("'{}' is not a valid count: {}", cnt, e); + Err(_) => { + show_error!("invalid line count: '{}'", count); return 1; } }, - None => MAX_USIZE, - }; - let output = matches.opt_str("output"); - let random = matches.opt_str("random-source"); + None => std::usize::MAX, + }, + output: matches.value_of(options::OUTPUT).map(String::from), + random_source: matches.value_of(options::RANDOM_SOURCE).map(String::from), + repeat: matches.is_present(options::REPEAT), + sep: if matches.is_present(options::ZERO_TERMINATED) { + 0x00_u8 + } else { + 0x0a_u8 + }, + }; - match mode { - Mode::Echo => { - // XXX: this doesn't correctly handle non-UTF-8 cmdline args - let mut evec = matches - .free - .iter() - .map(String::as_bytes) - .collect::>(); - find_seps(&mut evec, sep); - shuf_bytes(&mut evec, repeat, count, sep, output, random); - } - Mode::InputRange((b, e)) => { - let rvec = (b..e).map(|x| format!("{}", x)).collect::>(); - let mut rvec = rvec.iter().map(String::as_bytes).collect::>(); - shuf_bytes(&mut rvec, repeat, count, sep, output, random); - } - Mode::Default => { - let fdata = read_input_file(&matches.free[0][..]); - let mut fdata = vec![&fdata[..]]; - find_seps(&mut fdata, sep); - shuf_bytes(&mut fdata, repeat, count, sep, output, random); - } + match mode { + Mode::Echo(args) => { + let mut evec = args.iter().map(String::as_bytes).collect::>(); + find_seps(&mut evec, options.sep); + shuf_bytes(&mut evec, options); + } + Mode::InputRange((b, e)) => { + let rvec = (b..e).map(|x| format!("{}", x)).collect::>(); + let mut rvec = rvec.iter().map(String::as_bytes).collect::>(); + shuf_bytes(&mut rvec, options); + } + Mode::Default(filename) => { + let fdata = read_input_file(&filename); + let mut fdata = vec![&fdata[..]]; + find_seps(&mut fdata, options.sep); + shuf_bytes(&mut fdata, options); } } 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .template(TEMPLATE) + .usage(USAGE) + .arg( + Arg::with_name(options::ECHO) + .short("e") + .long(options::ECHO) + .takes_value(true) + .value_name("ARG") + .help("treat each ARG as an input line") + .multiple(true) + .use_delimiter(false) + .min_values(0) + .conflicts_with(options::INPUT_RANGE), + ) + .arg( + Arg::with_name(options::INPUT_RANGE) + .short("i") + .long(options::INPUT_RANGE) + .takes_value(true) + .value_name("LO-HI") + .help("treat each number LO through HI as an input line") + .conflicts_with(options::FILE), + ) + .arg( + Arg::with_name(options::HEAD_COUNT) + .short("n") + .long(options::HEAD_COUNT) + .takes_value(true) + .value_name("COUNT") + .help("output at most COUNT lines"), + ) + .arg( + Arg::with_name(options::OUTPUT) + .short("o") + .long(options::OUTPUT) + .takes_value(true) + .value_name("FILE") + .help("write result to FILE instead of standard output"), + ) + .arg( + Arg::with_name(options::RANDOM_SOURCE) + .long(options::RANDOM_SOURCE) + .takes_value(true) + .value_name("FILE") + .help("get random bytes from FILE"), + ) + .arg( + Arg::with_name(options::REPEAT) + .short("r") + .long(options::REPEAT) + .help("output lines can be repeated"), + ) + .arg( + Arg::with_name(options::ZERO_TERMINATED) + .short("z") + .long(options::ZERO_TERMINATED) + .help("line delimiter is NUL, not newline"), + ) + .arg(Arg::with_name(options::FILE).takes_value(true)) +} + fn read_input_file(filename: &str) -> Vec { let mut file = BufReader::new(if filename == "-" { Box::new(stdin()) as Box @@ -193,15 +230,8 @@ fn find_seps(data: &mut Vec<&[u8]>, sep: u8) { } } -fn shuf_bytes( - input: &mut Vec<&[u8]>, - repeat: bool, - count: usize, - sep: u8, - output: Option, - random: Option, -) { - let mut output = BufWriter::new(match output { +fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) { + let mut output = BufWriter::new(match opts.output { None => Box::new(stdout()) as Box, Some(s) => match File::create(&s[..]) { Ok(f) => Box::new(f) as Box, @@ -209,7 +239,7 @@ fn shuf_bytes( }, }); - let mut rng = match random { + let mut rng = match opts.random_source { Some(r) => WrappedRng::RngFile(rand::read::ReadRng::new(match File::open(&r[..]) { Ok(f) => f, Err(e) => crash!(1, "failed to open random source '{}': {}", &r[..], e), @@ -225,7 +255,7 @@ fn shuf_bytes( len_mod <<= 1; } - let mut count = count; + let mut count = opts.head_count; while count > 0 && !input.is_empty() { let mut r = input.len(); while r >= input.len() { @@ -237,11 +267,11 @@ fn shuf_bytes( .write_all(input[r]) .unwrap_or_else(|e| crash!(1, "write failed: {}", e)); output - .write_all(&[sep]) + .write_all(&[opts.sep]) .unwrap_or_else(|e| crash!(1, "write failed: {}", e)); // if we do not allow repeats, remove the chosen value from the input vector - if !repeat { + if !opts.repeat { // shrink the mask if we will drop below a power of 2 if input.len() % 2 == 0 && len_mod > 2 { len_mod >>= 1; @@ -253,19 +283,17 @@ fn shuf_bytes( } } -fn parse_range(input_range: String) -> Result<(usize, usize), String> { +fn parse_range(input_range: &str) -> Result<(usize, usize), String> { let split: Vec<&str> = input_range.split('-').collect(); if split.len() != 2 { - Err("invalid range format".to_owned()) + Err(format!("invalid input range: '{}'", input_range)) } else { - let begin = match split[0].parse::() { - Ok(m) => m, - Err(e) => return Err(format!("{} is not a valid number: {}", split[0], e)), - }; - let end = match split[1].parse::() { - Ok(m) => m, - Err(e) => return Err(format!("{} is not a valid number: {}", split[1], e)), - }; + let begin = split[0] + .parse::() + .map_err(|_| format!("invalid input range: '{}'", split[0]))?; + let end = split[1] + .parse::() + .map_err(|_| format!("invalid input range: '{}'", split[1]))?; Ok((begin, end + 1)) } } diff --git a/src/uu/sleep/Cargo.toml b/src/uu/sleep/Cargo.toml index 0ac5d6519..618ea7e28 100644 --- a/src/uu/sleep/Cargo.toml +++ b/src/uu/sleep/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_sleep" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "sleep ~ (uutils) pause for DURATION" @@ -16,7 +16,7 @@ path = "src/sleep.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["parse_time"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/sleep/src/main.rs b/src/uu/sleep/src/main.rs index 46ecd0969..16c3100aa 100644 --- a/src/uu/sleep/src/main.rs +++ b/src/uu/sleep/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_sleep); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_sleep); diff --git a/src/uu/sleep/src/sleep.rs b/src/uu/sleep/src/sleep.rs index 5c1f06e5e..ada3336df 100644 --- a/src/uu/sleep/src/sleep.rs +++ b/src/uu/sleep/src/sleep.rs @@ -11,9 +11,8 @@ extern crate uucore; use std::thread; use std::time::Duration; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Pause for NUMBER seconds."; static LONG_HELP: &str = "Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations @@ -36,10 +35,20 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + if let Some(values) = matches.values_of(options::NUMBER) { + let numbers = values.collect(); + sleep(numbers); + } + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .after_help(LONG_HELP) .arg( Arg::with_name(options::NUMBER) @@ -50,14 +59,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .multiple(true) .required(true), ) - .get_matches_from(args); - - if let Some(values) = matches.values_of(options::NUMBER) { - let numbers = values.collect(); - sleep(numbers); - } - - 0 } fn sleep(args: Vec<&str>) { diff --git a/src/uu/sort/BENCHMARKING.md b/src/uu/sort/BENCHMARKING.md new file mode 100644 index 000000000..77186318a --- /dev/null +++ b/src/uu/sort/BENCHMARKING.md @@ -0,0 +1,157 @@ +# Benchmarking sort + + + +Most of the time when sorting is spent comparing lines. The comparison functions however differ based +on which arguments are passed to `sort`, therefore it is important to always benchmark multiple scenarios. +This is an overview over what was benchmarked, and if you make changes to `sort`, you are encouraged to check +how performance was affected for the workloads listed below. Feel free to add other workloads to the +list that we should improve / make sure not to regress. + +Run `cargo build --release` before benchmarking after you make a change! + +## Sorting a wordlist + +- Get a wordlist, for example with [words]() on Linux. The exact wordlist + doesn't matter for performance comparisons. In this example I'm using `/usr/share/dict/american-english` as the wordlist. +- Shuffle the wordlist by running `sort -R /usr/share/dict/american-english > shuffled_wordlist.txt`. +- Benchmark sorting the wordlist with hyperfine: `hyperfine "target/release/coreutils sort shuffled_wordlist.txt -o output.txt"`. + +## Sorting a wordlist with ignore_case + +- Same wordlist as above +- Benchmark sorting the wordlist ignoring the case with hyperfine: `hyperfine "target/release/coreutils sort shuffled_wordlist.txt -f -o output.txt"`. + +## Sorting numbers + +- Generate a list of numbers: `seq 0 100000 | sort -R > shuffled_numbers.txt`. +- Benchmark numeric sorting with hyperfine: `hyperfine "target/release/coreutils sort shuffled_numbers.txt -n -o output.txt"`. + +## Sorting numbers with -g + +- Same list of numbers as above. +- Benchmark numeric sorting with hyperfine: `hyperfine "target/release/coreutils sort shuffled_numbers.txt -g -o output.txt"`. + +## Sorting numbers with SI prefixes + +- Generate a list of numbers: +
+ Rust script + + ## Cargo.toml + + ```toml + [dependencies] + rand = "0.8.3" + ``` + + ## main.rs + + ```rust + use rand::prelude::*; + fn main() { + let suffixes = ['k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']; + let mut rng = thread_rng(); + for _ in 0..100000 { + println!( + "{}{}", + rng.gen_range(0..1000000), + suffixes.choose(&mut rng).unwrap() + ) + } + } + + ``` + + ## running + + `cargo run > shuffled_numbers_si.txt` + +
+ +- Benchmark numeric sorting with hyperfine: `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt"`. + +## External sorting + +Try running commands with the `-S` option set to an amount of memory to be used, such as `1M`. Additionally, you could try sorting +huge files (ideally multiple Gigabytes) with `-S` (or without `-S` to benchmark with our default value). +Creating such a large file can be achieved by running `cat shuffled_wordlist.txt | sort -R >> shuffled_wordlist.txt` +multiple times (this will add the contents of `shuffled_wordlist.txt` to itself). +Example: Run `hyperfine './target/release/coreutils sort shuffled_wordlist.txt -S 1M' 'sort shuffled_wordlist.txt -S 1M'` + +## Merging + +"Merge" sort merges already sorted files. It is a sub-step of external sorting, so benchmarking it separately may be helpful. + +- Splitting `shuffled_wordlist.txt` can be achieved by running `split shuffled_wordlist.txt shuffled_wordlist_slice_ --additional-suffix=.txt` +- Sort each part by running `for f in shuffled_wordlist_slice_*; do sort $f -o $f; done` +- Benchmark merging by running `hyperfine "target/release/coreutils sort -m shuffled_wordlist_slice_*"` + +## Check + +When invoked with -c, we simply check if the input is already ordered. The input for benchmarking should be an already sorted file. + +- Benchmark checking by running `hyperfine "target/release/coreutils sort -c sorted_wordlist.txt"` + +## Stdout and stdin performance + +Try to run the above benchmarks by piping the input through stdin (standard input) and redirect the +output through stdout (standard output): + +- Remove the input file from the arguments and add `cat [input_file] | ` at the beginning. +- Remove `-o output.txt` and add `> output.txt` at the end. + +Example: `hyperfine "target/release/coreutils sort shuffled_numbers.txt -n -o output.txt"` becomes +`hyperfine "cat shuffled_numbers.txt | target/release/coreutils sort -n > output.txt` + +- Check that performance is similar to the original benchmark. + +## Comparing with GNU sort + +Hyperfine accepts multiple commands to run and will compare them. To compare performance with GNU sort +duplicate the string you passed to hyperfine but remove the `target/release/coreutils` bit from it. + +Example: `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt"` becomes +`hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt" "sort shuffled_numbers_si.txt -h -o output.txt"` +(This assumes GNU sort is installed as `sort`) + +## Memory and CPU usage + +The above benchmarks use hyperfine to measure the speed of sorting. There are however other useful metrics to determine overall +resource usage. One way to measure them is the `time` command. This is not to be confused with the `time` that is built in to the bash shell. +You may have to install `time` first, then you have to run it with `/bin/time -v` to give it precedence over the built in `time`. + +
+ Example output + + Command being timed: "target/release/coreutils sort shuffled_numbers.txt" + User time (seconds): 0.10 + System time (seconds): 0.00 + Percent of CPU this job got: 365% + Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.02 + Average shared text size (kbytes): 0 + Average unshared data size (kbytes): 0 + Average stack size (kbytes): 0 + Average total size (kbytes): 0 + Maximum resident set size (kbytes): 25360 + Average resident set size (kbytes): 0 + Major (requiring I/O) page faults: 0 + Minor (reclaiming a frame) page faults: 5802 + Voluntary context switches: 462 + Involuntary context switches: 73 + Swaps: 0 + File system inputs: 1184 + File system outputs: 0 + Socket messages sent: 0 + Socket messages received: 0 + Signals delivered: 0 + Page size (bytes): 4096 + Exit status: 0 + +
+ +Useful metrics to look at could be: + +- User time +- Percent of CPU this job got +- Maximum resident set size diff --git a/src/uu/sort/Cargo.toml b/src/uu/sort/Cargo.toml index 5158f6e52..f06610248 100644 --- a/src/uu/sort/Cargo.toml +++ b/src/uu/sort/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_sort" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "sort ~ (uutils) sort input lines" @@ -15,10 +15,19 @@ edition = "2018" path = "src/sort.rs" [dependencies] +binary-heap-plus = "0.4.1" clap = "2.33" -itertools = "0.8.0" +compare = "0.1.0" +fnv = "1.0.7" +itertools = "0.10.0" +memchr = "2.4.0" +ouroboros = "0.9.3" +rand = "0.7" +rayon = "1.5" semver = "0.9.0" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +tempfile = "3" +unicode-width = "0.1.8" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/sort/src/check.rs b/src/uu/sort/src/check.rs new file mode 100644 index 000000000..f1cd22686 --- /dev/null +++ b/src/uu/sort/src/check.rs @@ -0,0 +1,109 @@ +// * This file is part of the uutils coreutils package. +// * +// * (c) Michael Debertol +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +//! Check if a file is ordered + +use crate::{ + chunks::{self, Chunk, RecycledChunk}, + compare_by, open, GlobalSettings, +}; +use itertools::Itertools; +use std::{ + cmp::Ordering, + io::Read, + iter, + sync::mpsc::{sync_channel, Receiver, SyncSender}, + thread, +}; + +/// Check if the file at `path` is ordered. +/// +/// # Returns +/// +/// The code we should exit with. +pub fn check(path: &str, settings: &GlobalSettings) -> i32 { + let file = open(path); + let (recycled_sender, recycled_receiver) = sync_channel(2); + let (loaded_sender, loaded_receiver) = sync_channel(2); + thread::spawn({ + let settings = settings.clone(); + move || reader(file, recycled_receiver, loaded_sender, &settings) + }); + for _ in 0..2 { + let _ = recycled_sender.send(RecycledChunk::new(100 * 1024)); + } + + let mut prev_chunk: Option = None; + let mut line_idx = 0; + for chunk in loaded_receiver.iter() { + line_idx += 1; + if let Some(prev_chunk) = prev_chunk.take() { + // Check if the first element of the new chunk is greater than the last + // element from the previous chunk + let prev_last = prev_chunk.lines().last().unwrap(); + let new_first = chunk.lines().first().unwrap(); + + if compare_by( + prev_last, + new_first, + settings, + prev_chunk.line_data(), + chunk.line_data(), + ) == Ordering::Greater + { + if !settings.check_silent { + println!("sort: {}:{}: disorder: {}", path, line_idx, new_first.line); + } + return 1; + } + let _ = recycled_sender.send(prev_chunk.recycle()); + } + + for (a, b) in chunk.lines().iter().tuple_windows() { + line_idx += 1; + if compare_by(a, b, settings, chunk.line_data(), chunk.line_data()) == Ordering::Greater + { + if !settings.check_silent { + println!("sort: {}:{}: disorder: {}", path, line_idx, b.line); + } + return 1; + } + } + + prev_chunk = Some(chunk); + } + 0 +} + +/// The function running on the reader thread. +fn reader( + mut file: Box, + receiver: Receiver, + sender: SyncSender, + settings: &GlobalSettings, +) { + let mut carry_over = vec![]; + for recycled_chunk in receiver.iter() { + let should_continue = chunks::read( + &sender, + recycled_chunk, + None, + &mut carry_over, + &mut file, + &mut iter::empty(), + if settings.zero_terminated { + b'\0' + } else { + b'\n' + }, + settings, + ); + if !should_continue { + break; + } + } +} diff --git a/src/uu/sort/src/chunks.rs b/src/uu/sort/src/chunks.rs new file mode 100644 index 000000000..9e9d212c2 --- /dev/null +++ b/src/uu/sort/src/chunks.rs @@ -0,0 +1,319 @@ +// * This file is part of the uutils coreutils package. +// * +// * (c) Michael Debertol +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +//! Utilities for reading files as chunks. + +use std::{ + io::{ErrorKind, Read}, + sync::mpsc::SyncSender, +}; + +use memchr::memchr_iter; +use ouroboros::self_referencing; + +use crate::{numeric_str_cmp::NumInfo, GeneralF64ParseResult, GlobalSettings, Line}; + +/// The chunk that is passed around between threads. +/// `lines` consist of slices into `buffer`. +#[self_referencing(pub_extras)] +#[derive(Debug)] +pub struct Chunk { + pub buffer: Vec, + #[borrows(buffer)] + #[covariant] + pub contents: ChunkContents<'this>, +} + +#[derive(Debug)] +pub struct ChunkContents<'a> { + pub lines: Vec>, + pub line_data: LineData<'a>, +} + +#[derive(Debug)] +pub struct LineData<'a> { + pub selections: Vec<&'a str>, + pub num_infos: Vec, + pub parsed_floats: Vec, +} + +impl Chunk { + /// Destroy this chunk and return its components to be reused. + pub fn recycle(mut self) -> RecycledChunk { + let recycled_contents = self.with_contents_mut(|contents| { + contents.lines.clear(); + contents.line_data.selections.clear(); + contents.line_data.num_infos.clear(); + contents.line_data.parsed_floats.clear(); + let lines = unsafe { + // SAFETY: It is safe to (temporarily) transmute to a vector of lines with a longer lifetime, + // because the vector is empty. + // Transmuting is necessary to make recycling possible. See https://github.com/rust-lang/rfcs/pull/2802 + // for a rfc to make this unnecessary. Its example is similar to the code here. + std::mem::transmute::>, Vec>>(std::mem::take( + &mut contents.lines, + )) + }; + let selections = unsafe { + // SAFETY: (same as above) It is safe to (temporarily) transmute to a vector of &str with a longer lifetime, + // because the vector is empty. + std::mem::transmute::, Vec<&'static str>>(std::mem::take( + &mut contents.line_data.selections, + )) + }; + ( + lines, + selections, + std::mem::take(&mut contents.line_data.num_infos), + std::mem::take(&mut contents.line_data.parsed_floats), + ) + }); + RecycledChunk { + lines: recycled_contents.0, + selections: recycled_contents.1, + num_infos: recycled_contents.2, + parsed_floats: recycled_contents.3, + buffer: self.into_heads().buffer, + } + } + + pub fn lines(&self) -> &Vec { + &self.borrow_contents().lines + } + pub fn line_data(&self) -> &LineData { + &self.borrow_contents().line_data + } +} + +pub struct RecycledChunk { + lines: Vec>, + selections: Vec<&'static str>, + num_infos: Vec, + parsed_floats: Vec, + buffer: Vec, +} + +impl RecycledChunk { + pub fn new(capacity: usize) -> Self { + RecycledChunk { + lines: Vec::new(), + selections: Vec::new(), + num_infos: Vec::new(), + parsed_floats: Vec::new(), + buffer: vec![0; capacity], + } + } +} + +/// Read a chunk, parse lines and send them. +/// +/// No empty chunk will be sent. If we reach the end of the input, `false` is returned. +/// However, if this function returns `true`, it is not guaranteed that there is still +/// input left: If the input fits _exactly_ into a buffer, we will only notice that there's +/// nothing more to read at the next invocation. In case there is no input left, nothing will +/// be sent. +/// +/// # Arguments +/// +/// (see also `read_to_chunk` for a more detailed documentation) +/// +/// * `sender`: The sender to send the lines to the sorter. +/// * `recycled_chunk`: The recycled chunk, as returned by `Chunk::recycle`. +/// (i.e. `buffer.len()` should be equal to `buffer.capacity()`) +/// * `max_buffer_size`: How big `buffer` can be. +/// * `carry_over`: The bytes that must be carried over in between invocations. +/// * `file`: The current file. +/// * `next_files`: What `file` should be updated to next. +/// * `separator`: The line separator. +/// * `settings`: The global settings. +#[allow(clippy::too_many_arguments)] +pub fn read( + sender: &SyncSender, + recycled_chunk: RecycledChunk, + max_buffer_size: Option, + carry_over: &mut Vec, + file: &mut T, + next_files: &mut impl Iterator, + separator: u8, + settings: &GlobalSettings, +) -> bool { + let RecycledChunk { + lines, + selections, + num_infos, + parsed_floats, + mut buffer, + } = recycled_chunk; + if buffer.len() < carry_over.len() { + buffer.resize(carry_over.len() + 10 * 1024, 0); + } + buffer[..carry_over.len()].copy_from_slice(carry_over); + let (read, should_continue) = read_to_buffer( + file, + next_files, + &mut buffer, + max_buffer_size, + carry_over.len(), + separator, + ); + carry_over.clear(); + carry_over.extend_from_slice(&buffer[read..]); + + if read != 0 { + let payload = Chunk::new(buffer, |buffer| { + let selections = unsafe { + // SAFETY: It is safe to transmute to an empty vector of selections with shorter lifetime. + // It was only temporarily transmuted to a Vec> to make recycling possible. + std::mem::transmute::, Vec<&'_ str>>(selections) + }; + let mut lines = unsafe { + // SAFETY: (same as above) It is safe to transmute to a vector of lines with shorter lifetime, + // because it was only temporarily transmuted to a Vec> to make recycling possible. + std::mem::transmute::>, Vec>>(lines) + }; + let read = crash_if_err!(1, std::str::from_utf8(&buffer[..read])); + let mut line_data = LineData { + selections, + num_infos, + parsed_floats, + }; + parse_lines(read, &mut lines, &mut line_data, separator, settings); + ChunkContents { lines, line_data } + }); + sender.send(payload).unwrap(); + } + should_continue +} + +/// Split `read` into `Line`s, and add them to `lines`. +fn parse_lines<'a>( + mut read: &'a str, + lines: &mut Vec>, + line_data: &mut LineData<'a>, + separator: u8, + settings: &GlobalSettings, +) { + // Strip a trailing separator. TODO: Once our MinRustV is 1.45 or above, use strip_suffix() instead. + if read.ends_with(separator as char) { + read = &read[..read.len() - 1]; + } + + assert!(lines.is_empty()); + assert!(line_data.selections.is_empty()); + assert!(line_data.num_infos.is_empty()); + assert!(line_data.parsed_floats.is_empty()); + let mut token_buffer = vec![]; + lines.extend( + read.split(separator as char) + .enumerate() + .map(|(index, line)| Line::create(line, index, line_data, &mut token_buffer, settings)), + ); +} + +/// Read from `file` into `buffer`. +/// +/// This function makes sure that at least two lines are read (unless we reach EOF and there's no next file), +/// growing the buffer if necessary. +/// The last line is likely to not have been fully read into the buffer. Its bytes must be copied to +/// the front of the buffer for the next invocation so that it can be continued to be read +/// (see the return values and `start_offset`). +/// +/// # Arguments +/// +/// * `file`: The file to start reading from. +/// * `next_files`: When `file` reaches EOF, it is updated to `next_files.next()` if that is `Some`, +/// and this function continues reading. +/// * `buffer`: The buffer that is filled with bytes. Its contents will mostly be overwritten (see `start_offset` +/// as well). It will be grown up to `max_buffer_size` if necessary, but it will always grow to read at least two lines. +/// * `max_buffer_size`: Grow the buffer to at most this length. If None, the buffer will not grow, unless needed to read at least two lines. +/// * `start_offset`: The amount of bytes at the start of `buffer` that were carried over +/// from the previous read and should not be overwritten. +/// * `separator`: The byte that separates lines. +/// +/// # Returns +/// +/// * The amount of bytes in `buffer` that can now be interpreted as lines. +/// The remaining bytes must be copied to the start of the buffer for the next invocation, +/// if another invocation is necessary, which is determined by the other return value. +/// * Whether this function should be called again. +fn read_to_buffer( + file: &mut T, + next_files: &mut impl Iterator, + buffer: &mut Vec, + max_buffer_size: Option, + start_offset: usize, + separator: u8, +) -> (usize, bool) { + let mut read_target = &mut buffer[start_offset..]; + let mut last_file_target_size = read_target.len(); + loop { + match file.read(read_target) { + Ok(0) => { + if read_target.is_empty() { + // chunk is full + if let Some(max_buffer_size) = max_buffer_size { + if max_buffer_size > buffer.len() { + // we can grow the buffer + let prev_len = buffer.len(); + if buffer.len() < max_buffer_size / 2 { + buffer.resize(buffer.len() * 2, 0); + } else { + buffer.resize(max_buffer_size, 0); + } + read_target = &mut buffer[prev_len..]; + continue; + } + } + let mut sep_iter = memchr_iter(separator, buffer).rev(); + let last_line_end = sep_iter.next(); + if sep_iter.next().is_some() { + // We read enough lines. + let end = last_line_end.unwrap(); + // We want to include the separator here, because it shouldn't be carried over. + return (end + 1, true); + } else { + // We need to read more lines + let len = buffer.len(); + // resize the vector to 10 KB more + buffer.resize(len + 1024 * 10, 0); + read_target = &mut buffer[len..]; + } + } else { + // This file has been fully read. + let mut leftover_len = read_target.len(); + if last_file_target_size != leftover_len { + // The file was not empty. + let read_len = buffer.len() - leftover_len; + if buffer[read_len - 1] != separator { + // The file did not end with a separator. We have to insert one. + buffer[read_len] = separator; + leftover_len -= 1; + } + let read_len = buffer.len() - leftover_len; + read_target = &mut buffer[read_len..]; + } + if let Some(next_file) = next_files.next() { + // There is another file. + last_file_target_size = leftover_len; + *file = next_file; + } else { + // This was the last file. + let read_len = buffer.len() - leftover_len; + return (read_len, false); + } + } + } + Ok(n) => { + read_target = &mut read_target[n..]; + } + Err(e) if e.kind() == ErrorKind::Interrupted => { + // retry + } + Err(e) => crash!(1, "{}", e), + } + } +} diff --git a/src/uu/sort/src/custom_str_cmp.rs b/src/uu/sort/src/custom_str_cmp.rs new file mode 100644 index 000000000..089d33bc4 --- /dev/null +++ b/src/uu/sort/src/custom_str_cmp.rs @@ -0,0 +1,64 @@ +// * This file is part of the uutils coreutils package. +// * +// * (c) Michael Debertol +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +//! Custom string comparisons. +//! +//! The goal is to compare strings without transforming them first (i.e. not allocating new strings) + +use std::cmp::Ordering; + +fn filter_char(c: char, ignore_non_printing: bool, ignore_non_dictionary: bool) -> bool { + if ignore_non_dictionary && !(c.is_ascii_alphanumeric() || c.is_ascii_whitespace()) { + return false; + } + if ignore_non_printing && (c.is_ascii_control() || !c.is_ascii()) { + return false; + } + true +} + +fn cmp_chars(a: char, b: char, ignore_case: bool) -> Ordering { + if ignore_case { + a.to_ascii_uppercase().cmp(&b.to_ascii_uppercase()) + } else { + a.cmp(&b) + } +} + +pub fn custom_str_cmp( + a: &str, + b: &str, + ignore_non_printing: bool, + ignore_non_dictionary: bool, + ignore_case: bool, +) -> Ordering { + if !(ignore_case || ignore_non_dictionary || ignore_non_printing) { + // There are no custom settings. Fall back to the default strcmp, which is faster. + return a.cmp(b); + } + let mut a_chars = a + .chars() + .filter(|&c| filter_char(c, ignore_non_printing, ignore_non_dictionary)); + let mut b_chars = b + .chars() + .filter(|&c| filter_char(c, ignore_non_printing, ignore_non_dictionary)); + loop { + let a_char = a_chars.next(); + let b_char = b_chars.next(); + match (a_char, b_char) { + (None, None) => return Ordering::Equal, + (Some(_), None) => return Ordering::Greater, + (None, Some(_)) => return Ordering::Less, + (Some(a_char), Some(b_char)) => { + let ordering = cmp_chars(a_char, b_char, ignore_case); + if ordering != Ordering::Equal { + return ordering; + } + } + } + } +} diff --git a/src/uu/sort/src/ext_sort.rs b/src/uu/sort/src/ext_sort.rs new file mode 100644 index 000000000..e0814b7a2 --- /dev/null +++ b/src/uu/sort/src/ext_sort.rs @@ -0,0 +1,279 @@ +// * This file is part of the uutils coreutils package. +// * +// * (c) Michael Debertol +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +//! Sort big files by using auxiliary files for storing intermediate chunks. +//! +//! Files are read into chunks of memory which are then sorted individually and +//! written to temporary files. There are two threads: One sorter, and one reader/writer. +//! The buffers for the individual chunks are recycled. There are two buffers. + +use std::cmp::Ordering; +use std::io::Write; +use std::path::Path; +use std::path::PathBuf; +use std::{ + io::Read, + sync::mpsc::{Receiver, SyncSender}, + thread, +}; + +use itertools::Itertools; + +use crate::chunks::RecycledChunk; +use crate::merge::ClosedTmpFile; +use crate::merge::WriteableCompressedTmpFile; +use crate::merge::WriteablePlainTmpFile; +use crate::merge::WriteableTmpFile; +use crate::{ + chunks::{self, Chunk}, + compare_by, merge, sort_by, GlobalSettings, +}; +use crate::{print_sorted, Line}; +use tempfile::TempDir; + +const START_BUFFER_SIZE: usize = 8_000; + +/// Sort files by using auxiliary files for storing intermediate chunks (if needed), and output the result. +pub fn ext_sort(files: &mut impl Iterator>, settings: &GlobalSettings) { + let (sorted_sender, sorted_receiver) = std::sync::mpsc::sync_channel(1); + let (recycled_sender, recycled_receiver) = std::sync::mpsc::sync_channel(1); + thread::spawn({ + let settings = settings.clone(); + move || sorter(recycled_receiver, sorted_sender, settings) + }); + if settings.compress_prog.is_some() { + reader_writer::<_, WriteableCompressedTmpFile>( + files, + settings, + sorted_receiver, + recycled_sender, + ); + } else { + reader_writer::<_, WriteablePlainTmpFile>( + files, + settings, + sorted_receiver, + recycled_sender, + ); + } +} + +fn reader_writer>, Tmp: WriteableTmpFile + 'static>( + files: F, + settings: &GlobalSettings, + receiver: Receiver, + sender: SyncSender, +) { + let separator = if settings.zero_terminated { + b'\0' + } else { + b'\n' + }; + + // Heuristically chosen: Dividing by 10 seems to keep our memory usage roughly + // around settings.buffer_size as a whole. + let buffer_size = settings.buffer_size / 10; + let read_result: ReadResult = read_write_loop( + files, + &settings.tmp_dir, + separator, + // Heuristically chosen: Dividing by 10 seems to keep our memory usage roughly + // around settings.buffer_size as a whole. + buffer_size, + settings, + receiver, + sender, + ); + match read_result { + ReadResult::WroteChunksToFile { tmp_files, tmp_dir } => { + let tmp_dir_size = tmp_files.len(); + let mut merger = merge::merge_with_file_limit::<_, _, Tmp>( + tmp_files.into_iter().map(|c| c.reopen()), + settings, + Some((tmp_dir, tmp_dir_size)), + ); + merger.write_all(settings); + } + ReadResult::SortedSingleChunk(chunk) => { + if settings.unique { + print_sorted( + chunk.lines().iter().dedup_by(|a, b| { + compare_by(a, b, settings, chunk.line_data(), chunk.line_data()) + == Ordering::Equal + }), + settings, + ); + } else { + print_sorted(chunk.lines().iter(), settings); + } + } + ReadResult::SortedTwoChunks([a, b]) => { + let merged_iter = a.lines().iter().map(|line| (line, &a)).merge_by( + b.lines().iter().map(|line| (line, &b)), + |(line_a, a), (line_b, b)| { + compare_by(line_a, line_b, settings, a.line_data(), b.line_data()) + != Ordering::Greater + }, + ); + if settings.unique { + print_sorted( + merged_iter + .dedup_by(|(line_a, a), (line_b, b)| { + compare_by(line_a, line_b, settings, a.line_data(), b.line_data()) + == Ordering::Equal + }) + .map(|(line, _)| line), + settings, + ); + } else { + print_sorted(merged_iter.map(|(line, _)| line), settings); + } + } + ReadResult::EmptyInput => { + // don't output anything + } + } +} + +/// The function that is executed on the sorter thread. +fn sorter(receiver: Receiver, sender: SyncSender, settings: GlobalSettings) { + while let Ok(mut payload) = receiver.recv() { + payload.with_contents_mut(|contents| { + sort_by(&mut contents.lines, &settings, &contents.line_data) + }); + sender.send(payload).unwrap(); + } +} + +/// Describes how we read the chunks from the input. +enum ReadResult { + /// The input was empty. Nothing was read. + EmptyInput, + /// The input fits into a single Chunk, which was kept in memory. + SortedSingleChunk(Chunk), + /// The input fits into two chunks, which were kept in memory. + SortedTwoChunks([Chunk; 2]), + /// The input was read into multiple chunks, which were written to auxiliary files. + WroteChunksToFile { + tmp_files: Vec, + tmp_dir: TempDir, + }, +} +/// The function that is executed on the reader/writer thread. +fn read_write_loop( + mut files: impl Iterator>, + tmp_dir_parent: &Path, + separator: u8, + buffer_size: usize, + settings: &GlobalSettings, + receiver: Receiver, + sender: SyncSender, +) -> ReadResult { + let mut file = files.next().unwrap(); + + let mut carry_over = vec![]; + // kick things off with two reads + for _ in 0..2 { + let should_continue = chunks::read( + &sender, + RecycledChunk::new(if START_BUFFER_SIZE < buffer_size { + START_BUFFER_SIZE + } else { + buffer_size + }), + Some(buffer_size), + &mut carry_over, + &mut file, + &mut files, + separator, + settings, + ); + + if !should_continue { + drop(sender); + // We have already read the whole input. Since we are in our first two reads, + // this means that we can fit the whole input into memory. Bypass writing below and + // handle this case in a more straightforward way. + return if let Ok(first_chunk) = receiver.recv() { + if let Ok(second_chunk) = receiver.recv() { + ReadResult::SortedTwoChunks([first_chunk, second_chunk]) + } else { + ReadResult::SortedSingleChunk(first_chunk) + } + } else { + ReadResult::EmptyInput + }; + } + } + + let tmp_dir = crash_if_err!( + 1, + tempfile::Builder::new() + .prefix("uutils_sort") + .tempdir_in(tmp_dir_parent) + ); + + let mut sender_option = Some(sender); + let mut file_number = 0; + let mut tmp_files = vec![]; + loop { + let mut chunk = match receiver.recv() { + Ok(it) => it, + _ => { + return ReadResult::WroteChunksToFile { tmp_files, tmp_dir }; + } + }; + + let tmp_file = write::( + &mut chunk, + tmp_dir.path().join(file_number.to_string()), + settings.compress_prog.as_deref(), + separator, + ); + tmp_files.push(tmp_file); + + file_number += 1; + + let recycled_chunk = chunk.recycle(); + + if let Some(sender) = &sender_option { + let should_continue = chunks::read( + sender, + recycled_chunk, + None, + &mut carry_over, + &mut file, + &mut files, + separator, + settings, + ); + if !should_continue { + sender_option = None; + } + } + } +} + +/// Write the lines in `chunk` to `file`, separated by `separator`. +/// `compress_prog` is used to optionally compress file contents. +fn write( + chunk: &mut Chunk, + file: PathBuf, + compress_prog: Option<&str>, + separator: u8, +) -> I::Closed { + let mut tmp_file = I::create(file, compress_prog); + write_lines(chunk.lines(), tmp_file.as_write(), separator); + tmp_file.finished_writing() +} + +fn write_lines<'a, T: Write>(lines: &[Line<'a>], writer: &mut T, separator: u8) { + for s in lines { + writer.write_all(s.line.as_bytes()).unwrap(); + writer.write_all(&[separator]).unwrap(); + } +} diff --git a/src/uu/sort/src/main.rs b/src/uu/sort/src/main.rs index a59375b2f..ab463776d 100644 --- a/src/uu/sort/src/main.rs +++ b/src/uu/sort/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_sort); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_sort); diff --git a/src/uu/sort/src/merge.rs b/src/uu/sort/src/merge.rs new file mode 100644 index 000000000..12d7a9b9b --- /dev/null +++ b/src/uu/sort/src/merge.rs @@ -0,0 +1,516 @@ +//! Merge already sorted files. +//! +//! We achieve performance by splitting the tasks of sorting and writing, and reading and parsing between two threads. +//! The threads communicate over channels. There's one channel per file in the direction reader -> sorter, but only +//! one channel from the sorter back to the reader. The channels to the sorter are used to send the read chunks. +//! The sorter reads the next chunk from the channel whenever it needs the next chunk after running out of lines +//! from the previous read of the file. The channel back from the sorter to the reader has two purposes: To allow the reader +//! to reuse memory allocations and to tell the reader which file to read from next. + +use std::{ + cmp::Ordering, + fs::{self, File}, + io::{BufWriter, Read, Write}, + iter, + path::PathBuf, + process::{Child, ChildStdin, ChildStdout, Command, Stdio}, + rc::Rc, + sync::mpsc::{channel, sync_channel, Receiver, Sender, SyncSender}, + thread, +}; + +use compare::Compare; +use itertools::Itertools; +use tempfile::TempDir; + +use crate::{ + chunks::{self, Chunk, RecycledChunk}, + compare_by, GlobalSettings, +}; + +/// Merge pre-sorted `Box`s. +/// +/// If `settings.merge_batch_size` is greater than the length of `files`, intermediate files will be used. +/// If `settings.compress_prog` is `Some`, intermediate files will be compressed with it. +pub fn merge>>( + files: Files, + settings: &GlobalSettings, +) -> FileMerger { + if settings.compress_prog.is_none() { + merge_with_file_limit::<_, _, WriteablePlainTmpFile>( + files.map(|file| PlainMergeInput { inner: file }), + settings, + None, + ) + } else { + merge_with_file_limit::<_, _, WriteableCompressedTmpFile>( + files.map(|file| PlainMergeInput { inner: file }), + settings, + None, + ) + } +} + +// Merge already sorted `MergeInput`s. +pub fn merge_with_file_limit< + M: MergeInput + 'static, + F: ExactSizeIterator, + Tmp: WriteableTmpFile + 'static, +>( + files: F, + settings: &GlobalSettings, + tmp_dir: Option<(TempDir, usize)>, +) -> FileMerger { + if files.len() > settings.merge_batch_size { + // If we did not get a tmp_dir, create one. + let (tmp_dir, mut tmp_dir_size) = tmp_dir.unwrap_or_else(|| { + ( + tempfile::Builder::new() + .prefix("uutils_sort") + .tempdir_in(&settings.tmp_dir) + .unwrap(), + 0, + ) + }); + let mut remaining_files = files.len(); + let batches = files.chunks(settings.merge_batch_size); + let mut batches = batches.into_iter(); + let mut temporary_files = vec![]; + while remaining_files != 0 { + // Work around the fact that `Chunks` is not an `ExactSizeIterator`. + remaining_files = remaining_files.saturating_sub(settings.merge_batch_size); + let mut merger = merge_without_limit(batches.next().unwrap(), settings); + let mut tmp_file = Tmp::create( + tmp_dir.path().join(tmp_dir_size.to_string()), + settings.compress_prog.as_deref(), + ); + tmp_dir_size += 1; + merger.write_all_to(settings, tmp_file.as_write()); + temporary_files.push(tmp_file.finished_writing()); + } + assert!(batches.next().is_none()); + merge_with_file_limit::<_, _, Tmp>( + temporary_files + .into_iter() + .map(Box::new(|c: Tmp::Closed| c.reopen()) + as Box< + dyn FnMut(Tmp::Closed) -> ::Reopened, + >), + settings, + Some((tmp_dir, tmp_dir_size)), + ) + } else { + merge_without_limit(files, settings) + } +} + +/// Merge files without limiting how many files are concurrently open. +/// +/// It is the responsibility of the caller to ensure that `files` yields only +/// as many files as we are allowed to open concurrently. +fn merge_without_limit>( + files: F, + settings: &GlobalSettings, +) -> FileMerger { + let (request_sender, request_receiver) = channel(); + let mut reader_files = Vec::with_capacity(files.size_hint().0); + let mut loaded_receivers = Vec::with_capacity(files.size_hint().0); + for (file_number, file) in files.enumerate() { + let (sender, receiver) = sync_channel(2); + loaded_receivers.push(receiver); + reader_files.push(Some(ReaderFile { + file, + sender, + carry_over: vec![], + })); + // Send the initial chunk to trigger a read for each file + request_sender + .send((file_number, RecycledChunk::new(8 * 1024))) + .unwrap(); + } + + // Send the second chunk for each file + for file_number in 0..reader_files.len() { + request_sender + .send((file_number, RecycledChunk::new(8 * 1024))) + .unwrap(); + } + + thread::spawn({ + let settings = settings.clone(); + move || { + reader( + request_receiver, + &mut reader_files, + &settings, + if settings.zero_terminated { + b'\0' + } else { + b'\n' + }, + ) + } + }); + + let mut mergeable_files = vec![]; + + for (file_number, receiver) in loaded_receivers.into_iter().enumerate() { + mergeable_files.push(MergeableFile { + current_chunk: Rc::new(receiver.recv().unwrap()), + file_number, + line_idx: 0, + receiver, + }) + } + + FileMerger { + heap: binary_heap_plus::BinaryHeap::from_vec_cmp( + mergeable_files, + FileComparator { settings }, + ), + request_sender, + prev: None, + } +} +/// The struct on the reader thread representing an input file +struct ReaderFile { + file: M, + sender: SyncSender, + carry_over: Vec, +} + +/// The function running on the reader thread. +fn reader( + recycled_receiver: Receiver<(usize, RecycledChunk)>, + files: &mut [Option>], + settings: &GlobalSettings, + separator: u8, +) { + for (file_idx, recycled_chunk) in recycled_receiver.iter() { + if let Some(ReaderFile { + file, + sender, + carry_over, + }) = &mut files[file_idx] + { + let should_continue = chunks::read( + sender, + recycled_chunk, + None, + carry_over, + file.as_read(), + &mut iter::empty(), + separator, + settings, + ); + if !should_continue { + // Remove the file from the list by replacing it with `None`. + let ReaderFile { file, .. } = files[file_idx].take().unwrap(); + // Depending on the kind of the `MergeInput`, this may delete the file: + file.finished_reading(); + } + } + } +} +/// The struct on the main thread representing an input file +pub struct MergeableFile { + current_chunk: Rc, + line_idx: usize, + receiver: Receiver, + file_number: usize, +} + +/// A struct to keep track of the previous line we encountered. +/// +/// This is required for deduplication purposes. +struct PreviousLine { + chunk: Rc, + line_idx: usize, + file_number: usize, +} + +/// Merges files together. This is **not** an iterator because of lifetime problems. +pub struct FileMerger<'a> { + heap: binary_heap_plus::BinaryHeap>, + request_sender: Sender<(usize, RecycledChunk)>, + prev: Option, +} + +impl<'a> FileMerger<'a> { + /// Write the merged contents to the output file. + pub fn write_all(&mut self, settings: &GlobalSettings) { + let mut out = settings.out_writer(); + self.write_all_to(settings, &mut out); + } + + pub fn write_all_to(&mut self, settings: &GlobalSettings, out: &mut impl Write) { + while self.write_next(settings, out) {} + } + + fn write_next(&mut self, settings: &GlobalSettings, out: &mut impl Write) -> bool { + if let Some(file) = self.heap.peek() { + let prev = self.prev.replace(PreviousLine { + chunk: file.current_chunk.clone(), + line_idx: file.line_idx, + file_number: file.file_number, + }); + + file.current_chunk.with_contents(|contents| { + let current_line = &contents.lines[file.line_idx]; + if settings.unique { + if let Some(prev) = &prev { + let cmp = compare_by( + &prev.chunk.lines()[prev.line_idx], + current_line, + settings, + prev.chunk.line_data(), + file.current_chunk.line_data(), + ); + if cmp == Ordering::Equal { + return; + } + } + } + current_line.print(out, settings); + }); + + let was_last_line_for_file = file.current_chunk.lines().len() == file.line_idx + 1; + + if was_last_line_for_file { + if let Ok(next_chunk) = file.receiver.recv() { + let mut file = self.heap.peek_mut().unwrap(); + file.current_chunk = Rc::new(next_chunk); + file.line_idx = 0; + } else { + self.heap.pop(); + } + } else { + // This will cause the comparison to use a different line and the heap to readjust. + self.heap.peek_mut().unwrap().line_idx += 1; + } + + if let Some(prev) = prev { + if let Ok(prev_chunk) = Rc::try_unwrap(prev.chunk) { + // If nothing is referencing the previous chunk anymore, this means that the previous line + // was the last line of the chunk. We can recycle the chunk. + self.request_sender + .send((prev.file_number, prev_chunk.recycle())) + .ok(); + } + } + } + !self.heap.is_empty() + } +} + +/// Compares files by their current line. +struct FileComparator<'a> { + settings: &'a GlobalSettings, +} + +impl<'a> Compare for FileComparator<'a> { + fn compare(&self, a: &MergeableFile, b: &MergeableFile) -> Ordering { + let mut cmp = compare_by( + &a.current_chunk.lines()[a.line_idx], + &b.current_chunk.lines()[b.line_idx], + self.settings, + a.current_chunk.line_data(), + b.current_chunk.line_data(), + ); + if cmp == Ordering::Equal { + // To make sorting stable, we need to consider the file number as well, + // as lines from a file with a lower number are to be considered "earlier". + cmp = a.file_number.cmp(&b.file_number); + } + // BinaryHeap is a max heap. We use it as a min heap, so we need to reverse the ordering. + cmp.reverse() + } +} + +// Wait for the child to exit and check its exit code. +fn assert_child_success(mut child: Child, program: &str) { + if !matches!( + child.wait().map(|e| e.code()), + Ok(Some(0)) | Ok(None) | Err(_) + ) { + crash!(2, "'{}' terminated abnormally", program) + } +} + +/// A temporary file that can be written to. +pub trait WriteableTmpFile { + type Closed: ClosedTmpFile; + type InnerWrite: Write; + fn create(path: PathBuf, compress_prog: Option<&str>) -> Self; + /// Closes the temporary file. + fn finished_writing(self) -> Self::Closed; + fn as_write(&mut self) -> &mut Self::InnerWrite; +} +/// A temporary file that is (temporarily) closed, but can be reopened. +pub trait ClosedTmpFile { + type Reopened: MergeInput; + /// Reopens the temporary file. + fn reopen(self) -> Self::Reopened; +} +/// A pre-sorted input for merging. +pub trait MergeInput: Send { + type InnerRead: Read; + /// Cleans this `MergeInput` up. + /// Implementations may delete the backing file. + fn finished_reading(self); + fn as_read(&mut self) -> &mut Self::InnerRead; +} + +pub struct WriteablePlainTmpFile { + path: PathBuf, + file: BufWriter, +} +pub struct ClosedPlainTmpFile { + path: PathBuf, +} +pub struct PlainTmpMergeInput { + path: PathBuf, + file: File, +} +impl WriteableTmpFile for WriteablePlainTmpFile { + type Closed = ClosedPlainTmpFile; + type InnerWrite = BufWriter; + + fn create(path: PathBuf, _: Option<&str>) -> Self { + WriteablePlainTmpFile { + file: BufWriter::new(File::create(&path).unwrap()), + path, + } + } + + fn finished_writing(self) -> Self::Closed { + ClosedPlainTmpFile { path: self.path } + } + + fn as_write(&mut self) -> &mut Self::InnerWrite { + &mut self.file + } +} +impl ClosedTmpFile for ClosedPlainTmpFile { + type Reopened = PlainTmpMergeInput; + fn reopen(self) -> Self::Reopened { + PlainTmpMergeInput { + file: File::open(&self.path).unwrap(), + path: self.path, + } + } +} +impl MergeInput for PlainTmpMergeInput { + type InnerRead = File; + + fn finished_reading(self) { + fs::remove_file(self.path).ok(); + } + + fn as_read(&mut self) -> &mut Self::InnerRead { + &mut self.file + } +} + +pub struct WriteableCompressedTmpFile { + path: PathBuf, + compress_prog: String, + child: Child, + child_stdin: BufWriter, +} +pub struct ClosedCompressedTmpFile { + path: PathBuf, + compress_prog: String, +} +pub struct CompressedTmpMergeInput { + path: PathBuf, + compress_prog: String, + child: Child, + child_stdout: ChildStdout, +} +impl WriteableTmpFile for WriteableCompressedTmpFile { + type Closed = ClosedCompressedTmpFile; + type InnerWrite = BufWriter; + + fn create(path: PathBuf, compress_prog: Option<&str>) -> Self { + let compress_prog = compress_prog.unwrap(); + let mut command = Command::new(compress_prog); + command + .stdin(Stdio::piped()) + .stdout(File::create(&path).unwrap()); + let mut child = crash_if_err!( + 2, + command.spawn().map_err(|err| format!( + "couldn't execute compress program: errno {}", + err.raw_os_error().unwrap() + )) + ); + let child_stdin = child.stdin.take().unwrap(); + WriteableCompressedTmpFile { + path, + compress_prog: compress_prog.to_owned(), + child, + child_stdin: BufWriter::new(child_stdin), + } + } + + fn finished_writing(self) -> Self::Closed { + drop(self.child_stdin); + assert_child_success(self.child, &self.compress_prog); + ClosedCompressedTmpFile { + path: self.path, + compress_prog: self.compress_prog, + } + } + + fn as_write(&mut self) -> &mut Self::InnerWrite { + &mut self.child_stdin + } +} +impl ClosedTmpFile for ClosedCompressedTmpFile { + type Reopened = CompressedTmpMergeInput; + + fn reopen(self) -> Self::Reopened { + let mut command = Command::new(&self.compress_prog); + let file = File::open(&self.path).unwrap(); + command.stdin(file).stdout(Stdio::piped()).arg("-d"); + let mut child = crash_if_err!( + 2, + command.spawn().map_err(|err| format!( + "couldn't execute compress program: errno {}", + err.raw_os_error().unwrap() + )) + ); + let child_stdout = child.stdout.take().unwrap(); + CompressedTmpMergeInput { + path: self.path, + compress_prog: self.compress_prog, + child, + child_stdout, + } + } +} +impl MergeInput for CompressedTmpMergeInput { + type InnerRead = ChildStdout; + + fn finished_reading(self) { + drop(self.child_stdout); + assert_child_success(self.child, &self.compress_prog); + fs::remove_file(self.path).ok(); + } + + fn as_read(&mut self) -> &mut Self::InnerRead { + &mut self.child_stdout + } +} + +pub struct PlainMergeInput { + inner: R, +} +impl MergeInput for PlainMergeInput { + type InnerRead = R; + fn finished_reading(self) {} + fn as_read(&mut self) -> &mut Self::InnerRead { + &mut self.inner + } +} diff --git a/src/uu/sort/src/numeric_str_cmp.rs b/src/uu/sort/src/numeric_str_cmp.rs new file mode 100644 index 000000000..d753c2d9d --- /dev/null +++ b/src/uu/sort/src/numeric_str_cmp.rs @@ -0,0 +1,505 @@ +// * This file is part of the uutils coreutils package. +// * +// * (c) Michael Debertol +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +//! Fast comparison for strings representing a base 10 number without precision loss. +//! +//! To be able to short-circuit when comparing, [NumInfo] must be passed along with each number +//! to [numeric_str_cmp]. [NumInfo] is generally obtained by calling [NumInfo::parse] and should be cached. +//! It is allowed to arbitrarily modify the exponent afterwards, which is equivalent to shifting the decimal point. +//! +//! More specifically, exponent can be understood so that the original number is in (1..10)*10^exponent. +//! From that follows the constraints of this algorithm: It is able to compare numbers in ±(1*10^[i64::MIN]..10*10^[i64::MAX]). + +use std::{cmp::Ordering, ops::Range}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] +enum Sign { + Negative, + Positive, +} + +#[derive(Debug, PartialEq, Clone)] +pub struct NumInfo { + exponent: i64, + sign: Sign, +} +#[derive(Debug, PartialEq, Clone)] +pub struct NumInfoParseSettings { + pub accept_si_units: bool, + pub thousands_separator: Option, + pub decimal_pt: Option, +} + +impl Default for NumInfoParseSettings { + fn default() -> Self { + Self { + accept_si_units: false, + thousands_separator: None, + decimal_pt: Some('.'), + } + } +} + +impl NumInfo { + /// Parse NumInfo for this number. + /// Also returns the range of num that should be passed to numeric_str_cmp later. + /// + /// Leading zeros will be excluded from the returned range. If the number consists of only zeros, + /// an empty range (idx..idx) is returned so that idx is the char after the last zero. + /// If the input is not a number (which has to be treated as zero), the returned empty range + /// will be 0..0. + pub fn parse(num: &str, parse_settings: NumInfoParseSettings) -> (Self, Range) { + let mut exponent = -1; + let mut had_decimal_pt = false; + let mut had_digit = false; + let mut start = None; + let mut sign = Sign::Positive; + + let mut first_char = true; + + for (idx, char) in num.char_indices() { + if first_char && char.is_whitespace() { + continue; + } + + if first_char && char == '-' { + sign = Sign::Negative; + first_char = false; + continue; + } + first_char = false; + + if matches!( + parse_settings.thousands_separator, + Some(c) if c == char + ) { + continue; + } + + if Self::is_invalid_char(char, &mut had_decimal_pt, &parse_settings) { + return if let Some(start) = start { + let has_si_unit = parse_settings.accept_si_units + && matches!(char, 'K' | 'k' | 'M' | 'G' | 'T' | 'P' | 'E' | 'Z' | 'Y'); + ( + NumInfo { exponent, sign }, + start..if has_si_unit { idx + 1 } else { idx }, + ) + } else { + ( + NumInfo { + sign: Sign::Positive, + exponent: 0, + }, + if had_digit { + // In this case there were only zeroes. + // For debug output to work properly, we have to match the character after the last zero. + idx..idx + } else { + // This was no number at all. + // For debug output to work properly, we have to match 0..0. + 0..0 + }, + ) + }; + } + if Some(char) == parse_settings.decimal_pt { + continue; + } + had_digit = true; + if start.is_none() && char == '0' { + if had_decimal_pt { + // We're parsing a number whose first nonzero digit is after the decimal point. + exponent -= 1; + } else { + // Skip leading zeroes + continue; + } + } + if !had_decimal_pt { + exponent += 1; + } + if start.is_none() && char != '0' { + start = Some(idx); + } + } + if let Some(start) = start { + (NumInfo { exponent, sign }, start..num.len()) + } else { + ( + NumInfo { + sign: Sign::Positive, + exponent: 0, + }, + if had_digit { + // In this case there were only zeroes. + // For debug output to work properly, we have to claim to match the end of the number. + num.len()..num.len() + } else { + // This was no number at all. + // For debug output to work properly, we have to claim to match the start of the number. + 0..0 + }, + ) + } + } + + fn is_invalid_char( + c: char, + had_decimal_pt: &mut bool, + parse_settings: &NumInfoParseSettings, + ) -> bool { + if Some(c) == parse_settings.decimal_pt { + if *had_decimal_pt { + // this is a decimal pt but we already had one, so it is invalid + true + } else { + *had_decimal_pt = true; + false + } + } else { + !c.is_ascii_digit() + } + } +} + +fn get_unit(unit: Option) -> u8 { + if let Some(unit) = unit { + match unit { + 'K' | 'k' => 1, + 'M' => 2, + 'G' => 3, + 'T' => 4, + 'P' => 5, + 'E' => 6, + 'Z' => 7, + 'Y' => 8, + _ => 0, + } + } else { + 0 + } +} + +/// Compare two numbers according to the rules of human numeric comparison. +/// The SI-Unit takes precedence over the actual value (i.e. 2000M < 1G). +pub fn human_numeric_str_cmp( + (a, a_info): (&str, &NumInfo), + (b, b_info): (&str, &NumInfo), +) -> Ordering { + // 1. Sign + if a_info.sign != b_info.sign { + return a_info.sign.cmp(&b_info.sign); + } + // 2. Unit + let a_unit = get_unit(a.chars().next_back()); + let b_unit = get_unit(b.chars().next_back()); + let ordering = a_unit.cmp(&b_unit); + if ordering != Ordering::Equal { + if a_info.sign == Sign::Negative { + ordering.reverse() + } else { + ordering + } + } else { + // 3. Number + numeric_str_cmp((a, a_info), (b, b_info)) + } +} + +/// Compare two numbers as strings without parsing them as a number first. This should be more performant and can handle numbers more precisely. +/// NumInfo is needed to provide a fast path for most numbers. +#[inline(always)] +pub fn numeric_str_cmp((a, a_info): (&str, &NumInfo), (b, b_info): (&str, &NumInfo)) -> Ordering { + // check for a difference in the sign + if a_info.sign != b_info.sign { + return a_info.sign.cmp(&b_info.sign); + } + + // check for a difference in the exponent + let ordering = if a_info.exponent != b_info.exponent && !a.is_empty() && !b.is_empty() { + a_info.exponent.cmp(&b_info.exponent) + } else { + // walk the characters from the front until we find a difference + let mut a_chars = a.chars().filter(|c| c.is_ascii_digit()); + let mut b_chars = b.chars().filter(|c| c.is_ascii_digit()); + loop { + let a_next = a_chars.next(); + let b_next = b_chars.next(); + match (a_next, b_next) { + (None, None) => break Ordering::Equal, + (Some(c), None) => { + break if c == '0' && a_chars.all(|c| c == '0') { + Ordering::Equal + } else { + Ordering::Greater + } + } + (None, Some(c)) => { + break if c == '0' && b_chars.all(|c| c == '0') { + Ordering::Equal + } else { + Ordering::Less + } + } + (Some(a_char), Some(b_char)) => { + let ord = a_char.cmp(&b_char); + if ord != Ordering::Equal { + break ord; + } + } + } + } + }; + + if a_info.sign == Sign::Negative { + ordering.reverse() + } else { + ordering + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_exp() { + let n = "1"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: 0, + sign: Sign::Positive + }, + 0..1 + ) + ); + let n = "100"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: 2, + sign: Sign::Positive + }, + 0..3 + ) + ); + let n = "1,000"; + assert_eq!( + NumInfo::parse( + n, + NumInfoParseSettings { + thousands_separator: Some(','), + ..Default::default() + } + ), + ( + NumInfo { + exponent: 3, + sign: Sign::Positive + }, + 0..5 + ) + ); + let n = "1,000"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: 0, + sign: Sign::Positive + }, + 0..1 + ) + ); + let n = "1000.00"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: 3, + sign: Sign::Positive + }, + 0..7 + ) + ); + } + #[test] + fn parses_negative_exp() { + let n = "0.00005"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: -5, + sign: Sign::Positive + }, + 6..7 + ) + ); + let n = "00000.00005"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: -5, + sign: Sign::Positive + }, + 10..11 + ) + ); + } + + #[test] + fn parses_sign() { + let n = "5"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: 0, + sign: Sign::Positive + }, + 0..1 + ) + ); + let n = "-5"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: 0, + sign: Sign::Negative + }, + 1..2 + ) + ); + let n = " -5"; + assert_eq!( + NumInfo::parse(n, Default::default()), + ( + NumInfo { + exponent: 0, + sign: Sign::Negative + }, + 5..6 + ) + ); + } + + fn test_helper(a: &str, b: &str, expected: Ordering) { + let (a_info, a_range) = NumInfo::parse(a, Default::default()); + let (b_info, b_range) = NumInfo::parse(b, Default::default()); + let ordering = numeric_str_cmp( + (&a[a_range.to_owned()], &a_info), + (&b[b_range.to_owned()], &b_info), + ); + assert_eq!(ordering, expected); + let ordering = numeric_str_cmp((&b[b_range], &b_info), (&a[a_range], &a_info)); + assert_eq!(ordering, expected.reverse()); + } + #[test] + fn test_single_digit() { + test_helper("1", "2", Ordering::Less); + test_helper("0", "0", Ordering::Equal); + } + #[test] + fn test_minus() { + test_helper("-1", "-2", Ordering::Greater); + test_helper("-0", "-0", Ordering::Equal); + } + #[test] + fn test_different_len() { + test_helper("-20", "-100", Ordering::Greater); + test_helper("10.0", "2.000000", Ordering::Greater); + } + #[test] + fn test_decimal_digits() { + test_helper("20.1", "20.2", Ordering::Less); + test_helper("20.1", "20.15", Ordering::Less); + test_helper("-20.1", "+20.15", Ordering::Less); + test_helper("-20.1", "-20", Ordering::Less); + } + #[test] + fn test_trailing_zeroes() { + test_helper("20.00000", "20.1", Ordering::Less); + test_helper("20.00000", "20.0", Ordering::Equal); + } + #[test] + fn test_invalid_digits() { + test_helper("foo", "bar", Ordering::Equal); + test_helper("20.1", "a", Ordering::Greater); + test_helper("-20.1", "a", Ordering::Less); + test_helper("a", "0.15", Ordering::Less); + } + #[test] + fn test_multiple_decimal_pts() { + test_helper("10.0.0", "50.0.0", Ordering::Less); + test_helper("0.1.", "0.2.0", Ordering::Less); + test_helper("1.1.", "0", Ordering::Greater); + test_helper("1.1.", "-0", Ordering::Greater); + } + #[test] + fn test_leading_decimal_pts() { + test_helper(".0", ".0", Ordering::Equal); + test_helper(".1", ".0", Ordering::Greater); + test_helper(".02", "0", Ordering::Greater); + } + #[test] + fn test_leading_zeroes() { + test_helper("000000.0", ".0", Ordering::Equal); + test_helper("0.1", "0000000000000.0", Ordering::Greater); + test_helper("-01", "-2", Ordering::Greater); + } + + #[test] + fn minus_zero() { + // This matches GNU sort behavior. + test_helper("-0", "0", Ordering::Equal); + test_helper("-0x", "0", Ordering::Equal); + } + #[test] + fn double_minus() { + test_helper("--1", "0", Ordering::Equal); + } + #[test] + fn single_minus() { + let info = NumInfo::parse("-", Default::default()); + assert_eq!( + info, + ( + NumInfo { + exponent: 0, + sign: Sign::Positive + }, + 0..0 + ) + ); + } + #[test] + fn invalid_with_unit() { + let info = NumInfo::parse( + "-K", + NumInfoParseSettings { + accept_si_units: true, + ..Default::default() + }, + ); + assert_eq!( + info, + ( + NumInfo { + exponent: 0, + sign: Sign::Positive + }, + 0..0 + ) + ); + } +} diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 8e79ff947..fb0241945 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -1,507 +1,1560 @@ // * This file is part of the uutils coreutils package. // * // * (c) Michael Yin +// * (c) Robert Swinford +// * (c) Michael Debertol // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -#![allow(dead_code)] -// spell-checker:ignore (ToDO) outfile nondictionary +// Although these links don't always seem to describe reality, check out the POSIX and GNU specs: +// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html +// https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html + +// spell-checker:ignore (misc) HFKJFK Mbdfhn + #[macro_use] extern crate uucore; -use clap::{App, Arg}; -use itertools::Itertools; +mod check; +mod chunks; +mod custom_str_cmp; +mod ext_sort; +mod merge; +mod numeric_str_cmp; + +use chunks::LineData; +use clap::{crate_version, App, Arg}; +use custom_str_cmp::custom_str_cmp; +use ext_sort::ext_sort; +use fnv::FnvHasher; +use numeric_str_cmp::{human_numeric_str_cmp, numeric_str_cmp, NumInfo, NumInfoParseSettings}; +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; +use rayon::prelude::*; use semver::Version; use std::cmp::Ordering; -use std::collections::BinaryHeap; +use std::env; +use std::ffi::OsStr; use std::fs::File; -use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Lines, Read, Write}; -use std::mem::replace; +use std::hash::{Hash, Hasher}; +use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; +use std::ops::Range; use std::path::Path; -use uucore::fs::is_stdin_interactive; // for Iterator::dedup() +use std::path::PathBuf; +use unicode_width::UnicodeWidthStr; +use uucore::parse_size::{parse_size, ParseSizeError}; +use uucore::InvalidEncodingHandling; -static NAME: &str = "sort"; -static ABOUT: &str = "Display sorted concatenation of all FILE(s)."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +const NAME: &str = "sort"; +const ABOUT: &str = "Display sorted concatenation of all FILE(s)."; -static OPT_HUMAN_NUMERIC_SORT: &str = "human-numeric-sort"; -static OPT_MONTH_SORT: &str = "month-sort"; -static OPT_NUMERIC_SORT: &str = "numeric-sort"; -static OPT_VERSION_SORT: &str = "version-sort"; +const LONG_HELP_KEYS: &str = "The key format is FIELD[.CHAR][OPTIONS][,FIELD[.CHAR]][OPTIONS]. -static OPT_DICTIONARY_ORDER: &str = "dictionary-order"; -static OPT_MERGE: &str = "merge"; -static OPT_CHECK: &str = "check"; -static OPT_IGNORE_CASE: &str = "ignore-case"; -static OPT_OUTPUT: &str = "output"; -static OPT_REVERSE: &str = "reverse"; -static OPT_STABLE: &str = "stable"; -static OPT_UNIQUE: &str = "unique"; +Fields by default are separated by the first whitespace after a non-whitespace character. Use -t to specify a custom separator. +In the default case, whitespace is appended at the beginning of each field. Custom separators however are not included in fields. -static ARG_FILES: &str = "files"; +FIELD and CHAR both start at 1 (i.e. they are 1-indexed). If there is no end specified after a comma, the end will be the end of the line. +If CHAR is set 0, it means the end of the field. CHAR defaults to 1 for the start position and to 0 for the end position. -static DECIMAL_PT: char = '.'; -static THOUSANDS_SEP: char = ','; +Valid options are: MbdfhnRrV. They override the global options for this key."; +mod options { + pub mod modes { + pub const SORT: &str = "sort"; + + pub const HUMAN_NUMERIC: &str = "human-numeric-sort"; + pub const MONTH: &str = "month-sort"; + pub const NUMERIC: &str = "numeric-sort"; + pub const GENERAL_NUMERIC: &str = "general-numeric-sort"; + pub const VERSION: &str = "version-sort"; + pub const RANDOM: &str = "random-sort"; + + pub const ALL_SORT_MODES: [&str; 6] = [ + GENERAL_NUMERIC, + HUMAN_NUMERIC, + MONTH, + NUMERIC, + VERSION, + RANDOM, + ]; + } + + pub mod check { + pub const CHECK: &str = "check"; + pub const CHECK_SILENT: &str = "check-silent"; + pub const SILENT: &str = "silent"; + pub const QUIET: &str = "quiet"; + pub const DIAGNOSE_FIRST: &str = "diagnose-first"; + } + + pub const DICTIONARY_ORDER: &str = "dictionary-order"; + pub const MERGE: &str = "merge"; + pub const DEBUG: &str = "debug"; + pub const IGNORE_CASE: &str = "ignore-case"; + pub const IGNORE_LEADING_BLANKS: &str = "ignore-leading-blanks"; + pub const IGNORE_NONPRINTING: &str = "ignore-nonprinting"; + pub const OUTPUT: &str = "output"; + pub const REVERSE: &str = "reverse"; + pub const STABLE: &str = "stable"; + pub const UNIQUE: &str = "unique"; + pub const KEY: &str = "key"; + pub const SEPARATOR: &str = "field-separator"; + pub const ZERO_TERMINATED: &str = "zero-terminated"; + pub const PARALLEL: &str = "parallel"; + pub const FILES0_FROM: &str = "files0-from"; + pub const BUF_SIZE: &str = "buffer-size"; + pub const TMP_DIR: &str = "temporary-directory"; + pub const COMPRESS_PROG: &str = "compress-program"; + pub const BATCH_SIZE: &str = "batch-size"; + + pub const FILES: &str = "files"; +} + +const DECIMAL_PT: char = '.'; + +const NEGATIVE: char = '-'; +const POSITIVE: char = '+'; + +// Choosing a higher buffer size does not result in performance improvements +// (at least not on my machine). TODO: In the future, we should also take the amount of +// available memory into consideration, instead of relying on this constant only. +const DEFAULT_BUF_SIZE: usize = 1_000_000_000; // 1 GB + +#[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Copy, Debug)] enum SortMode { Numeric, HumanNumeric, + GeneralNumeric, Month, Version, + Random, Default, } -struct Settings { +impl SortMode { + fn get_short_name(&self) -> Option { + match self { + SortMode::Numeric => Some('n'), + SortMode::HumanNumeric => Some('h'), + SortMode::GeneralNumeric => Some('g'), + SortMode::Month => Some('M'), + SortMode::Version => Some('V'), + SortMode::Random => Some('R'), + SortMode::Default => None, + } + } +} + +#[derive(Clone)] +pub struct GlobalSettings { mode: SortMode, + debug: bool, + ignore_leading_blanks: bool, + ignore_case: bool, + dictionary_order: bool, + ignore_non_printing: bool, merge: bool, reverse: bool, - outfile: Option, + output_file: Option, stable: bool, unique: bool, check: bool, - compare_fns: Vec Ordering>, - transform_fns: Vec String>, + check_silent: bool, + salt: String, + selectors: Vec, + separator: Option, + threads: String, + zero_terminated: bool, + buffer_size: usize, + tmp_dir: PathBuf, + compress_prog: Option, + merge_batch_size: usize, + precomputed: Precomputed, } -impl Default for Settings { - fn default() -> Settings { - Settings { +/// Data needed for sorting. Should be computed once before starting to sort +/// by calling `GlobalSettings::init_precomputed`. +#[derive(Clone, Debug)] +struct Precomputed { + needs_tokens: bool, + num_infos_per_line: usize, + floats_per_line: usize, + selections_per_line: usize, +} + +impl GlobalSettings { + /// Parse a SIZE string into a number of bytes. + /// A size string comprises an integer and an optional unit. + /// The unit may be k, K, m, M, g, G, t, T, P, E, Z, Y (powers of 1024), or b which is 1. + /// Default is K. + fn parse_byte_count(input: &str) -> Result { + // GNU sort (8.32) valid: 1b, k, K, m, M, g, G, t, T, P, E, Z, Y + // GNU sort (8.32) invalid: b, B, 1B, p, e, z, y + const ALLOW_LIST: &[char] = &[ + 'b', 'k', 'K', 'm', 'M', 'g', 'G', 't', 'T', 'P', 'E', 'Z', 'Y', + ]; + let mut size_string = input.trim().to_string(); + + if size_string.ends_with(|c: char| ALLOW_LIST.contains(&c) || c.is_digit(10)) { + // b 1, K 1024 (default) + if size_string.ends_with(|c: char| c.is_digit(10)) { + size_string.push('K'); + } else if size_string.ends_with('b') { + size_string.pop(); + } + parse_size(&size_string) + } else { + Err(ParseSizeError::ParseFailure("invalid suffix".to_string())) + } + } + + fn out_writer(&self) -> BufWriter> { + match self.output_file { + Some(ref filename) => match File::create(Path::new(&filename)) { + Ok(f) => BufWriter::new(Box::new(f) as Box), + Err(e) => { + show_error!("{0}: {1}", filename, e.to_string()); + panic!("Could not open output file"); + } + }, + None => BufWriter::new(Box::new(stdout()) as Box), + } + } + + /// Precompute some data needed for sorting. + /// This function **must** be called before starting to sort, and `GlobalSettings` may not be altered + /// afterwards. + fn init_precomputed(&mut self) { + self.precomputed.needs_tokens = self.selectors.iter().any(|s| s.needs_tokens); + self.precomputed.selections_per_line = + self.selectors.iter().filter(|s| s.needs_selection).count(); + self.precomputed.num_infos_per_line = self + .selectors + .iter() + .filter(|s| matches!(s.settings.mode, SortMode::Numeric | SortMode::HumanNumeric)) + .count(); + self.precomputed.floats_per_line = self + .selectors + .iter() + .filter(|s| matches!(s.settings.mode, SortMode::GeneralNumeric)) + .count(); + } +} + +impl Default for GlobalSettings { + fn default() -> GlobalSettings { + GlobalSettings { mode: SortMode::Default, + debug: false, + ignore_leading_blanks: false, + ignore_case: false, + dictionary_order: false, + ignore_non_printing: false, merge: false, reverse: false, - outfile: None, + output_file: None, stable: false, unique: false, check: false, - compare_fns: Vec::new(), - transform_fns: Vec::new(), + check_silent: false, + salt: String::new(), + selectors: vec![], + separator: None, + threads: String::new(), + zero_terminated: false, + buffer_size: DEFAULT_BUF_SIZE, + tmp_dir: PathBuf::new(), + compress_prog: None, + merge_batch_size: 32, + precomputed: Precomputed { + num_infos_per_line: 0, + floats_per_line: 0, + selections_per_line: 0, + needs_tokens: false, + }, } } } -struct MergeableFile<'a> { - lines: Lines>>, - current_line: String, - settings: &'a Settings, +#[derive(Clone, PartialEq, Debug)] +struct KeySettings { + mode: SortMode, + ignore_blanks: bool, + ignore_case: bool, + dictionary_order: bool, + ignore_non_printing: bool, + reverse: bool, } -// BinaryHeap depends on `Ord`. Note that we want to pop smallest items -// from the heap first, and BinaryHeap.pop() returns the largest, so we -// trick it into the right order by calling reverse() here. -impl<'a> Ord for MergeableFile<'a> { - fn cmp(&self, other: &MergeableFile) -> Ordering { - compare_by(&self.current_line, &other.current_line, &self.settings).reverse() - } -} - -impl<'a> PartialOrd for MergeableFile<'a> { - fn partial_cmp(&self, other: &MergeableFile) -> Option { - Some(self.cmp(other)) - } -} - -impl<'a> PartialEq for MergeableFile<'a> { - fn eq(&self, other: &MergeableFile) -> bool { - Ordering::Equal == compare_by(&self.current_line, &other.current_line, &self.settings) - } -} - -impl<'a> Eq for MergeableFile<'a> {} - -struct FileMerger<'a> { - heap: BinaryHeap>, - settings: &'a Settings, -} - -impl<'a> FileMerger<'a> { - fn new(settings: &'a Settings) -> FileMerger<'a> { - FileMerger { - heap: BinaryHeap::new(), - settings, +impl KeySettings { + /// Checks if the supplied combination of `mode`, `ignore_non_printing` and `dictionary_order` is allowed. + fn check_compatibility( + mode: SortMode, + ignore_non_printing: bool, + dictionary_order: bool, + ) -> Result<(), String> { + if matches!( + mode, + SortMode::Numeric | SortMode::HumanNumeric | SortMode::GeneralNumeric | SortMode::Month + ) { + if dictionary_order { + return Err(format!( + "options '-{}{}' are incompatible", + 'd', + mode.get_short_name().unwrap() + )); + } else if ignore_non_printing { + return Err(format!( + "options '-{}{}' are incompatible", + 'i', + mode.get_short_name().unwrap() + )); + } } + Ok(()) } - fn push_file(&mut self, mut lines: Lines>>) { - if let Some(Ok(next_line)) = lines.next() { - let mergeable_file = MergeableFile { - lines, - current_line: next_line, - settings: &self.settings, - }; - self.heap.push(mergeable_file); + + fn set_sort_mode(&mut self, mode: SortMode) -> Result<(), String> { + if self.mode != SortMode::Default { + return Err(format!( + "options '-{}{}' are incompatible", + self.mode.get_short_name().unwrap(), + mode.get_short_name().unwrap() + )); + } + Self::check_compatibility(mode, self.ignore_non_printing, self.dictionary_order)?; + self.mode = mode; + Ok(()) + } + + fn set_dictionary_order(&mut self) -> Result<(), String> { + Self::check_compatibility(self.mode, self.ignore_non_printing, true)?; + self.dictionary_order = true; + Ok(()) + } + + fn set_ignore_non_printing(&mut self) -> Result<(), String> { + Self::check_compatibility(self.mode, true, self.dictionary_order)?; + self.ignore_non_printing = true; + Ok(()) + } +} + +impl From<&GlobalSettings> for KeySettings { + fn from(settings: &GlobalSettings) -> Self { + Self { + mode: settings.mode, + ignore_blanks: settings.ignore_leading_blanks, + ignore_case: settings.ignore_case, + ignore_non_printing: settings.ignore_non_printing, + reverse: settings.reverse, + dictionary_order: settings.dictionary_order, } } } -impl<'a> Iterator for FileMerger<'a> { - type Item = String; - fn next(&mut self) -> Option { - match self.heap.pop() { - Some(mut current) => { - match current.lines.next() { - Some(Ok(next_line)) => { - let ret = replace(&mut current.current_line, next_line); - self.heap.push(current); - Some(ret) - } - _ => { - // Don't put it back in the heap (it's empty/erroring) - // but its first line is still valid. - Some(current.current_line) +impl Default for KeySettings { + fn default() -> Self { + Self::from(&GlobalSettings::default()) + } +} +enum Selection<'a> { + AsF64(GeneralF64ParseResult), + WithNumInfo(&'a str, NumInfo), + Str(&'a str), +} + +type Field = Range; + +#[derive(Clone, Debug)] +pub struct Line<'a> { + line: &'a str, + index: usize, +} + +impl<'a> Line<'a> { + /// Creates a new `Line`. + /// + /// If additional data is needed for sorting it is added to `line_data`. + /// `token_buffer` allows to reuse the allocation for tokens. + fn create( + line: &'a str, + index: usize, + line_data: &mut LineData<'a>, + token_buffer: &mut Vec, + settings: &GlobalSettings, + ) -> Self { + token_buffer.clear(); + if settings.precomputed.needs_tokens { + tokenize(line, settings.separator, token_buffer); + } + for (selector, selection) in settings + .selectors + .iter() + .map(|selector| (selector, selector.get_selection(line, token_buffer))) + { + match selection { + Selection::AsF64(parsed_float) => line_data.parsed_floats.push(parsed_float), + Selection::WithNumInfo(str, num_info) => { + line_data.num_infos.push(num_info); + line_data.selections.push(str); + } + Selection::Str(str) => { + if selector.needs_selection { + line_data.selections.push(str) } } } - None => None, + } + Self { line, index } + } + + fn print(&self, writer: &mut impl Write, settings: &GlobalSettings) { + if settings.zero_terminated && !settings.debug { + writer.write_all(self.line.as_bytes()).unwrap(); + writer.write_all(b"\0").unwrap(); + } else if !settings.debug { + writer.write_all(self.line.as_bytes()).unwrap(); + writer.write_all(b"\n").unwrap(); + } else { + self.print_debug(settings, writer).unwrap(); + } + } + + /// Writes indicators for the selections this line matched. The original line content is NOT expected + /// to be already printed. + fn print_debug( + &self, + settings: &GlobalSettings, + writer: &mut impl Write, + ) -> std::io::Result<()> { + // We do not consider this function performance critical, as debug output is only useful for small files, + // which are not a performance problem in any case. Therefore there aren't any special performance + // optimizations here. + + let line = self.line.replace('\t', ">"); + writeln!(writer, "{}", line)?; + + let mut fields = vec![]; + tokenize(self.line, settings.separator, &mut fields); + for selector in settings.selectors.iter() { + let mut selection = selector.get_range(self.line, Some(&fields)); + match selector.settings.mode { + SortMode::Numeric | SortMode::HumanNumeric => { + // find out which range is used for numeric comparisons + let (_, num_range) = NumInfo::parse( + &self.line[selection.clone()], + NumInfoParseSettings { + accept_si_units: selector.settings.mode == SortMode::HumanNumeric, + ..Default::default() + }, + ); + let initial_selection = selection.clone(); + + // Shorten selection to num_range. + selection.start += num_range.start; + selection.end = selection.start + num_range.len(); + + if num_range != (0..0) { + // include a trailing si unit + if selector.settings.mode == SortMode::HumanNumeric + && self.line[selection.end..initial_selection.end] + .starts_with(&['k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'][..]) + { + selection.end += 1; + } + + // include leading zeroes, a leading minus or a leading decimal point + while self.line[initial_selection.start..selection.start] + .ends_with(&['-', '0', '.'][..]) + { + selection.start -= 1; + } + } else { + // This was not a valid number. + // Report no match at the first non-whitespace character. + let leading_whitespace = self.line[selection.clone()] + .find(|c: char| !c.is_whitespace()) + .unwrap_or(0); + selection.start += leading_whitespace; + selection.end += leading_whitespace; + } + } + SortMode::GeneralNumeric => { + let initial_selection = &self.line[selection.clone()]; + + let leading = get_leading_gen(initial_selection); + + // Shorten selection to leading. + selection.start += leading.start; + selection.end = selection.start + leading.len(); + } + SortMode::Month => { + let initial_selection = &self.line[selection.clone()]; + + let mut month_chars = initial_selection + .char_indices() + .skip_while(|(_, c)| c.is_whitespace()); + + let month = if month_parse(initial_selection) == Month::Unknown { + // We failed to parse a month, which is equivalent to matching nothing. + // Add the "no match for key" marker to the first non-whitespace character. + let first_non_whitespace = month_chars.next(); + first_non_whitespace.map_or( + initial_selection.len()..initial_selection.len(), + |(idx, _)| idx..idx, + ) + } else { + // We parsed a month. Match the first three non-whitespace characters, which must be the month we parsed. + month_chars.next().unwrap().0 + ..month_chars + .nth(2) + .map_or(initial_selection.len(), |(idx, _)| idx) + }; + + // Shorten selection to month. + selection.start += month.start; + selection.end = selection.start + month.len(); + } + _ => {} + } + + write!( + writer, + "{}", + " ".repeat(UnicodeWidthStr::width(&line[..selection.start])) + )?; + + // TODO: Once our minimum supported rust version is at least 1.47, use selection.is_empty() instead. + #[allow(clippy::len_zero)] + { + if selection.len() == 0 { + writeln!(writer, "^ no match for key")?; + } else { + writeln!( + writer, + "{}", + "_".repeat(UnicodeWidthStr::width(&line[selection])) + )?; + } + } + } + if settings.mode != SortMode::Random + && !settings.stable + && !settings.unique + && (settings.dictionary_order + || settings.ignore_leading_blanks + || settings.ignore_case + || settings.ignore_non_printing + || settings.mode != SortMode::Default + || settings + .selectors + .last() + .map_or(true, |selector| selector != &Default::default())) + { + // A last resort comparator is in use, underline the whole line. + if self.line.is_empty() { + writeln!(writer, "^ no match for key")?; + } else { + writeln!( + writer, + "{}", + "_".repeat(UnicodeWidthStr::width(line.as_str())) + )?; + } + } + Ok(()) + } +} + +/// Tokenize a line into fields. The result is stored into `token_buffer`. +fn tokenize(line: &str, separator: Option, token_buffer: &mut Vec) { + assert!(token_buffer.is_empty()); + if let Some(separator) = separator { + tokenize_with_separator(line, separator, token_buffer) + } else { + tokenize_default(line, token_buffer) + } +} + +/// By default fields are separated by the first whitespace after non-whitespace. +/// Whitespace is included in fields at the start. +/// The result is stored into `token_buffer`. +fn tokenize_default(line: &str, token_buffer: &mut Vec) { + token_buffer.push(0..0); + // pretend that there was whitespace in front of the line + let mut previous_was_whitespace = true; + for (idx, char) in line.char_indices() { + if char.is_whitespace() { + if !previous_was_whitespace { + token_buffer.last_mut().unwrap().end = idx; + token_buffer.push(idx..0); + } + previous_was_whitespace = true; + } else { + previous_was_whitespace = false; + } + } + token_buffer.last_mut().unwrap().end = line.len(); +} + +/// Split between separators. These separators are not included in fields. +/// The result is stored into `token_buffer`. +fn tokenize_with_separator(line: &str, separator: char, token_buffer: &mut Vec) { + let separator_indices = + line.char_indices() + .filter_map(|(i, c)| if c == separator { Some(i) } else { None }); + let mut start = 0; + for sep_idx in separator_indices { + token_buffer.push(start..sep_idx); + start = sep_idx + 1; + } + if start < line.len() { + token_buffer.push(start..line.len()); + } +} + +#[derive(Clone, PartialEq, Debug)] +struct KeyPosition { + /// 1-indexed, 0 is invalid. + field: usize, + /// 1-indexed, 0 is end of field. + char: usize, + ignore_blanks: bool, +} + +impl KeyPosition { + fn new(key: &str, default_char_index: usize, ignore_blanks: bool) -> Result { + let mut field_and_char = key.split('.'); + + let field = field_and_char + .next() + .ok_or_else(|| format!("invalid key `{}`", key))?; + let char = field_and_char.next(); + + let field = field + .parse() + .map_err(|e| format!("failed to parse field index `{}`: {}", field, e))?; + if field == 0 { + return Err("field index can not be 0".to_string()); + } + + let char = char.map_or(Ok(default_char_index), |char| { + char.parse() + .map_err(|e| format!("failed to parse character index `{}`: {}", char, e)) + })?; + + Ok(Self { + field, + char, + ignore_blanks, + }) + } +} + +impl Default for KeyPosition { + fn default() -> Self { + KeyPosition { + field: 1, + char: 1, + ignore_blanks: false, } } } + +#[derive(Clone, PartialEq, Debug)] +struct FieldSelector { + from: KeyPosition, + to: Option, + settings: KeySettings, + needs_tokens: bool, + // Whether this selector operates on a sub-slice of a line. + // Selections are therefore not needed when this selector matches the whole line + // or the sort mode is general-numeric. + needs_selection: bool, +} + +impl Default for FieldSelector { + fn default() -> Self { + Self { + from: Default::default(), + to: None, + settings: Default::default(), + needs_tokens: false, + needs_selection: false, + } + } +} + +impl FieldSelector { + /// Splits this position into the actual position and the attached options. + fn split_key_options(position: &str) -> (&str, &str) { + if let Some((options_start, _)) = position.char_indices().find(|(_, c)| c.is_alphabetic()) { + position.split_at(options_start) + } else { + (position, "") + } + } + + fn parse(key: &str, global_settings: &GlobalSettings) -> Self { + let mut from_to = key.split(','); + let (from, from_options) = Self::split_key_options(from_to.next().unwrap()); + let to = from_to.next().map(|to| Self::split_key_options(to)); + let options_are_empty = from_options.is_empty() && matches!(to, None | Some((_, ""))); + crash_if_err!( + 2, + if options_are_empty { + // Inherit the global settings if there are no options attached to this key. + (|| { + // This would be ideal for a try block, I think. In the meantime this closure allows + // to use the `?` operator here. + Self::new( + KeyPosition::new(from, 1, global_settings.ignore_leading_blanks)?, + to.map(|(to, _)| { + KeyPosition::new(to, 0, global_settings.ignore_leading_blanks) + }) + .transpose()?, + KeySettings::from(global_settings), + ) + })() + } else { + // Do not inherit from `global_settings`, as there are options attached to this key. + Self::parse_with_options((from, from_options), to) + } + .map_err(|e| format!("failed to parse key `{}`: {}", key, e)) + ) + } + + fn parse_with_options( + (from, from_options): (&str, &str), + to: Option<(&str, &str)>, + ) -> Result { + /// Applies `options` to `key_settings`, returning if the 'b'-flag (ignore blanks) was present. + fn parse_key_settings( + options: &str, + key_settings: &mut KeySettings, + ) -> Result { + let mut ignore_blanks = false; + for option in options.chars() { + match option { + 'M' => key_settings.set_sort_mode(SortMode::Month)?, + 'b' => ignore_blanks = true, + 'd' => key_settings.set_dictionary_order()?, + 'f' => key_settings.ignore_case = true, + 'g' => key_settings.set_sort_mode(SortMode::GeneralNumeric)?, + 'h' => key_settings.set_sort_mode(SortMode::HumanNumeric)?, + 'i' => key_settings.set_ignore_non_printing()?, + 'n' => key_settings.set_sort_mode(SortMode::Numeric)?, + 'R' => key_settings.set_sort_mode(SortMode::Random)?, + 'r' => key_settings.reverse = true, + 'V' => key_settings.set_sort_mode(SortMode::Version)?, + c => return Err(format!("invalid option: `{}`", c)), + } + } + Ok(ignore_blanks) + } + + let mut key_settings = KeySettings::default(); + let from = parse_key_settings(from_options, &mut key_settings) + .map(|ignore_blanks| KeyPosition::new(from, 1, ignore_blanks))??; + let to = if let Some((to, to_options)) = to { + Some( + parse_key_settings(to_options, &mut key_settings) + .map(|ignore_blanks| KeyPosition::new(to, 0, ignore_blanks))??, + ) + } else { + None + }; + Self::new(from, to, key_settings) + } + + fn new( + from: KeyPosition, + to: Option, + settings: KeySettings, + ) -> Result { + if from.char == 0 { + Err("invalid character index 0 for the start position of a field".to_string()) + } else { + Ok(Self { + needs_selection: (from.field != 1 + || from.char != 1 + || to.is_some() + || matches!(settings.mode, SortMode::Numeric | SortMode::HumanNumeric) + || from.ignore_blanks) + && !matches!(settings.mode, SortMode::GeneralNumeric), + needs_tokens: from.field != 1 || from.char == 0 || to.is_some(), + from, + to, + settings, + }) + } + } + + /// Get the selection that corresponds to this selector for the line. + /// If needs_fields returned false, tokens may be empty. + fn get_selection<'a>(&self, line: &'a str, tokens: &[Field]) -> Selection<'a> { + // `get_range` expects `None` when we don't need tokens and would get confused by an empty vector. + let tokens = if self.needs_tokens { + Some(tokens) + } else { + None + }; + let mut range = &line[self.get_range(line, tokens)]; + if self.settings.mode == SortMode::Numeric || self.settings.mode == SortMode::HumanNumeric { + // Parse NumInfo for this number. + let (info, num_range) = NumInfo::parse( + range, + NumInfoParseSettings { + accept_si_units: self.settings.mode == SortMode::HumanNumeric, + ..Default::default() + }, + ); + // Shorten the range to what we need to pass to numeric_str_cmp later. + range = &range[num_range]; + Selection::WithNumInfo(range, info) + } else if self.settings.mode == SortMode::GeneralNumeric { + // Parse this number as f64, as this is the requirement for general numeric sorting. + Selection::AsF64(general_f64_parse(&range[get_leading_gen(range)])) + } else { + // This is not a numeric sort, so we don't need a NumCache. + Selection::Str(range) + } + } + + /// Look up the range in the line that corresponds to this selector. + /// If needs_fields returned false, tokens must be None. + fn get_range<'a>(&self, line: &'a str, tokens: Option<&[Field]>) -> Range { + enum Resolution { + // The start index of the resolved character, inclusive + StartOfChar(usize), + // The end index of the resolved character, exclusive. + // This is only returned if the character index is 0. + EndOfChar(usize), + // The resolved character would be in front of the first character + TooLow, + // The resolved character would be after the last character + TooHigh, + } + + // Get the index for this line given the KeyPosition + fn resolve_index( + line: &str, + tokens: Option<&[Field]>, + position: &KeyPosition, + ) -> Resolution { + if matches!(tokens, Some(tokens) if tokens.len() < position.field) { + Resolution::TooHigh + } else if position.char == 0 { + let end = tokens.unwrap()[position.field - 1].end; + if end == 0 { + Resolution::TooLow + } else { + Resolution::EndOfChar(end) + } + } else { + let mut idx = if position.field == 1 { + // The first field always starts at 0. + // We don't need tokens for this case. + 0 + } else { + tokens.unwrap()[position.field - 1].start + }; + // strip blanks if needed + if position.ignore_blanks { + idx += line[idx..] + .char_indices() + .find(|(_, c)| !c.is_whitespace()) + .map_or(line[idx..].len(), |(idx, _)| idx); + } + // apply the character index + idx += line[idx..] + .char_indices() + .nth(position.char - 1) + .map_or(line[idx..].len(), |(idx, _)| idx); + if idx >= line.len() { + Resolution::TooHigh + } else { + Resolution::StartOfChar(idx) + } + } + } + + match resolve_index(line, tokens, &self.from) { + Resolution::StartOfChar(from) => { + let to = self.to.as_ref().map(|to| resolve_index(line, tokens, to)); + + let mut range = match to { + Some(Resolution::StartOfChar(mut to)) => { + // We need to include the character at `to`. + to += line[to..].chars().next().map_or(1, |c| c.len_utf8()); + from..to + } + Some(Resolution::EndOfChar(to)) => from..to, + // If `to` was not given or the match would be after the end of the line, + // match everything until the end of the line. + None | Some(Resolution::TooHigh) => from..line.len(), + // If `to` is before the start of the line, report no match. + // This can happen if the line starts with a separator. + Some(Resolution::TooLow) => 0..0, + }; + if range.start > range.end { + range.end = range.start; + } + range + } + Resolution::TooLow | Resolution::EndOfChar(_) => { + unreachable!("This should only happen if the field start index is 0, but that should already have caused an error.") + } + // While for comparisons it's only important that this is an empty slice, + // to produce accurate debug output we need to match an empty slice at the end of the line. + Resolution::TooHigh => line.len()..line.len(), + } + } +} + fn get_usage() -> String { format!( - "{0} {1} - + "{0} Usage: {0} [OPTION]... [FILE]... - Write the sorted concatenation of all FILE(s) to standard output. - Mandatory arguments for long options are mandatory for short options too. - With no FILE, or when FILE is -, read standard input.", - NAME, VERSION + NAME ) } -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - let usage = get_usage(); - let mut settings: Settings = Default::default(); +/// Creates an `Arg` that conflicts with all other sort modes. +fn make_sort_mode_arg<'a, 'b>(mode: &'a str, short: &'b str, help: &'b str) -> Arg<'a, 'b> { + let mut arg = Arg::with_name(mode).short(short).long(mode).help(help); + for possible_mode in &options::modes::ALL_SORT_MODES { + if *possible_mode != mode { + arg = arg.conflicts_with(possible_mode); + } + } + arg +} - let matches = App::new(executable!()) - .version(VERSION) +pub fn uumain(args: impl uucore::Args) -> i32 { + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); + let usage = get_usage(); + let mut settings: GlobalSettings = Default::default(); + + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + settings.debug = matches.is_present(options::DEBUG); + + // check whether user specified a zero terminated list of files for input, otherwise read files from args + let mut files: Vec = if matches.is_present(options::FILES0_FROM) { + let files0_from: Vec = matches + .values_of(options::FILES0_FROM) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + let mut files = Vec::new(); + for path in &files0_from { + let reader = open(path.as_str()); + let buf_reader = BufReader::new(reader); + for line in buf_reader.split(b'\0').flatten() { + files.push( + std::str::from_utf8(&line) + .expect("Could not parse string from zero terminated input.") + .to_string(), + ); + } + } + files + } else { + matches + .values_of(options::FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default() + }; + + settings.mode = if matches.is_present(options::modes::HUMAN_NUMERIC) + || matches.value_of(options::modes::SORT) == Some("human-numeric") + { + SortMode::HumanNumeric + } else if matches.is_present(options::modes::MONTH) + || matches.value_of(options::modes::SORT) == Some("month") + { + SortMode::Month + } else if matches.is_present(options::modes::GENERAL_NUMERIC) + || matches.value_of(options::modes::SORT) == Some("general-numeric") + { + SortMode::GeneralNumeric + } else if matches.is_present(options::modes::NUMERIC) + || matches.value_of(options::modes::SORT) == Some("numeric") + { + SortMode::Numeric + } else if matches.is_present(options::modes::VERSION) + || matches.value_of(options::modes::SORT) == Some("version") + { + SortMode::Version + } else if matches.is_present(options::modes::RANDOM) + || matches.value_of(options::modes::SORT) == Some("random") + { + settings.salt = get_rand_string(); + SortMode::Random + } else { + SortMode::Default + }; + + settings.dictionary_order = matches.is_present(options::DICTIONARY_ORDER); + settings.ignore_non_printing = matches.is_present(options::IGNORE_NONPRINTING); + if matches.is_present(options::PARALLEL) { + // "0" is default - threads = num of cores + settings.threads = matches + .value_of(options::PARALLEL) + .map(String::from) + .unwrap_or_else(|| "0".to_string()); + env::set_var("RAYON_NUM_THREADS", &settings.threads); + } + + settings.buffer_size = matches + .value_of(options::BUF_SIZE) + .map_or(DEFAULT_BUF_SIZE, |s| { + GlobalSettings::parse_byte_count(s) + .unwrap_or_else(|e| crash!(2, "{}", format_error_message(e, s, options::BUF_SIZE))) + }); + + settings.tmp_dir = matches + .value_of(options::TMP_DIR) + .map(PathBuf::from) + .unwrap_or_else(env::temp_dir); + + settings.compress_prog = matches.value_of(options::COMPRESS_PROG).map(String::from); + + if let Some(n_merge) = matches.value_of(options::BATCH_SIZE) { + settings.merge_batch_size = n_merge + .parse() + .unwrap_or_else(|_| crash!(2, "invalid --batch-size argument '{}'", n_merge)); + } + + settings.zero_terminated = matches.is_present(options::ZERO_TERMINATED); + settings.merge = matches.is_present(options::MERGE); + + settings.check = matches.is_present(options::check::CHECK); + if matches.is_present(options::check::CHECK_SILENT) + || matches!( + matches.value_of(options::check::CHECK), + Some(options::check::SILENT) | Some(options::check::QUIET) + ) + { + settings.check_silent = true; + settings.check = true; + }; + + settings.ignore_case = matches.is_present(options::IGNORE_CASE); + + settings.ignore_leading_blanks = matches.is_present(options::IGNORE_LEADING_BLANKS); + + settings.output_file = matches.value_of(options::OUTPUT).map(String::from); + settings.reverse = matches.is_present(options::REVERSE); + settings.stable = matches.is_present(options::STABLE); + settings.unique = matches.is_present(options::UNIQUE); + + if files.is_empty() { + /* if no file, default to stdin */ + files.push("-".to_owned()); + } else if settings.check && files.len() != 1 { + crash!(1, "extra operand `{}' not allowed with -c", files[1]) + } + + if let Some(arg) = matches.args.get(options::SEPARATOR) { + let separator = arg.vals[0].to_string_lossy(); + let separator = separator; + if separator.len() != 1 { + crash!(1, "separator must be exactly one character long"); + } + settings.separator = Some(separator.chars().next().unwrap()) + } + + if let Some(values) = matches.values_of(options::KEY) { + for value in values { + settings + .selectors + .push(FieldSelector::parse(value, &settings)); + } + } + + if !matches.is_present(options::KEY) { + // add a default selector matching the whole line + let key_settings = KeySettings::from(&settings); + settings.selectors.push( + FieldSelector::new( + KeyPosition { + field: 1, + char: 1, + ignore_blanks: key_settings.ignore_blanks, + }, + None, + key_settings, + ) + .unwrap(), + ); + } + + settings.init_precomputed(); + + exec(&files, &settings) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( - Arg::with_name(OPT_HUMAN_NUMERIC_SORT) - .short("h") - .long(OPT_HUMAN_NUMERIC_SORT) - .help("compare according to human readable sizes, eg 1M > 100k"), + Arg::with_name(options::modes::SORT) + .long(options::modes::SORT) + .takes_value(true) + .possible_values( + &[ + "general-numeric", + "human-numeric", + "month", + "numeric", + "version", + "random", + ] + ) + .conflicts_with_all(&options::modes::ALL_SORT_MODES) ) .arg( - Arg::with_name(OPT_MONTH_SORT) - .short("M") - .long(OPT_MONTH_SORT) - .help("compare according to month name abbreviation"), + make_sort_mode_arg( + options::modes::HUMAN_NUMERIC, + "h", + "compare according to human readable sizes, eg 1M > 100k" + ), ) .arg( - Arg::with_name(OPT_NUMERIC_SORT) - .short("n") - .long(OPT_NUMERIC_SORT) - .help("compare according to string numerical value"), + make_sort_mode_arg( + options::modes::MONTH, + "M", + "compare according to month name abbreviation" + ), ) .arg( - Arg::with_name(OPT_VERSION_SORT) - .short("V") - .long(OPT_VERSION_SORT) - .help("Sort by SemVer version number, eg 1.12.2 > 1.1.2"), + make_sort_mode_arg( + options::modes::NUMERIC, + "n", + "compare according to string numerical value" + ), ) .arg( - Arg::with_name(OPT_DICTIONARY_ORDER) + make_sort_mode_arg( + options::modes::GENERAL_NUMERIC, + "g", + "compare according to string general numerical value" + ), + ) + .arg( + make_sort_mode_arg( + options::modes::VERSION, + "V", + "Sort by SemVer version number, eg 1.12.2 > 1.1.2", + ), + ) + .arg( + make_sort_mode_arg( + options::modes::RANDOM, + "R", + "shuffle in random order", + ), + ) + .arg( + Arg::with_name(options::DICTIONARY_ORDER) .short("d") - .long(OPT_DICTIONARY_ORDER) - .help("consider only blanks and alphanumeric characters"), + .long(options::DICTIONARY_ORDER) + .help("consider only blanks and alphanumeric characters") + .conflicts_with_all( + &[ + options::modes::NUMERIC, + options::modes::GENERAL_NUMERIC, + options::modes::HUMAN_NUMERIC, + options::modes::MONTH, + ] + ), ) .arg( - Arg::with_name(OPT_MERGE) + Arg::with_name(options::MERGE) .short("m") - .long(OPT_MERGE) + .long(options::MERGE) .help("merge already sorted files; do not sort"), ) .arg( - Arg::with_name(OPT_CHECK) + Arg::with_name(options::check::CHECK) .short("c") - .long(OPT_CHECK) + .long(options::check::CHECK) + .takes_value(true) + .require_equals(true) + .min_values(0) + .possible_values(&[ + options::check::SILENT, + options::check::QUIET, + options::check::DIAGNOSE_FIRST, + ]) .help("check for sorted input; do not sort"), ) .arg( - Arg::with_name(OPT_IGNORE_CASE) + Arg::with_name(options::check::CHECK_SILENT) + .short("C") + .long(options::check::CHECK_SILENT) + .help("exit successfully if the given file is already sorted, and exit with status 1 otherwise."), + ) + .arg( + Arg::with_name(options::IGNORE_CASE) .short("f") - .long(OPT_IGNORE_CASE) + .long(options::IGNORE_CASE) .help("fold lower case to upper case characters"), ) .arg( - Arg::with_name(OPT_OUTPUT) + Arg::with_name(options::IGNORE_NONPRINTING) + .short("i") + .long(options::IGNORE_NONPRINTING) + .help("ignore nonprinting characters") + .conflicts_with_all( + &[ + options::modes::NUMERIC, + options::modes::GENERAL_NUMERIC, + options::modes::HUMAN_NUMERIC, + options::modes::MONTH + ] + ), + ) + .arg( + Arg::with_name(options::IGNORE_LEADING_BLANKS) + .short("b") + .long(options::IGNORE_LEADING_BLANKS) + .help("ignore leading blanks when finding sort keys in each line"), + ) + .arg( + Arg::with_name(options::OUTPUT) .short("o") - .long(OPT_OUTPUT) + .long(options::OUTPUT) .help("write output to FILENAME instead of stdout") .takes_value(true) .value_name("FILENAME"), ) .arg( - Arg::with_name(OPT_REVERSE) + Arg::with_name(options::REVERSE) .short("r") - .long(OPT_REVERSE) + .long(options::REVERSE) .help("reverse the output"), ) .arg( - Arg::with_name(OPT_STABLE) + Arg::with_name(options::STABLE) .short("s") - .long(OPT_STABLE) + .long(options::STABLE) .help("stabilize sort by disabling last-resort comparison"), ) .arg( - Arg::with_name(OPT_UNIQUE) + Arg::with_name(options::UNIQUE) .short("u") - .long(OPT_UNIQUE) + .long(options::UNIQUE) .help("output only the first of an equal run"), ) - .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) - .get_matches_from(args); - - let mut files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - settings.mode = if matches.is_present(OPT_HUMAN_NUMERIC_SORT) { - SortMode::HumanNumeric - } else if matches.is_present(OPT_MONTH_SORT) { - SortMode::Month - } else if matches.is_present(OPT_NUMERIC_SORT) { - SortMode::Numeric - } else if matches.is_present(OPT_VERSION_SORT) { - SortMode::Version - } else { - SortMode::Default - }; - - if matches.is_present(OPT_DICTIONARY_ORDER) { - settings.transform_fns.push(remove_nondictionary_chars); - } - - settings.merge = matches.is_present(OPT_MERGE); - settings.check = matches.is_present(OPT_CHECK); - - if matches.is_present(OPT_IGNORE_CASE) { - settings.transform_fns.push(|s| s.to_uppercase()); - } - - settings.outfile = matches.value_of(OPT_OUTPUT).map(String::from); - settings.reverse = matches.is_present(OPT_REVERSE); - settings.stable = matches.is_present(OPT_STABLE); - settings.unique = matches.is_present(OPT_UNIQUE); - - //let mut files = matches.free; - if files.is_empty() { - /* if no file, default to stdin */ - files.push("-".to_owned()); - } else if settings.check && files.len() != 1 { - crash!(1, "sort: extra operand `{}' not allowed with -c", files[1]) - } - - settings.compare_fns.push(match settings.mode { - SortMode::Numeric => numeric_compare, - SortMode::HumanNumeric => human_numeric_size_compare, - SortMode::Month => month_compare, - SortMode::Version => version_compare, - SortMode::Default => default_compare, - }); - - if !settings.stable { - match settings.mode { - SortMode::Default => {} - _ => settings.compare_fns.push(default_compare), - } - } - - exec(files, &settings) + .arg( + Arg::with_name(options::KEY) + .short("k") + .long(options::KEY) + .help("sort by a key") + .long_help(LONG_HELP_KEYS) + .multiple(true) + .takes_value(true), + ) + .arg( + Arg::with_name(options::SEPARATOR) + .short("t") + .long(options::SEPARATOR) + .help("custom separator for -k") + .takes_value(true)) + .arg( + Arg::with_name(options::ZERO_TERMINATED) + .short("z") + .long(options::ZERO_TERMINATED) + .help("line delimiter is NUL, not newline"), + ) + .arg( + Arg::with_name(options::PARALLEL) + .long(options::PARALLEL) + .help("change the number of threads running concurrently to NUM_THREADS") + .takes_value(true) + .value_name("NUM_THREADS"), + ) + .arg( + Arg::with_name(options::BUF_SIZE) + .short("S") + .long(options::BUF_SIZE) + .help("sets the maximum SIZE of each segment in number of sorted items") + .takes_value(true) + .value_name("SIZE"), + ) + .arg( + Arg::with_name(options::TMP_DIR) + .short("T") + .long(options::TMP_DIR) + .help("use DIR for temporaries, not $TMPDIR or /tmp") + .takes_value(true) + .value_name("DIR"), + ) + .arg( + Arg::with_name(options::COMPRESS_PROG) + .long(options::COMPRESS_PROG) + .help("compress temporary files with PROG, decompress with PROG -d") + .long_help("PROG has to take input from stdin and output to stdout") + .value_name("PROG") + ) + .arg( + Arg::with_name(options::BATCH_SIZE) + .long(options::BATCH_SIZE) + .help("Merge at most N_MERGE inputs at once.") + .value_name("N_MERGE") + ) + .arg( + Arg::with_name(options::FILES0_FROM) + .long(options::FILES0_FROM) + .help("read input from the files specified by NUL-terminated NUL_FILES") + .takes_value(true) + .value_name("NUL_FILES") + .multiple(true), + ) + .arg( + Arg::with_name(options::DEBUG) + .long(options::DEBUG) + .help("underline the parts of the line that are actually used for sorting"), + ) + .arg(Arg::with_name(options::FILES).multiple(true).takes_value(true)) } -fn exec(files: Vec, settings: &Settings) -> i32 { - let mut lines = Vec::new(); - let mut file_merger = FileMerger::new(&settings); - - for path in &files { - let (reader, _) = match open(path) { - Some(x) => x, - None => continue, - }; - - let buf_reader = BufReader::new(reader); - - if settings.merge { - file_merger.push_file(buf_reader.lines()); - } else if settings.check { - return exec_check_file(buf_reader.lines(), &settings); - } else { - for line in buf_reader.lines() { - if let Ok(n) = line { - lines.push(n); - } else { - break; - } - } - } - } - - sort_by(&mut lines, &settings); - +fn exec(files: &[String], settings: &GlobalSettings) -> i32 { if settings.merge { - if settings.unique { - print_sorted(file_merger.dedup(), &settings.outfile) - } else { - print_sorted(file_merger, &settings.outfile) + let mut file_merger = merge::merge(files.iter().map(open), settings); + file_merger.write_all(settings); + } else if settings.check { + if files.len() > 1 { + crash!(1, "only one file allowed with -c"); } - } else if settings.unique { - print_sorted(lines.iter().dedup(), &settings.outfile) + return check::check(files.first().unwrap(), settings); } else { - print_sorted(lines.iter(), &settings.outfile) - } + let mut lines = files.iter().map(open); + ext_sort(&mut lines, settings); + } 0 } -fn exec_check_file(lines: Lines>>, settings: &Settings) -> i32 { - // errors yields the line before each disorder, - // plus the last line (quirk of .coalesce()) - let unwrapped_lines = lines.filter_map(|maybe_line| { - if let Ok(line) = maybe_line { - Some(line) +fn sort_by<'a>(unsorted: &mut Vec>, settings: &GlobalSettings, line_data: &LineData<'a>) { + if settings.stable || settings.unique { + unsorted.par_sort_by(|a, b| compare_by(a, b, settings, line_data, line_data)) + } else { + unsorted.par_sort_unstable_by(|a, b| compare_by(a, b, settings, line_data, line_data)) + } +} + +fn compare_by<'a>( + a: &Line<'a>, + b: &Line<'a>, + global_settings: &GlobalSettings, + a_line_data: &LineData<'a>, + b_line_data: &LineData<'a>, +) -> Ordering { + let mut selection_index = 0; + let mut num_info_index = 0; + let mut parsed_float_index = 0; + for selector in &global_settings.selectors { + let (a_str, b_str) = if !selector.needs_selection { + // We can select the whole line. + (a.line, b.line) } else { - None - } - }); - let mut errors = unwrapped_lines - .enumerate() - .coalesce(|(last_i, last_line), (i, line)| { - if compare_by(&last_line, &line, &settings) == Ordering::Greater { - Err(((last_i, last_line), (i, line))) - } else { - Ok((i, line)) + let selections = ( + a_line_data.selections + [a.index * global_settings.precomputed.selections_per_line + selection_index], + b_line_data.selections + [b.index * global_settings.precomputed.selections_per_line + selection_index], + ); + selection_index += 1; + selections + }; + + let settings = &selector.settings; + + let cmp: Ordering = match settings.mode { + SortMode::Random => random_shuffle(a_str, b_str, &global_settings.salt), + SortMode::Numeric => { + let a_num_info = &a_line_data.num_infos + [a.index * global_settings.precomputed.num_infos_per_line + num_info_index]; + let b_num_info = &b_line_data.num_infos + [b.index * global_settings.precomputed.num_infos_per_line + num_info_index]; + num_info_index += 1; + numeric_str_cmp((a_str, a_num_info), (b_str, b_num_info)) } - }); - if let Some((first_error_index, _line)) = errors.next() { - // Check for a second "error", as .coalesce() always returns the last - // line, no matter what our merging function does. - if let Some(_last_line_or_next_error) = errors.next() { - println!("sort: disorder in line {}", first_error_index); - 1 - } else { - // first "error" was actually the last line. - 0 - } - } else { - // unwrapped_lines was empty. Empty files are defined to be sorted. - 0 - } -} - -fn transform(line: &str, settings: &Settings) -> String { - let mut transformed = line.to_string(); - for transform_fn in &settings.transform_fns { - transformed = transform_fn(&transformed); - } - - transformed -} - -fn sort_by(lines: &mut Vec, settings: &Settings) { - lines.sort_by(|a, b| compare_by(a, b, &settings)) -} - -fn compare_by(a: &str, b: &str, settings: &Settings) -> Ordering { - let (a_transformed, b_transformed): (String, String); - let (a, b) = if !settings.transform_fns.is_empty() { - a_transformed = transform(&a, &settings); - b_transformed = transform(&b, &settings); - (a_transformed.as_str(), b_transformed.as_str()) - } else { - (a, b) - }; - - for compare_fn in &settings.compare_fns { - let cmp = compare_fn(a, b); + SortMode::HumanNumeric => { + let a_num_info = &a_line_data.num_infos + [a.index * global_settings.precomputed.num_infos_per_line + num_info_index]; + let b_num_info = &b_line_data.num_infos + [b.index * global_settings.precomputed.num_infos_per_line + num_info_index]; + num_info_index += 1; + human_numeric_str_cmp((a_str, a_num_info), (b_str, b_num_info)) + } + SortMode::GeneralNumeric => { + let a_float = &a_line_data.parsed_floats + [a.index * global_settings.precomputed.floats_per_line + parsed_float_index]; + let b_float = &b_line_data.parsed_floats + [b.index * global_settings.precomputed.floats_per_line + parsed_float_index]; + parsed_float_index += 1; + general_numeric_compare(a_float, b_float) + } + SortMode::Month => month_compare(a_str, b_str), + SortMode::Version => version_compare(a_str, b_str), + SortMode::Default => custom_str_cmp( + a_str, + b_str, + settings.ignore_non_printing, + settings.dictionary_order, + settings.ignore_case, + ), + }; if cmp != Ordering::Equal { - if settings.reverse { - return cmp.reverse(); - } else { - return cmp; - } + return if settings.reverse { cmp.reverse() } else { cmp }; } } - Ordering::Equal -} -/// Parse the beginning string into an f64, returning -inf instead of NaN on errors. -fn permissive_f64_parse(a: &str) -> f64 { - // Maybe should be split on non-digit, but then 10e100 won't parse properly. - // On the flip side, this will give NEG_INFINITY for "1,234", which might be OK - // because there's no way to handle both CSV and thousands separators without a new flag. - // GNU sort treats "1,234" as "1" in numeric, so maybe it's fine. - // GNU sort treats "NaN" as non-number in numeric, so it needs special care. - match a.split_whitespace().next() { - None => std::f64::NEG_INFINITY, - Some(sa) => match sa.parse::() { - Ok(a) if a.is_nan() => std::f64::NEG_INFINITY, - Ok(a) => a, - Err(_) => std::f64::NEG_INFINITY, - }, - } -} - -fn default_compare(a: &str, b: &str) -> Ordering { - a.cmp(b) -} - -/// Compares two floating point numbers, with errors being assumed to be -inf. -/// Stops coercing at the first whitespace char, so 1e2 will parse as 100 but -/// 1,000 will parse as -inf. -fn numeric_compare(a: &str, b: &str) -> Ordering { - #![allow(clippy::comparison_chain)] - let fa = permissive_f64_parse(a); - let fb = permissive_f64_parse(b); - // f64::cmp isn't implemented because NaN messes with it - // but we sidestep that with permissive_f64_parse so just fake it - if fa > fb { - Ordering::Greater - } else if fa < fb { - Ordering::Less - } else { + // Call "last resort compare" if all selectors returned Equal + let cmp = if global_settings.mode == SortMode::Random + || global_settings.stable + || global_settings.unique + { Ordering::Equal - } -} - -fn human_numeric_convert(a: &str) -> f64 { - let int_str: String = a.chars().take_while(|c| c.is_numeric()).collect(); - let suffix = a.chars().find(|c| !c.is_numeric()); - let int_part = int_str.parse::().unwrap_or(-1f64) as f64; - let suffix: f64 = match suffix.unwrap_or('\0') { - 'K' => 1000f64, - 'M' => 1E6, - 'G' => 1E9, - 'T' => 1E12, - 'P' => 1E15, - _ => 1f64, + } else { + a.line.cmp(b.line) }; - int_part * suffix -} -/// Compare two strings as if they are human readable sizes. -/// AKA 1M > 100k -fn human_numeric_size_compare(a: &str, b: &str) -> Ordering { - #![allow(clippy::comparison_chain)] - let fa = human_numeric_convert(a); - let fb = human_numeric_convert(b); - // f64::cmp isn't implemented (due to NaN issues); implement directly instead - if fa > fb { - Ordering::Greater - } else if fa < fb { - Ordering::Less + if global_settings.reverse { + cmp.reverse() } else { - Ordering::Equal + cmp } } -#[derive(Eq, Ord, PartialEq, PartialOrd)] +// This function cleans up the initial comparison done by leading_num_common for a general numeric compare. +// In contrast to numeric compare, GNU general numeric/FP sort *should* recognize positive signs and +// scientific notation, so we strip those lines only after the end of the following numeric string. +// For example, 5e10KFD would be 5e10 or 5x10^10 and +10000HFKJFK would become 10000. +fn get_leading_gen(input: &str) -> Range { + let trimmed = input.trim_start(); + let leading_whitespace_len = input.len() - trimmed.len(); + + // check for inf, -inf and nan + for allowed_prefix in &["inf", "-inf", "nan"] { + if trimmed.is_char_boundary(allowed_prefix.len()) + && trimmed[..allowed_prefix.len()].eq_ignore_ascii_case(allowed_prefix) + { + return leading_whitespace_len..(leading_whitespace_len + allowed_prefix.len()); + } + } + // Make this iter peekable to see if next char is numeric + let mut char_indices = itertools::peek_nth(trimmed.char_indices()); + + let first = char_indices.peek(); + + if matches!(first, Some((_, NEGATIVE)) | Some((_, POSITIVE))) { + char_indices.next(); + } + + let mut had_e_notation = false; + let mut had_decimal_pt = false; + while let Some((idx, c)) = char_indices.next() { + if c.is_ascii_digit() { + continue; + } + if c == DECIMAL_PT && !had_decimal_pt && !had_e_notation { + had_decimal_pt = true; + continue; + } + if (c == 'e' || c == 'E') && !had_e_notation { + // we can only consume the 'e' if what follow is either a digit, or a sign followed by a digit. + if let Some(&(_, next_char)) = char_indices.peek() { + if (next_char == '+' || next_char == '-') + && matches!( + char_indices.peek_nth(2), + Some((_, c)) if c.is_ascii_digit() + ) + { + // Consume the sign. The following digits will be consumed by the main loop. + char_indices.next(); + had_e_notation = true; + continue; + } + if next_char.is_ascii_digit() { + had_e_notation = true; + continue; + } + } + } + return leading_whitespace_len..(leading_whitespace_len + idx); + } + leading_whitespace_len..input.len() +} + +#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)] +pub enum GeneralF64ParseResult { + Invalid, + NaN, + NegInfinity, + Number(f64), + Infinity, +} + +/// Parse the beginning string into a GeneralF64ParseResult. +/// Using a GeneralF64ParseResult instead of f64 is necessary to correctly order floats. +#[inline(always)] +fn general_f64_parse(a: &str) -> GeneralF64ParseResult { + // The actual behavior here relies on Rust's implementation of parsing floating points. + // For example "nan", "inf" (ignoring the case) and "infinity" are only parsed to floats starting from 1.53. + // TODO: Once our minimum supported Rust version is 1.53 or above, we should add tests for those cases. + match a.parse::() { + Ok(a) if a.is_nan() => GeneralF64ParseResult::NaN, + Ok(a) if a == std::f64::NEG_INFINITY => GeneralF64ParseResult::NegInfinity, + Ok(a) if a == std::f64::INFINITY => GeneralF64ParseResult::Infinity, + Ok(a) => GeneralF64ParseResult::Number(a), + Err(_) => GeneralF64ParseResult::Invalid, + } +} + +/// Compares two floats, with errors and non-numerics assumed to be -inf. +/// Stops coercing at the first non-numeric char. +/// We explicitly need to convert to f64 in this case. +fn general_numeric_compare(a: &GeneralF64ParseResult, b: &GeneralF64ParseResult) -> Ordering { + a.partial_cmp(b).unwrap() +} + +fn get_rand_string() -> String { + thread_rng() + .sample_iter(&Alphanumeric) + .take(16) + .map(char::from) + .collect::() +} + +fn get_hash(t: &T) -> u64 { + let mut s: FnvHasher = Default::default(); + t.hash(&mut s); + s.finish() +} + +fn random_shuffle(a: &str, b: &str, salt: &str) -> Ordering { + let da = get_hash(&[a, salt].concat()); + let db = get_hash(&[b, salt].concat()); + + da.cmp(&db) +} + +#[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Copy)] enum Month { Unknown, January, @@ -520,37 +1573,63 @@ enum Month { /// Parse the beginning string into a Month, returning Month::Unknown on errors. fn month_parse(line: &str) -> Month { - match line - .split_whitespace() - .next() - .unwrap() - .to_uppercase() - .as_ref() - { - "JAN" => Month::January, - "FEB" => Month::February, - "MAR" => Month::March, - "APR" => Month::April, - "MAY" => Month::May, - "JUN" => Month::June, - "JUL" => Month::July, - "AUG" => Month::August, - "SEP" => Month::September, - "OCT" => Month::October, - "NOV" => Month::November, - "DEC" => Month::December, - _ => Month::Unknown, + let line = line.trim(); + + const MONTHS: [(&str, Month); 12] = [ + ("JAN", Month::January), + ("FEB", Month::February), + ("MAR", Month::March), + ("APR", Month::April), + ("MAY", Month::May), + ("JUN", Month::June), + ("JUL", Month::July), + ("AUG", Month::August), + ("SEP", Month::September), + ("OCT", Month::October), + ("NOV", Month::November), + ("DEC", Month::December), + ]; + + for (month_str, month) in &MONTHS { + if line.is_char_boundary(month_str.len()) + && line[..month_str.len()].eq_ignore_ascii_case(month_str) + { + return *month; + } } + + Month::Unknown } fn month_compare(a: &str, b: &str) -> Ordering { - month_parse(a).cmp(&month_parse(b)) + #![allow(clippy::comparison_chain)] + let ma = month_parse(a); + let mb = month_parse(b); + + if ma > mb { + Ordering::Greater + } else if ma < mb { + Ordering::Less + } else { + Ordering::Equal + } +} + +fn version_parse(a: &str) -> Version { + let result = Version::parse(a); + + match result { + Ok(vers_a) => vers_a, + // Non-version lines parse to 0.0.0 + Err(_e) => Version::parse("0.0.0").unwrap(), + } } fn version_compare(a: &str, b: &str) -> Ordering { #![allow(clippy::comparison_chain)] - let ver_a = Version::parse(a); - let ver_b = Version::parse(b); + let ver_a = version_parse(a); + let ver_b = version_parse(b); + // Version::cmp is not implemented; implement comparison directly if ver_a > ver_b { Ordering::Greater @@ -561,48 +1640,173 @@ fn version_compare(a: &str, b: &str) -> Ordering { } } -fn remove_nondictionary_chars(s: &str) -> String { - // Using 'is_ascii_whitespace()' instead of 'is_whitespace()', because it - // uses only symbols compatible with UNIX sort (space, tab, newline). - // 'is_whitespace()' uses more symbols as whitespace (e.g. vertical tab). - s.chars() - .filter(|c| c.is_alphanumeric() || c.is_ascii_whitespace()) - .collect::() -} - -fn print_sorted>(iter: T, outfile: &Option) -where - S: std::fmt::Display, -{ - let mut file: Box = match *outfile { - Some(ref filename) => match File::create(Path::new(&filename)) { - Ok(f) => Box::new(BufWriter::new(f)) as Box, - Err(e) => { - show_error!("sort: {0}: {1}", filename, e.to_string()); - panic!("Could not open output file"); - } - }, - None => Box::new(stdout()) as Box, - }; - +fn print_sorted<'a, T: Iterator>>(iter: T, settings: &GlobalSettings) { + let mut writer = settings.out_writer(); for line in iter { - let str = format!("{}\n", line); - crash_if_err!(1, file.write_all(str.as_bytes())) + line.print(&mut writer, settings); } } // from cat.rs -fn open(path: &str) -> Option<(Box, bool)> { +fn open(path: impl AsRef) -> Box { + let path = path.as_ref(); if path == "-" { let stdin = stdin(); - return Some((Box::new(stdin) as Box, is_stdin_interactive())); + return Box::new(stdin) as Box; } match File::open(Path::new(path)) { - Ok(f) => Some((Box::new(f) as Box, false)), + Ok(f) => Box::new(f) as Box, Err(e) => { - show_error!("sort: {0}: {1}", path, e.to_string()); - None + crash!(2, "cannot read: {0:?}: {1}", path, e); + } + } +} + +fn format_error_message(error: ParseSizeError, s: &str, option: &str) -> String { + // NOTE: + // GNU's sort echos affected flag, -S or --buffer-size, depending user's selection + // GNU's sort does distinguish between "invalid (suffix in) argument" + match error { + ParseSizeError::ParseFailure(_) => format!("invalid --{} argument '{}'", option, s), + ParseSizeError::SizeTooBig(_) => format!("--{} argument '{}' too large", option, s), + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + fn tokenize_helper(line: &str, separator: Option) -> Vec { + let mut buffer = vec![]; + tokenize(line, separator, &mut buffer); + buffer + } + + #[test] + fn test_get_hash() { + let a = "Ted".to_string(); + + assert_eq!(2_646_829_031_758_483_623, get_hash(&a)); + } + + #[test] + fn test_random_shuffle() { + let a = "Ted"; + let b = "Ted"; + let c = get_rand_string(); + + assert_eq!(Ordering::Equal, random_shuffle(a, b, &c)); + } + + #[test] + fn test_month_compare() { + let a = "JaN"; + let b = "OCt"; + + assert_eq!(Ordering::Less, month_compare(a, b)); + } + #[test] + fn test_version_compare() { + let a = "1.2.3-alpha2"; + let b = "1.4.0"; + + assert_eq!(Ordering::Less, version_compare(a, b)); + } + + #[test] + fn test_random_compare() { + let a = "9"; + let b = "9"; + let c = get_rand_string(); + + assert_eq!(Ordering::Equal, random_shuffle(a, b, &c)); + } + + #[test] + fn test_tokenize_fields() { + let line = "foo bar b x"; + assert_eq!(tokenize_helper(line, None), vec![0..3, 3..7, 7..9, 9..14,],); + } + + #[test] + fn test_tokenize_fields_leading_whitespace() { + let line = " foo bar b x"; + assert_eq!( + tokenize_helper(line, None), + vec![0..7, 7..11, 11..13, 13..18,] + ); + } + + #[test] + fn test_tokenize_fields_custom_separator() { + let line = "aaa foo bar b x"; + assert_eq!( + tokenize_helper(line, Some('a')), + vec![0..0, 1..1, 2..2, 3..9, 10..18,] + ); + } + + #[test] + fn test_tokenize_fields_trailing_custom_separator() { + let line = "a"; + assert_eq!(tokenize_helper(line, Some('a')), vec![0..0]); + let line = "aa"; + assert_eq!(tokenize_helper(line, Some('a')), vec![0..0, 1..1]); + let line = "..a..a"; + assert_eq!(tokenize_helper(line, Some('a')), vec![0..2, 3..5]); + } + + #[test] + #[cfg(target_pointer_width = "64")] + fn test_line_size() { + // We should make sure to not regress the size of the Line struct because + // it is unconditional overhead for every line we sort. + assert_eq!(std::mem::size_of::(), 24); + } + + #[test] + fn test_parse_byte_count() { + let valid_input = [ + ("0", 0), + ("50K", 50 * 1024), + ("50k", 50 * 1024), + ("1M", 1024 * 1024), + ("100M", 100 * 1024 * 1024), + #[cfg(not(target_pointer_width = "32"))] + ("1000G", 1000 * 1024 * 1024 * 1024), + #[cfg(not(target_pointer_width = "32"))] + ("10T", 10 * 1024 * 1024 * 1024 * 1024), + ("1b", 1), + ("1024b", 1024), + ("1024Mb", 1024 * 1024 * 1024), // NOTE: This might not be how GNU `sort` behaves for 'Mb' + ("1", 1024), // K is default + ("50", 50 * 1024), + ("K", 1024), + ("k", 1024), + ("m", 1024 * 1024), + #[cfg(not(target_pointer_width = "32"))] + ("E", 1024 * 1024 * 1024 * 1024 * 1024 * 1024), + ]; + for (input, expected_output) in &valid_input { + assert_eq!( + GlobalSettings::parse_byte_count(input), + Ok(*expected_output) + ); + } + + // SizeTooBig + let invalid_input = ["500E", "1Y"]; + for input in &invalid_input { + #[cfg(not(target_pointer_width = "128"))] + assert!(GlobalSettings::parse_byte_count(input).is_err()); + } + + // ParseFailure + let invalid_input = ["nonsense", "1B", "B", "b", "p", "e", "z", "y"]; + for input in &invalid_input { + assert!(GlobalSettings::parse_byte_count(input).is_err()); } } } diff --git a/src/uu/split/Cargo.toml b/src/uu/split/Cargo.toml index 7c3f1a56e..056fbe034 100644 --- a/src/uu/split/Cargo.toml +++ b/src/uu/split/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_split" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "split ~ (uutils) split input into output files" @@ -16,7 +16,7 @@ path = "src/split.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/split/src/main.rs b/src/uu/split/src/main.rs index 2f0640db4..87f15f529 100644 --- a/src/uu/split/src/main.rs +++ b/src/uu/split/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_split); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_split); diff --git a/src/uu/split/src/platform/unix.rs b/src/uu/split/src/platform/unix.rs index 20d9d637b..a115d1959 100644 --- a/src/uu/split/src/platform/unix.rs +++ b/src/uu/split/src/platform/unix.rs @@ -66,7 +66,7 @@ impl FilterWriter { /// * `filepath` - Path of the output file (forwarded to command as $FILE) fn new(command: &str, filepath: &str) -> FilterWriter { // set $FILE, save previous value (if there was one) - let _with_env_var_set = WithEnvVarSet::new("FILE", &filepath); + let _with_env_var_set = WithEnvVarSet::new("FILE", filepath); let shell_process = Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_owned())) @@ -117,7 +117,7 @@ pub fn instantiate_current_writer( ) as Box), Some(ref filter_command) => BufWriter::new(Box::new( // spawn a shell command and write to it - FilterWriter::new(&filter_command, &filename), + FilterWriter::new(filter_command, filename), ) as Box), } } diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 4f80e25a3..ccc98ee5e 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -12,15 +12,16 @@ extern crate uucore; mod platform; -use clap::{App, Arg}; -use std::char; +use clap::{crate_version, App, Arg}; +use std::convert::TryFrom; use std::env; use std::fs::File; -use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; +use std::io::{stdin, BufRead, BufReader, BufWriter, Read, Write}; use std::path::Path; +use std::{char, fs::remove_file}; +use uucore::parse_size::parse_size; static NAME: &str = "split"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static OPT_BYTES: &str = "bytes"; static OPT_LINE_BYTES: &str = "line-bytes"; @@ -29,7 +30,7 @@ static OPT_ADDITIONAL_SUFFIX: &str = "additional-suffix"; static OPT_FILTER: &str = "filter"; static OPT_NUMERIC_SUFFIXES: &str = "numeric-suffixes"; static OPT_SUFFIX_LENGTH: &str = "suffix-length"; -static OPT_DEFAULT_SUFFIX_LENGTH: usize = 2; +static OPT_DEFAULT_SUFFIX_LENGTH: &str = "2"; static OPT_VERBOSE: &str = "verbose"; static ARG_INPUT: &str = "input"; @@ -53,13 +54,82 @@ size is 1000, and default PREFIX is 'x'. With no INPUT, or when INPUT is pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let long_usage = get_long_usage(); - let default_suffix_length_str = OPT_DEFAULT_SUFFIX_LENGTH.to_string(); - let matches = App::new(executable!()) - .version(VERSION) - .about("Create output files containing consecutive or interleaved sections of input") + let matches = uu_app() .usage(&usage[..]) .after_help(&long_usage[..]) + .get_matches_from(args); + + let mut settings = Settings { + prefix: "".to_owned(), + numeric_suffix: false, + suffix_length: 0, + additional_suffix: "".to_owned(), + input: "".to_owned(), + filter: None, + strategy: "".to_owned(), + strategy_param: "".to_owned(), + verbose: false, + }; + + settings.suffix_length = matches + .value_of(OPT_SUFFIX_LENGTH) + .unwrap() + .parse() + .unwrap_or_else(|_| panic!("Invalid number for {}", OPT_SUFFIX_LENGTH)); + + settings.numeric_suffix = matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0; + settings.additional_suffix = matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(); + + settings.verbose = matches.occurrences_of("verbose") > 0; + // check that the user is not specifying more than one strategy + // note: right now, this exact behavior cannot be handled by ArgGroup since ArgGroup + // considers a default value Arg as "defined" + let explicit_strategies = + vec![OPT_LINE_BYTES, OPT_LINES, OPT_BYTES] + .into_iter() + .fold(0, |count, strategy| { + if matches.occurrences_of(strategy) > 0 { + count + 1 + } else { + count + } + }); + if explicit_strategies > 1 { + crash!(1, "cannot split in more than one way"); + } + + // default strategy (if no strategy is passed, use this one) + settings.strategy = String::from(OPT_LINES); + settings.strategy_param = matches.value_of(OPT_LINES).unwrap().to_owned(); + // take any (other) defined strategy + for strategy in vec![OPT_LINE_BYTES, OPT_BYTES].into_iter() { + if matches.occurrences_of(strategy) > 0 { + settings.strategy = String::from(strategy); + settings.strategy_param = matches.value_of(strategy).unwrap().to_owned(); + } + } + + settings.input = matches.value_of(ARG_INPUT).unwrap().to_owned(); + settings.prefix = matches.value_of(ARG_PREFIX).unwrap().to_owned(); + + if matches.occurrences_of(OPT_FILTER) > 0 { + if cfg!(windows) { + // see https://github.com/rust-lang/rust/issues/29494 + show_error!("{} is currently not supported in this platform", OPT_FILTER); + exit!(-1); + } else { + settings.filter = Some(matches.value_of(OPT_FILTER).unwrap().to_owned()); + } + } + + split(&settings) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about("Create output files containing consecutive or interleaved sections of input") // strategy (mutually exclusive) .arg( Arg::with_name(OPT_BYTES) @@ -112,7 +182,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .short("a") .long(OPT_SUFFIX_LENGTH) .takes_value(true) - .default_value(default_suffix_length_str.as_str()) + .default_value(OPT_DEFAULT_SUFFIX_LENGTH) .help("use suffixes of length N (default 2)"), ) .arg( @@ -132,74 +202,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .default_value("x") .index(2) ) - .get_matches_from(args); - - let mut settings = Settings { - prefix: "".to_owned(), - numeric_suffix: false, - suffix_length: 0, - additional_suffix: "".to_owned(), - input: "".to_owned(), - filter: None, - strategy: "".to_owned(), - strategy_param: "".to_owned(), - verbose: false, - }; - - settings.suffix_length = matches - .value_of(OPT_SUFFIX_LENGTH) - .unwrap() - .parse() - .unwrap_or_else(|_| panic!("Invalid number for {}", OPT_SUFFIX_LENGTH)); - - settings.numeric_suffix = matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0; - settings.additional_suffix = matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(); - - settings.verbose = matches.occurrences_of("verbose") > 0; - // check that the user is not specifying more than one strategy - // note: right now, this exact behaviour cannot be handled by ArgGroup since ArgGroup - // considers a default value Arg as "defined" - let explicit_strategies = - vec![OPT_LINE_BYTES, OPT_LINES, OPT_BYTES] - .into_iter() - .fold(0, |count, strat| { - if matches.occurrences_of(strat) > 0 { - count + 1 - } else { - count - } - }); - if explicit_strategies > 1 { - crash!(1, "cannot split in more than one way"); - } - - // default strategy (if no strategy is passed, use this one) - settings.strategy = String::from(OPT_LINES); - settings.strategy_param = matches.value_of(OPT_LINES).unwrap().to_owned(); - // take any (other) defined strategy - for strat in vec![OPT_LINE_BYTES, OPT_BYTES].into_iter() { - if matches.occurrences_of(strat) > 0 { - settings.strategy = String::from(strat); - settings.strategy_param = matches.value_of(strat).unwrap().to_owned(); - } - } - - settings.input = matches.value_of(ARG_INPUT).unwrap().to_owned(); - settings.prefix = matches.value_of(ARG_PREFIX).unwrap().to_owned(); - - if matches.occurrences_of(OPT_FILTER) > 0 { - if cfg!(windows) { - // see https://github.com/rust-lang/rust/issues/29494 - show_error!("{} is currently not supported in this platform", OPT_FILTER); - exit!(-1); - } else { - settings.filter = Some(matches.value_of(OPT_FILTER).unwrap().to_owned()); - } - } - - split(&settings) } +#[allow(dead_code)] struct Settings { prefix: String, numeric_suffix: bool, @@ -210,110 +215,121 @@ struct Settings { filter: Option, strategy: String, strategy_param: String, - verbose: bool, -} - -struct SplitControl { - current_line: String, // Don't touch - request_new_file: bool, // Splitter implementation requests new file + verbose: bool, // TODO: warning: field is never read: `verbose` } trait Splitter { - // Consume the current_line and return the consumed string - fn consume(&mut self, _: &mut SplitControl) -> String; + // Consume as much as possible from `reader` so as to saturate `writer`. + // Equivalent to finishing one of the part files. Returns the number of + // bytes that have been moved. + fn consume( + &mut self, + reader: &mut BufReader>, + writer: &mut BufWriter>, + ) -> u128; } struct LineSplitter { - saved_lines_to_write: usize, - lines_to_write: usize, + lines_per_split: usize, } impl LineSplitter { fn new(settings: &Settings) -> LineSplitter { - let n = match settings.strategy_param.parse() { - Ok(a) => a, - Err(e) => crash!(1, "invalid number of lines: {}", e), - }; LineSplitter { - saved_lines_to_write: n, - lines_to_write: n, + lines_per_split: settings.strategy_param.parse().unwrap_or_else(|_| { + crash!(1, "invalid number of lines: '{}'", settings.strategy_param) + }), } } } impl Splitter for LineSplitter { - fn consume(&mut self, control: &mut SplitControl) -> String { - self.lines_to_write -= 1; - if self.lines_to_write == 0 { - self.lines_to_write = self.saved_lines_to_write; - control.request_new_file = true; + fn consume( + &mut self, + reader: &mut BufReader>, + writer: &mut BufWriter>, + ) -> u128 { + let mut bytes_consumed = 0u128; + let mut buffer = String::with_capacity(1024); + for _ in 0..self.lines_per_split { + let bytes_read = reader + .read_line(&mut buffer) + .unwrap_or_else(|_| crash!(1, "error reading bytes from input file")); + // If we ever read 0 bytes then we know we've hit EOF. + if bytes_read == 0 { + return bytes_consumed; + } + + writer + .write_all(buffer.as_bytes()) + .unwrap_or_else(|_| crash!(1, "error writing bytes to output file")); + // Empty out the String buffer since `read_line` appends instead of + // replaces. + buffer.clear(); + + bytes_consumed += bytes_read as u128; } - control.current_line.clone() + + bytes_consumed } } struct ByteSplitter { - saved_bytes_to_write: usize, - bytes_to_write: usize, - break_on_line_end: bool, - require_whole_line: bool, + bytes_per_split: u128, } impl ByteSplitter { fn new(settings: &Settings) -> ByteSplitter { - let mut strategy_param: Vec = settings.strategy_param.chars().collect(); - let suffix = strategy_param.pop().unwrap(); - let multiplier = match suffix { - '0'..='9' => 1usize, - 'b' => 512usize, - 'k' => 1024usize, - 'm' => 1024usize * 1024usize, - _ => crash!(1, "invalid number of bytes"), - }; - let n = if suffix.is_alphabetic() { - match strategy_param - .iter() - .cloned() - .collect::() - .parse::() - { - Ok(a) => a, - Err(e) => crash!(1, "invalid number of bytes: {}", e), - } - } else { - match settings.strategy_param.parse::() { - Ok(a) => a, - Err(e) => crash!(1, "invalid number of bytes: {}", e), - } + let size_string = &settings.strategy_param; + let size_num = match parse_size(size_string) { + Ok(n) => n, + Err(e) => crash!(1, "invalid number of bytes: {}", e.to_string()), }; + ByteSplitter { - saved_bytes_to_write: n * multiplier, - bytes_to_write: n * multiplier, - break_on_line_end: settings.strategy == "b", - require_whole_line: false, + bytes_per_split: u128::try_from(size_num).unwrap(), } } } impl Splitter for ByteSplitter { - fn consume(&mut self, control: &mut SplitControl) -> String { - let line = control.current_line.clone(); - let n = std::cmp::min(line.chars().count(), self.bytes_to_write); - if self.require_whole_line && n < line.chars().count() { - self.bytes_to_write = self.saved_bytes_to_write; - control.request_new_file = true; - self.require_whole_line = false; - return "".to_owned(); + fn consume( + &mut self, + reader: &mut BufReader>, + writer: &mut BufWriter>, + ) -> u128 { + // We buffer reads and writes. We proceed until `bytes_consumed` is + // equal to `self.bytes_per_split` or we reach EOF. + let mut bytes_consumed = 0u128; + const BUFFER_SIZE: usize = 1024; + let mut buffer = [0u8; BUFFER_SIZE]; + while bytes_consumed < self.bytes_per_split { + // Don't overshoot `self.bytes_per_split`! Note: Using std::cmp::min + // doesn't really work since we have to get types to match which + // can't be done in a way that keeps all conversions safe. + let bytes_desired = if (BUFFER_SIZE as u128) <= self.bytes_per_split - bytes_consumed { + BUFFER_SIZE + } else { + // This is a safe conversion since the difference must be less + // than BUFFER_SIZE in this branch. + (self.bytes_per_split - bytes_consumed) as usize + }; + let bytes_read = reader + .read(&mut buffer[0..bytes_desired]) + .unwrap_or_else(|_| crash!(1, "error reading bytes from input file")); + // If we ever read 0 bytes then we know we've hit EOF. + if bytes_read == 0 { + return bytes_consumed; + } + + writer + .write_all(&buffer[0..bytes_read]) + .unwrap_or_else(|_| crash!(1, "error writing bytes to output file")); + + bytes_consumed += bytes_read as u128; } - self.bytes_to_write -= n; - if n == 0 { - self.bytes_to_write = self.saved_bytes_to_write; - control.request_new_file = true; - } - if self.break_on_line_end && n == line.chars().count() { - self.require_whole_line = self.break_on_line_end; - } - line[..n].to_owned() + + bytes_consumed } } @@ -353,14 +369,13 @@ fn split(settings: &Settings) -> i32 { let mut reader = BufReader::new(if settings.input == "-" { Box::new(stdin()) as Box } else { - let r = match File::open(Path::new(&settings.input)) { - Ok(a) => a, - Err(_) => crash!( + let r = File::open(Path::new(&settings.input)).unwrap_or_else(|_| { + crash!( 1, "cannot open '{}' for reading: No such file or directory", settings.input - ), - }; + ) + }); Box::new(r) as Box }); @@ -370,48 +385,39 @@ fn split(settings: &Settings) -> i32 { a => crash!(1, "strategy {} not supported", a), }; - let mut control = SplitControl { - current_line: "".to_owned(), // Request new line - request_new_file: true, // Request new file - }; - - let mut writer = BufWriter::new(Box::new(stdout()) as Box); let mut fileno = 0; loop { - if control.current_line.chars().count() == 0 { - match reader.read_line(&mut control.current_line) { - Ok(0) | Err(_) => break, - _ => {} + // Get a new part file set up, and construct `writer` for it. + let mut filename = settings.prefix.clone(); + filename.push_str( + if settings.numeric_suffix { + num_prefix(fileno, settings.suffix_length) + } else { + str_prefix(fileno, settings.suffix_length) } - } - if control.request_new_file { - let mut filename = settings.prefix.clone(); - filename.push_str( - if settings.numeric_suffix { - num_prefix(fileno, settings.suffix_length) - } else { - str_prefix(fileno, settings.suffix_length) - } - .as_ref(), - ); - filename.push_str(settings.additional_suffix.as_ref()); + .as_ref(), + ); + filename.push_str(settings.additional_suffix.as_ref()); + let mut writer = platform::instantiate_current_writer(&settings.filter, filename.as_str()); - crash_if_err!(1, writer.flush()); - fileno += 1; - writer = platform::instantiate_current_writer(&settings.filter, filename.as_str()); - control.request_new_file = false; - if settings.verbose { - println!("creating file '{}'", filename); + let bytes_consumed = splitter.consume(&mut reader, &mut writer); + writer + .flush() + .unwrap_or_else(|e| crash!(1, "error flushing to output file: {}", e)); + + // If we didn't write anything we should clean up the empty file, and + // break from the loop. + if bytes_consumed == 0 { + // The output file is only ever created if --filter isn't used. + // Complicated, I know... + if settings.filter.is_none() { + remove_file(filename) + .unwrap_or_else(|e| crash!(1, "error removing empty file: {}", e)); } + break; } - let consumed = splitter.consume(&mut control); - crash_if_err!(1, writer.write_all(consumed.as_bytes())); - - let advance = consumed.chars().count(); - let clone = control.current_line.clone(); - let sl = clone; - control.current_line = sl[advance..sl.chars().count()].to_owned(); + fileno += 1; } 0 } diff --git a/src/uu/stat/Cargo.toml b/src/uu/stat/Cargo.toml index 52ea88110..86b7da139 100644 --- a/src/uu/stat/Cargo.toml +++ b/src/uu/stat/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_stat" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "stat ~ (uutils) display FILE status" @@ -16,8 +16,7 @@ path = "src/stat.rs" [dependencies] clap = "2.33" -time = "0.1.40" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries", "libc"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "libc", "fs", "fsext"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/stat/src/fsext.rs b/src/uu/stat/src/fsext.rs deleted file mode 100644 index 11c8f8095..000000000 --- a/src/uu/stat/src/fsext.rs +++ /dev/null @@ -1,415 +0,0 @@ -// This file is part of the uutils coreutils package. -// -// (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 (ToDO) strerror IFBLK IFCHR IFDIR IFLNK IFIFO IFMT IFREG IFSOCK subsec nanos gnulib statfs Sstatfs bitrig statvfs iosize blksize fnodes fsid namelen bsize bfree bavail ffree frsize namemax errno fstype adfs acfs aufs affs autofs befs bdevfs binfmt ceph cgroups cifs configfs cramfs cgroupfs debugfs devfs devpts ecryptfs btrfs efivarfs exofs fhgfs fuseblk fusectl futexfs gpfs hfsx hostfs hpfs inodefs ibrix inotifyfs isofs jffs logfs hugetlbfs mqueue nsfs ntfs ocfs panfs pipefs ramfs romfs nfsd nilfs pstorefs reiserfs securityfs smackfs snfs sockfs squashfs sysfs sysv tempfs tracefs ubifs usbdevfs vmhgfs tmpfs vxfs wslfs xenfs vzfs openprom overlayfs - -extern crate time; - -use self::time::Timespec; -use std::time::UNIX_EPOCH; -pub use uucore::libc::{ - c_int, mode_t, strerror, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, - S_IFSOCK, S_IRGRP, S_IROTH, S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, - S_IXGRP, S_IXOTH, S_IXUSR, -}; - -pub trait BirthTime { - fn pretty_birth(&self) -> String; - fn birth(&self) -> String; -} - -use std::fs::Metadata; -impl BirthTime for Metadata { - fn pretty_birth(&self) -> String { - self.created() - .ok() - .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) - .map(|e| pretty_time(e.as_secs() as i64, i64::from(e.subsec_nanos()))) - .unwrap_or_else(|| "-".to_owned()) - } - - fn birth(&self) -> String { - self.created() - .ok() - .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) - .map(|e| format!("{}", e.as_secs())) - .unwrap_or_else(|| "0".to_owned()) - } -} - -#[macro_export] -macro_rules! has { - ($mode:expr, $perm:expr) => { - $mode & $perm != 0 - }; -} - -pub fn pretty_time(sec: i64, nsec: i64) -> String { - // sec == seconds since UNIX_EPOCH - // nsec == nanoseconds since (UNIX_EPOCH + sec) - let tm = time::at(Timespec::new(sec, nsec as i32)); - let res = time::strftime("%Y-%m-%d %H:%M:%S.%f %z", &tm).unwrap(); - if res.ends_with(" -0000") { - res.replace(" -0000", " +0000") - } else { - res - } -} - -pub fn pretty_filetype<'a>(mode: mode_t, size: u64) -> &'a str { - match mode & S_IFMT { - S_IFREG => { - if size != 0 { - "regular file" - } else { - "regular empty file" - } - } - S_IFDIR => "directory", - S_IFLNK => "symbolic link", - S_IFCHR => "character special file", - S_IFBLK => "block special file", - S_IFIFO => "fifo", - S_IFSOCK => "socket", - // TODO: Other file types - // See coreutils/gnulib/lib/file-type.c - _ => "weird file", - } -} - -pub fn pretty_access(mode: mode_t) -> String { - let mut result = String::with_capacity(10); - result.push(match mode & S_IFMT { - S_IFDIR => 'd', - S_IFCHR => 'c', - S_IFBLK => 'b', - S_IFREG => '-', - S_IFIFO => 'p', - S_IFLNK => 'l', - S_IFSOCK => 's', - // TODO: Other file types - _ => '?', - }); - - result.push(if has!(mode, S_IRUSR) { 'r' } else { '-' }); - result.push(if has!(mode, S_IWUSR) { 'w' } else { '-' }); - result.push(if has!(mode, S_ISUID as mode_t) { - if has!(mode, S_IXUSR) { - 's' - } else { - 'S' - } - } else if has!(mode, S_IXUSR) { - 'x' - } else { - '-' - }); - - result.push(if has!(mode, S_IRGRP) { 'r' } else { '-' }); - result.push(if has!(mode, S_IWGRP) { 'w' } else { '-' }); - result.push(if has!(mode, S_ISGID as mode_t) { - if has!(mode, S_IXGRP) { - 's' - } else { - 'S' - } - } else if has!(mode, S_IXGRP) { - 'x' - } else { - '-' - }); - - result.push(if has!(mode, S_IROTH) { 'r' } else { '-' }); - result.push(if has!(mode, S_IWOTH) { 'w' } else { '-' }); - result.push(if has!(mode, S_ISVTX as mode_t) { - if has!(mode, S_IXOTH) { - 't' - } else { - 'T' - } - } else if has!(mode, S_IXOTH) { - 'x' - } else { - '-' - }); - - result -} - -use std::borrow::Cow; -use std::convert::{AsRef, From}; -use std::ffi::CString; -use std::io::Error as IOError; -use std::mem; -use std::path::Path; - -#[cfg(any( - target_os = "linux", - target_os = "macos", - target_os = "android", - target_os = "freebsd" -))] -use uucore::libc::statfs as Sstatfs; -#[cfg(any( - target_os = "openbsd", - target_os = "netbsd", - target_os = "openbsd", - target_os = "bitrig", - target_os = "dragonfly" -))] -use uucore::libc::statvfs as Sstatfs; - -#[cfg(any( - target_os = "linux", - target_os = "macos", - target_os = "android", - target_os = "freebsd" -))] -use uucore::libc::statfs as statfs_fn; -#[cfg(any( - target_os = "openbsd", - target_os = "netbsd", - target_os = "openbsd", - target_os = "bitrig", - target_os = "dragonfly" -))] -use uucore::libc::statvfs as statfs_fn; - -pub trait FsMeta { - fn fs_type(&self) -> i64; - fn iosize(&self) -> u64; - fn blksize(&self) -> i64; - fn total_blocks(&self) -> u64; - fn free_blocks(&self) -> u64; - fn avail_blocks(&self) -> u64; - fn total_fnodes(&self) -> u64; - fn free_fnodes(&self) -> u64; - fn fsid(&self) -> u64; - fn namelen(&self) -> u64; -} - -impl FsMeta for Sstatfs { - fn blksize(&self) -> i64 { - self.f_bsize as i64 - } - fn total_blocks(&self) -> u64 { - self.f_blocks as u64 - } - fn free_blocks(&self) -> u64 { - self.f_bfree as u64 - } - fn avail_blocks(&self) -> u64 { - self.f_bavail as u64 - } - fn total_fnodes(&self) -> u64 { - self.f_files as u64 - } - fn free_fnodes(&self) -> u64 { - self.f_ffree as u64 - } - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] - fn fs_type(&self) -> i64 { - self.f_type as i64 - } - #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "freebsd")))] - fn fs_type(&self) -> i64 { - // FIXME: statvfs doesn't have an equivalent, so we need to do something else - unimplemented!() - } - - #[cfg(target_os = "linux")] - fn iosize(&self) -> u64 { - self.f_frsize as u64 - } - #[cfg(any(target_os = "macos", target_os = "freebsd"))] - fn iosize(&self) -> u64 { - self.f_iosize as u64 - } - // XXX: dunno if this is right - #[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "linux")))] - fn iosize(&self) -> u64 { - self.f_bsize as u64 - } - - // Linux, SunOS, HP-UX, 4.4BSD, FreeBSD have a system call statfs() that returns - // a struct statfs, containing a fsid_t f_fsid, where fsid_t is defined - // as struct { int val[2]; } - // - // Solaris, Irix and POSIX have a system call statvfs(2) that returns a - // struct statvfs, containing an unsigned long f_fsid - #[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "linux"))] - fn fsid(&self) -> u64 { - let f_fsid: &[u32; 2] = - unsafe { &*(&self.f_fsid as *const uucore::libc::fsid_t as *const [u32; 2]) }; - (u64::from(f_fsid[0])) << 32 | u64::from(f_fsid[1]) - } - #[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "linux")))] - fn fsid(&self) -> u64 { - self.f_fsid as u64 - } - - #[cfg(target_os = "linux")] - fn namelen(&self) -> u64 { - self.f_namelen as u64 - } - #[cfg(target_os = "macos")] - fn namelen(&self) -> u64 { - 1024 - } - #[cfg(target_os = "freebsd")] - fn namelen(&self) -> u64 { - self.f_namemax as u64 - } - // XXX: should everything just use statvfs? - #[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "linux")))] - fn namelen(&self) -> u64 { - self.f_namemax as u64 - } -} - -pub fn statfs>(path: P) -> Result -where - Vec: From

, -{ - match CString::new(path) { - Ok(p) => { - let mut buffer: Sstatfs = unsafe { mem::zeroed() }; - unsafe { - match statfs_fn(p.as_ptr(), &mut buffer) { - 0 => Ok(buffer), - _ => { - let errno = IOError::last_os_error().raw_os_error().unwrap_or(0); - Err(CString::from_raw(strerror(errno)) - .into_string() - .unwrap_or_else(|_| "Unknown Error".to_owned())) - } - } - } - } - Err(e) => Err(e.to_string()), - } -} - -pub fn pretty_fstype<'a>(fstype: i64) -> Cow<'a, str> { - match fstype { - 0x6163_6673 => "acfs".into(), - 0xADF5 => "adfs".into(), - 0xADFF => "affs".into(), - 0x5346_414F => "afs".into(), - 0x0904_1934 => "anon-inode FS".into(), - 0x6175_6673 => "aufs".into(), - 0x0187 => "autofs".into(), - 0x4246_5331 => "befs".into(), - 0x6264_6576 => "bdevfs".into(), - 0x1BAD_FACE => "bfs".into(), - 0xCAFE_4A11 => "bpf_fs".into(), - 0x4249_4E4D => "binfmt_misc".into(), - 0x9123_683E => "btrfs".into(), - 0x7372_7279 => "btrfs_test".into(), - 0x00C3_6400 => "ceph".into(), - 0x0027_E0EB => "cgroupfs".into(), - 0xFF53_4D42 => "cifs".into(), - 0x7375_7245 => "coda".into(), - 0x012F_F7B7 => "coh".into(), - 0x6265_6570 => "configfs".into(), - 0x28CD_3D45 => "cramfs".into(), - 0x453D_CD28 => "cramfs-wend".into(), - 0x6462_6720 => "debugfs".into(), - 0x1373 => "devfs".into(), - 0x1CD1 => "devpts".into(), - 0xF15F => "ecryptfs".into(), - 0xDE5E_81E4 => "efivarfs".into(), - 0x0041_4A53 => "efs".into(), - 0x5DF5 => "exofs".into(), - 0x137D => "ext".into(), - 0xEF53 => "ext2/ext3".into(), - 0xEF51 => "ext2".into(), - 0xF2F5_2010 => "f2fs".into(), - 0x4006 => "fat".into(), - 0x1983_0326 => "fhgfs".into(), - 0x6573_5546 => "fuseblk".into(), - 0x6573_5543 => "fusectl".into(), - 0x0BAD_1DEA => "futexfs".into(), - 0x0116_1970 => "gfs/gfs2".into(), - 0x4750_4653 => "gpfs".into(), - 0x4244 => "hfs".into(), - 0x482B => "hfs+".into(), - 0x4858 => "hfsx".into(), - 0x00C0_FFEE => "hostfs".into(), - 0xF995_E849 => "hpfs".into(), - 0x9584_58F6 => "hugetlbfs".into(), - 0x1130_7854 => "inodefs".into(), - 0x0131_11A8 => "ibrix".into(), - 0x2BAD_1DEA => "inotifyfs".into(), - 0x9660 => "isofs".into(), - 0x4004 => "isofs".into(), - 0x4000 => "isofs".into(), - 0x07C0 => "jffs".into(), - 0x72B6 => "jffs2".into(), - 0x3153_464A => "jfs".into(), - 0x6B41_4653 => "k-afs".into(), - 0xC97E_8168 => "logfs".into(), - 0x0BD0_0BD0 => "lustre".into(), - 0x5346_314D => "m1fs".into(), - 0x137F => "minix".into(), - 0x138F => "minix (30 char.)".into(), - 0x2468 => "minix v2".into(), - 0x2478 => "minix v2 (30 char.)".into(), - 0x4D5A => "minix3".into(), - 0x1980_0202 => "mqueue".into(), - 0x4D44 => "msdos".into(), - 0x564C => "novell".into(), - 0x6969 => "nfs".into(), - 0x6E66_7364 => "nfsd".into(), - 0x3434 => "nilfs".into(), - 0x6E73_6673 => "nsfs".into(), - 0x5346_544E => "ntfs".into(), - 0x9FA1 => "openprom".into(), - 0x7461_636F => "ocfs2".into(), - 0x794C_7630 => "overlayfs".into(), - 0xAAD7_AAEA => "panfs".into(), - 0x5049_5045 => "pipefs".into(), - 0x7C7C_6673 => "prl_fs".into(), - 0x9FA0 => "proc".into(), - 0x6165_676C => "pstorefs".into(), - 0x002F => "qnx4".into(), - 0x6819_1122 => "qnx6".into(), - 0x8584_58F6 => "ramfs".into(), - 0x5265_4973 => "reiserfs".into(), - 0x7275 => "romfs".into(), - 0x6759_6969 => "rpc_pipefs".into(), - 0x7363_6673 => "securityfs".into(), - 0xF97C_FF8C => "selinux".into(), - 0x4341_5D53 => "smackfs".into(), - 0x517B => "smb".into(), - 0xFE53_4D42 => "smb2".into(), - 0xBEEF_DEAD => "snfs".into(), - 0x534F_434B => "sockfs".into(), - 0x7371_7368 => "squashfs".into(), - 0x6265_6572 => "sysfs".into(), - 0x012F_F7B6 => "sysv2".into(), - 0x012F_F7B5 => "sysv4".into(), - 0x0102_1994 => "tmpfs".into(), - 0x7472_6163 => "tracefs".into(), - 0x2405_1905 => "ubifs".into(), - 0x1501_3346 => "udf".into(), - 0x0001_1954 => "ufs".into(), - 0x5419_0100 => "ufs".into(), - 0x9FA2 => "usbdevfs".into(), - 0x0102_1997 => "v9fs".into(), - 0xBACB_ACBC => "vmhgfs".into(), - 0xA501_FCF5 => "vxfs".into(), - 0x565A_4653 => "vzfs".into(), - 0x5346_4846 => "wslfs".into(), - 0xABBA_1974 => "xenfs".into(), - 0x012F_F7B4 => "xenix".into(), - 0x5846_5342 => "xfs".into(), - 0x012F_D16D => "xia".into(), - 0x2FC1_2FC1 => "zfs".into(), - other => format!("UNKNOWN ({:#x})", other).into(), - } -} diff --git a/src/uu/stat/src/main.rs b/src/uu/stat/src/main.rs index 6e483c850..839eff7de 100644 --- a/src/uu/stat/src/main.rs +++ b/src/uu/stat/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_stat); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_stat); diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 5216fb293..70c06bdf6 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -5,21 +5,18 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -// spell-checker:ignore (ToDO) mtab fsext showfs otype fmtstr prec ftype blocksize nlink rdev fnodes fsid namelen blksize inodes fstype iosize statfs gnulib NBLOCKSIZE - -#[macro_use] -mod fsext; -pub use crate::fsext::*; - #[macro_use] extern crate uucore; use uucore::entries; +use uucore::fs::display_permissions; +use uucore::fsext::{ + pretty_filetype, pretty_fstype, pretty_time, read_fs_list, statfs, BirthTime, FsMeta, +}; +use uucore::libc::mode_t; -use clap::{App, Arg, ArgMatches}; +use clap::{crate_version, App, Arg, ArgMatches}; use std::borrow::Cow; use std::convert::AsRef; -use std::fs::File; -use std::io::{BufRead, BufReader}; use std::os::unix::fs::{FileTypeExt, MetadataExt}; use std::path::Path; use std::{cmp, fs, iter}; @@ -27,7 +24,7 @@ use std::{cmp, fs, iter}; macro_rules! check_bound { ($str: ident, $bound:expr, $beg: expr, $end: expr) => { if $end >= $bound { - return Err(format!("‘{}’: invalid directive", &$str[$beg..$end])); + return Err(format!("'{}': invalid directive", &$str[$beg..$end])); } }; } @@ -85,7 +82,6 @@ macro_rules! print_adjusted { } static ABOUT: &str = "Display file or file system status."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod options { pub static DEREFERENCE: &str = "dereference"; @@ -97,7 +93,6 @@ pub mod options { static ARG_FILES: &str = "files"; -const MOUNT_INFO: &str = "/etc/mtab"; pub const F_ALTER: u8 = 1; pub const F_ZERO: u8 = 1 << 1; pub const F_LEFT: u8 = 1 << 2; @@ -210,7 +205,7 @@ pub fn group_num(s: &str) -> Cow { pub struct Stater { follow: bool, - showfs: bool, + show_fs: bool, from_user: bool, files: Vec, mount_list: Option>, @@ -219,7 +214,7 @@ pub struct Stater { } #[allow(clippy::cognitive_complexity)] -fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32) { +fn print_it(arg: &str, output_type: OutputType, flag: u8, width: usize, precision: i32) { // If the precision is given as just '.', the precision is taken to be zero. // A negative precision is taken as if the precision were omitted. // This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, @@ -250,7 +245,7 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32 // By default, a sign is used only for negative numbers. // A + overrides a space if both are used. - if otype == OutputType::Unknown { + if output_type == OutputType::Unknown { return print!("?"); } @@ -264,7 +259,7 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32 let has_sign = has!(flag, F_SIGN) || has!(flag, F_SPACE); let should_alter = has!(flag, F_ALTER); - let prefix = match otype { + let prefix = match output_type { OutputType::UnsignedOct => "0", OutputType::UnsignedHex => "0x", OutputType::Integer => { @@ -277,7 +272,7 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32 _ => "", }; - match otype { + match output_type { OutputType::Str => { let limit = cmp::min(precision, arg.len() as i32); let s: &str = if limit >= 0 { @@ -336,10 +331,10 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32 } impl Stater { - pub fn generate_tokens(fmtstr: &str, use_printf: bool) -> Result, String> { + pub fn generate_tokens(format_str: &str, use_printf: bool) -> Result, String> { let mut tokens = Vec::new(); - let bound = fmtstr.len(); - let chars = fmtstr.chars().collect::>(); + let bound = format_str.len(); + let chars = format_str.chars().collect::>(); let mut i = 0_usize; while i < bound { match chars[i] { @@ -372,32 +367,32 @@ impl Stater { } i += 1; } - check_bound!(fmtstr, bound, old, i); + check_bound!(format_str, bound, old, i); let mut width = 0_usize; let mut precision = -1_i32; let mut j = i; - if let Some((field_width, offset)) = fmtstr[j..].scan_num::() { + if let Some((field_width, offset)) = format_str[j..].scan_num::() { width = field_width; j += offset; } - check_bound!(fmtstr, bound, old, j); + check_bound!(format_str, bound, old, j); if chars[j] == '.' { j += 1; - check_bound!(fmtstr, bound, old, j); + check_bound!(format_str, bound, old, j); - match fmtstr[j..].scan_num::() { - Some((prec, offset)) => { - if prec >= 0 { - precision = prec; + match format_str[j..].scan_num::() { + Some((value, offset)) => { + if value >= 0 { + precision = value; } j += offset; } None => precision = 0, } - check_bound!(fmtstr, bound, old, j); + check_bound!(format_str, bound, old, j); } i = j; @@ -420,7 +415,7 @@ impl Stater { } match chars[i] { 'x' if i + 1 < bound => { - if let Some((c, offset)) = fmtstr[i + 1..].scan_char(16) { + if let Some((c, offset)) = format_str[i + 1..].scan_char(16) { tokens.push(Token::Char(c)); i += offset; } else { @@ -429,7 +424,7 @@ impl Stater { } } '0'..='7' => { - let (c, offset) = fmtstr[i..].scan_char(8).unwrap(); + let (c, offset) = format_str[i..].scan_char(8).unwrap(); tokens.push(Token::Char(c)); i += offset - 1; } @@ -454,7 +449,7 @@ impl Stater { } i += 1; } - if !use_printf && !fmtstr.ends_with('\n') { + if !use_printf && !format_str.ends_with('\n') { tokens.push(Token::Char('\n')); } Ok(tokens) @@ -466,7 +461,7 @@ impl Stater { .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); - let fmtstr = if matches.is_present(options::PRINTF) { + let format_str = if matches.is_present(options::PRINTF) { matches .value_of(options::PRINTF) .expect("Invalid format string") @@ -476,27 +471,25 @@ impl Stater { let use_printf = matches.is_present(options::PRINTF); let terse = matches.is_present(options::TERSE); - let showfs = matches.is_present(options::FILE_SYSTEM); + let show_fs = matches.is_present(options::FILE_SYSTEM); - let default_tokens = if fmtstr.is_empty() { - Stater::generate_tokens(&Stater::default_fmt(showfs, terse, false), use_printf).unwrap() + let default_tokens = if format_str.is_empty() { + Stater::generate_tokens(&Stater::default_format(show_fs, terse, false), use_printf) + .unwrap() } else { - Stater::generate_tokens(&fmtstr, use_printf)? + Stater::generate_tokens(format_str, use_printf)? }; let default_dev_tokens = - Stater::generate_tokens(&Stater::default_fmt(showfs, terse, true), use_printf).unwrap(); + Stater::generate_tokens(&Stater::default_format(show_fs, terse, true), use_printf) + .unwrap(); - let mount_list = if showfs { + let mount_list = if show_fs { // mount points aren't displayed when showing filesystem information None } else { - let reader = BufReader::new( - File::open(MOUNT_INFO).unwrap_or_else(|_| panic!("Failed to read {}", MOUNT_INFO)), - ); - let mut mount_list = reader - .lines() - .filter_map(Result::ok) - .filter_map(|line| line.split_whitespace().nth(1).map(ToOwned::to_owned)) + let mut mount_list = read_fs_list() + .iter() + .map(|mi| mi.mount_dir.clone()) .collect::>(); // Reverse sort. The longer comes first. mount_list.sort(); @@ -506,8 +499,8 @@ impl Stater { Ok(Stater { follow: matches.is_present(options::DEREFERENCE), - showfs, - from_user: !fmtstr.is_empty(), + show_fs, + from_user: !format_str.is_empty(), files, default_tokens, default_dev_tokens, @@ -539,7 +532,7 @@ impl Stater { } fn do_stat(&self, file: &str) -> i32 { - if !self.showfs { + if !self.show_fs { let result = if self.follow { fs::metadata(file) } else { @@ -547,13 +540,14 @@ impl Stater { }; match result { Ok(meta) => { - let ftype = meta.file_type(); - let tokens = - if self.from_user || !(ftype.is_char_device() || ftype.is_block_device()) { - &self.default_tokens - } else { - &self.default_dev_tokens - }; + let file_type = meta.file_type(); + let tokens = if self.from_user + || !(file_type.is_char_device() || file_type.is_block_device()) + { + &self.default_tokens + } else { + &self.default_dev_tokens + }; for t in tokens.iter() { match *t { @@ -565,91 +559,91 @@ impl Stater { format, } => { let arg: String; - let otype: OutputType; + let output_type: OutputType; match format { // access rights in octal 'a' => { arg = format!("{:o}", 0o7777 & meta.mode()); - otype = OutputType::UnsignedOct; + output_type = OutputType::UnsignedOct; } // access rights in human readable form 'A' => { - arg = pretty_access(meta.mode() as mode_t); - otype = OutputType::Str; + arg = display_permissions(&meta, true); + output_type = OutputType::Str; } // number of blocks allocated (see %B) 'b' => { arg = format!("{}", meta.blocks()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // the size in bytes of each block reported by %b // FIXME: blocksize differs on various platform - // See coreutils/gnulib/lib/stat-size.h ST_NBLOCKSIZE + // See coreutils/gnulib/lib/stat-size.h ST_NBLOCKSIZE // spell-checker:disable-line 'B' => { // the size in bytes of each block reported by %b arg = format!("{}", 512); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // device number in decimal 'd' => { arg = format!("{}", meta.dev()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // device number in hex 'D' => { arg = format!("{:x}", meta.dev()); - otype = OutputType::UnsignedHex; + output_type = OutputType::UnsignedHex; } // raw mode in hex 'f' => { arg = format!("{:x}", meta.mode()); - otype = OutputType::UnsignedHex; + output_type = OutputType::UnsignedHex; } // file type 'F' => { arg = pretty_filetype(meta.mode() as mode_t, meta.len()) .to_owned(); - otype = OutputType::Str; + output_type = OutputType::Str; } // group ID of owner 'g' => { arg = format!("{}", meta.gid()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // group name of owner 'G' => { arg = entries::gid2grp(meta.gid()) .unwrap_or_else(|_| "UNKNOWN".to_owned()); - otype = OutputType::Str; + output_type = OutputType::Str; } // number of hard links 'h' => { arg = format!("{}", meta.nlink()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // inode number 'i' => { arg = format!("{}", meta.ino()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // mount point 'm' => { arg = self.find_mount_point(file).unwrap(); - otype = OutputType::Str; + output_type = OutputType::Str; } // file name 'n' => { arg = file.to_owned(); - otype = OutputType::Str; + output_type = OutputType::Str; } // quoted file name with dereference if symbolic link 'N' => { - if ftype.is_symlink() { + if file_type.is_symlink() { let dst = match fs::read_link(file) { Ok(path) => path, Err(e) => { @@ -663,99 +657,99 @@ impl Stater { dst.to_string_lossy() ); } else { - arg = format!("`{}'", file); + arg = file.to_string(); } - otype = OutputType::Str; + output_type = OutputType::Str; } // optimal I/O transfer size hint 'o' => { arg = format!("{}", meta.blksize()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // total size, in bytes 's' => { arg = format!("{}", meta.len()); - otype = OutputType::Integer; + output_type = OutputType::Integer; } // major device type in hex, for character/block device special // files 't' => { arg = format!("{:x}", meta.rdev() >> 8); - otype = OutputType::UnsignedHex; + output_type = OutputType::UnsignedHex; } // minor device type in hex, for character/block device special // files 'T' => { arg = format!("{:x}", meta.rdev() & 0xff); - otype = OutputType::UnsignedHex; + output_type = OutputType::UnsignedHex; } // user ID of owner 'u' => { arg = format!("{}", meta.uid()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // user name of owner 'U' => { arg = entries::uid2usr(meta.uid()) .unwrap_or_else(|_| "UNKNOWN".to_owned()); - otype = OutputType::Str; + output_type = OutputType::Str; } // time of file birth, human-readable; - if unknown 'w' => { arg = meta.pretty_birth(); - otype = OutputType::Str; + output_type = OutputType::Str; } // time of file birth, seconds since Epoch; 0 if unknown 'W' => { arg = meta.birth(); - otype = OutputType::Integer; + output_type = OutputType::Integer; } // time of last access, human-readable 'x' => { arg = pretty_time(meta.atime(), meta.atime_nsec()); - otype = OutputType::Str; + output_type = OutputType::Str; } // time of last access, seconds since Epoch 'X' => { arg = format!("{}", meta.atime()); - otype = OutputType::Integer; + output_type = OutputType::Integer; } // time of last data modification, human-readable 'y' => { arg = pretty_time(meta.mtime(), meta.mtime_nsec()); - otype = OutputType::Str; + output_type = OutputType::Str; } // time of last data modification, seconds since Epoch 'Y' => { arg = format!("{}", meta.mtime()); - otype = OutputType::Str; + output_type = OutputType::Str; } // time of last status change, human-readable 'z' => { arg = pretty_time(meta.ctime(), meta.ctime_nsec()); - otype = OutputType::Str; + output_type = OutputType::Str; } // time of last status change, seconds since Epoch 'Z' => { arg = format!("{}", meta.ctime()); - otype = OutputType::Integer; + output_type = OutputType::Integer; } _ => { arg = "?".to_owned(); - otype = OutputType::Unknown; + output_type = OutputType::Unknown; } } - print_it(&arg, otype, flag, width, precision); + print_it(&arg, output_type, flag, width, precision); } } } } Err(e) => { - show_info!("cannot stat '{}': {}", file, e); + show_error!("cannot stat '{}': {}", file, e); return 1; } } @@ -774,81 +768,81 @@ impl Stater { format, } => { let arg: String; - let otype: OutputType; + let output_type: OutputType; match format { // free blocks available to non-superuser 'a' => { arg = format!("{}", meta.avail_blocks()); - otype = OutputType::Integer; + output_type = OutputType::Integer; } // total data blocks in file system 'b' => { arg = format!("{}", meta.total_blocks()); - otype = OutputType::Integer; + output_type = OutputType::Integer; } // total file nodes in file system 'c' => { - arg = format!("{}", meta.total_fnodes()); - otype = OutputType::Unsigned; + arg = format!("{}", meta.total_file_nodes()); + output_type = OutputType::Unsigned; } // free file nodes in file system 'd' => { - arg = format!("{}", meta.free_fnodes()); - otype = OutputType::Integer; + arg = format!("{}", meta.free_file_nodes()); + output_type = OutputType::Integer; } // free blocks in file system 'f' => { arg = format!("{}", meta.free_blocks()); - otype = OutputType::Integer; + output_type = OutputType::Integer; } // file system ID in hex 'i' => { arg = format!("{:x}", meta.fsid()); - otype = OutputType::UnsignedHex; + output_type = OutputType::UnsignedHex; } // maximum length of filenames 'l' => { arg = format!("{}", meta.namelen()); - otype = OutputType::Unsigned; + output_type = OutputType::Unsigned; } // file name 'n' => { arg = file.to_owned(); - otype = OutputType::Str; + output_type = OutputType::Str; } // block size (for faster transfers) 's' => { - arg = format!("{}", meta.iosize()); - otype = OutputType::Unsigned; + arg = format!("{}", meta.io_size()); + output_type = OutputType::Unsigned; } // fundamental block size (for block counts) 'S' => { - arg = format!("{}", meta.blksize()); - otype = OutputType::Unsigned; + arg = format!("{}", meta.block_size()); + output_type = OutputType::Unsigned; } // file system type in hex 't' => { arg = format!("{:x}", meta.fs_type()); - otype = OutputType::UnsignedHex; + output_type = OutputType::UnsignedHex; } // file system type in human readable form 'T' => { arg = pretty_fstype(meta.fs_type()).into_owned(); - otype = OutputType::Str; + output_type = OutputType::Str; } _ => { arg = "?".to_owned(); - otype = OutputType::Unknown; + output_type = OutputType::Unknown; } } - print_it(&arg, otype, flag, width, precision); + print_it(&arg, output_type, flag, width, precision); } } } } Err(e) => { - show_info!("cannot read file system information for '{}': {}", file, e); + show_error!("cannot read file system information for '{}': {}", file, e); return 1; } } @@ -856,34 +850,34 @@ impl Stater { 0 } - // taken from coreutils/src/stat.c - fn default_fmt(showfs: bool, terse: bool, dev: bool) -> String { + fn default_format(show_fs: bool, terse: bool, show_dev_type: bool) -> String { // SELinux related format is *ignored* - let mut fmtstr = String::with_capacity(36); - if showfs { + let mut format_str = String::with_capacity(36); + if show_fs { if terse { - fmtstr.push_str("%n %i %l %t %s %S %b %f %a %c %d\n"); + format_str.push_str("%n %i %l %t %s %S %b %f %a %c %d\n"); } else { - fmtstr.push_str( + format_str.push_str( " File: \"%n\"\n ID: %-8i Namelen: %-7l Type: %T\nBlock \ size: %-10s Fundamental block size: %S\nBlocks: Total: %-10b \ Free: %-10f Available: %a\nInodes: Total: %-10c Free: %d\n", ); } } else if terse { - fmtstr.push_str("%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o\n"); + format_str.push_str("%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o\n"); } else { - fmtstr.push_str(" File: %N\n Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"); - if dev { - fmtstr.push_str("Device: %Dh/%dd\tInode: %-10i Links: %-5h Device type: %t,%T\n"); + format_str.push_str(" File: %N\n Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"); + if show_dev_type { + format_str + .push_str("Device: %Dh/%dd\tInode: %-10i Links: %-5h Device type: %t,%T\n"); } else { - fmtstr.push_str("Device: %Dh/%dd\tInode: %-10i Links: %h\n"); + format_str.push_str("Device: %Dh/%dd\tInode: %-10i Links: %h\n"); } - fmtstr.push_str("Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"); - fmtstr.push_str("Access: %x\nModify: %y\nChange: %z\n Birth: %w\n"); + format_str.push_str("Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"); + format_str.push_str("Access: %x\nModify: %y\nChange: %z\n Birth: %w\n"); } - fmtstr + format_str } } @@ -953,11 +947,24 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let long_usage = get_long_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() .usage(&usage[..]) .after_help(&long_usage[..]) + .get_matches_from(args); + + match Stater::new(matches) { + Ok(stater) => stater.exec(), + Err(e) => { + show_error!("{}", e); + 1 + } + } +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) .arg( Arg::with_name(options::DEREFERENCE) .short("L") @@ -1002,13 +1009,4 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .takes_value(true) .min_values(1), ) - .get_matches_from(args); - - match Stater::new(matches) { - Ok(stater) => stater.exec(), - Err(e) => { - show_info!("{}", e); - 1 - } - } } diff --git a/src/uu/stat/src/test_stat.rs b/src/uu/stat/src/test_stat.rs deleted file mode 100644 index 05e91fb84..000000000 --- a/src/uu/stat/src/test_stat.rs +++ /dev/null @@ -1,76 +0,0 @@ -// spell-checker:ignore (ToDO) scanutil qzxc dqzxc - -pub use super::*; - -#[test] -fn test_scanutil() { - assert_eq!(Some((-5, 2)), "-5zxc".scan_num::()); - assert_eq!(Some((51, 2)), "51zxc".scan_num::()); - assert_eq!(Some((192, 4)), "+192zxc".scan_num::()); - assert_eq!(None, "z192zxc".scan_num::()); - - assert_eq!(Some(('a', 3)), "141zxc".scan_char(8)); - assert_eq!(Some(('\n', 2)), "12qzxc".scan_char(8)); - assert_eq!(Some(('\r', 1)), "dqzxc".scan_char(16)); - assert_eq!(None, "z2qzxc".scan_char(8)); -} - -#[cfg(test)] -mod test_generate_tokens { - use super::*; - - #[test] - fn test_normal_format() { - let s = "%10.2ac%-5.w\n"; - let expected = vec![ - Token::Directive { - flag: 0, - width: 10, - precision: 2, - format: 'a', - }, - Token::Char('c'), - Token::Directive { - flag: F_LEFT, - width: 5, - precision: 0, - format: 'w', - }, - Token::Char('\n'), - ]; - assert_eq!(&expected, &Stater::generate_tokens(s, false).unwrap()); - } - - #[test] - fn test_printf_format() { - let s = "%-# 15a\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.-23w\\x12\\167\\132\\112\\n"; - let expected = vec![ - Token::Directive { - flag: F_LEFT | F_ALTER | F_SPACE, - width: 15, - precision: -1, - format: 'a', - }, - Token::Char('\r'), - Token::Char('"'), - Token::Char('\\'), - Token::Char('\x07'), - Token::Char('\x08'), - Token::Char('\x1B'), - Token::Char('\x0C'), - Token::Char('\x0B'), - Token::Directive { - flag: F_SIGN | F_ZERO, - width: 20, - precision: -1, - format: 'w', - }, - Token::Char('\x12'), - Token::Char('w'), - Token::Char('Z'), - Token::Char('J'), - Token::Char('\n'), - ]; - assert_eq!(&expected, &Stater::generate_tokens(s, true).unwrap()); - } -} diff --git a/src/uu/stdbuf/Cargo.toml b/src/uu/stdbuf/Cargo.toml index 7a80e99ae..884a98785 100644 --- a/src/uu/stdbuf/Cargo.toml +++ b/src/uu/stdbuf/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_stdbuf" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "stdbuf ~ (uutils) run COMMAND with modified standard stream buffering" @@ -15,13 +15,13 @@ edition = "2018" path = "src/stdbuf.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" tempfile = "3.1" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [build-dependencies] -libstdbuf = { version="0.0.4", package="uu_stdbuf_libstdbuf", path="src/libstdbuf" } +libstdbuf = { version="0.0.6", package="uu_stdbuf_libstdbuf", path="src/libstdbuf" } [[bin]] name = "stdbuf" diff --git a/src/uu/stdbuf/build.rs b/src/uu/stdbuf/build.rs index c005072d9..b14d503cf 100644 --- a/src/uu/stdbuf/build.rs +++ b/src/uu/stdbuf/build.rs @@ -4,12 +4,12 @@ use std::env; use std::fs; use std::path::Path; -#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "windows")))] +#[cfg(not(any(target_vendor = "apple", target_os = "windows")))] mod platform { pub const DYLIB_EXT: &str = ".so"; } -#[cfg(any(target_os = "macos", target_os = "ios"))] +#[cfg(any(target_vendor = "apple"))] mod platform { pub const DYLIB_EXT: &str = ".dylib"; } diff --git a/src/uu/stdbuf/src/libstdbuf/Cargo.toml b/src/uu/stdbuf/src/libstdbuf/Cargo.toml index ac9c7230f..86eb09d46 100644 --- a/src/uu/stdbuf/src/libstdbuf/Cargo.toml +++ b/src/uu/stdbuf/src/libstdbuf/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_stdbuf_libstdbuf" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "stdbuf/libstdbuf ~ (uutils); dynamic library required for stdbuf" @@ -19,7 +19,7 @@ crate-type = ["cdylib", "rlib"] # XXX: note: the rlib is just to prevent Cargo f [dependencies] cpp = "0.5" libc = "0.2" -uucore = { version=">=0.0.7", package="uucore", path="../../../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../../../uucore_procs" } [build-dependencies] diff --git a/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs b/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs index fa36d4ab5..d08427d98 100644 --- a/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs +++ b/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs @@ -35,8 +35,8 @@ extern "C" { fn set_buffer(stream: *mut FILE, value: &str) { let (mode, size): (c_int, size_t) = match value { - "0" => (_IONBF, 0 as size_t), - "L" => (_IOLBF, 0 as size_t), + "0" => (_IONBF, 0_usize), + "L" => (_IOLBF, 0_usize), input => { let buff_size: usize = match input.parse() { Ok(num) => num, diff --git a/src/uu/stdbuf/src/main.rs b/src/uu/stdbuf/src/main.rs index c020a7a07..1989a3b8d 100644 --- a/src/uu/stdbuf/src/main.rs +++ b/src/uu/stdbuf/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_stdbuf); // spell-checker:ignore procs uucore stdbuf +uucore_procs::main!(uu_stdbuf); diff --git a/src/uu/stdbuf/src/stdbuf.rs b/src/uu/stdbuf/src/stdbuf.rs index 86523144c..7460a2cb2 100644 --- a/src/uu/stdbuf/src/stdbuf.rs +++ b/src/uu/stdbuf/src/stdbuf.rs @@ -10,7 +10,8 @@ #[macro_use] extern crate uucore; -use getopts::{Matches, Options}; +use clap::{crate_version, App, AppSettings, Arg, ArgMatches}; +use std::convert::TryFrom; use std::fs::File; use std::io::{self, Write}; use std::os::unix::process::ExitStatusExt; @@ -18,16 +19,44 @@ use std::path::PathBuf; use std::process::Command; use tempfile::tempdir; use tempfile::TempDir; +use uucore::parse_size::parse_size; +use uucore::InvalidEncodingHandling; -static NAME: &str = "stdbuf"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = + "Run COMMAND, with modified buffering operations for its standard streams.\n\n\ + Mandatory arguments to long options are mandatory for short options too."; +static LONG_HELP: &str = "If MODE is 'L' the corresponding stream will be line buffered.\n\ + This option is invalid with standard input.\n\n\ + If MODE is '0' the corresponding stream will be unbuffered.\n\n\ + Otherwise MODE is a number which may be followed by one of the following:\n\n\ + KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\ + In this case the corresponding stream will be fully buffered with the buffer size set to \ + MODE bytes.\n\n\ + NOTE: If COMMAND adjusts the buffering of its standard streams ('tee' does for e.g.) then \ + that will override corresponding settings changed by 'stdbuf'.\n\ + Also some filters (like 'dd' and 'cat' etc.) don't use streams for I/O, \ + and are thus unaffected by 'stdbuf' settings.\n"; + +mod options { + pub const INPUT: &str = "input"; + pub const INPUT_SHORT: &str = "i"; + pub const OUTPUT: &str = "output"; + pub const OUTPUT_SHORT: &str = "o"; + pub const ERROR: &str = "error"; + pub const ERROR_SHORT: &str = "e"; + pub const COMMAND: &str = "command"; +} + +fn get_usage() -> String { + format!("{0} OPTION... COMMAND", executable!()) +} const STDBUF_INJECT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/libstdbuf.so")); enum BufferType { Default, Line, - Size(u64), + Size(usize), } struct ProgramOptions { @@ -36,16 +65,19 @@ struct ProgramOptions { stderr: BufferType, } -enum ErrMsg { - Retry, - Fatal, +impl<'a> TryFrom<&ArgMatches<'a>> for ProgramOptions { + type Error = ProgramOptionsError; + + fn try_from(matches: &ArgMatches) -> Result { + Ok(ProgramOptions { + stdin: check_option(matches, options::INPUT)?, + stdout: check_option(matches, options::OUTPUT)?, + stderr: check_option(matches, options::ERROR)?, + }) + } } -enum OkMsg { - Buffering, - Help, - Version, -} +struct ProgramOptionsError(String); #[cfg(any( target_os = "linux", @@ -57,7 +89,7 @@ fn preload_strings() -> (&'static str, &'static str) { ("LD_PRELOAD", "so") } -#[cfg(target_os = "macos")] +#[cfg(target_vendor = "apple")] fn preload_strings() -> (&'static str, &'static str) { ("DYLD_LIBRARY_PATH", "dylib") } @@ -67,131 +99,33 @@ fn preload_strings() -> (&'static str, &'static str) { target_os = "freebsd", target_os = "netbsd", target_os = "dragonflybsd", - target_os = "macos" + target_vendor = "apple" )))] fn preload_strings() -> (&'static str, &'static str) { crash!(1, "Command not supported for this operating system!") } -fn print_version() { - println!("{} {}", NAME, VERSION); -} - -fn print_usage(opts: &Options) { - let brief = "Run COMMAND, with modified buffering operations for its standard streams\n \ - Mandatory arguments to long options are mandatory for short options too."; - let explanation = "If MODE is 'L' the corresponding stream will be line buffered.\n \ - This option is invalid with standard input.\n\n \ - If MODE is '0' the corresponding stream will be unbuffered.\n\n \ - Otherwise MODE is a number which may be followed by one of the following:\n\n \ - KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n \ - In this case the corresponding stream will be fully buffered with the buffer size set to \ - MODE bytes.\n\n \ - NOTE: If COMMAND adjusts the buffering of its standard streams ('tee' does for e.g.) then \ - that will override corresponding settings changed by 'stdbuf'.\n \ - Also some filters (like 'dd' and 'cat' etc.) don't use streams for I/O, \ - and are thus unaffected by 'stdbuf' settings.\n"; - println!("{} {}", NAME, VERSION); - println!(); - println!("Usage: stdbuf OPTION... COMMAND"); - println!(); - println!("{}\n{}", opts.usage(brief), explanation); -} - -fn parse_size(size: &str) -> Option { - let ext = size.trim_start_matches(|c: char| c.is_digit(10)); - let num = size.trim_end_matches(char::is_alphabetic); - let mut recovered = num.to_owned(); - recovered.push_str(ext); - if recovered != size { - return None; - } - let buf_size: u64 = match num.parse().ok() { - Some(m) => m, - None => return None, - }; - let (power, base): (u32, u64) = match ext { - "" => (0, 0), - "KB" => (1, 1024), - "K" => (1, 1000), - "MB" => (2, 1024), - "M" => (2, 1000), - "GB" => (3, 1024), - "G" => (3, 1000), - "TB" => (4, 1024), - "T" => (4, 1000), - "PB" => (5, 1024), - "P" => (5, 1000), - "EB" => (6, 1024), - "E" => (6, 1000), - "ZB" => (7, 1024), - "Z" => (7, 1000), - "YB" => (8, 1024), - "Y" => (8, 1000), - _ => return None, - }; - Some(buf_size * base.pow(power)) -} - -fn check_option(matches: &Matches, name: &str, modified: &mut bool) -> Option { - match matches.opt_str(name) { - Some(value) => { - *modified = true; - match &value[..] { - "L" => { - if name == "input" { - show_info!("line buffering stdin is meaningless"); - None - } else { - Some(BufferType::Line) - } - } - x => { - let size = match parse_size(x) { - Some(m) => m, - None => { - show_error!("Invalid mode {}", x); - return None; - } - }; - Some(BufferType::Size(size)) +fn check_option(matches: &ArgMatches, name: &str) -> Result { + match matches.value_of(name) { + Some(value) => match value { + "L" => { + if name == options::INPUT { + Err(ProgramOptionsError( + "line buffering stdin is meaningless".to_string(), + )) + } else { + Ok(BufferType::Line) } } - } - None => Some(BufferType::Default), + x => parse_size(x).map_or_else( + |e| crash!(125, "invalid mode {}", e), + |m| Ok(BufferType::Size(m)), + ), + }, + None => Ok(BufferType::Default), } } -fn parse_options( - args: &[String], - options: &mut ProgramOptions, - optgrps: &Options, -) -> Result { - let matches = match optgrps.parse(args) { - Ok(m) => m, - Err(_) => return Err(ErrMsg::Retry), - }; - if matches.opt_present("help") { - return Ok(OkMsg::Help); - } - if matches.opt_present("version") { - return Ok(OkMsg::Version); - } - let mut modified = false; - options.stdin = check_option(&matches, "input", &mut modified).ok_or(ErrMsg::Fatal)?; - options.stdout = check_option(&matches, "output", &mut modified).ok_or(ErrMsg::Fatal)?; - options.stderr = check_option(&matches, "error", &mut modified).ok_or(ErrMsg::Fatal)?; - - if matches.free.len() != 1 { - return Err(ErrMsg::Retry); - } - if !modified { - show_error!("you must specify a buffering mode option"); - return Err(ErrMsg::Fatal); - } - Ok(OkMsg::Buffering) -} - fn set_command_env(command: &mut Command, buffer_name: &str, buffer_type: BufferType) { match buffer_type { BufferType::Size(m) => { @@ -215,72 +149,28 @@ fn get_preload_env(tmp_dir: &mut TempDir) -> io::Result<(String, PathBuf)> { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); + let usage = get_usage(); - let mut opts = Options::new(); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - opts.optopt( - "i", - "input", - "adjust standard input stream buffering", - "MODE", - ); - opts.optopt( - "o", - "output", - "adjust standard output stream buffering", - "MODE", - ); - opts.optopt( - "e", - "error", - "adjust standard error stream buffering", - "MODE", - ); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("", "version", "output version information and exit"); + let options = ProgramOptions::try_from(&matches) + .unwrap_or_else(|e| crash!(125, "{}\nTry 'stdbuf --help' for more information.", e.0)); - let mut options = ProgramOptions { - stdin: BufferType::Default, - stdout: BufferType::Default, - stderr: BufferType::Default, - }; - let mut command_idx: i32 = -1; - for i in 1..=args.len() { - match parse_options(&args[1..i], &mut options, &opts) { - Ok(OkMsg::Buffering) => { - command_idx = (i as i32) - 1; - break; - } - Ok(OkMsg::Help) => { - print_usage(&opts); - return 0; - } - Ok(OkMsg::Version) => { - print_version(); - return 0; - } - Err(ErrMsg::Fatal) => break, - Err(ErrMsg::Retry) => continue, - } - } - if command_idx == -1 { - crash!( - 125, - "Invalid options\nTry 'stdbuf --help' for more information." - ); - } - let command_name = &args[command_idx as usize]; - let mut command = Command::new(command_name); + let mut command_values = matches.values_of::<&str>(options::COMMAND).unwrap(); + let mut command = Command::new(command_values.next().unwrap()); + let command_params: Vec<&str> = command_values.collect(); let mut tmp_dir = tempdir().unwrap(); let (preload_env, libstdbuf) = return_if_err!(1, get_preload_env(&mut tmp_dir)); - command - .args(&args[(command_idx as usize) + 1..]) - .env(preload_env, libstdbuf); + command.env(preload_env, libstdbuf); set_command_env(&mut command, "_STDBUF_I", options.stdin); set_command_env(&mut command, "_STDBUF_O", options.stdout); set_command_env(&mut command, "_STDBUF_E", options.stderr); + command.args(command_params); + let mut process = match command.spawn() { Ok(p) => p, Err(e) => crash!(1, "failed to execute process: {}", e), @@ -293,3 +183,42 @@ pub fn uumain(args: impl uucore::Args) -> i32 { Err(e) => crash!(1, "{}", e), } } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .after_help(LONG_HELP) + .setting(AppSettings::TrailingVarArg) + .arg( + Arg::with_name(options::INPUT) + .long(options::INPUT) + .short(options::INPUT_SHORT) + .help("adjust standard input stream buffering") + .value_name("MODE") + .required_unless_one(&[options::OUTPUT, options::ERROR]), + ) + .arg( + Arg::with_name(options::OUTPUT) + .long(options::OUTPUT) + .short(options::OUTPUT_SHORT) + .help("adjust standard output stream buffering") + .value_name("MODE") + .required_unless_one(&[options::INPUT, options::ERROR]), + ) + .arg( + Arg::with_name(options::ERROR) + .long(options::ERROR) + .short(options::ERROR_SHORT) + .help("adjust standard error stream buffering") + .value_name("MODE") + .required_unless_one(&[options::INPUT, options::OUTPUT]), + ) + .arg( + Arg::with_name(options::COMMAND) + .multiple(true) + .takes_value(true) + .hidden(true) + .required(true), + ) +} diff --git a/src/uu/sum/Cargo.toml b/src/uu/sum/Cargo.toml index a880ba1f7..64b6d3de9 100644 --- a/src/uu/sum/Cargo.toml +++ b/src/uu/sum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_sum" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "sum ~ (uutils) display checksum and block counts for input" @@ -15,8 +15,8 @@ edition = "2018" path = "src/sum.rs" [dependencies] -getopts = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/sum/src/main.rs b/src/uu/sum/src/main.rs index 64b0d4254..85f4d0079 100644 --- a/src/uu/sum/src/main.rs +++ b/src/uu/sum/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_sum); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_sum); diff --git a/src/uu/sum/src/sum.rs b/src/uu/sum/src/sum.rs index 5c1652196..0ce612859 100644 --- a/src/uu/sum/src/sum.rs +++ b/src/uu/sum/src/sum.rs @@ -10,12 +10,16 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{stdin, Read, Result}; use std::path::Path; +use uucore::InvalidEncodingHandling; static NAME: &str = "sum"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static USAGE: &str = + "[OPTION]... [FILE]...\nWith no FILE, or when FILE is -, read standard input."; +static SUMMARY: &str = "Checksum and count the blocks in a file."; fn bsd_sum(mut reader: Box) -> (usize, u16) { let mut buf = [0; 1024]; @@ -64,55 +68,44 @@ fn open(name: &str) -> Result> { match name { "-" => Ok(Box::new(stdin()) as Box), _ => { - let f = File::open(&Path::new(name))?; + let path = &Path::new(name); + if path.is_dir() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "Is a directory", + )); + }; + if path.metadata().is_err() { + return Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "No such file or directory", + )); + }; + let f = File::open(path)?; Ok(Box::new(f) as Box) } } } +mod options { + pub static FILE: &str = "file"; + pub static BSD_COMPATIBLE: &str = "r"; + pub static SYSTEM_V_COMPATIBLE: &str = "sysv"; +} + pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = getopts::Options::new(); + let matches = uu_app().get_matches_from(args); - opts.optflag("r", "", "use the BSD compatible algorithm (default)"); - opts.optflag("s", "sysv", "use System V compatible algorithm"); - opts.optflag("h", "help", "show this help message"); - opts.optflag("v", "version", "print the version and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "Invalid options\n{}", f), + let files: Vec = match matches.values_of(options::FILE) { + Some(v) => v.clone().map(|v| v.to_owned()).collect(), + None => vec!["-".to_owned()], }; - if matches.opt_present("help") { - let msg = format!( - "{0} {1} - -Usage: - {0} [OPTION]... [FILE]... - -Checksum and count the blocks in a file.", - NAME, VERSION - ); - println!( - "{}\nWith no FILE, or when FILE is -, read standard input.", - opts.usage(&msg) - ); - return 0; - } - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; - } - - let sysv = matches.opt_present("sysv"); - - let files = if matches.free.is_empty() { - vec!["-".to_owned()] - } else { - matches.free - }; + let sysv = matches.is_present(options::SYSTEM_V_COMPATIBLE); let print_names = if sysv { files.len() > 1 || files[0] != "-" @@ -120,10 +113,15 @@ Checksum and count the blocks in a file.", files.len() > 1 }; + let mut exit_code = 0; for file in &files { let reader = match open(file) { Ok(f) => f, - _ => crash!(1, "unable to open file"), + Err(error) => { + show_error!("'{}' {}", file, error); + exit_code = 2; + continue; + } }; let (blocks, sum) = if sysv { sysv_sum(reader) @@ -138,5 +136,25 @@ Checksum and count the blocks in a file.", } } - 0 + exit_code +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(USAGE) + .about(SUMMARY) + .arg(Arg::with_name(options::FILE).multiple(true).hidden(true)) + .arg( + Arg::with_name(options::BSD_COMPATIBLE) + .short(options::BSD_COMPATIBLE) + .help("use the BSD sum algorithm, use 1K blocks (default)"), + ) + .arg( + Arg::with_name(options::SYSTEM_V_COMPATIBLE) + .short("s") + .long(options::SYSTEM_V_COMPATIBLE) + .help("use System V sum algorithm, use 512 bytes blocks"), + ) } diff --git a/src/uu/sync/Cargo.toml b/src/uu/sync/Cargo.toml index 4cbf69a41..fcff6002e 100644 --- a/src/uu/sync/Cargo.toml +++ b/src/uu/sync/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_sync" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "sync ~ (uutils) synchronize cache writes to storage" @@ -17,7 +17,7 @@ path = "src/sync.rs" [dependencies] clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["wide"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["wide"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } winapi = { version = "0.3", features = ["errhandlingapi", "fileapi", "handleapi", "std", "winbase", "winerror"] } diff --git a/src/uu/sync/src/main.rs b/src/uu/sync/src/main.rs index 06d85b278..9786fc371 100644 --- a/src/uu/sync/src/main.rs +++ b/src/uu/sync/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_sync); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_sync); diff --git a/src/uu/sync/src/sync.rs b/src/uu/sync/src/sync.rs index 985e7580d..4fcdf49f9 100644 --- a/src/uu/sync/src/sync.rs +++ b/src/uu/sync/src/sync.rs @@ -12,13 +12,12 @@ extern crate libc; #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use std::path::Path; static EXIT_ERR: i32 = 1; static ABOUT: &str = "Synchronize cached writes to persistent storage"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod options { pub static FILE_SYSTEM: &str = "file-system"; pub static DATA: &str = "data"; @@ -167,10 +166,36 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let files: Vec = matches + .values_of(ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + for f in &files { + if !Path::new(&f).exists() { + crash!(EXIT_ERR, "cannot stat '{}': No such file or directory", f); + } + } + + #[allow(clippy::if_same_then_else)] + if matches.is_present(options::FILE_SYSTEM) { + #[cfg(any(target_os = "linux", target_os = "windows"))] + syncfs(files); + } else if matches.is_present(options::DATA) { + #[cfg(target_os = "linux")] + fdatasync(files); + } else { + sync(); + } + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(options::FILE_SYSTEM) .short("f") @@ -186,29 +211,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .help("sync only file data, no unneeded metadata (Linux only)"), ) .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) - .get_matches_from(args); - - let files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - for f in &files { - if !Path::new(&f).exists() { - crash!(EXIT_ERR, "cannot stat '{}': No such file or directory", f); - } - } - - if matches.is_present(options::FILE_SYSTEM) { - #[cfg(any(target_os = "linux", target_os = "windows"))] - syncfs(files); - } else if matches.is_present(options::DATA) { - #[cfg(target_os = "linux")] - fdatasync(files); - } else { - sync(); - } - 0 } fn sync() -> isize { diff --git a/src/uu/tac/Cargo.toml b/src/uu/tac/Cargo.toml index 15e743006..3a530d0ce 100644 --- a/src/uu/tac/Cargo.toml +++ b/src/uu/tac/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_tac" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "tac ~ (uutils) concatenate and display input lines in reverse order" @@ -15,8 +15,8 @@ edition = "2018" path = "src/tac.rs" [dependencies] -getopts = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/tac/src/main.rs b/src/uu/tac/src/main.rs index 93d91e2b7..018821c73 100644 --- a/src/uu/tac/src/main.rs +++ b/src/uu/tac/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_tac); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_tac); diff --git a/src/uu/tac/src/tac.rs b/src/uu/tac/src/tac.rs index 843babac9..ae1fd9bc5 100644 --- a/src/uu/tac/src/tac.rs +++ b/src/uu/tac/src/tac.rs @@ -10,79 +10,82 @@ #[macro_use] extern crate uucore; -use std::fs::File; +use clap::{crate_version, App, Arg}; use std::io::{stdin, stdout, BufReader, Read, Stdout, Write}; +use std::{fs::File, path::Path}; +use uucore::InvalidEncodingHandling; static NAME: &str = "tac"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static USAGE: &str = "[OPTION]... [FILE]..."; +static SUMMARY: &str = "Write each file to standard output, last line first."; -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - - let mut opts = getopts::Options::new(); - - opts.optflag( - "b", - "before", - "attach the separator before instead of after", - ); - opts.optflag( - "r", - "regex", - "interpret the sequence as a regular expression (NOT IMPLEMENTED)", - ); - opts.optopt( - "s", - "separator", - "use STRING as the separator instead of newline", - "STRING", - ); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "{}", f), - }; - if matches.opt_present("help") { - let msg = format!( - "{0} {1} - -Usage: - {0} [OPTION]... [FILE]... - -Write each file to standard output, last line first.", - NAME, VERSION - ); - - print!("{}", opts.usage(&msg)); - } else if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - } else { - let before = matches.opt_present("b"); - let regex = matches.opt_present("r"); - let separator = match matches.opt_str("s") { - Some(m) => { - if m.is_empty() { - crash!(1, "separator cannot be empty") - } else { - m - } - } - None => "\n".to_owned(), - }; - let files = if matches.free.is_empty() { - vec!["-".to_owned()] - } else { - matches.free - }; - tac(files, before, regex, &separator[..]); - } - - 0 +mod options { + pub static BEFORE: &str = "before"; + pub static REGEX: &str = "regex"; + pub static SEPARATOR: &str = "separator"; + pub static FILE: &str = "file"; } -fn tac(filenames: Vec, before: bool, _: bool, separator: &str) { +pub fn uumain(args: impl uucore::Args) -> i32 { + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); + + let matches = uu_app().get_matches_from(args); + + let before = matches.is_present(options::BEFORE); + let regex = matches.is_present(options::REGEX); + let separator = match matches.value_of(options::SEPARATOR) { + Some(m) => { + if m.is_empty() { + crash!(1, "separator cannot be empty") + } else { + m.to_owned() + } + } + None => "\n".to_owned(), + }; + + let files: Vec = match matches.values_of(options::FILE) { + Some(v) => v.map(|v| v.to_owned()).collect(), + None => vec!["-".to_owned()], + }; + + tac(files, before, regex, &separator[..]) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(USAGE) + .about(SUMMARY) + .arg( + Arg::with_name(options::BEFORE) + .short("b") + .long(options::BEFORE) + .help("attach the separator before instead of after") + .takes_value(false), + ) + .arg( + Arg::with_name(options::REGEX) + .short("r") + .long(options::REGEX) + .help("interpret the sequence as a regular expression (NOT IMPLEMENTED)") + .takes_value(false), + ) + .arg( + Arg::with_name(options::SEPARATOR) + .short("s") + .long(options::SEPARATOR) + .help("use STRING as the separator instead of newline") + .takes_value(true), + ) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) +} + +fn tac(filenames: Vec, before: bool, _: bool, separator: &str) -> i32 { + let mut exit_code = 0; let mut out = stdout(); let sbytes = separator.as_bytes(); let slen = sbytes.len(); @@ -91,10 +94,24 @@ fn tac(filenames: Vec, before: bool, _: bool, separator: &str) { let mut file = BufReader::new(if filename == "-" { Box::new(stdin()) as Box } else { - match File::open(filename) { + let path = Path::new(filename); + if path.is_dir() || path.metadata().is_err() { + if path.is_dir() { + show_error!("dir: read error: Invalid argument"); + } else { + show_error!( + "failed to open '{}' for reading: No such file or directory", + filename + ); + } + exit_code = 1; + continue; + } + match File::open(path) { Ok(f) => Box::new(f) as Box, Err(e) => { - show_warning!("failed to open '{}' for reading: {}", filename, e); + show_error!("failed to open '{}' for reading: {}", filename, e); + exit_code = 1; continue; } } @@ -102,7 +119,8 @@ fn tac(filenames: Vec, before: bool, _: bool, separator: &str) { let mut data = Vec::new(); if let Err(e) = file.read_to_end(&mut data) { - show_warning!("failed to read '{}': {}", filename, e); + show_error!("failed to read '{}': {}", filename, e); + exit_code = 1; continue; }; @@ -141,6 +159,8 @@ fn tac(filenames: Vec, before: bool, _: bool, separator: &str) { } show_line(&mut out, sbytes, &data[0..prev], before); } + + exit_code } fn show_line(out: &mut Stdout, sep: &[u8], dat: &[u8], before: bool) { diff --git a/src/uu/tail/Cargo.toml b/src/uu/tail/Cargo.toml index 6e51e3e35..273c67bb3 100644 --- a/src/uu/tail/Cargo.toml +++ b/src/uu/tail/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_tail" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "tail ~ (uutils) display the last lines of input" @@ -17,7 +17,7 @@ path = "src/tail.rs" [dependencies] clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["ringbuffer"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } winapi = { version="0.3", features=["fileapi", "handleapi", "processthreadsapi", "synchapi", "winbase"] } diff --git a/src/uu/tail/README.md b/src/uu/tail/README.md index 53936a609..b7f92f8e4 100644 --- a/src/uu/tail/README.md +++ b/src/uu/tail/README.md @@ -2,17 +2,17 @@ - Rudimentary tail implementation. -## Missing features: +## Missing features ### Flags with features -* [ ] `--max-unchanged-stats` : with `--follow=name`, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files). With inotify, this option is rarely useful. -* [ ] `--retry` : keep trying to open a file even when it is or becomes inaccessible; useful when follow‐ing by name, i.e., with `--follow=name` +- [ ] `--max-unchanged-stats` : with `--follow=name`, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files). With `inotify`, this option is rarely useful. +- [ ] `--retry` : keep trying to open a file even when it is or becomes inaccessible; useful when follow‐ing by name, i.e., with `--follow=name` ### Others - [ ] The current implementation does not handle `-` as an alias for stdin. -## Possible optimizations: +## Possible optimizations -* [ ] Don't read the whole file if not using `-f` and input is regular file. Read in chunks from the end going backwards, reading each individual chunk forward. +- [ ] Don't read the whole file if not using `-f` and input is regular file. Read in chunks from the end going backwards, reading each individual chunk forward. diff --git a/src/uu/tail/src/chunks.rs b/src/uu/tail/src/chunks.rs new file mode 100644 index 000000000..57a26dabf --- /dev/null +++ b/src/uu/tail/src/chunks.rs @@ -0,0 +1,83 @@ +//! Iterating over a file by chunks, starting at the end of the file. +//! +//! Use [`ReverseChunks::new`] to create a new iterator over chunks of +//! bytes from the file. +use std::fs::File; +use std::io::{Read, Seek, SeekFrom}; + +/// When reading files in reverse in `bounded_tail`, this is the size of each +/// block read at a time. +pub const BLOCK_SIZE: u64 = 1 << 16; + +/// An iterator over a file in non-overlapping chunks from the end of the file. +/// +/// Each chunk is a [`Vec`]<[`u8`]> of size [`BLOCK_SIZE`] (except +/// possibly the last chunk, which might be smaller). Each call to +/// [`next`] will seek backwards through the given file. +pub struct ReverseChunks<'a> { + /// The file to iterate over, by blocks, from the end to the beginning. + file: &'a File, + + /// The total number of bytes in the file. + size: u64, + + /// The total number of blocks to read. + max_blocks_to_read: usize, + + /// The index of the next block to read. + block_idx: usize, +} + +impl<'a> ReverseChunks<'a> { + pub fn new(file: &'a mut File) -> ReverseChunks<'a> { + let size = file.seek(SeekFrom::End(0)).unwrap(); + let max_blocks_to_read = (size as f64 / BLOCK_SIZE as f64).ceil() as usize; + let block_idx = 0; + ReverseChunks { + file, + size, + max_blocks_to_read, + block_idx, + } + } +} + +impl<'a> Iterator for ReverseChunks<'a> { + type Item = Vec; + + fn next(&mut self) -> Option { + // If there are no more chunks to read, terminate the iterator. + if self.block_idx >= self.max_blocks_to_read { + return None; + } + + // The chunk size is `BLOCK_SIZE` for all but the last chunk + // (that is, the chunk closest to the beginning of the file), + // which contains the remainder of the bytes. + let block_size = if self.block_idx == self.max_blocks_to_read - 1 { + self.size % BLOCK_SIZE + } else { + BLOCK_SIZE + }; + + // Seek backwards by the next chunk, read the full chunk into + // `buf`, and then seek back to the start of the chunk again. + let mut buf = vec![0; BLOCK_SIZE as usize]; + let pos = self + .file + .seek(SeekFrom::Current(-(block_size as i64))) + .unwrap(); + self.file + .read_exact(&mut buf[0..(block_size as usize)]) + .unwrap(); + let pos2 = self + .file + .seek(SeekFrom::Current(-(block_size as i64))) + .unwrap(); + assert_eq!(pos, pos2); + + self.block_idx += 1; + + Some(buf[0..(block_size as usize)].to_vec()) + } +} diff --git a/src/uu/tail/src/main.rs b/src/uu/tail/src/main.rs index 52818fad6..dd89ce2c7 100644 --- a/src/uu/tail/src/main.rs +++ b/src/uu/tail/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_tail); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_tail); diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index cd391a53e..4970cdcc2 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -5,7 +5,6 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// * // spell-checker:ignore (ToDO) seekable seek'd tail'ing ringbuffer ringbuf @@ -15,22 +14,24 @@ extern crate clap; #[macro_use] extern crate uucore; +mod chunks; mod platform; +use chunks::ReverseChunks; use clap::{App, Arg}; use std::collections::VecDeque; -use std::error::Error; use std::fmt; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, Read, Seek, SeekFrom, Write}; use std::path::Path; use std::thread::sleep; use std::time::Duration; +use uucore::parse_size::{parse_size, ParseSizeError}; +use uucore::ringbuffer::RingBuffer; pub mod options { pub mod verbosity { pub static QUIET: &str = "quiet"; - pub static SILENT: &str = "silent"; pub static VERBOSE: &str = "verbose"; } pub static BYTES: &str = "bytes"; @@ -39,13 +40,12 @@ pub mod options { pub static PID: &str = "pid"; pub static SLEEP_INT: &str = "sleep-interval"; pub static ZERO_TERM: &str = "zero-terminated"; + pub static ARG_FILES: &str = "files"; } -static ARG_FILES: &str = "files"; - enum FilterMode { - Bytes(u64), - Lines(u64, u8), // (number of lines, delimiter) + Bytes(usize), + Lines(usize, u8), // (number of lines, delimiter) } struct Settings { @@ -72,70 +72,7 @@ impl Default for Settings { pub fn uumain(args: impl uucore::Args) -> i32 { let mut settings: Settings = Default::default(); - let app = App::new(executable!()) - .version(crate_version!()) - .about("output the last part of files") - .arg( - Arg::with_name(options::BYTES) - .short("c") - .long(options::BYTES) - .takes_value(true) - .help("Number of bytes to print"), - ) - .arg( - Arg::with_name(options::FOLLOW) - .short("f") - .long(options::FOLLOW) - .help("Print the file as it grows"), - ) - .arg( - Arg::with_name(options::LINES) - .short("n") - .long(options::LINES) - .takes_value(true) - .help("Number of lines to print"), - ) - .arg( - Arg::with_name(options::PID) - .long(options::PID) - .takes_value(true) - .help("with -f, terminate after process ID, PID dies"), - ) - .arg( - Arg::with_name(options::verbosity::QUIET) - .short("q") - .long(options::verbosity::QUIET) - .help("never output headers giving file names"), - ) - .arg( - Arg::with_name(options::verbosity::SILENT) - .long(options::verbosity::SILENT) - .help("synonym of --quiet"), - ) - .arg( - Arg::with_name(options::SLEEP_INT) - .short("s") - .long(options::SLEEP_INT) - .help("Number or seconds to sleep between polling the file when running with -f"), - ) - .arg( - Arg::with_name(options::verbosity::VERBOSE) - .short("v") - .long(options::verbosity::VERBOSE) - .help("always output headers giving file names"), - ) - .arg( - Arg::with_name(options::ZERO_TERM) - .short("z") - .long(options::ZERO_TERM) - .help("Line delimiter is NUL, not newline"), - ) - .arg( - Arg::with_name(ARG_FILES) - .multiple(true) - .takes_value(true) - .min_values(1), - ); + let app = uu_app(); let matches = app.get_matches_from(args); @@ -165,38 +102,21 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } - match matches.value_of(options::LINES) { - Some(n) => { - let mut slice: &str = n; - if slice.chars().next().unwrap_or('_') == '+' { - settings.beginning = true; - slice = &slice[1..]; - } - match parse_size(slice) { - Ok(m) => settings.mode = FilterMode::Lines(m, b'\n'), - Err(e) => { - show_error!("{}", e.to_string()); - return 1; - } - } + let mode_and_beginning = if let Some(arg) = matches.value_of(options::BYTES) { + match parse_num(arg) { + Ok((n, beginning)) => (FilterMode::Bytes(n), beginning), + Err(e) => crash!(1, "invalid number of bytes: {}", e.to_string()), } - None => { - if let Some(n) = matches.value_of(options::BYTES) { - let mut slice: &str = n; - if slice.chars().next().unwrap_or('_') == '+' { - settings.beginning = true; - slice = &slice[1..]; - } - match parse_size(slice) { - Ok(m) => settings.mode = FilterMode::Bytes(m), - Err(e) => { - show_error!("{}", e.to_string()); - return 1; - } - } - } + } else if let Some(arg) = matches.value_of(options::LINES) { + match parse_num(arg) { + Ok((n, beginning)) => (FilterMode::Lines(n, b'\n'), beginning), + Err(e) => crash!(1, "invalid number of lines: {}", e.to_string()), } + } else { + (FilterMode::Lines(10, b'\n'), false) }; + settings.mode = mode_and_beginning.0; + settings.beginning = mode_and_beginning.1; if matches.is_present(options::ZERO_TERM) { if let FilterMode::Lines(count, _) = settings.mode { @@ -205,11 +125,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } let verbose = matches.is_present(options::verbosity::VERBOSE); - let quiet = matches.is_present(options::verbosity::QUIET) - || matches.is_present(options::verbosity::SILENT); + let quiet = matches.is_present(options::verbosity::QUIET); let files: Vec = matches - .values_of(ARG_FILES) + .values_of(options::ARG_FILES) .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); @@ -236,7 +155,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } let mut file = File::open(&path).unwrap(); if is_seekable(&mut file) { - bounded_tail(&file, &settings); + bounded_tail(&mut file, &settings); if settings.follow { let reader = BufReader::new(file); readers.push(reader); @@ -258,102 +177,77 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } -#[derive(Debug, PartialEq, Eq)] -pub enum ParseSizeErr { - ParseFailure(String), - SizeTooBig(String), +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about("output the last part of files") + // TODO: add usage + .arg( + Arg::with_name(options::BYTES) + .short("c") + .long(options::BYTES) + .takes_value(true) + .allow_hyphen_values(true) + .overrides_with_all(&[options::BYTES, options::LINES]) + .help("Number of bytes to print"), + ) + .arg( + Arg::with_name(options::FOLLOW) + .short("f") + .long(options::FOLLOW) + .help("Print the file as it grows"), + ) + .arg( + Arg::with_name(options::LINES) + .short("n") + .long(options::LINES) + .takes_value(true) + .allow_hyphen_values(true) + .overrides_with_all(&[options::BYTES, options::LINES]) + .help("Number of lines to print"), + ) + .arg( + Arg::with_name(options::PID) + .long(options::PID) + .takes_value(true) + .help("with -f, terminate after process ID, PID dies"), + ) + .arg( + Arg::with_name(options::verbosity::QUIET) + .short("q") + .long(options::verbosity::QUIET) + .visible_alias("silent") + .overrides_with_all(&[options::verbosity::QUIET, options::verbosity::VERBOSE]) + .help("never output headers giving file names"), + ) + .arg( + Arg::with_name(options::SLEEP_INT) + .short("s") + .takes_value(true) + .long(options::SLEEP_INT) + .help("Number or seconds to sleep between polling the file when running with -f"), + ) + .arg( + Arg::with_name(options::verbosity::VERBOSE) + .short("v") + .long(options::verbosity::VERBOSE) + .overrides_with_all(&[options::verbosity::QUIET, options::verbosity::VERBOSE]) + .help("always output headers giving file names"), + ) + .arg( + Arg::with_name(options::ZERO_TERM) + .short("z") + .long(options::ZERO_TERM) + .help("Line delimiter is NUL, not newline"), + ) + .arg( + Arg::with_name(options::ARG_FILES) + .multiple(true) + .takes_value(true) + .min_values(1), + ) } -impl Error for ParseSizeErr { - fn description(&self) -> &str { - match *self { - ParseSizeErr::ParseFailure(ref s) => &*s, - ParseSizeErr::SizeTooBig(ref s) => &*s, - } - } -} - -impl fmt::Display for ParseSizeErr { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - let s = match self { - ParseSizeErr::ParseFailure(s) => s, - ParseSizeErr::SizeTooBig(s) => s, - }; - write!(f, "{}", s) - } -} - -impl ParseSizeErr { - fn parse_failure(s: &str) -> ParseSizeErr { - ParseSizeErr::ParseFailure(format!("invalid size: '{}'", s)) - } - - fn size_too_big(s: &str) -> ParseSizeErr { - ParseSizeErr::SizeTooBig(format!( - "invalid size: '{}': Value too large to be stored in data type", - s - )) - } -} - -pub type ParseSizeResult = Result; - -pub fn parse_size(mut size_slice: &str) -> Result { - let mut base = if size_slice.chars().last().unwrap_or('_') == 'B' { - size_slice = &size_slice[..size_slice.len() - 1]; - 1000u64 - } else { - 1024u64 - }; - - let exponent = if !size_slice.is_empty() { - let mut has_suffix = true; - let exp = match size_slice.chars().last().unwrap_or('_') { - 'K' | 'k' => 1u64, - 'M' => 2u64, - 'G' => 3u64, - 'T' => 4u64, - 'P' => 5u64, - 'E' => 6u64, - 'Z' | 'Y' => { - return Err(ParseSizeErr::size_too_big(size_slice)); - } - 'b' => { - base = 512u64; - 1u64 - } - _ => { - has_suffix = false; - 0u64 - } - }; - if has_suffix { - size_slice = &size_slice[..size_slice.len() - 1]; - } - exp - } else { - 0u64 - }; - - let mut multiplier = 1u64; - for _ in 0u64..exponent { - multiplier *= base; - } - if base == 1000u64 && exponent == 0u64 { - // sole B is not a valid suffix - Err(ParseSizeErr::parse_failure(size_slice)) - } else { - let value: Option = size_slice.parse().ok(); - value - .map(|v| Ok(multiplier * v)) - .unwrap_or_else(|| Err(ParseSizeErr::parse_failure(size_slice))) - } -} - -/// When reading files in reverse in `bounded_tail`, this is the size of each -/// block read at a time. -const BLOCK_SIZE: u64 = 1 << 16; - fn follow(readers: &mut [BufReader], filenames: &[String], settings: &Settings) { assert!(settings.follow); let mut last = readers.len() - 1; @@ -380,7 +274,7 @@ fn follow(readers: &mut [BufReader], filenames: &[String], settings: } print!("{}", datum); } - Err(err) => panic!(err), + Err(err) => panic!("{}", err), } } } @@ -391,48 +285,42 @@ fn follow(readers: &mut [BufReader], filenames: &[String], settings: } } -/// Iterate over bytes in the file, in reverse, until `should_stop` returns -/// true. The `file` is left seek'd to the position just after the byte that -/// `should_stop` returned true for. -fn backwards_thru_file( - mut file: &File, - size: u64, - buf: &mut Vec, - delimiter: u8, - should_stop: &mut F, -) where - F: FnMut(u8) -> bool, -{ - assert!(buf.len() >= BLOCK_SIZE as usize); +/// Iterate over bytes in the file, in reverse, until we find the +/// `num_delimiters` instance of `delimiter`. The `file` is left seek'd to the +/// position just after that delimiter. +fn backwards_thru_file(file: &mut File, num_delimiters: usize, delimiter: u8) { + // This variable counts the number of delimiters found in the file + // so far (reading from the end of the file toward the beginning). + let mut counter = 0; - let max_blocks_to_read = (size as f64 / BLOCK_SIZE as f64).ceil() as usize; + for (block_idx, slice) in ReverseChunks::new(file).enumerate() { + // Iterate over each byte in the slice in reverse order. + let mut iter = slice.iter().enumerate().rev(); - for block_idx in 0..max_blocks_to_read { - let block_size = if block_idx == max_blocks_to_read - 1 { - size % BLOCK_SIZE - } else { - BLOCK_SIZE - }; - - // Seek backwards by the next block, read the full block into - // `buf`, and then seek back to the start of the block again. - let pos = file.seek(SeekFrom::Current(-(block_size as i64))).unwrap(); - file.read_exact(&mut buf[0..(block_size as usize)]).unwrap(); - let pos2 = file.seek(SeekFrom::Current(-(block_size as i64))).unwrap(); - assert_eq!(pos, pos2); - - // Iterate backwards through the bytes, calling `should_stop` on each - // one. - let slice = &buf[0..(block_size as usize)]; - for (i, ch) in slice.iter().enumerate().rev() { - // Ignore one trailing newline. - if block_idx == 0 && i as u64 == block_size - 1 && *ch == delimiter { - continue; + // Ignore a trailing newline in the last block, if there is one. + if block_idx == 0 { + if let Some(c) = slice.last() { + if *c == delimiter { + iter.next(); + } } + } - if should_stop(*ch) { - file.seek(SeekFrom::Current((i + 1) as i64)).unwrap(); - return; + // For each byte, increment the count of the number of + // delimiters found. If we have found more than the specified + // number of delimiters, terminate the search and seek to the + // appropriate location in the file. + for (i, ch) in iter { + if *ch == delimiter { + counter += 1; + if counter >= num_delimiters { + // After each iteration of the outer loop, the + // cursor in the file is at the *beginning* of the + // block, so seeking forward by `i + 1` bytes puts + // us right after the found delimiter. + file.seek(SeekFrom::Current((i + 1) as i64)).unwrap(); + return; + } } } } @@ -443,21 +331,11 @@ fn backwards_thru_file( /// end of the file, and then read the file "backwards" in blocks of size /// `BLOCK_SIZE` until we find the location of the first line/byte. This ends up /// being a nice performance win for very large files. -fn bounded_tail(mut file: &File, settings: &Settings) { - let size = file.seek(SeekFrom::End(0)).unwrap(); - let mut buf = vec![0; BLOCK_SIZE as usize]; - +fn bounded_tail(file: &mut File, settings: &Settings) { // Find the position in the file to start printing from. match settings.mode { - FilterMode::Lines(mut count, delimiter) => { - backwards_thru_file(&file, size, &mut buf, delimiter, &mut |byte| { - if byte == delimiter { - count -= 1; - count == 0 - } else { - false - } - }); + FilterMode::Lines(count, delimiter) => { + backwards_thru_file(file, count as usize, delimiter); } FilterMode::Bytes(count) => { file.seek(SeekFrom::End(-(count as i64))).unwrap(); @@ -465,17 +343,37 @@ fn bounded_tail(mut file: &File, settings: &Settings) { } // Print the target section of the file. - loop { - let bytes_read = file.read(&mut buf).unwrap(); + let stdout = stdout(); + let mut stdout = stdout.lock(); + std::io::copy(file, &mut stdout).unwrap(); +} - let mut stdout = stdout(); - for b in &buf[0..bytes_read] { - print_byte(&mut stdout, *b); - } - - if bytes_read == 0 { - break; - } +/// Collect the last elements of an iterator into a `VecDeque`. +/// +/// This function returns a [`VecDeque`] containing either the last +/// `count` elements of `iter`, an [`Iterator`] over [`Result`] +/// instances, or all but the first `count` elements of `iter`. If +/// `beginning` is `true`, then all but the first `count` elements are +/// returned. +/// +/// # Panics +/// +/// If any element of `iter` is an [`Err`], then this function panics. +fn unbounded_tail_collect( + iter: impl Iterator>, + count: usize, + beginning: bool, +) -> VecDeque +where + E: fmt::Debug, +{ + if beginning { + // GNU `tail` seems to index bytes and lines starting at 1, not + // at 0. It seems to treat `+0` and `+1` as the same thing. + let i = count.max(1) - 1; + iter.skip(i as usize).map(|r| r.unwrap()).collect() + } else { + RingBuffer::from_iter(iter.map(|r| r.unwrap()), count as usize).data } } @@ -484,66 +382,15 @@ fn unbounded_tail(reader: &mut BufReader, settings: &Settings) { // contains count lines/chars. When reaching the end of file, output the // data in the ringbuf. match settings.mode { - FilterMode::Lines(mut count, _delimiter) => { - let mut ringbuf: VecDeque = VecDeque::new(); - let mut skip = if settings.beginning { - let temp = count; - count = ::std::u64::MAX; - temp - 1 - } else { - 0 - }; - loop { - let mut datum = String::new(); - match reader.read_line(&mut datum) { - Ok(0) => break, - Ok(_) => { - if skip > 0 { - skip -= 1; - } else { - if count <= ringbuf.len() as u64 { - ringbuf.pop_front(); - } - ringbuf.push_back(datum); - } - } - Err(err) => panic!(err), - } - } - let mut stdout = stdout(); - for datum in &ringbuf { - print_string(&mut stdout, datum); + FilterMode::Lines(count, _) => { + for line in unbounded_tail_collect(reader.lines(), count, settings.beginning) { + println!("{}", line); } } - FilterMode::Bytes(mut count) => { - let mut ringbuf: VecDeque = VecDeque::new(); - let mut skip = if settings.beginning { - let temp = count; - count = ::std::u64::MAX; - temp - 1 - } else { - 0 - }; - loop { - let mut datum = [0; 1]; - match reader.read(&mut datum) { - Ok(0) => break, - Ok(_) => { - if skip > 0 { - skip -= 1; - } else { - if count <= ringbuf.len() as u64 { - ringbuf.pop_front(); - } - ringbuf.push_back(datum[0]); - } - } - Err(err) => panic!(err), - } - } - let mut stdout = stdout(); - for datum in &ringbuf { - print_byte(&mut stdout, *datum); + FilterMode::Bytes(count) => { + for byte in unbounded_tail_collect(reader.bytes(), count, settings.beginning) { + let mut stdout = stdout(); + print_byte(&mut stdout, byte); } } } @@ -560,7 +407,21 @@ fn print_byte(stdout: &mut T, ch: u8) { } } -#[inline] -fn print_string(_: &mut T, s: &str) { - print!("{}", s); +fn parse_num(src: &str) -> Result<(usize, bool), ParseSizeError> { + let mut size_string = src.trim(); + let mut starting_with = false; + + if let Some(c) = size_string.chars().next() { + if c == '+' || c == '-' { + // tail: '-' is not documented (8.32 man pages) + size_string = &size_string[1..]; + if c == '+' { + starting_with = true; + } + } + } else { + return Err(ParseSizeError::ParseFailure(src.to_string())); + } + + parse_size(size_string).map(|n| (n, starting_with)) } diff --git a/src/uu/tee/Cargo.toml b/src/uu/tee/Cargo.toml index ee18e888f..7ac81adc4 100644 --- a/src/uu/tee/Cargo.toml +++ b/src/uu/tee/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_tee" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "tee ~ (uutils) display input and copy to FILE" @@ -15,9 +15,10 @@ edition = "2018" path = "src/tee.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33.3" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["libc"] } +retain_mut = "0.1.2" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["libc"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/tee/src/main.rs b/src/uu/tee/src/main.rs index 1314f353e..2b483d9d8 100644 --- a/src/uu/tee/src/main.rs +++ b/src/uu/tee/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_tee); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_tee); diff --git a/src/uu/tee/src/tee.rs b/src/uu/tee/src/tee.rs index e29945382..a207dee63 100644 --- a/src/uu/tee/src/tee.rs +++ b/src/uu/tee/src/tee.rs @@ -5,6 +5,11 @@ // * 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 clap::{crate_version, App, Arg}; +use retain_mut::RetainMut; use std::fs::OpenOptions; use std::io::{copy, sink, stdin, stdout, Error, ErrorKind, Read, Result, Write}; use std::path::{Path, PathBuf}; @@ -12,81 +17,63 @@ use std::path::{Path, PathBuf}; #[cfg(unix)] use uucore::libc; -static NAME: &str = "tee"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Copy standard input to each FILE, and also to standard output."; + +mod options { + pub const APPEND: &str = "append"; + pub const IGNORE_INTERRUPTS: &str = "ignore-interrupts"; + pub const FILE: &str = "file"; +} + +#[allow(dead_code)] +struct Options { + append: bool, + ignore_interrupts: bool, + files: Vec, +} + +fn get_usage() -> String { + format!("{0} [OPTION]... [FILE]...", executable!()) +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let usage = get_usage(); - match options(&args).and_then(exec) { + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let options = Options { + append: matches.is_present(options::APPEND), + ignore_interrupts: matches.is_present(options::IGNORE_INTERRUPTS), + files: matches + .values_of(options::FILE) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(), + }; + + match tee(options) { Ok(_) => 0, Err(_) => 1, } } -#[allow(dead_code)] -struct Options { - program: String, - append: bool, - ignore_interrupts: bool, - print_and_exit: Option, - files: Vec, -} - -fn options(args: &[String]) -> Result { - let mut opts = getopts::Options::new(); - - opts.optflag("a", "append", "append to the given FILEs, do not overwrite"); - opts.optflag( - "i", - "ignore-interrupts", - "ignore interrupt signals (ignored on non-Unix platforms)", - ); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - - opts.parse(&args[1..]) - .map_err(|e| Error::new(ErrorKind::Other, format!("{}", e))) - .map(|m| { - let version = format!("{} {}", NAME, VERSION); - let arguments = "[OPTION]... [FILE]..."; - let brief = "Copy standard input to each FILE, and also to standard output."; - let comment = "If a FILE is -, it refers to a file named - ."; - let help = format!( - "{}\n\nUsage:\n {} {}\n\n{}\n{}", - version, - NAME, - arguments, - opts.usage(brief), - comment - ); - let names: Vec = m.free.clone().into_iter().collect(); - let to_print = if m.opt_present("help") { - Some(help) - } else if m.opt_present("version") { - Some(version) - } else { - None - }; - Options { - program: NAME.to_owned(), - append: m.opt_present("append"), - ignore_interrupts: m.opt_present("ignore-interrupts"), - print_and_exit: to_print, - files: names, - } - }) - .map_err(|message| warn(format!("{}", message).as_ref())) -} - -fn exec(options: Options) -> Result<()> { - match options.print_and_exit { - Some(text) => { - println!("{}", text); - Ok(()) - } - None => tee(options), - } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .after_help("If a FILE is -, it refers to a file named - .") + .arg( + Arg::with_name(options::APPEND) + .long(options::APPEND) + .short("a") + .help("append to the given FILEs, do not overwrite"), + ) + .arg( + Arg::with_name(options::IGNORE_INTERRUPTS) + .long(options::IGNORE_INTERRUPTS) + .short("i") + .help("ignore interrupt signals (ignored on non-Unix platforms)"), + ) + .arg(Arg::with_name(options::FILE).multiple(true)) } #[cfg(unix)] @@ -108,18 +95,32 @@ fn tee(options: Options) -> Result<()> { if options.ignore_interrupts { ignore_interrupts()? } - let mut writers: Vec> = options + let mut writers: Vec = options .files .clone() .into_iter() - .map(|file| open(file, options.append)) + .map(|file| NamedWriter { + name: file.clone(), + inner: open(file, options.append), + }) .collect(); - writers.push(Box::new(stdout())); - let output = &mut MultiWriter { writers }; + + writers.insert( + 0, + NamedWriter { + name: "'standard output'".to_owned(), + inner: Box::new(stdout()), + }, + ); + + let mut output = MultiWriter::new(writers); let input = &mut NamedReader { inner: Box::new(stdin()) as Box, }; - if copy(input, output).is_err() || output.flush().is_err() { + + // TODO: replaced generic 'copy' call to be able to stop copying + // if all outputs are closed (due to errors) + if copy(input, &mut output).is_err() || output.flush().is_err() || output.error_occurred() { Err(Error::new(ErrorKind::Other, "")) } else { Ok(()) @@ -127,7 +128,7 @@ fn tee(options: Options) -> Result<()> { } fn open(name: String, append: bool) -> Box { - let path = PathBuf::from(name); + let path = PathBuf::from(name.clone()); let inner: Box = { let mut options = OpenOptions::new(); let mode = if append { @@ -140,55 +141,68 @@ fn open(name: String, append: bool) -> Box { Err(_) => Box::new(sink()), } }; - Box::new(NamedWriter { inner, path }) as Box + Box::new(NamedWriter { inner, name }) as Box } struct MultiWriter { - writers: Vec>, + writers: Vec, + initial_len: usize, +} + +impl MultiWriter { + fn new(writers: Vec) -> Self { + Self { + initial_len: writers.len(), + writers, + } + } + fn error_occurred(&self) -> bool { + self.writers.len() != self.initial_len + } } impl Write for MultiWriter { fn write(&mut self, buf: &[u8]) -> Result { - for writer in &mut self.writers { - writer.write_all(buf)?; - } + self.writers.retain_mut(|writer| { + let result = writer.write_all(buf); + match result { + Err(f) => { + show_error!("{}: {}", writer.name, f.to_string()); + false + } + _ => true, + } + }); Ok(buf.len()) } fn flush(&mut self) -> Result<()> { - for writer in &mut self.writers { - writer.flush()?; - } + self.writers.retain_mut(|writer| { + let result = writer.flush(); + match result { + Err(f) => { + show_error!("{}: {}", writer.name, f.to_string()); + false + } + _ => true, + } + }); Ok(()) } } struct NamedWriter { inner: Box, - path: PathBuf, + pub name: String, } impl Write for NamedWriter { fn write(&mut self, buf: &[u8]) -> Result { - match self.inner.write(buf) { - Err(f) => { - self.inner = Box::new(sink()) as Box; - warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref()); - Err(f) - } - okay => okay, - } + self.inner.write(buf) } fn flush(&mut self) -> Result<()> { - match self.inner.flush() { - Err(f) => { - self.inner = Box::new(sink()) as Box; - warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref()); - Err(f) - } - okay => okay, - } + self.inner.flush() } } @@ -200,15 +214,10 @@ impl Read for NamedReader { fn read(&mut self, buf: &mut [u8]) -> Result { match self.inner.read(buf) { Err(f) => { - warn(format!("{}: {}", Path::new("stdin").display(), f.to_string()).as_ref()); + show_error!("{}: {}", Path::new("stdin").display(), f.to_string()); Err(f) } okay => okay, } } } - -fn warn(message: &str) -> Error { - eprintln!("{}: {}", NAME, message); - Error::new(ErrorKind::Other, format!("{}: {}", NAME, message)) -} diff --git a/src/uu/test/Cargo.toml b/src/uu/test/Cargo.toml index 6471c9e62..cd0282a45 100644 --- a/src/uu/test/Cargo.toml +++ b/src/uu/test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_test" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "test ~ (uutils) evaluate comparison and file type expressions" @@ -15,8 +15,9 @@ edition = "2018" path = "src/test.rs" [dependencies] +clap = "2.33.3" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [target.'cfg(target_os = "redox")'.dependencies] diff --git a/src/uu/test/src/main.rs b/src/uu/test/src/main.rs index 5018a5c8c..9f4e6985f 100644 --- a/src/uu/test/src/main.rs +++ b/src/uu/test/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_test); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_test); diff --git a/src/uu/test/src/parser.rs b/src/uu/test/src/parser.rs new file mode 100644 index 000000000..5eec781ba --- /dev/null +++ b/src/uu/test/src/parser.rs @@ -0,0 +1,329 @@ +// This file is part of the uutils coreutils package. +// +// (c) Daniel Rocco +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +// spell-checker:ignore (grammar) BOOLOP STRLEN FILETEST FILEOP INTOP STRINGOP ; (vars) LParen StrlenOp + +use std::ffi::OsString; +use std::iter::Peekable; + +/// Represents a parsed token from a test expression +#[derive(Debug, PartialEq)] +pub enum Symbol { + LParen, + Bang, + BoolOp(OsString), + Literal(OsString), + StringOp(OsString), + IntOp(OsString), + FileOp(OsString), + StrlenOp(OsString), + FiletestOp(OsString), + None, +} + +impl Symbol { + /// Create a new Symbol from an OsString. + /// + /// Returns Symbol::None in place of None + fn new(token: Option) -> Symbol { + match token { + Some(s) => match s.to_string_lossy().as_ref() { + "(" => Symbol::LParen, + "!" => Symbol::Bang, + "-a" | "-o" => Symbol::BoolOp(s), + "=" | "==" | "!=" => Symbol::StringOp(s), + "-eq" | "-ge" | "-gt" | "-le" | "-lt" | "-ne" => Symbol::IntOp(s), + "-ef" | "-nt" | "-ot" => Symbol::FileOp(s), + "-n" | "-z" => Symbol::StrlenOp(s), + "-b" | "-c" | "-d" | "-e" | "-f" | "-g" | "-G" | "-h" | "-k" | "-L" | "-O" + | "-p" | "-r" | "-s" | "-S" | "-t" | "-u" | "-w" | "-x" => Symbol::FiletestOp(s), + _ => Symbol::Literal(s), + }, + None => Symbol::None, + } + } + + /// Convert this Symbol into a Symbol::Literal, useful for cases where + /// test treats an operator as a string operand (test has no reserved + /// words). + /// + /// # Panics + /// + /// Panics if `self` is Symbol::None + fn into_literal(self) -> Symbol { + Symbol::Literal(match self { + Symbol::LParen => OsString::from("("), + Symbol::Bang => OsString::from("!"), + Symbol::BoolOp(s) + | Symbol::Literal(s) + | Symbol::StringOp(s) + | Symbol::IntOp(s) + | Symbol::FileOp(s) + | Symbol::StrlenOp(s) + | Symbol::FiletestOp(s) => s, + Symbol::None => panic!(), + }) + } +} + +/// Recursive descent parser for test, which converts a list of OsStrings +/// (typically command line arguments) into a stack of Symbols in postfix +/// order. +/// +/// Grammar: +/// +/// EXPR → TERM | EXPR BOOLOP EXPR +/// TERM → ( EXPR ) +/// TERM → ( ) +/// TERM → ! EXPR +/// TERM → UOP str +/// UOP → STRLEN | FILETEST +/// TERM → str OP str +/// TERM → str | 𝜖 +/// OP → STRINGOP | INTOP | FILEOP +/// STRINGOP → = | == | != +/// INTOP → -eq | -ge | -gt | -le | -lt | -ne +/// FILEOP → -ef | -nt | -ot +/// STRLEN → -n | -z +/// FILETEST → -b | -c | -d | -e | -f | -g | -G | -h | -k | -L | -O | -p | +/// -r | -s | -S | -t | -u | -w | -x +/// BOOLOP → -a | -o +/// +#[derive(Debug)] +struct Parser { + tokens: Peekable>, + pub stack: Vec, +} + +impl Parser { + /// Construct a new Parser from a `Vec` of tokens. + fn new(tokens: Vec) -> Parser { + Parser { + tokens: tokens.into_iter().peekable(), + stack: vec![], + } + } + + /// Fetch the next token from the input stream as a Symbol. + fn next_token(&mut self) -> Symbol { + Symbol::new(self.tokens.next()) + } + + /// Peek at the next token from the input stream, returning it as a Symbol. + /// The stream is unchanged and will return the same Symbol on subsequent + /// calls to `next()` or `peek()`. + fn peek(&mut self) -> Symbol { + Symbol::new(self.tokens.peek().map(|s| s.to_os_string())) + } + + /// Test if the next token in the stream is a BOOLOP (-a or -o), without + /// removing the token from the stream. + fn peek_is_boolop(&mut self) -> bool { + matches!(self.peek(), Symbol::BoolOp(_)) + } + + /// Parse an expression. + /// + /// EXPR → TERM | EXPR BOOLOP EXPR + fn expr(&mut self) { + if !self.peek_is_boolop() { + self.term(); + } + self.maybe_boolop(); + } + + /// Parse a term token and possible subsequent symbols: "(", "!", UOP, + /// literal, or None. + fn term(&mut self) { + let symbol = self.next_token(); + + match symbol { + Symbol::LParen => self.lparen(), + Symbol::Bang => self.bang(), + Symbol::StrlenOp(_) => self.uop(symbol), + Symbol::FiletestOp(_) => self.uop(symbol), + Symbol::None => self.stack.push(symbol), + literal => self.literal(literal), + } + } + + /// Parse a (possibly) parenthesized expression. + /// + /// test has no reserved keywords, so "(" will be interpreted as a literal + /// if it is followed by nothing or a comparison operator OP. + fn lparen(&mut self) { + match self.peek() { + // lparen is a literal when followed by nothing or comparison + Symbol::None | Symbol::StringOp(_) | Symbol::IntOp(_) | Symbol::FileOp(_) => { + self.literal(Symbol::LParen.into_literal()); + } + // empty parenthetical + Symbol::Literal(s) if s == ")" => {} + _ => { + self.expr(); + match self.next_token() { + Symbol::Literal(s) if s == ")" => (), + _ => panic!("expected ')'"), + } + } + } + } + + /// Parse a (possibly) negated expression. + /// + /// Example cases: + /// + /// * `! =`: negate the result of the implicit string length test of `=` + /// * `! = foo`: compare the literal strings `!` and `foo` + /// * `! = = str`: negate comparison of literal `=` and `str` + /// * `!`: bang followed by nothing is literal + /// * `! EXPR`: negate the result of the expression + /// + /// Combined Boolean & negation: + /// + /// * `! ( EXPR ) [BOOLOP EXPR]`: negate the parenthesized expression only + /// * `! UOP str BOOLOP EXPR`: negate the unary subexpression + /// * `! str BOOLOP str`: negate the entire Boolean expression + /// * `! str BOOLOP EXPR BOOLOP EXPR`: negate the value of the first `str` term + /// + fn bang(&mut self) { + match self.peek() { + Symbol::StringOp(_) | Symbol::IntOp(_) | Symbol::FileOp(_) | Symbol::BoolOp(_) => { + // we need to peek ahead one more token to disambiguate the first + // three cases listed above + let peek2 = Symbol::new(self.tokens.clone().nth(1)); + + match peek2 { + // case 1: `! ` + // case 3: `! = OP str` + Symbol::StringOp(_) | Symbol::None => { + // op is literal + let op = self.next_token().into_literal(); + self.literal(op); + self.stack.push(Symbol::Bang); + } + // case 2: ` OP str [BOOLOP EXPR]`. + _ => { + // bang is literal; parsing continues with op + self.literal(Symbol::Bang.into_literal()); + self.maybe_boolop(); + } + } + } + + // bang followed by nothing is literal + Symbol::None => self.stack.push(Symbol::Bang.into_literal()), + + _ => { + // peek ahead up to 4 tokens to determine if we need to negate + // the entire expression or just the first term + let peek4: Vec = self + .tokens + .clone() + .take(4) + .map(|token| Symbol::new(Some(token))) + .collect(); + + match peek4.as_slice() { + // we peeked ahead 4 but there were only 3 tokens left + [Symbol::Literal(_), Symbol::BoolOp(_), Symbol::Literal(_)] => { + self.expr(); + self.stack.push(Symbol::Bang); + } + _ => { + self.term(); + self.stack.push(Symbol::Bang); + } + } + } + } + } + + /// Peek at the next token and parse it as a BOOLOP or string literal, + /// as appropriate. + fn maybe_boolop(&mut self) { + if self.peek_is_boolop() { + let symbol = self.next_token(); + + // BoolOp by itself interpreted as Literal + if let Symbol::None = self.peek() { + self.literal(symbol.into_literal()); + } else { + self.boolop(symbol); + self.maybe_boolop(); + } + } + } + + /// Parse a Boolean expression. + /// + /// Logical and (-a) has higher precedence than or (-o), so in an + /// expression like `foo -o '' -a ''`, the and subexpression is evaluated + /// first. + fn boolop(&mut self, op: Symbol) { + if op == Symbol::BoolOp(OsString::from("-a")) { + self.term(); + } else { + self.expr(); + } + self.stack.push(op); + } + + /// Parse a (possible) unary argument test (string length or file + /// attribute check). + /// + /// If a UOP is followed by nothing it is interpreted as a literal string. + fn uop(&mut self, op: Symbol) { + match self.next_token() { + Symbol::None => self.stack.push(op.into_literal()), + symbol => { + self.stack.push(symbol.into_literal()); + self.stack.push(op); + } + } + } + + /// Parse a string literal, optionally followed by a comparison operator + /// and a second string literal. + fn literal(&mut self, token: Symbol) { + self.stack.push(token.into_literal()); + + // EXPR → str OP str + match self.peek() { + Symbol::StringOp(_) | Symbol::IntOp(_) | Symbol::FileOp(_) => { + let op = self.next_token(); + + match self.next_token() { + Symbol::None => panic!("missing argument after {:?}", op), + token => self.stack.push(token.into_literal()), + } + + self.stack.push(op); + } + _ => {} + } + } + + /// Parser entry point: parse the token stream `self.tokens`, storing the + /// resulting `Symbol` stack in `self.stack`. + fn parse(&mut self) -> Result<(), String> { + self.expr(); + + match self.tokens.next() { + Some(token) => Err(format!("extra argument '{}'", token.to_string_lossy())), + None => Ok(()), + } + } +} + +/// Parse the token stream `args`, returning a `Symbol` stack representing the +/// operations to perform in postfix order. +pub fn parse(args: Vec) -> Result, String> { + let mut p = Parser::new(args); + p.parse()?; + Ok(p.stack) +} diff --git a/src/uu/test/src/test.rs b/src/uu/test/src/test.rs index 4394e4a8e..dba840d3c 100644 --- a/src/uu/test/src/test.rs +++ b/src/uu/test/src/test.rs @@ -1,144 +1,179 @@ -// * This file is part of the uutils coreutils package. -// * -// * (c) mahkoh (ju.orth [at] gmail [dot] com) -// * -// * For the full copyright and license information, please view the LICENSE -// * file that was distributed with this source code. +// This file is part of the uutils coreutils package. +// +// (c) mahkoh (ju.orth [at] gmail [dot] com) +// (c) Daniel Rocco +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. -// spell-checker:ignore (ToDO) retval paren prec subprec cond +// spell-checker:ignore (vars) egid euid FiletestOp StrlenOp -use std::collections::HashMap; -use std::str::from_utf8; +mod parser; -static NAME: &str = "test"; +use clap::{App, AppSettings}; +use parser::{parse, Symbol}; +use std::ffi::{OsStr, OsString}; +use std::path::Path; +use uucore::executable; -pub fn uumain(args: impl uucore::Args) -> i32 { - let args: Vec<_> = args.collect(); - // This is completely disregarding valid windows paths that aren't valid unicode - let args = args - .iter() - .map(|a| a.to_str().unwrap().as_bytes()) - .collect::>(); - if args.is_empty() { - return 2; - } - let args = if !args[0].ends_with(NAME.as_bytes()) { - &args[1..] - } else { - &args[..] - }; - let args = match args[0] { - b"[" => match args[args.len() - 1] { - b"]" => &args[1..args.len() - 1], - _ => return 2, - }, - _ => &args[1..args.len()], - }; - let mut error = false; - let retval = 1 - parse_expr(args, &mut error) as i32; - if error { - 2 - } else { - retval - } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .setting(AppSettings::DisableHelpFlags) + .setting(AppSettings::DisableVersion) } -fn one(args: &[&[u8]]) -> bool { - !args[0].is_empty() -} +pub fn uumain(mut args: impl uucore::Args) -> i32 { + let program = args.next().unwrap_or_else(|| OsString::from("test")); + let binary_name = Path::new(&program) + .file_name() + .unwrap_or_else(|| OsStr::new("test")) + .to_string_lossy(); + let mut args: Vec<_> = args.collect(); -fn two(args: &[&[u8]], error: &mut bool) -> bool { - match args[0] { - b"!" => !one(&args[1..]), - b"-b" => path(args[1], PathCondition::BlockSpecial), - b"-c" => path(args[1], PathCondition::CharacterSpecial), - b"-d" => path(args[1], PathCondition::Directory), - b"-e" => path(args[1], PathCondition::Exists), - b"-f" => path(args[1], PathCondition::Regular), - b"-g" => path(args[1], PathCondition::GroupIDFlag), - b"-h" => path(args[1], PathCondition::SymLink), - b"-L" => path(args[1], PathCondition::SymLink), - b"-n" => one(&args[1..]), - b"-p" => path(args[1], PathCondition::FIFO), - b"-r" => path(args[1], PathCondition::Readable), - b"-S" => path(args[1], PathCondition::Socket), - b"-s" => path(args[1], PathCondition::NonEmpty), - b"-t" => isatty(args[1]), - b"-u" => path(args[1], PathCondition::UserIDFlag), - b"-w" => path(args[1], PathCondition::Writable), - b"-x" => path(args[1], PathCondition::Executable), - b"-z" => !one(&args[1..]), - _ => { - *error = true; - false + // If invoked via name '[', matching ']' must be in the last arg + if binary_name == "[" { + let last = args.pop(); + if last != Some(OsString::from("]")) { + eprintln!("[: missing ']'"); + return 2; } } -} -fn three(args: &[&[u8]], error: &mut bool) -> bool { - match args[1] { - b"=" => args[0] == args[2], - b"==" => args[0] == args[2], - b"!=" => args[0] != args[2], - b"-eq" => integers(args[0], args[2], IntegerCondition::Equal), - b"-ne" => integers(args[0], args[2], IntegerCondition::Unequal), - b"-gt" => integers(args[0], args[2], IntegerCondition::Greater), - b"-ge" => integers(args[0], args[2], IntegerCondition::GreaterEqual), - b"-lt" => integers(args[0], args[2], IntegerCondition::Less), - b"-le" => integers(args[0], args[2], IntegerCondition::LessEqual), - _ => match args[0] { - b"!" => !two(&args[1..], error), - _ => { - *error = true; - false + let result = parse(args).and_then(|mut stack| eval(&mut stack)); + + match result { + Ok(result) => { + if result { + 0 + } else { + 1 } - }, - } -} - -fn four(args: &[&[u8]], error: &mut bool) -> bool { - match args[0] { - b"!" => !three(&args[1..], error), - _ => { - *error = true; - false + } + Err(e) => { + eprintln!("test: {}", e); + 2 } } } -enum IntegerCondition { - Equal, - Unequal, - Greater, - GreaterEqual, - Less, - LessEqual, -} +/// Evaluate a stack of Symbols, returning the result of the evaluation or +/// an error message if evaluation failed. +fn eval(stack: &mut Vec) -> Result { + macro_rules! pop_literal { + () => { + match stack.pop() { + Some(Symbol::Literal(s)) => s, + _ => panic!(), + } + }; + } -fn integers(a: &[u8], b: &[u8], cond: IntegerCondition) -> bool { - let (a, b): (&str, &str) = match (from_utf8(a), from_utf8(b)) { - (Ok(a), Ok(b)) => (a, b), - _ => return false, - }; - let (a, b): (i64, i64) = match (a.parse(), b.parse()) { - (Ok(a), Ok(b)) => (a, b), - _ => return false, - }; - match cond { - IntegerCondition::Equal => a == b, - IntegerCondition::Unequal => a != b, - IntegerCondition::Greater => a > b, - IntegerCondition::GreaterEqual => a >= b, - IntegerCondition::Less => a < b, - IntegerCondition::LessEqual => a <= b, + let s = stack.pop(); + + match s { + Some(Symbol::Bang) => { + let result = eval(stack)?; + + Ok(!result) + } + Some(Symbol::StringOp(op)) => { + let b = stack.pop(); + let a = stack.pop(); + Ok(if op == "!=" { a != b } else { a == b }) + } + Some(Symbol::IntOp(op)) => { + let b = pop_literal!(); + let a = pop_literal!(); + + Ok(integers(&a, &b, &op)?) + } + Some(Symbol::FileOp(_op)) => unimplemented!(), + Some(Symbol::StrlenOp(op)) => { + let s = match stack.pop() { + Some(Symbol::Literal(s)) => s, + Some(Symbol::None) => OsString::from(""), + None => { + return Ok(true); + } + _ => { + return Err(format!("missing argument after '{:?}'", op)); + } + }; + + Ok(if op == "-z" { + s.is_empty() + } else { + !s.is_empty() + }) + } + Some(Symbol::FiletestOp(op)) => { + let op = op.to_string_lossy(); + + let f = pop_literal!(); + + Ok(match op.as_ref() { + "-b" => path(&f, PathCondition::BlockSpecial), + "-c" => path(&f, PathCondition::CharacterSpecial), + "-d" => path(&f, PathCondition::Directory), + "-e" => path(&f, PathCondition::Exists), + "-f" => path(&f, PathCondition::Regular), + "-g" => path(&f, PathCondition::GroupIdFlag), + "-G" => path(&f, PathCondition::GroupOwns), + "-h" => path(&f, PathCondition::SymLink), + "-k" => path(&f, PathCondition::Sticky), + "-L" => path(&f, PathCondition::SymLink), + "-O" => path(&f, PathCondition::UserOwns), + "-p" => path(&f, PathCondition::Fifo), + "-r" => path(&f, PathCondition::Readable), + "-S" => path(&f, PathCondition::Socket), + "-s" => path(&f, PathCondition::NonEmpty), + "-t" => isatty(&f)?, + "-u" => path(&f, PathCondition::UserIdFlag), + "-w" => path(&f, PathCondition::Writable), + "-x" => path(&f, PathCondition::Executable), + _ => panic!(), + }) + } + Some(Symbol::Literal(s)) => Ok(!s.is_empty()), + Some(Symbol::None) => Ok(false), + Some(Symbol::BoolOp(op)) => { + let b = eval(stack)?; + let a = eval(stack)?; + + Ok(if op == "-a" { a && b } else { a || b }) + } + None => Ok(false), + _ => Err("expected value".to_string()), } } -fn isatty(fd: &[u8]) -> bool { - from_utf8(fd) - .ok() - .and_then(|s| s.parse().ok()) - .map_or(false, |i| { +fn integers(a: &OsStr, b: &OsStr, op: &OsStr) -> Result { + let format_err = |value| format!("invalid integer '{}'", value); + + let a = a.to_string_lossy(); + let a: i64 = a.parse().map_err(|_| format_err(a))?; + + let b = b.to_string_lossy(); + let b: i64 = b.parse().map_err(|_| format_err(b))?; + + let operator = op.to_string_lossy(); + Ok(match operator.as_ref() { + "-eq" => a == b, + "-ne" => a != b, + "-gt" => a > b, + "-ge" => a >= b, + "-lt" => a < b, + "-le" => a <= b, + _ => return Err(format!("unknown operator '{}'", operator)), + }) +} + +fn isatty(fd: &OsStr) -> Result { + let fd = fd.to_string_lossy(); + + fd.parse() + .map_err(|_| format!("invalid integer '{}'", fd)) + .map(|i| { #[cfg(not(target_os = "redox"))] unsafe { libc::isatty(i) == 1 @@ -148,173 +183,6 @@ fn isatty(fd: &[u8]) -> bool { }) } -fn dispatch(args: &mut &[&[u8]], error: &mut bool) -> bool { - let (val, idx) = match args.len() { - 0 => { - *error = true; - (false, 0) - } - 1 => (one(*args), 1), - 2 => dispatch_two(args, error), - 3 => dispatch_three(args, error), - _ => dispatch_four(args, error), - }; - *args = &(*args)[idx..]; - val -} - -fn dispatch_two(args: &mut &[&[u8]], error: &mut bool) -> (bool, usize) { - let val = two(*args, error); - if *error { - *error = false; - (one(*args), 1) - } else { - (val, 2) - } -} - -fn dispatch_three(args: &mut &[&[u8]], error: &mut bool) -> (bool, usize) { - let val = three(*args, error); - if *error { - *error = false; - dispatch_two(args, error) - } else { - (val, 3) - } -} - -fn dispatch_four(args: &mut &[&[u8]], error: &mut bool) -> (bool, usize) { - let val = four(*args, error); - if *error { - *error = false; - dispatch_three(args, error) - } else { - (val, 4) - } -} - -#[derive(Clone, Copy)] -enum Precedence { - Unknown = 0, - Paren, // FIXME: this is useless (parentheses have not been implemented) - Or, - And, - BUnOp, - BinOp, - UnOp, -} - -fn parse_expr(mut args: &[&[u8]], error: &mut bool) -> bool { - if args.is_empty() { - false - } else { - let hashmap = setup_hashmap(); - let lhs = dispatch(&mut args, error); - - if !args.is_empty() { - parse_expr_helper(&hashmap, &mut args, lhs, Precedence::Unknown, error) - } else { - lhs - } - } -} - -fn parse_expr_helper<'a>( - hashmap: &HashMap<&'a [u8], Precedence>, - args: &mut &[&'a [u8]], - mut lhs: bool, - min_prec: Precedence, - error: &mut bool, -) -> bool { - let mut prec = *hashmap.get(&args[0]).unwrap_or_else(|| { - *error = true; - &min_prec - }); - while !*error && !args.is_empty() && prec as usize >= min_prec as usize { - let op = args[0]; - *args = &(*args)[1..]; - let mut rhs = dispatch(args, error); - while !args.is_empty() { - let subprec = *hashmap.get(&args[0]).unwrap_or_else(|| { - *error = true; - &min_prec - }); - if subprec as usize <= prec as usize || *error { - break; - } - rhs = parse_expr_helper(hashmap, args, rhs, subprec, error); - } - lhs = match prec { - Precedence::UnOp | Precedence::BUnOp => { - *error = true; - false - } - Precedence::And => lhs && rhs, - Precedence::Or => lhs || rhs, - Precedence::BinOp => three( - &[ - if lhs { b" " } else { b"" }, - op, - if rhs { b" " } else { b"" }, - ], - error, - ), - Precedence::Paren => unimplemented!(), // TODO: implement parentheses - _ => unreachable!(), - }; - if !args.is_empty() { - prec = *hashmap.get(&args[0]).unwrap_or_else(|| { - *error = true; - &min_prec - }); - } - } - lhs -} - -#[inline] -fn setup_hashmap<'a>() -> HashMap<&'a [u8], Precedence> { - let mut hashmap = HashMap::<&'a [u8], Precedence>::new(); - - hashmap.insert(b"-b", Precedence::UnOp); - hashmap.insert(b"-c", Precedence::UnOp); - hashmap.insert(b"-d", Precedence::UnOp); - hashmap.insert(b"-e", Precedence::UnOp); - hashmap.insert(b"-f", Precedence::UnOp); - hashmap.insert(b"-g", Precedence::UnOp); - hashmap.insert(b"-h", Precedence::UnOp); - hashmap.insert(b"-L", Precedence::UnOp); - hashmap.insert(b"-n", Precedence::UnOp); - hashmap.insert(b"-p", Precedence::UnOp); - hashmap.insert(b"-r", Precedence::UnOp); - hashmap.insert(b"-S", Precedence::UnOp); - hashmap.insert(b"-s", Precedence::UnOp); - hashmap.insert(b"-t", Precedence::UnOp); - hashmap.insert(b"-u", Precedence::UnOp); - hashmap.insert(b"-w", Precedence::UnOp); - hashmap.insert(b"-x", Precedence::UnOp); - hashmap.insert(b"-z", Precedence::UnOp); - - hashmap.insert(b"=", Precedence::BinOp); - hashmap.insert(b"!=", Precedence::BinOp); - hashmap.insert(b"-eq", Precedence::BinOp); - hashmap.insert(b"-ne", Precedence::BinOp); - hashmap.insert(b"-gt", Precedence::BinOp); - hashmap.insert(b"-ge", Precedence::BinOp); - hashmap.insert(b"-lt", Precedence::BinOp); - hashmap.insert(b"-le", Precedence::BinOp); - - hashmap.insert(b"!", Precedence::BUnOp); - - hashmap.insert(b"-a", Precedence::And); - hashmap.insert(b"-o", Precedence::Or); - - hashmap.insert(b"(", Precedence::Paren); - hashmap.insert(b")", Precedence::Paren); - - hashmap -} - #[derive(Eq, PartialEq)] enum PathCondition { BlockSpecial, @@ -322,28 +190,28 @@ enum PathCondition { Directory, Exists, Regular, - GroupIDFlag, + GroupIdFlag, + GroupOwns, SymLink, - FIFO, + Sticky, + UserOwns, + Fifo, Readable, Socket, NonEmpty, - UserIDFlag, + UserIdFlag, Writable, Executable, } #[cfg(not(windows))] -fn path(path: &[u8], cond: PathCondition) -> bool { - use std::ffi::OsStr; +fn path(path: &OsStr, condition: PathCondition) -> bool { use std::fs::{self, Metadata}; - use std::os::unix::ffi::OsStrExt; use std::os::unix::fs::{FileTypeExt, MetadataExt}; - let path = OsStr::from_bytes(path); - const S_ISUID: u32 = 0o4000; const S_ISGID: u32 = 0o2000; + const S_ISVTX: u32 = 0o1000; enum Permission { Read = 0o4, @@ -351,25 +219,35 @@ fn path(path: &[u8], cond: PathCondition) -> bool { Execute = 0o1, } - let perm = |metadata: Metadata, p: Permission| { + let geteuid = || { #[cfg(not(target_os = "redox"))] - let (uid, gid) = unsafe { (libc::getuid(), libc::getgid()) }; + let euid = unsafe { libc::geteuid() }; #[cfg(target_os = "redox")] - let (uid, gid) = ( - syscall::getuid().unwrap() as u32, - syscall::getgid().unwrap() as u32, - ); + let euid = syscall::geteuid().unwrap() as u32; - if uid == metadata.uid() { + euid + }; + + let getegid = || { + #[cfg(not(target_os = "redox"))] + let egid = unsafe { libc::getegid() }; + #[cfg(target_os = "redox")] + let egid = syscall::getegid().unwrap() as u32; + + egid + }; + + let perm = |metadata: Metadata, p: Permission| { + if geteuid() == metadata.uid() { metadata.mode() & ((p as u32) << 6) != 0 - } else if gid == metadata.gid() { + } else if getegid() == metadata.gid() { metadata.mode() & ((p as u32) << 3) != 0 } else { metadata.mode() & (p as u32) != 0 } }; - let metadata = if cond == PathCondition::SymLink { + let metadata = if condition == PathCondition::SymLink { fs::symlink_metadata(path) } else { fs::metadata(path) @@ -384,45 +262,52 @@ fn path(path: &[u8], cond: PathCondition) -> bool { let file_type = metadata.file_type(); - match cond { + match condition { PathCondition::BlockSpecial => file_type.is_block_device(), PathCondition::CharacterSpecial => file_type.is_char_device(), PathCondition::Directory => file_type.is_dir(), PathCondition::Exists => true, PathCondition::Regular => file_type.is_file(), - PathCondition::GroupIDFlag => metadata.mode() & S_ISGID != 0, + PathCondition::GroupIdFlag => metadata.mode() & S_ISGID != 0, + PathCondition::GroupOwns => metadata.gid() == getegid(), PathCondition::SymLink => metadata.file_type().is_symlink(), - PathCondition::FIFO => file_type.is_fifo(), + PathCondition::Sticky => metadata.mode() & S_ISVTX != 0, + PathCondition::UserOwns => metadata.uid() == geteuid(), + PathCondition::Fifo => file_type.is_fifo(), PathCondition::Readable => perm(metadata, Permission::Read), PathCondition::Socket => file_type.is_socket(), PathCondition::NonEmpty => metadata.size() > 0, - PathCondition::UserIDFlag => metadata.mode() & S_ISUID != 0, + PathCondition::UserIdFlag => metadata.mode() & S_ISUID != 0, PathCondition::Writable => perm(metadata, Permission::Write), PathCondition::Executable => perm(metadata, Permission::Execute), } } #[cfg(windows)] -fn path(path: &[u8], cond: PathCondition) -> bool { +fn path(path: &OsStr, condition: PathCondition) -> bool { use std::fs::metadata; - let path = from_utf8(path).unwrap(); + let stat = match metadata(path) { Ok(s) => s, _ => return false, }; - match cond { + + match condition { PathCondition::BlockSpecial => false, PathCondition::CharacterSpecial => false, PathCondition::Directory => stat.is_dir(), PathCondition::Exists => true, PathCondition::Regular => stat.is_file(), - PathCondition::GroupIDFlag => false, + PathCondition::GroupIdFlag => false, + PathCondition::GroupOwns => unimplemented!(), PathCondition::SymLink => false, - PathCondition::FIFO => false, + PathCondition::Sticky => false, + PathCondition::UserOwns => unimplemented!(), + PathCondition::Fifo => false, PathCondition::Readable => false, // TODO PathCondition::Socket => false, PathCondition::NonEmpty => stat.len() > 0, - PathCondition::UserIDFlag => false, + PathCondition::UserIdFlag => false, PathCondition::Writable => false, // TODO PathCondition::Executable => false, // TODO } diff --git a/src/uu/timeout/Cargo.toml b/src/uu/timeout/Cargo.toml index c13a98d19..70ce64630 100644 --- a/src/uu/timeout/Cargo.toml +++ b/src/uu/timeout/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_timeout" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "timeout ~ (uutils) run COMMAND with a DURATION time limit" @@ -15,11 +15,13 @@ edition = "2018" path = "src/timeout.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["parse_time", "process", "signals"] } +nix = "0.20.0" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["process", "signals"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } + [[bin]] name = "timeout" path = "src/main.rs" diff --git a/src/uu/timeout/src/main.rs b/src/uu/timeout/src/main.rs index 20c4271d9..2479f91c1 100644 --- a/src/uu/timeout/src/main.rs +++ b/src/uu/timeout/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_timeout); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_timeout); diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index 0dd7c2016..464414c5e 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -5,118 +5,191 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore (ToDO) tstr sigstr cmdname setpgid +// spell-checker:ignore (ToDO) tstr sigstr cmdname setpgid sigchld #[macro_use] extern crate uucore; +extern crate clap; + +use clap::{crate_version, App, AppSettings, Arg}; use std::io::ErrorKind; use std::process::{Command, Stdio}; use std::time::Duration; use uucore::process::ChildExt; +use uucore::signals::{signal_by_name_or_value, signal_name_by_value}; +use uucore::InvalidEncodingHandling; -static NAME: &str = "timeout"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Start COMMAND, and kill it if still running after DURATION."; + +fn get_usage() -> String { + format!("{0} [OPTION] DURATION COMMAND...", executable!()) +} const ERR_EXIT_STATUS: i32 = 125; -pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); +pub mod options { + pub static FOREGROUND: &str = "foreground"; + pub static KILL_AFTER: &str = "kill-after"; + pub static SIGNAL: &str = "signal"; + pub static PRESERVE_STATUS: &str = "preserve-status"; + pub static VERBOSE: &str = "verbose"; - let program = args[0].clone(); - - let mut opts = getopts::Options::new(); - opts.optflag( - "", - "preserve-status", - "exit with the same status as COMMAND, even when the command times out", - ); - opts.optflag("", "foreground", "when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out"); - opts.optopt("k", "kill-after", "also send a KILL signal if COMMAND is still running this long after the initial signal was sent", "DURATION"); - opts.optflag("s", "signal", "specify the signal to be sent on timeout; SIGNAL may be a name like 'HUP' or a number; see 'kill -l' for a list of signals"); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(ERR_EXIT_STATUS, "{}", f), - }; - if matches.opt_present("help") { - print!( - "{} {} - -Usage: - {} [OPTION] DURATION COMMAND [ARG]... - -{}", - NAME, - VERSION, - program, - &opts.usage("Start COMMAND, and kill it if still running after DURATION.") - ); - } else if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - } else if matches.free.len() < 2 { - show_error!("missing an argument"); - show_error!("for help, try '{0} --help'", program); - return ERR_EXIT_STATUS; - } else { - let status = matches.opt_present("preserve-status"); - let foreground = matches.opt_present("foreground"); - let kill_after = match matches.opt_str("kill-after") { - Some(tstr) => match uucore::parse_time::from_str(&tstr) { - Ok(time) => time, - Err(f) => { - show_error!("{}", f); - return ERR_EXIT_STATUS; - } - }, - None => Duration::new(0, 0), - }; - let signal = match matches.opt_str("signal") { - Some(sigstr) => match uucore::signals::signal_by_name_or_value(&sigstr) { - Some(sig) => sig, - None => { - show_error!("invalid signal '{}'", sigstr); - return ERR_EXIT_STATUS; - } - }, - None => uucore::signals::signal_by_name_or_value("TERM").unwrap(), - }; - let duration = match uucore::parse_time::from_str(&matches.free[0]) { - Ok(time) => time, - Err(f) => { - show_error!("{}", f); - return ERR_EXIT_STATUS; - } - }; - return timeout( - &matches.free[1], - &matches.free[2..], - duration, - signal, - kill_after, - foreground, - status, - ); - } - - 0 + // Positional args. + pub static DURATION: &str = "duration"; + pub static COMMAND: &str = "command"; } +struct Config { + foreground: bool, + kill_after: Option, + signal: usize, + duration: Duration, + preserve_status: bool, + verbose: bool, + + command: Vec, +} + +impl Config { + fn from(options: clap::ArgMatches) -> Config { + let signal = match options.value_of(options::SIGNAL) { + Some(signal_) => { + let signal_result = signal_by_name_or_value(signal_); + match signal_result { + None => { + unreachable!("invalid signal '{}'", signal_); + } + Some(signal_value) => signal_value, + } + } + _ => uucore::signals::signal_by_name_or_value("TERM").unwrap(), + }; + + let kill_after = options + .value_of(options::KILL_AFTER) + .map(|time| uucore::parse_time::from_str(time).unwrap()); + + let duration: Duration = + uucore::parse_time::from_str(options.value_of(options::DURATION).unwrap()).unwrap(); + + let preserve_status: bool = options.is_present(options::PRESERVE_STATUS); + let foreground = options.is_present(options::FOREGROUND); + let verbose = options.is_present(options::VERBOSE); + + let command = options + .values_of(options::COMMAND) + .unwrap() + .map(String::from) + .collect::>(); + + Config { + foreground, + kill_after, + signal, + duration, + preserve_status, + verbose, + command, + } + } +} + +pub fn uumain(args: impl uucore::Args) -> i32 { + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); + + let usage = get_usage(); + + let app = uu_app().usage(&usage[..]); + + let matches = app.get_matches_from(args); + + let config = Config::from(matches); + timeout( + &config.command, + config.duration, + config.signal, + config.kill_after, + config.foreground, + config.preserve_status, + config.verbose, + ) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new("timeout") + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::FOREGROUND) + .long(options::FOREGROUND) + .help("when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out") + ) + .arg( + Arg::with_name(options::KILL_AFTER) + .short("k") + .takes_value(true)) + .arg( + Arg::with_name(options::PRESERVE_STATUS) + .long(options::PRESERVE_STATUS) + .help("exit with the same status as COMMAND, even when the command times out") + ) + .arg( + Arg::with_name(options::SIGNAL) + .short("s") + .long(options::SIGNAL) + .help("specify the signal to be sent on timeout; SIGNAL may be a name like 'HUP' or a number; see 'kill -l' for a list of signals") + .takes_value(true) + ) + .arg( + Arg::with_name(options::VERBOSE) + .short("v") + .long(options::VERBOSE) + .help("diagnose to stderr any signal sent upon timeout") + ) + .arg( + Arg::with_name(options::DURATION) + .index(1) + .required(true) + ) + .arg( + Arg::with_name(options::COMMAND) + .index(2) + .required(true) + .multiple(true) + ) + .setting(AppSettings::TrailingVarArg) +} + +/// Remove pre-existing SIGCHLD handlers that would make waiting for the child's exit code fail. +fn unblock_sigchld() { + unsafe { + nix::sys::signal::signal( + nix::sys::signal::Signal::SIGCHLD, + nix::sys::signal::SigHandler::SigDfl, + ) + .unwrap(); + } +} + +/// TODO: Improve exit codes, and make them consistent with the GNU Coreutils exit codes. + fn timeout( - cmdname: &str, - args: &[String], + cmd: &[String], duration: Duration, signal: usize, - kill_after: Duration, + kill_after: Option, foreground: bool, preserve_status: bool, + verbose: bool, ) -> i32 { if !foreground { unsafe { libc::setpgid(0, 0) }; } - let mut process = match Command::new(cmdname) - .args(args) + let mut process = match Command::new(&cmd[0]) + .args(&cmd[1..]) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) @@ -126,40 +199,52 @@ fn timeout( Err(err) => { show_error!("failed to execute process: {}", err); if err.kind() == ErrorKind::NotFound { - // XXX: not sure which to use + // FIXME: not sure which to use return 127; } else { - // XXX: this may not be 100% correct... + // FIXME: this may not be 100% correct... return 126; } } }; + unblock_sigchld(); match process.wait_or_timeout(duration) { Ok(Some(status)) => status.code().unwrap_or_else(|| status.signal().unwrap()), Ok(None) => { + if verbose { + show_error!( + "sending signal {} to command '{}'", + signal_name_by_value(signal).unwrap(), + cmd[0] + ); + } return_if_err!(ERR_EXIT_STATUS, process.send_signal(signal)); - match process.wait_or_timeout(kill_after) { - Ok(Some(status)) => { - if preserve_status { - status.code().unwrap_or_else(|| status.signal().unwrap()) - } else { - 124 + if let Some(kill_after) = kill_after { + match process.wait_or_timeout(kill_after) { + Ok(Some(status)) => { + if preserve_status { + status.code().unwrap_or_else(|| status.signal().unwrap()) + } else { + 124 + } } - } - Ok(None) => { - if kill_after == Duration::new(0, 0) { - // XXX: this may not be right - return 124; + Ok(None) => { + if verbose { + show_error!("sending signal KILL to command '{}'", cmd[0]); + } + return_if_err!( + ERR_EXIT_STATUS, + process.send_signal( + uucore::signals::signal_by_name_or_value("KILL").unwrap() + ) + ); + return_if_err!(ERR_EXIT_STATUS, process.wait()); + 137 } - return_if_err!( - ERR_EXIT_STATUS, - process - .send_signal(uucore::signals::signal_by_name_or_value("KILL").unwrap()) - ); - return_if_err!(ERR_EXIT_STATUS, process.wait()); - 137 + Err(_) => 124, } - Err(_) => 124, + } else { + 124 } } Err(_) => { diff --git a/src/uu/touch/Cargo.toml b/src/uu/touch/Cargo.toml index fc021c096..0608a7b7c 100644 --- a/src/uu/touch/Cargo.toml +++ b/src/uu/touch/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_touch" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "touch ~ (uutils) change FILE timestamps" @@ -18,7 +18,7 @@ path = "src/touch.rs" filetime = "0.2.1" clap = "2.33" time = "0.1.40" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["libc"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["libc"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/touch/src/main.rs b/src/uu/touch/src/main.rs index bad67efd4..e8a2729a2 100644 --- a/src/uu/touch/src/main.rs +++ b/src/uu/touch/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_touch); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_touch); diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 39405900e..dd2b05d0e 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -13,13 +13,12 @@ pub extern crate filetime; #[macro_use] extern crate uucore; -use clap::{App, Arg, ArgGroup}; +use clap::{crate_version, App, Arg, ArgGroup}; use filetime::*; use std::fs::{self, File}; -use std::io::Error; use std::path::Path; +use uucore::error::{FromIo, UResult, USimpleError}; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Update the access and modification times of each FILE to the current time."; pub mod options { // Both SOURCES and sources are needed as we need to be able to refer to the ArgGroup. @@ -52,13 +51,87 @@ fn get_usage() -> String { format!("{0} [OPTION]... [USER]", executable!()) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let files = matches.values_of_os(ARG_FILES).unwrap(); + + let (mut atime, mut mtime) = + if let Some(reference) = matches.value_of_os(options::sources::REFERENCE) { + stat(Path::new(reference), !matches.is_present(options::NO_DEREF))? + } else { + let timestamp = if let Some(date) = matches.value_of(options::sources::DATE) { + parse_date(date)? + } else if let Some(current) = matches.value_of(options::sources::CURRENT) { + parse_timestamp(current)? + } else { + local_tm_to_filetime(time::now()) + }; + (timestamp, timestamp) + }; + + for filename in files { + let path = Path::new(filename); + if !path.exists() { + // no-dereference included here for compatibility + if matches.is_present(options::NO_CREATE) || matches.is_present(options::NO_DEREF) { + continue; + } + + if let Err(e) = File::create(path) { + show!(e.map_err_context(|| format!("cannot touch '{}'", path.display()))); + continue; + }; + + // Minor optimization: if no reference time was specified, we're done. + if !matches.is_present(options::SOURCES) { + continue; + } + } + + // If changing "only" atime or mtime, grab the existing value of the other. + // Note that "-a" and "-m" may be passed together; this is not an xor. + if matches.is_present(options::ACCESS) + || matches.is_present(options::MODIFICATION) + || matches.is_present(options::TIME) + { + let st = stat(path, !matches.is_present(options::NO_DEREF))?; + let time = matches.value_of(options::TIME).unwrap_or(""); + + if !(matches.is_present(options::ACCESS) + || time.contains(&"access".to_owned()) + || time.contains(&"atime".to_owned()) + || time.contains(&"use".to_owned())) + { + atime = st.0; + } + + if !(matches.is_present(options::MODIFICATION) + || time.contains(&"modify".to_owned()) + || time.contains(&"mtime".to_owned())) + { + mtime = st.1; + } + } + + if matches.is_present(options::NO_DEREF) { + set_symlink_file_times(path, atime, mtime) + } else { + filetime::set_file_times(path, atime, mtime) + } + .map_err_context(|| format!("setting times of '{}'", path.display()))?; + } + + Ok(()) +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(options::ACCESS) .short("a") @@ -128,127 +201,46 @@ pub fn uumain(args: impl uucore::Args) -> i32 { options::sources::DATE, options::sources::REFERENCE, ])) - .get_matches_from(args); - - let files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let (mut atime, mut mtime) = if matches.is_present(options::sources::REFERENCE) { - stat( - &matches.value_of(options::sources::REFERENCE).unwrap()[..], - !matches.is_present(options::NO_DEREF), - ) - } else if matches.is_present(options::sources::DATE) - || matches.is_present(options::sources::CURRENT) - { - let timestamp = if matches.is_present(options::sources::DATE) { - parse_date(matches.value_of(options::sources::DATE).unwrap().as_ref()) - } else { - parse_timestamp( - matches - .value_of(options::sources::CURRENT) - .unwrap() - .as_ref(), - ) - }; - (timestamp, timestamp) - } else { - let now = local_tm_to_filetime(time::now()); - (now, now) - }; - - for filename in &files { - let path = &filename[..]; - - if !Path::new(path).exists() { - // no-dereference included here for compatibility - if matches.is_present(options::NO_CREATE) || matches.is_present(options::NO_DEREF) { - continue; - } - - if let Err(e) = File::create(path) { - show_warning!("cannot touch '{}': {}", path, e); - continue; - }; - - // Minor optimization: if no reference time was specified, we're done. - if !matches.is_present(options::SOURCES) { - continue; - } - } - - // If changing "only" atime or mtime, grab the existing value of the other. - // Note that "-a" and "-m" may be passed together; this is not an xor. - if matches.is_present(options::ACCESS) - || matches.is_present(options::MODIFICATION) - || matches.is_present(options::TIME) - { - let st = stat(path, !matches.is_present(options::NO_DEREF)); - let time = matches.value_of(options::TIME).unwrap_or(""); - - if !(matches.is_present(options::ACCESS) - || time.contains(&"access".to_owned()) - || time.contains(&"atime".to_owned()) - || time.contains(&"use".to_owned())) - { - atime = st.0; - } - - if !(matches.is_present(options::MODIFICATION) - || time.contains(&"modify".to_owned()) - || time.contains(&"mtime".to_owned())) - { - mtime = st.1; - } - } - - if matches.is_present(options::NO_DEREF) { - if let Err(e) = set_symlink_file_times(path, atime, mtime) { - show_warning!("cannot touch '{}': {}", path, e); - } - } else if let Err(e) = filetime::set_file_times(path, atime, mtime) { - show_warning!("cannot touch '{}': {}", path, e); - } - } - - 0 } -fn stat(path: &str, follow: bool) -> (FileTime, FileTime) { +fn stat(path: &Path, follow: bool) -> UResult<(FileTime, FileTime)> { let metadata = if follow { fs::symlink_metadata(path) } else { fs::metadata(path) - }; - - match metadata { - Ok(m) => ( - FileTime::from_last_access_time(&m), - FileTime::from_last_modification_time(&m), - ), - Err(_) => crash!( - 1, - "failed to get attributes of '{}': {}", - path, - Error::last_os_error() - ), } + .map_err_context(|| format!("failed to get attributes of '{}'", path.display()))?; + + Ok(( + FileTime::from_last_access_time(&metadata), + FileTime::from_last_modification_time(&metadata), + )) } -fn parse_date(str: &str) -> FileTime { +fn parse_date(str: &str) -> UResult { // This isn't actually compatible with GNU touch, but there doesn't seem to // be any simple specification for what format this parameter allows and I'm // not about to implement GNU parse_datetime. // http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y - match time::strptime(str, "%c") { - Ok(tm) => local_tm_to_filetime(to_local(tm)), - Err(e) => panic!("Unable to parse date\n{}", e), + let formats = vec!["%c", "%F"]; + for f in formats { + if let Ok(tm) = time::strptime(str, f) { + return Ok(local_tm_to_filetime(to_local(tm))); + } } + + if let Ok(tm) = time::strptime(str, "@%s") { + // Don't convert to local time in this case - seconds since epoch are not time-zone dependent + return Ok(local_tm_to_filetime(tm)); + } + + Err(USimpleError::new( + 1, + format!("Unable to parse date: {}", str), + )) } -fn parse_timestamp(s: &str) -> FileTime { +fn parse_timestamp(s: &str) -> UResult { let now = time::now(); let (format, ts) = match s.chars().count() { 15 => ("%Y%m%d%H%M.%S", s.to_owned()), @@ -257,11 +249,28 @@ fn parse_timestamp(s: &str) -> FileTime { 10 => ("%y%m%d%H%M", s.to_owned()), 11 => ("%Y%m%d%H%M.%S", format!("{}{}", now.tm_year + 1900, s)), 8 => ("%Y%m%d%H%M", format!("{}{}", now.tm_year + 1900, s)), - _ => panic!("Unknown timestamp format"), + _ => return Err(USimpleError::new(1, format!("invalid date format '{}'", s))), }; - match time::strptime(&ts, format) { - Ok(tm) => local_tm_to_filetime(to_local(tm)), - Err(e) => panic!("Unable to parse timestamp\n{}", e), + let tm = time::strptime(&ts, format) + .map_err(|_| USimpleError::new(1, format!("invalid date format '{}'", s)))?; + + let mut local = to_local(tm); + local.tm_isdst = -1; + let ft = local_tm_to_filetime(local); + + // We have to check that ft is valid time. Due to daylight saving + // time switch, local time can jump from 1:59 AM to 3:00 AM, + // in which case any time between 2:00 AM and 2:59 AM is not valid. + // Convert back to local time and see if we got the same value back. + let ts = time::Timespec { + sec: ft.unix_seconds(), + nsec: 0, + }; + let tm2 = time::at(ts); + if tm.tm_hour != tm2.tm_hour { + return Err(USimpleError::new(1, format!("invalid date format '{}'", s))); } + + Ok(ft) } diff --git a/src/uu/tr/Cargo.toml b/src/uu/tr/Cargo.toml index 918698e24..a3d066bfb 100644 --- a/src/uu/tr/Cargo.toml +++ b/src/uu/tr/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_tr" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "tr ~ (uutils) translate characters within input and display" @@ -17,8 +17,8 @@ path = "src/tr.rs" [dependencies] bit-set = "0.5.0" fnv = "1.0.5" -getopts = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/tr/src/expand.rs b/src/uu/tr/src/expand.rs index 3291d57ae..5d960921e 100644 --- a/src/uu/tr/src/expand.rs +++ b/src/uu/tr/src/expand.rs @@ -14,17 +14,47 @@ use std::cmp::min; use std::iter::Peekable; use std::ops::RangeInclusive; +/// Parse a backslash escape sequence to the corresponding character. Assumes +/// the string starts from the character _after_ the `\` and is not empty. +/// +/// Returns a tuple containing the character and the number of characters +/// consumed from the input. The alphabetic escape sequences consume 1 +/// character; octal escape sequences consume 1 to 3 octal digits. #[inline] -fn unescape_char(c: char) -> char { - match c { - 'a' => 0x07u8 as char, - 'b' => 0x08u8 as char, - 'f' => 0x0cu8 as char, - 'v' => 0x0bu8 as char, - 'n' => '\n', - 'r' => '\r', - 't' => '\t', - _ => c, +fn parse_sequence(s: &str) -> (char, usize) { + let mut s = s.chars(); + let c = s.next().expect("invalid escape: empty string"); + + if ('0'..='7').contains(&c) { + let mut v = c.to_digit(8).unwrap(); + let mut consumed = 1; + let bits_per_digit = 3; + + for c in s.take(2) { + match c.to_digit(8) { + Some(c) => { + v = (v << bits_per_digit) | c; + consumed += 1; + } + None => break, + } + } + + (from_u32(v).expect("invalid octal escape"), consumed) + } else { + ( + match c { + 'a' => 0x07u8 as char, + 'b' => 0x08u8 as char, + 'f' => 0x0cu8 as char, + 'v' => 0x0bu8 as char, + 'n' => '\n', + 'r' => '\r', + 't' => '\t', + c => c, + }, + 1, + ) } } @@ -52,8 +82,9 @@ impl<'a> Iterator for Unescape<'a> { '\\' if self.string.len() > 1 => { // yes---it's \ and it's not the last char in a string // we know that \ is 1 byte long so we can index into the string safely - let c = self.string[1..].chars().next().unwrap(); - (Some(unescape_char(c)), 1 + c.len_utf8()) + let (c, consumed) = parse_sequence(&self.string[1..]); + + (Some(c), 1 + consumed) } c => (Some(c), c.len_utf8()), // not an escape char }; @@ -80,7 +111,7 @@ impl<'a> Iterator for ExpandSet<'a> { fn next(&mut self) -> Option { // while the Range has elements, try to return chars from it // but make sure that they actually turn out to be Chars! - while let Some(n) = self.range.next() { + for n in &mut self.range { if let Some(c) = from_u32(n) { return Some(c); } diff --git a/src/uu/tr/src/main.rs b/src/uu/tr/src/main.rs index 8bed990e5..9118c3628 100644 --- a/src/uu/tr/src/main.rs +++ b/src/uu/tr/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_tr); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_tr); diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index d60d2559a..28ce70c22 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -16,16 +16,25 @@ extern crate uucore; mod expand; use bit_set::BitSet; +use clap::{crate_version, App, Arg}; use fnv::FnvHashMap; -use getopts::Options; use std::io::{stdin, stdout, BufRead, BufWriter, Write}; use crate::expand::ExpandSet; +use uucore::InvalidEncodingHandling; + +static ABOUT: &str = "translate or delete characters"; -static NAME: &str = "tr"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); const BUFFER_LEN: usize = 1024; +mod options { + pub const COMPLEMENT: &str = "complement"; + pub const DELETE: &str = "delete"; + pub const SQUEEZE: &str = "squeeze-repeats"; + pub const TRUNCATE: &str = "truncate"; + pub const SETS: &str = "sets"; +} + trait SymbolTranslator { fn translate(&self, c: char, prev_c: char) -> Option; } @@ -113,10 +122,17 @@ impl SymbolTranslator for DeleteAndSqueezeOperation { struct TranslateOperation { translate_map: FnvHashMap, + complement: bool, + s2_last: char, } impl TranslateOperation { - fn new(set1: ExpandSet, set2: &mut ExpandSet, truncate: bool) -> TranslateOperation { + fn new( + set1: ExpandSet, + set2: &mut ExpandSet, + truncate: bool, + complement: bool, + ) -> TranslateOperation { let mut map = FnvHashMap::default(); let mut s2_prev = '_'; for i in set1 { @@ -129,13 +145,54 @@ impl TranslateOperation { map.insert(i as usize, s2_prev); } } - TranslateOperation { translate_map: map } + TranslateOperation { + translate_map: map, + complement, + s2_last: set2.last().unwrap_or(s2_prev), + } } } impl SymbolTranslator for TranslateOperation { fn translate(&self, c: char, _prev_c: char) -> Option { - Some(*self.translate_map.get(&(c as usize)).unwrap_or(&c)) + if self.complement { + Some(if self.translate_map.contains_key(&(c as usize)) { + c + } else { + self.s2_last + }) + } else { + Some(*self.translate_map.get(&(c as usize)).unwrap_or(&c)) + } + } +} + +struct TranslateAndSqueezeOperation { + translate: TranslateOperation, + squeeze: SqueezeOperation, +} + +impl TranslateAndSqueezeOperation { + fn new( + set1: ExpandSet, + set2: &mut ExpandSet, + set2_: ExpandSet, + truncate: bool, + complement: bool, + ) -> TranslateAndSqueezeOperation { + TranslateAndSqueezeOperation { + translate: TranslateOperation::new(set1, set2, truncate, complement), + squeeze: SqueezeOperation::new(set2_, complement), + } + } +} + +impl SymbolTranslator for TranslateAndSqueezeOperation { + fn translate(&self, c: char, prev_c: char) -> Option { + // `unwrap()` will never panic because `Translate.translate()` + // always returns `Some`. + self.squeeze + .translate(self.translate.translate(c, 0 as char).unwrap(), prev_c) } } @@ -156,8 +213,11 @@ fn translate_input( // isolation to make borrow checker happy let filtered = buf.chars().filter_map(|c| { let res = translator.translate(c, prev_c); - if res.is_some() { - prev_c = c; + // Set `prev_c` to the post-translate character. This + // allows the squeeze operation to correctly function + // after the translate operation. + if let Some(rc) = res { + prev_c = rc; } res }); @@ -170,78 +230,57 @@ fn translate_input( } } -fn usage(opts: &Options) { - println!("{} {}", NAME, VERSION); - println!(); - println!("Usage:"); - println!(" {} [OPTIONS] SET1 [SET2]", NAME); - println!(); - println!("{}", opts.usage("Translate or delete characters.")); +fn get_usage() -> String { + format!("{} [OPTION]... SET1 [SET2]", executable!()) +} + +fn get_long_usage() -> String { + String::from( + "Translate, squeeze, and/or delete characters from standard input, +writing to standard output.", + ) } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = Options::new(); + let usage = get_usage(); + let after_help = get_long_usage(); - opts.optflag("c", "complement", "use the complement of SET1"); - opts.optflag("C", "", "same as -c"); - opts.optflag("d", "delete", "delete characters in SET1"); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("s", "squeeze", "replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character"); - opts.optflag( - "t", - "truncate-set1", - "first truncate SET1 to length of SET2", - ); - opts.optflag("V", "version", "output version information and exit"); + let matches = uu_app() + .usage(&usage[..]) + .after_help(&after_help[..]) + .get_matches_from(args); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(err) => { - show_error!("{}", err); - return 1; - } + let delete_flag = matches.is_present(options::DELETE); + let complement_flag = matches.is_present(options::COMPLEMENT) || matches.is_present("C"); + let squeeze_flag = matches.is_present(options::SQUEEZE); + let truncate_flag = matches.is_present(options::TRUNCATE); + + let sets: Vec = match matches.values_of(options::SETS) { + Some(v) => v.map(|v| v.to_string()).collect(), + None => vec![], }; - if matches.opt_present("help") { - usage(&opts); - return 0; - } - - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; - } - - let dflag = matches.opt_present("d"); - let cflag = matches.opts_present(&["c".to_owned(), "C".to_owned()]); - let sflag = matches.opt_present("s"); - let tflag = matches.opt_present("t"); - let sets = matches.free; - if sets.is_empty() { show_error!( "missing operand\nTry `{} --help` for more information.", - NAME + executable!() ); return 1; } - if !(dflag || sflag) && sets.len() < 2 { + if !(delete_flag || squeeze_flag) && sets.len() < 2 { show_error!( - "missing operand after ‘{}’\nTry `{} --help` for more information.", + "missing operand after '{}'\nTry `{} --help` for more information.", sets[0], - NAME + executable!() ); return 1; } - if cflag && !dflag && !sflag { - show_error!("-c is only supported with -d or -s"); - return 1; - } - let stdin = stdin(); let mut locked_stdin = stdin.lock(); let stdout = stdout(); @@ -249,23 +288,77 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let mut buffered_stdout = BufWriter::new(locked_stdout); let set1 = ExpandSet::new(sets[0].as_ref()); - if dflag { - if sflag { + if delete_flag { + if squeeze_flag { let set2 = ExpandSet::new(sets[1].as_ref()); - let op = DeleteAndSqueezeOperation::new(set1, set2, cflag); + let op = DeleteAndSqueezeOperation::new(set1, set2, complement_flag); translate_input(&mut locked_stdin, &mut buffered_stdout, op); } else { - let op = DeleteOperation::new(set1, cflag); + let op = DeleteOperation::new(set1, complement_flag); + translate_input(&mut locked_stdin, &mut buffered_stdout, op); + } + } else if squeeze_flag { + if sets.len() < 2 { + let op = SqueezeOperation::new(set1, complement_flag); + translate_input(&mut locked_stdin, &mut buffered_stdout, op); + } else { + let mut set2 = ExpandSet::new(sets[1].as_ref()); + let set2_ = ExpandSet::new(sets[1].as_ref()); + let op = TranslateAndSqueezeOperation::new( + set1, + &mut set2, + set2_, + complement_flag, + truncate_flag, + ); translate_input(&mut locked_stdin, &mut buffered_stdout, op); } - } else if sflag { - let op = SqueezeOperation::new(set1, cflag); - translate_input(&mut locked_stdin, &mut buffered_stdout, op); } else { let mut set2 = ExpandSet::new(sets[1].as_ref()); - let op = TranslateOperation::new(set1, &mut set2, tflag); - translate_input(&mut locked_stdin, &mut buffered_stdout, op) + let op = TranslateOperation::new(set1, &mut set2, truncate_flag, complement_flag); + translate_input(&mut locked_stdin, &mut buffered_stdout, op); } 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::COMPLEMENT) + // .visible_short_alias('C') // TODO: requires clap "3.0.0-beta.2" + .short("c") + .long(options::COMPLEMENT) + .help("use the complement of SET1"), + ) + .arg( + Arg::with_name("C") // work around for `Arg::visible_short_alias` + .short("C") + .help("same as -c"), + ) + .arg( + Arg::with_name(options::DELETE) + .short("d") + .long(options::DELETE) + .help("delete characters in SET1, do not translate"), + ) + .arg( + Arg::with_name(options::SQUEEZE) + .long(options::SQUEEZE) + .short("s") + .help( + "replace each sequence of a repeated character that is + listed in the last specified SET, with a single occurrence + of that character", + ), + ) + .arg( + Arg::with_name(options::TRUNCATE) + .long(options::TRUNCATE) + .short("t") + .help("first truncate SET1 to length of SET2"), + ) + .arg(Arg::with_name(options::SETS).multiple(true)) +} diff --git a/src/uu/true/Cargo.toml b/src/uu/true/Cargo.toml index a73bfddd5..f121d56de 100644 --- a/src/uu/true/Cargo.toml +++ b/src/uu/true/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_true" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "true ~ (uutils) do nothing and succeed" @@ -15,7 +15,8 @@ edition = "2018" path = "src/true.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap = "2.33.3" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/true/src/main.rs b/src/uu/true/src/main.rs index 7e009be8a..b30f4d4cb 100644 --- a/src/uu/true/src/main.rs +++ b/src/uu/true/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_true); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_true); diff --git a/src/uu/true/src/true.rs b/src/uu/true/src/true.rs index 7cb23f621..ea53b0075 100644 --- a/src/uu/true/src/true.rs +++ b/src/uu/true/src/true.rs @@ -5,6 +5,14 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -pub fn uumain(_: impl uucore::Args) -> i32 { +use clap::App; +use uucore::executable; + +pub fn uumain(args: impl uucore::Args) -> i32 { + uu_app().get_matches_from(args); 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) +} diff --git a/src/uu/truncate/Cargo.toml b/src/uu/truncate/Cargo.toml index 4f770e666..e2c0afadc 100644 --- a/src/uu/truncate/Cargo.toml +++ b/src/uu/truncate/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_truncate" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "truncate ~ (uutils) truncate (or extend) FILE to SIZE" @@ -16,7 +16,7 @@ path = "src/truncate.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/truncate/src/main.rs b/src/uu/truncate/src/main.rs index 91fe70b6d..46e65faea 100644 --- a/src/uu/truncate/src/main.rs +++ b/src/uu/truncate/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_truncate); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_truncate); diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index 9cd5865b7..bb7aa61d4 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -10,33 +10,59 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; -use std::fs::{metadata, File, OpenOptions}; +use clap::{crate_version, App, Arg}; +use std::convert::TryFrom; +use std::fs::{metadata, OpenOptions}; +use std::io::ErrorKind; use std::path::Path; +use uucore::parse_size::{parse_size, ParseSizeError}; -#[derive(Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq)] enum TruncateMode { - Reference, - Extend, - Reduce, - AtMost, - AtLeast, - RoundDown, - RoundUp, + Absolute(usize), + Extend(usize), + Reduce(usize), + AtMost(usize), + AtLeast(usize), + RoundDown(usize), + RoundUp(usize), +} + +impl TruncateMode { + /// Compute a target size in bytes for this truncate mode. + /// + /// `fsize` is the size of the reference file, in bytes. + /// + /// # Examples + /// + /// ```rust,ignore + /// let mode = TruncateMode::Extend(5); + /// let fsize = 10; + /// assert_eq!(mode.to_size(fsize), 15); + /// ``` + fn to_size(&self, fsize: usize) -> usize { + match self { + TruncateMode::Absolute(size) => *size, + TruncateMode::Extend(size) => fsize + size, + TruncateMode::Reduce(size) => fsize - size, + TruncateMode::AtMost(size) => fsize.min(*size), + TruncateMode::AtLeast(size) => fsize.max(*size), + TruncateMode::RoundDown(size) => fsize - fsize % size, + TruncateMode::RoundUp(size) => fsize + fsize % size, + } + } } static ABOUT: &str = "Shrink or extend the size of each file to the specified size."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod options { pub static IO_BLOCKS: &str = "io-blocks"; pub static NO_CREATE: &str = "no-create"; pub static REFERENCE: &str = "reference"; pub static SIZE: &str = "size"; + pub static ARG_FILES: &str = "files"; } -static ARG_FILES: &str = "files"; - fn get_usage() -> String { format!("{0} [OPTION]... [FILE]...", executable!()) } @@ -67,11 +93,49 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let long_usage = get_long_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() .usage(&usage[..]) .after_help(&long_usage[..]) + .get_matches_from(args); + + let files: Vec = matches + .values_of(options::ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + if files.is_empty() { + show_error!("Missing an argument"); + return 1; + } else { + let io_blocks = matches.is_present(options::IO_BLOCKS); + let no_create = matches.is_present(options::NO_CREATE); + let reference = matches.value_of(options::REFERENCE).map(String::from); + let size = matches.value_of(options::SIZE).map(String::from); + if let Err(e) = truncate(no_create, io_blocks, reference, size, files) { + match e.kind() { + ErrorKind::NotFound => { + // TODO Improve error-handling so that the error + // returned by `truncate()` provides the necessary + // parameter for formatting the error message. + let reference = matches.value_of(options::REFERENCE).map(String::from); + crash!( + 1, + "cannot stat '{}': No such file or directory", + reference.unwrap_or_else(|| "".to_string()) + ); // TODO: fix '--no-create' see test_reference and test_truncate_bytes_size + } + _ => crash!(1, "{}", e.to_string()), + } + } + } + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) .arg( Arg::with_name(options::IO_BLOCKS) .short("o") @@ -88,40 +152,135 @@ pub fn uumain(args: impl uucore::Args) -> i32 { Arg::with_name(options::REFERENCE) .short("r") .long(options::REFERENCE) + .required_unless(options::SIZE) .help("base the size of each file on the size of RFILE") .value_name("RFILE") ) .arg( Arg::with_name(options::SIZE) .short("s") - .long("size") + .long(options::SIZE) + .required_unless(options::REFERENCE) .help("set or adjust the size of each file according to SIZE, which is in bytes unless --io-blocks is specified") .value_name("SIZE") ) - .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true).min_values(1)) - .get_matches_from(args); + .arg(Arg::with_name(options::ARG_FILES) + .value_name("FILE") + .multiple(true) + .takes_value(true) + .required(true) + .min_values(1)) +} - let files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); +/// Truncate the named file to the specified size. +/// +/// If `create` is true, then the file will be created if it does not +/// already exist. If `size` is larger than the number of bytes in the +/// file, then the file will be padded with zeros. If `size` is smaller +/// than the number of bytes in the file, then the file will be +/// truncated and any bytes beyond `size` will be lost. +/// +/// # Errors +/// +/// If the file could not be opened, or there was a problem setting the +/// size of the file. +fn file_truncate(filename: &str, create: bool, size: usize) -> std::io::Result<()> { + let path = Path::new(filename); + let f = OpenOptions::new().write(true).create(create).open(path)?; + f.set_len(u64::try_from(size).unwrap()) +} - if files.is_empty() { - show_error!("Missing an argument"); - return 1; - } else { - let io_blocks = matches.is_present(options::IO_BLOCKS); - let no_create = matches.is_present(options::NO_CREATE); - let reference = matches.value_of(options::REFERENCE).map(String::from); - let size = matches.value_of(options::SIZE).map(String::from); - if reference.is_none() && size.is_none() { - crash!(1, "you must specify either --reference or --size"); - } else { - truncate(no_create, io_blocks, reference, size, files); - } +/// Truncate files to a size relative to a given file. +/// +/// `rfilename` is the name of the reference file. +/// +/// `size_string` gives the size relative to the reference file to which +/// to set the target files. For example, "+3K" means "set each file to +/// be three kilobytes larger than the size of the reference file". +/// +/// If `create` is true, then each file will be created if it does not +/// already exist. +/// +/// # Errors +/// +/// If the any file could not be opened, or there was a problem setting +/// the size of at least one file. +fn truncate_reference_and_size( + rfilename: &str, + size_string: &str, + filenames: Vec, + create: bool, +) -> std::io::Result<()> { + let mode = match parse_mode_and_size(size_string) { + Ok(m) => match m { + TruncateMode::Absolute(_) => { + crash!(1, "you must specify a relative '--size' with '--reference'") + } + _ => m, + }, + Err(e) => crash!(1, "Invalid number: {}", e.to_string()), + }; + let fsize = usize::try_from(metadata(rfilename)?.len()).unwrap(); + let tsize = mode.to_size(fsize); + for filename in &filenames { + file_truncate(filename, create, tsize)?; } + Ok(()) +} - 0 +/// Truncate files to match the size of a given reference file. +/// +/// `rfilename` is the name of the reference file. +/// +/// If `create` is true, then each file will be created if it does not +/// already exist. +/// +/// # Errors +/// +/// If the any file could not be opened, or there was a problem setting +/// the size of at least one file. +fn truncate_reference_file_only( + rfilename: &str, + filenames: Vec, + create: bool, +) -> std::io::Result<()> { + let tsize = usize::try_from(metadata(rfilename)?.len()).unwrap(); + for filename in &filenames { + file_truncate(filename, create, tsize)?; + } + Ok(()) +} + +/// Truncate files to a specified size. +/// +/// `size_string` gives either an absolute size or a relative size. A +/// relative size adjusts the size of each file relative to its current +/// size. For example, "3K" means "set each file to be three kilobytes" +/// whereas "+3K" means "set each file to be three kilobytes larger than +/// its current size". +/// +/// If `create` is true, then each file will be created if it does not +/// already exist. +/// +/// # Errors +/// +/// If the any file could not be opened, or there was a problem setting +/// the size of at least one file. +fn truncate_size_only( + size_string: &str, + filenames: Vec, + create: bool, +) -> std::io::Result<()> { + let mode = match parse_mode_and_size(size_string) { + Ok(m) => m, + Err(e) => crash!(1, "Invalid number: {}", e.to_string()), + }; + for filename in &filenames { + let fsize = usize::try_from(metadata(filename)?.len()).unwrap(); + let tsize = mode.to_size(fsize); + file_truncate(filename, create, tsize)?; + } + Ok(()) } fn truncate( @@ -130,124 +289,83 @@ fn truncate( reference: Option, size: Option, filenames: Vec, -) { - let (refsize, mode) = match reference { - Some(rfilename) => { - let _ = match File::open(Path::new(&rfilename)) { - Ok(m) => m, - Err(f) => crash!(1, "{}", f.to_string()), - }; - match metadata(rfilename) { - Ok(meta) => (meta.len(), TruncateMode::Reference), - Err(f) => crash!(1, "{}", f.to_string()), - } - } - None => parse_size(size.unwrap().as_ref()), - }; - for filename in &filenames { - let path = Path::new(filename); - match OpenOptions::new() - .read(true) - .write(true) - .create(!no_create) - .open(path) - { - Ok(file) => { - let fsize = match metadata(filename) { - Ok(meta) => meta.len(), - Err(f) => { - show_warning!("{}", f.to_string()); - continue; - } - }; - let tsize: u64 = match mode { - TruncateMode::Reference => refsize, - TruncateMode::Extend => fsize + refsize, - TruncateMode::Reduce => fsize - refsize, - TruncateMode::AtMost => { - if fsize > refsize { - refsize - } else { - fsize - } - } - TruncateMode::AtLeast => { - if fsize < refsize { - refsize - } else { - fsize - } - } - TruncateMode::RoundDown => fsize - fsize % refsize, - TruncateMode::RoundUp => fsize + fsize % refsize, - }; - match file.set_len(tsize) { - Ok(_) => {} - Err(f) => crash!(1, "{}", f.to_string()), - }; - } - Err(f) => crash!(1, "{}", f.to_string()), +) -> std::io::Result<()> { + let create = !no_create; + // There are four possibilities + // - reference file given and size given, + // - reference file given but no size given, + // - no reference file given but size given, + // - no reference file given and no size given, + match (reference, size) { + (Some(rfilename), Some(size_string)) => { + truncate_reference_and_size(&rfilename, &size_string, filenames, create) } + (Some(rfilename), None) => truncate_reference_file_only(&rfilename, filenames, create), + (None, Some(size_string)) => truncate_size_only(&size_string, filenames, create), + (None, None) => crash!(1, "you must specify either --reference or --size"), // this case cannot happen anymore because it's handled by clap } } -fn parse_size(size: &str) -> (u64, TruncateMode) { - let mode = match size.chars().next().unwrap() { - '+' => TruncateMode::Extend, - '-' => TruncateMode::Reduce, - '<' => TruncateMode::AtMost, - '>' => TruncateMode::AtLeast, - '/' => TruncateMode::RoundDown, - '*' => TruncateMode::RoundUp, - _ => TruncateMode::Reference, /* assume that the size is just a number */ - }; - let bytes = { - let mut slice = if mode == TruncateMode::Reference { - size - } else { - &size[1..] - }; - if slice.chars().last().unwrap().is_alphabetic() { - slice = &slice[..slice.len() - 1]; - if !slice.is_empty() && slice.chars().last().unwrap().is_alphabetic() { - slice = &slice[..slice.len() - 1]; - } - } - slice - } - .to_owned(); - let mut number: u64 = match bytes.parse() { - Ok(num) => num, - Err(e) => crash!(1, "'{}' is not a valid number: {}", size, e), - }; - if size.chars().last().unwrap().is_alphabetic() { - number *= match size.chars().last().unwrap().to_ascii_uppercase() { - 'B' => match size - .chars() - .nth(size.len() - 2) - .unwrap() - .to_ascii_uppercase() - { - 'K' => 1000u64, - 'M' => 1000u64.pow(2), - 'G' => 1000u64.pow(3), - 'T' => 1000u64.pow(4), - 'P' => 1000u64.pow(5), - 'E' => 1000u64.pow(6), - 'Z' => 1000u64.pow(7), - 'Y' => 1000u64.pow(8), - letter => crash!(1, "'{}B' is not a valid suffix.", letter), - }, - 'K' => 1024u64, - 'M' => 1024u64.pow(2), - 'G' => 1024u64.pow(3), - 'T' => 1024u64.pow(4), - 'P' => 1024u64.pow(5), - 'E' => 1024u64.pow(6), - 'Z' => 1024u64.pow(7), - 'Y' => 1024u64.pow(8), - letter => crash!(1, "'{}' is not a valid suffix.", letter), - }; - } - (number, mode) +/// Decide whether a character is one of the size modifiers, like '+' or '<'. +fn is_modifier(c: char) -> bool { + c == '+' || c == '-' || c == '<' || c == '>' || c == '/' || c == '%' +} + +/// Parse a size string with optional modifier symbol as its first character. +/// +/// A size string is as described in [`parse_size`]. The first character +/// of `size_string` might be a modifier symbol, like `'+'` or +/// `'<'`. The first element of the pair returned by this function +/// indicates which modifier symbol was present, or +/// [`TruncateMode::Absolute`] if none. +/// +/// # Panics +/// +/// If `size_string` is empty, or if no number could be parsed from the +/// given string (for example, if the string were `"abc"`). +/// +/// # Examples +/// +/// ```rust,ignore +/// assert_eq!(parse_mode_and_size("+123"), (TruncateMode::Extend, 123)); +/// ``` +fn parse_mode_and_size(size_string: &str) -> Result { + // Trim any whitespace. + let mut size_string = size_string.trim(); + + // Get the modifier character from the size string, if any. For + // example, if the argument is "+123", then the modifier is '+'. + if let Some(c) = size_string.chars().next() { + if is_modifier(c) { + size_string = &size_string[1..]; + } + parse_size(size_string).map(match c { + '+' => TruncateMode::Extend, + '-' => TruncateMode::Reduce, + '<' => TruncateMode::AtMost, + '>' => TruncateMode::AtLeast, + '/' => TruncateMode::RoundDown, + '%' => TruncateMode::RoundUp, + _ => TruncateMode::Absolute, + }) + } else { + Err(ParseSizeError::ParseFailure(size_string.to_string())) + } +} + +#[cfg(test)] +mod tests { + use crate::parse_mode_and_size; + use crate::TruncateMode; + + #[test] + fn test_parse_mode_and_size() { + assert_eq!(parse_mode_and_size("10"), Ok(TruncateMode::Absolute(10))); + assert_eq!(parse_mode_and_size("+10"), Ok(TruncateMode::Extend(10))); + assert_eq!(parse_mode_and_size("-10"), Ok(TruncateMode::Reduce(10))); + assert_eq!(parse_mode_and_size("<10"), Ok(TruncateMode::AtMost(10))); + assert_eq!(parse_mode_and_size(">10"), Ok(TruncateMode::AtLeast(10))); + assert_eq!(parse_mode_and_size("/10"), Ok(TruncateMode::RoundDown(10))); + assert_eq!(parse_mode_and_size("%10"), Ok(TruncateMode::RoundUp(10))); + } } diff --git a/src/uu/tsort/Cargo.toml b/src/uu/tsort/Cargo.toml index 10672a9e0..37f543012 100644 --- a/src/uu/tsort/Cargo.toml +++ b/src/uu/tsort/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_tsort" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "tsort ~ (uutils) topologically sort input (partially ordered) pairs" @@ -15,8 +15,8 @@ edition = "2018" path = "src/tsort.rs" [dependencies] -getopts = "0.2.18" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +clap= "2.33" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/tsort/src/main.rs b/src/uu/tsort/src/main.rs index 6b108cc5e..0694678d4 100644 --- a/src/uu/tsort/src/main.rs +++ b/src/uu/tsort/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_tsort); // spell-checker:ignore procs uucore tsort +uucore_procs::main!(uu_tsort); diff --git a/src/uu/tsort/src/tsort.rs b/src/uu/tsort/src/tsort.rs index 0a0023031..0a323f837 100644 --- a/src/uu/tsort/src/tsort.rs +++ b/src/uu/tsort/src/tsort.rs @@ -9,50 +9,32 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::collections::{HashMap, HashSet}; use std::fs::File; use std::io::{stdin, BufRead, BufReader, Read}; use std::path::Path; +use uucore::InvalidEncodingHandling; -static NAME: &str = "tsort"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static SUMMARY: &str = "Topological sort the strings in FILE. +Strings are defined as any sequence of tokens separated by whitespace (tab, space, or newline). +If FILE is not passed in, stdin is used instead."; +static USAGE: &str = "tsort [OPTIONS] FILE"; + +mod options { + pub const FILE: &str = "file"; +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = getopts::Options::new(); + let matches = uu_app().get_matches_from(args); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "{}", f), - }; - - if matches.opt_present("h") { - println!("{} {}", NAME, VERSION); - println!(); - println!("Usage:"); - println!(" {} [OPTIONS] FILE", NAME); - println!(); - println!("{}", opts.usage("Topological sort the strings in FILE. Strings are defined as any sequence of tokens separated by whitespace (tab, space, or newline). If FILE is not passed in, stdin is used instead.")); - return 0; - } - - if matches.opt_present("V") { - println!("{} {}", NAME, VERSION); - return 0; - } - - let files = matches.free.clone(); - let input = if files.len() > 1 { - crash!(1, "{}, extra operand '{}'", NAME, matches.free[1]); - } else if files.is_empty() { - "-".to_owned() - } else { - files[0].clone() - }; + let input = matches + .value_of(options::FILE) + .expect("Value is required by clap"); let mut stdin_buf; let mut file_buf; @@ -107,6 +89,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .usage(USAGE) + .about(SUMMARY) + .arg( + Arg::with_name(options::FILE) + .default_value("-") + .hidden(true), + ) +} + // We use String as a representation of node here // but using integer may improve performance. struct Graph { diff --git a/src/uu/tty/Cargo.toml b/src/uu/tty/Cargo.toml index 9912a3b9a..49b7669df 100644 --- a/src/uu/tty/Cargo.toml +++ b/src/uu/tty/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_tty" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "tty ~ (uutils) display the name of the terminal connected to standard input" @@ -15,9 +15,10 @@ edition = "2018" path = "src/tty.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] } +atty = "0.2" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["fs"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/tty/src/main.rs b/src/uu/tty/src/main.rs index 8cb1cbb4b..01a01e5ca 100644 --- a/src/uu/tty/src/main.rs +++ b/src/uu/tty/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_tty); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_tty); diff --git a/src/uu/tty/src/tty.rs b/src/uu/tty/src/tty.rs index 110f76d30..7412cdf45 100644 --- a/src/uu/tty/src/tty.rs +++ b/src/uu/tty/src/tty.rs @@ -12,68 +12,81 @@ #[macro_use] extern crate uucore; +use clap::{crate_version, App, Arg}; use std::ffi::CStr; -use uucore::fs::is_stdin_interactive; +use std::io::Write; +use uucore::InvalidEncodingHandling; -extern "C" { - fn ttyname(filedesc: libc::c_int) -> *const libc::c_char; +static ABOUT: &str = "Print the file name of the terminal connected to standard input."; + +mod options { + pub const SILENT: &str = "silent"; } -static NAME: &str = "tty"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +fn get_usage() -> String { + format!("{0} [OPTION]...", executable!()) +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let usage = get_usage(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = getopts::Options::new(); + let matches = uu_app().usage(&usage[..]).get_matches_from_safe(args); - opts.optflag("s", "silent", "print nothing, only return an exit status"); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - - let matches = match opts.parse(&args[1..]) { + let matches = match matches { Ok(m) => m, - Err(f) => crash!(2, "{}", f), + Err(e) => { + eprint!("{}", e); + return 2; + } }; - if matches.opt_present("help") { - println!("{} {}", NAME, VERSION); - println!(); - println!("Usage:"); - println!(" {} [OPTION]...", NAME); - println!(); - print!( - "{}", - opts.usage("Print the file name of the terminal connected to standard input.") - ); - } else if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - } else { - let silent = matches.opt_present("s"); + let silent = matches.is_present(options::SILENT); - let tty = unsafe { - let ptr = ttyname(libc::STDIN_FILENO); - if !ptr.is_null() { - String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string() - } else { - "".to_owned() - } - }; - - if !silent { - if !tty.chars().all(|c| c.is_whitespace()) { - println!("{}", tty); - } else { - println!("not a tty"); - } - } - - return if is_stdin_interactive() { - libc::EXIT_SUCCESS + // Call libc function ttyname + let tty = unsafe { + let ptr = libc::ttyname(libc::STDIN_FILENO); + if !ptr.is_null() { + String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string() } else { - libc::EXIT_FAILURE + "".to_owned() + } + }; + + let mut stdout = std::io::stdout(); + + if !silent { + let write_result = if !tty.chars().all(|c| c.is_whitespace()) { + writeln!(stdout, "{}", tty) + } else { + writeln!(stdout, "not a tty") }; + if write_result.is_err() || stdout.flush().is_err() { + // Don't return to prevent a panic later when another flush is attempted + // because the `uucore_procs::main` macro inserts a flush after execution for every utility. + std::process::exit(3); + } } - 0 + if atty::is(atty::Stream::Stdin) { + libc::EXIT_SUCCESS + } else { + libc::EXIT_FAILURE + } +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::SILENT) + .long(options::SILENT) + .visible_alias("quiet") + .short("s") + .help("print nothing, only return an exit status") + .required(false), + ) } diff --git a/src/uu/uname/Cargo.toml b/src/uu/uname/Cargo.toml index 87754f45a..9707d8444 100644 --- a/src/uu/uname/Cargo.toml +++ b/src/uu/uname/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_uname" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "uname ~ (uutils) display system information" @@ -17,7 +17,7 @@ path = "src/uname.rs" [dependencies] clap = "2.33" platform-info = "0.1" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/uname/src/main.rs b/src/uu/uname/src/main.rs index f40104670..5252f4716 100644 --- a/src/uu/uname/src/main.rs +++ b/src/uu/uname/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_uname); // spell-checker:ignore procs uucore uname +uucore_procs::main!(uu_uname); diff --git a/src/uu/uname/src/uname.rs b/src/uu/uname/src/uname.rs index 6575aa9fd..dda859722 100644 --- a/src/uu/uname/src/uname.rs +++ b/src/uu/uname/src/uname.rs @@ -13,10 +13,9 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; use platform_info::*; -const VERSION: &str = env!("CARGO_PKG_VERSION"); const ABOUT: &str = "Print certain system information. With no OPTION, same as -s."; pub mod options { @@ -39,7 +38,7 @@ const HOST_OS: &str = "Windows NT"; const HOST_OS: &str = "FreeBSD"; #[cfg(target_os = "openbsd")] const HOST_OS: &str = "OpenBSD"; -#[cfg(target_os = "macos")] +#[cfg(target_vendor = "apple")] const HOST_OS: &str = "Darwin"; #[cfg(target_os = "fuchsia")] const HOST_OS: &str = "Fuchsia"; @@ -48,49 +47,7 @@ const HOST_OS: &str = "Redox"; pub fn uumain(args: impl uucore::Args) -> i32 { let usage = format!("{} [OPTION]...", executable!()); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg(Arg::with_name(options::ALL) - .short("a") - .long(options::ALL) - .help("Behave as though all of the options -mnrsv were specified.")) - .arg(Arg::with_name(options::KERNELNAME) - .short("s") - .long(options::KERNELNAME) - .alias("sysname") // Obsolescent option in GNU uname - .help("print the kernel name.")) - .arg(Arg::with_name(options::NODENAME) - .short("n") - .long(options::NODENAME) - .help("print the nodename (the nodename may be a name that the system is known by to a communications network).")) - .arg(Arg::with_name(options::KERNELRELEASE) - .short("r") - .long(options::KERNELRELEASE) - .alias("release") // Obsolescent option in GNU uname - .help("print the operating system release.")) - .arg(Arg::with_name(options::KERNELVERSION) - .short("v") - .long(options::KERNELVERSION) - .help("print the operating system version.")) - .arg(Arg::with_name(options::HWPLATFORM) - .short("i") - .long(options::HWPLATFORM) - .help("print the hardware platform (non-portable)")) - .arg(Arg::with_name(options::MACHINE) - .short("m") - .long(options::MACHINE) - .help("print the machine hardware name.")) - .arg(Arg::with_name(options::PROCESSOR) - .short("p") - .long(options::PROCESSOR) - .help("print the processor type (non-portable)")) - .arg(Arg::with_name(options::OS) - .short("o") - .long(options::OS) - .help("print the operating system name.")) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); let uname = return_if_err!(1, PlatformInfo::new()); let mut output = String::new(); @@ -156,3 +113,47 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg(Arg::with_name(options::ALL) + .short("a") + .long(options::ALL) + .help("Behave as though all of the options -mnrsv were specified.")) + .arg(Arg::with_name(options::KERNELNAME) + .short("s") + .long(options::KERNELNAME) + .alias("sysname") // Obsolescent option in GNU uname + .help("print the kernel name.")) + .arg(Arg::with_name(options::NODENAME) + .short("n") + .long(options::NODENAME) + .help("print the nodename (the nodename may be a name that the system is known by to a communications network).")) + .arg(Arg::with_name(options::KERNELRELEASE) + .short("r") + .long(options::KERNELRELEASE) + .alias("release") // Obsolescent option in GNU uname + .help("print the operating system release.")) + .arg(Arg::with_name(options::KERNELVERSION) + .short("v") + .long(options::KERNELVERSION) + .help("print the operating system version.")) + .arg(Arg::with_name(options::HWPLATFORM) + .short("i") + .long(options::HWPLATFORM) + .help("print the hardware platform (non-portable)")) + .arg(Arg::with_name(options::MACHINE) + .short("m") + .long(options::MACHINE) + .help("print the machine hardware name.")) + .arg(Arg::with_name(options::PROCESSOR) + .short("p") + .long(options::PROCESSOR) + .help("print the processor type (non-portable)")) + .arg(Arg::with_name(options::OS) + .short("o") + .long(options::OS) + .help("print the operating system name.")) +} diff --git a/src/uu/unexpand/Cargo.toml b/src/uu/unexpand/Cargo.toml index ec6967e21..e39dd87ca 100644 --- a/src/uu/unexpand/Cargo.toml +++ b/src/uu/unexpand/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_unexpand" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "unexpand ~ (uutils) convert input spaces to tabs" @@ -15,9 +15,9 @@ edition = "2018" path = "src/unexpand.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" unicode-width = "0.1.5" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/unexpand/src/main.rs b/src/uu/unexpand/src/main.rs index 82d11dd95..2e7b1d967 100644 --- a/src/uu/unexpand/src/main.rs +++ b/src/uu/unexpand/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_unexpand); // spell-checker:ignore procs uucore unexpand +uucore_procs::main!(uu_unexpand); diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index 59a2dc6a0..50e3f186d 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -11,14 +11,17 @@ #[macro_use] extern crate uucore; - +use clap::{crate_version, App, Arg}; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Stdout, Write}; use std::str::from_utf8; use unicode_width::UnicodeWidthChar; +use uucore::InvalidEncodingHandling; static NAME: &str = "unexpand"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static USAGE: &str = "unexpand [OPTION]... [FILE]..."; +static SUMMARY: &str = "Convert blanks in each FILE to tabs, writing to standard output.\n + With no FILE, or when FILE is -, read standard input."; const DEFAULT_TABSTOP: usize = 8; @@ -46,6 +49,14 @@ fn tabstops_parse(s: String) -> Vec { nums } +mod options { + pub const FILE: &str = "file"; + pub const ALL: &str = "all"; + pub const FIRST_ONLY: &str = "first-only"; + pub const TABS: &str = "tabs"; + pub const NO_UTF8: &str = "no-utf8"; +} + struct Options { files: Vec, tabstops: Vec, @@ -54,20 +65,19 @@ struct Options { } impl Options { - fn new(matches: getopts::Matches) -> Options { - let tabstops = match matches.opt_str("t") { + fn new(matches: clap::ArgMatches) -> Options { + let tabstops = match matches.value_of(options::TABS) { None => vec![DEFAULT_TABSTOP], - Some(s) => tabstops_parse(s), + Some(s) => tabstops_parse(s.to_string()), }; - let aflag = (matches.opt_present("all") || matches.opt_present("tabs")) - && !matches.opt_present("first-only"); - let uflag = !matches.opt_present("U"); + let aflag = (matches.is_present(options::ALL) || matches.is_present(options::TABS)) + && !matches.is_present(options::FIRST_ONLY); + let uflag = !matches.is_present(options::NO_UTF8); - let files = if matches.free.is_empty() { - vec!["-".to_owned()] - } else { - matches.free + let files = match matches.value_of(options::FILE) { + Some(v) => vec![v.to_string()], + None => vec!["-".to_owned()], }; Options { @@ -80,68 +90,52 @@ impl Options { } pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let mut opts = getopts::Options::new(); - - opts.optflag( - "a", - "all", - "convert all blanks, instead of just initial blanks", - ); - opts.optflag( - "", - "first-only", - "convert only leading sequences of blanks (overrides -a)", - ); - opts.optopt( - "t", - "tabs", - "have tabs N characters apart instead of 8 (enables -a)", - "N", - ); - opts.optopt( - "t", - "tabs", - "use comma separated LIST of tab positions (enables -a)", - "LIST", - ); - opts.optflag( - "U", - "no-utf8", - "interpret input file as 8-bit ASCII rather than UTF-8", - ); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); - - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "{}", f), - }; - - if matches.opt_present("help") { - println!("{} {}\n", NAME, VERSION); - println!("Usage: {} [OPTION]... [FILE]...\n", NAME); - println!( - "{}", - opts.usage( - "Convert blanks in each FILE to tabs, writing to standard output.\n\ - With no FILE, or when FILE is -, read standard input." - ) - ); - return 0; - } - - if matches.opt_present("V") { - println!("{} {}", NAME, VERSION); - return 0; - } + let matches = uu_app().get_matches_from(args); unexpand(Options::new(matches)); 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .name(NAME) + .version(crate_version!()) + .usage(USAGE) + .about(SUMMARY) + .arg(Arg::with_name(options::FILE).hidden(true).multiple(true)) + .arg( + Arg::with_name(options::ALL) + .short("a") + .long(options::ALL) + .help("convert all blanks, instead of just initial blanks") + .takes_value(false), + ) + .arg( + Arg::with_name(options::FIRST_ONLY) + .long(options::FIRST_ONLY) + .help("convert only leading sequences of blanks (overrides -a)") + .takes_value(false), + ) + .arg( + Arg::with_name(options::TABS) + .short("t") + .long(options::TABS) + .long_help("use comma separated LIST of tab positions or have tabs N characters apart instead of 8 (enables -a)") + .takes_value(true) + ) + .arg( + Arg::with_name(options::NO_UTF8) + .short("U") + .long(options::NO_UTF8) + .takes_value(false) + .help("interpret input file as 8-bit ASCII rather than UTF-8")) +} + fn open(path: String) -> BufReader> { let file_buf; if path == "-" { @@ -160,10 +154,8 @@ fn next_tabstop(tabstops: &[usize], col: usize) -> Option { Some(tabstops[0] - col % tabstops[0]) } else { // find next larger tab - match tabstops.iter().find(|&&t| t > col) { - Some(t) => Some(t - col), - None => None, // if there isn't one in the list, tab becomes a single space - } + // if there isn't one in the list, tab becomes a single space + tabstops.iter().find(|&&t| t > col).map(|t| t - col) } } diff --git a/src/uu/uniq/Cargo.toml b/src/uu/uniq/Cargo.toml index d3cf4309d..3fe89b450 100644 --- a/src/uu/uniq/Cargo.toml +++ b/src/uu/uniq/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_uniq" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "uniq ~ (uutils) filter identical adjacent lines from input" @@ -16,7 +16,9 @@ path = "src/uniq.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +strum = "0.20" +strum_macros = "0.20" +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/uniq/src/main.rs b/src/uu/uniq/src/main.rs index dd4b6da1a..361c39f14 100644 --- a/src/uu/uniq/src/main.rs +++ b/src/uu/uniq/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_uniq); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_uniq); diff --git a/src/uu/uniq/src/uniq.rs b/src/uu/uniq/src/uniq.rs index a1809f0f0..20639c850 100644 --- a/src/uu/uniq/src/uniq.rs +++ b/src/uu/uniq/src/uniq.rs @@ -8,14 +8,14 @@ #[macro_use] extern crate uucore; -use clap::{App, Arg, ArgMatches}; +use clap::{crate_version, App, Arg, ArgMatches}; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Result, Write}; use std::path::Path; use std::str::FromStr; +use strum_macros::{AsRefStr, EnumString}; static ABOUT: &str = "Report or omit repeated lines."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod options { pub static ALL_REPEATED: &str = "all-repeated"; pub static CHECK_CHARS: &str = "check-chars"; @@ -26,14 +26,18 @@ pub mod options { pub static SKIP_CHARS: &str = "skip-chars"; pub static UNIQUE: &str = "unique"; pub static ZERO_TERMINATED: &str = "zero-terminated"; + pub static GROUP: &str = "group"; } static ARG_FILES: &str = "files"; -#[derive(PartialEq)] +#[derive(PartialEq, Clone, Copy, AsRefStr, EnumString)] +#[strum(serialize_all = "snake_case")] enum Delimiters { + Append, Prepend, Separate, + Both, None, } @@ -56,24 +60,44 @@ impl Uniq { reader: &mut BufReader, writer: &mut BufWriter, ) { - let mut lines: Vec = vec![]; let mut first_line_printed = false; - let delimiters = &self.delimiters; + let mut group_count = 1; let line_terminator = self.get_line_terminator(); + let mut lines = reader.split(line_terminator).map(get_line_string); + let mut line = match lines.next() { + Some(l) => l, + None => return, + }; - for line in reader.split(line_terminator).map(get_line_string) { - if !lines.is_empty() && self.cmp_keys(&lines[0], &line) { - let print_delimiter = delimiters == &Delimiters::Prepend - || (delimiters == &Delimiters::Separate && first_line_printed); - first_line_printed |= self.print_lines(writer, &lines, print_delimiter); - lines.truncate(0); + // compare current `line` with consecutive lines (`next_line`) of the input + // and if needed, print `line` based on the command line options provided + for next_line in lines { + if self.cmp_keys(&line, &next_line) { + if (group_count == 1 && !self.repeats_only) + || (group_count > 1 && !self.uniques_only) + { + self.print_line(writer, &line, group_count, first_line_printed); + first_line_printed = true; + } + line = next_line; + group_count = 1; + } else { + if self.all_repeated { + self.print_line(writer, &line, group_count, first_line_printed); + first_line_printed = true; + line = next_line; + } + group_count += 1; } - lines.push(line); } - if !lines.is_empty() { - let print_delimiter = delimiters == &Delimiters::Prepend - || (delimiters == &Delimiters::Separate && first_line_printed); - self.print_lines(writer, &lines, print_delimiter); + if (group_count == 1 && !self.repeats_only) || (group_count > 1 && !self.uniques_only) { + self.print_line(writer, &line, group_count, first_line_printed); + first_line_printed = true; + } + if (self.delimiters == Delimiters::Append || self.delimiters == Delimiters::Both) + && first_line_printed + { + crash_if_err!(1, writer.write_all(&[line_terminator])); } } @@ -147,27 +171,17 @@ impl Uniq { } } - fn print_lines( - &self, - writer: &mut BufWriter, - lines: &[String], - print_delimiter: bool, - ) -> bool { - let mut first_line_printed = false; - let mut count = if self.all_repeated { 1 } else { lines.len() }; - if lines.len() == 1 && !self.repeats_only || lines.len() > 1 && !self.uniques_only { - self.print_line(writer, &lines[0], count, print_delimiter); - first_line_printed = true; - count += 1; - } - if self.all_repeated { - for line in lines[1..].iter() { - self.print_line(writer, line, count, print_delimiter && !first_line_printed); - first_line_printed = true; - count += 1; - } - } - first_line_printed + fn should_print_delimiter(&self, group_count: usize, first_line_printed: bool) -> bool { + // if no delimiter option is selected then no other checks needed + self.delimiters != Delimiters::None + // print delimiter only before the first line of a group, not between lines of a group + && group_count == 1 + // if at least one line has been output before current group then print delimiter + && (first_line_printed + // or if we need to prepend delimiter then print it even at the start of the output + || self.delimiters == Delimiters::Prepend + // the 'both' delimit mode should prepend and append delimiters + || self.delimiters == Delimiters::Both) } fn print_line( @@ -175,11 +189,11 @@ impl Uniq { writer: &mut BufWriter, line: &str, count: usize, - print_delimiter: bool, + first_line_printed: bool, ) { let line_terminator = self.get_line_terminator(); - if print_delimiter { + if self.should_print_delimiter(count, first_line_printed) { crash_if_err!(1, writer.write_all(&[line_terminator])); } @@ -224,19 +238,80 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let long_usage = get_long_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() .usage(&usage[..]) .after_help(&long_usage[..]) + .get_matches_from(args); + + let files: Vec = matches + .values_of(ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); + + let (in_file_name, out_file_name) = match files.len() { + 0 => ("-".to_owned(), "-".to_owned()), + 1 => (files[0].clone(), "-".to_owned()), + 2 => (files[0].clone(), files[1].clone()), + _ => { + // Cannot happen as clap will fail earlier + crash!(1, "Extra operand: {}", files[2]); + } + }; + + let uniq = Uniq { + repeats_only: matches.is_present(options::REPEATED) + || matches.is_present(options::ALL_REPEATED), + uniques_only: matches.is_present(options::UNIQUE), + all_repeated: matches.is_present(options::ALL_REPEATED) + || matches.is_present(options::GROUP), + delimiters: get_delimiter(&matches), + show_counts: matches.is_present(options::COUNT), + skip_fields: opt_parsed(options::SKIP_FIELDS, &matches), + slice_start: opt_parsed(options::SKIP_CHARS, &matches), + slice_stop: opt_parsed(options::CHECK_CHARS, &matches), + ignore_case: matches.is_present(options::IGNORE_CASE), + zero_terminated: matches.is_present(options::ZERO_TERMINATED), + }; + uniq.print_uniq( + &mut open_input_file(in_file_name), + &mut open_output_file(out_file_name), + ); + + 0 +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) .arg( Arg::with_name(options::ALL_REPEATED) .short("D") .long(options::ALL_REPEATED) - .possible_values(&["none", "prepend", "separate"]) - .help("print all duplicate lines. Delimiting is done with blank lines") + .possible_values(&[ + Delimiters::None.as_ref(), Delimiters::Prepend.as_ref(), Delimiters::Separate.as_ref() + ]) + .help("print all duplicate lines. Delimiting is done with blank lines. [default: none]") .value_name("delimit-method") - .default_value("none"), + .min_values(0) + .max_values(1), + ) + .arg( + Arg::with_name(options::GROUP) + .long(options::GROUP) + .possible_values(&[ + Delimiters::Separate.as_ref(), Delimiters::Prepend.as_ref(), + Delimiters::Append.as_ref(), Delimiters::Both.as_ref() + ]) + .help("show all items, separating groups with an empty line. [default: separate]") + .value_name("group-method") + .min_values(0) + .max_values(1) + .conflicts_with_all(&[ + options::REPEATED, + options::ALL_REPEATED, + options::UNIQUE, + ]), ) .arg( Arg::with_name(options::CHECK_CHARS) @@ -295,49 +370,19 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .takes_value(true) .max_values(2), ) - .get_matches_from(args); +} - let files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - let (in_file_name, out_file_name) = match files.len() { - 0 => ("-".to_owned(), "-".to_owned()), - 1 => (files[0].clone(), "-".to_owned()), - 2 => (files[0].clone(), files[1].clone()), - _ => { - // Cannot happen as clap will fail earlier - crash!(1, "Extra operand: {}", files[2]); - } - }; - - let uniq = Uniq { - repeats_only: matches.is_present(options::REPEATED) - || matches.occurrences_of(options::ALL_REPEATED) > 0, - uniques_only: matches.is_present(options::UNIQUE), - all_repeated: matches.occurrences_of(options::ALL_REPEATED) > 0, - delimiters: match matches.value_of(options::ALL_REPEATED).map(String::from) { - Some(ref opt_arg) if opt_arg != "none" => match &(*opt_arg.as_str()) { - "prepend" => Delimiters::Prepend, - "separate" => Delimiters::Separate, - _ => crash!(1, "Incorrect argument for all-repeated: {}", opt_arg), - }, - _ => Delimiters::None, - }, - show_counts: matches.is_present(options::COUNT), - skip_fields: opt_parsed(options::SKIP_FIELDS, &matches), - slice_start: opt_parsed(options::SKIP_CHARS, &matches), - slice_stop: opt_parsed(options::CHECK_CHARS, &matches), - ignore_case: matches.is_present(options::IGNORE_CASE), - zero_terminated: matches.is_present(options::ZERO_TERMINATED), - }; - uniq.print_uniq( - &mut open_input_file(in_file_name), - &mut open_output_file(out_file_name), - ); - - 0 +fn get_delimiter(matches: &ArgMatches) -> Delimiters { + let value = matches + .value_of(options::ALL_REPEATED) + .or_else(|| matches.value_of(options::GROUP)); + if let Some(delimiter_arg) = value { + crash_if_err!(1, Delimiters::from_str(delimiter_arg)) + } else if matches.is_present(options::GROUP) { + Delimiters::Separate + } else { + Delimiters::None + } } fn open_input_file(in_file_name: String) -> BufReader> { diff --git a/src/uu/unlink/Cargo.toml b/src/uu/unlink/Cargo.toml index 68edec54d..08da2624e 100644 --- a/src/uu/unlink/Cargo.toml +++ b/src/uu/unlink/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_unlink" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "unlink ~ (uutils) remove a (file system) link to FILE" @@ -15,9 +15,9 @@ edition = "2018" path = "src/unlink.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/unlink/src/main.rs b/src/uu/unlink/src/main.rs index f01b6bac3..b03d4a675 100644 --- a/src/uu/unlink/src/main.rs +++ b/src/uu/unlink/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_unlink); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_unlink); diff --git a/src/uu/unlink/src/unlink.rs b/src/uu/unlink/src/unlink.rs index b85b6ea94..49f17cb12 100644 --- a/src/uu/unlink/src/unlink.rs +++ b/src/uu/unlink/src/unlink.rs @@ -12,59 +12,50 @@ #[macro_use] extern crate uucore; -use getopts::Options; +use clap::{crate_version, App, Arg}; use libc::{lstat, stat, unlink}; use libc::{S_IFLNK, S_IFMT, S_IFREG}; use std::ffi::CString; use std::io::{Error, ErrorKind}; +use uucore::InvalidEncodingHandling; -static NAME: &str = "unlink"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Unlink the file at [FILE]."; +static OPT_PATH: &str = "FILE"; + +fn get_usage() -> String { + format!("{} [OPTION]... FILE", executable!()) +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::ConvertLossy) + .accept_any(); - let mut opts = Options::new(); + let usage = get_usage(); - opts.optflag("h", "help", "display this help and exit"); - opts.optflag("V", "version", "output version information and exit"); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(f) => crash!(1, "invalid options\n{}", f), - }; + let paths: Vec = matches + .values_of(OPT_PATH) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); - if matches.opt_present("help") { - println!("{} {}", NAME, VERSION); - println!(); - println!("Usage:"); - println!(" {} [FILE]... [OPTION]...", NAME); - println!(); - println!("{}", opts.usage("Unlink the file at [FILE].")); - return 0; - } - - if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); - return 0; - } - - if matches.free.is_empty() { + if paths.is_empty() { crash!( 1, "missing operand\nTry '{0} --help' for more information.", - NAME + executable!() ); - } else if matches.free.len() > 1 { + } else if paths.len() > 1 { crash!( 1, "extra operand: '{1}'\nTry '{0} --help' for more information.", - NAME, - matches.free[1] + executable!(), + paths[1] ); } - let c_string = CString::new(matches.free[0].clone()).unwrap(); // unwrap() cannot fail, the string comes from argv so it cannot contain a \0. + let c_string = CString::new(paths[0].clone()).unwrap(); // unwrap() cannot fail, the string comes from argv so it cannot contain a \0. let st_mode = { #[allow(deprecated)] @@ -72,12 +63,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let result = unsafe { lstat(c_string.as_ptr(), &mut buf as *mut stat) }; if result < 0 { - crash!( - 1, - "Cannot stat '{}': {}", - matches.free[0], - Error::last_os_error() - ); + crash!(1, "Cannot stat '{}': {}", paths[0], Error::last_os_error()); } buf.st_mode & S_IFMT @@ -101,9 +87,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 { match result { Ok(_) => (), Err(e) => { - crash!(1, "cannot unlink '{0}': {1}", matches.free[0], e); + crash!(1, "cannot unlink '{0}': {1}", paths[0], e); } } 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg(Arg::with_name(OPT_PATH).hidden(true).multiple(true)) +} diff --git a/src/uu/uptime/Cargo.toml b/src/uu/uptime/Cargo.toml index d66acc77f..1136e6420 100644 --- a/src/uu/uptime/Cargo.toml +++ b/src/uu/uptime/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_uptime" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "uptime ~ (uutils) display dynamic system information" @@ -17,7 +17,7 @@ path = "src/uptime.rs" [dependencies] chrono = "0.4" clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["libc", "utmpx"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["libc", "utmpx"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/uptime/src/main.rs b/src/uu/uptime/src/main.rs index 352703eb3..be5d5ab01 100644 --- a/src/uu/uptime/src/main.rs +++ b/src/uu/uptime/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_uptime); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_uptime); diff --git a/src/uu/uptime/src/uptime.rs b/src/uu/uptime/src/uptime.rs index 670d7845b..35270093c 100644 --- a/src/uu/uptime/src/uptime.rs +++ b/src/uu/uptime/src/uptime.rs @@ -9,7 +9,7 @@ // spell-checker:ignore (ToDO) getloadavg upsecs updays nusers loadavg boottime uphours upmins use chrono::{Local, TimeZone, Utc}; -use clap::{App, Arg}; +use clap::{crate_version, App, Arg}; #[macro_use] extern crate uucore; @@ -17,10 +17,9 @@ extern crate uucore; pub use uucore::libc; use uucore::libc::time_t; -static VERSION: &str = env!("CARGO_PKG_VERSION"); static ABOUT: &str = "Display the current time, the length of time the system has been up,\n\ -the number of users on the system, and the average number of jobs\n\ -in the run queue over the last 1, 5 and 15 minutes."; + the number of users on the system, and the average number of jobs\n\ + in the run queue over the last 1, 5 and 15 minutes."; pub mod options { pub static SINCE: &str = "since"; } @@ -39,17 +38,7 @@ fn get_usage() -> String { pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) - .usage(&usage[..]) - .arg( - Arg::with_name(options::SINCE) - .short("s") - .long(options::SINCE) - .help("system up since"), - ) - .get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from(args); let (boot_time, user_count) = process_utmpx(); let uptime = get_uptime(boot_time); @@ -74,6 +63,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::SINCE) + .short("s") + .long(options::SINCE) + .help("system up since"), + ) +} + #[cfg(unix)] fn print_loadavg() { use uucore::libc::c_double; diff --git a/src/uu/users/Cargo.toml b/src/uu/users/Cargo.toml index ed6f110b6..84da13020 100644 --- a/src/uu/users/Cargo.toml +++ b/src/uu/users/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_users" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "users ~ (uutils) display names of currently logged-in users" @@ -16,7 +16,7 @@ path = "src/users.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["utmpx"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["utmpx"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/users/src/main.rs b/src/uu/users/src/main.rs index f065c633c..f30a01ecb 100644 --- a/src/uu/users/src/main.rs +++ b/src/uu/users/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_users); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_users); diff --git a/src/uu/users/src/users.rs b/src/uu/users/src/users.rs index 4bb628441..ef878497c 100644 --- a/src/uu/users/src/users.rs +++ b/src/uu/users/src/users.rs @@ -6,19 +6,15 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -/* last synced with: whoami (GNU coreutils) 8.22 */ -// Allow dead code here in order to keep all fields, constants here, for consistency. -#![allow(dead_code)] +// spell-checker:ignore (paths) wtmp #[macro_use] extern crate uucore; -use uucore::utmpx::*; +use clap::{crate_version, App, Arg}; +use uucore::utmpx::{self, Utmpx}; -use clap::{App, Arg}; - -static ABOUT: &str = "Display who is currently logged in, according to FILE."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static ABOUT: &str = "Print the user names of users currently logged in to the current host"; static ARG_FILES: &str = "files"; @@ -26,14 +22,21 @@ fn get_usage() -> String { format!("{0} [FILE]", executable!()) } +fn get_long_usage() -> String { + format!( + "Output who is currently logged in according to FILE. +If FILE is not specified, use {}. /var/log/wtmp as FILE is common.", + utmpx::DEFAULT_FILE + ) +} + pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); + let after_help = get_long_usage(); - let matches = App::new(executable!()) - .version(VERSION) - .about(ABOUT) + let matches = uu_app() .usage(&usage[..]) - .arg(Arg::with_name(ARG_FILES).takes_value(true).max_values(1)) + .after_help(&after_help[..]) .get_matches_from(args); let files: Vec = matches @@ -44,7 +47,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let filename = if !files.is_empty() { files[0].as_ref() } else { - DEFAULT_FILE + utmpx::DEFAULT_FILE }; let mut users = Utmpx::iter_all_records() @@ -60,3 +63,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg(Arg::with_name(ARG_FILES).takes_value(true).max_values(1)) +} diff --git a/src/uu/wc/Cargo.toml b/src/uu/wc/Cargo.toml index 894ac44dd..8ae79dc08 100644 --- a/src/uu/wc/Cargo.toml +++ b/src/uu/wc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_wc" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "wc ~ (uutils) display newline, word, and byte counts for input" @@ -16,8 +16,13 @@ path = "src/wc.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore" } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +thiserror = "1.0" + +[target.'cfg(unix)'.dependencies] +nix = "0.20" +libc = "0.2" [[bin]] name = "wc" diff --git a/src/uu/wc/src/count_bytes.rs b/src/uu/wc/src/count_bytes.rs new file mode 100644 index 000000000..7f06f8171 --- /dev/null +++ b/src/uu/wc/src/count_bytes.rs @@ -0,0 +1,107 @@ +use super::{WcResult, WordCountable}; + +#[cfg(any(target_os = "linux", target_os = "android"))] +use std::fs::OpenOptions; +use std::io::ErrorKind; + +#[cfg(unix)] +use libc::S_IFREG; +#[cfg(unix)] +use nix::sys::stat::fstat; +#[cfg(any(target_os = "linux", target_os = "android"))] +use std::os::unix::io::{AsRawFd, RawFd}; + +#[cfg(any(target_os = "linux", target_os = "android"))] +use libc::S_IFIFO; +#[cfg(any(target_os = "linux", target_os = "android"))] +use nix::fcntl::{splice, SpliceFFlags}; +#[cfg(any(target_os = "linux", target_os = "android"))] +use nix::unistd::pipe; + +const BUF_SIZE: usize = 16384; + +/// Splice wrapper which handles short writes +#[cfg(any(target_os = "linux", target_os = "android"))] +#[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(()) +} + +/// This is a Linux-specific function to count the number of bytes using the +/// `splice` system call, which is faster than using `read`. +#[inline] +#[cfg(any(target_os = "linux", target_os = "android"))] +fn count_bytes_using_splice(fd: RawFd) -> nix::Result { + let null_file = OpenOptions::new() + .write(true) + .open("/dev/null") + .map_err(|_| nix::Error::last())?; + let null = null_file.as_raw_fd(); + let (pipe_rd, pipe_wr) = pipe()?; + + let mut byte_count = 0; + loop { + let res = splice(fd, None, pipe_wr, None, BUF_SIZE, SpliceFFlags::empty())?; + if res == 0 { + break; + } + byte_count += res; + splice_exact(pipe_rd, null, res)?; + } + + Ok(byte_count) +} + +/// In the special case where we only need to count the number of bytes. There +/// are several optimizations we can do: +/// 1. On Unix, we can simply `stat` the file if it is regular. +/// 2. On Linux -- if the above did not work -- we can use splice to count +/// the number of bytes if the file is a FIFO. +/// 3. Otherwise, we just read normally, but without the overhead of counting +/// other things such as lines and words. +#[inline] +pub(crate) fn count_bytes_fast(handle: &mut T) -> WcResult { + #[cfg(unix)] + { + let fd = handle.as_raw_fd(); + if let Ok(stat) = fstat(fd) { + // If the file is regular, then the `st_size` should hold + // the file's size in bytes. + if (stat.st_mode & S_IFREG) != 0 { + return Ok(stat.st_size as usize); + } + #[cfg(any(target_os = "linux", target_os = "android"))] + { + // Else, if we're on Linux and our file is a FIFO pipe + // (or stdin), we use splice to count the number of bytes. + if (stat.st_mode & S_IFIFO) != 0 { + if let Ok(n) = count_bytes_using_splice(fd) { + return Ok(n); + } + } + } + } + } + + // Fall back on `read`, but without the overhead of counting words and lines. + let mut buf = [0_u8; BUF_SIZE]; + let mut byte_count = 0; + loop { + match handle.read(&mut buf) { + Ok(0) => return Ok(byte_count), + Ok(n) => { + byte_count += n; + } + Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, + Err(e) => return Err(e.into()), + } + } +} diff --git a/src/uu/wc/src/countable.rs b/src/uu/wc/src/countable.rs new file mode 100644 index 000000000..3da910a03 --- /dev/null +++ b/src/uu/wc/src/countable.rs @@ -0,0 +1,72 @@ +//! Traits and implementations for iterating over lines in a file-like object. +//! +//! This module provides a [`WordCountable`] trait and implementations +//! for some common file-like objects. Use the [`WordCountable::lines`] +//! method to get an iterator over lines of a file-like object. +use std::fs::File; +use std::io::{self, BufRead, BufReader, Read, StdinLock}; + +#[cfg(unix)] +use std::os::unix::io::AsRawFd; + +#[cfg(unix)] +pub trait WordCountable: AsRawFd + Read { + type Buffered: BufRead; + fn lines(self) -> Lines; +} + +#[cfg(not(unix))] +pub trait WordCountable: Read { + type Buffered: BufRead; + fn lines(self) -> Lines; +} + +impl WordCountable for StdinLock<'_> { + type Buffered = Self; + + fn lines(self) -> Lines + where + Self: Sized, + { + Lines { buf: self } + } +} +impl WordCountable for File { + type Buffered = BufReader; + + fn lines(self) -> Lines + where + Self: Sized, + { + Lines { + buf: BufReader::new(self), + } + } +} + +/// An iterator over the lines of an instance of `BufRead`. +/// +/// Similar to [`io::Lines`] but yields each line as a `Vec` and +/// includes the newline character (`\n`, the `0xA` byte) that +/// terminates the line. +/// +/// [`io::Lines`]:: io::Lines +pub struct Lines { + buf: B, +} + +impl Iterator for Lines { + type Item = io::Result>; + + fn next(&mut self) -> Option { + let mut line = Vec::new(); + + // reading from a TTY seems to raise a condition on, rather than return Some(0) like a file. + // hence the option wrapped in a result here + match self.buf.read_until(b'\n', &mut line) { + Ok(0) => None, + Ok(_n) => Some(Ok(line)), + Err(e) => Some(Err(e)), + } + } +} diff --git a/src/uu/wc/src/main.rs b/src/uu/wc/src/main.rs index ce5629e51..b58b9cac7 100644 --- a/src/uu/wc/src/main.rs +++ b/src/uu/wc/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_wc); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_wc); diff --git a/src/uu/wc/src/wc.rs b/src/uu/wc/src/wc.rs index 972802f81..0bcc66664 100644 --- a/src/uu/wc/src/wc.rs +++ b/src/uu/wc/src/wc.rs @@ -5,18 +5,35 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore (ToDO) fpath - #[macro_use] extern crate uucore; -use clap::{App, Arg, ArgMatches}; +mod count_bytes; +mod countable; +mod word_count; +use count_bytes::count_bytes_fast; +use countable::WordCountable; +use word_count::{TitledWordCount, WordCount}; -use std::fs::File; -use std::io::{stdin, BufRead, BufReader, Read}; +use clap::{crate_version, App, Arg, ArgMatches}; +use thiserror::Error; + +use std::fs::{self, File}; +use std::io::{self, ErrorKind, Write}; use std::path::Path; -use std::result::Result as StdResult; -use std::str::from_utf8; + +/// The minimum character width for formatting counts when reading from stdin. +const MINIMUM_WIDTH: usize = 7; + +#[derive(Error, Debug)] +pub enum WcError { + #[error("{0}")] + Io(#[from] io::Error), + #[error("Expected a file, found directory {0}")] + IsDirectory(String), +} + +type WcResult = Result; struct Settings { show_bytes: bool, @@ -53,20 +70,20 @@ impl Settings { show_max_line_length: false, } } -} -struct Result { - title: String, - bytes: usize, - chars: usize, - lines: usize, - words: usize, - max_line_length: usize, + fn number_enabled(&self) -> u32 { + let mut result = 0; + result += self.show_bytes as u32; + result += self.show_chars as u32; + result += self.show_lines as u32; + result += self.show_max_line_length as u32; + result += self.show_words as u32; + result + } } static ABOUT: &str = "Display newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified."; -static VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod options { pub static BYTES: &str = "bytes"; @@ -86,13 +103,70 @@ fn get_usage() -> String { ) } +enum StdinKind { + /// Stdin specified on command-line with "-". + Explicit, + + /// Stdin implicitly specified on command-line by not passing any positional argument. + Implicit, +} + +/// Supported inputs. +enum Input { + /// A regular file. + Path(String), + + /// Standard input. + Stdin(StdinKind), +} + +impl Input { + /// Converts input to title that appears in stats. + fn to_title(&self) -> Option<&str> { + match self { + Input::Path(path) => Some(path), + Input::Stdin(StdinKind::Explicit) => Some("-"), + Input::Stdin(StdinKind::Implicit) => None, + } + } +} + pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); - let matches = App::new(executable!()) - .version(VERSION) + let matches = uu_app().usage(&usage[..]).get_matches_from(args); + + let mut inputs: Vec = matches + .values_of(ARG_FILES) + .map(|v| { + v.map(|i| { + if i == "-" { + Input::Stdin(StdinKind::Explicit) + } else { + Input::Path(ToString::to_string(i)) + } + }) + .collect() + }) + .unwrap_or_default(); + + if inputs.is_empty() { + inputs.push(Input::Stdin(StdinKind::Implicit)); + } + + let settings = Settings::new(&matches); + + if wc(inputs, &settings).is_ok() { + 0 + } else { + 1 + } +} + +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) .about(ABOUT) - .usage(&usage[..]) .arg( Arg::with_name(options::BYTES) .short("c") @@ -124,184 +198,276 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .help("print the word counts"), ) .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) - .get_matches_from(args); - - let mut files: Vec = matches - .values_of(ARG_FILES) - .map(|v| v.map(ToString::to_string).collect()) - .unwrap_or_default(); - - if files.is_empty() { - files.push("-".to_owned()); - } - - let settings = Settings::new(&matches); - - match wc(files, &settings) { - Ok(()) => ( /* pass */ ), - Err(e) => return e, - } - - 0 } -const CR: u8 = b'\r'; -const LF: u8 = b'\n'; -const SPACE: u8 = b' '; -const TAB: u8 = b'\t'; -const SYN: u8 = 0x16_u8; -const FF: u8 = 0x0C_u8; - -#[inline(always)] -fn is_word_separator(byte: u8) -> bool { - byte == SPACE || byte == TAB || byte == CR || byte == SYN || byte == FF -} - -fn wc(files: Vec, settings: &Settings) -> StdResult<(), i32> { - let mut total_line_count: usize = 0; - let mut total_word_count: usize = 0; - let mut total_char_count: usize = 0; - let mut total_byte_count: usize = 0; - let mut total_longest_line_length: usize = 0; - - let mut results = vec![]; - let mut max_width: usize = 0; +fn word_count_from_reader( + mut reader: T, + settings: &Settings, + path: &str, +) -> WcResult { + let only_count_bytes = settings.show_bytes + && (!(settings.show_chars + || settings.show_lines + || settings.show_max_line_length + || settings.show_words)); + if only_count_bytes { + return Ok(WordCount { + bytes: count_bytes_fast(&mut reader)?, + ..WordCount::default() + }); + } // we do not need to decode the byte stream if we're only counting bytes/newlines let decode_chars = settings.show_chars || settings.show_words || settings.show_max_line_length; - for path in &files { - let mut reader = open(&path[..])?; - - let mut line_count: usize = 0; - let mut word_count: usize = 0; - let mut byte_count: usize = 0; - let mut char_count: usize = 0; - let mut longest_line_length: usize = 0; - let mut raw_line = Vec::new(); - let mut ends_lf: bool; - // reading from a TTY seems to raise a condition on, rather than return Some(0) like a file. - // hence the option wrapped in a result here - while match reader.read_until(LF, &mut raw_line) { - Ok(n) if n > 0 => true, - Err(ref e) if !raw_line.is_empty() => { + // Sum the WordCount for each line. Show a warning for each line + // that results in an IO error when trying to read it. + let total = reader + .lines() + .filter_map(|res| match res { + Ok(line) => Some(line), + Err(e) => { show_warning!("Error while reading {}: {}", path, e); - !raw_line.is_empty() + None } - _ => false, - } { - // GNU 'wc' only counts lines that end in LF as lines - ends_lf = *raw_line.last().unwrap() == LF; - line_count += ends_lf as usize; + }) + .map(|line| WordCount::from_line(&line, decode_chars)) + .sum(); + Ok(total) +} - byte_count += raw_line.len(); - - if decode_chars { - // try and convert the bytes to UTF-8 first - let current_char_count; - match from_utf8(&raw_line[..]) { - Ok(line) => { - word_count += line.split_whitespace().count(); - current_char_count = line.chars().count(); - } - Err(..) => { - word_count += raw_line.split(|&x| is_word_separator(x)).count(); - current_char_count = raw_line.iter().filter(|c| c.is_ascii()).count() - } - } - char_count += current_char_count; - if current_char_count > longest_line_length { - // -L is a GNU 'wc' extension so same behavior on LF - longest_line_length = current_char_count - (ends_lf as usize); - } - } - - raw_line.truncate(0); +fn word_count_from_input(input: &Input, settings: &Settings) -> WcResult { + match input { + Input::Stdin(_) => { + let stdin = io::stdin(); + let stdin_lock = stdin.lock(); + word_count_from_reader(stdin_lock, settings, "-") } + Input::Path(path) => { + let path_obj = Path::new(path); + if path_obj.is_dir() { + Err(WcError::IsDirectory(path.to_owned())) + } else { + let file = File::open(path)?; + word_count_from_reader(file, settings, path) + } + } + } +} - results.push(Result { - title: path.clone(), - bytes: byte_count, - chars: char_count, - lines: line_count, - words: word_count, - max_line_length: longest_line_length, +/// Print a message appropriate for the particular error to `stderr`. +/// +/// # Examples +/// +/// This will print `wc: /tmp: Is a directory` to `stderr`. +/// +/// ```rust,ignore +/// show_error(Input::Path("/tmp"), WcError::IsDirectory("/tmp")) +/// ``` +fn show_error(input: &Input, err: WcError) { + match (input, err) { + (_, WcError::IsDirectory(path)) => { + show_error_custom_description!(path, "Is a directory"); + } + (Input::Path(path), WcError::Io(e)) if e.kind() == ErrorKind::NotFound => { + show_error_custom_description!(path, "No such file or directory"); + } + (_, e) => { + show_error!("{}", e); + } + }; +} + +/// Compute the number of digits needed to represent any count for this input. +/// +/// If `input` is [`Input::Stdin`], then this function returns +/// [`MINIMUM_WIDTH`]. Otherwise, if metadata could not be read from +/// `input` then this function returns 1. +/// +/// # Errors +/// +/// This function will return an error if `input` is a [`Input::Path`] +/// and there is a problem accessing the metadata of the given `input`. +/// +/// # Examples +/// +/// A [`Input::Stdin`] gets a default minimum width: +/// +/// ```rust,ignore +/// let input = Input::Stdin(StdinKind::Explicit); +/// assert_eq!(7, digit_width(input)); +/// ``` +fn digit_width(input: &Input) -> WcResult> { + match input { + Input::Stdin(_) => Ok(Some(MINIMUM_WIDTH)), + Input::Path(filename) => { + let path = Path::new(filename); + let metadata = fs::metadata(path)?; + if metadata.is_file() { + // TODO We are now computing the number of bytes in a file + // twice: once here and once in `WordCount::from_line()` (or + // in `count_bytes_fast()` if that function is called + // instead). See GitHub issue #2201. + let num_bytes = metadata.len(); + let num_digits = num_bytes.to_string().len(); + Ok(Some(num_digits)) + } else { + Ok(None) + } + } + } +} + +/// Compute the number of digits needed to represent all counts in all inputs. +/// +/// `inputs` may include zero or more [`Input::Stdin`] entries, each of +/// which represents reading from `stdin`. The presence of any such +/// entry causes this function to return a width that is at least +/// [`MINIMUM_WIDTH`]. +/// +/// If `input` is empty, then this function returns 1. If file metadata +/// could not be read from any of the [`Input::Path`] inputs and there +/// are no [`Input::Stdin`] inputs, then this function returns 1. +/// +/// If there is a problem accessing the metadata, this function will +/// silently ignore the error and assume that the number of digits +/// needed to display the counts for that file is 1. +/// +/// # Examples +/// +/// An empty slice implies a width of 1: +/// +/// ```rust,ignore +/// assert_eq!(1, max_width(&vec![])); +/// ``` +/// +/// The presence of [`Input::Stdin`] implies a minimum width: +/// +/// ```rust,ignore +/// let inputs = vec![Input::Stdin(StdinKind::Explicit)]; +/// assert_eq!(7, max_width(&inputs)); +/// ``` +fn max_width(inputs: &[Input]) -> usize { + let mut result = 1; + for input in inputs { + match digit_width(input) { + Ok(maybe_n) => { + if let Some(n) = maybe_n { + result = result.max(n); + } + } + Err(_) => continue, + } + } + result +} + +fn wc(inputs: Vec, settings: &Settings) -> Result<(), u32> { + // Compute the width, in digits, to use when formatting counts. + // + // The width is the number of digits needed to print the number of + // bytes in the largest file. This is true regardless of whether + // the `settings` indicate that the bytes will be displayed. + let mut error_count = 0; + let max_width = max_width(&inputs); + + let mut total_word_count = WordCount::default(); + + let num_inputs = inputs.len(); + + for input in &inputs { + let word_count = word_count_from_input(input, settings).unwrap_or_else(|err| { + show_error(input, err); + error_count += 1; + WordCount::default() }); - - total_line_count += line_count; total_word_count += word_count; - total_char_count += char_count; - total_byte_count += byte_count; - - if longest_line_length > total_longest_line_length { - total_longest_line_length = longest_line_length; + let result = word_count.with_title(input.to_title()); + if let Err(err) = print_stats(settings, &result, max_width) { + show_warning!( + "failed to print result for {}: {}", + result.title.unwrap_or(""), + err + ); + error_count += 1; } - - // used for formatting - max_width = total_byte_count.to_string().len() + 1; } - for result in &results { - print_stats(settings, &result, max_width); + if num_inputs > 1 { + let total_result = total_word_count.with_title(Some("total")); + if let Err(err) = print_stats(settings, &total_result, max_width) { + show_warning!("failed to print total: {}", err); + error_count += 1; + } } - if files.len() > 1 { - let result = Result { - title: "total".to_owned(), - bytes: total_byte_count, - chars: total_char_count, - lines: total_line_count, - words: total_word_count, - max_line_length: total_longest_line_length, - }; - print_stats(settings, &result, max_width); + if error_count == 0 { + Ok(()) + } else { + Err(error_count) + } +} + +fn print_stats( + settings: &Settings, + result: &TitledWordCount, + mut min_width: usize, +) -> WcResult<()> { + let stdout = io::stdout(); + let mut stdout_lock = stdout.lock(); + + if settings.number_enabled() <= 1 { + // Prevent a leading space in case we only need to display a single + // number. + min_width = 0; + } + + let mut is_first: bool = true; + + if settings.show_lines { + if !is_first { + write!(stdout_lock, " ")?; + } + write!(stdout_lock, "{:1$}", result.count.lines, min_width)?; + is_first = false; + } + if settings.show_words { + if !is_first { + write!(stdout_lock, " ")?; + } + write!(stdout_lock, "{:1$}", result.count.words, min_width)?; + is_first = false; + } + if settings.show_bytes { + if !is_first { + write!(stdout_lock, " ")?; + } + write!(stdout_lock, "{:1$}", result.count.bytes, min_width)?; + is_first = false; + } + if settings.show_chars { + if !is_first { + write!(stdout_lock, " ")?; + } + write!(stdout_lock, "{:1$}", result.count.chars, min_width)?; + is_first = false; + } + if settings.show_max_line_length { + if !is_first { + write!(stdout_lock, " ")?; + } + write!( + stdout_lock, + "{:1$}", + result.count.max_line_length, min_width + )?; + } + + if let Some(title) = result.title { + writeln!(stdout_lock, " {}", title)?; + } else { + writeln!(stdout_lock)?; } Ok(()) } - -fn print_stats(settings: &Settings, result: &Result, max_width: usize) { - if settings.show_lines { - print!("{:1$}", result.lines, max_width); - } - if settings.show_words { - print!("{:1$}", result.words, max_width); - } - if settings.show_bytes { - print!("{:1$}", result.bytes, max_width); - } - if settings.show_chars { - print!("{:1$}", result.chars, max_width); - } - if settings.show_max_line_length { - print!("{:1$}", result.max_line_length, max_width); - } - - if result.title != "-" { - println!(" {}", result.title); - } else { - println!(); - } -} - -fn open(path: &str) -> StdResult>, i32> { - if "-" == path { - let reader = Box::new(stdin()) as Box; - return Ok(BufReader::new(reader)); - } - - let fpath = Path::new(path); - if fpath.is_dir() { - show_info!("{}: is a directory", path); - } - match File::open(&fpath) { - Ok(fd) => { - let reader = Box::new(fd) as Box; - Ok(BufReader::new(reader)) - } - Err(e) => { - show_error!("wc: {}: {}", path, e); - Err(1) - } - } -} diff --git a/src/uu/wc/src/word_count.rs b/src/uu/wc/src/word_count.rs new file mode 100644 index 000000000..bdb510f58 --- /dev/null +++ b/src/uu/wc/src/word_count.rs @@ -0,0 +1,131 @@ +use std::cmp::max; +use std::iter::Sum; +use std::ops::{Add, AddAssign}; +use std::str::from_utf8; + +const CR: u8 = b'\r'; +const LF: u8 = b'\n'; +const SPACE: u8 = b' '; +const TAB: u8 = b'\t'; +const SYN: u8 = 0x16_u8; +const FF: u8 = 0x0C_u8; + +#[inline(always)] +fn is_word_separator(byte: u8) -> bool { + byte == SPACE || byte == TAB || byte == CR || byte == SYN || byte == FF +} + +#[derive(Debug, Default, Copy, Clone)] +pub struct WordCount { + pub bytes: usize, + pub chars: usize, + pub lines: usize, + pub words: usize, + pub max_line_length: usize, +} + +impl Add for WordCount { + type Output = Self; + + fn add(self, other: Self) -> Self { + Self { + bytes: self.bytes + other.bytes, + chars: self.chars + other.chars, + lines: self.lines + other.lines, + words: self.words + other.words, + max_line_length: max(self.max_line_length, other.max_line_length), + } + } +} + +impl AddAssign for WordCount { + fn add_assign(&mut self, other: Self) { + *self = *self + other + } +} + +impl Sum for WordCount { + fn sum(iter: I) -> WordCount + where + I: Iterator, + { + iter.fold(WordCount::default(), |acc, x| acc + x) + } +} + +impl WordCount { + /// Count the characters and whitespace-separated words in the given bytes. + /// + /// `line` is a slice of bytes that will be decoded as ASCII characters. + fn ascii_word_and_char_count(line: &[u8]) -> (usize, usize) { + let word_count = line.split(|&x| is_word_separator(x)).count(); + let char_count = line.iter().filter(|c| c.is_ascii()).count(); + (word_count, char_count) + } + + /// Create a [`WordCount`] from a sequence of bytes representing a line. + /// + /// If the last byte of `line` encodes a newline character (`\n`), + /// then the [`lines`] field will be set to 1. Otherwise, it will + /// be set to 0. The [`bytes`] field is simply the length of + /// `line`. + /// + /// If `decode_chars` is `false`, the [`chars`] and [`words`] + /// fields will be set to 0. If it is `true`, this function will + /// attempt to decode the bytes first as UTF-8, and failing that, + /// as ASCII. + pub fn from_line(line: &[u8], decode_chars: bool) -> WordCount { + // GNU 'wc' only counts lines that end in LF as lines + let lines = (*line.last().unwrap() == LF) as usize; + let bytes = line.len(); + let (words, chars) = if decode_chars { + WordCount::word_and_char_count(line) + } else { + (0, 0) + }; + // -L is a GNU 'wc' extension so same behavior on LF + let max_line_length = if chars > 0 { chars - lines } else { 0 }; + WordCount { + bytes, + chars, + lines, + words, + max_line_length, + } + } + + /// Count the UTF-8 characters and words in the given string slice. + /// + /// `s` is a string slice that is assumed to be a UTF-8 string. + fn utf8_word_and_char_count(s: &str) -> (usize, usize) { + let word_count = s.split_whitespace().count(); + let char_count = s.chars().count(); + (word_count, char_count) + } + + pub fn with_title(self, title: Option<&str>) -> TitledWordCount { + TitledWordCount { title, count: self } + } + + /// Count the characters and words in the given slice of bytes. + /// + /// `line` is a slice of bytes that will be decoded as UTF-8 + /// characters, or if that fails, as ASCII characters. + fn word_and_char_count(line: &[u8]) -> (usize, usize) { + // try and convert the bytes to UTF-8 first + match from_utf8(line) { + Ok(s) => WordCount::utf8_word_and_char_count(s), + Err(..) => WordCount::ascii_word_and_char_count(line), + } + } +} + +/// This struct supplements the actual word count with an optional title that is +/// displayed to the user at the end of the program. +/// The reason we don't simply include title in the `WordCount` struct is that +/// it would result in unnecessary copying of `String`. +#[derive(Debug, Default, Clone)] +pub struct TitledWordCount<'a> { + pub title: Option<&'a str>, + pub count: WordCount, +} diff --git a/src/uu/who/Cargo.toml b/src/uu/who/Cargo.toml index fce0178aa..4d8eccb45 100644 --- a/src/uu/who/Cargo.toml +++ b/src/uu/who/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_who" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "who ~ (uutils) display information about currently logged-in users" @@ -15,8 +15,9 @@ edition = "2018" path = "src/who.rs" [dependencies] -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["utmpx"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["utmpx"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } +clap = "2.33.3" [[bin]] name = "who" diff --git a/src/uu/who/src/main.rs b/src/uu/who/src/main.rs index bc0015a80..a093201a1 100644 --- a/src/uu/who/src/main.rs +++ b/src/uu/who/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_who); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_who); diff --git a/src/uu/who/src/who.rs b/src/uu/who/src/who.rs index b028be0a0..6a9c88710 100644 --- a/src/uu/who/src/who.rs +++ b/src/uu/who/src/who.rs @@ -12,204 +12,128 @@ extern crate uucore; use uucore::libc::{ttyname, STDIN_FILENO, S_IWGRP}; use uucore::utmpx::{self, time, Utmpx}; +use clap::{crate_version, App, Arg}; use std::borrow::Cow; use std::ffi::CStr; use std::os::unix::fs::MetadataExt; use std::path::PathBuf; +use uucore::InvalidEncodingHandling; -static SYNTAX: &str = "[OPTION]... [ FILE | ARG1 ARG2 ]"; -static SUMMARY: &str = "Print information about users who are currently logged in."; -static LONG_HELP: &str = " - -a, --all same as -b -d --login -p -r -t -T -u - -b, --boot time of last system boot - -d, --dead print dead processes - -H, --heading print line of column headings - -l, --login print system login processes - --lookup attempt to canonicalize hostnames via DNS - -m only hostname and user associated with stdin - -p, --process print active processes spawned by init - -q, --count all login names and number of users logged on - -r, --runlevel print current runlevel (not available on BSDs) - -s, --short print only name, line, and time (default) - -t, --time print last system clock change - -T, -w, --mesg add user's message status as +, - or ? - -u, --users list users logged in - --message same as -T - --writable same as -T - --help display this help and exit - --version output version information and exit +mod options { + pub const ALL: &str = "all"; + pub const BOOT: &str = "boot"; + pub const DEAD: &str = "dead"; + pub const HEADING: &str = "heading"; + pub const LOGIN: &str = "login"; + pub const LOOKUP: &str = "lookup"; + pub const ONLY_HOSTNAME_USER: &str = "only_hostname_user"; + pub const PROCESS: &str = "process"; + pub const COUNT: &str = "count"; + pub const RUNLEVEL: &str = "runlevel"; + pub const SHORT: &str = "short"; + pub const TIME: &str = "time"; + pub const USERS: &str = "users"; + pub const MESG: &str = "mesg"; // aliases: --message, --writable + pub const FILE: &str = "FILE"; // if length=1: FILE, if length=2: ARG1 ARG2 +} -If FILE is not specified, use /var/run/utmp. /var/log/wtmp as FILE is common. -If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual. -"; +static ABOUT: &str = "Print information about users who are currently logged in."; + +#[cfg(any(target_os = "linux"))] +static RUNLEVEL_HELP: &str = "print current runlevel"; +#[cfg(not(target_os = "linux"))] +static RUNLEVEL_HELP: &str = "print current runlevel (This is meaningless on non Linux)"; + +fn get_usage() -> String { + format!("{0} [OPTION]... [ FILE | ARG1 ARG2 ]", executable!()) +} + +fn get_long_usage() -> String { + format!( + "If FILE is not specified, use {}. /var/log/wtmp as FILE is common.\n\ + If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual.", + utmpx::DEFAULT_FILE, + ) +} pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); + let args = args + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); - let mut opts = app!(SYNTAX, SUMMARY, LONG_HELP); - opts.optflag("a", "all", "same as -b -d --login -p -r -t -T -u"); - opts.optflag("b", "boot", "time of last system boot"); - opts.optflag("d", "dead", "print dead processes"); - opts.optflag("H", "heading", "print line of column headings"); - opts.optflag("l", "login", "print system login processes"); - opts.optflag("", "lookup", "attempt to canonicalize hostnames via DNS"); - opts.optflag("m", "", "only hostname and user associated with stdin"); - opts.optflag("p", "process", "print active processes spawned by init"); - opts.optflag( - "q", - "count", - "all login names and number of users logged on", - ); - #[cfg(any( - target_os = "macos", - target_os = "ios", - target_os = "linux", - target_os = "android" - ))] - opts.optflag("r", "runlevel", "print current runlevel"); - opts.optflag("s", "short", "print only name, line, and time (default)"); - opts.optflag("t", "time", "print last system clock change"); - opts.optflag("u", "users", "list users logged in"); - opts.optflag("w", "mesg", "add user's message status as +, - or ?"); - // --message, --writable are the same as --mesg - opts.optflag("T", "message", ""); - opts.optflag("T", "writable", ""); + let usage = get_usage(); + let after_help = get_long_usage(); - opts.optflag("", "help", "display this help and exit"); - opts.optflag("", "version", "output version information and exit"); + let matches = uu_app() + .usage(&usage[..]) + .after_help(&after_help[..]) + .get_matches_from(args); - let matches = opts.parse(args); + let files: Vec = matches + .values_of(options::FILE) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); // If true, attempt to canonicalize hostnames via a DNS lookup. - let do_lookup = matches.opt_present("lookup"); + let do_lookup = matches.is_present(options::LOOKUP); // If true, display only a list of usernames and count of // the users logged on. // Ignored for 'who am i'. - let short_list = matches.opt_present("q"); + let short_list = matches.is_present(options::COUNT); - // If true, display only name, line, and time fields. - let mut short_output = false; + let all = matches.is_present(options::ALL); + + // If true, display a line at the top describing each field. + let include_heading = matches.is_present(options::HEADING); + + // If true, display a '+' for each user if mesg y, a '-' if mesg n, + // or a '?' if their tty cannot be statted. + let include_mesg = all || matches.is_present(options::MESG) || matches.is_present("w"); + + // If true, display the last boot time. + let need_boottime = all || matches.is_present(options::BOOT); + + // If true, display dead processes. + let need_deadprocs = all || matches.is_present(options::DEAD); + + // If true, display processes waiting for user login. + let need_login = all || matches.is_present(options::LOGIN); + + // If true, display processes started by init. + let need_initspawn = all || matches.is_present(options::PROCESS); + + // If true, display the last clock change. + let need_clockchange = all || matches.is_present(options::TIME); + + // If true, display the current runlevel. + let need_runlevel = all || matches.is_present(options::RUNLEVEL); + + let use_defaults = !(all + || need_boottime + || need_deadprocs + || need_login + || need_initspawn + || need_runlevel + || need_clockchange + || matches.is_present(options::USERS)); + + // If true, display user processes. + let need_users = all || matches.is_present(options::USERS) || use_defaults; // If true, display the hours:minutes since each user has touched // the keyboard, or "." if within the last minute, or "old" if // not within the last day. - let mut include_idle = false; - - // If true, display a line at the top describing each field. - let include_heading = matches.opt_present("H"); - - // If true, display a '+' for each user if mesg y, a '-' if mesg n, - // or a '?' if their tty cannot be statted. - let include_mesg = - matches.opt_present("a") || matches.opt_present("T") || matches.opt_present("w"); + let include_idle = need_deadprocs || need_login || need_runlevel || need_users; // If true, display process termination & exit status. - let mut include_exit = false; + let include_exit = need_deadprocs; - // If true, display the last boot time. - let mut need_boottime = false; - - // If true, display dead processes. - let mut need_deadprocs = false; - - // If true, display processes waiting for user login. - let mut need_login = false; - - // If true, display processes started by init. - let mut need_initspawn = false; - - // If true, display the last clock change. - let mut need_clockchange = false; - - // If true, display the current runlevel. - let mut need_runlevel = false; - - // If true, display user processes. - let mut need_users = false; + // If true, display only name, line, and time fields. + let short_output = !include_exit && use_defaults; // If true, display info only for the controlling tty. - let mut my_line_only = false; - - let mut assumptions = true; - - #[allow(clippy::useless_let_if_seq)] - { - if matches.opt_present("a") { - need_boottime = true; - need_deadprocs = true; - need_login = true; - need_initspawn = true; - need_runlevel = true; - need_clockchange = true; - need_users = true; - include_idle = true; - include_exit = true; - assumptions = false; - } - - if matches.opt_present("b") { - need_boottime = true; - assumptions = false; - } - - if matches.opt_present("d") { - need_deadprocs = true; - include_idle = true; - include_exit = true; - assumptions = false; - } - - if matches.opt_present("l") { - need_login = true; - include_idle = true; - assumptions = false; - } - - if matches.opt_present("m") || matches.free.len() == 2 { - my_line_only = true; - } - - if matches.opt_present("p") { - need_initspawn = true; - assumptions = false; - } - - if matches.opt_present("r") { - need_runlevel = true; - include_idle = true; - assumptions = false; - } - - if matches.opt_present("s") { - short_output = true; - } - - if matches.opt_present("t") { - need_clockchange = true; - assumptions = false; - } - - if matches.opt_present("u") { - need_users = true; - include_idle = true; - assumptions = false; - } - - if assumptions { - need_users = true; - short_output = true; - } - - if include_exit { - short_output = false; - } - - if matches.free.len() > 2 { - show_usage_error!("{}", msg_wrong_number_of_arguments!()); - exit!(1); - } - } + let my_line_only = matches.is_present(options::ONLY_HOSTNAME_USER) || files.len() == 2; let mut who = Who { do_lookup, @@ -227,8 +151,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { need_runlevel, need_users, my_line_only, - has_records: false, - args: matches.free, + args: files, }; who.exec(); @@ -236,6 +159,107 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } +pub fn uu_app() -> App<'static, 'static> { + App::new(executable!()) + .version(crate_version!()) + .about(ABOUT) + .arg( + Arg::with_name(options::ALL) + .long(options::ALL) + .short("a") + .help("same as -b -d --login -p -r -t -T -u"), + ) + .arg( + Arg::with_name(options::BOOT) + .long(options::BOOT) + .short("b") + .help("time of last system boot"), + ) + .arg( + Arg::with_name(options::DEAD) + .long(options::DEAD) + .short("d") + .help("print dead processes"), + ) + .arg( + Arg::with_name(options::HEADING) + .long(options::HEADING) + .short("H") + .help("print line of column headings"), + ) + .arg( + Arg::with_name(options::LOGIN) + .long(options::LOGIN) + .short("l") + .help("print system login processes"), + ) + .arg( + Arg::with_name(options::LOOKUP) + .long(options::LOOKUP) + .help("attempt to canonicalize hostnames via DNS"), + ) + .arg( + Arg::with_name(options::ONLY_HOSTNAME_USER) + .short("m") + .help("only hostname and user associated with stdin"), + ) + .arg( + Arg::with_name(options::PROCESS) + .long(options::PROCESS) + .short("p") + .help("print active processes spawned by init"), + ) + .arg( + Arg::with_name(options::COUNT) + .long(options::COUNT) + .short("q") + .help("all login names and number of users logged on"), + ) + .arg( + Arg::with_name(options::RUNLEVEL) + .long(options::RUNLEVEL) + .short("r") + .help(RUNLEVEL_HELP), + ) + .arg( + Arg::with_name(options::SHORT) + .long(options::SHORT) + .short("s") + .help("print only name, line, and time (default)"), + ) + .arg( + Arg::with_name(options::TIME) + .long(options::TIME) + .short("t") + .help("print last system clock change"), + ) + .arg( + Arg::with_name(options::USERS) + .long(options::USERS) + .short("u") + .help("list users logged in"), + ) + .arg( + Arg::with_name(options::MESG) + .long(options::MESG) + .short("T") + // .visible_short_alias('w') // TODO: requires clap "3.0.0-beta.2" + .visible_aliases(&["message", "writable"]) + .help("add user's message status as +, - or ?"), + ) + .arg( + Arg::with_name("w") // work around for `Arg::visible_short_alias` + .short("w") + .help("same as -T"), + ) + .arg( + Arg::with_name(options::FILE) + .takes_value(true) + .min_values(1) + .max_values(2), + ) +} + struct Who { do_lookup: bool, short_list: bool, @@ -252,7 +276,6 @@ struct Who { need_runlevel: bool, need_users: bool, my_line_only: bool, - has_records: bool, args: Vec, } @@ -281,7 +304,7 @@ fn idle_string<'a>(when: i64, boottime: i64) -> Cow<'a, str> { } fn time_string(ut: &Utmpx) -> String { - time::strftime("%Y-%m-%d %H:%M", &ut.login_time()).unwrap() + time::strftime("%b %e %H:%M", &ut.login_time()).unwrap() // LC_ALL=C } #[inline] @@ -301,20 +324,12 @@ fn current_tty() -> String { impl Who { fn exec(&mut self) { - let run_level_chk = |record: i16| { - #[allow(unused_assignments)] - let mut res = false; + let run_level_chk = |_record: i16| { + #[cfg(not(target_os = "linux"))] + return false; - #[cfg(any( - target_os = "macos", - target_os = "ios", - target_os = "linux", - target_os = "android" - ))] - { - res = record == utmpx::RUN_LVL; - } - res + #[cfg(target_os = "linux")] + return _record == utmpx::RUN_LVL; }; let f = if self.args.len() == 1 { @@ -331,8 +346,7 @@ impl Who { println!("{}", users.join(" ")); println!("# users={}", users.len()); } else { - let mut records = Utmpx::iter_all_records().read_from(f).peekable(); - self.has_records = records.peek().is_some(); + let records = Utmpx::iter_all_records().read_from(f).peekable(); if self.include_heading { self.print_heading() @@ -348,7 +362,9 @@ impl Who { if self.need_users && ut.is_user_process() { self.print_user(&ut); } else if self.need_runlevel && run_level_chk(ut.record_type()) { - self.print_runlevel(&ut); + if cfg!(target_os = "linux") { + self.print_runlevel(&ut); + } } else if self.need_boottime && ut.record_type() == utmpx::BOOT_TIME { self.print_boottime(&ut); } else if self.need_clockchange && ut.record_type() == utmpx::NEW_TIME { @@ -472,20 +488,11 @@ impl Who { " ?".into() }; - let mut buf = vec![]; - let ut_host = ut.host(); - let mut res = ut_host.splitn(2, ':'); - if let Some(h) = res.next() { - if self.do_lookup { - buf.push(ut.canon_host().unwrap_or_else(|_| h.to_owned())); - } else { - buf.push(h.to_owned()); - } - } - if let Some(h) = res.next() { - buf.push(h.to_owned()); - } - let s = buf.join(":"); + let s = if self.do_lookup { + safe_unwrap!(ut.canon_host()) + } else { + ut.host() + }; let hoststr = if s.is_empty() { s } else { format!("({})", s) }; self.print_line( @@ -520,8 +527,8 @@ impl Who { buf.push_str(&msg); } buf.push_str(&format!(" {:<12}", line)); - // "%Y-%m-%d %H:%M" - let time_size = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2; + // "%b %e %H:%M" (LC_ALL=C) + let time_size = 3 + 2 + 2 + 1 + 2; buf.push_str(&format!(" {:<1$}", time, time_size)); if !self.short_output { diff --git a/src/uu/whoami/Cargo.toml b/src/uu/whoami/Cargo.toml index a3158f62a..28670c8b5 100644 --- a/src/uu/whoami/Cargo.toml +++ b/src/uu/whoami/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_whoami" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "whoami ~ (uutils) display user name of current effective user ID" @@ -16,11 +16,10 @@ path = "src/whoami.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["entries", "wide"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "wide"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [target.'cfg(target_os = "windows")'.dependencies] -advapi32-sys = "0.2.0" winapi = { version = "0.3", features = ["lmcons"] } [[bin]] diff --git a/src/uu/whoami/src/main.rs b/src/uu/whoami/src/main.rs index 0439923ee..40de26564 100644 --- a/src/uu/whoami/src/main.rs +++ b/src/uu/whoami/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_whoami); // spell-checker:ignore procs uucore whoami +uucore_procs::main!(uu_whoami); diff --git a/src/uu/whoami/src/platform/mod.rs b/src/uu/whoami/src/platform/mod.rs index ab0b856e6..b5064a8d2 100644 --- a/src/uu/whoami/src/platform/mod.rs +++ b/src/uu/whoami/src/platform/mod.rs @@ -10,10 +10,10 @@ // spell-checker:ignore (ToDO) getusername #[cfg(unix)] -pub use self::unix::getusername; +pub use self::unix::get_username; #[cfg(windows)] -pub use self::windows::getusername; +pub use self::windows::get_username; #[cfg(unix)] mod unix; diff --git a/src/uu/whoami/src/platform/unix.rs b/src/uu/whoami/src/platform/unix.rs index 33bfa6025..3d5fc6f54 100644 --- a/src/uu/whoami/src/platform/unix.rs +++ b/src/uu/whoami/src/platform/unix.rs @@ -14,7 +14,7 @@ use std::io::Result; use uucore::entries::uid2usr; use uucore::libc::geteuid; -pub unsafe fn getusername() -> Result { +pub unsafe fn get_username() -> Result { // Get effective user id let uid = geteuid(); uid2usr(uid) diff --git a/src/uu/whoami/src/platform/windows.rs b/src/uu/whoami/src/platform/windows.rs index 1d65281bd..3fe8eb1e7 100644 --- a/src/uu/whoami/src/platform/windows.rs +++ b/src/uu/whoami/src/platform/windows.rs @@ -7,22 +7,20 @@ * file that was distributed with this source code. */ -// spell-checker:ignore (ToDO) advapi lmcons winnt getusername WCHAR UNLEN - extern crate winapi; use self::winapi::shared::lmcons; use self::winapi::shared::minwindef; -use self::winapi::um::winnt; +use self::winapi::um::{winbase, winnt}; use std::io::{Error, Result}; use std::mem; use uucore::wide::FromWide; -pub unsafe fn getusername() -> Result { +pub unsafe fn get_username() -> Result { #[allow(deprecated)] let mut buffer: [winnt::WCHAR; lmcons::UNLEN as usize + 1] = mem::uninitialized(); let mut len = buffer.len() as minwindef::DWORD; - if advapi32::GetUserNameW(buffer.as_mut_ptr(), &mut len) == 0 { + if winbase::GetUserNameW(buffer.as_mut_ptr(), &mut len) == 0 { return Err(Error::last_os_error()); } let username = String::from_wide(&buffer[..len as usize - 1]); diff --git a/src/uu/whoami/src/whoami.rs b/src/uu/whoami/src/whoami.rs index 21e170dec..bd2eea1e3 100644 --- a/src/uu/whoami/src/whoami.rs +++ b/src/uu/whoami/src/whoami.rs @@ -1,3 +1,5 @@ +use clap::App; + // * This file is part of the uutils coreutils package. // * // * (c) Jordi Boggiano @@ -7,8 +9,6 @@ /* last synced with: whoami (GNU coreutils) 8.21 */ -// spell-checker:ignore (ToDO) getusername - #[macro_use] extern crate clap; #[macro_use] @@ -17,7 +17,7 @@ extern crate uucore; mod platform; pub fn uumain(args: impl uucore::Args) -> i32 { - let app = app_from_crate!(); + let app = uu_app(); if let Err(err) = app.get_matches_from_safe(args) { if err.kind == clap::ErrorKind::HelpDisplayed @@ -36,9 +36,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } +pub fn uu_app() -> App<'static, 'static> { + app_from_crate!() +} + pub fn exec() { unsafe { - match platform::getusername() { + match platform::get_username() { Ok(username) => println!("{}", username), Err(err) => match err.raw_os_error() { Some(0) | None => crash!(1, "failed to get username"), diff --git a/src/uu/yes/Cargo.toml b/src/uu/yes/Cargo.toml index 729ce693a..4a843ddd8 100644 --- a/src/uu/yes/Cargo.toml +++ b/src/uu/yes/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu_yes" -version = "0.0.4" +version = "0.0.6" authors = ["uutils developers"] license = "MIT" description = "yes ~ (uutils) repeatedly display a line with STRING (or 'y')" @@ -16,7 +16,7 @@ path = "src/yes.rs" [dependencies] clap = "2.33" -uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["zero-copy"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["zero-copy"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [features] diff --git a/src/uu/yes/src/main.rs b/src/uu/yes/src/main.rs index 597eb5b57..dc5bf6a0c 100644 --- a/src/uu/yes/src/main.rs +++ b/src/uu/yes/src/main.rs @@ -1 +1 @@ -uucore_procs::main!(uu_yes); // spell-checker:ignore procs uucore +uucore_procs::main!(uu_yes); diff --git a/src/uu/yes/src/yes.rs b/src/uu/yes/src/yes.rs index 1fc2d92bc..2c0d43000 100644 --- a/src/uu/yes/src/yes.rs +++ b/src/uu/yes/src/yes.rs @@ -12,7 +12,7 @@ extern crate clap; #[macro_use] extern crate uucore; -use clap::Arg; +use clap::{App, Arg}; use std::borrow::Cow; use std::io::{self, Write}; use uucore::zero_copy::ZeroCopyWriter; @@ -22,7 +22,7 @@ use uucore::zero_copy::ZeroCopyWriter; const BUF_SIZE: usize = 16 * 1024; pub fn uumain(args: impl uucore::Args) -> i32 { - let app = app_from_crate!().arg(Arg::with_name("STRING").index(1).multiple(true)); + let app = uu_app(); let matches = match app.get_matches_from_safe(args) { Ok(m) => m, @@ -56,6 +56,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { 0 } +pub fn uu_app() -> App<'static, 'static> { + app_from_crate!().arg(Arg::with_name("STRING").index(1).multiple(true)) +} + #[cfg(not(feature = "latency"))] fn prepare_buffer<'a>(input: &'a str, buffer: &'a mut [u8; BUF_SIZE]) -> &'a [u8] { if input.len() < BUF_SIZE / 2 { diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index a5fbe4c79..0c11d2c15 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uucore" -version = "0.0.7" +version = "0.0.8" authors = ["uutils developers"] license = "MIT" description = "uutils ~ 'core' uutils code library (cross-platform)" @@ -16,6 +16,7 @@ edition = "2018" path="src/lib/lib.rs" [dependencies] +dns-lookup = "1.0.5" dunce = "1.0.0" getopts = "<= 0.2.21" wild = "2.0.4" @@ -24,11 +25,14 @@ thiserror = { version="1.0", optional=true } lazy_static = { version="1.3", optional=true } nix = { version="<= 0.13", optional=true } platform-info = { version="<= 0.1", optional=true } -time = { version="<= 0.1.42", optional=true } +time = { version="<= 0.1.43", optional=true } # * "problem" dependencies (pinned) data-encoding = { version="~2.1", optional=true } ## data-encoding: require v2.1; but v2.2.0 breaks the build for MinSRV v1.31.0 libc = { version="0.2.15, <= 0.2.85", optional=true } ## libc: initial utmp support added in v0.2.15; but v0.2.68 breaks the build for MinSRV v1.31.0 +[target.'cfg(target_os = "windows")'.dependencies] +winapi = { version = "0.3", features = ["errhandlingapi", "fileapi", "handleapi", "winerror"] } + [target.'cfg(target_os = "redox")'.dependencies] termion = "1.5" @@ -38,10 +42,11 @@ default = [] encoding = ["data-encoding", "thiserror"] entries = ["libc"] fs = ["libc"] +fsext = ["libc", "time"] mode = ["libc"] -parse_time = [] perms = ["libc"] process = ["libc"] +ringbuffer = [] signals = [] utf8 = [] utmpx = ["time", "libc"] diff --git a/src/uucore/src/lib/features.rs b/src/uucore/src/lib/features.rs index c26225cb7..c1e1ec31e 100644 --- a/src/uucore/src/lib/features.rs +++ b/src/uucore/src/lib/features.rs @@ -4,8 +4,10 @@ pub mod encoding; #[cfg(feature = "fs")] pub mod fs; -#[cfg(feature = "parse_time")] -pub mod parse_time; +#[cfg(feature = "fsext")] +pub mod fsext; +#[cfg(feature = "ringbuffer")] +pub mod ringbuffer; #[cfg(feature = "zero-copy")] pub mod zero_copy; diff --git a/src/uucore/src/lib/features/entries.rs b/src/uucore/src/lib/features/entries.rs index a921af2d0..6b986e616 100644 --- a/src/uucore/src/lib/features/entries.rs +++ b/src/uucore/src/lib/features/entries.rs @@ -5,7 +5,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (vars) Passwd cstr fnam gecos ngroups +// spell-checker:ignore (vars) Passwd cstr fnam gecos ngroups egid //! Get password/group file entry //! @@ -34,7 +34,7 @@ //! assert!(entries::Group::locate(root_group).is_ok()); //! ``` -#[cfg(any(target_os = "freebsd", target_os = "macos"))] +#[cfg(any(target_os = "freebsd", target_vendor = "apple"))] use libc::time_t; use libc::{c_char, c_int, gid_t, uid_t}; use libc::{getgrgid, getgrnam, getgroups, getpwnam, getpwuid, group, passwd}; @@ -47,6 +47,9 @@ use std::io::Result as IOResult; use std::ptr; extern "C" { + /// From: https://man7.org/linux/man-pages/man3/getgrouplist.3.html + /// > The getgrouplist() function scans the group database to obtain + /// > the list of groups that user belongs to. fn getgrouplist( name: *const c_char, gid: gid_t, @@ -55,6 +58,13 @@ extern "C" { ) -> c_int; } +/// From: https://man7.org/linux/man-pages/man2/getgroups.2.html +/// > getgroups() returns the supplementary group IDs of the calling +/// > process in list. +/// > If size is zero, list is not modified, but the total number of +/// > supplementary group IDs for the process is returned. This allows +/// > the caller to determine the size of a dynamically allocated list +/// > to be used in a further call to getgroups(). pub fn get_groups() -> IOResult> { let ngroups = unsafe { getgroups(0, ptr::null_mut()) }; if ngroups == -1 { @@ -72,6 +82,46 @@ pub fn get_groups() -> IOResult> { } } +/// The list of group IDs returned from GNU's `groups` and GNU's `id --groups` +/// starts with the effective group ID (egid). +/// This is a wrapper for `get_groups()` to mimic this behavior. +/// +/// If `arg_id` is `None` (default), `get_groups_gnu` moves the effective +/// group id (egid) to the first entry in the returned Vector. +/// If `arg_id` is `Some(x)`, `get_groups_gnu` moves the id with value `x` +/// to the first entry in the returned Vector. This might be necessary +/// for `id --groups --real` if `gid` and `egid` are not equal. +/// +/// From: https://www.man7.org/linux/man-pages/man3/getgroups.3p.html +/// > As implied by the definition of supplementary groups, the +/// > effective group ID may appear in the array returned by +/// > getgroups() or it may be returned only by getegid(). Duplication +/// > may exist, but the application needs to call getegid() to be sure +/// > of getting all of the information. Various implementation +/// > variations and administrative sequences cause the set of groups +/// > appearing in the result of getgroups() to vary in order and as to +/// > whether the effective group ID is included, even when the set of +/// > groups is the same (in the mathematical sense of ``set''). (The +/// > history of a process and its parents could affect the details of +/// > the result.) +#[cfg(all(unix, feature = "process"))] +pub fn get_groups_gnu(arg_id: Option) -> IOResult> { + let groups = get_groups()?; + let egid = arg_id.unwrap_or_else(crate::features::process::getegid); + Ok(sort_groups(groups, egid)) +} + +#[cfg(all(unix, feature = "process"))] +fn sort_groups(mut groups: Vec, egid: gid_t) -> Vec { + if let Some(index) = groups.iter().position(|&x| x == egid) { + groups[..=index].rotate_right(1); + } else { + groups.insert(0, egid); + } + groups +} + +#[derive(Copy, Clone)] pub struct Passwd { inner: passwd, } @@ -119,19 +169,19 @@ impl Passwd { } /// AKA passwd.pw_class - #[cfg(any(target_os = "freebsd", target_os = "macos"))] + #[cfg(any(target_os = "freebsd", target_vendor = "apple"))] pub fn user_access_class(&self) -> Cow { cstr2cow!(self.inner.pw_class) } /// AKA passwd.pw_change - #[cfg(any(target_os = "freebsd", target_os = "macos"))] + #[cfg(any(target_os = "freebsd", target_vendor = "apple"))] pub fn passwd_change_time(&self) -> time_t { self.inner.pw_change } /// AKA passwd.pw_expire - #[cfg(any(target_os = "freebsd", target_os = "macos"))] + #[cfg(any(target_os = "freebsd", target_vendor = "apple"))] pub fn expiration(&self) -> time_t { self.inner.pw_expire } @@ -144,16 +194,38 @@ impl Passwd { self.inner } + /// This is a wrapper function for `libc::getgrouplist`. + /// + /// From: https://man7.org/linux/man-pages/man3/getgrouplist.3.html + /// > If the number of groups of which user is a member is less than or + /// > equal to *ngroups, then the value *ngroups is returned. + /// > If the user is a member of more than *ngroups groups, then + /// > getgrouplist() returns -1. In this case, the value returned in + /// > *ngroups can be used to resize the buffer passed to a further + /// > call getgrouplist(). + /// + /// However, on macOS/darwin (and maybe others?) `getgrouplist` does + /// not update `ngroups` if `ngroups` is too small. Therefore, if not + /// updated by `getgrouplist`, `ngroups` needs to be increased in a + /// loop until `getgrouplist` stops returning -1. pub fn belongs_to(&self) -> Vec { let mut ngroups: c_int = 8; + let mut ngroups_old: c_int; let mut groups = Vec::with_capacity(ngroups as usize); let gid = self.inner.pw_gid; let name = self.inner.pw_name; - unsafe { - if getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups) == -1 { + loop { + ngroups_old = ngroups; + if unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups) } == -1 { + if ngroups == ngroups_old { + ngroups *= 2; + } groups.resize(ngroups as usize, 0); - getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups); + } else { + break; } + } + unsafe { groups.set_len(ngroups as usize); } groups.truncate(ngroups as usize); @@ -268,3 +340,27 @@ pub fn usr2uid(name: &str) -> IOResult { pub fn grp2gid(name: &str) -> IOResult { Group::locate(name).map(|p| p.gid()) } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_sort_groups() { + assert_eq!(sort_groups(vec![1, 2, 3], 4), vec![4, 1, 2, 3]); + assert_eq!(sort_groups(vec![1, 2, 3], 3), vec![3, 1, 2]); + assert_eq!(sort_groups(vec![1, 2, 3], 2), vec![2, 1, 3]); + assert_eq!(sort_groups(vec![1, 2, 3], 1), vec![1, 2, 3]); + assert_eq!(sort_groups(vec![1, 2, 3], 0), vec![0, 1, 2, 3]); + } + + #[test] + fn test_entries_get_groups_gnu() { + if let Ok(mut groups) = get_groups() { + if let Some(last) = groups.pop() { + groups.insert(0, last); + assert_eq!(get_groups_gnu(Some(last)).unwrap(), groups); + } + } + } +} diff --git a/src/uucore/src/lib/features/fs.rs b/src/uucore/src/lib/features/fs.rs index adf4f6f82..36bdbfed0 100644 --- a/src/uucore/src/lib/features/fs.rs +++ b/src/uucore/src/lib/features/fs.rs @@ -8,8 +8,9 @@ #[cfg(unix)] use libc::{ - mode_t, S_IRGRP, S_IROTH, S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, - S_IXGRP, S_IXOTH, S_IXUSR, + mode_t, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, S_IRGRP, + S_IROTH, S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, + S_IXUSR, }; use std::borrow::Cow; use std::env; @@ -23,9 +24,10 @@ use std::os::unix::fs::MetadataExt; use std::path::{Component, Path, PathBuf}; #[cfg(unix)] +#[macro_export] macro_rules! has { ($mode:expr, $perm:expr) => { - $mode & ($perm as u32) != 0 + $mode & $perm != 0 }; } @@ -52,14 +54,53 @@ pub fn resolve_relative_path(path: &Path) -> Cow { result.into() } +/// Controls how symbolic links should be handled when canonicalizing a path. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum CanonicalizeMode { + /// Do not resolve any symbolic links. None, + + /// Resolve all symbolic links. Normal, + + /// Resolve symbolic links, ignoring errors on the final component. Existing, + + /// Resolve symbolic links, ignoring errors on the non-final components. Missing, } +// copied from https://github.com/rust-lang/cargo/blob/2e4cfc2b7d43328b207879228a2ca7d427d188bb/src/cargo/util/paths.rs#L65-L90 +// both projects are MIT https://github.com/rust-lang/cargo/blob/master/LICENSE-MIT +// for std impl progress see rfc https://github.com/rust-lang/rfcs/issues/2208 +// replace this once that lands +pub fn normalize_path(path: &Path) -> PathBuf { + let mut components = path.components().peekable(); + let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { + components.next(); + PathBuf::from(c.as_os_str()) + } else { + PathBuf::new() + }; + + for component in components { + match component { + Component::Prefix(..) => unreachable!(), + Component::RootDir => { + ret.push(component.as_os_str()); + } + Component::CurDir => {} + Component::ParentDir => { + ret.pop(); + } + Component::Normal(c) => { + ret.push(c); + } + } + } + ret +} + fn resolve>(original: P) -> IOResult { const MAX_LINKS_FOLLOWED: u32 = 255; let mut followed = 0; @@ -72,26 +113,36 @@ fn resolve>(original: P) -> IOResult { )); } - match fs::symlink_metadata(&result) { - Err(e) => return Err(e), - Ok(ref m) if !m.file_type().is_symlink() => break, - Ok(..) => { - followed += 1; - match fs::read_link(&result) { - Ok(path) => { - result.pop(); - result.push(path); - } - Err(e) => { - return Err(e); - } - } - } + if !fs::symlink_metadata(&result)?.file_type().is_symlink() { + break; } + + followed += 1; + let path = fs::read_link(&result)?; + result.pop(); + result.push(path); } Ok(result) } +/// Return the canonical, absolute form of a path. +/// +/// This function is a generalization of [`std::fs::canonicalize`] that +/// allows controlling how symbolic links are resolved and how to deal +/// with missing components. It returns the canonical, absolute form of +/// a path. The `can_mode` parameter controls how symbolic links are +/// resolved: +/// +/// * [`CanonicalizeMode::Normal`] makes this function behave like +/// [`std::fs::canonicalize`], resolving symbolic links and returning +/// an error if the path does not exist. +/// * [`CanonicalizeMode::Missing`] makes this function ignore non-final +/// components of the path that could not be resolved. +/// * [`CanonicalizeMode::Existing`] makes this function return an error +/// if the final component of the path does not exist. +/// * [`CanonicalizeMode::None`] makes this function not try to resolve +/// any symbolic links. +/// pub fn canonicalize>(original: P, can_mode: CanonicalizeMode) -> IOResult { // Create an absolute path let original = original.as_ref(); @@ -134,10 +185,8 @@ pub fn canonicalize>(original: P, can_mode: CanonicalizeMode) -> } match resolve(&result) { - Err(e) => match can_mode { - CanonicalizeMode::Missing => continue, - _ => return Err(e), - }, + Err(_) if can_mode == CanonicalizeMode::Missing => continue, + Err(e) => return Err(e), Ok(path) => { result.pop(); result.push(path); @@ -147,84 +196,62 @@ pub fn canonicalize>(original: P, can_mode: CanonicalizeMode) -> result.push(parts.last().unwrap()); + if can_mode == CanonicalizeMode::None { + return Ok(result); + } + match resolve(&result) { - Err(e) => { - if can_mode == CanonicalizeMode::Existing { - return Err(e); - } + Err(e) if can_mode == CanonicalizeMode::Existing => { + return Err(e); } Ok(path) => { result.pop(); result.push(path); } + Err(_) => (), } } Ok(result) } -#[cfg(unix)] -pub fn is_stdin_interactive() -> bool { - unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } -} - -#[cfg(windows)] -pub fn is_stdin_interactive() -> bool { - false -} - -#[cfg(target_os = "redox")] -pub fn is_stdin_interactive() -> bool { - termion::is_tty(&io::stdin()) -} - -#[cfg(unix)] -pub fn is_stdout_interactive() -> bool { - unsafe { libc::isatty(libc::STDOUT_FILENO) == 1 } -} - -#[cfg(windows)] -pub fn is_stdout_interactive() -> bool { - false -} - -#[cfg(target_os = "redox")] -pub fn is_stdout_interactive() -> bool { - termion::is_tty(&io::stdout()) -} - -#[cfg(unix)] -pub fn is_stderr_interactive() -> bool { - unsafe { libc::isatty(libc::STDERR_FILENO) == 1 } -} - -#[cfg(windows)] -pub fn is_stderr_interactive() -> bool { - false -} - -#[cfg(target_os = "redox")] -pub fn is_stderr_interactive() -> bool { - termion::is_tty(&io::stderr()) -} - #[cfg(not(unix))] #[allow(unused_variables)] -pub fn display_permissions(metadata: &fs::Metadata) -> String { +pub fn display_permissions(metadata: &fs::Metadata, display_file_type: bool) -> String { + if display_file_type { + return String::from("----------"); + } String::from("---------") } #[cfg(unix)] -pub fn display_permissions(metadata: &fs::Metadata) -> String { +pub fn display_permissions(metadata: &fs::Metadata, display_file_type: bool) -> String { let mode: mode_t = metadata.mode() as mode_t; - display_permissions_unix(mode as u32) + display_permissions_unix(mode, display_file_type) } #[cfg(unix)] -pub fn display_permissions_unix(mode: u32) -> String { - let mut result = String::with_capacity(9); +pub fn display_permissions_unix(mode: mode_t, display_file_type: bool) -> String { + let mut result; + if display_file_type { + result = String::with_capacity(10); + result.push(match mode & S_IFMT { + S_IFDIR => 'd', + S_IFCHR => 'c', + S_IFBLK => 'b', + S_IFREG => '-', + S_IFIFO => 'p', + S_IFLNK => 'l', + S_IFSOCK => 's', + // TODO: Other file types + _ => '?', + }); + } else { + result = String::with_capacity(9); + } + result.push(if has!(mode, S_IRUSR) { 'r' } else { '-' }); result.push(if has!(mode, S_IWUSR) { 'w' } else { '-' }); - result.push(if has!(mode, S_ISUID) { + result.push(if has!(mode, S_ISUID as mode_t) { if has!(mode, S_IXUSR) { 's' } else { @@ -238,7 +265,7 @@ pub fn display_permissions_unix(mode: u32) -> String { result.push(if has!(mode, S_IRGRP) { 'r' } else { '-' }); result.push(if has!(mode, S_IWGRP) { 'w' } else { '-' }); - result.push(if has!(mode, S_ISGID) { + result.push(if has!(mode, S_ISGID as mode_t) { if has!(mode, S_IXGRP) { 's' } else { @@ -252,7 +279,7 @@ pub fn display_permissions_unix(mode: u32) -> String { result.push(if has!(mode, S_IROTH) { 'r' } else { '-' }); result.push(if has!(mode, S_IWOTH) { 'w' } else { '-' }); - result.push(if has!(mode, S_ISVTX) { + result.push(if has!(mode, S_ISVTX as mode_t) { if has!(mode, S_IXOTH) { 't' } else { @@ -266,3 +293,116 @@ pub fn display_permissions_unix(mode: u32) -> String { result } + +#[cfg(test)] +mod tests { + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + struct NormalizePathTestCase<'a> { + path: &'a str, + test: &'a str, + } + + const NORMALIZE_PATH_TESTS: [NormalizePathTestCase; 8] = [ + NormalizePathTestCase { + path: "./foo/bar.txt", + test: "foo/bar.txt", + }, + NormalizePathTestCase { + path: "bar/../foo/bar.txt", + test: "foo/bar.txt", + }, + NormalizePathTestCase { + path: "foo///bar.txt", + test: "foo/bar.txt", + }, + NormalizePathTestCase { + path: "foo///bar", + test: "foo/bar", + }, + NormalizePathTestCase { + path: "foo//./bar", + test: "foo/bar", + }, + NormalizePathTestCase { + path: "/foo//./bar", + test: "/foo/bar", + }, + NormalizePathTestCase { + path: r"C:/you/later/", + test: "C:/you/later", + }, + NormalizePathTestCase { + path: "\\networkShare/a//foo//./bar", + test: "\\networkShare/a/foo/bar", + }, + ]; + + #[test] + fn test_normalize_path() { + for test in NORMALIZE_PATH_TESTS.iter() { + let path = Path::new(test.path); + let normalized = normalize_path(path); + assert_eq!( + test.test + .replace("/", std::path::MAIN_SEPARATOR.to_string().as_str()), + normalized.to_str().expect("Path is not valid utf-8!") + ); + } + } + + #[cfg(unix)] + #[test] + fn test_display_permissions() { + // spell-checker:ignore (perms) brwsr drwxr rwxr + assert_eq!( + "drwxr-xr-x", + display_permissions_unix(S_IFDIR | 0o755, true) + ); + assert_eq!( + "rwxr-xr-x", + display_permissions_unix(S_IFDIR | 0o755, false) + ); + assert_eq!( + "-rw-r--r--", + display_permissions_unix(S_IFREG | 0o644, true) + ); + assert_eq!( + "srw-r-----", + display_permissions_unix(S_IFSOCK | 0o640, true) + ); + assert_eq!( + "lrw-r-xr-x", + display_permissions_unix(S_IFLNK | 0o655, true) + ); + assert_eq!("?rw-r-xr-x", display_permissions_unix(0o655, true)); + + assert_eq!( + "brwSr-xr-x", + display_permissions_unix(S_IFBLK | S_ISUID as mode_t | 0o655, true) + ); + assert_eq!( + "brwsr-xr-x", + display_permissions_unix(S_IFBLK | S_ISUID as mode_t | 0o755, true) + ); + + assert_eq!( + "prw---sr--", + display_permissions_unix(S_IFIFO | S_ISGID as mode_t | 0o614, true) + ); + assert_eq!( + "prw---Sr--", + display_permissions_unix(S_IFIFO | S_ISGID as mode_t | 0o604, true) + ); + + assert_eq!( + "c---r-xr-t", + display_permissions_unix(S_IFCHR | S_ISVTX as mode_t | 0o055, true) + ); + assert_eq!( + "c---r-xr-T", + display_permissions_unix(S_IFCHR | S_ISVTX as mode_t | 0o054, true) + ); + } +} diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs new file mode 100644 index 000000000..d3eec468a --- /dev/null +++ b/src/uucore/src/lib/features/fsext.rs @@ -0,0 +1,832 @@ +// This file is part of the uutils coreutils package. +// +// (c) Jian Zeng +// (c) Fangxu Hu +// (c) Sylvestre Ledru +// +// For the full copyright and license information, please view the LICENSE file +// that was distributed with this source code. + +// spell-checker:ignore (arch) bitrig ; (fs) cifs smbfs + +extern crate time; + +pub use crate::*; // import macros from `../../macros.rs` + +#[cfg(target_os = "linux")] +const LINUX_MTAB: &str = "/etc/mtab"; +#[cfg(target_os = "linux")] +const LINUX_MOUNTINFO: &str = "/proc/self/mountinfo"; +static MOUNT_OPT_BIND: &str = "bind"; +#[cfg(windows)] +const MAX_PATH: usize = 266; +#[cfg(not(unix))] +static EXIT_ERR: i32 = 1; + +#[cfg(windows)] +use std::ffi::OsString; +#[cfg(windows)] +use std::os::windows::ffi::OsStrExt; +#[cfg(windows)] +use std::os::windows::ffi::OsStringExt; +#[cfg(windows)] +use winapi::shared::minwindef::DWORD; +#[cfg(windows)] +use winapi::um::errhandlingapi::GetLastError; +#[cfg(windows)] +use winapi::um::fileapi::GetDiskFreeSpaceW; +#[cfg(windows)] +use winapi::um::fileapi::{ + FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, GetDriveTypeW, GetVolumeInformationW, + GetVolumePathNamesForVolumeNameW, QueryDosDeviceW, +}; +#[cfg(windows)] +use winapi::um::handleapi::INVALID_HANDLE_VALUE; +#[cfg(windows)] +use winapi::um::winbase::DRIVE_REMOTE; + +#[cfg(windows)] +macro_rules! String2LPWSTR { + ($str: expr) => { + OsString::from($str.clone()) + .as_os_str() + .encode_wide() + .chain(Some(0)) + .collect::>() + .as_ptr() + }; +} + +#[cfg(windows)] +#[allow(non_snake_case)] +fn LPWSTR2String(buf: &[u16]) -> String { + let len = unsafe { libc::wcslen(buf.as_ptr()) }; + OsString::from_wide(&buf[..len as usize]) + .into_string() + .unwrap() +} + +use self::time::Timespec; +#[cfg(unix)] +use libc::{ + mode_t, strerror, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, +}; +use std::borrow::Cow; +use std::convert::{AsRef, From}; +#[cfg(unix)] +use std::ffi::CString; +#[cfg(unix)] +use std::io::Error as IOError; +#[cfg(unix)] +use std::mem; +use std::path::Path; +use std::time::UNIX_EPOCH; + +#[cfg(any( + target_os = "linux", + target_vendor = "apple", + target_os = "android", + target_os = "freebsd" +))] +pub use libc::statfs as StatFs; +#[cfg(any( + target_os = "openbsd", + target_os = "netbsd", + target_os = "openbsd", + target_os = "bitrig", + target_os = "dragonfly" +))] +pub use libc::statvfs as StatFs; + +#[cfg(any( + target_os = "linux", + target_vendor = "apple", + target_os = "android", + target_os = "freebsd" +))] +pub use libc::statfs as statfs_fn; +#[cfg(any( + target_os = "openbsd", + target_os = "netbsd", + target_os = "openbsd", + target_os = "bitrig", + target_os = "dragonfly" +))] +pub use libc::statvfs as statfs_fn; + +pub trait BirthTime { + fn pretty_birth(&self) -> String; + fn birth(&self) -> String; +} + +use std::fs::Metadata; +impl BirthTime for Metadata { + fn pretty_birth(&self) -> String { + self.created() + .ok() + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|e| pretty_time(e.as_secs() as i64, i64::from(e.subsec_nanos()))) + .unwrap_or_else(|| "-".to_owned()) + } + + fn birth(&self) -> String { + self.created() + .ok() + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|e| format!("{}", e.as_secs())) + .unwrap_or_else(|| "0".to_owned()) + } +} + +#[derive(Debug, Clone)] +pub struct MountInfo { + // it stores `volume_name` in windows platform and `dev_id` in unix platform + pub dev_id: String, + pub dev_name: String, + pub fs_type: String, + pub mount_dir: String, + pub mount_option: String, // we only care "bind" option + pub mount_root: String, + pub remote: bool, + pub dummy: bool, +} + +impl MountInfo { + fn set_missing_fields(&mut self) { + #[cfg(unix)] + { + // We want to keep the dev_id on Windows + // but set dev_id + let path = CString::new(self.mount_dir.clone()).unwrap(); + unsafe { + let mut stat = mem::zeroed(); + if libc::stat(path.as_ptr(), &mut stat) == 0 { + self.dev_id = (stat.st_dev as i32).to_string(); + } else { + self.dev_id = "".to_string(); + } + } + } + // set MountInfo::dummy + // spell-checker:disable + match self.fs_type.as_ref() { + "autofs" | "proc" | "subfs" + /* for Linux 2.6/3.x */ + | "debugfs" | "devpts" | "fusectl" | "mqueue" | "rpc_pipefs" | "sysfs" + /* FreeBSD, Linux 2.4 */ + | "devfs" + /* for NetBSD 3.0 */ + | "kernfs" + /* for Irix 6.5 */ + | "ignore" => self.dummy = true, + _ => self.dummy = self.fs_type == "none" + && !self.mount_option.contains(MOUNT_OPT_BIND) + } + // spell-checker:enable + // set MountInfo::remote + #[cfg(windows)] + { + self.remote = DRIVE_REMOTE == unsafe { GetDriveTypeW(String2LPWSTR!(self.mount_root)) }; + } + #[cfg(unix)] + { + if self.dev_name.find(':').is_some() + || (self.dev_name.starts_with("//") && self.fs_type == "smbfs" + || self.fs_type == "cifs") + || self.dev_name == "-hosts" + { + self.remote = true; + } else { + self.remote = false; + } + } + } + + #[cfg(target_os = "linux")] + fn new(file_name: &str, raw: Vec<&str>) -> Option { + match file_name { + // spell-checker:ignore (word) noatime + // Format: 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue + // "man proc" for more details + LINUX_MOUNTINFO => { + let mut m = MountInfo { + dev_id: "".to_string(), + dev_name: raw[9].to_string(), + fs_type: raw[8].to_string(), + mount_root: raw[3].to_string(), + mount_dir: raw[4].to_string(), + mount_option: raw[5].to_string(), + remote: false, + dummy: false, + }; + m.set_missing_fields(); + Some(m) + } + LINUX_MTAB => { + let mut m = MountInfo { + dev_id: "".to_string(), + dev_name: raw[0].to_string(), + fs_type: raw[2].to_string(), + mount_root: "".to_string(), + mount_dir: raw[1].to_string(), + mount_option: raw[3].to_string(), + remote: false, + dummy: false, + }; + m.set_missing_fields(); + Some(m) + } + _ => None, + } + } + #[cfg(windows)] + fn new(mut volume_name: String) -> Option { + let mut dev_name_buf = [0u16; MAX_PATH]; + volume_name.pop(); + unsafe { + QueryDosDeviceW( + OsString::from(volume_name.clone()) + .as_os_str() + .encode_wide() + .chain(Some(0)) + .skip(4) + .collect::>() + .as_ptr(), + dev_name_buf.as_mut_ptr(), + dev_name_buf.len() as DWORD, + ) + }; + volume_name.push('\\'); + let dev_name = LPWSTR2String(&dev_name_buf); + + let mut mount_root_buf = [0u16; MAX_PATH]; + let success = unsafe { + GetVolumePathNamesForVolumeNameW( + String2LPWSTR!(volume_name), + mount_root_buf.as_mut_ptr(), + mount_root_buf.len() as DWORD, + ptr::null_mut(), + ) + }; + if 0 == success { + // TODO: support the case when `GetLastError()` returns `ERROR_MORE_DATA` + return None; + } + let mount_root = LPWSTR2String(&mount_root_buf); + + let mut fs_type_buf = [0u16; MAX_PATH]; + let success = unsafe { + GetVolumeInformationW( + String2LPWSTR!(mount_root), + ptr::null_mut(), + 0, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + fs_type_buf.as_mut_ptr(), + fs_type_buf.len() as DWORD, + ) + }; + let fs_type = if 0 != success { + Some(LPWSTR2String(&fs_type_buf)) + } else { + None + }; + let mut mn_info = MountInfo { + dev_id: volume_name, + dev_name, + fs_type: fs_type.unwrap_or_else(|| "".to_string()), + mount_root, + mount_dir: "".to_string(), + mount_option: "".to_string(), + remote: false, + dummy: false, + }; + mn_info.set_missing_fields(); + Some(mn_info) + } +} + +#[cfg(any(target_vendor = "apple", target_os = "freebsd"))] +use std::ffi::CStr; +#[cfg(any(target_os = "freebsd", target_vendor = "apple"))] +impl From for MountInfo { + fn from(statfs: StatFs) -> Self { + let mut info = MountInfo { + dev_id: "".to_string(), + dev_name: unsafe { + // spell-checker:disable-next-line + CStr::from_ptr(&statfs.f_mntfromname[0]) + .to_string_lossy() + .into_owned() + }, + fs_type: unsafe { + // spell-checker:disable-next-line + CStr::from_ptr(&statfs.f_fstypename[0]) + .to_string_lossy() + .into_owned() + }, + mount_dir: unsafe { + // spell-checker:disable-next-line + CStr::from_ptr(&statfs.f_mntonname[0]) + .to_string_lossy() + .into_owned() + }, + mount_root: "".to_string(), + mount_option: "".to_string(), + remote: false, + dummy: false, + }; + info.set_missing_fields(); + info + } +} + +#[cfg(any(target_os = "freebsd", target_vendor = "apple"))] +use libc::c_int; +#[cfg(any(target_os = "freebsd", target_vendor = "apple"))] +extern "C" { + #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] + #[link_name = "getmntinfo$INODE64"] // spell-checker:disable-line + fn get_mount_info(mount_buffer_p: *mut *mut StatFs, flags: c_int) -> c_int; + + #[cfg(any( + all(target_os = "freebsd"), + all(target_vendor = "apple", target_arch = "aarch64") + ))] + #[link_name = "getmntinfo"] // spell-checker:disable-line + fn get_mount_info(mount_buffer_p: *mut *mut StatFs, flags: c_int) -> c_int; +} + +#[cfg(target_os = "linux")] +use std::fs::File; +#[cfg(target_os = "linux")] +use std::io::{BufRead, BufReader}; +#[cfg(any(target_vendor = "apple", target_os = "freebsd", target_os = "windows"))] +use std::ptr; +#[cfg(any(target_vendor = "apple", target_os = "freebsd"))] +use std::slice; +/// Read file system list. +pub fn read_fs_list() -> Vec { + #[cfg(target_os = "linux")] + { + let (file_name, f) = File::open(LINUX_MOUNTINFO) + .map(|f| (LINUX_MOUNTINFO, f)) + .or_else(|_| File::open(LINUX_MTAB).map(|f| (LINUX_MTAB, f))) + .expect("failed to find mount list files"); + let reader = BufReader::new(f); + reader + .lines() + .filter_map(|line| line.ok()) + .filter_map(|line| { + let raw_data = line.split_whitespace().collect::>(); + MountInfo::new(file_name, raw_data) + }) + .collect::>() + } + #[cfg(any(target_os = "freebsd", target_vendor = "apple"))] + { + let mut mount_buffer_ptr: *mut StatFs = ptr::null_mut(); + let len = unsafe { get_mount_info(&mut mount_buffer_ptr, 1_i32) }; + if len < 0 { + crash!(1, "get_mount_info() failed"); + } + let mounts = unsafe { slice::from_raw_parts(mount_buffer_ptr, len as usize) }; + mounts + .iter() + .map(|m| MountInfo::from(*m)) + .collect::>() + } + #[cfg(windows)] + { + let mut volume_name_buf = [0u16; MAX_PATH]; + // As recommended in the MS documentation, retrieve the first volume before the others + let find_handle = unsafe { + FindFirstVolumeW(volume_name_buf.as_mut_ptr(), volume_name_buf.len() as DWORD) + }; + if INVALID_HANDLE_VALUE == find_handle { + crash!(EXIT_ERR, "FindFirstVolumeW failed: {}", unsafe { + GetLastError() + }); + } + let mut mounts = Vec::::new(); + loop { + let volume_name = LPWSTR2String(&volume_name_buf); + if !volume_name.starts_with("\\\\?\\") || !volume_name.ends_with('\\') { + show_warning!("A bad path was skipped: {}", volume_name); + continue; + } + if let Some(m) = MountInfo::new(volume_name) { + mounts.push(m); + } + if 0 == unsafe { + FindNextVolumeW( + find_handle, + volume_name_buf.as_mut_ptr(), + volume_name_buf.len() as DWORD, + ) + } { + let err = unsafe { GetLastError() }; + if err != winapi::shared::winerror::ERROR_NO_MORE_FILES { + crash!(EXIT_ERR, "FindNextVolumeW failed: {}", err); + } + break; + } + } + unsafe { + FindVolumeClose(find_handle); + } + mounts + } +} + +#[derive(Debug, Clone)] +pub struct FsUsage { + pub blocksize: u64, + pub blocks: u64, + pub bfree: u64, + pub bavail: u64, + pub bavail_top_bit_set: bool, + pub files: u64, + pub ffree: u64, +} + +impl FsUsage { + #[cfg(unix)] + pub fn new(statvfs: StatFs) -> FsUsage { + { + FsUsage { + blocksize: statvfs.f_bsize as u64, // or `statvfs.f_frsize` ? + blocks: statvfs.f_blocks as u64, + bfree: statvfs.f_bfree as u64, + bavail: statvfs.f_bavail as u64, + bavail_top_bit_set: ((statvfs.f_bavail as u64) & (1u64.rotate_right(1))) != 0, + files: statvfs.f_files as u64, + ffree: statvfs.f_ffree as u64, + } + } + } + #[cfg(not(unix))] + pub fn new(path: &Path) -> FsUsage { + let mut root_path = [0u16; MAX_PATH]; + let success = unsafe { + GetVolumePathNamesForVolumeNameW( + //path_utf8.as_ptr(), + String2LPWSTR!(path.as_os_str()), + root_path.as_mut_ptr(), + root_path.len() as DWORD, + ptr::null_mut(), + ) + }; + if 0 == success { + crash!( + EXIT_ERR, + "GetVolumePathNamesForVolumeNameW failed: {}", + unsafe { GetLastError() } + ); + } + + let mut sectors_per_cluster = 0; + let mut bytes_per_sector = 0; + let mut number_of_free_clusters = 0; + let mut total_number_of_clusters = 0; + + let success = unsafe { + GetDiskFreeSpaceW( + String2LPWSTR!(path.as_os_str()), + &mut sectors_per_cluster, + &mut bytes_per_sector, + &mut number_of_free_clusters, + &mut total_number_of_clusters, + ) + }; + if 0 == success { + // Fails in case of CD for example + //crash!(EXIT_ERR, "GetDiskFreeSpaceW failed: {}", unsafe { + //GetLastError() + //}); + } + + let bytes_per_cluster = sectors_per_cluster as u64 * bytes_per_sector as u64; + FsUsage { + // f_bsize File system block size. + blocksize: bytes_per_cluster as u64, + // f_blocks - Total number of blocks on the file system, in units of f_frsize. + // frsize = Fundamental file system block size (fragment size). + blocks: total_number_of_clusters as u64, + // Total number of free blocks. + bfree: number_of_free_clusters as u64, + // Total number of free blocks available to non-privileged processes. + bavail: 0, + bavail_top_bit_set: ((bytes_per_sector as u64) & (1u64.rotate_right(1))) != 0, + // Total number of file nodes (inodes) on the file system. + files: 0, // Not available on windows + // Total number of free file nodes (inodes). + ffree: 4096, // Meaningless on Windows + } + } +} + +#[cfg(unix)] +pub trait FsMeta { + fn fs_type(&self) -> i64; + fn io_size(&self) -> u64; + fn block_size(&self) -> i64; + fn total_blocks(&self) -> u64; + fn free_blocks(&self) -> u64; + fn avail_blocks(&self) -> u64; + fn total_file_nodes(&self) -> u64; + fn free_file_nodes(&self) -> u64; + fn fsid(&self) -> u64; + fn namelen(&self) -> u64; +} + +#[cfg(unix)] +impl FsMeta for StatFs { + fn block_size(&self) -> i64 { + self.f_bsize as i64 + } + fn total_blocks(&self) -> u64 { + self.f_blocks as u64 + } + fn free_blocks(&self) -> u64 { + self.f_bfree as u64 + } + fn avail_blocks(&self) -> u64 { + self.f_bavail as u64 + } + fn total_file_nodes(&self) -> u64 { + self.f_files as u64 + } + fn free_file_nodes(&self) -> u64 { + self.f_ffree as u64 + } + #[cfg(any(target_os = "linux", target_vendor = "apple", target_os = "freebsd"))] + fn fs_type(&self) -> i64 { + self.f_type as i64 + } + #[cfg(not(any(target_os = "linux", target_vendor = "apple", target_os = "freebsd")))] + fn fs_type(&self) -> i64 { + // FIXME: statvfs doesn't have an equivalent, so we need to do something else + unimplemented!() + } + + #[cfg(target_os = "linux")] + fn io_size(&self) -> u64 { + self.f_frsize as u64 + } + #[cfg(any(target_vendor = "apple", target_os = "freebsd"))] + fn io_size(&self) -> u64 { + self.f_iosize as u64 + } + // XXX: dunno if this is right + #[cfg(not(any(target_vendor = "apple", target_os = "freebsd", target_os = "linux")))] + fn io_size(&self) -> u64 { + self.f_bsize as u64 + } + + // Linux, SunOS, HP-UX, 4.4BSD, FreeBSD have a system call statfs() that returns + // a struct statfs, containing a fsid_t f_fsid, where fsid_t is defined + // as struct { int val[2]; } + // + // Solaris, Irix and POSIX have a system call statvfs(2) that returns a + // struct statvfs, containing an unsigned long f_fsid + #[cfg(any(target_vendor = "apple", target_os = "freebsd", target_os = "linux"))] + fn fsid(&self) -> u64 { + let f_fsid: &[u32; 2] = + unsafe { &*(&self.f_fsid as *const libc::fsid_t as *const [u32; 2]) }; + (u64::from(f_fsid[0])) << 32 | u64::from(f_fsid[1]) + } + #[cfg(not(any(target_vendor = "apple", target_os = "freebsd", target_os = "linux")))] + fn fsid(&self) -> u64 { + self.f_fsid as u64 + } + + #[cfg(target_os = "linux")] + fn namelen(&self) -> u64 { + self.f_namelen as u64 + } + #[cfg(target_vendor = "apple")] + fn namelen(&self) -> u64 { + 1024 + } + #[cfg(target_os = "freebsd")] + fn namelen(&self) -> u64 { + self.f_namemax as u64 // spell-checker:disable-line + } + // XXX: should everything just use statvfs? + #[cfg(not(any(target_vendor = "apple", target_os = "freebsd", target_os = "linux")))] + fn namelen(&self) -> u64 { + self.f_namemax as u64 // spell-checker:disable-line + } +} + +#[cfg(unix)] +pub fn statfs>(path: P) -> Result +where + Vec: From

, +{ + match CString::new(path) { + Ok(p) => { + let mut buffer: StatFs = unsafe { mem::zeroed() }; + unsafe { + match statfs_fn(p.as_ptr(), &mut buffer) { + 0 => Ok(buffer), + _ => { + let errno = IOError::last_os_error().raw_os_error().unwrap_or(0); + Err(CString::from_raw(strerror(errno)) + .into_string() + .unwrap_or_else(|_| "Unknown Error".to_owned())) + } + } + } + } + Err(e) => Err(e.to_string()), + } +} + +pub fn pretty_time(sec: i64, nsec: i64) -> String { + // sec == seconds since UNIX_EPOCH + // nsec == nanoseconds since (UNIX_EPOCH + sec) + let tm = time::at(Timespec::new(sec, nsec as i32)); + let res = time::strftime("%Y-%m-%d %H:%M:%S.%f %z", &tm).unwrap(); + if res.ends_with(" -0000") { + res.replace(" -0000", " +0000") + } else { + res + } +} + +#[cfg(unix)] +pub fn pretty_filetype<'a>(mode: mode_t, size: u64) -> &'a str { + match mode & S_IFMT { + S_IFREG => { + if size != 0 { + "regular file" + } else { + "regular empty file" + } + } + S_IFDIR => "directory", + S_IFLNK => "symbolic link", + S_IFCHR => "character special file", + S_IFBLK => "block special file", + S_IFIFO => "fifo", + S_IFSOCK => "socket", + // TODO: Other file types + // See coreutils/gnulib/lib/file-type.c // spell-checker:disable-line + _ => "weird file", + } +} + +pub fn pretty_fstype<'a>(fstype: i64) -> Cow<'a, str> { + // spell-checker:disable + match fstype { + 0x6163_6673 => "acfs".into(), + 0xADF5 => "adfs".into(), + 0xADFF => "affs".into(), + 0x5346_414F => "afs".into(), + 0x0904_1934 => "anon-inode FS".into(), + 0x6175_6673 => "aufs".into(), + 0x0187 => "autofs".into(), + 0x4246_5331 => "befs".into(), + 0x6264_6576 => "bdevfs".into(), + 0x1BAD_FACE => "bfs".into(), + 0xCAFE_4A11 => "bpf_fs".into(), + 0x4249_4E4D => "binfmt_misc".into(), + 0x9123_683E => "btrfs".into(), + 0x7372_7279 => "btrfs_test".into(), + 0x00C3_6400 => "ceph".into(), + 0x0027_E0EB => "cgroupfs".into(), + 0xFF53_4D42 => "cifs".into(), + 0x7375_7245 => "coda".into(), + 0x012F_F7B7 => "coh".into(), + 0x6265_6570 => "configfs".into(), + 0x28CD_3D45 => "cramfs".into(), + 0x453D_CD28 => "cramfs-wend".into(), + 0x6462_6720 => "debugfs".into(), + 0x1373 => "devfs".into(), + 0x1CD1 => "devpts".into(), + 0xF15F => "ecryptfs".into(), + 0xDE5E_81E4 => "efivarfs".into(), + 0x0041_4A53 => "efs".into(), + 0x5DF5 => "exofs".into(), + 0x137D => "ext".into(), + 0xEF53 => "ext2/ext3".into(), + 0xEF51 => "ext2".into(), + 0xF2F5_2010 => "f2fs".into(), + 0x4006 => "fat".into(), + 0x1983_0326 => "fhgfs".into(), + 0x6573_5546 => "fuseblk".into(), + 0x6573_5543 => "fusectl".into(), + 0x0BAD_1DEA => "futexfs".into(), + 0x0116_1970 => "gfs/gfs2".into(), + 0x4750_4653 => "gpfs".into(), + 0x4244 => "hfs".into(), + 0x482B => "hfs+".into(), + 0x4858 => "hfsx".into(), + 0x00C0_FFEE => "hostfs".into(), + 0xF995_E849 => "hpfs".into(), + 0x9584_58F6 => "hugetlbfs".into(), + 0x1130_7854 => "inodefs".into(), + 0x0131_11A8 => "ibrix".into(), + 0x2BAD_1DEA => "inotifyfs".into(), + 0x9660 => "isofs".into(), + 0x4004 => "isofs".into(), + 0x4000 => "isofs".into(), + 0x07C0 => "jffs".into(), + 0x72B6 => "jffs2".into(), + 0x3153_464A => "jfs".into(), + 0x6B41_4653 => "k-afs".into(), + 0xC97E_8168 => "logfs".into(), + 0x0BD0_0BD0 => "lustre".into(), + 0x5346_314D => "m1fs".into(), + 0x137F => "minix".into(), + 0x138F => "minix (30 char.)".into(), + 0x2468 => "minix v2".into(), + 0x2478 => "minix v2 (30 char.)".into(), + 0x4D5A => "minix3".into(), + 0x1980_0202 => "mqueue".into(), + 0x4D44 => "msdos".into(), + 0x564C => "novell".into(), + 0x6969 => "nfs".into(), + 0x6E66_7364 => "nfsd".into(), + 0x3434 => "nilfs".into(), + 0x6E73_6673 => "nsfs".into(), + 0x5346_544E => "ntfs".into(), + 0x9FA1 => "openprom".into(), + 0x7461_636F => "ocfs2".into(), + 0x794C_7630 => "overlayfs".into(), + 0xAAD7_AAEA => "panfs".into(), + 0x5049_5045 => "pipefs".into(), + 0x7C7C_6673 => "prl_fs".into(), + 0x9FA0 => "proc".into(), + 0x6165_676C => "pstorefs".into(), + 0x002F => "qnx4".into(), + 0x6819_1122 => "qnx6".into(), + 0x8584_58F6 => "ramfs".into(), + 0x5265_4973 => "reiserfs".into(), + 0x7275 => "romfs".into(), + 0x6759_6969 => "rpc_pipefs".into(), + 0x7363_6673 => "securityfs".into(), + 0xF97C_FF8C => "selinux".into(), + 0x4341_5D53 => "smackfs".into(), + 0x517B => "smb".into(), + 0xFE53_4D42 => "smb2".into(), + 0xBEEF_DEAD => "snfs".into(), + 0x534F_434B => "sockfs".into(), + 0x7371_7368 => "squashfs".into(), + 0x6265_6572 => "sysfs".into(), + 0x012F_F7B6 => "sysv2".into(), + 0x012F_F7B5 => "sysv4".into(), + 0x0102_1994 => "tmpfs".into(), + 0x7472_6163 => "tracefs".into(), + 0x2405_1905 => "ubifs".into(), + 0x1501_3346 => "udf".into(), + 0x0001_1954 => "ufs".into(), + 0x5419_0100 => "ufs".into(), + 0x9FA2 => "usbdevfs".into(), + 0x0102_1997 => "v9fs".into(), + 0xBACB_ACBC => "vmhgfs".into(), + 0xA501_FCF5 => "vxfs".into(), + 0x565A_4653 => "vzfs".into(), + 0x5346_4846 => "wslfs".into(), + 0xABBA_1974 => "xenfs".into(), + 0x012F_F7B4 => "xenix".into(), + 0x5846_5342 => "xfs".into(), + 0x012F_D16D => "xia".into(), + 0x2FC1_2FC1 => "zfs".into(), + other => format!("UNKNOWN ({:#x})", other).into(), + } + // spell-checker:enable +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[cfg(unix)] + fn test_file_type() { + assert_eq!("block special file", pretty_filetype(S_IFBLK, 0)); + assert_eq!("character special file", pretty_filetype(S_IFCHR, 0)); + assert_eq!("regular file", pretty_filetype(S_IFREG, 1)); + assert_eq!("regular empty file", pretty_filetype(S_IFREG, 0)); + assert_eq!("weird file", pretty_filetype(0, 0)); + } + + #[test] + fn test_fs_type() { + // spell-checker:disable + assert_eq!("ext2/ext3", pretty_fstype(0xEF53)); + assert_eq!("tmpfs", pretty_fstype(0x01021994)); + assert_eq!("nfs", pretty_fstype(0x6969)); + assert_eq!("btrfs", pretty_fstype(0x9123683e)); + assert_eq!("xfs", pretty_fstype(0x58465342)); + assert_eq!("zfs", pretty_fstype(0x2FC12FC1)); + assert_eq!("ntfs", pretty_fstype(0x5346544e)); + assert_eq!("fat", pretty_fstype(0x4006)); + assert_eq!("UNKNOWN (0x1234)", pretty_fstype(0x1234)); + // spell-checker:enable + } +} diff --git a/src/uucore/src/lib/features/mode.rs b/src/uucore/src/lib/features/mode.rs index 8b5e71799..fe109d73d 100644 --- a/src/uucore/src/lib/features/mode.rs +++ b/src/uucore/src/lib/features/mode.rs @@ -7,6 +7,8 @@ // spell-checker:ignore (vars) fperm srwx +use libc::{mode_t, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR}; + pub fn parse_numeric(fperm: u32, mut mode: &str) -> Result { let (op, pos) = parse_op(mode, Some('='))?; mode = mode[pos..].trim().trim_start_matches('0'); @@ -87,19 +89,19 @@ fn parse_levels(mode: &str) -> (u32, usize) { } fn parse_op(mode: &str, default: Option) -> Result<(char, usize), String> { - match mode.chars().next() { - Some(ch) => match ch { - '+' | '-' | '=' => Ok((ch, 1)), - _ => match default { - Some(ch) => Ok((ch, 0)), - None => Err(format!( - "invalid operator (expected +, -, or =, but found {})", - ch - )), - }, - }, - None => Err("unexpected end of mode".to_owned()), - } + let ch = mode + .chars() + .next() + .ok_or_else(|| "unexpected end of mode".to_owned())?; + Ok(match ch { + '+' | '-' | '=' => (ch, 1), + _ => { + let ch = default.ok_or_else(|| { + format!("invalid operator (expected +, -, or =, but found {})", ch) + })?; + (ch, 0) + } + }) } fn parse_change(mode: &str, fperm: u32, considering_dir: bool) -> (u32, usize) { @@ -129,3 +131,36 @@ fn parse_change(mode: &str, fperm: u32, considering_dir: bool) -> (u32, usize) { } (srwx, pos) } + +pub fn parse_mode(mode: &str) -> Result { + let fperm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + let result = if mode.contains(arr) { + parse_numeric(fperm as u32, mode) + } else { + parse_symbolic(fperm as u32, mode, true) + }; + result.map(|mode| mode as mode_t) +} + +#[cfg(test)] +mod test { + + #[test] + fn symbolic_modes() { + assert_eq!(super::parse_mode("u+x").unwrap(), 0o766); + assert_eq!( + super::parse_mode("+x").unwrap(), + if !crate::os::is_wsl_1() { 0o777 } else { 0o776 } + ); + assert_eq!(super::parse_mode("a-w").unwrap(), 0o444); + assert_eq!(super::parse_mode("g-r").unwrap(), 0o626); + } + + #[test] + fn numeric_modes() { + assert_eq!(super::parse_mode("644").unwrap(), 0o644); + assert_eq!(super::parse_mode("+100").unwrap(), 0o766); + assert_eq!(super::parse_mode("-4").unwrap(), 0o662); + } +} diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index 66db15451..89c30b53b 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -26,14 +26,14 @@ pub enum Verbosity { } /// Actually perform the change of group on a path -fn chgrp>(path: P, dgid: gid_t, follow: bool) -> IOResult<()> { +fn chgrp>(path: P, gid: gid_t, follow: bool) -> IOResult<()> { let path = path.as_ref(); let s = CString::new(path.as_os_str().as_bytes()).unwrap(); let ret = unsafe { if follow { - libc::chown(s.as_ptr(), (0 as gid_t).wrapping_sub(1), dgid) + libc::chown(s.as_ptr(), 0_u32.wrapping_sub(1), gid) } else { - lchown(s.as_ptr(), (0 as gid_t).wrapping_sub(1), dgid) + lchown(s.as_ptr(), 0_u32.wrapping_sub(1), gid) } }; if ret == 0 { @@ -92,7 +92,7 @@ pub fn wrap_chgrp>( out = format!( "group of '{}' retained as {}", path.display(), - entries::gid2grp(dest_gid).unwrap() + entries::gid2grp(dest_gid).unwrap_or_default() ); } } @@ -100,14 +100,14 @@ pub fn wrap_chgrp>( } /// Actually perform the change of owner on a path -fn chown>(path: P, duid: uid_t, dgid: gid_t, follow: bool) -> IOResult<()> { +fn chown>(path: P, uid: uid_t, gid: gid_t, follow: bool) -> IOResult<()> { let path = path.as_ref(); let s = CString::new(path.as_os_str().as_bytes()).unwrap(); let ret = unsafe { if follow { - libc::chown(s.as_ptr(), duid, dgid) + libc::chown(s.as_ptr(), uid, gid) } else { - lchown(s.as_ptr(), duid, dgid) + lchown(s.as_ptr(), uid, gid) } }; if ret == 0 { diff --git a/src/uucore/src/lib/features/process.rs b/src/uucore/src/lib/features/process.rs index 078b782f5..21bfa992c 100644 --- a/src/uucore/src/lib/features/process.rs +++ b/src/uucore/src/lib/features/process.rs @@ -17,18 +17,22 @@ use std::process::ExitStatus as StdExitStatus; use std::thread; use std::time::{Duration, Instant}; +/// `geteuid()` returns the effective user ID of the calling process. pub fn geteuid() -> uid_t { unsafe { libc::geteuid() } } +/// `getegid()` returns the effective group ID of the calling process. pub fn getegid() -> gid_t { unsafe { libc::getegid() } } +/// `getgid()` returns the real group ID of the calling process. pub fn getgid() -> gid_t { unsafe { libc::getgid() } } +/// `getuid()` returns the real user ID of the calling process. pub fn getuid() -> uid_t { unsafe { libc::getuid() } } @@ -40,6 +44,7 @@ pub enum ExitStatus { Signal(i32), } +#[allow(clippy::trivially_copy_pass_by_ref)] impl ExitStatus { fn from_std_status(status: StdExitStatus) -> Self { #[cfg(unix)] @@ -92,6 +97,7 @@ pub trait ChildExt { fn send_signal(&mut self, signal: usize) -> io::Result<()>; /// Wait for a process to finish or return after the specified duration. + /// A `timeout` of zero disables the timeout. fn wait_or_timeout(&mut self, timeout: Duration) -> io::Result>; } @@ -105,6 +111,11 @@ impl ChildExt for Child { } fn wait_or_timeout(&mut self, timeout: Duration) -> io::Result> { + if timeout == Duration::from_micros(0) { + return self + .wait() + .map(|status| Some(ExitStatus::from_std_status(status))); + } // .try_wait() doesn't drop stdin, so we do it manually drop(self.stdin.take()); diff --git a/src/uucore/src/lib/features/ringbuffer.rs b/src/uucore/src/lib/features/ringbuffer.rs new file mode 100644 index 000000000..1cb0d2b0d --- /dev/null +++ b/src/uucore/src/lib/features/ringbuffer.rs @@ -0,0 +1,134 @@ +//! A fixed-size ring buffer. +use std::collections::VecDeque; + +/// A fixed-size ring buffer backed by a `VecDeque`. +/// +/// If the ring buffer is not full, then calling the [`push_back`] +/// method appends elements, as in a [`VecDeque`]. If the ring buffer +/// is full, then calling [`push_back`] removes the element at the +/// front of the buffer (in a first-in, first-out manner) before +/// appending the new element to the back of the buffer. +/// +/// Use [`from_iter`] to take the last `size` elements from an +/// iterator. +/// +/// # Examples +/// +/// After exceeding the size limit, the oldest elements are dropped in +/// favor of the newest element: +/// +/// ```rust,ignore +/// let mut buffer: RingBuffer = RingBuffer::new(2); +/// buffer.push_back(0); +/// buffer.push_back(1); +/// buffer.push_back(2); +/// assert_eq!(vec![1, 2], buffer.data); +/// ``` +/// +/// Take the last `n` elements from an iterator: +/// +/// ```rust,ignore +/// let iter = [0, 1, 2].iter(); +/// let actual = RingBuffer::from_iter(iter, 2).data; +/// let expected = VecDeque::from_iter([1, 2].iter()); +/// assert_eq!(expected, actual); +/// ``` +pub struct RingBuffer { + pub data: VecDeque, + size: usize, +} + +impl RingBuffer { + pub fn new(size: usize) -> RingBuffer { + RingBuffer { + data: VecDeque::new(), + size, + } + } + + pub fn from_iter(iter: impl Iterator, size: usize) -> RingBuffer { + let mut ring_buffer = RingBuffer::new(size); + for value in iter { + ring_buffer.push_back(value); + } + ring_buffer + } + + /// Append a value to the end of the ring buffer. + /// + /// If the ring buffer is not full, this method return [`None`]. If + /// the ring buffer is full, appending a new element will cause the + /// oldest element to be evicted. In that case this method returns + /// that element, or `None`. + /// + /// In the special case where the size limit is zero, each call to + /// this method with input `value` returns `Some(value)`, because + /// the input is immediately evicted. + /// + /// # Examples + /// + /// Appending an element when the buffer is full returns the oldest + /// element: + /// + /// ```rust,ignore + /// let mut buf = RingBuffer::new(3); + /// assert_eq!(None, buf.push_back(0)); + /// assert_eq!(None, buf.push_back(1)); + /// assert_eq!(None, buf.push_back(2)); + /// assert_eq!(Some(0), buf.push_back(3)); + /// ``` + /// + /// If the size limit is zero, then this method always returns the + /// input value: + /// + /// ```rust,ignore + /// let mut buf = RingBuffer::new(0); + /// assert_eq!(Some(0), buf.push_back(0)); + /// assert_eq!(Some(1), buf.push_back(1)); + /// assert_eq!(Some(2), buf.push_back(2)); + /// ``` + pub fn push_back(&mut self, value: T) -> Option { + if self.size == 0 { + return Some(value); + } + let result = if self.size <= self.data.len() { + self.data.pop_front() + } else { + None + }; + self.data.push_back(value); + result + } +} + +#[cfg(test)] +mod tests { + + use crate::ringbuffer::RingBuffer; + use std::collections::VecDeque; + use std::iter::FromIterator; + + #[test] + fn test_size_limit_zero() { + let mut buf = RingBuffer::new(0); + assert_eq!(Some(0), buf.push_back(0)); + assert_eq!(Some(1), buf.push_back(1)); + assert_eq!(Some(2), buf.push_back(2)); + } + + #[test] + fn test_evict_oldest() { + let mut buf = RingBuffer::new(2); + assert_eq!(None, buf.push_back(0)); + assert_eq!(None, buf.push_back(1)); + assert_eq!(Some(0), buf.push_back(2)); + } + + #[test] + fn test_from_iter() { + let iter = [0, 1, 2].iter(); + let actual = RingBuffer::from_iter(iter, 2).data; + let expected = VecDeque::from_iter([1, 2].iter()); + assert_eq!(expected, actual); + } +} diff --git a/src/uucore/src/lib/features/signals.rs b/src/uucore/src/lib/features/signals.rs index 294669eab..e6d2e7763 100644 --- a/src/uucore/src/lib/features/signals.rs +++ b/src/uucore/src/lib/features/signals.rs @@ -10,11 +10,6 @@ pub static DEFAULT_SIGNAL: usize = 15; -pub struct Signal<'a> { - pub name: &'a str, - pub value: usize, -} - /* Linux Programmer's Manual @@ -29,131 +24,10 @@ Linux Programmer's Manual */ #[cfg(target_os = "linux")] -pub static ALL_SIGNALS: [Signal<'static>; 31] = [ - Signal { - name: "HUP", - value: 1, - }, - Signal { - name: "INT", - value: 2, - }, - Signal { - name: "QUIT", - value: 3, - }, - Signal { - name: "ILL", - value: 4, - }, - Signal { - name: "TRAP", - value: 5, - }, - Signal { - name: "ABRT", - value: 6, - }, - Signal { - name: "BUS", - value: 7, - }, - Signal { - name: "FPE", - value: 8, - }, - Signal { - name: "KILL", - value: 9, - }, - Signal { - name: "USR1", - value: 10, - }, - Signal { - name: "SEGV", - value: 11, - }, - Signal { - name: "USR2", - value: 12, - }, - Signal { - name: "PIPE", - value: 13, - }, - Signal { - name: "ALRM", - value: 14, - }, - Signal { - name: "TERM", - value: 15, - }, - Signal { - name: "STKFLT", - value: 16, - }, - Signal { - name: "CHLD", - value: 17, - }, - Signal { - name: "CONT", - value: 18, - }, - Signal { - name: "STOP", - value: 19, - }, - Signal { - name: "TSTP", - value: 20, - }, - Signal { - name: "TTIN", - value: 21, - }, - Signal { - name: "TTOU", - value: 22, - }, - Signal { - name: "URG", - value: 23, - }, - Signal { - name: "XCPU", - value: 24, - }, - Signal { - name: "XFSZ", - value: 25, - }, - Signal { - name: "VTALRM", - value: 26, - }, - Signal { - name: "PROF", - value: 27, - }, - Signal { - name: "WINCH", - value: 28, - }, - Signal { - name: "POLL", - value: 29, - }, - Signal { - name: "PWR", - value: 30, - }, - Signal { - name: "SYS", - value: 31, - }, +pub static ALL_SIGNALS: [&str; 32] = [ + "EXIT", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE", "KILL", "USR1", "SEGV", + "USR2", "PIPE", "ALRM", "TERM", "STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", + "URG", "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH", "POLL", "PWR", "SYS", ]; /* @@ -197,132 +71,11 @@ No Name Default Action Description */ -#[cfg(any(target_os = "macos", target_os = "freebsd"))] -pub static ALL_SIGNALS: [Signal<'static>; 31] = [ - Signal { - name: "HUP", - value: 1, - }, - Signal { - name: "INT", - value: 2, - }, - Signal { - name: "QUIT", - value: 3, - }, - Signal { - name: "ILL", - value: 4, - }, - Signal { - name: "TRAP", - value: 5, - }, - Signal { - name: "ABRT", - value: 6, - }, - Signal { - name: "EMT", - value: 7, - }, - Signal { - name: "FPE", - value: 8, - }, - Signal { - name: "KILL", - value: 9, - }, - Signal { - name: "BUS", - value: 10, - }, - Signal { - name: "SEGV", - value: 11, - }, - Signal { - name: "SYS", - value: 12, - }, - Signal { - name: "PIPE", - value: 13, - }, - Signal { - name: "ALRM", - value: 14, - }, - Signal { - name: "TERM", - value: 15, - }, - Signal { - name: "URG", - value: 16, - }, - Signal { - name: "STOP", - value: 17, - }, - Signal { - name: "TSTP", - value: 18, - }, - Signal { - name: "CONT", - value: 19, - }, - Signal { - name: "CHLD", - value: 20, - }, - Signal { - name: "TTIN", - value: 21, - }, - Signal { - name: "TTOU", - value: 22, - }, - Signal { - name: "IO", - value: 23, - }, - Signal { - name: "XCPU", - value: 24, - }, - Signal { - name: "XFSZ", - value: 25, - }, - Signal { - name: "VTALRM", - value: 26, - }, - Signal { - name: "PROF", - value: 27, - }, - Signal { - name: "WINCH", - value: 28, - }, - Signal { - name: "INFO", - value: 29, - }, - Signal { - name: "USR1", - value: 30, - }, - Signal { - name: "USR2", - value: 31, - }, +#[cfg(any(target_vendor = "apple", target_os = "freebsd"))] +pub static ALL_SIGNALS: [&str; 32] = [ + "EXIT", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "EMT", "FPE", "KILL", "BUS", "SEGV", + "SYS", "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", "CONT", "CHLD", "TTIN", "TTOU", "IO", + "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH", "INFO", "USR1", "USR2", ]; pub fn signal_by_name_or_value(signal_name_or_value: &str) -> Option { @@ -335,56 +88,45 @@ pub fn signal_by_name_or_value(signal_name_or_value: &str) -> Option { } let signal_name = signal_name_or_value.trim_start_matches("SIG"); - ALL_SIGNALS - .iter() - .find(|s| s.name == signal_name) - .map(|s| s.value) + ALL_SIGNALS.iter().position(|&s| s == signal_name) } -#[inline(always)] pub fn is_signal(num: usize) -> bool { - // Named signals start at 1 - num <= ALL_SIGNALS.len() + num < ALL_SIGNALS.len() } -#[test] -fn signals_all_contiguous() { - for (i, signal) in ALL_SIGNALS.iter().enumerate() { - assert_eq!(signal.value, i + 1); - } -} - -#[test] -fn signals_all_are_signal() { - for signal in &ALL_SIGNALS { - assert!(is_signal(signal.value)); - } +pub fn signal_name_by_value(signal_value: usize) -> Option<&'static str> { + ALL_SIGNALS.get(signal_value).copied() } #[test] fn signal_by_value() { assert_eq!(signal_by_name_or_value("0"), Some(0)); - for signal in &ALL_SIGNALS { - assert_eq!( - signal_by_name_or_value(&signal.value.to_string()), - Some(signal.value) - ); + for (value, _signal) in ALL_SIGNALS.iter().enumerate() { + assert_eq!(signal_by_name_or_value(&value.to_string()), Some(value)); } } #[test] fn signal_by_short_name() { - for signal in &ALL_SIGNALS { - assert_eq!(signal_by_name_or_value(signal.name), Some(signal.value)); + for (value, signal) in ALL_SIGNALS.iter().enumerate() { + assert_eq!(signal_by_name_or_value(signal), Some(value)); } } #[test] fn signal_by_long_name() { - for signal in &ALL_SIGNALS { + for (value, signal) in ALL_SIGNALS.iter().enumerate() { assert_eq!( - signal_by_name_or_value(&format!("SIG{}", signal.name)), - Some(signal.value) + signal_by_name_or_value(&format!("SIG{}", signal)), + Some(value) ); } } + +#[test] +fn name() { + for (value, signal) in ALL_SIGNALS.iter().enumerate() { + assert_eq!(signal_name_by_value(value), Some(*signal)); + } +} diff --git a/src/uucore/src/lib/features/utmpx.rs b/src/uucore/src/lib/features/utmpx.rs index 31cd3b72c..5077d9e59 100644 --- a/src/uucore/src/lib/features/utmpx.rs +++ b/src/uucore/src/lib/features/utmpx.rs @@ -47,13 +47,15 @@ use libc::utmpx; pub use libc::endutxent; pub use libc::getutxent; pub use libc::setutxent; -#[cfg(any(target_os = "macos", target_os = "linux"))] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] pub use libc::utmpxname; #[cfg(target_os = "freebsd")] pub unsafe extern "C" fn utmpxname(_file: *const libc::c_char) -> libc::c_int { 0 } +pub use crate::*; // import macros from `../../macros.rs` + // In case the c_char array doesn't end with NULL macro_rules! chars2string { ($arr:expr) => { @@ -85,7 +87,7 @@ mod ut { pub use libc::USER_PROCESS; } -#[cfg(target_os = "macos")] +#[cfg(target_vendor = "apple")] mod ut { pub static DEFAULT_FILE: &str = "/var/run/utmpx"; @@ -188,47 +190,40 @@ impl Utmpx { /// Canonicalize host name using DNS pub fn canon_host(&self) -> IOResult { - const AI_CANONNAME: libc::c_int = 0x2; let host = self.host(); - let host = host.split(':').next().unwrap(); - let hints = libc::addrinfo { - ai_flags: AI_CANONNAME, - ai_family: 0, - ai_socktype: 0, - ai_protocol: 0, - ai_addrlen: 0, - ai_addr: ptr::null_mut(), - ai_canonname: ptr::null_mut(), - ai_next: ptr::null_mut(), - }; - let c_host = CString::new(host).unwrap(); - let mut res = ptr::null_mut(); - let status = unsafe { - libc::getaddrinfo( - c_host.as_ptr(), - ptr::null(), - &hints as *const _, - &mut res as *mut _, - ) - }; - if status == 0 { - let info: libc::addrinfo = unsafe { ptr::read(res as *const _) }; - // http://lists.gnu.org/archive/html/bug-coreutils/2006-09/msg00300.html - // says Darwin 7.9.0 getaddrinfo returns 0 but sets - // res->ai_canonname to NULL. - let ret = if info.ai_canonname.is_null() { - Ok(String::from(host)) - } else { - Ok(unsafe { CString::from_raw(info.ai_canonname).into_string().unwrap() }) + + // TODO: change to use `split_once` when MSRV hits 1.52.0 + // let (hostname, display) = host.split_once(':').unwrap_or((&host, "")); + let mut h = host.split(':'); + let hostname = h.next().unwrap_or(&host); + let display = h.next().unwrap_or(""); + + if !hostname.is_empty() { + extern crate dns_lookup; + use dns_lookup::{getaddrinfo, AddrInfoHints}; + + const AI_CANONNAME: i32 = 0x2; + let hints = AddrInfoHints { + flags: AI_CANONNAME, + ..AddrInfoHints::default() }; - unsafe { - libc::freeaddrinfo(res); + let sockets = getaddrinfo(Some(hostname), None, Some(hints)) + .unwrap() + .collect::>>()?; + for socket in sockets { + if let Some(ai_canonname) = socket.canonname { + return Ok(if display.is_empty() { + ai_canonname + } else { + format!("{}:{}", ai_canonname, display) + }); + } } - ret - } else { - Err(IOError::last_os_error()) } + + Ok(host.to_string()) } + pub fn iter_all_records() -> UtmpxIter { UtmpxIter } @@ -243,11 +238,11 @@ impl UtmpxIter { /// If not set, default record file will be used(file path depends on the target OS) pub fn read_from(self, f: &str) -> Self { let res = unsafe { - let cstr = CString::new(f).unwrap(); - utmpxname(cstr.as_ptr()) + let cstring = CString::new(f).unwrap(); + utmpxname(cstring.as_ptr()) }; if res != 0 { - println!("Warning: {}", IOError::last_os_error()); + show_warning!("utmpxname: {}", IOError::last_os_error()); } unsafe { setutxent(); diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 324095b6a..00477cd7e 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -19,23 +19,32 @@ pub extern crate winapi; //## internal modules -mod macros; // crate macros (macro_rules-type; exported to `crate::...`) - mod features; // feature-gated code modules +mod macros; // crate macros (macro_rules-type; exported to `crate::...`) mod mods; // core cross-platform modules +mod parser; // string parsing modules // * cross-platform modules +pub use crate::mods::backup_control; pub use crate::mods::coreopts; +pub use crate::mods::error; +pub use crate::mods::os; pub use crate::mods::panic; pub use crate::mods::ranges; +// * string parsing modules +pub use crate::parser::parse_size; +pub use crate::parser::parse_time; + // * feature-gated modules #[cfg(feature = "encoding")] pub use crate::features::encoding; #[cfg(feature = "fs")] pub use crate::features::fs; -#[cfg(feature = "parse_time")] -pub use crate::features::parse_time; +#[cfg(feature = "fsext")] +pub use crate::features::fsext; +#[cfg(feature = "ringbuffer")] +pub use crate::features::ringbuffer; #[cfg(feature = "zero-copy")] pub use crate::features::zero_copy; @@ -67,10 +76,95 @@ pub use crate::features::wide; use std::ffi::OsString; +pub enum InvalidEncodingHandling { + Ignore, + ConvertLossy, + Panic, +} + +#[must_use] +pub enum ConversionResult { + Complete(Vec), + Lossy(Vec), +} + +impl ConversionResult { + pub fn accept_any(self) -> Vec { + match self { + Self::Complete(result) => result, + Self::Lossy(result) => result, + } + } + + pub fn expect_lossy(self, msg: &str) -> Vec { + match self { + Self::Lossy(result) => result, + Self::Complete(_) => { + panic!("{}", msg); + } + } + } + + pub fn expect_complete(self, msg: &str) -> Vec { + match self { + Self::Complete(result) => result, + Self::Lossy(_) => { + panic!("{}", msg); + } + } + } +} + pub trait Args: Iterator + Sized { - fn collect_str(self) -> Vec { - // FIXME: avoid unwrap() - self.map(|s| s.into_string().unwrap()).collect() + /// Converts each iterator item to a String and collects these into a vector + /// On invalid encoding, the result will depend on the argument. This method allows to either drop entries with illegal encoding + /// completely (```InvalidEncodingHandling::Ignore```), convert them using lossy-conversion (```InvalidEncodingHandling::Lossy```) which will + /// result in strange strings or can chosen to panic (```InvalidEncodingHandling::Panic```). + /// # Arguments + /// * `handling` - This switch allows to switch the behavior, when invalid encoding is encountered + /// # Panics + /// * Occurs, when invalid encoding is encountered and handling is set to ```InvalidEncodingHandling::Panic``` + fn collect_str(self, handling: InvalidEncodingHandling) -> ConversionResult { + let mut full_conversion = true; + let result_vector: Vec = self + .map(|s| match s.into_string() { + Ok(string) => Ok(string), + Err(s_ret) => { + full_conversion = false; + let lossy_conversion = s_ret.to_string_lossy(); + eprintln!( + "Input with broken encoding occurred! (s = '{}') ", + &lossy_conversion + ); + match handling { + InvalidEncodingHandling::Ignore => Err(String::new()), + InvalidEncodingHandling::ConvertLossy => Err(lossy_conversion.to_string()), + InvalidEncodingHandling::Panic => { + panic!("Broken encoding found but caller cannot handle it") + } + } + } + }) + .filter(|s| match handling { + InvalidEncodingHandling::Ignore => s.is_ok(), + _ => true, + }) + .map(|s| match s { + Ok(v) => v, + Err(e) => e, + }) + .collect(); + + if full_conversion { + ConversionResult::Complete(result_vector) + } else { + ConversionResult::Lossy(result_vector) + } + } + + /// convenience function for a more slim interface + fn collect_str_lossy(self) -> ConversionResult { + self.collect_str(InvalidEncodingHandling::ConvertLossy) } } @@ -84,3 +178,85 @@ pub fn args() -> impl Iterator { pub fn args_os() -> impl Iterator { wild::args_os() } + +#[cfg(test)] +mod tests { + use super::*; + use std::ffi::OsStr; + + fn make_os_vec(os_str: &OsStr) -> Vec { + vec![ + OsString::from("test"), + OsString::from("สวัสดี"), // spell-checker:disable-line + os_str.to_os_string(), + ] + } + + fn collect_os_str(vec: Vec, handling: InvalidEncodingHandling) -> ConversionResult { + vec.into_iter().collect_str(handling) + } + + #[cfg(any(unix, target_os = "redox"))] + fn test_invalid_utf8_args_lossy(os_str: &OsStr) { + //assert our string is invalid utf8 + assert!(os_str.to_os_string().into_string().is_err()); + let test_vec = make_os_vec(os_str); + let collected_to_str = + collect_os_str(test_vec.clone(), InvalidEncodingHandling::ConvertLossy) + .expect_lossy("Lossy conversion expected in this test: bad encoding entries should be converted as good as possible"); + //conservation of length - when accepting lossy conversion no arguments may be dropped + assert_eq!(collected_to_str.len(), test_vec.len()); + //first indices identical + for index in 0..2 { + assert_eq!( + collected_to_str.get(index).unwrap(), + test_vec.get(index).unwrap().to_str().unwrap() + ); + } + //lossy conversion for string with illegal encoding is done + assert_eq!( + *collected_to_str.get(2).unwrap(), + os_str.to_os_string().to_string_lossy() + ); + } + + #[cfg(any(unix, target_os = "redox"))] + fn test_invalid_utf8_args_ignore(os_str: &OsStr) { + //assert our string is invalid utf8 + assert!(os_str.to_os_string().into_string().is_err()); + let test_vec = make_os_vec(os_str); + let collected_to_str = collect_os_str(test_vec.clone(), InvalidEncodingHandling::Ignore) + .expect_lossy( + "Lossy conversion expected in this test: bad encoding entries should be filtered", + ); + //assert that the broken entry is filtered out + assert_eq!(collected_to_str.len(), test_vec.len() - 1); + //assert that the unbroken indices are converted as expected + for index in 0..2 { + assert_eq!( + collected_to_str.get(index).unwrap(), + test_vec.get(index).unwrap().to_str().unwrap() + ); + } + } + + #[test] + fn valid_utf8_encoding_args() { + //create a vector containing only correct encoding + let test_vec = make_os_vec(&OsString::from("test2")); + //expect complete conversion without losses, even when lossy conversion is accepted + let _ = collect_os_str(test_vec, InvalidEncodingHandling::ConvertLossy) + .expect_complete("Lossy conversion not expected in this test"); + } + + #[cfg(any(unix, target_os = "redox"))] + #[test] + fn invalid_utf8_args_unix() { + use std::os::unix::ffi::OsStrExt; + + let source = [0x66, 0x6f, 0x80, 0x6f]; + let os_str = OsStr::from_bytes(&source[..]); + test_invalid_utf8_args_lossy(os_str); + test_invalid_utf8_args_ignore(os_str); + } +} diff --git a/src/uucore/src/lib/macros.rs b/src/uucore/src/lib/macros.rs index 0aa6d8f0a..e4d83e746 100644 --- a/src/uucore/src/lib/macros.rs +++ b/src/uucore/src/lib/macros.rs @@ -5,6 +5,9 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +/// Deduce the name of the binary from the current source code filename. +/// +/// e.g.: `src/uu/cp/src/cp.rs` -> `cp` #[macro_export] macro_rules! executable( () => ({ @@ -18,10 +21,38 @@ macro_rules! executable( }) ); +#[macro_export] +macro_rules! show( + ($err:expr) => ({ + let e = $err; + uucore::error::set_exit_code(e.code()); + eprintln!("{}: {}", executable!(), e); + }) +); + +#[macro_export] +macro_rules! show_if_err( + ($res:expr) => ({ + if let Err(e) = $res { + show!(e); + } + }) +); + +/// Show an error to stderr in a similar style to GNU coreutils. #[macro_export] macro_rules! show_error( ($($args:tt)+) => ({ - eprint!("{}: error: ", executable!()); + eprint!("{}: ", executable!()); + eprintln!($($args)+); + }) +); + +/// Show a warning to stderr in a similar style to GNU coreutils. +#[macro_export] +macro_rules! show_error_custom_description ( + ($err:expr,$($args:tt)+) => ({ + eprint!("{}: {}: ", executable!(), $err); eprintln!($($args)+); }) ); @@ -34,14 +65,7 @@ macro_rules! show_warning( }) ); -#[macro_export] -macro_rules! show_info( - ($($args:tt)+) => ({ - eprint!("{}: ", executable!()); - eprintln!($($args)+); - }) -); - +/// Show a bad invocation help message in a similar style to GNU coreutils. #[macro_export] macro_rules! show_usage_error( ($($args:tt)+) => ({ @@ -51,6 +75,7 @@ macro_rules! show_usage_error( }) ); +/// Display the provided error message, then `exit()` with the provided exit code #[macro_export] macro_rules! crash( ($exit_code:expr, $($args:tt)+) => ({ @@ -59,6 +84,7 @@ macro_rules! crash( }) ); +/// Calls `exit()` with the provided exit code. #[macro_export] macro_rules! exit( ($exit_code:expr) => ({ @@ -66,6 +92,8 @@ macro_rules! exit( }) ); +/// Unwraps the Result. Instead of panicking, it exists the program with the +/// provided exit code. #[macro_export] macro_rules! crash_if_err( ($exit_code:expr, $exp:expr) => ( @@ -76,6 +104,9 @@ macro_rules! crash_if_err( ) ); +/// Unwraps the Result. Instead of panicking, it shows the error and then +/// returns from the function with the provided exit code. +/// Assumes the current function returns an i32 value. #[macro_export] macro_rules! return_if_err( ($exit_code:expr, $exp:expr) => ( @@ -94,7 +125,7 @@ macro_rules! safe_write( ($fd:expr, $($args:tt)+) => ( match write!($fd, $($args)+) { Ok(_) => {} - Err(f) => panic!(f.to_string()) + Err(f) => panic!("{}", f) } ) ); @@ -104,11 +135,13 @@ macro_rules! safe_writeln( ($fd:expr, $($args:tt)+) => ( match writeln!($fd, $($args)+) { Ok(_) => {} - Err(f) => panic!(f.to_string()) + Err(f) => panic!("{}", f) } ) ); +/// Unwraps the Result. Instead of panicking, it exists the program with exit +/// code 1. #[macro_export] macro_rules! safe_unwrap( ($exp:expr) => ( @@ -152,13 +185,6 @@ macro_rules! msg_invalid_input { }; } -#[macro_export] -macro_rules! snippet_no_file_at_path { - ($path:expr) => { - format!("nonexistent path {}", $path) - }; -} - // -- message templates : invalid input : flag #[macro_export] @@ -205,55 +231,6 @@ macro_rules! msg_opt_invalid_should_be { }; } -// -- message templates : invalid input : args - -#[macro_export] -macro_rules! msg_arg_invalid_value { - ($expects:expr, $received:expr) => { - msg_invalid_input!(format!( - "expects its argument to be {}, but was provided {}", - $expects, $received - )) - }; -} - -#[macro_export] -macro_rules! msg_args_invalid_value { - ($expects:expr, $received:expr) => { - msg_invalid_input!(format!( - "expects its arguments to be {}, but was provided {}", - $expects, $received - )) - }; - ($msg:expr) => { - msg_invalid_input!($msg) - }; -} - -#[macro_export] -macro_rules! msg_args_nonexistent_file { - ($received:expr) => { - msg_args_invalid_value!("paths to files", snippet_no_file_at_path!($received)) - }; -} - -#[macro_export] -macro_rules! msg_wrong_number_of_arguments { - () => { - msg_args_invalid_value!("wrong number of arguments") - }; - ($min:expr, $max:expr) => { - msg_args_invalid_value!(format!("expects {}-{} arguments", $min, $max)) - }; - ($exact:expr) => { - if $exact == 1 { - msg_args_invalid_value!("expects 1 argument") - } else { - msg_args_invalid_value!(format!("expects {} arguments", $exact)) - } - }; -} - // -- message templates : invalid input : input combinations #[macro_export] diff --git a/src/uucore/src/lib/mods.rs b/src/uucore/src/lib/mods.rs index c73909dcc..040da2f02 100644 --- a/src/uucore/src/lib/mods.rs +++ b/src/uucore/src/lib/mods.rs @@ -1,5 +1,8 @@ // mods ~ cross-platforms modules (core/bundler file) +pub mod backup_control; pub mod coreopts; +pub mod error; +pub mod os; pub mod panic; pub mod ranges; diff --git a/src/uucore/src/lib/mods/backup_control.rs b/src/uucore/src/lib/mods/backup_control.rs new file mode 100644 index 000000000..83268d351 --- /dev/null +++ b/src/uucore/src/lib/mods/backup_control.rs @@ -0,0 +1,97 @@ +use std::{ + env, + path::{Path, PathBuf}, +}; + +pub static BACKUP_CONTROL_VALUES: &[&str] = &[ + "simple", "never", "numbered", "t", "existing", "nil", "none", "off", +]; + +pub static BACKUP_CONTROL_LONG_HELP: &str = "The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. Here are the version control values: + +none, off + never make backups (even if --backup is given) + +numbered, t + make numbered backups + +existing, nil + numbered if numbered backups exist, simple otherwise + +simple, never + always make simple backups"; + +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum BackupMode { + NoBackup, + SimpleBackup, + NumberedBackup, + ExistingBackup, +} + +pub fn determine_backup_suffix(supplied_suffix: Option<&str>) -> String { + if let Some(suffix) = supplied_suffix { + String::from(suffix) + } else { + env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| "~".to_owned()) + } +} + +pub fn determine_backup_mode(backup_opt_exists: bool, backup_opt: Option<&str>) -> BackupMode { + if backup_opt_exists { + match backup_opt.map(String::from) { + // default is existing, see: + // https://www.gnu.org/software/coreutils/manual/html_node/Backup-options.html + None => BackupMode::ExistingBackup, + Some(mode) => match &mode[..] { + "simple" | "never" => BackupMode::SimpleBackup, + "numbered" | "t" => BackupMode::NumberedBackup, + "existing" | "nil" => BackupMode::ExistingBackup, + "none" | "off" => BackupMode::NoBackup, + _ => panic!(), // cannot happen as it is managed by clap + }, + } + } else { + BackupMode::NoBackup + } +} + +pub fn get_backup_path( + backup_mode: BackupMode, + backup_path: &Path, + suffix: &str, +) -> Option { + match backup_mode { + BackupMode::NoBackup => None, + BackupMode::SimpleBackup => Some(simple_backup_path(backup_path, suffix)), + BackupMode::NumberedBackup => Some(numbered_backup_path(backup_path)), + BackupMode::ExistingBackup => Some(existing_backup_path(backup_path, suffix)), + } +} + +pub fn simple_backup_path(path: &Path, suffix: &str) -> PathBuf { + let mut p = path.to_string_lossy().into_owned(); + p.push_str(suffix); + PathBuf::from(p) +} + +pub fn numbered_backup_path(path: &Path) -> PathBuf { + for i in 1_u64.. { + let path_str = &format!("{}.~{}~", path.to_string_lossy(), i); + let path = Path::new(path_str); + if !path.exists() { + return path.to_path_buf(); + } + } + panic!("cannot create backup") +} + +pub fn existing_backup_path(path: &Path, suffix: &str) -> PathBuf { + let test_path_str = &format!("{}.~1~", path.to_string_lossy()); + let test_path = Path::new(test_path_str); + if test_path.exists() { + numbered_backup_path(path) + } else { + simple_backup_path(path, suffix) + } +} diff --git a/src/uucore/src/lib/mods/error.rs b/src/uucore/src/lib/mods/error.rs new file mode 100644 index 000000000..f13c777b6 --- /dev/null +++ b/src/uucore/src/lib/mods/error.rs @@ -0,0 +1,479 @@ +//! All utils return exit with an exit code. Usually, the following scheme is used: +//! * `0`: succeeded +//! * `1`: minor problems +//! * `2`: major problems +//! +//! This module provides types to reconcile these exit codes with idiomatic Rust error +//! handling. This has a couple advantages over manually using [`std::process::exit`]: +//! 1. It enables the use of `?`, `map_err`, `unwrap_or`, etc. in `uumain`. +//! 1. It encourages the use of `UResult`/`Result` in functions in the utils. +//! 1. The error messages are largely standardized across utils. +//! 1. Standardized error messages can be created from external result types +//! (i.e. [`std::io::Result`] & `clap::ClapResult`). +//! 1. `set_exit_code` takes away the burden of manually tracking exit codes for non-fatal errors. +//! +//! # Usage +//! The signature of a typical util should be: +//! ```ignore +//! fn uumain(args: impl uucore::Args) -> UResult<()> { +//! ... +//! } +//! ``` +//! [`UResult`] is a simple wrapper around [`Result`] with a custom error type: [`UError`]. The +//! most important difference with types implementing [`std::error::Error`] is that [`UError`]s +//! can specify the exit code of the program when they are returned from `uumain`: +//! * When `Ok` is returned, the code set with [`set_exit_code`] is used as exit code. If +//! [`set_exit_code`] was not used, then `0` is used. +//! * When `Err` is returned, the code corresponding with the error is used as exit code and the +//! error message is displayed. +//! +//! Additionally, the errors can be displayed manually with the [`show`] and [`show_if_err`] macros: +//! ```ignore +//! let res = Err(USimpleError::new(1, "Error!!")); +//! show_if_err!(res); +//! // or +//! if let Err(e) = res { +//! show!(e); +//! } +//! ``` +//! +//! **Note**: The [`show`] and [`show_if_err`] macros set the exit code of the program using +//! [`set_exit_code`]. See the documentation on that function for more information. +//! +//! # Guidelines +//! * Use common errors where possible. +//! * Add variants to [`UCommonError`] if an error appears in multiple utils. +//! * Prefer proper custom error types over [`ExitCode`] and [`USimpleError`]. +//! * [`USimpleError`] may be used in small utils with simple error handling. +//! * Using [`ExitCode`] is not recommended but can be useful for converting utils to use +//! [`UResult`]. + +use std::{ + error::Error, + fmt::{Display, Formatter}, + sync::atomic::{AtomicI32, Ordering}, +}; + +static EXIT_CODE: AtomicI32 = AtomicI32::new(0); + +/// Get the last exit code set with [`set_exit_code`]. +/// The default value is `0`. +pub fn get_exit_code() -> i32 { + EXIT_CODE.load(Ordering::SeqCst) +} + +/// Set the exit code for the program if `uumain` returns `Ok(())`. +/// +/// This function is most useful for non-fatal errors, for example when applying an operation to +/// multiple files: +/// ```ignore +/// use uucore::error::{UResult, set_exit_code}; +/// +/// fn uumain(args: impl uucore::Args) -> UResult<()> { +/// ... +/// for file in files { +/// let res = some_operation_that_might_fail(file); +/// match res { +/// Ok() => {}, +/// Err(_) => set_exit_code(1), +/// } +/// } +/// Ok(()) // If any of the operations failed, 1 is returned. +/// } +/// ``` +pub fn set_exit_code(code: i32) { + EXIT_CODE.store(code, Ordering::SeqCst); +} + +/// Should be returned by all utils. +/// +/// Two additional methods are implemented on [`UResult`] on top of the normal [`Result`] methods: +/// `map_err_code` & `map_err_code_message`. +/// +/// These methods are used to convert [`UCommonError`]s into errors with a custom error code and +/// message. +pub type UResult = Result; + +trait UResultTrait { + fn map_err_code(self, mapper: fn(&UCommonError) -> Option) -> Self; + fn map_err_code_and_message(self, mapper: fn(&UCommonError) -> Option<(i32, String)>) -> Self; +} + +impl UResultTrait for UResult { + fn map_err_code(self, mapper: fn(&UCommonError) -> Option) -> Self { + if let Err(UError::Common(error)) = self { + if let Some(code) = mapper(&error) { + Err(UCommonErrorWithCode { code, error }.into()) + } else { + Err(error.into()) + } + } else { + self + } + } + + fn map_err_code_and_message(self, mapper: fn(&UCommonError) -> Option<(i32, String)>) -> Self { + if let Err(UError::Common(ref error)) = self { + if let Some((code, message)) = mapper(error) { + return Err(USimpleError { code, message }.into()); + } + } + self + } +} + +/// The error type of [`UResult`]. +/// +/// `UError::Common` errors are defined in [`uucore`](crate) while `UError::Custom` errors are +/// defined by the utils. +/// ``` +/// use uucore::error::USimpleError; +/// let err = USimpleError::new(1, "Error!!".into()); +/// assert_eq!(1, err.code()); +/// assert_eq!(String::from("Error!!"), format!("{}", err)); +/// ``` +pub enum UError { + Common(UCommonError), + Custom(Box), +} + +impl UError { + pub fn code(&self) -> i32 { + match self { + UError::Common(e) => e.code(), + UError::Custom(e) => e.code(), + } + } +} + +impl From for UError { + fn from(v: UCommonError) -> Self { + UError::Common(v) + } +} + +impl From for UError { + fn from(v: i32) -> Self { + UError::Custom(Box::new(ExitCode(v))) + } +} + +impl From for UError { + fn from(v: E) -> Self { + UError::Custom(Box::new(v) as Box) + } +} + +impl Display for UError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + UError::Common(e) => e.fmt(f), + UError::Custom(e) => e.fmt(f), + } + } +} + +/// Custom errors defined by the utils. +/// +/// All errors should implement [`std::error::Error`], [`std::fmt::Display`] and +/// [`std::fmt::Debug`] and have an additional `code` method that specifies the exit code of the +/// program if the error is returned from `uumain`. +/// +/// An example of a custom error from `ls`: +/// ``` +/// use uucore::error::{UCustomError}; +/// use std::{ +/// error::Error, +/// fmt::{Display, Debug}, +/// path::PathBuf +/// }; +/// +/// #[derive(Debug)] +/// enum LsError { +/// InvalidLineWidth(String), +/// NoMetadata(PathBuf), +/// } +/// +/// impl UCustomError for LsError { +/// fn code(&self) -> i32 { +/// match self { +/// LsError::InvalidLineWidth(_) => 2, +/// LsError::NoMetadata(_) => 1, +/// } +/// } +/// } +/// +/// impl Error for LsError {} +/// +/// impl Display for LsError { +/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +/// match self { +/// LsError::InvalidLineWidth(s) => write!(f, "invalid line width: '{}'", s), +/// LsError::NoMetadata(p) => write!(f, "could not open file: '{}'", p.display()), +/// } +/// } +/// } +/// ``` +/// A crate like [`quick_error`](https://crates.io/crates/quick-error) might also be used, but will +/// still require an `impl` for the `code` method. +pub trait UCustomError: Error { + fn code(&self) -> i32 { + 1 + } +} + +impl From> for i32 { + fn from(e: Box) -> i32 { + e.code() + } +} + +/// A [`UCommonError`] with an overridden exit code. +/// +/// This exit code is returned instead of the default exit code for the [`UCommonError`]. This is +/// typically created with the either the `UResult::map_err_code` or `UCommonError::with_code` +/// method. +#[derive(Debug)] +pub struct UCommonErrorWithCode { + code: i32, + error: UCommonError, +} + +impl Error for UCommonErrorWithCode {} + +impl Display for UCommonErrorWithCode { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + self.error.fmt(f) + } +} + +impl UCustomError for UCommonErrorWithCode { + fn code(&self) -> i32 { + self.code + } +} + +/// A simple error type with an exit code and a message that implements [`UCustomError`]. +/// +/// It is typically created with the `UResult::map_err_code_and_message` method. Alternatively, it +/// can be constructed by manually: +/// ``` +/// use uucore::error::{UResult, USimpleError}; +/// let err = USimpleError { code: 1, message: "error!".into()}; +/// let res: UResult<()> = Err(err.into()); +/// // or using the `new` method: +/// let res: UResult<()> = Err(USimpleError::new(1, "error!".into())); +/// ``` +#[derive(Debug)] +pub struct USimpleError { + pub code: i32, + pub message: String, +} + +impl USimpleError { + #[allow(clippy::new_ret_no_self)] + pub fn new(code: i32, message: String) -> UError { + UError::Custom(Box::new(Self { code, message })) + } +} + +impl Error for USimpleError {} + +impl Display for USimpleError { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + self.message.fmt(f) + } +} + +impl UCustomError for USimpleError { + fn code(&self) -> i32 { + self.code + } +} + +/// Wrapper type around [`std::io::Error`]. +/// +/// The messages displayed by [`UIoError`] should match the error messages displayed by GNU +/// coreutils. +/// +/// There are two ways to construct this type: with [`UIoError::new`] or by calling the +/// [`FromIo::map_err_context`] method on a [`std::io::Result`] or [`std::io::Error`]. +/// ``` +/// use uucore::error::{FromIo, UResult, UIoError, UCommonError}; +/// use std::fs::File; +/// use std::path::Path; +/// let path = Path::new("test.txt"); +/// +/// // Manual construction +/// let e: UIoError = UIoError::new( +/// std::io::ErrorKind::NotFound, +/// format!("cannot access '{}'", path.display()) +/// ); +/// let res: UResult<()> = Err(e.into()); +/// +/// // Converting from an `std::io::Error`. +/// let res: UResult = File::open(path).map_err_context(|| format!("cannot access '{}'", path.display())); +/// ``` +#[derive(Debug)] +pub struct UIoError { + context: String, + inner: std::io::Error, +} + +impl UIoError { + pub fn new(kind: std::io::ErrorKind, context: String) -> Self { + Self { + context, + inner: std::io::Error::new(kind, ""), + } + } + + pub fn code(&self) -> i32 { + 1 + } +} + +impl Error for UIoError {} + +impl Display for UIoError { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + use std::io::ErrorKind::*; + write!( + f, + "{}: {}", + self.context, + match self.inner.kind() { + NotFound => "No such file or directory", + PermissionDenied => "Permission denied", + ConnectionRefused => "Connection refused", + ConnectionReset => "Connection reset", + ConnectionAborted => "Connection aborted", + NotConnected => "Not connected", + AddrInUse => "Address in use", + AddrNotAvailable => "Address not available", + BrokenPipe => "Broken pipe", + AlreadyExists => "Already exists", + WouldBlock => "Would block", + InvalidInput => "Invalid input", + InvalidData => "Invalid data", + TimedOut => "Timed out", + WriteZero => "Write zero", + Interrupted => "Interrupted", + Other => "Other", + UnexpectedEof => "Unexpected end of file", + _ => panic!("Unexpected io error: {}", self.inner), + }, + ) + } +} + +/// Enables the conversion from `std::io::Error` to `UError` and from `std::io::Result` to +/// `UResult`. +pub trait FromIo { + fn map_err_context(self, context: impl FnOnce() -> String) -> T; +} + +impl FromIo for std::io::Error { + fn map_err_context(self, context: impl FnOnce() -> String) -> UIoError { + UIoError { + context: (context)(), + inner: self, + } + } +} + +impl FromIo> for std::io::Result { + fn map_err_context(self, context: impl FnOnce() -> String) -> UResult { + self.map_err(|e| UError::Common(UCommonError::Io(e.map_err_context(context)))) + } +} + +impl FromIo for std::io::ErrorKind { + fn map_err_context(self, context: impl FnOnce() -> String) -> UIoError { + UIoError { + context: (context)(), + inner: std::io::Error::new(self, ""), + } + } +} + +impl From for UCommonError { + fn from(e: UIoError) -> UCommonError { + UCommonError::Io(e) + } +} + +impl From for UError { + fn from(e: UIoError) -> UError { + let common: UCommonError = e.into(); + common.into() + } +} + +/// Common errors for utilities. +/// +/// If identical errors appear across multiple utilities, they should be added here. +#[derive(Debug)] +pub enum UCommonError { + Io(UIoError), + // Clap(UClapError), +} + +impl UCommonError { + pub fn with_code(self, code: i32) -> UCommonErrorWithCode { + UCommonErrorWithCode { code, error: self } + } + + pub fn code(&self) -> i32 { + 1 + } +} + +impl From for i32 { + fn from(common: UCommonError) -> i32 { + match common { + UCommonError::Io(e) => e.code(), + } + } +} + +impl Error for UCommonError {} + +impl Display for UCommonError { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + match self { + UCommonError::Io(e) => e.fmt(f), + } + } +} + +/// A special error type that does not print any message when returned from +/// `uumain`. Especially useful for porting utilities to using [`UResult`]. +/// +/// There are two ways to construct an [`ExitCode`]: +/// ``` +/// use uucore::error::{ExitCode, UResult}; +/// // Explicit +/// let res: UResult<()> = Err(ExitCode(1).into()); +/// +/// // Using into on `i32`: +/// let res: UResult<()> = Err(1.into()); +/// ``` +/// This type is especially useful for a trivial conversion from utils returning [`i32`] to +/// returning [`UResult`]. +#[derive(Debug)] +pub struct ExitCode(pub i32); + +impl Error for ExitCode {} + +impl Display for ExitCode { + fn fmt(&self, _: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + Ok(()) + } +} + +impl UCustomError for ExitCode { + fn code(&self) -> i32 { + self.0 + } +} diff --git a/src/uucore/src/lib/mods/os.rs b/src/uucore/src/lib/mods/os.rs new file mode 100644 index 000000000..dd06f4739 --- /dev/null +++ b/src/uucore/src/lib/mods/os.rs @@ -0,0 +1,33 @@ +/// Test if the program is running under WSL +// ref: @@ + +// spell-checker:ignore (path) osrelease + +pub fn is_wsl_1() -> bool { + #[cfg(target_os = "linux")] + { + if is_wsl_2() { + return false; + } + if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") { + if let Ok(s) = std::str::from_utf8(&b) { + let a = s.to_ascii_lowercase(); + return a.contains("microsoft") || a.contains("wsl"); + } + } + } + false +} + +pub fn is_wsl_2() -> bool { + #[cfg(target_os = "linux")] + { + if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") { + if let Ok(s) = std::str::from_utf8(&b) { + let a = s.to_ascii_lowercase(); + return a.contains("wsl2"); + } + } + } + false +} diff --git a/src/uucore/src/lib/mods/panic.rs b/src/uucore/src/lib/mods/panic.rs index 6947df2ac..ba0ecdf12 100644 --- a/src/uucore/src/lib/mods/panic.rs +++ b/src/uucore/src/lib/mods/panic.rs @@ -8,7 +8,7 @@ pub fn mute_sigpipe_panic() { let hook = panic::take_hook(); panic::set_hook(Box::new(move |info| { if let Some(res) = info.payload().downcast_ref::() { - if res.contains("Broken pipe") { + if res.contains("BrokenPipe") { return; } } diff --git a/src/uucore/src/lib/mods/ranges.rs b/src/uucore/src/lib/mods/ranges.rs index d4a6bf601..9e1e67d5a 100644 --- a/src/uucore/src/lib/mods/ranges.rs +++ b/src/uucore/src/lib/mods/ranges.rs @@ -85,10 +85,9 @@ impl Range { let mut ranges: Vec = vec![]; for item in list.split(',') { - match FromStr::from_str(item) { - Ok(range_item) => ranges.push(range_item), - Err(e) => return Err(format!("range '{}' was invalid: {}", item, e)), - } + let range_item = FromStr::from_str(item) + .map_err(|e| format!("range '{}' was invalid: {}", item, e))?; + ranges.push(range_item); } ranges.sort(); diff --git a/src/uucore/src/lib/parser.rs b/src/uucore/src/lib/parser.rs new file mode 100644 index 000000000..d09777e10 --- /dev/null +++ b/src/uucore/src/lib/parser.rs @@ -0,0 +1,2 @@ +pub mod parse_size; +pub mod parse_time; diff --git a/src/uucore/src/lib/parser/parse_size.rs b/src/uucore/src/lib/parser/parse_size.rs new file mode 100644 index 000000000..ec0b08c9e --- /dev/null +++ b/src/uucore/src/lib/parser/parse_size.rs @@ -0,0 +1,324 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +// spell-checker:ignore (ToDO) hdsf ghead gtail + +use std::convert::TryFrom; +use std::error::Error; +use std::fmt; + +/// Parse a size string into a number of bytes. +/// +/// A size string comprises an integer and an optional unit. The unit +/// may be K, M, G, T, P, E, Z or Y (powers of 1024), or KB, MB, +/// etc. (powers of 1000), or b which is 512. +/// Binary prefixes can be used, too: KiB=K, MiB=M, and so on. +/// +/// # Errors +/// +/// Will return `ParseSizeError` if it's not possible to parse this +/// string into a number, e.g. if the string does not begin with a +/// numeral, or if the unit is not one of the supported units described +/// in the preceding section. +/// +/// # Examples +/// +/// ```rust +/// use uucore::parse_size::parse_size; +/// assert_eq!(Ok(123), parse_size("123")); +/// assert_eq!(Ok(9 * 1000), parse_size("9kB")); // kB is 1000 +/// assert_eq!(Ok(2 * 1024), parse_size("2K")); // K is 1024 +/// ``` +pub fn parse_size(size: &str) -> Result { + if size.is_empty() { + return Err(ParseSizeError::parse_failure(size)); + } + // Get the numeric part of the size argument. For example, if the + // argument is "123K", then the numeric part is "123". + let numeric_string: String = size.chars().take_while(|c| c.is_digit(10)).collect(); + let number: usize = if !numeric_string.is_empty() { + match numeric_string.parse() { + Ok(n) => n, + Err(_) => return Err(ParseSizeError::parse_failure(size)), + } + } else { + 1 + }; + + // Get the alphabetic units part of the size argument and compute + // the factor it represents. For example, if the argument is "123K", + // then the unit part is "K" and the factor is 1024. This may be the + // empty string, in which case, the factor is 1. + let unit = &size[numeric_string.len()..]; + let (base, exponent): (u128, u32) = match unit { + "" => (1, 0), + "b" => (512, 1), // (`od`, `head` and `tail` use "b") + "KiB" | "kiB" | "K" | "k" => (1024, 1), + "MiB" | "miB" | "M" | "m" => (1024, 2), + "GiB" | "giB" | "G" | "g" => (1024, 3), + "TiB" | "tiB" | "T" | "t" => (1024, 4), + "PiB" | "piB" | "P" | "p" => (1024, 5), + "EiB" | "eiB" | "E" | "e" => (1024, 6), + "ZiB" | "ziB" | "Z" | "z" => (1024, 7), + "YiB" | "yiB" | "Y" | "y" => (1024, 8), + "KB" | "kB" => (1000, 1), + "MB" | "mB" => (1000, 2), + "GB" | "gB" => (1000, 3), + "TB" | "tB" => (1000, 4), + "PB" | "pB" => (1000, 5), + "EB" | "eB" => (1000, 6), + "ZB" | "zB" => (1000, 7), + "YB" | "yB" => (1000, 8), + _ => return Err(ParseSizeError::parse_failure(size)), + }; + let factor = match usize::try_from(base.pow(exponent)) { + Ok(n) => n, + Err(_) => return Err(ParseSizeError::size_too_big(size)), + }; + number + .checked_mul(factor) + .ok_or_else(|| ParseSizeError::size_too_big(size)) +} + +#[derive(Debug, PartialEq, Eq)] +pub enum ParseSizeError { + ParseFailure(String), // Syntax + SizeTooBig(String), // Overflow +} + +impl Error for ParseSizeError { + fn description(&self) -> &str { + match *self { + ParseSizeError::ParseFailure(ref s) => &*s, + ParseSizeError::SizeTooBig(ref s) => &*s, + } + } +} + +impl fmt::Display for ParseSizeError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + let s = match self { + ParseSizeError::ParseFailure(s) => s, + ParseSizeError::SizeTooBig(s) => s, + }; + write!(f, "{}", s) + } +} + +impl ParseSizeError { + fn parse_failure(s: &str) -> ParseSizeError { + // stderr on linux (GNU coreutils 8.32) (LC_ALL=C) + // has to be handled in the respective uutils because strings differ, e.g.: + // + // `NUM` + // head: invalid number of bytes: '1fb' + // tail: invalid number of bytes: '1fb' + // + // `SIZE` + // split: invalid number of bytes: '1fb' + // truncate: Invalid number: '1fb' + // + // `MODE` + // stdbuf: invalid mode '1fb' + // + // `SIZE` + // sort: invalid suffix in --buffer-size argument '1fb' + // sort: invalid --buffer-size argument 'fb' + // + // `SIZE` + // du: invalid suffix in --buffer-size argument '1fb' + // du: invalid suffix in --threshold argument '1fb' + // du: invalid --buffer-size argument 'fb' + // du: invalid --threshold argument 'fb' + // + // `BYTES` + // od: invalid suffix in --read-bytes argument '1fb' + // od: invalid --read-bytes argument argument 'fb' + // --skip-bytes + // --width + // --strings + // etc. + ParseSizeError::ParseFailure(format!("'{}'", s)) + } + + fn size_too_big(s: &str) -> ParseSizeError { + // stderr on linux (GNU coreutils 8.32) (LC_ALL=C) + // has to be handled in the respective uutils because strings differ, e.g.: + // + // head: invalid number of bytes: '1Y': Value too large for defined data type + // tail: invalid number of bytes: '1Y': Value too large for defined data type + // split: invalid number of bytes: '1Y': Value too large for defined data type + // truncate: Invalid number: '1Y': Value too large for defined data type + // stdbuf: invalid mode '1Y': Value too large for defined data type + // sort: -S argument '1Y' too large + // du: -B argument '1Y' too large + // od: -N argument '1Y' too large + // etc. + // + // stderr on macos (brew - GNU coreutils 8.32) also differs for the same version, e.g.: + // ghead: invalid number of bytes: '1Y': Value too large to be stored in data type + // gtail: invalid number of bytes: '1Y': Value too large to be stored in data type + ParseSizeError::SizeTooBig(format!("'{}': Value too large for defined data type", s)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn variant_eq(a: &ParseSizeError, b: &ParseSizeError) -> bool { + std::mem::discriminant(a) == std::mem::discriminant(b) + } + + #[test] + fn all_suffixes() { + // Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000). + // Binary prefixes can be used, too: KiB=K, MiB=M, and so on. + let suffixes = [ + ('K', 1u32), + ('M', 2u32), + ('G', 3u32), + ('T', 4u32), + ('P', 5u32), + ('E', 6u32), + #[cfg(target_pointer_width = "128")] + ('Z', 7u32), // ParseSizeError::SizeTooBig on x64 + #[cfg(target_pointer_width = "128")] + ('Y', 8u32), // ParseSizeError::SizeTooBig on x64 + ]; + + for &(c, exp) in &suffixes { + let s = format!("2{}B", c); // KB + assert_eq!(Ok((2 * (1000_u128).pow(exp)) as usize), parse_size(&s)); + let s = format!("2{}", c); // K + assert_eq!(Ok((2 * (1024_u128).pow(exp)) as usize), parse_size(&s)); + let s = format!("2{}iB", c); // KiB + assert_eq!(Ok((2 * (1024_u128).pow(exp)) as usize), parse_size(&s)); + let s = format!("2{}iB", c.to_lowercase()); // kiB + assert_eq!(Ok((2 * (1024_u128).pow(exp)) as usize), parse_size(&s)); + + // suffix only + let s = format!("{}B", c); // KB + assert_eq!(Ok(((1000_u128).pow(exp)) as usize), parse_size(&s)); + let s = format!("{}", c); // K + assert_eq!(Ok(((1024_u128).pow(exp)) as usize), parse_size(&s)); + let s = format!("{}iB", c); // KiB + assert_eq!(Ok(((1024_u128).pow(exp)) as usize), parse_size(&s)); + let s = format!("{}iB", c.to_lowercase()); // kiB + assert_eq!(Ok(((1024_u128).pow(exp)) as usize), parse_size(&s)); + } + } + + #[test] + #[cfg(not(target_pointer_width = "128"))] + fn overflow_x64() { + assert!(parse_size("10000000000000000000000").is_err()); + assert!(parse_size("1000000000T").is_err()); + assert!(parse_size("100000P").is_err()); + assert!(parse_size("100E").is_err()); + assert!(parse_size("1Z").is_err()); + assert!(parse_size("1Y").is_err()); + + assert!(variant_eq( + &parse_size("1Z").unwrap_err(), + &ParseSizeError::SizeTooBig(String::new()) + )); + + assert_eq!( + ParseSizeError::SizeTooBig("'1Y': Value too large for defined data type".to_string()), + parse_size("1Y").unwrap_err() + ); + } + + #[test] + #[cfg(target_pointer_width = "32")] + fn overflow_x32() { + assert!(variant_eq( + &parse_size("1T").unwrap_err(), + &ParseSizeError::SizeTooBig(String::new()) + )); + assert!(variant_eq( + &parse_size("1000G").unwrap_err(), + &ParseSizeError::SizeTooBig(String::new()) + )); + } + + #[test] + fn invalid_syntax() { + let test_strings = [ + "328hdsf3290", + "5MiB nonsense", + "5mib", + "biB", + "-", + "+", + "", + "-1", + "1e2", + "∞", + ]; + for &test_string in &test_strings { + assert_eq!( + parse_size(test_string).unwrap_err(), + ParseSizeError::ParseFailure(format!("'{}'", test_string)) + ); + } + } + + #[test] + fn b_suffix() { + assert_eq!(Ok(3 * 512), parse_size("3b")); // b is 512 + } + + #[test] + fn no_suffix() { + assert_eq!(Ok(1234), parse_size("1234")); + assert_eq!(Ok(0), parse_size("0")); + assert_eq!(Ok(5), parse_size("5")); + assert_eq!(Ok(999), parse_size("999")); + } + + #[test] + fn kilobytes_suffix() { + assert_eq!(Ok(123 * 1000), parse_size("123KB")); // KB is 1000 + assert_eq!(Ok(9 * 1000), parse_size("9kB")); // kB is 1000 + assert_eq!(Ok(2 * 1024), parse_size("2K")); // K is 1024 + assert_eq!(Ok(0), parse_size("0K")); + assert_eq!(Ok(0), parse_size("0KB")); + assert_eq!(Ok(1000), parse_size("KB")); + assert_eq!(Ok(1024), parse_size("K")); + assert_eq!(Ok(2000), parse_size("2kB")); + assert_eq!(Ok(4000), parse_size("4KB")); + } + + #[test] + fn megabytes_suffix() { + assert_eq!(Ok(123 * 1024 * 1024), parse_size("123M")); + assert_eq!(Ok(123 * 1000 * 1000), parse_size("123MB")); + assert_eq!(Ok(1024 * 1024), parse_size("M")); + assert_eq!(Ok(1000 * 1000), parse_size("MB")); + assert_eq!(Ok(2 * 1_048_576), parse_size("2m")); + assert_eq!(Ok(4 * 1_048_576), parse_size("4M")); + assert_eq!(Ok(2_000_000), parse_size("2mB")); + assert_eq!(Ok(4_000_000), parse_size("4MB")); + } + + #[test] + fn gigabytes_suffix() { + assert_eq!(Ok(1_073_741_824), parse_size("1G")); + assert_eq!(Ok(2_000_000_000), parse_size("2GB")); + } + + #[test] + #[cfg(target_pointer_width = "64")] + fn x64() { + assert_eq!(Ok(1_099_511_627_776), parse_size("1T")); + assert_eq!(Ok(1_125_899_906_842_624), parse_size("1P")); + assert_eq!(Ok(1_152_921_504_606_846_976), parse_size("1E")); + assert_eq!(Ok(2_000_000_000_000), parse_size("2TB")); + assert_eq!(Ok(2_000_000_000_000_000), parse_size("2PB")); + assert_eq!(Ok(2_000_000_000_000_000_000), parse_size("2EB")); + } +} diff --git a/src/uucore/src/lib/features/parse_time.rs b/src/uucore/src/lib/parser/parse_time.rs similarity index 78% rename from src/uucore/src/lib/features/parse_time.rs rename to src/uucore/src/lib/parser/parse_time.rs index 8e822685b..fdf43b727 100644 --- a/src/uucore/src/lib/features/parse_time.rs +++ b/src/uucore/src/lib/parser/parse_time.rs @@ -20,20 +20,18 @@ pub fn from_str(string: &str) -> Result { 'm' | 'M' => (slice, 60), 'h' | 'H' => (slice, 60 * 60), 'd' | 'D' => (slice, 60 * 60 * 24), - val => { - if !val.is_alphabetic() { - (string, 1) - } else if string == "inf" || string == "infinity" { + val if !val.is_alphabetic() => (string, 1), + _ => { + if string == "inf" || string == "infinity" { ("inf", 1) } else { return Err(format!("invalid time interval '{}'", string)); } } }; - let num = match numstr.parse::() { - Ok(m) => m, - Err(e) => return Err(format!("invalid time interval '{}': {}", string, e)), - }; + let num = numstr + .parse::() + .map_err(|e| format!("invalid time interval '{}': {}", string, e))?; const NANOS_PER_SEC: u32 = 1_000_000_000; let whole_secs = num.trunc(); diff --git a/src/uucore_procs/src/lib.rs b/src/uucore_procs/src/lib.rs index aa77316ee..93567a12d 100644 --- a/src/uucore_procs/src/lib.rs +++ b/src/uucore_procs/src/lib.rs @@ -1,9 +1,10 @@ -#![allow(dead_code)] // work-around for GH:rust-lang/rust#62127; maint: can be removed when MinSRV >= v1.38.0 -#![allow(unused_macros)] // work-around for GH:rust-lang/rust#62127; maint: can be removed when MinSRV >= v1.38.0 - // Copyright (C) ~ Roy Ivy III ; MIT license extern crate proc_macro; +use proc_macro::TokenStream; +use proc_macro2::{Ident, Span}; +use quote::quote; +use syn::{self, parse_macro_input, ItemFn}; //## rust proc-macro background info //* ref: @@ @@ -44,8 +45,7 @@ impl syn::parse::Parse for Tokens { } #[proc_macro] -#[cfg(not(test))] // work-around for GH:rust-lang/rust#62127; maint: can be removed when MinSRV >= v1.38.0 -pub fn main(stream: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn main(stream: TokenStream) -> TokenStream { let Tokens { expr } = syn::parse_macro_input!(stream as Tokens); proc_dbg!(&expr); @@ -56,10 +56,10 @@ pub fn main(stream: proc_macro::TokenStream) -> proc_macro::TokenStream { let mut expr = match expr { syn::Expr::Lit(expr_lit) => match expr_lit.lit { syn::Lit::Str(ref lit_str) => lit_str.parse::().unwrap(), - _ => panic!(ARG_PANIC_TEXT), + _ => panic!("{}", ARG_PANIC_TEXT), }, syn::Expr::Path(expr_path) => expr_path, - _ => panic!(ARG_PANIC_TEXT), + _ => panic!("{}", ARG_PANIC_TEXT), }; proc_dbg!(&expr); @@ -82,5 +82,32 @@ pub fn main(stream: proc_macro::TokenStream) -> proc_macro::TokenStream { std::process::exit(code); } }; - proc_macro::TokenStream::from(result) + TokenStream::from(result) +} + +#[proc_macro_attribute] +pub fn gen_uumain(_args: TokenStream, stream: TokenStream) -> TokenStream { + let mut ast = parse_macro_input!(stream as ItemFn); + + // Change the name of the function to "uumain_result" to prevent name-conflicts + ast.sig.ident = Ident::new("uumain_result", Span::call_site()); + + let new = quote!( + pub fn uumain(args: impl uucore::Args) -> i32 { + #ast + let result = uumain_result(args); + match result { + Ok(()) => uucore::error::get_exit_code(), + Err(e) => { + let s = format!("{}", e); + if s != "" { + show_error!("{}", s); + } + e.code() + } + } + } + ); + + TokenStream::from(new) } diff --git a/tests/benches/factor/Cargo.toml b/tests/benches/factor/Cargo.toml new file mode 100644 index 000000000..b3b718477 --- /dev/null +++ b/tests/benches/factor/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "uu_factor_benches" +version = "0.0.0" +authors = ["nicoo "] +license = "MIT" +description = "Benchmarks for the uu_factor integer factorization tool" +homepage = "https://github.com/uutils/coreutils" +edition = "2018" + +[dependencies] +uu_factor = { path = "../../../src/uu/factor" } + +[dev-dependencies] +array-init = "2.0.0" +criterion = "0.3" +rand = "0.7" +rand_chacha = "0.2.2" + + +[[bench]] +name = "gcd" +harness = false + +[[bench]] +name = "table" +harness = false diff --git a/src/uu/factor/benches/gcd.rs b/tests/benches/factor/benches/gcd.rs similarity index 90% rename from src/uu/factor/benches/gcd.rs rename to tests/benches/factor/benches/gcd.rs index 803c37d3c..f2bae51c7 100644 --- a/src/uu/factor/benches/gcd.rs +++ b/tests/benches/factor/benches/gcd.rs @@ -3,7 +3,7 @@ use uu_factor::numeric; fn gcd(c: &mut Criterion) { let inputs = { - // Deterministic RNG; use an explicitely-named RNG to guarantee stability + // Deterministic RNG; use an explicitly-named RNG to guarantee stability use rand::{RngCore, SeedableRng}; use rand_chacha::ChaCha8Rng; const SEED: u64 = 0xa_b4d_1dea_dead_cafe; diff --git a/tests/benches/factor/benches/table.rs b/tests/benches/factor/benches/table.rs new file mode 100644 index 000000000..9090c7d51 --- /dev/null +++ b/tests/benches/factor/benches/table.rs @@ -0,0 +1,78 @@ +use array_init::array_init; +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; +use std::convert::TryInto; +use uu_factor::{table::*, Factors}; + +fn table(c: &mut Criterion) { + #[cfg(target_os = "linux")] + check_personality(); + + const INPUT_SIZE: usize = 128; + assert!( + INPUT_SIZE % CHUNK_SIZE == 0, + "INPUT_SIZE ({}) is not divisible by CHUNK_SIZE ({})", + INPUT_SIZE, + CHUNK_SIZE + ); + let inputs = { + // Deterministic RNG; use an explicitly-named RNG to guarantee stability + use rand::{RngCore, SeedableRng}; + use rand_chacha::ChaCha8Rng; + const SEED: u64 = 0xdead_bebe_ea75_cafe; // spell-checker:disable-line + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + + std::iter::repeat_with(move || array_init::<_, _, INPUT_SIZE>(|_| rng.next_u64())) + }; + + let mut group = c.benchmark_group("table"); + group.throughput(Throughput::Elements(INPUT_SIZE as _)); + for a in inputs.take(10) { + let a_str = format!("{:?}", a); + group.bench_with_input(BenchmarkId::new("factor_chunk", &a_str), &a, |b, &a| { + b.iter(|| { + let mut n_s = a.clone(); + let mut f_s: [_; INPUT_SIZE] = array_init(|_| Factors::one()); + for (n_s, f_s) in n_s.chunks_mut(CHUNK_SIZE).zip(f_s.chunks_mut(CHUNK_SIZE)) { + factor_chunk(n_s.try_into().unwrap(), f_s.try_into().unwrap()) + } + }) + }); + group.bench_with_input(BenchmarkId::new("factor", &a_str), &a, |b, &a| { + b.iter(|| { + let mut n_s = a.clone(); + let mut f_s: [_; INPUT_SIZE] = array_init(|_| Factors::one()); + for (n, f) in n_s.iter_mut().zip(f_s.iter_mut()) { + factor(n, f) + } + }) + }); + } + group.finish() +} + +#[cfg(target_os = "linux")] +fn check_personality() { + use std::fs; + const ADDR_NO_RANDOMIZE: u64 = 0x0040000; + const PERSONALITY_PATH: &'static str = "/proc/self/personality"; + + let p_string = fs::read_to_string(PERSONALITY_PATH) + .expect(&format!("Couldn't read '{}'", PERSONALITY_PATH)) + .strip_suffix("\n") + .unwrap() + .to_owned(); + + let personality = u64::from_str_radix(&p_string, 16).expect(&format!( + "Expected a hex value for personality, got '{:?}'", + p_string + )); + if personality & ADDR_NO_RANDOMIZE == 0 { + eprintln!( + "WARNING: Benchmarking with ASLR enabled (personality is {:x}), results might not be reproducible.", + personality + ); + } +} + +criterion_group!(benches, table); +criterion_main!(benches); diff --git a/tests/by-util/test_arch.rs b/tests/by-util/test_arch.rs index d2ec138d9..909e0ee80 100644 --- a/tests/by-util/test_arch.rs +++ b/tests/by-util/test_arch.rs @@ -2,17 +2,13 @@ use crate::common::util::*; #[test] fn test_arch() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.run(); - assert!(result.success); + new_ucmd!().succeeds(); } #[test] fn test_arch_help() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("--help").run(); - assert!(result.success); - assert!(result.stdout.contains("architecture name")); + new_ucmd!() + .arg("--help") + .succeeds() + .stdout_contains("architecture name"); } diff --git a/tests/by-util/test_base32.rs b/tests/by-util/test_base32.rs index d3527d26a..38ead28f1 100644 --- a/tests/by-util/test_base32.rs +++ b/tests/by-util/test_base32.rs @@ -14,13 +14,28 @@ fn test_encode() { new_ucmd!() .pipe_in(input) .succeeds() - .stdout_only("JBSWY3DPFQQFO33SNRSCC===\n"); + .stdout_only("JBSWY3DPFQQFO33SNRSCC===\n"); // spell-checker:disable-line + + // Using '-' as our file + new_ucmd!() + .arg("-") + .pipe_in(input) + .succeeds() + .stdout_only("JBSWY3DPFQQFO33SNRSCC===\n"); // spell-checker:disable-line +} + +#[test] +fn test_base32_encode_file() { + new_ucmd!() + .arg("input-simple.txt") + .succeeds() + .stdout_only("JBSWY3DPFQQFO33SNRSCCCQ=\n"); // spell-checker:disable-line } #[test] fn test_decode() { - for decode_param in vec!["-d", "--decode"] { - let input = "JBSWY3DPFQQFO33SNRSCC===\n"; + for decode_param in &["-d", "--decode"] { + let input = "JBSWY3DPFQQFO33SNRSCC===\n"; // spell-checker:disable-line new_ucmd!() .arg(decode_param) .pipe_in(input) @@ -31,7 +46,7 @@ fn test_decode() { #[test] fn test_garbage() { - let input = "aGVsbG8sIHdvcmxkIQ==\0"; + let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line new_ucmd!() .arg("-d") .pipe_in(input) @@ -41,8 +56,8 @@ fn test_garbage() { #[test] fn test_ignore_garbage() { - for ignore_garbage_param in vec!["-i", "--ignore-garbage"] { - let input = "JBSWY\x013DPFQ\x02QFO33SNRSCC===\n"; + for ignore_garbage_param in &["-i", "--ignore-garbage"] { + let input = "JBSWY\x013DPFQ\x02QFO33SNRSCC===\n"; // spell-checker:disable-line new_ucmd!() .arg("-d") .arg(ignore_garbage_param) @@ -54,7 +69,7 @@ fn test_ignore_garbage() { #[test] fn test_wrap() { - for wrap_param in vec!["-w", "--wrap"] { + for wrap_param in &["-w", "--wrap"] { let input = "The quick brown fox jumps over the lazy dog."; new_ucmd!() .arg(wrap_param) @@ -62,28 +77,50 @@ fn test_wrap() { .pipe_in(input) .succeeds() .stdout_only( - "KRUGKIDROVUWG2ZAMJZG\n653OEBTG66BANJ2W24DT\nEBXXMZLSEB2GQZJANRQX\nU6JAMRXWOLQ=\n", + "KRUGKIDROVUWG2ZAMJZG\n653OEBTG66BANJ2W24DT\nEBXXMZLSEB2GQZJANRQX\nU6JAMRXWOLQ=\n", // spell-checker:disable-line ); } } #[test] fn test_wrap_no_arg() { - for wrap_param in vec!["-w", "--wrap"] { - new_ucmd!().arg(wrap_param).fails().stderr_only(format!( - "base32: error: Argument to option '{}' missing\n", - if wrap_param == "-w" { "w" } else { "wrap" } - )); + for wrap_param in &["-w", "--wrap"] { + let expected_stderr = "error: The argument '--wrap \' requires a value but none was \ + supplied\n\nUSAGE:\n base32 [OPTION]... [FILE]\n\nFor more \ + information try --help" + .to_string(); + new_ucmd!() + .arg(wrap_param) + .fails() + .stderr_only(expected_stderr); } } #[test] fn test_wrap_bad_arg() { - for wrap_param in vec!["-w", "--wrap"] { + for wrap_param in &["-w", "--wrap"] { new_ucmd!() .arg(wrap_param) .arg("b") .fails() - .stderr_only("base32: error: invalid wrap size: ‘b’: invalid digit found in string\n"); + .stderr_only("base32: Invalid wrap size: 'b': invalid digit found in string\n"); } } + +#[test] +fn test_base32_extra_operand() { + // Expect a failure when multiple files are specified. + new_ucmd!() + .arg("a.txt") + .arg("a.txt") + .fails() + .stderr_only("base32: extra operand 'a.txt'"); +} + +#[test] +fn test_base32_file_not_found() { + new_ucmd!() + .arg("a.txt") + .fails() + .stderr_only("base32: a.txt: No such file or directory"); +} diff --git a/tests/by-util/test_base64.rs b/tests/by-util/test_base64.rs index 6bc0436c5..7c7f19205 100644 --- a/tests/by-util/test_base64.rs +++ b/tests/by-util/test_base64.rs @@ -6,13 +6,28 @@ fn test_encode() { new_ucmd!() .pipe_in(input) .succeeds() - .stdout_only("aGVsbG8sIHdvcmxkIQ==\n"); + .stdout_only("aGVsbG8sIHdvcmxkIQ==\n"); // spell-checker:disable-line + + // Using '-' as our file + new_ucmd!() + .arg("-") + .pipe_in(input) + .succeeds() + .stdout_only("aGVsbG8sIHdvcmxkIQ==\n"); // spell-checker:disable-line +} + +#[test] +fn test_base64_encode_file() { + new_ucmd!() + .arg("input-simple.txt") + .succeeds() + .stdout_only("SGVsbG8sIFdvcmxkIQo=\n"); // spell-checker:disable-line } #[test] fn test_decode() { - for decode_param in vec!["-d", "--decode"] { - let input = "aGVsbG8sIHdvcmxkIQ=="; + for decode_param in &["-d", "--decode"] { + let input = "aGVsbG8sIHdvcmxkIQ=="; // spell-checker:disable-line new_ucmd!() .arg(decode_param) .pipe_in(input) @@ -23,7 +38,7 @@ fn test_decode() { #[test] fn test_garbage() { - let input = "aGVsbG8sIHdvcmxkIQ==\0"; + let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line new_ucmd!() .arg("-d") .pipe_in(input) @@ -33,8 +48,8 @@ fn test_garbage() { #[test] fn test_ignore_garbage() { - for ignore_garbage_param in vec!["-i", "--ignore-garbage"] { - let input = "aGVsbG8sIHdvcmxkIQ==\0"; + for ignore_garbage_param in &["-i", "--ignore-garbage"] { + let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line new_ucmd!() .arg("-d") .arg(ignore_garbage_param) @@ -46,34 +61,52 @@ fn test_ignore_garbage() { #[test] fn test_wrap() { - for wrap_param in vec!["-w", "--wrap"] { + for wrap_param in &["-w", "--wrap"] { let input = "The quick brown fox jumps over the lazy dog."; new_ucmd!() .arg(wrap_param) .arg("20") .pipe_in(input) .succeeds() + // spell-checker:disable-next-line .stdout_only("VGhlIHF1aWNrIGJyb3du\nIGZveCBqdW1wcyBvdmVy\nIHRoZSBsYXp5IGRvZy4=\n"); } } #[test] fn test_wrap_no_arg() { - for wrap_param in vec!["-w", "--wrap"] { - new_ucmd!().arg(wrap_param).fails().stderr_only(format!( - "base64: error: Argument to option '{}' missing\n", - if wrap_param == "-w" { "w" } else { "wrap" } - )); + for wrap_param in &["-w", "--wrap"] { + new_ucmd!().arg(wrap_param).fails().stderr_contains( + &"The argument '--wrap ' requires a value but none was supplied", + ); } } #[test] fn test_wrap_bad_arg() { - for wrap_param in vec!["-w", "--wrap"] { + for wrap_param in &["-w", "--wrap"] { new_ucmd!() .arg(wrap_param) .arg("b") .fails() - .stderr_only("base64: error: invalid wrap size: ‘b’: invalid digit found in string\n"); + .stderr_only("base64: Invalid wrap size: 'b': invalid digit found in string\n"); } } + +#[test] +fn test_base64_extra_operand() { + // Expect a failure when multiple files are specified. + new_ucmd!() + .arg("a.txt") + .arg("a.txt") + .fails() + .stderr_only("base64: extra operand 'a.txt'"); +} + +#[test] +fn test_base64_file_not_found() { + new_ucmd!() + .arg("a.txt") + .fails() + .stderr_only("base64: a.txt: No such file or directory"); +} diff --git a/tests/by-util/test_basename.rs b/tests/by-util/test_basename.rs index fa599644d..d9632106e 100644 --- a/tests/by-util/test_basename.rs +++ b/tests/by-util/test_basename.rs @@ -1,4 +1,31 @@ +// spell-checker:ignore (words) reallylongexecutable + use crate::common::util::*; +#[cfg(any(unix, target_os = "redox"))] +use std::ffi::OsStr; + +#[test] +fn test_help() { + for help_flg in &["-h", "--help"] { + new_ucmd!() + .arg(&help_flg) + .succeeds() + .no_stderr() + .stdout_contains("USAGE:"); + } +} + +#[test] +fn test_version() { + for version_flg in &["-V", "--version"] { + assert!(new_ucmd!() + .arg(&version_flg) + .succeeds() + .no_stderr() + .stdout_str() + .starts_with("basename")); + } +} #[test] fn test_directory() { @@ -25,7 +52,7 @@ fn test_remove_suffix() { } #[test] -fn test_dont_remove_suffix() { +fn test_do_not_remove_suffix() { new_ucmd!() .args(&["/foo/bar/baz", "baz"]) .succeeds() @@ -34,29 +61,29 @@ fn test_dont_remove_suffix() { #[test] fn test_multiple_param() { - for multiple_param in vec!["-a", "--multiple"] { + for &multiple_param in &["-a", "--multiple"] { let path = "/foo/bar/baz"; new_ucmd!() .args(&[multiple_param, path, path]) .succeeds() - .stdout_only("baz\nbaz\n"); + .stdout_only("baz\nbaz\n"); // spell-checker:disable-line } } #[test] fn test_suffix_param() { - for suffix_param in vec!["-s", "--suffix"] { + for &suffix_param in &["-s", "--suffix"] { let path = "/foo/bar/baz.exe"; new_ucmd!() .args(&[suffix_param, ".exe", path, path]) .succeeds() - .stdout_only("baz\nbaz\n"); + .stdout_only("baz\nbaz\n"); // spell-checker:disable-line } } #[test] fn test_zero_param() { - for zero_param in vec!["-z", "--zero"] { + for &zero_param in &["-z", "--zero"] { let path = "/foo/bar/baz"; new_ucmd!() .args(&[zero_param, "-a", path, path]) @@ -66,7 +93,12 @@ fn test_zero_param() { } fn expect_error(input: Vec<&str>) { - assert!(new_ucmd!().args(&input).fails().no_stdout().stderr.len() > 0); + assert!(!new_ucmd!() + .args(&input) + .fails() + .no_stdout() + .stderr_str() + .is_empty()); } #[test] @@ -80,7 +112,38 @@ fn test_no_args() { expect_error(vec![]); } +#[test] +fn test_no_args_output() { + new_ucmd!() + .fails() + .stderr_is("basename: missing operand\nTry 'basename --help' for more information."); +} + #[test] fn test_too_many_args() { expect_error(vec!["a", "b", "c"]); } + +#[test] +fn test_too_many_args_output() { + new_ucmd!() + .args(&["a", "b", "c"]) + .fails() + .stderr_is("basename: extra operand 'c'\nTry 'basename --help' for more information."); +} + +#[cfg(any(unix, target_os = "redox"))] +fn test_invalid_utf8_args(os_str: &OsStr) { + let test_vec = vec![os_str.to_os_string()]; + new_ucmd!().args(&test_vec).succeeds().stdout_is("fo�o\n"); +} + +#[cfg(any(unix, target_os = "redox"))] +#[test] +fn invalid_utf8_args_unix() { + use std::os::unix::ffi::OsStrExt; + + let source = [0x66, 0x6f, 0x80, 0x6f]; + let os_str = OsStr::from_bytes(&source[..]); + test_invalid_utf8_args(os_str); +} diff --git a/tests/by-util/test_cat.rs b/tests/by-util/test_cat.rs index a3e321139..d83b5515b 100644 --- a/tests/by-util/test_cat.rs +++ b/tests/by-util/test_cat.rs @@ -1,42 +1,249 @@ -#[cfg(unix)] -extern crate unix_socket; - use crate::common::util::*; +#[cfg(unix)] +use std::fs::OpenOptions; +#[cfg(unix)] +use std::io::Read; + +#[test] +fn test_output_simple() { + new_ucmd!() + .args(&["alpha.txt"]) + .succeeds() + .stdout_only("abcde\nfghij\nklmno\npqrst\nuvwxyz\n"); // spell-checker:disable-line +} + +#[test] +fn test_no_options() { + // spell-checker:disable-next-line + for fixture in &["empty.txt", "alpha.txt", "nonewline.txt"] { + // Give fixture through command line file argument + new_ucmd!() + .args(&[fixture]) + .succeeds() + .stdout_is_fixture(fixture); + // Give fixture through stdin + new_ucmd!() + .pipe_in_fixture(fixture) + .succeeds() + .stdout_is_fixture(fixture); + } +} + +#[test] +#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] +fn test_no_options_big_input() { + for &n in &[ + 0, + 1, + 42, + 16 * 1024 - 7, + 16 * 1024 - 1, + 16 * 1024, + 16 * 1024 + 1, + 16 * 1024 + 3, + 32 * 1024, + 64 * 1024, + 80 * 1024, + 96 * 1024, + 112 * 1024, + 128 * 1024, + ] { + let data = vec_of_size(n); + let data2 = data.clone(); + assert_eq!(data.len(), data2.len()); + new_ucmd!().pipe_in(data).succeeds().stdout_is_bytes(&data2); + } +} + +#[test] +#[cfg(unix)] +fn test_fifo_symlink() { + use std::io::Write; + use std::thread; + + let s = TestScenario::new(util_name!()); + s.fixtures.mkdir("dir"); + s.fixtures.mkfifo("dir/pipe"); + assert!(s.fixtures.is_fifo("dir/pipe")); + + // Make cat read the pipe through a symlink + s.fixtures.symlink_file("dir/pipe", "sympipe"); // spell-checker:disable-line + let proc = s.ucmd().args(&["sympipe"]).run_no_wait(); // spell-checker:disable-line + + let data = vec_of_size(128 * 1024); + let data2 = data.clone(); + + let pipe_path = s.fixtures.plus("dir/pipe"); + let thread = thread::spawn(move || { + let mut pipe = OpenOptions::new() + .write(true) + .create(false) + .open(pipe_path) + .unwrap(); + pipe.write_all(&data).unwrap(); + }); + + let output = proc.wait_with_output().unwrap(); + assert_eq!(&output.stdout, &data2); + thread.join().unwrap(); +} + +#[test] +#[cfg(unix)] +fn test_piped_to_regular_file() { + use std::fs::read_to_string; + + for &append in &[true, false] { + let s = TestScenario::new(util_name!()); + let file_path = s.fixtures.plus("file.txt"); + + { + let file = OpenOptions::new() + .create_new(true) + .write(true) + .append(append) + .open(&file_path) + .unwrap(); + + s.ucmd() + .set_stdout(file) + .pipe_in_fixture("alpha.txt") + .succeeds(); + } + let contents = read_to_string(&file_path).unwrap(); + assert_eq!(contents, "abcde\nfghij\nklmno\npqrst\nuvwxyz\n"); // spell-checker:disable-line + } +} + +#[test] +#[cfg(unix)] +fn test_piped_to_dev_null() { + for &append in &[true, false] { + let s = TestScenario::new(util_name!()); + { + let dev_null = OpenOptions::new() + .write(true) + .append(append) + .open("/dev/null") + .unwrap(); + + s.ucmd() + .set_stdout(dev_null) + .pipe_in_fixture("alpha.txt") + .succeeds(); + } + } +} + +#[test] +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] +fn test_piped_to_dev_full() { + for &append in &[true, false] { + let s = TestScenario::new(util_name!()); + { + let dev_full = OpenOptions::new() + .write(true) + .append(append) + .open("/dev/full") + .unwrap(); + + s.ucmd() + .set_stdout(dev_full) + .pipe_in_fixture("alpha.txt") + .fails() + .stderr_contains(&"No space left on device".to_owned()); + } + } +} + +#[test] +fn test_directory() { + let s = TestScenario::new(util_name!()); + s.fixtures.mkdir("test_directory"); + s.ucmd() + .args(&["test_directory"]) + .fails() + .stderr_is("cat: test_directory: Is a directory"); +} + +#[test] +fn test_directory_and_file() { + let s = TestScenario::new(util_name!()); + s.fixtures.mkdir("test_directory2"); + // spell-checker:disable-next-line + for fixture in &["empty.txt", "alpha.txt", "nonewline.txt"] { + s.ucmd() + .args(&["test_directory2", fixture]) + .fails() + .stderr_is("cat: test_directory2: Is a directory") + .stdout_is_fixture(fixture); + } +} + +#[test] +#[cfg(unix)] +fn test_three_directories_and_file_and_stdin() { + let s = TestScenario::new(util_name!()); + s.fixtures.mkdir("test_directory3"); + s.fixtures.mkdir("test_directory3/test_directory4"); + s.fixtures.mkdir("test_directory3/test_directory5"); + s.ucmd() + .args(&[ + "test_directory3/test_directory4", + "alpha.txt", + "-", + "file_which_does_not_exist.txt", + "nonewline.txt", // spell-checker:disable-line + "test_directory3/test_directory5", + "test_directory3/../test_directory3/test_directory5", + "test_directory3", + ]) + .pipe_in("stdout bytes") + .fails() + .stderr_is_fixture("three_directories_and_file_and_stdin.stderr.expected") + .stdout_is( + "abcde\nfghij\nklmno\npqrst\nuvwxyz\nstdout bytestext without a trailing newline", // spell-checker:disable-line + ); +} #[test] fn test_output_multi_files_print_all_chars() { + // spell-checker:disable new_ucmd!() .args(&["alpha.txt", "256.txt", "-A", "-n"]) .succeeds() .stdout_only( " 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n \ - 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n \ - 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ \ - !\"#$%&\'()*+,-./0123456789:;\ - <=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^\ - BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^V\ - M-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- \ - M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:\ - M-;M-M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-U\ - M-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-\ - pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?", + 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n \ + 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ \ + !\"#$%&\'()*+,-./0123456789:;\ + <=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^\ + BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^V\ + M-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- \ + M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:\ + M-;M-M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-U\ + M-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-\ + pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?", ); + // spell-checker:enable } #[test] fn test_numbered_lines_no_trailing_newline() { + // spell-checker:disable new_ucmd!() .args(&["nonewline.txt", "alpha.txt", "-n"]) .succeeds() .stdout_only( " 1\ttext without a trailing newlineabcde\n 2\tfghij\n \ - 3\tklmno\n 4\tpqrst\n 5\tuvwxyz\n", + 3\tklmno\n 4\tpqrst\n 5\tuvwxyz\n", ); + // spell-checker:enable } #[test] fn test_stdin_show_nonprinting() { - for same_param in vec!["-v", "--show-nonprinting"] { + for same_param in &["-v", "--show-nonprinting"] { new_ucmd!() .args(&[same_param]) .pipe_in("\t\0\n") @@ -47,7 +254,7 @@ fn test_stdin_show_nonprinting() { #[test] fn test_stdin_show_tabs() { - for same_param in vec!["-T", "--show-tabs"] { + for same_param in &["-T", "--show-tabs"] { new_ucmd!() .args(&[same_param]) .pipe_in("\t\0\n") @@ -58,7 +265,7 @@ fn test_stdin_show_tabs() { #[test] fn test_stdin_show_ends() { - for same_param in vec!["-E", "--show-ends"] { + for &same_param in &["-E", "--show-ends"] { new_ucmd!() .args(&[same_param, "-"]) .pipe_in("\t\0\n\t") @@ -69,7 +276,7 @@ fn test_stdin_show_ends() { #[test] fn test_stdin_show_all() { - for same_param in vec!["-A", "--show-all"] { + for same_param in &["-A", "--show-all"] { new_ucmd!() .args(&[same_param]) .pipe_in("\t\0\n") @@ -98,7 +305,7 @@ fn test_stdin_nonprinting_and_tabs() { #[test] fn test_stdin_squeeze_blank() { - for same_param in vec!["-s", "--squeeze-blank"] { + for same_param in &["-s", "--squeeze-blank"] { new_ucmd!() .arg(same_param) .pipe_in("\n\na\n\n\n\n\nb\n\n\n") @@ -109,7 +316,8 @@ fn test_stdin_squeeze_blank() { #[test] fn test_stdin_number_non_blank() { - for same_param in vec!["-b", "--number-nonblank"] { + // spell-checker:disable-next-line + for same_param in &["-b", "--number-nonblank"] { new_ucmd!() .arg(same_param) .arg("-") @@ -121,7 +329,8 @@ fn test_stdin_number_non_blank() { #[test] fn test_non_blank_overrides_number() { - for same_param in vec!["-b", "--number-nonblank"] { + // spell-checker:disable-next-line + for &same_param in &["-b", "--number-nonblank"] { new_ucmd!() .args(&[same_param, "-"]) .pipe_in("\na\nb\n\n\nc") @@ -132,7 +341,7 @@ fn test_non_blank_overrides_number() { #[test] fn test_squeeze_blank_before_numbering() { - for same_param in vec!["-s", "--squeeze-blank"] { + for &same_param in &["-s", "--squeeze-blank"] { new_ucmd!() .args(&[same_param, "-n", "-"]) .pipe_in("a\n\n\nb") @@ -141,29 +350,96 @@ fn test_squeeze_blank_before_numbering() { } } +/// This tests reading from Unix character devices #[test] -#[cfg(foo)] -fn test_domain_socket() { - use self::tempdir::TempDir; - use self::unix_socket::UnixListener; - use std::io::prelude::*; - use std::thread; +#[cfg(unix)] +fn test_dev_random() { + let mut buf = [0; 2048]; + #[cfg(target_os = "linux")] + const DEV_RANDOM: &str = "/dev/urandom"; - let dir = TempDir::new("unix_socket").expect("failed to create dir"); + #[cfg(not(target_os = "linux"))] + const DEV_RANDOM: &str = "/dev/random"; + + let mut proc = new_ucmd!().args(&[DEV_RANDOM]).run_no_wait(); + let mut proc_stdout = proc.stdout.take().unwrap(); + proc_stdout.read_exact(&mut buf).unwrap(); + + let num_zeroes = buf.iter().fold(0, |mut acc, &n| { + if n == 0 { + acc += 1; + } + acc + }); + // The probability of more than 512 zero bytes is essentially zero if the + // output is truly random. + assert!(num_zeroes < 512); + proc.kill().unwrap(); +} + +/// Reading from /dev/full should return an infinite amount of zero bytes. +/// Wikipedia says there is support on Linux, FreeBSD, and NetBSD. +#[test] +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] +fn test_dev_full() { + let mut buf = [0; 2048]; + let mut proc = new_ucmd!().args(&["/dev/full"]).run_no_wait(); + let mut proc_stdout = proc.stdout.take().unwrap(); + let expected = [0; 2048]; + proc_stdout.read_exact(&mut buf).unwrap(); + assert_eq!(&buf[..], &expected[..]); + proc.kill().unwrap(); +} + +#[test] +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] +fn test_dev_full_show_all() { + let mut buf = [0; 2048]; + let mut proc = new_ucmd!().args(&["-A", "/dev/full"]).run_no_wait(); + let mut proc_stdout = proc.stdout.take().unwrap(); + proc_stdout.read_exact(&mut buf).unwrap(); + + let expected: Vec = (0..buf.len()) + .map(|n| if n & 1 == 0 { b'^' } else { b'@' }) + .collect(); + + assert_eq!(&buf[..], &expected[..]); + proc.kill().unwrap(); +} + +#[test] +#[cfg(unix)] +#[ignore] +fn test_domain_socket() { + use std::io::prelude::*; + use std::sync::{Arc, Barrier}; + use std::thread; + use unix_socket::UnixListener; + + let dir = tempfile::Builder::new() + .prefix("unix_socket") + .tempdir() + .expect("failed to create dir"); let socket_path = dir.path().join("sock"); let listener = UnixListener::bind(&socket_path).expect("failed to create socket"); + // use a barrier to ensure we don't run cat before the listener is setup + let barrier = Arc::new(Barrier::new(2)); + let barrier2 = Arc::clone(&barrier); + let thread = thread::spawn(move || { let mut stream = listener.accept().expect("failed to accept connection").0; + barrier2.wait(); stream .write_all(b"a\tb") .expect("failed to write test data"); }); - new_ucmd!() - .args(&[socket_path]) - .succeeds() - .stdout_only("a\tb"); + let child = new_ucmd!().args(&[socket_path]).run_no_wait(); + barrier.wait(); + let stdout = &child.wait_with_output().unwrap().stdout; + let output = String::from_utf8_lossy(stdout); + assert_eq!("a\tb", output); thread.join().unwrap(); } diff --git a/tests/by-util/test_chgrp.rs b/tests/by-util/test_chgrp.rs index d5afaf3a7..762e922c4 100644 --- a/tests/by-util/test_chgrp.rs +++ b/tests/by-util/test_chgrp.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (words) nosuchgroup groupname + use crate::common::util::*; use rust_users::*; @@ -6,7 +8,34 @@ fn test_invalid_option() { new_ucmd!().arg("-w").arg("/").fails(); } -static DIR: &'static str = "/tmp"; +static DIR: &str = "/tmp"; + +// we should always get both arguments, regardless of whether --reference was used +#[test] +fn test_help() { + new_ucmd!() + .arg("--help") + .succeeds() + .stdout_contains("ARGS:\n \n ... "); +} + +#[test] +fn test_help_ref() { + new_ucmd!() + .arg("--help") + .arg("--reference=ref_file") + .succeeds() + .stdout_contains("ARGS:\n \n ... "); +} + +#[test] +fn test_ref_help() { + new_ucmd!() + .arg("--reference=ref_file") + .arg("--help") + .succeeds() + .stdout_contains("ARGS:\n \n ... "); +} #[test] fn test_invalid_group() { @@ -104,7 +133,7 @@ fn test_reference() { // skip for root or MS-WSL // * MS-WSL is bugged (as of 2019-12-25), allowing non-root accounts su-level privileges for `chgrp` // * for MS-WSL, succeeds and stdout == 'group of /etc retained as root' - if !(get_effective_gid() == 0 || is_wsl()) { + if !(get_effective_gid() == 0 || uucore::os::is_wsl_1()) { new_ucmd!() .arg("-v") .arg("--reference=/etc/passwd") @@ -115,13 +144,56 @@ fn test_reference() { } #[test] -#[cfg(target_os = "macos")] +#[cfg(target_vendor = "apple")] fn test_reference() { new_ucmd!() .arg("-v") - .arg("--reference=/etc/passwd") + .arg("--reference=ref_file") .arg("/etc") - .succeeds(); + .fails() + // group name can differ, so just check the first part of the message + .stderr_contains("chgrp: changing group of '/etc': Operation not permitted (os error 1)\nfailed to change group of '/etc' from "); +} + +#[test] +#[cfg(any(target_os = "linux", target_vendor = "apple"))] +fn test_reference_multi_no_equal() { + new_ucmd!() + .arg("-v") + .arg("--reference") + .arg("ref_file") + .arg("file1") + .arg("file2") + .succeeds() + .stderr_contains("chgrp: group of 'file1' retained as ") + .stderr_contains("\nchgrp: group of 'file2' retained as "); +} + +#[test] +#[cfg(any(target_os = "linux", target_vendor = "apple"))] +fn test_reference_last() { + new_ucmd!() + .arg("-v") + .arg("file1") + .arg("file2") + .arg("file3") + .arg("--reference") + .arg("ref_file") + .succeeds() + .stderr_contains("chgrp: group of 'file1' retained as ") + .stderr_contains("\nchgrp: group of 'file2' retained as ") + .stderr_contains("\nchgrp: group of 'file3' retained as "); +} + +#[test] +fn test_missing_files() { + new_ucmd!() + .arg("-v") + .arg("groupname") + .fails() + .stderr_contains( + "error: The following required arguments were not provided:\n ...\n", + ); } #[test] @@ -133,7 +205,7 @@ fn test_big_p() { .arg("bin") .arg("/proc/self/cwd") .fails() - .stderr_is( + .stderr_contains( "chgrp: changing group of '/proc/self/cwd': Operation not permitted (os error 1)\n", ); } @@ -149,7 +221,7 @@ fn test_big_h() { .arg("bin") .arg("/proc/self/fd") .fails() - .stderr + .stderr_str() .lines() .fold(0, |acc, _| acc + 1) > 1 diff --git a/tests/by-util/test_chmod.rs b/tests/by-util/test_chmod.rs index e7bc72677..186c645e5 100644 --- a/tests/by-util/test_chmod.rs +++ b/tests/by-util/test_chmod.rs @@ -4,10 +4,12 @@ use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; use std::sync::Mutex; extern crate libc; +use self::chmod::strip_minus_from_mode; +extern crate chmod; use self::libc::umask; -static TEST_FILE: &'static str = "file"; -static REFERENCE_FILE: &'static str = "reference"; +static TEST_FILE: &str = "file"; +static REFERENCE_FILE: &str = "reference"; static REFERENCE_PERMS: u32 = 0o247; lazy_static! { static ref UMASK_MUTEX: Mutex<()> = Mutex::new(()); @@ -19,7 +21,7 @@ struct TestCase { after: u32, } -fn mkfile(file: &str, mode: u32) { +fn make_file(file: &str, mode: u32) { OpenOptions::new() .mode(mode) .create(true) @@ -32,30 +34,30 @@ fn mkfile(file: &str, mode: u32) { } fn run_single_test(test: &TestCase, at: AtPath, mut ucmd: UCommand) { - mkfile(&at.plus_as_string(TEST_FILE), test.before); + make_file(&at.plus_as_string(TEST_FILE), test.before); let perms = at.metadata(TEST_FILE).permissions().mode(); if perms != test.before { - panic!(format!( + panic!( "{}: expected: {:o} got: {:o}", "setting permissions on test files before actual test run failed", test.after, perms - )); + ); } for arg in &test.args { ucmd.arg(arg); } let r = ucmd.run(); - if !r.success { - println!("{}", r.stderr); - panic!(format!("{:?}: failed", ucmd.raw)); + if !r.succeeded() { + println!("{}", r.stderr_str()); + panic!("{:?}: failed", ucmd.raw); } let perms = at.metadata(TEST_FILE).permissions().mode(); if perms != test.after { - panic!(format!( + panic!( "{:?}: expected: {:o} got: {:o}", ucmd.raw, test.after, perms - )); + ); } } @@ -67,6 +69,7 @@ fn run_tests(tests: Vec) { } #[test] +#[allow(clippy::unreadable_literal)] fn test_chmod_octal() { let tests = vec![ TestCase { @@ -119,6 +122,8 @@ fn test_chmod_octal() { } #[test] +#[allow(clippy::unreadable_literal)] +// spell-checker:disable-next-line fn test_chmod_ugoa() { let _guard = UMASK_MUTEX.lock(); @@ -214,6 +219,7 @@ fn test_chmod_ugoa() { } #[test] +#[allow(clippy::unreadable_literal)] fn test_chmod_ugo_copy() { let tests = vec![ TestCase { @@ -246,6 +252,7 @@ fn test_chmod_ugo_copy() { } #[test] +#[allow(clippy::unreadable_literal)] fn test_chmod_many_options() { let _guard = UMASK_MUTEX.lock(); @@ -262,6 +269,7 @@ fn test_chmod_many_options() { } #[test] +#[allow(clippy::unreadable_literal)] fn test_chmod_reference_file() { let tests = vec![ TestCase { @@ -276,11 +284,32 @@ fn test_chmod_reference_file() { }, ]; let (at, ucmd) = at_and_ucmd!(); - mkfile(&at.plus_as_string(REFERENCE_FILE), REFERENCE_PERMS); + make_file(&at.plus_as_string(REFERENCE_FILE), REFERENCE_PERMS); run_single_test(&tests[0], at, ucmd); } #[test] +fn test_permission_denied() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir("d/"); + at.mkdir("d/no-x"); + at.mkdir("d/no-x/y"); + + scene.ucmd().arg("u=rw").arg("d/no-x").succeeds(); + + scene + .ucmd() + .arg("-R") + .arg("o=r") + .arg("d") + .fails() + .stderr_is("chmod: 'd/no-x/y': Permission denied"); +} + +#[test] +#[allow(clippy::unreadable_literal)] fn test_chmod_recursive() { let _guard = UMASK_MUTEX.lock(); @@ -290,18 +319,19 @@ fn test_chmod_recursive() { at.mkdir("a/b"); at.mkdir("a/b/c"); at.mkdir("z"); - mkfile(&at.plus_as_string("a/a"), 0o100444); - mkfile(&at.plus_as_string("a/b/b"), 0o100444); - mkfile(&at.plus_as_string("a/b/c/c"), 0o100444); - mkfile(&at.plus_as_string("z/y"), 0o100444); + make_file(&at.plus_as_string("a/a"), 0o100444); + make_file(&at.plus_as_string("a/b/b"), 0o100444); + make_file(&at.plus_as_string("a/b/c/c"), 0o100444); + make_file(&at.plus_as_string("z/y"), 0o100444); - let result = ucmd - .arg("-R") + ucmd.arg("-R") .arg("--verbose") .arg("-r,a+w") .arg("a") .arg("z") - .succeeds(); + .succeeds() + .stderr_contains(&"to 333 (-wx-wx-wx)") + .stderr_contains(&"to 222 (-w--w--w-)"); assert_eq!(at.metadata("z/y").permissions().mode(), 0o100222); assert_eq!(at.metadata("a/a").permissions().mode(), 0o100222); @@ -310,8 +340,6 @@ fn test_chmod_recursive() { println!("mode {:o}", at.metadata("a").permissions().mode()); assert_eq!(at.metadata("a").permissions().mode(), 0o40333); assert_eq!(at.metadata("z").permissions().mode(), 0o40333); - assert!(result.stderr.contains("to 333 (-wx-wx-wx)")); - assert!(result.stderr.contains("to 222 (-w--w--w-)")); unsafe { umask(original_umask); @@ -320,57 +348,145 @@ fn test_chmod_recursive() { #[test] fn test_chmod_non_existing_file() { - let (_at, mut ucmd) = at_and_ucmd!(); - let result = ucmd + new_ucmd!() .arg("-R") .arg("--verbose") .arg("-r,a+w") - .arg("dont-exist") - .fails(); - assert_eq!( - result.stderr, - "chmod: error: no such file or directory 'dont-exist'\n" - ); + .arg("does-not-exist") + .fails() + .stderr_contains(&"cannot access 'does-not-exist': No such file or directory"); } #[test] fn test_chmod_preserve_root() { - let (_at, mut ucmd) = at_and_ucmd!(); - let result = ucmd + new_ucmd!() .arg("-R") .arg("--preserve-root") .arg("755") .arg("/") - .fails(); - assert!(result - .stderr - .contains("chmod: error: it is dangerous to operate recursively on '/'")); + .fails() + .stderr_contains(&"chmod: it is dangerous to operate recursively on '/'"); } #[test] fn test_chmod_symlink_non_existing_file() { - let (at, mut ucmd) = at_and_ucmd!(); - at.symlink_file("/non-existing", "test-long.link"); + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; - let _result = ucmd - .arg("-R") + let non_existing = "test_chmod_symlink_non_existing_file"; + let test_symlink = "test_chmod_symlink_non_existing_file_symlink"; + let expected_stdout = &format!( + "failed to change mode of '{}' from 0000 (---------) to 0000 (---------)", + test_symlink + ); + let expected_stderr = &format!("cannot operate on dangling symlink '{}'", test_symlink); + + at.symlink_file(non_existing, test_symlink); + + // this cannot succeed since the symbolic link dangles + scene + .ucmd() .arg("755") .arg("-v") - .arg("test-long.link") - .fails(); + .arg(test_symlink) + .fails() + .code_is(1) + .stdout_contains(expected_stdout) + .stderr_contains(expected_stderr); + + // this should be the same than with just '-v' but without stderr + scene + .ucmd() + .arg("755") + .arg("-v") + .arg("-f") + .arg(test_symlink) + .run() + .code_is(1) + .no_stderr() + .stdout_contains(expected_stdout); } #[test] -fn test_chmod_symlink_non_existing_recursive() { - let (at, mut ucmd) = at_and_ucmd!(); - at.mkdir("tmp"); - at.symlink_file("/non-existing", "tmp/test-long.link"); +fn test_chmod_symlink_non_existing_file_recursive() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; - let result = ucmd.arg("-R").arg("755").arg("-v").arg("tmp").succeeds(); - // it should be a success - println!("stderr {}", result.stderr); - println!("stdout {}", result.stdout); - assert!(result - .stderr - .contains("neither symbolic link 'tmp/test-long.link' nor referent has been changed")); + let non_existing = "test_chmod_symlink_non_existing_file_recursive"; + let test_symlink = "test_chmod_symlink_non_existing_file_recursive_symlink"; + let test_directory = "test_chmod_symlink_non_existing_file_directory"; + + at.mkdir(test_directory); + at.symlink_file( + non_existing, + &format!("{}/{}", test_directory, test_symlink), + ); + + // this should succeed + scene + .ucmd() + .arg("-R") + .arg("755") + .arg(test_directory) + .succeeds() + .no_stderr() + .no_stdout(); + + let expected_stdout = &format!( + // spell-checker:disable-next-line + "mode of '{}' retained as 0755 (rwxr-xr-x)\nneither symbolic link '{}/{}' nor referent has been changed", + test_directory, test_directory, test_symlink + ); + + // '-v': this should succeed without stderr + scene + .ucmd() + .arg("-R") + .arg("-v") + .arg("755") + .arg(test_directory) + .succeeds() + .stdout_contains(expected_stdout) + .no_stderr(); + + // '-vf': this should be the same than with just '-v' + scene + .ucmd() + .arg("-R") + .arg("-v") + .arg("-f") + .arg("755") + .arg(test_directory) + .succeeds() + .stdout_contains(expected_stdout) + .no_stderr(); +} + +#[test] +fn test_chmod_strip_minus_from_mode() { + let tests = vec![ + // ( before, after ) + ("chmod -v -xw -R FILE", "chmod -v xw -R FILE"), + ("chmod g=rwx FILE -c", "chmod g=rwx FILE -c"), + ( + "chmod -c -R -w,o+w FILE --preserve-root", + "chmod -c -R w,o+w FILE --preserve-root", + ), + ("chmod -c -R +w FILE ", "chmod -c -R +w FILE "), + ("chmod a=r,=xX FILE", "chmod a=r,=xX FILE"), + ( + "chmod -v --reference REF_FILE -R FILE", + "chmod -v --reference REF_FILE -R FILE", + ), + ("chmod -Rvc -w-x FILE", "chmod -Rvc w-x FILE"), + ("chmod 755 -v FILE", "chmod 755 -v FILE"), + ("chmod -v +0004 FILE -R", "chmod -v +0004 FILE -R"), + ("chmod -v -0007 FILE -R", "chmod -v 0007 FILE -R"), + ]; + + for test in tests { + let mut args: Vec = test.0.split(' ').map(|v| v.to_string()).collect(); + let _mode_had_minus_prefix = strip_minus_from_mode(&mut args); + assert_eq!(test.1, args.join(" ")); + } } diff --git a/tests/by-util/test_chown.rs b/tests/by-util/test_chown.rs index 7b663e9c9..86365f51b 100644 --- a/tests/by-util/test_chown.rs +++ b/tests/by-util/test_chown.rs @@ -1,9 +1,39 @@ +// spell-checker:ignore (words) agroupthatdoesntexist auserthatdoesntexist groupname notexisting passgrp + use crate::common::util::*; #[cfg(target_os = "linux")] use rust_users::get_effective_uid; extern crate chown; +// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'. +// If we are running inside the CI and "needle" is in "stderr" skipping this test is +// considered okay. If we are not inside the CI this calls assert!(result.success). +// +// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)" +// +// stderr: "whoami: cannot find name for user ID 1001" +// TODO: Maybe `adduser --uid 1001 username` can put things right? +// +// stderr: "id: cannot find name for group ID 116" +// stderr: "thread 'main' panicked at 'called `Result::unwrap()` on an `Err` +// value: Custom { kind: NotFound, error: "No such id: 1001" }', +// /project/src/uucore/src/lib/features/perms.rs:176:44" +// +fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool { + if !result.succeeded() { + println!("result.stdout = {}", result.stdout_str()); + println!("result.stderr = {}", result.stderr_str()); + if is_ci() && result.stderr_str().contains(needle) { + println!("test skipped:"); + return true; + } else { + result.success(); + } + } + false +} + #[cfg(test)] mod test_passgrp { use super::chown::entries::{gid2grp, grp2gid, uid2usr, usr2uid}; @@ -11,7 +41,7 @@ mod test_passgrp { #[test] fn test_usr2uid() { assert_eq!(0, usr2uid("root").unwrap()); - assert!(usr2uid("88888888").is_err()); + assert!(usr2uid("88_888_888").is_err()); assert!(usr2uid("auserthatdoesntexist").is_err()); } @@ -22,14 +52,14 @@ mod test_passgrp { } else { assert_eq!(0, grp2gid("wheel").unwrap()); } - assert!(grp2gid("88888888").is_err()); + assert!(grp2gid("88_888_888").is_err()); assert!(grp2gid("agroupthatdoesntexist").is_err()); } #[test] fn test_uid2usr() { assert_eq!("root", uid2usr(0).unwrap()); - assert!(uid2usr(88888888).is_err()); + assert!(uid2usr(88_888_888).is_err()); } #[test] @@ -39,7 +69,7 @@ mod test_passgrp { } else { assert_eq!("wheel", gid2grp(0).unwrap()); } - assert!(gid2grp(88888888).is_err()); + assert!(gid2grp(88_888_888).is_err()); } } @@ -49,338 +79,427 @@ fn test_invalid_option() { } #[test] -fn test_chown_myself() { +fn test_chown_only_owner() { // test chown username file.txt + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let result = scene.cmd("whoami").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") { return; } - println!("results {}", result.stdout); - let username = result.stdout.trim_end(); - - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; + let user_name = String::from(result.stdout_str().trim()); + assert!(!user_name.is_empty()); + let file1 = "test_chown_file1"; at.touch(file1); - let result = ucmd.arg(username).arg(file1).run(); - println!("results stdout {}", result.stdout); - println!("results stderr {}", result.stderr); - if is_ci() && result.stderr.contains("invalid user") { - // In the CI, some server are failing to return id. - // As seems to be a configuration issue, ignoring it - return; - } - assert!(result.success); -} -#[test] -fn test_chown_myself_second() { - // test chown username: file.txt - let scene = TestScenario::new(util_name!()); - let result = scene.cmd("whoami").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it - return; - } - println!("results {}", result.stdout); - - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; - - at.touch(file1); - let result = ucmd - .arg(result.stdout.trim_end().to_owned() + ":") + // since only superuser can change owner, we have to change from ourself to ourself + let result = scene + .ucmd() + .arg(user_name) + .arg("--verbose") .arg(file1) .run(); + result.stderr_contains(&"retained as"); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); + // try to change to another existing user, e.g. 'root' + scene + .ucmd() + .arg("root") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); } #[test] -fn test_chown_myself_group() { - // test chown username:group file.txt +fn test_chown_only_owner_colon() { + // test chown username: file.txt + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let result = scene.cmd("whoami").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") { return; } - println!("user name = {}", result.stdout); - let username = result.stdout.trim_end(); + let user_name = String::from(result.stdout_str().trim()); + assert!(!user_name.is_empty()); + + let file1 = "test_chown_file1"; + at.touch(file1); + + scene + .ucmd() + .arg(format!("{}:", user_name)) + .arg("--verbose") + .arg(file1) + .succeeds() + .stderr_contains(&"retained as"); + + scene + .ucmd() + .arg("root:") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); +} + +#[test] +fn test_chown_only_colon() { + // test chown : file.txt + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let file1 = "test_chown_file1"; + at.touch(file1); + + // expected: + // $ chown -v : file.txt 2>out_err ; echo $? ; cat out_err + // ownership of 'file.txt' retained + // 0 + let result = scene.ucmd().arg(":").arg("--verbose").arg(file1).run(); + if skipping_test_is_okay(&result, "No such id") { + return; + } + result.stderr_contains(&"retained as"); // TODO: verbose is not printed to stderr in GNU chown + + // test chown : file.txt + // expected: + // $ chown -v :: file.txt 2>out_err ; echo $? ; cat out_err + // 1 + // chown: invalid group: '::' + scene + .ucmd() + .arg("::") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"invalid group: '::'"); +} + +#[test] +fn test_chown_failed_stdout() { + // test chown root file.txt + + // TODO: implement once output "failed to change" to stdout is fixed + // expected: + // $ chown -v root file.txt 2>out_err ; echo $? ; cat out_err + // failed to change ownership of 'file.txt' from jhs to root + // 1 + // chown: changing ownership of 'file.txt': Operation not permitted +} + +#[test] +fn test_chown_owner_group() { + // test chown username:group file.txt + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let result = scene.cmd("whoami").run(); + if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") { + return; + } + + let user_name = String::from(result.stdout_str().trim()); + assert!(!user_name.is_empty()); + + let file1 = "test_chown_file1"; + at.touch(file1); let result = scene.cmd("id").arg("-gn").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + if skipping_test_is_okay(&result, "id: cannot find name for group ID") { return; } - println!("group name = {}", result.stdout); - let group = result.stdout.trim_end(); + let group_name = String::from(result.stdout_str().trim()); + assert!(!group_name.is_empty()); - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; - let perm = username.to_owned() + ":" + group; - at.touch(file1); - let result = ucmd.arg(perm).arg(file1).run(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if is_ci() && result.stderr.contains("chown: invalid group:") { - // With some Ubuntu into the CI, we can get this answer + let result = scene + .ucmd() + .arg(format!("{}:{}", user_name, group_name)) + .arg("--verbose") + .arg(file1) + .run(); + if skipping_test_is_okay(&result, "chown: invalid group:") { return; } - assert!(result.success); + result.stderr_contains(&"retained as"); + + // TODO: on macos group name is not recognized correctly: "chown: invalid group: 'root:root' + #[cfg(any(windows, all(unix, not(target_os = "macos"))))] + scene + .ucmd() + .arg("root:root") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); } #[test] +// TODO: on macos group name is not recognized correctly: "chown: invalid group: ':groupname' +#[cfg(any(windows, all(unix, not(target_os = "macos"))))] fn test_chown_only_group() { // test chown :group file.txt + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let result = scene.cmd("whoami").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") { return; } - println!("results {}", result.stdout); + let user_name = String::from(result.stdout_str().trim()); + assert!(!user_name.is_empty()); - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; - let perm = ":".to_owned() + result.stdout.trim_end(); + let file1 = "test_chown_file1"; at.touch(file1); - let result = ucmd.arg(perm).arg(file1).run(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - - if is_ci() && result.stderr.contains("Operation not permitted") { + let result = scene + .ucmd() + .arg(format!(":{}", user_name)) + .arg("--verbose") + .arg(file1) + .run(); + if is_ci() && result.stderr_str().contains("Operation not permitted") { // With ubuntu with old Rust in the CI, we can get an error return; } - if is_ci() && result.stderr.contains("chown: invalid group:") { + if is_ci() && result.stderr_str().contains("chown: invalid group:") { // With mac into the CI, we can get this answer return; } - assert!(result.success); + result.stderr_contains(&"retained as"); + result.success(); + + scene + .ucmd() + .arg(":root") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); } #[test] -fn test_chown_only_id() { +fn test_chown_only_user_id() { // test chown 1111 file.txt - let result = TestScenario::new("id").ucmd_keepenv().arg("-u").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let result = scene.cmd_keepenv("id").arg("-u").run(); + if skipping_test_is_okay(&result, "id: cannot find name for group ID") { return; } - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let id = String::from(result.stdout.trim()); - - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; + let user_id = String::from(result.stdout_str().trim()); + assert!(!user_id.is_empty()); + let file1 = "test_chown_file1"; at.touch(file1); - let result = ucmd.arg(id).arg(file1).run(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if is_ci() && result.stderr.contains("chown: invalid user:") { - // With some Ubuntu into the CI, we can get this answer + let result = scene.ucmd().arg(user_id).arg("--verbose").arg(file1).run(); + if skipping_test_is_okay(&result, "invalid user") { + // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)" + // stderr: "chown: invalid user: '1001' return; } - assert!(result.success); + result.stderr_contains(&"retained as"); + + scene + .ucmd() + .arg("0") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); } #[test] fn test_chown_only_group_id() { // test chown :1111 file.txt - let result = TestScenario::new("id").ucmd_keepenv().arg("-g").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let result = scene.cmd_keepenv("id").arg("-g").run(); + if skipping_test_is_okay(&result, "id: cannot find name for group ID") { return; } - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let id = String::from(result.stdout.trim()); - - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; + let group_id = String::from(result.stdout_str().trim()); + assert!(!group_id.is_empty()); + let file1 = "test_chown_file1"; at.touch(file1); - let perm = ":".to_owned() + &id; - let result = ucmd.arg(perm).arg(file1).run(); - - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if is_ci() && result.stderr.contains("chown: invalid group:") { + let result = scene + .ucmd() + .arg(format!(":{}", group_id)) + .arg("--verbose") + .arg(file1) + .run(); + if skipping_test_is_okay(&result, "chown: invalid group:") { // With mac into the CI, we can get this answer return; } - assert!(result.success); + result.stderr_contains(&"retained as"); + + // Apparently on CI "macos-latest, x86_64-apple-darwin, feat_os_macos" + // the process has the rights to change from runner:staff to runner:wheel + #[cfg(any(windows, all(unix, not(target_os = "macos"))))] + scene + .ucmd() + .arg(":0") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); } #[test] -fn test_chown_both_id() { +fn test_chown_owner_group_id() { // test chown 1111:1111 file.txt - let result = TestScenario::new("id").ucmd_keepenv().arg("-u").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let result = scene.cmd_keepenv("id").arg("-u").run(); + if skipping_test_is_okay(&result, "id: cannot find name for group ID") { return; } - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let id_user = String::from(result.stdout.trim()); + let user_id = String::from(result.stdout_str().trim()); + assert!(!user_id.is_empty()); - let result = TestScenario::new("id").ucmd_keepenv().arg("-g").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + let result = scene.cmd_keepenv("id").arg("-g").run(); + if skipping_test_is_okay(&result, "id: cannot find name for group ID") { return; } - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let id_group = String::from(result.stdout.trim()); - - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; + let group_id = String::from(result.stdout_str().trim()); + assert!(!group_id.is_empty()); + let file1 = "test_chown_file1"; at.touch(file1); - let perm = id_user + &":".to_owned() + &id_group; - let result = ucmd.arg(perm).arg(file1).run(); - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - - if is_ci() && result.stderr.contains("invalid user") { - // In the CI, some server are failing to return id. - // As seems to be a configuration issue, ignoring it + let result = scene + .ucmd() + .arg(format!("{}:{}", user_id, group_id)) + .arg("--verbose") + .arg(file1) + .run(); + if skipping_test_is_okay(&result, "invalid user") { + // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)" + // stderr: "chown: invalid user: '1001:116' return; } + result.stderr_contains(&"retained as"); - assert!(result.success); + scene + .ucmd() + .arg("0:0") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); } #[test] -fn test_chown_both_mix() { - // test chown 1111:1111 file.txt - let result = TestScenario::new("id").ucmd_keepenv().arg("-u").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it +fn test_chown_owner_group_mix() { + // test chown 1111:group file.txt + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let result = scene.cmd_keepenv("id").arg("-u").run(); + if skipping_test_is_okay(&result, "id: cannot find name for group ID") { return; } - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let id_user = String::from(result.stdout.trim()); + let user_id = String::from(result.stdout_str().trim()); + assert!(!user_id.is_empty()); - let result = TestScenario::new("id").ucmd_keepenv().arg("-gn").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + let result = scene.cmd_keepenv("id").arg("-gn").run(); + if skipping_test_is_okay(&result, "id: cannot find name for group ID") { return; } - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let group_name = String::from(result.stdout.trim()); - - let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; + let group_name = String::from(result.stdout_str().trim()); + assert!(!group_name.is_empty()); + let file1 = "test_chown_file1"; at.touch(file1); - let perm = id_user + &":".to_owned() + &group_name; - let result = ucmd.arg(perm).arg(file1).run(); + let result = scene + .ucmd() + .arg(format!("{}:{}", user_id, group_name)) + .arg("--verbose") + .arg(file1) + .run(); + result.stderr_contains(&"retained as"); - if is_ci() && result.stderr.contains("invalid user") { - // In the CI, some server are failing to return id. - // As seems to be a configuration issue, ignoring it - return; - } - assert!(result.success); + // TODO: on macos group name is not recognized correctly: "chown: invalid group: '0:root' + #[cfg(any(windows, all(unix, not(target_os = "macos"))))] + scene + .ucmd() + .arg("0:root") + .arg("--verbose") + .arg(file1) + .fails() + .stderr_contains(&"failed to change"); } #[test] fn test_chown_recursive() { let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let result = scene.cmd("whoami").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") { return; } - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let username = result.stdout.trim_end(); + let user_name = String::from(result.stdout_str().trim()); + assert!(!user_name.is_empty()); - let (at, mut ucmd) = at_and_ucmd!(); - at.mkdir("a"); - at.mkdir("a/b"); - at.mkdir("a/b/c"); + at.mkdir_all("a/b/c"); at.mkdir("z"); at.touch(&at.plus_as_string("a/a")); at.touch(&at.plus_as_string("a/b/b")); at.touch(&at.plus_as_string("a/b/c/c")); at.touch(&at.plus_as_string("z/y")); - let result = ucmd + let result = scene + .ucmd() .arg("-R") .arg("--verbose") - .arg(username) + .arg(user_name) .arg("a") .arg("z") .run(); - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if is_ci() && result.stderr.contains("invalid user") { - // In the CI, some server are failing to return id. - // As seems to be a configuration issue, ignoring it - return; - } - - assert!(result.stderr.contains("ownership of 'a/a' retained as")); - assert!(result.stderr.contains("ownership of 'z/y' retained as")); - assert!(result.success); + result.stderr_contains(&"ownership of 'a/a' retained as"); + result.stderr_contains(&"ownership of 'z/y' retained as"); } #[test] fn test_root_preserve() { let scene = TestScenario::new(util_name!()); + let result = scene.cmd("whoami").run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") { return; } - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let username = result.stdout.trim_end(); + let user_name = String::from(result.stdout_str().trim()); + assert!(!user_name.is_empty()); - let result = new_ucmd!() + let result = scene + .ucmd() .arg("--preserve-root") .arg("-R") - .arg(username) + .arg(user_name) .arg("/") .fails(); - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if is_ci() && result.stderr.contains("invalid user") { - // In the CI, some server are failing to return id. - // As seems to be a configuration issue, ignoring it - return; - } - assert!(result - .stderr - .contains("chown: it is dangerous to operate recursively")); + result.stderr_contains(&"chown: it is dangerous to operate recursively"); } #[cfg(target_os = "linux")] @@ -392,8 +511,34 @@ fn test_big_p() { .arg("bin") .arg("/proc/self/cwd") .fails() - .stderr_is( - "chown: changing ownership of '/proc/self/cwd': Operation not permitted (os error 1)\n", + .stderr_contains( + "chown: changing ownership of '/proc/self/cwd': Operation not permitted (os error 1)", ); } } + +#[test] +fn test_chown_file_notexisting() { + // test chown username not_existing + + let scene = TestScenario::new(util_name!()); + + let result = scene.cmd("whoami").run(); + if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") { + return; + } + let user_name = String::from(result.stdout_str().trim()); + assert!(!user_name.is_empty()); + + let _result = scene + .ucmd() + .arg(user_name) + .arg("--verbose") + .arg("not_existing") + .fails(); + + // TODO: uncomment once "failed to change ownership of '{}' to {}" added to stdout + // result.stderr_contains(&"retained as"); + // TODO: uncomment once message changed from "cannot dereference" to "cannot access" + // result.stderr_contains(&"cannot access 'not_existing': No such file or directory"); +} diff --git a/tests/by-util/test_chroot.rs b/tests/by-util/test_chroot.rs index 651491045..3bac07d44 100644 --- a/tests/by-util/test_chroot.rs +++ b/tests/by-util/test_chroot.rs @@ -1 +1,91 @@ -// ToDO: add tests +// spell-checker:ignore (words) araba newroot userspec + +use crate::common::util::*; + +#[test] +fn test_missing_operand() { + let result = new_ucmd!().run(); + + assert!(result + .stderr_str() + .starts_with("error: The following required arguments were not provided")); + + assert!(result.stderr_str().contains("")); +} + +#[test] +fn test_enter_chroot_fails() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("jail"); + + let result = ucmd.arg("jail").fails(); + + assert!(result + .stderr_str() + .starts_with("chroot: cannot chroot to jail: Operation not permitted (os error 1)")); +} + +#[test] +fn test_no_such_directory() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.touch(&at.plus_as_string("a")); + + ucmd.arg("a") + .fails() + .stderr_is("chroot: cannot change root directory to `a`: no such directory"); +} + +#[test] +fn test_invalid_user_spec() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("a"); + + let result = ucmd.arg("a").arg("--userspec=ARABA:").fails(); + + assert!(result.stderr_str().starts_with("chroot: invalid userspec")); +} + +#[test] +fn test_preference_of_userspec() { + let scene = TestScenario::new(util_name!()); + let result = scene.cmd("whoami").run(); + if is_ci() && result.stderr_str().contains("No such user/group") { + // In the CI, some server are failing to return whoami. + // As seems to be a configuration issue, ignoring it + return; + } + println!("result.stdout = {}", result.stdout_str()); + println!("result.stderr = {}", result.stderr_str()); + let username = result.stdout_str().trim_end(); + + let ts = TestScenario::new("id"); + let result = ts.cmd("id").arg("-g").arg("-n").run(); + println!("result.stdout = {}", result.stdout_str()); + println!("result.stderr = {}", result.stderr_str()); + + if is_ci() && result.stderr_str().contains("cannot find name for user ID") { + // In the CI, some server are failing to return id. + // As seems to be a configuration issue, ignoring it + return; + } + + let group_name = result.stdout_str().trim_end(); + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("a"); + + let result = ucmd + .arg("a") + .arg("--user") + .arg("fake") + .arg("-G") + .arg("ABC,DEF") + .arg(format!("--userspec={}:{}", username, group_name)) + .run(); + + println!("result.stdout = {}", result.stdout_str()); + println!("result.stderr = {}", result.stderr_str()); +} diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index d0bf5ffc8..9590c1ac5 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (words) asdf + use crate::common::util::*; #[test] @@ -24,3 +26,87 @@ fn test_stdin() { .succeeds() .stdout_is_fixture("stdin.expected"); } + +#[test] +fn test_empty() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.touch("a"); + + ucmd.arg("a") + .succeeds() + .no_stderr() + .normalized_newlines_stdout_is("4294967295 0 a\n"); +} + +#[test] +fn test_arg_overrides_stdin() { + let (at, mut ucmd) = at_and_ucmd!(); + let input = "foobarfoobar"; // spell-checker:disable-line + + at.touch("a"); + + ucmd.arg("a") + .pipe_in(input.as_bytes()) + // the command might have exited before all bytes have been pipe in. + // in that case, we don't care about the error (broken pipe) + .ignore_stdin_write_error() + .succeeds() + .no_stderr() + .normalized_newlines_stdout_is("4294967295 0 a\n"); +} + +#[test] +fn test_invalid_file() { + let ts = TestScenario::new(util_name!()); + let at = ts.fixtures.clone(); + + let folder_name = "asdf"; + + // First check when file doesn't exist + ts.ucmd() + .arg(folder_name) + .fails() + .no_stdout() + .stderr_contains("cksum: 'asdf' No such file or directory"); + + // Then check when the file is of an invalid type + at.mkdir(folder_name); + ts.ucmd() + .arg(folder_name) + .fails() + .no_stdout() + .stderr_contains("cksum: 'asdf' Is a directory"); +} + +// Make sure crc is correct for files larger than 32 bytes +// but <128 bytes (1 fold pclmul) // spell-checker:disable-line +#[test] +fn test_crc_for_bigger_than_32_bytes() { + let (_, mut ucmd) = at_and_ucmd!(); + + let result = ucmd.arg("chars.txt").succeeds(); + + let mut stdout_split = result.stdout_str().split(' '); + + let cksum: i64 = stdout_split.next().unwrap().parse().unwrap(); + let bytes_cnt: i64 = stdout_split.next().unwrap().parse().unwrap(); + + assert_eq!(cksum, 586_047_089); + assert_eq!(bytes_cnt, 16); +} + +#[test] +fn test_stdin_larger_than_128_bytes() { + let (_, mut ucmd) = at_and_ucmd!(); + + let result = ucmd.arg("larger_than_2056_bytes.txt").succeeds(); + + let mut stdout_split = result.stdout_str().split(' '); + + let cksum: i64 = stdout_split.next().unwrap().parse().unwrap(); + let bytes_cnt: i64 = stdout_split.next().unwrap().parse().unwrap(); + + assert_eq!(cksum, 945_881_979); + assert_eq!(bytes_cnt, 2058); +} diff --git a/tests/by-util/test_comm.rs b/tests/by-util/test_comm.rs index 23aeb8309..01cdcc985 100644 --- a/tests/by-util/test_comm.rs +++ b/tests/by-util/test_comm.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (words) defaultcheck nocheck + use crate::common::util::*; #[test] @@ -33,19 +35,19 @@ fn ab_dash_three() { } #[test] -fn aempty() { +fn a_empty() { new_ucmd!() .args(&["a", "empty"]) .succeeds() - .stdout_only_fixture("aempty.expected"); + .stdout_only_fixture("aempty.expected"); // spell-checker:disable-line } #[test] -fn emptyempty() { +fn empty_empty() { new_ucmd!() .args(&["empty", "empty"]) .succeeds() - .stdout_only_fixture("emptyempty.expected"); + .stdout_only_fixture("emptyempty.expected"); // spell-checker:disable-line } #[cfg_attr(not(feature = "test_unimplemented"), ignore)] @@ -68,13 +70,13 @@ fn output_delimiter_require_arg() { // even though (info) documentation suggests this is an option // in latest GNU Coreutils comm, it actually is not. -// this test is essentially an alarm in case someone well-intendingly -// implements it. +// this test is essentially an alarm in case some well-intending +// developer implements it. //marked as unimplemented as error message not set yet. #[cfg_attr(not(feature = "test_unimplemented"), ignore)] #[test] fn zero_terminated() { - for param in vec!["-z", "--zero-terminated"] { + for ¶m in &["-z", "--zero-terminated"] { new_ucmd!() .args(&[param, "a", "b"]) .fails() diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index b96bd4e29..19f93e499 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE + use crate::common::util::*; #[cfg(not(windows))] use std::fs::set_permissions; @@ -5,11 +7,15 @@ use std::fs::set_permissions; #[cfg(not(windows))] use std::os::unix::fs; +#[cfg(target_os = "linux")] +use std::os::unix::fs::PermissionsExt; #[cfg(windows)] use std::os::windows::fs::symlink_file; #[cfg(target_os = "linux")] use filetime::FileTime; +#[cfg(target_os = "linux")] +use rlimit::Resource; #[cfg(not(windows))] use std::env; #[cfg(target_os = "linux")] @@ -31,18 +37,20 @@ static TEST_COPY_FROM_FOLDER: &str = "hello_dir_with_file/"; static TEST_COPY_FROM_FOLDER_FILE: &str = "hello_dir_with_file/hello_world.txt"; static TEST_COPY_TO_FOLDER_NEW: &str = "hello_dir_new"; static TEST_COPY_TO_FOLDER_NEW_FILE: &str = "hello_dir_new/hello_world.txt"; +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +static TEST_MOUNT_COPY_FROM_FOLDER: &str = "dir_with_mount"; +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +static TEST_MOUNT_MOUNTPOINT: &str = "mount"; +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +static TEST_MOUNT_OTHER_FILESYSTEM_FILE: &str = "mount/DO_NOT_copy_me.txt"; #[test] fn test_cp_cp() { let (at, mut ucmd) = at_and_ucmd!(); // Invoke our binary to make the copy. - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HELLO_WORLD_DEST) - .run(); - - // Check that the exit code represents a successful copy. - assert!(result.success); + .succeeds(); // Check the content of the destination file that was copied. assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); @@ -51,12 +59,9 @@ fn test_cp_cp() { #[test] fn test_cp_existing_target() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_EXISTING_FILE) - .run(); - - assert!(result.success); + .succeeds(); // Check the content of the destination file assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); @@ -68,69 +73,55 @@ fn test_cp_existing_target() { #[test] fn test_cp_duplicate_files() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_COPY_TO_FOLDER) - .run(); - - assert!(result.success); - assert!(result.stderr.contains("specified more than once")); + .succeeds() + .stderr_contains("specified more than once"); assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); } #[test] fn test_cp_multiple_files_target_is_file() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd + new_ucmd!() .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_EXISTING_FILE) - .run(); - - assert!(!result.success); - assert!(result.stderr.contains("not a directory")); + .fails() + .stderr_contains("not a directory"); } #[test] fn test_cp_directory_not_recursive() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd + new_ucmd!() .arg(TEST_COPY_TO_FOLDER) .arg(TEST_HELLO_WORLD_DEST) - .run(); - - assert!(!result.success); - assert!(result.stderr.contains("omitting directory")); + .fails() + .stderr_contains("omitting directory"); } #[test] fn test_cp_multiple_files() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HOW_ARE_YOU_SOURCE) .arg(TEST_COPY_TO_FOLDER) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); assert_eq!(at.read(TEST_HOW_ARE_YOU_DEST), "How are you?\n"); } #[test] // FixME: for MacOS, this has intermittent failures; track repair progress at GH:uutils/coreutils/issues/1590 -#[cfg(not(macos))] +#[cfg(not(target_os = "macos"))] fn test_cp_recurse() { let (at, mut ucmd) = at_and_ucmd!(); - - let result = ucmd - .arg("-r") + ucmd.arg("-r") .arg(TEST_COPY_FROM_FOLDER) .arg(TEST_COPY_TO_FOLDER_NEW) - .run(); + .succeeds(); - assert!(result.success); // Check the content of the destination file that was copied. assert_eq!(at.read(TEST_COPY_TO_FOLDER_NEW_FILE), "Hello, World!\n"); } @@ -138,81 +129,66 @@ fn test_cp_recurse() { #[test] fn test_cp_with_dirs_t() { let (at, mut ucmd) = at_and_ucmd!(); - - //using -t option - let result_to_dir_t = ucmd - .arg("-t") + ucmd.arg("-t") .arg(TEST_COPY_TO_FOLDER) .arg(TEST_HELLO_WORLD_SOURCE) - .run(); - assert!(result_to_dir_t.success); + .succeeds(); assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); } #[test] // FixME: for MacOS, this has intermittent failures; track repair progress at GH:uutils/coreutils/issues/1590 -#[cfg(not(macos))] +#[cfg(not(target_os = "macos"))] fn test_cp_with_dirs() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; - //using -t option - let result_to_dir = scene + scene .ucmd() .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_COPY_TO_FOLDER) - .run(); - assert!(result_to_dir.success); + .succeeds(); assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); - let result_from_dir = scene + scene .ucmd() .arg(TEST_COPY_FROM_FOLDER_FILE) .arg(TEST_HELLO_WORLD_DEST) - .run(); - assert!(result_from_dir.success); + .succeeds(); assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); } #[test] fn test_cp_arg_target_directory() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("-t") .arg(TEST_COPY_TO_FOLDER) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); } #[test] fn test_cp_arg_no_target_directory() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd + new_ucmd!() .arg(TEST_HELLO_WORLD_SOURCE) .arg("-v") .arg("-T") .arg(TEST_COPY_TO_FOLDER) - .run(); - - assert!(!result.success); - assert!(result.stderr.contains("cannot overwrite directory")); + .fails() + .stderr_contains("cannot overwrite directory"); } #[test] fn test_cp_arg_interactive() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd + new_ucmd!() .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HOW_ARE_YOU_SOURCE) .arg("-i") .pipe_in("N\n") - .run(); - - assert!(result.success); - assert!(result.stderr.contains("Not overwriting")); + .succeeds() + .stderr_contains("Not overwriting"); } #[test] @@ -221,39 +197,33 @@ fn test_cp_arg_link() { use std::os::linux::fs::MetadataExt; let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("--link") .arg(TEST_HELLO_WORLD_DEST) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.metadata(TEST_HELLO_WORLD_SOURCE).st_nlink(), 2); } #[test] fn test_cp_arg_symlink() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("--symbolic-link") .arg(TEST_HELLO_WORLD_DEST) - .run(); + .succeeds(); - assert!(result.success); assert!(at.is_symlink(TEST_HELLO_WORLD_DEST)); } #[test] fn test_cp_arg_no_clobber() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) - .arg("--no-clobber") + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HOW_ARE_YOU_SOURCE) - .run(); + .arg("--no-clobber") + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n"); } @@ -261,34 +231,31 @@ fn test_cp_arg_no_clobber() { fn test_cp_arg_no_clobber_twice() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; + at.touch("source.txt"); - let result = scene + scene .ucmd() .arg("--no-clobber") .arg("source.txt") .arg("dest.txt") - .run(); + .succeeds() + .no_stderr(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(result.stderr.is_empty()); assert_eq!(at.read("source.txt"), ""); at.append("source.txt", "some-content"); - let result = scene + scene .ucmd() .arg("--no-clobber") .arg("source.txt") .arg("dest.txt") - .run(); + .succeeds() + .stdout_does_not_contain("Not overwriting"); - assert!(result.success); assert_eq!(at.read("source.txt"), "some-content"); // Should be empty as the "no-clobber" should keep // the previous version assert_eq!(at.read("dest.txt"), ""); - assert!(!result.stderr.contains("Not overwriting")); } #[test] @@ -305,16 +272,11 @@ fn test_cp_arg_force() { permissions.set_readonly(true); set_permissions(at.plus(TEST_HELLO_WORLD_DEST), permissions).unwrap(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("--force") .arg(TEST_HELLO_WORLD_DEST) - .run(); + .succeeds(); - println!("{:?}", result.stderr); - println!("{:?}", result.stdout); - - assert!(result.success); assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); } @@ -336,13 +298,11 @@ fn test_cp_arg_remove_destination() { permissions.set_readonly(true); set_permissions(at.plus(TEST_HELLO_WORLD_DEST), permissions).unwrap(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("--remove-destination") .arg(TEST_HELLO_WORLD_DEST) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); } @@ -350,13 +310,43 @@ fn test_cp_arg_remove_destination() { fn test_cp_arg_backup() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) - .arg("--backup") + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HOW_ARE_YOU_SOURCE) - .run(); + .arg("-b") + .succeeds(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_arg_backup_with_other_args() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .arg("-vbL") + .succeeds(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_arg_backup_arg_first() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); assert_eq!( at.read(&*format!("{}~", TEST_HOW_ARE_YOU_SOURCE)), @@ -368,14 +358,13 @@ fn test_cp_arg_backup() { fn test_cp_arg_suffix() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) + .arg("-b") .arg("--suffix") .arg(".bak") .arg(TEST_HOW_ARE_YOU_SOURCE) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); assert_eq!( at.read(&*format!("{}.bak", TEST_HOW_ARE_YOU_SOURCE)), @@ -384,10 +373,210 @@ fn test_cp_arg_suffix() { } #[test] -fn test_cp_deref_conflicting_options() { - let (_at, mut ucmd) = at_and_ucmd!(); +fn test_cp_custom_backup_suffix_via_env() { + let (at, mut ucmd) = at_and_ucmd!(); + let suffix = "super-suffix-of-the-century"; - ucmd.arg("-LP") + ucmd.arg("-b") + .env("SIMPLE_BACKUP_SUFFIX", suffix) + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}{}", TEST_HOW_ARE_YOU_SOURCE, suffix)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_backup_numbered_with_t() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=t") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}.~1~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_backup_numbered() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=numbered") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}.~1~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_backup_existing() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=existing") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_backup_nil() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=nil") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_numbered_if_existing_backup_existing() { + let (at, mut ucmd) = at_and_ucmd!(); + let existing_backup = &*format!("{}.~1~", TEST_HOW_ARE_YOU_SOURCE); + at.touch(existing_backup); + + ucmd.arg("--backup=existing") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(TEST_HOW_ARE_YOU_SOURCE)); + assert!(at.file_exists(existing_backup)); + assert_eq!( + at.read(&*format!("{}.~2~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_numbered_if_existing_backup_nil() { + let (at, mut ucmd) = at_and_ucmd!(); + let existing_backup = &*format!("{}.~1~", TEST_HOW_ARE_YOU_SOURCE); + + at.touch(existing_backup); + ucmd.arg("--backup=nil") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(TEST_HOW_ARE_YOU_SOURCE)); + assert!(at.file_exists(existing_backup)); + assert_eq!( + at.read(&*format!("{}.~2~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_backup_simple() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=simple") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_backup_never() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=never") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}~", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + +#[test] +fn test_cp_backup_none() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=none") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert!(!at.file_exists(&format!("{}~", TEST_HOW_ARE_YOU_SOURCE))); +} + +#[test] +fn test_cp_backup_off() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup=off") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds() + .no_stderr(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert!(!at.file_exists(&format!("{}~", TEST_HOW_ARE_YOU_SOURCE))); +} + +#[test] +fn test_cp_backup_no_clobber_conflicting_options() { + let (_, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup") + .arg("--no-clobber") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .fails() + .stderr_is("cp: options --backup and --no-clobber are mutually exclusive\nTry 'cp --help' for more information."); +} + +#[test] +fn test_cp_deref_conflicting_options() { + new_ucmd!() + .arg("-LP") .arg(TEST_COPY_TO_FOLDER) .arg(TEST_HELLO_WORLD_SOURCE) .fails(); @@ -395,8 +584,7 @@ fn test_cp_deref_conflicting_options() { #[test] fn test_cp_deref() { - let scene = TestScenario::new(util_name!()); - let at = &scene.fixtures; + let (at, mut ucmd) = at_and_ucmd!(); #[cfg(not(windows))] let _r = fs::symlink( @@ -409,16 +597,12 @@ fn test_cp_deref() { at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), ); //using -L option - let result = scene - .ucmd() - .arg("-L") + ucmd.arg("-L") .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HELLO_WORLD_SOURCE_SYMLINK) .arg(TEST_COPY_TO_FOLDER) - .run(); + .succeeds(); - // Check that the exit code represents a successful copy. - assert!(result.success); let path_to_new_symlink = at .subdir .join(TEST_COPY_TO_FOLDER) @@ -434,12 +618,11 @@ fn test_cp_deref() { // Check the content of the destination file that was copied. assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); let path_to_check = path_to_new_symlink.to_str().unwrap(); - assert_eq!(at.read(&path_to_check), "Hello, World!\n"); + assert_eq!(at.read(path_to_check), "Hello, World!\n"); } #[test] fn test_cp_no_deref() { - let scene = TestScenario::new(util_name!()); - let at = &scene.fixtures; + let (at, mut ucmd) = at_and_ucmd!(); #[cfg(not(windows))] let _r = fs::symlink( @@ -452,16 +635,12 @@ fn test_cp_no_deref() { at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), ); //using -P option - let result = scene - .ucmd() - .arg("-P") + ucmd.arg("-P") .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HELLO_WORLD_SOURCE_SYMLINK) .arg(TEST_COPY_TO_FOLDER) - .run(); + .succeeds(); - // Check that the exit code represents a successful copy. - assert!(result.success); let path_to_new_symlink = at .subdir .join(TEST_COPY_TO_FOLDER) @@ -476,7 +655,7 @@ fn test_cp_no_deref() { // Check the content of the destination file that was copied. assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); let path_to_check = path_to_new_symlink.to_str().unwrap(); - assert_eq!(at.read(&path_to_check), "Hello, World!\n"); + assert_eq!(at.read(path_to_check), "Hello, World!\n"); } #[test] @@ -484,14 +663,10 @@ fn test_cp_strip_trailing_slashes() { let (at, mut ucmd) = at_and_ucmd!(); //using --strip-trailing-slashes option - let result = ucmd - .arg("--strip-trailing-slashes") + ucmd.arg("--strip-trailing-slashes") .arg(format!("{}/", TEST_HELLO_WORLD_SOURCE)) .arg(TEST_HELLO_WORLD_DEST) - .run(); - - // Check that the exit code represents a successful copy. - assert!(result.success); + .succeeds(); // Check the content of the destination file that was copied. assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); @@ -501,14 +676,11 @@ fn test_cp_strip_trailing_slashes() { fn test_cp_parents() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg("--parents") + ucmd.arg("--parents") .arg(TEST_COPY_FROM_FOLDER_FILE) .arg(TEST_COPY_TO_FOLDER) - .run(); + .succeeds(); - assert!(result.success); - // Check the content of the destination file that was copied. assert_eq!( at.read(&format!( "{}/{}", @@ -522,14 +694,12 @@ fn test_cp_parents() { fn test_cp_parents_multiple_files() { let (at, mut ucmd) = at_and_ucmd!(); - let result = ucmd - .arg("--parents") + ucmd.arg("--parents") .arg(TEST_COPY_FROM_FOLDER_FILE) .arg(TEST_HOW_ARE_YOU_SOURCE) .arg(TEST_COPY_TO_FOLDER) - .run(); + .succeeds(); - assert!(result.success); assert_eq!( at.read(&format!( "{}/{}", @@ -548,20 +718,21 @@ fn test_cp_parents_multiple_files() { #[test] fn test_cp_parents_dest_not_directory() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd + new_ucmd!() .arg("--parents") .arg(TEST_COPY_FROM_FOLDER_FILE) .arg(TEST_HELLO_WORLD_DEST) - .run(); - println!("{:?}", result); + .fails() + .stderr_contains("with --parents, the destination must be a directory"); +} - // Check that we did not succeed in copying. - assert!(!result.success); - assert!(result - .stderr - .contains("with --parents, the destination must be a directory")); +#[test] +fn test_cp_preserve_no_args() { + new_ucmd!() + .arg(TEST_COPY_FROM_FOLDER_FILE) + .arg(TEST_HELLO_WORLD_DEST) + .arg("--preserve") + .succeeds(); } #[test] @@ -588,29 +759,25 @@ fn test_cp_deref_folder_to_folder() { assert!(env::set_current_dir(&cwd).is_ok()); //using -P -R option - let result = scene + scene .ucmd() .arg("-L") .arg("-R") .arg("-v") .arg(TEST_COPY_FROM_FOLDER) .arg(TEST_COPY_TO_FOLDER_NEW) - .run(); - println!("cp output {}", result.stdout); - - // Check that the exit code represents a successful copy. - assert!(result.success); + .succeeds(); #[cfg(not(windows))] { let scene2 = TestScenario::new("ls"); let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); - println!("ls source {}", result.stdout); + println!("ls source {}", result.stdout_str()); let path_to_new_symlink = at.subdir.join(TEST_COPY_TO_FOLDER_NEW); let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); - println!("ls dest {}", result.stdout); + println!("ls dest {}", result.stdout_str()); } #[cfg(windows)] @@ -665,7 +832,7 @@ fn test_cp_deref_folder_to_folder() { // Check the content of the symlink let path_to_check = path_to_new_symlink.to_str().unwrap(); - assert_eq!(at.read(&path_to_check), "Hello, World!\n"); + assert_eq!(at.read(path_to_check), "Hello, World!\n"); } #[test] @@ -692,29 +859,25 @@ fn test_cp_no_deref_folder_to_folder() { assert!(env::set_current_dir(&cwd).is_ok()); //using -P -R option - let result = scene + scene .ucmd() .arg("-P") .arg("-R") .arg("-v") .arg(TEST_COPY_FROM_FOLDER) .arg(TEST_COPY_TO_FOLDER_NEW) - .run(); - println!("cp output {}", result.stdout); - - // Check that the exit code represents a successful copy. - assert!(result.success); + .succeeds(); #[cfg(not(windows))] { let scene2 = TestScenario::new("ls"); let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); - println!("ls source {}", result.stdout); + println!("ls source {}", result.stdout_str()); let path_to_new_symlink = at.subdir.join(TEST_COPY_TO_FOLDER_NEW); let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); - println!("ls dest {}", result.stdout); + println!("ls dest {}", result.stdout_str()); } #[cfg(windows)] @@ -769,7 +932,7 @@ fn test_cp_no_deref_folder_to_folder() { // Check the content of the symlink let path_to_check = path_to_new_symlink.to_str().unwrap(); - assert_eq!(at.read(&path_to_check), "Hello, World!\n"); + assert_eq!(at.read(path_to_check), "Hello, World!\n"); } #[test] @@ -778,20 +941,18 @@ fn test_cp_archive() { let (at, mut ucmd) = at_and_ucmd!(); let ts = time::now().to_timespec(); let previous = FileTime::from_unix_time(ts.sec as i64 - 3600, ts.nsec as u32); - // set the file creation/modif an hour ago + // set the file creation/modification an hour ago filetime::set_file_times( at.plus_as_string(TEST_HELLO_WORLD_SOURCE), previous, previous, ) .unwrap(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("--archive") .arg(TEST_HOW_ARE_YOU_SOURCE) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); let metadata = std_fs::metadata(at.subdir.join(TEST_HELLO_WORLD_SOURCE)).unwrap(); @@ -801,11 +962,10 @@ fn test_cp_archive() { let creation2 = metadata2.modified().unwrap(); let scene2 = TestScenario::new("ls"); - let result = scene2.cmd("ls").arg("-al").arg(at.subdir).run(); + let result = scene2.cmd("ls").arg("-al").arg(at.subdir).succeeds(); - println!("ls dest {}", result.stdout); + println!("ls dest {}", result.stdout_str()); assert_eq!(creation, creation2); - assert!(result.success); } #[test] @@ -844,11 +1004,10 @@ fn test_cp_archive_recursive() { // Back to the initial cwd (breaks the other tests) assert!(env::set_current_dir(&cwd).is_ok()); - let resultg = ucmd - .arg("--archive") + ucmd.arg("--archive") .arg(TEST_COPY_TO_FOLDER) .arg(TEST_COPY_TO_FOLDER_NEW) - .run(); + .fails(); // fails for now let scene2 = TestScenario::new("ls"); let result = scene2 @@ -857,16 +1016,15 @@ fn test_cp_archive_recursive() { .arg(&at.subdir.join(TEST_COPY_TO_FOLDER)) .run(); - println!("ls dest {}", result.stdout); + println!("ls dest {}", result.stdout_str()); - let scene2 = TestScenario::new("ls"); let result = scene2 .cmd("ls") .arg("-al") .arg(&at.subdir.join(TEST_COPY_TO_FOLDER_NEW)) .run(); - println!("ls dest {}", result.stdout); + println!("ls dest {}", result.stdout_str()); assert!(at.file_exists( &at.subdir .join(TEST_COPY_TO_FOLDER_NEW) @@ -904,9 +1062,6 @@ fn test_cp_archive_recursive() { .join("2.link") .to_string_lossy() )); - - // fails for now - assert!(resultg.success); } #[test] @@ -915,20 +1070,18 @@ fn test_cp_preserve_timestamps() { let (at, mut ucmd) = at_and_ucmd!(); let ts = time::now().to_timespec(); let previous = FileTime::from_unix_time(ts.sec as i64 - 3600, ts.nsec as u32); - // set the file creation/modif an hour ago + // set the file creation/modification an hour ago filetime::set_file_times( at.plus_as_string(TEST_HELLO_WORLD_SOURCE), previous, previous, ) .unwrap(); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("--preserve=timestamps") .arg(TEST_HOW_ARE_YOU_SOURCE) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); let metadata = std_fs::metadata(at.subdir.join(TEST_HELLO_WORLD_SOURCE)).unwrap(); @@ -940,18 +1093,17 @@ fn test_cp_preserve_timestamps() { let scene2 = TestScenario::new("ls"); let result = scene2.cmd("ls").arg("-al").arg(at.subdir).run(); - println!("ls dest {}", result.stdout); + println!("ls dest {}", result.stdout_str()); assert_eq!(creation, creation2); - assert!(result.success); } #[test] #[cfg(target_os = "linux")] -fn test_cp_dont_preserve_timestamps() { +fn test_cp_no_preserve_timestamps() { let (at, mut ucmd) = at_and_ucmd!(); let ts = time::now().to_timespec(); let previous = FileTime::from_unix_time(ts.sec as i64 - 3600, ts.nsec as u32); - // set the file creation/modif an hour ago + // set the file creation/modification an hour ago filetime::set_file_times( at.plus_as_string(TEST_HELLO_WORLD_SOURCE), previous, @@ -960,13 +1112,11 @@ fn test_cp_dont_preserve_timestamps() { .unwrap(); sleep(Duration::from_secs(3)); - let result = ucmd - .arg(TEST_HELLO_WORLD_SOURCE) + ucmd.arg(TEST_HELLO_WORLD_SOURCE) .arg("--no-preserve=timestamps") .arg(TEST_HOW_ARE_YOU_SOURCE) - .run(); + .succeeds(); - assert!(result.success); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); let metadata = std_fs::metadata(at.subdir.join(TEST_HELLO_WORLD_SOURCE)).unwrap(); @@ -978,7 +1128,7 @@ fn test_cp_dont_preserve_timestamps() { let scene2 = TestScenario::new("ls"); let result = scene2.cmd("ls").arg("-al").arg(at.subdir).run(); - println!("ls dest {}", result.stdout); + println!("ls dest {}", result.stdout_str()); println!("creation {:?} / {:?}", creation, creation2); assert_ne!(creation, creation2); @@ -986,7 +1136,6 @@ fn test_cp_dont_preserve_timestamps() { // Some margins with time check assert!(res.as_secs() > 3595); assert!(res.as_secs() < 3605); - assert!(result.success); } #[test] @@ -1001,3 +1150,191 @@ fn test_cp_target_file_dev_null() { assert!(at.file_exists(file2)); } + +#[test] +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +fn test_cp_one_file_system() { + use crate::common::util::AtPath; + use walkdir::WalkDir; + + let scene = TestScenario::new(util_name!()); + + // Test must be run as root (or with `sudo -E`) + if scene.cmd("whoami").run().stdout_str() != "root\n" { + return; + } + + let at = scene.fixtures.clone(); + let at_src = AtPath::new(&at.plus(TEST_MOUNT_COPY_FROM_FOLDER)); + let at_dst = AtPath::new(&at.plus(TEST_COPY_TO_FOLDER_NEW)); + + // Prepare the mount + at_src.mkdir(TEST_MOUNT_MOUNTPOINT); + let mountpoint_path = &at_src.plus_as_string(TEST_MOUNT_MOUNTPOINT); + + scene + .cmd("mount") + .arg("-t") + .arg("tmpfs") + .arg("-o") + .arg("size=640k") // ought to be enough + .arg("tmpfs") + .arg(mountpoint_path) + .succeeds(); + + at_src.touch(TEST_MOUNT_OTHER_FILESYSTEM_FILE); + + // Begin testing -x flag + scene + .ucmd() + .arg("-rx") + .arg(TEST_MOUNT_COPY_FROM_FOLDER) + .arg(TEST_COPY_TO_FOLDER_NEW) + .succeeds(); + + // Ditch the mount before the asserts + scene.cmd("umount").arg(mountpoint_path).succeeds(); + + assert!(!at_dst.file_exists(TEST_MOUNT_OTHER_FILESYSTEM_FILE)); + // Check if the other files were copied from the source folder hierarchy + for entry in WalkDir::new(at_src.as_string()) { + let entry = entry.unwrap(); + let relative_src = entry + .path() + .strip_prefix(at_src.as_string()) + .unwrap() + .to_str() + .unwrap(); + + let ft = entry.file_type(); + match (ft.is_dir(), ft.is_file(), ft.is_symlink()) { + (true, _, _) => assert!(at_dst.dir_exists(relative_src)), + (_, true, _) => assert!(at_dst.file_exists(relative_src)), + (_, _, _) => panic!(), + } + } +} + +#[test] +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn test_cp_reflink_always() { + let (at, mut ucmd) = at_and_ucmd!(); + let result = ucmd + .arg("--reflink=always") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_EXISTING_FILE) + .run(); + + if result.succeeded() { + // Check the content of the destination file + assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); + } else { + // Older Linux versions do not support cloning. + } +} + +#[test] +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn test_cp_reflink_auto() { + let (at, mut ucmd) = at_and_ucmd!(); + ucmd.arg("--reflink=auto") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_EXISTING_FILE) + .succeeds(); + + // Check the content of the destination file + assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); +} + +#[test] +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn test_cp_reflink_never() { + let (at, mut ucmd) = at_and_ucmd!(); + ucmd.arg("--reflink=never") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_EXISTING_FILE) + .succeeds(); + + // Check the content of the destination file + assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); +} + +#[test] +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn test_cp_reflink_bad() { + let (_, mut ucmd) = at_and_ucmd!(); + let _result = ucmd + .arg("--reflink=bad") + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_EXISTING_FILE) + .fails() + .stderr_contains("invalid argument"); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_cp_reflink_insufficient_permission() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.make_file("unreadable") + .set_permissions(PermissionsExt::from_mode(0o000)) + .unwrap(); + + ucmd.arg("-r") + .arg("--reflink=auto") + .arg("unreadable") + .arg(TEST_EXISTING_FILE) + .fails() + .stderr_only("cp: 'unreadable' -> 'existing_file.txt': Permission denied (os error 13)"); +} + +#[cfg(target_os = "linux")] +#[test] +fn test_closes_file_descriptors() { + new_ucmd!() + .arg("-r") + .arg("--reflink=auto") + .arg("dir_with_10_files/") + .arg("dir_with_10_files_new/") + .with_limit(Resource::NOFILE, 9, 9) + .succeeds(); +} + +#[test] +fn test_copy_dir_symlink() { + let (at, mut ucmd) = at_and_ucmd!(); + at.mkdir("dir"); + at.symlink_dir("dir", "dir-link"); + ucmd.args(&["-r", "dir-link", "copy"]).succeeds(); + assert_eq!(at.resolve_link("copy"), "dir"); +} + +#[test] +fn test_copy_dir_with_symlinks() { + let (at, mut ucmd) = at_and_ucmd!(); + at.mkdir("dir"); + at.make_file("dir/file"); + + TestScenario::new("ln") + .ucmd() + .arg("-sr") + .arg(at.subdir.join("dir/file")) + .arg(at.subdir.join("dir/file-link")) + .succeeds(); + + ucmd.args(&["-r", "dir", "copy"]).succeeds(); + assert_eq!(at.resolve_link("copy/file-link"), "file"); +} + +#[test] +#[cfg(not(windows))] +fn test_copy_symlink_force() { + let (at, mut ucmd) = at_and_ucmd!(); + at.touch("file"); + at.symlink_file("file", "file-link"); + at.touch("copy"); + + ucmd.args(&["file-link", "copy", "-f", "--no-dereference"]) + .succeeds(); + assert_eq!(at.resolve_link("copy"), "file"); +} diff --git a/tests/by-util/test_csplit.rs b/tests/by-util/test_csplit.rs index 51cab483c..7eeb584eb 100644 --- a/tests/by-util/test_csplit.rs +++ b/tests/by-util/test_csplit.rs @@ -208,7 +208,7 @@ fn test_up_to_match_repeat_over() { ucmd.args(&["numbers50.txt", "/9$/", "{50}"]) .fails() .stdout_is("16\n29\n30\n30\n30\n6\n") - .stderr_is("csplit: error: '/9$/': match not found on repetition 5"); + .stderr_is("csplit: '/9$/': match not found on repetition 5"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -219,7 +219,7 @@ fn test_up_to_match_repeat_over() { ucmd.args(&["numbers50.txt", "/9$/", "{50}", "-k"]) .fails() .stdout_is("16\n29\n30\n30\n30\n6\n") - .stderr_is("csplit: error: '/9$/': match not found on repetition 5"); + .stderr_is("csplit: '/9$/': match not found on repetition 5"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -365,7 +365,7 @@ fn test_option_keep() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["-k", "numbers50.txt", "/20/", "/nope/"]) .fails() - .stderr_is("csplit: error: '/nope/': match not found") + .stderr_is("csplit: '/nope/': match not found") .stdout_is("48\n93\n"); let count = glob(&at.plus_as_string("xx*")) @@ -541,7 +541,7 @@ fn test_up_to_match_context_overflow() { ucmd.args(&["numbers50.txt", "/45/+10"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/45/+10': line number out of range"); + .stderr_is("csplit: '/45/+10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -552,7 +552,7 @@ fn test_up_to_match_context_overflow() { ucmd.args(&["numbers50.txt", "/45/+10", "-k"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/45/+10': line number out of range"); + .stderr_is("csplit: '/45/+10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -567,7 +567,7 @@ fn test_skip_to_match_context_underflow() { ucmd.args(&["numbers50.txt", "%5%-10"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '%5%-10': line number out of range"); + .stderr_is("csplit: '%5%-10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -578,7 +578,7 @@ fn test_skip_to_match_context_underflow() { ucmd.args(&["numbers50.txt", "%5%-10", "-k"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '%5%-10': line number out of range"); + .stderr_is("csplit: '%5%-10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -592,7 +592,7 @@ fn test_skip_to_match_context_overflow() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%45%+10"]) .fails() - .stderr_only("csplit: error: '%45%+10': line number out of range"); + .stderr_only("csplit: '%45%+10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -602,7 +602,7 @@ fn test_skip_to_match_context_overflow() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%45%+10", "-k"]) .fails() - .stderr_only("csplit: error: '%45%+10': line number out of range"); + .stderr_only("csplit: '%45%+10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -616,7 +616,7 @@ fn test_up_to_no_match1() { ucmd.args(&["numbers50.txt", "/4/", "/nope/"]) .fails() .stdout_is("6\n135\n") - .stderr_is("csplit: error: '/nope/': match not found"); + .stderr_is("csplit: '/nope/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -627,7 +627,7 @@ fn test_up_to_no_match1() { ucmd.args(&["numbers50.txt", "/4/", "/nope/", "-k"]) .fails() .stdout_is("6\n135\n") - .stderr_is("csplit: error: '/nope/': match not found"); + .stderr_is("csplit: '/nope/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -643,7 +643,7 @@ fn test_up_to_no_match2() { ucmd.args(&["numbers50.txt", "/4/", "/nope/", "{50}"]) .fails() .stdout_is("6\n135\n") - .stderr_is("csplit: error: '/nope/': match not found"); + .stderr_is("csplit: '/nope/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -654,7 +654,7 @@ fn test_up_to_no_match2() { ucmd.args(&["numbers50.txt", "/4/", "/nope/", "{50}", "-k"]) .fails() .stdout_is("6\n135\n") - .stderr_is("csplit: error: '/nope/': match not found"); + .stderr_is("csplit: '/nope/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -670,7 +670,7 @@ fn test_up_to_no_match3() { ucmd.args(&["numbers50.txt", "/0$/", "{50}"]) .fails() .stdout_is("18\n30\n30\n30\n30\n3\n") - .stderr_is("csplit: error: '/0$/': match not found on repetition 5"); + .stderr_is("csplit: '/0$/': match not found on repetition 5"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -681,7 +681,7 @@ fn test_up_to_no_match3() { ucmd.args(&["numbers50.txt", "/0$/", "{50}", "-k"]) .fails() .stdout_is("18\n30\n30\n30\n30\n3\n") - .stderr_is("csplit: error: '/0$/': match not found on repetition 5"); + .stderr_is("csplit: '/0$/': match not found on repetition 5"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -701,7 +701,7 @@ fn test_up_to_no_match4() { ucmd.args(&["numbers50.txt", "/nope/", "/4/"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/nope/': match not found"); + .stderr_is("csplit: '/nope/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -712,7 +712,7 @@ fn test_up_to_no_match4() { ucmd.args(&["numbers50.txt", "/nope/", "/4/", "-k"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/nope/': match not found"); + .stderr_is("csplit: '/nope/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -741,7 +741,7 @@ fn test_up_to_no_match6() { ucmd.args(&["numbers50.txt", "/nope/-5"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/nope/-5': match not found"); + .stderr_is("csplit: '/nope/-5': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -752,7 +752,7 @@ fn test_up_to_no_match6() { ucmd.args(&["numbers50.txt", "/nope/-5", "-k"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/nope/-5': match not found"); + .stderr_is("csplit: '/nope/-5': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -767,7 +767,7 @@ fn test_up_to_no_match7() { ucmd.args(&["numbers50.txt", "/nope/+5"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/nope/+5': match not found"); + .stderr_is("csplit: '/nope/+5': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -778,7 +778,7 @@ fn test_up_to_no_match7() { ucmd.args(&["numbers50.txt", "/nope/+5", "-k"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/nope/+5': match not found"); + .stderr_is("csplit: '/nope/+5': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -792,7 +792,7 @@ fn test_skip_to_no_match1() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%nope%"]) .fails() - .stderr_only("csplit: error: '%nope%': match not found"); + .stderr_only("csplit: '%nope%': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -805,7 +805,7 @@ fn test_skip_to_no_match2() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%nope%", "{50}"]) .fails() - .stderr_only("csplit: error: '%nope%': match not found"); + .stderr_only("csplit: '%nope%': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -818,7 +818,7 @@ fn test_skip_to_no_match3() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%0$%", "{50}"]) .fails() - .stderr_only("csplit: error: '%0$%': match not found on repetition 5"); + .stderr_only("csplit: '%0$%': match not found on repetition 5"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -831,7 +831,7 @@ fn test_skip_to_no_match4() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%nope%", "/4/"]) .fails() - .stderr_only("csplit: error: '%nope%': match not found"); + .stderr_only("csplit: '%nope%': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -858,7 +858,7 @@ fn test_skip_to_no_match6() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%nope%-5"]) .fails() - .stderr_only("csplit: error: '%nope%-5': match not found"); + .stderr_only("csplit: '%nope%-5': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -871,7 +871,7 @@ fn test_skip_to_no_match7() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%nope%+5"]) .fails() - .stderr_only("csplit: error: '%nope%+5': match not found"); + .stderr_only("csplit: '%nope%+5': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -884,7 +884,7 @@ fn test_no_match() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "%nope%"]) .fails() - .stderr_only("csplit: error: '%nope%': match not found"); + .stderr_only("csplit: '%nope%': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -895,7 +895,7 @@ fn test_no_match() { ucmd.args(&["numbers50.txt", "/nope/"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '/nope/': match not found"); + .stderr_is("csplit: '/nope/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -904,7 +904,7 @@ fn test_no_match() { } #[test] -fn test_too_small_linenum() { +fn test_too_small_line_num() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/20/", "10", "/40/"]) .succeeds() @@ -921,7 +921,7 @@ fn test_too_small_linenum() { } #[test] -fn test_too_small_linenum_equal() { +fn test_too_small_line_num_equal() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/20/", "20"]) .succeeds() @@ -937,7 +937,7 @@ fn test_too_small_linenum_equal() { } #[test] -fn test_too_small_linenum_elided() { +fn test_too_small_line_num_elided() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "-z", "/20/", "10", "/40/"]) .succeeds() @@ -953,7 +953,7 @@ fn test_too_small_linenum_elided() { } #[test] -fn test_too_small_linenum_negative_offset() { +fn test_too_small_line_num_negative_offset() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/20/-5", "10", "/40/"]) .succeeds() @@ -970,7 +970,7 @@ fn test_too_small_linenum_negative_offset() { } #[test] -fn test_too_small_linenum_twice() { +fn test_too_small_line_num_twice() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/20/", "10", "15", "/40/"]) .succeeds() @@ -988,11 +988,11 @@ fn test_too_small_linenum_twice() { } #[test] -fn test_too_small_linenum_repeat() { +fn test_too_small_line_num_repeat() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/20/", "10", "{*}"]) .fails() - .stderr_is("csplit: error: '10': line number out of range on repetition 5") + .stderr_is("csplit: '10': line number out of range on repetition 5") .stdout_is("48\n0\n0\n30\n30\n30\n3\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1003,7 +1003,7 @@ fn test_too_small_linenum_repeat() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/20/", "10", "{*}", "-k"]) .fails() - .stderr_is("csplit: error: '10': line number out of range on repetition 5") + .stderr_is("csplit: '10': line number out of range on repetition 5") .stdout_is("48\n0\n0\n30\n30\n30\n3\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1020,12 +1020,12 @@ fn test_too_small_linenum_repeat() { } #[test] -fn test_linenum_out_of_range1() { +fn test_line_num_out_of_range1() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "100"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '100': line number out of range"); + .stderr_is("csplit: '100': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1036,7 +1036,7 @@ fn test_linenum_out_of_range1() { ucmd.args(&["numbers50.txt", "100", "-k"]) .fails() .stdout_is("141\n") - .stderr_is("csplit: error: '100': line number out of range"); + .stderr_is("csplit: '100': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1046,12 +1046,12 @@ fn test_linenum_out_of_range1() { } #[test] -fn test_linenum_out_of_range2() { +fn test_line_num_out_of_range2() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "10", "100"]) .fails() .stdout_is("18\n123\n") - .stderr_is("csplit: error: '100': line number out of range"); + .stderr_is("csplit: '100': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1062,7 +1062,7 @@ fn test_linenum_out_of_range2() { ucmd.args(&["numbers50.txt", "10", "100", "-k"]) .fails() .stdout_is("18\n123\n") - .stderr_is("csplit: error: '100': line number out of range"); + .stderr_is("csplit: '100': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1073,12 +1073,12 @@ fn test_linenum_out_of_range2() { } #[test] -fn test_linenum_out_of_range3() { +fn test_line_num_out_of_range3() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "40", "{2}"]) .fails() .stdout_is("108\n33\n") - .stderr_is("csplit: error: '40': line number out of range on repetition 1"); + .stderr_is("csplit: '40': line number out of range on repetition 1"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1089,7 +1089,7 @@ fn test_linenum_out_of_range3() { ucmd.args(&["numbers50.txt", "40", "{2}", "-k"]) .fails() .stdout_is("108\n33\n") - .stderr_is("csplit: error: '40': line number out of range on repetition 1"); + .stderr_is("csplit: '40': line number out of range on repetition 1"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1100,12 +1100,12 @@ fn test_linenum_out_of_range3() { } #[test] -fn test_linenum_out_of_range4() { +fn test_line_num_out_of_range4() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "40", "{*}"]) .fails() .stdout_is("108\n33\n") - .stderr_is("csplit: error: '40': line number out of range on repetition 1"); + .stderr_is("csplit: '40': line number out of range on repetition 1"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1116,7 +1116,7 @@ fn test_linenum_out_of_range4() { ucmd.args(&["numbers50.txt", "40", "{*}", "-k"]) .fails() .stdout_is("108\n33\n") - .stderr_is("csplit: error: '40': line number out of range on repetition 1"); + .stderr_is("csplit: '40': line number out of range on repetition 1"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1132,7 +1132,7 @@ fn test_skip_to_match_negative_offset_before_a_match() { ucmd.args(&["numbers50.txt", "/20/-10", "/15/"]) .fails() .stdout_is("18\n123\n") - .stderr_is("csplit: error: '/15/': match not found"); + .stderr_is("csplit: '/15/': match not found"); let count = glob(&at.plus_as_string("xx*")) .expect("there should be splits created") @@ -1141,7 +1141,7 @@ fn test_skip_to_match_negative_offset_before_a_match() { } #[test] -fn test_skip_to_match_negative_offset_before_a_linenum() { +fn test_skip_to_match_negative_offset_before_a_line_num() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/20/-10", "15"]) .succeeds() @@ -1177,7 +1177,7 @@ fn test_corner_case2() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/10/-5", "/10/"]) .fails() - .stderr_is("csplit: error: '/10/': match not found") + .stderr_is("csplit: '/10/': match not found") .stdout_is("8\n133\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1191,7 +1191,7 @@ fn test_corner_case3() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "/15/-3", "14", "/15/"]) .fails() - .stderr_is("csplit: error: '/15/': match not found") + .stderr_is("csplit: '/15/': match not found") .stdout_is("24\n6\n111\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1223,7 +1223,7 @@ fn test_up_to_match_context_underflow() { ucmd.args(&["numbers50.txt", "/5/-10"]) .fails() .stdout_is("0\n141\n") - .stderr_is("csplit: error: '/5/-10': line number out of range"); + .stderr_is("csplit: '/5/-10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -1234,7 +1234,7 @@ fn test_up_to_match_context_underflow() { ucmd.args(&["numbers50.txt", "/5/-10", "-k"]) .fails() .stdout_is("0\n141\n") - .stderr_is("csplit: error: '/5/-10': line number out of range"); + .stderr_is("csplit: '/5/-10': line number out of range"); let count = glob(&at.plus_as_string("xx*")) .expect("counting splits") @@ -1247,11 +1247,11 @@ fn test_up_to_match_context_underflow() { // the offset is out of range because of the first pattern // NOTE: output different than gnu's: the empty split is written but the rest of the input file is not #[test] -fn test_linenum_range_with_up_to_match1() { +fn test_line_num_range_with_up_to_match1() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "10", "/12/-5"]) .fails() - .stderr_is("csplit: error: '/12/-5': line number out of range") + .stderr_is("csplit: '/12/-5': line number out of range") .stdout_is("18\n0\n123\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1262,7 +1262,7 @@ fn test_linenum_range_with_up_to_match1() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "10", "/12/-5", "-k"]) .fails() - .stderr_is("csplit: error: '/12/-5': line number out of range") + .stderr_is("csplit: '/12/-5': line number out of range") .stdout_is("18\n0\n123\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1277,11 +1277,11 @@ fn test_linenum_range_with_up_to_match1() { // the offset is out of range because more lines are needed than physically available // NOTE: output different than gnu's: the empty split is not written but the rest of the input file is #[test] -fn test_linenum_range_with_up_to_match2() { +fn test_line_num_range_with_up_to_match2() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "10", "/12/-15"]) .fails() - .stderr_is("csplit: error: '/12/-15': line number out of range") + .stderr_is("csplit: '/12/-15': line number out of range") .stdout_is("18\n0\n123\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1292,7 +1292,7 @@ fn test_linenum_range_with_up_to_match2() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "10", "/12/-15", "-k"]) .fails() - .stderr_is("csplit: error: '/12/-15': line number out of range") + .stderr_is("csplit: '/12/-15': line number out of range") .stdout_is("18\n0\n123\n"); let count = glob(&at.plus_as_string("xx*")) @@ -1306,11 +1306,11 @@ fn test_linenum_range_with_up_to_match2() { // NOTE: output different than gnu's: the pattern /10/ is matched but should not #[test] -fn test_linenum_range_with_up_to_match3() { +fn test_line_num_range_with_up_to_match3() { let (at, mut ucmd) = at_and_ucmd!(); ucmd.args(&["numbers50.txt", "10", "/10/", "-k"]) .fails() - .stderr_is("csplit: error: '/10/': match not found") + .stderr_is("csplit: '/10/': match not found") .stdout_is("18\n123\n"); let count = glob(&at.plus_as_string("xx*")) diff --git a/tests/by-util/test_cut.rs b/tests/by-util/test_cut.rs index fc0f1b1f9..92bab4d75 100644 --- a/tests/by-util/test_cut.rs +++ b/tests/by-util/test_cut.rs @@ -1,13 +1,13 @@ use crate::common::util::*; -static INPUT: &'static str = "lists.txt"; +static INPUT: &str = "lists.txt"; struct TestedSequence<'b> { name: &'b str, sequence: &'b str, } -static EXAMPLE_SEQUENCES: &'static [TestedSequence<'static>] = &[ +static EXAMPLE_SEQUENCES: &[TestedSequence] = &[ TestedSequence { name: "singular", sequence: "2", @@ -34,14 +34,14 @@ static EXAMPLE_SEQUENCES: &'static [TestedSequence<'static>] = &[ }, ]; -static COMPLEX_SEQUENCE: &'static TestedSequence<'static> = &TestedSequence { +static COMPLEX_SEQUENCE: &TestedSequence = &TestedSequence { name: "", sequence: "9-,6-7,-2,4", }; #[test] fn test_byte_sequence() { - for param in vec!["-b", "--bytes"] { + for ¶m in &["-b", "--bytes"] { for example_seq in EXAMPLE_SEQUENCES { new_ucmd!() .args(&[param, example_seq.sequence, INPUT]) @@ -53,7 +53,7 @@ fn test_byte_sequence() { #[test] fn test_char_sequence() { - for param in vec!["-c", "--characters"] { + for ¶m in &["-c", "--characters"] { for example_seq in EXAMPLE_SEQUENCES { //as of coreutils 8.25 a char range is effectively the same as a byte range; there is no distinct treatment of utf8 chars. new_ucmd!() @@ -66,7 +66,7 @@ fn test_char_sequence() { #[test] fn test_field_sequence() { - for param in vec!["-f", "--fields"] { + for ¶m in &["-f", "--fields"] { for example_seq in EXAMPLE_SEQUENCES { new_ucmd!() .args(&[param, example_seq.sequence, INPUT]) @@ -78,7 +78,7 @@ fn test_field_sequence() { #[test] fn test_specify_delimiter() { - for param in vec!["-d", "--delimiter"] { + for ¶m in &["-d", "--delimiter"] { new_ucmd!() .args(&[param, ":", "-f", COMPLEX_SEQUENCE.sequence, INPUT]) .succeeds() @@ -122,7 +122,7 @@ fn test_zero_terminated() { #[test] fn test_only_delimited() { - for param in vec!["-s", "--only-delimited"] { + for param in &["-s", "--only-delimited"] { new_ucmd!() .args(&["-d_", param, "-f", "1"]) .pipe_in("91\n82\n7_3") @@ -139,3 +139,30 @@ fn test_zero_terminated_only_delimited() { .succeeds() .stdout_only("82\n7\0"); } + +#[test] +fn test_directory_and_no_such_file() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("some"); + + ucmd.arg("-b1") + .arg("some") + .run() + .stderr_is("cut: some: Is a directory\n"); + + new_ucmd!() + .arg("-b1") + .arg("some") + .run() + .stderr_is("cut: some: No such file or directory\n"); +} + +#[test] +fn test_equal_as_delimiter() { + new_ucmd!() + .args(&["-f", "2", "-d="]) + .pipe_in("--dir=./out/lib") + .succeeds() + .stdout_only("./out/lib\n"); +} diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index 0837878b2..a7a5fa583 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -2,132 +2,216 @@ extern crate regex; use self::regex::Regex; use crate::common::util::*; +#[cfg(all(unix, not(target_os = "macos")))] +use rust_users::*; #[test] fn test_date_email() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("--rfc-email").run(); - assert!(result.success); + new_ucmd!().arg("--rfc-email").succeeds(); } #[test] fn test_date_email2() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("-R").run(); - assert!(result.success); + new_ucmd!().arg("-R").succeeds(); } #[test] fn test_date_rfc_3339() { let scene = TestScenario::new(util_name!()); - let mut result = scene.ucmd().arg("--rfc-3339=ns").succeeds(); + let rfc_regexp = concat!( + r#"(\d+)-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\s([01]\d|2[0-3]):"#, + r#"([0-5]\d):([0-5]\d|60)(\.\d+)?(([Zz])|([\+|\-]([01]\d|2[0-3])))"# + ); + let re = Regex::new(rfc_regexp).unwrap(); // Check that the output matches the regexp - let rfc_regexp = r"(\d+)-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\s([01]\d|2[0-3]):([0-5]\d):([0-5]\d|60)(\.\d+)?(([Zz])|([\+|\-]([01]\d|2[0-3])))"; - let re = Regex::new(rfc_regexp).unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene + .ucmd() + .arg("--rfc-3339=ns") + .succeeds() + .stdout_matches(&re); - result = scene.ucmd().arg("--rfc-3339=seconds").succeeds(); - - // Check that the output matches the regexp - let re = Regex::new(rfc_regexp).unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene + .ucmd() + .arg("--rfc-3339=seconds") + .succeeds() + .stdout_matches(&re); } #[test] fn test_date_rfc_8601() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("--iso-8601=ns").run(); - assert!(result.success); + new_ucmd!().arg("--iso-8601=ns").succeeds(); } #[test] fn test_date_rfc_8601_second() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("--iso-8601=second").run(); - assert!(result.success); + new_ucmd!().arg("--iso-8601=second").succeeds(); } #[test] fn test_date_utc() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("--utc").run(); - assert!(result.success); + new_ucmd!().arg("--utc").succeeds(); } #[test] fn test_date_universal() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("--universal").run(); - assert!(result.success); + new_ucmd!().arg("--universal").succeeds(); } #[test] fn test_date_format_y() { let scene = TestScenario::new(util_name!()); - let mut result = scene.ucmd().arg("+%Y").succeeds(); - - assert!(result.success); let mut re = Regex::new(r"^\d{4}$").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene.ucmd().arg("+%Y").succeeds().stdout_matches(&re); - result = scene.ucmd().arg("+%y").succeeds(); - - assert!(result.success); re = Regex::new(r"^\d{2}$").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene.ucmd().arg("+%y").succeeds().stdout_matches(&re); } #[test] fn test_date_format_m() { let scene = TestScenario::new(util_name!()); - let mut result = scene.ucmd().arg("+%b").succeeds(); - - assert!(result.success); let mut re = Regex::new(r"\S+").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene.ucmd().arg("+%b").succeeds().stdout_matches(&re); - result = scene.ucmd().arg("+%m").succeeds(); - - assert!(result.success); re = Regex::new(r"^\d{2}$").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene.ucmd().arg("+%m").succeeds().stdout_matches(&re); } #[test] fn test_date_format_day() { let scene = TestScenario::new(util_name!()); - let mut result = scene.ucmd().arg("+%a").succeeds(); - - assert!(result.success); let mut re = Regex::new(r"\S+").unwrap(); - assert!(re.is_match(&result.stdout.trim())); - - result = scene.ucmd().arg("+%A").succeeds(); - - assert!(result.success); + scene.ucmd().arg("+%a").succeeds().stdout_matches(&re); re = Regex::new(r"\S+").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene.ucmd().arg("+%A").succeeds().stdout_matches(&re); - result = scene.ucmd().arg("+%u").succeeds(); - - assert!(result.success); re = Regex::new(r"^\d{1}$").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + scene.ucmd().arg("+%u").succeeds().stdout_matches(&re); } #[test] fn test_date_format_full_day() { - let scene = TestScenario::new(util_name!()); - - let result = scene.ucmd().arg("+'%a %Y-%m-%d'").succeeds(); - - assert!(result.success); let re = Regex::new(r"\S+ \d{4}-\d{2}-\d{2}").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + new_ucmd!() + .arg("+'%a %Y-%m-%d'") + .succeeds() + .stdout_matches(&re); +} + +#[test] +fn test_date_nano_seconds() { + // %N nanoseconds (000000000..999999999) + let re = Regex::new(r"^\d{1,9}$").unwrap(); + new_ucmd!().arg("+%N").succeeds().stdout_matches(&re); +} + +#[test] +fn test_date_format_without_plus() { + // [+FORMAT] + new_ucmd!() + .arg("%s") + .fails() + .stderr_contains("date: invalid date '%s'") + .code_is(1); +} + +#[test] +fn test_date_format_literal() { + new_ucmd!().arg("+%%s").succeeds().stdout_is("%s\n"); + new_ucmd!().arg("+%%N").succeeds().stdout_is("%N\n"); +} + +#[test] +#[cfg(all(unix, not(target_os = "macos")))] +fn test_date_set_valid() { + if get_effective_uid() == 0 { + new_ucmd!() + .arg("--set") + .arg("2020-03-12 13:30:00+08:00") + .succeeds() + .no_stdout() + .no_stderr(); + } +} + +#[test] +#[cfg(any(windows, all(unix, not(target_os = "macos"))))] +fn test_date_set_invalid() { + let result = new_ucmd!().arg("--set").arg("123abcd").fails(); + result.no_stdout(); + assert!(result.stderr_str().starts_with("date: invalid date ")); +} + +#[test] +#[cfg(all(unix, not(target_os = "macos")))] +fn test_date_set_permissions_error() { + if !(get_effective_uid() == 0 || uucore::os::is_wsl_1()) { + let result = new_ucmd!() + .arg("--set") + .arg("2020-03-11 21:45:00+08:00") + .fails(); + result.no_stdout(); + assert!(result.stderr_str().starts_with("date: cannot set date: ")); + } +} + +#[test] +#[cfg(target_os = "macos")] +fn test_date_set_mac_unavailable() { + let result = new_ucmd!() + .arg("--set") + .arg("2020-03-11 21:45:00+08:00") + .fails(); + result.no_stdout(); + assert!(result + .stderr_str() + .starts_with("date: setting the date is not supported by macOS")); +} + +#[test] +#[cfg(all(unix, not(target_os = "macos")))] +/// TODO: expected to fail currently; change to succeeds() when required. +fn test_date_set_valid_2() { + if get_effective_uid() == 0 { + let result = new_ucmd!() + .arg("--set") + .arg("Sat 20 Mar 2021 14:53:01 AWST") // spell-checker:disable-line + .fails(); + result.no_stdout(); + assert!(result.stderr_str().starts_with("date: invalid date ")); + } +} + +#[test] +#[cfg(all(unix, not(target_os = "macos")))] +/// TODO: expected to fail currently; change to succeeds() when required. +fn test_date_set_valid_3() { + if get_effective_uid() == 0 { + let result = new_ucmd!() + .arg("--set") + .arg("Sat 20 Mar 2021 14:53:01") // Local timezone + .fails(); + result.no_stdout(); + assert!(result.stderr_str().starts_with("date: invalid date ")); + } +} + +#[test] +#[cfg(all(unix, not(target_os = "macos")))] +/// TODO: expected to fail currently; change to succeeds() when required. +fn test_date_set_valid_4() { + if get_effective_uid() == 0 { + let result = new_ucmd!() + .arg("--set") + .arg("2020-03-11 21:45:00") // Local timezone + .fails(); + result.no_stdout(); + assert!(result.stderr_str().starts_with("date: invalid date ")); + } } diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index cf8be384c..84d299aa1 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -1,9 +1,8 @@ use crate::common::util::*; use std::io::{BufReader, Read, Write}; -use std::path::{Path, PathBuf}; -use std::fs::{self, OpenOptions, File}; -use std::time::{Duration, SystemTime}; +use std::path::{PathBuf}; +use std::fs::{OpenOptions, File}; use tempfile::tempfile; macro_rules! inf @@ -475,18 +474,18 @@ fn test_fullblock() { let tname = "fullblock-from-urand"; let tmp_fn = format!("TESTFILE-{}.tmp", &tname); - let stats = vec![ + let exp_stats = vec![ "1+0 records in\n", "1+0 records out\n", "134217728 bytes (134 MB, 128 MiB) copied,", ]; - let stats = stats.into_iter() - .fold(String::new(), | mut acc, s | { - acc.push_str(s); - acc - }); + let exp_stats = exp_stats.into_iter() + .fold(Vec::new(), | mut acc, s | { + acc.extend(s.bytes()); + acc + }); - let res = new_ucmd!() + let ucmd = new_ucmd!() .args(&[ "if=/dev/urandom", of!(&tmp_fn), @@ -499,12 +498,11 @@ fn test_fullblock() // a reasonable value for testing most systems. "count=1", "iflag=fullblock", - ]) - .run(); + ]).run(); + ucmd.success(); - res.success(); - let res_stats = &res.stderr[..stats.len()]; - assert_eq!(&stats, res_stats); + let run_stats = &ucmd.stderr()[..exp_stats.len()]; + assert_eq!(exp_stats, run_stats); } // Fileio diff --git a/tests/by-util/test_df.rs b/tests/by-util/test_df.rs index f79d1beb5..ac3776b96 100644 --- a/tests/by-util/test_df.rs +++ b/tests/by-util/test_df.rs @@ -2,30 +2,34 @@ use crate::common::util::*; #[test] fn test_df_compatible_no_size_arg() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("-a").run(); - assert!(result.success); + new_ucmd!().arg("-a").succeeds(); } #[test] fn test_df_compatible() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("-ah").run(); - assert!(result.success); + new_ucmd!().arg("-ah").succeeds(); } #[test] fn test_df_compatible_type() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("-aT").run(); - assert!(result.success); + new_ucmd!().arg("-aT").succeeds(); } #[test] fn test_df_compatible_si() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("-aH").run(); - assert!(result.success); + new_ucmd!().arg("-aH").succeeds(); +} + +#[test] +fn test_df_output() { + if cfg!(target_os = "macos") { + new_ucmd!().arg("-H").arg("-total").succeeds(). + stdout_only("Filesystem Size Used Available Capacity Use% Mounted on \n"); + } else { + new_ucmd!().arg("-H").arg("-total").succeeds().stdout_only( + "Filesystem Size Used Available Use% Mounted on \n", + ); + } } // ToDO: more tests... diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index f28074075..fac4161f3 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -30,14 +30,14 @@ fn test_shell_syntax() { } #[test] -fn test_strutils() { +fn test_str_utils() { let s = " asd#zcv #hk\t\n "; assert_eq!("asd#zcv", s.purify()); let s = "con256asd"; - assert!(s.fnmatch("*[2][3-6][5-9]?sd")); + assert!(s.fnmatch("*[2][3-6][5-9]?sd")); // spell-checker:disable-line - let s = "zxc \t\nqwe jlk hjl"; + let s = "zxc \t\nqwe jlk hjl"; // spell-checker:disable-line let (k, v) = s.split_two(); assert_eq!("zxc", k); assert_eq!("qwe jlk hjl", v); diff --git a/tests/by-util/test_dirname.rs b/tests/by-util/test_dirname.rs index bcb4378d6..026ac22bb 100644 --- a/tests/by-util/test_dirname.rs +++ b/tests/by-util/test_dirname.rs @@ -16,6 +16,21 @@ fn test_path_without_trailing_slashes() { .stdout_is("/root/alpha/beta/gamma/delta/epsilon\n"); } +#[test] +fn test_path_without_trailing_slashes_and_zero() { + new_ucmd!() + .arg("-z") + .arg("/root/alpha/beta/gamma/delta/epsilon/omega") + .succeeds() + .stdout_is("/root/alpha/beta/gamma/delta/epsilon\u{0}"); + + new_ucmd!() + .arg("--zero") + .arg("/root/alpha/beta/gamma/delta/epsilon/omega") + .succeeds() + .stdout_is("/root/alpha/beta/gamma/delta/epsilon\u{0}"); +} + #[test] fn test_root() { new_ucmd!().arg("/").run().stdout_is("/\n"); diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index c9704a658..67036be44 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1,54 +1,79 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +// spell-checker:ignore (paths) sublink subwords + use crate::common::util::*; const SUB_DIR: &str = "subdir/deeper"; +const SUB_DEEPER_DIR: &str = "subdir/deeper/deeper_dir"; const SUB_DIR_LINKS: &str = "subdir/links"; +const SUB_DIR_LINKS_DEEPER_SYM_DIR: &str = "subdir/links/deeper_dir"; const SUB_FILE: &str = "subdir/links/subwords.txt"; const SUB_LINK: &str = "subdir/links/sublink.txt"; #[test] fn test_du_basics() { - let (_at, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - assert!(result.success); - assert_eq!(result.stderr, ""); + new_ucmd!().succeeds().no_stderr(); } -#[cfg(target_os = "macos")] -fn _du_basics(s: String) { +#[cfg(target_vendor = "apple")] +fn _du_basics(s: &str) { let answer = "32\t./subdir 8\t./subdir/deeper 24\t./subdir/links -40\t./ +40\t. "; assert_eq!(s, answer); } -#[cfg(not(target_os = "macos"))] -fn _du_basics(s: String) { +#[cfg(not(target_vendor = "apple"))] +fn _du_basics(s: &str) { let answer = "28\t./subdir 8\t./subdir/deeper 16\t./subdir/links -36\t./ +36\t. "; assert_eq!(s, answer); } #[test] fn test_du_basics_subdir() { - let (_at, mut ucmd) = at_and_ucmd!(); + let scene = TestScenario::new(util_name!()); - let result = ucmd.arg(SUB_DIR).run(); - assert!(result.success); - assert_eq!(result.stderr, ""); - _du_basics_subdir(result.stdout); + let result = scene.ucmd().arg(SUB_DIR).succeeds(); + + #[cfg(target_os = "linux")] + { + let result_reference = scene.cmd("du").arg(SUB_DIR).run(); + if result_reference.succeeded() { + assert_eq!(result.stdout_str(), result_reference.stdout_str()); + return; + } + } + _du_basics_subdir(result.stdout_str()); } -#[cfg(target_os = "macos")] -fn _du_basics_subdir(s: String) { - assert_eq!(s, "4\tsubdir/deeper\n"); +#[cfg(target_vendor = "apple")] +fn _du_basics_subdir(s: &str) { + assert_eq!(s, "4\tsubdir/deeper/deeper_dir\n8\tsubdir/deeper\n"); } -#[cfg(not(target_os = "macos"))] -fn _du_basics_subdir(s: String) { +#[cfg(target_os = "windows")] +fn _du_basics_subdir(s: &str) { + assert_eq!(s, "0\tsubdir/deeper\\deeper_dir\n0\tsubdir/deeper\n"); +} +#[cfg(target_os = "freebsd")] +fn _du_basics_subdir(s: &str) { + assert_eq!(s, "8\tsubdir/deeper/deeper_dir\n16\tsubdir/deeper\n"); +} +#[cfg(all( + not(target_vendor = "apple"), + not(target_os = "windows"), + not(target_os = "freebsd") +))] +fn _du_basics_subdir(s: &str) { // MS-WSL linux has altered expected output - if !is_wsl() { + if !uucore::os::is_wsl_1() { assert_eq!(s, "8\tsubdir/deeper\n"); } else { assert_eq!(s, "0\tsubdir/deeper\n"); @@ -56,39 +81,74 @@ fn _du_basics_subdir(s: String) { } #[test] -fn test_du_basics_bad_name() { - let (_at, mut ucmd) = at_and_ucmd!(); +fn test_du_invalid_size() { + let args = &["block-size", "threshold"]; + for s in args { + new_ucmd!() + .arg(format!("--{}=1fb4t", s)) + .arg("/tmp") + .fails() + .code_is(1) + .stderr_only(format!("du: invalid --{} argument '1fb4t'", s)); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .arg(format!("--{}=1Y", s)) + .arg("/tmp") + .fails() + .code_is(1) + .stderr_only(format!("du: --{} argument '1Y' too large", s)); + } +} - let result = ucmd.arg("bad_name").run(); - assert_eq!(result.stdout, ""); - assert_eq!( - result.stderr, - "du: error: bad_name: No such file or directory\n" - ); +#[test] +fn test_du_basics_bad_name() { + new_ucmd!() + .arg("bad_name") + .succeeds() // TODO: replace with ".fails()" once `du` is fixed + .stderr_only("du: bad_name: No such file or directory\n"); } #[test] fn test_du_soft_link() { - let ts = TestScenario::new("du"); + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; - let link = ts.cmd("ln").arg("-s").arg(SUB_FILE).arg(SUB_LINK).run(); - assert!(link.success); + at.symlink_file(SUB_FILE, SUB_LINK); - let result = ts.ucmd().arg(SUB_DIR_LINKS).run(); - assert!(result.success); - assert_eq!(result.stderr, ""); - _du_soft_link(result.stdout); + let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds(); + + #[cfg(target_os = "linux")] + { + let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).run(); + if result_reference.succeeded() { + assert_eq!(result.stdout_str(), result_reference.stdout_str()); + return; + } + } + _du_soft_link(result.stdout_str()); } -#[cfg(target_os = "macos")] -fn _du_soft_link(s: String) { +#[cfg(target_vendor = "apple")] +fn _du_soft_link(s: &str) { // 'macos' host variants may have `du` output variation for soft links assert!((s == "12\tsubdir/links\n") || (s == "16\tsubdir/links\n")); } -#[cfg(not(target_os = "macos"))] -fn _du_soft_link(s: String) { +#[cfg(target_os = "windows")] +fn _du_soft_link(s: &str) { + assert_eq!(s, "8\tsubdir/links\n"); +} +#[cfg(target_os = "freebsd")] +fn _du_soft_link(s: &str) { + assert_eq!(s, "16\tsubdir/links\n"); +} +#[cfg(all( + not(target_vendor = "apple"), + not(target_os = "windows"), + not(target_os = "freebsd") +))] +fn _du_soft_link(s: &str) { // MS-WSL linux has altered expected output - if !is_wsl() { + if !uucore::os::is_wsl_1() { assert_eq!(s, "16\tsubdir/links\n"); } else { assert_eq!(s, "8\tsubdir/links\n"); @@ -97,26 +157,45 @@ fn _du_soft_link(s: String) { #[test] fn test_du_hard_link() { - let ts = TestScenario::new("du"); + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; - let link = ts.cmd("ln").arg(SUB_FILE).arg(SUB_LINK).run(); - assert!(link.success); + at.hard_link(SUB_FILE, SUB_LINK); - let result = ts.ucmd().arg(SUB_DIR_LINKS).run(); - assert!(result.success); - assert_eq!(result.stderr, ""); + let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds(); + + #[cfg(target_os = "linux")] + { + let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).run(); + if result_reference.succeeded() { + assert_eq!(result.stdout_str(), result_reference.stdout_str()); + return; + } + } // We do not double count hard links as the inodes are identical - _du_hard_link(result.stdout); + _du_hard_link(result.stdout_str()); } -#[cfg(target_os = "macos")] -fn _du_hard_link(s: String) { +#[cfg(target_vendor = "apple")] +fn _du_hard_link(s: &str) { assert_eq!(s, "12\tsubdir/links\n") } -#[cfg(not(target_os = "macos"))] -fn _du_hard_link(s: String) { +#[cfg(target_os = "windows")] +fn _du_hard_link(s: &str) { + assert_eq!(s, "8\tsubdir/links\n") +} +#[cfg(target_os = "freebsd")] +fn _du_hard_link(s: &str) { + assert_eq!(s, "16\tsubdir/links\n") +} +#[cfg(all( + not(target_vendor = "apple"), + not(target_os = "windows"), + not(target_os = "freebsd") +))] +fn _du_hard_link(s: &str) { // MS-WSL linux has altered expected output - if !is_wsl() { + if !uucore::os::is_wsl_1() { assert_eq!(s, "16\tsubdir/links\n"); } else { assert_eq!(s, "8\tsubdir/links\n"); @@ -125,24 +204,218 @@ fn _du_hard_link(s: String) { #[test] fn test_du_d_flag() { - let ts = TestScenario::new("du"); + let scene = TestScenario::new(util_name!()); - let result = ts.ucmd().arg("-d").arg("1").run(); - assert!(result.success); - assert_eq!(result.stderr, ""); - _du_d_flag(result.stdout); + let result = scene.ucmd().arg("-d1").succeeds(); + + #[cfg(target_os = "linux")] + { + let result_reference = scene.cmd("du").arg("-d1").run(); + if result_reference.succeeded() { + assert_eq!(result.stdout_str(), result_reference.stdout_str()); + return; + } + } + _du_d_flag(result.stdout_str()); } -#[cfg(target_os = "macos")] -fn _du_d_flag(s: String) { - assert_eq!(s, "16\t./subdir\n20\t./\n"); +#[cfg(target_vendor = "apple")] +fn _du_d_flag(s: &str) { + assert_eq!(s, "20\t./subdir\n24\t.\n"); } -#[cfg(not(target_os = "macos"))] -fn _du_d_flag(s: String) { +#[cfg(target_os = "windows")] +fn _du_d_flag(s: &str) { + assert_eq!(s, "8\t.\\subdir\n8\t.\n"); +} +#[cfg(target_os = "freebsd")] +fn _du_d_flag(s: &str) { + assert_eq!(s, "36\t./subdir\n44\t.\n"); +} +#[cfg(all( + not(target_vendor = "apple"), + not(target_os = "windows"), + not(target_os = "freebsd") +))] +fn _du_d_flag(s: &str) { // MS-WSL linux has altered expected output - if !is_wsl() { - assert_eq!(s, "28\t./subdir\n36\t./\n"); + if !uucore::os::is_wsl_1() { + assert_eq!(s, "28\t./subdir\n36\t.\n"); } else { - assert_eq!(s, "8\t./subdir\n8\t./\n"); + assert_eq!(s, "8\t./subdir\n8\t.\n"); } } + +#[test] +fn test_du_dereference() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.symlink_dir(SUB_DEEPER_DIR, SUB_DIR_LINKS_DEEPER_SYM_DIR); + + let result = scene.ucmd().arg("-L").arg(SUB_DIR_LINKS).succeeds(); + + #[cfg(target_os = "linux")] + { + let result_reference = scene.cmd("du").arg("-L").arg(SUB_DIR_LINKS).run(); + if result_reference.succeeded() { + assert_eq!(result.stdout_str(), result_reference.stdout_str()); + return; + } + } + + _du_dereference(result.stdout_str()); +} + +#[cfg(target_vendor = "apple")] +fn _du_dereference(s: &str) { + assert_eq!(s, "4\tsubdir/links/deeper_dir\n16\tsubdir/links\n"); +} +#[cfg(target_os = "windows")] +fn _du_dereference(s: &str) { + assert_eq!(s, "0\tsubdir/links\\deeper_dir\n8\tsubdir/links\n"); +} +#[cfg(target_os = "freebsd")] +fn _du_dereference(s: &str) { + assert_eq!(s, "8\tsubdir/links/deeper_dir\n24\tsubdir/links\n"); +} +#[cfg(all( + not(target_vendor = "apple"), + not(target_os = "windows"), + not(target_os = "freebsd") +))] +fn _du_dereference(s: &str) { + // MS-WSL linux has altered expected output + if !uucore::os::is_wsl_1() { + assert_eq!(s, "8\tsubdir/links/deeper_dir\n24\tsubdir/links\n"); + } else { + assert_eq!(s, "0\tsubdir/links/deeper_dir\n8\tsubdir/links\n"); + } +} + +#[test] +fn test_du_h_flag_empty_file() { + new_ucmd!() + .arg("-h") + .arg("empty.txt") + .succeeds() + .stdout_only("0\tempty.txt\n"); +} + +#[cfg(feature = "touch")] +#[test] +fn test_du_time() { + let scene = TestScenario::new(util_name!()); + + scene + .ccmd("touch") + .arg("-a") + .arg("-t") + .arg("201505150000") + .arg("date_test") + .succeeds(); + + scene + .ccmd("touch") + .arg("-m") + .arg("-t") + .arg("201606160000") + .arg("date_test") + .succeeds(); + + let result = scene.ucmd().arg("--time").arg("date_test").succeeds(); + result.stdout_only("0\t2016-06-16 00:00\tdate_test\n"); + + let result = scene.ucmd().arg("--time=atime").arg("date_test").succeeds(); + result.stdout_only("0\t2015-05-15 00:00\tdate_test\n"); + + let result = scene.ucmd().arg("--time=ctime").arg("date_test").succeeds(); + result.stdout_only("0\t2016-06-16 00:00\tdate_test\n"); + + #[cfg(not(target_env = "musl"))] + { + use regex::Regex; + + let re_birth = + Regex::new(r"0\t[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}\tdate_test").unwrap(); + let result = scene.ucmd().arg("--time=birth").arg("date_test").succeeds(); + result.stdout_matches(&re_birth); + } +} + +#[cfg(not(target_os = "windows"))] +#[cfg(feature = "chmod")] +#[test] +fn test_du_no_permission() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir_all(SUB_DIR_LINKS); + + scene.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds(); + + let result = scene.ucmd().arg(SUB_DIR_LINKS).run(); // TODO: replace with ".fails()" once `du` is fixed + result.stderr_contains( + "du: cannot read directory 'subdir/links': Permission denied (os error 13)", + ); + + #[cfg(target_os = "linux")] + { + let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).fails(); + if result_reference + .stderr_str() + .contains("du: cannot read directory 'subdir/links': Permission denied") + { + assert_eq!(result.stdout_str(), result_reference.stdout_str()); + return; + } + } + + _du_no_permission(result.stdout_str()); +} + +#[cfg(target_vendor = "apple")] +fn _du_no_permission(s: &str) { + assert_eq!(s, "0\tsubdir/links\n"); +} +#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))] +fn _du_no_permission(s: &str) { + assert_eq!(s, "4\tsubdir/links\n"); +} + +#[test] +fn test_du_one_file_system() { + let scene = TestScenario::new(util_name!()); + + let result = scene.ucmd().arg("-x").arg(SUB_DIR).succeeds(); + + #[cfg(target_os = "linux")] + { + let result_reference = scene.cmd("du").arg("-x").arg(SUB_DIR).run(); + if result_reference.succeeded() { + assert_eq!(result.stdout_str(), result_reference.stdout_str()); + return; + } + } + _du_basics_subdir(result.stdout_str()); +} + +#[test] +fn test_du_threshold() { + let scene = TestScenario::new(util_name!()); + + let threshold = if cfg!(windows) { "7K" } else { "10K" }; + + scene + .ucmd() + .arg(format!("--threshold={}", threshold)) + .succeeds() + .stdout_contains("links") + .stdout_does_not_contain("deeper_dir"); + + scene + .ucmd() + .arg(format!("--threshold=-{}", threshold)) + .succeeds() + .stdout_does_not_contain("links") + .stdout_contains("deeper_dir"); +} diff --git a/tests/by-util/test_echo.rs b/tests/by-util/test_echo.rs index 2d073d60b..09ed9658f 100644 --- a/tests/by-util/test_echo.rs +++ b/tests/by-util/test_echo.rs @@ -1,23 +1,20 @@ +// spell-checker:ignore (words) araba merci + use crate::common::util::*; #[test] fn test_default() { - //CmdResult.stdout_only(...) trims trailing newlines - assert_eq!("hi\n", new_ucmd!().arg("hi").succeeds().no_stderr().stdout); + new_ucmd!().arg("hi").succeeds().stdout_only("hi\n"); } #[test] fn test_no_trailing_newline() { - //CmdResult.stdout_only(...) trims trailing newlines - assert_eq!( - "hi", - new_ucmd!() - .arg("-n") - .arg("hi") - .succeeds() - .no_stderr() - .stdout - ); + new_ucmd!() + .arg("-n") + .arg("hi") + .succeeds() + .no_stderr() + .stdout_only("hi"); } #[test] @@ -173,3 +170,57 @@ fn test_disable_escapes() { .succeeds() .stdout_only(format!("{}\n", input_str)); } + +#[test] +fn test_hyphen_value() { + new_ucmd!().arg("-abc").succeeds().stdout_is("-abc\n"); +} + +#[test] +fn test_multiple_hyphen_values() { + new_ucmd!() + .args(&["-abc", "-def", "-edf"]) + .succeeds() + .stdout_is("-abc -def -edf\n"); +} + +#[test] +fn test_hyphen_values_inside_string() { + new_ucmd!() + .arg("'\"\n'CXXFLAGS=-g -O2'\n\"'") // spell-checker:disable-line + .succeeds() + .stdout_contains("CXXFLAGS"); // spell-checker:disable-line +} + +#[test] +fn test_hyphen_values_at_start() { + new_ucmd!() + .arg("-E") + .arg("-test") + .arg("araba") + .arg("-merci") + .run() + .success() + .stdout_does_not_contain("-E") + .stdout_is("-test araba -merci\n"); +} + +#[test] +fn test_hyphen_values_between() { + new_ucmd!() + .arg("test") + .arg("-E") + .arg("araba") + .run() + .success() + .stdout_is("test -E araba\n"); + + new_ucmd!() + .arg("dumdum ") + .arg("dum dum dum") + .arg("-e") + .arg("dum") + .run() + .success() + .stdout_is("dumdum dum dum dum -e dum\n"); +} diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index 2ffb2bc48..1d76c433d 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (words) bamf chdir + #[cfg(not(windows))] use std::fs; @@ -8,45 +10,36 @@ use tempfile::tempdir; #[test] fn test_env_help() { - assert!(new_ucmd!() + new_ucmd!() .arg("--help") .succeeds() .no_stderr() - .stdout - .contains("OPTIONS:")); + .stdout_contains("OPTIONS:"); } #[test] fn test_env_version() { - assert!(new_ucmd!() + new_ucmd!() .arg("--version") .succeeds() .no_stderr() - .stdout - .contains(util_name!())); + .stdout_contains(util_name!()); } #[test] fn test_echo() { - // assert!(new_ucmd!().arg("printf").arg("FOO-bar").succeeds().no_stderr().stdout.contains("FOO-bar")); - let mut cmd = new_ucmd!(); - cmd.arg("echo").arg("FOO-bar"); - println!("cmd={:?}", cmd); + let result = new_ucmd!().arg("echo").arg("FOO-bar").succeeds(); - let result = cmd.run(); - println!("success={:?}", result.success); - println!("stdout={:?}", result.stdout); - println!("stderr={:?}", result.stderr); - assert!(result.success); - - let out = result.stdout.trim_end(); - - assert_eq!(out, "FOO-bar"); + assert_eq!(result.stdout_str().trim(), "FOO-bar"); } #[test] fn test_file_option() { - let out = new_ucmd!().arg("-f").arg("vars.conf.txt").run().stdout; + let out = new_ucmd!() + .arg("-f") + .arg("vars.conf.txt") + .run() + .stdout_move_str(); assert_eq!( out.lines() @@ -63,7 +56,7 @@ fn test_combined_file_set() { .arg("vars.conf.txt") .arg("FOO=bar.alt") .run() - .stdout; + .stdout_move_str(); assert_eq!(out.lines().filter(|&line| line == "FOO=bar.alt").count(), 1); } @@ -76,8 +69,8 @@ fn test_combined_file_set_unset() { .arg("-f") .arg("vars.conf.txt") .arg("FOO=bar.alt") - .run() - .stdout; + .succeeds() + .stdout_move_str(); assert_eq!( out.lines() @@ -89,17 +82,18 @@ fn test_combined_file_set_unset() { #[test] fn test_single_name_value_pair() { - let out = new_ucmd!().arg("FOO=bar").run().stdout; + let out = new_ucmd!().arg("FOO=bar").run(); - assert!(out.lines().any(|line| line == "FOO=bar")); + assert!(out.stdout_str().lines().any(|line| line == "FOO=bar")); } #[test] fn test_multiple_name_value_pairs() { - let out = new_ucmd!().arg("FOO=bar").arg("ABC=xyz").run().stdout; + let out = new_ucmd!().arg("FOO=bar").arg("ABC=xyz").run(); assert_eq!( - out.lines() + out.stdout_str() + .lines() .filter(|&line| line == "FOO=bar" || line == "ABC=xyz") .count(), 2 @@ -110,13 +104,8 @@ fn test_multiple_name_value_pairs() { fn test_ignore_environment() { let scene = TestScenario::new(util_name!()); - let out = scene.ucmd().arg("-i").run().stdout; - - assert_eq!(out, ""); - - let out = scene.ucmd().arg("-").run().stdout; - - assert_eq!(out, ""); + scene.ucmd().arg("-i").run().no_stdout(); + scene.ucmd().arg("-").run().no_stdout(); } #[test] @@ -126,12 +115,12 @@ fn test_null_delimiter() { .arg("--null") .arg("FOO=bar") .arg("ABC=xyz") - .run() - .stdout; + .succeeds() + .stdout_move_str(); let mut vars: Vec<_> = out.split('\0').collect(); assert_eq!(vars.len(), 3); - vars.sort(); + vars.sort_unstable(); assert_eq!(vars[0], ""); assert_eq!(vars[1], "ABC=xyz"); assert_eq!(vars[2], "FOO=bar"); @@ -145,16 +134,19 @@ fn test_unset_variable() { .ucmd_keepenv() .arg("-u") .arg("HOME") - .run() - .stdout; + .succeeds() + .stdout_move_str(); - assert_eq!(out.lines().any(|line| line.starts_with("HOME=")), false); + assert!(!out.lines().any(|line| line.starts_with("HOME="))); } #[test] fn test_fail_null_with_program() { - let out = new_ucmd!().arg("--null").arg("cd").fails().stderr; - assert!(out.contains("cannot specify --null (-0) with command")); + new_ucmd!() + .arg("--null") + .arg("cd") + .fails() + .stderr_contains("cannot specify --null (-0) with command"); } #[cfg(not(windows))] @@ -173,8 +165,8 @@ fn test_change_directory() { .arg("--chdir") .arg(&temporary_path) .arg(pwd) - .run() - .stdout; + .succeeds() + .stdout_move_str(); assert_eq!(out.trim(), temporary_path.as_os_str()) } @@ -193,20 +185,19 @@ fn test_change_directory() { .ucmd() .arg("--chdir") .arg(&temporary_path) - .run() - .stdout; - assert_eq!( - out.lines() - .any(|line| line.ends_with(temporary_path.file_name().unwrap().to_str().unwrap())), - false - ); + .succeeds() + .stdout_move_str(); + + assert!(!out + .lines() + .any(|line| line.ends_with(temporary_path.file_name().unwrap().to_str().unwrap()))); } #[test] fn test_fail_change_directory() { let scene = TestScenario::new(util_name!()); let some_non_existing_path = "some_nonexistent_path"; - assert_eq!(Path::new(some_non_existing_path).is_dir(), false); + assert!(!Path::new(some_non_existing_path).is_dir()); let out = scene .ucmd() @@ -214,6 +205,6 @@ fn test_fail_change_directory() { .arg(some_non_existing_path) .arg("pwd") .fails() - .stderr; + .stderr_move_str(); assert!(out.contains("env: cannot change directory to ")); } diff --git a/tests/by-util/test_expand.rs b/tests/by-util/test_expand.rs index 121ccccec..834a09736 100644 --- a/tests/by-util/test_expand.rs +++ b/tests/by-util/test_expand.rs @@ -2,47 +2,54 @@ use crate::common::util::*; #[test] fn test_with_tab() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("with-tab.txt").run(); - assert!(result.success); - assert!(result.stdout.contains(" ")); - assert!(!result.stdout.contains("\t")); + new_ucmd!() + .arg("with-tab.txt") + .succeeds() + .stdout_contains(" ") + .stdout_does_not_contain("\t"); } #[test] fn test_with_trailing_tab() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("with-trailing-tab.txt").run(); - assert!(result.success); - assert!(result.stdout.contains("with tabs=> ")); - assert!(!result.stdout.contains("\t")); + new_ucmd!() + .arg("with-trailing-tab.txt") + .succeeds() + .stdout_contains("with tabs=> ") + .stdout_does_not_contain("\t"); } #[test] fn test_with_trailing_tab_i() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("with-trailing-tab.txt").arg("-i").run(); - assert!(result.success); - assert!(result.stdout.contains(" // with tabs=>\t")); + new_ucmd!() + .arg("with-trailing-tab.txt") + .arg("-i") + .succeeds() + .stdout_contains(" // with tabs=>\t"); } #[test] fn test_with_tab_size() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("with-tab.txt").arg("--tabs=10").run(); - assert!(result.success); - assert!(result.stdout.contains(" ")); + new_ucmd!() + .arg("with-tab.txt") + .arg("--tabs=10") + .succeeds() + .stdout_contains(" "); } #[test] fn test_with_space() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("with-spaces.txt").run(); - assert!(result.success); - assert!(result.stdout.contains(" return")); + new_ucmd!() + .arg("with-spaces.txt") + .succeeds() + .stdout_contains(" return"); +} + +#[test] +fn test_with_multiple_files() { + new_ucmd!() + .arg("with-spaces.txt") + .arg("with-tab.txt") + .succeeds() + .stdout_contains(" return") + .stdout_contains(" "); } diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index bb0760676..30e3016a3 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -2,55 +2,124 @@ use crate::common::util::*; #[test] fn test_simple_arithmetic() { - new_ucmd!().args(&["1", "+", "1"]).run().stdout_is("2\n"); + new_ucmd!() + .args(&["1", "+", "1"]) + .succeeds() + .stdout_only("2\n"); - new_ucmd!().args(&["1", "-", "1"]).run().stdout_is("0\n"); + new_ucmd!() + .args(&["1", "-", "1"]) + .fails() + .status_code(1) + .stdout_only("0\n"); - new_ucmd!().args(&["3", "*", "2"]).run().stdout_is("6\n"); + new_ucmd!() + .args(&["3", "*", "2"]) + .succeeds() + .stdout_only("6\n"); - new_ucmd!().args(&["4", "/", "2"]).run().stdout_is("2\n"); + new_ucmd!() + .args(&["4", "/", "2"]) + .succeeds() + .stdout_only("2\n"); } #[test] fn test_complex_arithmetic() { - let run = new_ucmd!() + new_ucmd!() .args(&["9223372036854775807", "+", "9223372036854775807"]) - .run(); - run.stdout_is(""); - run.stderr_is("expr: error: +: Numerical result out of range"); + .succeeds() + .stdout_only("18446744073709551614\n"); - let run = new_ucmd!().args(&["9", "/", "0"]).run(); - run.stdout_is(""); - run.stderr_is("expr: error: division by zero"); + new_ucmd!() + .args(&[ + "92233720368547758076549841651981984981498415651", + "%", + "922337203685", + ]) + .succeeds() + .stdout_only("533691697086\n"); + + new_ucmd!() + .args(&[ + "92233720368547758076549841651981984981498415651", + "*", + "922337203685", + ]) + .succeeds() + .stdout_only("85070591730190566808700855121818604965830915152801178873935\n"); + + new_ucmd!() + .args(&[ + "92233720368547758076549841651981984981498415651", + "-", + "922337203685", + ]) + .succeeds() + .stdout_only("92233720368547758076549841651981984059161211966\n"); + + new_ucmd!() + .args(&["9", "/", "0"]) + .fails() + .stderr_only("expr: division by zero\n"); } #[test] fn test_parenthesis() { new_ucmd!() .args(&["(", "1", "+", "1", ")", "*", "2"]) - .run() - .stdout_is("4\n"); + .succeeds() + .stdout_only("4\n"); } #[test] fn test_or() { new_ucmd!() .args(&["0", "|", "foo"]) - .run() - .stdout_is("foo\n"); + .succeeds() + .stdout_only("foo\n"); new_ucmd!() .args(&["foo", "|", "bar"]) - .run() - .stdout_is("foo\n"); + .succeeds() + .stdout_only("foo\n"); } #[test] fn test_and() { new_ucmd!() .args(&["foo", "&", "1"]) - .run() - .stdout_is("foo\n"); + .succeeds() + .stdout_only("foo\n"); new_ucmd!().args(&["", "&", "1"]).run().stdout_is("0\n"); } + +#[test] +fn test_substr() { + new_ucmd!() + .args(&["substr", "abc", "1", "1"]) + .succeeds() + .stdout_only("a\n"); +} + +#[test] +fn test_invalid_substr() { + new_ucmd!() + .args(&["substr", "abc", "0", "1"]) + .fails() + .status_code(1) + .stdout_only("\n"); + + new_ucmd!() + .args(&["substr", "abc", &(std::usize::MAX.to_string() + "0"), "1"]) + .fails() + .status_code(1) + .stdout_only("\n"); + + new_ucmd!() + .args(&["substr", "abc", "0", &(std::usize::MAX.to_string() + "0")]) + .fails() + .status_code(1) + .stdout_only("\n"); +} diff --git a/tests/by-util/test_factor.rs b/tests/by-util/test_factor.rs index 5bde17cdb..a3f06ea8a 100644 --- a/tests/by-util/test_factor.rs +++ b/tests/by-util/test_factor.rs @@ -4,6 +4,9 @@ // // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. +#![allow(clippy::unreadable_literal)] + +// spell-checker:ignore (methods) hexdigest use crate::common::util::*; use std::time::SystemTime; @@ -26,22 +29,31 @@ fn test_first_100000_integers() { extern crate sha1; let n_integers = 100_000; - let mut instring = String::new(); + let mut input_string = String::new(); for i in 0..=n_integers { - instring.push_str(&(format!("{} ", i))[..]); + input_string.push_str(&(format!("{} ", i))[..]); } - println!("STDIN='{}'", instring); - let result = new_ucmd!().pipe_in(instring.as_bytes()).run(); - let stdout = result.stdout; - - assert!(result.success); + println!("STDIN='{}'", input_string); + let result = new_ucmd!().pipe_in(input_string.as_bytes()).succeeds(); // `seq 0 100000 | factor | sha1sum` => "4ed2d8403934fa1c76fe4b84c5d4b8850299c359" - let hash_check = sha1::Sha1::from(stdout.as_bytes()).hexdigest(); + let hash_check = sha1::Sha1::from(result.stdout()).hexdigest(); assert_eq!(hash_check, "4ed2d8403934fa1c76fe4b84c5d4b8850299c359"); } +#[test] +fn test_cli_args() { + // Make sure that factor works with CLI arguments as well. + new_ucmd!().args(&["3"]).succeeds().stdout_contains("3: 3"); + + new_ucmd!() + .args(&["3", "6"]) + .succeeds() + .stdout_contains("3: 3") + .stdout_contains("6: 2 3"); +} + #[test] fn test_random() { use conv::prelude::*; @@ -80,25 +92,25 @@ fn test_random() { }; } - factors.sort(); + factors.sort_unstable(); (product, factors) }; // build an input and expected output string from factor - let mut instring = String::new(); - let mut outstring = String::new(); + let mut input_string = String::new(); + let mut output_string = String::new(); for _ in 0..NUM_TESTS { let (product, factors) = rand_gt(1 << 63); - instring.push_str(&(format!("{} ", product))[..]); + input_string.push_str(&(format!("{} ", product))[..]); - outstring.push_str(&(format!("{}:", product))[..]); + output_string.push_str(&(format!("{}:", product))[..]); for factor in factors { - outstring.push_str(&(format!(" {}", factor))[..]); + output_string.push_str(&(format!(" {}", factor))[..]); } - outstring.push_str("\n"); + output_string.push('\n'); } - run(instring.as_bytes(), outstring.as_bytes()); + run(input_string.as_bytes(), output_string.as_bytes()); } #[test] @@ -110,31 +122,31 @@ fn test_random_big() { println!("rng_seed={:?}", rng_seed); let mut rng = SmallRng::seed_from_u64(rng_seed); - let bitrange_1 = Uniform::new(14_usize, 51); + let bit_range_1 = Uniform::new(14_usize, 51); let mut rand_64 = move || { // first, choose a random number of bits for the first factor - let f_bit_1 = bitrange_1.sample(&mut rng); + let f_bit_1 = bit_range_1.sample(&mut rng); // how many more bits do we need? let rem = 64 - f_bit_1; - // we will have a number of additional factors equal to nfacts + 1 - // where nfacts is in [0, floor(rem/14) ) NOTE half-open interval + // we will have a number of additional factors equal to n_facts + 1 + // where n_facts is in [0, floor(rem/14) ) NOTE half-open interval // Each prime factor is at least 14 bits, hence floor(rem/14) - let nfacts = Uniform::new(0_usize, rem / 14).sample(&mut rng); - // we have to distribute extrabits among the (nfacts + 1) values - let extrabits = rem - (nfacts + 1) * 14; + let n_factors = Uniform::new(0_usize, rem / 14).sample(&mut rng); + // we have to distribute extra_bits among the (n_facts + 1) values + let extra_bits = rem - (n_factors + 1) * 14; // (remember, a Range is a half-open interval) - let extrarange = Uniform::new(0_usize, extrabits + 1); + let extra_range = Uniform::new(0_usize, extra_bits + 1); // to generate an even split of this range, generate n-1 random elements // in the range, add the desired total value to the end, sort this list, // and then compute the sequential differences. let mut f_bits = Vec::new(); - for _ in 0..nfacts { - f_bits.push(extrarange.sample(&mut rng)); + for _ in 0..n_factors { + f_bits.push(extra_range.sample(&mut rng)); } - f_bits.push(extrabits); - f_bits.sort(); + f_bits.push(extra_bits); + f_bits.sort_unstable(); // compute sequential differences here. We leave off the +14 bits // so we can just index PRIMES_BY_BITS @@ -150,62 +162,65 @@ fn test_random_big() { f_bits.push(f_bit_1 - 14); // index of f_bit_1 in PRIMES_BY_BITS let f_bits = f_bits; - let mut nbits = 0; + let mut n_bits = 0; let mut product = 1_u64; let mut factors = Vec::new(); for bit in f_bits { assert!(bit < 37); - nbits += 14 + bit; + n_bits += 14 + bit; let elm = Uniform::new(0, PRIMES_BY_BITS[bit].len()).sample(&mut rng); let factor = PRIMES_BY_BITS[bit][elm]; factors.push(factor); product *= factor; } - assert_eq!(nbits, 64); + assert_eq!(n_bits, 64); - factors.sort(); + factors.sort_unstable(); (product, factors) }; - let mut instring = String::new(); - let mut outstring = String::new(); + let mut input_string = String::new(); + let mut output_string = String::new(); for _ in 0..NUM_TESTS { let (product, factors) = rand_64(); - instring.push_str(&(format!("{} ", product))[..]); + input_string.push_str(&(format!("{} ", product))[..]); - outstring.push_str(&(format!("{}:", product))[..]); + output_string.push_str(&(format!("{}:", product))[..]); for factor in factors { - outstring.push_str(&(format!(" {}", factor))[..]); + output_string.push_str(&(format!(" {}", factor))[..]); } - outstring.push_str("\n"); + output_string.push('\n'); } - run(instring.as_bytes(), outstring.as_bytes()); + run(input_string.as_bytes(), output_string.as_bytes()); } #[test] fn test_big_primes() { - let mut instring = String::new(); - let mut outstring = String::new(); + let mut input_string = String::new(); + let mut output_string = String::new(); for prime in PRIMES64 { - instring.push_str(&(format!("{} ", prime))[..]); - outstring.push_str(&(format!("{0}: {0}\n", prime))[..]); + input_string.push_str(&(format!("{} ", prime))[..]); + output_string.push_str(&(format!("{0}: {0}\n", prime))[..]); } - run(instring.as_bytes(), outstring.as_bytes()); + run(input_string.as_bytes(), output_string.as_bytes()); } -fn run(instring: &[u8], outstring: &[u8]) { - println!("STDIN='{}'", String::from_utf8_lossy(instring)); - println!("STDOUT(expected)='{}'", String::from_utf8_lossy(outstring)); +fn run(input_string: &[u8], output_string: &[u8]) { + println!("STDIN='{}'", String::from_utf8_lossy(input_string)); + println!( + "STDOUT(expected)='{}'", + String::from_utf8_lossy(output_string) + ); // now run factor new_ucmd!() - .pipe_in(instring) + .pipe_in(input_string) .run() - .stdout_is(String::from_utf8(outstring.to_owned()).unwrap()); + .stdout_is(String::from_utf8(output_string.to_owned()).unwrap()); } -const PRIMES_BY_BITS: &'static [&'static [u64]] = &[ +const PRIMES_BY_BITS: &[&[u64]] = &[ PRIMES14, PRIMES15, PRIMES16, PRIMES17, PRIMES18, PRIMES19, PRIMES20, PRIMES21, PRIMES22, PRIMES23, PRIMES24, PRIMES25, PRIMES26, PRIMES27, PRIMES28, PRIMES29, PRIMES30, PRIMES31, PRIMES32, PRIMES33, PRIMES34, PRIMES35, PRIMES36, PRIMES37, PRIMES38, PRIMES39, PRIMES40, @@ -213,7 +228,7 @@ const PRIMES_BY_BITS: &'static [&'static [u64]] = &[ PRIMES50, ]; -const PRIMES64: &'static [u64] = &[ +const PRIMES64: &[u64] = &[ 18446744073709551557, 18446744073709551533, 18446744073709551521, @@ -265,7 +280,7 @@ const PRIMES64: &'static [u64] = &[ 18446744073709549571, ]; -const PRIMES14: &'static [u64] = &[ +const PRIMES14: &[u64] = &[ 16381, 16369, 16363, 16361, 16349, 16339, 16333, 16319, 16301, 16273, 16267, 16253, 16249, 16231, 16229, 16223, 16217, 16193, 16189, 16187, 16183, 16141, 16139, 16127, 16111, 16103, 16097, 16091, 16087, 16073, 16069, 16067, 16063, 16061, 16057, 16033, 16007, 16001, 15991, @@ -277,7 +292,7 @@ const PRIMES14: &'static [u64] = &[ 15373, ]; -const PRIMES15: &'static [u64] = &[ +const PRIMES15: &[u64] = &[ 32749, 32719, 32717, 32713, 32707, 32693, 32687, 32653, 32647, 32633, 32621, 32611, 32609, 32603, 32587, 32579, 32573, 32569, 32563, 32561, 32537, 32533, 32531, 32507, 32503, 32497, 32491, 32479, 32467, 32443, 32441, 32429, 32423, 32413, 32411, 32401, 32381, 32377, 32371, @@ -288,7 +303,7 @@ const PRIMES15: &'static [u64] = &[ 31847, 31817, 31799, 31793, 31771, 31769, 31751, ]; -const PRIMES16: &'static [u64] = &[ +const PRIMES16: &[u64] = &[ 65521, 65519, 65497, 65479, 65449, 65447, 65437, 65423, 65419, 65413, 65407, 65393, 65381, 65371, 65357, 65353, 65327, 65323, 65309, 65293, 65287, 65269, 65267, 65257, 65239, 65213, 65203, 65183, 65179, 65173, 65171, 65167, 65147, 65141, 65129, 65123, 65119, 65111, 65101, @@ -298,7 +313,7 @@ const PRIMES16: &'static [u64] = &[ 64627, 64621, 64613, 64609, 64601, 64591, 64579, 64577, 64567, 64553, ]; -const PRIMES17: &'static [u64] = &[ +const PRIMES17: &[u64] = &[ 131071, 131063, 131059, 131041, 131023, 131011, 131009, 130987, 130981, 130973, 130969, 130957, 130927, 130873, 130859, 130843, 130841, 130829, 130817, 130811, 130807, 130787, 130783, 130769, 130729, 130699, 130693, 130687, 130681, 130657, 130651, 130649, 130643, 130639, 130633, 130631, @@ -309,7 +324,7 @@ const PRIMES17: &'static [u64] = &[ 130073, 130069, 130057, 130051, ]; -const PRIMES18: &'static [u64] = &[ +const PRIMES18: &[u64] = &[ 262139, 262133, 262127, 262121, 262111, 262109, 262103, 262079, 262069, 262051, 262049, 262027, 262007, 261983, 261977, 261973, 261971, 261959, 261917, 261887, 261881, 261847, 261823, 261799, 261791, 261787, 261773, 261761, 261757, 261739, 261721, 261713, 261707, 261697, 261673, 261643, @@ -319,7 +334,7 @@ const PRIMES18: &'static [u64] = &[ 261169, 261167, 261127, ]; -const PRIMES19: &'static [u64] = &[ +const PRIMES19: &[u64] = &[ 524287, 524269, 524261, 524257, 524243, 524231, 524221, 524219, 524203, 524201, 524197, 524189, 524171, 524149, 524123, 524119, 524113, 524099, 524087, 524081, 524071, 524063, 524057, 524053, 524047, 523997, 523987, 523969, 523949, 523937, 523927, 523907, 523903, 523877, 523867, 523847, @@ -329,7 +344,7 @@ const PRIMES19: &'static [u64] = &[ 523403, 523387, 523357, 523351, 523349, 523333, 523307, 523297, ]; -const PRIMES20: &'static [u64] = &[ +const PRIMES20: &[u64] = &[ 1048573, 1048571, 1048559, 1048549, 1048517, 1048507, 1048447, 1048433, 1048423, 1048391, 1048387, 1048367, 1048361, 1048357, 1048343, 1048309, 1048291, 1048273, 1048261, 1048219, 1048217, 1048213, 1048193, 1048189, 1048139, 1048129, 1048127, 1048123, 1048063, 1048051, @@ -339,7 +354,7 @@ const PRIMES20: &'static [u64] = &[ 1047691, 1047689, 1047671, 1047667, 1047653, 1047649, 1047647, 1047589, 1047587, 1047559, ]; -const PRIMES21: &'static [u64] = &[ +const PRIMES21: &[u64] = &[ 2097143, 2097133, 2097131, 2097097, 2097091, 2097083, 2097047, 2097041, 2097031, 2097023, 2097013, 2096993, 2096987, 2096971, 2096959, 2096957, 2096947, 2096923, 2096911, 2096909, 2096893, 2096881, 2096873, 2096867, 2096851, 2096837, 2096807, 2096791, 2096789, 2096777, @@ -349,7 +364,7 @@ const PRIMES21: &'static [u64] = &[ 2096221, 2096209, 2096191, 2096183, 2096147, ]; -const PRIMES22: &'static [u64] = &[ +const PRIMES22: &[u64] = &[ 4194301, 4194287, 4194277, 4194271, 4194247, 4194217, 4194199, 4194191, 4194187, 4194181, 4194173, 4194167, 4194143, 4194137, 4194131, 4194107, 4194103, 4194023, 4194011, 4194007, 4193977, 4193971, 4193963, 4193957, 4193939, 4193929, 4193909, 4193869, 4193807, 4193803, @@ -359,7 +374,7 @@ const PRIMES22: &'static [u64] = &[ 4193297, ]; -const PRIMES23: &'static [u64] = &[ +const PRIMES23: &[u64] = &[ 8388593, 8388587, 8388581, 8388571, 8388547, 8388539, 8388473, 8388461, 8388451, 8388449, 8388439, 8388427, 8388421, 8388409, 8388377, 8388371, 8388319, 8388301, 8388287, 8388283, 8388277, 8388239, 8388209, 8388187, 8388113, 8388109, 8388091, 8388071, 8388059, 8388019, @@ -368,7 +383,7 @@ const PRIMES23: &'static [u64] = &[ 8387723, 8387707, 8387671, 8387611, 8387609, 8387591, ]; -const PRIMES24: &'static [u64] = &[ +const PRIMES24: &[u64] = &[ 16777213, 16777199, 16777183, 16777153, 16777141, 16777139, 16777127, 16777121, 16777099, 16777049, 16777027, 16776989, 16776973, 16776971, 16776967, 16776961, 16776941, 16776937, 16776931, 16776919, 16776901, 16776899, 16776869, 16776857, 16776839, 16776833, 16776817, @@ -378,7 +393,7 @@ const PRIMES24: &'static [u64] = &[ 16776317, 16776313, 16776289, 16776217, 16776211, ]; -const PRIMES25: &'static [u64] = &[ +const PRIMES25: &[u64] = &[ 33554393, 33554383, 33554371, 33554347, 33554341, 33554317, 33554291, 33554273, 33554267, 33554249, 33554239, 33554221, 33554201, 33554167, 33554159, 33554137, 33554123, 33554093, 33554083, 33554077, 33554051, 33554021, 33554011, 33554009, 33553999, 33553991, 33553969, @@ -388,7 +403,7 @@ const PRIMES25: &'static [u64] = &[ 33553519, 33553517, 33553511, 33553489, 33553463, 33553451, 33553417, ]; -const PRIMES26: &'static [u64] = &[ +const PRIMES26: &[u64] = &[ 67108859, 67108837, 67108819, 67108777, 67108763, 67108757, 67108753, 67108747, 67108739, 67108729, 67108721, 67108709, 67108693, 67108669, 67108667, 67108661, 67108649, 67108633, 67108597, 67108579, 67108529, 67108511, 67108507, 67108493, 67108471, 67108463, 67108453, @@ -399,7 +414,7 @@ const PRIMES26: &'static [u64] = &[ 67107863, ]; -const PRIMES27: &'static [u64] = &[ +const PRIMES27: &[u64] = &[ 134217689, 134217649, 134217617, 134217613, 134217593, 134217541, 134217529, 134217509, 134217497, 134217493, 134217487, 134217467, 134217439, 134217437, 134217409, 134217403, 134217401, 134217367, 134217361, 134217353, 134217323, 134217301, 134217277, 134217257, @@ -410,7 +425,7 @@ const PRIMES27: &'static [u64] = &[ 134216737, 134216729, ]; -const PRIMES28: &'static [u64] = &[ +const PRIMES28: &[u64] = &[ 268435399, 268435367, 268435361, 268435337, 268435331, 268435313, 268435291, 268435273, 268435243, 268435183, 268435171, 268435157, 268435147, 268435133, 268435129, 268435121, 268435109, 268435091, 268435067, 268435043, 268435039, 268435033, 268435019, 268435009, @@ -421,7 +436,7 @@ const PRIMES28: &'static [u64] = &[ 268434479, 268434461, ]; -const PRIMES29: &'static [u64] = &[ +const PRIMES29: &[u64] = &[ 536870909, 536870879, 536870869, 536870849, 536870839, 536870837, 536870819, 536870813, 536870791, 536870779, 536870767, 536870743, 536870729, 536870723, 536870717, 536870701, 536870683, 536870657, 536870641, 536870627, 536870611, 536870603, 536870599, 536870573, @@ -431,7 +446,7 @@ const PRIMES29: &'static [u64] = &[ 536870027, 536869999, 536869951, 536869943, 536869937, 536869919, 536869901, 536869891, ]; -const PRIMES30: &'static [u64] = &[ +const PRIMES30: &[u64] = &[ 1073741789, 1073741783, 1073741741, 1073741723, 1073741719, 1073741717, 1073741689, 1073741671, 1073741663, 1073741651, 1073741621, 1073741567, 1073741561, 1073741527, 1073741503, 1073741477, 1073741467, 1073741441, 1073741419, 1073741399, 1073741387, 1073741381, 1073741371, 1073741329, @@ -440,7 +455,7 @@ const PRIMES30: &'static [u64] = &[ 1073740853, 1073740847, 1073740819, 1073740807, ]; -const PRIMES31: &'static [u64] = &[ +const PRIMES31: &[u64] = &[ 2147483647, 2147483629, 2147483587, 2147483579, 2147483563, 2147483549, 2147483543, 2147483497, 2147483489, 2147483477, 2147483423, 2147483399, 2147483353, 2147483323, 2147483269, 2147483249, 2147483237, 2147483179, 2147483171, 2147483137, 2147483123, 2147483077, 2147483069, 2147483059, @@ -449,7 +464,7 @@ const PRIMES31: &'static [u64] = &[ 2147482763, 2147482739, 2147482697, 2147482693, 2147482681, 2147482663, 2147482661, ]; -const PRIMES32: &'static [u64] = &[ +const PRIMES32: &[u64] = &[ 4294967291, 4294967279, 4294967231, 4294967197, 4294967189, 4294967161, 4294967143, 4294967111, 4294967087, 4294967029, 4294966997, 4294966981, 4294966943, 4294966927, 4294966909, 4294966877, 4294966829, 4294966813, 4294966769, 4294966667, 4294966661, 4294966657, 4294966651, 4294966639, @@ -457,7 +472,7 @@ const PRIMES32: &'static [u64] = &[ 4294966373, 4294966367, 4294966337, 4294966297, ]; -const PRIMES33: &'static [u64] = &[ +const PRIMES33: &[u64] = &[ 8589934583, 8589934567, 8589934543, 8589934513, 8589934487, 8589934307, 8589934291, 8589934289, 8589934271, 8589934237, 8589934211, 8589934207, 8589934201, 8589934187, 8589934151, 8589934141, 8589934139, 8589934117, 8589934103, 8589934099, 8589934091, 8589934069, 8589934049, 8589934027, @@ -466,7 +481,7 @@ const PRIMES33: &'static [u64] = &[ 8589933647, 8589933641, 8589933637, 8589933631, 8589933629, 8589933619, 8589933601, 8589933581, ]; -const PRIMES34: &'static [u64] = &[ +const PRIMES34: &[u64] = &[ 17179869143, 17179869107, 17179869071, @@ -517,7 +532,7 @@ const PRIMES34: &'static [u64] = &[ 17179868183, ]; -const PRIMES35: &'static [u64] = &[ +const PRIMES35: &[u64] = &[ 34359738337, 34359738319, 34359738307, @@ -550,7 +565,7 @@ const PRIMES35: &'static [u64] = &[ 34359737371, ]; -const PRIMES36: &'static [u64] = &[ +const PRIMES36: &[u64] = &[ 68719476731, 68719476719, 68719476713, @@ -602,7 +617,7 @@ const PRIMES36: &'static [u64] = &[ 68719475729, ]; -const PRIMES37: &'static [u64] = &[ +const PRIMES37: &[u64] = &[ 137438953447, 137438953441, 137438953427, @@ -628,7 +643,7 @@ const PRIMES37: &'static [u64] = &[ 137438952491, ]; -const PRIMES38: &'static [u64] = &[ +const PRIMES38: &[u64] = &[ 274877906899, 274877906857, 274877906837, @@ -668,7 +683,7 @@ const PRIMES38: &'static [u64] = &[ 274877905931, ]; -const PRIMES39: &'static [u64] = &[ +const PRIMES39: &[u64] = &[ 549755813881, 549755813869, 549755813821, @@ -714,7 +729,7 @@ const PRIMES39: &'static [u64] = &[ 549755812867, ]; -const PRIMES40: &'static [u64] = &[ +const PRIMES40: &[u64] = &[ 1099511627689, 1099511627609, 1099511627581, @@ -744,7 +759,7 @@ const PRIMES40: &'static [u64] = &[ 1099511626771, ]; -const PRIMES41: &'static [u64] = &[ +const PRIMES41: &[u64] = &[ 2199023255531, 2199023255521, 2199023255497, @@ -783,7 +798,7 @@ const PRIMES41: &'static [u64] = &[ 2199023254567, ]; -const PRIMES42: &'static [u64] = &[ +const PRIMES42: &[u64] = &[ 4398046511093, 4398046511087, 4398046511071, @@ -823,7 +838,7 @@ const PRIMES42: &'static [u64] = &[ 4398046510093, ]; -const PRIMES43: &'static [u64] = &[ +const PRIMES43: &[u64] = &[ 8796093022151, 8796093022141, 8796093022091, @@ -858,7 +873,7 @@ const PRIMES43: &'static [u64] = &[ 8796093021269, ]; -const PRIMES44: &'static [u64] = &[ +const PRIMES44: &[u64] = &[ 17592186044399, 17592186044299, 17592186044297, @@ -891,7 +906,7 @@ const PRIMES44: &'static [u64] = &[ 17592186043409, ]; -const PRIMES45: &'static [u64] = &[ +const PRIMES45: &[u64] = &[ 35184372088777, 35184372088763, 35184372088751, @@ -932,7 +947,7 @@ const PRIMES45: &'static [u64] = &[ 35184372087869, ]; -const PRIMES46: &'static [u64] = &[ +const PRIMES46: &[u64] = &[ 70368744177643, 70368744177607, 70368744177601, @@ -967,7 +982,7 @@ const PRIMES46: &'static [u64] = &[ 70368744176711, ]; -const PRIMES47: &'static [u64] = &[ +const PRIMES47: &[u64] = &[ 140737488355213, 140737488355201, 140737488355181, @@ -989,7 +1004,7 @@ const PRIMES47: &'static [u64] = &[ 140737488354329, ]; -const PRIMES48: &'static [u64] = &[ +const PRIMES48: &[u64] = &[ 281474976710597, 281474976710591, 281474976710567, @@ -1022,7 +1037,7 @@ const PRIMES48: &'static [u64] = &[ 281474976709637, ]; -const PRIMES49: &'static [u64] = &[ +const PRIMES49: &[u64] = &[ 562949953421231, 562949953421201, 562949953421189, @@ -1056,7 +1071,7 @@ const PRIMES49: &'static [u64] = &[ 562949953420297, ]; -const PRIMES50: &'static [u64] = &[ +const PRIMES50: &[u64] = &[ 1125899906842597, 1125899906842589, 1125899906842573, diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index 4533cdf24..0d6d9bb24 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -5,7 +5,7 @@ fn test_fmt() { let result = new_ucmd!().arg("one-word-per-line.txt").run(); //.stdout_is_fixture("call_graph.expected"); assert_eq!( - result.stdout.trim(), + result.stdout_str().trim(), "this is a file with one word per line" ); } @@ -15,7 +15,7 @@ fn test_fmt_q() { let result = new_ucmd!().arg("-q").arg("one-word-per-line.txt").run(); //.stdout_is_fixture("call_graph.expected"); assert_eq!( - result.stdout.trim(), + result.stdout_str().trim(), "this is a file with one word per line" ); } @@ -29,22 +29,20 @@ fn test_fmt_w_too_big() { .run(); //.stdout_is_fixture("call_graph.expected"); assert_eq!( - result.stderr.trim(), - "fmt: error: invalid width: '2501': Numerical result out of range" + result.stderr_str().trim(), + "fmt: invalid width: '2501': Numerical result out of range" ); } -/* #[test] - Fails for now, see https://github.com/uutils/coreutils/issues/1501 +#[test] fn test_fmt_w() { let result = new_ucmd!() .arg("-w") .arg("10") .arg("one-word-per-line.txt") .run(); - //.stdout_is_fixture("call_graph.expected"); - assert_eq!(result.stdout.trim(), "this is a file with one word per line"); + //.stdout_is_fixture("call_graph.expected"); + assert_eq!( + result.stdout_str().trim(), + "this is\na file\nwith one\nword per\nline" + ); } - - -fmt is pretty broken in general, needs more works to have more tests - */ diff --git a/tests/by-util/test_fold.rs b/tests/by-util/test_fold.rs index c17f300d2..1acdadac1 100644 --- a/tests/by-util/test_fold.rs +++ b/tests/by-util/test_fold.rs @@ -25,9 +25,521 @@ fn test_40_column_word_boundary() { } #[test] -fn test_default_warp_with_newlines() { +fn test_default_wrap_with_newlines() { new_ucmd!() .arg("lorem_ipsum_new_line.txt") .run() .stdout_is_fixture("lorem_ipsum_new_line_80_column.expected"); } + +#[test] +fn test_should_preserve_empty_line_without_final_newline() { + new_ucmd!() + .arg("-w2") + .pipe_in("12\n\n34") + .succeeds() + .stdout_is("12\n\n34"); +} + +#[test] +fn test_should_preserve_empty_line_and_final_newline() { + new_ucmd!() + .arg("-w2") + .pipe_in("12\n\n34\n") + .succeeds() + .stdout_is("12\n\n34\n"); +} + +#[test] +fn test_should_preserve_empty_lines() { + new_ucmd!().pipe_in("\n").succeeds().stdout_is("\n"); + + new_ucmd!() + .arg("-w1") + .pipe_in("0\n1\n\n2\n\n\n") + .succeeds() + .stdout_is("0\n1\n\n2\n\n\n"); +} + +#[test] +fn test_word_boundary_split_should_preserve_empty_lines() { + new_ucmd!() + .arg("-s") + .pipe_in("\n") + .succeeds() + .stdout_is("\n"); + + new_ucmd!() + .args(&["-w1", "-s"]) + .pipe_in("0\n1\n\n2\n\n\n") + .succeeds() + .stdout_is("0\n1\n\n2\n\n\n"); +} + +#[test] +fn test_should_not_add_newline_when_line_less_than_fold() { + new_ucmd!().pipe_in("1234").succeeds().stdout_is("1234"); +} + +#[test] +fn test_should_not_add_newline_when_line_longer_than_fold() { + new_ucmd!() + .arg("-w2") + .pipe_in("1234") + .succeeds() + .stdout_is("12\n34"); +} + +#[test] +fn test_should_not_add_newline_when_line_equal_to_fold() { + new_ucmd!() + .arg("-w1") + .pipe_in(" ") + .succeeds() + .stdout_is(" "); +} + +#[test] +fn test_should_preserve_final_newline_when_line_less_than_fold() { + new_ucmd!().pipe_in("1234\n").succeeds().stdout_is("1234\n"); +} + +#[test] +fn test_should_preserve_final_newline_when_line_longer_than_fold() { + new_ucmd!() + .arg("-w2") + .pipe_in("1234\n") + .succeeds() + .stdout_is("12\n34\n"); +} + +#[test] +fn test_should_preserve_final_newline_when_line_equal_to_fold() { + new_ucmd!() + .arg("-w2") + .pipe_in("1\n") + .succeeds() + .stdout_is("1\n"); +} + +#[test] +fn test_single_tab_should_not_add_extra_newline() { + new_ucmd!() + .arg("-w1") + .pipe_in("\t") + .succeeds() + .stdout_is("\t"); +} + +#[test] +fn test_initial_tab_counts_as_8_columns() { + new_ucmd!() + .arg("-w8") + .pipe_in("\t1") + .succeeds() + .stdout_is("\t\n1"); +} + +#[test] +fn test_tab_should_advance_to_next_tab_stop() { + // tab advances the column count to the next tab stop, i.e. the width + // of the tab varies based on the leading text + new_ucmd!() + .args(&["-w8", "tab_stops.input"]) + .succeeds() + .stdout_is_fixture("tab_stops_w8.expected"); +} + +#[test] +fn test_all_tabs_should_advance_to_next_tab_stops() { + new_ucmd!() + .args(&["-w16", "tab_stops.input"]) + .succeeds() + .stdout_is_fixture("tab_stops_w16.expected"); +} + +#[test] +fn test_fold_before_tab_with_narrow_width() { + new_ucmd!() + .arg("-w7") + .pipe_in("a\t1") + .succeeds() + .stdout_is("a\n\t\n1"); +} + +#[test] +fn test_fold_at_word_boundary() { + new_ucmd!() + .args(&["-w4", "-s"]) + .pipe_in("one two") + .succeeds() + .stdout_is("one \ntwo"); +} + +#[test] +fn test_fold_at_leading_word_boundary() { + new_ucmd!() + .args(&["-w3", "-s"]) + .pipe_in(" aaa") + .succeeds() + .stdout_is(" \naaa"); +} + +#[test] +fn test_fold_at_word_boundary_preserve_final_newline() { + new_ucmd!() + .args(&["-w4", "-s"]) + .pipe_in("one two\n") + .succeeds() + .stdout_is("one \ntwo\n"); +} + +#[test] +fn test_fold_at_tab() { + new_ucmd!() + .arg("-w8") + .pipe_in("a\tbbb\n") + .succeeds() + .stdout_is("a\t\nbbb\n"); +} + +#[test] +fn test_fold_after_tab() { + new_ucmd!() + .arg("-w10") + .pipe_in("a\tbbb\n") + .succeeds() + .stdout_is("a\tbb\nb\n"); +} + +#[test] +fn test_fold_at_tab_as_word_boundary() { + new_ucmd!() + .args(&["-w8", "-s"]) + .pipe_in("a\tbbb\n") + .succeeds() + .stdout_is("a\t\nbbb\n"); +} + +#[test] +fn test_fold_after_tab_as_word_boundary() { + new_ucmd!() + .args(&["-w10", "-s"]) + .pipe_in("a\tbbb\n") + .succeeds() + .stdout_is("a\t\nbbb\n"); +} + +#[test] +fn test_fold_at_word_boundary_only_whitespace() { + new_ucmd!() + .args(&["-w2", "-s"]) + .pipe_in(" ") + .succeeds() + .stdout_is(" \n "); +} + +#[test] +fn test_fold_at_word_boundary_only_whitespace_preserve_final_newline() { + new_ucmd!() + .args(&["-w2", "-s"]) + .pipe_in(" \n") + .succeeds() + .stdout_is(" \n \n"); +} + +#[test] +fn test_backspace_should_be_preserved() { + new_ucmd!().pipe_in("\x08").succeeds().stdout_is("\x08"); +} + +#[test] +fn test_backspaced_char_should_be_preserved() { + new_ucmd!().pipe_in("x\x08").succeeds().stdout_is("x\x08"); +} + +#[test] +fn test_backspace_should_decrease_column_count() { + new_ucmd!() + .arg("-w2") + .pipe_in("1\x08345") + .succeeds() + .stdout_is("1\x0834\n5"); +} + +#[test] +fn test_backspace_should_not_decrease_column_count_past_zero() { + new_ucmd!() + .arg("-w2") + .pipe_in("1\x08\x083456") + .succeeds() + .stdout_is("1\x08\x0834\n56"); +} + +#[test] +fn test_backspace_is_not_word_boundary() { + new_ucmd!() + .args(&["-w10", "-s"]) + .pipe_in("foobar\x086789abcdef") + .succeeds() + .stdout_is("foobar\x086789a\nbcdef"); // spell-checker:disable-line +} + +#[test] +fn test_carriage_return_should_be_preserved() { + new_ucmd!().pipe_in("\r").succeeds().stdout_is("\r"); +} + +#[test] +fn test_carriage_return_overwritten_char_should_be_preserved() { + new_ucmd!().pipe_in("x\ry").succeeds().stdout_is("x\ry"); +} + +#[test] +fn test_carriage_return_should_reset_column_count() { + new_ucmd!() + .arg("-w6") + .pipe_in("12345\r123456789abcdef") + .succeeds() + .stdout_is("12345\r123456\n789abc\ndef"); +} + +#[test] +fn test_carriage_return_is_not_word_boundary() { + new_ucmd!() + .args(&["-w6", "-s"]) + .pipe_in("fizz\rbuzz\rfizzbuzz") // spell-checker:disable-line + .succeeds() + .stdout_is("fizz\rbuzz\rfizzbu\nzz"); // spell-checker:disable-line +} + +// +// bytewise tests + +#[test] +fn test_bytewise_should_preserve_empty_line_without_final_newline() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("123\n\n45") + .succeeds() + .stdout_is("12\n3\n\n45"); +} + +#[test] +fn test_bytewise_should_preserve_empty_line_and_final_newline() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("12\n\n34\n") + .succeeds() + .stdout_is("12\n\n34\n"); +} + +#[test] +fn test_bytewise_should_preserve_empty_lines() { + new_ucmd!() + .arg("-b") + .pipe_in("\n") + .succeeds() + .stdout_is("\n"); + + new_ucmd!() + .args(&["-w1", "-b"]) + .pipe_in("0\n1\n\n2\n\n\n") + .succeeds() + .stdout_is("0\n1\n\n2\n\n\n"); +} + +#[test] +fn test_bytewise_word_boundary_split_should_preserve_empty_lines() { + new_ucmd!() + .args(&["-s", "-b"]) + .pipe_in("\n") + .succeeds() + .stdout_is("\n"); + + new_ucmd!() + .args(&["-w1", "-s", "-b"]) + .pipe_in("0\n1\n\n2\n\n\n") + .succeeds() + .stdout_is("0\n1\n\n2\n\n\n"); +} + +#[test] +fn test_bytewise_should_not_add_newline_when_line_less_than_fold() { + new_ucmd!() + .arg("-b") + .pipe_in("1234") + .succeeds() + .stdout_is("1234"); +} + +#[test] +fn test_bytewise_should_not_add_newline_when_line_longer_than_fold() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("1234") + .succeeds() + .stdout_is("12\n34"); +} + +#[test] +fn test_bytewise_should_not_add_newline_when_line_equal_to_fold() { + new_ucmd!() + .args(&["-w1", "-b"]) + .pipe_in(" ") + .succeeds() + .stdout_is(" "); +} + +#[test] +fn test_bytewise_should_preserve_final_newline_when_line_less_than_fold() { + new_ucmd!() + .arg("-b") + .pipe_in("1234\n") + .succeeds() + .stdout_is("1234\n"); +} + +#[test] +fn test_bytewise_should_preserve_final_newline_when_line_longer_than_fold() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("1234\n") + .succeeds() + .stdout_is("12\n34\n"); +} + +#[test] +fn test_bytewise_should_preserve_final_newline_when_line_equal_to_fold() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("1\n") + .succeeds() + .stdout_is("1\n"); +} + +#[test] +fn test_bytewise_single_tab_should_not_add_extra_newline() { + new_ucmd!() + .args(&["-w1", "-b"]) + .pipe_in("\t") + .succeeds() + .stdout_is("\t"); +} + +#[test] +fn test_tab_counts_as_one_byte() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("1\t2\n") + .succeeds() + .stdout_is("1\t\n2\n"); +} + +#[test] +fn test_bytewise_fold_before_tab_with_narrow_width() { + new_ucmd!() + .args(&["-w7", "-b"]) + .pipe_in("a\t1") + .succeeds() + .stdout_is("a\t1"); +} + +#[test] +fn test_bytewise_fold_at_word_boundary_only_whitespace() { + new_ucmd!() + .args(&["-w2", "-s", "-b"]) + .pipe_in(" ") + .succeeds() + .stdout_is(" \n "); +} + +#[test] +fn test_bytewise_fold_at_word_boundary_only_whitespace_preserve_final_newline() { + new_ucmd!() + .args(&["-w2", "-s", "-b"]) + .pipe_in(" \n") + .succeeds() + .stdout_is(" \n \n"); +} + +#[test] +fn test_bytewise_backspace_should_be_preserved() { + new_ucmd!() + .arg("-b") + .pipe_in("\x08") + .succeeds() + .stdout_is("\x08"); +} + +#[test] +fn test_bytewise_backspaced_char_should_be_preserved() { + new_ucmd!() + .arg("-b") + .pipe_in("x\x08") + .succeeds() + .stdout_is("x\x08"); +} + +#[test] +fn test_bytewise_backspace_should_not_decrease_column_count() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("1\x08345") + .succeeds() + .stdout_is("1\x08\n34\n5"); +} + +#[test] +fn test_bytewise_backspace_is_not_word_boundary() { + new_ucmd!() + .args(&["-w10", "-s", "-b"]) + .pipe_in("foobar\x0889abcdef") + .succeeds() + .stdout_is("foobar\x0889a\nbcdef"); // spell-checker:disable-line +} + +#[test] +fn test_bytewise_carriage_return_should_be_preserved() { + new_ucmd!() + .arg("-b") + .pipe_in("\r") + .succeeds() + .stdout_is("\r"); +} + +#[test] +fn test_bytewise_carriage_return_overwritten_char_should_be_preserved() { + new_ucmd!() + .arg("-b") + .pipe_in("x\ry") + .succeeds() + .stdout_is("x\ry"); +} + +#[test] +fn test_bytewise_carriage_return_should_not_reset_column_count() { + new_ucmd!() + .args(&["-w6", "-b"]) + .pipe_in("12345\r123456789abcdef") + .succeeds() + .stdout_is("12345\r\n123456\n789abc\ndef"); +} + +#[test] +fn test_bytewise_carriage_return_is_not_word_boundary() { + new_ucmd!() + .args(&["-w6", "-s", "-b"]) + .pipe_in("fizz\rbuzz\rfizzbuzz") // spell-checker:disable-line + .succeeds() + .stdout_is("fizz\rb\nuzz\rfi\nzzbuzz"); // spell-checker:disable-line +} +#[test] +fn test_obsolete_syntax() { + new_ucmd!() + .arg("-5") + .arg("-s") + .arg("space_separated_words.txt") + .succeeds() + .stdout_is("test1\n \ntest2\n \ntest3\n \ntest4\n \ntest5\n \ntest6\n "); +} diff --git a/tests/by-util/test_groups.rs b/tests/by-util/test_groups.rs index 5c326fe2d..9bd0cd12a 100644 --- a/tests/by-util/test_groups.rs +++ b/tests/by-util/test_groups.rs @@ -1,46 +1,176 @@ use crate::common::util::*; +// spell-checker:ignore (ToDO) coreutil + +// These tests run the GNU coreutils `(g)groups` binary in `$PATH` in order to gather reference values. +// If the `(g)groups` in `$PATH` doesn't include a coreutils version string, +// or the version is too low, the test is skipped. + +// The reference version is 8.32. Here 8.30 was chosen because right now there's no +// ubuntu image for github action available with a higher version than 8.30. +const VERSION_MIN: &str = "8.30"; // minimum Version for the reference `groups` in $PATH +const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 8.31 +const UUTILS_WARNING: &str = "uutils-tests-warning"; +const UUTILS_INFO: &str = "uutils-tests-info"; + +macro_rules! unwrap_or_return { + ( $e:expr ) => { + match $e { + Ok(x) => x, + Err(e) => { + println!("{}: test skipped: {}", UUTILS_INFO, e); + return; + } + } + }; +} + +fn whoami() -> String { + // Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'. + // + // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)" + // whoami: cannot find name for user ID 1001 + // id --name: cannot find name for user ID 1001 + // id --name: cannot find name for group ID 116 + // + // However, when running "id" from within "/bin/bash" it looks fine: + // id: "uid=1001(runner) gid=118(docker) groups=118(docker),4(adm),101(systemd-journal)" + // whoami: "runner" + + // Use environment variable to get current user instead of + // invoking `whoami` and fall back to user "nobody" on error. + std::env::var("USER").unwrap_or_else(|e| { + println!("{}: {}, using \"nobody\" instead", UUTILS_WARNING, e); + "nobody".to_string() + }) +} + #[test] +#[cfg(unix)] fn test_groups() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if is_ci() && result.stdout.trim().is_empty() { - // In the CI, some server are failing to return the group. - // As seems to be a configuration issue, ignoring it - return; - } - assert!(result.success); - assert!(!result.stdout.trim().is_empty()); + let result = new_ucmd!().run(); + let exp_result = unwrap_or_return!(expected_result(&[])); + + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); } #[test] -fn test_groups_arg() { - // get the username with the "id -un" command - let result = TestScenario::new("id").ucmd_keepenv().arg("-un").run(); - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - let s1 = String::from(result.stdout.trim()); - if is_ci() && s1.parse::().is_ok() { - // In the CI, some server are failing to return id -un. - // So, if we are getting a uid, just skip this test - // As seems to be a configuration issue, ignoring it +#[cfg(unix)] +fn test_groups_username() { + let test_users = [&whoami()[..]]; + + let result = new_ucmd!().args(&test_users).run(); + let exp_result = unwrap_or_return!(expected_result(&test_users)); + + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); +} + +#[test] +#[cfg(unix)] +fn test_groups_username_multiple() { + // TODO: [2021-06; jhscheer] refactor this as `let util_name = host_name_for(util_name!())` when that function is added to 'tests/common' + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(all(unix, not(target_os = "linux")))] + let util_name = &format!("g{}", util_name!()); + let version_check_string = check_coreutil_version(util_name, VERSION_MIN_MULTIPLE_USERS); + if version_check_string.starts_with(UUTILS_WARNING) { + println!("{}\ntest skipped", version_check_string); return; } + let test_users = ["root", "man", "postfix", "sshd", &whoami()]; - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - assert!(!result.stdout.is_empty()); - let username = result.stdout.trim(); + let result = new_ucmd!().args(&test_users).run(); + let exp_result = unwrap_or_return!(expected_result(&test_users)); - // call groups with the user name to check that we - // are getting something - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg(username).run(); - println!("result.stdout {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - assert!(!result.stdout.is_empty()); + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); +} + +fn check_coreutil_version(util_name: &str, version_expected: &str) -> String { + // example: + // $ id --version | head -n 1 + // id (GNU coreutils) 8.32.162-4eda + let scene = TestScenario::new(util_name); + let version_check = scene + .cmd_keepenv(&util_name) + .env("LC_ALL", "C") + .arg("--version") + .run(); + version_check + .stdout_str() + .split('\n') + .collect::>() + .get(0) + .map_or_else( + || format!("{}: unexpected output format for reference coreutil: '{} --version'", UUTILS_WARNING, util_name), + |s| { + if s.contains(&format!("(GNU coreutils) {}", version_expected)) { + s.to_string() + } else if s.contains("(GNU coreutils)") { + let version_found = s.split_whitespace().last().unwrap()[..4].parse::().unwrap_or_default(); + let version_expected = version_expected.parse::().unwrap_or_default(); + if version_found > version_expected { + format!("{}: version for the reference coreutil '{}' is higher than expected; expected: {}, found: {}", UUTILS_INFO, util_name, version_expected, version_found) + } else { + format!("{}: version for the reference coreutil '{}' does not match; expected: {}, found: {}", UUTILS_WARNING, util_name, version_expected, version_found) } + } else { + format!("{}: no coreutils version string found for reference coreutils '{} --version'", UUTILS_WARNING, util_name) + } + }, + ) +} + +#[allow(clippy::needless_borrow)] +#[cfg(unix)] +fn expected_result(args: &[&str]) -> Result { + // TODO: [2021-06; jhscheer] refactor this as `let util_name = host_name_for(util_name!())` when that function is added to 'tests/common' + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(all(unix, not(target_os = "linux")))] + let util_name = &format!("g{}", util_name!()); + + let version_check_string = check_coreutil_version(util_name, VERSION_MIN); + if version_check_string.starts_with(UUTILS_WARNING) { + return Err(version_check_string); + } + println!("{}", version_check_string); + + let scene = TestScenario::new(util_name); + let result = scene + .cmd_keepenv(util_name) + .env("LC_ALL", "C") + .args(args) + .run(); + + let (stdout, stderr): (String, String) = if cfg!(target_os = "linux") { + ( + result.stdout_str().to_string(), + result.stderr_str().to_string(), + ) + } else { + // strip 'g' prefix from results: + let from = util_name.to_string() + ":"; + let to = &from[1..]; + ( + result.stdout_str().replace(&from, to), + result.stderr_str().replace(&from, to), + ) + }; + + Ok(CmdResult::new( + Some(result.tmpd()), + Some(result.code()), + result.succeeded(), + stdout.as_bytes(), + stderr.as_bytes(), + )) } diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 6e7d59107..f059e53f3 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -17,14 +17,14 @@ macro_rules! test_digest { fn test_single_file() { let ts = TestScenario::new("hashsum"); assert_eq!(ts.fixtures.read(EXPECTED_FILE), - get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("input.txt").succeeds().no_stderr().stdout)); + get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("input.txt").succeeds().no_stderr().stdout_str())); } #[test] fn test_stdin() { let ts = TestScenario::new("hashsum"); assert_eq!(ts.fixtures.read(EXPECTED_FILE), - get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout)); + get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout_str())); } } )*) diff --git a/tests/by-util/test_head.rs b/tests/by-util/test_head.rs old mode 100644 new mode 100755 index 4324290cb..8065cb490 --- a/tests/by-util/test_head.rs +++ b/tests/by-util/test_head.rs @@ -1,6 +1,13 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +// spell-checker:ignore (words) bogusfile emptyfile abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstu + use crate::common::util::*; -static INPUT: &'static str = "lorem_ipsum.txt"; +static INPUT: &str = "lorem_ipsum.txt"; #[test] fn test_stdin_default() { @@ -89,73 +96,214 @@ fn test_verbose() { #[test] #[ignore] fn test_spams_newline() { + //this test is does not mirror what GNU does new_ucmd!().pipe_in("a").succeeds().stdout_is("a\n"); } #[test] -#[ignore] -fn test_unsupported_byte_syntax() { +fn test_byte_syntax() { new_ucmd!() .args(&["-1c"]) .pipe_in("abc") - .fails() - //GNU head returns "a" - .stdout_is("") - .stderr_is("head: error: Unrecognized option: \'1\'"); + .run() + .stdout_is("a"); } #[test] -#[ignore] -fn test_unsupported_line_syntax() { +fn test_line_syntax() { new_ucmd!() .args(&["-n", "2048m"]) .pipe_in("a\n") - .fails() - //.stdout_is("a\n"); What GNU head returns. - .stdout_is("") - .stderr_is("head: error: invalid line count \'2048m\': invalid digit found in string"); + .run() + .stdout_is("a\n"); } #[test] -#[ignore] -fn test_unsupported_zero_terminated_syntax() { +fn test_zero_terminated_syntax() { new_ucmd!() - .args(&["-z -n 1"]) + .args(&["-z", "-n", "1"]) .pipe_in("x\0y") - .fails() - //GNU Head returns "x\0" - .stderr_is("head: error: Unrecognized option: \'z\'"); + .run() + .stdout_is("x\0"); } #[test] -#[ignore] -fn test_unsupported_zero_terminated_syntax_2() { +fn test_zero_terminated_syntax_2() { new_ucmd!() - .args(&["-z -n 2"]) + .args(&["-z", "-n", "2"]) .pipe_in("x\0y") - .fails() - //GNU Head returns "x\0y" - .stderr_is("head: error: Unrecognized option: \'z\'"); + .run() + .stdout_is("x\0y"); } #[test] -#[ignore] -fn test_unsupported_negative_byte_syntax() { +fn test_zero_terminated_negative_lines() { + new_ucmd!() + .args(&["-z", "-n", "-1"]) + .pipe_in("x\0y\0z\0") + .run() + .stdout_is("x\0y\0"); +} + +#[test] +fn test_negative_byte_syntax() { new_ucmd!() .args(&["--bytes=-2"]) .pipe_in("a\n") - .fails() - //GNU Head returns "" - .stderr_is("head: error: invalid byte count \'-2\': invalid digit found in string"); + .run() + .stdout_is(""); } #[test] -#[ignore] -fn test_bug_in_negative_zero_lines() { +fn test_negative_zero_lines() { new_ucmd!() .args(&["--lines=-0"]) .pipe_in("a\nb\n") .succeeds() - //GNU Head returns "a\nb\n" - .stdout_is(""); + .stdout_is("a\nb\n"); +} +#[test] +fn test_negative_zero_bytes() { + new_ucmd!() + .args(&["--bytes=-0"]) + .pipe_in("qwerty") + .succeeds() + .stdout_is("qwerty"); +} +#[test] +fn test_no_such_file_or_directory() { + new_ucmd!() + .arg("no_such_file.toml") + .fails() + .stderr_contains("cannot open 'no_such_file.toml' for reading: No such file or directory"); +} + +/// Test that each non-existent files gets its own error message printed. +#[test] +fn test_multiple_nonexistent_files() { + new_ucmd!() + .args(&["bogusfile1", "bogusfile2"]) + .fails() + .stdout_does_not_contain("==> bogusfile1 <==") + .stderr_contains("cannot open 'bogusfile1' for reading: No such file or directory") + .stdout_does_not_contain("==> bogusfile2 <==") + .stderr_contains("cannot open 'bogusfile2' for reading: No such file or directory"); +} + +// there was a bug not caught by previous tests +// where for negative n > 3, the total amount of lines +// was correct, but it would eat from the second line +#[test] +fn test_sequence_fixture() { + new_ucmd!() + .args(&["-n", "-10", "sequence"]) + .run() + .stdout_is_fixture("sequence.expected"); +} +#[test] +fn test_file_backwards() { + new_ucmd!() + .args(&["-c", "-10", "lorem_ipsum.txt"]) + .run() + .stdout_is_fixture("lorem_ipsum_backwards_file.expected"); +} + +#[test] +fn test_zero_terminated() { + new_ucmd!() + .args(&["-z", "zero_terminated.txt"]) + .run() + .stdout_is_fixture("zero_terminated.expected"); +} + +#[test] +fn test_obsolete_extras() { + new_ucmd!() + .args(&["-5zv"]) + .pipe_in("1\02\03\04\05\06") + .succeeds() + .stdout_is("==> standard input <==\n1\02\03\04\05\0"); +} + +#[test] +fn test_multiple_files() { + new_ucmd!() + .args(&["emptyfile.txt", "emptyfile.txt"]) + .succeeds() + .stdout_is("==> emptyfile.txt <==\n\n==> emptyfile.txt <==\n"); +} + +#[test] +fn test_multiple_files_with_stdin() { + new_ucmd!() + .args(&["emptyfile.txt", "-", "emptyfile.txt"]) + .pipe_in("hello\n") + .succeeds() + .stdout_is( + "==> emptyfile.txt <== + +==> standard input <== +hello + +==> emptyfile.txt <== +", + ); +} + +#[test] +fn test_head_invalid_num() { + new_ucmd!() + .args(&["-c", "1024R", "emptyfile.txt"]) + .fails() + .stderr_is("head: invalid number of bytes: '1024R'"); + new_ucmd!() + .args(&["-n", "1024R", "emptyfile.txt"]) + .fails() + .stderr_is("head: invalid number of lines: '1024R'"); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .args(&["-c", "1Y", "emptyfile.txt"]) + .fails() + .stderr_is("head: invalid number of bytes: '1Y': Value too large for defined data type"); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .args(&["-n", "1Y", "emptyfile.txt"]) + .fails() + .stderr_is("head: invalid number of lines: '1Y': Value too large for defined data type"); + #[cfg(target_pointer_width = "32")] + { + let sizes = ["1000G", "10T"]; + for size in &sizes { + new_ucmd!() + .args(&["-c", size]) + .fails() + .code_is(1) + .stderr_only(format!( + "head: invalid number of bytes: '{}': Value too large for defined data type", + size + )); + } + } +} + +#[test] +fn test_head_num_with_undocumented_sign_bytes() { + // tail: '-' is not documented (8.32 man pages) + // head: '+' is not documented (8.32 man pages) + const ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz"; + new_ucmd!() + .args(&["-c", "5"]) + .pipe_in(ALPHABET) + .succeeds() + .stdout_is("abcde"); + new_ucmd!() + .args(&["-c", "-5"]) + .pipe_in(ALPHABET) + .succeeds() + .stdout_is("abcdefghijklmnopqrstu"); + new_ucmd!() + .args(&["-c", "+5"]) + .pipe_in(ALPHABET) + .succeeds() + .stdout_is("abcde"); } diff --git a/tests/by-util/test_hostid.rs b/tests/by-util/test_hostid.rs index 17aad4aff..3ea818480 100644 --- a/tests/by-util/test_hostid.rs +++ b/tests/by-util/test_hostid.rs @@ -4,10 +4,6 @@ use self::regex::Regex; #[test] fn test_normal() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - - assert!(result.success); let re = Regex::new(r"^[0-9a-f]{8}").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + new_ucmd!().succeeds().stdout_matches(&re); } diff --git a/tests/by-util/test_hostname.rs b/tests/by-util/test_hostname.rs index a526ddd88..3fcb1ae8b 100644 --- a/tests/by-util/test_hostname.rs +++ b/tests/by-util/test_hostname.rs @@ -6,25 +6,25 @@ fn test_hostname() { let ls_short_res = new_ucmd!().arg("-s").succeeds(); let ls_domain_res = new_ucmd!().arg("-d").succeeds(); - assert!(ls_default_res.stdout.len() >= ls_short_res.stdout.len()); - assert!(ls_default_res.stdout.len() >= ls_domain_res.stdout.len()); + assert!(ls_default_res.stdout().len() >= ls_short_res.stdout().len()); + assert!(ls_default_res.stdout().len() >= ls_domain_res.stdout().len()); } // FixME: fails for "MacOS" -#[cfg(not(target_os = "macos"))] +#[cfg(not(target_vendor = "apple"))] #[test] fn test_hostname_ip() { - let result = new_ucmd!().arg("-i").run(); - println!("{:#?}", result); - assert!(result.success); - assert!(!result.stdout.trim().is_empty()); + let result = new_ucmd!().arg("-i").succeeds(); + assert!(!result.stdout_str().trim().is_empty()); } #[test] fn test_hostname_full() { - let result = new_ucmd!().arg("-f").succeeds(); - assert!(!result.stdout.trim().is_empty()); - let ls_short_res = new_ucmd!().arg("-s").succeeds(); - assert!(result.stdout.trim().contains(ls_short_res.stdout.trim())); + assert!(!ls_short_res.stdout_str().trim().is_empty()); + + new_ucmd!() + .arg("-f") + .succeeds() + .stdout_contains(ls_short_res.stdout_str().trim()); } diff --git a/tests/by-util/test_id.rs b/tests/by-util/test_id.rs index 116c73995..bacf57037 100644 --- a/tests/by-util/test_id.rs +++ b/tests/by-util/test_id.rs @@ -1,187 +1,505 @@ use crate::common::util::*; -fn return_whoami_username() -> String { - let scene = TestScenario::new("whoami"); - let result = scene.cmd("whoami").run(); - if is_ci() && result.stderr.contains("cannot find name for user ID") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it - return String::from(""); - } +// spell-checker:ignore (ToDO) coreutil - result.stdout.trim().to_string() +// These tests run the GNU coreutils `(g)id` binary in `$PATH` in order to gather reference values. +// If the `(g)id` in `$PATH` doesn't include a coreutils version string, +// or the version is too low, the test is skipped. + +// The reference version is 8.32. Here 8.30 was chosen because right now there's no +// ubuntu image for github action available with a higher version than 8.30. +const VERSION_MIN: &str = "8.30"; // minimum Version for the reference `id` in $PATH +const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 8.31 +const UUTILS_WARNING: &str = "uutils-tests-warning"; +const UUTILS_INFO: &str = "uutils-tests-info"; + +macro_rules! unwrap_or_return { + ( $e:expr ) => { + match $e { + Ok(x) => x, + Err(e) => { + println!("{}: test skipped: {}", UUTILS_INFO, e); + return; + } + } + }; +} + +fn whoami() -> String { + // Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'. + // + // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)" + // whoami: cannot find name for user ID 1001 + // id --name: cannot find name for user ID 1001 + // id --name: cannot find name for group ID 116 + // + // However, when running "id" from within "/bin/bash" it looks fine: + // id: "uid=1001(runner) gid=118(docker) groups=118(docker),4(adm),101(systemd-journal)" + // whoami: "runner" + + // Use environment variable to get current user instead of + // invoking `whoami` and fall back to user "nobody" on error. + std::env::var("USER").unwrap_or_else(|e| { + println!("{}: {}, using \"nobody\" instead", UUTILS_WARNING, e); + "nobody".to_string() + }) } #[test] -fn test_id() { - let scene = TestScenario::new(util_name!()); +#[cfg(unix)] +fn test_id_no_specified_user() { + let result = new_ucmd!().run(); + let exp_result = unwrap_or_return!(expected_result(&[])); + let mut _exp_stdout = exp_result.stdout_str().to_string(); - let mut result = scene.ucmd().arg("-u").run(); - if result.stderr.contains("cannot find name for user ID") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it - return; + #[cfg(target_os = "linux")] + { + // NOTE: (SELinux NotImplemented) strip 'context' part from exp_stdout: + if let Some(context_offset) = exp_result.stdout_str().find(" context=") { + _exp_stdout.replace_range(context_offset.._exp_stdout.len() - 1, ""); + } } - assert!(result.success); - let uid = String::from(result.stdout.trim()); - result = scene.ucmd().run(); - if is_ci() && result.stderr.contains("cannot find name for user ID") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it - return; - } - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if !result.stderr.contains("Could not find uid") { - // Verify that the id found by --user/-u exists in the list - assert!(result.stdout.contains(&uid)); - } + result + .stdout_is(_exp_stdout) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); } #[test] -fn test_id_from_name() { - let username = return_whoami_username(); - if username == "" { - // Sometimes, the CI is failing here - return; - } +#[cfg(unix)] +fn test_id_single_user() { + let test_users = [&whoami()[..]]; let scene = TestScenario::new(util_name!()); - let result = scene.ucmd().arg(&username).succeeds(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - let uid = String::from(result.stdout.trim()); - let result = scene.ucmd().succeeds(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - // Verify that the id found by --user/-u exists in the list - assert!(result.stdout.contains(&uid)); - // Verify that the username found by whoami exists in the list - assert!(result.stdout.contains(&username)); -} + let mut exp_result = unwrap_or_return!(expected_result(&test_users)); + scene + .ucmd() + .args(&test_users) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); -#[test] -fn test_id_name_from_id() { - let mut scene = TestScenario::new(util_name!()); - let result = scene.ucmd().arg("-u").run(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - let uid = String::from(result.stdout.trim()); - - scene = TestScenario::new(util_name!()); - let result = scene.ucmd().arg("-nu").arg(uid).run(); - if is_ci() && result.stderr.contains("No such user/group") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it - return; - } - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - - let username_id = String::from(result.stdout.trim()); - - scene = TestScenario::new("whoami"); - let result = scene.cmd("whoami").run(); - - let username_whoami = result.stdout.trim(); - - assert_eq!(username_id, username_whoami); -} - -#[test] -fn test_id_group() { - let scene = TestScenario::new(util_name!()); - - let mut result = scene.ucmd().arg("-g").succeeds(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - let s1 = String::from(result.stdout.trim()); - assert!(s1.parse::().is_ok()); - - result = scene.ucmd().arg("--group").succeeds(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - let s1 = String::from(result.stdout.trim()); - assert!(s1.parse::().is_ok()); -} - -#[test] -fn test_id_groups() { - let scene = TestScenario::new(util_name!()); - - let result = scene.ucmd().arg("-G").succeeds(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - let groups = result.stdout.trim().split_whitespace(); - for s in groups { - assert!(s.parse::().is_ok()); - } - - let result = scene.ucmd().arg("--groups").succeeds(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - let groups = result.stdout.trim().split_whitespace(); - for s in groups { - assert!(s.parse::().is_ok()); + // u/g/G z/n + for &opt in &["--user", "--group", "--groups"] { + let mut args = vec![opt]; + args.extend_from_slice(&test_users); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.push("--zero"); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.push("--name"); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.pop(); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); } } #[test] -fn test_id_user() { - let scene = TestScenario::new(util_name!()); +#[cfg(unix)] +fn test_id_single_user_non_existing() { + let args = &["hopefully_non_existing_username"]; + let result = new_ucmd!().args(args).run(); + let exp_result = unwrap_or_return!(expected_result(args)); - let mut result = scene.ucmd().arg("-u").succeeds(); - assert!(result.success); - let s1 = String::from(result.stdout.trim()); - assert!(s1.parse::().is_ok()); - result = scene.ucmd().arg("--user").succeeds(); - assert!(result.success); - let s1 = String::from(result.stdout.trim()); - assert!(s1.parse::().is_ok()); + // It is unknown why on macOS (and possibly others?) `id` adds "Invalid argument". + // coreutils 8.32: $ LC_ALL=C id foobar + // macOS: stderr: "id: 'foobar': no such user: Invalid argument" + // linux: stderr: "id: 'foobar': no such user" + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); } #[test] +#[cfg(unix)] +fn test_id_name() { + let scene = TestScenario::new(util_name!()); + for &opt in &["--user", "--group", "--groups"] { + let args = [opt, "--name"]; + let result = scene.ucmd().args(&args).run(); + let exp_result = unwrap_or_return!(expected_result(&args)); + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); + + if opt == "--user" { + assert_eq!(result.stdout_str().trim_end(), whoami()); + } + } +} + +#[test] +#[cfg(unix)] +fn test_id_real() { + let scene = TestScenario::new(util_name!()); + for &opt in &["--user", "--group", "--groups"] { + let args = [opt, "--real"]; + let result = scene.ucmd().args(&args).run(); + let exp_result = unwrap_or_return!(expected_result(&args)); + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); + } +} + +#[test] +#[cfg(all(unix, not(target_os = "linux")))] fn test_id_pretty_print() { - let username = return_whoami_username(); - if username == "" { - // Sometimes, the CI is failing here - return; - } + // `-p` is BSD only and not supported on GNU's `id` + let username = whoami(); - let scene = TestScenario::new(util_name!()); - let result = scene.ucmd().arg("-p").run(); - if result.stdout.trim() == "" { - // Sometimes, the CI is failing here with - // old rust versions on Linux - return; + let result = new_ucmd!().arg("-p").run(); + if result.stdout_str().trim().is_empty() { + // this fails only on: "MinRustV (ubuntu-latest, feat_os_unix)" + // `rustc 1.40.0 (73528e339 2019-12-16)` + // run: /home/runner/work/coreutils/coreutils/target/debug/coreutils id -p + // thread 'test_id::test_id_pretty_print' panicked at 'Command was expected to succeed. + // stdout = + // stderr = ', tests/common/util.rs:157:13 + println!("test skipped:"); + } else { + result.success().stdout_contains(username); } - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - assert!(result.stdout.contains(&username)); } #[test] +#[cfg(all(unix, not(target_os = "linux")))] fn test_id_password_style() { - let username = return_whoami_username(); - if username == "" { - // Sometimes, the CI is failing here + // `-P` is BSD only and not supported on GNU's `id` + let username = whoami(); + let result = new_ucmd!().arg("-P").arg(&username).succeeds(); + assert!(result.stdout_str().starts_with(&username)); +} + +#[test] +#[cfg(unix)] +fn test_id_multiple_users() { + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(all(unix, not(target_os = "linux")))] + let util_name = &format!("g{}", util_name!()); + let version_check_string = check_coreutil_version(util_name, VERSION_MIN_MULTIPLE_USERS); + if version_check_string.starts_with(UUTILS_WARNING) { + println!("{}\ntest skipped", version_check_string); return; } + + // Same typical users that GNU test suite is using. + let test_users = ["root", "man", "postfix", "sshd", &whoami()]; + let scene = TestScenario::new(util_name!()); + let mut exp_result = unwrap_or_return!(expected_result(&test_users)); + scene + .ucmd() + .args(&test_users) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); - let result = scene.ucmd().arg("-P").succeeds(); - - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - assert!(result.success); - assert!(result.stdout.starts_with(&username)); + // u/g/G z/n + for &opt in &["--user", "--group", "--groups"] { + let mut args = vec![opt]; + args.extend_from_slice(&test_users); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.push("--zero"); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.push("--name"); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.pop(); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + } +} + +#[test] +#[cfg(unix)] +fn test_id_multiple_users_non_existing() { + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(all(unix, not(target_os = "linux")))] + let util_name = &format!("g{}", util_name!()); + let version_check_string = check_coreutil_version(util_name, VERSION_MIN_MULTIPLE_USERS); + if version_check_string.starts_with(UUTILS_WARNING) { + println!("{}\ntest skipped", version_check_string); + return; + } + + let test_users = [ + "root", + "hopefully_non_existing_username1", + &whoami(), + "man", + "hopefully_non_existing_username2", + "hopefully_non_existing_username3", + "postfix", + "sshd", + "hopefully_non_existing_username4", + &whoami(), + ]; + + let scene = TestScenario::new(util_name!()); + let mut exp_result = unwrap_or_return!(expected_result(&test_users)); + scene + .ucmd() + .args(&test_users) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + + // u/g/G z/n + for &opt in &["--user", "--group", "--groups"] { + let mut args = vec![opt]; + args.extend_from_slice(&test_users); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.push("--zero"); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.push("--name"); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + args.pop(); + exp_result = unwrap_or_return!(expected_result(&args)); + scene + .ucmd() + .args(&args) + .run() + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str().replace(": Invalid argument", "")) + .code_is(exp_result.code()); + } +} + +#[test] +#[cfg(unix)] +fn test_id_default_format() { + let scene = TestScenario::new(util_name!()); + for &opt1 in &["--name", "--real"] { + // id: cannot print only names or real IDs in default format + let args = [opt1]; + scene + .ucmd() + .args(&args) + .fails() + .stderr_only(unwrap_or_return!(expected_result(&args)).stderr_str()); + for &opt2 in &["--user", "--group", "--groups"] { + // u/g/G n/r + let args = [opt2, opt1]; + let result = scene.ucmd().args(&args).run(); + let exp_result = unwrap_or_return!(expected_result(&args)); + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); + } + } + for &opt2 in &["--user", "--group", "--groups"] { + // u/g/G + let args = [opt2]; + scene + .ucmd() + .args(&args) + .succeeds() + .stdout_only(unwrap_or_return!(expected_result(&args)).stdout_str()); + } +} + +#[test] +#[cfg(unix)] +fn test_id_zero() { + let scene = TestScenario::new(util_name!()); + for z_flag in &["-z", "--zero"] { + // id: option --zero not permitted in default format + scene + .ucmd() + .args(&[z_flag]) + .fails() + .stderr_only(unwrap_or_return!(expected_result(&[z_flag])).stderr_str()); + for &opt1 in &["--name", "--real"] { + // id: cannot print only names or real IDs in default format + let args = [opt1, z_flag]; + scene + .ucmd() + .args(&args) + .fails() + .stderr_only(unwrap_or_return!(expected_result(&args)).stderr_str()); + for &opt2 in &["--user", "--group", "--groups"] { + // u/g/G n/r z + let args = [opt2, z_flag, opt1]; + let result = scene.ucmd().args(&args).run(); + let exp_result = unwrap_or_return!(expected_result(&args)); + result + .stdout_is(exp_result.stdout_str()) + .stderr_is(exp_result.stderr_str()) + .code_is(exp_result.code()); + } + } + for &opt2 in &["--user", "--group", "--groups"] { + // u/g/G z + let args = [opt2, z_flag]; + scene + .ucmd() + .args(&args) + .succeeds() + .stdout_only(unwrap_or_return!(expected_result(&args)).stdout_str()); + } + } +} + +fn check_coreutil_version(util_name: &str, version_expected: &str) -> String { + // example: + // $ id --version | head -n 1 + // id (GNU coreutils) 8.32.162-4eda + let scene = TestScenario::new(util_name); + let version_check = scene + .cmd_keepenv(&util_name) + .env("LC_ALL", "C") + .arg("--version") + .run(); + version_check + .stdout_str() + .split('\n') + .collect::>() + .get(0) + .map_or_else( + || format!("{}: unexpected output format for reference coreutil: '{} --version'", UUTILS_WARNING, util_name), + |s| { + if s.contains(&format!("(GNU coreutils) {}", version_expected)) { + s.to_string() + } else if s.contains("(GNU coreutils)") { + let version_found = s.split_whitespace().last().unwrap()[..4].parse::().unwrap_or_default(); + let version_expected = version_expected.parse::().unwrap_or_default(); + if version_found > version_expected { + format!("{}: version for the reference coreutil '{}' is higher than expected; expected: {}, found: {}", UUTILS_INFO, util_name, version_expected, version_found) + } else { + format!("{}: version for the reference coreutil '{}' does not match; expected: {}, found: {}", UUTILS_WARNING, util_name, version_expected, version_found) } + } else { + format!("{}: no coreutils version string found for reference coreutils '{} --version'", UUTILS_WARNING, util_name) + } + }, + ) +} + +#[allow(clippy::needless_borrow)] +#[cfg(unix)] +fn expected_result(args: &[&str]) -> Result { + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(all(unix, not(target_os = "linux")))] + let util_name = &format!("g{}", util_name!()); + + let version_check_string = check_coreutil_version(util_name, VERSION_MIN); + if version_check_string.starts_with(UUTILS_WARNING) { + return Err(version_check_string); + } + println!("{}", version_check_string); + + let scene = TestScenario::new(util_name); + let result = scene + .cmd_keepenv(util_name) + .env("LC_ALL", "C") + .args(args) + .run(); + + let (stdout, stderr): (String, String) = if cfg!(target_os = "linux") { + ( + result.stdout_str().to_string(), + result.stderr_str().to_string(), + ) + } else { + // strip 'g' prefix from results: + let from = util_name.to_string() + ":"; + let to = &from[1..]; + ( + result.stdout_str().replace(&from, to), + result.stderr_str().replace(&from, to), + ) + }; + + Ok(CmdResult::new( + Some(result.tmpd()), + Some(result.code()), + result.succeeded(), + stdout.as_bytes(), + stderr.as_bytes(), + )) } diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index a04c0ddfc..ea2c2818e 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -1,25 +1,30 @@ +// spell-checker:ignore (words) helloworld objdump + use crate::common::util::*; +use filetime::FileTime; use rust_users::*; use std::os::unix::fs::PermissionsExt; +#[cfg(not(windows))] +use std::process::Command; +#[cfg(target_os = "linux")] +use std::thread::sleep; #[test] fn test_install_help() { let (_, mut ucmd) = at_and_ucmd!(); - assert!(ucmd - .arg("--help") + ucmd.arg("--help") .succeeds() .no_stderr() - .stdout - .contains("FLAGS:")); + .stdout_contains("FLAGS:"); } #[test] fn test_install_basic() { let (at, mut ucmd) = at_and_ucmd!(); - let dir = "test_install_target_dir_dir_a"; - let file1 = "test_install_target_dir_file_a1"; - let file2 = "test_install_target_dir_file_a2"; + let dir = "target_dir"; + let file1 = "source_file1"; + let file2 = "source_file2"; at.touch(file1); at.touch(file2); @@ -34,7 +39,7 @@ fn test_install_basic() { #[test] fn test_install_twice_dir() { - let dir = "test_install_target_dir_dir_a"; + let dir = "dir"; let scene = TestScenario::new(util_name!()); scene.ucmd().arg("-d").arg(dir).succeeds(); @@ -47,67 +52,124 @@ fn test_install_twice_dir() { #[test] fn test_install_failing_not_dir() { let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; - let file2 = "test_install_target_dir_file_a2"; - let file3 = "test_install_target_dir_file_a3"; + let file1 = "file1"; + let file2 = "file2"; + let file3 = "file3"; at.touch(file1); at.touch(file2); at.touch(file3); - assert!(ucmd - .arg(file1) + ucmd.arg(file1) .arg(file2) .arg(file3) .fails() - .stderr - .contains("not a directory")); + .stderr_contains("not a directory"); } #[test] fn test_install_unimplemented_arg() { let (at, mut ucmd) = at_and_ucmd!(); - let dir = "test_install_target_dir_dir_b"; - let file = "test_install_target_dir_file_b"; + let dir = "target_dir"; + let file = "source_file"; let context_arg = "--context"; at.touch(file); at.mkdir(dir); - assert!(ucmd - .arg(context_arg) + ucmd.arg(context_arg) .arg(file) .arg(dir) .fails() - .stderr - .contains("Unimplemented")); + .stderr_contains("Unimplemented"); assert!(!at.file_exists(&format!("{}/{}", dir, file))); } #[test] -fn test_install_component_directories() { +fn test_install_ancestors_directories() { let (at, mut ucmd) = at_and_ucmd!(); - let component1 = "test_install_target_dir_component_c1"; - let component2 = "test_install_target_dir_component_c2"; - let component3 = "test_install_target_dir_component_c3"; + let ancestor1 = "ancestor1"; + let ancestor2 = "ancestor1/ancestor2"; + let target_dir = "ancestor1/ancestor2/target_dir"; let directories_arg = "-d"; - ucmd.args(&[directories_arg, component1, component2, component3]) + ucmd.args(&[directories_arg, target_dir]) .succeeds() .no_stderr(); - assert!(at.dir_exists(component1)); - assert!(at.dir_exists(component2)); - assert!(at.dir_exists(component3)); + assert!(at.dir_exists(ancestor1)); + assert!(at.dir_exists(ancestor2)); + assert!(at.dir_exists(target_dir)); +} + +#[test] +fn test_install_ancestors_mode_directories() { + let (at, mut ucmd) = at_and_ucmd!(); + let ancestor1 = "ancestor1"; + let ancestor2 = "ancestor1/ancestor2"; + let target_dir = "ancestor1/ancestor2/target_dir"; + let directories_arg = "-d"; + let mode_arg = "--mode=700"; + + ucmd.args(&[mode_arg, directories_arg, target_dir]) + .succeeds() + .no_stderr(); + + assert!(at.dir_exists(ancestor1)); + assert!(at.dir_exists(ancestor2)); + assert!(at.dir_exists(target_dir)); + + assert_ne!(0o40_700_u32, at.metadata(ancestor1).permissions().mode()); + assert_ne!(0o40_700_u32, at.metadata(ancestor2).permissions().mode()); + + // Expected mode only on the target_dir. + assert_eq!(0o40_700_u32, at.metadata(target_dir).permissions().mode()); +} + +#[test] +fn test_install_parent_directories() { + let (at, mut ucmd) = at_and_ucmd!(); + let ancestor1 = "ancestor1"; + let ancestor2 = "ancestor1/ancestor2"; + let target_dir = "ancestor1/ancestor2/target_dir"; + let directories_arg = "-d"; + + // Here one of the ancestors already exist and only the target_dir and + // its parent must be created. + at.mkdir(ancestor1); + + ucmd.args(&[directories_arg, target_dir]) + .succeeds() + .no_stderr(); + + assert!(at.dir_exists(ancestor2)); + assert!(at.dir_exists(target_dir)); +} + +#[test] +fn test_install_several_directories() { + let (at, mut ucmd) = at_and_ucmd!(); + let dir1 = "dir1"; + let dir2 = "dir2"; + let dir3 = "dir3"; + let directories_arg = "-d"; + + ucmd.args(&[directories_arg, dir1, dir2, dir3]) + .succeeds() + .no_stderr(); + + assert!(at.dir_exists(dir1)); + assert!(at.dir_exists(dir2)); + assert!(at.dir_exists(dir3)); } #[test] fn test_install_mode_numeric() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; - let dir = "test_install_target_dir_dir_e"; - let dir2 = "test_install_target_dir_dir_e2"; + let dir = "dir1"; + let dir2 = "dir2"; - let file = "test_install_target_dir_file_e"; + let file = "file"; let mode_arg = "--mode=333"; at.touch(file); @@ -124,29 +186,25 @@ fn test_install_mode_numeric() { assert!(at.file_exists(file)); assert!(at.file_exists(dest_file)); let permissions = at.metadata(dest_file).permissions(); - assert_eq!(0o100333 as u32, PermissionsExt::mode(&permissions)); + assert_eq!(0o100_333_u32, PermissionsExt::mode(&permissions)); let mode_arg = "-m 0333"; at.mkdir(dir2); - let result = scene.ucmd().arg(mode_arg).arg(file).arg(dir2).run(); + scene.ucmd().arg(mode_arg).arg(file).arg(dir2).succeeds(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - - assert!(result.success); let dest_file = &format!("{}/{}", dir2, file); assert!(at.file_exists(file)); assert!(at.file_exists(dest_file)); let permissions = at.metadata(dest_file).permissions(); - assert_eq!(0o100333 as u32, PermissionsExt::mode(&permissions)); + assert_eq!(0o100_333_u32, PermissionsExt::mode(&permissions)); } #[test] fn test_install_mode_symbolic() { let (at, mut ucmd) = at_and_ucmd!(); - let dir = "test_install_target_dir_dir_f"; - let file = "test_install_target_dir_file_f"; + let dir = "target_dir"; + let file = "source_file"; let mode_arg = "--mode=o+wx"; at.touch(file); @@ -157,25 +215,23 @@ fn test_install_mode_symbolic() { assert!(at.file_exists(file)); assert!(at.file_exists(dest_file)); let permissions = at.metadata(dest_file).permissions(); - assert_eq!(0o100003 as u32, PermissionsExt::mode(&permissions)); + assert_eq!(0o100_003_u32, PermissionsExt::mode(&permissions)); } #[test] fn test_install_mode_failing() { let (at, mut ucmd) = at_and_ucmd!(); - let dir = "test_install_target_dir_dir_g"; - let file = "test_install_target_dir_file_g"; + let dir = "target_dir"; + let file = "source_file"; let mode_arg = "--mode=999"; at.touch(file); at.mkdir(dir); - assert!(ucmd - .arg(file) + ucmd.arg(file) .arg(dir) .arg(mode_arg) .fails() - .stderr - .contains("Invalid mode string: invalid digit found in string")); + .stderr_contains("Invalid mode string: invalid digit found in string"); let dest_file = &format!("{}/{}", dir, file); assert!(at.file_exists(file)); @@ -185,7 +241,7 @@ fn test_install_mode_failing() { #[test] fn test_install_mode_directories() { let (at, mut ucmd) = at_and_ucmd!(); - let component = "test_install_target_dir_component_h"; + let component = "component"; let directories_arg = "-d"; let mode_arg = "--mode=333"; @@ -197,14 +253,14 @@ fn test_install_mode_directories() { assert!(at.dir_exists(component)); let permissions = at.metadata(component).permissions(); - assert_eq!(0o040333 as u32, PermissionsExt::mode(&permissions)); + assert_eq!(0o040_333_u32, PermissionsExt::mode(&permissions)); } #[test] fn test_install_target_file() { let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_file_file_i1"; - let file2 = "test_install_target_file_file_i2"; + let file1 = "source_file"; + let file2 = "target_file"; at.touch(file1); at.touch(file2); @@ -217,8 +273,8 @@ fn test_install_target_file() { #[test] fn test_install_target_new_file() { let (at, mut ucmd) = at_and_ucmd!(); - let file = "test_install_target_new_filer_file_j"; - let dir = "test_install_target_new_file_dir_j"; + let file = "file"; + let dir = "target_dir"; at.touch(file); at.mkdir(dir); @@ -234,8 +290,8 @@ fn test_install_target_new_file() { #[test] fn test_install_target_new_file_with_group() { let (at, mut ucmd) = at_and_ucmd!(); - let file = "test_install_target_new_filer_file_j"; - let dir = "test_install_target_new_file_dir_j"; + let file = "file"; + let dir = "target_dir"; let gid = get_effective_gid(); at.touch(file); @@ -247,16 +303,13 @@ fn test_install_target_new_file_with_group() { .arg(format!("{}/{}", dir, file)) .run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - - if is_ci() && result.stderr.contains("error: no such group:") { + if is_ci() && result.stderr_str().contains("no such group:") { // In the CI, some server are failing to return the group. // As seems to be a configuration issue, ignoring it return; } - assert!(result.success); + result.success(); assert!(at.file_exists(file)); assert!(at.file_exists(&format!("{}/{}", dir, file))); } @@ -264,8 +317,8 @@ fn test_install_target_new_file_with_group() { #[test] fn test_install_target_new_file_with_owner() { let (at, mut ucmd) = at_and_ucmd!(); - let file = "test_install_target_new_filer_file_j"; - let dir = "test_install_target_new_file_dir_j"; + let file = "file"; + let dir = "target_dir"; let uid = get_effective_uid(); at.touch(file); @@ -277,16 +330,13 @@ fn test_install_target_new_file_with_owner() { .arg(format!("{}/{}", dir, file)) .run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - - if is_ci() && result.stderr.contains("error: no such user:") { + if is_ci() && result.stderr_str().contains("no such user:") { // In the CI, some server are failing to return the user id. // As seems to be a configuration issue, ignoring it return; } - assert!(result.success); + result.success(); assert!(at.file_exists(file)); assert!(at.file_exists(&format!("{}/{}", dir, file))); } @@ -294,27 +344,49 @@ fn test_install_target_new_file_with_owner() { #[test] fn test_install_target_new_file_failing_nonexistent_parent() { let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_new_file_failing_file_k1"; - let file2 = "test_install_target_new_file_failing_file_k2"; - let dir = "test_install_target_new_file_failing_dir_k"; + let file1 = "source_file"; + let file2 = "target_file"; + let dir = "target_dir"; at.touch(file1); - let err = ucmd - .arg(file1) + ucmd.arg(file1) .arg(format!("{}/{}", dir, file2)) .fails() - .stderr; + .stderr_contains(&"No such file or directory"); +} - assert!(err.contains("not a directory")) +#[test] +fn test_install_preserve_timestamps() { + let (at, mut ucmd) = at_and_ucmd!(); + let file1 = "source_file"; + let file2 = "target_file"; + at.touch(file1); + + ucmd.arg(file1).arg(file2).arg("-p").succeeds().no_stderr(); + + assert!(at.file_exists(file1)); + assert!(at.file_exists(file2)); + + let file1_metadata = at.metadata(file1); + let file2_metadata = at.metadata(file2); + + assert_eq!( + file1_metadata.accessed().ok(), + file2_metadata.accessed().ok() + ); + assert_eq!( + file1_metadata.modified().ok(), + file2_metadata.modified().ok() + ); } // These two tests are failing but should work #[test] fn test_install_copy_file() { let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "test_install_target_dir_file_a1"; - let file2 = "test_install_target_dir_file_a2"; + let file1 = "source_file"; + let file2 = "target_file"; at.touch(file1); ucmd.arg(file1).arg(file2).succeeds().no_stderr(); @@ -327,9 +399,300 @@ fn test_install_copy_file() { #[cfg(target_os = "linux")] fn test_install_target_file_dev_null() { let (at, mut ucmd) = at_and_ucmd!(); - let file1 = "/dev/null"; - let file2 = "test_install_target_file_file_i2"; - ucmd.arg(file1).arg(file2).succeeds().no_stderr(); + let file1 = "/dev/null"; + let file2 = "target_file"; + + ucmd.arg(file1).arg(file2).succeeds(); + assert!(at.file_exists(file2)); } + +#[test] +fn test_install_nested_paths_copy_file() { + let (at, mut ucmd) = at_and_ucmd!(); + let file1 = "source_file"; + let dir1 = "source_dir"; + let dir2 = "target_dir"; + + at.mkdir(dir1); + at.mkdir(dir2); + at.touch(&format!("{}/{}", dir1, file1)); + + ucmd.arg(format!("{}/{}", dir1, file1)) + .arg(dir2) + .succeeds() + .no_stderr(); + assert!(at.file_exists(&format!("{}/{}", dir2, file1))); +} + +#[test] +fn test_install_failing_omitting_directory() { + let (at, mut ucmd) = at_and_ucmd!(); + let file1 = "source_file"; + let dir1 = "source_dir"; + let dir2 = "target_dir"; + + at.mkdir(dir1); + at.mkdir(dir2); + at.touch(file1); + + ucmd.arg(dir1) + .arg(file1) + .arg(dir2) + .fails() + .code_is(1) + .stderr_contains("omitting directory"); +} + +#[test] +fn test_install_failing_no_such_file() { + let (at, mut ucmd) = at_and_ucmd!(); + let file1 = "source_file"; + let file2 = "inexistent_file"; + let dir1 = "target_dir"; + + at.mkdir(dir1); + at.touch(file1); + + ucmd.arg(file1) + .arg(file2) + .arg(dir1) + .fails() + .code_is(1) + .stderr_contains("No such file or directory"); +} + +#[test] +fn test_install_copy_then_compare_file() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let file1 = "test_install_copy_then_compare_file_a1"; + let file2 = "test_install_copy_then_compare_file_a2"; + + at.touch(file1); + scene + .ucmd() + .arg("-C") + .arg(file1) + .arg(file2) + .succeeds() + .no_stderr(); + + let mut file2_meta = at.metadata(file2); + let before = FileTime::from_last_modification_time(&file2_meta); + + scene + .ucmd() + .arg("-C") + .arg(file1) + .arg(file2) + .succeeds() + .no_stderr(); + + file2_meta = at.metadata(file2); + let after = FileTime::from_last_modification_time(&file2_meta); + + assert!(before == after); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_install_copy_then_compare_file_with_extra_mode() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + // XXX: can't tests introspect on their own names? + let file1 = "test_install_copy_then_compare_file_with_extra_mode_a1"; + let file2 = "test_install_copy_then_compare_file_with_extra_mode_a2"; + + at.touch(file1); + scene + .ucmd() + .arg("-C") + .arg(file1) + .arg(file2) + .succeeds() + .no_stderr(); + + let mut file2_meta = at.metadata(file2); + let before = FileTime::from_last_modification_time(&file2_meta); + sleep(std::time::Duration::from_millis(1000)); + + scene + .ucmd() + .arg("-C") + .arg(file1) + .arg(file2) + .arg("-m") + .arg("1644") + .succeeds() + .no_stderr(); + + file2_meta = at.metadata(file2); + let after_install_sticky = FileTime::from_last_modification_time(&file2_meta); + + assert!(before != after_install_sticky); + + sleep(std::time::Duration::from_millis(1000)); + + // dest file still 1644, so need_copy ought to return `true` + scene + .ucmd() + .arg("-C") + .arg(file1) + .arg(file2) + .succeeds() + .no_stderr(); + + file2_meta = at.metadata(file2); + let after_install_sticky_again = FileTime::from_last_modification_time(&file2_meta); + + assert!(after_install_sticky != after_install_sticky_again); +} + +const STRIP_TARGET_FILE: &str = "helloworld_installed"; +const SYMBOL_DUMP_PROGRAM: &str = "objdump"; +const STRIP_SOURCE_FILE_SYMBOL: &str = "main"; + +fn strip_source_file() -> &'static str { + if cfg!(target_os = "macos") { + "helloworld_macos" + } else { + "helloworld_linux" + } +} + +#[test] +#[cfg(not(windows))] +fn test_install_and_strip() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + scene + .ucmd() + .arg("-s") + .arg(strip_source_file()) + .arg(STRIP_TARGET_FILE) + .succeeds() + .no_stderr(); + + let output = Command::new(SYMBOL_DUMP_PROGRAM) + .arg("-t") + .arg(at.plus(STRIP_TARGET_FILE)) + .output() + .unwrap(); + + let stdout = String::from_utf8(output.stdout).unwrap(); + assert!(!stdout.contains(STRIP_SOURCE_FILE_SYMBOL)); +} + +#[test] +#[cfg(not(windows))] +fn test_install_and_strip_with_program() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + scene + .ucmd() + .arg("-s") + .arg("--strip-program") + .arg("/usr/bin/strip") + .arg(strip_source_file()) + .arg(STRIP_TARGET_FILE) + .succeeds() + .no_stderr(); + + let output = Command::new(SYMBOL_DUMP_PROGRAM) + .arg("-t") + .arg(at.plus(STRIP_TARGET_FILE)) + .output() + .unwrap(); + + let stdout = String::from_utf8(output.stdout).unwrap(); + assert!(!stdout.contains(STRIP_SOURCE_FILE_SYMBOL)); +} + +#[test] +#[cfg(not(windows))] +fn test_install_and_strip_with_invalid_program() { + new_ucmd!() + .arg("-s") + .arg("--strip-program") + .arg("/bin/date") + .arg(strip_source_file()) + .arg(STRIP_TARGET_FILE) + .fails() + .stderr_contains("strip program failed"); +} + +#[test] +#[cfg(not(windows))] +fn test_install_and_strip_with_non_existent_program() { + new_ucmd!() + .arg("-s") + .arg("--strip-program") + .arg("/usr/bin/non_existent_program") + .arg(strip_source_file()) + .arg(STRIP_TARGET_FILE) + .fails() + .stderr_contains("No such file or directory"); +} + +#[test] +fn test_install_creating_leading_dirs() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let source = "create_leading_test_file"; + let target = "dir1/dir2/dir3/test_file"; + + at.touch(source); + + scene + .ucmd() + .arg("-D") + .arg(source) + .arg(at.plus(target)) + .succeeds() + .no_stderr(); +} + +#[test] +#[cfg(not(windows))] +fn test_install_creating_leading_dir_fails_on_long_name() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let source = "create_leading_test_file"; + let target = format!("{}/test_file", "d".repeat(libc::PATH_MAX as usize + 1)); + + at.touch(source); + + scene + .ucmd() + .arg("-D") + .arg(source) + .arg(at.plus(target.as_str())) + .fails() + .stderr_contains("failed to create"); +} + +#[test] +fn test_install_dir() { + let (at, mut ucmd) = at_and_ucmd!(); + let dir = "target_dir"; + let file1 = "source_file1"; + let file2 = "source_file2"; + + at.touch(file1); + at.touch(file2); + at.mkdir(dir); + ucmd.arg(file1) + .arg(file2) + .arg(&format!("--target-directory={}", dir)) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file1)); + assert!(at.file_exists(file2)); + assert!(at.file_exists(&format!("{}/{}", dir, file1))); + assert!(at.file_exists(&format!("{}/{}", dir, file2))); +} diff --git a/tests/by-util/test_join.rs b/tests/by-util/test_join.rs index b0311df84..8dbdadba7 100644 --- a/tests/by-util/test_join.rs +++ b/tests/by-util/test_join.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (words) autoformat + use crate::common::util::*; #[test] @@ -141,14 +143,14 @@ fn new_line_separated() { } #[test] -fn multitab_character() { +fn tab_multi_character() { new_ucmd!() .arg("semicolon_fields_1.txt") .arg("semicolon_fields_2.txt") .arg("-t") .arg("э") .fails() - .stderr_is("join: error: multi-character tab э"); + .stderr_is("join: multi-character tab э"); } #[test] @@ -211,7 +213,7 @@ fn empty_format() { .arg("-o") .arg("") .fails() - .stderr_is("join: error: invalid file number in field spec: ''"); + .stderr_is("join: invalid file number in field spec: ''"); } #[test] diff --git a/tests/by-util/test_kill.rs b/tests/by-util/test_kill.rs index 651491045..fe5d4557a 100644 --- a/tests/by-util/test_kill.rs +++ b/tests/by-util/test_kill.rs @@ -1 +1,127 @@ -// ToDO: add tests +use crate::common::util::*; +use regex::Regex; +use std::os::unix::process::ExitStatusExt; +use std::process::{Child, Command}; + +// A child process the tests will try to kill. +struct Target { + child: Child, + killed: bool, +} + +impl Target { + // Creates a target that will naturally die after some time if not killed + // fast enough. + // This timeout avoids hanging failing tests. + fn new() -> Target { + Target { + child: Command::new("sleep") + .arg("30") + .spawn() + .expect("cannot spawn target"), + killed: false, + } + } + + // Waits for the target to complete and returns the signal it received if any. + fn wait_for_signal(&mut self) -> Option { + let sig = self.child.wait().expect("cannot wait on target").signal(); + self.killed = true; + sig + } + + fn pid(&self) -> u32 { + self.child.id() + } +} + +impl Drop for Target { + // Terminates this target to avoid littering test boxes with zombi processes + // when a test fails after creating a target but before killing it. + fn drop(&mut self) { + if !self.killed { + self.child.kill().expect("cannot kill target"); + } + } +} + +#[test] +fn test_kill_list_all_signals() { + // Check for a few signals. Do not try to be comprehensive. + new_ucmd!() + .arg("-l") + .succeeds() + .stdout_contains("KILL") + .stdout_contains("TERM") + .stdout_contains("HUP"); +} + +#[test] +fn test_kill_list_all_signals_as_table() { + // Check for a few signals. Do not try to be comprehensive. + new_ucmd!() + .arg("-t") + .succeeds() + .stdout_contains("KILL") + .stdout_contains("TERM") + .stdout_contains("HUP"); +} + +#[test] +fn test_kill_list_one_signal_from_name() { + // Use SIGKILL because it is 9 on all unixes. + new_ucmd!() + .arg("-l") + .arg("KILL") + .succeeds() + .stdout_matches(&Regex::new("\\b9\\b").unwrap()); +} + +#[test] +fn test_kill_set_bad_signal_name() { + // spell-checker:disable-line + new_ucmd!() + .arg("-s") + .arg("IAMNOTASIGNAL") // spell-checker:disable-line + .fails() + .stderr_contains("unknown signal"); +} + +#[test] +fn test_kill_with_default_signal() { + let mut target = Target::new(); + new_ucmd!().arg(format!("{}", target.pid())).succeeds(); + assert_eq!(target.wait_for_signal(), Some(libc::SIGTERM)); +} + +#[test] +fn test_kill_with_signal_number_old_form() { + let mut target = Target::new(); + new_ucmd!() + .arg("-9") + .arg(format!("{}", target.pid())) + .succeeds(); + assert_eq!(target.wait_for_signal(), Some(9)); +} + +#[test] +fn test_kill_with_signal_number_new_form() { + let mut target = Target::new(); + new_ucmd!() + .arg("-s") + .arg("9") + .arg(format!("{}", target.pid())) + .succeeds(); + assert_eq!(target.wait_for_signal(), Some(9)); +} + +#[test] +fn test_kill_with_signal_name_new_form() { + let mut target = Target::new(); + new_ucmd!() + .arg("-s") + .arg("KILL") + .arg(format!("{}", target.pid())) + .succeeds(); + assert_eq!(target.wait_for_signal(), Some(libc::SIGKILL)); +} diff --git a/tests/by-util/test_link.rs b/tests/by-util/test_link.rs index 381ea168a..6ac3f35cc 100644 --- a/tests/by-util/test_link.rs +++ b/tests/by-util/test_link.rs @@ -23,7 +23,7 @@ fn test_link_no_circular() { ucmd.args(&[link, link]) .fails() - .stderr_is("link: error: No such file or directory (os error 2)\n"); + .stderr_is("link: No such file or directory (os error 2)\n"); assert!(!at.file_exists(link)); } @@ -35,7 +35,29 @@ fn test_link_nonexistent_file() { ucmd.args(&[file, link]) .fails() - .stderr_is("link: error: No such file or directory (os error 2)\n"); + .stderr_is("link: No such file or directory (os error 2)\n"); assert!(!at.file_exists(file)); assert!(!at.file_exists(link)); } + +#[test] +fn test_link_one_argument() { + let (_, mut ucmd) = at_and_ucmd!(); + let file = "test_link_argument"; + ucmd.args(&[file]).fails().stderr_contains( + "error: The argument '...' requires at least 2 values, but only 1 was provide", + ); +} + +#[test] +fn test_link_three_arguments() { + let (_, mut ucmd) = at_and_ucmd!(); + let arguments = vec![ + "test_link_argument1", + "test_link_argument2", + "test_link_argument3", + ]; + ucmd.args(&arguments[..]).fails().stderr_contains( + format!("error: The value '{}' was provided to '...', but it wasn't expecting any more values", arguments[2]), + ); +} diff --git a/tests/by-util/test_ln.rs b/tests/by-util/test_ln.rs index 89261036d..9fa73c0bc 100644 --- a/tests/by-util/test_ln.rs +++ b/tests/by-util/test_ln.rs @@ -65,10 +65,10 @@ fn test_symlink_circular() { } #[test] -fn test_symlink_dont_overwrite() { +fn test_symlink_do_not_overwrite() { let (at, mut ucmd) = at_and_ucmd!(); - let file = "test_symlink_dont_overwrite"; - let link = "test_symlink_dont_overwrite_link"; + let file = "test_symlink_do_not_overwrite"; + let link = "test_symlink_do_not_overwrite_link"; at.touch(file); at.touch(link); @@ -120,7 +120,7 @@ fn test_symlink_interactive() { scene .ucmd() .args(&["-i", "-s", file, link]) - .pipe_in("Yesh") + .pipe_in("Yesh") // spell-checker:disable-line .succeeds() .no_stderr(); @@ -299,13 +299,11 @@ fn test_symlink_overwrite_dir_fail() { at.touch(path_a); at.mkdir(path_b); - assert!( - ucmd.args(&["-s", "-T", path_a, path_b]) - .fails() - .stderr - .len() - > 0 - ); + assert!(!ucmd + .args(&["-s", "-T", path_a, path_b]) + .fails() + .stderr_str() + .is_empty()); } #[test] @@ -358,7 +356,11 @@ fn test_symlink_target_only() { at.mkdir(dir); - assert!(ucmd.args(&["-s", "-t", dir]).fails().stderr.len() > 0); + assert!(!ucmd + .args(&["-s", "-t", dir]) + .fails() + .stderr_str() + .is_empty()); } #[test] @@ -407,7 +409,7 @@ fn test_symlink_missing_destination() { at.touch(file); ucmd.args(&["-s", "-T", file]).fails().stderr_is(format!( - "ln: error: missing destination file operand after '{}'", + "ln: missing destination file operand after '{}'", file )); } @@ -426,20 +428,6 @@ fn test_symlink_relative() { assert_eq!(at.resolve_link(link), file_a); } -#[test] -fn test_hardlink_relative() { - let (at, mut ucmd) = at_and_ucmd!(); - let file_a = "test_hardlink_relative_a"; - let link = "test_hardlink_relative_link"; - - at.touch(file_a); - - // relative hardlink - ucmd.args(&["-r", "-v", file_a, link]) - .succeeds() - .stdout_only(format!("'{}' -> '{}'\n", link, file_a)); -} - #[test] fn test_symlink_relative_path() { let (at, mut ucmd) = at_and_ucmd!(); @@ -520,10 +508,7 @@ fn test_symlink_no_deref_dir() { scene.ucmd().args(&["-sn", dir1, link]).fails(); // Try with the no-deref - let result = scene.ucmd().args(&["-sfn", dir1, link]).run(); - println!("stdout {}", result.stdout); - println!("stderr {}", result.stderr); - assert!(result.success); + scene.ucmd().args(&["-sfn", dir1, link]).succeeds(); assert!(at.dir_exists(dir1)); assert!(at.dir_exists(dir2)); assert!(at.is_symlink(link)); @@ -566,12 +551,40 @@ fn test_symlink_no_deref_file() { scene.ucmd().args(&["-sn", file1, link]).fails(); // Try with the no-deref - let result = scene.ucmd().args(&["-sfn", file1, link]).run(); - println!("stdout {}", result.stdout); - println!("stderr {}", result.stderr); - assert!(result.success); + scene.ucmd().args(&["-sfn", file1, link]).succeeds(); assert!(at.file_exists(file1)); assert!(at.file_exists(file2)); assert!(at.is_symlink(link)); assert_eq!(at.resolve_link(link), file1); } + +#[test] +fn test_relative_requires_symbolic() { + new_ucmd!().args(&["-r", "foo", "bar"]).fails(); +} + +#[test] +fn test_relative_dst_already_symlink() { + let (at, mut ucmd) = at_and_ucmd!(); + at.touch("file1"); + at.symlink_file("file1", "file2"); + ucmd.arg("-srf").arg("file1").arg("file2").succeeds(); + at.is_symlink("file2"); +} + +#[test] +fn test_relative_src_already_symlink() { + let (at, mut ucmd) = at_and_ucmd!(); + at.touch("file1"); + at.symlink_file("file1", "file2"); + ucmd.arg("-sr").arg("file2").arg("file3").succeeds(); + assert!(at.resolve_link("file3").ends_with("file1")); +} + +#[test] +fn test_relative_recursive() { + let (at, mut ucmd) = at_and_ucmd!(); + at.mkdir("dir"); + ucmd.args(&["-sr", "dir", "dir/recursive"]).succeeds(); + assert_eq!(at.resolve_link("dir/recursive"), "."); +} diff --git a/tests/by-util/test_logname.rs b/tests/by-util/test_logname.rs index b15941c06..0e8125191 100644 --- a/tests/by-util/test_logname.rs +++ b/tests/by-util/test_logname.rs @@ -3,23 +3,19 @@ use std::env; #[test] fn test_normal() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.run(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); + let result = new_ucmd!().run(); println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok()); for (key, value) in env::vars() { println!("{}: {}", key, value); } - if (is_ci() || is_wsl()) && result.stderr.contains("error: no login name") { + if (is_ci() || uucore::os::is_wsl_1()) && result.stderr_str().contains("no login name") { // ToDO: investigate WSL failure // In the CI, some server are failing to return logname. // As seems to be a configuration issue, ignoring it return; } - assert!(result.success); - assert!(!result.stdout.trim().is_empty()); + result.success(); + assert!(!result.stdout_str().trim().is_empty()); } diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 422db8df9..44d14c304 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -1,8 +1,14 @@ +// spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup somefile somegroup somehiddenbackup somehiddenfile + +#[cfg(unix)] +extern crate unix_socket; use crate::common::util::*; extern crate regex; use self::regex::Regex; +use std::collections::HashMap; +use std::path::Path; use std::thread::sleep; use std::time::Duration; @@ -11,7 +17,11 @@ extern crate libc; #[cfg(not(windows))] use self::libc::umask; #[cfg(not(windows))] +use std::path::PathBuf; +#[cfg(not(windows))] use std::sync::Mutex; +#[cfg(not(windows))] +extern crate tempfile; #[cfg(not(windows))] lazy_static! { @@ -34,27 +44,235 @@ fn test_ls_a() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; at.touch(".test-1"); + at.mkdir("some-dir"); + at.touch( + Path::new("some-dir") + .join(".test-2") + .as_os_str() + .to_str() + .unwrap(), + ); - let result = scene.ucmd().run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(!result.stdout.contains(".test-1")); - assert!(!result.stdout.contains("..")); + #[allow(clippy::trivial_regex)] + let re_pwd = Regex::new(r"^\.\n").unwrap(); - let result = scene.ucmd().arg("-a").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(result.stdout.contains(".test-1")); - assert!(result.stdout.contains("..")); + // Using the present working directory + scene + .ucmd() + .arg("-1") + .succeeds() + .stdout_does_not_contain(".test-1") + .stdout_does_not_contain("..") + .stdout_does_not_match(&re_pwd); - let result = scene.ucmd().arg("-A").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(result.stdout.contains(".test-1")); - assert!(!result.stdout.contains("..")); + scene + .ucmd() + .arg("-a") + .arg("-1") + .succeeds() + .stdout_contains(&".test-1") + .stdout_contains(&"..") + .stdout_matches(&re_pwd); + + scene + .ucmd() + .arg("-A") + .arg("-1") + .succeeds() + .stdout_contains(".test-1") + .stdout_does_not_contain("..") + .stdout_does_not_match(&re_pwd); + + // Using a subdirectory + scene + .ucmd() + .arg("-1") + .arg("some-dir") + .succeeds() + .stdout_does_not_contain(".test-2") + .stdout_does_not_contain("..") + .stdout_does_not_match(&re_pwd); + + scene + .ucmd() + .arg("-a") + .arg("-1") + .arg("some-dir") + .succeeds() + .stdout_contains(&".test-2") + .stdout_contains(&"..") + .no_stderr() + .stdout_matches(&re_pwd); + + scene + .ucmd() + .arg("-A") + .arg("-1") + .arg("some-dir") + .succeeds() + .stdout_contains(".test-2") + .stdout_does_not_contain("..") + .stdout_does_not_match(&re_pwd); +} + +#[test] +fn test_ls_width() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch(&at.plus_as_string("test-width-1")); + at.touch(&at.plus_as_string("test-width-2")); + at.touch(&at.plus_as_string("test-width-3")); + at.touch(&at.plus_as_string("test-width-4")); + + for option in &["-w 100", "-w=100", "--width=100", "--width 100"] { + scene + .ucmd() + .args(&option.split(' ').collect::>()) + .succeeds() + .stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n"); + } + + for option in &["-w 50", "-w=50", "--width=50", "--width 50"] { + scene + .ucmd() + .args(&option.split(' ').collect::>()) + .succeeds() + .stdout_only("test-width-1 test-width-3\ntest-width-2 test-width-4\n"); + } + + for option in &[ + "-w 25", + "-w=25", + "--width=25", + "--width 25", + "-w 0", + "-w=0", + "--width=0", + "--width 0", + ] { + scene + .ucmd() + .args(&option.split(' ').collect::>()) + .succeeds() + .stdout_only("test-width-1\ntest-width-2\ntest-width-3\ntest-width-4\n"); + } + + scene + .ucmd() + .arg("-w=bad") + .fails() + .stderr_contains("invalid line width"); + + for option in &["-w 1a", "-w=1a", "--width=1a", "--width 1a"] { + scene + .ucmd() + .args(&option.split(' ').collect::>()) + .fails() + .stderr_only("ls: invalid line width: '1a'"); + } +} + +#[test] +fn test_ls_columns() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch(&at.plus_as_string("test-columns-1")); + at.touch(&at.plus_as_string("test-columns-2")); + at.touch(&at.plus_as_string("test-columns-3")); + at.touch(&at.plus_as_string("test-columns-4")); + + // Columns is the default + let result = scene.ucmd().succeeds(); + + #[cfg(not(windows))] + result.stdout_only("test-columns-1\ntest-columns-2\ntest-columns-3\ntest-columns-4\n"); + #[cfg(windows)] + result.stdout_only("test-columns-1 test-columns-2 test-columns-3 test-columns-4\n"); + + for option in &["-C", "--format=columns"] { + let result = scene.ucmd().arg(option).succeeds(); + #[cfg(not(windows))] + result.stdout_only("test-columns-1\ntest-columns-2\ntest-columns-3\ntest-columns-4\n"); + #[cfg(windows)] + result.stdout_only("test-columns-1 test-columns-2 test-columns-3 test-columns-4\n"); + } + + for option in &["-C", "--format=columns"] { + scene + .ucmd() + .arg("-w=40") + .arg(option) + .succeeds() + .stdout_only("test-columns-1 test-columns-3\ntest-columns-2 test-columns-4\n"); + } +} + +#[test] +fn test_ls_across() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch(&at.plus_as_string("test-across-1")); + at.touch(&at.plus_as_string("test-across-2")); + at.touch(&at.plus_as_string("test-across-3")); + at.touch(&at.plus_as_string("test-across-4")); + + for option in &["-x", "--format=across"] { + let result = scene.ucmd().arg(option).succeeds(); + // Because the test terminal has width 0, this is the same output as + // the columns option. + if cfg!(unix) { + result.stdout_only("test-across-1\ntest-across-2\ntest-across-3\ntest-across-4\n"); + } else { + result.stdout_only("test-across-1 test-across-2 test-across-3 test-across-4\n"); + } + } + + for option in &["-x", "--format=across"] { + // Because the test terminal has width 0, this is the same output as + // the columns option. + scene + .ucmd() + .arg("-w=30") + .arg(option) + .succeeds() + .stdout_only("test-across-1 test-across-2\ntest-across-3 test-across-4\n"); + } +} + +#[test] +fn test_ls_commas() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch(&at.plus_as_string("test-commas-1")); + at.touch(&at.plus_as_string("test-commas-2")); + at.touch(&at.plus_as_string("test-commas-3")); + at.touch(&at.plus_as_string("test-commas-4")); + + for option in &["-m", "--format=commas"] { + let result = scene.ucmd().arg(option).succeeds(); + if cfg!(unix) { + result.stdout_only("test-commas-1,\ntest-commas-2,\ntest-commas-3,\ntest-commas-4\n"); + } else { + result.stdout_only("test-commas-1, test-commas-2, test-commas-3, test-commas-4\n"); + } + } + + for option in &["-m", "--format=commas"] { + scene + .ucmd() + .arg("-w=30") + .arg(option) + .succeeds() + .stdout_only("test-commas-1, test-commas-2,\ntest-commas-3, test-commas-4\n"); + } + for option in &["-m", "--format=commas"] { + scene + .ucmd() + .arg("-w=45") + .arg(option) + .succeeds() + .stdout_only("test-commas-1, test-commas-2, test-commas-3,\ntest-commas-4\n"); + } } #[test] @@ -71,16 +289,18 @@ fn test_ls_long() { } } - let (at, mut ucmd) = at_and_ucmd!(); + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; at.touch(&at.plus_as_string("test-long")); - let result = ucmd.arg("-l").arg("test-long").succeeds(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - #[cfg(not(windows))] - assert!(result.stdout.contains("-rw-rw-r--")); - #[cfg(windows)] - assert!(result.stdout.contains("---------- 1 somebody somegroup")); + for arg in &["-l", "--long", "--format=long", "--format=verbose"] { + let result = scene.ucmd().arg(arg).arg("test-long").succeeds(); + #[cfg(not(windows))] + result.stdout_contains("-rw-rw-r--"); + + #[cfg(windows)] + result.stdout_contains("---------- 1 somebody somegroup"); + } #[cfg(not(windows))] { @@ -90,6 +310,213 @@ fn test_ls_long() { } } +#[test] +fn test_ls_long_total_size() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch(&at.plus_as_string("test-long")); + at.append("test-long", "1"); + at.touch(&at.plus_as_string("test-long2")); + at.append("test-long2", "2"); + + let expected_prints: HashMap<_, _> = if cfg!(unix) { + [ + ("long_vanilla", "total 8"), + ("long_human_readable", "total 8.0K"), + ("long_si", "total 8.2k"), + ] + .iter() + .cloned() + .collect() + } else { + [ + ("long_vanilla", "total 2"), + ("long_human_readable", "total 2"), + ("long_si", "total 2"), + ] + .iter() + .cloned() + .collect() + }; + + for arg in &["-l", "--long", "--format=long", "--format=verbose"] { + let result = scene.ucmd().arg(arg).succeeds(); + result.stdout_contains(expected_prints["long_vanilla"]); + + for arg2 in &["-h", "--human-readable", "--si"] { + let result = scene.ucmd().arg(arg).arg(arg2).succeeds(); + result.stdout_contains(if *arg2 == "--si" { + expected_prints["long_si"] + } else { + expected_prints["long_human_readable"] + }); + } + } +} + +#[test] +fn test_ls_long_formats() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch(&at.plus_as_string("test-long-formats")); + + // Regex for three names, so all of author, group and owner + let re_three = Regex::new(r"[xrw-]{9} \d ([-0-9_a-z]+ ){3}0").unwrap(); + + #[cfg(unix)] + let re_three_num = Regex::new(r"[xrw-]{9} \d (\d+ ){3}0").unwrap(); + + // Regex for two names, either: + // - group and owner + // - author and owner + // - author and group + let re_two = Regex::new(r"[xrw-]{9} \d ([-0-9_a-z]+ ){2}0").unwrap(); + + #[cfg(unix)] + let re_two_num = Regex::new(r"[xrw-]{9} \d (\d+ ){2}0").unwrap(); + + // Regex for one name: author, group or owner + let re_one = Regex::new(r"[xrw-]{9} \d [-0-9_a-z]+ 0").unwrap(); + + #[cfg(unix)] + let re_one_num = Regex::new(r"[xrw-]{9} \d \d+ 0").unwrap(); + + // Regex for no names + let re_zero = Regex::new(r"[xrw-]{9} \d 0").unwrap(); + + let result = scene + .ucmd() + .arg("-l") + .arg("--author") + .arg("test-long-formats") + .succeeds(); + assert!(re_three.is_match(result.stdout_str())); + + let result = scene + .ucmd() + .arg("-l1") + .arg("--author") + .arg("test-long-formats") + .succeeds(); + assert!(re_three.is_match(result.stdout_str())); + + #[cfg(unix)] + { + let result = scene + .ucmd() + .arg("-n") + .arg("--author") + .arg("test-long-formats") + .succeeds(); + assert!(re_three_num.is_match(result.stdout_str())); + } + + for arg in &[ + "-l", // only group and owner + "-g --author", // only author and group + "-o --author", // only author and owner + "-lG --author", // only author and owner + "-l --no-group --author", // only author and owner + ] { + let result = scene + .ucmd() + .args(&arg.split(' ').collect::>()) + .arg("test-long-formats") + .succeeds(); + assert!(re_two.is_match(result.stdout_str())); + + #[cfg(unix)] + { + let result = scene + .ucmd() + .arg("-n") + .args(&arg.split(' ').collect::>()) + .arg("test-long-formats") + .succeeds(); + assert!(re_two_num.is_match(result.stdout_str())); + } + } + + for arg in &[ + "-g", // only group + "-gl", // only group + "-o", // only owner + "-ol", // only owner + "-oG", // only owner + "-lG", // only owner + "-l --no-group", // only owner + "-gG --author", // only author + ] { + let result = scene + .ucmd() + .args(&arg.split(' ').collect::>()) + .arg("test-long-formats") + .succeeds(); + assert!(re_one.is_match(result.stdout_str())); + + #[cfg(unix)] + { + let result = scene + .ucmd() + .arg("-n") + .args(&arg.split(' ').collect::>()) + .arg("test-long-formats") + .succeeds(); + assert!(re_one_num.is_match(result.stdout_str())); + } + } + + for arg in &[ + "-og", + "-ogl", + "-lgo", + "-gG", + "-g --no-group", + "-og --no-group", + "-og --format=long", + "-ogCl", + "-og --format=vertical -l", + "-og1", + "-og1l", + ] { + let result = scene + .ucmd() + .args(&arg.split(' ').collect::>()) + .arg("test-long-formats") + .succeeds(); + assert!(re_zero.is_match(result.stdout_str())); + + #[cfg(unix)] + { + let result = scene + .ucmd() + .arg("-n") + .args(&arg.split(' ').collect::>()) + .arg("test-long-formats") + .succeeds(); + assert!(re_zero.is_match(result.stdout_str())); + } + } +} + +#[test] +fn test_ls_oneline() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch(&at.plus_as_string("test-oneline-1")); + at.touch(&at.plus_as_string("test-oneline-2")); + + // Bit of a weird situation: in the tests oneline and columns have the same output, + // except on Windows. + for option in &["-1", "--format=single-column"] { + scene + .ucmd() + .arg(option) + .succeeds() + .stdout_only("test-oneline-1\ntest-oneline-2\n"); + } +} + #[test] fn test_ls_deref() { let scene = TestScenario::new(util_name!()); @@ -107,11 +534,8 @@ fn test_ls_deref() { .arg("--color=never") .arg("test-long") .arg("test-long.link") - .run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(re.is_match(&result.stdout.trim())); + .succeeds(); + assert!(re.is_match(result.stdout_str().trim())); let result = scene .ucmd() @@ -119,11 +543,55 @@ fn test_ls_deref() { .arg("--color=never") .arg("test-long") .arg("test-long.link") - .run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(!re.is_match(&result.stdout.trim())); + .succeeds(); + assert!(!re.is_match(result.stdout_str().trim())); +} + +#[test] +fn test_ls_sort_none() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("test-3"); + at.touch("test-1"); + at.touch("test-2"); + + // Order is not specified so we just check that it doesn't + // give any errors. + scene.ucmd().arg("--sort=none").succeeds(); + scene.ucmd().arg("-U").succeeds(); +} + +#[test] +fn test_ls_sort_name() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("test-3"); + at.touch("test-1"); + at.touch("test-2"); + + let sep = if cfg!(unix) { "\n" } else { " " }; + + scene + .ucmd() + .arg("--sort=name") + .succeeds() + .stdout_is(["test-1", "test-2", "test-3\n"].join(sep)); + + let scene_dot = TestScenario::new(util_name!()); + let at = &scene_dot.fixtures; + at.touch(".a"); + at.touch("a"); + at.touch(".b"); + at.touch("b"); + + scene_dot + .ucmd() + .arg("--sort=name") + .arg("-A") + .succeeds() + .stdout_is([".a", ".b", "a", "b\n"].join(sep)); } #[test] @@ -141,69 +609,246 @@ fn test_ls_order_size() { at.touch("test-4"); at.append("test-4", "4444"); - let result = scene.ucmd().arg("-al").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); + scene.ucmd().arg("-al").succeeds(); - let result = scene.ucmd().arg("-S").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); + let result = scene.ucmd().arg("-S").succeeds(); #[cfg(not(windows))] - assert_eq!(result.stdout, "test-4\ntest-3\ntest-2\ntest-1\n"); + result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n"); #[cfg(windows)] - assert_eq!(result.stdout, "test-4 test-3 test-2 test-1\n"); + result.stdout_only("test-4 test-3 test-2 test-1\n"); - let result = scene.ucmd().arg("-S").arg("-r").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); + let result = scene.ucmd().arg("-S").arg("-r").succeeds(); #[cfg(not(windows))] - assert_eq!(result.stdout, "test-1\ntest-2\ntest-3\ntest-4\n"); + result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n"); #[cfg(windows)] - assert_eq!(result.stdout, "test-1 test-2 test-3 test-4\n"); + result.stdout_only("test-1 test-2 test-3 test-4\n"); + + let result = scene.ucmd().arg("--sort=size").succeeds(); + #[cfg(not(windows))] + result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n"); + #[cfg(windows)] + result.stdout_only("test-4 test-3 test-2 test-1\n"); + + let result = scene.ucmd().arg("--sort=size").arg("-r").succeeds(); + #[cfg(not(windows))] + result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n"); + #[cfg(windows)] + result.stdout_only("test-1 test-2 test-3 test-4\n"); } #[test] -fn test_ls_order_creation() { +fn test_ls_long_ctime() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("test-long-ctime-1"); + + for arg in &["-c", "--time=ctime", "--time=status"] { + let result = scene.ucmd().arg("-l").arg(arg).succeeds(); + + // Should show the time on Unix, but question marks on windows. + #[cfg(unix)] + result.stdout_contains(":"); + #[cfg(not(unix))] + result.stdout_contains("???"); + } +} + +#[test] +#[ignore] +fn test_ls_order_birthtime() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + /* + Here we make 2 files with a timeout in between. + After creating the first file try to sync it. + This ensures the file gets created immediately instead of being saved + inside the OS's IO operation buffer. + Without this, both files might accidentally be created at the same time. + */ + at.make_file("test-birthtime-1").sync_all().unwrap(); + at.make_file("test-birthtime-2").sync_all().unwrap(); + at.open("test-birthtime-1"); + + let result = scene.ucmd().arg("--time=birth").arg("-t").run(); + + #[cfg(not(windows))] + assert_eq!(result.stdout_str(), "test-birthtime-2\ntest-birthtime-1\n"); + #[cfg(windows)] + assert_eq!(result.stdout_str(), "test-birthtime-2 test-birthtime-1\n"); +} + +#[test] +fn test_ls_styles() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch("test"); + + let re_full = Regex::new( + r"[a-z-]* \d* \w* \w* \d* \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d* (\+|\-)\d{4} test\n", + ) + .unwrap(); + let re_long = + Regex::new(r"[a-z-]* \d* \w* \w* \d* \d{4}-\d{2}-\d{2} \d{2}:\d{2} test\n").unwrap(); + let re_iso = Regex::new(r"[a-z-]* \d* \w* \w* \d* \d{2}-\d{2} \d{2}:\d{2} test\n").unwrap(); + let re_locale = + Regex::new(r"[a-z-]* \d* \w* \w* \d* [A-Z][a-z]{2} ( |\d)\d \d{2}:\d{2} test\n").unwrap(); + + //full-iso + let result = scene + .ucmd() + .arg("-l") + .arg("--time-style=full-iso") + .succeeds(); + assert!(re_full.is_match(result.stdout_str())); + //long-iso + let result = scene + .ucmd() + .arg("-l") + .arg("--time-style=long-iso") + .succeeds(); + assert!(re_long.is_match(result.stdout_str())); + //iso + let result = scene.ucmd().arg("-l").arg("--time-style=iso").succeeds(); + assert!(re_iso.is_match(result.stdout_str())); + //locale + let result = scene.ucmd().arg("-l").arg("--time-style=locale").succeeds(); + assert!(re_locale.is_match(result.stdout_str())); + + //Overwrite options tests + let result = scene + .ucmd() + .arg("-l") + .arg("--time-style=long-iso") + .arg("--time-style=iso") + .succeeds(); + assert!(re_iso.is_match(result.stdout_str())); + let result = scene + .ucmd() + .arg("--time-style=iso") + .arg("--full-time") + .succeeds(); + assert!(re_full.is_match(result.stdout_str())); + let result = scene + .ucmd() + .arg("--full-time") + .arg("--time-style=iso") + .succeeds(); + assert!(re_iso.is_match(result.stdout_str())); + + let result = scene + .ucmd() + .arg("--full-time") + .arg("--time-style=iso") + .arg("--full-time") + .succeeds(); + assert!(re_full.is_match(result.stdout_str())); + + let result = scene + .ucmd() + .arg("--full-time") + .arg("-x") + .arg("-l") + .succeeds(); + assert!(re_full.is_match(result.stdout_str())); + + at.touch("test2"); + let result = scene.ucmd().arg("--full-time").arg("-x").succeeds(); + #[cfg(not(windows))] + assert_eq!(result.stdout_str(), "test\ntest2\n"); + #[cfg(windows)] + assert_eq!(result.stdout_str(), "test test2\n"); +} + +#[test] +fn test_ls_order_time() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; at.touch("test-1"); at.append("test-1", "1"); - sleep(Duration::from_millis(500)); + sleep(Duration::from_millis(100)); at.touch("test-2"); at.append("test-2", "22"); - sleep(Duration::from_millis(500)); + + sleep(Duration::from_millis(100)); at.touch("test-3"); at.append("test-3", "333"); - sleep(Duration::from_millis(500)); + sleep(Duration::from_millis(100)); at.touch("test-4"); at.append("test-4", "4444"); + sleep(Duration::from_millis(100)); - let result = scene.ucmd().arg("-al").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); + // Read test-3, only changing access time + at.read("test-3"); - let result = scene.ucmd().arg("-t").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); + // Set permissions of test-2, only changing ctime + std::fs::set_permissions( + at.plus_as_string("test-2"), + at.metadata("test-2").permissions(), + ) + .unwrap(); + + scene.ucmd().arg("-al").succeeds(); + + // ctime was changed at write, so the order is 4 3 2 1 + let result = scene.ucmd().arg("-t").succeeds(); #[cfg(not(windows))] - assert_eq!(result.stdout, "test-4\ntest-3\ntest-2\ntest-1\n"); + result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n"); #[cfg(windows)] - assert_eq!(result.stdout, "test-4 test-3 test-2 test-1\n"); + result.stdout_only("test-4 test-3 test-2 test-1\n"); - let result = scene.ucmd().arg("-t").arg("-r").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); + let result = scene.ucmd().arg("--sort=time").succeeds(); #[cfg(not(windows))] - assert_eq!(result.stdout, "test-1\ntest-2\ntest-3\ntest-4\n"); + result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n"); #[cfg(windows)] - assert_eq!(result.stdout, "test-1 test-2 test-3 test-4\n"); + result.stdout_only("test-4 test-3 test-2 test-1\n"); + + let result = scene.ucmd().arg("-tr").succeeds(); + #[cfg(not(windows))] + result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n"); + #[cfg(windows)] + result.stdout_only("test-1 test-2 test-3 test-4\n"); + + let result = scene.ucmd().arg("--sort=time").arg("-r").succeeds(); + #[cfg(not(windows))] + result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n"); + #[cfg(windows)] + result.stdout_only("test-1 test-2 test-3 test-4\n"); + + // 3 was accessed last in the read + // So the order should be 2 3 4 1 + for arg in &["-u", "--time=atime", "--time=access", "--time=use"] { + let result = scene.ucmd().arg("-t").arg(arg).succeeds(); + let file3_access = at.open("test-3").metadata().unwrap().accessed().unwrap(); + let file4_access = at.open("test-4").metadata().unwrap().accessed().unwrap(); + + // It seems to be dependent on the platform whether the access time is actually set + if file3_access > file4_access { + if cfg!(not(windows)) { + result.stdout_only("test-3\ntest-4\ntest-2\ntest-1\n"); + } else { + result.stdout_only("test-3 test-4 test-2 test-1\n"); + } + } else { + // Access time does not seem to be set on Windows and some other + // systems so the order is 4 3 2 1 + if cfg!(not(windows)) { + result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n"); + } else { + result.stdout_only("test-4 test-3 test-2 test-1\n"); + } + } + } + + // test-2 had the last ctime change when the permissions were set + // So the order should be 2 4 3 1 + #[cfg(unix)] + { + let result = scene.ucmd().arg("-tc").succeeds(); + result.stdout_only("test-2\ntest-4\ntest-3\ntest-1\n"); + } } #[test] @@ -226,18 +871,21 @@ fn test_ls_files_dirs() { scene.ucmd().arg("a/a").succeeds(); scene.ucmd().arg("a").arg("z").succeeds(); - let result = scene.ucmd().arg("doesntexist").fails(); // Doesn't exist - assert!(result - .stderr - .contains("error: 'doesntexist': No such file or directory")); + scene + .ucmd() + .arg("doesntexist") + .fails() + .stderr_contains(&"'doesntexist': No such file or directory"); - let result = scene.ucmd().arg("a").arg("doesntexist").fails(); // One exists, the other doesn't - assert!(result - .stderr - .contains("error: 'doesntexist': No such file or directory")); - assert!(result.stdout.contains("a:")); + scene + .ucmd() + .arg("a") + .arg("doesntexist") + .fails() + .stderr_contains(&"'doesntexist': No such file or directory") + .stdout_contains(&"a:"); } #[test] @@ -253,6 +901,12 @@ fn test_ls_recursive() { scene.ucmd().arg("a").succeeds(); scene.ucmd().arg("a/a").succeeds(); + scene + .ucmd() + .arg("z") + .arg("-R") + .succeeds() + .stdout_contains(&"z:"); let result = scene .ucmd() .arg("--color=never") @@ -261,54 +915,347 @@ fn test_ls_recursive() { .arg("z") .succeeds(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); #[cfg(not(windows))] - assert!(result.stdout.contains("a/b:\nb")); + result.stdout_contains(&"a/b:\nb"); #[cfg(windows)] - assert!(result.stdout.contains("a\\b:\nb")); + result.stdout_contains(&"a\\b:\nb"); } #[test] -fn test_ls_ls_color() { +fn test_ls_color() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; at.mkdir("a"); + let nested_dir = Path::new("a") + .join("nested_dir") + .to_string_lossy() + .to_string(); + at.mkdir(&nested_dir); at.mkdir("z"); - at.touch(&at.plus_as_string("a/a")); - scene.ucmd().arg("--color").succeeds(); - scene.ucmd().arg("--color=always").succeeds(); - scene.ucmd().arg("--color=never").succeeds(); - scene.ucmd().arg("--color").arg("a").succeeds(); - scene.ucmd().arg("--color=always").arg("a/a").succeeds(); - scene.ucmd().arg("--color=never").arg("z").succeeds(); + let nested_file = Path::new("a") + .join("nested_file") + .to_string_lossy() + .to_string(); + at.touch(&nested_file); + at.touch("test-color"); + + let a_with_colors = "\x1b[1;34ma\x1b[0m"; + let z_with_colors = "\x1b[1;34mz\x1b[0m"; + let nested_dir_with_colors = "\x1b[1;34mnested_dir\x1b[0m"; // spell-checker:disable-line + + // Color is disabled by default + let result = scene.ucmd().succeeds(); + assert!(!result.stdout_str().contains(a_with_colors)); + assert!(!result.stdout_str().contains(z_with_colors)); + + // Color should be enabled + scene + .ucmd() + .arg("--color") + .succeeds() + .stdout_contains(a_with_colors) + .stdout_contains(z_with_colors); + + // Color should be enabled + scene + .ucmd() + .arg("--color=always") + .succeeds() + .stdout_contains(a_with_colors) + .stdout_contains(z_with_colors); + + // Color should be disabled + let result = scene.ucmd().arg("--color=never").succeeds(); + assert!(!result.stdout_str().contains(a_with_colors)); + assert!(!result.stdout_str().contains(z_with_colors)); + + // Nested dir should be shown and colored + scene + .ucmd() + .arg("--color") + .arg("a") + .succeeds() + .stdout_contains(nested_dir_with_colors); + + // No output + scene + .ucmd() + .arg("--color=never") + .arg("z") + .succeeds() + .stdout_only(""); + + // The colors must not mess up the grid layout + at.touch("b"); + scene + .ucmd() + .arg("--color") + .arg("-w=15") + .succeeds() + .stdout_only(format!( + "{} test-color\nb {}\n", + a_with_colors, z_with_colors + )); } -#[cfg(not(any(target_os = "macos", target_os = "windows")))] // Truncate not available on mac or win +#[cfg(unix)] #[test] -fn test_ls_human() { +fn test_ls_inode() { let scene = TestScenario::new(util_name!()); - let file = "test_human"; - let result = scene.cmd("truncate").arg("-s").arg("+1000").arg(file).run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - let result = scene.ucmd().arg("-hl").arg(file).run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(result.stdout.contains("1.00K")); + let at = &scene.fixtures; + + let file = "test_inode"; + at.touch(file); + + let re_short = Regex::new(r" *(\d+) test_inode").unwrap(); + let re_long = Regex::new(r" *(\d+) [xrw-]{10} \d .+ test_inode").unwrap(); + + let result = scene.ucmd().arg("test_inode").arg("-i").succeeds(); + assert!(re_short.is_match(result.stdout_str())); + let inode_short = re_short + .captures(result.stdout_str()) + .unwrap() + .get(1) + .unwrap() + .as_str(); + + let result = scene.ucmd().arg("test_inode").succeeds(); + assert!(!re_short.is_match(result.stdout_str())); + assert!(!result.stdout_str().contains(inode_short)); + + let result = scene.ucmd().arg("-li").arg("test_inode").succeeds(); + assert!(re_long.is_match(result.stdout_str())); + let inode_long = re_long + .captures(result.stdout_str()) + .unwrap() + .get(1) + .unwrap() + .as_str(); + + let result = scene.ucmd().arg("-l").arg("test_inode").succeeds(); + assert!(!re_long.is_match(result.stdout_str())); + assert!(!result.stdout_str().contains(inode_long)); + + assert_eq!(inode_short, inode_long) +} + +#[test] +#[cfg(not(windows))] +fn test_ls_indicator_style() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + // Setup: Directory, Symlink, and Pipes. + at.mkdir("directory"); + assert!(at.dir_exists("directory")); + + at.touch(&at.plus_as_string("link-src")); + at.symlink_file("link-src", "link-dest.link"); + assert!(at.is_symlink("link-dest.link")); + + at.mkfifo("named-pipe.fifo"); + assert!(at.is_fifo("named-pipe.fifo")); + + // Classify, File-Type, and Slash all contain indicators for directories. + let options = vec!["classify", "file-type", "slash"]; + for opt in options { + // Verify that classify and file-type both contain indicators for symlinks. + scene + .ucmd() + .arg(format!("--indicator-style={}", opt)) + .succeeds() + .stdout_contains(&"/"); + } + + // Same test as above, but with the alternate flags. + let options = vec!["--classify", "--file-type", "-p"]; + for opt in options { + scene + .ucmd() + .arg(opt.to_string()) + .succeeds() + .stdout_contains(&"/"); + } + + // Classify and File-Type all contain indicators for pipes and links. + let options = vec!["classify", "file-type"]; + for opt in options { + // Verify that classify and file-type both contain indicators for symlinks. + scene + .ucmd() + .arg(format!("--indicator-style={}", opt)) + .succeeds() + .stdout_contains(&"@") + .stdout_contains(&"|"); + } + + // Test sockets. Because the canonical way of making sockets to test is with + // TempDir, we need a separate test. + { + use self::unix_socket::UnixListener; + + let dir = tempfile::Builder::new() + .prefix("unix_socket") + .tempdir() + .expect("failed to create dir"); + let socket_path = dir.path().join("sock"); + let _listener = UnixListener::bind(&socket_path).expect("failed to create socket"); + + new_ucmd!() + .args(&[ + PathBuf::from(dir.path().to_str().unwrap()), + PathBuf::from("--indicator-style=classify"), + ]) + .succeeds() + .stdout_only("sock=\n"); + } +} + +// Essentially the same test as above, but only test symlinks and directories, +// not pipes or sockets. +#[test] +#[cfg(not(unix))] +fn test_ls_indicator_style() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + // Setup: Directory, Symlink. + at.mkdir("directory"); + assert!(at.dir_exists("directory")); + + at.touch(&at.plus_as_string("link-src")); + at.symlink_file("link-src", "link-dest.link"); + assert!(at.is_symlink("link-dest.link")); + + // Classify, File-Type, and Slash all contain indicators for directories. + let options = vec!["classify", "file-type", "slash"]; + for opt in options { + // Verify that classify and file-type both contain indicators for symlinks. + scene + .ucmd() + .arg(format!("--indicator-style={}", opt)) + .succeeds() + .stdout_contains(&"/"); + } + + // Same test as above, but with the alternate flags. + let options = vec!["--classify", "--file-type", "-p"]; + for opt in options { + scene + .ucmd() + .arg(opt.to_string()) + .succeeds() + .stdout_contains(&"/"); + } + + // Classify and File-Type all contain indicators for pipes and links. + let options = vec!["classify", "file-type"]; + for opt in options { + // Verify that classify and file-type both contain indicators for symlinks. + scene + .ucmd() + .arg(format!("--indicator-style={}", opt)) + .succeeds() + .stdout_contains(&"@"); + } +} + +#[cfg(not(any(target_vendor = "apple", target_os = "windows")))] // Truncate not available on mac or win +#[test] +fn test_ls_human_si() { + let scene = TestScenario::new(util_name!()); + let file1 = "test_human-1"; + scene + .cmd("truncate") + .arg("-s") + .arg("+1000") + .arg(file1) + .succeeds(); + + scene + .ucmd() + .arg("-hl") + .arg(file1) + .succeeds() + .stdout_contains(" 1000 "); + + scene + .ucmd() + .arg("-l") + .arg("--si") + .arg(file1) + .succeeds() + .stdout_contains(" 1.0k "); + scene .cmd("truncate") .arg("-s") .arg("+1000k") - .arg(file) + .arg(file1) .run(); - let result = scene.ucmd().arg("-hl").arg(file).run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(result.stdout.contains("1.02M")); + + scene + .ucmd() + .arg("-hl") + .arg(file1) + .succeeds() + .stdout_contains(" 1001K "); + + scene + .ucmd() + .arg("-l") + .arg("--si") + .arg(file1) + .succeeds() + .stdout_contains(" 1.1M "); + + let file2 = "test-human-2"; + scene + .cmd("truncate") + .arg("-s") + .arg("+12300k") + .arg(file2) + .succeeds(); + + // GNU rounds up, so we must too. + scene + .ucmd() + .arg("-hl") + .arg(file2) + .succeeds() + .stdout_contains(" 13M "); + + // GNU rounds up, so we must too. + scene + .ucmd() + .arg("-l") + .arg("--si") + .arg(file2) + .succeeds() + .stdout_contains(" 13M "); + + let file3 = "test-human-3"; + scene + .cmd("truncate") + .arg("-s") + .arg("+9999") + .arg(file3) + .succeeds(); + + scene + .ucmd() + .arg("-hl") + .arg(file3) + .succeeds() + .stdout_contains(" 9.8K "); + + scene + .ucmd() + .arg("-l") + .arg("--si") + .arg(file3) + .succeeds() + .stdout_contains(" 10k "); } #[cfg(windows)] @@ -325,14 +1272,777 @@ fn test_ls_hidden_windows() { .arg("+S") .arg("+r") .arg(file) - .run(); - let result = scene.ucmd().run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - let result = scene.ucmd().arg("-a").run(); - println!("stderr = {:?}", result.stderr); - println!("stdout = {:?}", result.stdout); - assert!(result.success); - assert!(result.stdout.contains(file)); + .succeeds(); + + let result = scene.ucmd().succeeds(); + assert!(!result.stdout_str().contains(file)); + scene.ucmd().arg("-a").succeeds().stdout_contains(file); +} + +#[test] +fn test_ls_version_sort() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + for filename in &[ + "a2", + "b1", + "b20", + "a1.4", + "a1.40", + "b3", + "b11", + "b20b", + "b20a", + "a100", + "a1.13", + "aa", + "a1", + "aaa", + "a1.00000040", + "abab", + "ab", + "a01.40", + "a001.001", + "a01.0000001", + "a01.001", + "a001.01", + ] { + at.touch(filename); + } + + let mut expected = vec![ + "a1", + "a001.001", + "a001.01", + "a01.0000001", + "a01.001", + "a1.4", + "a1.13", + "a01.40", + "a1.00000040", + "a1.40", + "a2", + "a100", + "aa", + "aaa", + "ab", + "abab", + "b1", + "b3", + "b11", + "b20", + "b20a", + "b20b", + "", // because of '\n' at the end of the output + ]; + + let result = scene.ucmd().arg("-1v").succeeds(); + assert_eq!( + result.stdout_str().split('\n').collect::>(), + expected + ); + + let result = scene.ucmd().arg("-1").arg("--sort=version").succeeds(); + assert_eq!( + result.stdout_str().split('\n').collect::>(), + expected + ); + + let result = scene.ucmd().arg("-a1v").succeeds(); + expected.insert(0, ".."); + expected.insert(0, "."); + assert_eq!( + result.stdout_str().split('\n').collect::>(), + expected, + ) +} + +#[test] +fn test_ls_quoting_style() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("one two"); + at.touch("one"); + + // It seems that windows doesn't allow \n in filenames. + // And it also doesn't like \, of course. + #[cfg(unix)] + { + at.touch("one\ntwo"); + at.touch("one\\two"); + // Default is shell-escape + scene + .ucmd() + .arg("one\ntwo") + .succeeds() + .stdout_only("'one'$'\\n''two'\n"); + + for (arg, correct) in &[ + ("--quoting-style=literal", "one?two"), + ("-N", "one?two"), + ("--literal", "one?two"), + ("--quoting-style=c", "\"one\\ntwo\""), + ("-Q", "\"one\\ntwo\""), + ("--quote-name", "\"one\\ntwo\""), + ("--quoting-style=escape", "one\\ntwo"), + ("-b", "one\\ntwo"), + ("--escape", "one\\ntwo"), + ("--quoting-style=shell-escape", "'one'$'\\n''two'"), + ("--quoting-style=shell-escape-always", "'one'$'\\n''two'"), + ("--quoting-style=shell", "one?two"), + ("--quoting-style=shell-always", "'one?two'"), + ] { + scene + .ucmd() + .arg(arg) + .arg("one\ntwo") + .succeeds() + .stdout_only(format!("{}\n", correct)); + } + + for (arg, correct) in &[ + ("--quoting-style=literal", "one?two"), + ("-N", "one?two"), + ("--literal", "one?two"), + ("--quoting-style=shell", "one?two"), + ("--quoting-style=shell-always", "'one?two'"), + ] { + scene + .ucmd() + .arg(arg) + .arg("--hide-control-chars") + .arg("one\ntwo") + .succeeds() + .stdout_only(format!("{}\n", correct)); + } + + for (arg, correct) in &[ + ("--quoting-style=literal", "one\ntwo"), + ("-N", "one\ntwo"), + ("--literal", "one\ntwo"), + ("--quoting-style=shell", "one\ntwo"), + ("--quoting-style=shell-always", "'one\ntwo'"), + ] { + scene + .ucmd() + .arg(arg) + .arg("--show-control-chars") + .arg("one\ntwo") + .succeeds() + .stdout_only(format!("{}\n", correct)); + } + + for (arg, correct) in &[ + ("--quoting-style=literal", "one\\two"), + ("-N", "one\\two"), + ("--quoting-style=c", "\"one\\\\two\""), + ("-Q", "\"one\\\\two\""), + ("--quote-name", "\"one\\\\two\""), + ("--quoting-style=escape", "one\\\\two"), + ("-b", "one\\\\two"), + ("--quoting-style=shell-escape", "'one\\two'"), + ("--quoting-style=shell-escape-always", "'one\\two'"), + ("--quoting-style=shell", "'one\\two'"), + ("--quoting-style=shell-always", "'one\\two'"), + ] { + scene + .ucmd() + .arg(arg) + .arg("one\\two") + .succeeds() + .stdout_only(format!("{}\n", correct)); + } + + // Tests for a character that forces quotation in shell-style escaping + // after a character in a dollar expression + at.touch("one\n&two"); + for (arg, correct) in &[ + ("--quoting-style=shell-escape", "'one'$'\\n''&two'"), + ("--quoting-style=shell-escape-always", "'one'$'\\n''&two'"), + ] { + scene + .ucmd() + .arg(arg) + .arg("one\n&two") + .succeeds() + .stdout_only(format!("{}\n", correct)); + } + } + + scene + .ucmd() + .arg("one two") + .succeeds() + .stdout_only("'one two'\n"); + + for (arg, correct) in &[ + ("--quoting-style=literal", "one two"), + ("-N", "one two"), + ("--literal", "one two"), + ("--quoting-style=c", "\"one two\""), + ("-Q", "\"one two\""), + ("--quote-name", "\"one two\""), + ("--quoting-style=escape", "one\\ two"), + ("-b", "one\\ two"), + ("--escape", "one\\ two"), + ("--quoting-style=shell-escape", "'one two'"), + ("--quoting-style=shell-escape-always", "'one two'"), + ("--quoting-style=shell", "'one two'"), + ("--quoting-style=shell-always", "'one two'"), + ] { + scene + .ucmd() + .arg(arg) + .arg("one two") + .succeeds() + .stdout_only(format!("{}\n", correct)); + } + + scene.ucmd().arg("one").succeeds().stdout_only("one\n"); + + for (arg, correct) in &[ + ("--quoting-style=literal", "one"), + ("-N", "one"), + ("--quoting-style=c", "\"one\""), + ("-Q", "\"one\""), + ("--quote-name", "\"one\""), + ("--quoting-style=escape", "one"), + ("-b", "one"), + ("--quoting-style=shell-escape", "one"), + ("--quoting-style=shell-escape-always", "'one'"), + ("--quoting-style=shell", "one"), + ("--quoting-style=shell-always", "'one'"), + ] { + scene + .ucmd() + .arg(arg) + .arg("one") + .succeeds() + .stdout_only(format!("{}\n", correct)); + } +} + +#[test] +fn test_ls_ignore_hide() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("README.md"); + at.touch("CONTRIBUTING.md"); + at.touch("some_other_file"); + at.touch("READMECAREFULLY.md"); + + scene + .ucmd() + .arg("--hide") + .arg("*") + .arg("-1") + .succeeds() + .stdout_only(""); + + scene + .ucmd() + .arg("--ignore") + .arg("*") + .arg("-1") + .succeeds() + .stdout_only(""); + + scene + .ucmd() + .arg("--ignore") + .arg("irrelevant pattern") + .arg("-1") + .succeeds() + .stdout_only("CONTRIBUTING.md\nREADME.md\nREADMECAREFULLY.md\nsome_other_file\n"); + + scene + .ucmd() + .arg("--ignore") + .arg("README*.md") + .arg("-1") + .succeeds() + .stdout_only("CONTRIBUTING.md\nsome_other_file\n"); + + scene + .ucmd() + .arg("--hide") + .arg("README*.md") + .arg("-1") + .succeeds() + .stdout_only("CONTRIBUTING.md\nsome_other_file\n"); + + scene + .ucmd() + .arg("--ignore") + .arg("*.md") + .arg("-1") + .succeeds() + .stdout_only("some_other_file\n"); + + scene + .ucmd() + .arg("-a") + .arg("--ignore") + .arg("*.md") + .arg("-1") + .succeeds() + .stdout_only(".\n..\nsome_other_file\n"); + + scene + .ucmd() + .arg("-a") + .arg("--hide") + .arg("*.md") + .arg("-1") + .succeeds() + .stdout_only(".\n..\nCONTRIBUTING.md\nREADME.md\nREADMECAREFULLY.md\nsome_other_file\n"); + + scene + .ucmd() + .arg("-A") + .arg("--ignore") + .arg("*.md") + .arg("-1") + .succeeds() + .stdout_only("some_other_file\n"); + + scene + .ucmd() + .arg("-A") + .arg("--hide") + .arg("*.md") + .arg("-1") + .succeeds() + .stdout_only("CONTRIBUTING.md\nREADME.md\nREADMECAREFULLY.md\nsome_other_file\n"); + + // Stacking multiple patterns + scene + .ucmd() + .arg("--ignore") + .arg("README*") + .arg("--ignore") + .arg("CONTRIBUTING*") + .arg("-1") + .succeeds() + .stdout_only("some_other_file\n"); + + scene + .ucmd() + .arg("--hide") + .arg("README*") + .arg("--ignore") + .arg("CONTRIBUTING*") + .arg("-1") + .succeeds() + .stdout_only("some_other_file\n"); + + scene + .ucmd() + .arg("--hide") + .arg("README*") + .arg("--hide") + .arg("CONTRIBUTING*") + .arg("-1") + .succeeds() + .stdout_only("some_other_file\n"); + + // Invalid patterns + scene + .ucmd() + .arg("--ignore") + .arg("READ[ME") + .arg("-1") + .succeeds() + .stderr_contains(&"Invalid pattern") + .stdout_is("CONTRIBUTING.md\nREADME.md\nREADMECAREFULLY.md\nsome_other_file\n"); + + scene + .ucmd() + .arg("--hide") + .arg("READ[ME") + .arg("-1") + .succeeds() + .stderr_contains(&"Invalid pattern") + .stdout_is("CONTRIBUTING.md\nREADME.md\nREADMECAREFULLY.md\nsome_other_file\n"); +} + +#[test] +fn test_ls_ignore_backups() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("somefile"); + at.touch("somebackup~"); + at.touch(".somehiddenfile"); + at.touch(".somehiddenbackup~"); + + scene.ucmd().arg("-B").succeeds().stdout_is("somefile\n"); + scene + .ucmd() + .arg("--ignore-backups") + .succeeds() + .stdout_is("somefile\n"); + + scene + .ucmd() + .arg("-aB") + .succeeds() + .stdout_contains(".somehiddenfile") + .stdout_contains("somefile") + .stdout_does_not_contain("somebackup") + .stdout_does_not_contain(".somehiddenbackup~"); + + scene + .ucmd() + .arg("-a") + .arg("--ignore-backups") + .succeeds() + .stdout_contains(".somehiddenfile") + .stdout_contains("somefile") + .stdout_does_not_contain("somebackup") + .stdout_does_not_contain(".somehiddenbackup~"); +} + +#[test] +fn test_ls_directory() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir("some_dir"); + at.symlink_dir("some_dir", "sym_dir"); + + at.touch(Path::new("some_dir").join("nested_file").to_str().unwrap()); + + scene + .ucmd() + .arg("some_dir") + .succeeds() + .stdout_is("nested_file\n"); + + scene + .ucmd() + .arg("--directory") + .arg("some_dir") + .succeeds() + .stdout_is("some_dir\n"); + + scene + .ucmd() + .arg("sym_dir") + .succeeds() + .stdout_is("nested_file\n"); +} + +#[test] +fn test_ls_deref_command_line() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("some_file"); + at.symlink_file("some_file", "sym_file"); + + scene + .ucmd() + .arg("sym_file") + .succeeds() + .stdout_is("sym_file\n"); + + // -l changes the default to no dereferencing + scene + .ucmd() + .arg("-l") + .arg("sym_file") + .succeeds() + .stdout_contains("sym_file ->"); + + scene + .ucmd() + .arg("--dereference-command-line-symlink-to-dir") + .arg("sym_file") + .succeeds() + .stdout_is("sym_file\n"); + + scene + .ucmd() + .arg("-l") + .arg("--dereference-command-line-symlink-to-dir") + .arg("sym_file") + .succeeds() + .stdout_contains("sym_file ->"); + + scene + .ucmd() + .arg("--dereference-command-line") + .arg("sym_file") + .succeeds() + .stdout_is("sym_file\n"); + + let result = scene + .ucmd() + .arg("-l") + .arg("--dereference-command-line") + .arg("sym_file") + .succeeds(); + + assert!(!result.stdout_str().contains("->")); + + let result = scene.ucmd().arg("-lH").arg("sym_file").succeeds(); + + assert!(!result.stdout_str().contains("sym_file ->")); + + // If the symlink is not a command line argument, it must be shown normally + scene + .ucmd() + .arg("-l") + .arg("--dereference-command-line") + .succeeds() + .stdout_contains("sym_file ->"); +} + +#[test] +fn test_ls_deref_command_line_dir() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir("some_dir"); + at.symlink_dir("some_dir", "sym_dir"); + + at.touch(Path::new("some_dir").join("nested_file").to_str().unwrap()); + + scene + .ucmd() + .arg("sym_dir") + .succeeds() + .stdout_contains("nested_file"); + + scene + .ucmd() + .arg("-l") + .arg("sym_dir") + .succeeds() + .stdout_contains("sym_dir ->"); + + scene + .ucmd() + .arg("--dereference-command-line-symlink-to-dir") + .arg("sym_dir") + .succeeds() + .stdout_contains("nested_file"); + + scene + .ucmd() + .arg("-l") + .arg("--dereference-command-line-symlink-to-dir") + .arg("sym_dir") + .succeeds() + .stdout_contains("nested_file"); + + scene + .ucmd() + .arg("--dereference-command-line") + .arg("sym_dir") + .succeeds() + .stdout_contains("nested_file"); + + scene + .ucmd() + .arg("-l") + .arg("--dereference-command-line") + .arg("sym_dir") + .succeeds() + .stdout_contains("nested_file"); + + scene + .ucmd() + .arg("-lH") + .arg("sym_dir") + .succeeds() + .stdout_contains("nested_file"); + + // If the symlink is not a command line argument, it must be shown normally + scene + .ucmd() + .arg("-l") + .arg("--dereference-command-line") + .succeeds() + .stdout_contains("sym_dir ->"); + + scene + .ucmd() + .arg("-lH") + .succeeds() + .stdout_contains("sym_dir ->"); + + scene + .ucmd() + .arg("-l") + .arg("--dereference-command-line-symlink-to-dir") + .succeeds() + .stdout_contains("sym_dir ->"); + + // --directory does not dereference anything by default + scene + .ucmd() + .arg("-l") + .arg("--directory") + .arg("sym_dir") + .succeeds() + .stdout_contains("sym_dir ->"); + + let result = scene + .ucmd() + .arg("-l") + .arg("--directory") + .arg("--dereference-command-line-symlink-to-dir") + .arg("sym_dir") + .succeeds(); + + assert!(!result.stdout_str().ends_with("sym_dir")); + + // --classify does not dereference anything by default + scene + .ucmd() + .arg("-l") + .arg("--directory") + .arg("sym_dir") + .succeeds() + .stdout_contains("sym_dir ->"); + + let result = scene + .ucmd() + .arg("-l") + .arg("--directory") + .arg("--dereference-command-line-symlink-to-dir") + .arg("sym_dir") + .succeeds(); + + assert!(!result.stdout_str().ends_with("sym_dir")); +} + +#[test] +fn test_ls_sort_extension() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + for filename in &[ + "file1", + "file2", + "anotherFile", + ".hidden", + ".file.1", + ".file.2", + "file.1", + "file.2", + "anotherFile.1", + "anotherFile.2", + "file.ext", + "file.debug", + "anotherFile.ext", + "anotherFile.debug", + ] { + at.touch(filename); + } + + let expected = vec![ + ".", + "..", + ".hidden", + "anotherFile", + "file1", + "file2", + ".file.1", + "anotherFile.1", + "file.1", + ".file.2", + "anotherFile.2", + "file.2", + "anotherFile.debug", + "file.debug", + "anotherFile.ext", + "file.ext", + "", // because of '\n' at the end of the output + ]; + + let result = scene.ucmd().arg("-1aX").run(); + assert_eq!( + result.stdout_str().split('\n').collect::>(), + expected, + ); + + let result = scene.ucmd().arg("-1a").arg("--sort=extension").run(); + assert_eq!( + result.stdout_str().split('\n').collect::>(), + expected, + ); +} + +#[test] +fn test_ls_path() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let file1 = "file1"; + let file2 = "file2"; + let dir = "dir"; + let path = &format!("{}/{}", dir, file2); + + at.mkdir(dir); + at.touch(file1); + at.touch(path); + + let expected_stdout = &format!("{}\n", path); + scene.ucmd().arg(path).run().stdout_is(expected_stdout); + + let expected_stdout = &format!("./{}\n", path); + scene + .ucmd() + .arg(format!("./{}", path)) + .run() + .stdout_is(expected_stdout); + + let abs_path = format!("{}/{}", at.as_string(), path); + let expected_stdout = if cfg!(windows) { + format!("\'{}\'\n", abs_path) + } else { + format!("{}\n", abs_path) + }; + scene.ucmd().arg(&abs_path).run().stdout_is(expected_stdout); + + let expected_stdout = if cfg!(windows) { + format!("{} {}\n", path, file1) + } else { + format!("{}\n{}\n", path, file1) + }; + scene + .ucmd() + .arg(file1) + .arg(path) + .run() + .stdout_is(expected_stdout); +} + +#[test] +fn test_ls_dangling_symlinks() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir("temp_dir"); + at.symlink_file("does_not_exist", "temp_dir/dangle"); + + scene.ucmd().arg("-L").arg("temp_dir/dangle").fails(); + scene.ucmd().arg("-H").arg("temp_dir/dangle").fails(); + + scene + .ucmd() + .arg("temp_dir/dangle") + .succeeds() + .stdout_contains("dangle"); + + scene + .ucmd() + .arg("-Li") + .arg("temp_dir") + .succeeds() // this should fail, though at the moment, ls lacks a way to propagate errors encountered during display + .stdout_contains(if cfg!(windows) { "dangle" } else { "? dangle" }); } diff --git a/tests/by-util/test_mkdir.rs b/tests/by-util/test_mkdir.rs index ef3226c41..54a6fe3c8 100644 --- a/tests/by-util/test_mkdir.rs +++ b/tests/by-util/test_mkdir.rs @@ -1,12 +1,12 @@ use crate::common::util::*; -static TEST_DIR1: &'static str = "mkdir_test1"; -static TEST_DIR2: &'static str = "mkdir_test2"; -static TEST_DIR3: &'static str = "mkdir_test3"; -static TEST_DIR4: &'static str = "mkdir_test4/mkdir_test4_1"; -static TEST_DIR5: &'static str = "mkdir_test5/mkdir_test5_1"; -static TEST_DIR6: &'static str = "mkdir_test6"; -static TEST_FILE7: &'static str = "mkdir_test7"; +static TEST_DIR1: &str = "mkdir_test1"; +static TEST_DIR2: &str = "mkdir_test2"; +static TEST_DIR3: &str = "mkdir_test3"; +static TEST_DIR4: &str = "mkdir_test4/mkdir_test4_1"; +static TEST_DIR5: &str = "mkdir_test5/mkdir_test5_1"; +static TEST_DIR6: &str = "mkdir_test6"; +static TEST_FILE7: &str = "mkdir_test7"; #[test] fn test_mkdir_mkdir() { diff --git a/tests/by-util/test_mkfifo.rs b/tests/by-util/test_mkfifo.rs index 651491045..318a2ea5d 100644 --- a/tests/by-util/test_mkfifo.rs +++ b/tests/by-util/test_mkfifo.rs @@ -1 +1,45 @@ -// ToDO: add tests +use crate::common::util::*; + +#[test] +fn test_create_fifo_missing_operand() { + new_ucmd!().fails().stderr_is("mkfifo: missing operand"); +} + +#[test] +fn test_create_one_fifo() { + new_ucmd!().arg("abc").succeeds(); +} + +#[test] +fn test_create_one_fifo_with_invalid_mode() { + new_ucmd!() + .arg("abcd") + .arg("-m") + .arg("invalid") + .fails() + .stderr_contains("invalid mode"); +} + +#[test] +fn test_create_multiple_fifos() { + new_ucmd!() + .arg("abcde") + .arg("def") + .arg("sed") + .arg("dum") + .succeeds(); +} + +#[test] +fn test_create_one_fifo_with_mode() { + new_ucmd!().arg("abcde").arg("-m600").succeeds(); +} + +#[test] +fn test_create_one_fifo_already_exists() { + new_ucmd!() + .arg("abcdef") + .arg("abcdef") + .fails() + .stderr_is("mkfifo: cannot create fifo 'abcdef': File exists"); +} diff --git a/tests/by-util/test_mknod.rs b/tests/by-util/test_mknod.rs index 651491045..1d39372ac 100644 --- a/tests/by-util/test_mknod.rs +++ b/tests/by-util/test_mknod.rs @@ -1 +1,124 @@ -// ToDO: add tests +use crate::common::util::*; + +#[cfg(not(windows))] +#[test] +fn test_mknod_help() { + new_ucmd!() + .arg("--help") + .succeeds() + .no_stderr() + .stdout_contains("USAGE:"); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_version() { + assert!(new_ucmd!() + .arg("--version") + .succeeds() + .no_stderr() + .stdout_str() + .starts_with("mknod")); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_fifo_default_writable() { + let ts = TestScenario::new(util_name!()); + ts.ucmd().arg("test_file").arg("p").succeeds(); + assert!(ts.fixtures.is_fifo("test_file")); + assert!(!ts.fixtures.metadata("test_file").permissions().readonly()); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_fifo_mnemonic_usage() { + let ts = TestScenario::new(util_name!()); + ts.ucmd().arg("test_file").arg("pipe").succeeds(); + assert!(ts.fixtures.is_fifo("test_file")); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_fifo_read_only() { + let ts = TestScenario::new(util_name!()); + ts.ucmd() + .arg("-m") + .arg("a=r") + .arg("test_file") + .arg("p") + .succeeds(); + assert!(ts.fixtures.is_fifo("test_file")); + assert!(ts.fixtures.metadata("test_file").permissions().readonly()); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_fifo_invalid_extra_operand() { + new_ucmd!() + .arg("test_file") + .arg("p") + .arg("1") + .arg("2") + .fails() + .stderr_contains(&"Fifos do not have major and minor device numbers"); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_character_device_requires_major_and_minor() { + new_ucmd!() + .arg("test_file") + .arg("c") + .fails() + .status_code(1) + .stderr_contains(&"Special files require major and minor device numbers."); + new_ucmd!() + .arg("test_file") + .arg("c") + .arg("1") + .fails() + .status_code(1) + .stderr_contains(&"Special files require major and minor device numbers."); + new_ucmd!() + .arg("test_file") + .arg("c") + .arg("1") + .arg("c") + .fails() + .status_code(1) + .stderr_contains(&"Invalid value for ''"); + new_ucmd!() + .arg("test_file") + .arg("c") + .arg("c") + .arg("1") + .fails() + .status_code(1) + .stderr_contains(&"Invalid value for ''"); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_invalid_arg() { + new_ucmd!() + .arg("--foo") + .fails() + .status_code(1) + .no_stdout() + .stderr_contains(&"Found argument '--foo' which wasn't expected"); +} + +#[test] +#[cfg(not(windows))] +fn test_mknod_invalid_mode() { + new_ucmd!() + .arg("--mode") + .arg("rw") + .arg("test_file") + .arg("p") + .fails() + .no_stdout() + .status_code(1) + .stderr_contains(&"invalid mode"); +} diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index 2639a2c2f..e824df061 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -1,21 +1,26 @@ +// spell-checker:ignore (words) gpghome + use crate::common::util::*; use std::path::PathBuf; use tempfile::tempdir; -static TEST_TEMPLATE1: &'static str = "tempXXXXXX"; -static TEST_TEMPLATE2: &'static str = "temp"; -static TEST_TEMPLATE3: &'static str = "tempX"; -static TEST_TEMPLATE4: &'static str = "tempXX"; -static TEST_TEMPLATE5: &'static str = "tempXXX"; -static TEST_TEMPLATE6: &'static str = "tempXXXlate"; -static TEST_TEMPLATE7: &'static str = "XXXtemplate"; +static TEST_TEMPLATE1: &str = "tempXXXXXX"; +static TEST_TEMPLATE2: &str = "temp"; +static TEST_TEMPLATE3: &str = "tempX"; +static TEST_TEMPLATE4: &str = "tempXX"; +static TEST_TEMPLATE5: &str = "tempXXX"; +static TEST_TEMPLATE6: &str = "tempXXXlate"; // spell-checker:disable-line +static TEST_TEMPLATE7: &str = "XXXtemplate"; // spell-checker:disable-line #[cfg(unix)] -static TEST_TEMPLATE8: &'static str = "tempXXXl/ate"; +static TEST_TEMPLATE8: &str = "tempXXXl/ate"; #[cfg(windows)] -static TEST_TEMPLATE8: &'static str = "tempXXXl\\ate"; +static TEST_TEMPLATE8: &str = "tempXXXl\\ate"; -const TMPDIR: &'static str = "TMPDIR"; +#[cfg(not(windows))] +const TMPDIR: &str = "TMPDIR"; +#[cfg(windows)] +const TMPDIR: &str = "TMP"; #[test] fn test_mktemp_mktemp() { @@ -113,17 +118,15 @@ fn test_mktemp_mktemp_t() { .arg("-t") .arg(TEST_TEMPLATE7) .succeeds(); - let result = scene + scene .ucmd() .env(TMPDIR, &pathname) .arg("-t") .arg(TEST_TEMPLATE8) - .fails(); - println!("stdout {}", result.stdout); - println!("stderr {}", result.stderr); - assert!(result - .stderr - .contains("error: suffix cannot contain any path separators")); + .fails() + .no_stdout() + .stderr_contains("invalid suffix") + .stderr_contains("contains directory separator"); } #[test] @@ -387,14 +390,12 @@ fn test_mktemp_tmpdir_one_arg() { let scene = TestScenario::new(util_name!()); let result = scene - .ucmd() + .ucmd_keepenv() .arg("--tmpdir") .arg("apt-key-gpghome.XXXXXXXXXX") .succeeds(); - println!("stdout {}", result.stdout); - println!("stderr {}", result.stderr); - assert!(result.stdout.contains("apt-key-gpghome.")); - assert!(PathBuf::from(result.stdout.trim()).is_file()); + result.no_stderr().stdout_contains("apt-key-gpghome."); + assert!(PathBuf::from(result.stdout_str().trim()).is_file()); } #[test] @@ -402,13 +403,11 @@ fn test_mktemp_directory_tmpdir() { let scene = TestScenario::new(util_name!()); let result = scene - .ucmd() + .ucmd_keepenv() .arg("--directory") .arg("--tmpdir") .arg("apt-key-gpghome.XXXXXXXXXX") .succeeds(); - println!("stdout {}", result.stdout); - println!("stderr {}", result.stderr); - assert!(result.stdout.contains("apt-key-gpghome.")); - assert!(PathBuf::from(result.stdout.trim()).is_dir()); + result.no_stderr().stdout_contains("apt-key-gpghome."); + assert!(PathBuf::from(result.stdout_str().trim()).is_dir()); } diff --git a/tests/by-util/test_more.rs b/tests/by-util/test_more.rs index 3b7cfa9a8..9b28ee24e 100644 --- a/tests/by-util/test_more.rs +++ b/tests/by-util/test_more.rs @@ -2,7 +2,24 @@ use crate::common::util::*; #[test] fn test_more_no_arg() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - assert!(!result.success); + // Reading from stdin is now supported, so this must succeed + if atty::is(atty::Stream::Stdout) { + new_ucmd!().succeeds(); + } else { + } +} + +#[test] +fn test_more_dir_arg() { + // Run the test only if there's a valid terminal, else do nothing + // Maybe we could capture the error, i.e. "Device not found" in that case + // but I am leaving this for later + if atty::is(atty::Stream::Stdout) { + let result = new_ucmd!().arg(".").run(); + result.failure(); + const EXPECTED_ERROR_MESSAGE: &str = + "more: '.' is a directory.\nTry 'more --help' for more information."; + assert_eq!(result.stderr_str().trim(), EXPECTED_ERROR_MESSAGE); + } else { + } } diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index 0caeb1ef1..02c65f68d 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -82,7 +82,7 @@ fn test_mv_strip_slashes() { let dir = "test_mv_strip_slashes_dir"; let file = "test_mv_strip_slashes_file"; let mut source = file.to_owned(); - source.push_str("/"); + source.push('/'); at.mkdir(dir); at.touch(file); @@ -171,7 +171,7 @@ fn test_mv_interactive() { .arg("-i") .arg(file_a) .arg(file_b) - .pipe_in("Yesh") + .pipe_in("Yesh") // spell-checker:disable-line .succeeds() .no_stderr(); @@ -251,6 +251,40 @@ fn test_mv_simple_backup() { assert!(at.file_exists(&format!("{}~", file_b))); } +#[test] +fn test_mv_simple_backup_with_file_extension() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_simple_backup_file_a.txt"; + let file_b = "test_mv_simple_backup_file_b.txt"; + + at.touch(file_a); + at.touch(file_b); + ucmd.arg("-b") + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(!at.file_exists(file_a)); + assert!(at.file_exists(file_b)); + assert!(at.file_exists(&format!("{}~", file_b))); +} + +#[test] +fn test_mv_arg_backup_arg_first() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_simple_backup_file_a"; + let file_b = "test_mv_simple_backup_file_b"; + + at.touch(file_a); + at.touch(file_b); + ucmd.arg("--backup").arg(file_a).arg(file_b).succeeds(); + + assert!(!at.file_exists(file_a)); + assert!(at.file_exists(file_b)); + assert!(at.file_exists(&format!("{}~", file_b))); +} + #[test] fn test_mv_custom_backup_suffix() { let (at, mut ucmd) = at_and_ucmd!(); @@ -293,7 +327,7 @@ fn test_mv_custom_backup_suffix_via_env() { } #[test] -fn test_mv_backup_numbering() { +fn test_mv_backup_numbered_with_t() { let (at, mut ucmd) = at_and_ucmd!(); let file_a = "test_mv_backup_numbering_file_a"; let file_b = "test_mv_backup_numbering_file_b"; @@ -311,6 +345,25 @@ fn test_mv_backup_numbering() { assert!(at.file_exists(&format!("{}.~1~", file_b))); } +#[test] +fn test_mv_backup_numbered() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_backup_numbering_file_a"; + let file_b = "test_mv_backup_numbering_file_b"; + + at.touch(file_a); + at.touch(file_b); + ucmd.arg("--backup=numbered") + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(!at.file_exists(file_a)); + assert!(at.file_exists(file_b)); + assert!(at.file_exists(&format!("{}.~1~", file_b))); +} + #[test] fn test_mv_backup_existing() { let (at, mut ucmd) = at_and_ucmd!(); @@ -330,6 +383,67 @@ fn test_mv_backup_existing() { assert!(at.file_exists(&format!("{}~", file_b))); } +#[test] +fn test_mv_backup_nil() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_backup_numbering_file_a"; + let file_b = "test_mv_backup_numbering_file_b"; + + at.touch(file_a); + at.touch(file_b); + ucmd.arg("--backup=nil") + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(!at.file_exists(file_a)); + assert!(at.file_exists(file_b)); + assert!(at.file_exists(&format!("{}~", file_b))); +} + +#[test] +fn test_mv_numbered_if_existing_backup_existing() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_backup_numbering_file_a"; + let file_b = "test_mv_backup_numbering_file_b"; + let file_b_backup = "test_mv_backup_numbering_file_b.~1~"; + + at.touch(file_a); + at.touch(file_b); + at.touch(file_b_backup); + ucmd.arg("--backup=existing") + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file_b)); + assert!(at.file_exists(file_b_backup)); + assert!(at.file_exists(&*format!("{}.~2~", file_b))); +} + +#[test] +fn test_mv_numbered_if_existing_backup_nil() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_backup_numbering_file_a"; + let file_b = "test_mv_backup_numbering_file_b"; + let file_b_backup = "test_mv_backup_numbering_file_b.~1~"; + + at.touch(file_a); + at.touch(file_b); + at.touch(file_b_backup); + ucmd.arg("--backup=nil") + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file_b)); + assert!(at.file_exists(file_b_backup)); + assert!(at.file_exists(&*format!("{}.~2~", file_b))); +} + #[test] fn test_mv_backup_simple() { let (at, mut ucmd) = at_and_ucmd!(); @@ -349,6 +463,25 @@ fn test_mv_backup_simple() { assert!(at.file_exists(&format!("{}~", file_b))); } +#[test] +fn test_mv_backup_never() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_backup_numbering_file_a"; + let file_b = "test_mv_backup_numbering_file_b"; + + at.touch(file_a); + at.touch(file_b); + ucmd.arg("--backup=never") + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(!at.file_exists(file_a)); + assert!(at.file_exists(file_b)); + assert!(at.file_exists(&format!("{}~", file_b))); +} + #[test] fn test_mv_backup_none() { let (at, mut ucmd) = at_and_ucmd!(); @@ -369,17 +502,14 @@ fn test_mv_backup_none() { } #[test] -fn test_mv_existing_backup() { +fn test_mv_backup_off() { let (at, mut ucmd) = at_and_ucmd!(); - let file_a = "test_mv_existing_backup_file_a"; - let file_b = "test_mv_existing_backup_file_b"; - let file_b_backup = "test_mv_existing_backup_file_b.~1~"; - let resulting_backup = "test_mv_existing_backup_file_b.~2~"; + let file_a = "test_mv_backup_numbering_file_a"; + let file_b = "test_mv_backup_numbering_file_b"; at.touch(file_a); at.touch(file_b); - at.touch(file_b_backup); - ucmd.arg("--backup=nil") + ucmd.arg("--backup=off") .arg(file_a) .arg(file_b) .succeeds() @@ -387,8 +517,19 @@ fn test_mv_existing_backup() { assert!(!at.file_exists(file_a)); assert!(at.file_exists(file_b)); - assert!(at.file_exists(file_b_backup)); - assert!(at.file_exists(resulting_backup)); + assert!(!at.file_exists(&format!("{}~", file_b))); +} + +#[test] +fn test_mv_backup_no_clobber_conflicting_options() { + let (_, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("--backup") + .arg("--no-clobber") + .arg("file1") + .arg("file2") + .fails() + .stderr_is("mv: options --backup and --no-clobber are mutually exclusive\nTry 'mv --help' for more information."); } #[test] @@ -472,20 +613,13 @@ fn test_mv_overwrite_nonempty_dir() { at.touch(dummy); // Not same error as GNU; the error message is a rust builtin // TODO: test (and implement) correct error message (or at least decide whether to do so) - // Current: "mv: error: couldn't rename path (Directory not empty; from=a; to=b)" - // GNU: "mv: cannot move ‘a’ to ‘b’: Directory not empty" + // Current: "mv: couldn't rename path (Directory not empty; from=a; to=b)" + // GNU: "mv: cannot move 'a' to 'b': Directory not empty" // Verbose output for the move should not be shown on failure - assert!( - ucmd.arg("-vT") - .arg(dir_a) - .arg(dir_b) - .fails() - .no_stdout() - .stderr - .len() - > 0 - ); + let result = ucmd.arg("-vT").arg(dir_a).arg(dir_b).fails(); + result.no_stdout(); + assert!(!result.stderr_str().is_empty()); assert!(at.dir_exists(dir_a)); assert!(at.dir_exists(dir_b)); @@ -504,7 +638,7 @@ fn test_mv_backup_dir() { .arg(dir_b) .succeeds() .stdout_only(format!( - "‘{}’ -> ‘{}’ (backup: ‘{}~’)\n", + "'{}' -> '{}' (backup: '{}~')\n", dir_a, dir_b, dir_b )); @@ -526,19 +660,19 @@ fn test_mv_errors() { // $ mv -T -t a b // mv: cannot combine --target-directory (-t) and --no-target-directory (-T) - let result = scene + scene .ucmd() .arg("-T") .arg("-t") .arg(dir) .arg(file_a) .arg(file_b) - .fails(); - assert!(result.stderr.contains("cannot be used with")); + .fails() + .stderr_contains("cannot be used with"); // $ at.touch file && at.mkdir dir // $ mv -T file dir - // err == mv: cannot overwrite directory ‘dir’ with non-directory + // err == mv: cannot overwrite directory 'dir' with non-directory scene .ucmd() .arg("-T") @@ -546,14 +680,20 @@ fn test_mv_errors() { .arg(dir) .fails() .stderr_is(format!( - "mv: error: cannot overwrite directory ‘{}’ with non-directory\n", + "mv: cannot overwrite directory '{}' with non-directory\n", dir )); // $ at.mkdir dir && at.touch file // $ mv dir file - // err == mv: cannot overwrite non-directory ‘file’ with directory ‘dir’ - assert!(scene.ucmd().arg(dir).arg(file_a).fails().stderr.len() > 0); + // err == mv: cannot overwrite non-directory 'file' with directory 'dir' + assert!(!scene + .ucmd() + .arg(dir) + .arg(file_a) + .fails() + .stderr_str() + .is_empty()); } #[test] @@ -573,7 +713,7 @@ fn test_mv_verbose() { .arg(file_a) .arg(file_b) .succeeds() - .stdout_only(format!("‘{}’ -> ‘{}’\n", file_a, file_b)); + .stdout_only(format!("'{}' -> '{}'\n", file_a, file_b)); at.touch(file_a); scene @@ -583,11 +723,29 @@ fn test_mv_verbose() { .arg(file_b) .succeeds() .stdout_only(format!( - "‘{}’ -> ‘{}’ (backup: ‘{}~’)\n", + "'{}' -> '{}' (backup: '{}~')\n", file_a, file_b, file_b )); } +#[test] +#[cfg(target_os = "linux")] // mkdir does not support -m on windows. Freebsd doesn't return a permission error either. +fn test_mv_permission_error() { + let scene = TestScenario::new("mkdir"); + let folder1 = "bar"; + let folder2 = "foo"; + let folder_to_move = "bar/foo"; + scene.ucmd().arg("-m444").arg(folder1).succeeds(); + scene.ucmd().arg("-m777").arg(folder2).succeeds(); + + scene + .ccmd("mv") + .arg(folder2) + .arg(folder_to_move) + .fails() + .stderr_contains("Permission denied"); +} + // Todo: // $ at.touch a b @@ -598,5 +756,5 @@ fn test_mv_verbose() { // -r--r--r-- 1 user user 0 okt 25 11:21 b // $ // $ mv -v a b -// mv: try to overwrite ‘b’, overriding mode 0444 (r--r--r--)? y -// ‘a’ -> ‘b’ +// mv: try to overwrite 'b', overriding mode 0444 (r--r--r--)? y +// 'a' -> 'b' diff --git a/tests/by-util/test_nice.rs b/tests/by-util/test_nice.rs index 651491045..25886de78 100644 --- a/tests/by-util/test_nice.rs +++ b/tests/by-util/test_nice.rs @@ -1 +1,58 @@ -// ToDO: add tests +use crate::common::util::*; + +#[test] +fn test_get_current_niceness() { + // NOTE: this assumes the test suite is being run with a default niceness + // of 0, which may not necessarily be true + new_ucmd!().run().stdout_is("0\n"); +} + +#[test] +fn test_negative_adjustment() { + // This assumes the test suite is run as a normal (non-root) user, and as + // such attempting to set a negative niceness value will be rejected by + // the OS. If it gets denied, then we know a negative value was parsed + // correctly. + + let res = new_ucmd!().args(&["-n", "-1", "true"]).run(); + assert!(res + .stderr_str() + .starts_with("nice: warning: setpriority: Permission denied")); // spell-checker:disable-line +} + +#[test] +fn test_adjustment_with_no_command_should_error() { + new_ucmd!() + .args(&["-n", "19"]) + .run() + .stderr_is("nice: A command must be given with an adjustment.\nTry \"nice --help\" for more information.\n"); +} + +#[test] +fn test_command_with_no_adjustment() { + new_ucmd!().args(&["echo", "a"]).run().stdout_is("a\n"); +} + +#[test] +fn test_command_with_no_args() { + new_ucmd!() + .args(&["-n", "19", "echo"]) + .run() + .stdout_is("\n"); +} + +#[test] +fn test_command_with_args() { + new_ucmd!() + .args(&["-n", "19", "echo", "a", "b", "c"]) + .run() + .stdout_is("a b c\n"); +} + +#[test] +fn test_command_where_command_takes_n_flag() { + new_ucmd!() + .args(&["-n", "19", "echo", "-n", "a"]) + .run() + .stdout_is("a"); +} diff --git a/tests/by-util/test_nl.rs b/tests/by-util/test_nl.rs index fca73c37b..ce3014311 100644 --- a/tests/by-util/test_nl.rs +++ b/tests/by-util/test_nl.rs @@ -1,7 +1,7 @@ use crate::common::util::*; #[test] -fn test_stdin_nonewline() { +fn test_stdin_no_newline() { new_ucmd!() .pipe_in("No Newline") .run() @@ -23,8 +23,8 @@ fn test_padding_without_overflow() { .run() .stdout_is( "000001xL1\n001001xL2\n002001xL3\n003001xL4\n004001xL5\n005001xL6\n006001xL7\n0070\ - 01xL8\n008001xL9\n009001xL10\n010001xL11\n011001xL12\n012001xL13\n013001xL14\n014\ - 001xL15\n", + 01xL8\n008001xL9\n009001xL10\n010001xL11\n011001xL12\n012001xL13\n013001xL14\n014\ + 001xL15\n", ); } @@ -35,25 +35,26 @@ fn test_padding_with_overflow() { .run() .stdout_is( "0001xL1\n1001xL2\n2001xL3\n3001xL4\n4001xL5\n5001xL6\n6001xL7\n7001xL8\n8001xL9\n\ - 9001xL10\n10001xL11\n11001xL12\n12001xL13\n13001xL14\n14001xL15\n", + 9001xL10\n10001xL11\n11001xL12\n12001xL13\n13001xL14\n14001xL15\n", ); } #[test] fn test_sections_and_styles() { + // spell-checker:disable for &(fixture, output) in &[ ( "section.txt", "\nHEADER1\nHEADER2\n\n1 |BODY1\n2 \ - |BODY2\n\nFOOTER1\nFOOTER2\n\nNEXTHEADER1\nNEXTHEADER2\n\n1 \ - |NEXTBODY1\n2 |NEXTBODY2\n\nNEXTFOOTER1\nNEXTFOOTER2\n", + |BODY2\n\nFOOTER1\nFOOTER2\n\nNEXTHEADER1\nNEXTHEADER2\n\n1 \ + |NEXTBODY1\n2 |NEXTBODY2\n\nNEXTFOOTER1\nNEXTFOOTER2\n", ), ( "joinblanklines.txt", "1 |Nonempty\n2 |Nonempty\n3 |Followed by 10x empty\n\n\n\n\n4 \ - |\n\n\n\n\n5 |\n6 |Followed by 5x empty\n\n\n\n\n7 |\n8 \ - |Followed by 4x empty\n\n\n\n\n9 |Nonempty\n10 |Nonempty\n11 \ - |Nonempty.\n", + |\n\n\n\n\n5 |\n6 |Followed by 5x empty\n\n\n\n\n7 |\n8 \ + |Followed by 4x empty\n\n\n\n\n9 |Nonempty\n10 |Nonempty\n11 \ + |Nonempty.\n", ), ] { new_ucmd!() @@ -63,4 +64,5 @@ fn test_sections_and_styles() { .run() .stdout_is(output); } + // spell-checker:enable } diff --git a/tests/by-util/test_nohup.rs b/tests/by-util/test_nohup.rs index 651491045..b98ae007c 100644 --- a/tests/by-util/test_nohup.rs +++ b/tests/by-util/test_nohup.rs @@ -1 +1,19 @@ -// ToDO: add tests +use crate::common::util::*; +use std::thread::sleep; + +// General observation: nohup.out will not be created in tests run by cargo test +// because stdin/stdout is not attached to a TTY. +// All that can be tested is the side-effects. + +#[test] +#[cfg(any(target_os = "linux", target_os = "freebsd", target_vendor = "apple"))] +fn test_nohup_multiple_args_and_flags() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.args(&["touch", "-t", "1006161200", "file1", "file2"]) + .succeeds(); + sleep(std::time::Duration::from_millis(10)); + + assert!(at.file_exists("file1")); + assert!(at.file_exists("file2")); +} diff --git a/tests/by-util/test_nproc.rs b/tests/by-util/test_nproc.rs index 055b4890d..abf758829 100644 --- a/tests/by-util/test_nproc.rs +++ b/tests/by-util/test_nproc.rs @@ -2,54 +2,46 @@ use crate::common::util::*; #[test] fn test_nproc() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - assert!(result.success); - let nproc: u8 = result.stdout.trim().parse().unwrap(); + let nproc: u8 = new_ucmd!().succeeds().stdout_str().trim().parse().unwrap(); assert!(nproc > 0); } #[test] fn test_nproc_all_omp() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("--all").run(); - assert!(result.success); - let nproc: u8 = result.stdout.trim().parse().unwrap(); + let result = new_ucmd!().arg("--all").succeeds(); + + let nproc: u8 = result.stdout_str().trim().parse().unwrap(); assert!(nproc > 0); let result = TestScenario::new(util_name!()) .ucmd_keepenv() .env("OMP_NUM_THREADS", "1") - .run(); - assert!(result.success); - let nproc_omp: u8 = result.stdout.trim().parse().unwrap(); + .succeeds(); + + let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap(); assert!(nproc - 1 == nproc_omp); let result = TestScenario::new(util_name!()) .ucmd_keepenv() .env("OMP_NUM_THREADS", "1") // Has no effect .arg("--all") - .run(); - assert!(result.success); - let nproc_omp: u8 = result.stdout.trim().parse().unwrap(); + .succeeds(); + let nproc_omp: u8 = result.stdout_str().trim().parse().unwrap(); assert!(nproc == nproc_omp); } #[test] fn test_nproc_ignore() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - assert!(result.success); - let nproc: u8 = result.stdout.trim().parse().unwrap(); + let result = new_ucmd!().succeeds(); + let nproc: u8 = result.stdout_str().trim().parse().unwrap(); if nproc > 1 { // Ignore all CPU but one let result = TestScenario::new(util_name!()) .ucmd_keepenv() .arg("--ignore") .arg((nproc - 1).to_string()) - .run(); - assert!(result.success); - let nproc: u8 = result.stdout.trim().parse().unwrap(); + .succeeds(); + let nproc: u8 = result.stdout_str().trim().parse().unwrap(); assert!(nproc == 1); } } diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index c22db3bf5..336b0f7cd 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -1,3 +1,5 @@ +// spell-checker:ignore (paths) gnutest + use crate::common::util::*; #[test] @@ -33,7 +35,7 @@ fn test_from_iec_i_requires_suffix() { new_ucmd!() .args(&["--from=iec-i", "1024"]) .fails() - .stderr_is("numfmt: missing 'i' suffix in input: ‘1024’ (e.g Ki/Mi/Gi)"); + .stderr_is("numfmt: missing 'i' suffix in input: '1024' (e.g Ki/Mi/Gi)"); } #[test] @@ -121,7 +123,7 @@ fn test_header_error_if_non_numeric() { new_ucmd!() .args(&["--header=two"]) .run() - .stderr_is("numfmt: invalid header value ‘two’"); + .stderr_is("numfmt: invalid header value 'two'"); } #[test] @@ -129,7 +131,7 @@ fn test_header_error_if_0() { new_ucmd!() .args(&["--header=0"]) .run() - .stderr_is("numfmt: invalid header value ‘0’"); + .stderr_is("numfmt: invalid header value '0'"); } #[test] @@ -137,7 +139,7 @@ fn test_header_error_if_negative() { new_ucmd!() .args(&["--header=-3"]) .run() - .stderr_is("numfmt: invalid header value ‘-3’"); + .stderr_is("numfmt: invalid header value '-3'"); } #[test] @@ -185,7 +187,7 @@ fn test_should_report_invalid_empty_number_on_empty_stdin() { .args(&["--from=auto"]) .pipe_in("\n") .run() - .stderr_is("numfmt: invalid number: ‘’\n"); + .stderr_is("numfmt: invalid number: ''\n"); } #[test] @@ -194,7 +196,7 @@ fn test_should_report_invalid_empty_number_on_blank_stdin() { .args(&["--from=auto"]) .pipe_in(" \t \n") .run() - .stderr_is("numfmt: invalid number: ‘’\n"); + .stderr_is("numfmt: invalid number: ''\n"); } #[test] @@ -203,14 +205,14 @@ fn test_should_report_invalid_suffix_on_stdin() { .args(&["--from=auto"]) .pipe_in("1k") .run() - .stderr_is("numfmt: invalid suffix in input: ‘1k’\n"); + .stderr_is("numfmt: invalid suffix in input: '1k'\n"); // GNU numfmt reports this one as “invalid number” new_ucmd!() .args(&["--from=auto"]) .pipe_in("NaN") .run() - .stderr_is("numfmt: invalid suffix in input: ‘NaN’\n"); + .stderr_is("numfmt: invalid suffix in input: 'NaN'\n"); } #[test] @@ -220,7 +222,7 @@ fn test_should_report_invalid_number_with_interior_junk() { .args(&["--from=auto"]) .pipe_in("1x0K") .run() - .stderr_is("numfmt: invalid number: ‘1x0K’\n"); + .stderr_is("numfmt: invalid number: '1x0K'\n"); } #[test] @@ -231,7 +233,7 @@ fn test_should_skip_leading_space_from_stdin() { .run() .stdout_is("2048\n"); - // multiline + // multi-line new_ucmd!() .args(&["--from=auto"]) .pipe_in("\t1Ki\n 2K") @@ -281,6 +283,7 @@ fn test_leading_whitespace_in_free_argument_should_imply_padding() { } #[test] +#[ignore] fn test_should_calculate_implicit_padding_per_free_argument() { new_ucmd!() .args(&["--from=auto", " 1Ki", " 2K"]) @@ -383,3 +386,122 @@ fn test_field_df_example() { .succeeds() .stdout_is_fixture("df_expected.txt"); } + +#[test] +fn test_delimiter_must_not_be_empty() { + new_ucmd!().args(&["-d"]).fails(); +} + +#[test] +fn test_delimiter_must_not_be_more_than_one_character() { + new_ucmd!() + .args(&["--delimiter", "sad"]) + .fails() + .stderr_is("numfmt: the delimiter must be a single character"); +} + +#[test] +fn test_delimiter_only() { + new_ucmd!() + .args(&["-d", ","]) + .pipe_in("1234,56") + .succeeds() + .stdout_only("1234,56\n"); +} + +#[test] +fn test_line_is_field_with_no_delimiter() { + new_ucmd!() + .args(&["-d,", "--to=iec"]) + .pipe_in("123456") + .succeeds() + .stdout_only("121K\n"); +} + +#[test] +fn test_delimiter_to_si() { + new_ucmd!() + .args(&["-d=,", "--to=si"]) + .pipe_in("1234,56") + .succeeds() + .stdout_only("1.3K,56\n"); +} + +#[test] +fn test_delimiter_skips_leading_whitespace() { + new_ucmd!() + .args(&["-d=,", "--to=si"]) + .pipe_in(" \t 1234,56") + .succeeds() + .stdout_only("1.3K,56\n"); +} + +#[test] +fn test_delimiter_preserves_leading_whitespace_in_unselected_fields() { + new_ucmd!() + .args(&["-d=|", "--to=si"]) + .pipe_in(" 1000| 2000") + .succeeds() + .stdout_only("1.0K| 2000\n"); +} + +#[test] +fn test_delimiter_from_si() { + new_ucmd!() + .args(&["-d=,", "--from=si"]) + .pipe_in("1.2K,56") + .succeeds() + .stdout_only("1200,56\n"); +} + +#[test] +fn test_delimiter_overrides_whitespace_separator() { + // GNU numfmt reports this as “invalid suffix” + new_ucmd!() + .args(&["-d,"]) + .pipe_in("1 234,56") + .fails() + .stderr_is("numfmt: invalid number: '1 234'\n"); +} + +#[test] +fn test_delimiter_with_padding() { + new_ucmd!() + .args(&["-d=|", "--to=si", "--padding=5"]) + .pipe_in("1000|2000") + .succeeds() + .stdout_only(" 1.0K|2000\n"); +} + +#[test] +fn test_delimiter_with_padding_and_fields() { + new_ucmd!() + .args(&["-d=|", "--to=si", "--padding=5", "--field=-"]) + .pipe_in("1000|2000") + .succeeds() + .stdout_only(" 1.0K| 2.0K\n"); +} + +#[test] +fn test_round() { + for (method, exp) in &[ + ("from-zero", ["9.1K", "-9.1K", "9.1K", "-9.1K"]), + ("towards-zero", ["9.0K", "-9.0K", "9.0K", "-9.0K"]), + ("up", ["9.1K", "-9.0K", "9.1K", "-9.0K"]), + ("down", ["9.0K", "-9.1K", "9.0K", "-9.1K"]), + ("nearest", ["9.0K", "-9.0K", "9.1K", "-9.1K"]), + ] { + new_ucmd!() + .args(&[ + "--to=si", + &format!("--round={}", method), + "--", + "9001", + "-9001", + "9099", + "-9099", + ]) + .succeeds() + .stdout_only(exp.join("\n") + "\n"); + } +} diff --git a/tests/by-util/test_od.rs b/tests/by-util/test_od.rs index f3b766c26..33d7d4dc4 100644 --- a/tests/by-util/test_od.rs +++ b/tests/by-util/test_od.rs @@ -1,3 +1,8 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + extern crate unindent; use self::unindent::*; @@ -8,19 +13,20 @@ use std::fs::File; use std::io::Write; use std::path::Path; -// octal dump of 'abcdefghijklmnopqrstuvwxyz\n' -static ALPHA_OUT: &'static str = " +// octal dump of 'abcdefghijklmnopqrstuvwxyz\n' // spell-checker:disable-line +static ALPHA_OUT: &str = " 0000000 061141 062143 063145 064147 065151 066153 067155 070157 0000020 071161 072163 073165 074167 075171 000012 0000033 "; // XXX We could do a better job of ensuring that we have a fresh temp dir to ourselves, -// not a general one full of other proc's leftovers. +// not a general one full of other process leftovers. // Test that od can read one file and dump with default format #[test] fn test_file() { + // TODO: Can this be replaced by AtPath? use std::env; let temp = env::temp_dir(); let tmpdir = Path::new(&temp); @@ -28,20 +34,18 @@ fn test_file() { { let mut f = File::create(&file).unwrap(); + // spell-checker:disable-next-line if f.write_all(b"abcdefghijklmnopqrstuvwxyz\n").is_err() { panic!("Test setup failed - could not write file"); } } - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg(file.as_os_str()) - .run(); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, unindent(ALPHA_OUT)); - + .succeeds() + .no_stderr() + .stdout_is(unindent(ALPHA_OUT)); let _ = remove_file(file); } @@ -57,6 +61,7 @@ fn test_2files() { println!("number: {} letter:{}", n, a); } + // spell-checker:disable-next-line for &(path, data) in &[(&file1, "abcdefghijklmnop"), (&file2, "qrstuvwxyz\n")] { let mut f = File::create(&path).unwrap(); if f.write_all(data.as_bytes()).is_err() { @@ -64,16 +69,14 @@ fn test_2files() { } } - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg(file1.as_os_str()) .arg(file2.as_os_str()) - .run(); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, unindent(ALPHA_OUT)); - + .succeeds() + .no_stderr() + .stdout_is(unindent(ALPHA_OUT)); + // TODO: Handle errors? let _ = remove_file(file1); let _ = remove_file(file2); } @@ -83,24 +86,21 @@ fn test_2files() { fn test_no_file() { let temp = env::temp_dir(); let tmpdir = Path::new(&temp); - let file = tmpdir.join("}surely'none'would'thus'a'file'name"); + let file = tmpdir.join("}surely'none'would'thus'a'file'name"); // spell-checker:disable-line - let result = new_ucmd!().arg(file.as_os_str()).run(); - - assert!(!result.success); + new_ucmd!().arg(file.as_os_str()).fails(); } // Test that od reads from stdin instead of a file #[test] fn test_from_stdin() { - let input = "abcdefghijklmnopqrstuvwxyz\n"; - let result = new_ucmd!() + let input = "abcdefghijklmnopqrstuvwxyz\n"; // spell-checker:disable-line + new_ucmd!() .arg("--endian=little") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, unindent(ALPHA_OUT)); + .run_piped_stdin(input.as_bytes()) + .success() + .no_stderr() + .stdout_is(unindent(ALPHA_OUT)); } // Test that od reads from stdin and also from files @@ -111,6 +111,7 @@ fn test_from_mixed() { let file1 = tmpdir.join("test-1"); let file3 = tmpdir.join("test-3"); + // spell-checker:disable-next-line let (data1, data2, data3) = ("abcdefg", "hijklmnop", "qrstuvwxyz\n"); for &(path, data) in &[(&file1, data1), (&file3, data3)] { let mut f = File::create(&path).unwrap(); @@ -119,44 +120,40 @@ fn test_from_mixed() { } } - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg(file1.as_os_str()) .arg("-") .arg(file3.as_os_str()) - .run_piped_stdin(data2.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, unindent(ALPHA_OUT)); + .run_piped_stdin(data2.as_bytes()) + .success() + .no_stderr() + .stdout_is(unindent(ALPHA_OUT)); } #[test] fn test_multiple_formats() { - let input = "abcdefghijklmnopqrstuvwxyz\n"; - let result = new_ucmd!() + let input = "abcdefghijklmnopqrstuvwxyz\n"; // spell-checker:disable-line + new_ucmd!() .arg("-c") .arg("-b") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin(input.as_bytes()) + .success() + .no_stderr() + .stdout_is(unindent( " 0000000 a b c d e f g h i j k l m n o p 141 142 143 144 145 146 147 150 151 152 153 154 155 156 157 160 0000020 q r s t u v w x y z \\n 161 162 163 164 165 166 167 170 171 172 012 0000033 - " - ) - ); + ", + )); } #[test] fn test_dec() { + // spell-checker:ignore (words) 0xffu8 xffu let input = [ 0u8, 0u8, 1u8, 0u8, 2u8, 0u8, 3u8, 0u8, 0xffu8, 0x7fu8, 0x00u8, 0x80u8, 0x01u8, 0x80u8, ]; @@ -166,33 +163,33 @@ fn test_dec() { 0000016 ", ); - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg("-s") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] fn test_hex16() { let input: [u8; 9] = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]; + // spell-checker:disable let expected_output = unindent( " 0000000 2301 6745 ab89 efcd 00ff 0000011 ", ); - let result = new_ucmd!() + // spell-checker:enable + new_ucmd!() .arg("--endian=little") .arg("-x") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] @@ -204,14 +201,13 @@ fn test_hex32() { 0000011 ", ); - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg("-X") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] @@ -232,15 +228,14 @@ fn test_f16() { 0000016 ", ); - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg("-tf2") .arg("-w8") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] @@ -261,14 +256,13 @@ fn test_f32() { 0000034 ", ); - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg("-f") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] @@ -291,36 +285,31 @@ fn test_f64() { 0000050 ", ); - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg("-F") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] fn test_multibyte() { - let result = new_ucmd!() + new_ucmd!() .arg("-c") .arg("-w12") - .run_piped_stdin("Universität Tübingen \u{1B000}".as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin("Universität Tübingen \u{1B000}".as_bytes()) // spell-checker:disable-line + .success() + .no_stderr() + .stdout_is(unindent( " 0000000 U n i v e r s i t ä ** t 0000014 T ü ** b i n g e n \u{1B000} 0000030 ** ** ** 0000033 - " - ) - ); + ", + )); } #[test] @@ -334,11 +323,13 @@ fn test_width() { ", ); - let result = new_ucmd!().arg("-w4").arg("-v").run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + new_ucmd!() + .arg("-w4") + .arg("-v") + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] @@ -352,14 +343,13 @@ fn test_invalid_width() { ", ); - let result = new_ucmd!().arg("-w5").arg("-v").run_piped_stdin(&input[..]); - - assert_eq!( - result.stderr, - "od: warning: invalid width 5; using 2 instead\n" - ); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + new_ucmd!() + .arg("-w5") + .arg("-v") + .run_piped_stdin(&input[..]) + .success() + .stderr_is_bytes("od: warning: invalid width 5; using 2 instead\n".as_bytes()) + .stdout_is(expected_output); } #[test] @@ -373,14 +363,13 @@ fn test_zero_width() { ", ); - let result = new_ucmd!().arg("-w0").arg("-v").run_piped_stdin(&input[..]); - - assert_eq!( - result.stderr, - "od: warning: invalid width 0; using 2 instead\n" - ); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + new_ucmd!() + .arg("-w0") + .arg("-v") + .run_piped_stdin(&input[..]) + .success() + .stderr_is_bytes("od: warning: invalid width 0; using 2 instead\n".as_bytes()) + .stdout_is(expected_output); } #[test] @@ -392,11 +381,12 @@ fn test_width_without_value() { 0000050 "); - let result = new_ucmd!().arg("-w").run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + new_ucmd!() + .arg("-w") + .run_piped_stdin(&input[..]) + .success() + .no_stderr() + .stdout_is(expected_output); } #[test] @@ -421,15 +411,14 @@ fn test_suppress_duplicates() { ", ); - let result = new_ucmd!() + new_ucmd!() .arg("-w4") .arg("-O") .arg("-x") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] @@ -446,17 +435,16 @@ fn test_big_endian() { ", ); - let result = new_ucmd!() + new_ucmd!() .arg("--endian=big") .arg("-F") .arg("-f") .arg("-X") .arg("-x") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] @@ -474,16 +462,15 @@ fn test_alignment_Xxa() { ); // in this case the width of the -a (8-bit) determines the alignment for the other fields - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg("-X") .arg("-x") .arg("-a") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] @@ -500,19 +487,18 @@ fn test_alignment_Fx() { ); // in this case the width of the -F (64-bit) determines the alignment for the other field - let result = new_ucmd!() + new_ucmd!() .arg("--endian=little") .arg("-F") .arg("-x") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] -fn test_maxuint() { +fn test_max_uint() { let input = [0xFFu8; 8]; let expected_output = unindent( " @@ -528,16 +514,15 @@ fn test_maxuint() { ", ); - let result = new_ucmd!() + new_ucmd!() .arg("--format=o8") - .arg("-Oobtu8") + .arg("-Oobtu8") // spell-checker:disable-line .arg("-Dd") .arg("--format=u1") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] @@ -553,15 +538,14 @@ fn test_hex_offset() { ", ); - let result = new_ucmd!() + new_ucmd!() .arg("-Ax") .arg("-X") .arg("-X") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] @@ -577,83 +561,73 @@ fn test_dec_offset() { ", ); - let result = new_ucmd!() + new_ucmd!() .arg("-Ad") .arg("-X") .arg("-X") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] fn test_no_offset() { let input = [0u8; 31]; - const LINE: &'static str = " 00000000 00000000 00000000 00000000\n"; + const LINE: &str = " 00000000 00000000 00000000 00000000\n"; let expected_output = [LINE, LINE, LINE, LINE].join(""); - let result = new_ucmd!() + new_ucmd!() .arg("-An") .arg("-X") .arg("-X") - .run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, expected_output); + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(expected_output); } #[test] fn test_invalid_offset() { - let result = new_ucmd!().arg("-Ab").run(); - - assert!(!result.success); + new_ucmd!().arg("-Ab").fails(); } #[test] fn test_skip_bytes() { - let input = "abcdefghijklmnopq"; - let result = new_ucmd!() + let input = "abcdefghijklmnopq"; // spell-checker:disable-line + new_ucmd!() .arg("-c") .arg("--skip-bytes=5") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin(input.as_bytes()) + .no_stderr() + .success() + .stdout_is(unindent( " 0000005 f g h i j k l m n o p q 0000021 - " - ) - ); + ", + )); } #[test] fn test_skip_bytes_error() { let input = "12345"; - let result = new_ucmd!() + new_ucmd!() .arg("--skip-bytes=10") - .run_piped_stdin(input.as_bytes()); - - assert!(!result.success); + .run_piped_stdin(input.as_bytes()) + .failure(); } #[test] fn test_read_bytes() { - let input = "abcdefghijklmnopqrstuvwxyz\n12345678"; - let result = new_ucmd!() + let input = "abcdefghijklmnopqrstuvwxyz\n12345678"; // spell-checker:disable-line + new_ucmd!() .arg("--endian=little") .arg("--read-bytes=27") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!(result.stdout, unindent(ALPHA_OUT)); + .run_piped_stdin(input.as_bytes()) + .no_stderr() + .success() + .stdout_is(unindent(ALPHA_OUT)); } #[test] @@ -662,13 +636,12 @@ fn test_ascii_dump() { 0x00, 0x01, 0x0a, 0x0d, 0x10, 0x1f, 0x20, 0x61, 0x62, 0x63, 0x7d, 0x7e, 0x7f, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff, ]; - let result = new_ucmd!().arg("-tx1zacz").run_piped_stdin(&input[..]); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + new_ucmd!() + .arg("-tx1zacz") // spell-checker:disable-line + .run_piped_stdin(&input[..]) + .no_stderr() + .success() + .stdout_is(unindent( r" 0000000 00 01 0a 0d 10 1f 20 61 62 63 7d 7e 7f 80 90 a0 >...... abc}~....< nul soh nl cr dle us sp a b c } ~ del nul dle sp @@ -677,169 +650,145 @@ fn test_ascii_dump() { 0 @ P ` p del ** 300 320 340 360 377 >......< 0000026 - " - ) - ); + ", + )); } #[test] fn test_filename_parsing() { - // files "a" and "x" both exists, but are no filenames in the commandline below + // files "a" and "x" both exists, but are no filenames in the command line below // "-f" must be treated as a filename, it contains the text: minus lowercase f // so "-f" should not be interpreted as a formatting option. - let result = new_ucmd!() + new_ucmd!() .arg("--format") .arg("a") .arg("-A") .arg("x") .arg("--") .arg("-f") - .run(); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .succeeds() + .no_stderr() + .stdout_is(unindent( " 000000 m i n u s sp l o w e r c a s e sp 000010 f nl 000012 - " - ) - ); + ", + )); } #[test] fn test_stdin_offset() { - let input = "abcdefghijklmnopq"; - let result = new_ucmd!() + let input = "abcdefghijklmnopq"; // spell-checker:disable-line + new_ucmd!() .arg("-c") .arg("+5") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin(input.as_bytes()) + .no_stderr() + .success() + .stdout_is(unindent( " 0000005 f g h i j k l m n o p q 0000021 - " - ) - ); + ", + )); } #[test] fn test_file_offset() { - let result = new_ucmd!().arg("-c").arg("--").arg("-f").arg("10").run(); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + new_ucmd!() + .arg("-c") + .arg("--") + .arg("-f") + .arg("10") + .succeeds() + .no_stderr() + .stdout_is(unindent( r" 0000010 w e r c a s e f \n 0000022 - " - ) - ); + ", + )); } #[test] fn test_traditional() { // note gnu od does not align both lines - let input = "abcdefghijklmnopq"; - let result = new_ucmd!() + let input = "abcdefghijklmnopq"; // spell-checker:disable-line + new_ucmd!() .arg("--traditional") .arg("-a") .arg("-c") .arg("-") .arg("10") .arg("0") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin(input.as_bytes()) + .no_stderr() + .success() + .stdout_is(unindent( r" 0000010 (0000000) i j k l m n o p q i j k l m n o p q 0000021 (0000011) - " - ) - ); + ", + )); } #[test] fn test_traditional_with_skip_bytes_override() { // --skip-bytes is ignored in this case - let input = "abcdefghijklmnop"; - let result = new_ucmd!() + let input = "abcdefghijklmnop"; // spell-checker:disable-line + new_ucmd!() .arg("--traditional") .arg("--skip-bytes=10") .arg("-c") .arg("0") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin(input.as_bytes()) + .no_stderr() + .success() + .stdout_is(unindent( r" 0000000 a b c d e f g h i j k l m n o p 0000020 - " - ) - ); + ", + )); } #[test] fn test_traditional_with_skip_bytes_non_override() { // no offset specified in the traditional way, so --skip-bytes is used - let input = "abcdefghijklmnop"; - let result = new_ucmd!() + let input = "abcdefghijklmnop"; // spell-checker:disable-line + new_ucmd!() .arg("--traditional") .arg("--skip-bytes=10") .arg("-c") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin(input.as_bytes()) + .no_stderr() + .success() + .stdout_is(unindent( r" 0000012 k l m n o p 0000020 - " - ) - ); + ", + )); } #[test] fn test_traditional_error() { // file "0" exists - don't fail on that, but --traditional only accepts a single input - let result = new_ucmd!() + new_ucmd!() .arg("--traditional") .arg("0") .arg("0") .arg("0") .arg("0") - .run(); - - assert!(!result.success); + .fails(); } #[test] fn test_traditional_only_label() { - let input = "abcdefghijklmnopqrstuvwxyz"; - let result = new_ucmd!() + let input = "abcdefghijklmnopqrstuvwxyz"; // spell-checker:disable-line + new_ucmd!() .arg("-An") .arg("--traditional") .arg("-a") @@ -847,20 +796,53 @@ fn test_traditional_only_label() { .arg("-") .arg("10") .arg("0x10") - .run_piped_stdin(input.as_bytes()); - - assert_empty_stderr!(result); - assert!(result.success); - assert_eq!( - result.stdout, - unindent( + .run_piped_stdin(input.as_bytes()) + .no_stderr() + .success() + .stdout_is(unindent( r" (0000020) i j k l m n o p q r s t u v w x i j k l m n o p q r s t u v w x (0000040) y z y z (0000042) - " - ) - ); + ", + )); +} + +#[test] +fn test_od_invalid_bytes() { + const INVALID_SIZE: &str = "1fb4t"; + const BIG_SIZE: &str = "1Y"; + + // NOTE: + // GNU's od (8.32) with option '--width' does not accept 'Y' as valid suffix. + // According to the man page it should be valid in the same way it is valid for + // '--read-bytes' and '--skip-bytes'. + + let options = [ + "--read-bytes", + "--skip-bytes", + "--width", + // "--strings", // TODO: consider testing here once '--strings' is implemented + ]; + for option in &options { + new_ucmd!() + .arg(format!("{}={}", option, INVALID_SIZE)) + .arg("file") + .fails() + .code_is(1) + .stderr_only(format!( + "od: invalid {} argument '{}'", + option, INVALID_SIZE + )); + + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .arg(format!("{}={}", option, BIG_SIZE)) + .arg("file") + .fails() + .code_is(1) + .stderr_only(format!("od: {} argument '{}' too large", option, BIG_SIZE)); + } } diff --git a/tests/by-util/test_paste.rs b/tests/by-util/test_paste.rs index 4604c5cf5..1afe84be8 100644 --- a/tests/by-util/test_paste.rs +++ b/tests/by-util/test_paste.rs @@ -7,7 +7,7 @@ struct TestData<'b> { out: &'b str, } -static EXAMPLE_DATA: &'static [TestData<'static>] = &[ +static EXAMPLE_DATA: &[TestData] = &[ // Ensure that paste properly handles files lacking a final newline. TestData { name: "no-nl-1", @@ -64,8 +64,8 @@ static EXAMPLE_DATA: &'static [TestData<'static>] = &[ #[test] fn test_combine_pairs_of_lines() { - for s in vec!["-s", "--serial"] { - for d in vec!["-d", "--delimiters"] { + for &s in &["-s", "--serial"] { + for &d in &["-d", "--delimiters"] { new_ucmd!() .args(&[s, d, "\t\n", "html_colors.txt"]) .run() @@ -76,7 +76,7 @@ fn test_combine_pairs_of_lines() { #[test] fn test_multi_stdin() { - for d in vec!["-d", "--delimiters"] { + for &d in &["-d", "--delimiters"] { new_ucmd!() .args(&[d, "\t\n", "-", "-"]) .pipe_in_fixture("html_colors.txt") diff --git a/tests/by-util/test_pathchk.rs b/tests/by-util/test_pathchk.rs index bdce377b3..8ba3b9033 100644 --- a/tests/by-util/test_pathchk.rs +++ b/tests/by-util/test_pathchk.rs @@ -5,11 +5,152 @@ fn test_default_mode() { // test the default mode // accept some reasonable default - new_ucmd!().args(&["abc/def"]).succeeds().no_stdout(); + new_ucmd!().args(&["dir/file"]).succeeds().no_stdout(); - // fail on long inputs + // accept non-portable chars + new_ucmd!().args(&["dir#/$file"]).succeeds().no_stdout(); + + // accept empty path + new_ucmd!().args(&[""]).succeeds().no_stdout(); + + // fail on long path new_ucmd!() - .args(&[repeat_str("test", 20000)]) + .args(&["dir".repeat(libc::PATH_MAX as usize + 1)]) + .fails() + .no_stdout(); + + // fail on long filename + new_ucmd!() + .args(&[format!( + "dir/{}", + "file".repeat(libc::FILENAME_MAX as usize + 1) + )]) .fails() .no_stdout(); } + +#[test] +fn test_posix_mode() { + // test the posix mode + + // accept some reasonable default + new_ucmd!().args(&["-p", "dir/file"]).succeeds().no_stdout(); + + // fail on long path + new_ucmd!() + .args(&["-p", "dir".repeat(libc::PATH_MAX as usize + 1).as_str()]) + .fails() + .no_stdout(); + + // fail on long filename + new_ucmd!() + .args(&[ + "-p", + format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(), + ]) + .fails() + .no_stdout(); + + // fail on non-portable chars + new_ucmd!().args(&["-p", "dir#/$file"]).fails().no_stdout(); +} + +#[test] +fn test_posix_special() { + // test the posix special mode + + // accept some reasonable default + new_ucmd!().args(&["-P", "dir/file"]).succeeds().no_stdout(); + + // accept non-portable chars + new_ucmd!() + .args(&["-P", "dir#/$file"]) + .succeeds() + .no_stdout(); + + // accept non-leading hyphen + new_ucmd!() + .args(&["-P", "dir/file-name"]) + .succeeds() + .no_stdout(); + + // fail on long path + new_ucmd!() + .args(&["-P", "dir".repeat(libc::PATH_MAX as usize + 1).as_str()]) + .fails() + .no_stdout(); + + // fail on long filename + new_ucmd!() + .args(&[ + "-P", + format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(), + ]) + .fails() + .no_stdout(); + + // fail on leading hyphen char + new_ucmd!().args(&["-P", "dir/-file"]).fails().no_stdout(); + + // fail on empty path + new_ucmd!().args(&["-P", ""]).fails().no_stdout(); +} + +#[test] +fn test_posix_all() { + // test the posix special mode + + // accept some reasonable default + new_ucmd!() + .args(&["-p", "-P", "dir/file"]) + .succeeds() + .no_stdout(); + + // accept non-leading hyphen + new_ucmd!() + .args(&["-p", "-P", "dir/file-name"]) + .succeeds() + .no_stdout(); + + // fail on long path + new_ucmd!() + .args(&[ + "-p", + "-P", + "dir".repeat(libc::PATH_MAX as usize + 1).as_str(), + ]) + .fails() + .no_stdout(); + + // fail on long filename + new_ucmd!() + .args(&[ + "-p", + "-P", + format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(), + ]) + .fails() + .no_stdout(); + + // fail on non-portable chars + new_ucmd!() + .args(&["-p", "-P", "dir#/$file"]) + .fails() + .no_stdout(); + + // fail on leading hyphen char + new_ucmd!() + .args(&["-p", "-P", "dir/-file"]) + .fails() + .no_stdout(); + + // fail on empty path + new_ucmd!().args(&["-p", "-P", ""]).fails().no_stdout(); +} + +#[test] +fn test_args_parsing() { + // fail on no args + let empty_args: [String; 0] = []; + new_ucmd!().args(&empty_args).fails().no_stdout(); +} diff --git a/tests/by-util/test_pinky.rs b/tests/by-util/test_pinky.rs index c8e8334ab..bc2833a42 100644 --- a/tests/by-util/test_pinky.rs +++ b/tests/by-util/test_pinky.rs @@ -9,75 +9,105 @@ pub use self::pinky::*; #[test] fn test_capitalize() { - assert_eq!("Zbnmasd", "zbnmasd".capitalize()); - assert_eq!("Abnmasd", "Abnmasd".capitalize()); - assert_eq!("1masd", "1masd".capitalize()); + assert_eq!("Zbnmasd", "zbnmasd".capitalize()); // spell-checker:disable-line + assert_eq!("Abnmasd", "Abnmasd".capitalize()); // spell-checker:disable-line + assert_eq!("1masd", "1masd".capitalize()); // spell-checker:disable-line assert_eq!("", "".capitalize()); } #[test] fn test_long_format() { - let ulogin = "root"; - let pw: Passwd = Passwd::locate(ulogin).unwrap(); + let login = "root"; + let pw: Passwd = Passwd::locate(login).unwrap(); let real_name = pw.user_info().replace("&", &pw.name().capitalize()); - new_ucmd!().arg("-l").arg(ulogin).run().stdout_is(format!( - "Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n", - ulogin, - real_name, - pw.user_dir(), - pw.user_shell() - )); + new_ucmd!() + .arg("-l") + .arg(login) + .succeeds() + .stdout_is(format!( + "Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n", + login, + real_name, + pw.user_dir(), + pw.user_shell() + )); - new_ucmd!().arg("-lb").arg(ulogin).run().stdout_is(format!( - "Login name: {:<28}In real life: {1}\n\n", - ulogin, real_name - )); + new_ucmd!() + .arg("-lb") + .arg(login) + .succeeds() + .stdout_is(format!( + "Login name: {:<28}In real life: {1}\n\n", + login, real_name + )); } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_long_format_multiple_users() { + let args = ["-l", "root", "root", "root"]; + + new_ucmd!() + .args(&args) + .succeeds() + .stdout_is(expected_result(&args)); +} + +#[test] +fn test_long_format_wo_user() { + // "no username specified; at least one must be specified when using -l" + new_ucmd!().arg("-l").fails().code_is(1); +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_short_format_i() { // allow whitespace variation // * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant let args = ["-i"]; - let actual = TestScenario::new(util_name!()) - .ucmd() - .args(&args) - .run() - .stdout; + let actual = new_ucmd!().args(&args).succeeds().stdout_move_str(); let expect = expected_result(&args); - println!("actual: {:?}", actual); - println!("expect: {:?}", expect); let v_actual: Vec<&str> = actual.split_whitespace().collect(); let v_expect: Vec<&str> = expect.split_whitespace().collect(); assert_eq!(v_actual, v_expect); } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_short_format_q() { // allow whitespace variation // * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant let args = ["-q"]; - let actual = TestScenario::new(util_name!()) - .ucmd() - .args(&args) - .run() - .stdout; + let actual = new_ucmd!().args(&args).succeeds().stdout_move_str(); let expect = expected_result(&args); - println!("actual: {:?}", actual); - println!("expect: {:?}", expect); let v_actual: Vec<&str> = actual.split_whitespace().collect(); let v_expect: Vec<&str> = expect.split_whitespace().collect(); assert_eq!(v_actual, v_expect); } -#[cfg(target_os = "linux")] -fn expected_result(args: &[&str]) -> String { - TestScenario::new(util_name!()) - .cmd_keepenv(util_name!()) - .env("LANGUAGE", "C") - .args(args) - .run() - .stdout +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_no_flag() { + let actual = new_ucmd!().succeeds().stdout_move_str(); + let expect = expected_result(&[]); + let v_actual: Vec<&str> = actual.split_whitespace().collect(); + let v_expect: Vec<&str> = expect.split_whitespace().collect(); + assert_eq!(v_actual, v_expect); +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +fn expected_result(args: &[&str]) -> String { + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(target_vendor = "apple")] + let util_name = format!("g{}", util_name!()); + + // note: clippy::needless_borrow *false positive* + #[allow(clippy::needless_borrow)] + TestScenario::new(&util_name) + .cmd_keepenv(util_name) + .env("LC_ALL", "C") + .args(args) + .succeeds() + .stdout_move_str() } diff --git a/tests/by-util/test_pr.rs b/tests/by-util/test_pr.rs new file mode 100644 index 000000000..4a79a3eda --- /dev/null +++ b/tests/by-util/test_pr.rs @@ -0,0 +1,481 @@ +// spell-checker:ignore (ToDO) Sdivide + +use crate::common::util::*; +use chrono::offset::Local; +use chrono::DateTime; +use chrono::Duration; +use std::fs::metadata; + +fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String { + let tmp_dir_path = ucmd.get_full_fixture_path(path); + let file_metadata = metadata(tmp_dir_path); + file_metadata + .map(|i| { + i.modified() + .map(|x| { + let date_time: DateTime = x.into(); + date_time.format("%b %d %H:%M %Y").to_string() + }) + .unwrap_or_default() + }) + .unwrap_or_default() +} + +fn all_minutes(from: DateTime, to: DateTime) -> Vec { + let to = to + Duration::minutes(1); + const FORMAT: &str = "%b %d %H:%M %Y"; + let mut vec = vec![]; + let mut current = from; + while current < to { + vec.push(current.format(FORMAT).to_string()); + current = current + Duration::minutes(1); + } + vec +} + +fn valid_last_modified_template_vars(from: DateTime) -> Vec> { + all_minutes(from, Local::now()) + .into_iter() + .map(|time| vec![("{last_modified_time}".to_string(), time)]) + .collect() +} + +#[test] +fn test_without_any_options() { + let test_file_path = "test_one_page.log"; + let expected_test_file_path = "test_one_page.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&[test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_numbering_option_with_number_width() { + let test_file_path = "test_num_page.log"; + let expected_test_file_path = "test_num_page_2.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["-n", "2", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_long_header_option() { + let test_file_path = "test_one_page.log"; + let expected_test_file_path = "test_one_page_header.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + let header = "new file"; + scenario + .args(&["--header=new file", test_file_path]) + .succeeds() + .stdout_is_templated_fixture( + expected_test_file_path, + &[("{last_modified_time}", &value), ("{header}", header)], + ); + + new_ucmd!() + .args(&["-h", header, test_file_path]) + .succeeds() + .stdout_is_templated_fixture( + expected_test_file_path, + &[("{last_modified_time}", &value), ("{header}", header)], + ); +} + +#[test] +fn test_with_double_space_option() { + let test_file_path = "test_one_page.log"; + let expected_test_file_path = "test_one_page_double_line.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["-d", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); + + new_ucmd!() + .args(&["--double-space", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_first_line_number_option() { + let test_file_path = "test_one_page.log"; + let expected_test_file_path = "test_one_page_first_line.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["-N", "5", "-n", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_first_line_number_long_option() { + let test_file_path = "test_one_page.log"; + let expected_test_file_path = "test_one_page_first_line.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["--first-line-number=5", "-n", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_number_option_with_custom_separator_char() { + let test_file_path = "test_num_page.log"; + let expected_test_file_path = "test_num_page_char.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["-nc", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_number_option_with_custom_separator_char_and_width() { + let test_file_path = "test_num_page.log"; + let expected_test_file_path = "test_num_page_char_one.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["-nc1", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_valid_page_ranges() { + let test_file_path = "test_num_page.log"; + let mut scenario = new_ucmd!(); + scenario + .args(&["--pages=20:5", test_file_path]) + .fails() + .stderr_is("pr: invalid --pages argument '20:5'") + .stdout_is(""); + new_ucmd!() + .args(&["--pages=1:5", test_file_path]) + .succeeds(); + new_ucmd!().args(&["--pages=1", test_file_path]).succeeds(); + new_ucmd!() + .args(&["--pages=-1:5", test_file_path]) + .fails() + .stderr_is("pr: invalid --pages argument '-1:5'") + .stdout_is(""); + new_ucmd!() + .args(&["--pages=1:-5", test_file_path]) + .fails() + .stderr_is("pr: invalid --pages argument '1:-5'") + .stdout_is(""); + new_ucmd!() + .args(&["--pages=5:1", test_file_path]) + .fails() + .stderr_is("pr: invalid --pages argument '5:1'") + .stdout_is(""); +} + +#[test] +fn test_with_page_range() { + let test_file_path = "test.log"; + let expected_test_file_path = "test_page_range_1.log.expected"; + let expected_test_file_path1 = "test_page_range_2.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["--pages=15", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); + + new_ucmd!() + .args(&["+15", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); + + new_ucmd!() + .args(&["--pages=15:17", test_file_path]) + .succeeds() + .stdout_is_templated_fixture( + expected_test_file_path1, + &[("{last_modified_time}", &value)], + ); + + new_ucmd!() + .args(&["+15:17", test_file_path]) + .succeeds() + .stdout_is_templated_fixture( + expected_test_file_path1, + &[("{last_modified_time}", &value)], + ); +} + +#[test] +fn test_with_no_header_trailer_option() { + let test_file_path = "test_one_page.log"; + let expected_test_file_path = "test_one_page_no_ht.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["-t", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_page_length_option() { + let test_file_path = "test.log"; + let expected_test_file_path = "test_page_length.log.expected"; + let expected_test_file_path1 = "test_page_length1.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["--pages=2:3", "-l", "100", "-n", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); + + new_ucmd!() + .args(&["--pages=2:3", "-l", "5", "-n", test_file_path]) + .succeeds() + .stdout_is_fixture(expected_test_file_path1); +} + +#[test] +fn test_with_suppress_error_option() { + let test_file_path = "test_num_page.log"; + let mut scenario = new_ucmd!(); + scenario + .args(&["--pages=20:5", "-r", test_file_path]) + .fails() + .stderr_is("") + .stdout_is(""); +} + +#[test] +fn test_with_stdin() { + let expected_file_path = "stdin.log.expected"; + let mut scenario = new_ucmd!(); + let start = Local::now(); + scenario + .pipe_in_fixture("stdin.log") + .args(&["--pages=1:2", "-n", "-"]) + .run() + .stdout_is_templated_fixture_any( + expected_file_path, + &valid_last_modified_template_vars(start), + ); +} + +#[test] +fn test_with_column() { + let test_file_path = "column.log"; + let expected_test_file_path = "column.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["--pages=3:5", "--column=3", "-n", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); + + new_ucmd!() + .args(&["--pages=3:5", "-3", "-n", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_column_across_option() { + let test_file_path = "column.log"; + let expected_test_file_path = "column_across.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&["--pages=3:5", "--column=3", "-a", "-n", test_file_path]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_column_across_option_and_column_separator() { + let test_file_path = "column.log"; + let expected_test_file_path = "column_across_sep.log.expected"; + let expected_test_file_path1 = "column_across_sep1.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&[ + "--pages=3:5", + "--column=3", + "-s|", + "-a", + "-n", + test_file_path, + ]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); + + new_ucmd!() + .args(&[ + "--pages=3:5", + "--column=3", + "-Sdivide", + "-a", + "-n", + test_file_path, + ]) + .succeeds() + .stdout_is_templated_fixture( + expected_test_file_path1, + &[("{last_modified_time}", &value)], + ); +} + +#[test] +fn test_with_mpr() { + let test_file_path = "column.log"; + let test_file_path1 = "hosts.log"; + let expected_test_file_path = "mpr.log.expected"; + let expected_test_file_path1 = "mpr1.log.expected"; + let expected_test_file_path2 = "mpr2.log.expected"; + let start = Local::now(); + new_ucmd!() + .args(&["--pages=1:2", "-m", "-n", test_file_path, test_file_path1]) + .succeeds() + .stdout_is_templated_fixture_any( + expected_test_file_path, + &valid_last_modified_template_vars(start), + ); + + let start = Local::now(); + new_ucmd!() + .args(&["--pages=2:4", "-m", "-n", test_file_path, test_file_path1]) + .succeeds() + .stdout_is_templated_fixture_any( + expected_test_file_path1, + &valid_last_modified_template_vars(start), + ); + + let start = Local::now(); + new_ucmd!() + .args(&[ + "--pages=1:2", + "-l", + "100", + "-n", + "-m", + test_file_path, + test_file_path1, + test_file_path, + ]) + .succeeds() + .stdout_is_templated_fixture_any( + expected_test_file_path2, + &valid_last_modified_template_vars(start), + ); +} + +#[test] +fn test_with_mpr_and_column_options() { + let test_file_path = "column.log"; + new_ucmd!() + .args(&["--column=2", "-m", "-n", test_file_path]) + .fails() + .stderr_is("pr: cannot specify number of columns when printing in parallel") + .stdout_is(""); + + new_ucmd!() + .args(&["-a", "-m", "-n", test_file_path]) + .fails() + .stderr_is("pr: cannot specify both printing across and printing in parallel") + .stdout_is(""); +} + +#[test] +fn test_with_offset_space_option() { + let test_file_path = "column.log"; + let expected_test_file_path = "column_spaces_across.log.expected"; + let mut scenario = new_ucmd!(); + let value = file_last_modified_time(&scenario, test_file_path); + scenario + .args(&[ + "-o", + "5", + "--pages=3:5", + "--column=3", + "-a", + "-n", + test_file_path, + ]) + .succeeds() + .stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); +} + +#[test] +fn test_with_pr_core_utils_tests() { + let test_cases = vec![ + ("", vec!["0Ft"], vec!["0F"], 0), + ("", vec!["0Fnt"], vec!["0F"], 0), + ("+3", vec!["0Ft"], vec!["3-0F"], 0), + ("+3 -f", vec!["0Ft"], vec!["3f-0F"], 0), + ("-a -3", vec!["0Ft"], vec!["a3-0F"], 0), + ("-a -3 -f", vec!["0Ft"], vec!["a3f-0F"], 0), + ("-a -3 -f", vec!["0Fnt"], vec!["a3f-0F"], 0), + ("+3 -a -3 -f", vec!["0Ft"], vec!["3a3f-0F"], 0), + ("-l 24", vec!["FnFn"], vec!["l24-FF"], 0), + ("-W 20 -l24 -f", vec!["tFFt-ll"], vec!["W20l24f-ll"], 0), + ]; + + for test_case in test_cases { + let (flags, input_file, expected_file, return_code) = test_case; + let mut scenario = new_ucmd!(); + let input_file_path = input_file.get(0).unwrap(); + let test_file_path = expected_file.get(0).unwrap(); + let value = file_last_modified_time(&scenario, test_file_path); + let mut arguments: Vec<&str> = flags + .split(' ') + .into_iter() + .filter(|i| i.trim() != "") + .collect::>(); + + arguments.extend(input_file.clone()); + + let scenario_with_args = scenario.args(&arguments); + + let scenario_with_expected_status = if return_code == 0 { + scenario_with_args.succeeds() + } else { + scenario_with_args.fails() + }; + + scenario_with_expected_status.stdout_is_templated_fixture( + test_file_path, + &[ + ("{last_modified_time}", &value), + ("{file_name}", input_file_path), + ], + ); + } +} + +#[test] +fn test_with_join_lines_option() { + let test_file_1 = "hosts.log"; + let test_file_2 = "test.log"; + let expected_file_path = "joined.log.expected"; + let mut scenario = new_ucmd!(); + let start = Local::now(); + scenario + .args(&["+1:2", "-J", "-m", test_file_1, test_file_2]) + .run() + .stdout_is_templated_fixture_any( + expected_file_path, + &valid_last_modified_template_vars(start), + ); +} diff --git a/tests/by-util/test_printenv.rs b/tests/by-util/test_printenv.rs index 9d90051ea..bc0bc0f3c 100644 --- a/tests/by-util/test_printenv.rs +++ b/tests/by-util/test_printenv.rs @@ -7,10 +7,11 @@ fn test_get_all() { env::set_var(key, "VALUE"); assert_eq!(env::var(key), Ok("VALUE".to_string())); - let result = TestScenario::new(util_name!()).ucmd_keepenv().run(); - assert!(result.success); - assert!(result.stdout.contains("HOME=")); - assert!(result.stdout.contains("KEY=VALUE")); + TestScenario::new(util_name!()) + .ucmd_keepenv() + .succeeds() + .stdout_contains("HOME=") + .stdout_contains("KEY=VALUE"); } #[test] @@ -22,9 +23,8 @@ fn test_get_var() { let result = TestScenario::new(util_name!()) .ucmd_keepenv() .arg("KEY") - .run(); + .succeeds(); - assert!(result.success); - assert!(!result.stdout.is_empty()); - assert!(result.stdout.trim() == "VALUE"); + assert!(!result.stdout_str().is_empty()); + assert!(result.stdout_str().trim() == "VALUE"); } diff --git a/tests/by-util/test_printf.rs b/tests/by-util/test_printf.rs index 1e3e21d0c..b5c9dc3ed 100644 --- a/tests/by-util/test_printf.rs +++ b/tests/by-util/test_printf.rs @@ -43,12 +43,12 @@ fn escaped_octal() { } #[test] -fn escaped_unicode_fourdigit() { +fn escaped_unicode_four_digit() { new_ucmd!().args(&["\\u0125"]).succeeds().stdout_only("ĥ"); } #[test] -fn escaped_unicode_eightdigit() { +fn escaped_unicode_eight_digit() { new_ucmd!() .args(&["\\U00000125"]) .succeeds() @@ -77,7 +77,7 @@ fn sub_string() { } #[test] -fn sub_multifield() { +fn sub_multi_field() { new_ucmd!() .args(&["%s %s", "hello", "world"]) .succeeds() @@ -85,7 +85,7 @@ fn sub_multifield() { } #[test] -fn sub_repeat_formatstr() { +fn sub_repeat_format_str() { new_ucmd!() .args(&["%s.", "hello", "world"]) .succeeds() @@ -101,7 +101,7 @@ fn sub_string_ignore_escapes() { } #[test] -fn sub_bstring_handle_escapes() { +fn sub_b_string_handle_escapes() { new_ucmd!() .args(&["hello %b", "\\tworld"]) .succeeds() @@ -109,7 +109,7 @@ fn sub_bstring_handle_escapes() { } #[test] -fn sub_bstring_ignore_subs() { +fn sub_b_string_ignore_subs() { new_ucmd!() .args(&["hello %b", "world %% %i"]) .succeeds() @@ -133,7 +133,7 @@ fn sub_num_int() { } #[test] -fn sub_num_int_minwidth() { +fn sub_num_int_min_width() { new_ucmd!() .args(&["twenty is %1i", "20"]) .succeeds() @@ -181,11 +181,11 @@ fn sub_num_int_hex_in_neg() { } #[test] -fn sub_num_int_charconst_in() { +fn sub_num_int_char_const_in() { new_ucmd!() - .args(&["ninetyseven is %i", "'a"]) + .args(&["ninety seven is %i", "'a"]) .succeeds() - .stdout_only("ninetyseven is 97"); + .stdout_only("ninety seven is 97"); } #[test] @@ -287,7 +287,7 @@ fn sub_num_hex_float_upper() { } #[test] -fn sub_minwidth() { +fn sub_min_width() { new_ucmd!() .args(&["hello %7s", "world"]) .succeeds() @@ -295,7 +295,7 @@ fn sub_minwidth() { } #[test] -fn sub_minwidth_negative() { +fn sub_min_width_negative() { new_ucmd!() .args(&["hello %-7s", "world"]) .succeeds() @@ -327,7 +327,7 @@ fn sub_int_leading_zeroes() { } #[test] -fn sub_int_leading_zeroes_prio() { +fn sub_int_leading_zeroes_padded() { new_ucmd!() .args(&["%5.4i", "11"]) .succeeds() @@ -359,7 +359,7 @@ fn sub_float_no_octal_in() { } #[test] -fn sub_any_asterisk_firstparam() { +fn sub_any_asterisk_first_param() { new_ucmd!() .args(&["%*i", "3", "11", "4", "12"]) .succeeds() @@ -401,7 +401,7 @@ fn sub_any_asterisk_hex_arg() { #[test] fn sub_any_specifiers_no_params() { new_ucmd!() - .args(&["%ztlhLji", "3"]) + .args(&["%ztlhLji", "3"]) //spell-checker:disable-line .succeeds() .stdout_only("3"); } @@ -409,7 +409,7 @@ fn sub_any_specifiers_no_params() { #[test] fn sub_any_specifiers_after_first_param() { new_ucmd!() - .args(&["%0ztlhLji", "3"]) + .args(&["%0ztlhLji", "3"]) //spell-checker:disable-line .succeeds() .stdout_only("3"); } @@ -417,7 +417,7 @@ fn sub_any_specifiers_after_first_param() { #[test] fn sub_any_specifiers_after_period() { new_ucmd!() - .args(&["%0.ztlhLji", "3"]) + .args(&["%0.ztlhLji", "3"]) //spell-checker:disable-line .succeeds() .stdout_only("3"); } @@ -425,7 +425,7 @@ fn sub_any_specifiers_after_period() { #[test] fn sub_any_specifiers_after_second_param() { new_ucmd!() - .args(&["%0.0ztlhLji", "3"]) + .args(&["%0.0ztlhLji", "3"]) //spell-checker:disable-line .succeeds() .stdout_only("3"); } diff --git a/tests/by-util/test_ptx.rs b/tests/by-util/test_ptx.rs index e44943bfa..c17d473f5 100644 --- a/tests/by-util/test_ptx.rs +++ b/tests/by-util/test_ptx.rs @@ -1,43 +1,43 @@ use crate::common::util::*; #[test] -fn gnu_ext_disabled_roff_no_ref() { +fn gnu_ext_disabled_rightward_no_ref() { new_ucmd!() .args(&["-G", "-R", "input"]) .succeeds() - .stdout_only_fixture("gnu_ext_disabled_roff_no_ref.expected"); + .stdout_only_fixture("gnu_ext_disabled_rightward_no_ref.expected"); } #[test] -fn gnu_ext_disabled_roff_no_ref_empty_word_regexp() { +fn gnu_ext_disabled_rightward_no_ref_empty_word_regexp() { new_ucmd!() .args(&["-G", "-R", "-W", "", "input"]) .succeeds() - .stdout_only_fixture("gnu_ext_disabled_roff_no_ref.expected"); + .stdout_only_fixture("gnu_ext_disabled_rightward_no_ref.expected"); } #[test] -fn gnu_ext_disabled_roff_no_ref_word_regexp_exc_space() { +fn gnu_ext_disabled_rightward_no_ref_word_regexp_exc_space() { new_ucmd!() .args(&["-G", "-R", "-W", "[^\t\n]+", "input"]) .succeeds() - .stdout_only_fixture("gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected"); + .stdout_only_fixture("gnu_ext_disabled_rightward_no_ref_word_regexp_exc_space.expected"); } #[test] -fn gnu_ext_disabled_roff_input_ref() { +fn gnu_ext_disabled_rightward_input_ref() { new_ucmd!() .args(&["-G", "-r", "-R", "input"]) .succeeds() - .stdout_only_fixture("gnu_ext_disabled_roff_input_ref.expected"); + .stdout_only_fixture("gnu_ext_disabled_rightward_input_ref.expected"); } #[test] -fn gnu_ext_disabled_roff_auto_ref() { +fn gnu_ext_disabled_rightward_auto_ref() { new_ucmd!() .args(&["-G", "-A", "-R", "input"]) .succeeds() - .stdout_only_fixture("gnu_ext_disabled_roff_auto_ref.expected"); + .stdout_only_fixture("gnu_ext_disabled_rightward_auto_ref.expected"); } #[test] diff --git a/tests/by-util/test_pwd.rs b/tests/by-util/test_pwd.rs index b6a6c87a4..2779b9e62 100644 --- a/tests/by-util/test_pwd.rs +++ b/tests/by-util/test_pwd.rs @@ -9,5 +9,5 @@ fn test_default() { #[test] fn test_failed() { let (_at, mut ucmd) = at_and_ucmd!(); - ucmd.arg("willfail").fails(); + ucmd.arg("will-fail").fails(); } diff --git a/tests/by-util/test_readlink.rs b/tests/by-util/test_readlink.rs index 84747b24c..51aebbed2 100644 --- a/tests/by-util/test_readlink.rs +++ b/tests/by-util/test_readlink.rs @@ -1,11 +1,11 @@ use crate::common::util::*; -static GIBBERISH: &'static str = "supercalifragilisticexpialidocious"; +static GIBBERISH: &str = "supercalifragilisticexpialidocious"; #[test] fn test_canonicalize() { let (at, mut ucmd) = at_and_ucmd!(); - let actual = ucmd.arg("-f").arg(".").run().stdout; + let actual = ucmd.arg("-f").arg(".").run().stdout_move_str(); let expect = at.root_dir_resolved() + "\n"; println!("actual: {:?}", actual); println!("expect: {:?}", expect); @@ -15,7 +15,7 @@ fn test_canonicalize() { #[test] fn test_canonicalize_existing() { let (at, mut ucmd) = at_and_ucmd!(); - let actual = ucmd.arg("-e").arg(".").run().stdout; + let actual = ucmd.arg("-e").arg(".").run().stdout_move_str(); let expect = at.root_dir_resolved() + "\n"; println!("actual: {:?}", actual); println!("expect: {:?}", expect); @@ -25,7 +25,7 @@ fn test_canonicalize_existing() { #[test] fn test_canonicalize_missing() { let (at, mut ucmd) = at_and_ucmd!(); - let actual = ucmd.arg("-m").arg(GIBBERISH).run().stdout; + let actual = ucmd.arg("-m").arg(GIBBERISH).run().stdout_move_str(); let expect = path_concat!(at.root_dir_resolved(), GIBBERISH) + "\n"; println!("actual: {:?}", actual); println!("expect: {:?}", expect); @@ -37,7 +37,7 @@ fn test_long_redirection_to_current_dir() { let (at, mut ucmd) = at_and_ucmd!(); // Create a 256-character path to current directory let dir = path_concat!(".", ..128); - let actual = ucmd.arg("-n").arg("-m").arg(dir).run().stdout; + let actual = ucmd.arg("-n").arg("-m").arg(dir).run().stdout_move_str(); let expect = at.root_dir_resolved(); println!("actual: {:?}", actual); println!("expect: {:?}", expect); @@ -48,7 +48,12 @@ fn test_long_redirection_to_current_dir() { fn test_long_redirection_to_root() { // Create a 255-character path to root let dir = path_concat!("..", ..85); - let actual = new_ucmd!().arg("-n").arg("-m").arg(dir).run().stdout; + let actual = new_ucmd!() + .arg("-n") + .arg("-m") + .arg(dir) + .run() + .stdout_move_str(); let expect = get_root_path(); println!("actual: {:?}", actual); println!("expect: {:?}", expect); diff --git a/tests/by-util/test_realpath.rs b/tests/by-util/test_realpath.rs index 750a8db01..e1384ac74 100644 --- a/tests/by-util/test_realpath.rs +++ b/tests/by-util/test_realpath.rs @@ -1,106 +1,108 @@ use crate::common::util::*; #[test] -fn test_current_directory() { +fn test_realpath_current_directory() { let (at, mut ucmd) = at_and_ucmd!(); - let actual = ucmd.arg(".").run().stdout; let expect = at.root_dir_resolved() + "\n"; - println!("actual: {:?}", actual); - println!("expect: {:?}", expect); - assert_eq!(actual, expect); + ucmd.arg(".").succeeds().stdout_is(expect); } #[test] -fn test_long_redirection_to_current_dir() { +fn test_realpath_long_redirection_to_current_dir() { let (at, mut ucmd) = at_and_ucmd!(); // Create a 256-character path to current directory let dir = path_concat!(".", ..128); - let actual = ucmd.arg(dir).run().stdout; let expect = at.root_dir_resolved() + "\n"; - println!("actual: {:?}", actual); - println!("expect: {:?}", expect); - assert_eq!(actual, expect); + ucmd.arg(dir).succeeds().stdout_is(expect); } #[test] -fn test_long_redirection_to_root() { +fn test_realpath_long_redirection_to_root() { // Create a 255-character path to root let dir = path_concat!("..", ..85); - let actual = new_ucmd!().arg(dir).run().stdout; let expect = get_root_path().to_owned() + "\n"; - println!("actual: {:?}", actual); - println!("expect: {:?}", expect); - assert_eq!(actual, expect); + new_ucmd!().arg(dir).succeeds().stdout_is(expect); } #[test] -fn test_file_and_links() { +fn test_realpath_file_and_links() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; at.touch("foo"); at.symlink_file("foo", "bar"); - let actual = scene.ucmd().arg("foo").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("foo\n")); - - let actual = scene.ucmd().arg("bar").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("foo\n")); + scene.ucmd().arg("foo").succeeds().stdout_contains("foo\n"); + scene.ucmd().arg("bar").succeeds().stdout_contains("foo\n"); } #[test] -fn test_file_and_links_zero() { +fn test_realpath_file_and_links_zero() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; at.touch("foo"); at.symlink_file("foo", "bar"); - let actual = scene.ucmd().arg("foo").arg("-z").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("foo")); - assert!(!actual.contains("\n")); + scene + .ucmd() + .arg("foo") + .arg("-z") + .succeeds() + .stdout_contains("foo\u{0}"); - let actual = scene.ucmd().arg("bar").arg("-z").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("foo")); - assert!(!actual.contains("\n")); + scene + .ucmd() + .arg("bar") + .arg("-z") + .succeeds() + .stdout_contains("foo\u{0}"); } #[test] -fn test_file_and_links_strip() { +fn test_realpath_file_and_links_strip() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; at.touch("foo"); at.symlink_file("foo", "bar"); - let actual = scene.ucmd().arg("foo").arg("-s").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("foo\n")); + scene + .ucmd() + .arg("foo") + .arg("-s") + .succeeds() + .stdout_contains("foo\n"); - let actual = scene.ucmd().arg("bar").arg("-s").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("bar\n")); + scene + .ucmd() + .arg("bar") + .arg("-s") + .succeeds() + .stdout_contains("bar\n"); } #[test] -fn test_file_and_links_strip_zero() { +fn test_realpath_file_and_links_strip_zero() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; at.touch("foo"); at.symlink_file("foo", "bar"); - let actual = scene.ucmd().arg("foo").arg("-s").arg("-z").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("foo")); - assert!(!actual.contains("\n")); + scene + .ucmd() + .arg("foo") + .arg("-s") + .arg("-z") + .succeeds() + .stdout_contains("foo\u{0}"); - let actual = scene.ucmd().arg("bar").arg("-s").arg("-z").run().stdout; - println!("actual: {:?}", actual); - assert!(actual.contains("bar")); - assert!(!actual.contains("\n")); + scene + .ucmd() + .arg("bar") + .arg("-s") + .arg("-z") + .succeeds() + .stdout_contains("bar\u{0}"); } diff --git a/tests/by-util/test_relpath.rs b/tests/by-util/test_relpath.rs index 651491045..44a685c90 100644 --- a/tests/by-util/test_relpath.rs +++ b/tests/by-util/test_relpath.rs @@ -1 +1,178 @@ -// ToDO: add tests +use crate::common::util::*; +use std::borrow::Cow; +use std::path::Path; + +struct TestCase<'a> { + from: &'a str, + to: &'a str, + expected: &'a str, +} + +const TESTS: [TestCase; 10] = [ + TestCase { + from: "A/B/C", + to: "A", + expected: "../..", + }, + TestCase { + from: "A/B/C", + to: "A/B", + expected: "..", + }, + TestCase { + from: "A/B/C", + to: "A/B/C", + expected: "", + }, + TestCase { + from: "A/B/C", + to: "A/B/C/D", + expected: "D", + }, + TestCase { + from: "A/B/C", + to: "A/B/C/D/E", + expected: "D/E", + }, + TestCase { + from: "A/B/C", + to: "A/B/D", + expected: "../D", + }, + TestCase { + from: "A/B/C", + to: "A/B/D/E", + expected: "../D/E", + }, + TestCase { + from: "A/B/C", + to: "A/D", + expected: "../../D", + }, + TestCase { + from: "A/B/C", + to: "D/E/F", + expected: "../../../D/E/F", + }, + TestCase { + from: "A/B/C", + to: "A/D/E", + expected: "../../D/E", + }, +]; + +#[allow(clippy::needless_lifetimes)] +fn convert_path<'a>(path: &'a str) -> Cow<'a, str> { + #[cfg(windows)] + return path.replace("/", "\\").into(); + #[cfg(not(windows))] + return path.into(); +} + +#[test] +fn test_relpath_with_from_no_d() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + for test in TESTS.iter() { + let from: &str = &convert_path(test.from); + let to: &str = &convert_path(test.to); + let expected: &str = &convert_path(test.expected); + + at.mkdir_all(to); + at.mkdir_all(from); + + scene + .ucmd() + .arg(to) + .arg(from) + .succeeds() + .stdout_only(&format!("{}\n", expected)); + } +} + +#[test] +fn test_relpath_with_from_with_d() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + for test in TESTS.iter() { + let from: &str = &convert_path(test.from); + let to: &str = &convert_path(test.to); + let pwd = at.as_string(); + at.mkdir_all(to); + at.mkdir_all(from); + + // d is part of subpath -> expect relative path + let mut _result_stdout = scene + .ucmd() + .arg(to) + .arg(from) + .arg(&format!("-d{}", pwd)) + .succeeds() + .stdout_move_str(); + // relax rules for windows test environment + #[cfg(not(windows))] + assert!(Path::new(&_result_stdout).is_relative()); + + // d is not part of subpath -> expect absolute path + _result_stdout = scene + .ucmd() + .arg(to) + .arg(from) + .arg("-dnon_existing") // spell-checker:disable-line + .succeeds() + .stdout_move_str(); + assert!(Path::new(&_result_stdout).is_absolute()); + } +} + +#[test] +fn test_relpath_no_from_no_d() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + for test in TESTS.iter() { + let to: &str = &convert_path(test.to); + at.mkdir_all(to); + + let _result_stdout = scene.ucmd().arg(to).succeeds().stdout_move_str(); + #[cfg(not(windows))] + assert_eq!(_result_stdout, format!("{}\n", to)); + // relax rules for windows test environment + #[cfg(windows)] + assert!(_result_stdout.ends_with(&format!("{}\n", to))); + } +} + +#[test] +fn test_relpath_no_from_with_d() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + for test in TESTS.iter() { + let to: &str = &convert_path(test.to); + let pwd = at.as_string(); + at.mkdir_all(to); + + // d is part of subpath -> expect relative path + let _result_stdout = scene + .ucmd() + .arg(to) + .arg(&format!("-d{}", pwd)) + .succeeds() + .stdout_move_str(); + // relax rules for windows test environment + #[cfg(not(windows))] + assert!(Path::new(&_result_stdout).is_relative()); + + // d is not part of subpath -> expect absolute path + let result_stdout = scene + .ucmd() + .arg(to) + .arg("-dnon_existing") // spell-checker:disable-line + .succeeds() + .stdout_move_str(); + assert!(Path::new(&result_stdout).is_absolute()); + } +} diff --git a/tests/by-util/test_rm.rs b/tests/by-util/test_rm.rs index f9a4dd0d5..0592be244 100644 --- a/tests/by-util/test_rm.rs +++ b/tests/by-util/test_rm.rs @@ -15,9 +15,12 @@ fn test_rm_one_file() { #[test] fn test_rm_failed() { let (_at, mut ucmd) = at_and_ucmd!(); - let file = "test_rm_one_file"; + let file = "test_rm_one_file"; // Doesn't exist - ucmd.arg(file).fails(); // Doesn't exist + ucmd.arg(file).fails().stderr_contains(&format!( + "cannot remove '{}': No such file or directory", + file + )); } #[test] @@ -62,7 +65,7 @@ fn test_rm_interactive() { .arg("-i") .arg(file_a) .arg(file_b) - .pipe_in("Yesh") + .pipe_in("Yesh") // spell-checker:disable-line .succeeds(); assert!(!at.file_exists(file_a)); @@ -115,6 +118,39 @@ fn test_rm_empty_directory() { assert!(!at.dir_exists(dir)); } +#[test] +fn test_rm_empty_directory_verbose() { + let (at, mut ucmd) = at_and_ucmd!(); + let dir = "test_rm_empty_directory_verbose"; + + at.mkdir(dir); + + ucmd.arg("-d") + .arg("-v") + .arg(dir) + .succeeds() + .stdout_only(format!("removed directory '{}'\n", dir)); + + assert!(!at.dir_exists(dir)); +} + +#[test] +fn test_rm_non_empty_directory() { + let (at, mut ucmd) = at_and_ucmd!(); + let dir = "test_rm_non_empty_dir"; + let file_a = &format!("{}/test_rm_non_empty_file_a", dir); + + at.mkdir(dir); + at.touch(file_a); + + ucmd.arg("-d") + .arg(dir) + .fails() + .stderr_contains(&format!("cannot remove '{}': Directory not empty", dir)); + assert!(at.file_exists(file_a)); + assert!(at.dir_exists(dir)); +} + #[test] fn test_rm_recursive() { let (at, mut ucmd) = at_and_ucmd!(); @@ -134,22 +170,15 @@ fn test_rm_recursive() { } #[test] -fn test_rm_errors() { +fn test_rm_directory_without_flag() { let (at, mut ucmd) = at_and_ucmd!(); - let dir = "test_rm_errors_directory"; - let file_a = "test_rm_errors_directory/test_rm_errors_file_a"; - let file_b = "test_rm_errors_directory/test_rm_errors_file_b"; + let dir = "test_rm_directory_without_flag_dir"; at.mkdir(dir); - at.touch(file_a); - at.touch(file_b); - // $ rm test_rm_errors_directory - // rm: error: could not remove directory 'test_rm_errors_directory' (did you mean to pass '-r'?) - ucmd.arg(dir).fails().stderr_is( - "rm: error: could not remove directory 'test_rm_errors_directory' (did you mean \ - to pass '-r'?)\n", - ); + ucmd.arg(dir) + .fails() + .stderr_contains(&format!("cannot remove '{}': Is a directory", dir)); } #[test] @@ -169,10 +198,13 @@ fn test_rm_verbose() { } #[test] -fn test_rm_dir_symlink() { +#[cfg(not(windows))] +// on unix symlink_dir is a file +fn test_rm_symlink_dir() { let (at, mut ucmd) = at_and_ucmd!(); - let dir = "test_rm_dir_symlink_dir"; - let link = "test_rm_dir_symlink_link"; + + let dir = "test_rm_symlink_dir_directory"; + let link = "test_rm_symlink_dir_link"; at.mkdir(dir); at.symlink_dir(dir, link); @@ -180,6 +212,30 @@ fn test_rm_dir_symlink() { ucmd.arg(link).succeeds(); } +#[test] +#[cfg(windows)] +// on windows removing symlink_dir requires "-r" or "-d" +fn test_rm_symlink_dir() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let dir = "test_rm_symlink_dir_directory"; + let link = "test_rm_symlink_dir_link"; + + at.mkdir(dir); + at.symlink_dir(dir, link); + + scene + .ucmd() + .arg(link) + .fails() + .stderr_contains(&format!("cannot remove '{}': Is a directory", link)); + + assert!(at.dir_exists(link)); + + scene.ucmd().arg("-r").arg(link).succeeds(); +} + #[test] fn test_rm_invalid_symlink() { let (at, mut ucmd) = at_and_ucmd!(); @@ -202,5 +258,34 @@ fn test_rm_no_operand() { let mut ucmd = new_ucmd!(); ucmd.fails() - .stderr_is("rm: error: missing an argument\nrm: error: for help, try 'rm --help'\n"); + .stderr_is("rm: missing an argument\nrm: for help, try 'rm --help'\n"); +} + +#[test] +fn test_rm_verbose_slash() { + let (at, mut ucmd) = at_and_ucmd!(); + let dir = "test_rm_verbose_slash_directory"; + let file_a = &format!("{}/test_rm_verbose_slash_file_a", dir); + + at.mkdir(dir); + at.touch(file_a); + + let file_a_normalized = &format!( + "{}{}test_rm_verbose_slash_file_a", + dir, + std::path::MAIN_SEPARATOR + ); + + ucmd.arg("-r") + .arg("-f") + .arg("-v") + .arg(&format!("{}///", dir)) + .succeeds() + .stdout_only(format!( + "removed '{}'\nremoved directory '{}'\n", + file_a_normalized, dir + )); + + assert!(!at.dir_exists(dir)); + assert!(!at.file_exists(file_a)); } diff --git a/tests/by-util/test_rmdir.rs b/tests/by-util/test_rmdir.rs index 5f87b5af6..4b74b2522 100644 --- a/tests/by-util/test_rmdir.rs +++ b/tests/by-util/test_rmdir.rs @@ -39,8 +39,8 @@ fn test_rmdir_nonempty_directory_no_parents() { assert!(at.file_exists(file)); ucmd.arg(dir).fails().stderr_is( - "rmdir: error: failed to remove 'test_rmdir_nonempty_no_parents': Directory not \ - empty\n", + "rmdir: failed to remove 'test_rmdir_nonempty_no_parents': Directory not \ + empty\n", ); assert!(at.dir_exists(dir)); @@ -59,10 +59,10 @@ fn test_rmdir_nonempty_directory_with_parents() { assert!(at.file_exists(file)); ucmd.arg("-p").arg(dir).fails().stderr_is( - "rmdir: error: failed to remove 'test_rmdir_nonempty/with/parents': Directory not \ - empty\nrmdir: error: failed to remove 'test_rmdir_nonempty/with': Directory not \ - empty\nrmdir: error: failed to remove 'test_rmdir_nonempty': Directory not \ - empty\n", + "rmdir: failed to remove 'test_rmdir_nonempty/with/parents': Directory not \ + empty\nrmdir: failed to remove 'test_rmdir_nonempty/with': Directory not \ + empty\nrmdir: failed to remove 'test_rmdir_nonempty': Directory not \ + empty\n", ); assert!(at.dir_exists(dir)); @@ -108,3 +108,19 @@ fn test_rmdir_ignore_nonempty_directory_with_parents() { assert!(at.dir_exists(dir)); } + +#[test] +fn test_rmdir_remove_symlink_match_gnu_error() { + let (at, mut ucmd) = at_and_ucmd!(); + + let file = "file"; + let fl = "fl"; + at.touch(file); + assert!(at.file_exists(file)); + at.symlink_file(file, fl); + assert!(at.file_exists(fl)); + + ucmd.arg("fl/") + .fails() + .stderr_is("rmdir: failed to remove 'fl/': Not a directory"); +} diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index a74938377..be04bf1fd 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -1,5 +1,21 @@ use crate::common::util::*; +#[test] +fn test_rejects_nan() { + new_ucmd!().args(&["NaN"]).fails().stderr_only( + "seq: invalid 'not-a-number' argument: 'NaN'\nTry 'seq --help' for more information.", + ); +} + +#[test] +fn test_rejects_non_floats() { + new_ucmd!().args(&["foo"]).fails().stderr_only( + "seq: invalid floating point argument: 'foo'\nTry 'seq --help' for more information.", + ); +} + +// ---- Tests for the big integer based path ---- + #[test] fn test_count_up() { new_ucmd!() @@ -26,6 +42,18 @@ fn test_separator_and_terminator() { .args(&["-s", ",", "-t", "!", "2", "6"]) .run() .stdout_is("2,3,4,5,6!"); + new_ucmd!() + .args(&["-s", ",", "2", "6"]) + .run() + .stdout_is("2,3,4,5,6\n"); + new_ucmd!() + .args(&["-s", "\n", "2", "6"]) + .run() + .stdout_is("2\n3\n4\n5\n6\n"); + new_ucmd!() + .args(&["-s", "\\n", "2", "6"]) + .run() + .stdout_is("2\\n3\\n4\\n5\\n6\n"); } #[test] @@ -45,3 +73,62 @@ fn test_seq_wrong_arg() { fn test_zero_step() { new_ucmd!().args(&["10", "0", "32"]).fails(); } + +#[test] +fn test_big_numbers() { + new_ucmd!() + .args(&[ + "1000000000000000000000000000", + "1000000000000000000000000001", + ]) + .succeeds() + .stdout_only("1000000000000000000000000000\n1000000000000000000000000001\n"); +} + +// ---- Tests for the floating point based path ---- + +#[test] +fn test_count_up_floats() { + new_ucmd!() + .args(&["10.0"]) + .run() + .stdout_is("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"); +} + +#[test] +fn test_count_down_floats() { + new_ucmd!() + .args(&["--", "5", "-1.0", "1"]) + .run() + .stdout_is("5.0\n4.0\n3.0\n2.0\n1.0\n"); + new_ucmd!() + .args(&["5", "-1", "1.0"]) + .run() + .stdout_is("5\n4\n3\n2\n1\n"); +} + +#[test] +fn test_separator_and_terminator_floats() { + new_ucmd!() + .args(&["-s", ",", "-t", "!", "2.0", "6"]) + .run() + .stdout_is("2.0,3.0,4.0,5.0,6.0!"); +} + +#[test] +fn test_equalize_widths_floats() { + new_ucmd!() + .args(&["-w", "5", "10.0"]) + .run() + .stdout_is("05\n06\n07\n08\n09\n10\n"); +} + +#[test] +fn test_seq_wrong_arg_floats() { + new_ucmd!().args(&["-w", "5", "10.0", "33", "32"]).fails(); +} + +#[test] +fn test_zero_step_floats() { + new_ucmd!().args(&["10.0", "0", "32"]).fails(); +} diff --git a/tests/by-util/test_shred.rs b/tests/by-util/test_shred.rs index 651491045..b29b9bfec 100644 --- a/tests/by-util/test_shred.rs +++ b/tests/by-util/test_shred.rs @@ -1 +1,49 @@ -// ToDO: add tests +use crate::common::util::*; + +#[test] +fn test_shred_remove() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let file_a = "test_shred_remove_a"; + let file_b = "test_shred_remove_b"; + + // Create file_a and file_b. + at.touch(file_a); + at.touch(file_b); + + // Shred file_a. + scene.ucmd().arg("-u").arg(file_a).run(); + + // file_a was deleted, file_b exists. + assert!(!at.file_exists(file_a)); + assert!(at.file_exists(file_b)); +} + +#[cfg(not(target_os = "freebsd"))] +#[test] +fn test_shred_force() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let file = "test_shred_force"; + + // Create file_a. + at.touch(file); + assert!(at.file_exists(file)); + + // Make file_a readonly. + at.set_readonly(file); + + // Try shred -u. + scene.ucmd().arg("-u").arg(file).run(); + + // file_a was not deleted because it is readonly. + assert!(at.file_exists(file)); + + // Try shred -u -f. + scene.ucmd().arg("-u").arg("-f").arg(file).run(); + + // file_a was deleted. + assert!(!at.file_exists(file)); +} diff --git a/tests/by-util/test_shuf.rs b/tests/by-util/test_shuf.rs index 651491045..cbc01f8cd 100644 --- a/tests/by-util/test_shuf.rs +++ b/tests/by-util/test_shuf.rs @@ -1 +1,200 @@ -// ToDO: add tests +use crate::common::util::*; + +#[test] +fn test_output_is_random_permutation() { + let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let input = input_seq + .iter() + .map(|x| x.to_string()) + .collect::>() + .join("\n"); + + let result = new_ucmd!().pipe_in(input.as_bytes()).succeeds(); + result.no_stderr(); + + let mut result_seq: Vec = result + .stdout_str() + .split('\n') + .filter(|x| !x.is_empty()) + .map(|x| x.parse().unwrap()) + .collect(); + result_seq.sort_unstable(); + assert_ne!(result.stdout_str(), input, "Output is not randomized"); + assert_eq!(result_seq, input_seq, "Output is not a permutation"); +} + +#[test] +fn test_zero_termination() { + let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let result = new_ucmd!().arg("-z").arg("-i1-10").succeeds(); + result.no_stderr(); + + let mut result_seq: Vec = result + .stdout_str() + .split('\0') + .filter(|x| !x.is_empty()) + .map(|x| x.parse().unwrap()) + .collect(); + result_seq.sort_unstable(); + assert_eq!(result_seq, input_seq, "Output is not a permutation"); +} + +#[test] +fn test_echo() { + let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let result = new_ucmd!() + .arg("-e") + .args( + &input_seq + .iter() + .map(|x| x.to_string()) + .collect::>(), + ) + .succeeds(); + result.no_stderr(); + + let mut result_seq: Vec = result + .stdout_str() + .split('\n') + .filter(|x| !x.is_empty()) + .map(|x| x.parse().unwrap()) + .collect(); + result_seq.sort_unstable(); + assert_eq!(result_seq, input_seq, "Output is not a permutation"); +} + +#[test] +fn test_head_count() { + let repeat_limit = 5; + let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let input = input_seq + .iter() + .map(|x| x.to_string()) + .collect::>() + .join("\n"); + + let result = new_ucmd!() + .args(&["-n", &repeat_limit.to_string()]) + .pipe_in(input.as_bytes()) + .succeeds(); + result.no_stderr(); + + let mut result_seq: Vec = result + .stdout_str() + .split('\n') + .filter(|x| !x.is_empty()) + .map(|x| x.parse().unwrap()) + .collect(); + result_seq.sort_unstable(); + assert_eq!(result_seq.len(), repeat_limit, "Output is not limited"); + assert!( + result_seq.iter().all(|x| input_seq.contains(x)), + "Output includes element not from input: {}", + result.stdout_str() + ) +} + +#[test] +fn test_repeat() { + let repeat_limit = 15000; + let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let input = input_seq + .iter() + .map(|x| x.to_string()) + .collect::>() + .join("\n"); + + let result = new_ucmd!() + .arg("-r") + .args(&["-n", &repeat_limit.to_string()]) + .pipe_in(input.as_bytes()) + .succeeds(); + result.no_stderr(); + + let result_seq: Vec = result + .stdout_str() + .split('\n') + .filter(|x| !x.is_empty()) + .map(|x| x.parse().unwrap()) + .collect(); + assert_eq!( + result_seq.len(), + repeat_limit, + "Output is not repeating forever" + ); + assert!( + result_seq.iter().all(|x| input_seq.contains(x)), + "Output includes element not from input: {:?}", + result_seq + .iter() + .filter(|x| !input_seq.contains(x)) + .collect::>() + ) +} + +#[test] +fn test_file_input() { + let expected_seq = vec![11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; + + let result = new_ucmd!().arg("file_input.txt").succeeds(); + result.no_stderr(); + + let mut result_seq: Vec = result + .stdout_str() + .split('\n') + .filter(|x| !x.is_empty()) + .map(|x| x.parse().unwrap()) + .collect(); + result_seq.sort_unstable(); + assert_eq!(result_seq, expected_seq, "Output is not a permutation"); +} + +#[test] +fn test_shuf_echo_and_input_range_not_allowed() { + new_ucmd!() + .args(&["-e", "0", "-i", "0-2"]) + .fails() + .stderr_contains( + "The argument '--input-range ' cannot be used with '--echo ...'", + ); +} + +#[test] +fn test_shuf_input_range_and_file_not_allowed() { + new_ucmd!() + .args(&["-i", "0-9", "file"]) + .fails() + .stderr_contains("The argument '' cannot be used with '--input-range '"); +} + +#[test] +fn test_shuf_invalid_input_range_one() { + new_ucmd!() + .args(&["-i", "0"]) + .fails() + .stderr_contains("invalid input range"); +} + +#[test] +fn test_shuf_invalid_input_range_two() { + new_ucmd!() + .args(&["-i", "a-9"]) + .fails() + .stderr_contains("invalid input range: 'a'"); +} + +#[test] +fn test_shuf_invalid_input_range_three() { + new_ucmd!() + .args(&["-i", "0-b"]) + .fails() + .stderr_contains("invalid input range: 'b'"); +} + +#[test] +fn test_shuf_invalid_input_line_count() { + new_ucmd!() + .args(&["-n", "a"]) + .fails() + .stderr_contains("invalid line count: 'a'"); +} diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 9ff1b3522..3e841f630 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -1,73 +1,702 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +// spell-checker:ignore (words) ints + use crate::common::util::*; +fn test_helper(file_name: &str, possible_args: &[&str]) { + for args in possible_args { + new_ucmd!() + .arg(format!("{}.txt", file_name)) + .args(&args.split_whitespace().collect::>()) + .succeeds() + .stdout_is_fixture(format!("{}.expected", file_name)); + + new_ucmd!() + .arg(format!("{}.txt", file_name)) + .arg("--debug") + .args(&args.split_whitespace().collect::>()) + .succeeds() + .stdout_is_fixture(format!("{}.expected.debug", file_name)); + } +} + +#[test] +fn test_buffer_sizes() { + let buffer_sizes = ["0", "50K", "50k", "1M", "100M"]; + for buffer_size in &buffer_sizes { + TestScenario::new(util_name!()) + .ucmd_keepenv() + .arg("-n") + .arg("-S") + .arg(buffer_size) + .arg("ext_sort.txt") + .succeeds() + .stdout_is_fixture("ext_sort.expected"); + + #[cfg(not(target_pointer_width = "32"))] + { + let buffer_sizes = ["1000G", "10T"]; + for buffer_size in &buffer_sizes { + TestScenario::new(util_name!()) + .ucmd_keepenv() + .arg("-n") + .arg("-S") + .arg(buffer_size) + .arg("ext_sort.txt") + .succeeds() + .stdout_is_fixture("ext_sort.expected"); + } + } + } +} + +#[test] +fn test_invalid_buffer_size() { + let buffer_sizes = ["asd", "100f"]; + for invalid_buffer_size in &buffer_sizes { + new_ucmd!() + .arg("-S") + .arg(invalid_buffer_size) + .fails() + .code_is(2) + .stderr_only(format!( + "sort: invalid --buffer-size argument '{}'", + invalid_buffer_size + )); + } + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .arg("-n") + .arg("-S") + .arg("1Y") + .arg("ext_sort.txt") + .fails() + .code_is(2) + .stderr_only("sort: --buffer-size argument '1Y' too large"); + + #[cfg(target_pointer_width = "32")] + { + let buffer_sizes = ["1000G", "10T"]; + for buffer_size in &buffer_sizes { + new_ucmd!() + .arg("-n") + .arg("-S") + .arg(buffer_size) + .arg("ext_sort.txt") + .fails() + .code_is(2) + .stderr_only(format!( + "sort: --buffer-size argument '{}' too large", + buffer_size + )); + } + } +} + +#[test] +fn test_ext_sort_stable() { + new_ucmd!() + .arg("-n") + .arg("--stable") + .arg("-S") + .arg("0M") + .arg("ext_stable.txt") + .succeeds() + .stdout_only_fixture("ext_stable.expected"); +} + +#[test] +fn test_ext_sort_zero_terminated() { + new_ucmd!() + .arg("-z") + .arg("-S") + .arg("10K") + .arg("zero-terminated.txt") + .succeeds() + .stdout_is_fixture("zero-terminated.expected"); +} + +#[test] +fn test_months_whitespace() { + test_helper("months-whitespace", &["-M", "--month-sort", "--sort=month"]); +} + +#[test] +fn test_version_empty_lines() { + new_ucmd!() + .arg("-V") + .arg("version-empty-lines.txt") + .succeeds() + .stdout_is("\n\n\n\n\n\n\n1.2.3-alpha\n1.2.3-alpha2\n\t\t\t1.12.4\n11.2.3\n"); +} + +#[test] +fn test_human_numeric_whitespace() { + test_helper( + "human-numeric-whitespace", + &["-h", "--human-numeric-sort", "--sort=human-numeric"], + ); +} + +// This tests where serde often fails when reading back JSON +// if it finds a null value +#[test] +fn test_ext_sort_as64_bailout() { + new_ucmd!() + .arg("-g") + .arg("-S 5K") + .arg("multiple_decimals_general.txt") + .succeeds() + .stdout_is_fixture("multiple_decimals_general.expected"); +} + +#[test] +fn test_multiple_decimals_general() { + test_helper( + "multiple_decimals_general", + &["-g", "--general-numeric-sort", "--sort=general-numeric"], + ) +} + +#[test] +fn test_multiple_decimals_numeric() { + test_helper( + "multiple_decimals_numeric", + &["-n", "--numeric-sort", "--sort=numeric"], + ) +} + +#[test] +fn test_numeric_with_trailing_invalid_chars() { + test_helper( + "numeric_trailing_chars", + &["-n", "--numeric-sort", "--sort=numeric"], + ) +} + +#[test] +fn test_check_zero_terminated_failure() { + new_ucmd!() + .arg("-z") + .arg("-c") + .arg("zero-terminated.txt") + .fails() + .stdout_is("sort: zero-terminated.txt:2: disorder: ../../fixtures/du\n"); +} + +#[test] +fn test_check_zero_terminated_success() { + new_ucmd!() + .arg("-z") + .arg("-c") + .arg("zero-terminated.expected") + .succeeds(); +} + +#[test] +fn test_random_shuffle_len() { + // check whether output is the same length as the input + const FILE: &str = "default_unsorted_ints.expected"; + let (at, _ucmd) = at_and_ucmd!(); + let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str(); + let expected = at.read(FILE); + + assert_ne!(result, expected); + assert_eq!(result.len(), expected.len()); +} + +#[test] +fn test_random_shuffle_contains_all_lines() { + // check whether lines of input are all in output + const FILE: &str = "default_unsorted_ints.expected"; + let (at, _ucmd) = at_and_ucmd!(); + let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str(); + let expected = at.read(FILE); + let result_sorted = new_ucmd!().pipe_in(result.clone()).run().stdout_move_str(); + + assert_ne!(result, expected); + assert_eq!(result_sorted, expected); +} + +#[test] +fn test_random_shuffle_two_runs_not_the_same() { + // check to verify that two random shuffles are not equal; this has the + // potential to fail in the very unlikely event that the random order is the same + // as the starting order, or if both random sorts end up having the same order. + const FILE: &str = "default_unsorted_ints.expected"; + let (at, _ucmd) = at_and_ucmd!(); + let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str(); + let expected = at.read(FILE); + let unexpected = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str(); + + assert_ne!(result, expected); + assert_ne!(result, unexpected); +} + +#[test] +fn test_random_shuffle_contains_two_runs_not_the_same() { + // check to verify that two random shuffles are not equal; this has the + // potential to fail in the unlikely event that random order is the same + // as the starting order, or if both random sorts end up having the same order. + const FILE: &str = "default_unsorted_ints.expected"; + let (at, _ucmd) = at_and_ucmd!(); + let result = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str(); + let expected = at.read(FILE); + let unexpected = new_ucmd!().arg("-R").arg(FILE).run().stdout_move_str(); + + assert_ne!(result, expected); + assert_ne!(result, unexpected); +} + #[test] fn test_numeric_floats_and_ints() { - test_helper("numeric_floats_and_ints", "-n"); + test_helper( + "numeric_floats_and_ints", + &["-n", "--numeric-sort", "--sort=numeric"], + ); } #[test] fn test_numeric_floats() { - test_helper("numeric_floats", "-n"); + test_helper( + "numeric_floats", + &["-n", "--numeric-sort", "--sort=numeric"], + ); } #[test] fn test_numeric_floats_with_nan() { - test_helper("numeric_floats_with_nan", "-n"); + test_helper( + "numeric_floats_with_nan", + &["-n", "--numeric-sort", "--sort=numeric"], + ); } #[test] fn test_numeric_unfixed_floats() { - test_helper("numeric_unfixed_floats", "-n"); + test_helper( + "numeric_unfixed_floats", + &["-n", "--numeric-sort", "--sort=numeric"], + ); } #[test] fn test_numeric_fixed_floats() { - test_helper("numeric_fixed_floats", "-n"); + test_helper( + "numeric_fixed_floats", + &["-n", "--numeric-sort", "--sort=numeric"], + ); } #[test] fn test_numeric_unsorted_ints() { - test_helper("numeric_unsorted_ints", "-n"); + test_helper( + "numeric_unsorted_ints", + &["-n", "--numeric-sort", "--sort=numeric"], + ); } #[test] fn test_human_block_sizes() { - test_helper("human_block_sizes", "-h"); + test_helper( + "human_block_sizes", + &["-h", "--human-numeric-sort", "--sort=human-numeric"], + ); } #[test] fn test_month_default() { - test_helper("month_default", "-M"); + test_helper("month_default", &["-M", "--month-sort", "--sort=month"]); } #[test] fn test_month_stable() { - test_helper("month_stable", "-Ms"); + test_helper("month_stable", &["-Ms"]); } #[test] fn test_default_unsorted_ints() { - test_helper("default_unsorted_ints", ""); + test_helper("default_unsorted_ints", &[""]); } #[test] fn test_numeric_unique_ints() { - test_helper("numeric_unsorted_ints_unique", "-nu"); + test_helper("numeric_unsorted_ints_unique", &["-nu"]); } #[test] fn test_version() { - test_helper("version", "-V"); + test_helper("version", &["-V"]); } #[test] fn test_ignore_case() { - test_helper("ignore_case", "-f"); + test_helper("ignore_case", &["-f"]); } #[test] fn test_dictionary_order() { - test_helper("dictionary_order", "-d"); + test_helper("dictionary_order", &["-d"]); +} + +#[test] +fn test_dictionary_order2() { + for non_dictionary_order2_param in &["-d"] { + new_ucmd!() + .pipe_in("a👦🏻aa b\naaaa b") // spell-checker:disable-line + .arg(non_dictionary_order2_param) // spell-checker:disable-line + .succeeds() + .stdout_only("a👦🏻aa b\naaaa b\n"); // spell-checker:disable-line + } +} + +#[test] +fn test_non_printing_chars() { + for non_printing_chars_param in &["-i"] { + new_ucmd!() + .pipe_in("a👦🏻aa\naaaa") // spell-checker:disable-line + .arg(non_printing_chars_param) // spell-checker:disable-line + .succeeds() + .stdout_only("a👦🏻aa\naaaa\n"); // spell-checker:disable-line + } +} + +#[test] +fn test_exponents_positive_general_fixed() { + test_helper("exponents_general", &["-g"]); +} + +#[test] +fn test_exponents_positive_numeric() { + test_helper( + "exponents-positive-numeric", + &["-n", "--numeric-sort", "--sort=numeric"], + ); +} + +#[test] +fn test_months_dedup() { + test_helper("months-dedup", &["-Mu"]); +} + +#[test] +fn test_mixed_floats_ints_chars_numeric() { + test_helper( + "mixed_floats_ints_chars_numeric", + &["-n", "--numeric-sort", "--sort=numeric"], + ); +} + +#[test] +fn test_mixed_floats_ints_chars_numeric_unique() { + test_helper("mixed_floats_ints_chars_numeric_unique", &["-nu"]); +} + +#[test] +fn test_words_unique() { + test_helper("words_unique", &["-u"]); +} + +#[test] +fn test_numeric_unique() { + test_helper("numeric_unique", &["-nu"]); +} + +#[test] +fn test_mixed_floats_ints_chars_numeric_reverse() { + test_helper("mixed_floats_ints_chars_numeric_unique_reverse", &["-nur"]); +} + +#[test] +fn test_mixed_floats_ints_chars_numeric_stable() { + test_helper("mixed_floats_ints_chars_numeric_stable", &["-ns"]); +} + +#[test] +fn test_numeric_floats_and_ints2() { + for numeric_sort_param in &["-n", "--numeric-sort"] { + let input = "1.444\n8.013\n1\n-8\n1.04\n-1"; + new_ucmd!() + .arg(numeric_sort_param) + .pipe_in(input) + .succeeds() + .stdout_only("-8\n-1\n1\n1.04\n1.444\n8.013\n"); + } +} + +#[test] +fn test_numeric_floats2() { + for numeric_sort_param in &["-n", "--numeric-sort"] { + let input = "1.444\n8.013\n1.58590\n-8.90880\n1.040000000\n-.05"; + new_ucmd!() + .arg(numeric_sort_param) + .pipe_in(input) + .succeeds() + .stdout_only("-8.90880\n-.05\n1.040000000\n1.444\n1.58590\n8.013\n"); + } +} + +#[test] +fn test_numeric_floats_with_nan2() { + test_helper( + "numeric-floats-with-nan2", + &["-n", "--numeric-sort", "--sort=numeric"], + ); +} + +#[test] +fn test_human_block_sizes2() { + for human_numeric_sort_param in &["-h", "--human-numeric-sort", "--sort=human-numeric"] { + let input = "8981K\n909991M\n-8T\n21G\n0.8M"; + new_ucmd!() + .arg(human_numeric_sort_param) + .pipe_in(input) + .succeeds() + .stdout_only("-8T\n8981K\n0.8M\n909991M\n21G\n"); + } +} + +#[test] +fn test_human_numeric_zero_stable() { + let input = "0M\n0K\n-0K\n-P\n-0M\n"; + new_ucmd!() + .arg("-hs") + .pipe_in(input) + .succeeds() + .stdout_only(input); +} + +#[test] +fn test_month_default2() { + for month_sort_param in &["-M", "--month-sort", "--sort=month"] { + let input = "JAn\nMAY\n000may\nJun\nFeb"; + new_ucmd!() + .arg(month_sort_param) + .pipe_in(input) + .succeeds() + .stdout_only("000may\nJAn\nFeb\nMAY\nJun\n"); + } +} + +#[test] +fn test_default_unsorted_ints2() { + let input = "9\n1909888\n000\n1\n2"; + new_ucmd!() + .pipe_in(input) + .succeeds() + .stdout_only("000\n1\n1909888\n2\n9\n"); +} + +#[test] +fn test_numeric_unique_ints2() { + for numeric_unique_sort_param in &["-nu"] { + let input = "9\n9\n8\n1\n"; + new_ucmd!() + .arg(numeric_unique_sort_param) + .pipe_in(input) + .succeeds() + .stdout_only("1\n8\n9\n"); + } +} + +#[test] +fn test_keys_open_ended() { + test_helper("keys_open_ended", &["-k 2.3"]); +} + +#[test] +fn test_keys_closed_range() { + test_helper("keys_closed_range", &["-k 2.2,2.2"]); +} + +#[test] +fn test_keys_multiple_ranges() { + test_helper("keys_multiple_ranges", &["-k 2,2 -k 3,3"]); +} + +#[test] +fn test_keys_no_field_match() { + test_helper("keys_no_field_match", &["-k 4,4"]); +} + +#[test] +fn test_keys_no_char_match() { + test_helper("keys_no_char_match", &["-k 1.2"]); +} + +#[test] +fn test_keys_custom_separator() { + test_helper("keys_custom_separator", &["-k 2.2,2.2 -t x"]); +} + +#[test] +fn test_keys_invalid_field() { + new_ucmd!() + .args(&["-k", "1."]) + .fails() + .stderr_only("sort: failed to parse key `1.`: failed to parse character index ``: cannot parse integer from empty string"); +} + +#[test] +fn test_keys_invalid_field_option() { + new_ucmd!() + .args(&["-k", "1.1x"]) + .fails() + .stderr_only("sort: failed to parse key `1.1x`: invalid option: `x`"); +} + +#[test] +fn test_keys_invalid_field_zero() { + new_ucmd!() + .args(&["-k", "0.1"]) + .fails() + .stderr_only("sort: failed to parse key `0.1`: field index can not be 0"); +} + +#[test] +fn test_keys_invalid_char_zero() { + new_ucmd!() + .args(&["-k", "1.0"]) + .fails() + .stderr_only("sort: failed to parse key `1.0`: invalid character index 0 for the start position of a field"); +} + +#[test] +fn test_keys_with_options() { + let input = "aa 3 cc\ndd 1 ff\ngg 2 cc\n"; + for param in &[ + &["-k", "2,2n"][..], + &["-k", "2n,2"][..], + &["-k", "2,2", "-n"][..], + ] { + new_ucmd!() + .args(param) + .pipe_in(input) + .succeeds() + .stdout_only("dd 1 ff\ngg 2 cc\naa 3 cc\n"); + } +} + +#[test] +fn test_keys_with_options_blanks_start() { + let input = "aa 3 cc\ndd 1 ff\ngg 2 cc\n"; + for param in &[&["-k", "2b,2"][..], &["-k", "2,2", "-b"][..]] { + new_ucmd!() + .args(param) + .pipe_in(input) + .succeeds() + .stdout_only("dd 1 ff\ngg 2 cc\naa 3 cc\n"); + } +} + +#[test] +fn test_keys_blanks_with_char_idx() { + test_helper("keys_blanks", &["-k 1.2b"]) +} + +#[test] +fn test_keys_with_options_blanks_end() { + let input = "a b +a b +a b +"; + new_ucmd!() + .args(&["-k", "1,2.1b", "-s"]) + .pipe_in(input) + .succeeds() + .stdout_only( + "a b +a b +a b +", + ); +} + +#[test] +fn test_keys_stable() { + let input = "a b +a b +a b +"; + new_ucmd!() + .args(&["-k", "1,2.1", "-s"]) + .pipe_in(input) + .succeeds() + .stdout_only( + "a b +a b +a b +", + ); +} + +#[test] +fn test_keys_empty_match() { + let input = "a a a a +aaaa +"; + new_ucmd!() + .args(&["-k", "1,1", "-t", "a"]) + .pipe_in(input) + .succeeds() + .stdout_only(input); +} + +#[test] +fn test_keys_negative_size_match() { + // If the end of a field is before its start, we should not crash. + // Debug output should report "no match for key" at the start position (i.e. the later position). + test_helper("keys_negative_size", &["-k 3,1"]); +} + +#[test] +fn test_keys_ignore_flag() { + test_helper("keys_ignore_flag", &["-k 1n -b"]) +} + +#[test] +fn test_does_not_inherit_key_settings() { + let input = " 1 +2 + 10 +"; + new_ucmd!() + .args(&["-k", "1b", "-n"]) + .pipe_in(input) + .succeeds() + .stdout_only( + " 1 + 10 +2 +", + ); +} + +#[test] +fn test_inherits_key_settings() { + let input = " 1 +2 + 10 +"; + new_ucmd!() + .args(&["-k", "1", "-n"]) + .pipe_in(input) + .succeeds() + .stdout_only( + " 1 +2 + 10 +", + ); +} + +#[test] +fn test_zero_terminated() { + test_helper("zero-terminated", &["-z"]); } #[test] @@ -106,6 +735,18 @@ fn test_merge_unique() { .stdout_only_fixture("merge_ints_interleaved.expected"); } +#[test] +fn test_merge_stable() { + new_ucmd!() + .arg("-m") + .arg("--stable") + .arg("-n") + .arg("merge_stable_1.txt") + .arg("merge_stable_2.txt") + .succeeds() + .stdout_only_fixture("merge_stable.expected"); +} + #[test] fn test_merge_reversed() { new_ucmd!() @@ -133,23 +774,170 @@ fn test_pipe() { #[test] fn test_check() { + for diagnose_arg in &["-c", "--check", "--check=diagnose-first"] { + new_ucmd!() + .arg(diagnose_arg) + .arg("check_fail.txt") + .fails() + .stdout_is("sort: check_fail.txt:6: disorder: 5\n"); + + new_ucmd!() + .arg(diagnose_arg) + .arg("multiple_files.expected") + .succeeds() + .stdout_is(""); + } +} + +#[test] +fn test_check_silent() { + for silent_arg in &["-C", "--check=silent", "--check=quiet"] { + new_ucmd!() + .arg(silent_arg) + .arg("check_fail.txt") + .fails() + .stdout_is(""); + } +} + +#[test] +fn test_dictionary_and_nonprinting_conflicts() { + let conflicting_args = ["n", "h", "g", "M"]; + for restricted_arg in &["d", "i"] { + for conflicting_arg in &conflicting_args { + new_ucmd!() + .arg(&format!("-{}{}", restricted_arg, conflicting_arg)) + .fails(); + } + for conflicting_arg in &conflicting_args { + new_ucmd!() + .args(&[ + format!("-{}", restricted_arg).as_str(), + "-k", + &format!("1,1{}", conflicting_arg), + ]) + .succeeds(); + } + for conflicting_arg in &conflicting_args { + new_ucmd!() + .args(&["-k", &format!("1{},1{}", restricted_arg, conflicting_arg)]) + .fails(); + } + } +} + +#[test] +fn test_trailing_separator() { new_ucmd!() - .arg("-c") - .arg("check_fail.txt") + .args(&["-t", "x", "-k", "1,1"]) + .pipe_in("aax\naaa\n") + .succeeds() + .stdout_is("aax\naaa\n"); +} + +#[test] +fn test_nonexistent_file() { + new_ucmd!() + .arg("nonexistent.txt") .fails() - .stdout_is("sort: disorder in line 4\n"); - - new_ucmd!() - .arg("-c") - .arg("multiple_files.expected") - .succeeds() - .stdout_is(""); + .status_code(2) + .stderr_only( + #[cfg(not(windows))] + "sort: cannot read: \"nonexistent.txt\": No such file or directory (os error 2)", + #[cfg(windows)] + "sort: cannot read: \"nonexistent.txt\": The system cannot find the file specified. (os error 2)", + ); } -fn test_helper(file_name: &str, args: &str) { - new_ucmd!() - .arg(args) - .arg(format!("{}{}", file_name, ".txt")) - .succeeds() - .stdout_is_fixture(format!("{}{}", file_name, ".expected")); +#[test] +fn test_blanks() { + test_helper("blanks", &["-b", "--ignore-leading-blanks"]); +} + +#[test] +fn sort_multiple() { + new_ucmd!() + .args(&["no_trailing_newline1.txt", "no_trailing_newline2.txt"]) + .succeeds() + .stdout_is("a\nb\nb\n"); +} + +#[test] +fn sort_empty_chunk() { + new_ucmd!() + .args(&["-S", "40b"]) + .pipe_in("a\na\n") + .succeeds() + .stdout_is("a\na\n"); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_compress() { + new_ucmd!() + .args(&[ + "ext_sort.txt", + "-n", + "--compress-program", + "gzip", + "-S", + "10", + ]) + .succeeds() + .stdout_only_fixture("ext_sort.expected"); +} + +#[test] +fn test_compress_fail() { + TestScenario::new(util_name!()) + .ucmd_keepenv() + .args(&[ + "ext_sort.txt", + "-n", + "--compress-program", + "nonexistent-program", + "-S", + "10", + ]) + .fails() + .stderr_only("sort: couldn't execute compress program: errno 2"); +} + +#[test] +fn test_merge_batches() { + TestScenario::new(util_name!()) + .ucmd_keepenv() + .args(&["ext_sort.txt", "-n", "-S", "150b"]) + .succeeds() + .stdout_only_fixture("ext_sort.expected"); +} + +#[test] +fn test_merge_batch_size() { + TestScenario::new(util_name!()) + .ucmd_keepenv() + .arg("--batch-size=2") + .arg("-m") + .arg("--unique") + .arg("merge_ints_interleaved_1.txt") + .arg("merge_ints_interleaved_2.txt") + .arg("merge_ints_interleaved_3.txt") + .arg("merge_ints_interleaved_3.txt") + .arg("merge_ints_interleaved_2.txt") + .arg("merge_ints_interleaved_1.txt") + .succeeds() + .stdout_only_fixture("merge_ints_interleaved.expected"); +} + +#[test] +fn test_sigpipe_panic() { + let mut cmd = new_ucmd!(); + let mut child = cmd.args(&["ext_sort.txt"]).run_no_wait(); + // Dropping the stdout should not lead to an error. + // The "Broken pipe" error should be silently ignored. + drop(child.stdout.take()); + assert_eq!( + String::from_utf8(child.wait_with_output().unwrap().stderr), + Ok(String::new()) + ); } diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index 521cbbe9a..229925a1c 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -1,14 +1,23 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + extern crate rand; extern crate regex; use self::rand::{thread_rng, Rng}; use self::regex::Regex; use crate::common::util::*; +use rand::SeedableRng; #[cfg(not(windows))] use std::env; -use std::fs::{read_dir, File}; use std::io::Write; use std::path::Path; +use std::{ + fs::{read_dir, File}, + io::BufWriter, +}; fn random_chars(n: usize) -> String { thread_rng() @@ -58,7 +67,7 @@ impl Glob { files.sort(); let mut data: Vec = vec![]; for name in &files { - data.extend(self.directory.read(name).into_bytes()); + data.extend(self.directory.read_bytes(name)); } data } @@ -81,20 +90,30 @@ impl RandomFile { } fn add_bytes(&mut self, bytes: usize) { - let chunk_size: usize = if bytes >= 1024 { 1024 } else { bytes }; - let mut n = bytes; - while n > chunk_size { - let _ = write!(self.inner, "{}", random_chars(chunk_size)); - n -= chunk_size; + // Note that just writing random characters isn't enough to cover all + // cases. We need truly random bytes. + let mut writer = BufWriter::new(&self.inner); + + // Seed the rng so as to avoid spurious test failures. + let mut rng = rand::rngs::StdRng::seed_from_u64(123); + let mut buffer = [0; 1024]; + let mut remaining_size = bytes; + + while remaining_size > 0 { + let to_write = std::cmp::min(remaining_size, buffer.len()); + let buf = &mut buffer[..to_write]; + rng.fill(buf); + writer.write_all(buf).unwrap(); + + remaining_size -= to_write; } - let _ = write!(self.inner, "{}", random_chars(n)); } /// Add n lines each of size `RandomFile::LINESIZE` fn add_lines(&mut self, lines: usize) { let mut n = lines; while n > 0 { - let _ = writeln!(self.inner, "{}", random_chars(RandomFile::LINESIZE)); + writeln!(self.inner, "{}", random_chars(RandomFile::LINESIZE)).unwrap(); n -= 1; } } @@ -104,18 +123,18 @@ impl RandomFile { fn test_split_default() { let (at, mut ucmd) = at_and_ucmd!(); let name = "split_default"; - let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); RandomFile::new(&at, name).add_lines(2000); ucmd.args(&[name]).succeeds(); + + let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); assert_eq!(glob.count(), 2); - assert_eq!(glob.collate(), at.read(name).into_bytes()); + assert_eq!(glob.collate(), at.read_bytes(name)); } #[test] fn test_split_numeric_prefixed_chunks_by_bytes() { let (at, mut ucmd) = at_and_ucmd!(); let name = "split_num_prefixed_chunks_by_bytes"; - let glob = Glob::new(&at, ".", r"a\d\d$"); RandomFile::new(&at, name).add_bytes(10000); ucmd.args(&[ "-d", // --numeric-suffixes @@ -123,52 +142,90 @@ fn test_split_numeric_prefixed_chunks_by_bytes() { "1000", name, "a", ]) .succeeds(); + + let glob = Glob::new(&at, ".", r"a\d\d$"); assert_eq!(glob.count(), 10); - assert_eq!(glob.collate(), at.read(name).into_bytes()); + for filename in glob.collect() { + assert_eq!(glob.directory.metadata(&filename).len(), 1000); + } + assert_eq!(glob.collate(), at.read_bytes(name)); } #[test] fn test_split_str_prefixed_chunks_by_bytes() { let (at, mut ucmd) = at_and_ucmd!(); let name = "split_str_prefixed_chunks_by_bytes"; - let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$"); RandomFile::new(&at, name).add_bytes(10000); + // Important that this is less than 1024 since that's our internal buffer + // size. Good to test that we don't overshoot. ucmd.args(&["-b", "1000", name, "b"]).succeeds(); + + let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$"); assert_eq!(glob.count(), 10); - assert_eq!(glob.collate(), at.read(name).into_bytes()); + for filename in glob.collect() { + assert_eq!(glob.directory.metadata(&filename).len(), 1000); + } + assert_eq!(glob.collate(), at.read_bytes(name)); +} + +// This is designed to test what happens when the desired part size is not a +// multiple of the buffer size and we hopefully don't overshoot the desired part +// size. +#[test] +fn test_split_bytes_prime_part_size() { + let (at, mut ucmd) = at_and_ucmd!(); + let name = "test_split_bytes_prime_part_size"; + RandomFile::new(&at, name).add_bytes(10000); + // 1753 is prime and greater than the buffer size, 1024. + ucmd.args(&["-b", "1753", name, "b"]).succeeds(); + + let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$"); + assert_eq!(glob.count(), 6); + let mut fns = glob.collect(); + // glob.collect() is not guaranteed to return in sorted order, so we sort. + fns.sort(); + #[allow(clippy::needless_range_loop)] + for i in 0..5 { + assert_eq!(glob.directory.metadata(&fns[i]).len(), 1753); + } + assert_eq!(glob.directory.metadata(&fns[5]).len(), 1235); + assert_eq!(glob.collate(), at.read_bytes(name)); } #[test] fn test_split_num_prefixed_chunks_by_lines() { let (at, mut ucmd) = at_and_ucmd!(); let name = "split_num_prefixed_chunks_by_lines"; - let glob = Glob::new(&at, ".", r"c\d\d$"); RandomFile::new(&at, name).add_lines(10000); ucmd.args(&["-d", "-l", "1000", name, "c"]).succeeds(); + + let glob = Glob::new(&at, ".", r"c\d\d$"); assert_eq!(glob.count(), 10); - assert_eq!(glob.collate(), at.read(name).into_bytes()); + assert_eq!(glob.collate(), at.read_bytes(name)); } #[test] fn test_split_str_prefixed_chunks_by_lines() { let (at, mut ucmd) = at_and_ucmd!(); let name = "split_str_prefixed_chunks_by_lines"; - let glob = Glob::new(&at, ".", r"d[[:alpha:]][[:alpha:]]$"); RandomFile::new(&at, name).add_lines(10000); ucmd.args(&["-l", "1000", name, "d"]).succeeds(); + + let glob = Glob::new(&at, ".", r"d[[:alpha:]][[:alpha:]]$"); assert_eq!(glob.count(), 10); - assert_eq!(glob.collate(), at.read(name).into_bytes()); + assert_eq!(glob.collate(), at.read_bytes(name)); } #[test] fn test_split_additional_suffix() { let (at, mut ucmd) = at_and_ucmd!(); let name = "split_additional_suffix"; - let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]].txt$"); RandomFile::new(&at, name).add_lines(2000); ucmd.args(&["--additional-suffix", ".txt", name]).succeeds(); + + let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]].txt$"); assert_eq!(glob.count(), 2); - assert_eq!(glob.collate(), at.read(name).into_bytes()); + assert_eq!(glob.collate(), at.read_bytes(name)); } // note: the test_filter* tests below are unix-only @@ -182,21 +239,22 @@ fn test_filter() { // like `test_split_default()` but run a command before writing let (at, mut ucmd) = at_and_ucmd!(); let name = "filtered"; - let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); let n_lines = 3; RandomFile::new(&at, name).add_lines(n_lines); // change all characters to 'i' ucmd.args(&["--filter=sed s/./i/g > $FILE", name]) .succeeds(); + // assert all characters are 'i' / no character is not 'i' - // (assert that command succeded) + // (assert that command succeeded) + let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); assert!( glob.collate().iter().find(|&&c| { // is not i - c != ('i' as u8) + c != (b'i') // is not newline - && c != ('\n' as u8) + && c != (b'\n') }) == None ); } @@ -209,16 +267,17 @@ fn test_filter_with_env_var_set() { // implemented like `test_split_default()` but run a command before writing let (at, mut ucmd) = at_and_ucmd!(); let name = "filtered"; - let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); let n_lines = 3; RandomFile::new(&at, name).add_lines(n_lines); - let env_var_value = "somevalue"; + let env_var_value = "some-value"; env::set_var("FILE", &env_var_value); ucmd.args(&[format!("--filter={}", "cat > $FILE").as_str(), name]) .succeeds(); - assert_eq!(glob.collate(), at.read(name).into_bytes()); - assert!(env::var("FILE").unwrap_or("var was unset".to_owned()) == env_var_value); + + let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); + assert_eq!(glob.collate(), at.read_bytes(name)); + assert!(env::var("FILE").unwrap_or_else(|_| "var was unset".to_owned()) == env_var_value); } #[test] @@ -231,3 +290,53 @@ fn test_filter_command_fails() { ucmd.args(&["--filter=/a/path/that/totally/does/not/exist", name]) .fails(); } + +#[test] +fn test_split_lines_number() { + // Test if stdout/stderr for '--lines' option is correct + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch("file"); + + scene + .ucmd() + .args(&["--lines", "2", "file"]) + .succeeds() + .no_stderr() + .no_stdout(); + scene + .ucmd() + .args(&["--lines", "2fb", "file"]) + .fails() + .code_is(1) + .stderr_only("split: invalid number of lines: '2fb'"); +} + +#[test] +fn test_split_invalid_bytes_size() { + new_ucmd!() + .args(&["-b", "1024R"]) + .fails() + .code_is(1) + .stderr_only("split: invalid number of bytes: '1024R'"); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .args(&["-b", "1Y"]) + .fails() + .code_is(1) + .stderr_only("split: invalid number of bytes: '1Y': Value too large for defined data type"); + #[cfg(target_pointer_width = "32")] + { + let sizes = ["1000G", "10T"]; + for size in &sizes { + new_ucmd!() + .args(&["-b", size]) + .fails() + .code_is(1) + .stderr_only(format!( + "split: invalid number of bytes: '{}': Value too large for defined data type", + size + )); + } + } +} diff --git a/tests/by-util/test_stat.rs b/tests/by-util/test_stat.rs index 225ea52cd..ddf78815f 100644 --- a/tests/by-util/test_stat.rs +++ b/tests/by-util/test_stat.rs @@ -5,84 +5,21 @@ use crate::common::util::*; extern crate stat; pub use self::stat::*; -#[cfg(test)] -mod test_fsext { - use super::*; - - #[test] - fn test_access() { - assert_eq!("drwxr-xr-x", pretty_access(S_IFDIR | 0o755)); - assert_eq!("-rw-r--r--", pretty_access(S_IFREG | 0o644)); - assert_eq!("srw-r-----", pretty_access(S_IFSOCK | 0o640)); - assert_eq!("lrw-r-xr-x", pretty_access(S_IFLNK | 0o655)); - assert_eq!("?rw-r-xr-x", pretty_access(0o655)); - - assert_eq!( - "brwSr-xr-x", - pretty_access(S_IFBLK | S_ISUID as mode_t | 0o655) - ); - assert_eq!( - "brwsr-xr-x", - pretty_access(S_IFBLK | S_ISUID as mode_t | 0o755) - ); - - assert_eq!( - "prw---sr--", - pretty_access(S_IFIFO | S_ISGID as mode_t | 0o614) - ); - assert_eq!( - "prw---Sr--", - pretty_access(S_IFIFO | S_ISGID as mode_t | 0o604) - ); - - assert_eq!( - "c---r-xr-t", - pretty_access(S_IFCHR | S_ISVTX as mode_t | 0o055) - ); - assert_eq!( - "c---r-xr-T", - pretty_access(S_IFCHR | S_ISVTX as mode_t | 0o054) - ); - } - - #[test] - fn test_file_type() { - assert_eq!("block special file", pretty_filetype(S_IFBLK, 0)); - assert_eq!("character special file", pretty_filetype(S_IFCHR, 0)); - assert_eq!("regular file", pretty_filetype(S_IFREG, 1)); - assert_eq!("regular empty file", pretty_filetype(S_IFREG, 0)); - assert_eq!("weird file", pretty_filetype(0, 0)); - } - - #[test] - fn test_fs_type() { - assert_eq!("ext2/ext3", pretty_fstype(0xEF53)); - assert_eq!("tmpfs", pretty_fstype(0x01021994)); - assert_eq!("nfs", pretty_fstype(0x6969)); - assert_eq!("btrfs", pretty_fstype(0x9123683e)); - assert_eq!("xfs", pretty_fstype(0x58465342)); - assert_eq!("zfs", pretty_fstype(0x2FC12FC1)); - assert_eq!("ntfs", pretty_fstype(0x5346544e)); - assert_eq!("fat", pretty_fstype(0x4006)); - assert_eq!("UNKNOWN (0x1234)", pretty_fstype(0x1234)); - } -} - #[test] -fn test_scanutil() { +fn test_scanners() { assert_eq!(Some((-5, 2)), "-5zxc".scan_num::()); assert_eq!(Some((51, 2)), "51zxc".scan_num::()); assert_eq!(Some((192, 4)), "+192zxc".scan_num::()); assert_eq!(None, "z192zxc".scan_num::()); assert_eq!(Some(('a', 3)), "141zxc".scan_char(8)); - assert_eq!(Some(('\n', 2)), "12qzxc".scan_char(8)); - assert_eq!(Some(('\r', 1)), "dqzxc".scan_char(16)); - assert_eq!(None, "z2qzxc".scan_char(8)); + assert_eq!(Some(('\n', 2)), "12qzxc".scan_char(8)); // spell-checker:disable-line + assert_eq!(Some(('\r', 1)), "dqzxc".scan_char(16)); // spell-checker:disable-line + assert_eq!(None, "z2qzxc".scan_char(8)); // spell-checker:disable-line } #[test] -fn test_groupnum() { +fn test_group_num() { assert_eq!("12,379,821,234", group_num("12379821234")); assert_eq!("21,234", group_num("21234")); assert_eq!("821,234", group_num("821234")); @@ -159,14 +96,14 @@ fn test_invalid_option() { new_ucmd!().arg("-w").arg("-q").arg("/").fails(); } -#[cfg(target_os = "linux")] -const NORMAL_FMTSTR: &'static str = +#[cfg(any(target_os = "linux", target_vendor = "apple"))] +const NORMAL_FORMAT_STR: &str = "%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s %u %U %x %X %y %Y %z %Z"; // avoid "%w %W" (birth/creation) due to `stat` limitations and linux kernel & rust version capability variations -#[cfg(target_os = "linux")] -const DEV_FMTSTR: &'static str = +#[cfg(any(target_os = "linux"))] +const DEV_FORMAT_STR: &str = "%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s (%t/%T) %u %U %w %W %x %X %y %Y %z %Z"; #[cfg(target_os = "linux")] -const FS_FMTSTR: &'static str = "%b %c %i %l %n %s %S %t %T"; // avoid "%a %d %f" which can cause test failure due to race conditions +const FS_FORMAT_STR: &str = "%b %c %i %l %n %s %S %t %T"; // avoid "%a %d %f" which can cause test failure due to race conditions #[test] #[cfg(target_os = "linux")] @@ -181,26 +118,33 @@ fn test_terse_fs_format() { #[test] #[cfg(target_os = "linux")] fn test_fs_format() { - let args = ["-f", "-c", FS_FMTSTR, "/dev/shm"]; + let args = ["-f", "-c", FS_FORMAT_STR, "/dev/shm"]; new_ucmd!() .args(&args) .run() .stdout_is(expected_result(&args)); } +#[cfg(any(target_os = "linux", target_vendor = "apple"))] #[test] -#[cfg(target_os = "linux")] fn test_terse_normal_format() { // note: contains birth/creation date which increases test fragility // * results may vary due to built-in `stat` limitations as well as linux kernel and rust version capability variations let args = ["-t", "/"]; - let actual = new_ucmd!().args(&args).run().stdout; + let actual = new_ucmd!().args(&args).succeeds().stdout_move_str(); let expect = expected_result(&args); println!("actual: {:?}", actual); println!("expect: {:?}", expect); - let v_actual: Vec<&str> = actual.split(' ').collect(); - let v_expect: Vec<&str> = expect.split(' ').collect(); + let v_actual: Vec<&str> = actual.trim().split(' ').collect(); + let mut v_expect: Vec<&str> = expect.trim().split(' ').collect(); assert!(!v_expect.is_empty()); + + // uu_stat does not support selinux + if v_actual.len() == v_expect.len() - 1 && v_expect[v_expect.len() - 1].contains(':') { + // assume last element contains: `SELinux security context string` + v_expect.pop(); + } + // * allow for inequality if `stat` (aka, expect) returns "0" (unknown value) assert!( expect == "0" @@ -212,11 +156,11 @@ fn test_terse_normal_format() { ); } +#[cfg(any(target_os = "linux", target_vendor = "apple"))] #[test] -#[cfg(target_os = "linux")] fn test_format_created_time() { - let args = ["-c", "%w", "/boot"]; - let actual = new_ucmd!().args(&args).run().stdout; + let args = ["-c", "%w", "/bin"]; + let actual = new_ucmd!().args(&args).succeeds().stdout_move_str(); let expect = expected_result(&args); println!("actual: {:?}", actual); println!("expect: {:?}", expect); @@ -236,11 +180,11 @@ fn test_format_created_time() { ); } +#[cfg(any(target_os = "linux", target_vendor = "apple"))] #[test] -#[cfg(target_os = "linux")] fn test_format_created_seconds() { - let args = ["-c", "%W", "/boot"]; - let actual = new_ucmd!().args(&args).run().stdout; + let args = ["-c", "%W", "/bin"]; + let actual = new_ucmd!().args(&args).succeeds().stdout_move_str(); let expect = expected_result(&args); println!("actual: {:?}", actual); println!("expect: {:?}", expect); @@ -260,65 +204,97 @@ fn test_format_created_seconds() { ); } +#[cfg(any(target_os = "linux", target_vendor = "apple"))] #[test] -#[cfg(target_os = "linux")] fn test_normal_format() { - let args = ["-c", NORMAL_FMTSTR, "/boot"]; + let args = ["-c", NORMAL_FORMAT_STR, "/bin"]; new_ucmd!() .args(&args) - .run() + .succeeds() .stdout_is(expected_result(&args)); } +#[cfg(any(target_os = "linux", target_vendor = "apple"))] #[test] -#[cfg(target_os = "linux")] -fn test_follow_symlink() { - let args = ["-L", "-c", DEV_FMTSTR, "/dev/cdrom"]; - new_ucmd!() - .args(&args) - .run() - .stdout_is(expected_result(&args)); +fn test_symlinks() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let mut tested: bool = false; + // arbitrarily chosen symlinks with hope that the CI environment provides at least one of them + for file in &[ + "/bin/sh", + "/bin/sudoedit", + "/usr/bin/ex", + "/etc/localtime", + "/etc/aliases", + ] { + if at.file_exists(file) && at.is_symlink(file) { + tested = true; + let args = ["-c", NORMAL_FORMAT_STR, file]; + scene + .ucmd() + .args(&args) + .succeeds() + .stdout_is(expected_result(&args)); + // -L, --dereference follow links + let args = ["-L", "-c", NORMAL_FORMAT_STR, file]; + scene + .ucmd() + .args(&args) + .succeeds() + .stdout_is(expected_result(&args)); + } + } + if !tested { + panic!("No symlink found to test in this environment"); + } } +#[cfg(any(target_os = "linux", target_vendor = "apple"))] #[test] -#[cfg(target_os = "linux")] -fn test_symlink() { - let args = ["-c", DEV_FMTSTR, "/dev/cdrom"]; - new_ucmd!() - .args(&args) - .run() - .stdout_is(expected_result(&args)); -} - -#[test] -#[cfg(target_os = "linux")] fn test_char() { - let args = ["-c", DEV_FMTSTR, "/dev/pts/ptmx"]; + // TODO: "(%t) (%x) (%w)" deviate from GNU stat for `character special file` on macOS + // Diff < left / right > : + // <"(f0000) (2021-05-20 23:08:03.442555000 +0200) (1970-01-01 01:00:00.000000000 +0100)\n" + // >"(f) (2021-05-20 23:08:03.455598000 +0200) (-)\n" + let args = [ + "-c", + #[cfg(target_os = "linux")] + DEV_FORMAT_STR, + #[cfg(target_os = "linux")] + "/dev/pts/ptmx", + #[cfg(any(target_vendor = "apple"))] + "%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s (/%T) %u %U %W %X %y %Y %z %Z", + #[cfg(any(target_vendor = "apple"))] + "/dev/ptmx", + ]; new_ucmd!() .args(&args) - .run() + .succeeds() .stdout_is(expected_result(&args)); } +#[cfg(any(target_os = "linux", target_vendor = "apple"))] #[test] -#[cfg(target_os = "linux")] fn test_multi_files() { let args = [ "-c", - NORMAL_FMTSTR, + NORMAL_FORMAT_STR, "/dev", "/usr/lib", + #[cfg(target_os = "linux")] "/etc/fstab", "/var", ]; new_ucmd!() .args(&args) - .run() + .succeeds() .stdout_is(expected_result(&args)); } +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] -#[cfg(target_os = "linux")] fn test_printf() { let args = [ "--printf=123%-# 15q\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.23m\\x12\\167\\132\\112\\n", @@ -326,16 +302,23 @@ fn test_printf() { ]; new_ucmd!() .args(&args) - .run() + .succeeds() .stdout_is(expected_result(&args)); } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] fn expected_result(args: &[&str]) -> String { - TestScenario::new(util_name!()) - .cmd_keepenv(util_name!()) - .env("LANGUAGE", "C") + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(target_vendor = "apple")] + let util_name = format!("g{}", util_name!()); + + // note: clippy::needless_borrow *false positive* + #[allow(clippy::needless_borrow)] + TestScenario::new(&util_name) + .cmd_keepenv(util_name) + .env("LC_ALL", "C") .args(args) - .run() - .stdout + .succeeds() + .stdout_move_str() } diff --git a/tests/by-util/test_stdbuf.rs b/tests/by-util/test_stdbuf.rs index 9adb0cfda..66892ea0f 100644 --- a/tests/by-util/test_stdbuf.rs +++ b/tests/by-util/test_stdbuf.rs @@ -1,13 +1,74 @@ +#[cfg(not(target_os = "windows"))] use crate::common::util::*; +#[cfg(not(target_os = "windows"))] #[test] fn test_stdbuf_unbuffered_stdout() { - if cfg!(target_os = "linux") { - // This is a basic smoke test + // This is a basic smoke test + new_ucmd!() + .args(&["-o0", "head"]) + .pipe_in("The quick brown fox jumps over the lazy dog.") + .run() + .stdout_is("The quick brown fox jumps over the lazy dog."); +} + +#[cfg(not(target_os = "windows"))] +#[test] +fn test_stdbuf_line_buffered_stdout() { + new_ucmd!() + .args(&["-oL", "head"]) + .pipe_in("The quick brown fox jumps over the lazy dog.") + .run() + .stdout_is("The quick brown fox jumps over the lazy dog."); +} + +#[cfg(not(target_os = "windows"))] +#[test] +fn test_stdbuf_no_buffer_option_fails() { + new_ucmd!().args(&["head"]).fails().stderr_is( + "error: The following required arguments were not provided:\n \ + --error \n \ + --input \n \ + --output \n\n\ + USAGE:\n \ + stdbuf OPTION... COMMAND\n\n\ + For more information try --help", + ); +} + +#[cfg(not(target_os = "windows"))] +#[test] +fn test_stdbuf_trailing_var_arg() { + new_ucmd!() + .args(&["-i", "1024", "tail", "-1"]) + .pipe_in("The quick brown fox\njumps over the lazy dog.") + .run() + .stdout_is("jumps over the lazy dog."); +} + +#[cfg(not(target_os = "windows"))] +#[test] +fn test_stdbuf_line_buffering_stdin_fails() { + new_ucmd!().args(&["-i", "L", "head"]).fails().stderr_is( + "stdbuf: line buffering stdin is meaningless\nTry 'stdbuf --help' for more information.", + ); +} + +#[cfg(not(target_os = "windows"))] +#[test] +fn test_stdbuf_invalid_mode_fails() { + let options = ["--input", "--output", "--error"]; + for option in &options { new_ucmd!() - .args(&["-o0", "head"]) - .pipe_in("The quick brown fox jumps over the lazy dog.") - .run() - .stdout_is("The quick brown fox jumps over the lazy dog."); + .args(&[*option, "1024R", "head"]) + .fails() + .code_is(125) + .stderr_only("stdbuf: invalid mode '1024R'"); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .args(&[*option, "1Y", "head"]) + .fails() + .code_is(125) + .stderr_contains("stdbuf: invalid mode '1Y': Value too large for defined data type"); } } diff --git a/tests/by-util/test_sum.rs b/tests/by-util/test_sum.rs index 83cf689ac..f09ba9d00 100644 --- a/tests/by-util/test_sum.rs +++ b/tests/by-util/test_sum.rs @@ -52,3 +52,21 @@ fn test_sysv_stdin() { .succeeds() .stdout_only_fixture("sysv_stdin.expected"); } + +#[test] +fn test_invalid_file() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("a"); + + ucmd.arg("a").fails().stderr_is("sum: 'a' Is a directory"); +} + +#[test] +fn test_invalid_metadata() { + let (_, mut ucmd) = at_and_ucmd!(); + + ucmd.arg("b") + .fails() + .stderr_is("sum: 'b' No such file or directory"); +} diff --git a/tests/by-util/test_sync.rs b/tests/by-util/test_sync.rs index ddd6969a3..033651910 100644 --- a/tests/by-util/test_sync.rs +++ b/tests/by-util/test_sync.rs @@ -5,8 +5,7 @@ use tempfile::tempdir; #[test] fn test_sync_default() { - let result = new_ucmd!().run(); - assert!(result.success); + new_ucmd!().succeeds(); } #[test] @@ -18,8 +17,10 @@ fn test_sync_incorrect_arg() { fn test_sync_fs() { let temporary_directory = tempdir().unwrap(); let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap(); - let result = new_ucmd!().arg("--file-system").arg(&temporary_path).run(); - assert!(result.success); + new_ucmd!() + .arg("--file-system") + .arg(&temporary_path) + .succeeds(); } #[test] @@ -27,12 +28,14 @@ fn test_sync_data() { // Todo add a second arg let temporary_directory = tempdir().unwrap(); let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap(); - let result = new_ucmd!().arg("--data").arg(&temporary_path).run(); - assert!(result.success); + new_ucmd!().arg("--data").arg(&temporary_path).succeeds(); } #[test] fn test_sync_no_existing_files() { - let result = new_ucmd!().arg("--data").arg("do-no-exist").fails(); - assert!(result.stderr.contains("error: cannot stat")); + new_ucmd!() + .arg("--data") + .arg("do-no-exist") + .fails() + .stderr_contains("cannot stat"); } diff --git a/tests/by-util/test_tac.rs b/tests/by-util/test_tac.rs index d46f9aec6..a8adbb28e 100644 --- a/tests/by-util/test_tac.rs +++ b/tests/by-util/test_tac.rs @@ -49,3 +49,22 @@ fn test_single_non_newline_separator_before() { .run() .stdout_is_fixture("delimited_primes_before.expected"); } + +#[test] +fn test_invalid_input() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + scene + .ucmd() + .arg("b") + .fails() + .stderr_contains("failed to open 'b' for reading: No such file or directory"); + + at.mkdir("a"); + scene + .ucmd() + .arg("a") + .fails() + .stderr_contains("dir: read error: Invalid argument"); +} diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index 458fc6aa7..e8dd63317 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -1,13 +1,19 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +// spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile + extern crate tail; -use self::tail::parse_size; use crate::common::util::*; use std::char::from_digit; use std::io::Write; -static FOOBAR_TXT: &'static str = "foobar.txt"; -static FOOBAR_2_TXT: &'static str = "foobar2.txt"; -static FOOBAR_WITH_NULL_TXT: &'static str = "foobar_with_null.txt"; +static FOOBAR_TXT: &str = "foobar.txt"; +static FOOBAR_2_TXT: &str = "foobar2.txt"; +static FOOBAR_WITH_NULL_TXT: &str = "foobar_with_null.txt"; #[test] fn test_stdin_default() { @@ -78,7 +84,7 @@ fn test_follow_multiple() { at.append(FOOBAR_2_TXT, first_append); assert_eq!(read_size(&mut child, first_append.len()), first_append); - let second_append = "doce\ntrece\n"; + let second_append = "twenty\nthirty\n"; let expected = at.read("foobar_follow_multiple_appended.expected"); at.append(FOOBAR_TXT, second_append); assert_eq!(read_size(&mut child, expected.len()), expected); @@ -95,7 +101,7 @@ fn test_follow_stdin() { .stdout_is_fixture("follow_stdin.expected"); } -// FixME: test PASSES for usual windows builds, but fails for coverage testing builds (likely related to the specific RUSTFLAGS '-Zpanic_abort_tests -Cpanic=abort') This test also breaks tty settings under bash requiring a 'stty sane' or reset. +// FixME: test PASSES for usual windows builds, but fails for coverage testing builds (likely related to the specific RUSTFLAGS '-Zpanic_abort_tests -Cpanic=abort') This test also breaks tty settings under bash requiring a 'stty sane' or reset. // spell-checker:disable-line #[cfg(disable_until_fixed)] #[test] fn test_follow_with_pid() { @@ -130,7 +136,7 @@ fn test_follow_with_pid() { at.append(FOOBAR_2_TXT, first_append); assert_eq!(read_size(&mut child, first_append.len()), first_append); - let second_append = "doce\ntrece\n"; + let second_append = "twenty\nthirty\n"; let expected = at.read("foobar_follow_multiple_appended.expected"); at.append(FOOBAR_TXT, second_append); assert_eq!(read_size(&mut child, expected.len()), expected); @@ -153,8 +159,8 @@ fn test_follow_with_pid() { #[test] fn test_single_big_args() { - const FILE: &'static str = "single_big_args.txt"; - const EXPECTED_FILE: &'static str = "single_big_args_expected.txt"; + const FILE: &str = "single_big_args.txt"; + const EXPECTED_FILE: &str = "single_big_args_expected.txt"; const LINES: usize = 1_000_000; const N_ARG: usize = 100_000; @@ -162,13 +168,13 @@ fn test_single_big_args() { let mut big_input = at.make_file(FILE); for i in 0..LINES { - write!(&mut big_input, "Line {}\n", i).expect("Could not write to FILE"); + writeln!(&mut big_input, "Line {}", i).expect("Could not write to FILE"); } big_input.flush().expect("Could not flush FILE"); let mut big_expected = at.make_file(EXPECTED_FILE); for i in (LINES - N_ARG)..LINES { - write!(&mut big_expected, "Line {}\n", i).expect("Could not write to EXPECTED_FILE"); + writeln!(&mut big_expected, "Line {}", i).expect("Could not write to EXPECTED_FILE"); } big_expected.flush().expect("Could not flush EXPECTED_FILE"); @@ -201,8 +207,8 @@ fn test_bytes_stdin() { #[test] fn test_bytes_big() { - const FILE: &'static str = "test_bytes_big.txt"; - const EXPECTED_FILE: &'static str = "test_bytes_big_expected.txt"; + const FILE: &str = "test_bytes_big.txt"; + const EXPECTED_FILE: &str = "test_bytes_big_expected.txt"; const BYTES: usize = 1_000_000; const N_ARG: usize = 100_000; @@ -226,8 +232,8 @@ fn test_bytes_big() { .arg(FILE) .arg("-c") .arg(format!("{}", N_ARG)) - .run() - .stdout; + .succeeds() + .stdout_move_str(); let expected = at.read(EXPECTED_FILE); assert_eq!(result.len(), expected.len()); @@ -236,45 +242,10 @@ fn test_bytes_big() { } } -#[test] -fn test_parse_size() { - // No suffix. - assert_eq!(Ok(1234), parse_size("1234")); - - // kB is 1000 - assert_eq!(Ok(9 * 1000), parse_size("9kB")); - - // K is 1024 - assert_eq!(Ok(2 * 1024), parse_size("2K")); - - let suffixes = [ - ('M', 2u32), - ('G', 3u32), - ('T', 4u32), - ('P', 5u32), - ('E', 6u32), - ]; - - for &(c, exp) in &suffixes { - let s = format!("2{}B", c); - assert_eq!(Ok(2 * (1000 as u64).pow(exp)), parse_size(&s)); - - let s = format!("2{}", c); - assert_eq!(Ok(2 * (1024 as u64).pow(exp)), parse_size(&s)); - } - - // Sizes that are too big. - assert!(parse_size("1Z").is_err()); - assert!(parse_size("1Y").is_err()); - - // Bad number - assert!(parse_size("328hdsf3290").is_err()); -} - #[test] fn test_lines_with_size_suffix() { - const FILE: &'static str = "test_lines_with_size_suffix.txt"; - const EXPECTED_FILE: &'static str = "test_lines_with_size_suffix_expected.txt"; + const FILE: &str = "test_lines_with_size_suffix.txt"; + const EXPECTED_FILE: &str = "test_lines_with_size_suffix_expected.txt"; const LINES: usize = 3_000; const N_ARG: usize = 2 * 1024; @@ -320,12 +291,128 @@ fn test_multiple_input_files_with_suppressed_headers() { #[test] fn test_multiple_input_quiet_flag_overrides_verbose_flag_for_suppressing_headers() { - // TODO: actually the later one should win, i.e. -qv should lead to headers being printed, -vq to them being suppressed new_ucmd!() .arg(FOOBAR_TXT) .arg(FOOBAR_2_TXT) - .arg("-q") .arg("-v") + .arg("-q") .run() .stdout_is_fixture("foobar_multiple_quiet.expected"); } + +#[test] +fn test_negative_indexing() { + let positive_lines_index = new_ucmd!().arg("-n").arg("5").arg(FOOBAR_TXT).run(); + + let negative_lines_index = new_ucmd!().arg("-n").arg("-5").arg(FOOBAR_TXT).run(); + + let positive_bytes_index = new_ucmd!().arg("-c").arg("20").arg(FOOBAR_TXT).run(); + + let negative_bytes_index = new_ucmd!().arg("-c").arg("-20").arg(FOOBAR_TXT).run(); + + assert_eq!(positive_lines_index.stdout(), negative_lines_index.stdout()); + assert_eq!(positive_bytes_index.stdout(), negative_bytes_index.stdout()); +} + +#[test] +fn test_sleep_interval() { + new_ucmd!().arg("-s").arg("10").arg(FOOBAR_TXT).succeeds(); +} + +/// Test for reading all but the first NUM bytes: `tail -c +3`. +#[test] +fn test_positive_bytes() { + new_ucmd!() + .args(&["-c", "+3"]) + .pipe_in("abcde") + .succeeds() + .stdout_is("cde"); +} + +/// Test for reading all bytes, specified by `tail -c +0`. +#[test] +fn test_positive_zero_bytes() { + new_ucmd!() + .args(&["-c", "+0"]) + .pipe_in("abcde") + .succeeds() + .stdout_is("abcde"); +} + +/// Test for reading all but the first NUM lines: `tail -n +3`. +#[test] +fn test_positive_lines() { + new_ucmd!() + .args(&["-n", "+3"]) + .pipe_in("a\nb\nc\nd\ne\n") + .succeeds() + .stdout_is("c\nd\ne\n"); +} + +/// Test for reading all lines, specified by `tail -n +0`. +#[test] +fn test_positive_zero_lines() { + new_ucmd!() + .args(&["-n", "+0"]) + .pipe_in("a\nb\nc\nd\ne\n") + .succeeds() + .stdout_is("a\nb\nc\nd\ne\n"); +} + +#[test] +fn test_tail_invalid_num() { + new_ucmd!() + .args(&["-c", "1024R", "emptyfile.txt"]) + .fails() + .stderr_is("tail: invalid number of bytes: '1024R'"); + new_ucmd!() + .args(&["-n", "1024R", "emptyfile.txt"]) + .fails() + .stderr_is("tail: invalid number of lines: '1024R'"); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .args(&["-c", "1Y", "emptyfile.txt"]) + .fails() + .stderr_is("tail: invalid number of bytes: '1Y': Value too large for defined data type"); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .args(&["-n", "1Y", "emptyfile.txt"]) + .fails() + .stderr_is("tail: invalid number of lines: '1Y': Value too large for defined data type"); + #[cfg(target_pointer_width = "32")] + { + let sizes = ["1000G", "10T"]; + for size in &sizes { + new_ucmd!() + .args(&["-c", size]) + .fails() + .code_is(1) + .stderr_only(format!( + "tail: invalid number of bytes: '{}': Value too large for defined data type", + size + )); + } + } +} + +#[test] +fn test_tail_num_with_undocumented_sign_bytes() { + // tail: '-' is not documented (8.32 man pages) + // head: '+' is not documented (8.32 man pages) + const ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz"; + new_ucmd!() + .args(&["-c", "5"]) + .pipe_in(ALPHABET) + .succeeds() + .stdout_is("vwxyz"); + new_ucmd!() + .args(&["-c", "-5"]) + .pipe_in(ALPHABET) + .succeeds() + .stdout_is("vwxyz"); + new_ucmd!() + .args(&["-c", "+5"]) + .pipe_in(ALPHABET) + .succeeds() + .stdout_is("efghijklmnopqrstuvwxyz"); +} diff --git a/tests/by-util/test_tee.rs b/tests/by-util/test_tee.rs index 651491045..f2587a11f 100644 --- a/tests/by-util/test_tee.rs +++ b/tests/by-util/test_tee.rs @@ -1 +1,104 @@ -// ToDO: add tests +use crate::common::util::*; + +// tests for basic tee functionality. +// inspired by: +// https://github.com/coreutils/coreutils/tests/misc/tee.sh + +#[test] +fn test_tee_processing_multiple_operands() { + // POSIX says: "Processing of at least 13 file operands shall be supported." + + let content = "tee_sample_content"; + for &n in [1, 2, 12, 13].iter() { + let files = (1..=n).map(|x| x.to_string()).collect::>(); + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.args(&files) + .pipe_in(content) + .succeeds() + .stdout_is(content); + + for file in files.iter() { + assert!(at.file_exists(file)); + assert_eq!(at.read(file), content); + } + } +} + +#[test] +fn test_tee_treat_minus_as_filename() { + // Ensure tee treats '-' as the name of a file, as mandated by POSIX. + + let (at, mut ucmd) = at_and_ucmd!(); + let content = "tee_sample_content"; + let file = "-"; + + ucmd.arg("-").pipe_in(content).succeeds().stdout_is(content); + + assert!(at.file_exists(file)); + assert_eq!(at.read(file), content); +} + +#[test] +fn test_tee_append() { + let (at, mut ucmd) = at_and_ucmd!(); + let content = "tee_sample_content"; + let file = "tee_out"; + + at.touch(file); + at.write(file, content); + assert_eq!(at.read(file), content); + + ucmd.arg("-a") + .arg(file) + .pipe_in(content) + .succeeds() + .stdout_is(content); + assert!(at.file_exists(file)); + assert_eq!(at.read(file), content.repeat(2)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_tee_no_more_writeable_1() { + // equals to 'tee /dev/full out2 (); + let file_out = "tee_file_out"; + + ucmd.arg("/dev/full") + .arg(file_out) + .pipe_in(&content[..]) + .fails() + .stdout_contains(&content) + .stderr_contains(&"No space left on device"); + + assert_eq!(at.read(file_out), content); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_tee_no_more_writeable_2() { + // should be equals to 'tee out1 out2 >/dev/full (); + let file_out_a = "tee_file_out_a"; + let file_out_b = "tee_file_out_b"; + + let _result = ucmd + .arg(file_out_a) + .arg(file_out_b) + .pipe_in("/dev/full") + .succeeds(); // TODO: expected to succeed currently; change to fails() when required + + // TODO: comment in after https://github.com/uutils/coreutils/issues/1805 is fixed + // assert_eq!(at.read(file_out_a), content); + // assert_eq!(at.read(file_out_b), content); + // assert!(result.stderr.contains("No space left on device")); +} diff --git a/tests/by-util/test_test.rs b/tests/by-util/test_test.rs index 4b9a9ff55..1867927da 100644 --- a/tests/by-util/test_test.rs +++ b/tests/by-util/test_test.rs @@ -2,26 +2,719 @@ // This file is part of the uutils coreutils package. // // (c) mahkoh (ju.orth [at] gmail [dot] com) +// (c) Daniel Rocco // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. // +// spell-checker:ignore (words) egid euid pseudofloat + use crate::common::util::*; #[test] -fn test_op_prec_and_or_1() { +fn test_empty_test_equivalent_to_false() { + new_ucmd!().run().status_code(1); +} + +#[test] +fn test_empty_string_is_false() { + new_ucmd!().arg("").run().status_code(1); +} + +#[test] +fn test_solo_not() { + new_ucmd!().arg("!").succeeds(); +} + +#[test] +fn test_solo_and_or_or_is_a_literal() { + // /bin/test '' -a '' => 1; so test(1) must interpret `-a` by itself as + // a literal string + new_ucmd!().arg("-a").succeeds(); + new_ucmd!().arg("-o").succeeds(); +} + +#[test] +fn test_double_not_is_false() { + new_ucmd!().args(&["!", "!"]).run().status_code(1); +} + +#[test] +fn test_and_not_is_false() { + new_ucmd!().args(&["-a", "!"]).run().status_code(1); +} + +#[test] +fn test_not_and_is_false() { + // `-a` is a literal here & has nonzero length + new_ucmd!().args(&["!", "-a"]).run().status_code(1); +} + +#[test] +fn test_not_and_not_succeeds() { + new_ucmd!().args(&["!", "-a", "!"]).succeeds(); +} + +#[test] +fn test_simple_or() { + new_ucmd!().args(&["foo", "-o", ""]).succeeds(); +} + +#[test] +fn test_negated_or() { + new_ucmd!() + .args(&["!", "foo", "-o", "bar"]) + .run() + .status_code(1); + new_ucmd!().args(&["foo", "-o", "!", "bar"]).succeeds(); + new_ucmd!() + .args(&["!", "foo", "-o", "!", "bar"]) + .run() + .status_code(1); +} + +#[test] +fn test_string_length_of_nothing() { + // odd but matches GNU, which must interpret -n as a literal here + new_ucmd!().arg("-n").succeeds(); +} + +#[test] +fn test_string_length_of_empty() { + new_ucmd!().args(&["-n", ""]).run().status_code(1); + + // STRING equivalent to -n STRING + new_ucmd!().arg("").run().status_code(1); +} + +#[test] +fn test_nothing_is_empty() { + // -z is a literal here and has nonzero length + new_ucmd!().arg("-z").succeeds(); +} + +#[test] +fn test_zero_len_of_empty() { + new_ucmd!().args(&["-z", ""]).succeeds(); +} + +#[test] +fn test_solo_parenthesis_is_literal() { + let scenario = TestScenario::new(util_name!()); + let tests = [["("], [")"]]; + + for test in &tests { + scenario.ucmd().args(&test[..]).succeeds(); + } +} + +#[test] +fn test_solo_empty_parenthetical_is_error() { + new_ucmd!().args(&["(", ")"]).run().status_code(2); +} + +#[test] +fn test_zero_len_equals_zero_len() { + new_ucmd!().args(&["", "=", ""]).succeeds(); +} + +#[test] +fn test_zero_len_not_equals_zero_len_is_false() { + new_ucmd!().args(&["", "!=", ""]).run().status_code(1); +} + +#[test] +fn test_double_equal_is_string_comparison_op() { + // undocumented but part of the GNU test suite + new_ucmd!().args(&["t", "==", "t"]).succeeds(); + new_ucmd!().args(&["t", "==", "f"]).run().status_code(1); +} + +#[test] +fn test_string_comparison() { + let scenario = TestScenario::new(util_name!()); + let tests = [ + ["foo", "!=", "bar"], + ["contained\nnewline", "=", "contained\nnewline"], + ["(", "=", "("], + ["(", "!=", ")"], + ["!", "=", "!"], + ["=", "=", "="], + ]; + + for test in &tests { + scenario.ucmd().args(&test[..]).succeeds(); + } + + // run the inverse of all these tests + for test in &tests { + scenario + .ucmd() + .arg("!") + .args(&test[..]) + .run() + .status_code(1); + } +} + +#[test] +#[ignore = "fixme: error reporting"] +fn test_dangling_string_comparison_is_error() { + new_ucmd!() + .args(&["missing_something", "="]) + .run() + .status_code(2) + .stderr_is("test: missing argument after '='"); +} + +#[test] +fn test_string_operator_is_literal_after_bang() { + let scenario = TestScenario::new(util_name!()); + let tests = [ + ["!", "="], + ["!", "!="], + ["!", "-eq"], + ["!", "-ne"], + ["!", "-lt"], + ["!", "-le"], + ["!", "-gt"], + ["!", "-ge"], + ["!", "-ef"], + ["!", "-nt"], + ["!", "-ot"], + ]; + + for test in &tests { + scenario.ucmd().args(&test[..]).run().status_code(1); + } +} + +#[test] +fn test_a_bunch_of_not() { + new_ucmd!() + .args(&["!", "", "!=", "", "-a", "!", "", "!=", ""]) + .succeeds(); +} + +#[test] +fn test_pseudofloat_equal() { + new_ucmd!().args(&["123.45", "=", "123.45"]).succeeds(); +} + +#[test] +fn test_pseudofloat_not_equal() { + new_ucmd!().args(&["123.45", "!=", "123.450"]).succeeds(); +} + +#[test] +fn test_negative_arg_is_a_string() { + new_ucmd!().arg("-12345").succeeds(); + new_ucmd!().arg("--qwert").succeeds(); // spell-checker:disable-line +} + +#[test] +fn test_some_int_compares() { + let scenario = TestScenario::new(util_name!()); + + let tests = [ + ["0", "-eq", "0"], + ["0", "-ne", "1"], + ["421", "-lt", "3720"], + ["0", "-le", "0"], + ["11", "-gt", "10"], + ["1024", "-ge", "512"], + ["9223372036854775806", "-le", "9223372036854775807"], + ]; + + for test in &tests { + scenario.ucmd().args(&test[..]).succeeds(); + } +} + +#[test] +#[ignore = "fixme: evaluation error (code 1); GNU returns 0"] +fn test_values_greater_than_i64_allowed() { + new_ucmd!() + .args(&["9223372036854775808", "-gt", "0"]) + .succeeds(); +} + +#[test] +fn test_negative_int_compare() { + let scenario = TestScenario::new(util_name!()); + + let tests = [ + ["-1", "-eq", "-1"], + ["-1", "-ne", "-2"], + ["-3720", "-lt", "-421"], + ["-10", "-le", "-10"], + ["-21", "-gt", "-22"], + ["-128", "-ge", "-256"], + ["-9223372036854775808", "-le", "-9223372036854775807"], + ]; + + for test in &tests { + scenario.ucmd().args(&test[..]).succeeds(); + } +} + +#[test] +fn test_float_inequality_is_error() { + new_ucmd!() + .args(&["123.45", "-ge", "6"]) + .run() + .status_code(2) + .stderr_is("test: invalid integer '123.45'"); +} + +#[test] +#[cfg(not(windows))] +fn test_invalid_utf8_integer_compare() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + + let source = [0x66, 0x6f, 0x80, 0x6f]; + let arg = OsStr::from_bytes(&source[..]); + + let mut cmd = new_ucmd!(); + cmd.arg("123").arg("-ne"); + cmd.raw.arg(arg); + + cmd.run() + .status_code(2) + .stderr_is("test: invalid integer 'fo�o'"); + + let mut cmd = new_ucmd!(); + cmd.raw.arg(arg); + cmd.arg("-eq").arg("456"); + + cmd.run() + .status_code(2) + .stderr_is("test: invalid integer 'fo�o'"); +} + +#[test] +#[ignore = "fixme: parse/evaluation error (code 2); GNU returns 1"] +fn test_file_is_itself() { + new_ucmd!() + .args(&["regular_file", "-ef", "regular_file"]) + .succeeds(); +} + +#[test] +#[ignore = "fixme: parse/evaluation error (code 2); GNU returns 1"] +fn test_file_is_newer_than_and_older_than_itself() { + // odd but matches GNU + new_ucmd!() + .args(&["regular_file", "-nt", "regular_file"]) + .run() + .status_code(1); + new_ucmd!() + .args(&["regular_file", "-ot", "regular_file"]) + .run() + .status_code(1); +} + +#[test] +#[ignore = "todo: implement these"] +fn test_newer_file() { + let scenario = TestScenario::new(util_name!()); + + scenario.cmd("touch").arg("newer_file").succeeds(); + scenario + .cmd("touch") + .args(&["-m", "-d", "last Thursday", "regular_file"]) + .succeeds(); + + scenario + .ucmd() + .args(&["newer_file", "-nt", "regular_file"]) + .succeeds(); + scenario + .ucmd() + .args(&["regular_file", "-ot", "newer_file"]) + .succeeds(); +} + +#[test] +fn test_file_exists() { + new_ucmd!().args(&["-e", "regular_file"]).succeeds(); +} + +#[test] +fn test_nonexistent_file_does_not_exist() { + new_ucmd!() + .args(&["-e", "nonexistent_file"]) + .run() + .status_code(1); +} + +#[test] +fn test_nonexistent_file_is_not_regular() { + new_ucmd!() + .args(&["-f", "nonexistent_file"]) + .run() + .status_code(1); +} + +#[test] +fn test_file_exists_and_is_regular() { + new_ucmd!().args(&["-f", "regular_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] // FIXME: implement on Windows +fn test_file_is_readable() { + new_ucmd!().args(&["-r", "regular_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] // FIXME: implement on Windows +fn test_file_is_not_readable() { + let scenario = TestScenario::new(util_name!()); + let mut ucmd = scenario.ucmd(); + let mut chmod = scenario.cmd("chmod"); + + scenario.fixtures.touch("crypto_file"); + chmod.args(&["u-r", "crypto_file"]).succeeds(); + + ucmd.args(&["!", "-r", "crypto_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] // FIXME: implement on Windows +fn test_file_is_writable() { + new_ucmd!().args(&["-w", "regular_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] // FIXME: implement on Windows +fn test_file_is_not_writable() { + let scenario = TestScenario::new(util_name!()); + let mut ucmd = scenario.ucmd(); + let mut chmod = scenario.cmd("chmod"); + + scenario.fixtures.touch("immutable_file"); + chmod.args(&["u-w", "immutable_file"]).succeeds(); + + ucmd.args(&["!", "-w", "immutable_file"]).succeeds(); +} + +#[test] +fn test_file_is_not_executable() { + new_ucmd!().args(&["!", "-x", "regular_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] // FIXME: implement on Windows +fn test_file_is_executable() { + let scenario = TestScenario::new(util_name!()); + let mut chmod = scenario.cmd("chmod"); + + chmod.args(&["u+x", "regular_file"]).succeeds(); + + scenario.ucmd().args(&["-x", "regular_file"]).succeeds(); +} + +#[test] +fn test_is_not_empty() { + new_ucmd!().args(&["-s", "non_empty_file"]).succeeds(); +} + +#[test] +fn test_nonexistent_file_size_test_is_false() { + new_ucmd!() + .args(&["-s", "nonexistent_file"]) + .run() + .status_code(1); +} + +#[test] +fn test_not_is_not_empty() { + new_ucmd!().args(&["!", "-s", "regular_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] +fn test_symlink_is_symlink() { + let scenario = TestScenario::new(util_name!()); + let at = &scenario.fixtures; + + at.symlink_file("regular_file", "symlink"); + + // FIXME: implement on Windows + scenario.ucmd().args(&["-h", "symlink"]).succeeds(); + scenario.ucmd().args(&["-L", "symlink"]).succeeds(); +} + +#[test] +fn test_file_is_not_symlink() { + let scenario = TestScenario::new(util_name!()); + + scenario + .ucmd() + .args(&["!", "-h", "regular_file"]) + .succeeds(); + scenario + .ucmd() + .args(&["!", "-L", "regular_file"]) + .succeeds(); +} + +#[test] +fn test_nonexistent_file_is_not_symlink() { + let scenario = TestScenario::new(util_name!()); + + scenario + .ucmd() + .args(&["!", "-h", "nonexistent_file"]) + .succeeds(); + scenario + .ucmd() + .args(&["!", "-L", "nonexistent_file"]) + .succeeds(); +} + +#[test] +#[cfg(not(windows))] // Windows has no concept of sticky bit +fn test_file_is_sticky() { + let scenario = TestScenario::new(util_name!()); + let mut ucmd = scenario.ucmd(); + let mut chmod = scenario.cmd("chmod"); + + scenario.fixtures.touch("sticky_file"); + chmod.args(&["+t", "sticky_file"]).succeeds(); + + ucmd.args(&["-k", "sticky_file"]).succeeds(); +} + +#[test] +fn test_file_is_not_sticky() { + new_ucmd!() + .args(&["-k", "regular_file"]) + .run() + .status_code(1); +} + +#[test] +#[cfg(not(windows))] +fn test_file_owned_by_euid() { + new_ucmd!().args(&["-O", "regular_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] +fn test_nonexistent_file_not_owned_by_euid() { + new_ucmd!() + .args(&["-O", "nonexistent_file"]) + .run() + .status_code(1); +} + +#[test] +#[cfg(all(not(windows), not(target_os = "freebsd")))] +fn test_file_not_owned_by_euid() { + new_ucmd!() + .args(&["-f", "/bin/sh", "-a", "!", "-O", "/bin/sh"]) + .succeeds(); +} + +#[test] +#[cfg(not(windows))] +fn test_file_owned_by_egid() { + new_ucmd!().args(&["-G", "regular_file"]).succeeds(); +} + +#[test] +#[cfg(not(windows))] +fn test_nonexistent_file_not_owned_by_egid() { + new_ucmd!() + .args(&["-G", "nonexistent_file"]) + .run() + .status_code(1); +} + +#[test] +#[cfg(all(not(windows), not(target_os = "freebsd")))] +fn test_file_not_owned_by_egid() { + new_ucmd!() + .args(&["-f", "/bin/sh", "-a", "!", "-G", "/bin/sh"]) + .succeeds(); +} + +#[test] +fn test_op_precedence_and_or_1() { new_ucmd!().args(&[" ", "-o", "", "-a", ""]).succeeds(); } #[test] -fn test_op_prec_and_or_2() { +fn test_op_precedence_and_or_1_overridden_by_parentheses() { + new_ucmd!() + .args(&["(", " ", "-o", "", ")", "-a", ""]) + .run() + .status_code(1); +} + +#[test] +fn test_op_precedence_and_or_2() { new_ucmd!() .args(&["", "-a", "", "-o", " ", "-a", " "]) .succeeds(); } #[test] -fn test_or_as_filename() { - new_ucmd!().args(&["x", "-a", "-z", "-o"]).fails(); +fn test_op_precedence_and_or_2_overridden_by_parentheses() { + new_ucmd!() + .args(&["", "-a", "(", "", "-o", " ", ")", "-a", " "]) + .run() + .status_code(1); +} + +#[test] +fn test_negated_boolean_precedence() { + let scenario = TestScenario::new(util_name!()); + + let tests = [ + vec!["!", "(", "foo", ")", "-o", "bar"], + vec!["!", "", "-o", "", "-a", ""], + vec!["!", "(", "", "-a", "", ")", "-o", ""], + ]; + + for test in &tests { + scenario.ucmd().args(&test[..]).succeeds(); + } + + let negative_tests = [ + vec!["!", "-n", "", "-a", ""], + vec!["", "-a", "", "-o", ""], + vec!["!", "", "-a", "", "-o", ""], + vec!["!", "(", "", "-a", "", ")", "-a", ""], + ]; + + for test in &negative_tests { + scenario.ucmd().args(&test[..]).run().status_code(1); + } +} + +#[test] +fn test_bang_bool_op_precedence() { + // For a Boolean combination of two literals, bang inverts the entire expression + new_ucmd!().args(&["!", "", "-a", ""]).succeeds(); + new_ucmd!().args(&["!", "", "-o", ""]).succeeds(); + + new_ucmd!() + .args(&["!", "a value", "-o", "another value"]) + .run() + .status_code(1); + + // Introducing a UOP — even one that is equivalent to a bare string — causes + // bang to invert only the first term + new_ucmd!() + .args(&["!", "-n", "", "-a", ""]) + .run() + .status_code(1); + new_ucmd!() + .args(&["!", "", "-a", "-n", ""]) + .run() + .status_code(1); + + // for compound Boolean expressions, bang inverts the _next_ expression + // only, not the entire compound expression + new_ucmd!() + .args(&["!", "", "-a", "", "-a", ""]) + .run() + .status_code(1); + + // parentheses can override this + new_ucmd!() + .args(&["!", "(", "", "-a", "", "-a", "", ")"]) + .succeeds(); +} + +#[test] +fn test_inverted_parenthetical_bool_op_precedence() { + // For a Boolean combination of two literals, bang inverts the entire expression + new_ucmd!() + .args(&["!", "a value", "-o", "another value"]) + .run() + .status_code(1); + + // only the parenthetical is inverted, not the entire expression + new_ucmd!() + .args(&["!", "(", "a value", ")", "-o", "another value"]) + .succeeds(); +} + +#[test] +#[ignore = "fixme: error reporting"] +fn test_dangling_parenthesis() { + new_ucmd!() + .args(&["(", "(", "a", "!=", "b", ")", "-o", "-n", "c"]) + .run() + .status_code(2); + new_ucmd!() + .args(&["(", "(", "a", "!=", "b", ")", "-o", "-n", "c", ")"]) + .succeeds(); +} + +#[test] +fn test_complicated_parenthesized_expression() { + new_ucmd!() + .args(&[ + "(", "(", "!", "(", "a", "=", "b", ")", "-o", "c", "=", "d", ")", "-a", "(", "q", "!=", + "r", ")", ")", + ]) + .succeeds(); +} + +#[test] +fn test_erroneous_parenthesized_expression() { + new_ucmd!() + .args(&["a", "!=", "(", "b", "-a", "b", ")", "!=", "c"]) + .run() + .status_code(2) + .stderr_is("test: extra argument 'b'"); +} + +#[test] +fn test_or_as_filename() { + new_ucmd!() + .args(&["x", "-a", "-z", "-o"]) + .run() + .status_code(1); +} + +#[test] +#[ignore = "GNU considers this an error"] +fn test_string_length_and_nothing() { + new_ucmd!().args(&["-n", "a", "-a"]).run().status_code(2); +} + +#[test] +fn test_bracket_syntax_success() { + let scenario = TestScenario::new("["); + let mut ucmd = scenario.ucmd(); + + ucmd.args(&["1", "-eq", "1", "]"]).succeeds(); +} + +#[test] +fn test_bracket_syntax_failure() { + let scenario = TestScenario::new("["); + let mut ucmd = scenario.ucmd(); + + ucmd.args(&["1", "-eq", "2", "]"]).run().status_code(1); +} + +#[test] +fn test_bracket_syntax_missing_right_bracket() { + let scenario = TestScenario::new("["); + let mut ucmd = scenario.ucmd(); + + // Missing closing bracket takes precedence over other possible errors. + ucmd.args(&["1", "-eq"]) + .run() + .status_code(2) + .stderr_is("[: missing ']'"); } diff --git a/tests/by-util/test_timeout.rs b/tests/by-util/test_timeout.rs index 592516cca..9be29065a 100644 --- a/tests/by-util/test_timeout.rs +++ b/tests/by-util/test_timeout.rs @@ -4,8 +4,44 @@ use crate::common::util::*; // the best solution is probably to generate some test binaries that we can call for any // utility that requires executing another program (kill, for instance) #[test] -fn test_subcommand_retcode() { +fn test_subcommand_return_code() { new_ucmd!().arg("1").arg("true").succeeds(); new_ucmd!().arg("1").arg("false").run().status_code(1); } + +#[test] +fn test_command_with_args() { + new_ucmd!() + .args(&["1700", "echo", "-n", "abcd"]) + .succeeds() + .stdout_only("abcd"); +} + +#[test] +fn test_verbose() { + for &verbose_flag in &["-v", "--verbose"] { + new_ucmd!() + .args(&[verbose_flag, ".1", "sleep", "10"]) + .fails() + .stderr_only("timeout: sending signal TERM to command 'sleep'"); + new_ucmd!() + .args(&[verbose_flag, "-s0", "-k.1", ".1", "sleep", "10"]) + .fails() + .stderr_only("timeout: sending signal EXIT to command 'sleep'\ntimeout: sending signal KILL to command 'sleep'"); + } +} + +#[test] +fn test_zero_timeout() { + new_ucmd!() + .args(&["-v", "0", "sleep", ".1"]) + .succeeds() + .no_stderr() + .no_stdout(); + new_ucmd!() + .args(&["-v", "0", "-s0", "-k0", "sleep", ".1"]) + .succeeds() + .no_stderr() + .no_stdout(); +} diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 9921c16b5..3ed7f3bb2 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -1,9 +1,12 @@ +// spell-checker:ignore (formats) cymdhm cymdhms mdhm mdhms ymdhm ymdhms + extern crate touch; use self::touch::filetime::{self, FileTime}; extern crate time; use crate::common::util::*; +use std::path::PathBuf; fn get_file_times(at: &AtPath, path: &str) -> (FileTime, FileTime) { let m = at.metadata(path); @@ -29,6 +32,7 @@ fn set_file_times(at: &AtPath, path: &str, atime: FileTime, mtime: FileTime) { fn str_to_filetime(format: &str, s: &str) -> FileTime { let mut tm = time::strptime(s, format).unwrap(); tm.tm_utcoff = time::now().tm_utcoff; + tm.tm_isdst = -1; // Unknown flag DST let ts = tm.to_timespec(); FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32) } @@ -352,3 +356,166 @@ fn test_touch_set_date() { assert_eq!(atime, start_of_year); assert_eq!(mtime, start_of_year); } + +#[test] +fn test_touch_set_date2() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date"; + + ucmd.args(&["-d", "2000-01-23", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let start_of_year = str_to_filetime("%Y%m%d%H%M", "200001230000"); + let (atime, mtime) = get_file_times(&at, file); + assert_eq!(atime, mtime); + assert_eq!(atime, start_of_year); + assert_eq!(mtime, start_of_year); +} + +#[test] +fn test_touch_set_date3() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date"; + + ucmd.args(&["-d", "@1623786360", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let expected = FileTime::from_unix_time(1623786360, 0); + let (atime, mtime) = get_file_times(&at, file); + assert_eq!(atime, mtime); + assert_eq!(atime, expected); + assert_eq!(mtime, expected); +} + +#[test] +fn test_touch_set_date_wrong_format() { + let (_at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date_wrong_format"; + + ucmd.args(&["-d", "2005-43-21", file]) + .fails() + .stderr_contains("Unable to parse date: 2005-43-21"); +} + +#[test] +fn test_touch_mtime_dst_succeeds() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_mtime_dst_succeeds"; + + ucmd.args(&["-m", "-t", "202103140300", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let target_time = str_to_filetime("%Y%m%d%H%M", "202103140300"); + let (_, mtime) = get_file_times(&at, file); + assert!(target_time == mtime); +} + +// is_dst_switch_hour returns true if timespec ts is just before the switch +// to Daylight Saving Time. +// For example, in EST (UTC-5), Timespec { sec: 1583647200, nsec: 0 } +// for March 8 2020 01:00:00 AM +// is just before the switch because on that day clock jumps by 1 hour, +// so 1 minute after 01:59:00 is 03:00:00. +fn is_dst_switch_hour(ts: time::Timespec) -> bool { + let ts_after = ts + time::Duration::hours(1); + let tm = time::at(ts); + let tm_after = time::at(ts_after); + tm_after.tm_hour == tm.tm_hour + 2 +} + +// get_dst_switch_hour returns date string for which touch -m -t fails. +// For example, in EST (UTC-5), that will be "202003080200" so +// touch -m -t 202003080200 file +// fails (that date/time does not exist). +// In other locales it will be a different date/time, and in some locales +// it doesn't exist at all, in which case this function will return None. +fn get_dst_switch_hour() -> Option { + let now = time::now(); + // Start from January 1, 2020, 00:00. + let mut tm = time::strptime("20200101-0000", "%Y%m%d-%H%M").unwrap(); + tm.tm_isdst = -1; + tm.tm_utcoff = now.tm_utcoff; + let mut ts = tm.to_timespec(); + // Loop through all hours in year 2020 until we find the hour just + // before the switch to DST. + for _i in 0..(366 * 24) { + if is_dst_switch_hour(ts) { + let mut tm = time::at(ts); + tm.tm_hour += 1; + let s = time::strftime("%Y%m%d%H%M", &tm).unwrap(); + return Some(s); + } + ts = ts + time::Duration::hours(1); + } + None +} + +#[test] +fn test_touch_mtime_dst_fails() { + let (_at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_mtime_dst_fails"; + + if let Some(s) = get_dst_switch_hour() { + ucmd.args(&["-m", "-t", &s, file]).fails(); + } +} + +#[test] +#[cfg(unix)] +fn test_touch_system_fails() { + let (_at, mut ucmd) = at_and_ucmd!(); + let file = "/"; + ucmd.args(&[file]) + .fails() + .stderr_contains("setting times of '/'"); +} + +#[test] +fn test_touch_trailing_slash() { + let (_at, mut ucmd) = at_and_ucmd!(); + let file = "no-file/"; + ucmd.args(&[file]).fails(); +} + +#[test] +fn test_touch_no_such_file_error_msg() { + let dirname = "nonexistent"; + let filename = "file"; + let path = PathBuf::from(dirname).join(filename); + let path_str = path.to_str().unwrap(); + + new_ucmd!().arg(&path).fails().stderr_only(format!( + "touch: cannot touch '{}': No such file or directory", + path_str + )); +} + +#[test] +#[cfg(unix)] +fn test_touch_permission_denied_error_msg() { + let (at, mut ucmd) = at_and_ucmd!(); + + let dirname = "dir_with_read_only_access"; + let filename = "file"; + let path = PathBuf::from(dirname).join(filename); + let path_str = path.to_str().unwrap(); + + // create dest without write permissions + at.mkdir(dirname); + at.set_readonly(dirname); + + let full_path = at.plus_as_string(path_str); + ucmd.arg(&full_path).fails().stderr_only(format!( + "touch: cannot touch '{}': Permission denied", + &full_path + )); +} diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index b32d98d29..936af2ca8 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -1,7 +1,7 @@ use crate::common::util::*; #[test] -fn test_toupper() { +fn test_to_upper() { new_ucmd!() .args(&["a-z", "A-Z"]) .pipe_in("!abcd!") @@ -45,6 +45,70 @@ fn test_delete_complement() { .stdout_is("ac"); } +#[test] +fn test_delete_complement_2() { + new_ucmd!() + .args(&["-d", "-C", "0-9"]) + .pipe_in("Phone: 01234 567890") + .succeeds() + .stdout_is("01234567890"); + new_ucmd!() + .args(&["-d", "--complement", "0-9"]) + .pipe_in("Phone: 01234 567890") + .succeeds() + .stdout_is("01234567890"); +} + +#[test] +fn test_complement1() { + new_ucmd!() + .args(&["-c", "a", "X"]) + .pipe_in("ab") + .run() + .stdout_is("aX"); +} + +#[test] +fn test_complement2() { + new_ucmd!() + .args(&["-c", "0-9", "x"]) + .pipe_in("Phone: 01234 567890") + .run() + .stdout_is("xxxxxxx01234x567890"); +} + +#[test] +fn test_complement3() { + new_ucmd!() + .args(&["-c", "abcdefgh", "123"]) // spell-checker:disable-line + .pipe_in("the cat and the bat") + .run() + .stdout_is("3he3ca33a3d33he3ba3"); // spell-checker:disable-line +} + +#[test] +fn test_complement4() { + // $ echo -n '0x1y2z3' | tr -c '0-@' '*-~' + // 0~1~2~3 + new_ucmd!() + .args(&["-c", "0-@", "*-~"]) + .pipe_in("0x1y2z3") + .run() + .stdout_is("0~1~2~3"); +} + +#[test] +#[ignore = "fixme: GNU tr returns '0a1b2c3' instead of '0~1~2~3', see #2158"] +fn test_complement5() { + // $ echo '0x1y2z3' | tr -c '\0-@' '*-~' + // 0a1b2c3 + new_ucmd!() + .args(&["-c", "\\0-@", "*-~"]) + .pipe_in("0x1y2z3") + .run() + .stdout_is("0a1b2c3"); +} + #[test] fn test_squeeze() { new_ucmd!() @@ -63,6 +127,24 @@ fn test_squeeze_complement() { .stdout_is("aaBcDcc"); } +#[test] +fn test_translate_and_squeeze() { + new_ucmd!() + .args(&["-s", "x", "y"]) + .pipe_in("xx") + .run() + .stdout_is("y"); +} + +#[test] +fn test_translate_and_squeeze_multiple_lines() { + new_ucmd!() + .args(&["-s", "x", "y"]) + .pipe_in("xxaax\nxaaxx") // spell-checker:disable-line + .run() + .stdout_is("yaay\nyaay"); // spell-checker:disable-line +} + #[test] fn test_delete_and_squeeze() { new_ucmd!() @@ -87,7 +169,7 @@ fn test_set1_longer_than_set2() { .args(&["abc", "xy"]) .pipe_in("abcde") .run() - .stdout_is("xyyde"); + .stdout_is("xyyde"); // spell-checker:disable-line } #[test] @@ -96,7 +178,7 @@ fn test_set1_shorter_than_set2() { .args(&["ab", "xyz"]) .pipe_in("abcde") .run() - .stdout_is("xycde"); + .stdout_is("xycde"); // spell-checker:disable-line } #[test] @@ -105,7 +187,7 @@ fn test_truncate() { .args(&["-t", "abc", "xy"]) .pipe_in("abcde") .run() - .stdout_is("xycde"); + .stdout_is("xycde"); // spell-checker:disable-line } #[test] @@ -114,23 +196,82 @@ fn test_truncate_with_set1_shorter_than_set2() { .args(&["-t", "ab", "xyz"]) .pipe_in("abcde") .run() - .stdout_is("xycde"); + .stdout_is("xycde"); // spell-checker:disable-line } #[test] fn missing_args_fails() { let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - - assert!(!result.success); - assert!(result.stderr.contains("missing operand")); + ucmd.fails().stderr_contains("missing operand"); } #[test] fn missing_required_second_arg_fails() { let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.args(&["foo"]).run(); - - assert!(!result.success); - assert!(result.stderr.contains("missing operand after")); + ucmd.args(&["foo"]) + .fails() + .stderr_contains("missing operand after"); +} + +#[test] +fn test_interpret_backslash_escapes() { + new_ucmd!() + .args(&["abfnrtv", r"\a\b\f\n\r\t\v"]) // spell-checker:disable-line + .pipe_in("abfnrtv") // spell-checker:disable-line + .succeeds() + .stdout_is("\u{7}\u{8}\u{c}\n\r\t\u{b}"); +} + +#[test] +fn test_interpret_unrecognized_backslash_escape_as_character() { + new_ucmd!() + .args(&["qcz+=~-", r"\q\c\z\+\=\~\-"]) + .pipe_in("qcz+=~-") + .succeeds() + .stdout_is("qcz+=~-"); +} + +#[test] +fn test_interpret_single_octal_escape() { + new_ucmd!() + .args(&["X", r"\015"]) + .pipe_in("X") + .succeeds() + .stdout_is("\r"); +} + +#[test] +fn test_interpret_one_and_two_digit_octal_escape() { + new_ucmd!() + .args(&["XYZ", r"\0\11\77"]) + .pipe_in("XYZ") + .succeeds() + .stdout_is("\0\t?"); +} + +#[test] +fn test_octal_escape_is_at_most_three_digits() { + new_ucmd!() + .args(&["XY", r"\0156"]) + .pipe_in("XY") + .succeeds() + .stdout_is("\r6"); +} + +#[test] +fn test_non_octal_digit_ends_escape() { + new_ucmd!() + .args(&["rust", r"\08\11956"]) + .pipe_in("rust") + .succeeds() + .stdout_is("\08\t9"); +} + +#[test] +fn test_interpret_backslash_at_eol_literally() { + new_ucmd!() + .args(&["X", r"\"]) + .pipe_in("X") + .succeeds() + .stdout_is("\\"); } diff --git a/tests/by-util/test_truncate.rs b/tests/by-util/test_truncate.rs index ce7964d57..4b2e9e502 100644 --- a/tests/by-util/test_truncate.rs +++ b/tests/by-util/test_truncate.rs @@ -1,56 +1,112 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. + +// spell-checker:ignore (words) RFILE + use crate::common::util::*; use std::io::{Seek, SeekFrom, Write}; -static TFILE1: &'static str = "truncate_test_1"; -static TFILE2: &'static str = "truncate_test_2"; +static FILE1: &str = "truncate_test_1"; +static FILE2: &str = "truncate_test_2"; #[test] fn test_increase_file_size() { + let expected = 5 * 1024; let (at, mut ucmd) = at_and_ucmd!(); - let mut file = at.make_file(TFILE1); - ucmd.args(&["-s", "+5K", TFILE1]).succeeds(); + let mut file = at.make_file(FILE1); + ucmd.args(&["-s", "+5K", FILE1]).succeeds(); file.seek(SeekFrom::End(0)).unwrap(); - assert!(file.seek(SeekFrom::Current(0)).unwrap() == 5 * 1024); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); } #[test] fn test_increase_file_size_kb() { + let expected = 5 * 1000; let (at, mut ucmd) = at_and_ucmd!(); - let mut file = at.make_file(TFILE1); - ucmd.args(&["-s", "+5KB", TFILE1]).succeeds(); + let mut file = at.make_file(FILE1); + ucmd.args(&["-s", "+5KB", FILE1]).succeeds(); file.seek(SeekFrom::End(0)).unwrap(); - assert!(file.seek(SeekFrom::Current(0)).unwrap() == 5 * 1000); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); } #[test] fn test_reference() { + let expected = 5 * 1000; let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; - let mut file = at.make_file(TFILE2); + let mut file = at.make_file(FILE2); - scene.ucmd().arg("-s").arg("+5KB").arg(TFILE1).run(); + // manpage: "A FILE argument that does not exist is created." + // TODO: 'truncate' does not create the file in this case, + // but should because '--no-create' wasn't specified. + at.touch(FILE1); // TODO: remove this when 'no-create' is fixed + scene.ucmd().arg("-s").arg("+5KB").arg(FILE1).succeeds(); scene .ucmd() .arg("--reference") - .arg(TFILE1) - .arg(TFILE2) - .run(); + .arg(FILE1) + .arg(FILE2) + .succeeds(); file.seek(SeekFrom::End(0)).unwrap(); - assert!(file.seek(SeekFrom::Current(0)).unwrap() == 5 * 1000); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); } #[test] fn test_decrease_file_size() { + let expected = 6; let (at, mut ucmd) = at_and_ucmd!(); - let mut file = at.make_file(TFILE2); + let mut file = at.make_file(FILE2); file.write_all(b"1234567890").unwrap(); - ucmd.args(&["--size=-4", TFILE2]).succeeds(); + ucmd.args(&["--size=-4", FILE2]).succeeds(); file.seek(SeekFrom::End(0)).unwrap(); - assert!(file.seek(SeekFrom::Current(0)).unwrap() == 6); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_space_in_size() { + let expected = 4; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file = at.make_file(FILE2); + file.write_all(b"1234567890").unwrap(); + ucmd.args(&["--size", " 4", FILE2]).succeeds(); + file.seek(SeekFrom::End(0)).unwrap(); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); } #[test] @@ -61,11 +117,207 @@ fn test_failed() { #[test] fn test_failed_2() { let (_at, mut ucmd) = at_and_ucmd!(); - ucmd.args(&[TFILE1]).fails(); + ucmd.args(&[FILE1]).fails(); } #[test] fn test_failed_incorrect_arg() { let (_at, mut ucmd) = at_and_ucmd!(); - ucmd.args(&["-s", "+5A", TFILE1]).fails(); + ucmd.args(&["-s", "+5A", FILE1]).fails(); +} + +#[test] +fn test_at_most_shrinks() { + let expected = 4; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file = at.make_file(FILE2); + file.write_all(b"1234567890").unwrap(); + ucmd.args(&["--size", "<4", FILE2]).succeeds(); + file.seek(SeekFrom::End(0)).unwrap(); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_at_most_no_change() { + let expected = 10; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file = at.make_file(FILE2); + file.write_all(b"1234567890").unwrap(); + ucmd.args(&["--size", "<40", FILE2]).succeeds(); + file.seek(SeekFrom::End(0)).unwrap(); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_at_least_grows() { + let expected = 15; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file = at.make_file(FILE2); + file.write_all(b"1234567890").unwrap(); + ucmd.args(&["--size", ">15", FILE2]).succeeds(); + file.seek(SeekFrom::End(0)).unwrap(); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_at_least_no_change() { + let expected = 10; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file = at.make_file(FILE2); + file.write_all(b"1234567890").unwrap(); + ucmd.args(&["--size", ">4", FILE2]).succeeds(); + file.seek(SeekFrom::End(0)).unwrap(); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_round_down() { + let expected = 8; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file = at.make_file(FILE2); + file.write_all(b"1234567890").unwrap(); + ucmd.args(&["--size", "/4", FILE2]).succeeds(); + file.seek(SeekFrom::End(0)).unwrap(); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_round_up() { + let expected = 12; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file = at.make_file(FILE2); + file.write_all(b"1234567890").unwrap(); + ucmd.args(&["--size", "%4", FILE2]).succeeds(); + file.seek(SeekFrom::End(0)).unwrap(); + let actual = file.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_size_and_reference() { + let expected = 15; + let (at, mut ucmd) = at_and_ucmd!(); + let mut file1 = at.make_file(FILE1); + let mut file2 = at.make_file(FILE2); + file1.write_all(b"1234567890").unwrap(); + ucmd.args(&["--reference", FILE1, "--size", "+5", FILE2]) + .succeeds(); + file2.seek(SeekFrom::End(0)).unwrap(); + let actual = file2.seek(SeekFrom::Current(0)).unwrap(); + assert!( + expected == actual, + "expected '{}' got '{}'", + expected, + actual + ); +} + +#[test] +fn test_error_filename_only() { + // truncate: you must specify either '--size' or '--reference' + new_ucmd!().args(&["file"]).fails().stderr_contains( + "error: The following required arguments were not provided: + --reference + --size ", + ); +} + +#[test] +fn test_invalid_numbers() { + new_ucmd!() + .args(&["-s", "0X", "file"]) + .fails() + .stderr_contains("Invalid number: '0X'"); + new_ucmd!() + .args(&["-s", "0XB", "file"]) + .fails() + .stderr_contains("Invalid number: '0XB'"); + new_ucmd!() + .args(&["-s", "0B", "file"]) + .fails() + .stderr_contains("Invalid number: '0B'"); +} + +#[test] +fn test_reference_file_not_found() { + new_ucmd!() + .args(&["-r", "a", "b"]) + .fails() + .stderr_contains("cannot stat 'a': No such file or directory"); +} + +#[test] +fn test_reference_with_size_file_not_found() { + new_ucmd!() + .args(&["-r", "a", "-s", "+1", "b"]) + .fails() + .stderr_contains("cannot stat 'a': No such file or directory"); +} + +#[test] +fn test_truncate_bytes_size() { + // TODO: this should succeed without error, uncomment when '--no-create' is fixed + // new_ucmd!() + // .args(&["--no-create", "--size", "K", "file"]) + // .succeeds(); + new_ucmd!() + .args(&["--size", "1024R", "file"]) + .fails() + .code_is(1) + .stderr_only("truncate: Invalid number: '1024R'"); + #[cfg(not(target_pointer_width = "128"))] + new_ucmd!() + .args(&["--size", "1Y", "file"]) + .fails() + .code_is(1) + .stderr_only("truncate: Invalid number: '1Y': Value too large for defined data type"); + #[cfg(target_pointer_width = "32")] + { + let sizes = ["1000G", "10T"]; + for size in &sizes { + new_ucmd!() + .args(&["--size", size, "file"]) + .fails() + .code_is(1) + .stderr_only(format!( + "truncate: Invalid number: '{}': Value too large for defined data type", + size + )); + } + } } diff --git a/tests/by-util/test_tsort.rs b/tests/by-util/test_tsort.rs index c743868ec..0da6f44e4 100644 --- a/tests/by-util/test_tsort.rs +++ b/tests/by-util/test_tsort.rs @@ -15,3 +15,38 @@ fn test_sort_self_loop() { .succeeds() .stdout_only("first\nsecond\n"); } + +#[test] +fn test_no_such_file() { + new_ucmd!() + .arg("invalid_file_txt") + .fails() + .stderr_contains("No such file or directory"); +} + +#[test] +fn test_version_flag() { + let version_short = new_ucmd!().arg("-V").succeeds(); + let version_long = new_ucmd!().arg("--version").succeeds(); + + assert_eq!(version_short.stdout_str(), version_long.stdout_str()); +} + +#[test] +fn test_help_flag() { + let help_short = new_ucmd!().arg("-h").succeeds(); + let help_long = new_ucmd!().arg("--help").succeeds(); + + assert_eq!(help_short.stdout_str(), help_long.stdout_str()); +} + +#[test] +fn test_multiple_arguments() { + new_ucmd!() + .arg("call_graph.txt") + .arg("invalid_file") + .fails() + .stderr_contains( + "Found argument 'invalid_file' which wasn't expected, or isn't valid in this context", + ); +} diff --git a/tests/by-util/test_tty.rs b/tests/by-util/test_tty.rs index 651491045..6ba8cd029 100644 --- a/tests/by-util/test_tty.rs +++ b/tests/by-util/test_tty.rs @@ -1 +1,74 @@ -// ToDO: add tests +use std::fs::File; + +use crate::common::util::*; + +#[test] +#[cfg(not(windows))] +fn test_dev_null() { + new_ucmd!() + .set_stdin(File::open("/dev/null").unwrap()) + .fails() + .code_is(1) + .stdout_is("not a tty\n"); +} + +#[test] +#[cfg(not(windows))] +fn test_dev_null_silent() { + new_ucmd!() + .args(&["-s"]) + .set_stdin(File::open("/dev/null").unwrap()) + .fails() + .code_is(1) + .stdout_is(""); +} + +#[test] +fn test_close_stdin() { + let mut child = new_ucmd!().run_no_wait(); + drop(child.stdin.take()); + let output = child.wait_with_output().unwrap(); + assert_eq!(output.status.code(), Some(1)); + assert_eq!(std::str::from_utf8(&output.stdout), Ok("not a tty\n")); +} + +#[test] +fn test_close_stdin_silent() { + let mut child = new_ucmd!().arg("-s").run_no_wait(); + drop(child.stdin.take()); + let output = child.wait_with_output().unwrap(); + assert_eq!(output.status.code(), Some(1)); + assert!(output.stdout.is_empty()); +} + +#[test] +fn test_close_stdin_silent_long() { + let mut child = new_ucmd!().arg("--silent").run_no_wait(); + drop(child.stdin.take()); + let output = child.wait_with_output().unwrap(); + assert_eq!(output.status.code(), Some(1)); + assert!(output.stdout.is_empty()); +} + +#[test] +fn test_close_stdin_silent_alias() { + let mut child = new_ucmd!().arg("--quiet").run_no_wait(); + drop(child.stdin.take()); + let output = child.wait_with_output().unwrap(); + assert_eq!(output.status.code(), Some(1)); + assert!(output.stdout.is_empty()); +} + +#[test] +fn test_wrong_argument() { + new_ucmd!().args(&["a"]).fails().code_is(2); +} + +#[test] +#[cfg(not(windows))] +fn test_stdout_fail() { + let mut child = new_ucmd!().run_no_wait(); + drop(child.stdout.take()); + let status = child.wait().unwrap(); + assert_eq!(status.code(), Some(3)); +} diff --git a/tests/by-util/test_uname.rs b/tests/by-util/test_uname.rs index f0e32b430..de3f42a6b 100644 --- a/tests/by-util/test_uname.rs +++ b/tests/by-util/test_uname.rs @@ -2,60 +2,46 @@ use crate::common::util::*; #[test] fn test_uname_compatible() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("-a").run(); - assert!(result.success); + new_ucmd!().arg("-a").succeeds(); } #[test] fn test_uname_name() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("-n").run(); - assert!(result.success); + new_ucmd!().arg("-n").succeeds(); } #[test] fn test_uname_processor() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("-p").run(); - assert!(result.success); - assert_eq!(result.stdout.trim_end(), "unknown"); + let result = new_ucmd!().arg("-p").succeeds(); + assert_eq!(result.stdout_str().trim_end(), "unknown"); } #[test] -fn test_uname_hwplatform() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("-i").run(); - assert!(result.success); - assert_eq!(result.stdout.trim_end(), "unknown"); +fn test_uname_hardware_platform() { + let result = new_ucmd!().arg("-i").succeeds(); + assert_eq!(result.stdout_str().trim_end(), "unknown"); } #[test] fn test_uname_machine() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("-m").run(); - assert!(result.success); + new_ucmd!().arg("-m").succeeds(); } #[test] fn test_uname_kernel_version() { - let (_, mut ucmd) = at_and_ucmd!(); - - let result = ucmd.arg("-v").run(); - assert!(result.success); + new_ucmd!().arg("-v").succeeds(); } #[test] fn test_uname_kernel() { let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("-o").run(); - assert!(result.success); #[cfg(target_os = "linux")] - assert!(result.stdout.to_lowercase().contains("linux")); + { + let result = ucmd.arg("-o").succeeds(); + assert!(result.stdout_str().to_lowercase().contains("linux")); + } + + #[cfg(not(target_os = "linux"))] + ucmd.arg("-o").succeeds(); } diff --git a/tests/by-util/test_unexpand.rs b/tests/by-util/test_unexpand.rs index 93cc42d90..692599361 100644 --- a/tests/by-util/test_unexpand.rs +++ b/tests/by-util/test_unexpand.rs @@ -38,7 +38,7 @@ fn unexpand_init_list_1() { } #[test] -fn unexpand_aflag_0() { +fn unexpand_flag_a_0() { new_ucmd!() .args(&["--"]) .pipe_in("e E\nf F\ng G\nh H\n") @@ -47,7 +47,7 @@ fn unexpand_aflag_0() { } #[test] -fn unexpand_aflag_1() { +fn unexpand_flag_a_1() { new_ucmd!() .args(&["-a"]) .pipe_in("e E\nf F\ng G\nh H\n") @@ -56,7 +56,7 @@ fn unexpand_aflag_1() { } #[test] -fn unexpand_aflag_2() { +fn unexpand_flag_a_2() { new_ucmd!() .args(&["-t8"]) .pipe_in("e E\nf F\ng G\nh H\n") @@ -136,3 +136,22 @@ fn unexpand_spaces_after_fields() { .run() .stdout_is("\t\tA B C D\t\t A\t\n"); } + +#[test] +fn unexpand_read_from_file() { + new_ucmd!() + .arg("with_spaces.txt") + .arg("-t4") + .run() + .success(); +} + +#[test] +fn unexpand_read_from_two_file() { + new_ucmd!() + .arg("with_spaces.txt") + .arg("with_spaces.txt") + .arg("-t4") + .run() + .success(); +} diff --git a/tests/by-util/test_uniq.rs b/tests/by-util/test_uniq.rs index 22e67540e..c191ffcaf 100644 --- a/tests/by-util/test_uniq.rs +++ b/tests/by-util/test_uniq.rs @@ -1,10 +1,10 @@ use crate::common::util::*; -static INPUT: &'static str = "sorted.txt"; -static OUTPUT: &'static str = "sorted-output.txt"; -static SKIP_CHARS: &'static str = "skip-chars.txt"; -static SKIP_FIELDS: &'static str = "skip-fields.txt"; -static SORTED_ZERO_TERMINATED: &'static str = "sorted-zero-terminated.txt"; +static INPUT: &str = "sorted.txt"; +static OUTPUT: &str = "sorted-output.txt"; +static SKIP_CHARS: &str = "skip-chars.txt"; +static SKIP_FIELDS: &str = "skip-fields.txt"; +static SORTED_ZERO_TERMINATED: &str = "sorted-zero-terminated.txt"; #[test] fn test_stdin_default() { @@ -145,5 +145,50 @@ fn test_invalid_utf8() { .arg("not-utf8-sequence.txt") .run() .failure() - .stderr_only("uniq: error: invalid utf-8 sequence of 1 bytes from index 0"); + .stderr_only("uniq: invalid utf-8 sequence of 1 bytes from index 0"); +} + +#[test] +fn test_group() { + new_ucmd!() + .args(&["--group"]) + .pipe_in_fixture(INPUT) + .run() + .stdout_is_fixture("group.expected"); +} + +#[test] +fn test_group_prepend() { + new_ucmd!() + .args(&["--group=prepend"]) + .pipe_in_fixture(INPUT) + .run() + .stdout_is_fixture("group-prepend.expected"); +} + +#[test] +fn test_group_append() { + new_ucmd!() + .args(&["--group=append"]) + .pipe_in_fixture(INPUT) + .run() + .stdout_is_fixture("group-append.expected"); +} + +#[test] +fn test_group_both() { + new_ucmd!() + .args(&["--group=both"]) + .pipe_in_fixture(INPUT) + .run() + .stdout_is_fixture("group-both.expected"); +} + +#[test] +fn test_group_separate() { + new_ucmd!() + .args(&["--group=separate"]) + .pipe_in_fixture(INPUT) + .run() + .stdout_is_fixture("group.expected"); } diff --git a/tests/by-util/test_unlink.rs b/tests/by-util/test_unlink.rs index daac6d3b3..1999e965c 100644 --- a/tests/by-util/test_unlink.rs +++ b/tests/by-util/test_unlink.rs @@ -22,8 +22,8 @@ fn test_unlink_multiple_files() { at.touch(file_b); ucmd.arg(file_a).arg(file_b).fails().stderr_is( - "unlink: error: extra operand: 'test_unlink_multiple_file_b'\nTry 'unlink --help' \ - for more information.\n", + "unlink: extra operand: 'test_unlink_multiple_file_b'\nTry 'unlink --help' \ + for more information.\n", ); } @@ -35,8 +35,8 @@ fn test_unlink_directory() { at.mkdir(dir); ucmd.arg(dir).fails().stderr_is( - "unlink: error: cannot unlink 'test_unlink_empty_directory': Not a regular file \ - or symlink\n", + "unlink: cannot unlink 'test_unlink_empty_directory': Not a regular file \ + or symlink\n", ); } @@ -45,7 +45,7 @@ fn test_unlink_nonexistent() { let file = "test_unlink_nonexistent"; new_ucmd!().arg(file).fails().stderr_is( - "unlink: error: Cannot stat 'test_unlink_nonexistent': No such file or directory \ - (os error 2)\n", + "unlink: Cannot stat 'test_unlink_nonexistent': No such file or directory \ + (os error 2)\n", ); } diff --git a/tests/by-util/test_uptime.rs b/tests/by-util/test_uptime.rs index c8f6a11d3..4dc90eb31 100644 --- a/tests/by-util/test_uptime.rs +++ b/tests/by-util/test_uptime.rs @@ -4,33 +4,23 @@ use crate::common::util::*; #[test] fn test_uptime() { - let result = TestScenario::new(util_name!()).ucmd_keepenv().run(); + TestScenario::new(util_name!()) + .ucmd_keepenv() + .succeeds() + .stdout_contains("load average:") + .stdout_contains(" up "); - println!("stdout = {}", result.stdout); - println!("stderr = {}", result.stderr); - - assert!(result.success); - assert!(result.stdout.contains("load average:")); - assert!(result.stdout.contains(" up ")); // Don't check for users as it doesn't show in some CI } #[test] fn test_uptime_since() { - let scene = TestScenario::new(util_name!()); - - let result = scene.ucmd().arg("--since").succeeds(); - - println!("stdout = {}", result.stdout); - println!("stderr = {}", result.stderr); - - assert!(result.success); let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}").unwrap(); - assert!(re.is_match(&result.stdout.trim())); + + new_ucmd!().arg("--since").succeeds().stdout_matches(&re); } #[test] fn test_failed() { - let (_at, mut ucmd) = at_and_ucmd!(); - ucmd.arg("willfail").fails(); + new_ucmd!().arg("will-fail").fails(); } diff --git a/tests/by-util/test_users.rs b/tests/by-util/test_users.rs index cb444b8be..1bcbdbdc1 100644 --- a/tests/by-util/test_users.rs +++ b/tests/by-util/test_users.rs @@ -1,30 +1,25 @@ use crate::common::util::*; -use std::env; #[test] -fn test_users_noarg() { - let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.run(); - assert!(result.success); +fn test_users_no_arg() { + new_ucmd!().succeeds(); } + #[test] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] fn test_users_check_name() { - let result = TestScenario::new(util_name!()).ucmd_keepenv().run(); - assert!(result.success); + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(target_vendor = "apple")] + let util_name = format!("g{}", util_name!()); - // Expectation: USER is often set - let key = "USER"; + // note: clippy::needless_borrow *false positive* + #[allow(clippy::needless_borrow)] + let expected = TestScenario::new(&util_name) + .cmd_keepenv(util_name) + .env("LC_ALL", "C") + .succeeds() + .stdout_move_str(); - match env::var(key) { - Err(e) => println!("Key {} isn't set. Found {}", &key, e), - Ok(username) => - // Check if "users" contains the name of the user - { - println!("username found {}", &username); - println!("result.stdout {}", &result.stdout); - if !&result.stdout.is_empty() { - assert!(result.stdout.contains(&username)) - } - } - } + new_ucmd!().succeeds().stdout_is(&expected); } diff --git a/tests/by-util/test_wc.rs b/tests/by-util/test_wc.rs index fc9c00ecc..88c65c997 100644 --- a/tests/by-util/test_wc.rs +++ b/tests/by-util/test_wc.rs @@ -1,11 +1,50 @@ use crate::common::util::*; +// spell-checker:ignore (flags) lwmcL clmwL ; (path) bogusfile emptyfile manyemptylines moby notrailingnewline onelongemptyline onelongword + +#[test] +fn test_count_bytes_large_stdin() { + for &n in &[ + 0, + 1, + 42, + 16 * 1024 - 7, + 16 * 1024 - 1, + 16 * 1024, + 16 * 1024 + 1, + 16 * 1024 + 3, + 32 * 1024, + 64 * 1024, + 80 * 1024, + 96 * 1024, + 112 * 1024, + 128 * 1024, + ] { + let data = vec_of_size(n); + let expected = format!("{}\n", n); + new_ucmd!() + .args(&["-c"]) + .pipe_in(data) + .succeeds() + .stdout_is_bytes(&expected.as_bytes()); + } +} + #[test] fn test_stdin_default() { new_ucmd!() .pipe_in_fixture("lorem_ipsum.txt") .run() - .stdout_is(" 13 109 772\n"); + .stdout_is(" 13 109 772\n"); +} + +#[test] +fn test_stdin_explicit() { + new_ucmd!() + .pipe_in_fixture("lorem_ipsum.txt") + .arg("-") + .run() + .stdout_is(" 13 109 772 -\n"); } #[test] @@ -14,9 +53,11 @@ fn test_utf8() { .args(&["-lwmcL"]) .pipe_in_fixture("UTF_8_test.txt") .run() - .stdout_is(" 0 0 0 0 0\n"); - // GNU returns " 300 2086 22219 22781 79" - // TODO: we should fix that to match GNU's behavior + .stdout_is(" 300 4969 22781 22213 79\n"); + // GNU returns " 300 2086 22219 22781 79" + // + // TODO: we should fix the word, character, and byte count to + // match the behavior of GNU wc } #[test] @@ -25,7 +66,7 @@ fn test_stdin_line_len_regression() { .args(&["-L"]) .pipe_in("\n123456") .run() - .stdout_is(" 6\n"); + .stdout_is("6\n"); } #[test] @@ -34,7 +75,7 @@ fn test_stdin_only_bytes() { .args(&["-c"]) .pipe_in_fixture("lorem_ipsum.txt") .run() - .stdout_is(" 772\n"); + .stdout_is("772\n"); } #[test] @@ -43,7 +84,7 @@ fn test_stdin_all_counts() { .args(&["-c", "-m", "-l", "-L", "-w"]) .pipe_in_fixture("alice_in_wonderland.txt") .run() - .stdout_is(" 5 57 302 302 66\n"); + .stdout_is(" 5 57 302 302 66\n"); } #[test] @@ -51,7 +92,7 @@ fn test_single_default() { new_ucmd!() .arg("moby_dick.txt") .run() - .stdout_is(" 18 204 1115 moby_dick.txt\n"); + .stdout_is(" 18 204 1115 moby_dick.txt\n"); } #[test] @@ -59,7 +100,7 @@ fn test_single_only_lines() { new_ucmd!() .args(&["-l", "moby_dick.txt"]) .run() - .stdout_is(" 18 moby_dick.txt\n"); + .stdout_is("18 moby_dick.txt\n"); } #[test] @@ -67,7 +108,7 @@ fn test_single_all_counts() { new_ucmd!() .args(&["-c", "-l", "-L", "-m", "-w", "alice_in_wonderland.txt"]) .run() - .stdout_is(" 5 57 302 302 66 alice_in_wonderland.txt\n"); + .stdout_is(" 5 57 302 302 66 alice_in_wonderland.txt\n"); } #[test] @@ -80,7 +121,101 @@ fn test_multiple_default() { ]) .run() .stdout_is( - " 13 109 772 lorem_ipsum.txt\n 18 204 1115 moby_dick.txt\n 5 57 302 \ - alice_in_wonderland.txt\n 36 370 2189 total\n", + " 13 109 772 lorem_ipsum.txt\n 18 204 1115 moby_dick.txt\n 5 57 302 \ + alice_in_wonderland.txt\n 36 370 2189 total\n", ); } + +/// Test for an empty file. +#[test] +fn test_file_empty() { + new_ucmd!() + .args(&["-clmwL", "emptyfile.txt"]) + .run() + .stdout_is("0 0 0 0 0 emptyfile.txt\n"); +} + +/// Test for an file containing a single non-whitespace character +/// *without* a trailing newline. +#[test] +fn test_file_single_line_no_trailing_newline() { + new_ucmd!() + .args(&["-clmwL", "notrailingnewline.txt"]) + .run() + .stdout_is("1 1 2 2 1 notrailingnewline.txt\n"); +} + +/// Test for a file that has 100 empty lines (that is, the contents of +/// the file are the newline character repeated one hundred times). +#[test] +fn test_file_many_empty_lines() { + new_ucmd!() + .args(&["-clmwL", "manyemptylines.txt"]) + .run() + .stdout_is("100 0 100 100 0 manyemptylines.txt\n"); +} + +/// Test for a file that has one long line comprising only spaces. +#[test] +fn test_file_one_long_line_only_spaces() { + new_ucmd!() + .args(&["-clmwL", "onelongemptyline.txt"]) + .run() + .stdout_is(" 1 0 10001 10001 10000 onelongemptyline.txt\n"); +} + +/// Test for a file that has one long line comprising a single "word". +#[test] +fn test_file_one_long_word() { + new_ucmd!() + .args(&["-clmwL", "onelongword.txt"]) + .run() + .stdout_is(" 1 1 10001 10001 10000 onelongword.txt\n"); +} + +/// Test that the number of bytes in the file dictate the display width. +/// +/// The width in digits of any count is the width in digits of the +/// number of bytes in the file, regardless of whether the number of +/// bytes are displayed. +#[test] +fn test_file_bytes_dictate_width() { + // This file has 10,001 bytes. Five digits are required to + // represent that. Even though the number of lines is 1 and the + // number of words is 0, each of those counts is formatted with + // five characters, filled with whitespace. + new_ucmd!() + .args(&["-lw", "onelongemptyline.txt"]) + .run() + .stdout_is(" 1 0 onelongemptyline.txt\n"); + + // This file has zero bytes. Only one digit is required to + // represent that. + new_ucmd!() + .args(&["-lw", "emptyfile.txt"]) + .run() + .stdout_is("0 0 emptyfile.txt\n"); +} + +/// Test that getting counts from a directory is an error. +#[test] +fn test_read_from_directory_error() { + // TODO To match GNU `wc`, the `stdout` should be: + // + // " 0 0 0 .\n" + // + new_ucmd!() + .args(&["."]) + .fails() + .stderr_contains(".: Is a directory\n") + .stdout_is("0 0 0 .\n"); +} + +/// Test that getting counts from nonexistent file is an error. +#[test] +fn test_read_from_nonexistent_file() { + new_ucmd!() + .args(&["bogusfile"]) + .fails() + .stderr_contains("bogusfile: No such file or directory\n"); +} diff --git a/tests/by-util/test_who.rs b/tests/by-util/test_who.rs index 89b7cec93..9315a5956 100644 --- a/tests/by-util/test_who.rs +++ b/tests/by-util/test_who.rs @@ -1,30 +1,38 @@ -#[cfg(target_os = "linux")] use crate::common::util::*; -#[cfg(target_os = "linux")] +// spell-checker:ignore (flags) runlevel mesg + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_count() { - for opt in vec!["-q", "--count"] { - new_ucmd!().arg(opt).run().stdout_is(expected_result(opt)); + for opt in &["-q", "--count"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); } } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_boot() { - for opt in vec!["-b", "--boot"] { - new_ucmd!().arg(opt).run().stdout_is(expected_result(opt)); + for opt in &["-b", "--boot"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); } } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_heading() { - for opt in vec!["-H"] { + for opt in &["-H", "--heading"] { // allow whitespace variation - // * minor whitespace differences occur between platform built-in outputs; specifically number of TABs between "TIME" and "COMMENT" may be variant - let actual = new_ucmd!().arg(opt).run().stdout; - let expect = expected_result(opt); + // * minor whitespace differences occur between platform built-in outputs; + // specifically number of TABs between "TIME" and "COMMENT" may be variant + let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str(); + let expect = expected_result(&[opt]); println!("actual: {:?}", actual); println!("expect: {:?}", expect); let v_actual: Vec<&str> = actual.split_whitespace().collect(); @@ -33,52 +41,208 @@ fn test_heading() { } } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_short() { - for opt in vec!["-s", "--short"] { - new_ucmd!().arg(opt).run().stdout_is(expected_result(opt)); + for opt in &["-s", "--short"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); } } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_login() { - for opt in vec!["-l", "--login"] { - new_ucmd!().arg(opt).run().stdout_is(expected_result(opt)); + for opt in &["-l", "--login"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); } } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_m() { - for opt in vec!["-m"] { - new_ucmd!().arg(opt).run().stdout_is(expected_result(opt)); + for opt in &["-m"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); } } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_process() { + for opt in &["-p", "--process"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); + } +} + +#[test] +fn test_runlevel() { + for opt in &["-r", "--runlevel"] { + #[cfg(any(target_vendor = "apple", target_os = "linux"))] + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); + + #[cfg(not(target_os = "linux"))] + new_ucmd!().arg(opt).succeeds().stdout_is(""); + } +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_time() { + for opt in &["-t", "--time"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); + } +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_mesg() { + // -T, -w, --mesg + // add user's message status as +, - or ? + // --message + // same as -T + // --writable + // same as -T + for opt in &["-T", "-w", "--mesg", "--message", "--writable"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); + } +} + +#[test] +fn test_arg1_arg2() { + let args = ["am", "i"]; + + new_ucmd!() + .args(&args) + .succeeds() + .stdout_is(expected_result(&args)); +} + +#[test] +fn test_too_many_args() { + const EXPECTED: &str = + "error: The value 'u' was provided to '...', but it wasn't expecting any more values"; + + let args = ["am", "i", "u"]; + new_ucmd!().args(&args).fails().stderr_contains(EXPECTED); +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_users() { + for opt in &["-u", "--users"] { + let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str(); + let expect = expected_result(&[opt]); + println!("actual: {:?}", actual); + println!("expect: {:?}", expect); + + let mut v_actual: Vec<&str> = actual.split_whitespace().collect(); + let mut v_expect: Vec<&str> = expect.split_whitespace().collect(); + + // TODO: `--users` sometimes differs from GNU's output on macOS (race condition?) + // actual: "runner console Jun 23 06:37 00:34 196\n" + // expect: "runner console Jun 23 06:37 old 196\n" + if cfg!(target_os = "macos") { + v_actual.remove(5); + v_expect.remove(5); + } + + assert_eq!(v_actual, v_expect); + } +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_lookup() { + let opt = "--lookup"; + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_dead() { - for opt in vec!["-d", "--dead"] { - new_ucmd!().arg(opt).run().stdout_is(expected_result(opt)); + for opt in &["-d", "--dead"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); } } -#[cfg(target_os = "linux")] +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +#[test] +fn test_all_separately() { + if cfg!(target_os = "macos") { + // TODO: fix `-u`, see: test_users + return; + } + + // -a, --all same as -b -d --login -p -r -t -T -u + let args = ["-b", "-d", "--login", "-p", "-r", "-t", "-T", "-u"]; + let scene = TestScenario::new(util_name!()); + scene + .ucmd() + .args(&args) + .succeeds() + .stdout_is(expected_result(&args)); + scene + .ucmd() + .arg("--all") + .succeeds() + .stdout_is(expected_result(&args)); +} + +#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[test] fn test_all() { - for opt in vec!["-a", "--all"] { - new_ucmd!().arg(opt).run().stdout_is(expected_result(opt)); + if cfg!(target_os = "macos") { + // TODO: fix `-u`, see: test_users + return; + } + + for opt in &["-a", "--all"] { + new_ucmd!() + .arg(opt) + .succeeds() + .stdout_is(expected_result(&[opt])); } } -#[cfg(target_os = "linux")] -fn expected_result(arg: &str) -> String { - TestScenario::new(util_name!()) - .cmd_keepenv(util_name!()) - .env("LANGUAGE", "C") - .args(&[arg]) - .run() - .stdout +#[cfg(any(target_vendor = "apple", target_os = "linux"))] +fn expected_result(args: &[&str]) -> String { + #[cfg(target_os = "linux")] + let util_name = util_name!(); + #[cfg(target_vendor = "apple")] + let util_name = format!("g{}", util_name!()); + + // note: clippy::needless_borrow *false positive* + #[allow(clippy::needless_borrow)] + TestScenario::new(&util_name) + .cmd_keepenv(util_name) + .env("LC_ALL", "C") + .args(args) + .succeeds() + .stdout_move_str() } diff --git a/tests/by-util/test_whoami.rs b/tests/by-util/test_whoami.rs index c6ec6990f..a98541b2d 100644 --- a/tests/by-util/test_whoami.rs +++ b/tests/by-util/test_whoami.rs @@ -1,50 +1,63 @@ use crate::common::util::*; -use std::env; + +// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'. +// If we are running inside the CI and "needle" is in "stderr" skipping this test is +// considered okay. If we are not inside the CI this calls assert!(result.success). +// +// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)" +// stderr: "whoami: failed to get username" +// Maybe: "adduser --uid 1001 username" can put things right? +fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool { + if !result.succeeded() { + println!("result.stdout = {}", result.stdout_str()); + println!("result.stderr = {}", result.stderr_str()); + if is_ci() && result.stderr_str().contains(needle) { + println!("test skipped:"); + return true; + } else { + result.success(); + } + } + false +} #[test] fn test_normal() { let (_, mut ucmd) = at_and_ucmd!(); let result = ucmd.run(); - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok()); - for (key, value) in env::vars() { - println!("{}: {}", key, value); - } - if is_ci() && result.stderr.contains("failed to get username") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + // use std::env; + // println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok()); + // for (key, value) in env::vars() { + // println!("{}: {}", key, value); + // } + + if skipping_test_is_okay(&result, "failed to get username") { return; } - assert!(result.success); - assert!(!result.stdout.trim().is_empty()); + result.no_stderr(); + assert!(!result.stdout_str().trim().is_empty()); } #[test] #[cfg(not(windows))] fn test_normal_compare_id() { - let (_, mut ucmd) = at_and_ucmd!(); + let scene = TestScenario::new(util_name!()); - let result = ucmd.run(); - - println!("result.stdout = {}", result.stdout); - println!("result.stderr = {}", result.stderr); - if is_ci() && result.stderr.contains("failed to get username") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + let result_ucmd = scene.ucmd().run(); + if skipping_test_is_okay(&result_ucmd, "failed to get username") { return; } - assert!(result.success); - let ts = TestScenario::new("id"); - let id = ts.cmd("id").arg("-un").run(); - if is_ci() && id.stderr.contains("cannot find name for user ID") { - // In the CI, some server are failing to return whoami. - // As seems to be a configuration issue, ignoring it + let result_cmd = scene.cmd("id").arg("-un").run(); + if skipping_test_is_okay(&result_cmd, "cannot find name for user ID") { return; } - assert_eq!(result.stdout.trim(), id.stdout.trim()); + + assert_eq!( + result_ucmd.stdout_str().trim(), + result_cmd.stdout_str().trim() + ); } diff --git a/tests/common/macros.rs b/tests/common/macros.rs index 645cfcc67..03d2051d0 100644 --- a/tests/common/macros.rs +++ b/tests/common/macros.rs @@ -1,31 +1,4 @@ -#[macro_export] -macro_rules! assert_empty_stderr( - ($cond:expr) => ( - if $cond.stderr.len() > 0 { - panic!(format!("stderr: {}", $cond.stderr)) - } - ); -); - -#[macro_export] -macro_rules! assert_empty_stdout( - ($cond:expr) => ( - if $cond.stdout.len() > 0 { - panic!(format!("stdout: {}", $cond.stdout)) - } - ); -); - -#[macro_export] -macro_rules! assert_no_error( - ($cond:expr) => ( - assert!($cond.success); - if $cond.stderr.len() > 0 { - panic!(format!("stderr: {}", $cond.stderr)) - } - ); -); - +/// Platform-independent helper for constructing a PathBuf from individual elements #[macro_export] macro_rules! path_concat { ($e:expr, ..$n:expr) => {{ @@ -47,6 +20,9 @@ macro_rules! path_concat { }}; } +/// Deduce the name of the test binary from the test filename. +/// +/// e.g.: `tests/by-util/test_cat.rs` -> `cat` #[macro_export] macro_rules! util_name { () => { @@ -54,6 +30,16 @@ macro_rules! util_name { }; } +/// Convenience macro for acquiring a [`UCommand`] builder. +/// +/// Returns the following: +/// - a [`UCommand`] builder for invoking the binary to be tested +/// +/// This macro is intended for quick, single-call tests. For more complex tests +/// that require multiple invocations of the tested binary, see [`TestScenario`] +/// +/// [`UCommand`]: crate::tests::common::util::UCommand +/// [`TestScenario]: crate::tests::common::util::TestScenario #[macro_export] macro_rules! new_ucmd { () => { @@ -61,6 +47,18 @@ macro_rules! new_ucmd { }; } +/// Convenience macro for acquiring a [`UCommand`] builder and a test path. +/// +/// Returns a tuple containing the following: +/// - an [`AtPath`] that points to a unique temporary test directory +/// - a [`UCommand`] builder for invoking the binary to be tested +/// +/// This macro is intended for quick, single-call tests. For more complex tests +/// that require multiple invocations of the tested binary, see [`TestScenario`] +/// +/// [`UCommand`]: crate::tests::common::util::UCommand +/// [`AtPath`]: crate::tests::common::util::AtPath +/// [`TestScenario]: crate::tests::common::util::TestScenario #[macro_export] macro_rules! at_and_ucmd { () => {{ diff --git a/tests/common/util.rs b/tests/common/util.rs index 0f1acd49a..f881cff21 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -1,6 +1,15 @@ +//spell-checker: ignore (linux) rlimit prlimit Rlim + +#![allow(dead_code)] + +use pretty_assertions::assert_eq; +#[cfg(target_os = "linux")] +use rlimit::{prlimit, rlim}; use std::env; +#[cfg(not(windows))] +use std::ffi::CString; use std::ffi::OsStr; -use std::fs::{self, File, OpenOptions}; +use std::fs::{self, hard_link, File, OpenOptions}; use std::io::{Read, Result, Write}; #[cfg(unix)] use std::os::unix::fs::{symlink as symlink_dir, symlink as symlink_file}; @@ -9,10 +18,10 @@ use std::os::windows::fs::{symlink_dir, symlink_file}; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; use std::rc::Rc; -use std::str::from_utf8; use std::thread::sleep; use std::time::Duration; use tempfile::TempDir; +use uucore::{Args, InvalidEncodingHandling}; #[cfg(windows)] static PROGNAME: &str = concat!(env!("CARGO_PKG_NAME"), ".exe"); @@ -23,154 +32,375 @@ static TESTS_DIR: &str = "tests"; static FIXTURES_DIR: &str = "fixtures"; static ALREADY_RUN: &str = " you have already run this UCommand, if you want to run \ - another command in the same test, use TestScenario::new instead of \ - testing();"; + another command in the same test, use TestScenario::new instead of \ + testing();"; static MULTIPLE_STDIN_MEANINGLESS: &str = "Ucommand is designed around a typical use case of: provide args and input stream -> spawn process -> block until completion -> return output streams. For verifying that a particular section of the input stream is what causes a particular behavior, use the Command type directly."; -/// Test if the program are running under CI +static NO_STDIN_MEANINGLESS: &str = "Setting this flag has no effect if there is no stdin"; + +/// Test if the program is running under CI pub fn is_ci() -> bool { std::env::var("CI") - .unwrap_or(String::from("false")) + .unwrap_or_else(|_| String::from("false")) .eq_ignore_ascii_case("true") } -/// Test if the program is running under WSL -// ref: @@ -// ToDO: test on WSL2 which likely doesn't need special handling; plan change to `is_wsl_1()` if WSL2 is less needy -pub fn is_wsl() -> bool { - #[cfg(target_os = "linux")] - { - if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") { - if let Ok(s) = std::str::from_utf8(&b) { - let a = s.to_ascii_lowercase(); - return a.contains("microsoft") || a.contains("wsl"); - } - } - } - false -} - -fn read_scenario_fixture>(tmpd: &Option>, file_rel_path: S) -> String { +/// Read a test scenario fixture, returning its bytes +fn read_scenario_fixture>(tmpd: &Option>, file_rel_path: S) -> Vec { let tmpdir_path = tmpd.as_ref().unwrap().as_ref().path(); - AtPath::new(tmpdir_path).read(file_rel_path.as_ref().to_str().unwrap()) -} - -pub fn repeat_str(s: &str, n: u32) -> String { - let mut repeated = String::new(); - for _ in 0..n { - repeated.push_str(s); - } - repeated + AtPath::new(tmpdir_path).read_bytes(file_rel_path.as_ref().to_str().unwrap()) } /// A command result is the outputs of a command (streams and status code) /// within a struct which has convenience assertion functions about those outputs -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct CmdResult { //tmpd is used for convenience functions for asserts against fixtures tmpd: Option>, /// exit status for command (if there is one) - pub code: Option, + code: Option, /// zero-exit from running the Command? /// see [`success`] - pub success: bool, - /// captured utf-8 standard output after running the Command - pub stdout: String, - /// captured utf-8 standard error after running the Command - pub stderr: String, + success: bool, + /// captured standard output after running the Command + stdout: Vec, + /// captured standard error after running the Command + stderr: Vec, } impl CmdResult { + pub fn new( + tmpd: Option>, + code: Option, + success: bool, + stdout: &[u8], + stderr: &[u8], + ) -> CmdResult { + CmdResult { + tmpd, + code, + success, + stdout: stdout.to_vec(), + stderr: stderr.to_vec(), + } + } + + /// Returns a reference to the program's standard output as a slice of bytes + pub fn stdout(&self) -> &[u8] { + &self.stdout + } + + /// Returns the program's standard output as a string slice + pub fn stdout_str(&self) -> &str { + std::str::from_utf8(&self.stdout).unwrap() + } + + /// Returns the program's standard output as a string + /// consumes self + pub fn stdout_move_str(self) -> String { + String::from_utf8(self.stdout).unwrap() + } + + /// Returns the program's standard output as a vec of bytes + /// consumes self + pub fn stdout_move_bytes(self) -> Vec { + self.stdout + } + + /// Returns a reference to the program's standard error as a slice of bytes + pub fn stderr(&self) -> &[u8] { + &self.stderr + } + + /// Returns the program's standard error as a string slice + pub fn stderr_str(&self) -> &str { + std::str::from_utf8(&self.stderr).unwrap() + } + + /// Returns the program's standard error as a string + /// consumes self + pub fn stderr_move_str(self) -> String { + String::from_utf8(self.stderr).unwrap() + } + + /// Returns the program's standard error as a vec of bytes + /// consumes self + pub fn stderr_move_bytes(self) -> Vec { + self.stderr + } + + /// Returns the program's exit code + /// Panics if not run + pub fn code(&self) -> i32 { + self.code.expect("Program must be run first") + } + + pub fn code_is(&self, expected_code: i32) -> &CmdResult { + assert_eq!(self.code(), expected_code); + self + } + + /// Returns the program's TempDir + /// Panics if not present + pub fn tmpd(&self) -> Rc { + match &self.tmpd { + Some(ptr) => ptr.clone(), + None => panic!("Command not associated with a TempDir"), + } + } + + /// Returns whether the program succeeded + pub fn succeeded(&self) -> bool { + self.success + } + /// asserts that the command resulted in a success (zero) status code - pub fn success(&self) -> Box<&CmdResult> { - assert!(self.success); - Box::new(self) + pub fn success(&self) -> &CmdResult { + if !self.success { + panic!( + "Command was expected to succeed.\nstdout = {}\n stderr = {}", + self.stdout_str(), + self.stderr_str() + ); + } + self } /// asserts that the command resulted in a failure (non-zero) status code - pub fn failure(&self) -> Box<&CmdResult> { - assert!(!self.success); - Box::new(self) + pub fn failure(&self) -> &CmdResult { + if self.success { + panic!( + "Command was expected to fail.\nstdout = {}\n stderr = {}", + self.stdout_str(), + self.stderr_str() + ); + } + self } /// asserts that the command's exit code is the same as the given one - pub fn status_code(&self, code: i32) -> Box<&CmdResult> { - assert!(self.code == Some(code)); - Box::new(self) + pub fn status_code(&self, code: i32) -> &CmdResult { + assert_eq!(self.code, Some(code)); + self } /// asserts that the command resulted in empty (zero-length) stderr stream output /// generally, it's better to use stdout_only() instead, /// but you might find yourself using this function if - /// 1. you can not know exactly what stdout will be - /// or 2. you know that stdout will also be empty - pub fn no_stderr(&self) -> Box<&CmdResult> { - assert_eq!(self.stderr, ""); - Box::new(self) + /// 1. you can not know exactly what stdout will be or + /// 2. you know that stdout will also be empty + pub fn no_stderr(&self) -> &CmdResult { + if !self.stderr.is_empty() { + panic!( + "Expected stderr to be empty, but it's:\n{}", + self.stderr_str() + ); + } + self } /// asserts that the command resulted in empty (zero-length) stderr stream output /// unless asserting there was neither stdout or stderr, stderr_only is usually a better choice /// generally, it's better to use stderr_only() instead, /// but you might find yourself using this function if - /// 1. you can not know exactly what stderr will be - /// or 2. you know that stderr will also be empty - pub fn no_stdout(&self) -> Box<&CmdResult> { - assert_eq!(self.stdout, ""); - Box::new(self) + /// 1. you can not know exactly what stderr will be or + /// 2. you know that stderr will also be empty + pub fn no_stdout(&self) -> &CmdResult { + if !self.stdout.is_empty() { + panic!( + "Expected stdout to be empty, but it's:\n{}", + self.stderr_str() + ); + } + self } /// asserts that the command resulted in stdout stream output that equals the /// passed in value, trailing whitespace are kept to force strict comparison (#1235) /// stdout_only is a better choice unless stderr may or will be non-empty - pub fn stdout_is>(&self, msg: T) -> Box<&CmdResult> { - assert_eq!(self.stdout, String::from(msg.as_ref())); - Box::new(self) + pub fn stdout_is>(&self, msg: T) -> &CmdResult { + assert_eq!(self.stdout_str(), String::from(msg.as_ref())); + self + } + + /// like `stdout_is`, but succeeds if any elements of `expected` matches stdout. + pub fn stdout_is_any + std::fmt::Debug>(&self, expected: Vec) -> &CmdResult { + if !expected.iter().any(|msg| self.stdout_str() == msg.as_ref()) { + panic!( + "stdout was {}\nExpected any of {:#?}", + self.stdout_str(), + expected + ) + } + self + } + + /// Like `stdout_is` but newlines are normalized to `\n`. + pub fn normalized_newlines_stdout_is>(&self, msg: T) -> &CmdResult { + let msg = msg.as_ref().replace("\r\n", "\n"); + assert_eq!(self.stdout_str().replace("\r\n", "\n"), msg); + self + } + + /// asserts that the command resulted in stdout stream output, + /// whose bytes equal those of the passed in slice + pub fn stdout_is_bytes>(&self, msg: T) -> &CmdResult { + assert_eq!(self.stdout, msg.as_ref()); + self } /// like stdout_is(...), but expects the contents of the file at the provided relative path - pub fn stdout_is_fixture>(&self, file_rel_path: T) -> Box<&CmdResult> { + pub fn stdout_is_fixture>(&self, file_rel_path: T) -> &CmdResult { let contents = read_scenario_fixture(&self.tmpd, file_rel_path); + self.stdout_is(String::from_utf8(contents).unwrap()) + } + /// like stdout_is_fixture(...), but replaces the data in fixture file based on values provided in template_vars + /// command output + pub fn stdout_is_templated_fixture>( + &self, + file_rel_path: T, + template_vars: &[(&str, &str)], + ) -> &CmdResult { + let mut contents = + String::from_utf8(read_scenario_fixture(&self.tmpd, file_rel_path)).unwrap(); + for kv in template_vars { + contents = contents.replace(kv.0, kv.1); + } self.stdout_is(contents) } + /// like `stdout_is_templated_fixture`, but succeeds if any replacement by `template_vars` results in the actual stdout. + pub fn stdout_is_templated_fixture_any>( + &self, + file_rel_path: T, + template_vars: &[Vec<(String, String)>], + ) { + let contents = String::from_utf8(read_scenario_fixture(&self.tmpd, file_rel_path)).unwrap(); + let possible_values = template_vars.iter().map(|vars| { + let mut contents = contents.clone(); + for kv in vars.iter() { + contents = contents.replace(&kv.0, &kv.1); + } + contents + }); + self.stdout_is_any(possible_values.collect()); + } + /// asserts that the command resulted in stderr stream output that equals the /// passed in value, when both are trimmed of trailing whitespace /// stderr_only is a better choice unless stdout may or will be non-empty - pub fn stderr_is>(&self, msg: T) -> Box<&CmdResult> { + pub fn stderr_is>(&self, msg: T) -> &CmdResult { assert_eq!( - self.stderr.trim_end(), + self.stderr_str().trim_end(), String::from(msg.as_ref()).trim_end() ); - Box::new(self) + self + } + + /// asserts that the command resulted in stderr stream output, + /// whose bytes equal those of the passed in slice + pub fn stderr_is_bytes>(&self, msg: T) -> &CmdResult { + assert_eq!(self.stderr, msg.as_ref()); + self + } + + /// Like stdout_is_fixture, but for stderr + pub fn stderr_is_fixture>(&self, file_rel_path: T) -> &CmdResult { + let contents = read_scenario_fixture(&self.tmpd, file_rel_path); + self.stderr_is(String::from_utf8(contents).unwrap()) } /// asserts that - /// 1. the command resulted in stdout stream output that equals the - /// passed in value, when both are trimmed of trailing whitespace - /// and 2. the command resulted in empty (zero-length) stderr stream output - pub fn stdout_only>(&self, msg: T) -> Box<&CmdResult> { + /// 1. the command resulted in stdout stream output that equals the + /// passed in value + /// 2. the command resulted in empty (zero-length) stderr stream output + pub fn stdout_only>(&self, msg: T) -> &CmdResult { self.no_stderr().stdout_is(msg) } + /// asserts that + /// 1. the command resulted in a stdout stream whose bytes + /// equal those of the passed in value + /// 2. the command resulted in an empty stderr stream + pub fn stdout_only_bytes>(&self, msg: T) -> &CmdResult { + self.no_stderr().stdout_is_bytes(msg) + } + /// like stdout_only(...), but expects the contents of the file at the provided relative path - pub fn stdout_only_fixture>(&self, file_rel_path: T) -> Box<&CmdResult> { + pub fn stdout_only_fixture>(&self, file_rel_path: T) -> &CmdResult { let contents = read_scenario_fixture(&self.tmpd, file_rel_path); - self.stdout_only(contents) + self.stdout_only_bytes(contents) } /// asserts that - /// 1. the command resulted in stderr stream output that equals the - /// passed in value, when both are trimmed of trailing whitespace - /// and 2. the command resulted in empty (zero-length) stdout stream output - pub fn stderr_only>(&self, msg: T) -> Box<&CmdResult> { + /// 1. the command resulted in stderr stream output that equals the + /// passed in value, when both are trimmed of trailing whitespace + /// 2. the command resulted in empty (zero-length) stdout stream output + pub fn stderr_only>(&self, msg: T) -> &CmdResult { self.no_stdout().stderr_is(msg) } - pub fn fails_silently(&self) -> Box<&CmdResult> { + /// asserts that + /// 1. the command resulted in a stderr stream whose bytes equal the ones + /// of the passed value + /// 2. the command resulted in an empty stdout stream + pub fn stderr_only_bytes>(&self, msg: T) -> &CmdResult { + self.no_stderr().stderr_is_bytes(msg) + } + + pub fn fails_silently(&self) -> &CmdResult { assert!(!self.success); - assert_eq!(self.stderr, ""); - Box::new(self) + assert!(self.stderr.is_empty()); + self + } + + pub fn stdout_contains>(&self, cmp: T) -> &CmdResult { + assert!( + self.stdout_str().contains(cmp.as_ref()), + "'{}' does not contain '{}'", + self.stdout_str(), + cmp.as_ref() + ); + self + } + + pub fn stderr_contains>(&self, cmp: T) -> &CmdResult { + assert!( + self.stderr_str().contains(cmp.as_ref()), + "'{}' does not contain '{}'", + self.stderr_str(), + cmp.as_ref() + ); + self + } + + pub fn stdout_does_not_contain>(&self, cmp: T) -> &CmdResult { + assert!( + !self.stdout_str().contains(cmp.as_ref()), + "'{}' contains '{}' but should not", + self.stdout_str(), + cmp.as_ref(), + ); + self + } + + pub fn stderr_does_not_contain>(&self, cmp: T) -> &CmdResult { + assert!(!self.stderr_str().contains(cmp.as_ref())); + self + } + + pub fn stdout_matches(&self, regex: ®ex::Regex) -> &CmdResult { + if !regex.is_match(self.stdout_str().trim()) { + panic!("Stdout does not match regex:\n{}", self.stdout_str()) + } + self + } + + pub fn stdout_does_not_match(&self, regex: ®ex::Regex) -> &CmdResult { + if regex.is_match(self.stdout_str().trim()) { + panic!("Stdout matches regex:\n{}", self.stdout_str()) + } + self } } @@ -248,6 +478,13 @@ impl AtPath { String::from(self.minus(name).to_str().unwrap()) } + pub fn set_readonly(&self, name: &str) { + let metadata = fs::metadata(self.plus(name)).unwrap(); + let mut permissions = metadata.permissions(); + permissions.set_readonly(true); + fs::set_permissions(self.plus(name), permissions).unwrap(); + } + pub fn open(&self, name: &str) -> File { log_info("open", self.plus_as_string(name)); File::open(self.plus(name)).unwrap() @@ -256,13 +493,29 @@ impl AtPath { pub fn read(&self, name: &str) -> String { let mut f = self.open(name); let mut contents = String::new(); - let _ = f.read_to_string(&mut contents); + f.read_to_string(&mut contents) + .unwrap_or_else(|e| panic!("Couldn't read {}: {}", name, e)); + contents + } + + pub fn read_bytes(&self, name: &str) -> Vec { + let mut f = self.open(name); + let mut contents = Vec::new(); + f.read_to_end(&mut contents) + .unwrap_or_else(|e| panic!("Couldn't read {}: {}", name, e)); contents } pub fn write(&self, name: &str, contents: &str) { log_info("open(write)", self.plus_as_string(name)); - let _ = std::fs::write(self.plus(name), contents); + std::fs::write(self.plus(name), contents) + .unwrap_or_else(|e| panic!("Couldn't write {}: {}", name, e)); + } + + pub fn write_bytes(&self, name: &str, contents: &[u8]) { + log_info("open(write)", self.plus_as_string(name)); + std::fs::write(self.plus(name), contents) + .unwrap_or_else(|e| panic!("Couldn't write {}: {}", name, e)); } pub fn append(&self, name: &str, contents: &str) { @@ -272,7 +525,19 @@ impl AtPath { .append(true) .open(self.plus(name)) .unwrap(); - let _ = f.write(contents.as_bytes()); + f.write_all(contents.as_bytes()) + .unwrap_or_else(|e| panic!("Couldn't write {}: {}", name, e)); + } + + pub fn append_bytes(&self, name: &str, contents: &[u8]) { + log_info("open(append)", self.plus_as_string(name)); + let mut f = OpenOptions::new() + .write(true) + .append(true) + .open(self.plus(name)) + .unwrap(); + f.write_all(contents) + .unwrap_or_else(|e| panic!("Couldn't append to {}: {}", name, e)); } pub fn mkdir(&self, dir: &str) { @@ -296,6 +561,37 @@ impl AtPath { File::create(&self.plus(file)).unwrap(); } + #[cfg(not(windows))] + pub fn mkfifo(&self, fifo: &str) { + let full_path = self.plus_as_string(fifo); + log_info("mkfifo", &full_path); + unsafe { + let fifo_name: CString = CString::new(full_path).expect("CString creation failed."); + libc::mkfifo(fifo_name.as_ptr(), libc::S_IWUSR | libc::S_IRUSR); + } + } + + #[cfg(not(windows))] + pub fn is_fifo(&self, fifo: &str) -> bool { + unsafe { + let name = CString::new(self.plus_as_string(fifo)).unwrap(); + let mut stat: libc::stat = std::mem::zeroed(); + if libc::stat(name.as_ptr(), &mut stat) >= 0 { + libc::S_IFIFO & stat.st_mode != 0 + } else { + false + } + } + } + + pub fn hard_link(&self, src: &str, dst: &str) { + log_info( + "hard_link", + &format!("{},{}", self.plus_as_string(src), self.plus_as_string(dst)), + ); + hard_link(&self.plus(src), &self.plus(dst)).unwrap(); + } + pub fn symlink_file(&self, src: &str, dst: &str) { log_info( "symlink", @@ -374,18 +670,29 @@ impl AtPath { // Source: // http://stackoverflow.com/questions/31439011/getfinalpathnamebyhandle-without-prepended let prefix = "\\\\?\\"; + // FixME: replace ... + #[allow(clippy::manual_strip)] if s.starts_with(prefix) { String::from(&s[prefix.len()..]) } else { s } + // ... with ... + // if let Some(stripped) = s.strip_prefix(prefix) { + // String::from(stripped) + // } else { + // s + // } + // ... when using MSRV with stabilized `strip_prefix()` } } /// An environment for running a single uutils test case, serves three functions: /// 1. centralizes logic for locating the uutils binary and calling the utility -/// 2. provides a temporary directory for the test case +/// 2. provides a unique temporary directory for the test case /// 3. copies over fixtures for the utility to the temporary directory +/// +/// Fixtures can be found under `tests/fixtures/$util_name/` pub struct TestScenario { bin_path: PathBuf, util_name: String, @@ -398,7 +705,7 @@ impl TestScenario { let tmpd = Rc::new(TempDir::new().unwrap()); let ts = TestScenario { bin_path: { - // Instead of hardcoding the path relative to the current + // Instead of hard coding the path relative to the current // directory, use Cargo's OUT_DIR to find path to executable. // This allows tests to be run using profiles other than debug. let target_dir = path_concat!(env!("OUT_DIR"), "..", "..", "..", PROGNAME); @@ -406,7 +713,7 @@ impl TestScenario { }, util_name: String::from(util_name), fixtures: AtPath::new(tmpd.as_ref().path()), - tmpd: tmpd, + tmpd, }; let mut fixture_path_builder = env::current_dir().unwrap(); fixture_path_builder.push(TESTS_DIR); @@ -420,16 +727,28 @@ impl TestScenario { ts } + /// Returns builder for invoking the target uutils binary. Paths given are + /// treated relative to the environment's unique temporary test directory. pub fn ucmd(&self) -> UCommand { let mut cmd = self.cmd(&self.bin_path); cmd.arg(&self.util_name); cmd } + /// Returns builder for invoking any system command. Paths given are treated + /// relative to the environment's unique temporary test directory. pub fn cmd>(&self, bin: S) -> UCommand { UCommand::new_from_tmp(bin, self.tmpd.clone(), true) } + /// Returns builder for invoking any uutils command. Paths given are treated + /// relative to the environment's unique temporary test directory. + pub fn ccmd>(&self, bin: S) -> UCommand { + let mut cmd = self.cmd(&self.bin_path); + cmd.arg(bin); + cmd + } + // different names are used rather than an argument // because the need to keep the environment is exceedingly rare. pub fn ucmd_keepenv(&self) -> UCommand { @@ -438,6 +757,10 @@ impl TestScenario { cmd } + /// Returns builder for invoking any system command. Paths given are treated + /// relative to the environment's unique temporary test directory. + /// Differs from the builder returned by `cmd` in that `cmd_keepenv` does not call + /// `Command::env_clear` (Clears the entire environment map for the child process.) pub fn cmd_keepenv>(&self, bin: S) -> UCommand { UCommand::new_from_tmp(bin, self.tmpd.clone(), false) } @@ -455,7 +778,13 @@ pub struct UCommand { comm_string: String, tmpd: Option>, has_run: bool, - stdin: Option>, + ignore_stdin_write_error: bool, + stdin: Option, + stdout: Option, + stderr: Option, + bytes_into_stdin: Option>, + #[cfg(target_os = "linux")] + limits: Vec<(rlimit::Resource, rlim, rlim)>, } impl UCommand { @@ -468,9 +797,10 @@ impl UCommand { cmd.current_dir(curdir.as_ref()); if env_clear { if cfg!(windows) { + // spell-checker:ignore (dll) rsaenh // %SYSTEMROOT% is required on Windows to initialize crypto provider // ... and crypto provider is required for std::rand - // From procmon: RegQueryValue HKLM\SOFTWARE\Microsoft\Cryptography\Defaults\Provider\Microsoft Strong Cryptographic Provider\Image Path + // From `procmon`: RegQueryValue HKLM\SOFTWARE\Microsoft\Cryptography\Defaults\Provider\Microsoft Strong Cryptographic Provider\Image Path // SUCCESS Type: REG_SZ, Length: 66, Data: %SystemRoot%\system32\rsaenh.dll" for (key, _) in env::vars_os() { if key.as_os_str() != "SYSTEMROOT" { @@ -484,7 +814,13 @@ impl UCommand { cmd }, comm_string: String::from(arg.as_ref().to_str().unwrap()), + ignore_stdin_write_error: false, + bytes_into_stdin: None, stdin: None, + stdout: None, + stderr: None, + #[cfg(target_os = "linux")] + limits: vec![], } } @@ -495,82 +831,145 @@ impl UCommand { ucmd } - pub fn arg>(&mut self, arg: S) -> Box<&mut UCommand> { - if self.has_run { - panic!(ALREADY_RUN); - } - self.comm_string.push_str(" "); - self.comm_string.push_str(arg.as_ref().to_str().unwrap()); - self.raw.arg(arg.as_ref()); - Box::new(self) + pub fn set_stdin>(&mut self, stdin: T) -> &mut UCommand { + self.stdin = Some(stdin.into()); + self } - pub fn args>(&mut self, args: &[S]) -> Box<&mut UCommand> { + pub fn set_stdout>(&mut self, stdout: T) -> &mut UCommand { + self.stdout = Some(stdout.into()); + self + } + + pub fn set_stderr>(&mut self, stderr: T) -> &mut UCommand { + self.stderr = Some(stderr.into()); + self + } + + /// Add a parameter to the invocation. Path arguments are treated relative + /// to the test environment directory. + pub fn arg>(&mut self, arg: S) -> &mut UCommand { if self.has_run { - panic!(MULTIPLE_STDIN_MEANINGLESS); + panic!("{}", ALREADY_RUN); } - for s in args { - self.comm_string.push_str(" "); - self.comm_string.push_str(s.as_ref().to_str().unwrap()); + self.comm_string.push(' '); + self.comm_string + .push_str(arg.as_ref().to_str().unwrap_or_default()); + self.raw.arg(arg.as_ref()); + self + } + + /// Add multiple parameters to the invocation. Path arguments are treated relative + /// to the test environment directory. + pub fn args>(&mut self, args: &[S]) -> &mut UCommand { + if self.has_run { + panic!("{}", MULTIPLE_STDIN_MEANINGLESS); + } + let strings = args + .iter() + .map(|s| s.as_ref().to_os_string()) + .collect_str(InvalidEncodingHandling::Ignore) + .accept_any(); + + for s in strings { + self.comm_string.push(' '); + self.comm_string.push_str(&s); } self.raw.args(args.as_ref()); - Box::new(self) + self } - /// provides stdinput to feed in to the command when spawned - pub fn pipe_in>>(&mut self, input: T) -> Box<&mut UCommand> { - if self.stdin.is_some() { - panic!(MULTIPLE_STDIN_MEANINGLESS); + /// provides standard input to feed in to the command when spawned + pub fn pipe_in>>(&mut self, input: T) -> &mut UCommand { + if self.bytes_into_stdin.is_some() { + panic!("{}", MULTIPLE_STDIN_MEANINGLESS); } - self.stdin = Some(input.into()); - Box::new(self) + self.bytes_into_stdin = Some(input.into()); + self } /// like pipe_in(...), but uses the contents of the file at the provided relative path as the piped in data - pub fn pipe_in_fixture>(&mut self, file_rel_path: S) -> Box<&mut UCommand> { + pub fn pipe_in_fixture>(&mut self, file_rel_path: S) -> &mut UCommand { let contents = read_scenario_fixture(&self.tmpd, file_rel_path); self.pipe_in(contents) } - pub fn env(&mut self, key: K, val: V) -> Box<&mut UCommand> + /// Ignores error caused by feeding stdin to the command. + /// This is typically useful to test non-standard workflows + /// like feeding something to a command that does not read it + pub fn ignore_stdin_write_error(&mut self) -> &mut UCommand { + if self.bytes_into_stdin.is_none() { + panic!("{}", NO_STDIN_MEANINGLESS); + } + self.ignore_stdin_write_error = true; + self + } + + pub fn env(&mut self, key: K, val: V) -> &mut UCommand where K: AsRef, V: AsRef, { if self.has_run { - panic!(ALREADY_RUN); + panic!("{}", ALREADY_RUN); } self.raw.env(key, val); - Box::new(self) + self + } + + #[cfg(target_os = "linux")] + pub fn with_limit( + &mut self, + resource: rlimit::Resource, + soft_limit: rlim, + hard_limit: rlim, + ) -> &mut Self { + self.limits.push((resource, soft_limit, hard_limit)); + self } /// Spawns the command, feeds the stdin if any, and returns the /// child process immediately. pub fn run_no_wait(&mut self) -> Child { if self.has_run { - panic!(ALREADY_RUN); + panic!("{}", ALREADY_RUN); } self.has_run = true; log_info("run", &self.comm_string); - let mut result = self + let mut child = self .raw - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) + .stdin(self.stdin.take().unwrap_or_else(Stdio::piped)) + .stdout(self.stdout.take().unwrap_or_else(Stdio::piped)) + .stderr(self.stderr.take().unwrap_or_else(Stdio::piped)) .spawn() .unwrap(); - if let Some(ref input) = self.stdin { - result + #[cfg(target_os = "linux")] + for &(resource, soft_limit, hard_limit) in &self.limits { + prlimit( + child.id() as i32, + resource, + Some((soft_limit, hard_limit)), + None, + ) + .unwrap(); + } + + if let Some(ref input) = self.bytes_into_stdin { + let write_result = child .stdin .take() .unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input) - .unwrap_or_else(|e| panic!("{}", e)); + .write_all(input); + if !self.ignore_stdin_write_error { + if let Err(e) = write_result { + panic!("failed to write to stdin of child: {}", e) + } + } } - result + child } /// Spawns the command, feeds the stdin if any, waits for the result @@ -583,8 +982,8 @@ impl UCommand { tmpd: self.tmpd.clone(), code: prog.status.code(), success: prog.status.success(), - stdout: from_utf8(&prog.stdout).unwrap().to_string(), - stderr: from_utf8(&prog.stderr).unwrap().to_string(), + stdout: prog.stdout, + stderr: prog.stderr, } } @@ -611,6 +1010,11 @@ impl UCommand { cmd_result.failure(); cmd_result } + + pub fn get_full_fixture_path(&self, file_rel_path: &str) -> String { + let tmpdir_path = self.tmpd.as_ref().unwrap().path(); + format!("{}/{}", tmpdir_path.to_str().unwrap(), file_rel_path) + } } pub fn read_size(child: &mut Child, size: usize) -> String { @@ -621,7 +1025,251 @@ pub fn read_size(child: &mut Child, size: usize) -> String { .stdout .as_mut() .unwrap() - .read(output.as_mut_slice()) + .read_exact(output.as_mut_slice()) .unwrap(); String::from_utf8(output).unwrap() } + +pub fn vec_of_size(n: usize) -> Vec { + let result = vec![b'a'; n]; + assert_eq!(result.len(), n); + result +} + +/// Sanity checks for test utils +#[cfg(test)] +mod tests { + // spell-checker:ignore (tests) asdfsadfa + use super::*; + + #[test] + fn test_code_is() { + let res = CmdResult { + tmpd: None, + code: Some(32), + success: false, + stdout: "".into(), + stderr: "".into(), + }; + res.code_is(32); + } + + #[test] + #[should_panic] + fn test_code_is_fail() { + let res = CmdResult { + tmpd: None, + code: Some(32), + success: false, + stdout: "".into(), + stderr: "".into(), + }; + res.code_is(1); + } + + #[test] + fn test_failure() { + let res = CmdResult { + tmpd: None, + code: None, + success: false, + stdout: "".into(), + stderr: "".into(), + }; + res.failure(); + } + + #[test] + #[should_panic] + fn test_failure_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "".into(), + stderr: "".into(), + }; + res.failure(); + } + + #[test] + fn test_success() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "".into(), + stderr: "".into(), + }; + res.success(); + } + + #[test] + #[should_panic] + fn test_success_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: false, + stdout: "".into(), + stderr: "".into(), + }; + res.success(); + } + + #[test] + fn test_no_stderr_output() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "".into(), + stderr: "".into(), + }; + res.no_stderr(); + res.no_stdout(); + } + + #[test] + #[should_panic] + fn test_no_stderr_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "".into(), + stderr: "asdfsadfa".into(), + }; + + res.no_stderr(); + } + + #[test] + #[should_panic] + fn test_no_stdout_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "asdfsadfa".into(), + stderr: "".into(), + }; + + res.no_stdout(); + } + + #[test] + fn test_std_does_not_contain() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "This is a likely error message\n".into(), + stderr: "This is a likely error message\n".into(), + }; + res.stdout_does_not_contain("unlikely"); + res.stderr_does_not_contain("unlikely"); + } + + #[test] + #[should_panic] + fn test_stdout_does_not_contain_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "This is a likely error message\n".into(), + stderr: "".into(), + }; + + res.stdout_does_not_contain("likely"); + } + + #[test] + #[should_panic] + fn test_stderr_does_not_contain_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "".into(), + stderr: "This is a likely error message\n".into(), + }; + + res.stderr_does_not_contain("likely"); + } + + #[test] + fn test_stdout_matches() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "This is a likely error message\n".into(), + stderr: "This is a likely error message\n".into(), + }; + let positive = regex::Regex::new(".*likely.*").unwrap(); + let negative = regex::Regex::new(".*unlikely.*").unwrap(); + res.stdout_matches(&positive); + res.stdout_does_not_match(&negative); + } + + #[test] + #[should_panic] + fn test_stdout_matches_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "This is a likely error message\n".into(), + stderr: "This is a likely error message\n".into(), + }; + let negative = regex::Regex::new(".*unlikely.*").unwrap(); + + res.stdout_matches(&negative); + } + + #[test] + #[should_panic] + fn test_stdout_not_matches_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "This is a likely error message\n".into(), + stderr: "This is a likely error message\n".into(), + }; + let positive = regex::Regex::new(".*likely.*").unwrap(); + + res.stdout_does_not_match(&positive); + } + + #[test] + fn test_normalized_newlines_stdout_is() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "A\r\nB\nC".into(), + stderr: "".into(), + }; + + res.normalized_newlines_stdout_is("A\r\nB\nC"); + res.normalized_newlines_stdout_is("A\nB\nC"); + res.normalized_newlines_stdout_is("A\nB\r\nC"); + } + + #[test] + #[should_panic] + fn test_normalized_newlines_stdout_is_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "A\r\nB\nC".into(), + stderr: "".into(), + }; + + res.normalized_newlines_stdout_is("A\r\nB\nC\n"); + } +} diff --git a/tests/fixtures/base32/input-simple.txt b/tests/fixtures/base32/input-simple.txt new file mode 100644 index 000000000..8ab686eaf --- /dev/null +++ b/tests/fixtures/base32/input-simple.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/tests/fixtures/base64/input-simple.txt b/tests/fixtures/base64/input-simple.txt new file mode 100644 index 000000000..8ab686eaf --- /dev/null +++ b/tests/fixtures/base64/input-simple.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/tests/fixtures/cat/empty.txt b/tests/fixtures/cat/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cat/three_directories_and_file_and_stdin.stderr.expected b/tests/fixtures/cat/three_directories_and_file_and_stdin.stderr.expected new file mode 100644 index 000000000..04a2d4be8 --- /dev/null +++ b/tests/fixtures/cat/three_directories_and_file_and_stdin.stderr.expected @@ -0,0 +1,5 @@ +cat: test_directory3/test_directory4: Is a directory +cat: file_which_does_not_exist.txt: No such file or directory (os error 2) +cat: test_directory3/test_directory5: Is a directory +cat: test_directory3/../test_directory3/test_directory5: Is a directory +cat: test_directory3: Is a directory diff --git a/tests/fixtures/chgrp/file1 b/tests/fixtures/chgrp/file1 new file mode 100644 index 000000000..73b6f48ab --- /dev/null +++ b/tests/fixtures/chgrp/file1 @@ -0,0 +1 @@ +target file 1 diff --git a/tests/fixtures/chgrp/file2 b/tests/fixtures/chgrp/file2 new file mode 100644 index 000000000..7ecd32965 --- /dev/null +++ b/tests/fixtures/chgrp/file2 @@ -0,0 +1 @@ +target file 2 diff --git a/tests/fixtures/chgrp/file3 b/tests/fixtures/chgrp/file3 new file mode 100644 index 000000000..73d293aba --- /dev/null +++ b/tests/fixtures/chgrp/file3 @@ -0,0 +1 @@ +target file 3 diff --git a/tests/fixtures/chgrp/ref_file b/tests/fixtures/chgrp/ref_file new file mode 100644 index 000000000..aba32d56e --- /dev/null +++ b/tests/fixtures/chgrp/ref_file @@ -0,0 +1 @@ +Reference file diff --git a/tests/fixtures/cksum/chars.txt b/tests/fixtures/cksum/chars.txt new file mode 100644 index 000000000..26eec03fe --- /dev/null +++ b/tests/fixtures/cksum/chars.txt @@ -0,0 +1 @@ +123456789:;<=>?@ \ No newline at end of file diff --git a/tests/fixtures/cksum/larger_than_2056_bytes.txt b/tests/fixtures/cksum/larger_than_2056_bytes.txt new file mode 100644 index 000000000..668de449a --- /dev/null +++ b/tests/fixtures/cksum/larger_than_2056_bytes.txt @@ -0,0 +1 @@ +\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\400\401\402\403\404\405\406\407\410\411\412\413\414\415\416\417\420\421\422\423\424\425\426\427\430\431\432\433\434\435\436\437\440\441\442\443\444\445\446\447\450\451\452\453\454\455\456\457\460\461\462\463\464\465\466\467\470\471\472\473\474\475\476\477\500\501\502\503\504\505\506\507\510\511\512\513\514\515\516\517\520\521\522\523\524\525\526\527\530\531\532\533\534\535\536\537\540\541\542\543\544\545\546\547\550\551\552\553\554\555\556\557\560\561\562\563\564\565\566\567\570\571\572\573\574\575\576\577\600\601\602\603\604\605\606\607\610\611\612\613\614\615\616\617\620\621\622\623\624\625\626\627\630\631\632\633\634\635\636\637\640\641\642\643\644\645\646\647\650\651\652\653\654\655\656\657\660\661\662\663\664\665\666\667\670\671\672\673\674\675\676\677\700\701\702\703\704\705\706\707\710\711\712\713\714\715\716\717\720\721\722\723\724\725\726\727\730\731\732\733\734\735\736\737\740\741\742\743\744\745\746\747\750\751\752\753\754\755\756\757\760\761\762\763\764\765\766\767\770\771\772\773\774\775\776\777\1000\1001 \ No newline at end of file diff --git a/tests/fixtures/cp/dir_with_10_files/0 b/tests/fixtures/cp/dir_with_10_files/0 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/1 b/tests/fixtures/cp/dir_with_10_files/1 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/2 b/tests/fixtures/cp/dir_with_10_files/2 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/3 b/tests/fixtures/cp/dir_with_10_files/3 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/4 b/tests/fixtures/cp/dir_with_10_files/4 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/5 b/tests/fixtures/cp/dir_with_10_files/5 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/6 b/tests/fixtures/cp/dir_with_10_files/6 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/7 b/tests/fixtures/cp/dir_with_10_files/7 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/8 b/tests/fixtures/cp/dir_with_10_files/8 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_10_files/9 b/tests/fixtures/cp/dir_with_10_files/9 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_mount/copy_me.txt b/tests/fixtures/cp/dir_with_mount/copy_me.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/cp/dir_with_mount/copy_me/copy_me.txt b/tests/fixtures/cp/dir_with_mount/copy_me/copy_me.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/du/empty.txt b/tests/fixtures/du/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/du/subdir/deeper/deeper_dir/deeper_words.txt b/tests/fixtures/du/subdir/deeper/deeper_dir/deeper_words.txt new file mode 100644 index 000000000..a04238969 --- /dev/null +++ b/tests/fixtures/du/subdir/deeper/deeper_dir/deeper_words.txt @@ -0,0 +1 @@ +hello world! diff --git a/tests/fixtures/fold/space_separated_words.txt b/tests/fixtures/fold/space_separated_words.txt new file mode 100644 index 000000000..13b980632 --- /dev/null +++ b/tests/fixtures/fold/space_separated_words.txt @@ -0,0 +1 @@ +test1 test2 test3 test4 test5 test6 \ No newline at end of file diff --git a/tests/fixtures/fold/tab_stops.input b/tests/fixtures/fold/tab_stops.input new file mode 100644 index 000000000..a96a378ea --- /dev/null +++ b/tests/fixtures/fold/tab_stops.input @@ -0,0 +1,11 @@ +1 +12 +123 +1234 +12345 +123456 +1234567 +12345678 +123456781 +12345678 2 +12345678 2 4 diff --git a/tests/fixtures/fold/tab_stops_w16.expected b/tests/fixtures/fold/tab_stops_w16.expected new file mode 100644 index 000000000..8122ac16d --- /dev/null +++ b/tests/fixtures/fold/tab_stops_w16.expected @@ -0,0 +1,13 @@ +1 +12 +123 +1234 +12345 +123456 +1234567 +12345678 +123456781 +12345678 +2 +12345678 +2 4 diff --git a/tests/fixtures/fold/tab_stops_w8.expected b/tests/fixtures/fold/tab_stops_w8.expected new file mode 100644 index 000000000..3173a4a22 --- /dev/null +++ b/tests/fixtures/fold/tab_stops_w8.expected @@ -0,0 +1,18 @@ +1 +12 +123 +1234 +12345 +123456 +1234567 +12345678 + +12345678 +1 +12345678 + +2 +12345678 + +2 +4 diff --git a/tests/fixtures/head/emptyfile.txt b/tests/fixtures/head/emptyfile.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/head/lorem_ipsum_backwards_file.expected b/tests/fixtures/head/lorem_ipsum_backwards_file.expected new file mode 100644 index 000000000..fcf432187 --- /dev/null +++ b/tests/fixtures/head/lorem_ipsum_backwards_file.expected @@ -0,0 +1,24 @@ +Lorem ipsum dolor sit amet, +consectetur adipiscing elit. +Nunc interdum suscipit sem vel ornare. +Proin euismod, +justo sed mollis dictum, +eros urna ultricies augue, +eu pharetra mi ex id ante. +Duis convallis porttitor aliquam. +Nunc vitae tincidunt ex. +Suspendisse iaculis ligula ac diam consectetur lacinia. +Donec vel velit dui. +Etiam fringilla, +dolor quis tempor vehicula, +lacus turpis bibendum velit, +et pellentesque elit odio a magna. +Cras vulputate tortor non libero vehicula euismod. +Aliquam tincidunt nisl eget enim cursus, +viverra sagittis magna commodo. +Cras rhoncus egestas leo nec blandit. +Suspendisse potenti. +Etiam ullamcorper leo vel lacus vestibulum, +cursus semper eros efficitur. +In hac habitasse platea dictumst. +Phasellus scelerisque vehicula f \ No newline at end of file diff --git a/tests/fixtures/head/sequence b/tests/fixtures/head/sequence new file mode 100644 index 000000000..190423f88 --- /dev/null +++ b/tests/fixtures/head/sequence @@ -0,0 +1,100 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 diff --git a/tests/fixtures/head/sequence.expected b/tests/fixtures/head/sequence.expected new file mode 100644 index 000000000..17d2a1390 --- /dev/null +++ b/tests/fixtures/head/sequence.expected @@ -0,0 +1,90 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 diff --git a/tests/fixtures/head/zero_terminated.expected b/tests/fixtures/head/zero_terminated.expected new file mode 100644 index 000000000..3c4cc058c Binary files /dev/null and b/tests/fixtures/head/zero_terminated.expected differ diff --git a/tests/fixtures/head/zero_terminated.txt b/tests/fixtures/head/zero_terminated.txt new file mode 100644 index 000000000..6c7968122 Binary files /dev/null and b/tests/fixtures/head/zero_terminated.txt differ diff --git a/tests/fixtures/install/helloworld.rs b/tests/fixtures/install/helloworld.rs new file mode 100644 index 000000000..47ad8c634 --- /dev/null +++ b/tests/fixtures/install/helloworld.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello World!"); +} diff --git a/tests/fixtures/install/helloworld_linux b/tests/fixtures/install/helloworld_linux new file mode 100755 index 000000000..c1c6b9b37 Binary files /dev/null and b/tests/fixtures/install/helloworld_linux differ diff --git a/tests/fixtures/install/helloworld_macos b/tests/fixtures/install/helloworld_macos new file mode 100755 index 000000000..40e6ee515 Binary files /dev/null and b/tests/fixtures/install/helloworld_macos differ diff --git a/tests/fixtures/pr/0F b/tests/fixtures/pr/0F new file mode 100644 index 000000000..223765391 --- /dev/null +++ b/tests/fixtures/pr/0F @@ -0,0 +1,330 @@ + + +{last_modified_time} {file_name} Page 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 2 + + +1 FF-Test: FF's at Start of File V +2 Options -b -3 / -a -3 / ... +3 -------------------------------------------- +4 3456789 123456789 123456789 123456789 12345678 +5 3 Columns downwards ..., <= 5 lines per page +6 FF-Arangements: Empty Pages at start +7 \ftext; \f\ntext; +8 \f\ftext; \f\f\ntext; \f\n\ftext; \f\n\f\n; +9 3456789 123456789 123456789 +10 zzzzzzzzzzzzzzzzzzzzzzzzzz123456789 +1 12345678 +2 12345678 +3 line truncation before FF; r_r_o_l-test: +14 456789 123456789 123456789 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 4 + + +15 xyzxyzxyz XYZXYZXYZ abcabcab +16 456789 123456789 xyzxyzxyz XYZXYZXYZ +7 12345678 +8 12345678 +9 3456789 ab +20 DEFGHI 123 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +5 12345678 +6 12345678 +27 no truncation before FF; (r_l-test): +28 no trunc + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 5 + + +29 xyzxyzxyz XYZXYZXYZ abcabcab +30 456789 123456789 xyzxyzxyz XYZXYZXYZ +1 12345678 +2 3456789 abcdefghi +3 12345678 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/fixtures/pr/0Fnt b/tests/fixtures/pr/0Fnt new file mode 100644 index 000000000..9ba3a906c --- /dev/null +++ b/tests/fixtures/pr/0Fnt @@ -0,0 +1,36 @@ + +1 FF-Test: FF's at Start of File V +2 Options -b -3 / -a -3 / ... +3 -------------------------------------------- +4 3456789 123456789 123456789 123456789 12345678 +5 3 Columns downwards ..., <= 5 lines per page +6 FF-Arangements: Empty Pages at start +7 \ftext; \f\ntext; +8 \f\ftext; \f\f\ntext; \f\n\ftext; \f\n\f\n; +9 3456789 123456789 123456789 +10 zzzzzzzzzzzzzzzzzzzzzzzzzz123456789 +1 12345678 +2 12345678 +3 line truncation before FF; r_r_o_l-test: +14 456789 123456789 123456789 123456789 + +15 xyzxyzxyz XYZXYZXYZ abcabcab +16 456789 123456789 xyzxyzxyz XYZXYZXYZ +7 12345678 +8 12345678 +9 3456789 ab +20 DEFGHI 123 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +5 12345678 +6 12345678 +27 no truncation before FF; (r_l-test): +28 no trunc + +29 xyzxyzxyz XYZXYZXYZ abcabcab +30 456789 123456789 xyzxyzxyz XYZXYZXYZ +1 12345678 +2 3456789 abcdefghi +3 12345678 diff --git a/tests/fixtures/pr/0Ft b/tests/fixtures/pr/0Ft new file mode 100644 index 000000000..bdd599d47 --- /dev/null +++ b/tests/fixtures/pr/0Ft @@ -0,0 +1,35 @@ + 1 FF-Test: FF's at Start of File V +2 Options -b -3 / -a -3 / ... +3 -------------------------------------------- +4 3456789 123456789 123456789 123456789 12345678 +5 3 Columns downwards ..., <= 5 lines per page +6 FF-Arangements: Empty Pages at start +7 \ftext; \f\ntext; +8 \f\ftext; \f\f\ntext; \f\n\ftext; \f\n\f\n; +9 3456789 123456789 123456789 +10 zzzzzzzzzzzzzzzzzzzzzzzzzz123456789 +1 12345678 +2 12345678 +3 line truncation before FF; r_r_o_l-test: +14 456789 123456789 123456789 123456789 + +15 xyzxyzxyz XYZXYZXYZ abcabcab +16 456789 123456789 xyzxyzxyz XYZXYZXYZ +7 12345678 +8 12345678 +9 3456789 ab +20 DEFGHI 123 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +5 12345678 +6 12345678 +27 no truncation before FF; (r_l-test): +28 no trunc + +29 xyzxyzxyz XYZXYZXYZ abcabcab +30 456789 123456789 xyzxyzxyz XYZXYZXYZ +1 12345678 +2 3456789 abcdefghi +3 12345678 diff --git a/tests/fixtures/pr/3-0F b/tests/fixtures/pr/3-0F new file mode 100644 index 000000000..25a9db171 --- /dev/null +++ b/tests/fixtures/pr/3-0F @@ -0,0 +1,198 @@ + + +{last_modified_time} {file_name} Page 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 4 + + +15 xyzxyzxyz XYZXYZXYZ abcabcab +16 456789 123456789 xyzxyzxyz XYZXYZXYZ +7 12345678 +8 12345678 +9 3456789 ab +20 DEFGHI 123 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +5 12345678 +6 12345678 +27 no truncation before FF; (r_l-test): +28 no trunc + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 5 + + +29 xyzxyzxyz XYZXYZXYZ abcabcab +30 456789 123456789 xyzxyzxyz XYZXYZXYZ +1 12345678 +2 3456789 abcdefghi +3 12345678 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/fixtures/pr/3a3f-0F b/tests/fixtures/pr/3a3f-0F new file mode 100644 index 000000000..6097374c7 --- /dev/null +++ b/tests/fixtures/pr/3a3f-0F @@ -0,0 +1,22 @@ + + +{last_modified_time} {file_name} Page 3 + + + + +{last_modified_time} {file_name} Page 4 + + +15 xyzxyzxyz XYZXYZXYZ 16 456789 123456789 xyz 7 +8 9 3456789 ab 20 DEFGHI 123 +1 2 3 +4 5 6 +27 no truncation before 28 no trunc + + +{last_modified_time} {file_name} Page 5 + + +29 xyzxyzxyz XYZXYZXYZ 30 456789 123456789 xyz 1 +2 3456789 abcdefghi 3 \ No newline at end of file diff --git a/tests/fixtures/pr/3f-0F b/tests/fixtures/pr/3f-0F new file mode 100644 index 000000000..d32c1f8f6 --- /dev/null +++ b/tests/fixtures/pr/3f-0F @@ -0,0 +1,36 @@ + + +{last_modified_time} {file_name} Page 3 + + + + +{last_modified_time} {file_name} Page 4 + + +15 xyzxyzxyz XYZXYZXYZ abcabcab +16 456789 123456789 xyzxyzxyz XYZXYZXYZ +7 12345678 +8 12345678 +9 3456789 ab +20 DEFGHI 123 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +5 12345678 +6 12345678 +27 no truncation before FF; (r_l-test): +28 no trunc + + + +{last_modified_time} {file_name} Page 5 + + +29 xyzxyzxyz XYZXYZXYZ abcabcab +30 456789 123456789 xyzxyzxyz XYZXYZXYZ +1 12345678 +2 3456789 abcdefghi +3 12345678 + \ No newline at end of file diff --git a/tests/fixtures/pr/FnFn b/tests/fixtures/pr/FnFn new file mode 100644 index 000000000..fa91abafc --- /dev/null +++ b/tests/fixtures/pr/FnFn @@ -0,0 +1,68 @@ +1 FF-Test: FF's in Text V +2 Options -b -3 / -a -3 / ... +3 -------------------------------------------- +4 3456789 123456789 123456789 123456789 12345678 +5 3 Columns downwards ..., <= 5 lines per page +6 FF-Arangements: One Empty Page +7 text\f\f\n; text\f\n\ftext; \f\ftext; +8 \f\f\n; \f\n\f\n; +9 +10 zzzzzzzzzzzzzzzzzzzzzzzzzz123456789 +1 12345678 +2 12345678 +3 line truncation before FF; r_r_o_l-test: +14 456789 123456789 123456789 123456789 + + +15 xyzxyzxyz XYZXYZXYZ abcabcab +16 456789 123456789 xyzxyzxyz XYZXYZXYZ +7 12345678 +8 12345678 +9 3456789 ab +20 DEFGHI 123 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +5 12345678 +6 12345678 +27 no truncation before FF; (r_l-test): +28 no trunc + + +29 xyzxyzxyz XYZXYZXYZ abcabcab +30 456789 123456789 xyzxyzxyz XYZXYZXYZ +1 12345678 +2 3456789 abcdefghi +3 12345678 +4 12345678 +5 12345678 +6 12345678 +7 12345678 +8 12345678 +9 3456789 abcdefghi +40 DEFGHI 123456789 +41 yzxyzxyz XYZXYZXYZ abcabcab +42 456789 123456789 abcdefghi ABCDEDFHI + + + +43 xyzxyzxyz XYZXYZXYZ abcabcab +44 456789 123456789 xyzxyzxyz XYZXYZXYZ +5 12345678 +6 12345678 +7 12345678 +8 12345678 +9 12345678 +50 12345678 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +55 yzxyzxyz XYZXYZXYZ abcabcab +56 456789 123456789 abcdefghi ABCDEDFHI + +57 xyzxyzxyz XYZXYZXYZ abcabcab +58 456789 123456789 xyzxyzxyz XYZXYZXYZ +9 12345678 +60 DEFGHI 123456789 diff --git a/tests/fixtures/pr/W20l24f-ll b/tests/fixtures/pr/W20l24f-ll new file mode 100644 index 000000000..d95c04c37 --- /dev/null +++ b/tests/fixtures/pr/W20l24f-ll @@ -0,0 +1,106 @@ + + +{last_modified_time} {file_name} Page 1 + + +1<<< -Test: FF's in +2<<< -b -3 / -a -3 +3<<< >>> +4<<< 123456789 1234 + +6<<< -Arangements: +7<<< \f\f\n; text\f +8<<< f\f\n; \f\n\f\ +9<<< >>> +10<<< >>> +1<<< >>> +2<<< >>> +3<<< truncation bef +14<<< 123456789 123 + + +{last_modified_time} {file_name} Page 2 + + + + +{last_modified_time} {file_name} Page 3 + + +15<<< xyzxyzxyz XYZ +16<<< 123456789 xyz +7<<< >>> +8<<< >>> +9<<< >>> +20<<< >>> +1<<< >>> + + +4<<< >>> +5<<< >>> +6<<< >>> +27<<< truncation be +28<<< trunc + + +{last_modified_time} {file_name} Page 4 + + + + +{last_modified_time} {file_name} Page 5 + + +29<<>> +2<<< abcdefghi >>> +3<<< >>> +4<<< >>> +5<<< >>> +6<<< >>> +7<<< >>> +8<<< >>> +9<<< abcdefghi >>> +40<<< 123456789 >> +41<<< XYZXYZXYZ abc +42<<< 123456789 abc + + +{last_modified_time} {file_name} Page 6 + + + + +{last_modified_time} {file_name} Page 7 + + + + +{last_modified_time} {file_name} Page 8 + + +43<<< xyzxyzxyz XYZ +44<<< 123456789 xyz +5<<< >>> +6<<< >>> +7<<< >>> +8<<< >>> +9<<< >>> +50<<< >>> +1<<< >>> +2<<< >>> +3<<< >>> +4<<< >>> +55<<< XYZXYZXYZ abc +56<<< 123456789 abc + + +{last_modified_time} {file_name} Page 9 + + +57<<< xyzxyzxyz XYZ +58<<< 123456789 xyz +9<<< >>> +60<<< 123456789 >> + \ No newline at end of file diff --git a/tests/fixtures/pr/a3-0F b/tests/fixtures/pr/a3-0F new file mode 100644 index 000000000..58aeb07c2 --- /dev/null +++ b/tests/fixtures/pr/a3-0F @@ -0,0 +1,330 @@ + + +{last_modified_time} {file_name} Page 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 2 + + +1 FF-Test: FF's at St 2 Options -b -3 / -a 3 ------------------- +4 3456789 123456789 123 5 3 Columns downwards 6 FF-Arangements: Emp +7 \ftext; \f\ntext; 8 \f\ftext; \f\f\ntex 9 3456789 123456789 123 +10 zzzzzzzzzzzzzzzzzzz 1 2 +3 line truncation befor 14 456789 123456789 123 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 4 + + +15 xyzxyzxyz XYZXYZXYZ 16 456789 123456789 xyz 7 +8 9 3456789 ab 20 DEFGHI 123 +1 2 3 +4 5 6 +27 no truncation before 28 no trunc + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 5 + + +29 xyzxyzxyz XYZXYZXYZ 30 456789 123456789 xyz 1 +2 3456789 abcdefghi 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/fixtures/pr/a3f-0F b/tests/fixtures/pr/a3f-0F new file mode 100644 index 000000000..24939c004 --- /dev/null +++ b/tests/fixtures/pr/a3f-0F @@ -0,0 +1,37 @@ + + +{last_modified_time} {file_name} Page 1 + + + + +{last_modified_time} {file_name} Page 2 + + +1 FF-Test: FF's at St 2 Options -b -3 / -a 3 ------------------- +4 3456789 123456789 123 5 3 Columns downwards 6 FF-Arangements: Emp +7 \ftext; \f\ntext; 8 \f\ftext; \f\f\ntex 9 3456789 123456789 123 +10 zzzzzzzzzzzzzzzzzzz 1 2 +3 line truncation befor 14 456789 123456789 123 + + +{last_modified_time} {file_name} Page 3 + + + + +{last_modified_time} {file_name} Page 4 + + +15 xyzxyzxyz XYZXYZXYZ 16 456789 123456789 xyz 7 +8 9 3456789 ab 20 DEFGHI 123 +1 2 3 +4 5 6 +27 no truncation before 28 no trunc + + +{last_modified_time} {file_name} Page 5 + + +29 xyzxyzxyz XYZXYZXYZ 30 456789 123456789 xyz 1 +2 3456789 abcdefghi 3 \ No newline at end of file diff --git a/tests/fixtures/pr/column.log b/tests/fixtures/pr/column.log new file mode 100644 index 000000000..7972c09aa --- /dev/null +++ b/tests/fixtures/pr/column.log @@ -0,0 +1,2000 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103 +104 +105 +106 +107 +108 +109 +110 +111 +112 +113 +114 +115 +116 +117 +118 +119 +120 +121 +122 +123 +124 +125 +126 +127 +128 +129 +130 +131 +132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159 +160 +161 +162 +163 +164 +165 +166 +167 +168 +169 +170 +171 +172 +173 +174 +175 +176 +177 +178 +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260 +261 +262 +263 +264 +265 +266 +267 +268 +269 +270 +271 +272 +273 +274 +275 +276 +277 +278 +279 +280 +281 +282 +283 +284 +285 +286 +287 +288 +289 +290 +291 +292 +293 +294 +295 +296 +297 +298 +299 +300 +301 +302 +303 +304 +305 +306 +307 +308 +309 +310 +311 +312 +313 +314 +315 +316 +317 +318 +319 +320 +321 +322 +323 +324 +325 +326 +327 +328 +329 +330 +331 +332 +333 +334 +335 +336 +337 +338 +339 +340 +341 +342 +343 +344 +345 +346 +347 +348 +349 +350 +351 +352 +353 +354 +355 +356 +357 +358 +359 +360 +361 +362 +363 +364 +365 +366 +367 +368 +369 +370 +371 +372 +373 +374 +375 +376 +377 +378 +379 +380 +381 +382 +383 +384 +385 +386 +387 +388 +389 +390 +391 +392 +393 +394 +395 +396 +397 +398 +399 +400 +401 +402 +403 +404 +405 +406 +407 +408 +409 +410 +411 +412 +413 +414 +415 +416 +417 +418 +419 +420 +421 +422 +423 +424 +425 +426 +427 +428 +429 +430 +431 +432 +433 +434 +435 +436 +437 +438 +439 +440 +441 +442 +443 +444 +445 +446 +447 +448 +449 +450 +451 +452 +453 +454 +455 +456 +457 +458 +459 +460 +461 +462 +463 +464 +465 +466 +467 +468 +469 +470 +471 +472 +473 +474 +475 +476 +477 +478 +479 +480 +481 +482 +483 +484 +485 +486 +487 +488 +489 +490 +491 +492 +493 +494 +495 +496 +497 +498 +499 +500 +501 +502 +503 +504 +505 +506 +507 +508 +509 +510 +511 +512 +513 +514 +515 +516 +517 +518 +519 +520 +521 +522 +523 +524 +525 +526 +527 +528 +529 +530 +531 +532 +533 +534 +535 +536 +537 +538 +539 +540 +541 +542 +543 +544 +545 +546 +547 +548 +549 +550 +551 +552 +553 +554 +555 +556 +557 +558 +559 +560 +561 +562 +563 +564 +565 +566 +567 +568 +569 +570 +571 +572 +573 +574 +575 +576 +577 +578 +579 +580 +581 +582 +583 +584 +585 +586 +587 +588 +589 +590 +591 +592 +593 +594 +595 +596 +597 +598 +599 +600 +601 +602 +603 +604 +605 +606 +607 +608 +609 +610 +611 +612 +613 +614 +615 +616 +617 +618 +619 +620 +621 +622 +623 +624 +625 +626 +627 +628 +629 +630 +631 +632 +633 +634 +635 +636 +637 +638 +639 +640 +641 +642 +643 +644 +645 +646 +647 +648 +649 +650 +651 +652 +653 +654 +655 +656 +657 +658 +659 +660 +661 +662 +663 +664 +665 +666 +667 +668 +669 +670 +671 +672 +673 +674 +675 +676 +677 +678 +679 +680 +681 +682 +683 +684 +685 +686 +687 +688 +689 +690 +691 +692 +693 +694 +695 +696 +697 +698 +699 +700 +701 +702 +703 +704 +705 +706 +707 +708 +709 +710 +711 +712 +713 +714 +715 +716 +717 +718 +719 +720 +721 +722 +723 +724 +725 +726 +727 +728 +729 +730 +731 +732 +733 +734 +735 +736 +737 +738 +739 +740 +741 +742 +743 +744 +745 +746 +747 +748 +749 +750 +751 +752 +753 +754 +755 +756 +757 +758 +759 +760 +761 +762 +763 +764 +765 +766 +767 +768 +769 +770 +771 +772 +773 +774 +775 +776 +777 +778 +779 +780 +781 +782 +783 +784 +785 +786 +787 +788 +789 +790 +791 +792 +793 +794 +795 +796 +797 +798 +799 +800 +801 +802 +803 +804 +805 +806 +807 +808 +809 +810 +811 +812 +813 +814 +815 +816 +817 +818 +819 +820 +821 +822 +823 +824 +825 +826 +827 +828 +829 +830 +831 +832 +833 +834 +835 +836 +837 +838 +839 +840 +841 +842 +843 +844 +845 +846 +847 +848 +849 +850 +851 +852 +853 +854 +855 +856 +857 +858 +859 +860 +861 +862 +863 +864 +865 +866 +867 +868 +869 +870 +871 +872 +873 +874 +875 +876 +877 +878 +879 +880 +881 +882 +883 +884 +885 +886 +887 +888 +889 +890 +891 +892 +893 +894 +895 +896 +897 +898 +899 +900 +901 +902 +903 +904 +905 +906 +907 +908 +909 +910 +911 +912 +913 +914 +915 +916 +917 +918 +919 +920 +921 +922 +923 +924 +925 +926 +927 +928 +929 +930 +931 +932 +933 +934 +935 +936 +937 +938 +939 +940 +941 +942 +943 +944 +945 +946 +947 +948 +949 +950 +951 +952 +953 +954 +955 +956 +957 +958 +959 +960 +961 +962 +963 +964 +965 +966 +967 +968 +969 +970 +971 +972 +973 +974 +975 +976 +977 +978 +979 +980 +981 +982 +983 +984 +985 +986 +987 +988 +989 +990 +991 +992 +993 +994 +995 +996 +997 +998 +999 +1000 +1001 +1002 +1003 +1004 +1005 +1006 +1007 +1008 +1009 +1010 +1011 +1012 +1013 +1014 +1015 +1016 +1017 +1018 +1019 +1020 +1021 +1022 +1023 +1024 +1025 +1026 +1027 +1028 +1029 +1030 +1031 +1032 +1033 +1034 +1035 +1036 +1037 +1038 +1039 +1040 +1041 +1042 +1043 +1044 +1045 +1046 +1047 +1048 +1049 +1050 +1051 +1052 +1053 +1054 +1055 +1056 +1057 +1058 +1059 +1060 +1061 +1062 +1063 +1064 +1065 +1066 +1067 +1068 +1069 +1070 +1071 +1072 +1073 +1074 +1075 +1076 +1077 +1078 +1079 +1080 +1081 +1082 +1083 +1084 +1085 +1086 +1087 +1088 +1089 +1090 +1091 +1092 +1093 +1094 +1095 +1096 +1097 +1098 +1099 +1100 +1101 +1102 +1103 +1104 +1105 +1106 +1107 +1108 +1109 +1110 +1111 +1112 +1113 +1114 +1115 +1116 +1117 +1118 +1119 +1120 +1121 +1122 +1123 +1124 +1125 +1126 +1127 +1128 +1129 +1130 +1131 +1132 +1133 +1134 +1135 +1136 +1137 +1138 +1139 +1140 +1141 +1142 +1143 +1144 +1145 +1146 +1147 +1148 +1149 +1150 +1151 +1152 +1153 +1154 +1155 +1156 +1157 +1158 +1159 +1160 +1161 +1162 +1163 +1164 +1165 +1166 +1167 +1168 +1169 +1170 +1171 +1172 +1173 +1174 +1175 +1176 +1177 +1178 +1179 +1180 +1181 +1182 +1183 +1184 +1185 +1186 +1187 +1188 +1189 +1190 +1191 +1192 +1193 +1194 +1195 +1196 +1197 +1198 +1199 +1200 +1201 +1202 +1203 +1204 +1205 +1206 +1207 +1208 +1209 +1210 +1211 +1212 +1213 +1214 +1215 +1216 +1217 +1218 +1219 +1220 +1221 +1222 +1223 +1224 +1225 +1226 +1227 +1228 +1229 +1230 +1231 +1232 +1233 +1234 +1235 +1236 +1237 +1238 +1239 +1240 +1241 +1242 +1243 +1244 +1245 +1246 +1247 +1248 +1249 +1250 +1251 +1252 +1253 +1254 +1255 +1256 +1257 +1258 +1259 +1260 +1261 +1262 +1263 +1264 +1265 +1266 +1267 +1268 +1269 +1270 +1271 +1272 +1273 +1274 +1275 +1276 +1277 +1278 +1279 +1280 +1281 +1282 +1283 +1284 +1285 +1286 +1287 +1288 +1289 +1290 +1291 +1292 +1293 +1294 +1295 +1296 +1297 +1298 +1299 +1300 +1301 +1302 +1303 +1304 +1305 +1306 +1307 +1308 +1309 +1310 +1311 +1312 +1313 +1314 +1315 +1316 +1317 +1318 +1319 +1320 +1321 +1322 +1323 +1324 +1325 +1326 +1327 +1328 +1329 +1330 +1331 +1332 +1333 +1334 +1335 +1336 +1337 +1338 +1339 +1340 +1341 +1342 +1343 +1344 +1345 +1346 +1347 +1348 +1349 +1350 +1351 +1352 +1353 +1354 +1355 +1356 +1357 +1358 +1359 +1360 +1361 +1362 +1363 +1364 +1365 +1366 +1367 +1368 +1369 +1370 +1371 +1372 +1373 +1374 +1375 +1376 +1377 +1378 +1379 +1380 +1381 +1382 +1383 +1384 +1385 +1386 +1387 +1388 +1389 +1390 +1391 +1392 +1393 +1394 +1395 +1396 +1397 +1398 +1399 +1400 +1401 +1402 +1403 +1404 +1405 +1406 +1407 +1408 +1409 +1410 +1411 +1412 +1413 +1414 +1415 +1416 +1417 +1418 +1419 +1420 +1421 +1422 +1423 +1424 +1425 +1426 +1427 +1428 +1429 +1430 +1431 +1432 +1433 +1434 +1435 +1436 +1437 +1438 +1439 +1440 +1441 +1442 +1443 +1444 +1445 +1446 +1447 +1448 +1449 +1450 +1451 +1452 +1453 +1454 +1455 +1456 +1457 +1458 +1459 +1460 +1461 +1462 +1463 +1464 +1465 +1466 +1467 +1468 +1469 +1470 +1471 +1472 +1473 +1474 +1475 +1476 +1477 +1478 +1479 +1480 +1481 +1482 +1483 +1484 +1485 +1486 +1487 +1488 +1489 +1490 +1491 +1492 +1493 +1494 +1495 +1496 +1497 +1498 +1499 +1500 +1501 +1502 +1503 +1504 +1505 +1506 +1507 +1508 +1509 +1510 +1511 +1512 +1513 +1514 +1515 +1516 +1517 +1518 +1519 +1520 +1521 +1522 +1523 +1524 +1525 +1526 +1527 +1528 +1529 +1530 +1531 +1532 +1533 +1534 +1535 +1536 +1537 +1538 +1539 +1540 +1541 +1542 +1543 +1544 +1545 +1546 +1547 +1548 +1549 +1550 +1551 +1552 +1553 +1554 +1555 +1556 +1557 +1558 +1559 +1560 +1561 +1562 +1563 +1564 +1565 +1566 +1567 +1568 +1569 +1570 +1571 +1572 +1573 +1574 +1575 +1576 +1577 +1578 +1579 +1580 +1581 +1582 +1583 +1584 +1585 +1586 +1587 +1588 +1589 +1590 +1591 +1592 +1593 +1594 +1595 +1596 +1597 +1598 +1599 +1600 +1601 +1602 +1603 +1604 +1605 +1606 +1607 +1608 +1609 +1610 +1611 +1612 +1613 +1614 +1615 +1616 +1617 +1618 +1619 +1620 +1621 +1622 +1623 +1624 +1625 +1626 +1627 +1628 +1629 +1630 +1631 +1632 +1633 +1634 +1635 +1636 +1637 +1638 +1639 +1640 +1641 +1642 +1643 +1644 +1645 +1646 +1647 +1648 +1649 +1650 +1651 +1652 +1653 +1654 +1655 +1656 +1657 +1658 +1659 +1660 +1661 +1662 +1663 +1664 +1665 +1666 +1667 +1668 +1669 +1670 +1671 +1672 +1673 +1674 +1675 +1676 +1677 +1678 +1679 +1680 +1681 +1682 +1683 +1684 +1685 +1686 +1687 +1688 +1689 +1690 +1691 +1692 +1693 +1694 +1695 +1696 +1697 +1698 +1699 +1700 +1701 +1702 +1703 +1704 +1705 +1706 +1707 +1708 +1709 +1710 +1711 +1712 +1713 +1714 +1715 +1716 +1717 +1718 +1719 +1720 +1721 +1722 +1723 +1724 +1725 +1726 +1727 +1728 +1729 +1730 +1731 +1732 +1733 +1734 +1735 +1736 +1737 +1738 +1739 +1740 +1741 +1742 +1743 +1744 +1745 +1746 +1747 +1748 +1749 +1750 +1751 +1752 +1753 +1754 +1755 +1756 +1757 +1758 +1759 +1760 +1761 +1762 +1763 +1764 +1765 +1766 +1767 +1768 +1769 +1770 +1771 +1772 +1773 +1774 +1775 +1776 +1777 +1778 +1779 +1780 +1781 +1782 +1783 +1784 +1785 +1786 +1787 +1788 +1789 +1790 +1791 +1792 +1793 +1794 +1795 +1796 +1797 +1798 +1799 +1800 +1801 +1802 +1803 +1804 +1805 +1806 +1807 +1808 +1809 +1810 +1811 +1812 +1813 +1814 +1815 +1816 +1817 +1818 +1819 +1820 +1821 +1822 +1823 +1824 +1825 +1826 +1827 +1828 +1829 +1830 +1831 +1832 +1833 +1834 +1835 +1836 +1837 +1838 +1839 +1840 +1841 +1842 +1843 +1844 +1845 +1846 +1847 +1848 +1849 +1850 +1851 +1852 +1853 +1854 +1855 +1856 +1857 +1858 +1859 +1860 +1861 +1862 +1863 +1864 +1865 +1866 +1867 +1868 +1869 +1870 +1871 +1872 +1873 +1874 +1875 +1876 +1877 +1878 +1879 +1880 +1881 +1882 +1883 +1884 +1885 +1886 +1887 +1888 +1889 +1890 +1891 +1892 +1893 +1894 +1895 +1896 +1897 +1898 +1899 +1900 +1901 +1902 +1903 +1904 +1905 +1906 +1907 +1908 +1909 +1910 +1911 +1912 +1913 +1914 +1915 +1916 +1917 +1918 +1919 +1920 +1921 +1922 +1923 +1924 +1925 +1926 +1927 +1928 +1929 +1930 +1931 +1932 +1933 +1934 +1935 +1936 +1937 +1938 +1939 +1940 +1941 +1942 +1943 +1944 +1945 +1946 +1947 +1948 +1949 +1950 +1951 +1952 +1953 +1954 +1955 +1956 +1957 +1958 +1959 +1960 +1961 +1962 +1963 +1964 +1965 +1966 +1967 +1968 +1969 +1970 +1971 +1972 +1973 +1974 +1975 +1976 +1977 +1978 +1979 +1980 +1981 +1982 +1983 +1984 +1985 +1986 +1987 +1988 +1989 +1990 +1991 +1992 +1993 +1994 +1995 +1996 +1997 +1998 +1999 +2000 diff --git a/tests/fixtures/pr/column.log.expected b/tests/fixtures/pr/column.log.expected new file mode 100644 index 000000000..e548d4128 --- /dev/null +++ b/tests/fixtures/pr/column.log.expected @@ -0,0 +1,198 @@ + + +{last_modified_time} column.log Page 3 + + + 337 337 393 393 449 449 + 338 338 394 394 450 450 + 339 339 395 395 451 451 + 340 340 396 396 452 452 + 341 341 397 397 453 453 + 342 342 398 398 454 454 + 343 343 399 399 455 455 + 344 344 400 400 456 456 + 345 345 401 401 457 457 + 346 346 402 402 458 458 + 347 347 403 403 459 459 + 348 348 404 404 460 460 + 349 349 405 405 461 461 + 350 350 406 406 462 462 + 351 351 407 407 463 463 + 352 352 408 408 464 464 + 353 353 409 409 465 465 + 354 354 410 410 466 466 + 355 355 411 411 467 467 + 356 356 412 412 468 468 + 357 357 413 413 469 469 + 358 358 414 414 470 470 + 359 359 415 415 471 471 + 360 360 416 416 472 472 + 361 361 417 417 473 473 + 362 362 418 418 474 474 + 363 363 419 419 475 475 + 364 364 420 420 476 476 + 365 365 421 421 477 477 + 366 366 422 422 478 478 + 367 367 423 423 479 479 + 368 368 424 424 480 480 + 369 369 425 425 481 481 + 370 370 426 426 482 482 + 371 371 427 427 483 483 + 372 372 428 428 484 484 + 373 373 429 429 485 485 + 374 374 430 430 486 486 + 375 375 431 431 487 487 + 376 376 432 432 488 488 + 377 377 433 433 489 489 + 378 378 434 434 490 490 + 379 379 435 435 491 491 + 380 380 436 436 492 492 + 381 381 437 437 493 493 + 382 382 438 438 494 494 + 383 383 439 439 495 495 + 384 384 440 440 496 496 + 385 385 441 441 497 497 + 386 386 442 442 498 498 + 387 387 443 443 499 499 + 388 388 444 444 500 500 + 389 389 445 445 501 501 + 390 390 446 446 502 502 + 391 391 447 447 503 503 + 392 392 448 448 504 504 + + + + + + + +{last_modified_time} column.log Page 4 + + + 505 505 561 561 617 617 + 506 506 562 562 618 618 + 507 507 563 563 619 619 + 508 508 564 564 620 620 + 509 509 565 565 621 621 + 510 510 566 566 622 622 + 511 511 567 567 623 623 + 512 512 568 568 624 624 + 513 513 569 569 625 625 + 514 514 570 570 626 626 + 515 515 571 571 627 627 + 516 516 572 572 628 628 + 517 517 573 573 629 629 + 518 518 574 574 630 630 + 519 519 575 575 631 631 + 520 520 576 576 632 632 + 521 521 577 577 633 633 + 522 522 578 578 634 634 + 523 523 579 579 635 635 + 524 524 580 580 636 636 + 525 525 581 581 637 637 + 526 526 582 582 638 638 + 527 527 583 583 639 639 + 528 528 584 584 640 640 + 529 529 585 585 641 641 + 530 530 586 586 642 642 + 531 531 587 587 643 643 + 532 532 588 588 644 644 + 533 533 589 589 645 645 + 534 534 590 590 646 646 + 535 535 591 591 647 647 + 536 536 592 592 648 648 + 537 537 593 593 649 649 + 538 538 594 594 650 650 + 539 539 595 595 651 651 + 540 540 596 596 652 652 + 541 541 597 597 653 653 + 542 542 598 598 654 654 + 543 543 599 599 655 655 + 544 544 600 600 656 656 + 545 545 601 601 657 657 + 546 546 602 602 658 658 + 547 547 603 603 659 659 + 548 548 604 604 660 660 + 549 549 605 605 661 661 + 550 550 606 606 662 662 + 551 551 607 607 663 663 + 552 552 608 608 664 664 + 553 553 609 609 665 665 + 554 554 610 610 666 666 + 555 555 611 611 667 667 + 556 556 612 612 668 668 + 557 557 613 613 669 669 + 558 558 614 614 670 670 + 559 559 615 615 671 671 + 560 560 616 616 672 672 + + + + + + + +{last_modified_time} column.log Page 5 + + + 673 673 729 729 785 785 + 674 674 730 730 786 786 + 675 675 731 731 787 787 + 676 676 732 732 788 788 + 677 677 733 733 789 789 + 678 678 734 734 790 790 + 679 679 735 735 791 791 + 680 680 736 736 792 792 + 681 681 737 737 793 793 + 682 682 738 738 794 794 + 683 683 739 739 795 795 + 684 684 740 740 796 796 + 685 685 741 741 797 797 + 686 686 742 742 798 798 + 687 687 743 743 799 799 + 688 688 744 744 800 800 + 689 689 745 745 801 801 + 690 690 746 746 802 802 + 691 691 747 747 803 803 + 692 692 748 748 804 804 + 693 693 749 749 805 805 + 694 694 750 750 806 806 + 695 695 751 751 807 807 + 696 696 752 752 808 808 + 697 697 753 753 809 809 + 698 698 754 754 810 810 + 699 699 755 755 811 811 + 700 700 756 756 812 812 + 701 701 757 757 813 813 + 702 702 758 758 814 814 + 703 703 759 759 815 815 + 704 704 760 760 816 816 + 705 705 761 761 817 817 + 706 706 762 762 818 818 + 707 707 763 763 819 819 + 708 708 764 764 820 820 + 709 709 765 765 821 821 + 710 710 766 766 822 822 + 711 711 767 767 823 823 + 712 712 768 768 824 824 + 713 713 769 769 825 825 + 714 714 770 770 826 826 + 715 715 771 771 827 827 + 716 716 772 772 828 828 + 717 717 773 773 829 829 + 718 718 774 774 830 830 + 719 719 775 775 831 831 + 720 720 776 776 832 832 + 721 721 777 777 833 833 + 722 722 778 778 834 834 + 723 723 779 779 835 835 + 724 724 780 780 836 836 + 725 725 781 781 837 837 + 726 726 782 782 838 838 + 727 727 783 783 839 839 + 728 728 784 784 840 840 + + + + + diff --git a/tests/fixtures/pr/column_across.log.expected b/tests/fixtures/pr/column_across.log.expected new file mode 100644 index 000000000..9d5a1dc1c --- /dev/null +++ b/tests/fixtures/pr/column_across.log.expected @@ -0,0 +1,198 @@ + + +{last_modified_time} column.log Page 3 + + + 337 337 338 338 339 339 + 340 340 341 341 342 342 + 343 343 344 344 345 345 + 346 346 347 347 348 348 + 349 349 350 350 351 351 + 352 352 353 353 354 354 + 355 355 356 356 357 357 + 358 358 359 359 360 360 + 361 361 362 362 363 363 + 364 364 365 365 366 366 + 367 367 368 368 369 369 + 370 370 371 371 372 372 + 373 373 374 374 375 375 + 376 376 377 377 378 378 + 379 379 380 380 381 381 + 382 382 383 383 384 384 + 385 385 386 386 387 387 + 388 388 389 389 390 390 + 391 391 392 392 393 393 + 394 394 395 395 396 396 + 397 397 398 398 399 399 + 400 400 401 401 402 402 + 403 403 404 404 405 405 + 406 406 407 407 408 408 + 409 409 410 410 411 411 + 412 412 413 413 414 414 + 415 415 416 416 417 417 + 418 418 419 419 420 420 + 421 421 422 422 423 423 + 424 424 425 425 426 426 + 427 427 428 428 429 429 + 430 430 431 431 432 432 + 433 433 434 434 435 435 + 436 436 437 437 438 438 + 439 439 440 440 441 441 + 442 442 443 443 444 444 + 445 445 446 446 447 447 + 448 448 449 449 450 450 + 451 451 452 452 453 453 + 454 454 455 455 456 456 + 457 457 458 458 459 459 + 460 460 461 461 462 462 + 463 463 464 464 465 465 + 466 466 467 467 468 468 + 469 469 470 470 471 471 + 472 472 473 473 474 474 + 475 475 476 476 477 477 + 478 478 479 479 480 480 + 481 481 482 482 483 483 + 484 484 485 485 486 486 + 487 487 488 488 489 489 + 490 490 491 491 492 492 + 493 493 494 494 495 495 + 496 496 497 497 498 498 + 499 499 500 500 501 501 + 502 502 503 503 504 504 + + + + + + + +{last_modified_time} column.log Page 4 + + + 505 505 506 506 507 507 + 508 508 509 509 510 510 + 511 511 512 512 513 513 + 514 514 515 515 516 516 + 517 517 518 518 519 519 + 520 520 521 521 522 522 + 523 523 524 524 525 525 + 526 526 527 527 528 528 + 529 529 530 530 531 531 + 532 532 533 533 534 534 + 535 535 536 536 537 537 + 538 538 539 539 540 540 + 541 541 542 542 543 543 + 544 544 545 545 546 546 + 547 547 548 548 549 549 + 550 550 551 551 552 552 + 553 553 554 554 555 555 + 556 556 557 557 558 558 + 559 559 560 560 561 561 + 562 562 563 563 564 564 + 565 565 566 566 567 567 + 568 568 569 569 570 570 + 571 571 572 572 573 573 + 574 574 575 575 576 576 + 577 577 578 578 579 579 + 580 580 581 581 582 582 + 583 583 584 584 585 585 + 586 586 587 587 588 588 + 589 589 590 590 591 591 + 592 592 593 593 594 594 + 595 595 596 596 597 597 + 598 598 599 599 600 600 + 601 601 602 602 603 603 + 604 604 605 605 606 606 + 607 607 608 608 609 609 + 610 610 611 611 612 612 + 613 613 614 614 615 615 + 616 616 617 617 618 618 + 619 619 620 620 621 621 + 622 622 623 623 624 624 + 625 625 626 626 627 627 + 628 628 629 629 630 630 + 631 631 632 632 633 633 + 634 634 635 635 636 636 + 637 637 638 638 639 639 + 640 640 641 641 642 642 + 643 643 644 644 645 645 + 646 646 647 647 648 648 + 649 649 650 650 651 651 + 652 652 653 653 654 654 + 655 655 656 656 657 657 + 658 658 659 659 660 660 + 661 661 662 662 663 663 + 664 664 665 665 666 666 + 667 667 668 668 669 669 + 670 670 671 671 672 672 + + + + + + + +{last_modified_time} column.log Page 5 + + + 673 673 674 674 675 675 + 676 676 677 677 678 678 + 679 679 680 680 681 681 + 682 682 683 683 684 684 + 685 685 686 686 687 687 + 688 688 689 689 690 690 + 691 691 692 692 693 693 + 694 694 695 695 696 696 + 697 697 698 698 699 699 + 700 700 701 701 702 702 + 703 703 704 704 705 705 + 706 706 707 707 708 708 + 709 709 710 710 711 711 + 712 712 713 713 714 714 + 715 715 716 716 717 717 + 718 718 719 719 720 720 + 721 721 722 722 723 723 + 724 724 725 725 726 726 + 727 727 728 728 729 729 + 730 730 731 731 732 732 + 733 733 734 734 735 735 + 736 736 737 737 738 738 + 739 739 740 740 741 741 + 742 742 743 743 744 744 + 745 745 746 746 747 747 + 748 748 749 749 750 750 + 751 751 752 752 753 753 + 754 754 755 755 756 756 + 757 757 758 758 759 759 + 760 760 761 761 762 762 + 763 763 764 764 765 765 + 766 766 767 767 768 768 + 769 769 770 770 771 771 + 772 772 773 773 774 774 + 775 775 776 776 777 777 + 778 778 779 779 780 780 + 781 781 782 782 783 783 + 784 784 785 785 786 786 + 787 787 788 788 789 789 + 790 790 791 791 792 792 + 793 793 794 794 795 795 + 796 796 797 797 798 798 + 799 799 800 800 801 801 + 802 802 803 803 804 804 + 805 805 806 806 807 807 + 808 808 809 809 810 810 + 811 811 812 812 813 813 + 814 814 815 815 816 816 + 817 817 818 818 819 819 + 820 820 821 821 822 822 + 823 823 824 824 825 825 + 826 826 827 827 828 828 + 829 829 830 830 831 831 + 832 832 833 833 834 834 + 835 835 836 836 837 837 + 838 838 839 839 840 840 + + + + + diff --git a/tests/fixtures/pr/column_across_sep.log.expected b/tests/fixtures/pr/column_across_sep.log.expected new file mode 100644 index 000000000..65c3e71c8 --- /dev/null +++ b/tests/fixtures/pr/column_across_sep.log.expected @@ -0,0 +1,198 @@ + + +{last_modified_time} column.log Page 3 + + + 337 337 | 338 338 | 339 339 + 340 340 | 341 341 | 342 342 + 343 343 | 344 344 | 345 345 + 346 346 | 347 347 | 348 348 + 349 349 | 350 350 | 351 351 + 352 352 | 353 353 | 354 354 + 355 355 | 356 356 | 357 357 + 358 358 | 359 359 | 360 360 + 361 361 | 362 362 | 363 363 + 364 364 | 365 365 | 366 366 + 367 367 | 368 368 | 369 369 + 370 370 | 371 371 | 372 372 + 373 373 | 374 374 | 375 375 + 376 376 | 377 377 | 378 378 + 379 379 | 380 380 | 381 381 + 382 382 | 383 383 | 384 384 + 385 385 | 386 386 | 387 387 + 388 388 | 389 389 | 390 390 + 391 391 | 392 392 | 393 393 + 394 394 | 395 395 | 396 396 + 397 397 | 398 398 | 399 399 + 400 400 | 401 401 | 402 402 + 403 403 | 404 404 | 405 405 + 406 406 | 407 407 | 408 408 + 409 409 | 410 410 | 411 411 + 412 412 | 413 413 | 414 414 + 415 415 | 416 416 | 417 417 + 418 418 | 419 419 | 420 420 + 421 421 | 422 422 | 423 423 + 424 424 | 425 425 | 426 426 + 427 427 | 428 428 | 429 429 + 430 430 | 431 431 | 432 432 + 433 433 | 434 434 | 435 435 + 436 436 | 437 437 | 438 438 + 439 439 | 440 440 | 441 441 + 442 442 | 443 443 | 444 444 + 445 445 | 446 446 | 447 447 + 448 448 | 449 449 | 450 450 + 451 451 | 452 452 | 453 453 + 454 454 | 455 455 | 456 456 + 457 457 | 458 458 | 459 459 + 460 460 | 461 461 | 462 462 + 463 463 | 464 464 | 465 465 + 466 466 | 467 467 | 468 468 + 469 469 | 470 470 | 471 471 + 472 472 | 473 473 | 474 474 + 475 475 | 476 476 | 477 477 + 478 478 | 479 479 | 480 480 + 481 481 | 482 482 | 483 483 + 484 484 | 485 485 | 486 486 + 487 487 | 488 488 | 489 489 + 490 490 | 491 491 | 492 492 + 493 493 | 494 494 | 495 495 + 496 496 | 497 497 | 498 498 + 499 499 | 500 500 | 501 501 + 502 502 | 503 503 | 504 504 + + + + + + + +{last_modified_time} column.log Page 4 + + + 505 505 | 506 506 | 507 507 + 508 508 | 509 509 | 510 510 + 511 511 | 512 512 | 513 513 + 514 514 | 515 515 | 516 516 + 517 517 | 518 518 | 519 519 + 520 520 | 521 521 | 522 522 + 523 523 | 524 524 | 525 525 + 526 526 | 527 527 | 528 528 + 529 529 | 530 530 | 531 531 + 532 532 | 533 533 | 534 534 + 535 535 | 536 536 | 537 537 + 538 538 | 539 539 | 540 540 + 541 541 | 542 542 | 543 543 + 544 544 | 545 545 | 546 546 + 547 547 | 548 548 | 549 549 + 550 550 | 551 551 | 552 552 + 553 553 | 554 554 | 555 555 + 556 556 | 557 557 | 558 558 + 559 559 | 560 560 | 561 561 + 562 562 | 563 563 | 564 564 + 565 565 | 566 566 | 567 567 + 568 568 | 569 569 | 570 570 + 571 571 | 572 572 | 573 573 + 574 574 | 575 575 | 576 576 + 577 577 | 578 578 | 579 579 + 580 580 | 581 581 | 582 582 + 583 583 | 584 584 | 585 585 + 586 586 | 587 587 | 588 588 + 589 589 | 590 590 | 591 591 + 592 592 | 593 593 | 594 594 + 595 595 | 596 596 | 597 597 + 598 598 | 599 599 | 600 600 + 601 601 | 602 602 | 603 603 + 604 604 | 605 605 | 606 606 + 607 607 | 608 608 | 609 609 + 610 610 | 611 611 | 612 612 + 613 613 | 614 614 | 615 615 + 616 616 | 617 617 | 618 618 + 619 619 | 620 620 | 621 621 + 622 622 | 623 623 | 624 624 + 625 625 | 626 626 | 627 627 + 628 628 | 629 629 | 630 630 + 631 631 | 632 632 | 633 633 + 634 634 | 635 635 | 636 636 + 637 637 | 638 638 | 639 639 + 640 640 | 641 641 | 642 642 + 643 643 | 644 644 | 645 645 + 646 646 | 647 647 | 648 648 + 649 649 | 650 650 | 651 651 + 652 652 | 653 653 | 654 654 + 655 655 | 656 656 | 657 657 + 658 658 | 659 659 | 660 660 + 661 661 | 662 662 | 663 663 + 664 664 | 665 665 | 666 666 + 667 667 | 668 668 | 669 669 + 670 670 | 671 671 | 672 672 + + + + + + + +{last_modified_time} column.log Page 5 + + + 673 673 | 674 674 | 675 675 + 676 676 | 677 677 | 678 678 + 679 679 | 680 680 | 681 681 + 682 682 | 683 683 | 684 684 + 685 685 | 686 686 | 687 687 + 688 688 | 689 689 | 690 690 + 691 691 | 692 692 | 693 693 + 694 694 | 695 695 | 696 696 + 697 697 | 698 698 | 699 699 + 700 700 | 701 701 | 702 702 + 703 703 | 704 704 | 705 705 + 706 706 | 707 707 | 708 708 + 709 709 | 710 710 | 711 711 + 712 712 | 713 713 | 714 714 + 715 715 | 716 716 | 717 717 + 718 718 | 719 719 | 720 720 + 721 721 | 722 722 | 723 723 + 724 724 | 725 725 | 726 726 + 727 727 | 728 728 | 729 729 + 730 730 | 731 731 | 732 732 + 733 733 | 734 734 | 735 735 + 736 736 | 737 737 | 738 738 + 739 739 | 740 740 | 741 741 + 742 742 | 743 743 | 744 744 + 745 745 | 746 746 | 747 747 + 748 748 | 749 749 | 750 750 + 751 751 | 752 752 | 753 753 + 754 754 | 755 755 | 756 756 + 757 757 | 758 758 | 759 759 + 760 760 | 761 761 | 762 762 + 763 763 | 764 764 | 765 765 + 766 766 | 767 767 | 768 768 + 769 769 | 770 770 | 771 771 + 772 772 | 773 773 | 774 774 + 775 775 | 776 776 | 777 777 + 778 778 | 779 779 | 780 780 + 781 781 | 782 782 | 783 783 + 784 784 | 785 785 | 786 786 + 787 787 | 788 788 | 789 789 + 790 790 | 791 791 | 792 792 + 793 793 | 794 794 | 795 795 + 796 796 | 797 797 | 798 798 + 799 799 | 800 800 | 801 801 + 802 802 | 803 803 | 804 804 + 805 805 | 806 806 | 807 807 + 808 808 | 809 809 | 810 810 + 811 811 | 812 812 | 813 813 + 814 814 | 815 815 | 816 816 + 817 817 | 818 818 | 819 819 + 820 820 | 821 821 | 822 822 + 823 823 | 824 824 | 825 825 + 826 826 | 827 827 | 828 828 + 829 829 | 830 830 | 831 831 + 832 832 | 833 833 | 834 834 + 835 835 | 836 836 | 837 837 + 838 838 | 839 839 | 840 840 + + + + + diff --git a/tests/fixtures/pr/column_across_sep1.log.expected b/tests/fixtures/pr/column_across_sep1.log.expected new file mode 100644 index 000000000..f9dd454d7 --- /dev/null +++ b/tests/fixtures/pr/column_across_sep1.log.expected @@ -0,0 +1,198 @@ + + +{last_modified_time} column.log Page 3 + + + 337 337 divide 338 338 divide 339 339 + 340 340 divide 341 341 divide 342 342 + 343 343 divide 344 344 divide 345 345 + 346 346 divide 347 347 divide 348 348 + 349 349 divide 350 350 divide 351 351 + 352 352 divide 353 353 divide 354 354 + 355 355 divide 356 356 divide 357 357 + 358 358 divide 359 359 divide 360 360 + 361 361 divide 362 362 divide 363 363 + 364 364 divide 365 365 divide 366 366 + 367 367 divide 368 368 divide 369 369 + 370 370 divide 371 371 divide 372 372 + 373 373 divide 374 374 divide 375 375 + 376 376 divide 377 377 divide 378 378 + 379 379 divide 380 380 divide 381 381 + 382 382 divide 383 383 divide 384 384 + 385 385 divide 386 386 divide 387 387 + 388 388 divide 389 389 divide 390 390 + 391 391 divide 392 392 divide 393 393 + 394 394 divide 395 395 divide 396 396 + 397 397 divide 398 398 divide 399 399 + 400 400 divide 401 401 divide 402 402 + 403 403 divide 404 404 divide 405 405 + 406 406 divide 407 407 divide 408 408 + 409 409 divide 410 410 divide 411 411 + 412 412 divide 413 413 divide 414 414 + 415 415 divide 416 416 divide 417 417 + 418 418 divide 419 419 divide 420 420 + 421 421 divide 422 422 divide 423 423 + 424 424 divide 425 425 divide 426 426 + 427 427 divide 428 428 divide 429 429 + 430 430 divide 431 431 divide 432 432 + 433 433 divide 434 434 divide 435 435 + 436 436 divide 437 437 divide 438 438 + 439 439 divide 440 440 divide 441 441 + 442 442 divide 443 443 divide 444 444 + 445 445 divide 446 446 divide 447 447 + 448 448 divide 449 449 divide 450 450 + 451 451 divide 452 452 divide 453 453 + 454 454 divide 455 455 divide 456 456 + 457 457 divide 458 458 divide 459 459 + 460 460 divide 461 461 divide 462 462 + 463 463 divide 464 464 divide 465 465 + 466 466 divide 467 467 divide 468 468 + 469 469 divide 470 470 divide 471 471 + 472 472 divide 473 473 divide 474 474 + 475 475 divide 476 476 divide 477 477 + 478 478 divide 479 479 divide 480 480 + 481 481 divide 482 482 divide 483 483 + 484 484 divide 485 485 divide 486 486 + 487 487 divide 488 488 divide 489 489 + 490 490 divide 491 491 divide 492 492 + 493 493 divide 494 494 divide 495 495 + 496 496 divide 497 497 divide 498 498 + 499 499 divide 500 500 divide 501 501 + 502 502 divide 503 503 divide 504 504 + + + + + + + +{last_modified_time} column.log Page 4 + + + 505 505 divide 506 506 divide 507 507 + 508 508 divide 509 509 divide 510 510 + 511 511 divide 512 512 divide 513 513 + 514 514 divide 515 515 divide 516 516 + 517 517 divide 518 518 divide 519 519 + 520 520 divide 521 521 divide 522 522 + 523 523 divide 524 524 divide 525 525 + 526 526 divide 527 527 divide 528 528 + 529 529 divide 530 530 divide 531 531 + 532 532 divide 533 533 divide 534 534 + 535 535 divide 536 536 divide 537 537 + 538 538 divide 539 539 divide 540 540 + 541 541 divide 542 542 divide 543 543 + 544 544 divide 545 545 divide 546 546 + 547 547 divide 548 548 divide 549 549 + 550 550 divide 551 551 divide 552 552 + 553 553 divide 554 554 divide 555 555 + 556 556 divide 557 557 divide 558 558 + 559 559 divide 560 560 divide 561 561 + 562 562 divide 563 563 divide 564 564 + 565 565 divide 566 566 divide 567 567 + 568 568 divide 569 569 divide 570 570 + 571 571 divide 572 572 divide 573 573 + 574 574 divide 575 575 divide 576 576 + 577 577 divide 578 578 divide 579 579 + 580 580 divide 581 581 divide 582 582 + 583 583 divide 584 584 divide 585 585 + 586 586 divide 587 587 divide 588 588 + 589 589 divide 590 590 divide 591 591 + 592 592 divide 593 593 divide 594 594 + 595 595 divide 596 596 divide 597 597 + 598 598 divide 599 599 divide 600 600 + 601 601 divide 602 602 divide 603 603 + 604 604 divide 605 605 divide 606 606 + 607 607 divide 608 608 divide 609 609 + 610 610 divide 611 611 divide 612 612 + 613 613 divide 614 614 divide 615 615 + 616 616 divide 617 617 divide 618 618 + 619 619 divide 620 620 divide 621 621 + 622 622 divide 623 623 divide 624 624 + 625 625 divide 626 626 divide 627 627 + 628 628 divide 629 629 divide 630 630 + 631 631 divide 632 632 divide 633 633 + 634 634 divide 635 635 divide 636 636 + 637 637 divide 638 638 divide 639 639 + 640 640 divide 641 641 divide 642 642 + 643 643 divide 644 644 divide 645 645 + 646 646 divide 647 647 divide 648 648 + 649 649 divide 650 650 divide 651 651 + 652 652 divide 653 653 divide 654 654 + 655 655 divide 656 656 divide 657 657 + 658 658 divide 659 659 divide 660 660 + 661 661 divide 662 662 divide 663 663 + 664 664 divide 665 665 divide 666 666 + 667 667 divide 668 668 divide 669 669 + 670 670 divide 671 671 divide 672 672 + + + + + + + +{last_modified_time} column.log Page 5 + + + 673 673 divide 674 674 divide 675 675 + 676 676 divide 677 677 divide 678 678 + 679 679 divide 680 680 divide 681 681 + 682 682 divide 683 683 divide 684 684 + 685 685 divide 686 686 divide 687 687 + 688 688 divide 689 689 divide 690 690 + 691 691 divide 692 692 divide 693 693 + 694 694 divide 695 695 divide 696 696 + 697 697 divide 698 698 divide 699 699 + 700 700 divide 701 701 divide 702 702 + 703 703 divide 704 704 divide 705 705 + 706 706 divide 707 707 divide 708 708 + 709 709 divide 710 710 divide 711 711 + 712 712 divide 713 713 divide 714 714 + 715 715 divide 716 716 divide 717 717 + 718 718 divide 719 719 divide 720 720 + 721 721 divide 722 722 divide 723 723 + 724 724 divide 725 725 divide 726 726 + 727 727 divide 728 728 divide 729 729 + 730 730 divide 731 731 divide 732 732 + 733 733 divide 734 734 divide 735 735 + 736 736 divide 737 737 divide 738 738 + 739 739 divide 740 740 divide 741 741 + 742 742 divide 743 743 divide 744 744 + 745 745 divide 746 746 divide 747 747 + 748 748 divide 749 749 divide 750 750 + 751 751 divide 752 752 divide 753 753 + 754 754 divide 755 755 divide 756 756 + 757 757 divide 758 758 divide 759 759 + 760 760 divide 761 761 divide 762 762 + 763 763 divide 764 764 divide 765 765 + 766 766 divide 767 767 divide 768 768 + 769 769 divide 770 770 divide 771 771 + 772 772 divide 773 773 divide 774 774 + 775 775 divide 776 776 divide 777 777 + 778 778 divide 779 779 divide 780 780 + 781 781 divide 782 782 divide 783 783 + 784 784 divide 785 785 divide 786 786 + 787 787 divide 788 788 divide 789 789 + 790 790 divide 791 791 divide 792 792 + 793 793 divide 794 794 divide 795 795 + 796 796 divide 797 797 divide 798 798 + 799 799 divide 800 800 divide 801 801 + 802 802 divide 803 803 divide 804 804 + 805 805 divide 806 806 divide 807 807 + 808 808 divide 809 809 divide 810 810 + 811 811 divide 812 812 divide 813 813 + 814 814 divide 815 815 divide 816 816 + 817 817 divide 818 818 divide 819 819 + 820 820 divide 821 821 divide 822 822 + 823 823 divide 824 824 divide 825 825 + 826 826 divide 827 827 divide 828 828 + 829 829 divide 830 830 divide 831 831 + 832 832 divide 833 833 divide 834 834 + 835 835 divide 836 836 divide 837 837 + 838 838 divide 839 839 divide 840 840 + + + + + diff --git a/tests/fixtures/pr/column_spaces_across.log.expected b/tests/fixtures/pr/column_spaces_across.log.expected new file mode 100644 index 000000000..037dd814b --- /dev/null +++ b/tests/fixtures/pr/column_spaces_across.log.expected @@ -0,0 +1,198 @@ + + +{last_modified_time} column.log Page 3 + + + 337 337 338 338 339 339 + 340 340 341 341 342 342 + 343 343 344 344 345 345 + 346 346 347 347 348 348 + 349 349 350 350 351 351 + 352 352 353 353 354 354 + 355 355 356 356 357 357 + 358 358 359 359 360 360 + 361 361 362 362 363 363 + 364 364 365 365 366 366 + 367 367 368 368 369 369 + 370 370 371 371 372 372 + 373 373 374 374 375 375 + 376 376 377 377 378 378 + 379 379 380 380 381 381 + 382 382 383 383 384 384 + 385 385 386 386 387 387 + 388 388 389 389 390 390 + 391 391 392 392 393 393 + 394 394 395 395 396 396 + 397 397 398 398 399 399 + 400 400 401 401 402 402 + 403 403 404 404 405 405 + 406 406 407 407 408 408 + 409 409 410 410 411 411 + 412 412 413 413 414 414 + 415 415 416 416 417 417 + 418 418 419 419 420 420 + 421 421 422 422 423 423 + 424 424 425 425 426 426 + 427 427 428 428 429 429 + 430 430 431 431 432 432 + 433 433 434 434 435 435 + 436 436 437 437 438 438 + 439 439 440 440 441 441 + 442 442 443 443 444 444 + 445 445 446 446 447 447 + 448 448 449 449 450 450 + 451 451 452 452 453 453 + 454 454 455 455 456 456 + 457 457 458 458 459 459 + 460 460 461 461 462 462 + 463 463 464 464 465 465 + 466 466 467 467 468 468 + 469 469 470 470 471 471 + 472 472 473 473 474 474 + 475 475 476 476 477 477 + 478 478 479 479 480 480 + 481 481 482 482 483 483 + 484 484 485 485 486 486 + 487 487 488 488 489 489 + 490 490 491 491 492 492 + 493 493 494 494 495 495 + 496 496 497 497 498 498 + 499 499 500 500 501 501 + 502 502 503 503 504 504 + + + + + + + +{last_modified_time} column.log Page 4 + + + 505 505 506 506 507 507 + 508 508 509 509 510 510 + 511 511 512 512 513 513 + 514 514 515 515 516 516 + 517 517 518 518 519 519 + 520 520 521 521 522 522 + 523 523 524 524 525 525 + 526 526 527 527 528 528 + 529 529 530 530 531 531 + 532 532 533 533 534 534 + 535 535 536 536 537 537 + 538 538 539 539 540 540 + 541 541 542 542 543 543 + 544 544 545 545 546 546 + 547 547 548 548 549 549 + 550 550 551 551 552 552 + 553 553 554 554 555 555 + 556 556 557 557 558 558 + 559 559 560 560 561 561 + 562 562 563 563 564 564 + 565 565 566 566 567 567 + 568 568 569 569 570 570 + 571 571 572 572 573 573 + 574 574 575 575 576 576 + 577 577 578 578 579 579 + 580 580 581 581 582 582 + 583 583 584 584 585 585 + 586 586 587 587 588 588 + 589 589 590 590 591 591 + 592 592 593 593 594 594 + 595 595 596 596 597 597 + 598 598 599 599 600 600 + 601 601 602 602 603 603 + 604 604 605 605 606 606 + 607 607 608 608 609 609 + 610 610 611 611 612 612 + 613 613 614 614 615 615 + 616 616 617 617 618 618 + 619 619 620 620 621 621 + 622 622 623 623 624 624 + 625 625 626 626 627 627 + 628 628 629 629 630 630 + 631 631 632 632 633 633 + 634 634 635 635 636 636 + 637 637 638 638 639 639 + 640 640 641 641 642 642 + 643 643 644 644 645 645 + 646 646 647 647 648 648 + 649 649 650 650 651 651 + 652 652 653 653 654 654 + 655 655 656 656 657 657 + 658 658 659 659 660 660 + 661 661 662 662 663 663 + 664 664 665 665 666 666 + 667 667 668 668 669 669 + 670 670 671 671 672 672 + + + + + + + +{last_modified_time} column.log Page 5 + + + 673 673 674 674 675 675 + 676 676 677 677 678 678 + 679 679 680 680 681 681 + 682 682 683 683 684 684 + 685 685 686 686 687 687 + 688 688 689 689 690 690 + 691 691 692 692 693 693 + 694 694 695 695 696 696 + 697 697 698 698 699 699 + 700 700 701 701 702 702 + 703 703 704 704 705 705 + 706 706 707 707 708 708 + 709 709 710 710 711 711 + 712 712 713 713 714 714 + 715 715 716 716 717 717 + 718 718 719 719 720 720 + 721 721 722 722 723 723 + 724 724 725 725 726 726 + 727 727 728 728 729 729 + 730 730 731 731 732 732 + 733 733 734 734 735 735 + 736 736 737 737 738 738 + 739 739 740 740 741 741 + 742 742 743 743 744 744 + 745 745 746 746 747 747 + 748 748 749 749 750 750 + 751 751 752 752 753 753 + 754 754 755 755 756 756 + 757 757 758 758 759 759 + 760 760 761 761 762 762 + 763 763 764 764 765 765 + 766 766 767 767 768 768 + 769 769 770 770 771 771 + 772 772 773 773 774 774 + 775 775 776 776 777 777 + 778 778 779 779 780 780 + 781 781 782 782 783 783 + 784 784 785 785 786 786 + 787 787 788 788 789 789 + 790 790 791 791 792 792 + 793 793 794 794 795 795 + 796 796 797 797 798 798 + 799 799 800 800 801 801 + 802 802 803 803 804 804 + 805 805 806 806 807 807 + 808 808 809 809 810 810 + 811 811 812 812 813 813 + 814 814 815 815 816 816 + 817 817 818 818 819 819 + 820 820 821 821 822 822 + 823 823 824 824 825 825 + 826 826 827 827 828 828 + 829 829 830 830 831 831 + 832 832 833 833 834 834 + 835 835 836 836 837 837 + 838 838 839 839 840 840 + + + + + diff --git a/tests/fixtures/pr/hosts.log b/tests/fixtures/pr/hosts.log new file mode 100644 index 000000000..8d725920c --- /dev/null +++ b/tests/fixtures/pr/hosts.log @@ -0,0 +1,11 @@ +## +# Host Database +# +# localhost is used to configure the loopback interface +# when the system is booting. Do not change this entry. +## +127.0.0.1 localhost +127.0.0.1 Techopss-MacBook-Pro.local +127.0.0.1 tilakpr +255.255.255.255 broadcasthost +::1 localhost \ No newline at end of file diff --git a/tests/fixtures/pr/joined.log.expected b/tests/fixtures/pr/joined.log.expected new file mode 100644 index 000000000..a9cee6e4f --- /dev/null +++ b/tests/fixtures/pr/joined.log.expected @@ -0,0 +1,132 @@ + + +{last_modified_time} Page 1 + + +##ntation processAirPortStateChanges]: pppConnectionState 0 +# Host DatabaseMon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +#Mon Dec 10 11:42:56.705 Info: 802.1X changed +# localhost is used to configure the loopback interfaceMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +# when the system is booting. Do not change this entry.Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +##Mon Dec 10 11:42:56.854 Info: 802.1X changed +127.0.0.1 localhostMon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +127.0.0.1 Techopss-MacBook-Pro.localMon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +127.0.0.1 tilakprMon Dec 10 11:42:57.002 Info: 802.1X changed +255.255.255.255 broadcasthostMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +::1 localhostMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + + + +{last_modified_time} Page 2 + + +Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: APPLE80211_M_ROAM_END (en0) +Mon Dec 10 11:42:59.372 Info: Roaming ended on interface en0 +Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: RSN_HANDSHAKE_DONE (en0) +Mon Dec 10 11:42:59.373 Info: -[CWXPCInterfaceContext setRoamInProgress:reason:]_block_invoke: roam status metric data: CWAWDMetricRoamStatus: status:0 security: 4 profile:5 origin:<34fcb9>(-69) target:<6cf37f>(-56) latency:6.083439s +Mon Dec 10 11:42:59.373 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90046 +Mon Dec 10 11:42:59.373 Info: RESUME AWDL for interface en0, reason=Roam token=2685 +Mon Dec 10 11:42:59.373 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=5] +Mon Dec 10 11:42:59.374 Info: -[CWXPCInterfaceContext __setAWDLOperatingMode:interface:error:]: attempting to set AWDL mode to 0 +Mon Dec 10 11:43:01.072 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP' +Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP +Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: DHCP airport_changed = 1 +Mon Dec 10 11:43:01.073 Info: -[CWXPCSubsystem internal_submitIPConfigLatencyMetric:leaseDuration:]: IPConfig Latency metric data: CWAWDMetricIPConfigLatencyData: DHCP latency: 29010 msecs, duration: 480 mins, security: 4 +Mon Dec 10 11:43:01.073 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90007 +Mon Dec 10 11:43:01.073 SC: _setDHCPMessage: dhcpInfoKey "State:/Network/Interface/en0/AirPort/DHCP Message" = (null) +Mon Dec 10 11:43:10.369 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:10.369 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=162 Mbps +Mon Dec 10 11:43:10.369 Info: link quality changed +Mon Dec 10 11:43:23.376 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:23.377 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=243 Mbps +Mon Dec 10 11:43:23.377 Info: link quality changed +Mon Dec 10 11:43:28.380 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:28.380 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 11:43:28.380 Info: link quality changed +Mon Dec 10 11:43:31.744 AutoJoin: BACKGROUND SCAN request on interface en0 with SSID list (null) +Mon Dec 10 11:43:31.747 Scan: Cache-assisted scan request on channel 1 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 2 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 3 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.748 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.748 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.748 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.748 [channelNumber=3(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.748 )} took 0.0025 seconds, returned 10 results +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 4 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 5 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 6 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.749 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.749 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.749 )} took 0.0008 seconds, returned 7 results +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 7 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 8 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 9 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.749 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.749 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=9(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.749 )} took 0.0002 seconds, returned 1 results +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 10 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 11 does not require a live scan +Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 12 does not require a live scan + + + + + diff --git a/tests/fixtures/pr/l24-FF b/tests/fixtures/pr/l24-FF new file mode 100644 index 000000000..de219b2fb --- /dev/null +++ b/tests/fixtures/pr/l24-FF @@ -0,0 +1,312 @@ + + +{last_modified_time} {file_name} Page 1 + + +1 FF-Test: FF's in Text V +2 Options -b -3 / -a -3 / ... +3 -------------------------------------------- +4 3456789 123456789 123456789 123456789 12345678 +5 3 Columns downwards ..., <= 5 lines per page +6 FF-Arangements: One Empty Page +7 text\f\f\n; text\f\n\ftext; \f\ftext; +8 \f\f\n; \f\n\f\n; +9 +10 zzzzzzzzzzzzzzzzzzzzzzzzzz123456789 +1 12345678 +2 12345678 +3 line truncation before FF; r_r_o_l-test: +14 456789 123456789 123456789 123456789 + + + + + + + +{last_modified_time} {file_name} Page 2 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 3 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 4 + + +15 xyzxyzxyz XYZXYZXYZ abcabcab +16 456789 123456789 xyzxyzxyz XYZXYZXYZ +7 12345678 +8 12345678 +9 3456789 ab +20 DEFGHI 123 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +5 12345678 +6 12345678 +27 no truncation before FF; (r_l-test): +28 no trunc + + + + + + + +{last_modified_time} {file_name} Page 5 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 6 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 7 + + +29 xyzxyzxyz XYZXYZXYZ abcabcab +30 456789 123456789 xyzxyzxyz XYZXYZXYZ +1 12345678 +2 3456789 abcdefghi +3 12345678 +4 12345678 +5 12345678 +6 12345678 +7 12345678 +8 12345678 +9 3456789 abcdefghi +40 DEFGHI 123456789 +41 yzxyzxyz XYZXYZXYZ abcabcab +42 456789 123456789 abcdefghi ABCDEDFHI + + + + + + + +{last_modified_time} {file_name} Page 8 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 9 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 10 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 11 + + +43 xyzxyzxyz XYZXYZXYZ abcabcab +44 456789 123456789 xyzxyzxyz XYZXYZXYZ +5 12345678 +6 12345678 +7 12345678 +8 12345678 +9 12345678 +50 12345678 +1 12345678 +2 12345678 +3 12345678 +4 12345678 +55 yzxyzxyz XYZXYZXYZ abcabcab +56 456789 123456789 abcdefghi ABCDEDFHI + + + + + + + +{last_modified_time} {file_name} Page 12 + + + + + + + + + + + + + + + + + + + + + + + +{last_modified_time} {file_name} Page 13 + + +57 xyzxyzxyz XYZXYZXYZ abcabcab +58 456789 123456789 xyzxyzxyz XYZXYZXYZ +9 12345678 +60 DEFGHI 123456789 + + + + + + + + + + + + + + + diff --git a/tests/fixtures/pr/mpr.log.expected b/tests/fixtures/pr/mpr.log.expected new file mode 100644 index 000000000..f6fffd191 --- /dev/null +++ b/tests/fixtures/pr/mpr.log.expected @@ -0,0 +1,132 @@ + + +{last_modified_time} Page 1 + + + 1 1 ## + 2 2 # Host Database + 3 3 # + 4 4 # localhost is used to configure th + 5 5 # when the system is booting. Do n + 6 6 ## + 7 7 127.0.0.1 localhost + 8 8 127.0.0.1 Techopss-MacBook-Pro.loca + 9 9 127.0.0.1 tilakpr + 10 10 255.255.255.255 broadcasthost + 11 11 ::1 localhost + 12 12 + 13 13 + 14 14 + 15 15 + 16 16 + 17 17 + 18 18 + 19 19 + 20 20 + 21 21 + 22 22 + 23 23 + 24 24 + 25 25 + 26 26 + 27 27 + 28 28 + 29 29 + 30 30 + 31 31 + 32 32 + 33 33 + 34 34 + 35 35 + 36 36 + 37 37 + 38 38 + 39 39 + 40 40 + 41 41 + 42 42 + 43 43 + 44 44 + 45 45 + 46 46 + 47 47 + 48 48 + 49 49 + 50 50 + 51 51 + 52 52 + 53 53 + 54 54 + 55 55 + 56 56 + + + + + + + +{last_modified_time} Page 2 + + + 57 57 + 58 58 + 59 59 + 60 60 + 61 61 + 62 62 + 63 63 + 64 64 + 65 65 + 66 66 + 67 67 + 68 68 + 69 69 + 70 70 + 71 71 + 72 72 + 73 73 + 74 74 + 75 75 + 76 76 + 77 77 + 78 78 + 79 79 + 80 80 + 81 81 + 82 82 + 83 83 + 84 84 + 85 85 + 86 86 + 87 87 + 88 88 + 89 89 + 90 90 + 91 91 + 92 92 + 93 93 + 94 94 + 95 95 + 96 96 + 97 97 + 98 98 + 99 99 + 100 100 + 101 101 + 102 102 + 103 103 + 104 104 + 105 105 + 106 106 + 107 107 + 108 108 + 109 109 + 110 110 + 111 111 + 112 112 + + + + + diff --git a/tests/fixtures/pr/mpr1.log.expected b/tests/fixtures/pr/mpr1.log.expected new file mode 100644 index 000000000..64d786d90 --- /dev/null +++ b/tests/fixtures/pr/mpr1.log.expected @@ -0,0 +1,198 @@ + + +{last_modified_time} Page 2 + + + 57 57 + 58 58 + 59 59 + 60 60 + 61 61 + 62 62 + 63 63 + 64 64 + 65 65 + 66 66 + 67 67 + 68 68 + 69 69 + 70 70 + 71 71 + 72 72 + 73 73 + 74 74 + 75 75 + 76 76 + 77 77 + 78 78 + 79 79 + 80 80 + 81 81 + 82 82 + 83 83 + 84 84 + 85 85 + 86 86 + 87 87 + 88 88 + 89 89 + 90 90 + 91 91 + 92 92 + 93 93 + 94 94 + 95 95 + 96 96 + 97 97 + 98 98 + 99 99 + 100 100 + 101 101 + 102 102 + 103 103 + 104 104 + 105 105 + 106 106 + 107 107 + 108 108 + 109 109 + 110 110 + 111 111 + 112 112 + + + + + + + +{last_modified_time} Page 3 + + + 113 113 + 114 114 + 115 115 + 116 116 + 117 117 + 118 118 + 119 119 + 120 120 + 121 121 + 122 122 + 123 123 + 124 124 + 125 125 + 126 126 + 127 127 + 128 128 + 129 129 + 130 130 + 131 131 + 132 132 + 133 133 + 134 134 + 135 135 + 136 136 + 137 137 + 138 138 + 139 139 + 140 140 + 141 141 + 142 142 + 143 143 + 144 144 + 145 145 + 146 146 + 147 147 + 148 148 + 149 149 + 150 150 + 151 151 + 152 152 + 153 153 + 154 154 + 155 155 + 156 156 + 157 157 + 158 158 + 159 159 + 160 160 + 161 161 + 162 162 + 163 163 + 164 164 + 165 165 + 166 166 + 167 167 + 168 168 + + + + + + + +{last_modified_time} Page 4 + + + 169 169 + 170 170 + 171 171 + 172 172 + 173 173 + 174 174 + 175 175 + 176 176 + 177 177 + 178 178 + 179 179 + 180 180 + 181 181 + 182 182 + 183 183 + 184 184 + 185 185 + 186 186 + 187 187 + 188 188 + 189 189 + 190 190 + 191 191 + 192 192 + 193 193 + 194 194 + 195 195 + 196 196 + 197 197 + 198 198 + 199 199 + 200 200 + 201 201 + 202 202 + 203 203 + 204 204 + 205 205 + 206 206 + 207 207 + 208 208 + 209 209 + 210 210 + 211 211 + 212 212 + 213 213 + 214 214 + 215 215 + 216 216 + 217 217 + 218 218 + 219 219 + 220 220 + 221 221 + 222 222 + 223 223 + 224 224 + + + + + diff --git a/tests/fixtures/pr/mpr2.log.expected b/tests/fixtures/pr/mpr2.log.expected new file mode 100644 index 000000000..091f0f228 --- /dev/null +++ b/tests/fixtures/pr/mpr2.log.expected @@ -0,0 +1,200 @@ + + +{last_modified_time} Page 1 + + + 1 1 ## 1 + 2 2 # Host Database 2 + 3 3 # 3 + 4 4 # localhost is used to 4 + 5 5 # when the system is bo 5 + 6 6 ## 6 + 7 7 127.0.0.1 localhost 7 + 8 8 127.0.0.1 Techopss-MacB 8 + 9 9 127.0.0.1 tilakpr 9 + 10 10 255.255.255.255 broadca 10 + 11 11 ::1 localho 11 + 12 12 12 + 13 13 13 + 14 14 14 + 15 15 15 + 16 16 16 + 17 17 17 + 18 18 18 + 19 19 19 + 20 20 20 + 21 21 21 + 22 22 22 + 23 23 23 + 24 24 24 + 25 25 25 + 26 26 26 + 27 27 27 + 28 28 28 + 29 29 29 + 30 30 30 + 31 31 31 + 32 32 32 + 33 33 33 + 34 34 34 + 35 35 35 + 36 36 36 + 37 37 37 + 38 38 38 + 39 39 39 + 40 40 40 + 41 41 41 + 42 42 42 + 43 43 43 + 44 44 44 + 45 45 45 + 46 46 46 + 47 47 47 + 48 48 48 + 49 49 49 + 50 50 50 + 51 51 51 + 52 52 52 + 53 53 53 + 54 54 54 + 55 55 55 + 56 56 56 + 57 57 57 + 58 58 58 + 59 59 59 + 60 60 60 + 61 61 61 + 62 62 62 + 63 63 63 + 64 64 64 + 65 65 65 + 66 66 66 + 67 67 67 + 68 68 68 + 69 69 69 + 70 70 70 + 71 71 71 + 72 72 72 + 73 73 73 + 74 74 74 + 75 75 75 + 76 76 76 + 77 77 77 + 78 78 78 + 79 79 79 + 80 80 80 + 81 81 81 + 82 82 82 + 83 83 83 + 84 84 84 + 85 85 85 + 86 86 86 + 87 87 87 + 88 88 88 + 89 89 89 + 90 90 90 + + + + + + + +{last_modified_time} Page 2 + + + 91 91 91 + 92 92 92 + 93 93 93 + 94 94 94 + 95 95 95 + 96 96 96 + 97 97 97 + 98 98 98 + 99 99 99 + 100 100 100 + 101 101 101 + 102 102 102 + 103 103 103 + 104 104 104 + 105 105 105 + 106 106 106 + 107 107 107 + 108 108 108 + 109 109 109 + 110 110 110 + 111 111 111 + 112 112 112 + 113 113 113 + 114 114 114 + 115 115 115 + 116 116 116 + 117 117 117 + 118 118 118 + 119 119 119 + 120 120 120 + 121 121 121 + 122 122 122 + 123 123 123 + 124 124 124 + 125 125 125 + 126 126 126 + 127 127 127 + 128 128 128 + 129 129 129 + 130 130 130 + 131 131 131 + 132 132 132 + 133 133 133 + 134 134 134 + 135 135 135 + 136 136 136 + 137 137 137 + 138 138 138 + 139 139 139 + 140 140 140 + 141 141 141 + 142 142 142 + 143 143 143 + 144 144 144 + 145 145 145 + 146 146 146 + 147 147 147 + 148 148 148 + 149 149 149 + 150 150 150 + 151 151 151 + 152 152 152 + 153 153 153 + 154 154 154 + 155 155 155 + 156 156 156 + 157 157 157 + 158 158 158 + 159 159 159 + 160 160 160 + 161 161 161 + 162 162 162 + 163 163 163 + 164 164 164 + 165 165 165 + 166 166 166 + 167 167 167 + 168 168 168 + 169 169 169 + 170 170 170 + 171 171 171 + 172 172 172 + 173 173 173 + 174 174 174 + 175 175 175 + 176 176 176 + 177 177 177 + 178 178 178 + 179 179 179 + 180 180 180 + + + + + diff --git a/tests/fixtures/pr/stdin.log b/tests/fixtures/pr/stdin.log new file mode 100644 index 000000000..72c7f4604 --- /dev/null +++ b/tests/fixtures/pr/stdin.log @@ -0,0 +1,82 @@ +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed +Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: APPLE80211_M_ROAM_END (en0) +Mon Dec 10 11:42:59.372 Info: Roaming ended on interface en0 +Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: RSN_HANDSHAKE_DONE (en0) +Mon Dec 10 11:42:59.373 Info: -[CWXPCInterfaceContext setRoamInProgress:reason:]_block_invoke: roam status metric data: CWAWDMetricRoamStatus: status:0 security: 4 profile:5 origin:<34fcb9>(-69) target:<6cf37f>(-56) latency:6.083439s +Mon Dec 10 11:42:59.373 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90046 +Mon Dec 10 11:42:59.373 Info: RESUME AWDL for interface en0, reason=Roam token=2685 +Mon Dec 10 11:42:59.373 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=5] +Mon Dec 10 11:42:59.374 Info: -[CWXPCInterfaceContext __setAWDLOperatingMode:interface:error:]: attempting to set AWDL mode to 0 +Mon Dec 10 11:43:01.072 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP' +Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP +Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: DHCP airport_changed = 1 +Mon Dec 10 11:43:01.073 Info: -[CWXPCSubsystem internal_submitIPConfigLatencyMetric:leaseDuration:]: IPConfig Latency metric data: CWAWDMetricIPConfigLatencyData: DHCP latency: 29010 msecs, duration: 480 mins, security: 4 +Mon Dec 10 11:43:01.073 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90007 +Mon Dec 10 11:43:01.073 SC: _setDHCPMessage: dhcpInfoKey "State:/Network/Interface/en0/AirPort/DHCP Message" = (null) +Mon Dec 10 11:43:10.369 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:10.369 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=162 Mbps +Mon Dec 10 11:43:10.369 Info: link quality changed +Mon Dec 10 11:43:23.376 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:23.377 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=243 Mbps +Mon Dec 10 11:43:23.377 Info: link quality changed +Mon Dec 10 11:43:28.380 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:28.380 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 11:43:28.380 Info: link quality changed +Mon Dec 10 11:43:31.744 AutoJoin: BACKGROUND SCAN request on interface en0 with SSID list (null) diff --git a/tests/fixtures/pr/stdin.log.expected b/tests/fixtures/pr/stdin.log.expected new file mode 100644 index 000000000..6922ee594 --- /dev/null +++ b/tests/fixtures/pr/stdin.log.expected @@ -0,0 +1,132 @@ + + +{last_modified_time} Page 1 + + + 1 ntation processAirPortStateChanges]: pppConnectionState 0 + 2 Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 3 Mon Dec 10 11:42:56.705 Info: 802.1X changed + 4 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 5 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 6 Mon Dec 10 11:42:56.854 Info: 802.1X changed + 7 Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 8 Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 9 Mon Dec 10 11:42:57.002 Info: 802.1X changed + 10 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 11 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 12 Mon Dec 10 11:42:57.152 Info: 802.1X changed + 13 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 14 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 15 Mon Dec 10 11:42:57.302 Info: 802.1X changed + 16 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 17 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 18 Mon Dec 10 11:42:57.449 Info: 802.1X changed + 19 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 20 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 21 Mon Dec 10 11:42:57.600 Info: 802.1X changed + 22 Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 23 Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 24 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 25 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 26 Mon Dec 10 11:42:57.749 Info: 802.1X changed + 27 Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 28 Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 29 Mon Dec 10 11:42:57.896 Info: 802.1X changed + 30 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 31 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 32 Mon Dec 10 11:42:58.045 Info: 802.1X changed + 33 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 34 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 35 Mon Dec 10 11:42:58.193 Info: 802.1X changed + 36 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 37 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 38 Mon Dec 10 11:42:58.342 Info: 802.1X changed + 39 Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 40 Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 41 Mon Dec 10 11:42:58.491 Info: 802.1X changed + 42 Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 43 Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 44 Mon Dec 10 11:42:58.640 Info: 802.1X changed + 45 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 46 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 47 Mon Dec 10 11:42:58.805 Info: 802.1X changed + 48 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 49 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 50 Mon Dec 10 11:42:58.958 Info: 802.1X changed + 51 Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 52 Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 53 Mon Dec 10 11:42:59.155 Info: 802.1X changed + 54 Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 55 Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 56 Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + + + +{last_modified_time} Page 2 + + + 57 Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 58 Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 59 Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: APPLE80211_M_ROAM_END (en0) + 60 Mon Dec 10 11:42:59.372 Info: Roaming ended on interface en0 + 61 Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: RSN_HANDSHAKE_DONE (en0) + 62 Mon Dec 10 11:42:59.373 Info: -[CWXPCInterfaceContext setRoamInProgress:reason:]_block_invoke: roam status metric data: CWAWDMetricRoamStatus: status:0 security: 4 profile:5 origin:<34fcb9>(-69) target:<6cf37f>(-56) latency:6.083439s + 63 Mon Dec 10 11:42:59.373 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90046 + 64 Mon Dec 10 11:42:59.373 Info: RESUME AWDL for interface en0, reason=Roam token=2685 + 65 Mon Dec 10 11:42:59.373 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=5] + 66 Mon Dec 10 11:42:59.374 Info: -[CWXPCInterfaceContext __setAWDLOperatingMode:interface:error:]: attempting to set AWDL mode to 0 + 67 Mon Dec 10 11:43:01.072 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP' + 68 Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP + 69 Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: DHCP airport_changed = 1 + 70 Mon Dec 10 11:43:01.073 Info: -[CWXPCSubsystem internal_submitIPConfigLatencyMetric:leaseDuration:]: IPConfig Latency metric data: CWAWDMetricIPConfigLatencyData: DHCP latency: 29010 msecs, duration: 480 mins, security: 4 + 71 Mon Dec 10 11:43:01.073 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90007 + 72 Mon Dec 10 11:43:01.073 SC: _setDHCPMessage: dhcpInfoKey "State:/Network/Interface/en0/AirPort/DHCP Message" = (null) + 73 Mon Dec 10 11:43:10.369 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) + 74 Mon Dec 10 11:43:10.369 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=162 Mbps + 75 Mon Dec 10 11:43:10.369 Info: link quality changed + 76 Mon Dec 10 11:43:23.376 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) + 77 Mon Dec 10 11:43:23.377 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=243 Mbps + 78 Mon Dec 10 11:43:23.377 Info: link quality changed + 79 Mon Dec 10 11:43:28.380 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) + 80 Mon Dec 10 11:43:28.380 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps + 81 Mon Dec 10 11:43:28.380 Info: link quality changed + 82 Mon Dec 10 11:43:31.744 AutoJoin: BACKGROUND SCAN request on interface en0 with SSID list (null) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/fixtures/pr/tFFt-ll b/tests/fixtures/pr/tFFt-ll new file mode 100644 index 000000000..39eca655f --- /dev/null +++ b/tests/fixtures/pr/tFFt-ll @@ -0,0 +1,56 @@ +1<<< -Test: FF's in Text >>> +2<<< -b -3 / -a -3 / ... >>> +3<<< >>> +4<<< 123456789 123456789 123456789 123456789 123456789 123456789 123456789 >>> + +6<<< -Arangements: One Empty Page >>> +7<<< \f\f\n; text\f\n\ftext; \f\ftext; >>> +8<<< f\f\n; \f\n\f\n; >>> +9<<< >>> +10<<< >>> +1<<< >>> +2<<< >>> +3<<< truncation before FF; r_r_o_l-test: >>> +14<<< 123456789 123456789 123456789 >>> 15<<< xyzxyzxyz XYZXYZXYZ abcabcab >>> +16<<< 123456789 xyzxyzxyz XYZXYZXYZ >>> +7<<< >>> +8<<< >>> +9<<< >>> +20<<< >>> +1<<< >>> + + +4<<< >>> +5<<< >>> +6<<< >>> +27<<< truncation before FF; (r_l-test): >>> +28<<< trunc 29<<>> +30<<< 123456789 xyzxyzxyz XYZXYZXYZ >>> +1<<< >>> +2<<< abcdefghi >>> +3<<< >>> +4<<< >>> +5<<< >>> +6<<< >>> +7<<< >>> +8<<< >>> +9<<< abcdefghi >>> +40<<< 123456789 >>> +41<<< XYZXYZXYZ abcabcab >>> +42<<< 123456789 abcdefghi ABCDEDFHI >>> 43<<< xyzxyzxyz XYZXYZXYZ abcabcab >>> +44<<< 123456789 xyzxyzxyz XYZXYZXYZ >>> +5<<< >>> +6<<< >>> +7<<< >>> +8<<< >>> +9<<< >>> +50<<< >>> +1<<< >>> +2<<< >>> +3<<< >>> +4<<< >>> +55<<< XYZXYZXYZ abcabcab >>> +56<<< 123456789 abcdefghi ABCDEDFHI >>> 57<<< xyzxyzxyz XYZXYZXYZ abcabcab >>> +58<<< 123456789 xyzxyzxyz XYZXYZXYZ >>> +9<<< >>> +60<<< 123456789 >>> diff --git a/tests/fixtures/pr/test.log b/tests/fixtures/pr/test.log new file mode 100644 index 000000000..53aaa0151 --- /dev/null +++ b/tests/fixtures/pr/test.log @@ -0,0 +1,1000 @@ +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed +Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.354 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: APPLE80211_M_ROAM_END (en0) +Mon Dec 10 11:42:59.372 Info: Roaming ended on interface en0 +Mon Dec 10 11:42:59.372 Driver Event: _bsd_80211_event_callback: RSN_HANDSHAKE_DONE (en0) +Mon Dec 10 11:42:59.373 Info: -[CWXPCInterfaceContext setRoamInProgress:reason:]_block_invoke: roam status metric data: CWAWDMetricRoamStatus: status:0 security: 4 profile:5 origin:<34fcb9>(-69) target:<6cf37f>(-56) latency:6.083439s +Mon Dec 10 11:42:59.373 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90046 +Mon Dec 10 11:42:59.373 Info: RESUME AWDL for interface en0, reason=Roam token=2685 +Mon Dec 10 11:42:59.373 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=5] +Mon Dec 10 11:42:59.374 Info: -[CWXPCInterfaceContext __setAWDLOperatingMode:interface:error:]: attempting to set AWDL mode to 0 +Mon Dec 10 11:43:01.072 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP' +Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: State:/Network/Service/18E14EA7-4641-4104-B315-A9315814912A/DHCP +Mon Dec 10 11:43:01.072 SC: _processDHCPChanges: DHCP airport_changed = 1 +Mon Dec 10 11:43:01.073 Info: -[CWXPCSubsystem internal_submitIPConfigLatencyMetric:leaseDuration:]: IPConfig Latency metric data: CWAWDMetricIPConfigLatencyData: DHCP latency: 29010 msecs, duration: 480 mins, security: 4 +Mon Dec 10 11:43:01.073 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90007 +Mon Dec 10 11:43:01.073 SC: _setDHCPMessage: dhcpInfoKey "State:/Network/Interface/en0/AirPort/DHCP Message" = (null) +Mon Dec 10 11:43:10.369 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:10.369 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=162 Mbps +Mon Dec 10 11:43:10.369 Info: link quality changed +Mon Dec 10 11:43:23.376 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:23.377 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=243 Mbps +Mon Dec 10 11:43:23.377 Info: link quality changed +Mon Dec 10 11:43:28.380 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:28.380 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 11:43:28.380 Info: link quality changed +Mon Dec 10 11:43:31.744 AutoJoin: BACKGROUND SCAN request on interface en0 with SSID list (null) +Mon Dec 10 11:43:31.747 Scan: Cache-assisted scan request on channel 1 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 2 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 3 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.748 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.748 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.748 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.748 [channelNumber=3(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.748 )} took 0.0025 seconds, returned 10 results +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 4 does not require a live scan +Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 5 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 6 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.749 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.749 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.749 )} took 0.0008 seconds, returned 7 results +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 7 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 8 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 9 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.749 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.749 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.749 [channelNumber=9(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.749 )} took 0.0002 seconds, returned 1 results +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 10 does not require a live scan +Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 11 does not require a live scan +Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 12 does not require a live scan +Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.750 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.750 [channelNumber=10(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.750 [channelNumber=11(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.750 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.750 )} took 0.0004 seconds, returned 4 results +Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 13 does not require a live scan +Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 36 does not require a live scan +Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 40 does not require a live scan +Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.751 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.751 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.751 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 11:43:31.751 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 11:43:31.751 )} took 0.0009 seconds, returned 9 results +Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request on channel 44 does not require a live scan +Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request on channel 48 does not require a live scan +Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request on channel 149 does not require a live scan +Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.752 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.752 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 11:43:31.752 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 11:43:31.752 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:43:31.752 )} took 0.0010 seconds, returned 9 results +Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request on channel 153 does not require a live scan +Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request on channel 157 does not require a live scan +Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request on channel 161 does not require a live scan +Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.753 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.753 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 11:43:31.753 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.753 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 11:43:31.753 )} took 0.0007 seconds, returned 9 results +Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 165 does not require a live scan +Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 52 does not require a live scan +Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 56 does not require a live scan +Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.753 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.753 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:43:31.753 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 11:43:31.753 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 11:43:31.753 )} took 0.0005 seconds, returned 6 results +Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 60 does not require a live scan +Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 64 does not require a live scan +Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 11:43:31.753 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 11:43:31.754 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 11:43:31.754 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 11:43:31.754 )} took 0.0003 seconds, returned 4 results +Mon Dec 10 11:43:42.382 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 11:43:42.382 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=270 Mbps +Mon Dec 10 11:43:42.383 Info: link quality changed +Mon Dec 10 11:49:12.347 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 11:49:12.350 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 11:52:32.194 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 11:52:32.448 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 11:52:32.450 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 11:52:32.451 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 11:52:32.451 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.451 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.451 [channelNumber=3(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.451 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.451 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.451 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:52:32.451 )} took 0.2566 seconds, returned 10 results +Mon Dec 10 11:52:32.451 Info: scan cache updated +Mon Dec 10 11:52:32.712 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 11:52:32.715 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 11:52:32.715 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 11:52:32.715 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.715 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.715 [channelNumber=9(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.715 [channelNumber=10(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.715 [channelNumber=11(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.715 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:52:32.715 )} took 0.2636 seconds, returned 10 results +Mon Dec 10 11:52:32.994 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 11:52:32.997 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 11:52:32.998 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 11:52:32.998 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:32.998 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 11:52:32.998 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 11:52:32.998 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 11:52:32.998 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 11:52:32.998 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 11:52:32.998 )} took 0.2822 seconds, returned 14 results +Mon Dec 10 11:52:33.405 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 11:52:33.408 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 11:52:33.409 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 11:52:33.409 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 11:52:33.409 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:33.409 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 11:52:33.409 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 11:52:33.409 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 11:52:33.409 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 11:52:33.409 )} took 0.4099 seconds, returned 11 results +Mon Dec 10 11:52:33.669 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 11:52:33.672 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 11:52:33.672 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 11:52:33.672 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 11:52:33.672 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 11:52:33.672 )} took 0.2625 seconds, returned 8 results +Mon Dec 10 11:52:33.673 Info: scan cache updated +Mon Dec 10 11:52:33.693 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 11:52:33.693 Scan: locationd requested a live scan less than 10 seconds after previous request (1.4991s) returning cached scan results +Mon Dec 10 11:52:33.728 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 11:52:33.728 Scan: locationd requested a live scan less than 10 seconds after previous request (1.5339s) returning cached scan results +Mon Dec 10 11:55:47.609 Driver Discovery: _PMConnectionHandler: caps = CPU Net Disk +Mon Dec 10 11:55:47.609 Driver Discovery: _PMConnectionHandler: Being put into maintenance wake mode while fully awake. +Mon Dec 10 11:55:47.610 Info: psCallback: powerSource = AC Power +Mon Dec 10 11:55:47.610 Info: psCallback: set powersave disabled on en0 +Mon Dec 10 11:55:47.637 Info: RELINQUISH BT PAGING LOCK request received from pid 106 (bluetoothd) +Mon Dec 10 11:55:47.637 Info: RELINQUISH BT PAGING LOCK request received from pid 106 (bluetoothd) +Mon Dec 10 11:55:47.638 BTC: BT PAGING state already set to 0 +Mon Dec 10 11:55:47.638 BTC: BT PAGING state already set to 0 +Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED after 0.0 seconds +Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED after 0.0 seconds +Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED, re-enabling deferred WiFi requests +Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED, re-enabling deferred WiFi requests +Mon Dec 10 11:55:48.093 IPC: ADDED XPC CLIENT CONNECTION [loginwindow (pid=101, euid=1651299376, egid=604256670)] +Mon Dec 10 11:55:48.093 Info: START MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.093 Info: START MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:55:48.105 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) +Mon Dec 10 11:56:07.629 Driver Discovery: _PMConnectionHandler: caps = +Mon Dec 10 11:56:07.629 AutoJoin: BEST CONNECTED SCAN CANCELLED on interface en0 +Mon Dec 10 11:56:07.629 AutoJoin: BACKGROUND SCAN CANCELLED on interface en0 +Mon Dec 10 11:56:07.629 AutoJoin: Auto-join retry cancelled on interface en0 +Mon Dec 10 11:56:07.629 Offload: _tcpKeepAliveActive: TCP keep-alive is active. +Mon Dec 10 11:56:07.637 P2P: _changeInterfaceFlags: Marking p2p0 down +Mon Dec 10 11:56:07.637 Info: SleepAcknowledgementCheckForHostAP: Checking sleep readiness for HostAP +Mon Dec 10 11:56:07.637 SleepAcknowledgementCheck: Checking sleep readiness +Mon Dec 10 11:56:07.637 SleepAcknowledgementCheck: Delaying sleep acknowledgement because of VifUp +Mon Dec 10 11:56:07.638 _interfaceFlagsChanged: KEV_DL_SIFFLAGS received for p2p0 [flags=0xffff8802 (down)]. +Mon Dec 10 11:56:07.638 _interfaceFlagsChanged: Flags changed for p2p0 (0xffff8843 -> 0xffff8802) (down). +Mon Dec 10 11:56:07.638 P2P: _deviceInterfaceMarkedDown: p2p0 marked down +Mon Dec 10 11:56:07.638 P2P: _removeTimeoutForActionAndParam: Attempting to remove 'Stop GO' action (param = 0x0) +Mon Dec 10 11:56:07.638 P2P: _removeTimeoutForActionAndParam: Attempting to remove 'Scan' action (param = 0x0) +Mon Dec 10 11:56:07.638 P2P: _p2pSupervisorEventRunLoopCallback: Mark down complete for p2p0 +Mon Dec 10 11:56:07.638 SleepAcknowledgementCheck: Checking sleep readiness +Mon Dec 10 11:56:07.638 WoW: SleepAcknowledgementCheck: Checking if auto-join is in progress before sleep acknowledgement +Mon Dec 10 11:56:07.638 WoW: SleepAcknowledgementDelayForAutoJoinAttempt_block_invoke: AUTO-JOIN attempt complete for en0, re-checking sleep readiness +Mon Dec 10 11:56:07.638 SleepAcknowledgementCheck: Checking sleep readiness +Mon Dec 10 11:56:07.639 WoW: _wowEnabled: WoW is active on en0 +Mon Dec 10 11:56:07.639 WoW: wowExchangeRequiredForNode: WoW exchange not required for en0 +Mon Dec 10 11:56:07.639 _acknowledgeSleepEvent: Acknowledging sleep event +Mon Dec 10 11:56:07.639 Info: PRIORITY LOCK ADDED [client=airportd, type=4, interface=(null), priority=7] +Mon Dec 10 11:56:07.640 AutoJoin: Auto-join retry cancelled on interface en0 +Mon Dec 10 11:56:07.640 Info: -[CWXPCSubsystem resetAutoJoinDisableState]_block_invoke: Auto-join disabled state reset +Mon Dec 10 11:56:17.640 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=(null), priority=7] +Mon Dec 10 12:04:46.023 Driver Discovery: _PMConnectionHandler: caps = CPU Net Disk Early +Mon Dec 10 12:04:46.023 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:46.023 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on awdl0 +Mon Dec 10 12:04:46.023 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:46.023 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on p2p0 +Mon Dec 10 12:04:46.023 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:46.023 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on en0 +Mon Dec 10 12:04:46.023 Driver Discovery: _PMConnectionHandler: DARK WAKE with maintenance SSID, performing maintenance wake auto-join for interface en0 +Mon Dec 10 12:04:46.023 AutoJoin: AUTO-JOIN trigger requested (Maintenance Wake) +Mon Dec 10 12:04:46.026 Info: psCallback: powerSource = AC Power +Mon Dec 10 12:04:46.026 Info: psCallback: set powersave disabled on en0 +Mon Dec 10 12:04:46.331 BTC: BluetoothCoexStatusMonitoringCallback: Bluetooth Status Notification +Mon Dec 10 12:04:46.331 BTC: BluetoothCoexStatusNotificationProcess: BT: ON, Num HID Devices is <0>, Num SCO Devices is <0>, Num A2DP Devices is <0>, Bluetooth Bandwidth Utilization is <3>, LWM <5>, HWM <26> +Mon Dec 10 12:04:46.332 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:04:46.332 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:04:46.332 Info: AWDL real time mode ended +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:04:46.332 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:04:46.332 {type = mutable dict, count = 2, +Mon Dec 10 12:04:46.332 entries => +Mon Dec 10 12:04:46.332 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.332 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.332 } +Mon Dec 10 12:04:46.332 +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:04:46.332 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:04:46.333 AutoJoin: user: patidar +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:04:46.333 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:04:46.333 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:04:46.333 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:04:46.333 Info: AWDL real time mode ended +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:04:46.333 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:04:46.333 {type = mutable dict, count = 2, +Mon Dec 10 12:04:46.333 entries => +Mon Dec 10 12:04:46.333 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.333 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.333 } +Mon Dec 10 12:04:46.333 +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:04:46.333 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:04:46.334 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 12:04:46.334 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:04:46.334 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:04:46.334 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:04:46.334 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:04:46.334 Info: -[CWXPCSubsystem clearScanCacheWithInterfaceName:connection:error:]: Clearing scan cache for interface en0 +Mon Dec 10 12:04:46.334 Info: -[CWXPCSubsystem clearScanCacheWithInterfaceName:connection:error:]: Clearing family+driver scan cache for interface en0 +Mon Dec 10 12:04:46.334 AutoJoin: AUTO-JOIN STARTED for interface en0 (Maintenance Wake) +Mon Dec 10 12:04:46.334 Info: PRIORITY LOCK ADDED [client=airportd, type=4, interface=en0, priority=7] +Mon Dec 10 12:04:46.334 AutoJoin: NOT RECOVERY MODE => continuing +Mon Dec 10 12:04:46.334 Info: scan cache updated +Mon Dec 10 12:04:46.335 AutoJoin: NOT LOGINWINDOW MODE 802.1X => continuing +Mon Dec 10 12:04:46.335 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:04:46.335 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:04:46.335 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:04:46.335 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:04:46.335 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:04:46.336 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:04:46.336 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:04:46.336 {type = mutable dict, count = 2, +Mon Dec 10 12:04:46.336 entries => +Mon Dec 10 12:04:46.336 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.336 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.336 } +Mon Dec 10 12:04:46.336 +Mon Dec 10 12:04:46.336 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:04:46.336 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:04:46.336 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:04:46.336 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:04:46.336 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:04:46.336 Info: REQUEST DEFERRED minimum=7 [client=locationd, type=4, interface=en0, priority=5] +Mon Dec 10 12:04:46.336 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:04:46.336 P2P: _interfaceLinkChanged: Stopping IPv6 link local on awdl0 +Mon Dec 10 12:04:46.336 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:04:46.337 Info: AWDL real time mode ended +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:04:46.337 AutoJoin: Reviewing the preferred networks list +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:04:46.337 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:04:46.337 AutoJoin: Adding network ['XT1635-02 9086' (wifi.ssid.5854313633352d30322039303836) - WPA2 Personal] +Mon Dec 10 12:04:46.337 AutoJoin: Adding network ['Prawin' (wifi.ssid.50726177696e) - WPA/WPA2 Personal] +Mon Dec 10 12:04:46.337 AutoJoin: Adding network ['Tilak' (wifi.ssid.54696c616b) - WPA Personal] +Mon Dec 10 12:04:46.337 AutoJoin: Ignoring disabled network ['Redmi' (wifi.ssid.5265646d69) - Open] +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:04:46.337 {type = mutable dict, count = 2, +Mon Dec 10 12:04:46.337 entries => +Mon Dec 10 12:04:46.337 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.337 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.337 } +Mon Dec 10 12:04:46.337 +Mon Dec 10 12:04:46.337 AutoJoin: Adding network ['twdata' (wifi.ssid.747764617461) - WPA2 Enterprise] +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:04:46.337 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:04:46.337 AutoJoin: Adding network ['401UnauthorizedAccess' (wifi.ssid.343031556e617574686f72697a6564416363657373) - WPA2 Personal] +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['Ayush' (wifi.ssid.4179757368) - WPA/WPA2 Personal] +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['twguest' (wifi.ssid.74776775657374) - WPA2 Personal] +Mon Dec 10 12:04:46.338 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:04:46.338 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:04:46.338 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:04:46.338 AutoJoin: Ignoring disabled network ['Illiad' (wifi.ssid.496c6c696164) - WPA2 Personal] +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['NOSI' (wifi.ssid.4e4f5349) - WPA2 Enterprise] +Mon Dec 10 12:04:46.338 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['Network Not Found' (wifi.ssid.4e6574776f726b204e6f7420466f756e64) - WPA2 Personal] +Mon Dec 10 12:04:46.338 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['403Forbidden' (wifi.ssid.343033466f7262696464656e) - WPA2 Personal] +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['NETGEAR' (wifi.ssid.4e455447454152) - WPA2 Personal] +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['No' (wifi.ssid.4e6f) - WPA2 Personal] +Mon Dec 10 12:04:46.338 Info: AWDL real time mode ended +Mon Dec 10 12:04:46.338 AutoJoin: Adding network ['gruppopam' (wifi.ssid.67727570706f70616d) - WPA2 Personal] +Mon Dec 10 12:04:46.338 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:04:46.339 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:04:46.340 AutoJoin: NOT LINK DOWN RECOVERY => continuing +Mon Dec 10 12:04:46.340 AutoJoin: MAINTENANCE WAKE => will attempt to restore maintenance wake association +Mon Dec 10 12:04:46.340 AutoJoin: Already associated to 'NOSI' (<4e4f5349>), will not continue auto-join +Mon Dec 10 12:04:46.340 AutoJoin: AUTO-JOIN COMPLETED for interface en0, took 0.0056 seconds, returned result 'success', error [NO ERROR] +Mon Dec 10 12:04:46.340 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=7] +Mon Dec 10 12:04:46.340 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:04:46.340 Info: REQUEST UN-DEFERRED minimum=-9223372036854775808 [client=locationd, type=4, interface=en0, priority=5] +Mon Dec 10 12:04:46.340 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:04:46.340 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:04:46.340 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:04:46.341 {type = mutable dict, count = 2, +Mon Dec 10 12:04:46.341 entries => +Mon Dec 10 12:04:46.341 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.341 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:46.341 } +Mon Dec 10 12:04:46.341 +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:04:46.341 Driver Event: _bsd_80211_event_callback: POWER_CHANGED (en0) +Mon Dec 10 12:04:46.341 BTC: BluetoothCoexistenceTickle: Triggering BTC status notification +Mon Dec 10 12:04:46.341 P2P: _bsd_80211_event_callback: Marking p2p0 up because en0 was powered on +Mon Dec 10 12:04:46.341 P2P: _changeInterfaceFlags: Marking p2p0 up +Mon Dec 10 12:04:46.342 Info: -[CWXPCSubsystem performScanWithChannelList:ssidList:legacyScanSSID:dwellTimeOverride:includeHiddenNetworks:mergeScanResults:interfaceName:connection:error:]: Failed to perform Wi-Fi scan, returned error code 16, will try again in 200 ms +Mon Dec 10 12:04:46.342 P2P: AwdlNodeShouldBeMarkedUp: awdl0 should be marked up +Mon Dec 10 12:04:46.342 P2P: _bsd_80211_event_callback: Marking awdl0 up because en0 was powered on +Mon Dec 10 12:04:46.342 P2P: _interfaceLinkChanged: Starting IPv6 link local on awdl0 +Mon Dec 10 12:04:46.342 Info: power changed +Mon Dec 10 12:04:46.342 _interfaceFlagsChanged: KEV_DL_SIFFLAGS received for p2p0 [flags=0xffff8843 (up)]. +Mon Dec 10 12:04:46.342 _interfaceFlagsChanged: Flags changed for p2p0 (0xffff8802 -> 0xffff8843) (up). +Mon Dec 10 12:04:46.342 Driver Discovery: _interfaceFlagsChanged: p2p0 transitioning from down to up. +Mon Dec 10 12:04:46.342 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:04:46.342 P2P: _deviceInterfaceMarkedUp: p2p0 marked up +Mon Dec 10 12:04:46.342 P2P: _deviceInterfaceMarkedUp: on p2p0 Num Advertised[0] +Mon Dec 10 12:04:46.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:04:46.343 BTC: BluetoothCoexStatusMonitoringCallback: Bluetooth Status Notification +Mon Dec 10 12:04:46.343 BTC: BluetoothCoexStatusNotificationProcess: BT: ON, Num HID Devices is <0>, Num SCO Devices is <0>, Num A2DP Devices is <0>, Bluetooth Bandwidth Utilization is <3>, LWM <5>, HWM <26> +Mon Dec 10 12:04:46.344 AutoJoin: BACKGROUND SCAN SCHEDULED on interface en0 in 60.0s with SSID list (null), remaining SSID list (null) +Mon Dec 10 12:04:46.501 P2P: _interfaceLinkChanged: Starting IPv6 link local on awdl0 +Mon Dec 10 12:04:46.501 Driver Event: _bsd_80211_event_callback: CHANNEL_SWITCH (en0) +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/ProfileID' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEP40' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID_STR' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AirPlay' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AutoJoinTimestamp' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BSSID' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CHANNEL' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Busy' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Power Status' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEPOPENSYSTEM' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CachedScanRecord' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/UserMode8021X' +Mon Dec 10 12:04:46.509 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BusyUI' +Mon Dec 10 12:04:47.037 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:04:47.041 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:04:47.041 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:04:47.041 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.041 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.041 [channelNumber=3(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.041 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.041 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.041 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:04:47.041 )} took 0.7073 seconds, returned 11 results +Mon Dec 10 12:04:47.041 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:04:47.041 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:04:47.041 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:04:47.041 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:04:47.042 {type = mutable dict, count = 2, +Mon Dec 10 12:04:47.042 entries => +Mon Dec 10 12:04:47.042 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:47.042 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:04:47.042 } +Mon Dec 10 12:04:47.042 +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:04:47.042 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:04:47.288 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:04:47.290 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:04:47.290 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.290 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.290 [channelNumber=9(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.290 [channelNumber=10(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.290 [channelNumber=11(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.290 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:04:47.290 )} took 0.2476 seconds, returned 3 results +Mon Dec 10 12:04:47.291 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:04:47.559 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:04:47.562 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:04:47.562 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:04:47.562 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:47.562 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:04:47.562 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:04:47.562 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:04:47.562 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:04:47.562 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:04:47.562 )} took 0.2715 seconds, returned 11 results +Mon Dec 10 12:04:47.563 Info: scan cache updated +Mon Dec 10 12:04:47.807 Driver Discovery: _PMConnectionHandler: caps = CPU Net Disk +Mon Dec 10 12:04:47.808 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:47.808 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on awdl0 +Mon Dec 10 12:04:47.808 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:47.808 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on p2p0 +Mon Dec 10 12:04:47.809 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:47.809 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on en0 +Mon Dec 10 12:04:47.809 Driver Discovery: _PMConnectionHandler: DARK WAKE with maintenance SSID, performing maintenance wake auto-join for interface en0 +Mon Dec 10 12:04:47.811 AutoJoin: AUTO-JOIN trigger requested (Maintenance Wake) +Mon Dec 10 12:04:47.811 Info: psCallback: powerSource = AC Power +Mon Dec 10 12:04:47.811 Info: psCallback: set powersave disabled on en0 +Mon Dec 10 12:04:47.815 IPC: INVALIDATED XPC CLIENT CONNECTION [loginwindow (pid=101, euid=1651299376, egid=604256670)] +Mon Dec 10 12:04:47.815 WoW: WoW not supported on awdl0, skipping +Mon Dec 10 12:04:47.815 WoW: WoW not supported on p2p0, skipping +Mon Dec 10 12:04:47.815 AutoJoin: user: patidar +Mon Dec 10 12:04:47.816 Info: -[CWXPCSubsystem clearScanCacheWithInterfaceName:connection:error:]: Clearing scan cache for interface en0 +Mon Dec 10 12:04:47.816 Info: -[CWXPCSubsystem clearScanCacheWithInterfaceName:connection:error:]: Clearing family+driver scan cache for interface en0 +Mon Dec 10 12:04:47.816 Info: PRIORITY LOCK ADDED [client=airportd, type=4, interface=en0, priority=7] +Mon Dec 10 12:04:47.816 AutoJoin: AUTO-JOIN STARTED for interface en0 (Maintenance Wake) +Mon Dec 10 12:04:47.816 AutoJoin: NOT RECOVERY MODE => continuing +Mon Dec 10 12:04:47.818 AutoJoin: NOT LOGINWINDOW MODE 802.1X => continuing +Mon Dec 10 12:04:47.821 AutoJoin: Reviewing the preferred networks list +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['XT1635-02 9086' (wifi.ssid.5854313633352d30322039303836) - WPA2 Personal] +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['Prawin' (wifi.ssid.50726177696e) - WPA/WPA2 Personal] +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['Tilak' (wifi.ssid.54696c616b) - WPA Personal] +Mon Dec 10 12:04:47.821 AutoJoin: Ignoring disabled network ['Redmi' (wifi.ssid.5265646d69) - Open] +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['twdata' (wifi.ssid.747764617461) - WPA2 Enterprise] +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['401UnauthorizedAccess' (wifi.ssid.343031556e617574686f72697a6564416363657373) - WPA2 Personal] +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['Ayush' (wifi.ssid.4179757368) - WPA/WPA2 Personal] +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['twguest' (wifi.ssid.74776775657374) - WPA2 Personal] +Mon Dec 10 12:04:47.821 AutoJoin: Ignoring disabled network ['Illiad' (wifi.ssid.496c6c696164) - WPA2 Personal] +Mon Dec 10 12:04:47.821 AutoJoin: Adding network ['NOSI' (wifi.ssid.4e4f5349) - WPA2 Enterprise] +Mon Dec 10 12:04:47.822 AutoJoin: Adding network ['Network Not Found' (wifi.ssid.4e6574776f726b204e6f7420466f756e64) - WPA2 Personal] +Mon Dec 10 12:04:47.822 AutoJoin: Adding network ['403Forbidden' (wifi.ssid.343033466f7262696464656e) - WPA2 Personal] +Mon Dec 10 12:04:47.822 AutoJoin: Adding network ['NETGEAR' (wifi.ssid.4e455447454152) - WPA2 Personal] +Mon Dec 10 12:04:47.822 AutoJoin: Adding network ['No' (wifi.ssid.4e6f) - WPA2 Personal] +Mon Dec 10 12:04:47.822 AutoJoin: Adding network ['gruppopam' (wifi.ssid.67727570706f70616d) - WPA2 Personal] +Mon Dec 10 12:04:47.823 AutoJoin: NOT LINK DOWN RECOVERY => continuing +Mon Dec 10 12:04:47.823 AutoJoin: MAINTENANCE WAKE => will attempt to restore maintenance wake association +Mon Dec 10 12:04:47.823 AutoJoin: Already associated to 'NOSI' (<4e4f5349>), will not continue auto-join +Mon Dec 10 12:04:47.823 AutoJoin: AUTO-JOIN COMPLETED for interface en0, took 0.0074 seconds, returned result 'success', error [NO ERROR] +Mon Dec 10 12:04:47.823 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=7] +Mon Dec 10 12:04:48.030 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:04:48.031 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:04:48.032 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:04:48.032 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:04:48.032 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:48.032 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:04:48.032 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:04:48.032 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:04:48.032 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:04:48.032 )} took 0.4685 seconds, returned 8 results +Mon Dec 10 12:04:48.177 Driver Discovery: _PMConnectionHandler: caps = CPU Video Audio Net Disk +Mon Dec 10 12:04:48.177 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:48.177 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on awdl0 +Mon Dec 10 12:04:48.178 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:48.178 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on p2p0 +Mon Dec 10 12:04:48.178 Info: _systemWokenByWiFi: System wake reason: , was not woken by WiFi +Mon Dec 10 12:04:48.178 Info: _updateWakeLimitCounters: TCP keep alive timed out 0 times on en0 +Mon Dec 10 12:04:48.178 AutoJoin: -[CWXPCInterfaceContext clearAllProblematicNetworks]_block_invoke: Unblacklisting all networks. +Mon Dec 10 12:04:48.178 AutoJoin: -[CWXPCInterfaceContext clearAllNetworksBlacklistedForScanOffload]_block_invoke: Clearing all networks blacklisted for scan offload. +Mon Dec 10 12:04:48.178 Offload: _tcpKeepAliveActive: TCP keep-alive is active. +Mon Dec 10 12:04:48.178 AutoJoin: airportdProcessFullWake: Received full wake event for en0 +Mon Dec 10 12:04:48.178 AutoJoin: airportdProcessFullWake: Invoking auto-join for full wake event for en0 +Mon Dec 10 12:04:48.178 Info: __enableTemporarilyDisabledNetworks: Attempting to re-enable any temporarily-disabled network profiles +Mon Dec 10 12:04:48.178 Info: psCallback: powerSource = AC Power +Mon Dec 10 12:04:48.178 Info: psCallback: set powersave disabled on en0 +Mon Dec 10 12:04:48.180 AutoJoin: AUTO-JOIN trigger requested (Full Wake) +Mon Dec 10 12:04:48.180 AutoJoin: BEST CONNECTED SCAN SCHEDULED on interface en0 in 20.0s for network 'NOSI' +Mon Dec 10 12:04:48.181 AutoJoin: user: patidar +Mon Dec 10 12:04:48.182 AutoJoin: BACKGROUND SCAN SCHEDULED on interface en0 in 60.0s with SSID list (null), remaining SSID list (null) +Mon Dec 10 12:04:48.182 Info: -[CWXPCSubsystem clearScanCacheWithInterfaceName:connection:error:]: Clearing scan cache for interface en0 +Mon Dec 10 12:04:48.182 Info: -[CWXPCSubsystem clearScanCacheWithInterfaceName:connection:error:]: Clearing family+driver scan cache for interface en0 +Mon Dec 10 12:04:48.182 Info: PRIORITY LOCK ADDED [client=airportd, type=4, interface=en0, priority=7] +Mon Dec 10 12:04:48.182 AutoJoin: AUTO-JOIN STARTED for interface en0 (Full Wake) +Mon Dec 10 12:04:48.182 AutoJoin: NOT RECOVERY MODE => continuing +Mon Dec 10 12:04:48.183 AutoJoin: NOT LOGINWINDOW MODE 802.1X => continuing +Mon Dec 10 12:04:48.185 AutoJoin: Reviewing the preferred networks list +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['XT1635-02 9086' (wifi.ssid.5854313633352d30322039303836) - WPA2 Personal] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['Prawin' (wifi.ssid.50726177696e) - WPA/WPA2 Personal] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['Tilak' (wifi.ssid.54696c616b) - WPA Personal] +Mon Dec 10 12:04:48.185 AutoJoin: Ignoring disabled network ['Redmi' (wifi.ssid.5265646d69) - Open] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['twdata' (wifi.ssid.747764617461) - WPA2 Enterprise] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['401UnauthorizedAccess' (wifi.ssid.343031556e617574686f72697a6564416363657373) - WPA2 Personal] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['Ayush' (wifi.ssid.4179757368) - WPA/WPA2 Personal] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['twguest' (wifi.ssid.74776775657374) - WPA2 Personal] +Mon Dec 10 12:04:48.185 AutoJoin: Ignoring disabled network ['Illiad' (wifi.ssid.496c6c696164) - WPA2 Personal] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['NOSI' (wifi.ssid.4e4f5349) - WPA2 Enterprise] +Mon Dec 10 12:04:48.185 AutoJoin: Adding network ['Network Not Found' (wifi.ssid.4e6574776f726b204e6f7420466f756e64) - WPA2 Personal] +Mon Dec 10 12:04:48.186 AutoJoin: Adding network ['403Forbidden' (wifi.ssid.343033466f7262696464656e) - WPA2 Personal] +Mon Dec 10 12:04:48.186 AutoJoin: Adding network ['NETGEAR' (wifi.ssid.4e455447454152) - WPA2 Personal] +Mon Dec 10 12:04:48.186 AutoJoin: Adding network ['No' (wifi.ssid.4e6f) - WPA2 Personal] +Mon Dec 10 12:04:48.186 AutoJoin: Adding network ['gruppopam' (wifi.ssid.67727570706f70616d) - WPA2 Personal] +Mon Dec 10 12:04:48.187 AutoJoin: NOT LINK DOWN RECOVERY => continuing +Mon Dec 10 12:04:48.187 AutoJoin: NOT MAINTENANCE WAKE => continuing +Mon Dec 10 12:04:48.189 AutoJoin: No known colocated network for preferred network wifi.ssid.4e4f5349 +Mon Dec 10 12:04:48.189 AutoJoin: NOT MORE-PREFERRED USER MODE 802.1X NETWORKS => continuing +Mon Dec 10 12:04:48.189 AutoJoin: NOT SYSTEM MODE 802.1X => continuing +Mon Dec 10 12:04:48.189 AutoJoin: NOT WOW => continuing +Mon Dec 10 12:04:48.189 AutoJoin: NOT Power On / Reinit => continuing +Mon Dec 10 12:04:48.189 AutoJoin: NOT PNL Changed => continuing +Mon Dec 10 12:04:48.189 AutoJoin: AUTOJOIN => auto-join using the entire preferred networks list +Mon Dec 10 12:04:48.189 AutoJoin: Already associated to 'NOSI' (<4e4f5349>), will not continue auto-join +Mon Dec 10 12:04:48.189 AutoJoin: AUTO-JOIN COMPLETED for interface en0, took 0.0069 seconds, returned result 'success', error [NO ERROR] +Mon Dec 10 12:04:48.189 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=7] +Mon Dec 10 12:04:48.216 Info: machine wake +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.216 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.217 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.217 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.217 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.217 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.218 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.218 Info: START MONITORING EVENT request received from pid 383 (WiFiProxy) +Mon Dec 10 12:04:48.225 Info: -[AirPortExtraImplementation initBackend]_block_invoke: _isBusy 0 +Mon Dec 10 12:04:48.226 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:04:48.227 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:04:48.344 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:04:48.344 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:04:48.344 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:04:48.344 )} took 0.3106 seconds, returned 8 results +Mon Dec 10 12:04:48.344 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:04:48.345 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:04:48.957 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 12:04:48.957 Scan: locationd requested a live scan less than 10 seconds after previous request (2.6229s) returning cached scan results +Mon Dec 10 12:04:48.969 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 12:04:48.969 Scan: locationd requested a live scan less than 10 seconds after previous request (2.6358s) returning cached scan results +Mon Dec 10 12:04:49.319 Info: ACQUIRE BT PAGING LOCK request received from pid 106 (bluetoothd) +Mon Dec 10 12:04:49.320 Info: BT PAGING LOCK GRANTED immediately, auto-join has had a chance to run since wake +Mon Dec 10 12:04:49.320 Info: BT PAGING LOCK GRANTED after 0.0 seconds +Mon Dec 10 12:04:49.321 Info: RELINQUISH BT PAGING LOCK request received from pid 106 (bluetoothd) +Mon Dec 10 12:04:49.321 Info: BT PAGING LOCK RELINQUISHED after 0.0 seconds +Mon Dec 10 12:04:49.321 Info: BT PAGING LOCK RELINQUISHED, re-enabling deferred WiFi requests +Mon Dec 10 12:05:06.345 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:06.345 Info: _bsd_80211_event_callback: link quality: RSSI=-56 dBm TxRate=162 Mbps +Mon Dec 10 12:05:06.345 Info: link quality changed +Mon Dec 10 12:05:08.180 AutoJoin: BEST CONNECTED SCAN request on interface en0 for network 'XT1635-02 9086' +Mon Dec 10 12:05:08.281 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:08.284 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:08.284 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.284 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.284 [channelNumber=3(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:08.284 )} took 0.1034 seconds, returned 7 results +Mon Dec 10 12:05:08.284 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:08.284 Info: scan cache updated +Mon Dec 10 12:05:08.383 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:08.386 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:08.386 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.386 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.386 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:08.386 )} took 0.1023 seconds, returned 7 results +Mon Dec 10 12:05:08.386 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:08.486 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:08.488 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:08.488 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.488 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.488 [channelNumber=9(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:08.488 )} took 0.1016 seconds, returned 1 results +Mon Dec 10 12:05:08.488 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:08.589 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:08.592 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:08.592 [channelNumber=10(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.592 [channelNumber=11(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.592 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:08.592 )} took 0.1036 seconds, returned 4 results +Mon Dec 10 12:05:08.592 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:08.719 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:08.722 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:08.722 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.722 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:08.722 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:08.722 )} took 0.1298 seconds, returned 5 results +Mon Dec 10 12:05:08.722 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:08.826 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:08.828 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:08.828 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:08.828 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:08.828 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:08.828 )} took 0.1067 seconds, returned 4 results +Mon Dec 10 12:05:08.829 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:08.929 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:08.932 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:08.932 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:08.932 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:08.932 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:08.932 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:08.932 )} took 0.1035 seconds, returned 7 results +Mon Dec 10 12:05:09.215 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:09.217 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:09.217 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:09.217 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:09.217 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:09.217 )} took 0.2851 seconds, returned 5 results +Mon Dec 10 12:05:09.217 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:09.482 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:09.485 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:09.485 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:09.485 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:09.485 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:09.485 )} took 0.2679 seconds, returned 8 results +Mon Dec 10 12:05:09.486 Info: scan cache updated +Mon Dec 10 12:05:09.487 Roam: ROAMING PROFILES already set to AC POWER for 2.4GHz on en0 +Mon Dec 10 12:05:09.487 Roam: ROAMING PROFILES already set to AC POWER for 5GHz on en0 +Mon Dec 10 12:05:09.487 AutoJoin: BEST CONNECTED ROAM triggered +Mon Dec 10 12:05:09.488 Driver Event: _bsd_80211_event_callback: APPLE80211_M_ROAM_START (en0) +Mon Dec 10 12:05:09.488 Info: Roaming started on interface en0 +Mon Dec 10 12:05:09.488 Info: PRIORITY LOCK ADDED [client=airportd, type=4, interface=en0, priority=5] +Mon Dec 10 12:05:09.490 Info: -[CWXPCInterfaceContext __setAWDLOperatingMode:interface:error:]: attempting to set AWDL mode to 2 +Mon Dec 10 12:05:09.490 Info: SUSPEND AWDL for interface en0, timeout=10.0s, reason=Roam, token=2686 +Mon Dec 10 12:05:10.934 Driver Event: _bsd_80211_event_callback: APPLE80211_M_ROAM_END (en0) +Mon Dec 10 12:05:10.934 Info: Roaming ended on interface en0 +Mon Dec 10 12:05:10.935 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:05:10.935 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:05:10.935 Info: -[CWXPCInterfaceContext setRoamInProgress:reason:]_block_invoke: roam status metric data: CWAWDMetricRoamStatus: status:3 security: 4 profile:5 origin:<6cf37f>(-56) target:<6cf37f>(-57) latency:1.445767s +Mon Dec 10 12:05:10.935 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90046 +Mon Dec 10 12:05:10.935 Info: RESUME AWDL for interface en0, reason=Roam token=2686 +Mon Dec 10 12:05:10.935 Info: PRIORITY LOCK REMOVED [client=airportd, type=4, interface=en0, priority=5] +Mon Dec 10 12:05:10.936 Info: -[CWXPCInterfaceContext __setAWDLOperatingMode:interface:error:]: attempting to set AWDL mode to 0 +Mon Dec 10 12:05:11.354 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:11.354 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=243 Mbps +Mon Dec 10 12:05:11.354 Info: link quality changed +Mon Dec 10 12:05:16.355 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:16.355 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 12:05:16.356 Info: link quality changed +Mon Dec 10 12:05:21.359 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:21.359 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=300 Mbps +Mon Dec 10 12:05:21.360 Info: link quality changed +Mon Dec 10 12:05:29.363 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:29.363 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=216 Mbps +Mon Dec 10 12:05:29.364 Info: link quality changed +Mon Dec 10 12:05:38.367 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:38.367 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=243 Mbps +Mon Dec 10 12:05:38.368 Info: link quality changed +Mon Dec 10 12:05:48.180 AutoJoin: BACKGROUND SCAN request on interface en0 with SSID list (null) +Mon Dec 10 12:05:48.181 Scan: Cache-assisted scan request on channel 1 does not require a live scan +Mon Dec 10 12:05:48.181 Scan: Cache-assisted scan request on channel 2 does not require a live scan +Mon Dec 10 12:05:48.181 Scan: Cache-assisted scan request on channel 3 does not require a live scan +Mon Dec 10 12:05:48.181 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.181 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.181 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.181 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.181 [channelNumber=3(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.181 )} took 0.0006 seconds, returned 7 results +Mon Dec 10 12:05:48.181 Scan: Cache-assisted scan request on channel 4 does not require a live scan +Mon Dec 10 12:05:48.181 Scan: Cache-assisted scan request on channel 5 does not require a live scan +Mon Dec 10 12:05:48.182 Scan: Cache-assisted scan request on channel 6 does not require a live scan +Mon Dec 10 12:05:48.182 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.182 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.182 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.182 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.182 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.182 )} took 0.0006 seconds, returned 7 results +Mon Dec 10 12:05:48.182 Scan: Cache-assisted scan request on channel 7 does not require a live scan +Mon Dec 10 12:05:48.182 Scan: Cache-assisted scan request on channel 8 does not require a live scan +Mon Dec 10 12:05:48.182 Scan: Cache-assisted scan request on channel 9 does not require a live scan +Mon Dec 10 12:05:48.182 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.182 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.182 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.182 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.182 [channelNumber=9(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.182 )} took 0.0003 seconds, returned 1 results +Mon Dec 10 12:05:48.182 Scan: Cache-assisted scan request on channel 10 does not require a live scan +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 11 does not require a live scan +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 12 does not require a live scan +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.183 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.183 [channelNumber=10(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.183 [channelNumber=11(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.183 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.183 )} took 0.0007 seconds, returned 4 results +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 13 does not require a live scan +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 36 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 40 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.184 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.184 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.184 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:48.184 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:48.184 )} took 0.0007 seconds, returned 5 results +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 44 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 48 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 149 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.185 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.185 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:48.185 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:48.185 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.185 )} took 0.0006 seconds, returned 4 results +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 153 does not require a live scan +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 157 does not require a live scan +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 161 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.186 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.186 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:48.186 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.186 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:48.186 )} took 0.0010 seconds, returned 7 results +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 165 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 52 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 56 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.187 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.187 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.187 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:48.187 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:48.187 )} took 0.0008 seconds, returned 5 results +Mon Dec 10 12:05:48.187 Scan: Cache-assisted scan request on channel 60 does not require a live scan +Mon Dec 10 12:05:48.187 Scan: Cache-assisted scan request on channel 64 does not require a live scan +Mon Dec 10 12:05:48.188 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.188 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.188 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:48.188 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:48.188 )} took 0.0013 seconds, returned 8 results +Mon Dec 10 12:05:50.375 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:50.375 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=216 Mbps +Mon Dec 10 12:05:50.376 Info: link quality changed +Mon Dec 10 12:05:55.378 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:55.378 Info: _bsd_80211_event_callback: link quality: RSSI=-56 dBm TxRate=243 Mbps +Mon Dec 10 12:05:55.379 Info: link quality changed +Mon Dec 10 12:06:28.759 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:06:28.763 Info: AWDL started +Mon Dec 10 12:06:28.763 Driver Event: _bsd_80211_event_callback: CHANNEL_SWITCH (en0) +Mon Dec 10 12:06:28.765 Roam: ROAMING PROFILES updated to SINGLE-BAND, SINGLE-AP for 2.4GHz on en0 +Mon Dec 10 12:06:28.765 Roam: ROAMING PROFILES updated to SINGLE-BAND, SINGLE-AP for 5GHz on en0 +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/ProfileID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEP40' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID_STR' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AirPlay' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AutoJoinTimestamp' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BSSID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CHANNEL' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Busy' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Power Status' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEPOPENSYSTEM' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CachedScanRecord' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/UserMode8021X' +Mon Dec 10 12:06:28.771 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BusyUI' +Mon Dec 10 12:06:28.945 Info: INTERFACE STATE INFO request received from pid 362 (sharingd) +Mon Dec 10 12:06:28.946 Info: -[CWXPCInterfaceContext __copyMACAddressForInterface:]: MAC address: <82cd0ac3 bf73> +Mon Dec 10 12:06:30.064 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:30.151 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:30.151 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.137 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.138 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.832 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.832 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:48.742 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:48.742 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.121 P2P: _terminateGroupOwnerTimer: Terminate group owner timer notification received. +Mon Dec 10 12:06:49.267 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.267 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.793 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.793 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.931 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:06:50.931 Driver Event: _bsd_80211_event_callback: AWDL_STATISTICS (awdl0) +Mon Dec 10 12:06:50.932 Info: AWDL real time mode ended +Mon Dec 10 12:06:50.945 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:06:50.945 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:06:50.945 {type = mutable dict, count = 2, +Mon Dec 10 12:06:50.945 entries => +Mon Dec 10 12:06:50.945 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.945 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.945 } +Mon Dec 10 12:06:50.945 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:06:50.945 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:06:50.945 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:06:50.945 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.945 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:06:50.945 Info: AWDL real time mode ended +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:06:50.946 {type = mutable dict, count = 2, +Mon Dec 10 12:06:50.946 entries => +Mon Dec 10 12:06:50.946 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.946 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.946 } +Mon Dec 10 12:06:50.946 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:06:50.946 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:06:50.947 Info: AWDL ended +Mon Dec 10 12:06:50.951 Info: -[CWXPCInterfaceContext __submitAWDLUsageMetric:duration:]: AWDL usage metric data: CWAWDMetricAwdlUsageData: selfInfraChannel 40, peerInfraChannel 0, numOfPeers 6. Keys: Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90013 +Mon Dec 10 12:06:50.952 Info: INTERFACE STATE INFO request received from pid 362 (sharingd) +Mon Dec 10 12:06:50.952 Info: -[CWXPCInterfaceContext __copyMACAddressForInterface:]: MAC address: <82cd0ac3 bf73> +Mon Dec 10 12:06:50.953 Roam: ROAMING PROFILES updated to AC POWER for 2.4GHz on en0 +Mon Dec 10 12:06:50.953 Roam: ROAMING PROFILES updated to AC POWER for 5GHz on en0 +Mon Dec 10 12:06:53.680 Info: -[CWXPCSubsystem handleDeviceCountMetricQuery:]_block_invoke: DeviceCount metric data: CWAWDMetricDeviceCountData: timeSinceBoot: 1515466.360642, deviceCount: 1 +Mon Dec 10 12:06:53.684 Info: -[CWXPCSubsystem handleNetworkPrefsMetricQuery:]: NetworkPrefs metric data: CWAWDMetricNetworkPrefsData: preferred 15 hidden 0 open 1 wep 0 wpa 12 eap 2 +Mon Dec 10 12:06:53.684 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90004 +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90001 +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x9000d +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x9000b +Mon Dec 10 12:12:03.594 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:03.594 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=216 Mbps +Mon Dec 10 12:12:03.595 Info: link quality changed +Mon Dec 10 12:12:09.598 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:09.598 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=270 Mbps +Mon Dec 10 12:12:09.599 Info: link quality changed +Mon Dec 10 12:12:56.625 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:56.625 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 12:12:56.625 Info: link quality changed +Mon Dec 10 12:13:01.625 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:01.625 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=243 Mbps +Mon Dec 10 12:13:01.626 Info: link quality changed +Mon Dec 10 12:13:06.628 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:06.628 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 12:13:06.628 Info: link quality changed +Mon Dec 10 12:13:27.639 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:27.639 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=243 Mbps +Mon Dec 10 12:13:27.640 Info: link quality changed +Mon Dec 10 12:14:46.658 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 12:14:46.902 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:46.905 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:46.906 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:46.906 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=3(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:14:46.906 )} took 0.2478 seconds, returned 13 results +Mon Dec 10 12:14:46.906 Info: scan cache updated +Mon Dec 10 12:14:47.158 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:47.160 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:47.160 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=9(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=10(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=11(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:14:47.160 )} took 0.2532 seconds, returned 4 results +Mon Dec 10 12:14:47.161 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:47.433 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:47.436 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:47.437 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:47.437 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.437 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:14:47.437 )} took 0.2763 seconds, returned 11 results +Mon Dec 10 12:14:47.778 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:47.781 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:47.782 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:47.782 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.782 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.782 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.782 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.782 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:14:47.782 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:14:47.782 )} took 0.3436 seconds, returned 9 results +Mon Dec 10 12:14:48.052 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:48.055 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:48.055 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:14:48.055 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:14:48.055 )} took 0.2714 seconds, returned 6 results +Mon Dec 10 12:14:48.055 Info: QUERY SCAN CACHE request received from pid 92 (locationd) diff --git a/tests/fixtures/pr/test_num_page.log b/tests/fixtures/pr/test_num_page.log new file mode 100644 index 000000000..da467dce2 --- /dev/null +++ b/tests/fixtures/pr/test_num_page.log @@ -0,0 +1,112 @@ +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed diff --git a/tests/fixtures/pr/test_num_page_2.log.expected b/tests/fixtures/pr/test_num_page_2.log.expected new file mode 100644 index 000000000..dae437ef8 --- /dev/null +++ b/tests/fixtures/pr/test_num_page_2.log.expected @@ -0,0 +1,132 @@ + + +{last_modified_time} test_num_page.log Page 1 + + + 1 ntation processAirPortStateChanges]: pppConnectionState 0 + 2 Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 3 Mon Dec 10 11:42:56.705 Info: 802.1X changed + 4 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 5 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 6 Mon Dec 10 11:42:56.854 Info: 802.1X changed + 7 Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 8 Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 9 Mon Dec 10 11:42:57.002 Info: 802.1X changed +10 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +11 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +12 Mon Dec 10 11:42:57.152 Info: 802.1X changed +13 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +14 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +15 Mon Dec 10 11:42:57.302 Info: 802.1X changed +16 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +17 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +18 Mon Dec 10 11:42:57.449 Info: 802.1X changed +19 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +20 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +21 Mon Dec 10 11:42:57.600 Info: 802.1X changed +22 Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +23 Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +24 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +25 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +26 Mon Dec 10 11:42:57.749 Info: 802.1X changed +27 Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +28 Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +29 Mon Dec 10 11:42:57.896 Info: 802.1X changed +30 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +31 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +32 Mon Dec 10 11:42:58.045 Info: 802.1X changed +33 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +34 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +35 Mon Dec 10 11:42:58.193 Info: 802.1X changed +36 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +37 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +38 Mon Dec 10 11:42:58.342 Info: 802.1X changed +39 Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +40 Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +41 Mon Dec 10 11:42:58.491 Info: 802.1X changed +42 Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +43 Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +44 Mon Dec 10 11:42:58.640 Info: 802.1X changed +45 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +46 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +47 Mon Dec 10 11:42:58.805 Info: 802.1X changed +48 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +49 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +50 Mon Dec 10 11:42:58.958 Info: 802.1X changed +51 Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +52 Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +53 Mon Dec 10 11:42:59.155 Info: 802.1X changed +54 Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +55 Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +56 Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + + + +{last_modified_time} test_num_page.log Page 2 + + +57 ntation processAirPortStateChanges]: pppConnectionState 0 +58 Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +59 Mon Dec 10 11:42:56.705 Info: 802.1X changed +60 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +61 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +62 Mon Dec 10 11:42:56.854 Info: 802.1X changed +63 Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +64 Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +65 Mon Dec 10 11:42:57.002 Info: 802.1X changed +66 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +67 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +68 Mon Dec 10 11:42:57.152 Info: 802.1X changed +69 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +70 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +71 Mon Dec 10 11:42:57.302 Info: 802.1X changed +72 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +73 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +74 Mon Dec 10 11:42:57.449 Info: 802.1X changed +75 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +76 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +77 Mon Dec 10 11:42:57.600 Info: 802.1X changed +78 Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +79 Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +80 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +81 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +82 Mon Dec 10 11:42:57.749 Info: 802.1X changed +83 Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +84 Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +85 Mon Dec 10 11:42:57.896 Info: 802.1X changed +86 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +87 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +88 Mon Dec 10 11:42:58.045 Info: 802.1X changed +89 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +90 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +91 Mon Dec 10 11:42:58.193 Info: 802.1X changed +92 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +93 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +94 Mon Dec 10 11:42:58.342 Info: 802.1X changed +95 Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +96 Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +97 Mon Dec 10 11:42:58.491 Info: 802.1X changed +98 Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +99 Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +00 Mon Dec 10 11:42:58.640 Info: 802.1X changed +01 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +02 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +03 Mon Dec 10 11:42:58.805 Info: 802.1X changed +04 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +05 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +06 Mon Dec 10 11:42:58.958 Info: 802.1X changed +07 Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +08 Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +09 Mon Dec 10 11:42:59.155 Info: 802.1X changed +10 Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +11 Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +12 Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + diff --git a/tests/fixtures/pr/test_num_page_char.log.expected b/tests/fixtures/pr/test_num_page_char.log.expected new file mode 100644 index 000000000..169dbd844 --- /dev/null +++ b/tests/fixtures/pr/test_num_page_char.log.expected @@ -0,0 +1,132 @@ + + +{last_modified_time} test_num_page.log Page 1 + + + 1cntation processAirPortStateChanges]: pppConnectionState 0 + 2cMon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 3cMon Dec 10 11:42:56.705 Info: 802.1X changed + 4cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 5cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 6cMon Dec 10 11:42:56.854 Info: 802.1X changed + 7cMon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 8cMon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 9cMon Dec 10 11:42:57.002 Info: 802.1X changed + 10cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 11cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 12cMon Dec 10 11:42:57.152 Info: 802.1X changed + 13cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 14cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 15cMon Dec 10 11:42:57.302 Info: 802.1X changed + 16cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 17cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 18cMon Dec 10 11:42:57.449 Info: 802.1X changed + 19cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 20cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 21cMon Dec 10 11:42:57.600 Info: 802.1X changed + 22cMon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 23cMon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 24cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 25cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 26cMon Dec 10 11:42:57.749 Info: 802.1X changed + 27cMon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 28cMon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 29cMon Dec 10 11:42:57.896 Info: 802.1X changed + 30cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 31cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 32cMon Dec 10 11:42:58.045 Info: 802.1X changed + 33cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 34cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 35cMon Dec 10 11:42:58.193 Info: 802.1X changed + 36cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 37cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 38cMon Dec 10 11:42:58.342 Info: 802.1X changed + 39cMon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 40cMon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 41cMon Dec 10 11:42:58.491 Info: 802.1X changed + 42cMon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 43cMon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 44cMon Dec 10 11:42:58.640 Info: 802.1X changed + 45cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 46cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 47cMon Dec 10 11:42:58.805 Info: 802.1X changed + 48cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 49cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 50cMon Dec 10 11:42:58.958 Info: 802.1X changed + 51cMon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 52cMon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 53cMon Dec 10 11:42:59.155 Info: 802.1X changed + 54cMon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 55cMon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 56cMon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + + + +{last_modified_time} test_num_page.log Page 2 + + + 57cntation processAirPortStateChanges]: pppConnectionState 0 + 58cMon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 59cMon Dec 10 11:42:56.705 Info: 802.1X changed + 60cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 61cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 62cMon Dec 10 11:42:56.854 Info: 802.1X changed + 63cMon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 64cMon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 65cMon Dec 10 11:42:57.002 Info: 802.1X changed + 66cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 67cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 68cMon Dec 10 11:42:57.152 Info: 802.1X changed + 69cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 70cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 71cMon Dec 10 11:42:57.302 Info: 802.1X changed + 72cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 73cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 74cMon Dec 10 11:42:57.449 Info: 802.1X changed + 75cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 76cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 77cMon Dec 10 11:42:57.600 Info: 802.1X changed + 78cMon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 79cMon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 80cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 81cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 82cMon Dec 10 11:42:57.749 Info: 802.1X changed + 83cMon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 84cMon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 85cMon Dec 10 11:42:57.896 Info: 802.1X changed + 86cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 87cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 88cMon Dec 10 11:42:58.045 Info: 802.1X changed + 89cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 90cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 91cMon Dec 10 11:42:58.193 Info: 802.1X changed + 92cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 93cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 94cMon Dec 10 11:42:58.342 Info: 802.1X changed + 95cMon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 96cMon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 97cMon Dec 10 11:42:58.491 Info: 802.1X changed + 98cMon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 99cMon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 100cMon Dec 10 11:42:58.640 Info: 802.1X changed + 101cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 102cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 103cMon Dec 10 11:42:58.805 Info: 802.1X changed + 104cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 105cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 106cMon Dec 10 11:42:58.958 Info: 802.1X changed + 107cMon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 108cMon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 109cMon Dec 10 11:42:59.155 Info: 802.1X changed + 110cMon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 111cMon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 112cMon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + diff --git a/tests/fixtures/pr/test_num_page_char_one.log.expected b/tests/fixtures/pr/test_num_page_char_one.log.expected new file mode 100644 index 000000000..dd7813192 --- /dev/null +++ b/tests/fixtures/pr/test_num_page_char_one.log.expected @@ -0,0 +1,132 @@ + + +{last_modified_time} test_num_page.log Page 1 + + +1cntation processAirPortStateChanges]: pppConnectionState 0 +2cMon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +3cMon Dec 10 11:42:56.705 Info: 802.1X changed +4cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +5cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +6cMon Dec 10 11:42:56.854 Info: 802.1X changed +7cMon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +8cMon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +9cMon Dec 10 11:42:57.002 Info: 802.1X changed +0cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +1cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +2cMon Dec 10 11:42:57.152 Info: 802.1X changed +3cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +4cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +5cMon Dec 10 11:42:57.302 Info: 802.1X changed +6cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +7cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +8cMon Dec 10 11:42:57.449 Info: 802.1X changed +9cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +0cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +1cMon Dec 10 11:42:57.600 Info: 802.1X changed +2cMon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +3cMon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +4cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +5cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +6cMon Dec 10 11:42:57.749 Info: 802.1X changed +7cMon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +8cMon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +9cMon Dec 10 11:42:57.896 Info: 802.1X changed +0cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +1cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +2cMon Dec 10 11:42:58.045 Info: 802.1X changed +3cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +4cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +5cMon Dec 10 11:42:58.193 Info: 802.1X changed +6cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +7cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +8cMon Dec 10 11:42:58.342 Info: 802.1X changed +9cMon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +0cMon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +1cMon Dec 10 11:42:58.491 Info: 802.1X changed +2cMon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +3cMon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +4cMon Dec 10 11:42:58.640 Info: 802.1X changed +5cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +6cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +7cMon Dec 10 11:42:58.805 Info: 802.1X changed +8cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +9cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +0cMon Dec 10 11:42:58.958 Info: 802.1X changed +1cMon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +2cMon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +3cMon Dec 10 11:42:59.155 Info: 802.1X changed +4cMon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +5cMon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +6cMon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + + + +{last_modified_time} test_num_page.log Page 2 + + +7cntation processAirPortStateChanges]: pppConnectionState 0 +8cMon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +9cMon Dec 10 11:42:56.705 Info: 802.1X changed +0cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +1cMon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +2cMon Dec 10 11:42:56.854 Info: 802.1X changed +3cMon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +4cMon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +5cMon Dec 10 11:42:57.002 Info: 802.1X changed +6cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +7cMon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +8cMon Dec 10 11:42:57.152 Info: 802.1X changed +9cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +0cMon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +1cMon Dec 10 11:42:57.302 Info: 802.1X changed +2cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +3cMon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +4cMon Dec 10 11:42:57.449 Info: 802.1X changed +5cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +6cMon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +7cMon Dec 10 11:42:57.600 Info: 802.1X changed +8cMon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +9cMon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +0cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +1cMon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +2cMon Dec 10 11:42:57.749 Info: 802.1X changed +3cMon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +4cMon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +5cMon Dec 10 11:42:57.896 Info: 802.1X changed +6cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +7cMon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +8cMon Dec 10 11:42:58.045 Info: 802.1X changed +9cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +0cMon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +1cMon Dec 10 11:42:58.193 Info: 802.1X changed +2cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +3cMon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +4cMon Dec 10 11:42:58.342 Info: 802.1X changed +5cMon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +6cMon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +7cMon Dec 10 11:42:58.491 Info: 802.1X changed +8cMon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +9cMon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +0cMon Dec 10 11:42:58.640 Info: 802.1X changed +1cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +2cMon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +3cMon Dec 10 11:42:58.805 Info: 802.1X changed +4cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +5cMon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +6cMon Dec 10 11:42:58.958 Info: 802.1X changed +7cMon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +8cMon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +9cMon Dec 10 11:42:59.155 Info: 802.1X changed +0cMon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +1cMon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +2cMon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + diff --git a/tests/fixtures/pr/test_one_page.log b/tests/fixtures/pr/test_one_page.log new file mode 100644 index 000000000..4de6f5bf3 --- /dev/null +++ b/tests/fixtures/pr/test_one_page.log @@ -0,0 +1,56 @@ +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed diff --git a/tests/fixtures/pr/test_one_page.log.expected b/tests/fixtures/pr/test_one_page.log.expected new file mode 100644 index 000000000..54f772392 --- /dev/null +++ b/tests/fixtures/pr/test_one_page.log.expected @@ -0,0 +1,66 @@ + + +{last_modified_time} test_one_page.log Page 1 + + +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + diff --git a/tests/fixtures/pr/test_one_page_double_line.log.expected b/tests/fixtures/pr/test_one_page_double_line.log.expected new file mode 100644 index 000000000..e32101fcf --- /dev/null +++ b/tests/fixtures/pr/test_one_page_double_line.log.expected @@ -0,0 +1,132 @@ + + +{last_modified_time} test_one_page.log Page 1 + + +ntation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:56.705 Info: 802.1X changed + +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:56.854 Info: 802.1X changed + +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:57.002 Info: 802.1X changed + +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:57.152 Info: 802.1X changed + +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:57.302 Info: 802.1X changed + +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:57.449 Info: 802.1X changed + +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:57.600 Info: 802.1X changed + +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:57.749 Info: 802.1X changed + +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + + + + + + + + +{last_modified_time} test_one_page.log Page 2 + + +Mon Dec 10 11:42:57.896 Info: 802.1X changed + +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:58.045 Info: 802.1X changed + +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:58.193 Info: 802.1X changed + +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:58.342 Info: 802.1X changed + +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:58.491 Info: 802.1X changed + +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:58.640 Info: 802.1X changed + +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:58.805 Info: 802.1X changed + +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:58.958 Info: 802.1X changed + +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:59.155 Info: 802.1X changed + +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + +Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + + diff --git a/tests/fixtures/pr/test_one_page_first_line.log.expected b/tests/fixtures/pr/test_one_page_first_line.log.expected new file mode 100644 index 000000000..303f01c73 --- /dev/null +++ b/tests/fixtures/pr/test_one_page_first_line.log.expected @@ -0,0 +1,66 @@ + + +{last_modified_time} test_one_page.log Page 1 + + + 5 ntation processAirPortStateChanges]: pppConnectionState 0 + 6 Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 7 Mon Dec 10 11:42:56.705 Info: 802.1X changed + 8 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 9 Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 10 Mon Dec 10 11:42:56.854 Info: 802.1X changed + 11 Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 12 Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 13 Mon Dec 10 11:42:57.002 Info: 802.1X changed + 14 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 15 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 16 Mon Dec 10 11:42:57.152 Info: 802.1X changed + 17 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 18 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 19 Mon Dec 10 11:42:57.302 Info: 802.1X changed + 20 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 21 Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 22 Mon Dec 10 11:42:57.449 Info: 802.1X changed + 23 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 24 Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 25 Mon Dec 10 11:42:57.600 Info: 802.1X changed + 26 Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 27 Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 28 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 29 Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 30 Mon Dec 10 11:42:57.749 Info: 802.1X changed + 31 Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 32 Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 33 Mon Dec 10 11:42:57.896 Info: 802.1X changed + 34 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 35 Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 36 Mon Dec 10 11:42:58.045 Info: 802.1X changed + 37 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 38 Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 39 Mon Dec 10 11:42:58.193 Info: 802.1X changed + 40 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 41 Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 42 Mon Dec 10 11:42:58.342 Info: 802.1X changed + 43 Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 44 Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 45 Mon Dec 10 11:42:58.491 Info: 802.1X changed + 46 Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 47 Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 48 Mon Dec 10 11:42:58.640 Info: 802.1X changed + 49 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 50 Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 51 Mon Dec 10 11:42:58.805 Info: 802.1X changed + 52 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 53 Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 54 Mon Dec 10 11:42:58.958 Info: 802.1X changed + 55 Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 56 Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 57 Mon Dec 10 11:42:59.155 Info: 802.1X changed + 58 Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 59 Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 60 Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + diff --git a/tests/fixtures/pr/test_one_page_header.log.expected b/tests/fixtures/pr/test_one_page_header.log.expected new file mode 100644 index 000000000..a00d5f855 --- /dev/null +++ b/tests/fixtures/pr/test_one_page_header.log.expected @@ -0,0 +1,66 @@ + + +{last_modified_time} {header} Page 1 + + +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed + + + + + diff --git a/tests/fixtures/pr/test_one_page_no_ht.log.expected b/tests/fixtures/pr/test_one_page_no_ht.log.expected new file mode 100644 index 000000000..2abea9890 --- /dev/null +++ b/tests/fixtures/pr/test_one_page_no_ht.log.expected @@ -0,0 +1,57 @@ +ntation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.558 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.705 Info: 802.1X changed +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.706 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:56.854 Info: 802.1X changed +Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.002 Info: 802.1X changed +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.152 Info: 802.1X changed +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.302 Info: 802.1X changed +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.304 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.449 Info: 802.1X changed +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.451 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.600 Info: 802.1X changed +Mon Dec 10 11:42:57.601 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.602 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.624 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.749 Info: 802.1X changed +Mon Dec 10 11:42:57.750 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.751 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:57.896 Info: 802.1X changed +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:57.897 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.045 Info: 802.1X changed +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.047 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.193 Info: 802.1X changed +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.195 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.342 Info: 802.1X changed +Mon Dec 10 11:42:58.343 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.344 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.491 Info: 802.1X changed +Mon Dec 10 11:42:58.493 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.494 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.640 Info: 802.1X changed +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.642 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.805 Info: 802.1X changed +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.806 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:58.958 Info: 802.1X changed +Mon Dec 10 11:42:58.959 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:58.960 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.155 Info: 802.1X changed +Mon Dec 10 11:42:59.157 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 11:42:59.159 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 11:42:59.352 Info: 802.1X changed + diff --git a/tests/fixtures/pr/test_page_length.log.expected b/tests/fixtures/pr/test_page_length.log.expected new file mode 100644 index 000000000..8f4ab82d1 --- /dev/null +++ b/tests/fixtures/pr/test_page_length.log.expected @@ -0,0 +1,200 @@ + + +{last_modified_time} test.log Page 2 + + + 91 Mon Dec 10 11:43:31.748 )} took 0.0025 seconds, returned 10 results + 92 Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 4 does not require a live scan + 93 Mon Dec 10 11:43:31.748 Scan: Cache-assisted scan request on channel 5 does not require a live scan + 94 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 6 does not require a live scan + 95 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request does not require a live scan + 96 Mon Dec 10 11:43:31.749 AutoJoin: Successful cache-assisted background scan request with channels {( + 97 Mon Dec 10 11:43:31.749 [channelNumber=4(2GHz), channelWidth={20MHz}, active], + 98 Mon Dec 10 11:43:31.749 [channelNumber=5(2GHz), channelWidth={20MHz}, active], + 99 Mon Dec 10 11:43:31.749 [channelNumber=6(2GHz), channelWidth={20MHz}, active] + 100 Mon Dec 10 11:43:31.749 )} took 0.0008 seconds, returned 7 results + 101 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 7 does not require a live scan + 102 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 8 does not require a live scan + 103 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 9 does not require a live scan + 104 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request does not require a live scan + 105 Mon Dec 10 11:43:31.749 AutoJoin: Successful cache-assisted background scan request with channels {( + 106 Mon Dec 10 11:43:31.749 [channelNumber=7(2GHz), channelWidth={20MHz}, active], + 107 Mon Dec 10 11:43:31.749 [channelNumber=8(2GHz), channelWidth={20MHz}, active], + 108 Mon Dec 10 11:43:31.749 [channelNumber=9(2GHz), channelWidth={20MHz}, active] + 109 Mon Dec 10 11:43:31.749 )} took 0.0002 seconds, returned 1 results + 110 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 10 does not require a live scan + 111 Mon Dec 10 11:43:31.749 Scan: Cache-assisted scan request on channel 11 does not require a live scan + 112 Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 12 does not require a live scan + 113 Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request does not require a live scan + 114 Mon Dec 10 11:43:31.750 AutoJoin: Successful cache-assisted background scan request with channels {( + 115 Mon Dec 10 11:43:31.750 [channelNumber=10(2GHz), channelWidth={20MHz}, active], + 116 Mon Dec 10 11:43:31.750 [channelNumber=11(2GHz), channelWidth={20MHz}, active], + 117 Mon Dec 10 11:43:31.750 [channelNumber=12(2GHz), channelWidth={20MHz}, active] + 118 Mon Dec 10 11:43:31.750 )} took 0.0004 seconds, returned 4 results + 119 Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 13 does not require a live scan + 120 Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 36 does not require a live scan + 121 Mon Dec 10 11:43:31.750 Scan: Cache-assisted scan request on channel 40 does not require a live scan + 122 Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request does not require a live scan + 123 Mon Dec 10 11:43:31.751 AutoJoin: Successful cache-assisted background scan request with channels {( + 124 Mon Dec 10 11:43:31.751 [channelNumber=13(2GHz), channelWidth={20MHz}, active], + 125 Mon Dec 10 11:43:31.751 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], + 126 Mon Dec 10 11:43:31.751 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active] + 127 Mon Dec 10 11:43:31.751 )} took 0.0009 seconds, returned 9 results + 128 Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request on channel 44 does not require a live scan + 129 Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request on channel 48 does not require a live scan + 130 Mon Dec 10 11:43:31.751 Scan: Cache-assisted scan request on channel 149 does not require a live scan + 131 Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request does not require a live scan + 132 Mon Dec 10 11:43:31.752 AutoJoin: Successful cache-assisted background scan request with channels {( + 133 Mon Dec 10 11:43:31.752 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], + 134 Mon Dec 10 11:43:31.752 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], + 135 Mon Dec 10 11:43:31.752 [channelNumber=149(5GHz), channelWidth={20MHz}, active] + 136 Mon Dec 10 11:43:31.752 )} took 0.0010 seconds, returned 9 results + 137 Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request on channel 153 does not require a live scan + 138 Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request on channel 157 does not require a live scan + 139 Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request on channel 161 does not require a live scan + 140 Mon Dec 10 11:43:31.752 Scan: Cache-assisted scan request does not require a live scan + 141 Mon Dec 10 11:43:31.753 AutoJoin: Successful cache-assisted background scan request with channels {( + 142 Mon Dec 10 11:43:31.753 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], + 143 Mon Dec 10 11:43:31.753 [channelNumber=157(5GHz), channelWidth={20MHz}, active], + 144 Mon Dec 10 11:43:31.753 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active] + 145 Mon Dec 10 11:43:31.753 )} took 0.0007 seconds, returned 9 results + 146 Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 165 does not require a live scan + 147 Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 52 does not require a live scan + 148 Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 56 does not require a live scan + 149 Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request does not require a live scan + 150 Mon Dec 10 11:43:31.753 AutoJoin: Successful cache-assisted background scan request with channels {( + 151 Mon Dec 10 11:43:31.753 [channelNumber=165(5GHz), channelWidth={20MHz}, active], + 152 Mon Dec 10 11:43:31.753 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], + 153 Mon Dec 10 11:43:31.753 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] + 154 Mon Dec 10 11:43:31.753 )} took 0.0005 seconds, returned 6 results + 155 Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 60 does not require a live scan + 156 Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request on channel 64 does not require a live scan + 157 Mon Dec 10 11:43:31.753 Scan: Cache-assisted scan request does not require a live scan + 158 Mon Dec 10 11:43:31.753 AutoJoin: Successful cache-assisted background scan request with channels {( + 159 Mon Dec 10 11:43:31.754 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], + 160 Mon Dec 10 11:43:31.754 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] + 161 Mon Dec 10 11:43:31.754 )} took 0.0003 seconds, returned 4 results + 162 Mon Dec 10 11:43:42.382 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) + 163 Mon Dec 10 11:43:42.382 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=270 Mbps + 164 Mon Dec 10 11:43:42.383 Info: link quality changed + 165 Mon Dec 10 11:49:12.347 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) + 166 Mon Dec 10 11:49:12.350 Info: QUERY SCAN CACHE request received from pid 92 (locationd) + 167 Mon Dec 10 11:52:32.194 Info: SCAN request received from pid 92 (locationd) with priority 2 + 168 Mon Dec 10 11:52:32.448 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) + 169 Mon Dec 10 11:52:32.450 Info: QUERY SCAN CACHE request received from pid 92 (locationd) + 170 Mon Dec 10 11:52:32.451 AutoJoin: Successful cache-assisted scan request for locationd with channels {( + 171 Mon Dec 10 11:52:32.451 [channelNumber=1(2GHz), channelWidth={20MHz}, active], + 172 Mon Dec 10 11:52:32.451 [channelNumber=2(2GHz), channelWidth={20MHz}, active], + 173 Mon Dec 10 11:52:32.451 [channelNumber=3(2GHz), channelWidth={20MHz}, active], + 174 Mon Dec 10 11:52:32.451 [channelNumber=4(2GHz), channelWidth={20MHz}, active], + 175 Mon Dec 10 11:52:32.451 [channelNumber=5(2GHz), channelWidth={20MHz}, active], + 176 Mon Dec 10 11:52:32.451 [channelNumber=6(2GHz), channelWidth={20MHz}, active] + 177 Mon Dec 10 11:52:32.451 )} took 0.2566 seconds, returned 10 results + 178 Mon Dec 10 11:52:32.451 Info: scan cache updated + 179 Mon Dec 10 11:52:32.712 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) + 180 Mon Dec 10 11:52:32.715 Info: QUERY SCAN CACHE request received from pid 92 (locationd) + + + + + + + +{last_modified_time} test.log Page 3 + + + 181 Mon Dec 10 11:52:32.715 AutoJoin: Successful cache-assisted scan request for locationd with channels {( + 182 Mon Dec 10 11:52:32.715 [channelNumber=7(2GHz), channelWidth={20MHz}, active], + 183 Mon Dec 10 11:52:32.715 [channelNumber=8(2GHz), channelWidth={20MHz}, active], + 184 Mon Dec 10 11:52:32.715 [channelNumber=9(2GHz), channelWidth={20MHz}, active], + 185 Mon Dec 10 11:52:32.715 [channelNumber=10(2GHz), channelWidth={20MHz}, active], + 186 Mon Dec 10 11:52:32.715 [channelNumber=11(2GHz), channelWidth={20MHz}, active], + 187 Mon Dec 10 11:52:32.715 [channelNumber=12(2GHz), channelWidth={20MHz}, active] + 188 Mon Dec 10 11:52:32.715 )} took 0.2636 seconds, returned 10 results + 189 Mon Dec 10 11:52:32.994 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) + 190 Mon Dec 10 11:52:32.997 Info: QUERY SCAN CACHE request received from pid 92 (locationd) + 191 Mon Dec 10 11:52:32.998 AutoJoin: Successful cache-assisted scan request for locationd with channels {( + 192 Mon Dec 10 11:52:32.998 [channelNumber=13(2GHz), channelWidth={20MHz}, active], + 193 Mon Dec 10 11:52:32.998 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], + 194 Mon Dec 10 11:52:32.998 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active], + 195 Mon Dec 10 11:52:32.998 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], + 196 Mon Dec 10 11:52:32.998 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], + 197 Mon Dec 10 11:52:32.998 [channelNumber=149(5GHz), channelWidth={20MHz}, active] + 198 Mon Dec 10 11:52:32.998 )} took 0.2822 seconds, returned 14 results + 199 Mon Dec 10 11:52:33.405 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) + 200 Mon Dec 10 11:52:33.408 Info: QUERY SCAN CACHE request received from pid 92 (locationd) + 201 Mon Dec 10 11:52:33.409 AutoJoin: Successful cache-assisted scan request for locationd with channels {( + 202 Mon Dec 10 11:52:33.409 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], + 203 Mon Dec 10 11:52:33.409 [channelNumber=157(5GHz), channelWidth={20MHz}, active], + 204 Mon Dec 10 11:52:33.409 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active], + 205 Mon Dec 10 11:52:33.409 [channelNumber=165(5GHz), channelWidth={20MHz}, active], + 206 Mon Dec 10 11:52:33.409 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], + 207 Mon Dec 10 11:52:33.409 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] + 208 Mon Dec 10 11:52:33.409 )} took 0.4099 seconds, returned 11 results + 209 Mon Dec 10 11:52:33.669 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) + 210 Mon Dec 10 11:52:33.672 Info: QUERY SCAN CACHE request received from pid 92 (locationd) + 211 Mon Dec 10 11:52:33.672 AutoJoin: Successful cache-assisted scan request for locationd with channels {( + 212 Mon Dec 10 11:52:33.672 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], + 213 Mon Dec 10 11:52:33.672 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] + 214 Mon Dec 10 11:52:33.672 )} took 0.2625 seconds, returned 8 results + 215 Mon Dec 10 11:52:33.673 Info: scan cache updated + 216 Mon Dec 10 11:52:33.693 Info: SCAN request received from pid 92 (locationd) with priority 2 + 217 Mon Dec 10 11:52:33.693 Scan: locationd requested a live scan less than 10 seconds after previous request (1.4991s) returning cached scan results + 218 Mon Dec 10 11:52:33.728 Info: SCAN request received from pid 92 (locationd) with priority 2 + 219 Mon Dec 10 11:52:33.728 Scan: locationd requested a live scan less than 10 seconds after previous request (1.5339s) returning cached scan results + 220 Mon Dec 10 11:55:47.609 Driver Discovery: _PMConnectionHandler: caps = CPU Net Disk + 221 Mon Dec 10 11:55:47.609 Driver Discovery: _PMConnectionHandler: Being put into maintenance wake mode while fully awake. + 222 Mon Dec 10 11:55:47.610 Info: psCallback: powerSource = AC Power + 223 Mon Dec 10 11:55:47.610 Info: psCallback: set powersave disabled on en0 + 224 Mon Dec 10 11:55:47.637 Info: RELINQUISH BT PAGING LOCK request received from pid 106 (bluetoothd) + 225 Mon Dec 10 11:55:47.637 Info: RELINQUISH BT PAGING LOCK request received from pid 106 (bluetoothd) + 226 Mon Dec 10 11:55:47.638 BTC: BT PAGING state already set to 0 + 227 Mon Dec 10 11:55:47.638 BTC: BT PAGING state already set to 0 + 228 Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED after 0.0 seconds + 229 Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED after 0.0 seconds + 230 Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED, re-enabling deferred WiFi requests + 231 Mon Dec 10 11:55:47.638 Info: BT PAGING LOCK RELINQUISHED, re-enabling deferred WiFi requests + 232 Mon Dec 10 11:55:48.093 IPC: ADDED XPC CLIENT CONNECTION [loginwindow (pid=101, euid=1651299376, egid=604256670)] + 233 Mon Dec 10 11:55:48.093 Info: START MONITORING EVENT request received from pid 101 (loginwindow) + 234 Mon Dec 10 11:55:48.093 Info: START MONITORING EVENT request received from pid 101 (loginwindow) + 235 Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) + 236 Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) + 237 Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) + 238 Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) + 239 Mon Dec 10 11:55:48.094 Info: START MONITORING EVENT request received from pid 101 (loginwindow) + 240 Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) + 241 Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) + 242 Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) + 243 Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) + 244 Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) + 245 Mon Dec 10 11:55:48.104 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) + 246 Mon Dec 10 11:55:48.105 Info: STOP MONITORING EVENT request received from pid 101 (loginwindow) + 247 Mon Dec 10 11:56:07.629 Driver Discovery: _PMConnectionHandler: caps = + 248 Mon Dec 10 11:56:07.629 AutoJoin: BEST CONNECTED SCAN CANCELLED on interface en0 + 249 Mon Dec 10 11:56:07.629 AutoJoin: BACKGROUND SCAN CANCELLED on interface en0 + 250 Mon Dec 10 11:56:07.629 AutoJoin: Auto-join retry cancelled on interface en0 + 251 Mon Dec 10 11:56:07.629 Offload: _tcpKeepAliveActive: TCP keep-alive is active. + 252 Mon Dec 10 11:56:07.637 P2P: _changeInterfaceFlags: Marking p2p0 down + 253 Mon Dec 10 11:56:07.637 Info: SleepAcknowledgementCheckForHostAP: Checking sleep readiness for HostAP + 254 Mon Dec 10 11:56:07.637 SleepAcknowledgementCheck: Checking sleep readiness + 255 Mon Dec 10 11:56:07.637 SleepAcknowledgementCheck: Delaying sleep acknowledgement because of VifUp + 256 Mon Dec 10 11:56:07.638 _interfaceFlagsChanged: KEV_DL_SIFFLAGS received for p2p0 [flags=0xffff8802 (down)]. + 257 Mon Dec 10 11:56:07.638 _interfaceFlagsChanged: Flags changed for p2p0 (0xffff8843 -> 0xffff8802) (down). + 258 Mon Dec 10 11:56:07.638 P2P: _deviceInterfaceMarkedDown: p2p0 marked down + 259 Mon Dec 10 11:56:07.638 P2P: _removeTimeoutForActionAndParam: Attempting to remove 'Stop GO' action (param = 0x0) + 260 Mon Dec 10 11:56:07.638 P2P: _removeTimeoutForActionAndParam: Attempting to remove 'Scan' action (param = 0x0) + 261 Mon Dec 10 11:56:07.638 P2P: _p2pSupervisorEventRunLoopCallback: Mark down complete for p2p0 + 262 Mon Dec 10 11:56:07.638 SleepAcknowledgementCheck: Checking sleep readiness + 263 Mon Dec 10 11:56:07.638 WoW: SleepAcknowledgementCheck: Checking if auto-join is in progress before sleep acknowledgement + 264 Mon Dec 10 11:56:07.638 WoW: SleepAcknowledgementDelayForAutoJoinAttempt_block_invoke: AUTO-JOIN attempt complete for en0, re-checking sleep readiness + 265 Mon Dec 10 11:56:07.638 SleepAcknowledgementCheck: Checking sleep readiness + 266 Mon Dec 10 11:56:07.639 WoW: _wowEnabled: WoW is active on en0 + 267 Mon Dec 10 11:56:07.639 WoW: wowExchangeRequiredForNode: WoW exchange not required for en0 + 268 Mon Dec 10 11:56:07.639 _acknowledgeSleepEvent: Acknowledging sleep event + 269 Mon Dec 10 11:56:07.639 Info: PRIORITY LOCK ADDED [client=airportd, type=4, interface=(null), priority=7] + 270 Mon Dec 10 11:56:07.640 AutoJoin: Auto-join retry cancelled on interface en0 + + + + + diff --git a/tests/fixtures/pr/test_page_length1.log.expected b/tests/fixtures/pr/test_page_length1.log.expected new file mode 100644 index 000000000..a09a2a986 --- /dev/null +++ b/tests/fixtures/pr/test_page_length1.log.expected @@ -0,0 +1,12 @@ + 6 Mon Dec 10 11:42:56.854 Info: 802.1X changed + 7 Mon Dec 10 11:42:56.855 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 8 Mon Dec 10 11:42:56.856 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 9 Mon Dec 10 11:42:57.002 Info: 802.1X changed + 10 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + + 11 Mon Dec 10 11:42:57.003 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 12 Mon Dec 10 11:42:57.152 Info: 802.1X changed + 13 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 + 14 Mon Dec 10 11:42:57.154 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars + 15 Mon Dec 10 11:42:57.302 Info: 802.1X changed + diff --git a/tests/fixtures/pr/test_page_range_1.log.expected b/tests/fixtures/pr/test_page_range_1.log.expected new file mode 100644 index 000000000..f254261d4 --- /dev/null +++ b/tests/fixtures/pr/test_page_range_1.log.expected @@ -0,0 +1,264 @@ + + +{last_modified_time} test.log Page 15 + + +Mon Dec 10 12:05:48.183 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.183 )} took 0.0007 seconds, returned 4 results +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 13 does not require a live scan +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 36 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 40 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.184 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.184 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.184 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:48.184 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:48.184 )} took 0.0007 seconds, returned 5 results +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 44 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 48 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 149 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.185 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.185 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:48.185 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:48.185 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.185 )} took 0.0006 seconds, returned 4 results +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 153 does not require a live scan +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 157 does not require a live scan +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 161 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.186 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.186 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:48.186 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.186 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:48.186 )} took 0.0010 seconds, returned 7 results +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 165 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 52 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 56 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.187 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.187 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.187 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:48.187 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:48.187 )} took 0.0008 seconds, returned 5 results +Mon Dec 10 12:05:48.187 Scan: Cache-assisted scan request on channel 60 does not require a live scan +Mon Dec 10 12:05:48.187 Scan: Cache-assisted scan request on channel 64 does not require a live scan +Mon Dec 10 12:05:48.188 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.188 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.188 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:48.188 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:48.188 )} took 0.0013 seconds, returned 8 results +Mon Dec 10 12:05:50.375 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:50.375 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=216 Mbps +Mon Dec 10 12:05:50.376 Info: link quality changed +Mon Dec 10 12:05:55.378 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:55.378 Info: _bsd_80211_event_callback: link quality: RSSI=-56 dBm TxRate=243 Mbps +Mon Dec 10 12:05:55.379 Info: link quality changed +Mon Dec 10 12:06:28.759 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:06:28.763 Info: AWDL started +Mon Dec 10 12:06:28.763 Driver Event: _bsd_80211_event_callback: CHANNEL_SWITCH (en0) +Mon Dec 10 12:06:28.765 Roam: ROAMING PROFILES updated to SINGLE-BAND, SINGLE-AP for 2.4GHz on en0 +Mon Dec 10 12:06:28.765 Roam: ROAMING PROFILES updated to SINGLE-BAND, SINGLE-AP for 5GHz on en0 + + + + + + + +{last_modified_time} test.log Page 16 + + +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/ProfileID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEP40' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID_STR' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AirPlay' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AutoJoinTimestamp' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BSSID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CHANNEL' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Busy' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Power Status' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEPOPENSYSTEM' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CachedScanRecord' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/UserMode8021X' +Mon Dec 10 12:06:28.771 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BusyUI' +Mon Dec 10 12:06:28.945 Info: INTERFACE STATE INFO request received from pid 362 (sharingd) +Mon Dec 10 12:06:28.946 Info: -[CWXPCInterfaceContext __copyMACAddressForInterface:]: MAC address: <82cd0ac3 bf73> +Mon Dec 10 12:06:30.064 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:30.151 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:30.151 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.137 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.138 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.832 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.832 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:48.742 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:48.742 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.121 P2P: _terminateGroupOwnerTimer: Terminate group owner timer notification received. +Mon Dec 10 12:06:49.267 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.267 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.793 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.793 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.931 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:06:50.931 Driver Event: _bsd_80211_event_callback: AWDL_STATISTICS (awdl0) +Mon Dec 10 12:06:50.932 Info: AWDL real time mode ended +Mon Dec 10 12:06:50.945 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:06:50.945 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:06:50.945 {type = mutable dict, count = 2, +Mon Dec 10 12:06:50.945 entries => +Mon Dec 10 12:06:50.945 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.945 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.945 } +Mon Dec 10 12:06:50.945 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:06:50.945 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:06:50.945 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:06:50.945 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.945 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> + + + + + + + +{last_modified_time} test.log Page 17 + + +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:06:50.945 Info: AWDL real time mode ended +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:06:50.946 {type = mutable dict, count = 2, +Mon Dec 10 12:06:50.946 entries => +Mon Dec 10 12:06:50.946 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.946 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.946 } +Mon Dec 10 12:06:50.946 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:06:50.946 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:06:50.947 Info: AWDL ended +Mon Dec 10 12:06:50.951 Info: -[CWXPCInterfaceContext __submitAWDLUsageMetric:duration:]: AWDL usage metric data: CWAWDMetricAwdlUsageData: selfInfraChannel 40, peerInfraChannel 0, numOfPeers 6. Keys: Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90013 +Mon Dec 10 12:06:50.952 Info: INTERFACE STATE INFO request received from pid 362 (sharingd) +Mon Dec 10 12:06:50.952 Info: -[CWXPCInterfaceContext __copyMACAddressForInterface:]: MAC address: <82cd0ac3 bf73> +Mon Dec 10 12:06:50.953 Roam: ROAMING PROFILES updated to AC POWER for 2.4GHz on en0 +Mon Dec 10 12:06:50.953 Roam: ROAMING PROFILES updated to AC POWER for 5GHz on en0 +Mon Dec 10 12:06:53.680 Info: -[CWXPCSubsystem handleDeviceCountMetricQuery:]_block_invoke: DeviceCount metric data: CWAWDMetricDeviceCountData: timeSinceBoot: 1515466.360642, deviceCount: 1 +Mon Dec 10 12:06:53.684 Info: -[CWXPCSubsystem handleNetworkPrefsMetricQuery:]: NetworkPrefs metric data: CWAWDMetricNetworkPrefsData: preferred 15 hidden 0 open 1 wep 0 wpa 12 eap 2 +Mon Dec 10 12:06:53.684 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90004 +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90001 +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x9000d +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x9000b +Mon Dec 10 12:12:03.594 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:03.594 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=216 Mbps +Mon Dec 10 12:12:03.595 Info: link quality changed +Mon Dec 10 12:12:09.598 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:09.598 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=270 Mbps +Mon Dec 10 12:12:09.599 Info: link quality changed +Mon Dec 10 12:12:56.625 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:56.625 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 12:12:56.625 Info: link quality changed +Mon Dec 10 12:13:01.625 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:01.625 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=243 Mbps +Mon Dec 10 12:13:01.626 Info: link quality changed +Mon Dec 10 12:13:06.628 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:06.628 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 12:13:06.628 Info: link quality changed +Mon Dec 10 12:13:27.639 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:27.639 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=243 Mbps +Mon Dec 10 12:13:27.640 Info: link quality changed + + + + + + + +{last_modified_time} test.log Page 18 + + +Mon Dec 10 12:14:46.658 Info: SCAN request received from pid 92 (locationd) with priority 2 +Mon Dec 10 12:14:46.902 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:46.905 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:46.906 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:46.906 [channelNumber=1(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=2(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=3(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=4(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=5(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:46.906 [channelNumber=6(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:14:46.906 )} took 0.2478 seconds, returned 13 results +Mon Dec 10 12:14:46.906 Info: scan cache updated +Mon Dec 10 12:14:47.158 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:47.160 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:47.160 [channelNumber=7(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=8(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=9(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=10(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=11(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.160 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:14:47.160 )} took 0.2532 seconds, returned 4 results +Mon Dec 10 12:14:47.161 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:47.433 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:47.436 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:47.437 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:47.437 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.437 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.437 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:14:47.437 )} took 0.2763 seconds, returned 11 results +Mon Dec 10 12:14:47.778 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:47.781 Info: QUERY SCAN CACHE request received from pid 92 (locationd) +Mon Dec 10 12:14:47.782 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:47.782 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.782 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.782 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:14:47.782 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:14:47.782 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:14:47.782 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:14:47.782 )} took 0.3436 seconds, returned 9 results +Mon Dec 10 12:14:48.052 Driver Event: _bsd_80211_event_callback: SCAN_CACHE_UPDATED (en0) +Mon Dec 10 12:14:48.055 AutoJoin: Successful cache-assisted scan request for locationd with channels {( +Mon Dec 10 12:14:48.055 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:14:48.055 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:14:48.055 )} took 0.2714 seconds, returned 6 results +Mon Dec 10 12:14:48.055 Info: QUERY SCAN CACHE request received from pid 92 (locationd) + + + + + + + + + + + + + diff --git a/tests/fixtures/pr/test_page_range_2.log.expected b/tests/fixtures/pr/test_page_range_2.log.expected new file mode 100644 index 000000000..4f260eb65 --- /dev/null +++ b/tests/fixtures/pr/test_page_range_2.log.expected @@ -0,0 +1,198 @@ + + +{last_modified_time} test.log Page 15 + + +Mon Dec 10 12:05:48.183 [channelNumber=12(2GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.183 )} took 0.0007 seconds, returned 4 results +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 13 does not require a live scan +Mon Dec 10 12:05:48.183 Scan: Cache-assisted scan request on channel 36 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 40 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.184 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.184 [channelNumber=13(2GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.184 [channelNumber=36(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:48.184 [channelNumber=40(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:48.184 )} took 0.0007 seconds, returned 5 results +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 44 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 48 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request on channel 149 does not require a live scan +Mon Dec 10 12:05:48.184 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.185 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.185 [channelNumber=44(5GHz), channelWidth={40MHz(+1)}, active], +Mon Dec 10 12:05:48.185 [channelNumber=48(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:48.185 [channelNumber=149(5GHz), channelWidth={20MHz}, active] +Mon Dec 10 12:05:48.185 )} took 0.0006 seconds, returned 4 results +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 153 does not require a live scan +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 157 does not require a live scan +Mon Dec 10 12:05:48.185 Scan: Cache-assisted scan request on channel 161 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.186 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.186 [channelNumber=153(5GHz), channelWidth={40MHz(-1)}, active], +Mon Dec 10 12:05:48.186 [channelNumber=157(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.186 [channelNumber=161(5GHz), channelWidth={40MHz(-1)}, active] +Mon Dec 10 12:05:48.186 )} took 0.0010 seconds, returned 7 results +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 165 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 52 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request on channel 56 does not require a live scan +Mon Dec 10 12:05:48.186 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.187 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.187 [channelNumber=165(5GHz), channelWidth={20MHz}, active], +Mon Dec 10 12:05:48.187 [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:48.187 [channelNumber=56(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:48.187 )} took 0.0008 seconds, returned 5 results +Mon Dec 10 12:05:48.187 Scan: Cache-assisted scan request on channel 60 does not require a live scan +Mon Dec 10 12:05:48.187 Scan: Cache-assisted scan request on channel 64 does not require a live scan +Mon Dec 10 12:05:48.188 Scan: Cache-assisted scan request does not require a live scan +Mon Dec 10 12:05:48.188 AutoJoin: Successful cache-assisted background scan request with channels {( +Mon Dec 10 12:05:48.188 [channelNumber=60(5GHz), channelWidth={40MHz(+1)}, DFS], +Mon Dec 10 12:05:48.188 [channelNumber=64(5GHz), channelWidth={40MHz(-1)}, DFS] +Mon Dec 10 12:05:48.188 )} took 0.0013 seconds, returned 8 results +Mon Dec 10 12:05:50.375 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:50.375 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=216 Mbps +Mon Dec 10 12:05:50.376 Info: link quality changed +Mon Dec 10 12:05:55.378 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:05:55.378 Info: _bsd_80211_event_callback: link quality: RSSI=-56 dBm TxRate=243 Mbps +Mon Dec 10 12:05:55.379 Info: link quality changed +Mon Dec 10 12:06:28.759 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:06:28.763 Info: AWDL started +Mon Dec 10 12:06:28.763 Driver Event: _bsd_80211_event_callback: CHANNEL_SWITCH (en0) +Mon Dec 10 12:06:28.765 Roam: ROAMING PROFILES updated to SINGLE-BAND, SINGLE-AP for 2.4GHz on en0 +Mon Dec 10 12:06:28.765 Roam: ROAMING PROFILES updated to SINGLE-BAND, SINGLE-AP for 5GHz on en0 + + + + + + + +{last_modified_time} test.log Page 16 + + +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/ProfileID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEP40' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID_STR' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AirPlay' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/AutoJoinTimestamp' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BSSID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/SSID' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CHANNEL' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Busy' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/Power Status' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/WEPOPENSYSTEM' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/CachedScanRecord' +Mon Dec 10 12:06:28.770 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/UserMode8021X' +Mon Dec 10 12:06:28.771 SC: airportdProcessSystemConfigurationEvent: Processing 'State:/Network/Interface/en0/AirPort/BusyUI' +Mon Dec 10 12:06:28.945 Info: INTERFACE STATE INFO request received from pid 362 (sharingd) +Mon Dec 10 12:06:28.946 Info: -[CWXPCInterfaceContext __copyMACAddressForInterface:]: MAC address: <82cd0ac3 bf73> +Mon Dec 10 12:06:30.064 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:30.151 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:30.151 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.137 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.138 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.832 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:31.832 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:48.742 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:48.742 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.121 P2P: _terminateGroupOwnerTimer: Terminate group owner timer notification received. +Mon Dec 10 12:06:49.267 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.267 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.793 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:49.793 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.931 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:06:50.931 Driver Event: _bsd_80211_event_callback: AWDL_STATISTICS (awdl0) +Mon Dec 10 12:06:50.932 Info: AWDL real time mode ended +Mon Dec 10 12:06:50.945 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:06:50.945 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:06:50.945 {type = mutable dict, count = 2, +Mon Dec 10 12:06:50.945 entries => +Mon Dec 10 12:06:50.945 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.945 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.945 } +Mon Dec 10 12:06:50.945 +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:06:50.945 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:06:50.945 Driver Event: _bsd_80211_event_callback: AWDL_REALTIME_MODE_END (awdl0) +Mon Dec 10 12:06:50.945 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.945 BTC: __BluetoothCoexHandleUpdateForNode: Handle Bluetooth Coex: FrequencyBand <2>, Bluetooth Bandwidth Utilization <3>, Clamshell Mode <0> + + + + + + + +{last_modified_time} test.log Page 17 + + +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 2.4GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexSetProfile: profile for band 5GHz didn't change +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: band = 0x2 +Mon Dec 10 12:06:50.945 Info: AWDL real time mode ended +Mon Dec 10 12:06:50.945 BTC: BluetoothCoexHandle_ApplyPolicy: Bluetooth Coex: hosting AP = NO, assoc as STA = YES, assoced in 2.4GHz = NO +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: band = 2 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexGetCurrentBssidPhyMode: Bluetooth Coex: Active PHY Mode 16. PHY Mode +Mon Dec 10 12:06:50.946 {type = mutable dict, count = 2, +Mon Dec 10 12:06:50.946 entries => +Mon Dec 10 12:06:50.946 0 : {contents = "PHYMODE_ACTIVE"} = {value = +16, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.946 1 : {contents = "PHYMODE_SUPPORTED"} = {value = +159, type = kCFNumberSInt32Type} +Mon Dec 10 12:06:50.946 } +Mon Dec 10 12:06:50.946 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: PHY mode: <10> 5GHz: YES +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: MCS index set size = 16, NSS = 2SS, need to re-assoc = YES +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Bluetooth Coex: 11bg only = NO, BT on = YES, # HIDs = 0, # A2DP = 0, # SCO = 0, fallback = normal -> Middle Chain is ON +Mon Dec 10 12:06:50.946 Info: -[AirPortExtraImplementation processAirPortStateChanges]: pppConnectionState 0 +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexSettingPerChainPower: Chain Power Setting does not need to be updated +Mon Dec 10 12:06:50.946 BTC: BluetoothCoexHandle_ReconfigureAntennas: Skipping REASSOC - The # of chains did not change. +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Info: -[AirPortExtraImplementation processAirPortStateChanges]: old state=4 bars, new state=4 bars +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_PEER_PRESENCE (awdl0) +Mon Dec 10 12:06:50.946 Driver Event: _bsd_80211_event_callback: AWDL_SYNC_STATE_CHANGED (awdl0) +Mon Dec 10 12:06:50.947 Info: AWDL ended +Mon Dec 10 12:06:50.951 Info: -[CWXPCInterfaceContext __submitAWDLUsageMetric:duration:]: AWDL usage metric data: CWAWDMetricAwdlUsageData: selfInfraChannel 40, peerInfraChannel 0, numOfPeers 6. Keys: Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90013 +Mon Dec 10 12:06:50.952 Info: INTERFACE STATE INFO request received from pid 362 (sharingd) +Mon Dec 10 12:06:50.952 Info: -[CWXPCInterfaceContext __copyMACAddressForInterface:]: MAC address: <82cd0ac3 bf73> +Mon Dec 10 12:06:50.953 Roam: ROAMING PROFILES updated to AC POWER for 2.4GHz on en0 +Mon Dec 10 12:06:50.953 Roam: ROAMING PROFILES updated to AC POWER for 5GHz on en0 +Mon Dec 10 12:06:53.680 Info: -[CWXPCSubsystem handleDeviceCountMetricQuery:]_block_invoke: DeviceCount metric data: CWAWDMetricDeviceCountData: timeSinceBoot: 1515466.360642, deviceCount: 1 +Mon Dec 10 12:06:53.684 Info: -[CWXPCSubsystem handleNetworkPrefsMetricQuery:]: NetworkPrefs metric data: CWAWDMetricNetworkPrefsData: preferred 15 hidden 0 open 1 wep 0 wpa 12 eap 2 +Mon Dec 10 12:06:53.684 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90004 +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x90001 +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x9000d +Mon Dec 10 12:06:53.852 Info: -[CWAWDManager submitMetric:]: submitting metric id 0x9000b +Mon Dec 10 12:12:03.594 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:03.594 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=216 Mbps +Mon Dec 10 12:12:03.595 Info: link quality changed +Mon Dec 10 12:12:09.598 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:09.598 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=270 Mbps +Mon Dec 10 12:12:09.599 Info: link quality changed +Mon Dec 10 12:12:56.625 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:12:56.625 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 12:12:56.625 Info: link quality changed +Mon Dec 10 12:13:01.625 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:01.625 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=243 Mbps +Mon Dec 10 12:13:01.626 Info: link quality changed +Mon Dec 10 12:13:06.628 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:06.628 Info: _bsd_80211_event_callback: link quality: RSSI=-58 dBm TxRate=216 Mbps +Mon Dec 10 12:13:06.628 Info: link quality changed +Mon Dec 10 12:13:27.639 Driver Event: _bsd_80211_event_callback: LINK_QUALITY (en0) +Mon Dec 10 12:13:27.639 Info: _bsd_80211_event_callback: link quality: RSSI=-57 dBm TxRate=243 Mbps +Mon Dec 10 12:13:27.640 Info: link quality changed + + + + + diff --git a/tests/fixtures/ptx/gnu_ext_disabled_roff_auto_ref.expected b/tests/fixtures/ptx/gnu_ext_disabled_rightward_auto_ref.expected similarity index 100% rename from tests/fixtures/ptx/gnu_ext_disabled_roff_auto_ref.expected rename to tests/fixtures/ptx/gnu_ext_disabled_rightward_auto_ref.expected diff --git a/tests/fixtures/ptx/gnu_ext_disabled_roff_input_ref.expected b/tests/fixtures/ptx/gnu_ext_disabled_rightward_input_ref.expected similarity index 100% rename from tests/fixtures/ptx/gnu_ext_disabled_roff_input_ref.expected rename to tests/fixtures/ptx/gnu_ext_disabled_rightward_input_ref.expected diff --git a/tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref.expected b/tests/fixtures/ptx/gnu_ext_disabled_rightward_no_ref.expected similarity index 100% rename from tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref.expected rename to tests/fixtures/ptx/gnu_ext_disabled_rightward_no_ref.expected diff --git a/tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected b/tests/fixtures/ptx/gnu_ext_disabled_rightward_no_ref_word_regexp_exc_space.expected similarity index 100% rename from tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected rename to tests/fixtures/ptx/gnu_ext_disabled_rightward_no_ref_word_regexp_exc_space.expected diff --git a/tests/fixtures/shuf/file_input.txt b/tests/fixtures/shuf/file_input.txt new file mode 100644 index 000000000..fc316949e --- /dev/null +++ b/tests/fixtures/shuf/file_input.txt @@ -0,0 +1,10 @@ +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 diff --git a/tests/fixtures/sort/blanks.expected b/tests/fixtures/sort/blanks.expected new file mode 100644 index 000000000..3469e434c --- /dev/null +++ b/tests/fixtures/sort/blanks.expected @@ -0,0 +1,5 @@ + a +b + x + x + z diff --git a/tests/fixtures/sort/blanks.expected.debug b/tests/fixtures/sort/blanks.expected.debug new file mode 100644 index 000000000..28c8fc960 --- /dev/null +++ b/tests/fixtures/sort/blanks.expected.debug @@ -0,0 +1,15 @@ + a + _ +___ +b +_ +_ + x + _ +__________ + x + _ +___ + z + _ +__ diff --git a/tests/fixtures/sort/blanks.txt b/tests/fixtures/sort/blanks.txt new file mode 100644 index 000000000..eb2f3c94e --- /dev/null +++ b/tests/fixtures/sort/blanks.txt @@ -0,0 +1,5 @@ +b + a + z + x + x diff --git a/tests/fixtures/sort/default_unsorted_ints.expected.debug b/tests/fixtures/sort/default_unsorted_ints.expected.debug new file mode 100644 index 000000000..2bf082d3b --- /dev/null +++ b/tests/fixtures/sort/default_unsorted_ints.expected.debug @@ -0,0 +1,200 @@ +1 +_ +10 +__ +100 +___ +11 +__ +12 +__ +13 +__ +14 +__ +15 +__ +16 +__ +17 +__ +18 +__ +19 +__ +2 +_ +20 +__ +21 +__ +22 +__ +23 +__ +24 +__ +25 +__ +26 +__ +27 +__ +28 +__ +29 +__ +3 +_ +30 +__ +31 +__ +32 +__ +33 +__ +34 +__ +35 +__ +36 +__ +37 +__ +38 +__ +39 +__ +4 +_ +40 +__ +41 +__ +42 +__ +43 +__ +44 +__ +45 +__ +46 +__ +47 +__ +48 +__ +49 +__ +5 +_ +50 +__ +51 +__ +52 +__ +53 +__ +54 +__ +55 +__ +56 +__ +57 +__ +58 +__ +59 +__ +6 +_ +60 +__ +61 +__ +62 +__ +63 +__ +64 +__ +65 +__ +66 +__ +67 +__ +68 +__ +69 +__ +7 +_ +70 +__ +71 +__ +72 +__ +73 +__ +74 +__ +75 +__ +76 +__ +77 +__ +78 +__ +79 +__ +8 +_ +80 +__ +81 +__ +82 +__ +83 +__ +84 +__ +85 +__ +86 +__ +87 +__ +88 +__ +89 +__ +9 +_ +90 +__ +91 +__ +92 +__ +93 +__ +94 +__ +95 +__ +96 +__ +97 +__ +98 +__ +99 +__ diff --git a/tests/fixtures/sort/dictionary_order.expected.debug b/tests/fixtures/sort/dictionary_order.expected.debug new file mode 100644 index 000000000..f4a2d17db --- /dev/null +++ b/tests/fixtures/sort/dictionary_order.expected.debug @@ -0,0 +1,9 @@ +bbb +___ +___ +./bbc +_____ +_____ +bbd +___ +___ diff --git a/tests/fixtures/sort/exponents-positive-general.expected b/tests/fixtures/sort/exponents-positive-general.expected new file mode 100644 index 000000000..3dbc92fe5 --- /dev/null +++ b/tests/fixtures/sort/exponents-positive-general.expected @@ -0,0 +1,12 @@ + + + + + + +10E +1000EDKLD +10000K78 ++100000 +100E6 +50e10 diff --git a/tests/fixtures/sort/exponents-positive-general.txt b/tests/fixtures/sort/exponents-positive-general.txt new file mode 100644 index 000000000..23ea52771 --- /dev/null +++ b/tests/fixtures/sort/exponents-positive-general.txt @@ -0,0 +1,12 @@ +10000K78 +10E + + +1000EDKLD + + +100E6 + +50e10 ++100000 + diff --git a/tests/fixtures/sort/exponents-positive-numeric.expected b/tests/fixtures/sort/exponents-positive-numeric.expected new file mode 100644 index 000000000..174088f63 --- /dev/null +++ b/tests/fixtures/sort/exponents-positive-numeric.expected @@ -0,0 +1,12 @@ + + + + + + ++100000 +10E +50e10 +100E6 +1000EDKLD +10000K78 diff --git a/tests/fixtures/sort/exponents-positive-numeric.expected.debug b/tests/fixtures/sort/exponents-positive-numeric.expected.debug new file mode 100644 index 000000000..f5a32bad1 --- /dev/null +++ b/tests/fixtures/sort/exponents-positive-numeric.expected.debug @@ -0,0 +1,36 @@ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key ++100000 +^ no match for key +_______ +10E +__ +___ +50e10 +__ +_____ +100E6 +___ +_____ +1000EDKLD +____ +_________ +10000K78 +_____ +________ diff --git a/tests/fixtures/sort/exponents-positive-numeric.txt b/tests/fixtures/sort/exponents-positive-numeric.txt new file mode 100644 index 000000000..23ea52771 --- /dev/null +++ b/tests/fixtures/sort/exponents-positive-numeric.txt @@ -0,0 +1,12 @@ +10000K78 +10E + + +1000EDKLD + + +100E6 + +50e10 ++100000 + diff --git a/tests/fixtures/sort/exponents_general.expected b/tests/fixtures/sort/exponents_general.expected new file mode 100644 index 000000000..308a82e1e --- /dev/null +++ b/tests/fixtures/sort/exponents_general.expected @@ -0,0 +1,28 @@ + + + + + + + + + -12e-5555.5 +0b10 // binary not supported +0x10 // hexadecimal not supported, but it should be +55e-20 +55e-20.10 +5.5.5.5 +10E +64e+ +99e- +1000EDKLD +10000K78 ++100000 ++100000 +100E6 +100E6 +10e10e10e10 +13e+10 +45e+10.5 +50e10 +50e10 diff --git a/tests/fixtures/sort/exponents_general.expected.debug b/tests/fixtures/sort/exponents_general.expected.debug new file mode 100644 index 000000000..a7238d10e --- /dev/null +++ b/tests/fixtures/sort/exponents_general.expected.debug @@ -0,0 +1,84 @@ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + > -12e-5555.5 + _________ +______________ +0b10 // binary not supported +_ +____________________________ +0x10 // hexadecimal not supported, but it should be +_ +___________________________________________________ +55e-20 +______ +______ +55e-20.10 +______ +_________ +5.5.5.5 +___ +_______ +10E +__ +___ +64e+ +__ +____ +99e- +__ +____ +1000EDKLD +____ +_________ +10000K78 +_____ +________ ++100000 +_______ +_______ ++100000 +_______ +_______ +100E6 +_____ +_____ +100E6 +_____ +_____ +10e10e10e10 +_____ +___________ +13e+10 +______ +______ +45e+10.5 +______ +________ +50e10 +_____ +_____ +50e10 +_____ +_____ diff --git a/tests/fixtures/sort/exponents_general.txt b/tests/fixtures/sort/exponents_general.txt new file mode 100644 index 000000000..4a9bbba2e --- /dev/null +++ b/tests/fixtures/sort/exponents_general.txt @@ -0,0 +1,28 @@ +100E6 + +50e10 ++100000 + +10000K78 +0x10 // hexadecimal not supported, but it should be +10E +0b10 // binary not supported +64e+ +99e- + +45e+10.5 + +1000EDKLD + +13e+10 + +100E6 + +50e10 + -12e-5555.5 ++100000 + +10e10e10e10 +5.5.5.5 +55e-20 +55e-20.10 diff --git a/tests/fixtures/sort/ext_sort.expected b/tests/fixtures/sort/ext_sort.expected new file mode 100644 index 000000000..7599e0c96 --- /dev/null +++ b/tests/fixtures/sort/ext_sort.expected @@ -0,0 +1,20000 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103 +104 +105 +106 +107 +108 +109 +110 +111 +112 +113 +114 +115 +116 +117 +118 +119 +120 +121 +122 +123 +124 +125 +126 +127 +128 +129 +130 +131 +132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159 +160 +161 +162 +163 +164 +165 +166 +167 +168 +169 +170 +171 +172 +173 +174 +175 +176 +177 +178 +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260 +261 +262 +263 +264 +265 +266 +267 +268 +269 +270 +271 +272 +273 +274 +275 +276 +277 +278 +279 +280 +281 +282 +283 +284 +285 +286 +287 +288 +289 +290 +291 +292 +293 +294 +295 +296 +297 +298 +299 +300 +301 +302 +303 +304 +305 +306 +307 +308 +309 +310 +311 +312 +313 +314 +315 +316 +317 +318 +319 +320 +321 +322 +323 +324 +325 +326 +327 +328 +329 +330 +331 +332 +333 +334 +335 +336 +337 +338 +339 +340 +341 +342 +343 +344 +345 +346 +347 +348 +349 +350 +351 +352 +353 +354 +355 +356 +357 +358 +359 +360 +361 +362 +363 +364 +365 +366 +367 +368 +369 +370 +371 +372 +373 +374 +375 +376 +377 +378 +379 +380 +381 +382 +383 +384 +385 +386 +387 +388 +389 +390 +391 +392 +393 +394 +395 +396 +397 +398 +399 +400 +401 +402 +403 +404 +405 +406 +407 +408 +409 +410 +411 +412 +413 +414 +415 +416 +417 +418 +419 +420 +421 +422 +423 +424 +425 +426 +427 +428 +429 +430 +431 +432 +433 +434 +435 +436 +437 +438 +439 +440 +441 +442 +443 +444 +445 +446 +447 +448 +449 +450 +451 +452 +453 +454 +455 +456 +457 +458 +459 +460 +461 +462 +463 +464 +465 +466 +467 +468 +469 +470 +471 +472 +473 +474 +475 +476 +477 +478 +479 +480 +481 +482 +483 +484 +485 +486 +487 +488 +489 +490 +491 +492 +493 +494 +495 +496 +497 +498 +499 +500 +501 +502 +503 +504 +505 +506 +507 +508 +509 +510 +511 +512 +513 +514 +515 +516 +517 +518 +519 +520 +521 +522 +523 +524 +525 +526 +527 +528 +529 +530 +531 +532 +533 +534 +535 +536 +537 +538 +539 +540 +541 +542 +543 +544 +545 +546 +547 +548 +549 +550 +551 +552 +553 +554 +555 +556 +557 +558 +559 +560 +561 +562 +563 +564 +565 +566 +567 +568 +569 +570 +571 +572 +573 +574 +575 +576 +577 +578 +579 +580 +581 +582 +583 +584 +585 +586 +587 +588 +589 +590 +591 +592 +593 +594 +595 +596 +597 +598 +599 +600 +601 +602 +603 +604 +605 +606 +607 +608 +609 +610 +611 +612 +613 +614 +615 +616 +617 +618 +619 +620 +621 +622 +623 +624 +625 +626 +627 +628 +629 +630 +631 +632 +633 +634 +635 +636 +637 +638 +639 +640 +641 +642 +643 +644 +645 +646 +647 +648 +649 +650 +651 +652 +653 +654 +655 +656 +657 +658 +659 +660 +661 +662 +663 +664 +665 +666 +667 +668 +669 +670 +671 +672 +673 +674 +675 +676 +677 +678 +679 +680 +681 +682 +683 +684 +685 +686 +687 +688 +689 +690 +691 +692 +693 +694 +695 +696 +697 +698 +699 +700 +701 +702 +703 +704 +705 +706 +707 +708 +709 +710 +711 +712 +713 +714 +715 +716 +717 +718 +719 +720 +721 +722 +723 +724 +725 +726 +727 +728 +729 +730 +731 +732 +733 +734 +735 +736 +737 +738 +739 +740 +741 +742 +743 +744 +745 +746 +747 +748 +749 +750 +751 +752 +753 +754 +755 +756 +757 +758 +759 +760 +761 +762 +763 +764 +765 +766 +767 +768 +769 +770 +771 +772 +773 +774 +775 +776 +777 +778 +779 +780 +781 +782 +783 +784 +785 +786 +787 +788 +789 +790 +791 +792 +793 +794 +795 +796 +797 +798 +799 +800 +801 +802 +803 +804 +805 +806 +807 +808 +809 +810 +811 +812 +813 +814 +815 +816 +817 +818 +819 +820 +821 +822 +823 +824 +825 +826 +827 +828 +829 +830 +831 +832 +833 +834 +835 +836 +837 +838 +839 +840 +841 +842 +843 +844 +845 +846 +847 +848 +849 +850 +851 +852 +853 +854 +855 +856 +857 +858 +859 +860 +861 +862 +863 +864 +865 +866 +867 +868 +869 +870 +871 +872 +873 +874 +875 +876 +877 +878 +879 +880 +881 +882 +883 +884 +885 +886 +887 +888 +889 +890 +891 +892 +893 +894 +895 +896 +897 +898 +899 +900 +901 +902 +903 +904 +905 +906 +907 +908 +909 +910 +911 +912 +913 +914 +915 +916 +917 +918 +919 +920 +921 +922 +923 +924 +925 +926 +927 +928 +929 +930 +931 +932 +933 +934 +935 +936 +937 +938 +939 +940 +941 +942 +943 +944 +945 +946 +947 +948 +949 +950 +951 +952 +953 +954 +955 +956 +957 +958 +959 +960 +961 +962 +963 +964 +965 +966 +967 +968 +969 +970 +971 +972 +973 +974 +975 +976 +977 +978 +979 +980 +981 +982 +983 +984 +985 +986 +987 +988 +989 +990 +991 +992 +993 +994 +995 +996 +997 +998 +999 +1000 +1001 +1002 +1003 +1004 +1005 +1006 +1007 +1008 +1009 +1010 +1011 +1012 +1013 +1014 +1015 +1016 +1017 +1018 +1019 +1020 +1021 +1022 +1023 +1024 +1025 +1026 +1027 +1028 +1029 +1030 +1031 +1032 +1033 +1034 +1035 +1036 +1037 +1038 +1039 +1040 +1041 +1042 +1043 +1044 +1045 +1046 +1047 +1048 +1049 +1050 +1051 +1052 +1053 +1054 +1055 +1056 +1057 +1058 +1059 +1060 +1061 +1062 +1063 +1064 +1065 +1066 +1067 +1068 +1069 +1070 +1071 +1072 +1073 +1074 +1075 +1076 +1077 +1078 +1079 +1080 +1081 +1082 +1083 +1084 +1085 +1086 +1087 +1088 +1089 +1090 +1091 +1092 +1093 +1094 +1095 +1096 +1097 +1098 +1099 +1100 +1101 +1102 +1103 +1104 +1105 +1106 +1107 +1108 +1109 +1110 +1111 +1112 +1113 +1114 +1115 +1116 +1117 +1118 +1119 +1120 +1121 +1122 +1123 +1124 +1125 +1126 +1127 +1128 +1129 +1130 +1131 +1132 +1133 +1134 +1135 +1136 +1137 +1138 +1139 +1140 +1141 +1142 +1143 +1144 +1145 +1146 +1147 +1148 +1149 +1150 +1151 +1152 +1153 +1154 +1155 +1156 +1157 +1158 +1159 +1160 +1161 +1162 +1163 +1164 +1165 +1166 +1167 +1168 +1169 +1170 +1171 +1172 +1173 +1174 +1175 +1176 +1177 +1178 +1179 +1180 +1181 +1182 +1183 +1184 +1185 +1186 +1187 +1188 +1189 +1190 +1191 +1192 +1193 +1194 +1195 +1196 +1197 +1198 +1199 +1200 +1201 +1202 +1203 +1204 +1205 +1206 +1207 +1208 +1209 +1210 +1211 +1212 +1213 +1214 +1215 +1216 +1217 +1218 +1219 +1220 +1221 +1222 +1223 +1224 +1225 +1226 +1227 +1228 +1229 +1230 +1231 +1232 +1233 +1234 +1235 +1236 +1237 +1238 +1239 +1240 +1241 +1242 +1243 +1244 +1245 +1246 +1247 +1248 +1249 +1250 +1251 +1252 +1253 +1254 +1255 +1256 +1257 +1258 +1259 +1260 +1261 +1262 +1263 +1264 +1265 +1266 +1267 +1268 +1269 +1270 +1271 +1272 +1273 +1274 +1275 +1276 +1277 +1278 +1279 +1280 +1281 +1282 +1283 +1284 +1285 +1286 +1287 +1288 +1289 +1290 +1291 +1292 +1293 +1294 +1295 +1296 +1297 +1298 +1299 +1300 +1301 +1302 +1303 +1304 +1305 +1306 +1307 +1308 +1309 +1310 +1311 +1312 +1313 +1314 +1315 +1316 +1317 +1318 +1319 +1320 +1321 +1322 +1323 +1324 +1325 +1326 +1327 +1328 +1329 +1330 +1331 +1332 +1333 +1334 +1335 +1336 +1337 +1338 +1339 +1340 +1341 +1342 +1343 +1344 +1345 +1346 +1347 +1348 +1349 +1350 +1351 +1352 +1353 +1354 +1355 +1356 +1357 +1358 +1359 +1360 +1361 +1362 +1363 +1364 +1365 +1366 +1367 +1368 +1369 +1370 +1371 +1372 +1373 +1374 +1375 +1376 +1377 +1378 +1379 +1380 +1381 +1382 +1383 +1384 +1385 +1386 +1387 +1388 +1389 +1390 +1391 +1392 +1393 +1394 +1395 +1396 +1397 +1398 +1399 +1400 +1401 +1402 +1403 +1404 +1405 +1406 +1407 +1408 +1409 +1410 +1411 +1412 +1413 +1414 +1415 +1416 +1417 +1418 +1419 +1420 +1421 +1422 +1423 +1424 +1425 +1426 +1427 +1428 +1429 +1430 +1431 +1432 +1433 +1434 +1435 +1436 +1437 +1438 +1439 +1440 +1441 +1442 +1443 +1444 +1445 +1446 +1447 +1448 +1449 +1450 +1451 +1452 +1453 +1454 +1455 +1456 +1457 +1458 +1459 +1460 +1461 +1462 +1463 +1464 +1465 +1466 +1467 +1468 +1469 +1470 +1471 +1472 +1473 +1474 +1475 +1476 +1477 +1478 +1479 +1480 +1481 +1482 +1483 +1484 +1485 +1486 +1487 +1488 +1489 +1490 +1491 +1492 +1493 +1494 +1495 +1496 +1497 +1498 +1499 +1500 +1501 +1502 +1503 +1504 +1505 +1506 +1507 +1508 +1509 +1510 +1511 +1512 +1513 +1514 +1515 +1516 +1517 +1518 +1519 +1520 +1521 +1522 +1523 +1524 +1525 +1526 +1527 +1528 +1529 +1530 +1531 +1532 +1533 +1534 +1535 +1536 +1537 +1538 +1539 +1540 +1541 +1542 +1543 +1544 +1545 +1546 +1547 +1548 +1549 +1550 +1551 +1552 +1553 +1554 +1555 +1556 +1557 +1558 +1559 +1560 +1561 +1562 +1563 +1564 +1565 +1566 +1567 +1568 +1569 +1570 +1571 +1572 +1573 +1574 +1575 +1576 +1577 +1578 +1579 +1580 +1581 +1582 +1583 +1584 +1585 +1586 +1587 +1588 +1589 +1590 +1591 +1592 +1593 +1594 +1595 +1596 +1597 +1598 +1599 +1600 +1601 +1602 +1603 +1604 +1605 +1606 +1607 +1608 +1609 +1610 +1611 +1612 +1613 +1614 +1615 +1616 +1617 +1618 +1619 +1620 +1621 +1622 +1623 +1624 +1625 +1626 +1627 +1628 +1629 +1630 +1631 +1632 +1633 +1634 +1635 +1636 +1637 +1638 +1639 +1640 +1641 +1642 +1643 +1644 +1645 +1646 +1647 +1648 +1649 +1650 +1651 +1652 +1653 +1654 +1655 +1656 +1657 +1658 +1659 +1660 +1661 +1662 +1663 +1664 +1665 +1666 +1667 +1668 +1669 +1670 +1671 +1672 +1673 +1674 +1675 +1676 +1677 +1678 +1679 +1680 +1681 +1682 +1683 +1684 +1685 +1686 +1687 +1688 +1689 +1690 +1691 +1692 +1693 +1694 +1695 +1696 +1697 +1698 +1699 +1700 +1701 +1702 +1703 +1704 +1705 +1706 +1707 +1708 +1709 +1710 +1711 +1712 +1713 +1714 +1715 +1716 +1717 +1718 +1719 +1720 +1721 +1722 +1723 +1724 +1725 +1726 +1727 +1728 +1729 +1730 +1731 +1732 +1733 +1734 +1735 +1736 +1737 +1738 +1739 +1740 +1741 +1742 +1743 +1744 +1745 +1746 +1747 +1748 +1749 +1750 +1751 +1752 +1753 +1754 +1755 +1756 +1757 +1758 +1759 +1760 +1761 +1762 +1763 +1764 +1765 +1766 +1767 +1768 +1769 +1770 +1771 +1772 +1773 +1774 +1775 +1776 +1777 +1778 +1779 +1780 +1781 +1782 +1783 +1784 +1785 +1786 +1787 +1788 +1789 +1790 +1791 +1792 +1793 +1794 +1795 +1796 +1797 +1798 +1799 +1800 +1801 +1802 +1803 +1804 +1805 +1806 +1807 +1808 +1809 +1810 +1811 +1812 +1813 +1814 +1815 +1816 +1817 +1818 +1819 +1820 +1821 +1822 +1823 +1824 +1825 +1826 +1827 +1828 +1829 +1830 +1831 +1832 +1833 +1834 +1835 +1836 +1837 +1838 +1839 +1840 +1841 +1842 +1843 +1844 +1845 +1846 +1847 +1848 +1849 +1850 +1851 +1852 +1853 +1854 +1855 +1856 +1857 +1858 +1859 +1860 +1861 +1862 +1863 +1864 +1865 +1866 +1867 +1868 +1869 +1870 +1871 +1872 +1873 +1874 +1875 +1876 +1877 +1878 +1879 +1880 +1881 +1882 +1883 +1884 +1885 +1886 +1887 +1888 +1889 +1890 +1891 +1892 +1893 +1894 +1895 +1896 +1897 +1898 +1899 +1900 +1901 +1902 +1903 +1904 +1905 +1906 +1907 +1908 +1909 +1910 +1911 +1912 +1913 +1914 +1915 +1916 +1917 +1918 +1919 +1920 +1921 +1922 +1923 +1924 +1925 +1926 +1927 +1928 +1929 +1930 +1931 +1932 +1933 +1934 +1935 +1936 +1937 +1938 +1939 +1940 +1941 +1942 +1943 +1944 +1945 +1946 +1947 +1948 +1949 +1950 +1951 +1952 +1953 +1954 +1955 +1956 +1957 +1958 +1959 +1960 +1961 +1962 +1963 +1964 +1965 +1966 +1967 +1968 +1969 +1970 +1971 +1972 +1973 +1974 +1975 +1976 +1977 +1978 +1979 +1980 +1981 +1982 +1983 +1984 +1985 +1986 +1987 +1988 +1989 +1990 +1991 +1992 +1993 +1994 +1995 +1996 +1997 +1998 +1999 +2000 +2001 +2002 +2003 +2004 +2005 +2006 +2007 +2008 +2009 +2010 +2011 +2012 +2013 +2014 +2015 +2016 +2017 +2018 +2019 +2020 +2021 +2022 +2023 +2024 +2025 +2026 +2027 +2028 +2029 +2030 +2031 +2032 +2033 +2034 +2035 +2036 +2037 +2038 +2039 +2040 +2041 +2042 +2043 +2044 +2045 +2046 +2047 +2048 +2049 +2050 +2051 +2052 +2053 +2054 +2055 +2056 +2057 +2058 +2059 +2060 +2061 +2062 +2063 +2064 +2065 +2066 +2067 +2068 +2069 +2070 +2071 +2072 +2073 +2074 +2075 +2076 +2077 +2078 +2079 +2080 +2081 +2082 +2083 +2084 +2085 +2086 +2087 +2088 +2089 +2090 +2091 +2092 +2093 +2094 +2095 +2096 +2097 +2098 +2099 +2100 +2101 +2102 +2103 +2104 +2105 +2106 +2107 +2108 +2109 +2110 +2111 +2112 +2113 +2114 +2115 +2116 +2117 +2118 +2119 +2120 +2121 +2122 +2123 +2124 +2125 +2126 +2127 +2128 +2129 +2130 +2131 +2132 +2133 +2134 +2135 +2136 +2137 +2138 +2139 +2140 +2141 +2142 +2143 +2144 +2145 +2146 +2147 +2148 +2149 +2150 +2151 +2152 +2153 +2154 +2155 +2156 +2157 +2158 +2159 +2160 +2161 +2162 +2163 +2164 +2165 +2166 +2167 +2168 +2169 +2170 +2171 +2172 +2173 +2174 +2175 +2176 +2177 +2178 +2179 +2180 +2181 +2182 +2183 +2184 +2185 +2186 +2187 +2188 +2189 +2190 +2191 +2192 +2193 +2194 +2195 +2196 +2197 +2198 +2199 +2200 +2201 +2202 +2203 +2204 +2205 +2206 +2207 +2208 +2209 +2210 +2211 +2212 +2213 +2214 +2215 +2216 +2217 +2218 +2219 +2220 +2221 +2222 +2223 +2224 +2225 +2226 +2227 +2228 +2229 +2230 +2231 +2232 +2233 +2234 +2235 +2236 +2237 +2238 +2239 +2240 +2241 +2242 +2243 +2244 +2245 +2246 +2247 +2248 +2249 +2250 +2251 +2252 +2253 +2254 +2255 +2256 +2257 +2258 +2259 +2260 +2261 +2262 +2263 +2264 +2265 +2266 +2267 +2268 +2269 +2270 +2271 +2272 +2273 +2274 +2275 +2276 +2277 +2278 +2279 +2280 +2281 +2282 +2283 +2284 +2285 +2286 +2287 +2288 +2289 +2290 +2291 +2292 +2293 +2294 +2295 +2296 +2297 +2298 +2299 +2300 +2301 +2302 +2303 +2304 +2305 +2306 +2307 +2308 +2309 +2310 +2311 +2312 +2313 +2314 +2315 +2316 +2317 +2318 +2319 +2320 +2321 +2322 +2323 +2324 +2325 +2326 +2327 +2328 +2329 +2330 +2331 +2332 +2333 +2334 +2335 +2336 +2337 +2338 +2339 +2340 +2341 +2342 +2343 +2344 +2345 +2346 +2347 +2348 +2349 +2350 +2351 +2352 +2353 +2354 +2355 +2356 +2357 +2358 +2359 +2360 +2361 +2362 +2363 +2364 +2365 +2366 +2367 +2368 +2369 +2370 +2371 +2372 +2373 +2374 +2375 +2376 +2377 +2378 +2379 +2380 +2381 +2382 +2383 +2384 +2385 +2386 +2387 +2388 +2389 +2390 +2391 +2392 +2393 +2394 +2395 +2396 +2397 +2398 +2399 +2400 +2401 +2402 +2403 +2404 +2405 +2406 +2407 +2408 +2409 +2410 +2411 +2412 +2413 +2414 +2415 +2416 +2417 +2418 +2419 +2420 +2421 +2422 +2423 +2424 +2425 +2426 +2427 +2428 +2429 +2430 +2431 +2432 +2433 +2434 +2435 +2436 +2437 +2438 +2439 +2440 +2441 +2442 +2443 +2444 +2445 +2446 +2447 +2448 +2449 +2450 +2451 +2452 +2453 +2454 +2455 +2456 +2457 +2458 +2459 +2460 +2461 +2462 +2463 +2464 +2465 +2466 +2467 +2468 +2469 +2470 +2471 +2472 +2473 +2474 +2475 +2476 +2477 +2478 +2479 +2480 +2481 +2482 +2483 +2484 +2485 +2486 +2487 +2488 +2489 +2490 +2491 +2492 +2493 +2494 +2495 +2496 +2497 +2498 +2499 +2500 +2501 +2502 +2503 +2504 +2505 +2506 +2507 +2508 +2509 +2510 +2511 +2512 +2513 +2514 +2515 +2516 +2517 +2518 +2519 +2520 +2521 +2522 +2523 +2524 +2525 +2526 +2527 +2528 +2529 +2530 +2531 +2532 +2533 +2534 +2535 +2536 +2537 +2538 +2539 +2540 +2541 +2542 +2543 +2544 +2545 +2546 +2547 +2548 +2549 +2550 +2551 +2552 +2553 +2554 +2555 +2556 +2557 +2558 +2559 +2560 +2561 +2562 +2563 +2564 +2565 +2566 +2567 +2568 +2569 +2570 +2571 +2572 +2573 +2574 +2575 +2576 +2577 +2578 +2579 +2580 +2581 +2582 +2583 +2584 +2585 +2586 +2587 +2588 +2589 +2590 +2591 +2592 +2593 +2594 +2595 +2596 +2597 +2598 +2599 +2600 +2601 +2602 +2603 +2604 +2605 +2606 +2607 +2608 +2609 +2610 +2611 +2612 +2613 +2614 +2615 +2616 +2617 +2618 +2619 +2620 +2621 +2622 +2623 +2624 +2625 +2626 +2627 +2628 +2629 +2630 +2631 +2632 +2633 +2634 +2635 +2636 +2637 +2638 +2639 +2640 +2641 +2642 +2643 +2644 +2645 +2646 +2647 +2648 +2649 +2650 +2651 +2652 +2653 +2654 +2655 +2656 +2657 +2658 +2659 +2660 +2661 +2662 +2663 +2664 +2665 +2666 +2667 +2668 +2669 +2670 +2671 +2672 +2673 +2674 +2675 +2676 +2677 +2678 +2679 +2680 +2681 +2682 +2683 +2684 +2685 +2686 +2687 +2688 +2689 +2690 +2691 +2692 +2693 +2694 +2695 +2696 +2697 +2698 +2699 +2700 +2701 +2702 +2703 +2704 +2705 +2706 +2707 +2708 +2709 +2710 +2711 +2712 +2713 +2714 +2715 +2716 +2717 +2718 +2719 +2720 +2721 +2722 +2723 +2724 +2725 +2726 +2727 +2728 +2729 +2730 +2731 +2732 +2733 +2734 +2735 +2736 +2737 +2738 +2739 +2740 +2741 +2742 +2743 +2744 +2745 +2746 +2747 +2748 +2749 +2750 +2751 +2752 +2753 +2754 +2755 +2756 +2757 +2758 +2759 +2760 +2761 +2762 +2763 +2764 +2765 +2766 +2767 +2768 +2769 +2770 +2771 +2772 +2773 +2774 +2775 +2776 +2777 +2778 +2779 +2780 +2781 +2782 +2783 +2784 +2785 +2786 +2787 +2788 +2789 +2790 +2791 +2792 +2793 +2794 +2795 +2796 +2797 +2798 +2799 +2800 +2801 +2802 +2803 +2804 +2805 +2806 +2807 +2808 +2809 +2810 +2811 +2812 +2813 +2814 +2815 +2816 +2817 +2818 +2819 +2820 +2821 +2822 +2823 +2824 +2825 +2826 +2827 +2828 +2829 +2830 +2831 +2832 +2833 +2834 +2835 +2836 +2837 +2838 +2839 +2840 +2841 +2842 +2843 +2844 +2845 +2846 +2847 +2848 +2849 +2850 +2851 +2852 +2853 +2854 +2855 +2856 +2857 +2858 +2859 +2860 +2861 +2862 +2863 +2864 +2865 +2866 +2867 +2868 +2869 +2870 +2871 +2872 +2873 +2874 +2875 +2876 +2877 +2878 +2879 +2880 +2881 +2882 +2883 +2884 +2885 +2886 +2887 +2888 +2889 +2890 +2891 +2892 +2893 +2894 +2895 +2896 +2897 +2898 +2899 +2900 +2901 +2902 +2903 +2904 +2905 +2906 +2907 +2908 +2909 +2910 +2911 +2912 +2913 +2914 +2915 +2916 +2917 +2918 +2919 +2920 +2921 +2922 +2923 +2924 +2925 +2926 +2927 +2928 +2929 +2930 +2931 +2932 +2933 +2934 +2935 +2936 +2937 +2938 +2939 +2940 +2941 +2942 +2943 +2944 +2945 +2946 +2947 +2948 +2949 +2950 +2951 +2952 +2953 +2954 +2955 +2956 +2957 +2958 +2959 +2960 +2961 +2962 +2963 +2964 +2965 +2966 +2967 +2968 +2969 +2970 +2971 +2972 +2973 +2974 +2975 +2976 +2977 +2978 +2979 +2980 +2981 +2982 +2983 +2984 +2985 +2986 +2987 +2988 +2989 +2990 +2991 +2992 +2993 +2994 +2995 +2996 +2997 +2998 +2999 +3000 +3001 +3002 +3003 +3004 +3005 +3006 +3007 +3008 +3009 +3010 +3011 +3012 +3013 +3014 +3015 +3016 +3017 +3018 +3019 +3020 +3021 +3022 +3023 +3024 +3025 +3026 +3027 +3028 +3029 +3030 +3031 +3032 +3033 +3034 +3035 +3036 +3037 +3038 +3039 +3040 +3041 +3042 +3043 +3044 +3045 +3046 +3047 +3048 +3049 +3050 +3051 +3052 +3053 +3054 +3055 +3056 +3057 +3058 +3059 +3060 +3061 +3062 +3063 +3064 +3065 +3066 +3067 +3068 +3069 +3070 +3071 +3072 +3073 +3074 +3075 +3076 +3077 +3078 +3079 +3080 +3081 +3082 +3083 +3084 +3085 +3086 +3087 +3088 +3089 +3090 +3091 +3092 +3093 +3094 +3095 +3096 +3097 +3098 +3099 +3100 +3101 +3102 +3103 +3104 +3105 +3106 +3107 +3108 +3109 +3110 +3111 +3112 +3113 +3114 +3115 +3116 +3117 +3118 +3119 +3120 +3121 +3122 +3123 +3124 +3125 +3126 +3127 +3128 +3129 +3130 +3131 +3132 +3133 +3134 +3135 +3136 +3137 +3138 +3139 +3140 +3141 +3142 +3143 +3144 +3145 +3146 +3147 +3148 +3149 +3150 +3151 +3152 +3153 +3154 +3155 +3156 +3157 +3158 +3159 +3160 +3161 +3162 +3163 +3164 +3165 +3166 +3167 +3168 +3169 +3170 +3171 +3172 +3173 +3174 +3175 +3176 +3177 +3178 +3179 +3180 +3181 +3182 +3183 +3184 +3185 +3186 +3187 +3188 +3189 +3190 +3191 +3192 +3193 +3194 +3195 +3196 +3197 +3198 +3199 +3200 +3201 +3202 +3203 +3204 +3205 +3206 +3207 +3208 +3209 +3210 +3211 +3212 +3213 +3214 +3215 +3216 +3217 +3218 +3219 +3220 +3221 +3222 +3223 +3224 +3225 +3226 +3227 +3228 +3229 +3230 +3231 +3232 +3233 +3234 +3235 +3236 +3237 +3238 +3239 +3240 +3241 +3242 +3243 +3244 +3245 +3246 +3247 +3248 +3249 +3250 +3251 +3252 +3253 +3254 +3255 +3256 +3257 +3258 +3259 +3260 +3261 +3262 +3263 +3264 +3265 +3266 +3267 +3268 +3269 +3270 +3271 +3272 +3273 +3274 +3275 +3276 +3277 +3278 +3279 +3280 +3281 +3282 +3283 +3284 +3285 +3286 +3287 +3288 +3289 +3290 +3291 +3292 +3293 +3294 +3295 +3296 +3297 +3298 +3299 +3300 +3301 +3302 +3303 +3304 +3305 +3306 +3307 +3308 +3309 +3310 +3311 +3312 +3313 +3314 +3315 +3316 +3317 +3318 +3319 +3320 +3321 +3322 +3323 +3324 +3325 +3326 +3327 +3328 +3329 +3330 +3331 +3332 +3333 +3334 +3335 +3336 +3337 +3338 +3339 +3340 +3341 +3342 +3343 +3344 +3345 +3346 +3347 +3348 +3349 +3350 +3351 +3352 +3353 +3354 +3355 +3356 +3357 +3358 +3359 +3360 +3361 +3362 +3363 +3364 +3365 +3366 +3367 +3368 +3369 +3370 +3371 +3372 +3373 +3374 +3375 +3376 +3377 +3378 +3379 +3380 +3381 +3382 +3383 +3384 +3385 +3386 +3387 +3388 +3389 +3390 +3391 +3392 +3393 +3394 +3395 +3396 +3397 +3398 +3399 +3400 +3401 +3402 +3403 +3404 +3405 +3406 +3407 +3408 +3409 +3410 +3411 +3412 +3413 +3414 +3415 +3416 +3417 +3418 +3419 +3420 +3421 +3422 +3423 +3424 +3425 +3426 +3427 +3428 +3429 +3430 +3431 +3432 +3433 +3434 +3435 +3436 +3437 +3438 +3439 +3440 +3441 +3442 +3443 +3444 +3445 +3446 +3447 +3448 +3449 +3450 +3451 +3452 +3453 +3454 +3455 +3456 +3457 +3458 +3459 +3460 +3461 +3462 +3463 +3464 +3465 +3466 +3467 +3468 +3469 +3470 +3471 +3472 +3473 +3474 +3475 +3476 +3477 +3478 +3479 +3480 +3481 +3482 +3483 +3484 +3485 +3486 +3487 +3488 +3489 +3490 +3491 +3492 +3493 +3494 +3495 +3496 +3497 +3498 +3499 +3500 +3501 +3502 +3503 +3504 +3505 +3506 +3507 +3508 +3509 +3510 +3511 +3512 +3513 +3514 +3515 +3516 +3517 +3518 +3519 +3520 +3521 +3522 +3523 +3524 +3525 +3526 +3527 +3528 +3529 +3530 +3531 +3532 +3533 +3534 +3535 +3536 +3537 +3538 +3539 +3540 +3541 +3542 +3543 +3544 +3545 +3546 +3547 +3548 +3549 +3550 +3551 +3552 +3553 +3554 +3555 +3556 +3557 +3558 +3559 +3560 +3561 +3562 +3563 +3564 +3565 +3566 +3567 +3568 +3569 +3570 +3571 +3572 +3573 +3574 +3575 +3576 +3577 +3578 +3579 +3580 +3581 +3582 +3583 +3584 +3585 +3586 +3587 +3588 +3589 +3590 +3591 +3592 +3593 +3594 +3595 +3596 +3597 +3598 +3599 +3600 +3601 +3602 +3603 +3604 +3605 +3606 +3607 +3608 +3609 +3610 +3611 +3612 +3613 +3614 +3615 +3616 +3617 +3618 +3619 +3620 +3621 +3622 +3623 +3624 +3625 +3626 +3627 +3628 +3629 +3630 +3631 +3632 +3633 +3634 +3635 +3636 +3637 +3638 +3639 +3640 +3641 +3642 +3643 +3644 +3645 +3646 +3647 +3648 +3649 +3650 +3651 +3652 +3653 +3654 +3655 +3656 +3657 +3658 +3659 +3660 +3661 +3662 +3663 +3664 +3665 +3666 +3667 +3668 +3669 +3670 +3671 +3672 +3673 +3674 +3675 +3676 +3677 +3678 +3679 +3680 +3681 +3682 +3683 +3684 +3685 +3686 +3687 +3688 +3689 +3690 +3691 +3692 +3693 +3694 +3695 +3696 +3697 +3698 +3699 +3700 +3701 +3702 +3703 +3704 +3705 +3706 +3707 +3708 +3709 +3710 +3711 +3712 +3713 +3714 +3715 +3716 +3717 +3718 +3719 +3720 +3721 +3722 +3723 +3724 +3725 +3726 +3727 +3728 +3729 +3730 +3731 +3732 +3733 +3734 +3735 +3736 +3737 +3738 +3739 +3740 +3741 +3742 +3743 +3744 +3745 +3746 +3747 +3748 +3749 +3750 +3751 +3752 +3753 +3754 +3755 +3756 +3757 +3758 +3759 +3760 +3761 +3762 +3763 +3764 +3765 +3766 +3767 +3768 +3769 +3770 +3771 +3772 +3773 +3774 +3775 +3776 +3777 +3778 +3779 +3780 +3781 +3782 +3783 +3784 +3785 +3786 +3787 +3788 +3789 +3790 +3791 +3792 +3793 +3794 +3795 +3796 +3797 +3798 +3799 +3800 +3801 +3802 +3803 +3804 +3805 +3806 +3807 +3808 +3809 +3810 +3811 +3812 +3813 +3814 +3815 +3816 +3817 +3818 +3819 +3820 +3821 +3822 +3823 +3824 +3825 +3826 +3827 +3828 +3829 +3830 +3831 +3832 +3833 +3834 +3835 +3836 +3837 +3838 +3839 +3840 +3841 +3842 +3843 +3844 +3845 +3846 +3847 +3848 +3849 +3850 +3851 +3852 +3853 +3854 +3855 +3856 +3857 +3858 +3859 +3860 +3861 +3862 +3863 +3864 +3865 +3866 +3867 +3868 +3869 +3870 +3871 +3872 +3873 +3874 +3875 +3876 +3877 +3878 +3879 +3880 +3881 +3882 +3883 +3884 +3885 +3886 +3887 +3888 +3889 +3890 +3891 +3892 +3893 +3894 +3895 +3896 +3897 +3898 +3899 +3900 +3901 +3902 +3903 +3904 +3905 +3906 +3907 +3908 +3909 +3910 +3911 +3912 +3913 +3914 +3915 +3916 +3917 +3918 +3919 +3920 +3921 +3922 +3923 +3924 +3925 +3926 +3927 +3928 +3929 +3930 +3931 +3932 +3933 +3934 +3935 +3936 +3937 +3938 +3939 +3940 +3941 +3942 +3943 +3944 +3945 +3946 +3947 +3948 +3949 +3950 +3951 +3952 +3953 +3954 +3955 +3956 +3957 +3958 +3959 +3960 +3961 +3962 +3963 +3964 +3965 +3966 +3967 +3968 +3969 +3970 +3971 +3972 +3973 +3974 +3975 +3976 +3977 +3978 +3979 +3980 +3981 +3982 +3983 +3984 +3985 +3986 +3987 +3988 +3989 +3990 +3991 +3992 +3993 +3994 +3995 +3996 +3997 +3998 +3999 +4000 +4001 +4002 +4003 +4004 +4005 +4006 +4007 +4008 +4009 +4010 +4011 +4012 +4013 +4014 +4015 +4016 +4017 +4018 +4019 +4020 +4021 +4022 +4023 +4024 +4025 +4026 +4027 +4028 +4029 +4030 +4031 +4032 +4033 +4034 +4035 +4036 +4037 +4038 +4039 +4040 +4041 +4042 +4043 +4044 +4045 +4046 +4047 +4048 +4049 +4050 +4051 +4052 +4053 +4054 +4055 +4056 +4057 +4058 +4059 +4060 +4061 +4062 +4063 +4064 +4065 +4066 +4067 +4068 +4069 +4070 +4071 +4072 +4073 +4074 +4075 +4076 +4077 +4078 +4079 +4080 +4081 +4082 +4083 +4084 +4085 +4086 +4087 +4088 +4089 +4090 +4091 +4092 +4093 +4094 +4095 +4096 +4097 +4098 +4099 +4100 +4101 +4102 +4103 +4104 +4105 +4106 +4107 +4108 +4109 +4110 +4111 +4112 +4113 +4114 +4115 +4116 +4117 +4118 +4119 +4120 +4121 +4122 +4123 +4124 +4125 +4126 +4127 +4128 +4129 +4130 +4131 +4132 +4133 +4134 +4135 +4136 +4137 +4138 +4139 +4140 +4141 +4142 +4143 +4144 +4145 +4146 +4147 +4148 +4149 +4150 +4151 +4152 +4153 +4154 +4155 +4156 +4157 +4158 +4159 +4160 +4161 +4162 +4163 +4164 +4165 +4166 +4167 +4168 +4169 +4170 +4171 +4172 +4173 +4174 +4175 +4176 +4177 +4178 +4179 +4180 +4181 +4182 +4183 +4184 +4185 +4186 +4187 +4188 +4189 +4190 +4191 +4192 +4193 +4194 +4195 +4196 +4197 +4198 +4199 +4200 +4201 +4202 +4203 +4204 +4205 +4206 +4207 +4208 +4209 +4210 +4211 +4212 +4213 +4214 +4215 +4216 +4217 +4218 +4219 +4220 +4221 +4222 +4223 +4224 +4225 +4226 +4227 +4228 +4229 +4230 +4231 +4232 +4233 +4234 +4235 +4236 +4237 +4238 +4239 +4240 +4241 +4242 +4243 +4244 +4245 +4246 +4247 +4248 +4249 +4250 +4251 +4252 +4253 +4254 +4255 +4256 +4257 +4258 +4259 +4260 +4261 +4262 +4263 +4264 +4265 +4266 +4267 +4268 +4269 +4270 +4271 +4272 +4273 +4274 +4275 +4276 +4277 +4278 +4279 +4280 +4281 +4282 +4283 +4284 +4285 +4286 +4287 +4288 +4289 +4290 +4291 +4292 +4293 +4294 +4295 +4296 +4297 +4298 +4299 +4300 +4301 +4302 +4303 +4304 +4305 +4306 +4307 +4308 +4309 +4310 +4311 +4312 +4313 +4314 +4315 +4316 +4317 +4318 +4319 +4320 +4321 +4322 +4323 +4324 +4325 +4326 +4327 +4328 +4329 +4330 +4331 +4332 +4333 +4334 +4335 +4336 +4337 +4338 +4339 +4340 +4341 +4342 +4343 +4344 +4345 +4346 +4347 +4348 +4349 +4350 +4351 +4352 +4353 +4354 +4355 +4356 +4357 +4358 +4359 +4360 +4361 +4362 +4363 +4364 +4365 +4366 +4367 +4368 +4369 +4370 +4371 +4372 +4373 +4374 +4375 +4376 +4377 +4378 +4379 +4380 +4381 +4382 +4383 +4384 +4385 +4386 +4387 +4388 +4389 +4390 +4391 +4392 +4393 +4394 +4395 +4396 +4397 +4398 +4399 +4400 +4401 +4402 +4403 +4404 +4405 +4406 +4407 +4408 +4409 +4410 +4411 +4412 +4413 +4414 +4415 +4416 +4417 +4418 +4419 +4420 +4421 +4422 +4423 +4424 +4425 +4426 +4427 +4428 +4429 +4430 +4431 +4432 +4433 +4434 +4435 +4436 +4437 +4438 +4439 +4440 +4441 +4442 +4443 +4444 +4445 +4446 +4447 +4448 +4449 +4450 +4451 +4452 +4453 +4454 +4455 +4456 +4457 +4458 +4459 +4460 +4461 +4462 +4463 +4464 +4465 +4466 +4467 +4468 +4469 +4470 +4471 +4472 +4473 +4474 +4475 +4476 +4477 +4478 +4479 +4480 +4481 +4482 +4483 +4484 +4485 +4486 +4487 +4488 +4489 +4490 +4491 +4492 +4493 +4494 +4495 +4496 +4497 +4498 +4499 +4500 +4501 +4502 +4503 +4504 +4505 +4506 +4507 +4508 +4509 +4510 +4511 +4512 +4513 +4514 +4515 +4516 +4517 +4518 +4519 +4520 +4521 +4522 +4523 +4524 +4525 +4526 +4527 +4528 +4529 +4530 +4531 +4532 +4533 +4534 +4535 +4536 +4537 +4538 +4539 +4540 +4541 +4542 +4543 +4544 +4545 +4546 +4547 +4548 +4549 +4550 +4551 +4552 +4553 +4554 +4555 +4556 +4557 +4558 +4559 +4560 +4561 +4562 +4563 +4564 +4565 +4566 +4567 +4568 +4569 +4570 +4571 +4572 +4573 +4574 +4575 +4576 +4577 +4578 +4579 +4580 +4581 +4582 +4583 +4584 +4585 +4586 +4587 +4588 +4589 +4590 +4591 +4592 +4593 +4594 +4595 +4596 +4597 +4598 +4599 +4600 +4601 +4602 +4603 +4604 +4605 +4606 +4607 +4608 +4609 +4610 +4611 +4612 +4613 +4614 +4615 +4616 +4617 +4618 +4619 +4620 +4621 +4622 +4623 +4624 +4625 +4626 +4627 +4628 +4629 +4630 +4631 +4632 +4633 +4634 +4635 +4636 +4637 +4638 +4639 +4640 +4641 +4642 +4643 +4644 +4645 +4646 +4647 +4648 +4649 +4650 +4651 +4652 +4653 +4654 +4655 +4656 +4657 +4658 +4659 +4660 +4661 +4662 +4663 +4664 +4665 +4666 +4667 +4668 +4669 +4670 +4671 +4672 +4673 +4674 +4675 +4676 +4677 +4678 +4679 +4680 +4681 +4682 +4683 +4684 +4685 +4686 +4687 +4688 +4689 +4690 +4691 +4692 +4693 +4694 +4695 +4696 +4697 +4698 +4699 +4700 +4701 +4702 +4703 +4704 +4705 +4706 +4707 +4708 +4709 +4710 +4711 +4712 +4713 +4714 +4715 +4716 +4717 +4718 +4719 +4720 +4721 +4722 +4723 +4724 +4725 +4726 +4727 +4728 +4729 +4730 +4731 +4732 +4733 +4734 +4735 +4736 +4737 +4738 +4739 +4740 +4741 +4742 +4743 +4744 +4745 +4746 +4747 +4748 +4749 +4750 +4751 +4752 +4753 +4754 +4755 +4756 +4757 +4758 +4759 +4760 +4761 +4762 +4763 +4764 +4765 +4766 +4767 +4768 +4769 +4770 +4771 +4772 +4773 +4774 +4775 +4776 +4777 +4778 +4779 +4780 +4781 +4782 +4783 +4784 +4785 +4786 +4787 +4788 +4789 +4790 +4791 +4792 +4793 +4794 +4795 +4796 +4797 +4798 +4799 +4800 +4801 +4802 +4803 +4804 +4805 +4806 +4807 +4808 +4809 +4810 +4811 +4812 +4813 +4814 +4815 +4816 +4817 +4818 +4819 +4820 +4821 +4822 +4823 +4824 +4825 +4826 +4827 +4828 +4829 +4830 +4831 +4832 +4833 +4834 +4835 +4836 +4837 +4838 +4839 +4840 +4841 +4842 +4843 +4844 +4845 +4846 +4847 +4848 +4849 +4850 +4851 +4852 +4853 +4854 +4855 +4856 +4857 +4858 +4859 +4860 +4861 +4862 +4863 +4864 +4865 +4866 +4867 +4868 +4869 +4870 +4871 +4872 +4873 +4874 +4875 +4876 +4877 +4878 +4879 +4880 +4881 +4882 +4883 +4884 +4885 +4886 +4887 +4888 +4889 +4890 +4891 +4892 +4893 +4894 +4895 +4896 +4897 +4898 +4899 +4900 +4901 +4902 +4903 +4904 +4905 +4906 +4907 +4908 +4909 +4910 +4911 +4912 +4913 +4914 +4915 +4916 +4917 +4918 +4919 +4920 +4921 +4922 +4923 +4924 +4925 +4926 +4927 +4928 +4929 +4930 +4931 +4932 +4933 +4934 +4935 +4936 +4937 +4938 +4939 +4940 +4941 +4942 +4943 +4944 +4945 +4946 +4947 +4948 +4949 +4950 +4951 +4952 +4953 +4954 +4955 +4956 +4957 +4958 +4959 +4960 +4961 +4962 +4963 +4964 +4965 +4966 +4967 +4968 +4969 +4970 +4971 +4972 +4973 +4974 +4975 +4976 +4977 +4978 +4979 +4980 +4981 +4982 +4983 +4984 +4985 +4986 +4987 +4988 +4989 +4990 +4991 +4992 +4993 +4994 +4995 +4996 +4997 +4998 +4999 +5000 +5001 +5002 +5003 +5004 +5005 +5006 +5007 +5008 +5009 +5010 +5011 +5012 +5013 +5014 +5015 +5016 +5017 +5018 +5019 +5020 +5021 +5022 +5023 +5024 +5025 +5026 +5027 +5028 +5029 +5030 +5031 +5032 +5033 +5034 +5035 +5036 +5037 +5038 +5039 +5040 +5041 +5042 +5043 +5044 +5045 +5046 +5047 +5048 +5049 +5050 +5051 +5052 +5053 +5054 +5055 +5056 +5057 +5058 +5059 +5060 +5061 +5062 +5063 +5064 +5065 +5066 +5067 +5068 +5069 +5070 +5071 +5072 +5073 +5074 +5075 +5076 +5077 +5078 +5079 +5080 +5081 +5082 +5083 +5084 +5085 +5086 +5087 +5088 +5089 +5090 +5091 +5092 +5093 +5094 +5095 +5096 +5097 +5098 +5099 +5100 +5101 +5102 +5103 +5104 +5105 +5106 +5107 +5108 +5109 +5110 +5111 +5112 +5113 +5114 +5115 +5116 +5117 +5118 +5119 +5120 +5121 +5122 +5123 +5124 +5125 +5126 +5127 +5128 +5129 +5130 +5131 +5132 +5133 +5134 +5135 +5136 +5137 +5138 +5139 +5140 +5141 +5142 +5143 +5144 +5145 +5146 +5147 +5148 +5149 +5150 +5151 +5152 +5153 +5154 +5155 +5156 +5157 +5158 +5159 +5160 +5161 +5162 +5163 +5164 +5165 +5166 +5167 +5168 +5169 +5170 +5171 +5172 +5173 +5174 +5175 +5176 +5177 +5178 +5179 +5180 +5181 +5182 +5183 +5184 +5185 +5186 +5187 +5188 +5189 +5190 +5191 +5192 +5193 +5194 +5195 +5196 +5197 +5198 +5199 +5200 +5201 +5202 +5203 +5204 +5205 +5206 +5207 +5208 +5209 +5210 +5211 +5212 +5213 +5214 +5215 +5216 +5217 +5218 +5219 +5220 +5221 +5222 +5223 +5224 +5225 +5226 +5227 +5228 +5229 +5230 +5231 +5232 +5233 +5234 +5235 +5236 +5237 +5238 +5239 +5240 +5241 +5242 +5243 +5244 +5245 +5246 +5247 +5248 +5249 +5250 +5251 +5252 +5253 +5254 +5255 +5256 +5257 +5258 +5259 +5260 +5261 +5262 +5263 +5264 +5265 +5266 +5267 +5268 +5269 +5270 +5271 +5272 +5273 +5274 +5275 +5276 +5277 +5278 +5279 +5280 +5281 +5282 +5283 +5284 +5285 +5286 +5287 +5288 +5289 +5290 +5291 +5292 +5293 +5294 +5295 +5296 +5297 +5298 +5299 +5300 +5301 +5302 +5303 +5304 +5305 +5306 +5307 +5308 +5309 +5310 +5311 +5312 +5313 +5314 +5315 +5316 +5317 +5318 +5319 +5320 +5321 +5322 +5323 +5324 +5325 +5326 +5327 +5328 +5329 +5330 +5331 +5332 +5333 +5334 +5335 +5336 +5337 +5338 +5339 +5340 +5341 +5342 +5343 +5344 +5345 +5346 +5347 +5348 +5349 +5350 +5351 +5352 +5353 +5354 +5355 +5356 +5357 +5358 +5359 +5360 +5361 +5362 +5363 +5364 +5365 +5366 +5367 +5368 +5369 +5370 +5371 +5372 +5373 +5374 +5375 +5376 +5377 +5378 +5379 +5380 +5381 +5382 +5383 +5384 +5385 +5386 +5387 +5388 +5389 +5390 +5391 +5392 +5393 +5394 +5395 +5396 +5397 +5398 +5399 +5400 +5401 +5402 +5403 +5404 +5405 +5406 +5407 +5408 +5409 +5410 +5411 +5412 +5413 +5414 +5415 +5416 +5417 +5418 +5419 +5420 +5421 +5422 +5423 +5424 +5425 +5426 +5427 +5428 +5429 +5430 +5431 +5432 +5433 +5434 +5435 +5436 +5437 +5438 +5439 +5440 +5441 +5442 +5443 +5444 +5445 +5446 +5447 +5448 +5449 +5450 +5451 +5452 +5453 +5454 +5455 +5456 +5457 +5458 +5459 +5460 +5461 +5462 +5463 +5464 +5465 +5466 +5467 +5468 +5469 +5470 +5471 +5472 +5473 +5474 +5475 +5476 +5477 +5478 +5479 +5480 +5481 +5482 +5483 +5484 +5485 +5486 +5487 +5488 +5489 +5490 +5491 +5492 +5493 +5494 +5495 +5496 +5497 +5498 +5499 +5500 +5501 +5502 +5503 +5504 +5505 +5506 +5507 +5508 +5509 +5510 +5511 +5512 +5513 +5514 +5515 +5516 +5517 +5518 +5519 +5520 +5521 +5522 +5523 +5524 +5525 +5526 +5527 +5528 +5529 +5530 +5531 +5532 +5533 +5534 +5535 +5536 +5537 +5538 +5539 +5540 +5541 +5542 +5543 +5544 +5545 +5546 +5547 +5548 +5549 +5550 +5551 +5552 +5553 +5554 +5555 +5556 +5557 +5558 +5559 +5560 +5561 +5562 +5563 +5564 +5565 +5566 +5567 +5568 +5569 +5570 +5571 +5572 +5573 +5574 +5575 +5576 +5577 +5578 +5579 +5580 +5581 +5582 +5583 +5584 +5585 +5586 +5587 +5588 +5589 +5590 +5591 +5592 +5593 +5594 +5595 +5596 +5597 +5598 +5599 +5600 +5601 +5602 +5603 +5604 +5605 +5606 +5607 +5608 +5609 +5610 +5611 +5612 +5613 +5614 +5615 +5616 +5617 +5618 +5619 +5620 +5621 +5622 +5623 +5624 +5625 +5626 +5627 +5628 +5629 +5630 +5631 +5632 +5633 +5634 +5635 +5636 +5637 +5638 +5639 +5640 +5641 +5642 +5643 +5644 +5645 +5646 +5647 +5648 +5649 +5650 +5651 +5652 +5653 +5654 +5655 +5656 +5657 +5658 +5659 +5660 +5661 +5662 +5663 +5664 +5665 +5666 +5667 +5668 +5669 +5670 +5671 +5672 +5673 +5674 +5675 +5676 +5677 +5678 +5679 +5680 +5681 +5682 +5683 +5684 +5685 +5686 +5687 +5688 +5689 +5690 +5691 +5692 +5693 +5694 +5695 +5696 +5697 +5698 +5699 +5700 +5701 +5702 +5703 +5704 +5705 +5706 +5707 +5708 +5709 +5710 +5711 +5712 +5713 +5714 +5715 +5716 +5717 +5718 +5719 +5720 +5721 +5722 +5723 +5724 +5725 +5726 +5727 +5728 +5729 +5730 +5731 +5732 +5733 +5734 +5735 +5736 +5737 +5738 +5739 +5740 +5741 +5742 +5743 +5744 +5745 +5746 +5747 +5748 +5749 +5750 +5751 +5752 +5753 +5754 +5755 +5756 +5757 +5758 +5759 +5760 +5761 +5762 +5763 +5764 +5765 +5766 +5767 +5768 +5769 +5770 +5771 +5772 +5773 +5774 +5775 +5776 +5777 +5778 +5779 +5780 +5781 +5782 +5783 +5784 +5785 +5786 +5787 +5788 +5789 +5790 +5791 +5792 +5793 +5794 +5795 +5796 +5797 +5798 +5799 +5800 +5801 +5802 +5803 +5804 +5805 +5806 +5807 +5808 +5809 +5810 +5811 +5812 +5813 +5814 +5815 +5816 +5817 +5818 +5819 +5820 +5821 +5822 +5823 +5824 +5825 +5826 +5827 +5828 +5829 +5830 +5831 +5832 +5833 +5834 +5835 +5836 +5837 +5838 +5839 +5840 +5841 +5842 +5843 +5844 +5845 +5846 +5847 +5848 +5849 +5850 +5851 +5852 +5853 +5854 +5855 +5856 +5857 +5858 +5859 +5860 +5861 +5862 +5863 +5864 +5865 +5866 +5867 +5868 +5869 +5870 +5871 +5872 +5873 +5874 +5875 +5876 +5877 +5878 +5879 +5880 +5881 +5882 +5883 +5884 +5885 +5886 +5887 +5888 +5889 +5890 +5891 +5892 +5893 +5894 +5895 +5896 +5897 +5898 +5899 +5900 +5901 +5902 +5903 +5904 +5905 +5906 +5907 +5908 +5909 +5910 +5911 +5912 +5913 +5914 +5915 +5916 +5917 +5918 +5919 +5920 +5921 +5922 +5923 +5924 +5925 +5926 +5927 +5928 +5929 +5930 +5931 +5932 +5933 +5934 +5935 +5936 +5937 +5938 +5939 +5940 +5941 +5942 +5943 +5944 +5945 +5946 +5947 +5948 +5949 +5950 +5951 +5952 +5953 +5954 +5955 +5956 +5957 +5958 +5959 +5960 +5961 +5962 +5963 +5964 +5965 +5966 +5967 +5968 +5969 +5970 +5971 +5972 +5973 +5974 +5975 +5976 +5977 +5978 +5979 +5980 +5981 +5982 +5983 +5984 +5985 +5986 +5987 +5988 +5989 +5990 +5991 +5992 +5993 +5994 +5995 +5996 +5997 +5998 +5999 +6000 +6001 +6002 +6003 +6004 +6005 +6006 +6007 +6008 +6009 +6010 +6011 +6012 +6013 +6014 +6015 +6016 +6017 +6018 +6019 +6020 +6021 +6022 +6023 +6024 +6025 +6026 +6027 +6028 +6029 +6030 +6031 +6032 +6033 +6034 +6035 +6036 +6037 +6038 +6039 +6040 +6041 +6042 +6043 +6044 +6045 +6046 +6047 +6048 +6049 +6050 +6051 +6052 +6053 +6054 +6055 +6056 +6057 +6058 +6059 +6060 +6061 +6062 +6063 +6064 +6065 +6066 +6067 +6068 +6069 +6070 +6071 +6072 +6073 +6074 +6075 +6076 +6077 +6078 +6079 +6080 +6081 +6082 +6083 +6084 +6085 +6086 +6087 +6088 +6089 +6090 +6091 +6092 +6093 +6094 +6095 +6096 +6097 +6098 +6099 +6100 +6101 +6102 +6103 +6104 +6105 +6106 +6107 +6108 +6109 +6110 +6111 +6112 +6113 +6114 +6115 +6116 +6117 +6118 +6119 +6120 +6121 +6122 +6123 +6124 +6125 +6126 +6127 +6128 +6129 +6130 +6131 +6132 +6133 +6134 +6135 +6136 +6137 +6138 +6139 +6140 +6141 +6142 +6143 +6144 +6145 +6146 +6147 +6148 +6149 +6150 +6151 +6152 +6153 +6154 +6155 +6156 +6157 +6158 +6159 +6160 +6161 +6162 +6163 +6164 +6165 +6166 +6167 +6168 +6169 +6170 +6171 +6172 +6173 +6174 +6175 +6176 +6177 +6178 +6179 +6180 +6181 +6182 +6183 +6184 +6185 +6186 +6187 +6188 +6189 +6190 +6191 +6192 +6193 +6194 +6195 +6196 +6197 +6198 +6199 +6200 +6201 +6202 +6203 +6204 +6205 +6206 +6207 +6208 +6209 +6210 +6211 +6212 +6213 +6214 +6215 +6216 +6217 +6218 +6219 +6220 +6221 +6222 +6223 +6224 +6225 +6226 +6227 +6228 +6229 +6230 +6231 +6232 +6233 +6234 +6235 +6236 +6237 +6238 +6239 +6240 +6241 +6242 +6243 +6244 +6245 +6246 +6247 +6248 +6249 +6250 +6251 +6252 +6253 +6254 +6255 +6256 +6257 +6258 +6259 +6260 +6261 +6262 +6263 +6264 +6265 +6266 +6267 +6268 +6269 +6270 +6271 +6272 +6273 +6274 +6275 +6276 +6277 +6278 +6279 +6280 +6281 +6282 +6283 +6284 +6285 +6286 +6287 +6288 +6289 +6290 +6291 +6292 +6293 +6294 +6295 +6296 +6297 +6298 +6299 +6300 +6301 +6302 +6303 +6304 +6305 +6306 +6307 +6308 +6309 +6310 +6311 +6312 +6313 +6314 +6315 +6316 +6317 +6318 +6319 +6320 +6321 +6322 +6323 +6324 +6325 +6326 +6327 +6328 +6329 +6330 +6331 +6332 +6333 +6334 +6335 +6336 +6337 +6338 +6339 +6340 +6341 +6342 +6343 +6344 +6345 +6346 +6347 +6348 +6349 +6350 +6351 +6352 +6353 +6354 +6355 +6356 +6357 +6358 +6359 +6360 +6361 +6362 +6363 +6364 +6365 +6366 +6367 +6368 +6369 +6370 +6371 +6372 +6373 +6374 +6375 +6376 +6377 +6378 +6379 +6380 +6381 +6382 +6383 +6384 +6385 +6386 +6387 +6388 +6389 +6390 +6391 +6392 +6393 +6394 +6395 +6396 +6397 +6398 +6399 +6400 +6401 +6402 +6403 +6404 +6405 +6406 +6407 +6408 +6409 +6410 +6411 +6412 +6413 +6414 +6415 +6416 +6417 +6418 +6419 +6420 +6421 +6422 +6423 +6424 +6425 +6426 +6427 +6428 +6429 +6430 +6431 +6432 +6433 +6434 +6435 +6436 +6437 +6438 +6439 +6440 +6441 +6442 +6443 +6444 +6445 +6446 +6447 +6448 +6449 +6450 +6451 +6452 +6453 +6454 +6455 +6456 +6457 +6458 +6459 +6460 +6461 +6462 +6463 +6464 +6465 +6466 +6467 +6468 +6469 +6470 +6471 +6472 +6473 +6474 +6475 +6476 +6477 +6478 +6479 +6480 +6481 +6482 +6483 +6484 +6485 +6486 +6487 +6488 +6489 +6490 +6491 +6492 +6493 +6494 +6495 +6496 +6497 +6498 +6499 +6500 +6501 +6502 +6503 +6504 +6505 +6506 +6507 +6508 +6509 +6510 +6511 +6512 +6513 +6514 +6515 +6516 +6517 +6518 +6519 +6520 +6521 +6522 +6523 +6524 +6525 +6526 +6527 +6528 +6529 +6530 +6531 +6532 +6533 +6534 +6535 +6536 +6537 +6538 +6539 +6540 +6541 +6542 +6543 +6544 +6545 +6546 +6547 +6548 +6549 +6550 +6551 +6552 +6553 +6554 +6555 +6556 +6557 +6558 +6559 +6560 +6561 +6562 +6563 +6564 +6565 +6566 +6567 +6568 +6569 +6570 +6571 +6572 +6573 +6574 +6575 +6576 +6577 +6578 +6579 +6580 +6581 +6582 +6583 +6584 +6585 +6586 +6587 +6588 +6589 +6590 +6591 +6592 +6593 +6594 +6595 +6596 +6597 +6598 +6599 +6600 +6601 +6602 +6603 +6604 +6605 +6606 +6607 +6608 +6609 +6610 +6611 +6612 +6613 +6614 +6615 +6616 +6617 +6618 +6619 +6620 +6621 +6622 +6623 +6624 +6625 +6626 +6627 +6628 +6629 +6630 +6631 +6632 +6633 +6634 +6635 +6636 +6637 +6638 +6639 +6640 +6641 +6642 +6643 +6644 +6645 +6646 +6647 +6648 +6649 +6650 +6651 +6652 +6653 +6654 +6655 +6656 +6657 +6658 +6659 +6660 +6661 +6662 +6663 +6664 +6665 +6666 +6667 +6668 +6669 +6670 +6671 +6672 +6673 +6674 +6675 +6676 +6677 +6678 +6679 +6680 +6681 +6682 +6683 +6684 +6685 +6686 +6687 +6688 +6689 +6690 +6691 +6692 +6693 +6694 +6695 +6696 +6697 +6698 +6699 +6700 +6701 +6702 +6703 +6704 +6705 +6706 +6707 +6708 +6709 +6710 +6711 +6712 +6713 +6714 +6715 +6716 +6717 +6718 +6719 +6720 +6721 +6722 +6723 +6724 +6725 +6726 +6727 +6728 +6729 +6730 +6731 +6732 +6733 +6734 +6735 +6736 +6737 +6738 +6739 +6740 +6741 +6742 +6743 +6744 +6745 +6746 +6747 +6748 +6749 +6750 +6751 +6752 +6753 +6754 +6755 +6756 +6757 +6758 +6759 +6760 +6761 +6762 +6763 +6764 +6765 +6766 +6767 +6768 +6769 +6770 +6771 +6772 +6773 +6774 +6775 +6776 +6777 +6778 +6779 +6780 +6781 +6782 +6783 +6784 +6785 +6786 +6787 +6788 +6789 +6790 +6791 +6792 +6793 +6794 +6795 +6796 +6797 +6798 +6799 +6800 +6801 +6802 +6803 +6804 +6805 +6806 +6807 +6808 +6809 +6810 +6811 +6812 +6813 +6814 +6815 +6816 +6817 +6818 +6819 +6820 +6821 +6822 +6823 +6824 +6825 +6826 +6827 +6828 +6829 +6830 +6831 +6832 +6833 +6834 +6835 +6836 +6837 +6838 +6839 +6840 +6841 +6842 +6843 +6844 +6845 +6846 +6847 +6848 +6849 +6850 +6851 +6852 +6853 +6854 +6855 +6856 +6857 +6858 +6859 +6860 +6861 +6862 +6863 +6864 +6865 +6866 +6867 +6868 +6869 +6870 +6871 +6872 +6873 +6874 +6875 +6876 +6877 +6878 +6879 +6880 +6881 +6882 +6883 +6884 +6885 +6886 +6887 +6888 +6889 +6890 +6891 +6892 +6893 +6894 +6895 +6896 +6897 +6898 +6899 +6900 +6901 +6902 +6903 +6904 +6905 +6906 +6907 +6908 +6909 +6910 +6911 +6912 +6913 +6914 +6915 +6916 +6917 +6918 +6919 +6920 +6921 +6922 +6923 +6924 +6925 +6926 +6927 +6928 +6929 +6930 +6931 +6932 +6933 +6934 +6935 +6936 +6937 +6938 +6939 +6940 +6941 +6942 +6943 +6944 +6945 +6946 +6947 +6948 +6949 +6950 +6951 +6952 +6953 +6954 +6955 +6956 +6957 +6958 +6959 +6960 +6961 +6962 +6963 +6964 +6965 +6966 +6967 +6968 +6969 +6970 +6971 +6972 +6973 +6974 +6975 +6976 +6977 +6978 +6979 +6980 +6981 +6982 +6983 +6984 +6985 +6986 +6987 +6988 +6989 +6990 +6991 +6992 +6993 +6994 +6995 +6996 +6997 +6998 +6999 +7000 +7001 +7002 +7003 +7004 +7005 +7006 +7007 +7008 +7009 +7010 +7011 +7012 +7013 +7014 +7015 +7016 +7017 +7018 +7019 +7020 +7021 +7022 +7023 +7024 +7025 +7026 +7027 +7028 +7029 +7030 +7031 +7032 +7033 +7034 +7035 +7036 +7037 +7038 +7039 +7040 +7041 +7042 +7043 +7044 +7045 +7046 +7047 +7048 +7049 +7050 +7051 +7052 +7053 +7054 +7055 +7056 +7057 +7058 +7059 +7060 +7061 +7062 +7063 +7064 +7065 +7066 +7067 +7068 +7069 +7070 +7071 +7072 +7073 +7074 +7075 +7076 +7077 +7078 +7079 +7080 +7081 +7082 +7083 +7084 +7085 +7086 +7087 +7088 +7089 +7090 +7091 +7092 +7093 +7094 +7095 +7096 +7097 +7098 +7099 +7100 +7101 +7102 +7103 +7104 +7105 +7106 +7107 +7108 +7109 +7110 +7111 +7112 +7113 +7114 +7115 +7116 +7117 +7118 +7119 +7120 +7121 +7122 +7123 +7124 +7125 +7126 +7127 +7128 +7129 +7130 +7131 +7132 +7133 +7134 +7135 +7136 +7137 +7138 +7139 +7140 +7141 +7142 +7143 +7144 +7145 +7146 +7147 +7148 +7149 +7150 +7151 +7152 +7153 +7154 +7155 +7156 +7157 +7158 +7159 +7160 +7161 +7162 +7163 +7164 +7165 +7166 +7167 +7168 +7169 +7170 +7171 +7172 +7173 +7174 +7175 +7176 +7177 +7178 +7179 +7180 +7181 +7182 +7183 +7184 +7185 +7186 +7187 +7188 +7189 +7190 +7191 +7192 +7193 +7194 +7195 +7196 +7197 +7198 +7199 +7200 +7201 +7202 +7203 +7204 +7205 +7206 +7207 +7208 +7209 +7210 +7211 +7212 +7213 +7214 +7215 +7216 +7217 +7218 +7219 +7220 +7221 +7222 +7223 +7224 +7225 +7226 +7227 +7228 +7229 +7230 +7231 +7232 +7233 +7234 +7235 +7236 +7237 +7238 +7239 +7240 +7241 +7242 +7243 +7244 +7245 +7246 +7247 +7248 +7249 +7250 +7251 +7252 +7253 +7254 +7255 +7256 +7257 +7258 +7259 +7260 +7261 +7262 +7263 +7264 +7265 +7266 +7267 +7268 +7269 +7270 +7271 +7272 +7273 +7274 +7275 +7276 +7277 +7278 +7279 +7280 +7281 +7282 +7283 +7284 +7285 +7286 +7287 +7288 +7289 +7290 +7291 +7292 +7293 +7294 +7295 +7296 +7297 +7298 +7299 +7300 +7301 +7302 +7303 +7304 +7305 +7306 +7307 +7308 +7309 +7310 +7311 +7312 +7313 +7314 +7315 +7316 +7317 +7318 +7319 +7320 +7321 +7322 +7323 +7324 +7325 +7326 +7327 +7328 +7329 +7330 +7331 +7332 +7333 +7334 +7335 +7336 +7337 +7338 +7339 +7340 +7341 +7342 +7343 +7344 +7345 +7346 +7347 +7348 +7349 +7350 +7351 +7352 +7353 +7354 +7355 +7356 +7357 +7358 +7359 +7360 +7361 +7362 +7363 +7364 +7365 +7366 +7367 +7368 +7369 +7370 +7371 +7372 +7373 +7374 +7375 +7376 +7377 +7378 +7379 +7380 +7381 +7382 +7383 +7384 +7385 +7386 +7387 +7388 +7389 +7390 +7391 +7392 +7393 +7394 +7395 +7396 +7397 +7398 +7399 +7400 +7401 +7402 +7403 +7404 +7405 +7406 +7407 +7408 +7409 +7410 +7411 +7412 +7413 +7414 +7415 +7416 +7417 +7418 +7419 +7420 +7421 +7422 +7423 +7424 +7425 +7426 +7427 +7428 +7429 +7430 +7431 +7432 +7433 +7434 +7435 +7436 +7437 +7438 +7439 +7440 +7441 +7442 +7443 +7444 +7445 +7446 +7447 +7448 +7449 +7450 +7451 +7452 +7453 +7454 +7455 +7456 +7457 +7458 +7459 +7460 +7461 +7462 +7463 +7464 +7465 +7466 +7467 +7468 +7469 +7470 +7471 +7472 +7473 +7474 +7475 +7476 +7477 +7478 +7479 +7480 +7481 +7482 +7483 +7484 +7485 +7486 +7487 +7488 +7489 +7490 +7491 +7492 +7493 +7494 +7495 +7496 +7497 +7498 +7499 +7500 +7501 +7502 +7503 +7504 +7505 +7506 +7507 +7508 +7509 +7510 +7511 +7512 +7513 +7514 +7515 +7516 +7517 +7518 +7519 +7520 +7521 +7522 +7523 +7524 +7525 +7526 +7527 +7528 +7529 +7530 +7531 +7532 +7533 +7534 +7535 +7536 +7537 +7538 +7539 +7540 +7541 +7542 +7543 +7544 +7545 +7546 +7547 +7548 +7549 +7550 +7551 +7552 +7553 +7554 +7555 +7556 +7557 +7558 +7559 +7560 +7561 +7562 +7563 +7564 +7565 +7566 +7567 +7568 +7569 +7570 +7571 +7572 +7573 +7574 +7575 +7576 +7577 +7578 +7579 +7580 +7581 +7582 +7583 +7584 +7585 +7586 +7587 +7588 +7589 +7590 +7591 +7592 +7593 +7594 +7595 +7596 +7597 +7598 +7599 +7600 +7601 +7602 +7603 +7604 +7605 +7606 +7607 +7608 +7609 +7610 +7611 +7612 +7613 +7614 +7615 +7616 +7617 +7618 +7619 +7620 +7621 +7622 +7623 +7624 +7625 +7626 +7627 +7628 +7629 +7630 +7631 +7632 +7633 +7634 +7635 +7636 +7637 +7638 +7639 +7640 +7641 +7642 +7643 +7644 +7645 +7646 +7647 +7648 +7649 +7650 +7651 +7652 +7653 +7654 +7655 +7656 +7657 +7658 +7659 +7660 +7661 +7662 +7663 +7664 +7665 +7666 +7667 +7668 +7669 +7670 +7671 +7672 +7673 +7674 +7675 +7676 +7677 +7678 +7679 +7680 +7681 +7682 +7683 +7684 +7685 +7686 +7687 +7688 +7689 +7690 +7691 +7692 +7693 +7694 +7695 +7696 +7697 +7698 +7699 +7700 +7701 +7702 +7703 +7704 +7705 +7706 +7707 +7708 +7709 +7710 +7711 +7712 +7713 +7714 +7715 +7716 +7717 +7718 +7719 +7720 +7721 +7722 +7723 +7724 +7725 +7726 +7727 +7728 +7729 +7730 +7731 +7732 +7733 +7734 +7735 +7736 +7737 +7738 +7739 +7740 +7741 +7742 +7743 +7744 +7745 +7746 +7747 +7748 +7749 +7750 +7751 +7752 +7753 +7754 +7755 +7756 +7757 +7758 +7759 +7760 +7761 +7762 +7763 +7764 +7765 +7766 +7767 +7768 +7769 +7770 +7771 +7772 +7773 +7774 +7775 +7776 +7777 +7778 +7779 +7780 +7781 +7782 +7783 +7784 +7785 +7786 +7787 +7788 +7789 +7790 +7791 +7792 +7793 +7794 +7795 +7796 +7797 +7798 +7799 +7800 +7801 +7802 +7803 +7804 +7805 +7806 +7807 +7808 +7809 +7810 +7811 +7812 +7813 +7814 +7815 +7816 +7817 +7818 +7819 +7820 +7821 +7822 +7823 +7824 +7825 +7826 +7827 +7828 +7829 +7830 +7831 +7832 +7833 +7834 +7835 +7836 +7837 +7838 +7839 +7840 +7841 +7842 +7843 +7844 +7845 +7846 +7847 +7848 +7849 +7850 +7851 +7852 +7853 +7854 +7855 +7856 +7857 +7858 +7859 +7860 +7861 +7862 +7863 +7864 +7865 +7866 +7867 +7868 +7869 +7870 +7871 +7872 +7873 +7874 +7875 +7876 +7877 +7878 +7879 +7880 +7881 +7882 +7883 +7884 +7885 +7886 +7887 +7888 +7889 +7890 +7891 +7892 +7893 +7894 +7895 +7896 +7897 +7898 +7899 +7900 +7901 +7902 +7903 +7904 +7905 +7906 +7907 +7908 +7909 +7910 +7911 +7912 +7913 +7914 +7915 +7916 +7917 +7918 +7919 +7920 +7921 +7922 +7923 +7924 +7925 +7926 +7927 +7928 +7929 +7930 +7931 +7932 +7933 +7934 +7935 +7936 +7937 +7938 +7939 +7940 +7941 +7942 +7943 +7944 +7945 +7946 +7947 +7948 +7949 +7950 +7951 +7952 +7953 +7954 +7955 +7956 +7957 +7958 +7959 +7960 +7961 +7962 +7963 +7964 +7965 +7966 +7967 +7968 +7969 +7970 +7971 +7972 +7973 +7974 +7975 +7976 +7977 +7978 +7979 +7980 +7981 +7982 +7983 +7984 +7985 +7986 +7987 +7988 +7989 +7990 +7991 +7992 +7993 +7994 +7995 +7996 +7997 +7998 +7999 +8000 +8001 +8002 +8003 +8004 +8005 +8006 +8007 +8008 +8009 +8010 +8011 +8012 +8013 +8014 +8015 +8016 +8017 +8018 +8019 +8020 +8021 +8022 +8023 +8024 +8025 +8026 +8027 +8028 +8029 +8030 +8031 +8032 +8033 +8034 +8035 +8036 +8037 +8038 +8039 +8040 +8041 +8042 +8043 +8044 +8045 +8046 +8047 +8048 +8049 +8050 +8051 +8052 +8053 +8054 +8055 +8056 +8057 +8058 +8059 +8060 +8061 +8062 +8063 +8064 +8065 +8066 +8067 +8068 +8069 +8070 +8071 +8072 +8073 +8074 +8075 +8076 +8077 +8078 +8079 +8080 +8081 +8082 +8083 +8084 +8085 +8086 +8087 +8088 +8089 +8090 +8091 +8092 +8093 +8094 +8095 +8096 +8097 +8098 +8099 +8100 +8101 +8102 +8103 +8104 +8105 +8106 +8107 +8108 +8109 +8110 +8111 +8112 +8113 +8114 +8115 +8116 +8117 +8118 +8119 +8120 +8121 +8122 +8123 +8124 +8125 +8126 +8127 +8128 +8129 +8130 +8131 +8132 +8133 +8134 +8135 +8136 +8137 +8138 +8139 +8140 +8141 +8142 +8143 +8144 +8145 +8146 +8147 +8148 +8149 +8150 +8151 +8152 +8153 +8154 +8155 +8156 +8157 +8158 +8159 +8160 +8161 +8162 +8163 +8164 +8165 +8166 +8167 +8168 +8169 +8170 +8171 +8172 +8173 +8174 +8175 +8176 +8177 +8178 +8179 +8180 +8181 +8182 +8183 +8184 +8185 +8186 +8187 +8188 +8189 +8190 +8191 +8192 +8193 +8194 +8195 +8196 +8197 +8198 +8199 +8200 +8201 +8202 +8203 +8204 +8205 +8206 +8207 +8208 +8209 +8210 +8211 +8212 +8213 +8214 +8215 +8216 +8217 +8218 +8219 +8220 +8221 +8222 +8223 +8224 +8225 +8226 +8227 +8228 +8229 +8230 +8231 +8232 +8233 +8234 +8235 +8236 +8237 +8238 +8239 +8240 +8241 +8242 +8243 +8244 +8245 +8246 +8247 +8248 +8249 +8250 +8251 +8252 +8253 +8254 +8255 +8256 +8257 +8258 +8259 +8260 +8261 +8262 +8263 +8264 +8265 +8266 +8267 +8268 +8269 +8270 +8271 +8272 +8273 +8274 +8275 +8276 +8277 +8278 +8279 +8280 +8281 +8282 +8283 +8284 +8285 +8286 +8287 +8288 +8289 +8290 +8291 +8292 +8293 +8294 +8295 +8296 +8297 +8298 +8299 +8300 +8301 +8302 +8303 +8304 +8305 +8306 +8307 +8308 +8309 +8310 +8311 +8312 +8313 +8314 +8315 +8316 +8317 +8318 +8319 +8320 +8321 +8322 +8323 +8324 +8325 +8326 +8327 +8328 +8329 +8330 +8331 +8332 +8333 +8334 +8335 +8336 +8337 +8338 +8339 +8340 +8341 +8342 +8343 +8344 +8345 +8346 +8347 +8348 +8349 +8350 +8351 +8352 +8353 +8354 +8355 +8356 +8357 +8358 +8359 +8360 +8361 +8362 +8363 +8364 +8365 +8366 +8367 +8368 +8369 +8370 +8371 +8372 +8373 +8374 +8375 +8376 +8377 +8378 +8379 +8380 +8381 +8382 +8383 +8384 +8385 +8386 +8387 +8388 +8389 +8390 +8391 +8392 +8393 +8394 +8395 +8396 +8397 +8398 +8399 +8400 +8401 +8402 +8403 +8404 +8405 +8406 +8407 +8408 +8409 +8410 +8411 +8412 +8413 +8414 +8415 +8416 +8417 +8418 +8419 +8420 +8421 +8422 +8423 +8424 +8425 +8426 +8427 +8428 +8429 +8430 +8431 +8432 +8433 +8434 +8435 +8436 +8437 +8438 +8439 +8440 +8441 +8442 +8443 +8444 +8445 +8446 +8447 +8448 +8449 +8450 +8451 +8452 +8453 +8454 +8455 +8456 +8457 +8458 +8459 +8460 +8461 +8462 +8463 +8464 +8465 +8466 +8467 +8468 +8469 +8470 +8471 +8472 +8473 +8474 +8475 +8476 +8477 +8478 +8479 +8480 +8481 +8482 +8483 +8484 +8485 +8486 +8487 +8488 +8489 +8490 +8491 +8492 +8493 +8494 +8495 +8496 +8497 +8498 +8499 +8500 +8501 +8502 +8503 +8504 +8505 +8506 +8507 +8508 +8509 +8510 +8511 +8512 +8513 +8514 +8515 +8516 +8517 +8518 +8519 +8520 +8521 +8522 +8523 +8524 +8525 +8526 +8527 +8528 +8529 +8530 +8531 +8532 +8533 +8534 +8535 +8536 +8537 +8538 +8539 +8540 +8541 +8542 +8543 +8544 +8545 +8546 +8547 +8548 +8549 +8550 +8551 +8552 +8553 +8554 +8555 +8556 +8557 +8558 +8559 +8560 +8561 +8562 +8563 +8564 +8565 +8566 +8567 +8568 +8569 +8570 +8571 +8572 +8573 +8574 +8575 +8576 +8577 +8578 +8579 +8580 +8581 +8582 +8583 +8584 +8585 +8586 +8587 +8588 +8589 +8590 +8591 +8592 +8593 +8594 +8595 +8596 +8597 +8598 +8599 +8600 +8601 +8602 +8603 +8604 +8605 +8606 +8607 +8608 +8609 +8610 +8611 +8612 +8613 +8614 +8615 +8616 +8617 +8618 +8619 +8620 +8621 +8622 +8623 +8624 +8625 +8626 +8627 +8628 +8629 +8630 +8631 +8632 +8633 +8634 +8635 +8636 +8637 +8638 +8639 +8640 +8641 +8642 +8643 +8644 +8645 +8646 +8647 +8648 +8649 +8650 +8651 +8652 +8653 +8654 +8655 +8656 +8657 +8658 +8659 +8660 +8661 +8662 +8663 +8664 +8665 +8666 +8667 +8668 +8669 +8670 +8671 +8672 +8673 +8674 +8675 +8676 +8677 +8678 +8679 +8680 +8681 +8682 +8683 +8684 +8685 +8686 +8687 +8688 +8689 +8690 +8691 +8692 +8693 +8694 +8695 +8696 +8697 +8698 +8699 +8700 +8701 +8702 +8703 +8704 +8705 +8706 +8707 +8708 +8709 +8710 +8711 +8712 +8713 +8714 +8715 +8716 +8717 +8718 +8719 +8720 +8721 +8722 +8723 +8724 +8725 +8726 +8727 +8728 +8729 +8730 +8731 +8732 +8733 +8734 +8735 +8736 +8737 +8738 +8739 +8740 +8741 +8742 +8743 +8744 +8745 +8746 +8747 +8748 +8749 +8750 +8751 +8752 +8753 +8754 +8755 +8756 +8757 +8758 +8759 +8760 +8761 +8762 +8763 +8764 +8765 +8766 +8767 +8768 +8769 +8770 +8771 +8772 +8773 +8774 +8775 +8776 +8777 +8778 +8779 +8780 +8781 +8782 +8783 +8784 +8785 +8786 +8787 +8788 +8789 +8790 +8791 +8792 +8793 +8794 +8795 +8796 +8797 +8798 +8799 +8800 +8801 +8802 +8803 +8804 +8805 +8806 +8807 +8808 +8809 +8810 +8811 +8812 +8813 +8814 +8815 +8816 +8817 +8818 +8819 +8820 +8821 +8822 +8823 +8824 +8825 +8826 +8827 +8828 +8829 +8830 +8831 +8832 +8833 +8834 +8835 +8836 +8837 +8838 +8839 +8840 +8841 +8842 +8843 +8844 +8845 +8846 +8847 +8848 +8849 +8850 +8851 +8852 +8853 +8854 +8855 +8856 +8857 +8858 +8859 +8860 +8861 +8862 +8863 +8864 +8865 +8866 +8867 +8868 +8869 +8870 +8871 +8872 +8873 +8874 +8875 +8876 +8877 +8878 +8879 +8880 +8881 +8882 +8883 +8884 +8885 +8886 +8887 +8888 +8889 +8890 +8891 +8892 +8893 +8894 +8895 +8896 +8897 +8898 +8899 +8900 +8901 +8902 +8903 +8904 +8905 +8906 +8907 +8908 +8909 +8910 +8911 +8912 +8913 +8914 +8915 +8916 +8917 +8918 +8919 +8920 +8921 +8922 +8923 +8924 +8925 +8926 +8927 +8928 +8929 +8930 +8931 +8932 +8933 +8934 +8935 +8936 +8937 +8938 +8939 +8940 +8941 +8942 +8943 +8944 +8945 +8946 +8947 +8948 +8949 +8950 +8951 +8952 +8953 +8954 +8955 +8956 +8957 +8958 +8959 +8960 +8961 +8962 +8963 +8964 +8965 +8966 +8967 +8968 +8969 +8970 +8971 +8972 +8973 +8974 +8975 +8976 +8977 +8978 +8979 +8980 +8981 +8982 +8983 +8984 +8985 +8986 +8987 +8988 +8989 +8990 +8991 +8992 +8993 +8994 +8995 +8996 +8997 +8998 +8999 +9000 +9001 +9002 +9003 +9004 +9005 +9006 +9007 +9008 +9009 +9010 +9011 +9012 +9013 +9014 +9015 +9016 +9017 +9018 +9019 +9020 +9021 +9022 +9023 +9024 +9025 +9026 +9027 +9028 +9029 +9030 +9031 +9032 +9033 +9034 +9035 +9036 +9037 +9038 +9039 +9040 +9041 +9042 +9043 +9044 +9045 +9046 +9047 +9048 +9049 +9050 +9051 +9052 +9053 +9054 +9055 +9056 +9057 +9058 +9059 +9060 +9061 +9062 +9063 +9064 +9065 +9066 +9067 +9068 +9069 +9070 +9071 +9072 +9073 +9074 +9075 +9076 +9077 +9078 +9079 +9080 +9081 +9082 +9083 +9084 +9085 +9086 +9087 +9088 +9089 +9090 +9091 +9092 +9093 +9094 +9095 +9096 +9097 +9098 +9099 +9100 +9101 +9102 +9103 +9104 +9105 +9106 +9107 +9108 +9109 +9110 +9111 +9112 +9113 +9114 +9115 +9116 +9117 +9118 +9119 +9120 +9121 +9122 +9123 +9124 +9125 +9126 +9127 +9128 +9129 +9130 +9131 +9132 +9133 +9134 +9135 +9136 +9137 +9138 +9139 +9140 +9141 +9142 +9143 +9144 +9145 +9146 +9147 +9148 +9149 +9150 +9151 +9152 +9153 +9154 +9155 +9156 +9157 +9158 +9159 +9160 +9161 +9162 +9163 +9164 +9165 +9166 +9167 +9168 +9169 +9170 +9171 +9172 +9173 +9174 +9175 +9176 +9177 +9178 +9179 +9180 +9181 +9182 +9183 +9184 +9185 +9186 +9187 +9188 +9189 +9190 +9191 +9192 +9193 +9194 +9195 +9196 +9197 +9198 +9199 +9200 +9201 +9202 +9203 +9204 +9205 +9206 +9207 +9208 +9209 +9210 +9211 +9212 +9213 +9214 +9215 +9216 +9217 +9218 +9219 +9220 +9221 +9222 +9223 +9224 +9225 +9226 +9227 +9228 +9229 +9230 +9231 +9232 +9233 +9234 +9235 +9236 +9237 +9238 +9239 +9240 +9241 +9242 +9243 +9244 +9245 +9246 +9247 +9248 +9249 +9250 +9251 +9252 +9253 +9254 +9255 +9256 +9257 +9258 +9259 +9260 +9261 +9262 +9263 +9264 +9265 +9266 +9267 +9268 +9269 +9270 +9271 +9272 +9273 +9274 +9275 +9276 +9277 +9278 +9279 +9280 +9281 +9282 +9283 +9284 +9285 +9286 +9287 +9288 +9289 +9290 +9291 +9292 +9293 +9294 +9295 +9296 +9297 +9298 +9299 +9300 +9301 +9302 +9303 +9304 +9305 +9306 +9307 +9308 +9309 +9310 +9311 +9312 +9313 +9314 +9315 +9316 +9317 +9318 +9319 +9320 +9321 +9322 +9323 +9324 +9325 +9326 +9327 +9328 +9329 +9330 +9331 +9332 +9333 +9334 +9335 +9336 +9337 +9338 +9339 +9340 +9341 +9342 +9343 +9344 +9345 +9346 +9347 +9348 +9349 +9350 +9351 +9352 +9353 +9354 +9355 +9356 +9357 +9358 +9359 +9360 +9361 +9362 +9363 +9364 +9365 +9366 +9367 +9368 +9369 +9370 +9371 +9372 +9373 +9374 +9375 +9376 +9377 +9378 +9379 +9380 +9381 +9382 +9383 +9384 +9385 +9386 +9387 +9388 +9389 +9390 +9391 +9392 +9393 +9394 +9395 +9396 +9397 +9398 +9399 +9400 +9401 +9402 +9403 +9404 +9405 +9406 +9407 +9408 +9409 +9410 +9411 +9412 +9413 +9414 +9415 +9416 +9417 +9418 +9419 +9420 +9421 +9422 +9423 +9424 +9425 +9426 +9427 +9428 +9429 +9430 +9431 +9432 +9433 +9434 +9435 +9436 +9437 +9438 +9439 +9440 +9441 +9442 +9443 +9444 +9445 +9446 +9447 +9448 +9449 +9450 +9451 +9452 +9453 +9454 +9455 +9456 +9457 +9458 +9459 +9460 +9461 +9462 +9463 +9464 +9465 +9466 +9467 +9468 +9469 +9470 +9471 +9472 +9473 +9474 +9475 +9476 +9477 +9478 +9479 +9480 +9481 +9482 +9483 +9484 +9485 +9486 +9487 +9488 +9489 +9490 +9491 +9492 +9493 +9494 +9495 +9496 +9497 +9498 +9499 +9500 +9501 +9502 +9503 +9504 +9505 +9506 +9507 +9508 +9509 +9510 +9511 +9512 +9513 +9514 +9515 +9516 +9517 +9518 +9519 +9520 +9521 +9522 +9523 +9524 +9525 +9526 +9527 +9528 +9529 +9530 +9531 +9532 +9533 +9534 +9535 +9536 +9537 +9538 +9539 +9540 +9541 +9542 +9543 +9544 +9545 +9546 +9547 +9548 +9549 +9550 +9551 +9552 +9553 +9554 +9555 +9556 +9557 +9558 +9559 +9560 +9561 +9562 +9563 +9564 +9565 +9566 +9567 +9568 +9569 +9570 +9571 +9572 +9573 +9574 +9575 +9576 +9577 +9578 +9579 +9580 +9581 +9582 +9583 +9584 +9585 +9586 +9587 +9588 +9589 +9590 +9591 +9592 +9593 +9594 +9595 +9596 +9597 +9598 +9599 +9600 +9601 +9602 +9603 +9604 +9605 +9606 +9607 +9608 +9609 +9610 +9611 +9612 +9613 +9614 +9615 +9616 +9617 +9618 +9619 +9620 +9621 +9622 +9623 +9624 +9625 +9626 +9627 +9628 +9629 +9630 +9631 +9632 +9633 +9634 +9635 +9636 +9637 +9638 +9639 +9640 +9641 +9642 +9643 +9644 +9645 +9646 +9647 +9648 +9649 +9650 +9651 +9652 +9653 +9654 +9655 +9656 +9657 +9658 +9659 +9660 +9661 +9662 +9663 +9664 +9665 +9666 +9667 +9668 +9669 +9670 +9671 +9672 +9673 +9674 +9675 +9676 +9677 +9678 +9679 +9680 +9681 +9682 +9683 +9684 +9685 +9686 +9687 +9688 +9689 +9690 +9691 +9692 +9693 +9694 +9695 +9696 +9697 +9698 +9699 +9700 +9701 +9702 +9703 +9704 +9705 +9706 +9707 +9708 +9709 +9710 +9711 +9712 +9713 +9714 +9715 +9716 +9717 +9718 +9719 +9720 +9721 +9722 +9723 +9724 +9725 +9726 +9727 +9728 +9729 +9730 +9731 +9732 +9733 +9734 +9735 +9736 +9737 +9738 +9739 +9740 +9741 +9742 +9743 +9744 +9745 +9746 +9747 +9748 +9749 +9750 +9751 +9752 +9753 +9754 +9755 +9756 +9757 +9758 +9759 +9760 +9761 +9762 +9763 +9764 +9765 +9766 +9767 +9768 +9769 +9770 +9771 +9772 +9773 +9774 +9775 +9776 +9777 +9778 +9779 +9780 +9781 +9782 +9783 +9784 +9785 +9786 +9787 +9788 +9789 +9790 +9791 +9792 +9793 +9794 +9795 +9796 +9797 +9798 +9799 +9800 +9801 +9802 +9803 +9804 +9805 +9806 +9807 +9808 +9809 +9810 +9811 +9812 +9813 +9814 +9815 +9816 +9817 +9818 +9819 +9820 +9821 +9822 +9823 +9824 +9825 +9826 +9827 +9828 +9829 +9830 +9831 +9832 +9833 +9834 +9835 +9836 +9837 +9838 +9839 +9840 +9841 +9842 +9843 +9844 +9845 +9846 +9847 +9848 +9849 +9850 +9851 +9852 +9853 +9854 +9855 +9856 +9857 +9858 +9859 +9860 +9861 +9862 +9863 +9864 +9865 +9866 +9867 +9868 +9869 +9870 +9871 +9872 +9873 +9874 +9875 +9876 +9877 +9878 +9879 +9880 +9881 +9882 +9883 +9884 +9885 +9886 +9887 +9888 +9889 +9890 +9891 +9892 +9893 +9894 +9895 +9896 +9897 +9898 +9899 +9900 +9901 +9902 +9903 +9904 +9905 +9906 +9907 +9908 +9909 +9910 +9911 +9912 +9913 +9914 +9915 +9916 +9917 +9918 +9919 +9920 +9921 +9922 +9923 +9924 +9925 +9926 +9927 +9928 +9929 +9930 +9931 +9932 +9933 +9934 +9935 +9936 +9937 +9938 +9939 +9940 +9941 +9942 +9943 +9944 +9945 +9946 +9947 +9948 +9949 +9950 +9951 +9952 +9953 +9954 +9955 +9956 +9957 +9958 +9959 +9960 +9961 +9962 +9963 +9964 +9965 +9966 +9967 +9968 +9969 +9970 +9971 +9972 +9973 +9974 +9975 +9976 +9977 +9978 +9979 +9980 +9981 +9982 +9983 +9984 +9985 +9986 +9987 +9988 +9989 +9990 +9991 +9992 +9993 +9994 +9995 +9996 +9997 +9998 +9999 +10000 +10001 +10002 +10003 +10004 +10005 +10006 +10007 +10008 +10009 +10010 +10011 +10012 +10013 +10014 +10015 +10016 +10017 +10018 +10019 +10020 +10021 +10022 +10023 +10024 +10025 +10026 +10027 +10028 +10029 +10030 +10031 +10032 +10033 +10034 +10035 +10036 +10037 +10038 +10039 +10040 +10041 +10042 +10043 +10044 +10045 +10046 +10047 +10048 +10049 +10050 +10051 +10052 +10053 +10054 +10055 +10056 +10057 +10058 +10059 +10060 +10061 +10062 +10063 +10064 +10065 +10066 +10067 +10068 +10069 +10070 +10071 +10072 +10073 +10074 +10075 +10076 +10077 +10078 +10079 +10080 +10081 +10082 +10083 +10084 +10085 +10086 +10087 +10088 +10089 +10090 +10091 +10092 +10093 +10094 +10095 +10096 +10097 +10098 +10099 +10100 +10101 +10102 +10103 +10104 +10105 +10106 +10107 +10108 +10109 +10110 +10111 +10112 +10113 +10114 +10115 +10116 +10117 +10118 +10119 +10120 +10121 +10122 +10123 +10124 +10125 +10126 +10127 +10128 +10129 +10130 +10131 +10132 +10133 +10134 +10135 +10136 +10137 +10138 +10139 +10140 +10141 +10142 +10143 +10144 +10145 +10146 +10147 +10148 +10149 +10150 +10151 +10152 +10153 +10154 +10155 +10156 +10157 +10158 +10159 +10160 +10161 +10162 +10163 +10164 +10165 +10166 +10167 +10168 +10169 +10170 +10171 +10172 +10173 +10174 +10175 +10176 +10177 +10178 +10179 +10180 +10181 +10182 +10183 +10184 +10185 +10186 +10187 +10188 +10189 +10190 +10191 +10192 +10193 +10194 +10195 +10196 +10197 +10198 +10199 +10200 +10201 +10202 +10203 +10204 +10205 +10206 +10207 +10208 +10209 +10210 +10211 +10212 +10213 +10214 +10215 +10216 +10217 +10218 +10219 +10220 +10221 +10222 +10223 +10224 +10225 +10226 +10227 +10228 +10229 +10230 +10231 +10232 +10233 +10234 +10235 +10236 +10237 +10238 +10239 +10240 +10241 +10242 +10243 +10244 +10245 +10246 +10247 +10248 +10249 +10250 +10251 +10252 +10253 +10254 +10255 +10256 +10257 +10258 +10259 +10260 +10261 +10262 +10263 +10264 +10265 +10266 +10267 +10268 +10269 +10270 +10271 +10272 +10273 +10274 +10275 +10276 +10277 +10278 +10279 +10280 +10281 +10282 +10283 +10284 +10285 +10286 +10287 +10288 +10289 +10290 +10291 +10292 +10293 +10294 +10295 +10296 +10297 +10298 +10299 +10300 +10301 +10302 +10303 +10304 +10305 +10306 +10307 +10308 +10309 +10310 +10311 +10312 +10313 +10314 +10315 +10316 +10317 +10318 +10319 +10320 +10321 +10322 +10323 +10324 +10325 +10326 +10327 +10328 +10329 +10330 +10331 +10332 +10333 +10334 +10335 +10336 +10337 +10338 +10339 +10340 +10341 +10342 +10343 +10344 +10345 +10346 +10347 +10348 +10349 +10350 +10351 +10352 +10353 +10354 +10355 +10356 +10357 +10358 +10359 +10360 +10361 +10362 +10363 +10364 +10365 +10366 +10367 +10368 +10369 +10370 +10371 +10372 +10373 +10374 +10375 +10376 +10377 +10378 +10379 +10380 +10381 +10382 +10383 +10384 +10385 +10386 +10387 +10388 +10389 +10390 +10391 +10392 +10393 +10394 +10395 +10396 +10397 +10398 +10399 +10400 +10401 +10402 +10403 +10404 +10405 +10406 +10407 +10408 +10409 +10410 +10411 +10412 +10413 +10414 +10415 +10416 +10417 +10418 +10419 +10420 +10421 +10422 +10423 +10424 +10425 +10426 +10427 +10428 +10429 +10430 +10431 +10432 +10433 +10434 +10435 +10436 +10437 +10438 +10439 +10440 +10441 +10442 +10443 +10444 +10445 +10446 +10447 +10448 +10449 +10450 +10451 +10452 +10453 +10454 +10455 +10456 +10457 +10458 +10459 +10460 +10461 +10462 +10463 +10464 +10465 +10466 +10467 +10468 +10469 +10470 +10471 +10472 +10473 +10474 +10475 +10476 +10477 +10478 +10479 +10480 +10481 +10482 +10483 +10484 +10485 +10486 +10487 +10488 +10489 +10490 +10491 +10492 +10493 +10494 +10495 +10496 +10497 +10498 +10499 +10500 +10501 +10502 +10503 +10504 +10505 +10506 +10507 +10508 +10509 +10510 +10511 +10512 +10513 +10514 +10515 +10516 +10517 +10518 +10519 +10520 +10521 +10522 +10523 +10524 +10525 +10526 +10527 +10528 +10529 +10530 +10531 +10532 +10533 +10534 +10535 +10536 +10537 +10538 +10539 +10540 +10541 +10542 +10543 +10544 +10545 +10546 +10547 +10548 +10549 +10550 +10551 +10552 +10553 +10554 +10555 +10556 +10557 +10558 +10559 +10560 +10561 +10562 +10563 +10564 +10565 +10566 +10567 +10568 +10569 +10570 +10571 +10572 +10573 +10574 +10575 +10576 +10577 +10578 +10579 +10580 +10581 +10582 +10583 +10584 +10585 +10586 +10587 +10588 +10589 +10590 +10591 +10592 +10593 +10594 +10595 +10596 +10597 +10598 +10599 +10600 +10601 +10602 +10603 +10604 +10605 +10606 +10607 +10608 +10609 +10610 +10611 +10612 +10613 +10614 +10615 +10616 +10617 +10618 +10619 +10620 +10621 +10622 +10623 +10624 +10625 +10626 +10627 +10628 +10629 +10630 +10631 +10632 +10633 +10634 +10635 +10636 +10637 +10638 +10639 +10640 +10641 +10642 +10643 +10644 +10645 +10646 +10647 +10648 +10649 +10650 +10651 +10652 +10653 +10654 +10655 +10656 +10657 +10658 +10659 +10660 +10661 +10662 +10663 +10664 +10665 +10666 +10667 +10668 +10669 +10670 +10671 +10672 +10673 +10674 +10675 +10676 +10677 +10678 +10679 +10680 +10681 +10682 +10683 +10684 +10685 +10686 +10687 +10688 +10689 +10690 +10691 +10692 +10693 +10694 +10695 +10696 +10697 +10698 +10699 +10700 +10701 +10702 +10703 +10704 +10705 +10706 +10707 +10708 +10709 +10710 +10711 +10712 +10713 +10714 +10715 +10716 +10717 +10718 +10719 +10720 +10721 +10722 +10723 +10724 +10725 +10726 +10727 +10728 +10729 +10730 +10731 +10732 +10733 +10734 +10735 +10736 +10737 +10738 +10739 +10740 +10741 +10742 +10743 +10744 +10745 +10746 +10747 +10748 +10749 +10750 +10751 +10752 +10753 +10754 +10755 +10756 +10757 +10758 +10759 +10760 +10761 +10762 +10763 +10764 +10765 +10766 +10767 +10768 +10769 +10770 +10771 +10772 +10773 +10774 +10775 +10776 +10777 +10778 +10779 +10780 +10781 +10782 +10783 +10784 +10785 +10786 +10787 +10788 +10789 +10790 +10791 +10792 +10793 +10794 +10795 +10796 +10797 +10798 +10799 +10800 +10801 +10802 +10803 +10804 +10805 +10806 +10807 +10808 +10809 +10810 +10811 +10812 +10813 +10814 +10815 +10816 +10817 +10818 +10819 +10820 +10821 +10822 +10823 +10824 +10825 +10826 +10827 +10828 +10829 +10830 +10831 +10832 +10833 +10834 +10835 +10836 +10837 +10838 +10839 +10840 +10841 +10842 +10843 +10844 +10845 +10846 +10847 +10848 +10849 +10850 +10851 +10852 +10853 +10854 +10855 +10856 +10857 +10858 +10859 +10860 +10861 +10862 +10863 +10864 +10865 +10866 +10867 +10868 +10869 +10870 +10871 +10872 +10873 +10874 +10875 +10876 +10877 +10878 +10879 +10880 +10881 +10882 +10883 +10884 +10885 +10886 +10887 +10888 +10889 +10890 +10891 +10892 +10893 +10894 +10895 +10896 +10897 +10898 +10899 +10900 +10901 +10902 +10903 +10904 +10905 +10906 +10907 +10908 +10909 +10910 +10911 +10912 +10913 +10914 +10915 +10916 +10917 +10918 +10919 +10920 +10921 +10922 +10923 +10924 +10925 +10926 +10927 +10928 +10929 +10930 +10931 +10932 +10933 +10934 +10935 +10936 +10937 +10938 +10939 +10940 +10941 +10942 +10943 +10944 +10945 +10946 +10947 +10948 +10949 +10950 +10951 +10952 +10953 +10954 +10955 +10956 +10957 +10958 +10959 +10960 +10961 +10962 +10963 +10964 +10965 +10966 +10967 +10968 +10969 +10970 +10971 +10972 +10973 +10974 +10975 +10976 +10977 +10978 +10979 +10980 +10981 +10982 +10983 +10984 +10985 +10986 +10987 +10988 +10989 +10990 +10991 +10992 +10993 +10994 +10995 +10996 +10997 +10998 +10999 +11000 +11001 +11002 +11003 +11004 +11005 +11006 +11007 +11008 +11009 +11010 +11011 +11012 +11013 +11014 +11015 +11016 +11017 +11018 +11019 +11020 +11021 +11022 +11023 +11024 +11025 +11026 +11027 +11028 +11029 +11030 +11031 +11032 +11033 +11034 +11035 +11036 +11037 +11038 +11039 +11040 +11041 +11042 +11043 +11044 +11045 +11046 +11047 +11048 +11049 +11050 +11051 +11052 +11053 +11054 +11055 +11056 +11057 +11058 +11059 +11060 +11061 +11062 +11063 +11064 +11065 +11066 +11067 +11068 +11069 +11070 +11071 +11072 +11073 +11074 +11075 +11076 +11077 +11078 +11079 +11080 +11081 +11082 +11083 +11084 +11085 +11086 +11087 +11088 +11089 +11090 +11091 +11092 +11093 +11094 +11095 +11096 +11097 +11098 +11099 +11100 +11101 +11102 +11103 +11104 +11105 +11106 +11107 +11108 +11109 +11110 +11111 +11112 +11113 +11114 +11115 +11116 +11117 +11118 +11119 +11120 +11121 +11122 +11123 +11124 +11125 +11126 +11127 +11128 +11129 +11130 +11131 +11132 +11133 +11134 +11135 +11136 +11137 +11138 +11139 +11140 +11141 +11142 +11143 +11144 +11145 +11146 +11147 +11148 +11149 +11150 +11151 +11152 +11153 +11154 +11155 +11156 +11157 +11158 +11159 +11160 +11161 +11162 +11163 +11164 +11165 +11166 +11167 +11168 +11169 +11170 +11171 +11172 +11173 +11174 +11175 +11176 +11177 +11178 +11179 +11180 +11181 +11182 +11183 +11184 +11185 +11186 +11187 +11188 +11189 +11190 +11191 +11192 +11193 +11194 +11195 +11196 +11197 +11198 +11199 +11200 +11201 +11202 +11203 +11204 +11205 +11206 +11207 +11208 +11209 +11210 +11211 +11212 +11213 +11214 +11215 +11216 +11217 +11218 +11219 +11220 +11221 +11222 +11223 +11224 +11225 +11226 +11227 +11228 +11229 +11230 +11231 +11232 +11233 +11234 +11235 +11236 +11237 +11238 +11239 +11240 +11241 +11242 +11243 +11244 +11245 +11246 +11247 +11248 +11249 +11250 +11251 +11252 +11253 +11254 +11255 +11256 +11257 +11258 +11259 +11260 +11261 +11262 +11263 +11264 +11265 +11266 +11267 +11268 +11269 +11270 +11271 +11272 +11273 +11274 +11275 +11276 +11277 +11278 +11279 +11280 +11281 +11282 +11283 +11284 +11285 +11286 +11287 +11288 +11289 +11290 +11291 +11292 +11293 +11294 +11295 +11296 +11297 +11298 +11299 +11300 +11301 +11302 +11303 +11304 +11305 +11306 +11307 +11308 +11309 +11310 +11311 +11312 +11313 +11314 +11315 +11316 +11317 +11318 +11319 +11320 +11321 +11322 +11323 +11324 +11325 +11326 +11327 +11328 +11329 +11330 +11331 +11332 +11333 +11334 +11335 +11336 +11337 +11338 +11339 +11340 +11341 +11342 +11343 +11344 +11345 +11346 +11347 +11348 +11349 +11350 +11351 +11352 +11353 +11354 +11355 +11356 +11357 +11358 +11359 +11360 +11361 +11362 +11363 +11364 +11365 +11366 +11367 +11368 +11369 +11370 +11371 +11372 +11373 +11374 +11375 +11376 +11377 +11378 +11379 +11380 +11381 +11382 +11383 +11384 +11385 +11386 +11387 +11388 +11389 +11390 +11391 +11392 +11393 +11394 +11395 +11396 +11397 +11398 +11399 +11400 +11401 +11402 +11403 +11404 +11405 +11406 +11407 +11408 +11409 +11410 +11411 +11412 +11413 +11414 +11415 +11416 +11417 +11418 +11419 +11420 +11421 +11422 +11423 +11424 +11425 +11426 +11427 +11428 +11429 +11430 +11431 +11432 +11433 +11434 +11435 +11436 +11437 +11438 +11439 +11440 +11441 +11442 +11443 +11444 +11445 +11446 +11447 +11448 +11449 +11450 +11451 +11452 +11453 +11454 +11455 +11456 +11457 +11458 +11459 +11460 +11461 +11462 +11463 +11464 +11465 +11466 +11467 +11468 +11469 +11470 +11471 +11472 +11473 +11474 +11475 +11476 +11477 +11478 +11479 +11480 +11481 +11482 +11483 +11484 +11485 +11486 +11487 +11488 +11489 +11490 +11491 +11492 +11493 +11494 +11495 +11496 +11497 +11498 +11499 +11500 +11501 +11502 +11503 +11504 +11505 +11506 +11507 +11508 +11509 +11510 +11511 +11512 +11513 +11514 +11515 +11516 +11517 +11518 +11519 +11520 +11521 +11522 +11523 +11524 +11525 +11526 +11527 +11528 +11529 +11530 +11531 +11532 +11533 +11534 +11535 +11536 +11537 +11538 +11539 +11540 +11541 +11542 +11543 +11544 +11545 +11546 +11547 +11548 +11549 +11550 +11551 +11552 +11553 +11554 +11555 +11556 +11557 +11558 +11559 +11560 +11561 +11562 +11563 +11564 +11565 +11566 +11567 +11568 +11569 +11570 +11571 +11572 +11573 +11574 +11575 +11576 +11577 +11578 +11579 +11580 +11581 +11582 +11583 +11584 +11585 +11586 +11587 +11588 +11589 +11590 +11591 +11592 +11593 +11594 +11595 +11596 +11597 +11598 +11599 +11600 +11601 +11602 +11603 +11604 +11605 +11606 +11607 +11608 +11609 +11610 +11611 +11612 +11613 +11614 +11615 +11616 +11617 +11618 +11619 +11620 +11621 +11622 +11623 +11624 +11625 +11626 +11627 +11628 +11629 +11630 +11631 +11632 +11633 +11634 +11635 +11636 +11637 +11638 +11639 +11640 +11641 +11642 +11643 +11644 +11645 +11646 +11647 +11648 +11649 +11650 +11651 +11652 +11653 +11654 +11655 +11656 +11657 +11658 +11659 +11660 +11661 +11662 +11663 +11664 +11665 +11666 +11667 +11668 +11669 +11670 +11671 +11672 +11673 +11674 +11675 +11676 +11677 +11678 +11679 +11680 +11681 +11682 +11683 +11684 +11685 +11686 +11687 +11688 +11689 +11690 +11691 +11692 +11693 +11694 +11695 +11696 +11697 +11698 +11699 +11700 +11701 +11702 +11703 +11704 +11705 +11706 +11707 +11708 +11709 +11710 +11711 +11712 +11713 +11714 +11715 +11716 +11717 +11718 +11719 +11720 +11721 +11722 +11723 +11724 +11725 +11726 +11727 +11728 +11729 +11730 +11731 +11732 +11733 +11734 +11735 +11736 +11737 +11738 +11739 +11740 +11741 +11742 +11743 +11744 +11745 +11746 +11747 +11748 +11749 +11750 +11751 +11752 +11753 +11754 +11755 +11756 +11757 +11758 +11759 +11760 +11761 +11762 +11763 +11764 +11765 +11766 +11767 +11768 +11769 +11770 +11771 +11772 +11773 +11774 +11775 +11776 +11777 +11778 +11779 +11780 +11781 +11782 +11783 +11784 +11785 +11786 +11787 +11788 +11789 +11790 +11791 +11792 +11793 +11794 +11795 +11796 +11797 +11798 +11799 +11800 +11801 +11802 +11803 +11804 +11805 +11806 +11807 +11808 +11809 +11810 +11811 +11812 +11813 +11814 +11815 +11816 +11817 +11818 +11819 +11820 +11821 +11822 +11823 +11824 +11825 +11826 +11827 +11828 +11829 +11830 +11831 +11832 +11833 +11834 +11835 +11836 +11837 +11838 +11839 +11840 +11841 +11842 +11843 +11844 +11845 +11846 +11847 +11848 +11849 +11850 +11851 +11852 +11853 +11854 +11855 +11856 +11857 +11858 +11859 +11860 +11861 +11862 +11863 +11864 +11865 +11866 +11867 +11868 +11869 +11870 +11871 +11872 +11873 +11874 +11875 +11876 +11877 +11878 +11879 +11880 +11881 +11882 +11883 +11884 +11885 +11886 +11887 +11888 +11889 +11890 +11891 +11892 +11893 +11894 +11895 +11896 +11897 +11898 +11899 +11900 +11901 +11902 +11903 +11904 +11905 +11906 +11907 +11908 +11909 +11910 +11911 +11912 +11913 +11914 +11915 +11916 +11917 +11918 +11919 +11920 +11921 +11922 +11923 +11924 +11925 +11926 +11927 +11928 +11929 +11930 +11931 +11932 +11933 +11934 +11935 +11936 +11937 +11938 +11939 +11940 +11941 +11942 +11943 +11944 +11945 +11946 +11947 +11948 +11949 +11950 +11951 +11952 +11953 +11954 +11955 +11956 +11957 +11958 +11959 +11960 +11961 +11962 +11963 +11964 +11965 +11966 +11967 +11968 +11969 +11970 +11971 +11972 +11973 +11974 +11975 +11976 +11977 +11978 +11979 +11980 +11981 +11982 +11983 +11984 +11985 +11986 +11987 +11988 +11989 +11990 +11991 +11992 +11993 +11994 +11995 +11996 +11997 +11998 +11999 +12000 +12001 +12002 +12003 +12004 +12005 +12006 +12007 +12008 +12009 +12010 +12011 +12012 +12013 +12014 +12015 +12016 +12017 +12018 +12019 +12020 +12021 +12022 +12023 +12024 +12025 +12026 +12027 +12028 +12029 +12030 +12031 +12032 +12033 +12034 +12035 +12036 +12037 +12038 +12039 +12040 +12041 +12042 +12043 +12044 +12045 +12046 +12047 +12048 +12049 +12050 +12051 +12052 +12053 +12054 +12055 +12056 +12057 +12058 +12059 +12060 +12061 +12062 +12063 +12064 +12065 +12066 +12067 +12068 +12069 +12070 +12071 +12072 +12073 +12074 +12075 +12076 +12077 +12078 +12079 +12080 +12081 +12082 +12083 +12084 +12085 +12086 +12087 +12088 +12089 +12090 +12091 +12092 +12093 +12094 +12095 +12096 +12097 +12098 +12099 +12100 +12101 +12102 +12103 +12104 +12105 +12106 +12107 +12108 +12109 +12110 +12111 +12112 +12113 +12114 +12115 +12116 +12117 +12118 +12119 +12120 +12121 +12122 +12123 +12124 +12125 +12126 +12127 +12128 +12129 +12130 +12131 +12132 +12133 +12134 +12135 +12136 +12137 +12138 +12139 +12140 +12141 +12142 +12143 +12144 +12145 +12146 +12147 +12148 +12149 +12150 +12151 +12152 +12153 +12154 +12155 +12156 +12157 +12158 +12159 +12160 +12161 +12162 +12163 +12164 +12165 +12166 +12167 +12168 +12169 +12170 +12171 +12172 +12173 +12174 +12175 +12176 +12177 +12178 +12179 +12180 +12181 +12182 +12183 +12184 +12185 +12186 +12187 +12188 +12189 +12190 +12191 +12192 +12193 +12194 +12195 +12196 +12197 +12198 +12199 +12200 +12201 +12202 +12203 +12204 +12205 +12206 +12207 +12208 +12209 +12210 +12211 +12212 +12213 +12214 +12215 +12216 +12217 +12218 +12219 +12220 +12221 +12222 +12223 +12224 +12225 +12226 +12227 +12228 +12229 +12230 +12231 +12232 +12233 +12234 +12235 +12236 +12237 +12238 +12239 +12240 +12241 +12242 +12243 +12244 +12245 +12246 +12247 +12248 +12249 +12250 +12251 +12252 +12253 +12254 +12255 +12256 +12257 +12258 +12259 +12260 +12261 +12262 +12263 +12264 +12265 +12266 +12267 +12268 +12269 +12270 +12271 +12272 +12273 +12274 +12275 +12276 +12277 +12278 +12279 +12280 +12281 +12282 +12283 +12284 +12285 +12286 +12287 +12288 +12289 +12290 +12291 +12292 +12293 +12294 +12295 +12296 +12297 +12298 +12299 +12300 +12301 +12302 +12303 +12304 +12305 +12306 +12307 +12308 +12309 +12310 +12311 +12312 +12313 +12314 +12315 +12316 +12317 +12318 +12319 +12320 +12321 +12322 +12323 +12324 +12325 +12326 +12327 +12328 +12329 +12330 +12331 +12332 +12333 +12334 +12335 +12336 +12337 +12338 +12339 +12340 +12341 +12342 +12343 +12344 +12345 +12346 +12347 +12348 +12349 +12350 +12351 +12352 +12353 +12354 +12355 +12356 +12357 +12358 +12359 +12360 +12361 +12362 +12363 +12364 +12365 +12366 +12367 +12368 +12369 +12370 +12371 +12372 +12373 +12374 +12375 +12376 +12377 +12378 +12379 +12380 +12381 +12382 +12383 +12384 +12385 +12386 +12387 +12388 +12389 +12390 +12391 +12392 +12393 +12394 +12395 +12396 +12397 +12398 +12399 +12400 +12401 +12402 +12403 +12404 +12405 +12406 +12407 +12408 +12409 +12410 +12411 +12412 +12413 +12414 +12415 +12416 +12417 +12418 +12419 +12420 +12421 +12422 +12423 +12424 +12425 +12426 +12427 +12428 +12429 +12430 +12431 +12432 +12433 +12434 +12435 +12436 +12437 +12438 +12439 +12440 +12441 +12442 +12443 +12444 +12445 +12446 +12447 +12448 +12449 +12450 +12451 +12452 +12453 +12454 +12455 +12456 +12457 +12458 +12459 +12460 +12461 +12462 +12463 +12464 +12465 +12466 +12467 +12468 +12469 +12470 +12471 +12472 +12473 +12474 +12475 +12476 +12477 +12478 +12479 +12480 +12481 +12482 +12483 +12484 +12485 +12486 +12487 +12488 +12489 +12490 +12491 +12492 +12493 +12494 +12495 +12496 +12497 +12498 +12499 +12500 +12501 +12502 +12503 +12504 +12505 +12506 +12507 +12508 +12509 +12510 +12511 +12512 +12513 +12514 +12515 +12516 +12517 +12518 +12519 +12520 +12521 +12522 +12523 +12524 +12525 +12526 +12527 +12528 +12529 +12530 +12531 +12532 +12533 +12534 +12535 +12536 +12537 +12538 +12539 +12540 +12541 +12542 +12543 +12544 +12545 +12546 +12547 +12548 +12549 +12550 +12551 +12552 +12553 +12554 +12555 +12556 +12557 +12558 +12559 +12560 +12561 +12562 +12563 +12564 +12565 +12566 +12567 +12568 +12569 +12570 +12571 +12572 +12573 +12574 +12575 +12576 +12577 +12578 +12579 +12580 +12581 +12582 +12583 +12584 +12585 +12586 +12587 +12588 +12589 +12590 +12591 +12592 +12593 +12594 +12595 +12596 +12597 +12598 +12599 +12600 +12601 +12602 +12603 +12604 +12605 +12606 +12607 +12608 +12609 +12610 +12611 +12612 +12613 +12614 +12615 +12616 +12617 +12618 +12619 +12620 +12621 +12622 +12623 +12624 +12625 +12626 +12627 +12628 +12629 +12630 +12631 +12632 +12633 +12634 +12635 +12636 +12637 +12638 +12639 +12640 +12641 +12642 +12643 +12644 +12645 +12646 +12647 +12648 +12649 +12650 +12651 +12652 +12653 +12654 +12655 +12656 +12657 +12658 +12659 +12660 +12661 +12662 +12663 +12664 +12665 +12666 +12667 +12668 +12669 +12670 +12671 +12672 +12673 +12674 +12675 +12676 +12677 +12678 +12679 +12680 +12681 +12682 +12683 +12684 +12685 +12686 +12687 +12688 +12689 +12690 +12691 +12692 +12693 +12694 +12695 +12696 +12697 +12698 +12699 +12700 +12701 +12702 +12703 +12704 +12705 +12706 +12707 +12708 +12709 +12710 +12711 +12712 +12713 +12714 +12715 +12716 +12717 +12718 +12719 +12720 +12721 +12722 +12723 +12724 +12725 +12726 +12727 +12728 +12729 +12730 +12731 +12732 +12733 +12734 +12735 +12736 +12737 +12738 +12739 +12740 +12741 +12742 +12743 +12744 +12745 +12746 +12747 +12748 +12749 +12750 +12751 +12752 +12753 +12754 +12755 +12756 +12757 +12758 +12759 +12760 +12761 +12762 +12763 +12764 +12765 +12766 +12767 +12768 +12769 +12770 +12771 +12772 +12773 +12774 +12775 +12776 +12777 +12778 +12779 +12780 +12781 +12782 +12783 +12784 +12785 +12786 +12787 +12788 +12789 +12790 +12791 +12792 +12793 +12794 +12795 +12796 +12797 +12798 +12799 +12800 +12801 +12802 +12803 +12804 +12805 +12806 +12807 +12808 +12809 +12810 +12811 +12812 +12813 +12814 +12815 +12816 +12817 +12818 +12819 +12820 +12821 +12822 +12823 +12824 +12825 +12826 +12827 +12828 +12829 +12830 +12831 +12832 +12833 +12834 +12835 +12836 +12837 +12838 +12839 +12840 +12841 +12842 +12843 +12844 +12845 +12846 +12847 +12848 +12849 +12850 +12851 +12852 +12853 +12854 +12855 +12856 +12857 +12858 +12859 +12860 +12861 +12862 +12863 +12864 +12865 +12866 +12867 +12868 +12869 +12870 +12871 +12872 +12873 +12874 +12875 +12876 +12877 +12878 +12879 +12880 +12881 +12882 +12883 +12884 +12885 +12886 +12887 +12888 +12889 +12890 +12891 +12892 +12893 +12894 +12895 +12896 +12897 +12898 +12899 +12900 +12901 +12902 +12903 +12904 +12905 +12906 +12907 +12908 +12909 +12910 +12911 +12912 +12913 +12914 +12915 +12916 +12917 +12918 +12919 +12920 +12921 +12922 +12923 +12924 +12925 +12926 +12927 +12928 +12929 +12930 +12931 +12932 +12933 +12934 +12935 +12936 +12937 +12938 +12939 +12940 +12941 +12942 +12943 +12944 +12945 +12946 +12947 +12948 +12949 +12950 +12951 +12952 +12953 +12954 +12955 +12956 +12957 +12958 +12959 +12960 +12961 +12962 +12963 +12964 +12965 +12966 +12967 +12968 +12969 +12970 +12971 +12972 +12973 +12974 +12975 +12976 +12977 +12978 +12979 +12980 +12981 +12982 +12983 +12984 +12985 +12986 +12987 +12988 +12989 +12990 +12991 +12992 +12993 +12994 +12995 +12996 +12997 +12998 +12999 +13000 +13001 +13002 +13003 +13004 +13005 +13006 +13007 +13008 +13009 +13010 +13011 +13012 +13013 +13014 +13015 +13016 +13017 +13018 +13019 +13020 +13021 +13022 +13023 +13024 +13025 +13026 +13027 +13028 +13029 +13030 +13031 +13032 +13033 +13034 +13035 +13036 +13037 +13038 +13039 +13040 +13041 +13042 +13043 +13044 +13045 +13046 +13047 +13048 +13049 +13050 +13051 +13052 +13053 +13054 +13055 +13056 +13057 +13058 +13059 +13060 +13061 +13062 +13063 +13064 +13065 +13066 +13067 +13068 +13069 +13070 +13071 +13072 +13073 +13074 +13075 +13076 +13077 +13078 +13079 +13080 +13081 +13082 +13083 +13084 +13085 +13086 +13087 +13088 +13089 +13090 +13091 +13092 +13093 +13094 +13095 +13096 +13097 +13098 +13099 +13100 +13101 +13102 +13103 +13104 +13105 +13106 +13107 +13108 +13109 +13110 +13111 +13112 +13113 +13114 +13115 +13116 +13117 +13118 +13119 +13120 +13121 +13122 +13123 +13124 +13125 +13126 +13127 +13128 +13129 +13130 +13131 +13132 +13133 +13134 +13135 +13136 +13137 +13138 +13139 +13140 +13141 +13142 +13143 +13144 +13145 +13146 +13147 +13148 +13149 +13150 +13151 +13152 +13153 +13154 +13155 +13156 +13157 +13158 +13159 +13160 +13161 +13162 +13163 +13164 +13165 +13166 +13167 +13168 +13169 +13170 +13171 +13172 +13173 +13174 +13175 +13176 +13177 +13178 +13179 +13180 +13181 +13182 +13183 +13184 +13185 +13186 +13187 +13188 +13189 +13190 +13191 +13192 +13193 +13194 +13195 +13196 +13197 +13198 +13199 +13200 +13201 +13202 +13203 +13204 +13205 +13206 +13207 +13208 +13209 +13210 +13211 +13212 +13213 +13214 +13215 +13216 +13217 +13218 +13219 +13220 +13221 +13222 +13223 +13224 +13225 +13226 +13227 +13228 +13229 +13230 +13231 +13232 +13233 +13234 +13235 +13236 +13237 +13238 +13239 +13240 +13241 +13242 +13243 +13244 +13245 +13246 +13247 +13248 +13249 +13250 +13251 +13252 +13253 +13254 +13255 +13256 +13257 +13258 +13259 +13260 +13261 +13262 +13263 +13264 +13265 +13266 +13267 +13268 +13269 +13270 +13271 +13272 +13273 +13274 +13275 +13276 +13277 +13278 +13279 +13280 +13281 +13282 +13283 +13284 +13285 +13286 +13287 +13288 +13289 +13290 +13291 +13292 +13293 +13294 +13295 +13296 +13297 +13298 +13299 +13300 +13301 +13302 +13303 +13304 +13305 +13306 +13307 +13308 +13309 +13310 +13311 +13312 +13313 +13314 +13315 +13316 +13317 +13318 +13319 +13320 +13321 +13322 +13323 +13324 +13325 +13326 +13327 +13328 +13329 +13330 +13331 +13332 +13333 +13334 +13335 +13336 +13337 +13338 +13339 +13340 +13341 +13342 +13343 +13344 +13345 +13346 +13347 +13348 +13349 +13350 +13351 +13352 +13353 +13354 +13355 +13356 +13357 +13358 +13359 +13360 +13361 +13362 +13363 +13364 +13365 +13366 +13367 +13368 +13369 +13370 +13371 +13372 +13373 +13374 +13375 +13376 +13377 +13378 +13379 +13380 +13381 +13382 +13383 +13384 +13385 +13386 +13387 +13388 +13389 +13390 +13391 +13392 +13393 +13394 +13395 +13396 +13397 +13398 +13399 +13400 +13401 +13402 +13403 +13404 +13405 +13406 +13407 +13408 +13409 +13410 +13411 +13412 +13413 +13414 +13415 +13416 +13417 +13418 +13419 +13420 +13421 +13422 +13423 +13424 +13425 +13426 +13427 +13428 +13429 +13430 +13431 +13432 +13433 +13434 +13435 +13436 +13437 +13438 +13439 +13440 +13441 +13442 +13443 +13444 +13445 +13446 +13447 +13448 +13449 +13450 +13451 +13452 +13453 +13454 +13455 +13456 +13457 +13458 +13459 +13460 +13461 +13462 +13463 +13464 +13465 +13466 +13467 +13468 +13469 +13470 +13471 +13472 +13473 +13474 +13475 +13476 +13477 +13478 +13479 +13480 +13481 +13482 +13483 +13484 +13485 +13486 +13487 +13488 +13489 +13490 +13491 +13492 +13493 +13494 +13495 +13496 +13497 +13498 +13499 +13500 +13501 +13502 +13503 +13504 +13505 +13506 +13507 +13508 +13509 +13510 +13511 +13512 +13513 +13514 +13515 +13516 +13517 +13518 +13519 +13520 +13521 +13522 +13523 +13524 +13525 +13526 +13527 +13528 +13529 +13530 +13531 +13532 +13533 +13534 +13535 +13536 +13537 +13538 +13539 +13540 +13541 +13542 +13543 +13544 +13545 +13546 +13547 +13548 +13549 +13550 +13551 +13552 +13553 +13554 +13555 +13556 +13557 +13558 +13559 +13560 +13561 +13562 +13563 +13564 +13565 +13566 +13567 +13568 +13569 +13570 +13571 +13572 +13573 +13574 +13575 +13576 +13577 +13578 +13579 +13580 +13581 +13582 +13583 +13584 +13585 +13586 +13587 +13588 +13589 +13590 +13591 +13592 +13593 +13594 +13595 +13596 +13597 +13598 +13599 +13600 +13601 +13602 +13603 +13604 +13605 +13606 +13607 +13608 +13609 +13610 +13611 +13612 +13613 +13614 +13615 +13616 +13617 +13618 +13619 +13620 +13621 +13622 +13623 +13624 +13625 +13626 +13627 +13628 +13629 +13630 +13631 +13632 +13633 +13634 +13635 +13636 +13637 +13638 +13639 +13640 +13641 +13642 +13643 +13644 +13645 +13646 +13647 +13648 +13649 +13650 +13651 +13652 +13653 +13654 +13655 +13656 +13657 +13658 +13659 +13660 +13661 +13662 +13663 +13664 +13665 +13666 +13667 +13668 +13669 +13670 +13671 +13672 +13673 +13674 +13675 +13676 +13677 +13678 +13679 +13680 +13681 +13682 +13683 +13684 +13685 +13686 +13687 +13688 +13689 +13690 +13691 +13692 +13693 +13694 +13695 +13696 +13697 +13698 +13699 +13700 +13701 +13702 +13703 +13704 +13705 +13706 +13707 +13708 +13709 +13710 +13711 +13712 +13713 +13714 +13715 +13716 +13717 +13718 +13719 +13720 +13721 +13722 +13723 +13724 +13725 +13726 +13727 +13728 +13729 +13730 +13731 +13732 +13733 +13734 +13735 +13736 +13737 +13738 +13739 +13740 +13741 +13742 +13743 +13744 +13745 +13746 +13747 +13748 +13749 +13750 +13751 +13752 +13753 +13754 +13755 +13756 +13757 +13758 +13759 +13760 +13761 +13762 +13763 +13764 +13765 +13766 +13767 +13768 +13769 +13770 +13771 +13772 +13773 +13774 +13775 +13776 +13777 +13778 +13779 +13780 +13781 +13782 +13783 +13784 +13785 +13786 +13787 +13788 +13789 +13790 +13791 +13792 +13793 +13794 +13795 +13796 +13797 +13798 +13799 +13800 +13801 +13802 +13803 +13804 +13805 +13806 +13807 +13808 +13809 +13810 +13811 +13812 +13813 +13814 +13815 +13816 +13817 +13818 +13819 +13820 +13821 +13822 +13823 +13824 +13825 +13826 +13827 +13828 +13829 +13830 +13831 +13832 +13833 +13834 +13835 +13836 +13837 +13838 +13839 +13840 +13841 +13842 +13843 +13844 +13845 +13846 +13847 +13848 +13849 +13850 +13851 +13852 +13853 +13854 +13855 +13856 +13857 +13858 +13859 +13860 +13861 +13862 +13863 +13864 +13865 +13866 +13867 +13868 +13869 +13870 +13871 +13872 +13873 +13874 +13875 +13876 +13877 +13878 +13879 +13880 +13881 +13882 +13883 +13884 +13885 +13886 +13887 +13888 +13889 +13890 +13891 +13892 +13893 +13894 +13895 +13896 +13897 +13898 +13899 +13900 +13901 +13902 +13903 +13904 +13905 +13906 +13907 +13908 +13909 +13910 +13911 +13912 +13913 +13914 +13915 +13916 +13917 +13918 +13919 +13920 +13921 +13922 +13923 +13924 +13925 +13926 +13927 +13928 +13929 +13930 +13931 +13932 +13933 +13934 +13935 +13936 +13937 +13938 +13939 +13940 +13941 +13942 +13943 +13944 +13945 +13946 +13947 +13948 +13949 +13950 +13951 +13952 +13953 +13954 +13955 +13956 +13957 +13958 +13959 +13960 +13961 +13962 +13963 +13964 +13965 +13966 +13967 +13968 +13969 +13970 +13971 +13972 +13973 +13974 +13975 +13976 +13977 +13978 +13979 +13980 +13981 +13982 +13983 +13984 +13985 +13986 +13987 +13988 +13989 +13990 +13991 +13992 +13993 +13994 +13995 +13996 +13997 +13998 +13999 +14000 +14001 +14002 +14003 +14004 +14005 +14006 +14007 +14008 +14009 +14010 +14011 +14012 +14013 +14014 +14015 +14016 +14017 +14018 +14019 +14020 +14021 +14022 +14023 +14024 +14025 +14026 +14027 +14028 +14029 +14030 +14031 +14032 +14033 +14034 +14035 +14036 +14037 +14038 +14039 +14040 +14041 +14042 +14043 +14044 +14045 +14046 +14047 +14048 +14049 +14050 +14051 +14052 +14053 +14054 +14055 +14056 +14057 +14058 +14059 +14060 +14061 +14062 +14063 +14064 +14065 +14066 +14067 +14068 +14069 +14070 +14071 +14072 +14073 +14074 +14075 +14076 +14077 +14078 +14079 +14080 +14081 +14082 +14083 +14084 +14085 +14086 +14087 +14088 +14089 +14090 +14091 +14092 +14093 +14094 +14095 +14096 +14097 +14098 +14099 +14100 +14101 +14102 +14103 +14104 +14105 +14106 +14107 +14108 +14109 +14110 +14111 +14112 +14113 +14114 +14115 +14116 +14117 +14118 +14119 +14120 +14121 +14122 +14123 +14124 +14125 +14126 +14127 +14128 +14129 +14130 +14131 +14132 +14133 +14134 +14135 +14136 +14137 +14138 +14139 +14140 +14141 +14142 +14143 +14144 +14145 +14146 +14147 +14148 +14149 +14150 +14151 +14152 +14153 +14154 +14155 +14156 +14157 +14158 +14159 +14160 +14161 +14162 +14163 +14164 +14165 +14166 +14167 +14168 +14169 +14170 +14171 +14172 +14173 +14174 +14175 +14176 +14177 +14178 +14179 +14180 +14181 +14182 +14183 +14184 +14185 +14186 +14187 +14188 +14189 +14190 +14191 +14192 +14193 +14194 +14195 +14196 +14197 +14198 +14199 +14200 +14201 +14202 +14203 +14204 +14205 +14206 +14207 +14208 +14209 +14210 +14211 +14212 +14213 +14214 +14215 +14216 +14217 +14218 +14219 +14220 +14221 +14222 +14223 +14224 +14225 +14226 +14227 +14228 +14229 +14230 +14231 +14232 +14233 +14234 +14235 +14236 +14237 +14238 +14239 +14240 +14241 +14242 +14243 +14244 +14245 +14246 +14247 +14248 +14249 +14250 +14251 +14252 +14253 +14254 +14255 +14256 +14257 +14258 +14259 +14260 +14261 +14262 +14263 +14264 +14265 +14266 +14267 +14268 +14269 +14270 +14271 +14272 +14273 +14274 +14275 +14276 +14277 +14278 +14279 +14280 +14281 +14282 +14283 +14284 +14285 +14286 +14287 +14288 +14289 +14290 +14291 +14292 +14293 +14294 +14295 +14296 +14297 +14298 +14299 +14300 +14301 +14302 +14303 +14304 +14305 +14306 +14307 +14308 +14309 +14310 +14311 +14312 +14313 +14314 +14315 +14316 +14317 +14318 +14319 +14320 +14321 +14322 +14323 +14324 +14325 +14326 +14327 +14328 +14329 +14330 +14331 +14332 +14333 +14334 +14335 +14336 +14337 +14338 +14339 +14340 +14341 +14342 +14343 +14344 +14345 +14346 +14347 +14348 +14349 +14350 +14351 +14352 +14353 +14354 +14355 +14356 +14357 +14358 +14359 +14360 +14361 +14362 +14363 +14364 +14365 +14366 +14367 +14368 +14369 +14370 +14371 +14372 +14373 +14374 +14375 +14376 +14377 +14378 +14379 +14380 +14381 +14382 +14383 +14384 +14385 +14386 +14387 +14388 +14389 +14390 +14391 +14392 +14393 +14394 +14395 +14396 +14397 +14398 +14399 +14400 +14401 +14402 +14403 +14404 +14405 +14406 +14407 +14408 +14409 +14410 +14411 +14412 +14413 +14414 +14415 +14416 +14417 +14418 +14419 +14420 +14421 +14422 +14423 +14424 +14425 +14426 +14427 +14428 +14429 +14430 +14431 +14432 +14433 +14434 +14435 +14436 +14437 +14438 +14439 +14440 +14441 +14442 +14443 +14444 +14445 +14446 +14447 +14448 +14449 +14450 +14451 +14452 +14453 +14454 +14455 +14456 +14457 +14458 +14459 +14460 +14461 +14462 +14463 +14464 +14465 +14466 +14467 +14468 +14469 +14470 +14471 +14472 +14473 +14474 +14475 +14476 +14477 +14478 +14479 +14480 +14481 +14482 +14483 +14484 +14485 +14486 +14487 +14488 +14489 +14490 +14491 +14492 +14493 +14494 +14495 +14496 +14497 +14498 +14499 +14500 +14501 +14502 +14503 +14504 +14505 +14506 +14507 +14508 +14509 +14510 +14511 +14512 +14513 +14514 +14515 +14516 +14517 +14518 +14519 +14520 +14521 +14522 +14523 +14524 +14525 +14526 +14527 +14528 +14529 +14530 +14531 +14532 +14533 +14534 +14535 +14536 +14537 +14538 +14539 +14540 +14541 +14542 +14543 +14544 +14545 +14546 +14547 +14548 +14549 +14550 +14551 +14552 +14553 +14554 +14555 +14556 +14557 +14558 +14559 +14560 +14561 +14562 +14563 +14564 +14565 +14566 +14567 +14568 +14569 +14570 +14571 +14572 +14573 +14574 +14575 +14576 +14577 +14578 +14579 +14580 +14581 +14582 +14583 +14584 +14585 +14586 +14587 +14588 +14589 +14590 +14591 +14592 +14593 +14594 +14595 +14596 +14597 +14598 +14599 +14600 +14601 +14602 +14603 +14604 +14605 +14606 +14607 +14608 +14609 +14610 +14611 +14612 +14613 +14614 +14615 +14616 +14617 +14618 +14619 +14620 +14621 +14622 +14623 +14624 +14625 +14626 +14627 +14628 +14629 +14630 +14631 +14632 +14633 +14634 +14635 +14636 +14637 +14638 +14639 +14640 +14641 +14642 +14643 +14644 +14645 +14646 +14647 +14648 +14649 +14650 +14651 +14652 +14653 +14654 +14655 +14656 +14657 +14658 +14659 +14660 +14661 +14662 +14663 +14664 +14665 +14666 +14667 +14668 +14669 +14670 +14671 +14672 +14673 +14674 +14675 +14676 +14677 +14678 +14679 +14680 +14681 +14682 +14683 +14684 +14685 +14686 +14687 +14688 +14689 +14690 +14691 +14692 +14693 +14694 +14695 +14696 +14697 +14698 +14699 +14700 +14701 +14702 +14703 +14704 +14705 +14706 +14707 +14708 +14709 +14710 +14711 +14712 +14713 +14714 +14715 +14716 +14717 +14718 +14719 +14720 +14721 +14722 +14723 +14724 +14725 +14726 +14727 +14728 +14729 +14730 +14731 +14732 +14733 +14734 +14735 +14736 +14737 +14738 +14739 +14740 +14741 +14742 +14743 +14744 +14745 +14746 +14747 +14748 +14749 +14750 +14751 +14752 +14753 +14754 +14755 +14756 +14757 +14758 +14759 +14760 +14761 +14762 +14763 +14764 +14765 +14766 +14767 +14768 +14769 +14770 +14771 +14772 +14773 +14774 +14775 +14776 +14777 +14778 +14779 +14780 +14781 +14782 +14783 +14784 +14785 +14786 +14787 +14788 +14789 +14790 +14791 +14792 +14793 +14794 +14795 +14796 +14797 +14798 +14799 +14800 +14801 +14802 +14803 +14804 +14805 +14806 +14807 +14808 +14809 +14810 +14811 +14812 +14813 +14814 +14815 +14816 +14817 +14818 +14819 +14820 +14821 +14822 +14823 +14824 +14825 +14826 +14827 +14828 +14829 +14830 +14831 +14832 +14833 +14834 +14835 +14836 +14837 +14838 +14839 +14840 +14841 +14842 +14843 +14844 +14845 +14846 +14847 +14848 +14849 +14850 +14851 +14852 +14853 +14854 +14855 +14856 +14857 +14858 +14859 +14860 +14861 +14862 +14863 +14864 +14865 +14866 +14867 +14868 +14869 +14870 +14871 +14872 +14873 +14874 +14875 +14876 +14877 +14878 +14879 +14880 +14881 +14882 +14883 +14884 +14885 +14886 +14887 +14888 +14889 +14890 +14891 +14892 +14893 +14894 +14895 +14896 +14897 +14898 +14899 +14900 +14901 +14902 +14903 +14904 +14905 +14906 +14907 +14908 +14909 +14910 +14911 +14912 +14913 +14914 +14915 +14916 +14917 +14918 +14919 +14920 +14921 +14922 +14923 +14924 +14925 +14926 +14927 +14928 +14929 +14930 +14931 +14932 +14933 +14934 +14935 +14936 +14937 +14938 +14939 +14940 +14941 +14942 +14943 +14944 +14945 +14946 +14947 +14948 +14949 +14950 +14951 +14952 +14953 +14954 +14955 +14956 +14957 +14958 +14959 +14960 +14961 +14962 +14963 +14964 +14965 +14966 +14967 +14968 +14969 +14970 +14971 +14972 +14973 +14974 +14975 +14976 +14977 +14978 +14979 +14980 +14981 +14982 +14983 +14984 +14985 +14986 +14987 +14988 +14989 +14990 +14991 +14992 +14993 +14994 +14995 +14996 +14997 +14998 +14999 +15000 +15001 +15002 +15003 +15004 +15005 +15006 +15007 +15008 +15009 +15010 +15011 +15012 +15013 +15014 +15015 +15016 +15017 +15018 +15019 +15020 +15021 +15022 +15023 +15024 +15025 +15026 +15027 +15028 +15029 +15030 +15031 +15032 +15033 +15034 +15035 +15036 +15037 +15038 +15039 +15040 +15041 +15042 +15043 +15044 +15045 +15046 +15047 +15048 +15049 +15050 +15051 +15052 +15053 +15054 +15055 +15056 +15057 +15058 +15059 +15060 +15061 +15062 +15063 +15064 +15065 +15066 +15067 +15068 +15069 +15070 +15071 +15072 +15073 +15074 +15075 +15076 +15077 +15078 +15079 +15080 +15081 +15082 +15083 +15084 +15085 +15086 +15087 +15088 +15089 +15090 +15091 +15092 +15093 +15094 +15095 +15096 +15097 +15098 +15099 +15100 +15101 +15102 +15103 +15104 +15105 +15106 +15107 +15108 +15109 +15110 +15111 +15112 +15113 +15114 +15115 +15116 +15117 +15118 +15119 +15120 +15121 +15122 +15123 +15124 +15125 +15126 +15127 +15128 +15129 +15130 +15131 +15132 +15133 +15134 +15135 +15136 +15137 +15138 +15139 +15140 +15141 +15142 +15143 +15144 +15145 +15146 +15147 +15148 +15149 +15150 +15151 +15152 +15153 +15154 +15155 +15156 +15157 +15158 +15159 +15160 +15161 +15162 +15163 +15164 +15165 +15166 +15167 +15168 +15169 +15170 +15171 +15172 +15173 +15174 +15175 +15176 +15177 +15178 +15179 +15180 +15181 +15182 +15183 +15184 +15185 +15186 +15187 +15188 +15189 +15190 +15191 +15192 +15193 +15194 +15195 +15196 +15197 +15198 +15199 +15200 +15201 +15202 +15203 +15204 +15205 +15206 +15207 +15208 +15209 +15210 +15211 +15212 +15213 +15214 +15215 +15216 +15217 +15218 +15219 +15220 +15221 +15222 +15223 +15224 +15225 +15226 +15227 +15228 +15229 +15230 +15231 +15232 +15233 +15234 +15235 +15236 +15237 +15238 +15239 +15240 +15241 +15242 +15243 +15244 +15245 +15246 +15247 +15248 +15249 +15250 +15251 +15252 +15253 +15254 +15255 +15256 +15257 +15258 +15259 +15260 +15261 +15262 +15263 +15264 +15265 +15266 +15267 +15268 +15269 +15270 +15271 +15272 +15273 +15274 +15275 +15276 +15277 +15278 +15279 +15280 +15281 +15282 +15283 +15284 +15285 +15286 +15287 +15288 +15289 +15290 +15291 +15292 +15293 +15294 +15295 +15296 +15297 +15298 +15299 +15300 +15301 +15302 +15303 +15304 +15305 +15306 +15307 +15308 +15309 +15310 +15311 +15312 +15313 +15314 +15315 +15316 +15317 +15318 +15319 +15320 +15321 +15322 +15323 +15324 +15325 +15326 +15327 +15328 +15329 +15330 +15331 +15332 +15333 +15334 +15335 +15336 +15337 +15338 +15339 +15340 +15341 +15342 +15343 +15344 +15345 +15346 +15347 +15348 +15349 +15350 +15351 +15352 +15353 +15354 +15355 +15356 +15357 +15358 +15359 +15360 +15361 +15362 +15363 +15364 +15365 +15366 +15367 +15368 +15369 +15370 +15371 +15372 +15373 +15374 +15375 +15376 +15377 +15378 +15379 +15380 +15381 +15382 +15383 +15384 +15385 +15386 +15387 +15388 +15389 +15390 +15391 +15392 +15393 +15394 +15395 +15396 +15397 +15398 +15399 +15400 +15401 +15402 +15403 +15404 +15405 +15406 +15407 +15408 +15409 +15410 +15411 +15412 +15413 +15414 +15415 +15416 +15417 +15418 +15419 +15420 +15421 +15422 +15423 +15424 +15425 +15426 +15427 +15428 +15429 +15430 +15431 +15432 +15433 +15434 +15435 +15436 +15437 +15438 +15439 +15440 +15441 +15442 +15443 +15444 +15445 +15446 +15447 +15448 +15449 +15450 +15451 +15452 +15453 +15454 +15455 +15456 +15457 +15458 +15459 +15460 +15461 +15462 +15463 +15464 +15465 +15466 +15467 +15468 +15469 +15470 +15471 +15472 +15473 +15474 +15475 +15476 +15477 +15478 +15479 +15480 +15481 +15482 +15483 +15484 +15485 +15486 +15487 +15488 +15489 +15490 +15491 +15492 +15493 +15494 +15495 +15496 +15497 +15498 +15499 +15500 +15501 +15502 +15503 +15504 +15505 +15506 +15507 +15508 +15509 +15510 +15511 +15512 +15513 +15514 +15515 +15516 +15517 +15518 +15519 +15520 +15521 +15522 +15523 +15524 +15525 +15526 +15527 +15528 +15529 +15530 +15531 +15532 +15533 +15534 +15535 +15536 +15537 +15538 +15539 +15540 +15541 +15542 +15543 +15544 +15545 +15546 +15547 +15548 +15549 +15550 +15551 +15552 +15553 +15554 +15555 +15556 +15557 +15558 +15559 +15560 +15561 +15562 +15563 +15564 +15565 +15566 +15567 +15568 +15569 +15570 +15571 +15572 +15573 +15574 +15575 +15576 +15577 +15578 +15579 +15580 +15581 +15582 +15583 +15584 +15585 +15586 +15587 +15588 +15589 +15590 +15591 +15592 +15593 +15594 +15595 +15596 +15597 +15598 +15599 +15600 +15601 +15602 +15603 +15604 +15605 +15606 +15607 +15608 +15609 +15610 +15611 +15612 +15613 +15614 +15615 +15616 +15617 +15618 +15619 +15620 +15621 +15622 +15623 +15624 +15625 +15626 +15627 +15628 +15629 +15630 +15631 +15632 +15633 +15634 +15635 +15636 +15637 +15638 +15639 +15640 +15641 +15642 +15643 +15644 +15645 +15646 +15647 +15648 +15649 +15650 +15651 +15652 +15653 +15654 +15655 +15656 +15657 +15658 +15659 +15660 +15661 +15662 +15663 +15664 +15665 +15666 +15667 +15668 +15669 +15670 +15671 +15672 +15673 +15674 +15675 +15676 +15677 +15678 +15679 +15680 +15681 +15682 +15683 +15684 +15685 +15686 +15687 +15688 +15689 +15690 +15691 +15692 +15693 +15694 +15695 +15696 +15697 +15698 +15699 +15700 +15701 +15702 +15703 +15704 +15705 +15706 +15707 +15708 +15709 +15710 +15711 +15712 +15713 +15714 +15715 +15716 +15717 +15718 +15719 +15720 +15721 +15722 +15723 +15724 +15725 +15726 +15727 +15728 +15729 +15730 +15731 +15732 +15733 +15734 +15735 +15736 +15737 +15738 +15739 +15740 +15741 +15742 +15743 +15744 +15745 +15746 +15747 +15748 +15749 +15750 +15751 +15752 +15753 +15754 +15755 +15756 +15757 +15758 +15759 +15760 +15761 +15762 +15763 +15764 +15765 +15766 +15767 +15768 +15769 +15770 +15771 +15772 +15773 +15774 +15775 +15776 +15777 +15778 +15779 +15780 +15781 +15782 +15783 +15784 +15785 +15786 +15787 +15788 +15789 +15790 +15791 +15792 +15793 +15794 +15795 +15796 +15797 +15798 +15799 +15800 +15801 +15802 +15803 +15804 +15805 +15806 +15807 +15808 +15809 +15810 +15811 +15812 +15813 +15814 +15815 +15816 +15817 +15818 +15819 +15820 +15821 +15822 +15823 +15824 +15825 +15826 +15827 +15828 +15829 +15830 +15831 +15832 +15833 +15834 +15835 +15836 +15837 +15838 +15839 +15840 +15841 +15842 +15843 +15844 +15845 +15846 +15847 +15848 +15849 +15850 +15851 +15852 +15853 +15854 +15855 +15856 +15857 +15858 +15859 +15860 +15861 +15862 +15863 +15864 +15865 +15866 +15867 +15868 +15869 +15870 +15871 +15872 +15873 +15874 +15875 +15876 +15877 +15878 +15879 +15880 +15881 +15882 +15883 +15884 +15885 +15886 +15887 +15888 +15889 +15890 +15891 +15892 +15893 +15894 +15895 +15896 +15897 +15898 +15899 +15900 +15901 +15902 +15903 +15904 +15905 +15906 +15907 +15908 +15909 +15910 +15911 +15912 +15913 +15914 +15915 +15916 +15917 +15918 +15919 +15920 +15921 +15922 +15923 +15924 +15925 +15926 +15927 +15928 +15929 +15930 +15931 +15932 +15933 +15934 +15935 +15936 +15937 +15938 +15939 +15940 +15941 +15942 +15943 +15944 +15945 +15946 +15947 +15948 +15949 +15950 +15951 +15952 +15953 +15954 +15955 +15956 +15957 +15958 +15959 +15960 +15961 +15962 +15963 +15964 +15965 +15966 +15967 +15968 +15969 +15970 +15971 +15972 +15973 +15974 +15975 +15976 +15977 +15978 +15979 +15980 +15981 +15982 +15983 +15984 +15985 +15986 +15987 +15988 +15989 +15990 +15991 +15992 +15993 +15994 +15995 +15996 +15997 +15998 +15999 +16000 +16001 +16002 +16003 +16004 +16005 +16006 +16007 +16008 +16009 +16010 +16011 +16012 +16013 +16014 +16015 +16016 +16017 +16018 +16019 +16020 +16021 +16022 +16023 +16024 +16025 +16026 +16027 +16028 +16029 +16030 +16031 +16032 +16033 +16034 +16035 +16036 +16037 +16038 +16039 +16040 +16041 +16042 +16043 +16044 +16045 +16046 +16047 +16048 +16049 +16050 +16051 +16052 +16053 +16054 +16055 +16056 +16057 +16058 +16059 +16060 +16061 +16062 +16063 +16064 +16065 +16066 +16067 +16068 +16069 +16070 +16071 +16072 +16073 +16074 +16075 +16076 +16077 +16078 +16079 +16080 +16081 +16082 +16083 +16084 +16085 +16086 +16087 +16088 +16089 +16090 +16091 +16092 +16093 +16094 +16095 +16096 +16097 +16098 +16099 +16100 +16101 +16102 +16103 +16104 +16105 +16106 +16107 +16108 +16109 +16110 +16111 +16112 +16113 +16114 +16115 +16116 +16117 +16118 +16119 +16120 +16121 +16122 +16123 +16124 +16125 +16126 +16127 +16128 +16129 +16130 +16131 +16132 +16133 +16134 +16135 +16136 +16137 +16138 +16139 +16140 +16141 +16142 +16143 +16144 +16145 +16146 +16147 +16148 +16149 +16150 +16151 +16152 +16153 +16154 +16155 +16156 +16157 +16158 +16159 +16160 +16161 +16162 +16163 +16164 +16165 +16166 +16167 +16168 +16169 +16170 +16171 +16172 +16173 +16174 +16175 +16176 +16177 +16178 +16179 +16180 +16181 +16182 +16183 +16184 +16185 +16186 +16187 +16188 +16189 +16190 +16191 +16192 +16193 +16194 +16195 +16196 +16197 +16198 +16199 +16200 +16201 +16202 +16203 +16204 +16205 +16206 +16207 +16208 +16209 +16210 +16211 +16212 +16213 +16214 +16215 +16216 +16217 +16218 +16219 +16220 +16221 +16222 +16223 +16224 +16225 +16226 +16227 +16228 +16229 +16230 +16231 +16232 +16233 +16234 +16235 +16236 +16237 +16238 +16239 +16240 +16241 +16242 +16243 +16244 +16245 +16246 +16247 +16248 +16249 +16250 +16251 +16252 +16253 +16254 +16255 +16256 +16257 +16258 +16259 +16260 +16261 +16262 +16263 +16264 +16265 +16266 +16267 +16268 +16269 +16270 +16271 +16272 +16273 +16274 +16275 +16276 +16277 +16278 +16279 +16280 +16281 +16282 +16283 +16284 +16285 +16286 +16287 +16288 +16289 +16290 +16291 +16292 +16293 +16294 +16295 +16296 +16297 +16298 +16299 +16300 +16301 +16302 +16303 +16304 +16305 +16306 +16307 +16308 +16309 +16310 +16311 +16312 +16313 +16314 +16315 +16316 +16317 +16318 +16319 +16320 +16321 +16322 +16323 +16324 +16325 +16326 +16327 +16328 +16329 +16330 +16331 +16332 +16333 +16334 +16335 +16336 +16337 +16338 +16339 +16340 +16341 +16342 +16343 +16344 +16345 +16346 +16347 +16348 +16349 +16350 +16351 +16352 +16353 +16354 +16355 +16356 +16357 +16358 +16359 +16360 +16361 +16362 +16363 +16364 +16365 +16366 +16367 +16368 +16369 +16370 +16371 +16372 +16373 +16374 +16375 +16376 +16377 +16378 +16379 +16380 +16381 +16382 +16383 +16384 +16385 +16386 +16387 +16388 +16389 +16390 +16391 +16392 +16393 +16394 +16395 +16396 +16397 +16398 +16399 +16400 +16401 +16402 +16403 +16404 +16405 +16406 +16407 +16408 +16409 +16410 +16411 +16412 +16413 +16414 +16415 +16416 +16417 +16418 +16419 +16420 +16421 +16422 +16423 +16424 +16425 +16426 +16427 +16428 +16429 +16430 +16431 +16432 +16433 +16434 +16435 +16436 +16437 +16438 +16439 +16440 +16441 +16442 +16443 +16444 +16445 +16446 +16447 +16448 +16449 +16450 +16451 +16452 +16453 +16454 +16455 +16456 +16457 +16458 +16459 +16460 +16461 +16462 +16463 +16464 +16465 +16466 +16467 +16468 +16469 +16470 +16471 +16472 +16473 +16474 +16475 +16476 +16477 +16478 +16479 +16480 +16481 +16482 +16483 +16484 +16485 +16486 +16487 +16488 +16489 +16490 +16491 +16492 +16493 +16494 +16495 +16496 +16497 +16498 +16499 +16500 +16501 +16502 +16503 +16504 +16505 +16506 +16507 +16508 +16509 +16510 +16511 +16512 +16513 +16514 +16515 +16516 +16517 +16518 +16519 +16520 +16521 +16522 +16523 +16524 +16525 +16526 +16527 +16528 +16529 +16530 +16531 +16532 +16533 +16534 +16535 +16536 +16537 +16538 +16539 +16540 +16541 +16542 +16543 +16544 +16545 +16546 +16547 +16548 +16549 +16550 +16551 +16552 +16553 +16554 +16555 +16556 +16557 +16558 +16559 +16560 +16561 +16562 +16563 +16564 +16565 +16566 +16567 +16568 +16569 +16570 +16571 +16572 +16573 +16574 +16575 +16576 +16577 +16578 +16579 +16580 +16581 +16582 +16583 +16584 +16585 +16586 +16587 +16588 +16589 +16590 +16591 +16592 +16593 +16594 +16595 +16596 +16597 +16598 +16599 +16600 +16601 +16602 +16603 +16604 +16605 +16606 +16607 +16608 +16609 +16610 +16611 +16612 +16613 +16614 +16615 +16616 +16617 +16618 +16619 +16620 +16621 +16622 +16623 +16624 +16625 +16626 +16627 +16628 +16629 +16630 +16631 +16632 +16633 +16634 +16635 +16636 +16637 +16638 +16639 +16640 +16641 +16642 +16643 +16644 +16645 +16646 +16647 +16648 +16649 +16650 +16651 +16652 +16653 +16654 +16655 +16656 +16657 +16658 +16659 +16660 +16661 +16662 +16663 +16664 +16665 +16666 +16667 +16668 +16669 +16670 +16671 +16672 +16673 +16674 +16675 +16676 +16677 +16678 +16679 +16680 +16681 +16682 +16683 +16684 +16685 +16686 +16687 +16688 +16689 +16690 +16691 +16692 +16693 +16694 +16695 +16696 +16697 +16698 +16699 +16700 +16701 +16702 +16703 +16704 +16705 +16706 +16707 +16708 +16709 +16710 +16711 +16712 +16713 +16714 +16715 +16716 +16717 +16718 +16719 +16720 +16721 +16722 +16723 +16724 +16725 +16726 +16727 +16728 +16729 +16730 +16731 +16732 +16733 +16734 +16735 +16736 +16737 +16738 +16739 +16740 +16741 +16742 +16743 +16744 +16745 +16746 +16747 +16748 +16749 +16750 +16751 +16752 +16753 +16754 +16755 +16756 +16757 +16758 +16759 +16760 +16761 +16762 +16763 +16764 +16765 +16766 +16767 +16768 +16769 +16770 +16771 +16772 +16773 +16774 +16775 +16776 +16777 +16778 +16779 +16780 +16781 +16782 +16783 +16784 +16785 +16786 +16787 +16788 +16789 +16790 +16791 +16792 +16793 +16794 +16795 +16796 +16797 +16798 +16799 +16800 +16801 +16802 +16803 +16804 +16805 +16806 +16807 +16808 +16809 +16810 +16811 +16812 +16813 +16814 +16815 +16816 +16817 +16818 +16819 +16820 +16821 +16822 +16823 +16824 +16825 +16826 +16827 +16828 +16829 +16830 +16831 +16832 +16833 +16834 +16835 +16836 +16837 +16838 +16839 +16840 +16841 +16842 +16843 +16844 +16845 +16846 +16847 +16848 +16849 +16850 +16851 +16852 +16853 +16854 +16855 +16856 +16857 +16858 +16859 +16860 +16861 +16862 +16863 +16864 +16865 +16866 +16867 +16868 +16869 +16870 +16871 +16872 +16873 +16874 +16875 +16876 +16877 +16878 +16879 +16880 +16881 +16882 +16883 +16884 +16885 +16886 +16887 +16888 +16889 +16890 +16891 +16892 +16893 +16894 +16895 +16896 +16897 +16898 +16899 +16900 +16901 +16902 +16903 +16904 +16905 +16906 +16907 +16908 +16909 +16910 +16911 +16912 +16913 +16914 +16915 +16916 +16917 +16918 +16919 +16920 +16921 +16922 +16923 +16924 +16925 +16926 +16927 +16928 +16929 +16930 +16931 +16932 +16933 +16934 +16935 +16936 +16937 +16938 +16939 +16940 +16941 +16942 +16943 +16944 +16945 +16946 +16947 +16948 +16949 +16950 +16951 +16952 +16953 +16954 +16955 +16956 +16957 +16958 +16959 +16960 +16961 +16962 +16963 +16964 +16965 +16966 +16967 +16968 +16969 +16970 +16971 +16972 +16973 +16974 +16975 +16976 +16977 +16978 +16979 +16980 +16981 +16982 +16983 +16984 +16985 +16986 +16987 +16988 +16989 +16990 +16991 +16992 +16993 +16994 +16995 +16996 +16997 +16998 +16999 +17000 +17001 +17002 +17003 +17004 +17005 +17006 +17007 +17008 +17009 +17010 +17011 +17012 +17013 +17014 +17015 +17016 +17017 +17018 +17019 +17020 +17021 +17022 +17023 +17024 +17025 +17026 +17027 +17028 +17029 +17030 +17031 +17032 +17033 +17034 +17035 +17036 +17037 +17038 +17039 +17040 +17041 +17042 +17043 +17044 +17045 +17046 +17047 +17048 +17049 +17050 +17051 +17052 +17053 +17054 +17055 +17056 +17057 +17058 +17059 +17060 +17061 +17062 +17063 +17064 +17065 +17066 +17067 +17068 +17069 +17070 +17071 +17072 +17073 +17074 +17075 +17076 +17077 +17078 +17079 +17080 +17081 +17082 +17083 +17084 +17085 +17086 +17087 +17088 +17089 +17090 +17091 +17092 +17093 +17094 +17095 +17096 +17097 +17098 +17099 +17100 +17101 +17102 +17103 +17104 +17105 +17106 +17107 +17108 +17109 +17110 +17111 +17112 +17113 +17114 +17115 +17116 +17117 +17118 +17119 +17120 +17121 +17122 +17123 +17124 +17125 +17126 +17127 +17128 +17129 +17130 +17131 +17132 +17133 +17134 +17135 +17136 +17137 +17138 +17139 +17140 +17141 +17142 +17143 +17144 +17145 +17146 +17147 +17148 +17149 +17150 +17151 +17152 +17153 +17154 +17155 +17156 +17157 +17158 +17159 +17160 +17161 +17162 +17163 +17164 +17165 +17166 +17167 +17168 +17169 +17170 +17171 +17172 +17173 +17174 +17175 +17176 +17177 +17178 +17179 +17180 +17181 +17182 +17183 +17184 +17185 +17186 +17187 +17188 +17189 +17190 +17191 +17192 +17193 +17194 +17195 +17196 +17197 +17198 +17199 +17200 +17201 +17202 +17203 +17204 +17205 +17206 +17207 +17208 +17209 +17210 +17211 +17212 +17213 +17214 +17215 +17216 +17217 +17218 +17219 +17220 +17221 +17222 +17223 +17224 +17225 +17226 +17227 +17228 +17229 +17230 +17231 +17232 +17233 +17234 +17235 +17236 +17237 +17238 +17239 +17240 +17241 +17242 +17243 +17244 +17245 +17246 +17247 +17248 +17249 +17250 +17251 +17252 +17253 +17254 +17255 +17256 +17257 +17258 +17259 +17260 +17261 +17262 +17263 +17264 +17265 +17266 +17267 +17268 +17269 +17270 +17271 +17272 +17273 +17274 +17275 +17276 +17277 +17278 +17279 +17280 +17281 +17282 +17283 +17284 +17285 +17286 +17287 +17288 +17289 +17290 +17291 +17292 +17293 +17294 +17295 +17296 +17297 +17298 +17299 +17300 +17301 +17302 +17303 +17304 +17305 +17306 +17307 +17308 +17309 +17310 +17311 +17312 +17313 +17314 +17315 +17316 +17317 +17318 +17319 +17320 +17321 +17322 +17323 +17324 +17325 +17326 +17327 +17328 +17329 +17330 +17331 +17332 +17333 +17334 +17335 +17336 +17337 +17338 +17339 +17340 +17341 +17342 +17343 +17344 +17345 +17346 +17347 +17348 +17349 +17350 +17351 +17352 +17353 +17354 +17355 +17356 +17357 +17358 +17359 +17360 +17361 +17362 +17363 +17364 +17365 +17366 +17367 +17368 +17369 +17370 +17371 +17372 +17373 +17374 +17375 +17376 +17377 +17378 +17379 +17380 +17381 +17382 +17383 +17384 +17385 +17386 +17387 +17388 +17389 +17390 +17391 +17392 +17393 +17394 +17395 +17396 +17397 +17398 +17399 +17400 +17401 +17402 +17403 +17404 +17405 +17406 +17407 +17408 +17409 +17410 +17411 +17412 +17413 +17414 +17415 +17416 +17417 +17418 +17419 +17420 +17421 +17422 +17423 +17424 +17425 +17426 +17427 +17428 +17429 +17430 +17431 +17432 +17433 +17434 +17435 +17436 +17437 +17438 +17439 +17440 +17441 +17442 +17443 +17444 +17445 +17446 +17447 +17448 +17449 +17450 +17451 +17452 +17453 +17454 +17455 +17456 +17457 +17458 +17459 +17460 +17461 +17462 +17463 +17464 +17465 +17466 +17467 +17468 +17469 +17470 +17471 +17472 +17473 +17474 +17475 +17476 +17477 +17478 +17479 +17480 +17481 +17482 +17483 +17484 +17485 +17486 +17487 +17488 +17489 +17490 +17491 +17492 +17493 +17494 +17495 +17496 +17497 +17498 +17499 +17500 +17501 +17502 +17503 +17504 +17505 +17506 +17507 +17508 +17509 +17510 +17511 +17512 +17513 +17514 +17515 +17516 +17517 +17518 +17519 +17520 +17521 +17522 +17523 +17524 +17525 +17526 +17527 +17528 +17529 +17530 +17531 +17532 +17533 +17534 +17535 +17536 +17537 +17538 +17539 +17540 +17541 +17542 +17543 +17544 +17545 +17546 +17547 +17548 +17549 +17550 +17551 +17552 +17553 +17554 +17555 +17556 +17557 +17558 +17559 +17560 +17561 +17562 +17563 +17564 +17565 +17566 +17567 +17568 +17569 +17570 +17571 +17572 +17573 +17574 +17575 +17576 +17577 +17578 +17579 +17580 +17581 +17582 +17583 +17584 +17585 +17586 +17587 +17588 +17589 +17590 +17591 +17592 +17593 +17594 +17595 +17596 +17597 +17598 +17599 +17600 +17601 +17602 +17603 +17604 +17605 +17606 +17607 +17608 +17609 +17610 +17611 +17612 +17613 +17614 +17615 +17616 +17617 +17618 +17619 +17620 +17621 +17622 +17623 +17624 +17625 +17626 +17627 +17628 +17629 +17630 +17631 +17632 +17633 +17634 +17635 +17636 +17637 +17638 +17639 +17640 +17641 +17642 +17643 +17644 +17645 +17646 +17647 +17648 +17649 +17650 +17651 +17652 +17653 +17654 +17655 +17656 +17657 +17658 +17659 +17660 +17661 +17662 +17663 +17664 +17665 +17666 +17667 +17668 +17669 +17670 +17671 +17672 +17673 +17674 +17675 +17676 +17677 +17678 +17679 +17680 +17681 +17682 +17683 +17684 +17685 +17686 +17687 +17688 +17689 +17690 +17691 +17692 +17693 +17694 +17695 +17696 +17697 +17698 +17699 +17700 +17701 +17702 +17703 +17704 +17705 +17706 +17707 +17708 +17709 +17710 +17711 +17712 +17713 +17714 +17715 +17716 +17717 +17718 +17719 +17720 +17721 +17722 +17723 +17724 +17725 +17726 +17727 +17728 +17729 +17730 +17731 +17732 +17733 +17734 +17735 +17736 +17737 +17738 +17739 +17740 +17741 +17742 +17743 +17744 +17745 +17746 +17747 +17748 +17749 +17750 +17751 +17752 +17753 +17754 +17755 +17756 +17757 +17758 +17759 +17760 +17761 +17762 +17763 +17764 +17765 +17766 +17767 +17768 +17769 +17770 +17771 +17772 +17773 +17774 +17775 +17776 +17777 +17778 +17779 +17780 +17781 +17782 +17783 +17784 +17785 +17786 +17787 +17788 +17789 +17790 +17791 +17792 +17793 +17794 +17795 +17796 +17797 +17798 +17799 +17800 +17801 +17802 +17803 +17804 +17805 +17806 +17807 +17808 +17809 +17810 +17811 +17812 +17813 +17814 +17815 +17816 +17817 +17818 +17819 +17820 +17821 +17822 +17823 +17824 +17825 +17826 +17827 +17828 +17829 +17830 +17831 +17832 +17833 +17834 +17835 +17836 +17837 +17838 +17839 +17840 +17841 +17842 +17843 +17844 +17845 +17846 +17847 +17848 +17849 +17850 +17851 +17852 +17853 +17854 +17855 +17856 +17857 +17858 +17859 +17860 +17861 +17862 +17863 +17864 +17865 +17866 +17867 +17868 +17869 +17870 +17871 +17872 +17873 +17874 +17875 +17876 +17877 +17878 +17879 +17880 +17881 +17882 +17883 +17884 +17885 +17886 +17887 +17888 +17889 +17890 +17891 +17892 +17893 +17894 +17895 +17896 +17897 +17898 +17899 +17900 +17901 +17902 +17903 +17904 +17905 +17906 +17907 +17908 +17909 +17910 +17911 +17912 +17913 +17914 +17915 +17916 +17917 +17918 +17919 +17920 +17921 +17922 +17923 +17924 +17925 +17926 +17927 +17928 +17929 +17930 +17931 +17932 +17933 +17934 +17935 +17936 +17937 +17938 +17939 +17940 +17941 +17942 +17943 +17944 +17945 +17946 +17947 +17948 +17949 +17950 +17951 +17952 +17953 +17954 +17955 +17956 +17957 +17958 +17959 +17960 +17961 +17962 +17963 +17964 +17965 +17966 +17967 +17968 +17969 +17970 +17971 +17972 +17973 +17974 +17975 +17976 +17977 +17978 +17979 +17980 +17981 +17982 +17983 +17984 +17985 +17986 +17987 +17988 +17989 +17990 +17991 +17992 +17993 +17994 +17995 +17996 +17997 +17998 +17999 +18000 +18001 +18002 +18003 +18004 +18005 +18006 +18007 +18008 +18009 +18010 +18011 +18012 +18013 +18014 +18015 +18016 +18017 +18018 +18019 +18020 +18021 +18022 +18023 +18024 +18025 +18026 +18027 +18028 +18029 +18030 +18031 +18032 +18033 +18034 +18035 +18036 +18037 +18038 +18039 +18040 +18041 +18042 +18043 +18044 +18045 +18046 +18047 +18048 +18049 +18050 +18051 +18052 +18053 +18054 +18055 +18056 +18057 +18058 +18059 +18060 +18061 +18062 +18063 +18064 +18065 +18066 +18067 +18068 +18069 +18070 +18071 +18072 +18073 +18074 +18075 +18076 +18077 +18078 +18079 +18080 +18081 +18082 +18083 +18084 +18085 +18086 +18087 +18088 +18089 +18090 +18091 +18092 +18093 +18094 +18095 +18096 +18097 +18098 +18099 +18100 +18101 +18102 +18103 +18104 +18105 +18106 +18107 +18108 +18109 +18110 +18111 +18112 +18113 +18114 +18115 +18116 +18117 +18118 +18119 +18120 +18121 +18122 +18123 +18124 +18125 +18126 +18127 +18128 +18129 +18130 +18131 +18132 +18133 +18134 +18135 +18136 +18137 +18138 +18139 +18140 +18141 +18142 +18143 +18144 +18145 +18146 +18147 +18148 +18149 +18150 +18151 +18152 +18153 +18154 +18155 +18156 +18157 +18158 +18159 +18160 +18161 +18162 +18163 +18164 +18165 +18166 +18167 +18168 +18169 +18170 +18171 +18172 +18173 +18174 +18175 +18176 +18177 +18178 +18179 +18180 +18181 +18182 +18183 +18184 +18185 +18186 +18187 +18188 +18189 +18190 +18191 +18192 +18193 +18194 +18195 +18196 +18197 +18198 +18199 +18200 +18201 +18202 +18203 +18204 +18205 +18206 +18207 +18208 +18209 +18210 +18211 +18212 +18213 +18214 +18215 +18216 +18217 +18218 +18219 +18220 +18221 +18222 +18223 +18224 +18225 +18226 +18227 +18228 +18229 +18230 +18231 +18232 +18233 +18234 +18235 +18236 +18237 +18238 +18239 +18240 +18241 +18242 +18243 +18244 +18245 +18246 +18247 +18248 +18249 +18250 +18251 +18252 +18253 +18254 +18255 +18256 +18257 +18258 +18259 +18260 +18261 +18262 +18263 +18264 +18265 +18266 +18267 +18268 +18269 +18270 +18271 +18272 +18273 +18274 +18275 +18276 +18277 +18278 +18279 +18280 +18281 +18282 +18283 +18284 +18285 +18286 +18287 +18288 +18289 +18290 +18291 +18292 +18293 +18294 +18295 +18296 +18297 +18298 +18299 +18300 +18301 +18302 +18303 +18304 +18305 +18306 +18307 +18308 +18309 +18310 +18311 +18312 +18313 +18314 +18315 +18316 +18317 +18318 +18319 +18320 +18321 +18322 +18323 +18324 +18325 +18326 +18327 +18328 +18329 +18330 +18331 +18332 +18333 +18334 +18335 +18336 +18337 +18338 +18339 +18340 +18341 +18342 +18343 +18344 +18345 +18346 +18347 +18348 +18349 +18350 +18351 +18352 +18353 +18354 +18355 +18356 +18357 +18358 +18359 +18360 +18361 +18362 +18363 +18364 +18365 +18366 +18367 +18368 +18369 +18370 +18371 +18372 +18373 +18374 +18375 +18376 +18377 +18378 +18379 +18380 +18381 +18382 +18383 +18384 +18385 +18386 +18387 +18388 +18389 +18390 +18391 +18392 +18393 +18394 +18395 +18396 +18397 +18398 +18399 +18400 +18401 +18402 +18403 +18404 +18405 +18406 +18407 +18408 +18409 +18410 +18411 +18412 +18413 +18414 +18415 +18416 +18417 +18418 +18419 +18420 +18421 +18422 +18423 +18424 +18425 +18426 +18427 +18428 +18429 +18430 +18431 +18432 +18433 +18434 +18435 +18436 +18437 +18438 +18439 +18440 +18441 +18442 +18443 +18444 +18445 +18446 +18447 +18448 +18449 +18450 +18451 +18452 +18453 +18454 +18455 +18456 +18457 +18458 +18459 +18460 +18461 +18462 +18463 +18464 +18465 +18466 +18467 +18468 +18469 +18470 +18471 +18472 +18473 +18474 +18475 +18476 +18477 +18478 +18479 +18480 +18481 +18482 +18483 +18484 +18485 +18486 +18487 +18488 +18489 +18490 +18491 +18492 +18493 +18494 +18495 +18496 +18497 +18498 +18499 +18500 +18501 +18502 +18503 +18504 +18505 +18506 +18507 +18508 +18509 +18510 +18511 +18512 +18513 +18514 +18515 +18516 +18517 +18518 +18519 +18520 +18521 +18522 +18523 +18524 +18525 +18526 +18527 +18528 +18529 +18530 +18531 +18532 +18533 +18534 +18535 +18536 +18537 +18538 +18539 +18540 +18541 +18542 +18543 +18544 +18545 +18546 +18547 +18548 +18549 +18550 +18551 +18552 +18553 +18554 +18555 +18556 +18557 +18558 +18559 +18560 +18561 +18562 +18563 +18564 +18565 +18566 +18567 +18568 +18569 +18570 +18571 +18572 +18573 +18574 +18575 +18576 +18577 +18578 +18579 +18580 +18581 +18582 +18583 +18584 +18585 +18586 +18587 +18588 +18589 +18590 +18591 +18592 +18593 +18594 +18595 +18596 +18597 +18598 +18599 +18600 +18601 +18602 +18603 +18604 +18605 +18606 +18607 +18608 +18609 +18610 +18611 +18612 +18613 +18614 +18615 +18616 +18617 +18618 +18619 +18620 +18621 +18622 +18623 +18624 +18625 +18626 +18627 +18628 +18629 +18630 +18631 +18632 +18633 +18634 +18635 +18636 +18637 +18638 +18639 +18640 +18641 +18642 +18643 +18644 +18645 +18646 +18647 +18648 +18649 +18650 +18651 +18652 +18653 +18654 +18655 +18656 +18657 +18658 +18659 +18660 +18661 +18662 +18663 +18664 +18665 +18666 +18667 +18668 +18669 +18670 +18671 +18672 +18673 +18674 +18675 +18676 +18677 +18678 +18679 +18680 +18681 +18682 +18683 +18684 +18685 +18686 +18687 +18688 +18689 +18690 +18691 +18692 +18693 +18694 +18695 +18696 +18697 +18698 +18699 +18700 +18701 +18702 +18703 +18704 +18705 +18706 +18707 +18708 +18709 +18710 +18711 +18712 +18713 +18714 +18715 +18716 +18717 +18718 +18719 +18720 +18721 +18722 +18723 +18724 +18725 +18726 +18727 +18728 +18729 +18730 +18731 +18732 +18733 +18734 +18735 +18736 +18737 +18738 +18739 +18740 +18741 +18742 +18743 +18744 +18745 +18746 +18747 +18748 +18749 +18750 +18751 +18752 +18753 +18754 +18755 +18756 +18757 +18758 +18759 +18760 +18761 +18762 +18763 +18764 +18765 +18766 +18767 +18768 +18769 +18770 +18771 +18772 +18773 +18774 +18775 +18776 +18777 +18778 +18779 +18780 +18781 +18782 +18783 +18784 +18785 +18786 +18787 +18788 +18789 +18790 +18791 +18792 +18793 +18794 +18795 +18796 +18797 +18798 +18799 +18800 +18801 +18802 +18803 +18804 +18805 +18806 +18807 +18808 +18809 +18810 +18811 +18812 +18813 +18814 +18815 +18816 +18817 +18818 +18819 +18820 +18821 +18822 +18823 +18824 +18825 +18826 +18827 +18828 +18829 +18830 +18831 +18832 +18833 +18834 +18835 +18836 +18837 +18838 +18839 +18840 +18841 +18842 +18843 +18844 +18845 +18846 +18847 +18848 +18849 +18850 +18851 +18852 +18853 +18854 +18855 +18856 +18857 +18858 +18859 +18860 +18861 +18862 +18863 +18864 +18865 +18866 +18867 +18868 +18869 +18870 +18871 +18872 +18873 +18874 +18875 +18876 +18877 +18878 +18879 +18880 +18881 +18882 +18883 +18884 +18885 +18886 +18887 +18888 +18889 +18890 +18891 +18892 +18893 +18894 +18895 +18896 +18897 +18898 +18899 +18900 +18901 +18902 +18903 +18904 +18905 +18906 +18907 +18908 +18909 +18910 +18911 +18912 +18913 +18914 +18915 +18916 +18917 +18918 +18919 +18920 +18921 +18922 +18923 +18924 +18925 +18926 +18927 +18928 +18929 +18930 +18931 +18932 +18933 +18934 +18935 +18936 +18937 +18938 +18939 +18940 +18941 +18942 +18943 +18944 +18945 +18946 +18947 +18948 +18949 +18950 +18951 +18952 +18953 +18954 +18955 +18956 +18957 +18958 +18959 +18960 +18961 +18962 +18963 +18964 +18965 +18966 +18967 +18968 +18969 +18970 +18971 +18972 +18973 +18974 +18975 +18976 +18977 +18978 +18979 +18980 +18981 +18982 +18983 +18984 +18985 +18986 +18987 +18988 +18989 +18990 +18991 +18992 +18993 +18994 +18995 +18996 +18997 +18998 +18999 +19000 +19001 +19002 +19003 +19004 +19005 +19006 +19007 +19008 +19009 +19010 +19011 +19012 +19013 +19014 +19015 +19016 +19017 +19018 +19019 +19020 +19021 +19022 +19023 +19024 +19025 +19026 +19027 +19028 +19029 +19030 +19031 +19032 +19033 +19034 +19035 +19036 +19037 +19038 +19039 +19040 +19041 +19042 +19043 +19044 +19045 +19046 +19047 +19048 +19049 +19050 +19051 +19052 +19053 +19054 +19055 +19056 +19057 +19058 +19059 +19060 +19061 +19062 +19063 +19064 +19065 +19066 +19067 +19068 +19069 +19070 +19071 +19072 +19073 +19074 +19075 +19076 +19077 +19078 +19079 +19080 +19081 +19082 +19083 +19084 +19085 +19086 +19087 +19088 +19089 +19090 +19091 +19092 +19093 +19094 +19095 +19096 +19097 +19098 +19099 +19100 +19101 +19102 +19103 +19104 +19105 +19106 +19107 +19108 +19109 +19110 +19111 +19112 +19113 +19114 +19115 +19116 +19117 +19118 +19119 +19120 +19121 +19122 +19123 +19124 +19125 +19126 +19127 +19128 +19129 +19130 +19131 +19132 +19133 +19134 +19135 +19136 +19137 +19138 +19139 +19140 +19141 +19142 +19143 +19144 +19145 +19146 +19147 +19148 +19149 +19150 +19151 +19152 +19153 +19154 +19155 +19156 +19157 +19158 +19159 +19160 +19161 +19162 +19163 +19164 +19165 +19166 +19167 +19168 +19169 +19170 +19171 +19172 +19173 +19174 +19175 +19176 +19177 +19178 +19179 +19180 +19181 +19182 +19183 +19184 +19185 +19186 +19187 +19188 +19189 +19190 +19191 +19192 +19193 +19194 +19195 +19196 +19197 +19198 +19199 +19200 +19201 +19202 +19203 +19204 +19205 +19206 +19207 +19208 +19209 +19210 +19211 +19212 +19213 +19214 +19215 +19216 +19217 +19218 +19219 +19220 +19221 +19222 +19223 +19224 +19225 +19226 +19227 +19228 +19229 +19230 +19231 +19232 +19233 +19234 +19235 +19236 +19237 +19238 +19239 +19240 +19241 +19242 +19243 +19244 +19245 +19246 +19247 +19248 +19249 +19250 +19251 +19252 +19253 +19254 +19255 +19256 +19257 +19258 +19259 +19260 +19261 +19262 +19263 +19264 +19265 +19266 +19267 +19268 +19269 +19270 +19271 +19272 +19273 +19274 +19275 +19276 +19277 +19278 +19279 +19280 +19281 +19282 +19283 +19284 +19285 +19286 +19287 +19288 +19289 +19290 +19291 +19292 +19293 +19294 +19295 +19296 +19297 +19298 +19299 +19300 +19301 +19302 +19303 +19304 +19305 +19306 +19307 +19308 +19309 +19310 +19311 +19312 +19313 +19314 +19315 +19316 +19317 +19318 +19319 +19320 +19321 +19322 +19323 +19324 +19325 +19326 +19327 +19328 +19329 +19330 +19331 +19332 +19333 +19334 +19335 +19336 +19337 +19338 +19339 +19340 +19341 +19342 +19343 +19344 +19345 +19346 +19347 +19348 +19349 +19350 +19351 +19352 +19353 +19354 +19355 +19356 +19357 +19358 +19359 +19360 +19361 +19362 +19363 +19364 +19365 +19366 +19367 +19368 +19369 +19370 +19371 +19372 +19373 +19374 +19375 +19376 +19377 +19378 +19379 +19380 +19381 +19382 +19383 +19384 +19385 +19386 +19387 +19388 +19389 +19390 +19391 +19392 +19393 +19394 +19395 +19396 +19397 +19398 +19399 +19400 +19401 +19402 +19403 +19404 +19405 +19406 +19407 +19408 +19409 +19410 +19411 +19412 +19413 +19414 +19415 +19416 +19417 +19418 +19419 +19420 +19421 +19422 +19423 +19424 +19425 +19426 +19427 +19428 +19429 +19430 +19431 +19432 +19433 +19434 +19435 +19436 +19437 +19438 +19439 +19440 +19441 +19442 +19443 +19444 +19445 +19446 +19447 +19448 +19449 +19450 +19451 +19452 +19453 +19454 +19455 +19456 +19457 +19458 +19459 +19460 +19461 +19462 +19463 +19464 +19465 +19466 +19467 +19468 +19469 +19470 +19471 +19472 +19473 +19474 +19475 +19476 +19477 +19478 +19479 +19480 +19481 +19482 +19483 +19484 +19485 +19486 +19487 +19488 +19489 +19490 +19491 +19492 +19493 +19494 +19495 +19496 +19497 +19498 +19499 +19500 +19501 +19502 +19503 +19504 +19505 +19506 +19507 +19508 +19509 +19510 +19511 +19512 +19513 +19514 +19515 +19516 +19517 +19518 +19519 +19520 +19521 +19522 +19523 +19524 +19525 +19526 +19527 +19528 +19529 +19530 +19531 +19532 +19533 +19534 +19535 +19536 +19537 +19538 +19539 +19540 +19541 +19542 +19543 +19544 +19545 +19546 +19547 +19548 +19549 +19550 +19551 +19552 +19553 +19554 +19555 +19556 +19557 +19558 +19559 +19560 +19561 +19562 +19563 +19564 +19565 +19566 +19567 +19568 +19569 +19570 +19571 +19572 +19573 +19574 +19575 +19576 +19577 +19578 +19579 +19580 +19581 +19582 +19583 +19584 +19585 +19586 +19587 +19588 +19589 +19590 +19591 +19592 +19593 +19594 +19595 +19596 +19597 +19598 +19599 +19600 +19601 +19602 +19603 +19604 +19605 +19606 +19607 +19608 +19609 +19610 +19611 +19612 +19613 +19614 +19615 +19616 +19617 +19618 +19619 +19620 +19621 +19622 +19623 +19624 +19625 +19626 +19627 +19628 +19629 +19630 +19631 +19632 +19633 +19634 +19635 +19636 +19637 +19638 +19639 +19640 +19641 +19642 +19643 +19644 +19645 +19646 +19647 +19648 +19649 +19650 +19651 +19652 +19653 +19654 +19655 +19656 +19657 +19658 +19659 +19660 +19661 +19662 +19663 +19664 +19665 +19666 +19667 +19668 +19669 +19670 +19671 +19672 +19673 +19674 +19675 +19676 +19677 +19678 +19679 +19680 +19681 +19682 +19683 +19684 +19685 +19686 +19687 +19688 +19689 +19690 +19691 +19692 +19693 +19694 +19695 +19696 +19697 +19698 +19699 +19700 +19701 +19702 +19703 +19704 +19705 +19706 +19707 +19708 +19709 +19710 +19711 +19712 +19713 +19714 +19715 +19716 +19717 +19718 +19719 +19720 +19721 +19722 +19723 +19724 +19725 +19726 +19727 +19728 +19729 +19730 +19731 +19732 +19733 +19734 +19735 +19736 +19737 +19738 +19739 +19740 +19741 +19742 +19743 +19744 +19745 +19746 +19747 +19748 +19749 +19750 +19751 +19752 +19753 +19754 +19755 +19756 +19757 +19758 +19759 +19760 +19761 +19762 +19763 +19764 +19765 +19766 +19767 +19768 +19769 +19770 +19771 +19772 +19773 +19774 +19775 +19776 +19777 +19778 +19779 +19780 +19781 +19782 +19783 +19784 +19785 +19786 +19787 +19788 +19789 +19790 +19791 +19792 +19793 +19794 +19795 +19796 +19797 +19798 +19799 +19800 +19801 +19802 +19803 +19804 +19805 +19806 +19807 +19808 +19809 +19810 +19811 +19812 +19813 +19814 +19815 +19816 +19817 +19818 +19819 +19820 +19821 +19822 +19823 +19824 +19825 +19826 +19827 +19828 +19829 +19830 +19831 +19832 +19833 +19834 +19835 +19836 +19837 +19838 +19839 +19840 +19841 +19842 +19843 +19844 +19845 +19846 +19847 +19848 +19849 +19850 +19851 +19852 +19853 +19854 +19855 +19856 +19857 +19858 +19859 +19860 +19861 +19862 +19863 +19864 +19865 +19866 +19867 +19868 +19869 +19870 +19871 +19872 +19873 +19874 +19875 +19876 +19877 +19878 +19879 +19880 +19881 +19882 +19883 +19884 +19885 +19886 +19887 +19888 +19889 +19890 +19891 +19892 +19893 +19894 +19895 +19896 +19897 +19898 +19899 +19900 +19901 +19902 +19903 +19904 +19905 +19906 +19907 +19908 +19909 +19910 +19911 +19912 +19913 +19914 +19915 +19916 +19917 +19918 +19919 +19920 +19921 +19922 +19923 +19924 +19925 +19926 +19927 +19928 +19929 +19930 +19931 +19932 +19933 +19934 +19935 +19936 +19937 +19938 +19939 +19940 +19941 +19942 +19943 +19944 +19945 +19946 +19947 +19948 +19949 +19950 +19951 +19952 +19953 +19954 +19955 +19956 +19957 +19958 +19959 +19960 +19961 +19962 +19963 +19964 +19965 +19966 +19967 +19968 +19969 +19970 +19971 +19972 +19973 +19974 +19975 +19976 +19977 +19978 +19979 +19980 +19981 +19982 +19983 +19984 +19985 +19986 +19987 +19988 +19989 +19990 +19991 +19992 +19993 +19994 +19995 +19996 +19997 +19998 +19999 +20000 diff --git a/tests/fixtures/sort/ext_sort.txt b/tests/fixtures/sort/ext_sort.txt new file mode 100644 index 000000000..a409d67e1 --- /dev/null +++ b/tests/fixtures/sort/ext_sort.txt @@ -0,0 +1,20000 @@ +9155 +10575 +10442 +15874 +17013 +12130 +15558 +18263 +6574 +8957 +9851 +16606 +12331 +9865 +13795 +270 +6590 +11141 +4620 +5945 +10904 +8652 +8442 +8907 +1935 +13100 +3961 +7538 +4159 +11986 +4394 +9321 +15560 +5264 +5121 +11532 +6980 +2807 +19760 +8032 +5158 +13698 +4458 +9106 +3773 +1625 +9914 +8287 +5155 +14326 +18137 +19522 +9270 +6153 +1920 +12517 +13259 +17618 +9930 +3630 +15924 +4540 +263 +14212 +15620 +11328 +8704 +17848 +1614 +12587 +17970 +4542 +11976 +12885 +8743 +16323 +14582 +12101 +12472 +12620 +10713 +5148 +7522 +2417 +8602 +7860 +19596 +2892 +13359 +7731 +3707 +4628 +4710 +5642 +1610 +4784 +10128 +16341 +14168 +5829 +17901 +9447 +1041 +15193 +15260 +11224 +11723 +13368 +14011 +12200 +2001 +18479 +3965 +16642 +16680 +2384 +7557 +5539 +4305 +14588 +17386 +1359 +17721 +1142 +7287 +9946 +13139 +15022 +17237 +14454 +14358 +11297 +12485 +857 +19282 +117 +19179 +16040 +6978 +10743 +19161 +12308 +9509 +13233 +4666 +5850 +17040 +2473 +17323 +6728 +11500 +4401 +13887 +19944 +17601 +19731 +10715 +8327 +11196 +7944 +9068 +13253 +13913 +13419 +17649 +16894 +3333 +5747 +3000 +12724 +17772 +17341 +11854 +6042 +10362 +3438 +13828 +471 +17747 +13707 +17902 +8184 +3385 +19387 +46 +1848 +15848 +10768 +17481 +436 +17581 +4629 +13210 +5085 +19485 +44 +12549 +16407 +9646 +9884 +15802 +2461 +15134 +2936 +8156 +17411 +1228 +18200 +14616 +17443 +5512 +6017 +10137 +11424 +11940 +6655 +17669 +6147 +16570 +17281 +18915 +19689 +18013 +17895 +6925 +19766 +18634 +7195 +7001 +12335 +3908 +18754 +4833 +12486 +12892 +18879 +3691 +1693 +158 +8682 +1029 +3429 +644 +1664 +13972 +5352 +15422 +1928 +14561 +3322 +3570 +6298 +2251 +12216 +19823 +12658 +2081 +4177 +13843 +18454 +13755 +9340 +6373 +3290 +2893 +16274 +16610 +12782 +7269 +5681 +19372 +7843 +14844 +16236 +16004 +1136 +5759 +19347 +7684 +7435 +5892 +7305 +2504 +7064 +13264 +8781 +1327 +19943 +18122 +1803 +9999 +13695 +9742 +8274 +744 +2252 +10524 +1434 +4601 +6071 +10489 +19626 +10745 +18312 +872 +19166 +3417 +9272 +1433 +9431 +6486 +3532 +18452 +17830 +9835 +2495 +10475 +16725 +16019 +6594 +11355 +13055 +14782 +11924 +18838 +3563 +428 +12993 +14223 +6658 +2783 +1726 +6929 +13053 +19175 +8564 +4867 +19604 +17416 +9347 +2275 +16381 +9817 +11176 +14576 +6906 +9805 +14149 +2241 +11030 +453 +835 +15452 +2879 +4018 +17396 +16133 +1301 +3450 +10182 +2389 +19201 +4443 +14368 +2537 +7452 +1583 +13955 +3875 +7479 +17561 +3247 +16310 +5253 +8899 +6523 +10260 +13065 +11077 +15109 +7249 +12046 +5313 +8914 +19949 +2196 +3654 +7145 +12166 +18340 +17929 +12466 +7866 +3831 +15095 +2506 +17691 +12992 +6591 +9661 +19538 +2161 +3991 +6766 +18180 +182 +15952 +9709 +19601 +13427 +19071 +12698 +12157 +7963 +3485 +19327 +6029 +12948 +2261 +2844 +4864 +12148 +9187 +6695 +8171 +19771 +3782 +14122 +11658 +330 +10750 +6932 +4436 +15622 +5710 +18750 +5765 +10545 +10897 +16609 +9183 +802 +14708 +3423 +11557 +19098 +3641 +14490 +3249 +1355 +16886 +18500 +15309 +8010 +18543 +2342 +3813 +2135 +9055 +15148 +12720 +3253 +737 +11788 +253 +13352 +1521 +13949 +1957 +10884 +2273 +14730 +13979 +2401 +595 +12697 +9932 +11372 +11915 +760 +10930 +10659 +6472 +19706 +6488 +9730 +17705 +16085 +4134 +2070 +4852 +5122 +16359 +7044 +8510 +10868 +10172 +510 +11550 +260 +11181 +3018 +3668 +904 +10271 +17104 +12764 +3364 +1878 +2803 +14040 +1149 +15626 +12809 +11008 +2903 +8352 +12761 +13470 +11258 +1400 +8381 +12422 +8402 +3919 +7164 +17263 +18167 +4549 +7654 +6034 +8438 +19451 +12122 +5167 +19334 +9771 +1111 +2932 +18324 +4897 +12687 +1720 +2767 +7616 +6431 +16918 +579 +16504 +4559 +14384 +6337 +4008 +7937 +1086 +5314 +12489 +5211 +17500 +19641 +14815 +7967 +8140 +5239 +2571 +18601 +16197 +5142 +12714 +14895 +3432 +3995 +9206 +7668 +8703 +1661 +4315 +5941 +6849 +2505 +19547 +11736 +5319 +986 +7846 +16050 +9227 +13121 +1012 +18236 +4888 +3885 +11135 +17395 +4303 +3836 +18544 +9807 +15248 +10626 +13846 +17286 +5581 +14007 +2062 +4619 +14864 +13869 +2442 +17728 +11590 +16382 +19117 +19446 +6843 +8694 +14439 +3453 +2700 +9821 +8089 +9645 +14679 +4356 +11980 +5408 +2668 +8053 +1647 +19959 +17083 +14916 +8841 +2319 +11984 +12867 +4292 +4633 +1492 +10716 +12880 +8243 +3929 +2225 +2943 +17578 +12138 +5648 +9614 +15487 +18868 +10779 +12716 +403 +2908 +7040 +4772 +19912 +14823 +5045 +14250 +19733 +13073 +6947 +10387 +8021 +5201 +4488 +18161 +4100 +1422 +16865 +7646 +4370 +17271 +19121 +9808 +2613 +15130 +18893 +11654 +5903 +15058 +12954 +6480 +5764 +15830 +17813 +10224 +6324 +4412 +5607 +9497 +7849 +1291 +9401 +19126 +15067 +10197 +18480 +6258 +8590 +17216 +7437 +16511 +18807 +4267 +18809 +14488 +2345 +4395 +11054 +7624 +3708 +896 +18870 +19517 +2950 +7950 +19529 +155 +19786 +138 +7168 +2130 +10699 +19821 +19156 +11071 +8912 +9515 +17234 +10388 +195 +19909 +16687 +18628 +5870 +10436 +16939 +6562 +8748 +4929 +6282 +9147 +12262 +18067 +17029 +5593 +11043 +15042 +19516 +7618 +13353 +3793 +108 +8255 +8446 +9675 +6111 +5162 +17668 +11558 +17151 +4502 +4353 +19681 +9842 +9229 +13095 +6533 +5706 +14713 +130 +4435 +918 +5195 +4348 +13863 +1247 +13221 +11826 +14356 +7315 +19382 +14025 +10586 +13495 +2418 +13930 +17352 +10751 +5332 +19001 +148 +12921 +8219 +11851 +12210 +5136 +16621 +309 +11602 +3224 +2787 +831 +2423 +14749 +12053 +9389 +15413 +17125 +7143 +19332 +10719 +10433 +4229 +6003 +3725 +17978 +3086 +17007 +6993 +16758 +6015 +16832 +2292 +16344 +13161 +7474 +12363 +10615 +19599 +6856 +10583 +19326 +15872 +3318 +17397 +14798 +8994 +13566 +949 +8506 +17722 +14915 +16273 +14643 +12334 +6859 +3148 +16594 +1809 +16350 +10681 +5696 +19560 +11625 +12856 +14535 +12292 +10444 +14707 +19226 +15085 +17377 +10766 +1833 +14675 +6297 +11865 +9647 +1269 +11299 +18222 +12310 +5715 +3293 +5580 +16180 +3198 +5655 +14952 +4632 +18179 +14464 +18546 +19062 +14401 +10772 +11719 +12926 +6267 +9163 +7559 +6358 +6903 +12503 +842 +90 +11725 +5854 +2083 +9137 +3292 +2781 +10855 +13137 +1993 +6499 +1395 +4910 +9378 +445 +13443 +7039 +15416 +7434 +13093 +14981 +10377 +13571 +5652 +19404 +14335 +8636 +13404 +18715 +12722 +19189 +9989 +12956 +7993 +7848 +5771 +16660 +9063 +8452 +4845 +14997 +6883 +11982 +1931 +9618 +11593 +12712 +16973 +17452 +9993 +3728 +18598 +15326 +15360 +7612 +18837 +5048 +9659 +14572 +732 +10948 +8383 +15938 +9822 +15502 +231 +9438 +13629 +4987 +16527 +7250 +17364 +14600 +13817 +7983 +7154 +4103 +14313 +6931 +13983 +14009 +11778 +5478 +11543 +13446 +13037 +17142 +8307 +17676 +16495 +2274 +15324 +8828 +7999 +861 +6566 +17141 +9039 +694 +2882 +10144 +17051 +15743 +11263 +17156 +9122 +9916 +14398 +7063 +6237 +5638 +15763 +1781 +2049 +10105 +1246 +7266 +7500 +18846 +5535 +15135 +18616 +10987 +4354 +18659 +18721 +4950 +14666 +12702 +663 +11894 +11411 +19965 +3137 +5388 +7768 +5069 +7826 +19221 +1176 +18638 +3314 +36 +17709 +15578 +14710 +4791 +6413 +5461 +7776 +13204 +2302 +6382 +9622 +8769 +8967 +4208 +15083 +2464 +9639 +19391 +14833 +9760 +1577 +681 +236 +1985 +12792 +10147 +11477 +14536 +8012 +18444 +13183 +7810 +7717 +8593 +1654 +18958 +19769 +5659 +13596 +8469 +17760 +16629 +17402 +2140 +16696 +14869 +8103 +18250 +6429 +469 +7934 +16281 +16623 +7074 +4984 +9624 +16339 +7825 +19700 +6866 +15914 +19964 +19336 +13712 +11268 +3216 +10218 +12589 +4453 +14596 +16420 +1512 +18977 +11886 +11637 +12934 +496 +18503 +7326 +1082 +19257 +12682 +5177 +12065 +1500 +8097 +17636 +1973 +12448 +2209 +6165 +17875 +13036 +11911 +17546 +18476 +14284 +12114 +15674 +7720 +3484 +10471 +6089 +3441 +12887 +19772 +5168 +16222 +18075 +6793 +1074 +3428 +7593 +8465 +14048 +3463 +15554 +15329 +4352 +6135 +11756 +4268 +1482 +3523 +14390 +3681 +3090 +15748 +2656 +9255 +6536 +10528 +8574 +1926 +18022 +8625 +19593 +12288 +10632 +18212 +9782 +1875 +2082 +13643 +16171 +8385 +15465 +5751 +9420 +1004 +7771 +12499 +12525 +6097 +1513 +15921 +15484 +12663 +17072 +4693 +15491 +9584 +5864 +4977 +7841 +15412 +7923 +3536 +2540 +6911 +10031 +2206 +18931 +1856 +205 +8152 +12603 +4552 +19026 +9223 +10389 +11271 +16914 +16266 +9829 +8124 +3549 +11605 +16055 +5537 +19801 +11601 +3499 +7699 +735 +9037 +18773 +11078 +18295 +11935 +16267 +5072 +1822 +16710 +5097 +2812 +13191 +12077 +9819 +5524 +17767 +10587 +10513 +1994 +19862 +18907 +7968 +15272 +7789 +2409 +54 +19531 +8092 +3347 +673 +5449 +18599 +11457 +9023 +18216 +151 +3325 +7491 +12272 +1089 +17925 +12543 +4855 +910 +8895 +2662 +17435 +11455 +6263 +6049 +10308 +14696 +1120 +1857 +14658 +14709 +1827 +9133 +3732 +2308 +5021 +19895 +5130 +2321 +8478 +13751 +14668 +12758 +6368 +925 +6567 +13960 +4392 +17533 +11140 +8696 +4902 +19715 +19592 +19009 +6990 +7368 +2884 +1761 +9096 +5776 +19203 +12034 +13669 +14639 +8126 +11779 +16533 +1682 +208 +7340 +6351 +6564 +16168 +11449 +15859 +14482 +19057 +13581 +5965 +18130 +1427 +8670 +16155 +17607 +3754 +5050 +8369 +10157 +7623 +4580 +10289 +14147 +4613 +6316 +8441 +13630 +19850 +15986 +1160 +17770 +19266 +83 +6643 +4694 +10353 +4909 +4727 +15659 +5584 +2641 +3698 +3165 +13039 +13312 +4756 +3598 +8958 +1898 +16121 +18728 +6332 +6539 +12401 +18019 +13123 +15388 +1972 +3111 +13250 +13944 +15408 +9078 +452 +14346 +7560 +2306 +3883 +6312 +3541 +5518 +3377 +19440 +11416 +6459 +19730 +1018 +17717 +19070 +2979 +7628 +3294 +14038 +17208 +16535 +14850 +7 +16427 +18239 +5066 +9609 +9194 +11734 +14200 +4917 +13090 +2490 +3735 +3829 +13487 +7105 +4108 +450 +10527 +3391 +13080 +3884 +9246 +14599 +12759 +9940 +5528 +1425 +3209 +4 +11016 +14852 +17253 +15713 +4653 +5849 +4787 +16819 +12491 +19370 +12594 +3657 +10088 +3741 +9981 +3779 +18367 +4760 +8416 +16092 +18242 +12718 +18491 +13873 +10163 +11696 +4073 +1570 +18443 +8328 +3675 +4282 +7763 +6861 +17516 +18429 +7823 +11156 +9205 +18297 +13738 +7811 +3578 +8944 +2115 +16468 +3907 +15293 +10044 +6555 +2826 +9838 +6750 +15892 +4279 +6867 +7938 +9788 +1115 +13625 +2146 +11581 +1096 +18237 +10236 +9201 +17460 +13171 +9963 +7202 +3966 +4294 +5573 +10232 +13791 +19875 +9298 +11298 +18253 +10352 +14014 +10952 +16802 +17984 +3027 +11733 +3150 +3514 +5796 +16181 +16322 +15572 +9196 +9392 +6842 +13963 +7868 +4416 +5411 +4598 +12392 +8187 +8809 +1722 +14154 +7136 +10114 +806 +4506 +2210 +1234 +6487 +12601 +6345 +1222 +11032 +17837 +1436 +18510 +13192 +13409 +2916 +15236 +2171 +19849 +18365 +13536 +7186 +18356 +13562 +13042 +15819 +11301 +5777 +15869 +9462 +264 +6749 +6724 +19018 +3737 +4210 +13420 +16942 +8870 +14209 +19898 +882 +4021 +18094 +438 +15220 +12025 +19916 +8802 +16090 +7216 +13396 +19431 +15182 +11773 +2064 +2735 +4187 +14432 +6551 +14174 +4687 +14750 +19246 +5868 +8400 +16515 +3028 +1855 +15898 +12605 +2643 +9103 +7130 +10895 +9089 +2771 +19857 +10033 +19263 +9119 +14706 +14207 +18470 +11743 +18163 +859 +13245 +14698 +11764 +9748 +15584 +19089 +13388 +18928 +3289 +19660 +16732 +16194 +70 +19077 +2745 +11216 +5550 +15300 +7565 +1766 +9684 +18285 +3060 +14989 +5227 +2072 +8080 +14517 +12391 +13686 +7525 +14548 +11403 +8754 +16046 +10953 +7112 +5592 +6882 +9080 +4589 +13437 +16114 +5753 +6118 +16947 +4566 +9633 +7033 +13356 +9164 +13671 +9689 +11207 +18007 +2402 +15376 +16038 +6571 +4875 +3055 +16146 +18484 +19778 +12574 +966 +16007 +6908 +10248 +9118 +1820 +1830 +14591 +15893 +13881 +6987 +5794 +13255 +12003 +4537 +18485 +3567 +7203 +19079 +6338 +8671 +15590 +19654 +2552 +10446 +14189 +3207 +9582 +12209 +18993 +6941 +14448 +18639 +18694 +5814 +10292 +8377 +10796 +10680 +15 +870 +1777 +15404 +2044 +4232 +3917 +5371 +7725 +8974 +8227 +6073 +12404 +9084 +19466 +14267 +8223 +7381 +7682 +632 +12671 +1325 +11845 +11113 +8412 +19417 +6357 +17205 +380 +4919 +7842 +14100 +16317 +15235 +14613 +18211 +7461 +19872 +9913 +11784 +13436 +1859 +6597 +13750 +10136 +14029 +10842 +1795 +682 +11266 +16047 +5758 +1194 +16086 +7301 +6798 +10818 +3495 +17518 +298 +16767 +18903 +15113 +10358 +5676 +18567 +4812 +15165 +9477 +2283 +11622 +13722 +3073 +16187 +2544 +2482 +7534 +18691 +14260 +274 +14682 +3231 +16237 +6606 +16494 +12612 +17826 +18266 +17652 +11954 +19394 +16356 +15781 +6498 +7613 +18308 +9852 +12495 +11569 +6822 +15384 +18414 +16240 +17425 +3619 +2998 +4060 +281 +12126 +13552 +17956 +8648 +5199 +11470 +19559 +2184 +4127 +1782 +11302 +8272 +7227 +4378 +6617 +17001 +5565 +12212 +15110 +12717 +9177 +203 +15928 +14386 +18671 +3664 +5577 +5825 +18883 +11563 +7790 +16821 +19075 +11407 +4067 +481 +15007 +10607 +1437 +16173 +10793 +647 +3305 +2691 +15054 +1132 +8766 +14500 +14137 +11852 +14871 +765 +16596 +1727 +2747 +9784 +9337 +16309 +16810 +16508 +12105 +1127 +13997 +17502 +17305 +18799 +12701 +5281 +13678 +4044 +18969 +14380 +19526 +15873 +8501 +299 +1764 +18487 +19620 +4094 +9237 +12970 +4240 +14861 +11285 +1470 +3766 +9496 +9656 +5356 +7509 +9637 +15046 +19038 +1072 +13336 +784 +18523 +6996 +15991 +581 +14929 +16799 +4522 +8701 +9889 +5016 +7199 +19270 +15705 +2611 +10397 +19448 +17801 +11447 +16190 +16130 +3153 +12318 +5677 +14727 +13117 +9800 +15894 +15709 +9081 +5403 +15579 +6359 +5576 +552 +19272 +11427 +4343 +11192 +12732 +4424 +17024 +7715 +12633 +2562 +12400 +4330 +16677 +1150 +6402 +18957 +9338 +4726 +10064 +8644 +17968 +79 +14922 +12178 +7837 +11881 +18551 +2096 +7547 +6879 +16607 +17532 +17944 +10284 +10009 +9868 +2088 +7165 +17038 +14403 +11088 +9668 +13714 +9125 +13804 +13547 +200 +11058 +19642 +3016 +5375 +14376 +5202 +3268 +8476 +14820 +12021 +17213 +12957 +8549 +15190 +5397 +3339 +8084 +15262 +10297 +14127 +18950 +14402 +11073 +5657 +10318 +13866 +9180 +18302 +3183 +2397 +11747 +9357 +2022 +10447 +16582 +19247 +14261 +19923 +14503 +16422 +8778 +18439 +19673 +1679 +9156 +14756 +8836 +792 +14801 +14510 +4556 +12417 +6110 +15972 +1030 +2328 +525 +16238 +11164 +11570 +17610 +4118 +11897 +9444 +14758 +2978 +16124 +7333 +6201 +14646 +819 +16867 +758 +4095 +19080 +15720 +17338 +18410 +12800 +16679 +10495 +6063 +10001 +18512 +14521 +6137 +18160 +5900 +3606 +15776 +9127 +13491 +17690 +9173 +5013 +3745 +13371 +9315 +4604 +14737 +4625 +7850 +3071 +2681 +13915 +3716 +7617 +12597 +3512 +3565 +19435 +7564 +17592 +9035 +12394 +2941 +6473 +13995 +18469 +11981 +17680 +1932 +19172 +1959 +16466 +2043 +11677 +7471 +7635 +12870 +10337 +15199 +2517 +17905 +10239 +5615 +2269 +13794 +2664 +8883 +10087 +9073 +10617 +5548 +10940 +19822 +11659 +13147 +12171 +3164 +17034 +9484 +17682 +7142 +3559 +12980 +11388 +7572 +17292 +3311 +3561 +7590 +14998 +7454 +2874 +6168 +937 +14366 +8090 +8710 +12599 +8443 +8970 +18892 +329 +3254 +7781 +16904 +4896 +12343 +11478 +10553 +4132 +17450 +12864 +3510 +1402 +12055 +8183 +165 +9948 +3214 +6878 +5933 +228 +3230 +1281 +15215 +10404 +3367 +4014 +6632 +9043 +4818 +6661 +19398 +18517 +7085 +17011 +13682 +15769 +13317 +3843 +11294 +7302 +8179 +12539 +16626 +3607 +15203 +2254 +5182 +16659 +12406 +12573 +5917 +2373 +11062 +17231 +3916 +3105 +9085 +18613 +11408 +17161 +17366 +11591 +4238 +4660 +2848 +5891 +12804 +10288 +8174 +14239 +12514 +4654 +4250 +2852 +6314 +2368 +4331 +10102 +9215 +4154 +9775 +15534 +16486 +6072 +17897 +6804 +9025 +6688 +8484 +7362 +7728 +18465 +17285 +7308 +13520 +11839 +8577 +3723 +4068 +5628 +18978 +9294 +13440 +3112 +13821 +12027 +13670 +1564 +15275 +10179 +9740 +7109 +8716 +11499 +11306 +4092 +15096 +18461 +19703 +15349 +1079 +15249 +4831 +8609 +5402 +6252 +12462 +17598 +8256 +4873 +901 +1505 +10888 +13587 +16191 +1683 +13035 +13771 +4377 +17164 +4385 +9517 +18346 +10202 +807 +3176 +3393 +10311 +11856 +4420 +17807 +16514 +17296 +10185 +7689 +2121 +603 +19848 +14957 +6235 +3765 +6958 +16961 +16835 +11823 +15218 +3673 +14694 +9799 +10988 +11521 +16713 +2904 +6079 +3201 +18911 +4517 +5304 +6689 +3034 +17456 +14288 +3025 +9030 +10834 +72 +3640 +5419 +17997 +12664 +17332 +3392 +18034 +8415 +10580 +2582 +7067 +12922 +8116 +6069 +9456 +3945 +12121 +3205 +1471 +18203 +4563 +11758 +15828 +12696 +3029 +5878 +7584 +18021 +9781 +13367 +12159 +8708 +17930 +18482 +3810 +19468 +9992 +9466 +16650 +8319 +8075 +13026 +12750 +4248 +3756 +6904 +5987 +10300 +19513 +7835 +14553 +7997 +13118 +13895 +14201 +828 +19873 +14254 +14321 +6442 +7713 +18594 +9283 +3803 +18739 +5800 +8088 +11797 +12172 +17438 +8013 +6444 +2526 +19303 +7752 +5243 +10222 +14888 +1987 +12728 +2281 +14094 +13189 +18421 +232 +17792 +12315 +5112 +10252 +14472 +11282 +13205 +8666 +16375 +13298 +53 +16122 +15019 +18453 +11551 +17430 +10461 +11789 +5997 +2790 +18547 +12547 +6067 +19149 +2820 +18032 +10802 +10133 +18017 +7667 +13452 +16972 +4192 +2519 +15381 +12924 +10438 +19016 +4955 +3903 +10237 +15152 +2860 +19938 +5412 +14294 +2867 +15553 +8672 +13106 +11603 +408 +546 +6437 +7921 +7487 +11587 +14507 +4111 +15628 +13295 +8340 +18279 +7259 +14649 +19506 +14785 +10293 +14742 +19136 +6265 +1087 +12052 +15843 +18727 +5484 +845 +15185 +13976 +15646 +17664 +1068 +4335 +8722 +12020 +488 +2706 +282 +3534 +1294 +3043 +11397 +2713 +2975 +944 +10208 +100 +19744 +4924 +2107 +17519 +7385 +2507 +17006 +2763 +7137 +17314 +15094 +12296 +12237 +10544 +2758 +76 +12678 +10763 +3780 +17067 +8968 +975 +3659 +13505 +15863 +17766 +3363 +5423 +7463 +2178 +4126 +14612 +7382 +19859 +676 +12313 +9295 +8640 +11309 +10754 +13801 +12608 +13889 +18117 +16269 +3412 +10649 +12279 +3508 +7951 +13929 +12250 +17637 +8922 +18762 +18329 +14702 +9507 +19961 +850 +9597 +4414 +13247 +16822 +10422 +19205 +3713 +3436 +7248 +4243 +10902 +8762 +15545 +12653 +18681 +13455 +14691 +10967 +3489 +18920 +12677 +9470 +2134 +15958 +350 +19280 +17241 +19725 +3551 +7246 +19535 +7724 +18758 +1731 +3332 +17522 +467 +2625 +9608 +14969 +1323 +9233 +16160 +14099 +5302 +8554 +4449 +5333 +9273 +16185 +14522 +17604 +19605 +6303 +12330 +15146 +14008 +7440 +541 +11439 +1576 +16787 +16643 +3187 +1217 +9606 +833 +7777 +15549 +18991 +11612 +4806 +5366 +12275 +19215 +19449 +12385 +8947 +15472 +13739 +14947 +1225 +12670 +6053 +2155 +5435 +15931 +13807 +16673 +8413 +15099 +10069 +13744 +9676 +12002 +16917 +1192 +13262 +15795 +12097 +4419 +2704 +15031 +10581 +13753 +5473 +721 +8536 +15470 +12176 +11335 +3264 +1075 +3213 +12203 +1035 +18985 +621 +4571 +832 +18668 +1008 +6614 +10274 +7827 +18731 +2421 +10075 +15523 +2499 +3761 +13927 +7374 +2263 +4507 +209 +8779 +492 +9195 +18556 +10960 +16021 +10464 +5662 +18906 +13364 +13332 +13413 +8647 +2909 +6985 +16880 +16692 +17599 +788 +14233 +7122 +14248 +14319 +9678 +7149 +10526 +1587 +19510 +5975 +4104 +3319 +9437 +996 +4384 +6922 +5501 +17736 +3483 +7518 +8495 +368 +1461 +9559 +15729 +6638 +5407 +13111 +14491 +8824 +6615 +2284 +14186 +10279 +11785 +10097 +16919 +14273 +15533 +4079 +18344 +2175 +15635 +14883 +4928 +8034 +19930 +13460 +5421 +8072 +18176 +18157 +14272 +6271 +15237 +8601 +12886 +8145 +3390 +11900 +8524 +3615 +3893 +17727 +3817 +16812 +1177 +19385 +2759 +14657 +11853 +19444 +12660 +3987 +2960 +16110 +12808 +1706 +9939 +11800 +4441 +14638 +459 +13076 +18192 +10061 +18226 +8324 +19067 +17264 +14165 +19323 +3747 +9056 +4321 +9546 +8030 +2356 +18989 +16340 +11970 +16797 +14564 +5216 +359 +1881 +17087 +11333 +18725 +10957 +5118 +2959 +4838 +12425 +14879 +11620 +5585 +12994 +3695 +18873 +2286 +10737 +16869 +11498 +18970 +4309 +772 +4029 +18787 +8505 +4596 +2731 +12749 +1349 +16805 +7028 +14818 +5098 +7543 +6474 +8697 +4439 +5308 +12946 +18423 +13965 +11975 +2260 +7046 +17257 +15904 +1114 +7306 +5780 +2764 +8788 +19176 +15016 +7772 +5060 +10836 +9873 +11267 +10981 +8305 +5044 +5872 +19841 +7894 +8600 +19411 +5946 +13648 +255 +2447 +1558 +490 +17820 +17806 +15541 +893 +6841 +15371 +11170 +10039 +9423 +17294 +18620 +8311 +17065 +968 +3834 +545 +16969 +12174 +4056 +16630 +8260 +8432 +10238 +14779 +4390 +2883 +14841 +10338 +13475 +7354 +14736 +13167 +2772 +18162 +15228 +12305 +12719 +1644 +10361 +7441 +17815 +11553 +8711 +1066 +5487 +9175 +9275 +5210 +18973 +13240 +17780 +2550 +12982 +14832 +10927 +4661 +16513 +12471 +15925 +2872 +12541 +17750 +18498 +6578 +3611 +17994 +13313 +22 +18334 +2349 +1415 +16900 +13975 +3906 +7735 +9625 +11194 +18855 +11188 +16154 +10403 +3064 +15075 +1773 +5986 +8845 +12083 +920 +3050 +17827 +5658 +16020 +17909 +7881 +4933 +12873 +8433 +8631 +18760 +15821 +7722 +11460 +17342 +2034 +17490 +10780 +883 +14097 +15098 +14621 +6636 +5720 +1124 +16757 +10874 +15537 +1557 +10173 +17788 +1749 +8910 +13488 +11561 +18073 +15930 +12831 +14569 +2545 +11320 +11238 +19196 +3753 +12191 +1336 +17805 +8758 +8200 +7914 +2095 +8798 +6651 +11808 +2970 +19186 +15383 +19024 +8654 +14744 +12256 +1023 +963 +7280 +8548 +11983 +1423 +3704 +17529 +10627 +743 +3001 +16400 +5489 +5010 +180 +5853 +13238 +12358 +15963 +14717 +11350 +3159 +18665 +14449 +15660 +19809 +18975 +2655 +306 +4030 +7739 +8509 +14567 +8664 +9500 +13230 +16056 +567 +15875 +2716 +18559 +17496 +1212 +8394 +1828 +864 +16868 +16018 +4259 +2470 +10454 +14166 +5460 +4926 +10011 +3869 +1324 +13729 +5361 +11782 +9954 +10348 +16472 +11978 +10548 +2312 +14032 +4262 +4674 +1050 +855 +6933 +18283 +15984 +15960 +609 +5280 +7784 +3261 +10588 +13579 +19058 +13563 +15211 +17376 +18894 +13797 +14550 +15929 +11391 +19746 +2497 +2762 +5351 +3573 +7663 +13316 +5052 +18248 +10496 +9108 +17914 +1589 +10976 +12564 +5640 +7052 +3650 +19288 +17354 +5334 +1612 +8429 +19457 +12336 +844 +17372 +2722 +4020 +13935 +2141 +13269 +4597 +14880 +17706 +10707 +4160 +8588 +7049 +909 +17520 +17317 +18777 +10 +18364 +11199 +17916 +11227 +1696 +14325 +12214 +7281 +14539 +16857 +1591 +6780 +6432 +8874 +10189 +9146 +15350 +5802 +18704 +2868 +4437 +172 +4893 +14474 +13974 +13089 +5457 +10797 +13858 +6339 +821 +8035 +10046 +14123 +19109 +15420 +16257 +11615 +7462 +8867 +8253 +13737 +20 +107 +17031 +13078 +8634 +1974 +14051 +16800 +8299 +19040 +17896 +9109 +7124 +12631 +17931 +1890 +6823 +8000 +4767 +2295 +15732 +1915 +10664 +15103 +9049 +18741 +17887 +3320 +12120 +9574 +17724 +9563 +6734 +2316 +31 +3671 +4605 +1084 +9813 +6435 +520 +19309 +3978 +6141 +17935 +6068 +18886 +3372 +4828 +99 +14056 +13544 +17509 +853 +14034 +18028 +11904 +16706 +11293 +8434 +7642 +12996 +14371 +3298 +17073 +267 +18636 +6920 +4529 +4574 +8390 +11360 +5255 +5601 +12248 +10778 +13658 +5784 +5476 +7671 +19286 +18144 +1927 +14367 +6863 +3805 +17986 +16333 +3227 +18191 +3301 +18071 +7150 +8780 +13907 +12393 +16166 +11973 +15147 +9541 +14193 +7318 +3712 +13708 +11345 +10427 +4323 +12026 +10245 +19967 +1405 +388 +16958 +2546 +7439 +10000 +16496 +16774 +6023 +16728 +13478 +2087 +11657 +14383 +4916 +18463 +4677 +4421 +17658 +8330 +1958 +11352 +7005 +19148 +5629 +14120 +5343 +10312 +18412 +17808 +770 +13891 +7283 +1547 +19493 +8091 +13424 +303 +3252 +12888 +2924 +12908 +13878 +13442 +15359 +5838 +11929 +5483 +8948 +15000 +8852 +12754 +7103 +17378 +1990 +2635 +1314 +19376 +10616 +10319 +569 +16644 +18499 +6572 +7071 +8093 +2218 +7749 +8333 +3011 +6260 +3358 +13649 +3821 +11575 +17623 +8707 +19293 +9585 +16920 +16461 +15377 +19556 +8660 +11807 +9534 +12326 +6289 +16950 +12742 +7347 +339 +12876 +15013 +6976 +16105 +15043 +18193 +1348 +14931 +583 +19476 +8455 +7530 +13897 +13373 +13684 +13655 +5635 +9475 +377 +16225 +12223 +17025 +13706 +11763 +8756 +834 +10618 +19095 +11740 +15561 +7160 +11522 +8688 +7273 +4477 +14811 +9832 +12352 +14281 +7299 +8576 +1020 +2961 +12276 +1193 +16897 +3473 +1185 +1198 +1353 +12881 +4077 +15642 +5511 +12840 +9750 +13101 +8026 +2282 +14224 +5480 +10594 +9903 +16358 +458 +18927 +19045 +9355 +7312 +3094 +12584 +6116 +16287 +3091 +967 +19947 +5920 +5979 +4883 +11583 +15033 +2160 +3479 +15639 +1204 +16521 +2777 +13765 +3517 +18076 +16376 +4802 +1197 +11109 +11985 +7585 +11451 +19169 +10648 +18223 +9554 +18688 +16116 +19412 +19371 +17483 +10726 +13054 +15421 +15637 +5861 +2355 +6874 +2054 +8052 +4763 +5086 +17479 +16568 +286 +11314 +5630 +7638 +4785 +18259 +15063 +5927 +1902 +9487 +4430 +12127 +7984 +5323 +14082 +10574 +11798 +19408 +13315 +17400 +12810 +6405 +10885 +18982 +6347 +6107 +12879 +14257 +6212 +5437 +12789 +17322 +2320 +17370 +1352 +14275 +8316 +19375 +2163 +10911 +12257 +13001 +3599 +13911 +12429 +5992 +16178 +11690 +6844 +10925 +11040 +13829 +3140 +5848 +18252 +14050 +18210 +13620 +4801 +6090 +8264 +1200 +13511 +14581 +18087 +15366 +4711 +5058 +18509 +10826 +10217 +19119 +6825 +12066 +17167 +2869 +262 +14434 +2468 +11247 +4607 +16667 +4655 +4009 +19788 +11664 +15257 +3981 +11512 +18254 +241 +14508 +5385 +14884 +508 +1010 +3533 +9474 +12500 +476 +19878 +8245 +6221 +16128 +11381 +13202 +13770 +9372 +15750 +9753 +12263 +7979 +8317 +16030 +1428 +3304 +5890 +10040 +16840 +11006 +103 +15437 +1237 +11452 +6471 +14688 +815 +10416 +14624 +15082 +12329 +10168 +7885 +8055 +6173 +1700 +14259 +19945 +3396 +11197 +5867 +14058 +2610 +7808 +18272 +19800 +11895 +5748 +5678 +19439 +18288 +7690 +17950 +4670 +6855 +13832 +2375 +16247 +11035 +8411 +7119 +8172 +19577 +18407 +16033 +9886 +618 +6757 +8663 +7024 +4404 +3775 +5904 +3496 +11175 +1178 +18651 +540 +14839 +715 +14427 +19913 +11250 +10014 +18733 +1745 +8873 +19741 +17799 +19854 +17954 +1250 +10275 +9579 +1962 +7512 +15141 +4980 +7405 +8585 +10711 +16475 +11717 +18530 +7008 +17316 +2051 +17537 +13796 +1477 +7907 +6230 +8903 +15358 +17492 +12721 +12635 +9425 +7215 +4657 +15880 +5509 +9729 +5597 +15150 +928 +4194 +1885 +3352 +8450 +12802 +4699 +12373 +19727 +1007 +6365 +5531 +4505 +5181 +6139 +2523 +10598 +10730 +12987 +2547 +3516 +680 +18052 +19078 +4568 +18027 +2796 +1980 +19124 +7240 +10159 +19131 +17431 +8023 +2190 +13260 +5143 +7262 +5203 +1040 +509 +8107 +6132 +547 +7740 +865 +3225 +14348 +3 +17698 +4518 +14070 +8993 +3922 +15050 +7829 +15039 +9427 +18434 +2757 +6380 +6816 +19475 +4465 +1804 +19770 +10633 +6774 +768 +5538 +14069 +4741 +18234 +16102 +7251 +2454 +18843 +9332 +5262 +16198 +9344 +2407 +10856 +1814 +12693 +3466 +12384 +14777 +2629 +16280 +9697 +6262 +6032 +6644 +5723 +8028 +8690 +13169 +19321 +3191 +2749 +10658 +8676 +4347 +17068 +19545 +2317 +2813 +8048 +17365 +16087 +5632 +7524 +5071 +16539 +3634 +18685 +1946 +12445 +16760 +18307 +8191 +6206 +18602 +2410 +11873 +8244 +2398 +9473 +19936 +14607 +13920 +4149 +17223 +13809 +9226 +5991 +16764 +19141 +5709 +8294 +1017 +14026 +13389 +10963 +19174 +8850 +16622 +8344 +11248 +3721 +16117 +5731 +2810 +12726 +3596 +3072 +18576 +13369 +5425 +5430 +8846 +15783 +9879 +12443 +4728 +17085 +3188 +16256 +15425 +19267 +1227 +6170 +12137 +4440 +10004 +7949 +8129 +11707 +11220 +1256 +17421 +2007 +1174 +10396 +16049 +9773 +606 +2132 +9313 +5728 +4251 +14010 +10174 +16283 +3203 +11755 +2623 +2621 +15918 +18477 +14574 +6723 +5389 +14221 +7239 +2113 +16454 +16901 +6715 +4547 +12311 +19231 +15456 +18515 +5515 +245 +13456 +15386 +19927 +13823 +13702 +19978 +12766 +8962 +18392 +11673 +12983 +19551 +11223 +12740 +5505 +5675 +3331 +3480 +15799 +19170 +14468 +160 +19132 +6284 +19458 +16980 +1611 +15836 +2937 +10568 +14133 +7288 +13865 +13763 +19851 +16933 +12944 +10334 +9367 +8276 +11863 +9117 +2688 +16561 +13508 +14352 +13068 +4508 +14538 +13431 +5919 +17369 +12828 +13981 +2997 +16843 +4373 +8359 +15842 +12756 +2176 +13393 +811 +3988 +19814 +5265 +14033 +10860 +9549 +8273 +5788 +1121 +18359 +16169 +5434 +1772 +18744 +1245 +11874 +7131 +15168 +2703 +3079 +18947 +12397 +11053 +14900 +13163 +18433 +9651 +1265 +3100 +12452 +688 +18261 +19883 +14141 +4545 +16244 +2580 +9951 +16887 +9712 +2110 +17558 +13218 +5065 +4403 +2004 +18790 +17525 +285 +19661 +3891 +14106 +4975 +1769 +2406 +2606 +9744 +19775 +6918 +15410 +16248 +13083 +5247 +16733 +4649 +7420 +19254 +2432 +11139 +16608 +2 +11832 +7230 +1347 +16937 +15047 +3078 +9306 +6563 +10611 +5911 +7459 +634 +17381 +18323 +15443 +12902 +16385 +17239 +12229 +12747 +13786 +16923 +123 +11850 +17773 +12220 +14943 +4254 +3024 +6683 +9112 +9558 +16065 +9754 +6114 +10785 +9192 +15021 +4091 +10762 +3976 +13894 +7588 +13490 +2973 +3498 +6313 +14176 +12108 +3786 +17893 +5156 +18260 +7296 +4740 +15948 +10610 +12735 +5967 +2618 +7175 +558 +16931 +14886 +17757 +13469 +1182 +4252 +16241 +13638 +6514 +12370 +917 +18078 +16360 +8323 +8702 +7892 +1787 +9268 +12665 +16747 +8786 +10053 +4096 +4359 +9432 +12741 +4991 +9333 +12123 +1365 +1734 +8892 +804 +1002 +8888 +14001 +19777 +1742 +7191 +6492 +17321 +2068 +991 +15616 +18921 +2856 +8685 +8401 +19251 +7252 +16467 +4786 +1337 +7162 +12561 +660 +18128 +16329 +15805 +8765 +8106 +13931 +4253 +1899 +8547 +9874 +10070 +19855 +7469 +9369 +10993 +18299 +14560 +421 +11541 +17590 +11107 +17359 +8500 +18818 +17552 +3470 +8992 +2177 +9326 +16060 +1450 +8070 +6250 +9581 +5212 +12286 +10537 +16717 +17325 +16510 +11787 +9036 +16108 +16005 +15393 +15649 +12496 +1824 +561 +10873 +19807 +1411 +6834 +19425 +4272 +7146 +9611 +18896 +18294 +17823 +4757 +14002 +2660 +3994 +14277 +969 +5206 +6735 +16567 +4575 +13624 +9110 +798 +15233 +15876 +13539 +16505 +15269 +12232 +9980 +15998 +7732 +14228 +14795 +3918 +11534 +7952 +11627 +16205 +16946 +17793 +9019 +3471 +1285 +239 +17953 +15475 +5977 +12189 +13401 +10815 +6292 +19903 +7327 +6760 +6556 +16262 +12474 +3330 +12228 +2534 +19901 +5445 +6362 +3049 +1628 +6325 +16137 +15806 +3920 +19692 +1698 +8638 +10448 +7519 +2776 +2619 +13450 +19679 +12958 +18504 +7800 +15653 +19708 +13496 +11813 +3969 +1617 +5836 +14118 +3660 +3449 +959 +17861 +17043 +16370 +3242 +16253 +11202 +17489 +5862 +1627 +18955 +19988 +1843 +7603 +2440 +15520 +11045 +7795 +19496 +9489 +9346 +6272 +11126 +18224 +1203 +18554 +11653 +18772 +16097 +9587 +7388 +8880 +19014 +543 +14562 +12990 +3748 +5647 +8797 +14232 +11971 +19479 +7610 +10091 +1271 +7820 +1673 +17328 +2594 +7343 +11236 +874 +3089 +7018 +16476 +2543 +6482 +17442 +67 +8750 +16804 +12381 +5006 +10131 +14996 +19473 +8490 +2193 +16635 +1165 +7183 +814 +17197 +6759 +6047 +10512 +2091 +9638 +204 +12493 +4565 +15707 +8232 +6329 +12355 +5023 +17163 +729 +11060 +6342 +14723 +11649 +16541 +15807 +10782 +8629 +6041 +11896 +8801 +17349 +10470 +9858 +14075 +14626 +19697 +6045 +6171 +9956 +49 +12119 +3902 +14407 +15065 +15294 +6202 +11537 +16072 +17906 +2861 +7833 +16529 +7693 +8692 +614 +8952 +17057 +17227 +4899 +4345 +6184 +16556 +12150 +6706 +17543 +19292 +18274 +10212 +15172 +18316 +3349 +6396 +2695 +1666 +4209 +18277 +14509 +7526 +14587 +13711 +2701 +11318 +7359 +7451 +14937 +2357 +12236 +9354 +4396 +15724 +774 +9248 +8741 +13075 +13720 +1303 +8266 +297 +12084 +10243 +16373 +17437 +4478 +1064 +15761 +8887 +3265 +7413 +14851 +18791 +16852 +667 +6673 +251 +16703 +16756 +5197 +8570 +18065 +13676 +11280 +7581 +14199 +1681 +1094 +10998 +14419 +15655 +3625 +2520 +13185 +7110 +15500 +6580 +8159 +3202 +14387 +10684 +19307 +5348 +14265 +13122 +11848 +1550 +12552 +8395 +6506 +17169 +14684 +7286 +12039 +1476 +5620 +8775 +14177 +19511 +1005 +15419 +1601 +1675 +17138 +9162 +9172 +10298 +7258 +16748 +12974 +7620 +3694 +16357 +7943 +11426 +5682 +6565 +10333 +15589 +12567 +2227 +9777 +18432 +613 +839 +13344 +12205 +7941 +19245 +14968 +15595 +19147 +11613 +1695 +7502 +13653 +16324 +803 +4036 +14420 +2923 +18862 +11313 +10819 +8182 +15715 +6773 +12703 +6414 +17412 +2858 +17174 +7742 +9877 +11493 +38 +15556 +16962 +2829 +6205 +11902 +396 +11129 +5880 +14792 +12441 +17998 +69 +13664 +11580 +6084 +3525 +9990 +7069 +12692 +11726 +6145 +16715 +10095 +4293 +8728 +2823 +19998 +11276 +11329 +17646 +8009 +35 +5078 +10708 +7392 +11222 +4618 +12078 +1442 +14970 +15156 +6881 +3971 +2911 +7753 +4560 +14590 +4355 +10084 +6390 +8986 +7987 +7601 +2294 +17913 +7088 +15866 +15701 +5670 +14004 +8526 +17111 +14183 +14182 +6203 +18037 +4071 +18852 +10211 +7609 +15179 +15823 +10365 +13672 +19276 +15221 +11722 +6340 +16977 +9780 +14231 +11031 +719 +9797 +10705 +6775 +15435 +2592 +5224 +11256 +1386 +15251 +3125 +441 +6043 +16791 +19722 +16489 +15714 +2712 +7068 +4070 +16193 +7334 +8960 +1948 +9576 +13483 +11828 +18934 +7745 +13740 +16678 +718 +19145 +9998 +18233 +15327 +13971 +2756 +9602 +11095 +16348 +97 +10999 +8721 +13599 +4590 +11814 +8074 +16174 +8943 +12654 +12045 +15975 +3539 +10518 +15246 +15548 +14337 +16401 +468 +17409 +11063 +3783 +13608 +4621 +1536 +18990 +4994 +8020 +2780 +638 +11465 +14086 +4173 +14497 +6868 +316 +2151 +3672 +6934 +12739 +1026 +17584 +15290 +18151 +12861 +8203 +17444 +18532 +17967 +15313 +16661 +15814 +14410 +9567 +8268 +1418 +16936 +17695 +14013 +5707 +15870 +16975 +17117 +11635 +18866 +16530 +10301 +16424 +9982 +2076 +19111 +1867 +9856 +8551 +13097 +1984 +11011 +10725 +16015 +371 +17 +2327 +5102 +13302 +9442 +13025 +5669 +4622 +1660 +7876 +18531 +6832 +13570 +11001 +8166 +15467 +16666 +1251 +13377 +5770 +7265 +3851 +726 +18150 +18646 +10270 +3239 +871 +7779 +141 +11644 +879 +18105 +14687 +17158 +18605 +3895 +5298 +5081 +3272 +16441 +7632 +7404 +18597 +8138 +5721 +10678 +8186 +17795 +140 +16811 +3569 +6559 +5141 +7292 +15254 +325 +12348 +11977 +9511 +13403 +14563 +8578 +8341 +18473 +5754 +4098 +19989 +9179 +5426 +8502 +7425 +2617 +9323 +8332 +13509 +3338 +9921 +16739 +3402 +6214 +15692 +17999 +14914 +18267 +4713 +8558 +6464 +17779 +15722 +5367 +15661 +3244 +12322 +4212 +6552 +6885 +14614 +15927 +218 +14159 +10385 +12655 +10478 +1455 +16632 +8738 +19565 +1549 +14692 +17387 +17918 +17168 +4199 +19484 +16932 +5324 +5631 +4647 +10460 +10110 +17033 +13275 +12807 +19811 +5860 +2595 +16139 +10339 +11386 +17716 +15566 +822 +8953 +19795 +14760 +9985 +15775 +3328 +7022 +5666 +15704 +19926 +12575 +16347 +1313 +9941 +2840 +15093 +14148 +13627 +14344 +11941 +19970 +269 +1318 +14241 +3019 +8419 +19940 +10171 +4662 +5194 +4841 +4117 +14623 +19575 +14416 +824 +938 +7891 +6103 +15516 +10614 +5925 +16818 +18166 +13515 +95 +15406 +19134 +11628 +17979 +1319 +2744 +5070 +18099 +2359 +16881 +7317 +10696 +7630 +13391 +15771 +7346 +18313 +8844 +16699 +12388 +19591 +6455 +14269 +1646 +11540 +8579 +12195 +6517 +10214 +3041 +4329 +16465 +14135 +2266 +8262 +7932 +16970 +7365 +15173 +16220 +18402 +18717 +8235 +12535 +15352 +3006 +14791 +3232 +18218 +15011 +17642 +3547 +753 +5095 +17252 +3986 +3626 +4497 +1981 +9866 +1851 +2915 +633 +3008 +19073 +1672 +16067 +1304 +2770 +17594 +10531 +1316 +3147 +17340 +8314 +4191 +5851 +19306 +12734 +9571 +7472 +1162 +762 +12501 +14104 +3462 +12118 +19537 +10867 +16985 +17121 +10331 +7207 +19291 +14819 +19171 +16103 +1810 +11822 +10167 +5644 +2750 +11956 +2638 +13430 +8726 +15164 +16921 +14834 +3228 +13214 +12320 +9083 +7657 +9446 +14592 +14480 +11566 +17878 +625 +1444 +19721 +16743 +17076 +2512 +19734 +5115 +8468 +16031 +6970 +12674 +17941 +12439 +16286 +15833 +611 +8489 +15367 +1186 +12645 +19794 +12851 +15045 +5815 +13926 +7450 +5447 +5587 +10840 +1473 +19572 +10193 +4492 +1052 +13756 +13386 +14158 +15816 +17053 +19329 +19381 +4953 +8058 +4528 +12776 +18810 +2164 +9786 +16531 +7587 +17128 +6062 +15194 +8386 +15312 +4862 +10516 +18457 +15501 +8014 +5049 +18686 +9979 +13290 +15217 +8258 +11885 +4992 +4804 +1051 +6808 +8996 +11544 +11998 +15433 +2593 +3503 +4553 +11775 +11830 +11155 +19097 +2350 +16824 +3867 +16311 +4512 +886 +9208 +17629 +15605 +10366 +15288 +1553 +14342 +5401 +7079 +4521 +17777 +13521 +11149 +10379 +18112 +11283 +19934 +11485 +11910 +14617 +14283 +514 +19618 +13292 +9308 +19877 +1636 +17647 +13923 +370 +14766 +15962 +10177 +9713 +18321 +14160 +6218 +13667 +5873 +8849 +10158 +7221 +6839 +11160 +11390 +147 +5220 +6953 +934 +13510 +15121 +15338 +1278 +1170 +7464 +2101 +11571 +9134 +7423 +6336 +15471 +17458 +7918 +3378 +7319 +19483 +8734 +9613 +2720 +11265 +6765 +8372 +16232 +10871 +8774 +19504 +6028 +1021 +11576 +1953 +15631 +5944 +12261 +612 +6211 +3979 +2476 +10484 +9700 +12035 +6418 +4433 +12461 +1613 +9820 +10101 +13982 +11020 +6507 +14873 +12825 +848 +2488 +15477 +17672 +6393 +11936 +13241 +7177 +18271 +16063 +5349 +378 +19032 +2933 +3890 +15297 +14979 +1748 +1585 +19406 +16633 +9888 +14586 +8125 +5082 +12337 +16759 +19325 +7031 +19274 +17178 +16104 +12095 +11438 +9745 +3021 +19471 +6770 +685 +11585 +14793 +18847 +6305 +2910 +14485 +15512 +4915 +2846 +19600 +19826 +17586 +7415 +17362 +19755 +15594 +11122 +8146 +12596 +19407 +14661 +18918 +740 +10393 +10191 +8210 +19827 +13785 +5128 +8170 +12032 +11174 +18322 +10912 +9443 +9480 +17635 +11351 +17084 +1498 +19752 +6420 +16387 +2785 +10149 +9115 +16305 +15375 +5907 +16639 +11511 +12763 +14669 +19299 +18490 +18933 +6629 +16839 +13719 +15680 +7349 +17553 +14295 +18195 +18256 +10788 +9975 +6476 +10935 +10392 +389 +3844 +19319 +17549 +1842 +10006 +10265 +6475 +9405 +11574 +14731 +8437 +669 +2441 +8966 +7289 +17410 +1950 +4738 +12818 +4793 +15009 +9634 +4324 +18380 +9530 +9671 +17036 +6731 +13583 +7000 +9502 +12830 +2057 +15810 +19666 +2205 +4409 +18016 +410 +5606 +5739 +13569 +4962 +18270 +5416 +6407 +13061 +9050 +1272 +8997 +13365 +5845 +13713 +14425 +2265 +10491 +592 +18033 +6223 +16491 +15591 +738 +5190 +826 +5386 +2798 +293 +19242 +14475 +6215 +10246 +9257 +6672 +7032 +11375 +17202 +11877 +4074 +9239 +14305 +13632 +14683 +5833 +505 +3046 +14960 +11614 +3942 +14130 +5686 +16352 +10081 +8296 +13752 +8627 +1530 +338 +448 +13840 +18948 +8403 +9402 +5948 +19030 +4638 +176 +2649 +15559 +1741 +8406 +3346 +6269 +15673 +10151 +5292 +1710 +11436 +10951 +978 +12139 +6625 +1317 +8876 +5074 +16409 +4326 +3646 +2822 +10979 +11204 +4072 +3444 +16458 +4057 +16554 +1263 +3146 +5159 +15116 +6853 +2358 +18281 +8954 +1703 +3167 +8099 +12532 +7680 +19549 +5730 +6675 +10926 +13175 +915 +2866 +17992 +3420 +9885 +8557 +4295 +2335 +9846 +11275 +9523 +11905 +19419 +15062 +8896 +10465 +7295 +15543 +13428 +4676 +19146 +10621 +8863 +15911 +17798 +18672 +10390 +3336 +7783 +18747 +12730 +5523 +13064 +11019 +6104 +2153 +12998 +19804 +8382 +11757 +7257 +1593 +7521 +3356 +18181 +17710 +2027 +6584 +16875 +16215 +8757 +14496 +15645 +9207 +13318 +10850 +10286 +654 +6096 +16083 +6467 +17575 +4066 +16453 +9550 +6660 +7766 +19152 +18049 +10443 +41 +13514 +16334 +7436 +12473 +16583 +14053 +14297 +15656 +12490 +6886 +19208 +10432 +6377 +18863 +256 +10634 +2990 +7431 +19759 +6002 +2585 +9536 +3886 +6257 +16525 +15323 +1707 +16165 +15648 +5876 +1048 +4265 +15344 +307 +17066 +5471 +7759 +19561 +8494 +11599 +13033 +14924 +13999 +3749 +12344 +4163 +19128 +19181 +3887 +2925 +12744 +12087 +17291 +11311 +3662 +17814 +10228 +19105 +18335 +19362 +5667 +18221 +17856 +9600 +6035 +5779 +17796 +11086 +15926 +3731 +1908 +16463 +320 +3222 +1451 +14375 +14012 +939 +11594 +9718 +11737 +15853 +14296 +13639 +14856 +9824 +507 +4520 +15803 +16938 +15506 +16470 +11068 +17334 +585 +8791 +7489 +19713 +12283 +7707 +3277 +2922 +10703 +13287 +18629 +4452 +15291 +18096 +5059 +9210 +14525 +11264 +4363 +15985 +7696 +7087 +3120 +1823 +11117 +14340 +16978 +2399 +13609 +7387 +1223 +10276 +3259 +628 +10430 +13209 +1452 +14247 +5018 +7081 +1868 +14663 +11959 +3143 +1219 +9732 +14714 +13594 +1358 +17261 +12848 +9178 +3235 +7091 +4715 +16349 +3386 +4183 +2654 +15049 +11768 +6525 +1254 +19219 +17371 +2554 +19981 +14935 +19342 +3506 +1253 +6705 +5429 +12177 +8613 +16303 +15514 +1978 +4585 +8365 +10140 +11950 +10947 +10651 +16127 +104 +16307 +1472 +10463 +12915 +1869 +17835 +8275 +10313 +11693 +18844 +14498 +12745 +16452 +13059 +19867 +16929 +5899 +1169 +5028 +6875 +6076 +3645 +11112 +12965 +7086 +13105 +11584 +19634 +14686 +7704 +16408 +16825 +14870 +13901 +11735 +10240 +4206 +4372 +6818 +6966 +7073 +8078 +503 +10373 +19271 +7351 +1416 +9702 +14438 +4273 +16421 +10187 +17427 +17797 +19366 +2521 +19397 +12893 +18562 +14896 +7338 +17932 +14203 +5328 +14418 +1519 +1737 +13480 +16145 +9785 +8458 +14726 +6824 +3310 +5844 +16095 +6259 +7621 +407 +12332 +1768 +14428 +2949 +12868 +2459 +9959 +4235 +2453 +15633 +18381 +18265 +10798 +19553 +6570 +19683 +13912 +4956 +16701 +7492 +3388 +8057 +7060 +16726 +9445 +5902 +9935 +17300 +6714 +86 +9015 +6465 +12651 +13030 +6620 +7225 +10315 +7121 +9095 +13362 +14194 +16129 +5679 +13769 +5767 +13162 +14483 +5204 +1019 +3174 +1449 +2905 +6050 +16589 +17834 +13582 +6598 +12528 +6971 +13350 +6102 +16308 +17064 +12426 +10974 +8112 +1864 +5856 +10451 +17559 +12475 +10499 +8163 +13683 +3580 +7045 +18352 +7551 +3700 +10320 +12245 +10160 +9726 +14578 +2031 +5491 +10494 +15716 +15909 +9305 +4371 +1955 +2396 +14843 +607 +19224 +12076 +11346 +13781 +2080 +19571 +1340 +7270 +10090 +15693 +6521 +2337 +10535 +15053 +12504 +7303 +3973 +10345 +18965 +1756 +2566 +2982 +695 +11573 +12760 +19702 +17811 +18104 +11103 +8990 +3733 +13172 +3842 +7247 +16693 +19401 +432 +15256 +15841 +6010 +11327 +1286 +10485 +1971 +1866 +9683 +13271 +14751 +5999 +2766 +5272 +7797 +8620 +2299 +11316 +11133 +5782 +14115 +6175 +7272 +5107 +2945 +16915 +18776 +11021 +13335 +5957 +14566 +15760 +3989 +3800 +7457 +4890 +11228 +2531 +686 +6115 +15981 +14391 +13766 +17630 +17154 +9555 +13019 +10872 +11234 +5340 +1863 +1370 +14740 +4696 +5940 +16460 +13689 +16637 +8303 +12407 +5600 +9533 +19776 +18427 +17960 +4938 +10310 +4055 +12240 +11317 +548 +12440 +11530 +3898 +1574 +9605 +6099 +7442 +13387 +16315 +5396 +17035 +17877 +15302 +7410 +10299 +17225 +16548 +10487 +5377 +19773 +8735 +6194 +11480 +17802 +10625 +7793 +5671 +13685 +15966 +6278 +4336 +455 +15178 +11860 +16209 +7867 +8376 +3062 +12403 +12938 +8787 +17420 +9419 +4813 +5622 +13646 +10469 +2914 +8238 +5269 +13951 +6064 +6140 +18419 +11691 +14216 +14502 +15767 +16925 +10567 +6489 +14825 +2124 +11036 +19312 +6526 +1417 +19688 +15686 +1945 +8192 +250 +11190 +133 +14045 +1532 +8040 +6543 +9400 +3492 +19554 +2147 +19503 +13124 +3688 +4002 +14845 +8580 +18435 +14868 +5075 +15577 +354 +19876 +19144 +11075 +10962 +6633 +16722 +14885 +14299 +11838 +13305 +12772 +3932 +14951 +17939 +3031 +7323 +6950 +852 +13363 +14899 +2280 +16826 +3702 +18951 +1091 +6722 +11533 +13493 +10468 +11598 +12737 +6310 +577 +14609 +5214 +1762 +17122 +11048 +13327 +8569 +18535 +19894 +6281 +9934 +2634 +14894 +10866 +3475 +4038 +9783 +1273 +17957 +14467 +14236 +13904 +14214 +14541 +2074 +12175 +12103 +15535 +4486 +4398 +2964 +1949 +10382 +4874 +7688 +15407 +13482 +17181 +11261 +13127 +19277 +2023 +6077 +7275 +9377 +8527 +15550 +1333 +5219 +9526 +12349 +8185 +16890 +1711 +15551 +15787 +3093 +14372 +14809 +12968 +10799 +2962 +17273 +13548 +8261 +10261 +11642 +622 +1904 +2162 +17288 +15265 +17126 +12512 +5824 +11646 +11960 +17214 +19724 +9793 +14379 +2120 +16520 +16141 +8193 +12282 +15688 +10020 +4837 +7787 +10477 +15160 +10748 +705 +400 +460 +16391 +15749 +19480 +12273 +17088 +14699 +18840 +14936 +14023 +7279 +10559 +5689 +2224 +1153 +7903 +3552 +16959 +17667 +17120 +18425 +11482 +18264 +15253 +1657 +7214 +14136 +11004 +8188 +16316 +9540 +3864 +18474 +162 +18604 +902 +12929 +4256 +8560 +1027 +8281 +11085 +7595 +5993 +7698 +15978 +19090 +3045 +4970 +16614 +9643 +9131 +12238 +19655 +10226 +15959 +8282 +11286 +2984 +11394 +2133 +19586 +761 +4551 +18276 +13194 +7337 +14770 +19065 +15191 +17765 +18905 +11882 +12131 +10882 +18656 +19550 +596 +9725 +13297 +3693 +2428 +16485 +9569 +4201 +5363 +725 +5273 +17098 +8931 +362 +10770 +10340 +1940 +3013 +6516 +10542 +3308 +16371 +18478 +9044 +2307 +284 +1888 +2325 +18719 +9845 +423 +5781 +3273 +1467 +11749 +9641 +626 +7718 +13674 +10922 +15280 +6896 +7379 +10062 +9297 +17937 +11474 +9111 +4461 +19049 +8298 +14370 +1947 +5145 +18411 +19017 +12477 +11189 +16061 +1060 +15899 +5859 +19452 +16459 +11762 +742 +10520 +11469 +19933 +15974 +7395 +8820 +6119 +14287 +19360 +3181 +12085 +3968 +941 +11968 +11166 +1097 +18215 +14204 +16455 +2732 +13499 +6439 +1376 +18674 +14211 +13531 +2935 +4308 +791 +10316 +2696 +19330 +2037 +5054 +1723 +17714 +5760 +11347 +7117 +17910 +19498 +2365 +16898 +7394 +8668 +16335 +14940 +2094 +6481 +14309 +7661 +3493 +2715 +18742 +13598 +12327 +13445 +12533 +3927 +5610 +15112 +3957 +249 +4463 +16670 +776 +9071 +1894 +4716 +16708 +11464 +10753 +1725 +11680 +2220 +16285 +14743 +10295 +12075 +17333 +15529 +16010 +1641 +18549 +10728 +8515 +17018 +9765 +18745 +1754 +2877 +13622 +3889 +1753 +5032 +6425 +3998 +13268 +18086 +6124 +9672 +10994 +12976 +1493 +8039 +6802 +312 +11252 +18244 +19843 +18273 +17254 +7764 +2940 +16572 +8587 +11556 +18522 +3419 +11039 +14084 +3649 +15987 +7172 +14810 +7016 +17060 +13877 +1871 +11376 +5176 +4584 +12354 +10178 +18832 +18569 +14443 +219 +2150 +18328 +2311 +2370 +5488 +17743 +1970 +7738 +15562 +3845 +1146 +1100 +916 +1308 +1537 +19514 +9833 +3848 +16558 +8033 +1404 +6013 +6291 +1378 +19580 +7096 +17146 +3862 +17870 +846 +12736 +2855 +10034 +15604 +9230 +15270 +101 +440 +723 +2615 +13576 +3211 +19581 +17771 +11631 +3647 +1835 +8641 +4827 +8098 +13977 +10411 +17632 +16948 +2608 +373 +2233 +8980 +18729 +9277 +14417 +5691 +14108 +15746 +11056 +16425 +6320 +13618 +15906 +11572 +4595 +8773 +13120 +964 +15387 +15092 +9421 +7151 +17886 +4730 +16956 +16296 +3434 +10851 +9667 +4573 +10449 +6927 +16545 +12056 +11187 +892 +6889 +3017 +14949 +10702 +563 +17595 +17625 +12779 +713 +5529 +15189 +9512 +10591 +7277 +19636 +5160 +17784 +13013 +2258 +9892 +15846 +1049 +16321 +18912 +18534 +2541 +14494 +7645 +739 +593 +7652 +14119 +706 +4843 +248 +4645 +14126 +16547 +11262 +14393 +3967 +10667 +6900 +10923 +7138 +1350 +4868 +313 +19297 +18600 +11524 +4810 +5183 +11776 +2438 +2011 +15691 +6454 +1059 +13072 +1092 +6315 +16500 +17965 +6113 +12995 +17171 +19764 +14942 +2058 +16433 +2709 +159 +7077 +9580 +16157 +7733 +1964 +9762 +18158 +16611 +19454 +17005 +16738 +4569 +17220 +17232 +2063 +10749 +7924 +3763 +7563 +5801 +11663 +2951 +2760 +9996 +16059 +11545 +14519 +14320 +14530 +16587 +9330 +13952 +7913 +9424 +3939 +12379 +2805 +19612 +14863 +6327 +4216 +8927 +3457 +4161 +5192 +6276 +11930 +13811 +4947 +1551 +11051 +12770 +2603 +1568 +10025 +9815 +12170 +10886 +14733 +6125 +16147 +7647 +2089 +127 +5617 +4269 +9869 +211 +8821 +7182 +773 +3282 +2024 +18935 +18489 +9693 +12551 +2510 +4368 +15201 +11091 +13418 +8794 +16934 +8565 +7115 +14739 +12751 +9292 +11110 +797 +8362 +18954 +9219 +1305 +3852 +13234 +7947 +15610 +17274 +8364 +17507 +3799 +19316 +629 +5821 +5380 +9772 +8680 +2069 +9957 +19628 +14470 +1490 +6322 +9217 +10894 +15912 +19072 +7546 +18711 +9351 +17344 +16406 +6415 +3959 +11623 +9160 +16294 +7222 +10098 +18890 +19443 +734 +14258 +15864 +18055 +18246 +6952 +6609 +8740 +16001 +9927 +6227 +11079 +8645 +2831 +11816 +14363 +13110 +7886 +4339 +13742 +10878 +3590 +4367 +18580 +18164 +15478 +8747 +8293 +3047 +17462 +12161 +15668 +11724 +10645 +1356 +17465 +11186 +3727 +17337 +659 +2360 +17991 +19640 +18114 +4882 +8533 +10305 +10492 +1688 +3665 +10652 +19392 +11198 +10619 +8539 +6588 +202 +17699 +10700 +12293 +18008 +1095 +1288 +7094 +6729 +5572 +16779 +10148 +10890 +15373 +9518 +17190 +18618 +4302 +15469 +11796 +5261 +122 +17466 +19447 +3274 +9795 +11991 +9519 +5778 +9665 +1635 +18131 +15427 +17103 +8905 +18095 +11588 +261 +7822 +62 +11419 +10227 +10517 +7116 +16457 +16965 +18396 +8595 +15547 +3528 +7210 +9254 +11244 +11212 +19311 +4615 +8425 +18269 +18696 +13793 +5586 +13113 +5564 +17045 +2481 +6008 +2795 +4734 +18793 +17000 +1816 +10467 +9492 +15747 +3426 +9901 +17713 +18100 +17977 +18676 +4190 +6447 +7129 +19236 +3460 +7909 +4431 +19501 +18626 +5369 +12849 +6301 +10500 +4374 +4567 +17293 +6733 +2963 +17132 +605 +8854 +12211 +4789 +13052 +10774 +2017 +8015 +2614 +5858 +235 +12576 +14343 +4922 +10671 +11567 +15774 +14374 +4541 +19063 +19127 +7798 +8865 +12836 +2487 +7329 +1374 +17585 +15231 +7887 +178 +9238 +4853 +11334 +5582 +3938 +2466 +4554 +16792 +13012 +6969 +8270 +19735 +3772 +73 +5399 +12822 +2969 +2449 +11242 +16487 +10213 +2400 +16873 +16410 +858 +18653 +5307 +17497 +185 +13165 +6163 +4049 +13697 +3059 +4869 +1807 +17432 +16156 +10456 +17152 +12289 +2602 +8389 +19662 +15718 +5722 +11290 +10880 +9857 +887 +18643 +8479 +19928 +8683 +12984 +5602 +8661 +10905 +10283 +6754 +169 +14716 +9011 +8027 +14197 +6600 +14988 +12833 +14043 +14173 +4865 +15399 +16534 +18426 +18932 +7367 +8195 +19627 +3233 +11855 +10906 +1306 +3002 +12252 +15698 +3686 +8691 +7433 +6355 +515 +15365 +15792 +6835 +12959 +2492 +12167 +13079 +15476 +4448 +5609 +14565 +4766 +19833 +10251 +7996 +12524 +1900 +11099 +19864 +10577 +17485 +13293 +5513 +7649 +1258 +9091 +16369 +19542 +16597 +18165 +6074 +1919 +15933 +5830 +9029 +6037 +16183 +11732 +19318 +15450 +15849 +13164 +19983 +16278 +15728 +3899 +3546 +13134 +9193 +823 +5110 +2705 +17755 +17570 +17742 +324 +8350 +18757 +1701 +16686 +280 +5384 +18751 +14360 +4879 +12368 +1406 +15391 +16833 +18391 +11402 +5553 +12571 +19140 +6217 +12347 +17947 +19528 +17166 +2974 +18291 +6809 +6669 +17279 +19214 +3588 +16084 +9191 +495 +11090 +14771 +9669 +5983 +12523 +936 +10085 +4671 +16565 +18606 +9997 +3237 +4035 +14838 +14312 +19151 +10384 +3448 +2451 +17883 +15967 +11651 +17093 +10507 +19902 +10324 +17904 +19882 +19900 +14545 +9190 +3769 +12907 +1746 +3257 +11883 +6231 +11105 +8805 +8336 +5755 +645 +19052 +14412 +17748 +6387 +12985 +1393 +6764 +3877 +9361 +11440 +12346 +675 +12017 +4707 +13942 +10989 +10650 +12102 +11636 +14206 +19305 +2727 +15731 +9468 +6149 +14234 +12074 +16152 +7915 +5151 +12725 +8674 +4204 +19991 +13550 +14573 +17671 +17651 +27 +2640 +3554 +3361 +19783 +66 +2573 +12249 +14556 +5008 +61 +4264 +8263 +18409 +80 +6667 +3530 +9801 +7316 +7517 +17828 +52 +952 +5209 +5982 +3830 +7134 +15370 +11547 +13602 +6708 +16806 +19590 +5301 +5633 +8908 +9950 +841 +16730 +2109 +6060 +6915 +8353 +13086 +9952 +9301 +9449 +12165 +13024 +946 +19044 +30 +12837 +4968 +7857 +15117 +9839 +12898 +13051 +1341 +4535 +4982 +8334 +14171 +4635 +8435 +14079 +17272 +5774 +11843 +7989 +17933 +5953 +3467 +7520 +16231 +11182 +15613 +2789 +15163 +12458 +19614 +17393 +6277 +10314 +2354 +14161 +2824 +11909 +4770 +15581 +18891 +4826 +13497 +12040 +3540 +5560 +4277 +16318 +6738 +19472 +19344 +1475 +1705 +7973 +12852 +9735 +9016 +9592 +4391 +19209 +961 +7515 +14681 +7568 +2864 +17477 +8571 +9121 +3401 +14306 +8461 +14476 +10724 +4705 +15333 +4039 +1465 +2477 +2183 +17209 +18621 +17140 +8833 +18347 +19269 +17183 +19213 +13603 +18152 +6595 +15114 +11868 +17711 +2533 +591 +16742 +4052 +16053 +700 +8729 +3256 +19633 +18336 +8724 +6702 +1671 +19818 +382 +16 +16447 +5683 +5248 +4749 +6758 +11708 +752 +12797 +1494 +4703 +5157 +183 +19955 +12015 +9076 +15188 +15625 +19415 +7757 +11507 +15700 +19069 +18963 +14164 +11888 +19283 +17157 +394 +14840 +14513 +464 +11163 +15624 +4739 +18529 +12859 +2955 +3135 +3061 +4931 +6718 +10241 +17568 +17791 +19036 +4180 +11739 +15131 +14618 +17382 +242 +13273 +19837 +12625 +10363 +1344 +18788 +19028 +9907 +9261 +6656 +5113 +8095 +391 +3337 +16601 +1735 +6685 +10663 +913 +1721 +18056 +6468 +17737 +3098 +7917 +15449 +14897 +2475 +10296 +7658 +9244 +7393 +5544 +9486 +18857 +1685 +19229 +2170 +14208 +10582 +3690 +1221 +10675 +2753 +9972 +8881 +6155 +19762 +4474 +8736 +16992 +19671 +19979 +19110 +17617 +18592 +6479 +14134 +8375 +18015 +14678 +1071 +4820 +14020 +11147 +878 +10569 +6830 +6891 +439 +3795 +9936 +13331 +17982 +381 +3507 +6031 +12832 +15267 +6461 +6522 +2719 +8568 +3285 +4905 +15241 +1173 +11857 +10741 +15871 +5812 +4217 +15939 +11997 +273 +9654 +15536 +16036 +15105 +15708 +4665 +13862 +2954 +6634 +13320 +9861 +8301 +14575 +6778 +9339 +11272 +4880 +12945 +18349 +12218 +18024 +3129 +17170 +18168 +17759 +1360 +9435 +15989 +3431 +6851 +14891 +14738 +7321 +9336 +4558 +9461 +3548 +10630 +16000 +10483 +9918 +19210 +19569 +19896 +11083 +18936 +7653 +14225 +1514 +19748 +9034 +15258 +1516 +3770 +12785 +3781 +11315 +12424 +6892 +10414 +12314 +3157 +5096 +9166 +15303 +2189 +8838 +18492 +2680 +6426 +10697 +12333 +1503 +10769 +4426 +1744 +1667 +9736 +13034 +3518 +14252 +39 +19635 +7834 +19081 +8712 +6510 +2262 +18516 +16549 +18310 +1487 +6143 +3010 +8938 +8199 +19665 +17116 +12405 +9583 +9768 +14913 +7445 +7284 +1648 +17852 +16507 +19845 +8220 +7633 +11504 +16816 +12246 +9138 +19042 +2111 +2103 +8101 +4376 +19343 +17733 +9891 +11721 +14944 +13011 +7233 +13709 +7497 +8891 +18775 +10374 +9212 +9214 +18577 +8814 +11918 +3865 +3897 +8919 +18946 +18640 +12360 +17318 +8739 +383 +16762 +3873 +18292 +7537 +8677 +15824 +18232 +3194 +5787 +12028 +15332 +290 +12153 +1439 +5179 +16210 +19941 +13131 +11859 +11461 +17375 +14773 +146 +17014 +15061 +33 +9267 +16428 +9417 +12916 +9472 +1670 +19143 +16099 +5207 +8894 +11279 +16501 +361 +17180 +2569 +18449 +19718 +500 +19966 +3750 +4579 +16723 +8076 +4322 +18058 +17470 +4795 +13477 +5350 +6955 +5634 +14198 +3537 +13970 +17670 +6253 +15321 +10971 +328 +13640 +18502 +17659 +17541 +8991 +5567 +2298 +11490 +12821 +12527 +1208 +4152 +8764 +19745 +10821 +12727 +5492 +7025 +4223 +6156 +2073 +2799 +12353 +15820 +11932 +9629 +28 +8407 +9738 +11246 +13471 +12611 +11138 +6945 +6367 +7170 +7942 +13479 +4743 +8982 +8853 +6547 +12207 +19828 +3952 +8217 +538 +16993 +10727 +19123 +19118 +18574 +19974 +16464 +13631 +16828 +206 +13190 +11750 +658 +7879 +10083 +15459 +17922 +1443 +17613 +18045 +3329 +2714 +2587 +11538 +153 +10941 +11061 +2827 +2841 +15304 +13414 +3825 +4737 +1070 +4366 +15585 +7775 +2006 +19929 +1458 +7208 +15862 +9717 +7930 +10688 +18001 +9434 +4298 +1216 +7871 +15060 +15934 +9575 +12632 +549 +3870 +16910 +5007 +7226 +7174 +9203 +139 +4623 +12201 +15495 +18009 +3487 +8800 +13607 +4725 +1879 +8289 +10371 +19138 +16200 +3373 +6998 +16566 +15488 +837 +14529 +18170 +4815 +8308 +4611 +9564 +11694 +9410 +15106 +4664 +10041 +1510 +10835 +11841 +19533 +18327 +13441 +10486 +91 +4405 +9995 +1343 +17137 +13673 +19595 +10005 +14806 +18703 +14991 +17962 +3464 +11582 +13716 +14196 +7741 +8581 +1460 +18501 +5019 +10733 +1780 +4934 +9157 +19518 +7796 +15493 +13257 +10092 +1524 +9415 +7140 +18177 +8151 +4263 +7494 +3088 +10876 +19832 +16850 +6829 +2498 +18806 +4042 +15615 +19469 +4858 +18851 +13180 +11382 +13199 +533 +1464 +17678 +8929 +12911 +8209 +13900 +8388 +950 +11406 +11319 +1895 +9430 +199 +7061 +11799 +9794 +19258 +11861 +4923 +13227 +12434 +18900 +13917 +7992 +9379 +18004 +9398 +15494 +13617 +13277 +8486 +2229 +6800 +17394 +7196 +132 +17662 +8066 +11308 +18061 +7760 +2653 +19492 +4476 +11177 +17404 +1649 +12086 +5101 +16246 +15982 +16860 +2301 +17193 +14664 +15780 +3309 +85 +14415 +15586 +10800 +8965 +9384 +2047 +3118 +13551 +13157 +14022 +6192 +7371 +12258 +5589 +2697 +13140 +1495 +10028 +17589 +5970 +19410 +14039 +10096 +19607 +7758 +4702 +14821 +7755 +15005 +11592 +16602 +5062 +6178 +15634 +8306 +5969 +9557 +6840 +11698 +2913 +6519 +5613 +16986 +1618 +11728 +16964 +9827 +19768 +16351 +16081 +2041 +20000 +19840 +425 +16736 +5961 +9537 +7053 +7328 +18140 +17990 +5379 +2408 +16744 +10723 +13309 +18235 +532 +19810 +10423 +149 +5716 +10557 +8693 +6640 +16555 +4974 +16798 +11157 +7332 +17008 +9126 +4525 +6186 +15576 +8913 +8659 +11829 +4365 +1107 +1802 +9291 +197 +5368 +12778 +9264 +15612 +11287 +18333 +3699 +19486 +6968 +11494 +2145 +13340 +5803 +13996 +14826 +17842 +2556 +13613 +3740 +12562 +2313 +14665 +13324 +19985 +12914 +19182 +8045 +10325 +18876 +18386 +5056 +18655 +19922 +4245 +5120 +15460 +9364 +12080 +17101 +8511 +14332 +4612 +4782 +8532 +15298 +10424 +757 +8839 +5752 +16955 +2341 +3642 +5541 +5034 +4059 +12266 +18647 +8816 +781 +13267 +12622 +10514 +18828 +3037 +14938 +1907 +187 +17248 +16877 +5664 +18680 +16663 +15362 +10546 +8207 +771 +13824 +7488 +17846 +5690 +19954 +18339 +2900 +2687 +6356 +3583 +11656 +6225 +13465 +7291 +9296 +18139 +14052 +18149 +17484 +11145 +2560 +11009 +10813 +10828 +4193 +1624 +6162 +8175 +17588 +11955 +18029 +10115 +1322 +14889 +4515 +4691 +17524 +17787 +6618 +1481 +7508 +16484 +12415 +9174 +18111 +14062 +2897 +7417 +11700 +7743 +16106 +6174 +2279 +7390 +13408 +177 +4280 +14347 +12790 +9561 +16586 +14129 +16766 +9003 +14523 +10123 +12005 +15650 +310 +9924 +19436 +10417 +15540 +75 +8871 +10116 +15314 +15818 +14055 +16217 +7665 +728 +13464 +7945 +2752 +18395 +16814 +11817 +6977 +18240 +11923 +980 +15882 +17301 +19982 +15905 +14945 +13099 +7263 +14413 +6051 +7803 +4142 +2902 +2056 +2865 +799 +11741 +15907 +587 +14784 +10686 +336 +1854 +225 +142 +3592 +13961 +10503 +10488 +19160 +12375 +911 +2009 +2042 +17563 +8723 +17424 +4129 +11951 +5546 +13500 +11456 +14142 +13666 +9418 +18587 +15896 +9082 +5196 +18320 +19615 +17551 +9064 +10565 +4500 +14585 +16436 +12483 +1840 +17899 +9362 +9265 +5914 +11898 +11167 +9854 +4034 +5603 +8410 +3760 +19308 +17423 +16598 +15754 +8761 +6956 +3490 +3789 +12964 +13390 +10810 +12432 +3838 +3940 +12895 +15567 +15108 +7453 +8940 +5738 +11901 +6198 +3397 +14695 +16878 +5909 +13232 +17926 +3679 +3238 +7424 +15736 +5806 +19995 +7589 +88 +9062 +8669 +19248 +11074 +12419 +3215 +4120 +3170 +16863 +1817 +522 +4447 +13208 +13705 +6716 +6302 +10037 +6181 +17873 +6654 +16859 +6287 +19237 +16149 +1923 +891 +12891 +18735 +9451 +15936 +9271 +2738 +4239 +18558 +18238 +15856 +19092 +14689 +17996 +6589 +9790 +3509 +1147 +11270 +15614 +5257 +1000 +16135 +6852 +1573 +584 +126 +17694 +16945 +12143 +18584 +17415 +2443 +7664 +1929 +14887 +3007 +1571 +5508 +18113 +18537 +19310 +14948 +6219 +19055 +9327 +19233 +14977 +19158 +17198 +18438 +1297 +19463 +9834 +2185 +289 +2174 +3279 +18722 +3739 +5981 +18889 +8717 +6870 +12827 +16306 +6255 +15222 +3175 +10656 +8059 +5289 +6052 +10126 +3560 +19019 +19074 +12548 +5330 +15107 +18956 +5288 +4637 +6821 +11801 +5088 +5452 +2576 +2834 +4381 +13668 +11780 +11804 +5424 +14355 +6981 +2104 +16546 +8161 +10689 +18147 +15017 +12454 +18385 +2188 +343 +4839 +18811 +14270 +2967 +8460 +4386 +9252 +16823 +11793 +10022 +10653 +19374 +8698 +457 +894 +7594 +18736 +19646 +10901 +18689 +11811 +8414 +7155 +12829 +1658 +14408 +15804 +6374 +9964 +12155 +12140 +10759 +4932 +11621 +6410 +5931 +18136 +14831 +7458 +17869 +11119 +3141 +5364 +19699 +8658 +13235 +17915 +5036 +16390 +15583 +17150 +11454 +9910 +7687 +5442 +18189 +3206 +11422 +1501 +11769 +12845 +13812 +8449 +9623 +5164 +2168 +16268 +6984 +4464 +3909 +5980 +18325 +16419 +4709 +9107 +19421 +8760 +5287 +17942 +4958 +12387 +19304 +17419 +3734 +6524 +4895 +7821 +1716 +1546 +14829 +16990 +11678 +8834 +5479 +4444 +9386 +18126 +5303 +17539 +10729 +6092 +8165 +15001 +13300 +8236 +9150 +8047 +10551 +23 +3545 +1274 +11937 +8542 +7556 +10419 +10356 +17900 +17353 +9499 +3443 +1009 +19184 +2166 +3286 +4697 +17062 +8110 +18209 +10401 +4301 +1332 +10394 +6138 +12113 +15988 +518 +8851 +5053 +17620 +8265 +14409 +3035 +6495 +9881 +10823 +11887 +16272 +12427 +7411 +16009 +11610 +1774 +19133 +16576 +7034 +12846 +491 +3263 +10455 +12707 +6398 +18204 +11277 +18709 +7026 +1548 +1872 +4779 +10606 +10162 +14620 +9721 +11912 +2666 +9061 +15600 +13962 +3960 +17738 +10266 +18363 +296 +15379 +14978 +5068 +19585 +5063 +15742 +7960 +9703 +18496 +16483 +6962 +16024 +5847 +18880 +17082 +16627 +1158 +8713 +6611 +10861 +8002 +14392 +13153 +2005 +9893 +9373 +2981 +2404 +14315 +10758 +12193 +19139 +1109 +7555 +12242 +10900 +19676 +5433 +17606 +12966 +13381 +11096 +11559 +7201 +11098 +6936 +9796 +1639 +3491 +4075 +1847 +13108 +12556 +1138 +19437 +5810 +10578 +8128 +11076 +356 +14980 +16203 +17092 +6511 +11810 +2248 +7762 +2859 +19566 +15143 +16479 +7911 +334 +15087 +15355 +7859 +19225 +4031 +10309 +1850 +14217 +15213 +12197 +15837 +6239 +9542 +10685 +8347 +18355 +5353 +13973 +19705 +13486 +12640 +18286 +7935 +15308 +19663 +3828 +9048 +2102 +4413 +15773 +18959 +5736 +11906 +10121 +7106 +12519 +14813 +8283 +17995 +18245 +17528 +9664 +17514 +12975 +19180 +10776 +5813 +8248 +10809 +5714 +18275 +17511 +3266 +6741 +18541 +15913 +11962 +17903 +16954 +11969 +10747 +10817 +4768 +12361 +4644 +1078 +10225 +15292 +7541 +16364 +8121 +10849 +10508 +12672 +13805 +12359 +3415 +105 +12544 +2669 +6109 +8445 +10024 +1011 +13058 +12643 +25 +10913 +18623 +4593 +3931 +12 +2221 +3544 +15357 +193 +8483 +7874 +4218 +19784 +17358 +18553 +9388 +13395 +10285 +7980 +15753 +13612 +14110 +8226 +12962 +9413 +2444 +3839 +8799 +8120 +14653 +1806 +6353 +16080 +4093 +7975 +16968 +2983 +11979 +11746 +11862 +5064 +8423 +698 +10196 +18712 +5556 +17463 +16295 +14169 +6756 +4706 +6466 +12484 +6914 +5041 +444 +9153 +10742 +9094 +6229 +14835 +1788 +8358 +17297 +8194 +15854 +19056 +5225 +12367 +3178 +7933 +18853 +13392 +12610 +5908 +11284 +15829 +358 +8681 +243 +9363 +17255 +15461 +15770 +8428 +9848 +9961 +2207 +18578 +18187 +4603 +3604 +1631 +11597 +18360 +15034 +17912 +4234 +4361 +4811 +2907 +2663 +4064 +5274 +10457 +7662 +13062 +2525 +3350 +10254 +8331 +18062 +10376 +5816 +14534 +3204 +4854 +15587 +13195 +3990 +14365 +4017 +18262 +5611 +11794 +13077 +9691 +12285 +13835 +10349 +19198 +11448 +15544 +1689 +13986 +16431 +1164 +366 +19223 +9911 +14965 +3609 +9947 +1315 +19685 +18734 +11761 +6602 +511 +18486 +17360 +19869 +18089 +14054 +19378 +19515 +664 +13338 +12624 +19588 +13516 +15006 +19774 +717 +19102 +18654 +17433 +4752 +3812 +10509 +14495 +679 +5734 +13129 +12281 +1133 +3697 +7499 +18278 +15640 +10425 +15827 +8545 +18831 +13749 +17888 +10938 +6204 +18059 +5646 +11803 +13074 +10833 +14143 +11005 +9293 +12834 +1786 +4866 +19249 +9836 +16392 +9181 +13855 +13176 +9478 +701 +9132 +1542 +8862 +12341 +17309 +18661 +16443 +4640 +14857 +8290 +17283 +5496 +14242 +8793 +4576 +4679 +11889 +1438 +4925 +10355 +3819 +6376 +2532 +13526 +14322 +10347 +18854 +5956 +19423 +15665 +8915 +4663 +10692 +19644 +15832 +18590 +8621 +13148 +1924 +17794 +9825 +15755 +10445 +19190 +17758 +14146 +6411 +2590 +8168 +15901 +3581 +6440 +15382 +14036 +13349 +6535 +6592 +13203 +3161 +4546 +8444 +17303 +18984 +15002 +8550 +19625 +19884 +233 +7041 +19499 +18047 +4468 +17987 +13641 +10589 +17550 +7576 +7093 +19113 +1199 +1380 +9182 +12319 +19033 +1001 +17730 +13731 +7802 +19430 +6872 +8684 +6579 +8562 +6561 +11864 +6199 +15051 +15499 +11065 +18566 +5996 +13174 +4025 +7480 +15136 +17700 +3085 +5949 +7681 +9905 +11608 +13104 +17189 +7514 +3618 +3353 +17233 +8916 +15953 +18436 +13280 +5084 +7360 +5594 +6404 +6187 +10143 +3949 +14028 +4990 +2245 +13736 +17844 +11748 +15719 +17244 +876 +3124 +15721 +14763 +5663 +11000 +18467 +6098 +17920 +2939 +6864 +7719 +2765 +5726 +16995 +13325 +11705 +18849 +8292 +3368 +13861 +16438 +6196 +17304 +18376 +4872 +1275 +2509 +2557 +2340 +7021 +2650 +1794 +12043 +14059 +9408 +18205 +18720 +13010 +17627 +11010 +17859 +17921 +10917 +12656 +5336 +8553 +6975 +4383 +4116 +14693 +11771 +6093 +26 +9224 +14712 +17335 +17173 +9586 +17776 +1680 +10825 +4244 +15878 +8628 +17165 +3066 +818 +7383 +15126 +18393 +10612 +15174 +5763 +12617 +14167 +5003 +3096 +13585 +1448 +9831 +3218 +16882 +15123 +18705 +2600 +15032 +7241 +11132 +3297 +13040 +18608 +9974 +14192 +13958 +7070 +8843 +7418 +4296 +4012 +5566 +16074 +5409 +5514 +14423 +10210 +9240 +10558 +8792 +4174 +13021 +2508 +8744 +3624 +9826 +2099 +2450 +4997 +8300 +9202 +7290 +8354 +9455 +11114 +7824 +18422 +16705 +14690 +5329 +6739 +15794 +1497 +2426 +5643 +15341 +4695 +17307 +18641 +16288 +6546 +3326 +1076 +6025 +19048 +5454 +11089 +10899 +16578 +1260 +17090 +2870 +11028 +12158 +13954 +17078 +18718 +16446 +9290 +4775 +1233 +19424 +15685 +18182 +11836 +5741 +18378 +11354 +1724 +11609 +10093 +5968 +19296 +16590 +3108 +10896 +12243 +11 +14765 +11872 +12395 +11295 +17536 +18464 +5233 +995 +13449 +11846 +427 +4382 +11744 +12932 +18627 +1129 +7773 +1205 +15155 +12680 +10125 +16326 +6385 +13849 +11225 +3097 +16889 +399 +10042 +15394 +7503 +3511 +3026 +16831 +2012 +7969 +15158 +13916 +15538 +19165 +15629 +3383 +1615 +4608 +8345 +2181 +15602 +2985 +1206 +13910 +17242 +9573 +10789 +13366 +9324 +627 +10717 +18922 +2137 +6038 +8617 +14878 +1819 +12227 +16619 +7412 +15689 +13830 +14674 +15857 +18942 +8806 +3933 +5087 +17243 +17602 +4797 +9471 +12786 +6785 +15817 +12382 +5942 +13423 +657 +766 +12638 +15369 +2293 +4261 +4313 +19519 +13182 +13050 +294 +11471 +9368 +3217 +8225 +3714 +416 +12791 +19712 +11496 +10593 +1969 +1261 +9453 +5450 +2264 +1520 +17027 +14102 +8043 +16588 +17379 +16002 +7426 +4960 +1687 +12676 +17614 +7422 +2558 +10843 +16071 +16109 +11505 +9696 +2708 +15539 +15414 +560 +12428 +3582 +9599 +12300 +404 +5525 +7065 +19368 +15657 +14440 +15111 +13909 +3300 +10929 +13225 +17587 +4320 +2199 +5742 +11038 +4081 +7977 +12058 +3130 +1240 +13057 +11639 +5235 +8789 +2850 +5839 +19562 +8872 +17666 +18377 +10257 +17356 +18091 +7705 +215 +9917 +7102 +18268 +16976 +9009 +16032 +7962 +7666 +15885 +7128 +14114 +17499 +12351 +7184 +1280 +9320 +12328 +19482 +11432 +8508 +6719 +16981 +1659 +14253 +13553 +17390 +8635 +10945 +13402 +1201 +15144 +18331 +1181 +5335 +10864 +16844 +11055 +4745 +6403 +13747 +1579 +6133 +8077 +7402 +17330 +10048 +102 +838 +6401 +12303 +14218 +9804 +17107 +13621 +10206 +881 +11995 +17268 +15072 +12536 +9458 +18251 +8868 +13994 +13206 +15980 +17688 +13754 +1299 +17566 +15345 +6375 +3924 +17069 +5004 +14362 +9065 +18043 +714 +18225 +15089 +3084 +287 +11080 +7256 +7701 +18466 +15354 +11668 +3553 +14395 +19915 +18786 +825 +19835 +13575 +1831 +247 +2016 +4286 +8007 +12230 +1643 +3260 +1430 +19238 +16397 +11291 +17482 +18813 +12673 +11067 +1375 +13775 +16079 +7496 +6538 +8212 +10677 +12710 +15102 +1845 +10450 +11695 +3706 +8988 +16641 +3005 +5552 +11908 +6518 +18416 +18560 +14484 +10812 +311 +6700 +2489 +4814 +6725 +8462 +8875 +8512 +6328 +7243 +15055 +18698 +12542 +10775 +5152 +5131 +5549 +16658 +17703 +10351 +15186 +9407 +13696 +14357 +4714 +18871 +6300 +5477 +222 +5256 +14735 +9200 +16765 +4856 +13549 +1656 +18619 +13966 +2214 +5952 +796 +8537 +3193 +5094 +16830 +11210 +11692 +11994 +8379 +17603 +2092 +13879 +18172 +15026 +4179 +16301 +19341 +9136 +11273 +5930 +7936 +5398 +14274 +18913 +13555 +14098 +1330 +2296 +12950 +16616 +11661 +2529 +6732 +14431 +12913 +13222 +3720 +11359 +18399 +9565 +18706 +17075 +12089 +17130 +2672 +5208 +15159 +9660 +3923 +2693 +16745 +2934 +9287 +13990 +4122 +17963 +6070 +2628 +19739 +6648 +8719 +17515 +11774 +15489 +14652 +5338 +4892 +3433 +4484 +11632 +9010 +3033 +8230 +3200 +3977 +17847 +8202 +18675 +1119 +10056 +1413 +7573 +14341 +1036 +17108 +12098 +4137 +3651 +13379 +7904 +10641 +14976 +2180 +10944 +19356 +473 +16989 +8832 +15224 +6890 +2891 +2126 +4276 +11488 +12117 +6388 +4027 +8068 +409 +17687 +18778 +4822 +414 +18290 +3038 +6865 +10262 +14837 +10141 +9792 +13545 +18885 +4761 +4475 +3529 +19217 +19693 +3348 +7212 +18332 +7372 +12269 +3032 +11697 +16051 +13558 +2718 +19082 +6046 +11137 +6485 +9052 +14647 +13333 +5828 +4397 +7386 +8605 +5651 +6024 +5189 +16345 +15434 +14725 +4514 +11023 +13382 +7578 +13239 +4532 +15295 +11892 +9545 +2152 +14703 +18904 +8531 +15223 +18084 +4524 +74 +367 +3302 +15187 +17948 +5692 +6128 +6172 +862 +1800 +4684 +9414 +11033 +13573 +6000 +9615 +17476 +9059 +4805 +17054 +8582 +11549 +5650 +12626 +14308 +2238 +11714 +566 +5316 +2751 +4733 +970 +18769 +12843 +4658 +6209 +8830 +18171 +19497 +14780 +3730 +2661 +12284 +7012 +12277 +10008 +11933 +3142 +18428 +3667 +161 +12152 +12630 +18370 +7939 +2046 +3771 +16803 +13993 +2129 +14608 +18839 +12060 +16404 +11870 +18923 +10698 +6659 +2352 +1561 +4226 +14741 +17692 +9649 +5073 +15142 +19353 +6009 +3240 +5344 +10170 +4550 +17907 +11679 +7651 +6796 +397 +7148 +12621 +5558 +3054 +14049 +2090 +13856 +12317 +10660 +2514 +14338 +10482 +19202 +11648 +16403 +1335 +19574 +16665 +11989 +4482 +16313 +4481 +17290 +3742 +4407 +1528 +12901 +7516 +15834 +10231 +4945 +11337 +12609 +5516 +17039 +18026 +11509 +15831 +6085 +976 +18368 +2249 +17712 +16657 +2085 +1054 +1880 +6681 +8094 +9636 +18833 +14291 +7659 +11754 +19714 +17347 +1565 +18074 +7791 +6470 +2289 +10027 +1934 +12826 +19295 +17179 +6112 +15008 +17860 +9711 +12338 +18888 +9216 +17573 +15741 +6557 +11821 +3785 +13764 +14853 +5129 +15446 +1243 +2836 +11920 +4115 +19743 +1410 +16817 +18379 +3104 +5661 +13223 +4483 +2670 +945 +17124 +4863 +13476 +18169 +15448 +18006 +19349 +8378 +1572 +12920 +7204 +4230 +19243 +9426 +12260 +16377 +10194 +2825 +4908 +11368 +3711 +19583 +17367 +292 +12903 +16330 +5391 +13254 +494 +16445 +13690 +7236 +15348 +14251 +9734 +13249 +16374 +11516 +7995 +5557 +5923 +11866 +7832 +5799 +19500 +8329 +18652 +19034 +5186 +14753 +1061 +3947 +5497 +4861 +9169 +15018 +12213 +2179 +13884 +5766 +4358 +14866 +3880 +19470 +14598 +11652 +10155 +19732 +13138 +4287 +131 +16430 +7010 +4214 +12811 +1373 +9830 +13243 +3291 +12009 +14404 +3379 +12855 +1414 +13853 +14429 +4053 +3056 +4022 +9318 +7637 +3941 +5417 +16719 +8978 +16432 +19505 +12738 +1730 +16789 +18145 +18371 +3497 +8119 +7470 +17800 +13870 +18571 +6554 +11431 +14445 +7197 +1933 +15623 +12777 +7361 +13844 +143 +10883 +14593 +11806 +2645 +17512 +6887 +18030 +3128 +4333 +8911 +10198 +12129 +5486 +8823 +17547 +14551 +11753 +10950 +16379 +8214 +2067 +18622 +17927 +14904 +4844 +14790 +5246 +12162 +10631 +649 +15091 +14794 +11946 +6781 +17949 +15902 +6055 +8503 +9719 +7506 +15682 +4291 +15994 +13645 +9256 +4156 +12447 +7339 +3122 +3601 +19670 +15202 +14577 +17212 +6424 +6674 +4651 +2075 +8017 +8278 +12156 +8651 +5077 +1480 +3701 +4011 +9612 +17701 +12062 +19899 +15976 +12877 +14421 +18792 +6460 +12295 +13661 +3963 +3847 +12668 +5080 +16134 +2026 +12629 +10804 +10771 +12274 +214 +4299 +9503 +19905 +6019 +2035 +6059 +17521 +5218 +16809 +536 +6746 +12068 +9279 +19432 +15122 +10060 +10991 +6585 +1998 +10142 +3087 +11964 +1525 +1860 +11818 +2219 +14103 +13693 +5011 +7139 +13527 +5879 +7955 +9316 +16697 +8869 +11251 +1534 +4284 +733 +17527 +5569 +14351 +17418 +15542 +14860 +9767 +13284 +15301 +15678 +15219 +17311 +11024 +5699 +4445 +12438 +13540 +746 +6888 +19798 +6721 +1389 +145 +4078 +8049 +15621 +6921 +12862 +14073 +18173 +10683 +5789 +18085 +2374 +16355 +14745 +12662 +4894 +8104 +12685 +6399 +13724 +10841 +18069 +9887 +2652 +11241 +18298 +8535 +8474 +16926 +9249 +7268 +6697 +19167 +1891 +11305 +8575 +18527 +6213 +8178 +13619 +19999 +18898 +13321 +10640 +8731 +7009 +7686 +2565 +4069 +12508 +15261 +2480 +18716 +17238 +15868 +8197 +19619 +14934 +18585 +10536 +767 +13067 +8857 +18953 +16893 +13144 +19008 +4964 +9658 +4364 +9335 +8459 +379 +18910 +3409 +6979 +1409 +4906 +15490 +630 +2038 +2139 +5842 +6783 +696 +18035 +4543 +14226 +19690 +5147 +9021 +6036 +1128 +4325 +13201 +18119 +10323 +9701 +9494 +10550 +18311 +15527 +19917 +1122 +3407 +19589 +4884 +11907 +196 +19568 +6717 +13372 +13179 +16909 +14987 +6436 +14334 +5811 +15522 +1797 +3359 +15798 +8064 +7523 +11405 +15664 +8189 +14549 +19802 +2658 +3425 +14066 +15800 +11281 +16488 +9329 +8763 +10973 +3798 +5228 +3687 +13819 +9967 +3652 +8239 +8705 +8318 +10694 +7897 +9937 +11380 +17016 +10220 +12399 +17621 +1650 +17071 +14603 +15735 +7510 +3269 +17403 +4318 +3343 +2797 +3306 +3109 +5897 +3526 +9286 +14601 +10375 +16691 +10859 +16013 +14381 +1922 +8004 +7460 +19022 +10605 +14881 +8109 +16389 +9692 +435 +5 +1713 +7550 +12168 +4808 +9708 +9148 +4876 +16569 +6508 +13657 +17582 +1709 +1232 +16140 +4037 +17249 +14610 +1293 +8356 +7380 +8971 +14378 +19185 +16906 +15180 +14222 +3003 +8038 +17821 +11942 +18488 +10903 +11667 +608 +2157 +12806 +8649 +3983 +16528 +14 +15311 +17327 +13842 +19256 +16648 +184 +9485 +15858 +1309 +4362 +16655 +17745 +13184 +9441 +4408 +16967 +19195 +16827 +16671 +9188 +3824 +18471 +6317 +14144 +13056 +10402 +1108 +18707 +4121 +16297 +11827 +18002 +19301 +16290 +10822 +7114 +17389 +534 +2364 +1159 +17258 +18341 +6967 +13564 +2743 +6768 +8031 +18141 +363 +18456 +14654 +7884 +19790 +8115 +12111 +8521 +12506 +7756 +7607 +5973 +8925 +18952 +11686 +6895 +5100 +9731 +3107 +7847 +1580 +4143 +2344 +7721 +7342 +13237 +6268 +12144 +4032 +10943 +4063 +565 +15758 +18159 +1539 +14732 +8563 +18624 +7076 +9595 +13354 +4046 +7679 +5455 +14993 +13154 +4082 +2148 +10549 +12991 +5394 +4182 +12226 +16029 +19740 +16292 +4952 +19380 +10330 +10990 +17685 +6678 +6527 +18455 +17874 +9650 +18494 +3807 +2346 +6256 +11483 +1874 +14111 +10073 +10328 +692 +10003 +10563 +9710 +8137 +3685 +16689 +129 +6753 +5559 +12231 +1290 +4105 +8819 +5636 +14720 +5296 +11586 +16631 +7615 +11047 +14797 +7355 +1883 +14151 +8546 +15245 +11203 +1408 +11596 +1398 +4197 +14191 +7156 +16091 +19409 +1633 +5277 +4310 +6226 +454 +9288 +5453 +9412 +3163 +5238 +10146 +6151 +2424 +4145 +18612 +16892 +19907 +5693 +15343 +12492 +13020 +965 +4898 +17448 +4652 +10150 +17739 +16189 +4860 +4473 +8136 +8583 +7726 +1771 +2539 +8935 +12164 +12192 +11527 +5119 +14890 +9766 +2570 +16100 +9570 +14602 +16325 +7391 +6299 +14230 +8447 +5740 +6443 +14046 +17740 +10235 +19488 +13597 +2875 +17644 +16773 +1058 +12614 +13906 +16543 +16394 +14939 +14905 +11468 +18827 +18564 +19462 +13002 +14430 +13677 +9396 +5223 +16987 +11600 +8430 +6621 +13135 +12534 +4158 +10596 +14768 +3334 +16593 +7267 +82 +1042 +13700 +13928 +3593 +2728 +17783 +13616 +18860 +16477 +1884 +3341 +4455 +8523 +5360 +6744 +9787 +712 +12234 +64 +9539 +6601 +14071 +18366 +5169 +15012 +16284 +4748 +2977 +5519 +8456 +7158 +8475 +9258 +16579 +8251 +3106 +10428 +19736 +1630 +16367 +4317 +15059 +16195 +7278 +5500 +8624 +6245 +18284 +12479 +4221 +17256 +17119 +135 +9422 +8909 +7927 +10264 +13017 +7051 +12580 +4089 +192 +11988 +6610 +19422 +8392 +17224 +5795 +5322 +1995 +6348 +9236 +4495 +6127 +19207 +16255 +4836 +9538 +19629 +14271 +11400 +3575 +18824 +4141 +15209 +13699 +2686 +14671 +6450 +13634 +18974 +19253 +14865 +16388 +18247 +1431 +12762 +7066 +5608 +5924 +6989 +14656 +9274 +6333 +15028 +6261 +1608 +11467 +19389 +13533 +1491 +3458 +1015 +2794 +8610 +18369 +7108 +7727 +10071 +19320 +8491 +12090 +8934 +3015 +9067 +16834 +4963 +14605 +18596 +2405 +1991 +6503 +15378 +8231 +17919 +4285 +2496 +555 +793 +2366 +13341 +18687 +1202 +6599 +17973 +11949 +9640 +5875 +12414 +14414 +6094 +10920 +13905 +18533 +10406 +13899 +8976 +1188 +7873 +19188 +12857 +16770 +10304 +18077 +9698 +17593 +12390 +11536 +9862 +5744 +5033 +13998 +5743 +1031 +10481 +9928 +5362 +5057 +5149 +18682 +5295 +2131 +5474 +8808 +3726 +14455 +4422 +12459 +8650 +17260 +14754 +15993 +5910 +18194 +19914 +4904 +11235 +6712 +2430 +9566 +7643 +19651 +10145 +19816 +18051 +15056 +6195 +2502 +12838 +259 +8380 +13378 +5311 +5826 +19153 +13762 +14531 +12340 +19532 +4780 +15970 +5958 +17384 +15415 +11795 +9404 +7356 +11945 +8321 +10830 +321 +18044 +5786 +6319 +12251 +10016 +2739 +7976 +15319 +4319 +4911 +5599 +12753 +11303 +1210 +10440 +11546 +6635 +18617 +19729 +662 +18724 +16559 +17829 +8114 +19373 +14057 +16199 +5306 +2419 +3513 +17503 +3405 +1569 +15166 +3577 +15124 +9460 +3982 +12280 +18257 +5138 +15084 +5883 +3655 +18036 +18505 +11417 +7229 +11943 +19333 +7107 +19524 +4591 +754 +17184 +9066 +5834 +11322 +8985 +3984 +13421 +3023 +13334 +18132 +2854 +7330 +5410 +18118 +4630 +6441 +12951 +7407 +170 +12047 +5117 +683 +3403 +5236 +48 +14544 +18723 +2059 +816 +14524 +6826 +17732 +7325 +17945 +14759 +16490 +12132 +8464 +7878 +12376 +13459 +304 +11191 +13578 +3797 +16282 +3876 +14933 +9528 +3901 +16331 +429 +7880 +2484 +3398 +19582 +1596 +933 +9747 +18174 +2976 +12715 +1793 +8877 +2291 +2237 +6528 +16638 +6294 +279 +16974 +11660 +14995 +6694 +8041 +10029 +7527 +4754 +4013 +671 +7549 +18405 +12204 +9904 +11927 +11064 +7059 +14336 +4228 +15510 +1463 +1763 +17946 +1144 +14279 +15274 +9282 +16781 +17761 +14361 +4040 +9075 +19980 +4399 +13015 +8746 +17579 +17764 +18041 +7678 +3115 +14892 +13945 +4227 +1014 +5415 +570 +18354 +18808 +19142 +6619 +19507 +15353 +1381 +10597 +14597 +11939 +11383 +4369 +19836 +18420 +11034 +15451 +2912 +13181 +17693 +1844 +14101 +6400 +4602 +813 +17818 +14729 +13534 +10620 +18667 +3751 +15942 +9006 +10752 +11965 +13946 +2648 +11486 +19726 +7409 +11815 +15088 +1116 +5694 +1914 +433 +14816 +9631 +17106 +7899 +14328 +6912 +485 +16628 +11183 +17172 +7634 +10806 +5383 +16801 +14746 +19564 +19742 +13883 +13397 +15364 +1255 +2775 +12225 +7235 +19495 +3944 +17472 +1581 +19200 +7814 +3585 +19346 +14017 +899 +18850 +4157 +16396 +16035 +8732 +13932 +17177 +13345 +19379 +1145 +897 +17560 +4224 +16014 +16988 +4311 +13296 +1552 +10120 +13326 +5962 +16790 +19631 +10321 +10996 +4099 +17222 +7567 +18442 +19656 +6238 +19874 +16230 +12050 +2236 +11548 +7056 +14571 +1382 +1642 +14526 +14518 +16550 +6581 +19885 +223 +7449 +1605 +16119 +10624 +15328 +2097 +10058 +6965 +5217 +1196 +11506 +4668 +9397 +4270 +18060 +11229 +7898 +7554 +13048 +13155 +15733 +4900 +1715 +12112 +3515 +16874 +9746 +42 +13347 +11552 +1110 +3879 +19871 +974 +7765 +6836 +3355 +124 +13405 +1469 +13989 +11169 +14533 +1105 +3168 +15740 +18925 +9135 +736 +13177 +1101 +13116 +19539 +19803 +11259 +10571 +93 +4170 +2136 +2149 +8315 +5835 +5146 +16683 +14300 +8141 +14570 +16242 +10722 +6364 +11082 +13458 +8339 +7965 +6334 +16884 +12771 +14021 +550 +8998 +51 +17976 +19806 +703 +1339 +4380 +8285 +9915 +9806 +16158 +3052 +18125 +1999 +18684 +13119 +2395 +8639 +19154 +2367 +19891 +5733 +14450 +8069 +17660 +16498 +997 +1941 +7448 +8520 +14805 +6120 +11676 +14629 +186 +17567 +5106 +355 +8831 +8257 +18066 +19011 +17346 +17940 +17746 +3905 +11148 +14121 +16966 +9385 +13780 +6134 +13248 +17871 +19839 +8060 +2276 +19749 +2901 +15838 +16115 +7095 +8085 +16777 +1674 +10712 +15745 +3953 +3837 +8037 +10572 +9635 +14477 +11699 +827 +5470 +5852 +9543 +1751 +5889 +18669 +16258 +9054 +17331 +13029 +12408 +4429 +10307 +15826 +14301 +973 +16123 +276 +16012 +9945 +18403 +15923 +17262 +15662 +9810 +9923 +7320 +8457 +16911 +10992 +14162 +12969 +10473 +16845 +16935 +3251 +11115 +8087 +12731 +9823 +5242 +7432 +10216 +18202 +10939 +17401 +15813 +3446 +3081 +18924 +15029 +16668 +317 +3012 +19552 +11206 +6558 +6081 +3628 +9662 +6419 +15498 +13943 +15513 +7818 +990 +6923 +68 +15508 +15161 +5127 +4534 +9165 +3951 +8100 +14187 +16434 +18842 +10691 +1620 +14783 +7801 +3241 +2583 +10907 +14552 +19695 +3846 +13358 +2516 +6664 +13523 +1545 +11097 +17269 +19369 +14156 +17245 +17640 +14426 +780 +17131 +11392 +4758 +7819 +19694 +6701 +6 +13947 +17021 +17924 +7401 +4138 +322 +13588 +10490 +8715 +8267 +6898 +10476 +5051 +17392 +3042 +14317 +17980 +6122 +13792 +12560 +11510 +15968 +13481 +17974 +2988 +14875 +4936 +10290 +6408 +3543 +8371 +13400 +19824 +6448 +92 +5547 +6228 +15940 +19420 +2314 +14093 +19285 +16682 +17955 +18802 +7961 +15630 +16023 +11819 +13723 +10369 +3833 +9724 +14906 +18783 +8086 +12179 +6687 +19870 +18518 +4144 +16795 +4781 +14676 +9051 +16876 +6409 +5315 +12644 +1062 +2581 +14210 +14092 +18300 +7029 +15641 +9977 +12109 +13322 +1312 +7861 +8218 +17468 +156 +12623 +14235 +16161 +18642 +16952 +15766 +5871 +7614 +18929 +6142 +12679 +18408 +15070 +1377 +12570 +13385 +16243 +17250 +18081 +19255 +506 +3915 +571 +12497 +4080 +19023 +8920 +6752 +7676 +4499 +4480 +16328 +16656 +14090 +8466 +9488 +2876 +11340 +19962 +7599 +136 +11041 +1976 +13485 +15259 +6786 +16277 +17096 +6086 +1155 +1056 +7694 +19490 +14437 +7592 +19787 +5475 +17600 +2226 +18997 +9883 +12463 +12711 +6354 +2538 +13032 +13256 +5251 +9382 +15900 +342 +60 +14505 +10564 +16440 +18611 +12910 +4570 +5290 +6246 +12646 +12124 +11903 +4114 +5877 +14619 +16366 +17310 +8269 +8063 +12941 +5439 +4672 +13660 +19265 +6164 +691 +5819 +19567 +3586 +12884 +463 +3722 +9383 +15725 +13096 +18398 +16383 +2272 +12977 +14807 +9041 +1445 +4237 +6946 +15317 +1607 +11531 +11336 +16343 +4271 +19812 +6220 +2839 +8725 +13567 +3746 +7737 +8132 +5393 +1279 +19012 +9170 +12350 +7839 +19813 +840 +3313 +7600 +12378 +19107 +5762 +4650 +17686 +601 +13133 +8396 +3794 +19 +13838 +3387 +5441 +12389 +15696 +10875 +12803 +16761 +5972 +5866 +15504 +17399 +8488 +2958 +18573 +8886 +16413 +5205 +13433 +10655 +661 +6616 +15515 +7253 +8342 +2612 +3134 +4165 +4972 +782 +1829 +994 +12931 +12936 +4155 +13115 +4503 +15575 +12267 +17414 +16263 +6652 +15759 +8463 +2156 +4001 +17493 +17236 +10519 +15734 +9902 +2899 +12783 +11925 +19649 +18825 +15683 +18897 +18155 +278 +13790 +6180 +14615 +5616 +9942 +12801 +15687 +5249 +6727 +11421 +16176 +15891 +16112 +3219 +19367 +10367 +12593 +2889 +6144 +650 +12616 +7037 +8346 +808 +18834 +15385 +2616 +617 +14786 +12559 +19508 +17657 +9012 +19716 +4107 +8946 +956 +12154 +4215 +11869 +16151 +17287 +18666 +5318 +6745 +10410 +1067 +8018 +5305 +1983 +18588 +12684 +8572 +529 +13503 +5510 +6185 +14035 +10441 +1183 +7232 +9933 +12188 +18404 +3853 +11560 +15524 +789 +10959 +5000 +6490 +8541 +16202 +544 +19494 +8642 +4562 +4776 +19919 +13538 +18005 +9880 +8858 +13826 +16786 +4957 +16752 +4800 +15603 +9596 +16896 +2578 +12051 +7294 +17473 +3669 +11445 +18692 +6846 +10007 +4470 +5649 +16365 +5376 +13628 +13590 +5188 +2250 +2390 +14215 +18135 +420 +1652 +13217 +7493 +12689 +7716 +13398 +4297 +18088 +12986 +7571 +13886 +2045 +18737 +18243 +18971 +3610 +14397 +3579 +15336 +6321 +6391 +1719 +17319 +17572 +15230 +6030 +17246 +9619 +12294 +2268 +15325 +9604 +14781 +17879 +3826 +4181 +15004 +12981 +11931 +3501 +12309 +19467 +16188 +8 +5761 +5047 +7931 +9004 +17186 +14911 +2842 +1834 +19606 +12927 +12540 +17812 +13687 +17206 +9098 +16417 +11704 +486 +2431 +17720 +11230 +4778 +19738 +8234 +1102 +15532 +906 +10229 +4889 +16302 +10964 +9673 +4084 +9912 +3422 +7104 +14634 +10845 +10107 +11378 +14685 +2387 +18342 +15699 +3608 +13286 +12217 +5020 +16120 +8543 +12979 +15796 +6285 +16456 +4969 +18845 +751 +6159 +16228 +2015 +3849 +9102 +849 +13417 +1778 +2108 +810 +6833 +17100 +4136 +4526 +348 +17187 +272 +5831 +16304 +12874 +7566 +4659 +3616 +9359 +18645 +3395 +8105 +1424 +14024 +18314 +18123 +2361 +17857 +2684 +2114 +8646 +10010 +17429 +12024 +10773 +6044 +9399 +16045 +542 +15225 +7062 +17619 +4266 +9973 +17451 +11670 +749 +9490 +5913 +5705 +19084 +13556 +7908 +8753 +1939 +5448 +15643 +11219 +6994 +5668 +7353 +19668 +18092 +2999 +15511 +18207 +2458 +9269 +18782 +14041 +19358 +18175 +7113 +5123 +11606 +13263 +17892 +16218 +17449 +302 +8544 +15266 +19465 +15546 +10679 +13494 +6645 +18063 +12033 +8167 +4509 +13501 +258 +18154 +12290 +955 +13412 +12081 +7255 +3542 +11523 +5713 +16957 +10784 +19886 +6200 +12116 +10209 +10858 +18213 +4051 +5413 +3900 +5108 +16709 +17534 +19460 +8025 +8304 +7176 +9374 +3312 +4346 +8566 +19106 +6582 +14715 +12146 +18011 +1930 +353 +4631 +212 +7994 +9144 +18080 +12526 +18229 +5619 +6973 +19112 +9953 +5365 +7482 +3955 +19937 +7042 +13816 +17966 +2216 +9590 +17203 +8882 +4918 +5865 +17565 +10643 +6811 +12356 +3950 +3632 +5804 +2244 +15663 +19481 +19534 +14651 +15785 +152 +13732 +2563 +13773 +11820 +12989 +7313 +16704 +8337 +19352 +7817 +2633 +15432 +8955 +18497 +15417 +8848 +4148 +3764 +10395 +14673 +2832 +6877 +15756 +19707 +4682 +8989 +9450 +2535 +9723 +6006 +12912 +2255 +521 +7730 +277 +15079 +8893 +11751 +318 +15397 +6416 +4171 +12817 +12345 +11092 +19364 +18796 +19477 +5517 +5241 +14964 +10554 +16073 +5680 +9393 +12410 +305 +4274 +4973 +7369 +15226 +5568 +12591 +2721 +9655 +11535 +9032 +8160 +10585 +16016 +7561 +8812 +9828 +5266 +1351 +5358 +7389 +3806 +18586 +12487 +15436 +4986 +9498 +12180 +4337 +15483 +8271 +11957 +16279 +9859 +15845 +1502 +11066 +6412 +2620 +3718 +14954 +19976 +10790 +4471 +4189 +18102 +18519 +14333 +12339 +17361 +8204 +4937 +1295 +19094 +868 +4859 +19365 +3413 +1775 +13156 +5936 +5390 +11015 +2632 +11963 +7828 +6039 +3276 +5656 +17665 +7501 +1080 +9674 +11243 +7922 +11042 +6307 +13892 +13940 +9482 +8866 +15118 +6576 +1784 +17951 +1901 +8366 +17129 +10200 +16885 +19240 +16170 +3948 +3835 +7631 +12396 +19611 +17295 +9348 +8686 +12530 +3715 +3212 +9841 +8213 +1604 +11849 +1684 +465 +10127 +6136 +10256 +1758 +13323 +1538 +17436 +2032 +12036 +8338 +10050 +17148 +14112 +15592 +5090 +13384 +15492 +15339 +13173 +6208 +4736 +18764 +18690 +18124 +1396 +18789 +9741 +18713 +10086 +16093 +4799 +19020 +1485 +365 +17407 +15632 +3063 +5342 +13070 +1329 +8878 +11152 +13787 +15979 +4891 +16398 +7218 +11289 +18249 +5469 +11418 +10870 +1887 +16429 +8675 +7986 +11159 +5387 +58 +9168 +16846 +17467 +9452 +17447 +16503 +651 +5458 +6370 +10169 +10063 +18820 +406 +17339 +17417 +1384 +19709 +12480 +9303 +2448 +5043 +6791 +3077 +6666 +19340 +17611 +10865 +9900 +11742 +12695 +17002 +14767 +11634 +17841 +19678 +9097 +11650 +1808 +110 +1770 +10863 +17491 +14461 +3759 +5698 +12149 +19093 +17735 +5231 +2271 +14514 +12100 +13467 +2066 +2445 +15481 +5126 +3149 +16664 +6392 +10399 +65 +7855 +7970 +18525 +6126 +15844 +5768 +18805 +9621 +15101 +5300 +19530 +5312 +4151 +13860 +19004 +1266 +10777 +5150 +4816 +5172 +252 +702 +5823 +5863 +11111 +11508 +13339 +3729 +10030 +9843 +2624 +17380 +8679 +3709 +8051 +9589 +3365 +3280 +18010 +15057 +230 +12690 +8969 +11484 +19218 +13348 +17118 +2100 +14262 +10255 +594 +19852 +7830 +19461 +5345 +5502 +6363 +2256 +13852 +17012 +2494 +19958 +4995 +15409 +6789 +3974 +10122 +1511 +7786 +1248 +8127 +13084 +2500 +18524 +19013 +5258 +15961 +3004 +5337 +523 +19968 +6275 +8662 +19779 +10343 +1977 +15801 +12321 +16162 +7341 +9281 +14796 +9310 +19512 +19405 +19782 +14145 +13741 +19751 +150 +11990 +11568 +9552 +1184 +11165 +2873 +17804 +18743 +14394 +16041 +15676 +8228 +15025 +8795 +720 +5934 +349 +16651 +11366 +19021 +424 +14289 +2211 +8771 +5009 +18528 +17697 +19558 +3653 +11674 +14124 +6360 +18317 +13045 +16862 +17105 +14501 +10910 +18357 +13959 +17312 +11254 +6699 +1163 +14463 +17133 +1379 +3424 +19235 +16506 +13474 +2966 +750 +6251 +17774 +6665 +11172 +16449 +18591 +11218 +15077 +11681 +7650 +1566 +16690 +971 +5472 +19354 +623 +5621 +14700 +9018 +15048 +10975 +12805 +2052 +19103 +16048 +12467 +6146 +11128 +13798 +16299 +5624 +11716 +15569 +12681 +19003 +14089 +5005 +17643 +18054 +8507 +624 +9764 +430 +5139 +12955 +17633 +18795 +1088 +19129 +843 +19819 +14469 +15666 +4582 +9920 +13681 +10066 +8065 +9648 +2202 +96 +7484 +1267 +690 +16179 +3182 +5125 +10811 +5724 +15850 +2222 +9046 +6423 +13733 +15684 +15392 +17495 +12863 +498 +10164 +18966 +12418 +6222 +9976 +288 +16999 +15426 +13872 +14451 +9547 +10515 +7344 +854 +7902 +2768 +15997 +19294 +17308 +12595 +18887 +2689 +6909 +12091 +12844 +3926 +6542 +15474 +18227 +201 +5498 +17574 +4109 +9572 +4954 +6101 +7505 +16538 +16793 +17192 +17517 +351 +5133 +2050 +1282 +18186 +11233 +14645 +19337 +6158 +3351 +914 +5701 +3136 +5414 +1420 +11999 +11489 +7586 +442 +7805 +3809 +3138 +8291 +13592 +16618 +16338 +2127 +3972 +5562 +9028 +14912 +5653 +4166 +3743 +7872 +599 +6954 +10668 +2845 +19100 +16617 +154 +11374 +6779 +14547 +13415 +17097 +18994 +8608 +14527 +2486 +1154 +17689 +7869 +15346 +2994 +13851 +7350 +5431 +19834 +13357 +5989 +17009 +10533 +2351 +14369 +2285 +15677 +15380 +1910 +13132 +5639 +4735 +3009 +12004 +14330 +12835 +7845 +19647 +4479 +7473 +9394 +11296 +5279 +7863 +10458 +3954 +8373 +5988 +1220 +461 +2378 +14855 +5317 +10956 +1916 +4531 +4698 +15306 +11094 +12270 +13226 +8061 +14314 +9906 +8102 +11665 +3427 +962 +4411 +9899 +4033 +2513 +18110 +12481 +6737 +3661 +13591 +1504 +5545 +17726 +11208 +2362 +17204 +12377 +13014 +15207 +4708 +2123 +18387 +10018 +10180 +3360 +13529 +972 +12769 +6091 +11867 +2515 +17622 +7430 +18493 +19051 +18031 +221 +9493 +18830 +106 +12860 +6790 +1446 +1412 +3044 +4097 +9986 +3684 +9728 +656 +8351 +10291 +19880 +9679 +3629 +15564 +7370 +13046 +5887 +11953 +12133 +5966 +3892 +19597 +7750 +707 +5503 +1714 +9459 +12006 +1362 +12357 +14220 +5717 +18583 +1321 +15403 +19546 +3620 +890 +17508 +4586 +9158 +15310 +923 +17278 +6502 +9406 +1790 +1540 +16916 +14091 +13304 +4494 +10326 +2230 +56 +13219 +17320 +14202 +15503 +3861 +78 +10493 +4999 +1081 +17061 +11221 +10599 +12757 +8972 +1653 +15651 +9007 +3796 +5485 +14396 +13088 +4283 +5605 +13224 +12841 +4878 +9395 +10511 +11666 +4720 +5286 +9628 +18394 +3878 +16354 +4939 +8961 +14625 +19956 +1130 +227 +3913 +5466 +3840 +8302 +5604 +10791 +985 +7015 +210 +8742 +3455 +14814 +15884 +6188 +10504 +18972 +5749 +9758 +7990 +1103 +17428 +17696 +7485 +18153 +16044 +19587 +7929 +14339 +14636 +8643 +19199 +16971 +1486 +11765 +10794 +11709 +7038 +9211 +19761 +10590 +18417 +15283 +3850 +11144 +11415 +17838 +8117 +19322 +9644 +18451 +12386 +7331 +17077 +1241 +13242 +11125 +18440 +6857 +14276 +948 +13000 +13679 +18108 +14331 +14504 +9428 +15808 +15276 +1889 +15889 +7092 +4491 +9983 +5339 +14909 +2277 +18390 +8313 +19687 +2677 +15712 +4442 +8932 +17952 +9513 +9342 +16675 +14298 +11027 +2809 +12147 +16592 +1466 +9322 +17985 +1361 +15154 +6515 +15035 +3816 +7558 +11435 +19043 +6607 +13969 +4050 +19652 +17195 +16509 +18631 +13091 +121 +4472 +6568 +63 +12768 +10195 +16562 +13875 +19791 +1584 +7606 +4387 +15320 +11616 +711 +3083 +9262 +10710 +431 +15922 +17958 +4246 +513 +4971 +15273 +4172 +179 +3030 +3110 +17836 +3411 +7670 +12478 +2343 +11805 +5184 +18447 +4592 +12949 +16164 +8917 +14532 +10124 +14711 +18508 +5684 +2267 +15883 +5745 +19792 +5436 +3051 +8936 +8933 +11459 +14963 +7533 +14460 +16624 +19992 +10765 +10332 +14389 +13601 +10117 +19429 +7209 +5347 +2379 +4765 +12342 +17445 +13760 +6061 +13939 +3600 +4168 +17313 +11921 +7373 +2793 +4088 +10740 +9908 +1873 +13799 +11871 +6386 +17673 +18756 +6457 +9522 +14324 +7836 +19278 +19573 +14453 +710 +12582 +6241 +16363 +8485 +16552 +14762 +15601 +5534 +14245 +13043 +16439 +17385 +10985 +13933 +12906 +3602 +727 +18115 +3648 +17155 +10543 +8471 +13825 +10986 +5137 +9331 +15772 +18383 +11781 +10937 +10474 +3173 +13848 +10555 +15690 +7083 +3299 +12421 +5542 +9260 +14095 +9350 +14971 +6232 +7345 +77 +17822 +374 +912 +14680 +12700 +10161 +16652 +18815 +16724 +4907 +5555 +12470 +7205 +578 +7780 +851 +1865 +16522 +9008 +7293 +9463 +3192 +18536 +5030 +4959 +18579 +4587 +6512 +1024 +10980 +10898 +13636 +3095 +18779 +1733 +12699 +16512 +16480 +1179 +10921 +14594 +14388 +18748 +8898 +7486 +55 +17440 +9243 +1668 +8540 +13289 +9436 +17648 +346 +12871 +16749 +164 +12634 +19758 +8937 +15947 +1861 +17849 +4989 +12096 +16698 +11948 +8399 +12457 +15480 +12110 +11619 +16899 +11292 +9074 +5373 +14465 +15305 +10982 +6406 +112 +1284 +9356 +13890 +8959 +15879 +580 +7714 +5506 +3190 +17881 +14872 +4306 +18338 +9994 +17684 +17898 +87 +472 +6161 +4723 +3447 +18148 +3158 +18542 +4948 +3738 +5297 +19281 +14788 +15127 +13776 +13611 +10153 +7853 +12450 +10108 +14227 +19753 +6458 +1003 +19973 +15140 +6123 +16735 +6928 +4327 +10420 +16963 +940 +19622 +17196 +1925 +10566 +9358 +12819 +2030 +9681 +10077 +4334 +6690 +14318 +16043 +426 +1151 +8349 +4942 +5598 +2853 +19639 +11215 +4920 +13434 +9791 +2586 +5757 +5697 +14377 +16353 +18217 +8190 +10013 +18738 +19502 +18120 +9467 +9864 +16532 +11069 +8612 +1937 +1372 +15299 +16473 +5612 +12577 +15073 +716 +13517 +16768 +18633 +14902 +15125 +1093 +2678 +7539 +5928 +6696 +8923 +10954 +2918 +12820 +2851 +16694 +7692 +16254 +19650 +2386 +4220 +4701 +4004 +17718 +2989 +6924 +477 +5937 +9535 +4961 +16542 +12774 +1691 +10135 +14188 +17597 +15935 +14882 +4357 +13662 +18206 +8885 +5504 +7217 +5111 +5964 +11358 +3451 +8604 +8519 +15887 +12545 +982 +16497 +11323 +11385 +18188 +19645 +10984 +15886 +2551 +6679 +12606 +10824 +3956 +11180 +7574 +4028 +16212 +3914 +7677 +14076 +13885 +11514 +1767 +10012 +12423 +9778 +2821 +2811 +7956 +769 +14842 +19029 +13439 +18214 +2223 +3792 +10346 +9860 +517 +15205 +19674 +15361 +16625 +14219 +16820 +19101 +4351 +640 +18765 +11121 +4350 +16482 +6707 +6810 +14542 +2791 +8006 +12823 +689 +2929 +19232 +8752 +1342 +7597 +19672 +16838 +830 +14492 +16941 +14633 +3724 +989 +4184 +1453 +8029 +3327 +3677 +16206 +2457 +17398 +8335 +10118 +266 +5533 +4680 +11379 +17478 +9527 +19617 +1678 +10329 +2741 +18138 +2456 +16540 +12104 +11443 +1298 +2118 +17615 +5995 +6862 +3488 +15526 +11304 +4840 +8637 +15790 +775 +1137 +8983 +18397 +9798 +722 +5346 +15242 +12505 +15983 +19275 +15580 +13633 +13717 +11944 +12196 +10522 +13473 +2644 +10497 +17461 +17569 +6378 +8597 +16327 +1211 +4162 +6847 +4340 +19951 +1599 +7135 +15964 +5626 +14662 +11134 +5105 +12657 +17545 +3080 +18801 +11012 +16064 +4083 +2642 +18572 +977 +12963 +1483 +9630 +11837 +71 +10721 +3758 +18526 +10761 +8113 +4746 +9448 +3092 +16557 +17825 +18446 +8252 +392 +18106 +2957 +14830 +13937 +13308 +3804 +755 +7778 +17863 +8619 +13266 +17882 +16879 +19924 +11389 +11255 +14986 +15788 +1779 +7856 +19252 +11341 +805 +3380 +9491 +5805 +10094 +15694 +17089 +3040 +11579 +3307 +15278 +10400 +6058 +9593 +13908 +17052 +7700 +4979 +11938 +16412 +18864 +4819 +12537 +4342 +8687 +12683 +12049 +13008 +15497 +992 +6943 +19289 +12947 +1456 +14316 +8409 +16042 +13950 +13145 +14558 +14961 +10435 +15751 +2927 +15037 +13411 +13988 +16928 +12420 +14982 +3404 +15797 +14812 +19720 +11288 +1366 +4130 +9898 +998 +14385 +2657 +12093 +18980 +12937 +15151 +224 +10603 +2698 +3295 +6210 +10846 +11517 +11958 +7695 +9031 +2278 +17046 +15210 +7702 +11565 +2208 +877 +14359 +14139 +14411 +9053 +12569 +5575 +14752 +6876 +10816 +9560 +3562 +3894 +11237 +12842 +3335 +17299 +5688 +16983 +12775 +8516 +18919 +5076 +6027 +11966 +18406 +13103 +18220 +7896 +14930 +1302 +8829 +8453 +314 +10673 +4087 +7300 +19948 +800 +10413 +8825 +11842 +17749 +8810 +4490 +17681 +836 +12925 +11007 +9020 +5874 +9433 +7709 +8667 +4530 +1106 +9895 +17803 +16113 +8158 +1187 +19264 +6286 +19216 +16332 +2287 +19260 +15395 +17989 +17056 +4257 +6020 +12588 +11996 +9770 +6531 +5827 +1785 +12064 +10807 +10294 +19059 +4504 +15596 +6438 +4825 +7552 +9057 +18677 +8279 +17911 +19664 +4538 +17226 +19027 +9047 +17028 +14974 +14105 +2303 +11840 +9568 +10111 +3082 +10249 +13152 +5532 +14213 +16516 +18014 +19653 +8949 +10972 +14520 +16575 +3656 +14027 +15027 +9694 +12890 +10080 +18771 +16314 +10556 +12661 +19972 +18649 +699 +13948 +7751 +3131 +6784 +2676 +275 +2888 +16062 +7163 +19120 +12723 +17616 +18763 +10595 +15530 +2995 +783 +13107 +3416 +8081 +8176 +17959 +1728 +10932 +19737 +12468 +15440 +10682 +10281 +10498 +11967 +9314 +9189 +11332 +17661 +18196 +14557 +786 +17267 +8999 +4686 +1022 +7812 +18280 +456 +4312 +10302 +2906 +8606 +19544 +14775 +730 +16858 +19191 +6550 +13128 +17408 +18023 +9971 +19711 +11312 +4487 +4085 +15128 +14255 +8297 +18539 +787 +331 +6999 +12733 +10978 +14555 +16517 +15097 +8241 +3234 +15937 +13922 +17459 +1870 +17768 +14584 +19259 +5116 +5727 +931 +3767 +8005 +2607 +11446 +16883 +13893 +17149 +6018 +13261 +19066 +4685 +13725 +10893 +8622 +19603 +3692 +7888 +10635 +14670 +14441 +16672 +10622 +10074 +11786 +2699 +19206 +8367 +18976 +3421 +10434 +4451 +7403 +3857 +1799 +13049 +18178 +8514 +10714 +6771 +3152 +18337 +10109 +1083 +8326 +19805 +5950 +19847 +4527 +5855 +11501 +19808 +6397 +15479 +19244 +4186 +2528 +12598 +7043 +17510 +15752 +11162 +6982 +6216 +6264 +13484 +2039 +19015 +2433 +2694 +1544 +8215 +10538 +16695 +118 +16077 +11429 +1496 +11057 +13818 +14005 +12615 +5221 +12602 +3674 +301 +14175 +13343 +600 +14983 +524 +820 +15281 +11539 +4133 +4198 +19963 +5947 +10165 +15234 +11491 +4135 +4983 +1876 +7528 +7304 +1562 +15212 +1043 +19677 +4850 +16854 +8897 +18780 +14903 +3881 +5463 +5893 +15405 +16276 +1209 +13525 +6806 +10732 +7767 +13288 +15528 +6372 +12709 +6129 +12409 +9811 +18287 +2895 +15777 +401 +3278 +8573 +1554 +6500 +15252 +13647 +11399 +2242 +12502 +12507 +3477 +17207 +10341 +15525 +4823 +9161 +6308 +13294 +10629 +16245 +3296 +3538 +2801 +10584 +9403 +12498 +8714 +5198 +1338 +19990 +3574 +7314 +16620 +7569 +3494 +1 +14065 +3145 +17457 +9284 +7180 +10892 +7111 +8180 +1821 +2014 +16794 +19005 +12917 +8470 +17343 +1518 +7529 +6587 +11100 +1760 +16159 +6964 +19509 +8222 +11116 +15229 +13559 +6183 +709 +2098 +15239 +6169 +19667 +17363 +13213 +3144 +10767 +11387 +5134 +2003 +12185 +19682 +9113 +4577 +19384 +8926 +537 +11326 +11783 +16912 +4178 +16291 +10344 +17866 +5561 +2746 +10562 +11578 +5294 +14800 +19925 +12412 +15835 +11577 +9481 +14515 +17350 +14493 +7078 +6986 +18146 +9531 +16411 +5954 +2511 +5791 +11662 +13463 +14537 +6179 +2025 +4119 +6428 +7919 +12301 +741 +17003 +11520 +2322 +18545 +2816 +5994 +6105 +16107 +1073 +14622 +13342 +18050 +10078 +17345 +17554 +1543 +18 +7660 +5438 +9943 +3594 +3703 +5395 +11370 +16581 +15418 +3400 +10412 +14131 +13197 +13665 +12372 +18872 +12839 +2460 +16996 +12585 +13902 +15263 +9686 +16563 +1457 +13276 +947 +3589 +1135 +18197 +11713 +12163 +2204 +2919 +9626 +16423 +15582 +15949 +4205 +17938 +17413 +19576 +4993 +5283 +14579 +5309 +14546 +15726 +2849 +2142 +11410 +13810 +9479 +3820 +866 +9978 +16784 +11671 +6837 +16320 +11812 +12184 +8153 +6873 +7419 +7101 +3344 +19846 +9263 +1478 +10669 +12194 +4501 +10924 +7264 +13394 +18353 +1956 +1277 +12092 +12460 +5232 +19324 +2119 +16437 +9390 +11362 +7475 +2597 +15271 +10529 +6995 +19228 +4045 +4951 +8139 +4466 +18481 +4561 +16783 +10099 +6860 +17063 +927 +2033 +12648 +17781 +2898 +1320 +9871 +6326 +17469 +10601 +14705 +9870 +19192 +7219 +8847 +2792 +15438 +6422 +12590 +11420 +2627 +6361 +18901 +19204 +16903 +15181 +11050 +18231 +2536 +1986 +9228 +19402 +2165 +6001 +7311 +4519 +11158 +16208 +13691 +1942 +7375 +6005 +19039 +12972 +16462 +12515 +9276 +6880 +11200 +9931 +5623 +2862 +9716 +50 +9944 +8391 +12853 +11878 +7254 +15717 +14919 +19413 +7322 +4016 +2527 +16907 +2524 +5135 +17276 +10647 +13279 +10015 +9776 +12604 +3113 +16471 +14999 +11702 +19608 +10787 +8154 +5807 +13462 +604 +8939 +6273 +8979 +12619 +10386 +13069 +15727 +11689 +4536 +16075 +13938 +13580 +15599 +18962 +10735 +11802 +10383 +1399 +4410 +3535 +17480 +14985 +2814 +3123 +2724 +6224 +18865 +8240 +17731 +19163 +11893 +3245 +12224 +10103 +2013 +3435 +5017 +19932 +18568 +2339 +13 +3243 +17153 +10233 +4851 +2574 +17494 +10453 +8242 +9960 +372 +3410 +11084 +5357 +9627 +3896 +12304 +15424 +4673 +3841 +3832 +17147 +8123 +13614 +19935 +18746 +8343 +2887 +11414 +2347 +7245 +589 +13837 +5735 +18374 +5222 +2040 +10839 +15184 +16493 +1697 +8528 +3958 +15919 +14802 +14478 +12316 +47 +3814 +15941 +17556 +5025 +6505 +2599 +4807 +16573 +9026 +4753 +13330 +17961 +6951 +4176 +18581 +6280 +482 +7481 +847 +5703 +6686 +12430 +9343 +15955 +14406 +1063 +5293 +1944 +10734 +14286 +17833 +9802 +6627 +4307 +11310 +7206 +4848 +13644 +17175 +11330 +4675 +8977 +15247 +14724 +535 +16143 +3270 +4548 +6938 +6677 +9005 +7020 +9088 +15010 +17070 +704 +119 +12456 +15356 +13788 +9116 +7416 +237 +15198 +59 +13761 +10205 +9325 +10372 +1454 +12134 +17095 +13936 +11049 +502 +4606 +6182 +3863 +15307 +4583 +11554 +15812 +14138 +3258 +6628 +2885 +16888 +641 +11710 +5554 +14854 +9242 +16270 +11171 +19856 +18552 +15074 +17754 +2806 +7711 +4314 +19993 +3468 +12255 +6787 +15711 +7231 +4434 +10023 +9285 +5031 +19987 +13934 +12706 +5672 +9465 +7875 +10303 +19459 +4489 +6807 +6819 +11240 +5921 +12952 +3370 +9733 +10462 +12708 +4175 +5746 +5906 +4485 +10783 +12094 +9690 +18895 +10977 +880 +15847 +8134 +7030 +2154 +18109 +13728 +5571 +11363 +13663 +4131 +8656 +10532 +14433 +17810 +15439 +17251 +4456 +14559 +17505 +1586 +8790 +12115 +3177 +6671 +670 +8561 +17786 +18107 +16930 +14761 +1432 +5153 +6021 +111 +14955 +4624 +16076 +14237 +9844 +1270 +17917 +17015 +15227 +19331 +18072 +7003 +1960 +10914 +6799 +15138 +15068 +13651 +19053 +8900 +16740 +12291 +2503 +5685 +6639 +8718 +7792 +8309 +12079 +45 +84 +13028 +19830 +17453 +10848 +18701 +3321 +2212 +8817 +19290 +19602 +3811 +13789 +144 +7167 +7729 +2717 +6197 +13454 +13704 +14757 +17324 +15910 +9469 +14907 +4555 +708 +1123 +7625 +13782 +13126 +17596 +13957 +9220 +6709 +8019 +11503 +2173 +4388 +16776 +7399 +18430 +6831 +4496 +642 +7723 +5530 +19442 +3778 +2952 +11253 +8700 +5520 +4446 +12978 +1215 +7685 +9872 +15951 +3934 +9149 +1390 +2471 +12160 +14399 +2434 +18415 +1218 +32 +7982 +6812 +4124 +16855 +18941 +2465 +2734 +16235 +6157 +11791 +2737 +4717 +6371 +13109 +7912 +11730 +3555 +16654 +6456 +15368 +7580 +8096 +10407 +14311 +10043 +14424 +369 +12222 +930 +11154 +10068 +4544 +5456 +19536 +4857 +17200 +8804 +13532 +8733 +422 +1506 +18343 +14719 +14153 +3048 +10267 +14506 +5029 +19942 +5898 +2382 +3445 +9501 +18362 +15809 +12221 +17889 +1979 +2217 +16234 +5817 +10847 +16634 +18902 +1743 +4247 +8945 +19623 +10002 +4438 +12048 +6623 +9521 +13864 +17763 +9245 +17975 +11607 +1909 +17851 +10919 +18575 +9464 +10642 +6247 +2061 +18940 +6649 +2106 +14928 +9727 +5719 +8118 +11365 +19383 +4681 +1252 +5370 +9341 +932 +19183 +2420 +11824 +15570 +3993 +15431 +12494 +6548 +16249 +9922 +8387 +17677 +1629 +11720 +3636 +16336 +14087 +8133 +3755 +960 +3946 +16751 +8827 +4885 +11718 +17113 +2463 +17058 +2197 +11153 +17542 +13968 +1692 +9510 +1598 +17488 +19609 +3440 +173 +1849 +6383 +13880 +5226 +8374 +7870 +4086 +12667 +7468 +6736 +10201 +2125 +2589 +6608 +5270 +7683 +3717 +14243 +10644 +11638 +1189 +8767 +19540 +9812 +3980 +13301 +13319 +16386 +14660 +6190 +19137 +18090 +6845 +9849 +7178 +9816 +308 +9653 +11880 +4949 +1903 +7656 +2818 +15129 +13426 +14721 +12555 +7133 +9304 +1140 +5193 +17641 +19302 +2755 +9232 +2290 +12070 +14373 +18000 +6573 +1429 +18199 +12099 +9266 +19799 +19842 +14630 +15890 +5400 +1168 +1334 +678 +6820 +19767 +9867 +3627 +15482 +4219 +18098 +11759 +6569 +1522 +5971 +14975 +19122 +3102 +10832 +14152 +15486 +11430 +14481 +10335 +3613 +19350 +4041 +3053 +8884 +8173 +14611 +5180 +9204 +3476 +18133 +7054 +15400 +17218 +2182 +5252 +13778 +10306 +8472 +17474 +13635 +15264 +8964 +19173 +13839 +5596 +12642 +2008 +6478 +4278 +10186 +6869 +984 +4803 +3481 +18944 +14282 +13615 +15811 +19363 +11136 +7189 +15192 +1759 +3303 +7237 +13518 +11633 +14817 +14946 +16346 +5785 +4870 +8768 +4729 +14672 +13143 +12444 +7398 +14648 +16298 +8079 +14285 +7397 +14078 +13859 +4581 +14442 +16714 +9176 +8370 +188 +12186 +11185 +2192 +556 +6540 +17373 +7988 +7629 +8772 +15852 +1841 +7050 +3644 +1975 +16088 +10223 +3437 +5406 +398 +9409 +8221 +11022 +4599 +17923 +19212 +8525 +13827 +13178 +13063 +13568 +597 +8534 +12787 +9439 +12550 +17022 +7335 +4231 +14528 +17725 +4976 +14923 +15675 +12650 +747 +13745 +4043 +10501 +10969 +4941 +4153 +2195 +12187 +11124 +447 +15401 +18301 +10561 +6296 +1363 +3414 +12037 +4513 +9714 +4258 +4202 +1567 +9251 +3195 +2028 +13727 +10188 +17434 +5451 +393 +10472 +18208 +5378 +3288 +15950 +3389 +16984 +10052 +7082 +1509 +16150 +11193 +3482 +18241 +15206 +12694 +8589 +19453 +14117 +8083 +18296 +15671 +903 +16727 +14083 +19159 +10439 +493 +5493 +17086 +15611 +1575 +16574 +11052 +2315 +19831 +1676 +14627 +19279 +9199 +7674 +5230 +15390 +15977 +19765 +11356 +12465 +18800 +5462 +2300 +8322 +4146 +16519 +16998 +4393 +7958 +3184 +13159 +2890 +1996 +14181 +637 +13125 +13038 +3623 +3067 +11766 +12208 +5955 +12813 +16951 +8522 +3119 +15340 +19563 +19939 +4996 +19335 +1037 +18714 +17876 +18389 +13231 +15917 +7782 +14898 +18670 +11630 +17753 +8603 +3584 +10272 +18520 +19060 +15779 +6553 +2105 +3022 +6160 +11143 +15697 +13675 +220 +13874 +6529 +13783 +8706 +1262 +10317 +12012 +12435 +18774 +19300 +1419 +11201 +16096 +12869 +4769 +14310 +1364 +16991 +6117 +2631 +6040 +3103 +6942 +11453 +7901 +6972 +14364 +12145 +417 +9965 +12878 +2626 +17576 +19338 +15036 +13574 +12971 +12247 +13085 +2336 +19684 +5237 +5187 +11919 +562 +13007 +16523 +12939 +5250 +5588 +16600 +10405 +9663 +12558 +19723 +16362 +8135 +19763 +9391 +2427 +7476 +2029 +18293 +15895 +2385 +1489 +636 +981 +1156 +13282 +15115 +9598 +1213 +16544 +1296 +15737 +2784 +5932 +11104 +13888 +2530 +9706 +11081 +3524 +8956 +13166 +18557 +6065 +163 +6033 +516 +18459 +16866 +11518 +5428 +478 +11492 +10280 +7532 +7002 +14327 +3220 +14828 +7036 +16393 +13768 +2930 +1117 +6726 +2318 +13211 +4689 +3342 +10203 +12398 +16526 +8782 +4418 +8237 +3169 +19441 +10112 +18812 +13734 +4102 +17219 +5543 +1906 +14728 +19262 +15429 +7648 +7893 +17037 +4627 +16891 +11123 +5327 +6657 +2501 +1812 +13229 +3099 +3375 +5468 +3815 +4459 +860 +11151 +4140 +15652 +11792 +7192 +1686 +19348 +13925 +19637 +7427 +18068 +2010 +13216 +10431 +17501 +17030 +9687 +18018 +13730 +19817 +18679 +8473 +10827 +341 +4169 +1846 +9755 +14042 +1180 +13438 +7194 +15654 +9099 +15454 +12018 +19197 +1755 +4150 +15169 +10857 +6352 +7006 +10408 +13355 +19450 +15119 +7004 +257 +10915 +6684 +8840 +18930 +4641 +10877 +229 +7166 +9896 +5905 +7545 +344 +15920 +9152 +246 +9058 +19520 +4300 +4792 +19691 +3504 +817 +8737 +12905 +2415 +6907 +8860 +5027 +8024 +3888 +16163 +11647 +14006 +12023 +4877 +7644 +1563 +11462 +18219 +12019 +1989 +8497 +6763 +3996 +181 +11278 +11703 +554 +6004 +16785 +5024 +17144 +11675 +2857 +2485 +13659 +17741 +11130 +12704 +19747 +6166 +778 +6670 +19756 +8796 +19227 +385 +10731 +11961 +4047 +15990 +19669 +12521 +16944 +16474 +13432 +10829 +6960 +19887 +2559 +18752 +8361 +13833 +17824 +13186 +8008 +10746 +9302 +12298 +19491 +17194 +94 +2726 +9562 +17936 +14229 +17211 +1264 +2388 +7536 +9309 +1090 +4704 +2987 +16669 +1069 +5974 +666 +4110 +12960 +7011 +3430 +3658 +7957 +13867 +2305 +6814 +9642 +18184 +6066 +10263 +6331 +6583 +11401 +18710 +7761 +9120 +18732 +7057 +15086 +13461 +9607 +620 +357 +10244 +16750 +12518 +14047 +13311 +1637 +8493 +9688 +3472 +12299 +5998 +13924 +6504 +7669 +1921 +9307 +3126 +6080 +18350 +17884 +19096 +19377 +1765 +3605 +16444 +6057 +19150 +7336 +745 +12446 +18967 +17715 +954 +6207 +12627 +3154 +11384 +14172 +15881 +10026 +2786 +18142 +18848 +14487 +19946 +14489 +12259 +1326 +10541 +1739 +7035 +13278 +10706 +1226 +352 +7017 +19211 +1966 +5756 +19780 +18829 +34 +11245 +2462 +4457 +14180 +2692 +14910 +13246 +6710 +10638 +7946 +7282 +8727 +1368 +3519 +5185 +6794 +8042 +5645 +5808 +4835 +17102 +3921 +11209 +12136 +10628 +3576 +4998 +7242 +18183 +4817 +1796 +6850 +16380 +8288 +16177 +12374 +2774 +12953 +19135 +17368 +12202 +2671 +12030 +3502 +13272 +18318 +2947 +9429 +17981 +13291 +16647 +9353 +13506 +1345 +1606 +17865 +15331 +3223 +9213 +2329 +6693 +15916 +2748 +1597 +12141 +14140 +10183 +17864 +15681 +10661 +17526 +3179 +2682 +9129 +17455 +9142 +7890 +908 +4428 +1257 +6910 +4718 +19099 +15214 +168 +15250 +19414 +6957 +7013 +3617 +15080 +2637 +19908 +7213 +2778 +8424 +6742 +13447 +1388 +8942 +6449 +13283 +6520 +14973 +1632 +9919 +3172 +10166 +15706 +1954 +12442 +6048 +265 +884 +19234 +2575 +19868 +18708 +1191 +3020 +14077 +15453 +1936 +2761 +13577 +17654 +9508 +15423 +12865 +3808 +2055 +13652 +5522 +8046 +17315 +11752 +2304 +4058 +7626 +9317 +10968 +6605 +18609 +12029 +9514 +12933 +6311 +6106 +4719 +10381 +6532 +12583 +14195 +11685 +4255 +15557 +6704 +15840 +15208 +6497 +8632 +3696 +7840 +12973 +18856 +9969 +19613 +18563 +6743 +13422 +6575 +12637 +3962 +14827 +1112 +19268 +5240 +5901 +13009 +5481 +15268 +13265 +16451 +18319 +1837 +8355 +8598 +4207 +983 +12961 +13919 +14456 +1836 +1239 +3682 +2158 +15971 +12416 +17843 +12431 +5091 +2773 +11993 +10113 +10676 +17265 +115 +11344 +13027 +6813 +17885 +15398 +5191 +16829 +7964 +13565 +2469 +15284 +15334 +15636 +16577 +17943 +13522 +13595 +15555 +2598 +17872 +10530 +17790 +5792 +4328 +19037 +4015 +14635 +1783 +3928 +17277 +10138 +10764 +21 +7171 +5579 +2956 +15658 +4832 +9159 +12366 +2086 +1677 +1346 +13813 +14918 +19250 +3229 +5583 +15552 +17391 +19445 +19064 +19222 +8963 +10540 +14799 +9 +11515 +12578 +13606 +8198 +18794 +4379 +3132 +19960 +18979 +631 +5846 +12072 +15204 +5022 +7193 +12746 +8665 +4090 +8310 +4594 +11466 +15411 +13457 +5418 +16502 +10360 +13018 +1912 +2683 +19853 +2078 +3221 +17284 +6288 +14554 +7948 +16763 +3317 +3633 +3568 +6613 +16070 +17081 +15908 +12464 +8082 +13060 +2555 +3439 +8477 +12271 +5038 +19860 +4829 +11706 +7998 +8384 +1595 +1805 +1640 +474 +14984 +3614 +10814 +14329 +419 +4940 +10881 +9506 +13006 +10918 +8530 +17562 +5725 +17235 +1167 +17756 +462 +11525 +8492 +2843 +6761 +11361 +6935 +11226 +8856 +16721 +17026 +57 +5769 +14925 +9033 +10129 +2837 +8439 +1616 +6434 +11611 +14205 +6075 +11770 +5291 +2326 +7364 +586 +12935 +14640 +9955 +335 +18730 +1813 +11972 +4690 +672 +5793 +13220 +5178 +13492 +19396 +15145 +8657 +11858 +19994 +19162 +1033 +7147 +2330 +19757 +572 +643 +384 +9925 +1125 +16239 +1468 +14128 +11184 +17656 +19701 +15014 +2804 +19155 +18053 +16216 +4912 +17608 +12940 +9707 +16399 +9962 +1229 +2002 +9092 +4211 +14353 +15441 +18468 +9370 +16688 +987 +9588 +8286 +7260 +40 +2980 +12044 +3622 +16853 +6776 +1292 +11037 +14858 +11338 +3271 +6792 +5869 +17176 +4935 +9850 +3133 +530 +5284 +11481 +15789 +14037 +1792 +10600 +16448 +16402 +7271 +10259 +18759 +8559 +16204 +16052 +3246 +12904 +15531 +10970 +4450 +483 +12566 +15607 +588 +15992 +5443 +1447 +12752 +2411 +166 +19825 +16716 +6513 +18826 +12041 +10452 +16902 +829 +4188 +4493 +3208 +17099 +18648 +13847 +8111 +18607 +13743 +18881 +3827 +6130 +4139 +4965 +6177 +10051 +12253 +13041 +9234 +18908 +8614 +1776 +14967 +18358 +3366 +11589 +413 +18726 +18555 +17591 +2060 +8879 +3791 +18548 +2665 +7895 +15282 +19893 +2338 +14473 +11013 +207 +13584 +19911 +18401 +16144 +13453 +5700 +10181 +13519 +4557 +16640 +10933 +8755 +6884 +10786 +8921 +8348 +8360 +3935 +7072 +11688 +18657 +16435 +16612 +9620 +15485 +12302 +1287 +4750 +17816 +6462 +19313 +13429 +10803 +19658 +1811 +2333 +2788 +12509 +2391 +13374 +756 +10119 +526 +11502 +4846 +16478 +6897 +18228 +14718 +9334 +1484 +16094 +14722 +17019 +15695 +18441 +11231 +12369 +6240 +9002 +3631 +615 +7153 +4790 +10958 +17839 +10995 +9926 +6795 +12125 +1982 +4400 +9387 +1529 +5381 +18595 +17504 +2169 +4636 +2246 +18988 +17023 +11444 +16361 +13146 +17374 +14877 +18345 +300 +18462 +12824 +564 +134 +17523 +1134 +2452 +1038 +18878 +5092 +18926 +451 +11879 +15764 +13985 +5625 +8596 +12546 +12755 +10421 +3777 +10152 +11441 +19910 +12031 +16841 +7655 +10693 +5392 +18506 +12563 +18995 +15619 +11364 +14263 +13044 +12997 +13346 +13160 +11672 +17259 +2736 +319 +9151 +10928 +1832 +7854 +15517 +3459 +2878 +17775 +19969 +6304 +18289 +37 +7244 +13589 +3371 +8941 +13784 +7754 +5591 +89 +18899 +16011 +1249 +7622 +8592 +16098 +7570 +12850 +7579 +13637 +6854 +11655 +14479 +3180 +18981 +19456 +4523 +10036 +504 +8811 +12882 +14072 +4101 +602 +1818 +8398 +2965 +4771 +14349 +619 +3381 +9045 +17723 +11003 +551 +11987 +6641 +10576 +5382 +4609 +2232 +7400 +14606 +17782 +1882 +11952 +12883 +1013 +1045 +2239 +4588 +12565 +9769 +15195 +15723 +19314 +14132 +8295 +942 +5104 +16908 +10156 +4667 +13361 +13593 +17199 +17751 +15670 +16848 +4233 +7844 +13082 +18753 +16815 +5325 +9544 +18949 +15867 +18450 +1523 +13507 +3925 +9375 +8022 +16922 +19002 +18483 +6997 +9114 +869 +6463 +18983 +1099 +7611 +15944 +19025 +11257 +19088 +16057 +4747 +3210 +12362 +8623 +553 +7619 +8730 +13142 +12287 +17628 +5674 +2143 +2667 +19194 +14067 +14994 +5916 +6983 +11142 +18968 +9128 +234 +16008 +13918 +14901 +8770 +7161 +7706 +8783 +4742 +7058 +16184 +14926 +1098 +2953 +13416 +9591 +13168 +6290 +2187 +13489 +12669 +3316 +1603 +14778 +11423 +17530 +11102 +13092 +10690 +11992 +15793 +7483 +3486 +14589 +17624 +3801 +13150 +6451 +9171 +17302 +4744 +12516 +7190 +17853 +17486 +15132 +575 +16293 +10909 +19815 +7396 +16384 +18258 +4260 +9017 +1057 +418 +15588 +18699 +7408 +1669 +9855 +17928 +4332 +19007 +7179 +7097 +10636 +8254 +15571 +17880 +17858 +2467 +4824 +8630 +5507 +7748 +13207 +7708 +9601 +13215 +9756 +16026 +7734 +12007 +3985 +3975 +24 +8499 +19781 +16997 +19931 +7498 +18003 +898 +3774 +5267 +19904 +1141 +3236 +3997 +340 +8277 +10219 +19621 +9143 +17010 +11669 +12691 +8169 +9079 +14583 +14459 +12061 +11205 +14789 +3757 +4913 +4123 +1307 +687 +8143 +5783 +6087 +3116 +6893 +18700 +1126 +18939 +14849 +19168 +13694 +3354 +4732 +7220 +9715 +2729 +9476 +7883 +3283 +18448 +7298 +19525 +6913 +10908 +17048 +9894 +3151 +1736 +12383 +4842 +18693 +11914 +7090 +7865 +5521 +4669 +12482 +19952 +8529 +15462 +2518 +327 +10523 +1055 +13857 +19428 +11179 +2896 +15066 +11934 +935 +4289 +11899 +568 +1028 +3057 +1623 +12628 +9328 +9278 +19555 +8131 +10592 +16847 +11519 +4304 +18582 +5299 +2381 +2830 +3465 +14862 +2112 +3199 +15457 +9221 +15445 +6534 +10946 +1244 +4061 +14787 +19829 +10695 +19455 +5951 +3522 +14155 +18282 +4454 +13337 +16782 +9130 +8003 +1143 +6838 +6755 +16851 +6626 +15090 +5895 +18185 +9123 +19638 +11475 +1588 +5718 +8144 +12244 +7806 +9909 +13328 +12618 +16186 +14803 +3571 +1175 +6242 +13141 +4981 +16953 +15739 +15888 +15825 +8584 +12073 +12181 +6668 +13546 +17247 +7490 +5641 +18695 +17270 +19130 +15593 +9991 +8480 +19426 +17306 +3868 +2172 +4648 +16842 +7443 +12297 +16167 +16153 +8206 +17653 +16223 +18306 +8011 +13258 +8749 +11413 +17355 +18821 +2392 +7736 +1065 +3762 +11127 +13274 +14848 +7261 +11626 +14264 +10701 +9139 +6803 +7406 +16136 +15216 +18770 +8147 +10579 +5432 +1354 +10479 +8928 +13710 +2474 +11118 +13383 +15609 +5990 +19489 +15815 +8567 +16068 +2522 +3666 +11847 +16138 +7991 +12520 +15287 +18644 +4712 +9022 +14031 +1952 +10045 +2917 +13650 +2881 +19104 +3556 +5163 +6530 +16196 +3566 +9300 +5245 +2996 +18038 +2647 +3943 +13251 +13680 +17217 +1077 +6341 +15865 +1918 +437 +4834 +2572 +7862 +5711 +8408 +8924 +3418 +19594 +13314 +16731 +18513 +3075 +19643 +11476 +18781 +11249 +16940 +13425 +8467 +10608 +5912 +4692 +6496 +17159 +18817 +9774 +17707 +10887 +2646 +3101 +15565 +13814 +7910 +10234 +7672 +18593 +16125 +3872 +4688 +13502 +15285 +10258 +15956 +16674 +17513 +16734 +18127 +283 +16584 +3550 +2235 +19977 +4930 +2725 +2122 +13871 +5420 +13472 +5896 +17210 +9381 +2334 +15279 +7181 +3162 +10175 +2835 +6150 +13023 +1663 +5737 +5310 +2815 +7940 +12773 +16395 +11629 +15496 +2439 +17094 +5132 +3999 +17160 +10801 +3114 +4724 +1838 +4275 +19750 +10670 +7456 +12380 +15744 +14920 +4978 +5790 +9529 +8616 +12128 +4683 +6244 +1403 +3531 +360 +13158 +11178 +1997 +14972 +3992 +1034 +10672 +5935 +5578 +19888 +6243 +6653 +2371 +9222 +18521 +2413 +16275 +785 +3406 +16603 +9376 +4375 +12899 +17609 +8678 +13468 +16027 +14774 +14184 +3171 +2946 +15157 +6421 +18315 +18057 +13759 +14435 +17583 +11371 +4966 +15100 +1712 +8162 +13435 +7357 +11300 +17969 +16426 +13370 +6748 +10065 +6604 +5001 +9209 +3874 +14068 +5079 +14734 +434 +6630 +6747 +1394 +1085 +4425 +3478 +2053 +1367 +1025 +1560 +7981 +7309 +993 +15315 +9454 +7358 +11342 +7099 +7703 +12000 +4024 +14266 +9311 +2993 +15389 +19584 +18740 +8699 +11727 +6541 +11947 +3186 +7348 +13898 +693 +3189 +3074 +2425 +18658 +3065 +2567 +12010 +16450 +13757 +13022 +14769 +14444 +15466 +17832 +8016 +7075 +9185 +4847 +16771 +10268 +9578 +6919 +19624 +13031 +13380 +5124 +13554 +15078 +19046 +470 +3930 +655 +4019 +8440 +19527 +194 +9411 +9840 +7478 +6682 +4887 +4617 +17055 +759 +16034 +14466 +684 +16571 +2455 +16807 +6394 +5985 +15965 +10079 +2847 +13081 +7466 +15162 +17571 +13721 +6698 +11339 +14030 +14447 +10426 +13351 +8454 +18388 +4572 +13310 +13512 +6676 +2730 +18079 +15177 +895 +14003 +16172 +3572 +14307 +7807 +8842 +3323 +6491 +8751 +18445 +7376 +1592 +7774 +18987 +9532 +12636 +8777 +7540 +4054 +17548 +16481 +19598 +19357 +6078 +9000 +11767 +190 +2819 +17894 +3474 +4462 +13444 +8448 +15973 +10837 +13252 +12071 +15238 +5285 +7605 +7234 +7421 +8906 +11564 +1479 +14953 +19400 +5465 +15855 +2019 +16192 +10889 +8776 +15519 +6817 +345 +16653 +13688 +2403 +14018 +16537 +19797 +19997 +4634 +14400 +8655 +9483 +18683 +13841 +9504 +16741 +5014 +2577 +4225 +9013 +14064 +16518 +10623 +387 +17050 +6189 +999 +12600 +13726 +5888 +6007 +11479 +16718 +12795 +19897 +16182 +18458 +13772 +7673 +19890 +7047 +5618 +5959 +7185 +15330 +2138 +8591 +2605 +7691 +18431 +9186 +17634 +15342 +11687 +919 +8615 +10055 +2690 +11161 +2548 +10916 +8538 +5012 +1559 +5083 +8487 +2702 +18625 +9752 +6730 +11701 +10378 +7582 +5144 +16250 +17020 +18819 +17867 +2077 +10760 +6501 +16111 +5174 +1236 +8417 +15243 +15428 +4185 +17971 +11150 +2270 +4076 +10418 +9616 +14304 +9739 +14354 +11173 +9077 +2553 +17544 +16796 +7414 +5750 +16219 +11777 +3621 +12054 +6991 +4516 +3866 +13047 +17679 +2416 +13399 +337 +14655 +18382 +16054 +18064 +17422 +17215 +17498 +1750 +7126 +3127 +18565 +6762 +18960 +10966 +6662 +6082 +3070 +15563 +12371 +7905 +4700 +11434 +3014 +18514 +12815 +17114 +14966 +3384 +2723 +12173 +291 +1462 +3643 +17993 +1139 +7562 +8067 +8759 +8205 +14015 +3287 +1738 +10139 +1963 +15196 +7513 +5494 +15932 +17383 +6751 +7542 +5886 +15038 +3784 +2253 +7851 +2779 +12151 +7157 +19881 +11373 +17650 +174 +4010 +5259 +18803 +11562 +7188 +11876 +17734 +6949 +10287 +14157 +18143 +8517 +1622 +16646 +6088 +12780 +5103 +3603 +13777 +11604 +16943 +2542 +4967 +1369 +6433 +11825 +12008 +6494 +7531 +15518 +11926 +1104 +18507 +2332 +2257 +4511 +1230 +7978 +958 +10757 +4564 +15232 +5841 +6680 +10704 +17564 +15669 +5712 +2926 +3964 +3324 +15679 +15296 +5331 +3663 +1717 +17708 +5943 +11395 +16378 +5660 +4341 +1166 +17091 +1582 +6016 +216 +18784 +4639 +17240 +18603 +3597 +6350 +17348 +12607 +10364 +3058 +10709 +7744 +2871 +6905 +4195 +17230 +6343 +10021 +16126 +15597 +17983 +5405 +16319 +885 +15877 +14568 +3527 +7602 +13623 +1531 +14019 +12641 +157 +4128 +2733 +12364 +18822 +1283 +15255 +8284 +11428 +4062 +15606 +7132 +16755 +2863 +16252 +17191 +10100 +10459 +13758 +13200 +3710 +7159 +13800 +15040 +5271 +6711 +13654 +9837 +5173 +19220 +10368 +11398 +6330 +4006 +12476 +6586 +5797 +4242 +15703 +1665 +8427 +14178 +5244 +1968 +5818 +12107 +15505 +13285 +11529 +8822 +6603 +16260 +19339 +7627 +5654 +14824 +4389 +17580 +12135 +3680 +889 +10104 +15702 +13504 +1214 +7198 +19579 +15374 +12365 +12999 +11772 +14776 +9617 +6293 +9577 +13967 +18943 +16729 +7577 +14436 +19298 +6622 +10665 +8431 +19395 +19984 +10662 +14632 +16580 +16368 +12239 +6318 +13228 +8861 +7813 +6366 +1645 +13066 +2491 +11844 +15839 +12306 +14755 +19548 +18460 +2393 +5422 +364 +17185 +7575 +16636 +4921 +11833 +3376 +3818 +3752 +5026 +14107 +18861 +19157 +9505 +271 +17577 +2479 +9984 +2376 +15784 +14631 +6083 +957 +3039 +15020 +18999 +1508 +6772 +10931 +17855 +6992 +19287 +12666 +14170 +1740 +12923 +6858 +2309 +9440 +18070 +2886 +10054 +15402 +4626 +4196 +9897 +5702 +13410 +8233 +12649 +590 +19261 +238 +1938 +12592 +12402 +11487 +1732 +3452 +1752 +6939 +9524 +16769 +9060 +1911 +5708 +2742 +697 +13692 +17972 +10130 +12765 +6963 +1276 +7169 +9722 +3860 +9525 +6254 +5355 +8312 +12798 +8280 +1371 +18400 +15762 +1407 +4048 +9087 +15999 +2369 +6100 +7596 +1131 +19386 +3638 +1032 +12675 +18835 +812 +6274 +17032 +386 +2167 +3678 +10792 +13541 +10961 +7152 +18589 +11442 +7770 +2065 +9704 +5282 +6167 +16058 +5089 +18413 +6249 +8785 +1896 +171 +5035 +12241 +1190 +17145 +7144 +17135 +8142 +9241 +4830 +18909 +2636 +14302 +8987 +18964 +17752 +12531 +19686 +16553 +8709 +14063 +12538 +7511 +10082 +2968 +1893 +12781 +13845 +2048 +10552 +6937 +9259 +15372 +4901 +10380 +240 +19041 +17426 +9757 +244 +13914 +2372 +3036 +6899 +4460 +14836 +11513 +98 +2310 +12522 +7953 +531 +19399 +19557 +6452 +18372 +2159 +15351 +4288 +2639 +8950 +7120 +3454 +14580 +19091 +13984 +8855 +8837 +5926 +13956 +9366 +15757 +12812 +10755 +14921 +5929 +14512 +15133 +7920 +6593 +5687 +8889 +15860 +2928 +19630 +6427 +4721 +16681 +11324 +19844 +17817 +10370 +1708 +16101 +347 +10038 +8229 +315 +16837 +17819 +15710 +15957 +1594 +4849 +988 +14540 +14061 +466 +10409 +1590 +13376 +14016 +5551 +2128 +7123 +5960 +2971 +2894 +2673 +113 +18841 +19657 +943 +17769 +10736 +12748 +3823 +10505 +1441 +6012 +5093 +5732 +8689 +4642 +7535 +11738 +12190 +2754 +15430 +390 +6266 +12967 +3068 +18040 +14457 +19114 +17612 +1231 +14044 +17221 +6801 +6131 +763 +9072 +2323 +18702 +11260 +16132 +4616 +3505 +14452 +10756 +191 +8073 +479 +13987 +14278 +10273 +9516 +13779 +11450 +10853 +6560 +10506 +19086 +18039 +9299 +11353 +5320 +16870 +14867 +167 +4794 +17329 +14958 +10076 +17110 +6788 +18190 +16720 +8224 +795 +4646 +6493 +1161 +5263 +10666 +748 +10573 +9966 +6233 +17645 +6417 +16039 +10192 +17280 +6430 +18198 +8044 +4755 +7504 +2203 +6848 +16849 +7324 +7591 +5536 +2021 +18495 +17862 +3744 +11217 +7378 +9632 +10720 +11790 +7906 +8422 +16003 +9140 +11526 +7747 +1148 +17188 +15170 +14893 +2288 +1242 +1044 +15730 +2828 +17845 +15822 +8368 +18914 +3357 +16524 +3520 +7310 +8211 +9024 +6767 +9319 +7926 +7467 +8259 +2377 +2483 +15244 +4914 +17406 +8216 +17683 +19085 +1387 +6646 +14486 +10510 +18046 +11070 +13360 +16754 +15081 +9737 +3911 +4777 +10278 +15289 +14511 +17201 +7858 +18664 +12469 +18097 +6663 +10134 +3399 +3912 +10869 +3442 +17538 +17850 +17631 +2782 +326 +16022 +18134 +9553 +16702 +7882 +7788 +7816 +1877 +1951 +1047 +5321 +11269 +1555 +11555 +10739 +8981 +16342 +13822 +979 +17702 +9247 +921 +18937 +16591 +1535 +13921 +4600 +12206 +12042 +19793 +10602 +19418 +7187 +9416 +4614 +16415 +14096 +13803 +226 +16737 +19230 +13198 +18116 +10277 +14249 +17439 +19986 +13236 +17136 +12900 +5341 +7985 +2679 +5627 +9312 +10199 +16861 +2942 +1852 +10466 +7838 +11922 +8420 +15176 +2986 +17555 +5978 +7507 +4112 +1992 +1988 +6446 +10154 +6176 +16089 +9104 +11211 +18823 +18351 +8555 +11458 +10327 +15240 +3768 +1397 +16753 +1435 +19820 +2920 +17298 +14659 +9014 +16372 +7697 +17405 +11495 +14499 +7238 +2422 +3904 +13281 +17674 +10891 +2231 +16960 +7274 +12581 +14323 +16416 +15765 +724 +17605 +2601 +16006 +4423 +17675 +16772 +15137 +12001 +12449 +14088 +2348 +19239 +19610 +12057 +18615 +11643 +5915 +19006 +9876 +17123 +18082 +17785 +4026 +17663 +17042 +17964 +11624 +5976 +15444 +10059 +15667 +15786 +12511 +11528 +7769 +17282 +12557 +10398 +18996 +2584 +6022 +16775 +14244 +5590 +7363 +875 +6815 +15076 +14959 +557 +2437 +9090 +2020 +19675 +405 +18129 +12453 +18373 +8859 +16864 +17115 +14085 +17182 +11106 +12069 +12909 +7080 +11712 +16142 +217 +7297 +11393 +18678 +14908 +8633 +8695 +10844 +6283 +527 +17049 +8815 +18650 +18884 +13586 +15995 +19789 +4881 +7954 +19487 +12038 +2817 +19863 +9280 +8054 +19464 +13003 +3267 +8071 +16069 +10342 +6306 +11396 +6477 +8864 +5704 +2710 +12199 +19054 +8150 +18916 +598 +1798 +10862 +3564 +2493 +11425 +19355 +43 +2000 +6381 +7100 +11343 +863 +13642 +17638 +268 +13767 +4498 +5229 +3469 +11367 +14847 +4946 +11731 +13604 +11640 +402 +15347 +19050 +10879 +2674 +13005 +15778 +10391 +11884 +3160 +7852 +14628 +18986 +1357 +10049 +15644 +12059 +17044 +10057 +11641 +5467 +19892 +11377 +9988 +4316 +12799 +18538 +1474 +13114 +14950 +5039 +16025 +9250 +10657 +7118 +4222 +19918 +14667 +15052 +16492 +5938 +900 +7900 +7710 +5822 +18875 +8196 +18882 +2880 +1152 +12659 +5037 +10089 +10221 +4065 +13978 +926 +16585 +18992 +12586 +2191 +9141 +528 +19659 +7864 +12705 +19187 +17454 +5459 +794 +2685 +3822 +14642 +12897 +12235 +18761 +7972 +18561 +15638 +3558 +3281 +11239 +12014 +81 +14644 +6095 +7307 +19241 +11026 +19273 +4402 +9027 +17908 +13087 +9145 +5881 +13560 +12142 +15568 +7465 +5444 +16213 +19698 +2972 +6944 +17487 +19328 +2036 +9863 +13626 +18917 +11146 +9070 +19393 +7971 +11890 +5695 +635 +10250 +295 +15996 +17229 +9818 +17134 +15200 +3936 +1634 +12688 +3340 +7352 +18614 +16175 +12743 +12930 +17127 +15322 +11875 +7639 +13703 +17626 +17535 +19031 +6930 +14697 +1825 +16979 +3705 +18768 +8208 +16300 +4000 +11325 +16131 +15139 +12729 +13528 +332 +18635 +16676 +10965 +15507 +7223 +639 +13513 +856 +8973 +9929 +9743 +19521 +19061 +11018 +3185 +11891 +16895 +2198 +19403 +3970 +10820 +9093 +4773 +15782 +10687 +12854 +5884 +11595 +15030 +2711 +5526 +12793 +6369 +6740 +14772 +18938 +3802 +19193 +6108 +4678 +907 +3461 +16201 +5374 +11025 +539 +11369 +2622 +12082 +11433 +9720 +11321 +12455 +2353 +3790 +6902 +449 +7641 +5161 +19164 +14956 +8586 +2591 +16662 +19906 +2380 +12652 +9882 +14462 +779 +8482 +2800 +16037 +15024 +11349 +12814 +9198 +4722 +2446 +1556 +12784 +8673 +16469 +415 +7447 +11195 +10674 +1046 +12767 +18816 +14060 +6056 +4578 +10502 +12510 +2802 +10838 +3637 +1690 +3156 +8481 +1662 +7583 +6152 +1862 +9289 +7438 +7598 +5326 +13903 +16604 +17475 +9371 +3069 +4759 +19035 +16499 +11168 +2194 +10852 +8599 +8552 +4281 +18637 +18012 +375 +15003 +5165 +5574 +16872 +12433 +10637 +5673 +653 +10539 +10521 +8807 +12451 +19728 +10072 +5563 +13953 +8904 +15337 +16685 +19478 +7023 +9759 +616 +15171 +2579 +2117 +10525 +3776 +8050 +12265 +2659 +10955 +4871 +8826 +5215 +8556 +7636 +5354 +9040 +16536 +2478 +4610 +7428 +1310 +14804 +15627 +5729 +13802 +4533 +7916 +17655 +9218 +9878 +8901 +14109 +15473 +18424 +14516 +17854 +922 +9809 +777 +15617 +1328 +6959 +16227 +11101 +16684 +1801 +5166 +3937 +8975 +19858 +5359 +4985 +11760 +5918 +16780 +10560 +15954 +7804 +3155 +6148 +12639 +6777 +120 +15463 +11542 +3639 +1391 +19390 +8181 +7014 +10253 +3250 +15768 +1224 +3635 +17809 +1016 +18230 +8803 +11834 +14932 +9345 +8653 +9225 +17041 +213 +1541 +13991 +16261 +12413 +19838 +8918 +809 +13016 +5260 +5055 +10934 +10983 +19317 +3855 +18375 +13094 +17275 +3345 +582 +8155 +15791 +2259 +9749 +3612 +13735 +1619 +18673 +10429 +7055 +3676 +3787 +11307 +8421 +1385 +5837 +11412 +8363 +9380 +5042 +8496 +10106 +6121 +4167 +18201 +13748 +12307 +109 +6346 +13306 +10480 +12215 +17004 +14747 +13535 +16264 +1172 +14846 +5114 +19359 +11684 +10032 +8835 +12411 +4147 +19345 +2363 +10354 +9495 +9197 +15041 +1943 +19543 +13605 +2240 +7746 +4005 +15943 +2549 +1421 +13992 +15064 +12013 +5570 +18797 +16442 +13196 +6916 +9235 +137 +7200 +5527 +6624 +16927 +13187 +17080 +12312 +5464 +8951 +6445 +9958 +12928 +1917 +14677 +13004 +2429 +4643 +15183 +12254 +19921 +6191 +11831 +13834 +16233 +19879 +4656 +14240 +5820 +13572 +5939 +13868 +4798 +9814 +10744 +4417 +15897 +11917 +12437 +3197 +14604 +646 +17778 +6926 +7007 +18540 +4762 +11729 +6453 +16615 +15672 +8745 +8164 +1053 +10646 +18766 +7477 +14917 +19578 +4236 +6537 +10230 +18156 +10047 +16229 +497 +18798 +9853 +2838 +1259 +8518 +9677 +10997 +18749 +19570 +13151 +13964 +9652 +14081 +2596 +8611 +10570 +10854 +5200 +2944 +951 +12686 +16778 +2324 +17719 +8357 +489 +18662 +18101 +9890 +8036 +1729 +443 +1289 +3196 +12816 +12016 +559 +13561 +5773 +2018 +12529 +19632 +2992 +7877 +8451 +2213 +10654 +1527 +11087 +18384 +9763 +6650 +11809 +1609 +4467 +10204 +1488 +15738 +14701 +15455 +2079 +3456 +11029 +15574 +7377 +9987 +2948 +5175 +14345 +7366 +5213 +7608 +19866 +10247 +3374 +11014 +5775 +8122 +2564 +11404 +10184 +12264 +7048 +18945 +19704 +8325 +18305 +13815 +10132 +6797 +10936 +1702 +10350 +3315 +1039 +19438 +7141 +499 +4415 +17139 +12106 +15573 +13098 +9349 +2740 +6011 +17074 +14704 +13188 +12325 +5268 +128 +3117 +17471 +7712 +19416 +6805 +5840 +18755 +1747 +12198 +2297 +6940 +7928 +3557 +16605 +9100 +3284 +929 +2991 +5171 +9105 +6612 +8201 +17441 +10795 +7211 +9938 +4213 +9610 +15608 +6395 +14637 +18330 +11331 +13876 +9594 +2243 +13102 +12866 +13537 +8418 +15277 +16949 +11213 +19616 +8513 +15023 +14446 +6948 +8498 +10269 +9670 +14458 +6901 +11357 +19957 +10534 +16712 +18348 +1383 +5275 +16251 +333 +2412 +14927 +7276 +1858 +4349 +3275 +648 +5278 +9666 +6384 +7228 +17464 +4764 +16289 +2921 +19010 +7815 +3736 +11913 +19719 +16259 +8890 +18025 +1886 +18121 +4821 +7384 +5276 +11473 +6379 +12858 +13407 +2561 +19861 +7675 +15363 +16265 +10215 +15396 +17059 +14382 +9101 +13831 +17112 +1118 +19648 +12896 +15509 +1621 +18998 +17789 +14808 +2200 +17934 +9253 +16813 +8397 +9695 +17162 +12788 +15149 +4886 +9657 +17540 +5440 +6871 +446 +7966 +3719 +18020 +8594 +19427 +4106 +1655 +11974 +17266 +18550 +5482 +4944 +17868 +8149 +13808 +1459 +677 +4943 +3369 +13600 +12436 +4788 +16414 +13820 +12613 +17079 +9685 +1791 +19696 +11232 +8813 +6279 +13149 +4003 +1331 +4751 +13718 +17326 +10718 +4988 +8720 +13244 +12894 +4469 +19523 +4164 +574 +3262 +3121 +15197 +12488 +14422 +3394 +15618 +12647 +5404 +8148 +18660 +6270 +6769 +12942 +16017 +4344 +14650 +2435 +4241 +18303 +9680 +15286 +13701 +16994 +11131 +7553 +2568 +9968 +6309 +11711 +8818 +14293 +13980 +1157 +6691 +17351 +801 +17557 +1268 +10067 +18697 +15903 +11645 +7544 +7831 +10738 +18610 +19076 +10609 +7455 +12919 +412 +4731 +15175 +9352 +4903 +3856 +12088 +7429 +16924 +5427 +15464 +10176 +11120 +19541 +1626 +17762 +17228 +3076 +17289 +7974 +15069 +17047 +11409 +7799 +2707 +7794 +15153 +16613 +3139 +14179 +12875 +17143 +9761 +12067 +6544 +4249 +6828 +11108 +17891 +13329 +6349 +1526 +14859 +3882 +14876 +13375 +1311 +512 +19351 +1517 +10942 +19710 +12323 +3382 +16148 +9603 +15167 +7444 +1961 +17744 +18475 +6961 +11835 +12011 +13212 +19177 +15521 +16905 +8607 +13854 +905 +4023 +19920 +1113 +665 +888 +16595 +10604 +10613 +6549 +4809 +12713 +6988 +7495 +4539 +1238 +9551 +14471 +13112 +18767 +17729 +10282 +14074 +9360 +12943 +116 +10781 +18042 +8436 +14543 +5234 +5963 +14405 +19971 +1195 +1694 +19717 +16271 +9803 +2931 +3670 +5140 +12918 +9154 +15316 +4203 +5170 +13130 +12889 +18632 +12169 +5109 +4927 +13882 +1892 +9167 +5772 +11017 +7604 +8626 +254 +17357 +519 +11928 +610 +19953 +8246 +1815 +18859 +19000 +11059 +13451 +12572 +7127 +5540 +2675 +12233 +11916 +19785 +5499 +18869 +13170 +14185 +7098 +17890 +7125 +3362 +4796 +12872 +13299 +18437 +1515 +16028 +924 +15969 +13406 +6335 +19083 +19680 +8405 +6974 +14150 +18472 +175 +17704 +6026 +10359 +125 +9949 +323 +16066 +8426 +9705 +17840 +11618 +16405 +17831 +5495 +7925 +5446 +13136 +16856 +501 +5798 +5046 +18804 +2436 +15071 +1789 +3595 +9365 +573 +14080 +6014 +1401 +10190 +7089 +1300 +15946 +5040 +3788 +7285 +10547 +8250 +13270 +19474 +5254 +18304 +3166 +8157 +14246 +1905 +18877 +1826 +14292 +2116 +14290 +14303 +19315 +3500 +1440 +17336 +4125 +5832 +13656 +7446 +1578 +18255 +13466 +2394 +10357 +10322 +16599 +484 +14238 +16871 +576 +19975 +13303 +3587 +11715 +3858 +19108 +13193 +6484 +16913 +13746 +3689 +7785 +11274 +3226 +867 +3521 +9875 +7548 +11683 +2938 +2808 +10639 +5637 +12182 +14113 +8393 +19388 +14595 +2186 +13448 +16746 +4338 +14350 +6545 +15318 +11072 +12553 +189 +5154 +19178 +14990 +7640 +16551 +114 +1718 +17988 +16207 +18663 +8784 +19889 +15104 +14822 +10831 +1235 +3859 +6637 +8056 +15442 +3255 +12219 +1651 +13557 +2588 +674 +4427 +1533 +790 +4406 +19996 +6344 +6631 +2247 +5894 +5984 +9042 +18361 +16707 +1207 +10035 +2604 +9086 +9038 +2093 +9184 +12278 +14256 +475 +18083 +376 +16078 +17109 +16982 +6483 +14280 +18418 +18093 +16337 +12183 +11093 +11437 +16118 +14962 +13543 +11745 +4113 +5099 +13806 +19115 +15015 +16645 +10805 +16214 +19047 +12324 +16211 +18511 +16564 +14116 +7889 +1392 +16836 +764 +5843 +6642 +10019 +652 +9520 +12063 +12796 +5614 +3854 +13715 +1967 +6234 +19116 +17506 +5372 +1757 +9779 +1602 +17388 +1426 +10242 +15945 +15458 +16226 +3591 +1600 +8249 +19284 +11497 +19087 +19865 +1171 +13498 +9789 +11682 +10949 +1897 +16649 +6389 +17446 +4783 +6917 +7019 +16224 +11046 +6236 +15851 +1965 +6323 +15120 +13774 +1913 +668 +6193 +11617 +18867 +1839 +1507 +19433 +18326 +5922 +15468 +14764 +19434 +15915 +17531 +2201 +9970 +8108 +6647 +3871 +15044 +198 +13071 +13836 +2234 +3248 +7027 +18630 +6894 +8504 +14268 +13542 +12579 +4510 +14641 +11348 +10437 +411 +9682 +13850 +12554 +2215 +4432 +19754 +6577 +6692 +5885 +13896 +13530 +8995 +8984 +6713 +1638 +13610 +12847 +8177 +17639 +5061 +7959 +16560 +5002 +5882 +10017 +7224 +2383 +395 +10415 +14874 +10808 +19361 +1853 +5595 +2833 +2630 +4200 +9069 +953 +1704 +2651 +5490 +15447 +6469 +18785 +15647 +8618 +18836 +8001 +6054 +12794 +8130 +11472 +14941 +12022 +5015 +14748 +19796 +10336 +16788 +9751 +8247 +16711 +5665 +480 +9231 +19950 +9124 +5067 +12568 +8320 +16221 +12513 +4007 +11214 +7084 +18858 +15861 +5809 +3408 +12988 +18103 +2228 +2414 +11002 +11463 +487 +2084 +873 +1499 +6827 +8902 +19125 +1699 +6154 +29 +14125 +9699 +16418 +3683 +6248 +7809 +2144 +9001 +8404 +8930 +6509 +10207 +2609 +14163 +15598 +5857 +731 +6295 +16312 +13307 +8062 +18874 +6720 +14190 +9457 +18814 +18570 +4360 +18309 +6782 +9548 +14992 +17017 +16082 +2331 +6703 +13941 +16808 +6596 +18048 +12268 +2472 +11044 +19068 +18961 +3910 +14000 +9847 +2769 +4774 +15335 +2071 +7173 +13524 +16700 +1006 +4290 +9556 diff --git a/tests/fixtures/sort/ext_stable.expected b/tests/fixtures/sort/ext_stable.expected new file mode 100644 index 000000000..11ca4deb7 --- /dev/null +++ b/tests/fixtures/sort/ext_stable.expected @@ -0,0 +1,4 @@ +0a +0a +0b +0b diff --git a/tests/fixtures/sort/ext_stable.txt b/tests/fixtures/sort/ext_stable.txt new file mode 100644 index 000000000..11ca4deb7 --- /dev/null +++ b/tests/fixtures/sort/ext_stable.txt @@ -0,0 +1,4 @@ +0a +0a +0b +0b diff --git a/tests/fixtures/sort/human-mixed-inputs-reverse.expected b/tests/fixtures/sort/human-mixed-inputs-reverse.expected new file mode 100644 index 000000000..463f44a2a --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs-reverse.expected @@ -0,0 +1,37 @@ +.2T +2G +100M +7800900K +51887300- +1890777 +56908-90078 +6780.0009866 +6780.000986 +789----009999 90-0 90-0 +1 +0001 +apr +MAY +JUNNNN +JAN +AUG +APR +0000000 +00 + + + + + + + + + + + + + + + + +-1.4 diff --git a/tests/fixtures/sort/human-mixed-inputs-reverse.txt b/tests/fixtures/sort/human-mixed-inputs-reverse.txt new file mode 100644 index 000000000..ebef388b9 --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs-reverse.txt @@ -0,0 +1,37 @@ +JAN + +0000000 + +00 + +0001 + +1 + +-1.4 + +JUNNNN +AUG + +apr + +APR + + +MAY +1890777 + +56908-90078 + +51887300- + +6780.0009866 + +789----009999 90-0 90-0 + +6780.000986 + +100M +7800900K +2G +.2T diff --git a/tests/fixtures/sort/human-mixed-inputs-stable.expected b/tests/fixtures/sort/human-mixed-inputs-stable.expected new file mode 100644 index 000000000..e1c85b8ce --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs-stable.expected @@ -0,0 +1,37 @@ +-1.4 +JAN + +0000000 + +00 + + + + +JUNNNN +AUG + +apr + +APR + + +MAY + + + + + + +0001 +1 +789----009999 90-0 90-0 +6780.000986 +6780.0009866 +56908-90078 +1890777 +51887300- +7800900K +100M +2G +.2T diff --git a/tests/fixtures/sort/human-mixed-inputs-stable.txt b/tests/fixtures/sort/human-mixed-inputs-stable.txt new file mode 100644 index 000000000..ebef388b9 --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs-stable.txt @@ -0,0 +1,37 @@ +JAN + +0000000 + +00 + +0001 + +1 + +-1.4 + +JUNNNN +AUG + +apr + +APR + + +MAY +1890777 + +56908-90078 + +51887300- + +6780.0009866 + +789----009999 90-0 90-0 + +6780.000986 + +100M +7800900K +2G +.2T diff --git a/tests/fixtures/sort/human-mixed-inputs-unique.expected b/tests/fixtures/sort/human-mixed-inputs-unique.expected new file mode 100644 index 000000000..50f53b6a0 --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs-unique.expected @@ -0,0 +1,13 @@ +-1.4 +JAN +0001 +789----009999 90-0 90-0 +6780.000986 +6780.0009866 +56908-90078 +1890777 +51887300- +7800900K +100M +2G +.2T diff --git a/tests/fixtures/sort/human-mixed-inputs-unique.txt b/tests/fixtures/sort/human-mixed-inputs-unique.txt new file mode 100644 index 000000000..ebef388b9 --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs-unique.txt @@ -0,0 +1,37 @@ +JAN + +0000000 + +00 + +0001 + +1 + +-1.4 + +JUNNNN +AUG + +apr + +APR + + +MAY +1890777 + +56908-90078 + +51887300- + +6780.0009866 + +789----009999 90-0 90-0 + +6780.000986 + +100M +7800900K +2G +.2T diff --git a/tests/fixtures/sort/human-mixed-inputs.expected b/tests/fixtures/sort/human-mixed-inputs.expected new file mode 100644 index 000000000..3f5692b7b --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs.expected @@ -0,0 +1,37 @@ +-1.4 + + + + + + + + + + + + + + + + +00 +0000000 +APR +AUG +JAN +JUNNNN +MAY +apr +0001 +1 +789----009999 90-0 90-0 +6780.000986 +6780.0009866 +56908-90078 +1890777 +51887300- +7800900K +100M +2G +.2T diff --git a/tests/fixtures/sort/human-mixed-inputs.txt b/tests/fixtures/sort/human-mixed-inputs.txt new file mode 100644 index 000000000..ce5986d6e --- /dev/null +++ b/tests/fixtures/sort/human-mixed-inputs.txt @@ -0,0 +1,46 @@ +JAN + +0000000 + +00 + +0001 + +1 + +-1.4 + +JUNNNN +AUG + +apr + +APR + + +MAY +1890777 + +56908-90078 + +51887300- + +6780.0009866 + +789----009999 90-0 90-0 + +6780.000986 + +1M +10M +100M +1000M +10000M + +7800900K +780090K +78009K +7800K +780K +2G +.2T diff --git a/tests/fixtures/sort/human-numeric-whitespace.expected b/tests/fixtures/sort/human-numeric-whitespace.expected new file mode 100644 index 000000000..6fb9291ff --- /dev/null +++ b/tests/fixtures/sort/human-numeric-whitespace.expected @@ -0,0 +1,11 @@ + + + + + + + +456K +4568K + 456M + 6.2G diff --git a/tests/fixtures/sort/human-numeric-whitespace.expected.debug b/tests/fixtures/sort/human-numeric-whitespace.expected.debug new file mode 100644 index 000000000..66afcda66 --- /dev/null +++ b/tests/fixtures/sort/human-numeric-whitespace.expected.debug @@ -0,0 +1,33 @@ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key +456K +____ +____ +4568K +_____ +_____ +>>>456M + ____ +_______ + 6.2G + ____ +__________________ diff --git a/tests/fixtures/sort/human-numeric-whitespace.txt b/tests/fixtures/sort/human-numeric-whitespace.txt new file mode 100644 index 000000000..19db648b1 --- /dev/null +++ b/tests/fixtures/sort/human-numeric-whitespace.txt @@ -0,0 +1,11 @@ + + +456K + + 456M + + +4568K + + 6.2G + diff --git a/tests/fixtures/sort/human_block_sizes.expected b/tests/fixtures/sort/human_block_sizes.expected index 74fad9fdf..5b4f8bb83 100644 --- a/tests/fixtures/sort/human_block_sizes.expected +++ b/tests/fixtures/sort/human_block_sizes.expected @@ -1,3 +1,5 @@ +0K +K 844K 981K 11M diff --git a/tests/fixtures/sort/human_block_sizes.expected.debug b/tests/fixtures/sort/human_block_sizes.expected.debug new file mode 100644 index 000000000..398ff9db4 --- /dev/null +++ b/tests/fixtures/sort/human_block_sizes.expected.debug @@ -0,0 +1,39 @@ +0K +__ +__ +K +^ no match for key +_ +844K +____ +____ +981K +____ +____ +11M +___ +___ +13M +___ +___ +14M +___ +___ +16M +___ +___ +18M +___ +___ +19M +___ +___ +20M +___ +___ +981T +____ +____ +20P +___ +___ diff --git a/tests/fixtures/sort/human_block_sizes.txt b/tests/fixtures/sort/human_block_sizes.txt index 803666dbe..a5adb9b5e 100644 --- a/tests/fixtures/sort/human_block_sizes.txt +++ b/tests/fixtures/sort/human_block_sizes.txt @@ -9,3 +9,5 @@ 844K 981K 13M +K +0K \ No newline at end of file diff --git a/tests/fixtures/sort/ignore_case.expected.debug b/tests/fixtures/sort/ignore_case.expected.debug new file mode 100644 index 000000000..08f0abb8d --- /dev/null +++ b/tests/fixtures/sort/ignore_case.expected.debug @@ -0,0 +1,21 @@ +aaa +___ +___ +BBB +___ +___ +ccc +___ +___ +DDD +___ +___ +eee +___ +___ +FFF +___ +___ +ggg +___ +___ diff --git a/tests/fixtures/sort/keys_blanks.expected b/tests/fixtures/sort/keys_blanks.expected new file mode 100644 index 000000000..1789659c4 --- /dev/null +++ b/tests/fixtures/sort/keys_blanks.expected @@ -0,0 +1,3 @@ + cab + abc + bca diff --git a/tests/fixtures/sort/keys_blanks.expected.debug b/tests/fixtures/sort/keys_blanks.expected.debug new file mode 100644 index 000000000..bb09ea8a2 --- /dev/null +++ b/tests/fixtures/sort/keys_blanks.expected.debug @@ -0,0 +1,9 @@ +>cab + __ +____ +>abc + __ +____ +>bca + __ +____ diff --git a/tests/fixtures/sort/keys_blanks.txt b/tests/fixtures/sort/keys_blanks.txt new file mode 100644 index 000000000..7c4f313a3 --- /dev/null +++ b/tests/fixtures/sort/keys_blanks.txt @@ -0,0 +1,3 @@ + abc + cab + bca diff --git a/tests/fixtures/sort/keys_closed_range.expected b/tests/fixtures/sort/keys_closed_range.expected new file mode 100644 index 000000000..45005621b --- /dev/null +++ b/tests/fixtures/sort/keys_closed_range.expected @@ -0,0 +1,6 @@ +dd aa ff +gg aa cc +aa bb cc +èè éé èè +👩‍🔬 👩‍🔬 👩‍🔬 +💣💣 💣💣 💣💣 diff --git a/tests/fixtures/sort/keys_closed_range.expected.debug b/tests/fixtures/sort/keys_closed_range.expected.debug new file mode 100644 index 000000000..b78db4af1 --- /dev/null +++ b/tests/fixtures/sort/keys_closed_range.expected.debug @@ -0,0 +1,18 @@ +dd aa ff + _ +________ +gg aa cc + _ +________ +aa bb cc + _ +________ +èè éé èè + _ +________ +👩‍🔬 👩‍🔬 👩‍🔬 + __ +______________ +💣💣 💣💣 💣💣 + __ +______________ diff --git a/tests/fixtures/sort/keys_closed_range.txt b/tests/fixtures/sort/keys_closed_range.txt new file mode 100644 index 000000000..d6bf40785 --- /dev/null +++ b/tests/fixtures/sort/keys_closed_range.txt @@ -0,0 +1,6 @@ +aa bb cc +dd aa ff +gg aa cc +èè éé èè +💣💣 💣💣 💣💣 +👩‍🔬 👩‍🔬 👩‍🔬 \ No newline at end of file diff --git a/tests/fixtures/sort/keys_custom_separator.expected b/tests/fixtures/sort/keys_custom_separator.expected new file mode 100644 index 000000000..2aba42033 --- /dev/null +++ b/tests/fixtures/sort/keys_custom_separator.expected @@ -0,0 +1,3 @@ +ddxaaxff +ggxaaxcc +aaxbbxcc diff --git a/tests/fixtures/sort/keys_custom_separator.expected.debug b/tests/fixtures/sort/keys_custom_separator.expected.debug new file mode 100644 index 000000000..5d4dbc776 --- /dev/null +++ b/tests/fixtures/sort/keys_custom_separator.expected.debug @@ -0,0 +1,9 @@ +ddxaaxff + _ +________ +ggxaaxcc + _ +________ +aaxbbxcc + _ +________ diff --git a/tests/fixtures/sort/keys_custom_separator.txt b/tests/fixtures/sort/keys_custom_separator.txt new file mode 100644 index 000000000..a8f473061 --- /dev/null +++ b/tests/fixtures/sort/keys_custom_separator.txt @@ -0,0 +1,3 @@ +aaxbbxcc +ddxaaxff +ggxaaxcc diff --git a/tests/fixtures/sort/keys_ignore_flag.expected b/tests/fixtures/sort/keys_ignore_flag.expected new file mode 100644 index 000000000..964d04663 --- /dev/null +++ b/tests/fixtures/sort/keys_ignore_flag.expected @@ -0,0 +1,2 @@ + 1a +1A diff --git a/tests/fixtures/sort/keys_ignore_flag.expected.debug b/tests/fixtures/sort/keys_ignore_flag.expected.debug new file mode 100644 index 000000000..6d0da711f --- /dev/null +++ b/tests/fixtures/sort/keys_ignore_flag.expected.debug @@ -0,0 +1,6 @@ + 1a + _ +___ +1A +_ +__ diff --git a/tests/fixtures/sort/keys_ignore_flag.txt b/tests/fixtures/sort/keys_ignore_flag.txt new file mode 100644 index 000000000..964d04663 --- /dev/null +++ b/tests/fixtures/sort/keys_ignore_flag.txt @@ -0,0 +1,2 @@ + 1a +1A diff --git a/tests/fixtures/sort/keys_multiple_ranges.expected b/tests/fixtures/sort/keys_multiple_ranges.expected new file mode 100644 index 000000000..09e4e8729 --- /dev/null +++ b/tests/fixtures/sort/keys_multiple_ranges.expected @@ -0,0 +1,6 @@ +gg aa cc +dd aa ff +aa bb cc +èè éé èè +👩‍🔬 👩‍🔬 👩‍🔬 +💣💣 💣💣 💣💣 diff --git a/tests/fixtures/sort/keys_multiple_ranges.expected.debug b/tests/fixtures/sort/keys_multiple_ranges.expected.debug new file mode 100644 index 000000000..830e9afd0 --- /dev/null +++ b/tests/fixtures/sort/keys_multiple_ranges.expected.debug @@ -0,0 +1,24 @@ +gg aa cc + ___ + ___ +________ +dd aa ff + ___ + ___ +________ +aa bb cc + ___ + ___ +________ +èè éé èè + ___ + ___ +________ +👩‍🔬 👩‍🔬 👩‍🔬 + _____ + _____ +______________ +💣💣 💣💣 💣💣 + _____ + _____ +______________ diff --git a/tests/fixtures/sort/keys_multiple_ranges.txt b/tests/fixtures/sort/keys_multiple_ranges.txt new file mode 100644 index 000000000..d6bf40785 --- /dev/null +++ b/tests/fixtures/sort/keys_multiple_ranges.txt @@ -0,0 +1,6 @@ +aa bb cc +dd aa ff +gg aa cc +èè éé èè +💣💣 💣💣 💣💣 +👩‍🔬 👩‍🔬 👩‍🔬 \ No newline at end of file diff --git a/tests/fixtures/sort/keys_negative_size.expected b/tests/fixtures/sort/keys_negative_size.expected new file mode 100644 index 000000000..3774da60e --- /dev/null +++ b/tests/fixtures/sort/keys_negative_size.expected @@ -0,0 +1 @@ +a b c diff --git a/tests/fixtures/sort/keys_negative_size.expected.debug b/tests/fixtures/sort/keys_negative_size.expected.debug new file mode 100644 index 000000000..e392ec7f3 --- /dev/null +++ b/tests/fixtures/sort/keys_negative_size.expected.debug @@ -0,0 +1,3 @@ +a b c + ^ no match for key +_____ diff --git a/tests/fixtures/sort/keys_negative_size.txt b/tests/fixtures/sort/keys_negative_size.txt new file mode 100644 index 000000000..3774da60e --- /dev/null +++ b/tests/fixtures/sort/keys_negative_size.txt @@ -0,0 +1 @@ +a b c diff --git a/tests/fixtures/sort/keys_no_char_match.expected b/tests/fixtures/sort/keys_no_char_match.expected new file mode 100644 index 000000000..dcb361837 --- /dev/null +++ b/tests/fixtures/sort/keys_no_char_match.expected @@ -0,0 +1,3 @@ +c +ba +aaa diff --git a/tests/fixtures/sort/keys_no_char_match.expected.debug b/tests/fixtures/sort/keys_no_char_match.expected.debug new file mode 100644 index 000000000..5287a0de9 --- /dev/null +++ b/tests/fixtures/sort/keys_no_char_match.expected.debug @@ -0,0 +1,9 @@ +c + ^ no match for key +_ +ba + _ +__ +aaa + __ +___ diff --git a/tests/fixtures/sort/keys_no_char_match.txt b/tests/fixtures/sort/keys_no_char_match.txt new file mode 100644 index 000000000..1c952a6b8 --- /dev/null +++ b/tests/fixtures/sort/keys_no_char_match.txt @@ -0,0 +1,3 @@ +aaa +ba +c diff --git a/tests/fixtures/sort/keys_no_field_match.expected b/tests/fixtures/sort/keys_no_field_match.expected new file mode 100644 index 000000000..e2f183e13 --- /dev/null +++ b/tests/fixtures/sort/keys_no_field_match.expected @@ -0,0 +1,6 @@ +aa bb cc +dd aa ff +gg aa cc +èè éé èè +👩‍🔬 👩‍🔬 👩‍🔬 +💣💣 💣💣 💣💣 diff --git a/tests/fixtures/sort/keys_no_field_match.expected.debug b/tests/fixtures/sort/keys_no_field_match.expected.debug new file mode 100644 index 000000000..60197b1de --- /dev/null +++ b/tests/fixtures/sort/keys_no_field_match.expected.debug @@ -0,0 +1,18 @@ +aa bb cc + ^ no match for key +________ +dd aa ff + ^ no match for key +________ +gg aa cc + ^ no match for key +________ +èè éé èè + ^ no match for key +________ +👩‍🔬 👩‍🔬 👩‍🔬 + ^ no match for key +______________ +💣💣 💣💣 💣💣 + ^ no match for key +______________ diff --git a/tests/fixtures/sort/keys_no_field_match.txt b/tests/fixtures/sort/keys_no_field_match.txt new file mode 100644 index 000000000..d6bf40785 --- /dev/null +++ b/tests/fixtures/sort/keys_no_field_match.txt @@ -0,0 +1,6 @@ +aa bb cc +dd aa ff +gg aa cc +èè éé èè +💣💣 💣💣 💣💣 +👩‍🔬 👩‍🔬 👩‍🔬 \ No newline at end of file diff --git a/tests/fixtures/sort/keys_open_ended.expected b/tests/fixtures/sort/keys_open_ended.expected new file mode 100644 index 000000000..09e4e8729 --- /dev/null +++ b/tests/fixtures/sort/keys_open_ended.expected @@ -0,0 +1,6 @@ +gg aa cc +dd aa ff +aa bb cc +èè éé èè +👩‍🔬 👩‍🔬 👩‍🔬 +💣💣 💣💣 💣💣 diff --git a/tests/fixtures/sort/keys_open_ended.expected.debug b/tests/fixtures/sort/keys_open_ended.expected.debug new file mode 100644 index 000000000..d3a56ffd6 --- /dev/null +++ b/tests/fixtures/sort/keys_open_ended.expected.debug @@ -0,0 +1,18 @@ +gg aa cc + ____ +________ +dd aa ff + ____ +________ +aa bb cc + ____ +________ +èè éé èè + ____ +________ +👩‍🔬 👩‍🔬 👩‍🔬 + _______ +______________ +💣💣 💣💣 💣💣 + _______ +______________ diff --git a/tests/fixtures/sort/keys_open_ended.txt b/tests/fixtures/sort/keys_open_ended.txt new file mode 100644 index 000000000..d6bf40785 --- /dev/null +++ b/tests/fixtures/sort/keys_open_ended.txt @@ -0,0 +1,6 @@ +aa bb cc +dd aa ff +gg aa cc +èè éé èè +💣💣 💣💣 💣💣 +👩‍🔬 👩‍🔬 👩‍🔬 \ No newline at end of file diff --git a/tests/fixtures/sort/merge_stable.expected b/tests/fixtures/sort/merge_stable.expected new file mode 100644 index 000000000..49f57888d --- /dev/null +++ b/tests/fixtures/sort/merge_stable.expected @@ -0,0 +1,3 @@ +0a +0c +0b diff --git a/tests/fixtures/sort/merge_stable_1.txt b/tests/fixtures/sort/merge_stable_1.txt new file mode 100644 index 000000000..20528104f --- /dev/null +++ b/tests/fixtures/sort/merge_stable_1.txt @@ -0,0 +1,2 @@ +0a +0c \ No newline at end of file diff --git a/tests/fixtures/sort/merge_stable_2.txt b/tests/fixtures/sort/merge_stable_2.txt new file mode 100644 index 000000000..d3523d976 --- /dev/null +++ b/tests/fixtures/sort/merge_stable_2.txt @@ -0,0 +1 @@ +0b \ No newline at end of file diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric.expected b/tests/fixtures/sort/mixed_floats_ints_chars_numeric.expected new file mode 100644 index 000000000..59541af32 --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric.expected @@ -0,0 +1,30 @@ +-2028789030 +-896689 +-8.90880 +-1 +-.05 + + + + + + + + +000 +CARAvan +00000001 +1 +1.040000000 +1.444 +1.58590 +8.013 +45 +46.89 +576,446.88800000 +576,446.890 + 4567. + 37800 +4798908.340000000000 +4798908.45 +4798908.8909800 diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric.expected.debug b/tests/fixtures/sort/mixed_floats_ints_chars_numeric.expected.debug new file mode 100644 index 000000000..b7b76e589 --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric.expected.debug @@ -0,0 +1,90 @@ +-2028789030 +___________ +___________ +-896689 +_______ +_______ +-8.90880 +________ +________ +-1 +__ +__ +-.05 +____ +____ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key +000 +___ +___ +CARAvan +^ no match for key +_______ +00000001 +________ +________ +1 +_ +_ +1.040000000 +___________ +___________ +1.444 +_____ +_____ +1.58590 +_______ +_______ +8.013 +_____ +_____ +45 +__ +__ +46.89 +_____ +_____ +576,446.88800000 +___ +________________ +576,446.890 +___ +___________ + 4567. + _____ +____________________ +>>>>37800 + _____ +_________ +4798908.340000000000 +____________________ +____________________ +4798908.45 +__________ +__________ +4798908.8909800 +_______________ +_______________ diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric.txt b/tests/fixtures/sort/mixed_floats_ints_chars_numeric.txt new file mode 100644 index 000000000..a5813ea3a --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric.txt @@ -0,0 +1,30 @@ +576,446.890 +576,446.88800000 + + + 4567. +45 +46.89 +-1 +1 +00000001 +4798908.340000000000 +4798908.45 +4798908.8909800 + + + 37800 + +-2028789030 +-896689 +CARAvan + +-8.90880 +-.05 +1.444 +1.58590 +1.040000000 + +8.013 + +000 diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.expected b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.expected new file mode 100644 index 000000000..0ccdd84c0 --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.expected @@ -0,0 +1,33 @@ +-2028789030 +-896689 +-8.90880 +-1 +-.05 + + + + + +-0 +CARAvan +-0 + + + +000 +-0 +1 +00000001 +1.040000000 +1.444 +1.58590 +8.013 +45 +46.89 +576,446.890 +576,446.88800000 + 4567. + 37800 +4798908.340000000000 +4798908.45 +4798908.8909800 diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.expected.debug b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.expected.debug new file mode 100644 index 000000000..66a98b208 --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.expected.debug @@ -0,0 +1,66 @@ +-2028789030 +___________ +-896689 +_______ +-8.90880 +________ +-1 +__ +-.05 +____ + +^ no match for key + +^ no match for key + +^ no match for key + +^ no match for key + +^ no match for key +-0 +__ +CARAvan +^ no match for key +-0 +__ + +^ no match for key + +^ no match for key + +^ no match for key +000 +___ +-0 +__ +1 +_ +00000001 +________ +1.040000000 +___________ +1.444 +_____ +1.58590 +_______ +8.013 +_____ +45 +__ +46.89 +_____ +576,446.890 +___ +576,446.88800000 +___ + 4567. + _____ +>>>>37800 + _____ +4798908.340000000000 +____________________ +4798908.45 +__________ +4798908.8909800 +_______________ diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.txt b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.txt new file mode 100644 index 000000000..44667ef03 --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_stable.txt @@ -0,0 +1,33 @@ +576,446.890 +576,446.88800000 + + + 4567. +45 +46.89 +-1 +1 +00000001 +4798908.340000000000 +4798908.45 +4798908.8909800 + + + 37800 + +-2028789030 +-896689 +-0 +CARAvan +-0 + +-8.90880 +-.05 +1.444 +1.58590 +1.040000000 + +8.013 + +000 +-0 \ No newline at end of file diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.expected b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.expected new file mode 100644 index 000000000..cd4256c5f --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.expected @@ -0,0 +1,19 @@ +-2028789030 +-896689 +-8.90880 +-1 +-.05 + +1 +1.040000000 +1.444 +1.58590 +8.013 +45 +46.89 +576,446.890 + 4567. + 37800 +4798908.340000000000 +4798908.45 +4798908.8909800 diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.expected.debug b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.expected.debug new file mode 100644 index 000000000..663a4b3a9 --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.expected.debug @@ -0,0 +1,38 @@ +-2028789030 +___________ +-896689 +_______ +-8.90880 +________ +-1 +__ +-.05 +____ + +^ no match for key +1 +_ +1.040000000 +___________ +1.444 +_____ +1.58590 +_______ +8.013 +_____ +45 +__ +46.89 +_____ +576,446.890 +___ + 4567. + _____ +>>>>37800 + _____ +4798908.340000000000 +____________________ +4798908.45 +__________ +4798908.8909800 +_______________ diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.txt b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.txt new file mode 100644 index 000000000..a5813ea3a --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique.txt @@ -0,0 +1,30 @@ +576,446.890 +576,446.88800000 + + + 4567. +45 +46.89 +-1 +1 +00000001 +4798908.340000000000 +4798908.45 +4798908.8909800 + + + 37800 + +-2028789030 +-896689 +CARAvan + +-8.90880 +-.05 +1.444 +1.58590 +1.040000000 + +8.013 + +000 diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.expected b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.expected new file mode 100644 index 000000000..97e261f14 --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.expected @@ -0,0 +1,19 @@ +4798908.8909800 +4798908.45 +4798908.340000000000 + 37800 + 4567. +576,446.890 +46.89 +45 +8.013 +1.58590 +1.444 +1.040000000 +1 + +-.05 +-1 +-8.90880 +-896689 +-2028789030 diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.expected.debug b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.expected.debug new file mode 100644 index 000000000..01f7abf5b --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.expected.debug @@ -0,0 +1,38 @@ +4798908.8909800 +_______________ +4798908.45 +__________ +4798908.340000000000 +____________________ +>>>>37800 + _____ + 4567. + _____ +576,446.890 +___ +46.89 +_____ +45 +__ +8.013 +_____ +1.58590 +_______ +1.444 +_____ +1.040000000 +___________ +1 +_ + +^ no match for key +-.05 +____ +-1 +__ +-8.90880 +________ +-896689 +_______ +-2028789030 +___________ diff --git a/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.txt b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.txt new file mode 100644 index 000000000..a5813ea3a --- /dev/null +++ b/tests/fixtures/sort/mixed_floats_ints_chars_numeric_unique_reverse.txt @@ -0,0 +1,30 @@ +576,446.890 +576,446.88800000 + + + 4567. +45 +46.89 +-1 +1 +00000001 +4798908.340000000000 +4798908.45 +4798908.8909800 + + + 37800 + +-2028789030 +-896689 +CARAvan + +-8.90880 +-.05 +1.444 +1.58590 +1.040000000 + +8.013 + +000 diff --git a/tests/fixtures/sort/month_default.expected b/tests/fixtures/sort/month_default.expected index dc51f5d5e..99baa235a 100644 --- a/tests/fixtures/sort/month_default.expected +++ b/tests/fixtures/sort/month_default.expected @@ -1,3 +1,4 @@ + asdf N/A Ut enim ad minim veniam, quis Jan Lorem ipsum dolor sit amet mar laboris nisi ut aliquip ex ea diff --git a/tests/fixtures/sort/month_default.expected.debug b/tests/fixtures/sort/month_default.expected.debug new file mode 100644 index 000000000..f28ba1c48 --- /dev/null +++ b/tests/fixtures/sort/month_default.expected.debug @@ -0,0 +1,33 @@ + asdf + ^ no match for key +_____ +N/A Ut enim ad minim veniam, quis +^ no match for key +_________________________________ +Jan Lorem ipsum dolor sit amet +___ +______________________________ +mar laboris nisi ut aliquip ex ea +___ +_________________________________ +May sed do eiusmod tempor incididunt +___ +____________________________________ +JUN nostrud exercitation ullamco +___ +________________________________ +Jul 1 should remain 2,1,3 +___ +_________________________ +Jul 2 these three lines +___ +_______________________ +Jul 3 if --stable is provided +___ +_____________________________ +Oct ut labore et dolore magna aliqua +___ +____________________________________ +Dec consectetur adipiscing elit +___ +_______________________________ diff --git a/tests/fixtures/sort/month_default.txt b/tests/fixtures/sort/month_default.txt index 6d64bf4f8..c96bad300 100644 --- a/tests/fixtures/sort/month_default.txt +++ b/tests/fixtures/sort/month_default.txt @@ -8,3 +8,4 @@ mar laboris nisi ut aliquip ex ea Jul 2 these three lines Jul 1 should remain 2,1,3 Jul 3 if --stable is provided + asdf diff --git a/tests/fixtures/sort/month_stable.expected b/tests/fixtures/sort/month_stable.expected index 868728a18..cb3a8da8f 100644 --- a/tests/fixtures/sort/month_stable.expected +++ b/tests/fixtures/sort/month_stable.expected @@ -1,4 +1,5 @@ N/A Ut enim ad minim veniam, quis + asdf Jan Lorem ipsum dolor sit amet mar laboris nisi ut aliquip ex ea May sed do eiusmod tempor incididunt diff --git a/tests/fixtures/sort/month_stable.expected.debug b/tests/fixtures/sort/month_stable.expected.debug new file mode 100644 index 000000000..740f9187c --- /dev/null +++ b/tests/fixtures/sort/month_stable.expected.debug @@ -0,0 +1,22 @@ +N/A Ut enim ad minim veniam, quis +^ no match for key + asdf + ^ no match for key +Jan Lorem ipsum dolor sit amet +___ +mar laboris nisi ut aliquip ex ea +___ +May sed do eiusmod tempor incididunt +___ +JUN nostrud exercitation ullamco +___ +Jul 2 these three lines +___ +Jul 1 should remain 2,1,3 +___ +Jul 3 if --stable is provided +___ +Oct ut labore et dolore magna aliqua +___ +Dec consectetur adipiscing elit +___ diff --git a/tests/fixtures/sort/month_stable.txt b/tests/fixtures/sort/month_stable.txt index 6d64bf4f8..c96bad300 100644 --- a/tests/fixtures/sort/month_stable.txt +++ b/tests/fixtures/sort/month_stable.txt @@ -8,3 +8,4 @@ mar laboris nisi ut aliquip ex ea Jul 2 these three lines Jul 1 should remain 2,1,3 Jul 3 if --stable is provided + asdf diff --git a/tests/fixtures/sort/months-dedup.expected b/tests/fixtures/sort/months-dedup.expected new file mode 100644 index 000000000..dfb693492 --- /dev/null +++ b/tests/fixtures/sort/months-dedup.expected @@ -0,0 +1,6 @@ + +JAN +apr +MAY +JUNNNN +AUG diff --git a/tests/fixtures/sort/months-dedup.expected.debug b/tests/fixtures/sort/months-dedup.expected.debug new file mode 100644 index 000000000..aded4b951 --- /dev/null +++ b/tests/fixtures/sort/months-dedup.expected.debug @@ -0,0 +1,12 @@ + +^ no match for key +JAN +___ +apr +___ +MAY +___ +JUNNNN +___ +AUG +___ diff --git a/tests/fixtures/sort/months-dedup.txt b/tests/fixtures/sort/months-dedup.txt new file mode 100644 index 000000000..ebef388b9 --- /dev/null +++ b/tests/fixtures/sort/months-dedup.txt @@ -0,0 +1,37 @@ +JAN + +0000000 + +00 + +0001 + +1 + +-1.4 + +JUNNNN +AUG + +apr + +APR + + +MAY +1890777 + +56908-90078 + +51887300- + +6780.0009866 + +789----009999 90-0 90-0 + +6780.000986 + +100M +7800900K +2G +.2T diff --git a/tests/fixtures/sort/months-whitespace.expected b/tests/fixtures/sort/months-whitespace.expected new file mode 100644 index 000000000..84a44d564 --- /dev/null +++ b/tests/fixtures/sort/months-whitespace.expected @@ -0,0 +1,8 @@ + + +JAN + FEb + apr + apr + JUNNNN +AUG diff --git a/tests/fixtures/sort/months-whitespace.expected.debug b/tests/fixtures/sort/months-whitespace.expected.debug new file mode 100644 index 000000000..ef626f505 --- /dev/null +++ b/tests/fixtures/sort/months-whitespace.expected.debug @@ -0,0 +1,24 @@ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key +JAN +___ +___ + FEb + ___ +_____ + apr + ___ +____ + apr + ___ +____ +>>>JUNNNN + ___ +_________ +AUG +___ +____ diff --git a/tests/fixtures/sort/months-whitespace.txt b/tests/fixtures/sort/months-whitespace.txt new file mode 100644 index 000000000..45c477477 --- /dev/null +++ b/tests/fixtures/sort/months-whitespace.txt @@ -0,0 +1,8 @@ +JAN + JUNNNN +AUG + + apr + apr + + FEb diff --git a/tests/fixtures/sort/multiple_decimals.expected b/tests/fixtures/sort/multiple_decimals.expected new file mode 100644 index 000000000..6afbdcaa0 --- /dev/null +++ b/tests/fixtures/sort/multiple_decimals.expected @@ -0,0 +1,33 @@ +-2028789030 +-896689 +-8.90880 +-1 +-.05 + + + + + + + + +000 +CARAvan +00000001 +1 +1.040000000 +1.444 +1.58590 +8.013 +45 +46.89 + 4567..457 + 4567. +4567.1 +4567.34 + 37800 +576,446.88800000 +576,446.890 +4798908.340000000000 +4798908.45 +4798908.8909800 diff --git a/tests/fixtures/sort/multiple_decimals_general.expected b/tests/fixtures/sort/multiple_decimals_general.expected new file mode 100644 index 000000000..b08ada324 --- /dev/null +++ b/tests/fixtures/sort/multiple_decimals_general.expected @@ -0,0 +1,37 @@ + + + + + + + +CARAvan + NaN + -inf +-2028789030 +-896689 +-8.90880 +-1 +-.05 +000 +00000001 +1 +1.040000000 +1.444 +1.58590 +8.013 +45 +46.89 +576,446.88800000 +576,446.890 + 4567..457 + 4567. +4567.1 +4567.34 + 37800 + 45670.89079.098 + 45670.89079.1 +4798908.340000000000 +4798908.45 +4798908.8909800 +inf diff --git a/tests/fixtures/sort/multiple_decimals_general.expected.debug b/tests/fixtures/sort/multiple_decimals_general.expected.debug new file mode 100644 index 000000000..1bf5d2669 --- /dev/null +++ b/tests/fixtures/sort/multiple_decimals_general.expected.debug @@ -0,0 +1,111 @@ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key +CARAvan +^ no match for key +_______ + NaN + ___ +_____ +>-inf + ____ +_____ +-2028789030 +___________ +___________ +-896689 +_______ +_______ +-8.90880 +________ +________ +-1 +__ +__ +-.05 +____ +____ +000 +___ +___ +00000001 +________ +________ +1 +_ +_ +1.040000000 +___________ +___________ +1.444 +_____ +_____ +1.58590 +_______ +_______ +8.013 +_____ +_____ +45 +__ +__ +46.89 +_____ +_____ +576,446.88800000 +___ +________________ +576,446.890 +___ +___________ +>>>>>>>>>>4567..457 + _____ +___________________ + 4567. + _____ +____________________ +4567.1 +______ +______ +4567.34 +_______ +_______ +>>>>37800 + _____ +_________ +>>>>>>45670.89079.098 + ___________ +_____________________ +>>>>>>45670.89079.1 + ___________ +___________________ +4798908.340000000000 +____________________ +____________________ +4798908.45 +__________ +__________ +4798908.8909800 +_______________ +_______________ +inf +___ +___ diff --git a/tests/fixtures/sort/multiple_decimals_general.txt b/tests/fixtures/sort/multiple_decimals_general.txt new file mode 100644 index 000000000..0feb0ce7f --- /dev/null +++ b/tests/fixtures/sort/multiple_decimals_general.txt @@ -0,0 +1,37 @@ +576,446.890 +576,446.88800000 + +4567.1 + 4567..457 + 45670.89079.1 + 45670.89079.098 +4567.34 + 4567. +45 +46.89 +-1 +1 +00000001 +4798908.340000000000 +4798908.45 +4798908.8909800 + + + 37800 + +-2028789030 +-896689 +CARAvan + +-8.90880 +-.05 +1.444 +1.58590 +1.040000000 + +8.013 + +000 + NaN +inf + -inf diff --git a/tests/fixtures/sort/multiple_decimals_numeric.expected b/tests/fixtures/sort/multiple_decimals_numeric.expected new file mode 100644 index 000000000..8f42e7ce5 --- /dev/null +++ b/tests/fixtures/sort/multiple_decimals_numeric.expected @@ -0,0 +1,35 @@ +-2028789030 +-896689 +-8.90880 +-1 +-.05 + + + + + + + + +000 +CARAvan +00000001 +1 +1.040000000 +1.444 +1.58590 +8.013 +45 +46.89 +576,446.88800000 +576,446.890 + 4567..457 + 4567. +4567.1 +4567.34 + 37800 + 45670.89079.098 + 45670.89079.1 +4798908.340000000000 +4798908.45 +4798908.8909800 diff --git a/tests/fixtures/sort/multiple_decimals_numeric.expected.debug b/tests/fixtures/sort/multiple_decimals_numeric.expected.debug new file mode 100644 index 000000000..948c4869c --- /dev/null +++ b/tests/fixtures/sort/multiple_decimals_numeric.expected.debug @@ -0,0 +1,105 @@ +-2028789030 +___________ +___________ +-896689 +_______ +_______ +-8.90880 +________ +________ +-1 +__ +__ +-.05 +____ +____ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key +000 +___ +___ +CARAvan +^ no match for key +_______ +00000001 +________ +________ +1 +_ +_ +1.040000000 +___________ +___________ +1.444 +_____ +_____ +1.58590 +_______ +_______ +8.013 +_____ +_____ +45 +__ +__ +46.89 +_____ +_____ +576,446.88800000 +___ +________________ +576,446.890 +___ +___________ +>>>>>>>>>>4567..457 + _____ +___________________ + 4567. + _____ +____________________ +4567.1 +______ +______ +4567.34 +_______ +_______ +>>>>37800 + _____ +_________ +>>>>>>45670.89079.098 + ___________ +_____________________ +>>>>>>45670.89079.1 + ___________ +___________________ +4798908.340000000000 +____________________ +____________________ +4798908.45 +__________ +__________ +4798908.8909800 +_______________ +_______________ diff --git a/tests/fixtures/sort/multiple_decimals_numeric.txt b/tests/fixtures/sort/multiple_decimals_numeric.txt new file mode 100644 index 000000000..4e65ecfda --- /dev/null +++ b/tests/fixtures/sort/multiple_decimals_numeric.txt @@ -0,0 +1,35 @@ +576,446.890 +576,446.88800000 + +4567.1 + 4567..457 + 45670.89079.1 + 45670.89079.098 +4567.34 + 4567. +45 +46.89 +-1 +1 +00000001 +4798908.340000000000 +4798908.45 +4798908.8909800 + + + 37800 + +-2028789030 +-896689 +CARAvan + +-8.90880 +-.05 +1.444 +1.58590 +1.040000000 + +8.013 + +000 + diff --git a/tests/fixtures/sort/no_trailing_newline1.txt b/tests/fixtures/sort/no_trailing_newline1.txt new file mode 100644 index 000000000..0a207c060 --- /dev/null +++ b/tests/fixtures/sort/no_trailing_newline1.txt @@ -0,0 +1,2 @@ +a +b \ No newline at end of file diff --git a/tests/fixtures/sort/no_trailing_newline2.txt b/tests/fixtures/sort/no_trailing_newline2.txt new file mode 100644 index 000000000..63d8dbd40 --- /dev/null +++ b/tests/fixtures/sort/no_trailing_newline2.txt @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/tests/fixtures/sort/numeric-floats-with-nan2.expected b/tests/fixtures/sort/numeric-floats-with-nan2.expected new file mode 100644 index 000000000..51c9985c3 --- /dev/null +++ b/tests/fixtures/sort/numeric-floats-with-nan2.expected @@ -0,0 +1,23 @@ +-8.90880 +-.05 + + + + + + + + + + + + + + +Karma +1 +1.0/0.0 +1.040000000 +1.2 +1.444 +1.58590 diff --git a/tests/fixtures/sort/numeric-floats-with-nan2.expected.debug b/tests/fixtures/sort/numeric-floats-with-nan2.expected.debug new file mode 100644 index 000000000..b5a2c2f64 --- /dev/null +++ b/tests/fixtures/sort/numeric-floats-with-nan2.expected.debug @@ -0,0 +1,69 @@ +-8.90880 +________ +________ +-.05 +____ +____ + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key + +^ no match for key +^ no match for key +Karma +^ no match for key +_____ +1 +_ +_ +1.0/0.0 +___ +_______ +1.040000000 +___________ +___________ +1.2 +___ +___ +1.444 +_____ +_____ +1.58590 +_______ +_______ diff --git a/tests/fixtures/sort/numeric-floats-with-nan2.txt b/tests/fixtures/sort/numeric-floats-with-nan2.txt new file mode 100644 index 000000000..9b78741fe --- /dev/null +++ b/tests/fixtures/sort/numeric-floats-with-nan2.txt @@ -0,0 +1,23 @@ +Karma + +1.0/0.0 + + +-8.90880 + + +-.05 + + +1.040000000 + +1.444 + + +1.58590 + + +1 + +1.2 + diff --git a/tests/fixtures/sort/numeric_fixed_floats.expected.debug b/tests/fixtures/sort/numeric_fixed_floats.expected.debug new file mode 100644 index 000000000..fa8a909c5 --- /dev/null +++ b/tests/fixtures/sort/numeric_fixed_floats.expected.debug @@ -0,0 +1,6 @@ +.00 +___ +___ +.01 +___ +___ diff --git a/tests/fixtures/sort/numeric_floats.expected.debug b/tests/fixtures/sort/numeric_floats.expected.debug new file mode 100644 index 000000000..e24056376 --- /dev/null +++ b/tests/fixtures/sort/numeric_floats.expected.debug @@ -0,0 +1,6 @@ +.02 +___ +___ +.03 +___ +___ diff --git a/tests/fixtures/sort/numeric_floats_and_ints.expected.debug b/tests/fixtures/sort/numeric_floats_and_ints.expected.debug new file mode 100644 index 000000000..c43d6bfb6 --- /dev/null +++ b/tests/fixtures/sort/numeric_floats_and_ints.expected.debug @@ -0,0 +1,6 @@ +0 +_ +_ +.02 +___ +___ diff --git a/tests/fixtures/sort/numeric_floats_with_nan.expected.debug b/tests/fixtures/sort/numeric_floats_with_nan.expected.debug new file mode 100644 index 000000000..07e72db53 --- /dev/null +++ b/tests/fixtures/sort/numeric_floats_with_nan.expected.debug @@ -0,0 +1,9 @@ +NaN +^ no match for key +___ +.02 +___ +___ +.03 +___ +___ diff --git a/tests/fixtures/sort/numeric_trailing_chars.expected b/tests/fixtures/sort/numeric_trailing_chars.expected new file mode 100644 index 000000000..96226fd38 --- /dev/null +++ b/tests/fixtures/sort/numeric_trailing_chars.expected @@ -0,0 +1,5 @@ +-.05, +-x +0.abc +0foo +100 diff --git a/tests/fixtures/sort/numeric_trailing_chars.expected.debug b/tests/fixtures/sort/numeric_trailing_chars.expected.debug new file mode 100644 index 000000000..0226a636f --- /dev/null +++ b/tests/fixtures/sort/numeric_trailing_chars.expected.debug @@ -0,0 +1,15 @@ +-.05, +____ +_____ +-x +^ no match for key +__ +0.abc +__ +_____ +0foo +_ +____ +100 +___ +____ diff --git a/tests/fixtures/sort/numeric_trailing_chars.txt b/tests/fixtures/sort/numeric_trailing_chars.txt new file mode 100644 index 000000000..d6fe3e48d --- /dev/null +++ b/tests/fixtures/sort/numeric_trailing_chars.txt @@ -0,0 +1,5 @@ +0foo +0.abc +100 +-.05, +-x \ No newline at end of file diff --git a/tests/fixtures/sort/numeric_unfixed_floats.expected.debug b/tests/fixtures/sort/numeric_unfixed_floats.expected.debug new file mode 100644 index 000000000..a60daf623 --- /dev/null +++ b/tests/fixtures/sort/numeric_unfixed_floats.expected.debug @@ -0,0 +1,6 @@ +.000 +____ +____ +.01 +___ +___ diff --git a/tests/fixtures/sort/numeric_unique.expected b/tests/fixtures/sort/numeric_unique.expected new file mode 100644 index 000000000..8a31187f6 --- /dev/null +++ b/tests/fixtures/sort/numeric_unique.expected @@ -0,0 +1,2 @@ +-10 bb +aa diff --git a/tests/fixtures/sort/numeric_unique.expected.debug b/tests/fixtures/sort/numeric_unique.expected.debug new file mode 100644 index 000000000..79ec70364 --- /dev/null +++ b/tests/fixtures/sort/numeric_unique.expected.debug @@ -0,0 +1,4 @@ +-10 bb +___ +aa +^ no match for key diff --git a/tests/fixtures/sort/numeric_unique.txt b/tests/fixtures/sort/numeric_unique.txt new file mode 100644 index 000000000..15cc08022 --- /dev/null +++ b/tests/fixtures/sort/numeric_unique.txt @@ -0,0 +1,3 @@ +aa +-10 bb +-10 aa diff --git a/tests/fixtures/sort/numeric_unsorted_ints.expected.debug b/tests/fixtures/sort/numeric_unsorted_ints.expected.debug new file mode 100644 index 000000000..b16931f5e --- /dev/null +++ b/tests/fixtures/sort/numeric_unsorted_ints.expected.debug @@ -0,0 +1,300 @@ +1 +_ +_ +2 +_ +_ +3 +_ +_ +4 +_ +_ +5 +_ +_ +6 +_ +_ +7 +_ +_ +8 +_ +_ +9 +_ +_ +10 +__ +__ +11 +__ +__ +12 +__ +__ +13 +__ +__ +14 +__ +__ +15 +__ +__ +16 +__ +__ +17 +__ +__ +18 +__ +__ +19 +__ +__ +20 +__ +__ +21 +__ +__ +22 +__ +__ +23 +__ +__ +24 +__ +__ +25 +__ +__ +26 +__ +__ +27 +__ +__ +28 +__ +__ +29 +__ +__ +30 +__ +__ +31 +__ +__ +32 +__ +__ +33 +__ +__ +34 +__ +__ +35 +__ +__ +36 +__ +__ +37 +__ +__ +38 +__ +__ +39 +__ +__ +40 +__ +__ +41 +__ +__ +42 +__ +__ +43 +__ +__ +44 +__ +__ +45 +__ +__ +46 +__ +__ +47 +__ +__ +48 +__ +__ +49 +__ +__ +50 +__ +__ +51 +__ +__ +52 +__ +__ +53 +__ +__ +54 +__ +__ +55 +__ +__ +56 +__ +__ +57 +__ +__ +58 +__ +__ +59 +__ +__ +60 +__ +__ +61 +__ +__ +62 +__ +__ +63 +__ +__ +64 +__ +__ +65 +__ +__ +66 +__ +__ +67 +__ +__ +68 +__ +__ +69 +__ +__ +70 +__ +__ +71 +__ +__ +72 +__ +__ +73 +__ +__ +74 +__ +__ +75 +__ +__ +76 +__ +__ +77 +__ +__ +78 +__ +__ +79 +__ +__ +80 +__ +__ +81 +__ +__ +82 +__ +__ +83 +__ +__ +84 +__ +__ +85 +__ +__ +86 +__ +__ +87 +__ +__ +88 +__ +__ +89 +__ +__ +90 +__ +__ +91 +__ +__ +92 +__ +__ +93 +__ +__ +94 +__ +__ +95 +__ +__ +96 +__ +__ +97 +__ +__ +98 +__ +__ +99 +__ +__ +100 +___ +___ diff --git a/tests/fixtures/sort/numeric_unsorted_ints_unique.expected.debug b/tests/fixtures/sort/numeric_unsorted_ints_unique.expected.debug new file mode 100644 index 000000000..072a57ccf --- /dev/null +++ b/tests/fixtures/sort/numeric_unsorted_ints_unique.expected.debug @@ -0,0 +1,8 @@ +1 +_ +2 +_ +3 +_ +4 +_ diff --git a/tests/fixtures/sort/version-empty-lines.expected b/tests/fixtures/sort/version-empty-lines.expected new file mode 100644 index 000000000..c496c0ff5 --- /dev/null +++ b/tests/fixtures/sort/version-empty-lines.expected @@ -0,0 +1,11 @@ + + + + + + + +1.2.3-alpha +1.2.3-alpha2 +11.2.3 + 1.12.4 diff --git a/tests/fixtures/sort/version-empty-lines.txt b/tests/fixtures/sort/version-empty-lines.txt new file mode 100644 index 000000000..9b6b89788 --- /dev/null +++ b/tests/fixtures/sort/version-empty-lines.txt @@ -0,0 +1,11 @@ +11.2.3 + + + +1.2.3-alpha2 + + +1.2.3-alpha + + + 1.12.4 diff --git a/tests/fixtures/sort/version.expected.debug b/tests/fixtures/sort/version.expected.debug new file mode 100644 index 000000000..2d922b5c0 --- /dev/null +++ b/tests/fixtures/sort/version.expected.debug @@ -0,0 +1,12 @@ +1.2.3-alpha +___________ +___________ +1.2.3-alpha2 +____________ +____________ +1.12.4 +______ +______ +11.2.3 +______ +______ diff --git a/tests/fixtures/sort/words_unique.expected b/tests/fixtures/sort/words_unique.expected new file mode 100644 index 000000000..2444ce1c6 --- /dev/null +++ b/tests/fixtures/sort/words_unique.expected @@ -0,0 +1,3 @@ +aaa +bbb +zzz diff --git a/tests/fixtures/sort/words_unique.expected.debug b/tests/fixtures/sort/words_unique.expected.debug new file mode 100644 index 000000000..0c32daf74 --- /dev/null +++ b/tests/fixtures/sort/words_unique.expected.debug @@ -0,0 +1,6 @@ +aaa +___ +bbb +___ +zzz +___ diff --git a/tests/fixtures/sort/words_unique.txt b/tests/fixtures/sort/words_unique.txt new file mode 100644 index 000000000..9c6666029 --- /dev/null +++ b/tests/fixtures/sort/words_unique.txt @@ -0,0 +1,4 @@ +zzz +aaa +bbb +bbb diff --git a/tests/fixtures/sort/zero-terminated.expected b/tests/fixtures/sort/zero-terminated.expected new file mode 100644 index 000000000..4e53b304b Binary files /dev/null and b/tests/fixtures/sort/zero-terminated.expected differ diff --git a/tests/fixtures/sort/zero-terminated.expected.debug b/tests/fixtures/sort/zero-terminated.expected.debug new file mode 100644 index 000000000..fbef272b0 --- /dev/null +++ b/tests/fixtures/sort/zero-terminated.expected.debug @@ -0,0 +1,84 @@ +../.. +_____ +../../by-util +_____________ +../../common +____________ +../../fixtures +______________ +../../fixtures/cat +__________________ +../../fixtures/cksum +____________________ +../../fixtures/comm +___________________ +../../fixtures/cp +_________________ +../../fixtures/cp/dir_with_mount +________________________________ +../../fixtures/cp/dir_with_mount/copy_me +________________________________________ +../../fixtures/cp/hello_dir +___________________________ +../../fixtures/cp/hello_dir_with_file +_____________________________________ +../../fixtures/csplit +_____________________ +../../fixtures/cut +__________________ +../../fixtures/cut/sequences +____________________________ +../../fixtures/dircolors +________________________ +../../fixtures/du +_________________ +../../fixtures/du/subdir +________________________ +../../fixtures/du/subdir/deeper +_______________________________ +../../fixtures/du/subdir/links +______________________________ +../../fixtures/env +__________________ +../../fixtures/expand +_____________________ +../../fixtures/fmt +__________________ +../../fixtures/fold +___________________ +../../fixtures/hashsum +______________________ +../../fixtures/head +___________________ +../../fixtures/join +___________________ +../../fixtures/mv +_________________ +../../fixtures/nl +_________________ +../../fixtures/numfmt +_____________________ +../../fixtures/od +_________________ +../../fixtures/paste +____________________ +../../fixtures/ptx +__________________ +../../fixtures/shuf +___________________ +../../fixtures/sort +___________________ +../../fixtures/sum +__________________ +../../fixtures/tac +__________________ +../../fixtures/tail +___________________ +../../fixtures/tsort +____________________ +../../fixtures/unexpand +_______________________ +../../fixtures/uniq +___________________ +../../fixtures/wc +_________________ diff --git a/tests/fixtures/sort/zero-terminated.txt b/tests/fixtures/sort/zero-terminated.txt new file mode 100644 index 000000000..5c547c851 Binary files /dev/null and b/tests/fixtures/sort/zero-terminated.txt differ diff --git a/tests/fixtures/tail/foobar_follow_multiple_appended.expected b/tests/fixtures/tail/foobar_follow_multiple_appended.expected index 0896d3743..891eb6920 100644 --- a/tests/fixtures/tail/foobar_follow_multiple_appended.expected +++ b/tests/fixtures/tail/foobar_follow_multiple_appended.expected @@ -1,4 +1,4 @@ ==> foobar.txt <== -doce -trece +twenty +thirty diff --git a/tests/fixtures/test/non_empty_file b/tests/fixtures/test/non_empty_file new file mode 100644 index 000000000..34425caf6 --- /dev/null +++ b/tests/fixtures/test/non_empty_file @@ -0,0 +1 @@ +Not empty! diff --git a/tests/fixtures/test/regular_file b/tests/fixtures/test/regular_file new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/unexpand/with_spaces.txt b/tests/fixtures/unexpand/with_spaces.txt new file mode 100644 index 000000000..3f39671a1 --- /dev/null +++ b/tests/fixtures/unexpand/with_spaces.txt @@ -0,0 +1,2 @@ + abc d e f g \t\t A + \ No newline at end of file diff --git a/tests/fixtures/uniq/group-append.expected b/tests/fixtures/uniq/group-append.expected new file mode 100644 index 000000000..62f53e69f --- /dev/null +++ b/tests/fixtures/uniq/group-append.expected @@ -0,0 +1,26 @@ + aaaaa ⅰ + + bbbbb ⅱ + bbbbb ⅱ + + ccccc ⅲ + ccccc ⅲ + ccccc ⅲ + + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + + eeeee ⅲ + + fffff ⅲ + fffff ⅲ + + ggggg ⅲ + ggggg ⅲ + ggggg ⅲ + + GGGGG ⅲ + GGGGG ⅲ + diff --git a/tests/fixtures/uniq/group-both.expected b/tests/fixtures/uniq/group-both.expected new file mode 100644 index 000000000..8a0f06bf2 --- /dev/null +++ b/tests/fixtures/uniq/group-both.expected @@ -0,0 +1,27 @@ + + aaaaa ⅰ + + bbbbb ⅱ + bbbbb ⅱ + + ccccc ⅲ + ccccc ⅲ + ccccc ⅲ + + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + + eeeee ⅲ + + fffff ⅲ + fffff ⅲ + + ggggg ⅲ + ggggg ⅲ + ggggg ⅲ + + GGGGG ⅲ + GGGGG ⅲ + diff --git a/tests/fixtures/uniq/group-prepend.expected b/tests/fixtures/uniq/group-prepend.expected new file mode 100644 index 000000000..5209f7fbe --- /dev/null +++ b/tests/fixtures/uniq/group-prepend.expected @@ -0,0 +1,26 @@ + + aaaaa ⅰ + + bbbbb ⅱ + bbbbb ⅱ + + ccccc ⅲ + ccccc ⅲ + ccccc ⅲ + + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + + eeeee ⅲ + + fffff ⅲ + fffff ⅲ + + ggggg ⅲ + ggggg ⅲ + ggggg ⅲ + + GGGGG ⅲ + GGGGG ⅲ diff --git a/tests/fixtures/uniq/group.expected b/tests/fixtures/uniq/group.expected new file mode 100644 index 000000000..145a78011 --- /dev/null +++ b/tests/fixtures/uniq/group.expected @@ -0,0 +1,25 @@ + aaaaa ⅰ + + bbbbb ⅱ + bbbbb ⅱ + + ccccc ⅲ + ccccc ⅲ + ccccc ⅲ + + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + ddddd ⅲ + + eeeee ⅲ + + fffff ⅲ + fffff ⅲ + + ggggg ⅲ + ggggg ⅲ + ggggg ⅲ + + GGGGG ⅲ + GGGGG ⅲ diff --git a/tests/fixtures/wc/emptyfile.txt b/tests/fixtures/wc/emptyfile.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/wc/manyemptylines.txt b/tests/fixtures/wc/manyemptylines.txt new file mode 100644 index 000000000..716f02896 --- /dev/null +++ b/tests/fixtures/wc/manyemptylines.txt @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/fixtures/wc/notrailingnewline.txt b/tests/fixtures/wc/notrailingnewline.txt new file mode 100644 index 000000000..789819226 --- /dev/null +++ b/tests/fixtures/wc/notrailingnewline.txt @@ -0,0 +1 @@ +a diff --git a/tests/fixtures/wc/onelongemptyline.txt b/tests/fixtures/wc/onelongemptyline.txt new file mode 100644 index 000000000..f93ac3c2c --- /dev/null +++ b/tests/fixtures/wc/onelongemptyline.txt @@ -0,0 +1 @@ + diff --git a/tests/fixtures/wc/onelongword.txt b/tests/fixtures/wc/onelongword.txt new file mode 100644 index 000000000..9d693a7ca --- /dev/null +++ b/tests/fixtures/wc/onelongword.txt @@ -0,0 +1 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa diff --git a/util/GHA-delete-GNU-workflow-logs.sh b/util/GHA-delete-GNU-workflow-logs.sh new file mode 100644 index 000000000..19e3311d4 --- /dev/null +++ b/util/GHA-delete-GNU-workflow-logs.sh @@ -0,0 +1,44 @@ +#!/bin/sh + +# spell-checker:ignore (utils) gitsome jq ; (gh) repos + +ME="${0}" +ME_dir="$(dirname -- "${ME}")" +ME_parent_dir="$(dirname -- "${ME_dir}")" +ME_parent_dir_abs="$(realpath -mP -- "${ME_parent_dir}")" + +# ref: + +# note: requires `gh` and `jq` + +## tools available? + +# * `gh` available? +unset GH +gh --version 1>/dev/null 2>&1 +if [ $? -eq 0 ]; then export GH="gh"; fi + +# * `jq` available? +unset JQ +jq --version 1>/dev/null 2>&1 +if [ $? -eq 0 ]; then export JQ="jq"; fi + +if [ -z "${GH}" ] || [ -z "${JQ}" ]; then + if [ -z "${GH}" ]; then + echo 'ERR!: missing `gh` (see install instructions at )' 1>&2 + fi + if [ -z "${JQ}" ]; then + echo 'ERR!: missing `jq` (install with `sudo apt install jq`)' 1>&2 + fi + exit 1 +fi + +dry_run=true + +USER_NAME=uutils +REPO_NAME=coreutils +WORK_NAME=GNU + +# * `--paginate` retrieves all pages +# gh api --paginate "repos/${USER_NAME}/${REPO_NAME}/actions/runs" | jq -r ".workflow_runs[] | select(.name == \"${WORK_NAME}\") | (.id)" | xargs -n1 sh -c "for arg do { echo gh api repos/${USER_NAME}/${REPO_NAME}/actions/runs/\${arg} -X DELETE ; if [ -z "$dry_run" ]; then gh api repos/${USER_NAME}/${REPO_NAME}/actions/runs/\${arg} -X DELETE ; fi ; } ; done ;" _ +gh api "repos/${USER_NAME}/${REPO_NAME}/actions/runs" | jq -r ".workflow_runs[] | select(.name == \"${WORK_NAME}\") | (.id)" | xargs -n1 sh -c "for arg do { echo gh api repos/${USER_NAME}/${REPO_NAME}/actions/runs/\${arg} -X DELETE ; if [ -z "$dry_run" ]; then gh api repos/${USER_NAME}/${REPO_NAME}/actions/runs/\${arg} -X DELETE ; fi ; } ; done ;" _ diff --git a/util/build-gnu.sh b/util/build-gnu.sh new file mode 100644 index 000000000..798a33456 --- /dev/null +++ b/util/build-gnu.sh @@ -0,0 +1,121 @@ +#!/bin/bash + +# spell-checker:ignore (paths) abmon deref discrim eacces getlimits getopt ginstall gnulib inacc infloop inotify reflink ; (misc) INT_OFLOW OFLOW ; (vars/env) BUILDDIR SRCDIR + +set -e +if test ! -d ../gnu; then + echo "Could not find ../gnu" + echo "git clone git@github.com:coreutils/coreutils.git gnu" + exit 1 +fi +if test ! -d ../gnulib; then + echo "Could not find ../gnulib" + echo "git clone git@github.com:coreutils/gnulib.git gnulib" + exit 1 +fi + + +pushd $(pwd) +make PROFILE=release +BUILDDIR="$PWD/target/release/" +cp "${BUILDDIR}/install" "${BUILDDIR}/ginstall" # The GNU tests rename this script before running, to avoid confusion with the make target +# Create *sum binaries +for sum in b2sum md5sum sha1sum sha224sum sha256sum sha384sum sha512sum +do + sum_path="${BUILDDIR}/${sum}" + test -f "${sum_path}" || cp "${BUILDDIR}/hashsum" "${sum_path}" +done +test -f "${BUILDDIR}/[" || cp "${BUILDDIR}/test" "${BUILDDIR}/[" +popd +GNULIB_SRCDIR="$PWD/../gnulib" +pushd ../gnu/ + +# Any binaries that aren't built become `false` so their tests fail +for binary in $(./build-aux/gen-lists-of-programs.sh --list-progs) +do + bin_path="${BUILDDIR}/${binary}" + test -f "${bin_path}" || { echo "'${binary}' was not built with uutils, using the 'false' program"; cp "${BUILDDIR}/false" "${bin_path}"; } +done + +./bootstrap --gnulib-srcdir="$GNULIB_SRCDIR" +./configure --quiet --disable-gcc-warnings +#Add timeout to to protect against hangs +sed -i 's|"\$@|/usr/bin/timeout 600 "\$@|' build-aux/test-driver +# Change the PATH in the Makefile to test the uutils coreutils instead of the GNU coreutils +sed -i "s/^[[:blank:]]*PATH=.*/ PATH='${BUILDDIR//\//\\/}\$(PATH_SEPARATOR)'\"\$\$PATH\" \\\/" Makefile +sed -i 's| tr | /usr/bin/tr |' tests/init.sh +make -j "$(nproc)" +# Generate the factor tests, so they can be fixed +# Used to be 36. Reduced to 20 to decrease the log size +for i in {00..20} +do + make tests/factor/t${i}.sh +done + +# strip the long stuff +for i in {21..36} +do + sed -i -e "s/\$(tf)\/t${i}.sh//g" Makefile +done + + +grep -rl 'path_prepend_' tests/* | xargs sed -i 's| path_prepend_ ./src||' +sed -i -e 's|^seq |/usr/bin/seq |' -e 's|sha1sum |/usr/bin/sha1sum |' tests/factor/t*sh + +# Remove tests checking for --version & --help +# Not really interesting for us and logs are too big +sed -i -e '/tests\/misc\/invalid-opt.pl/ D' \ + -e '/tests\/misc\/help-version.sh/ D' \ + -e '/tests\/misc\/help-version-getopt.sh/ D' \ + Makefile + +# logs are clotted because of this test +sed -i -e '/tests\/misc\/seq-precision.sh/ D' \ + Makefile + +# printf doesn't limit the values used in its arg, so this produced ~2GB of output +sed -i '/INT_OFLOW/ D' tests/misc/printf.sh + +# Use the system coreutils where the test fails due to error in a util that is not the one being tested +sed -i 's|stat|/usr/bin/stat|' tests/chgrp/basic.sh tests/cp/existing-perm-dir.sh tests/touch/60-seconds.sh tests/misc/sort-compress-proc.sh +sed -i 's|ls -|/usr/bin/ls -|' tests/chgrp/posix-H.sh tests/chown/deref.sh tests/cp/same-file.sh tests/misc/mknod.sh tests/mv/part-symlink.sh tests/du/8gb.sh +sed -i 's|mkdir |/usr/bin/mkdir |' tests/cp/existing-perm-dir.sh tests/rm/empty-inacc.sh +sed -i 's|timeout \([[:digit:]]\)| /usr/bin/timeout \1|' tests/tail-2/inotify-rotate.sh tests/tail-2/inotify-dir-recreate.sh tests/tail-2/inotify-rotate-resources.sh tests/cp/parent-perm-race.sh tests/ls/infloop.sh tests/misc/sort-exit-early.sh tests/misc/sort-NaN-infloop.sh tests/misc/uniq-perf.sh tests/tail-2/inotify-only-regular.sh tests/tail-2/pipe-f2.sh tests/tail-2/retry.sh tests/tail-2/symlink.sh tests/tail-2/wait.sh tests/tail-2/pid.sh tests/dd/stats.sh tests/tail-2/follow-name.sh tests/misc/shuf.sh # Don't break the function called 'grep_timeout' +sed -i 's|chmod |/usr/bin/chmod |' tests/du/inacc-dir.sh tests/mkdir/p-3.sh tests/tail-2/tail-n0f.sh tests/cp/fail-perm.sh tests/du/inaccessible-cwd.sh tests/mv/i-2.sh tests/chgrp/basic.sh tests/misc/shuf.sh +sed -i 's|sort |/usr/bin/sort |' tests/ls/hyperlink.sh tests/misc/test-N.sh +sed -i 's|split |/usr/bin/split |' tests/misc/factor-parallel.sh +sed -i 's|truncate |/usr/bin/truncate |' tests/split/fail.sh +sed -i 's|dd |/usr/bin/dd |' tests/du/8gb.sh tests/tail-2/big-4gb.sh init.cfg +sed -i 's|id -|/usr/bin/id -|' tests/misc/runcon-no-reorder.sh +sed -i 's|touch |/usr/bin/touch |' tests/cp/preserve-link.sh tests/cp/reflink-perm.sh tests/ls/block-size.sh tests/ls/abmon-align.sh tests/ls/rt-1.sh tests/mv/update.sh tests/misc/ls-time.sh tests/misc/stat-nanoseconds.sh tests/misc/time-style.sh tests/misc/test-N.sh +sed -i 's|ln -|/usr/bin/ln -|' tests/cp/link-deref.sh +sed -i 's|printf |/usr/bin/printf |' tests/dd/ascii.sh +sed -i 's|cp |/usr/bin/cp |' tests/mv/hard-2.sh +sed -i 's|paste |/usr/bin/paste |' tests/misc/od-endian.sh +sed -i 's|seq |/usr/bin/seq |' tests/misc/sort-discrim.sh + +# Add specific timeout to tests that currently hang to limit time spent waiting +sed -i 's|seq \$|/usr/bin/timeout 0.1 seq \$|' tests/misc/seq-precision.sh tests/misc/seq-long-double.sh +sed -i 's|cat |/usr/bin/timeout 0.1 cat |' tests/misc/cat-self.sh + + +# Remove dup of /usr/bin/ when executed several times +grep -rl '/usr/bin//usr/bin/' tests/* | xargs --no-run-if-empty sed -i 's|/usr/bin//usr/bin/|/usr/bin/|g' + + +#### Adjust tests to make them work with Rust/coreutils +# in some cases, what we are doing in rust/coreutils is good (or better) +# we should not regress our project just to match what GNU is going. +# So, do some changes on the fly + +sed -i -e "s|rm: cannot remove 'e/slink'|rm: cannot remove 'e'|g" tests/rm/fail-eacces.sh + +sed -i -e "s|rm: cannot remove 'a/b/file'|rm: cannot remove 'a'|g" tests/rm/cycle.sh + +sed -i -e "s|rm: cannot remove directory 'b/a/p'|rm: cannot remove 'b'|g" tests/rm/rm1.sh + +sed -i -e "s|rm: cannot remove 'a/1'|rm: cannot remove 'a'|g" tests/rm/rm2.sh + +sed -i -e "s|removed directory 'a/'|removed directory 'a'|g" tests/rm/v-slash.sh + +test -f "${BUILDDIR}/getlimits" || cp src/getlimits "${BUILDDIR}" diff --git a/util/rewrite_rules.rs b/util/rewrite_rules.rs index 8b29bbe92..3d680e24c 100644 --- a/util/rewrite_rules.rs +++ b/util/rewrite_rules.rs @@ -1,4 +1,4 @@ -//! Rules to update the codebase using Rerast +//! Rules to update the codebase using `rerast` /// Converts try!() to ? fn try_to_question_mark>(r: Result) -> Result { diff --git a/util/run-gnu-test.sh b/util/run-gnu-test.sh new file mode 100644 index 000000000..9d51a983e --- /dev/null +++ b/util/run-gnu-test.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# spell-checker:ignore (env/vars) BUILDDIR GNULIB SUBDIRS +set -e +BUILDDIR="${PWD}/uutils/target/release" +GNULIB_DIR="${PWD}/gnulib" +pushd gnu + +export RUST_BACKTRACE=1 + +if test -n "$1"; then + # if set, run only the test passed + export RUN_TEST="TESTS=$1" +fi + +timeout -sKILL 2h make -j "$(nproc)" check $RUN_TEST SUBDIRS=. RUN_EXPENSIVE_TESTS=yes RUN_VERY_EXPENSIVE_TESTS=yes VERBOSE=no || : # Kill after 4 hours in case something gets stuck in make diff --git a/util/update-version.sh b/util/update-version.sh index 584d13608..c71467fc4 100644 --- a/util/update-version.sh +++ b/util/update-version.sh @@ -3,24 +3,22 @@ # So, it should be triple-checked -FROM="0.0.3" -TO="0.0.4" +FROM="0.0.5" +TO="0.0.6" -UUCORE_FROM="0.0.6" -UUCORE_TO="0.0.7" +UUCORE_FROM="0.0.7" +UUCORE_TO="0.0.8" PROGS=$(ls -1d src/uu/*/Cargo.toml src/uu/stdbuf/src/libstdbuf/Cargo.toml Cargo.toml) # update the version of all programs sed -i -e "s|version = \"$FROM\"|version = \"$TO\"|" $PROGS -# Update the stbuff stuff +# Update the stdbuf stuff sed -i -e "s|libstdbuf = { version=\"$FROM\"|libstdbuf = { version=\"$TO\"|" src/uu/stdbuf/Cargo.toml sed -i -e "s|= { optional=true, version=\"$FROM\", package=\"uu_|= { optional=true, version=\"$TO\", package=\"uu_|g" Cargo.toml # Update uucore itself -sed -i -e "s|version = \"$UUCORE_FROM\"|version = \"$UUCORE_TO\"|" src/uucore/Cargo.toml +#sed -i -e "s|version = \"$UUCORE_FROM\"|version = \"$UUCORE_TO\"|" src/uucore/Cargo.toml # Update crates using uucore -sed -i -e "s|uucore = { version=\">=$UUCORE_FROM\",|uucore = { version=\">=$UUCORE_TO\",|" $PROGS - - +#sed -i -e "s|uucore = { version=\">=$UUCORE_FROM\",|uucore = { version=\">=$UUCORE_TO\",|" $PROGS