pathrs 0.2.4

C-friendly API to make path resolution safer on Linux.
Documentation
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
# SPDX-License-Identifier: MPL-2.0
#
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2025 SUSE LLC
# Copyright (C) 2026 Aleksa Sarai <cyphar@cyphar.com>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

on:
  push:
    branches: [ main ]
    tags:
      - 'v*'
  pull_request:
    branches: [ main ]
  release:
    types: [ published ]
  schedule:
    - cron: '0 0 * * *'

name: rust-ci

env:
  RUST_MSRV: &RUST_MSRV "1.63"

jobs:
  codespell:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: pip install codespell==v2.3.0
      - run: codespell -L crate

  check:
    name: cargo check (stable)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: taiki-e/install-action@cargo-hack
      - name: cargo check
        run: >-
          cargo hack --workspace --each-feature --keep-going \
            check --all-targets

  check-msrv:
    name: cargo check (msrv)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: *RUST_MSRV
      - uses: taiki-e/install-action@cargo-hack
      - name: cargo check
        run: >-
          cargo hack --each-feature --keep-going \
            check --all-targets

  check-cross:
    strategy:
      fail-fast: false
      matrix:
        target:
          - x86_64-unknown-linux-gnu
          - x86_64-unknown-linux-musl
          - aarch64-unknown-linux-musl
          - arm-unknown-linux-gnueabi
          - arm-unknown-linux-gnueabihf
          - armv7-unknown-linux-gnueabihf
          - i686-unknown-linux-gnu
          - loongarch64-unknown-linux-gnu
          - loongarch64-unknown-linux-musl
          - powerpc-unknown-linux-gnu
          - powerpc64-unknown-linux-gnu
          - powerpc64le-unknown-linux-gnu
          - riscv64gc-unknown-linux-gnu
          - sparc64-unknown-linux-gnu
          - s390x-unknown-linux-gnu
    name: cargo check (${{ matrix.target }})
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          # TODO: Should we use MSRV for this?
          targets: ${{ matrix.target }}
      - uses: taiki-e/install-action@cargo-hack
      - name: cargo check --target=${{ matrix.target }}
        run: >-
          cargo hack --each-feature --keep-going \
            check --target=${{ matrix.target }} --all-targets
      - name: cargo build --target=${{ matrix.target }}
        run: >-
          cargo hack --each-feature --keep-going \
            build --target=${{ matrix.target }} --release

  fmt:
    name: rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      # We need to use nightly Rust to check the formatting.
      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: rustfmt
      - run: cargo fmt --all -- --check

  clippy:
    name: clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      # Pin the Rust version to avoid Rust updates breaking our clippy lints.
      - uses: dtolnay/rust-toolchain@1.88
        with:
          components: clippy
      - uses: taiki-e/install-action@cargo-hack
      - name: cargo clippy
        run: >-
          cargo hack --workspace --each-feature --keep-going \
            clippy --all-targets

  check-lint-nohack:
    name: make lint (no cargo-hack)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt,clippy
      - name: install cbindgen
        run: cargo install --force cbindgen
      - name: make lint
        run: make CARGO_NIGHTLY=cargo lint

  validate-cbindgen:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - name: install cbindgen
        run: cargo install --force cbindgen
      - run: make validate-cbindgen

  validate-elf-symbols:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        rust-version:
          - *RUST_MSRV
          - "1.72" # loongarch64 global_asm! stabilised
          - "1.84" # arm64ex / s390x global_asm! stabilised
          - "1.91" # loongarch32 global_asm! stabilised
          - stable
          - nightly
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust-version }}
      - name: install lld
        run: sudo apt-get install -y lld
      - run: make validate-elf-symbols

  validate-keyring:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: make validate-keyring

  validate-dist-release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - run: make dist-release
      - name: check release artefacts
        run: |-
          ls -la release/*/
          cat release/*/libpathrs.sha256sum

  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        rust-version:
          - *RUST_MSRV # debian bookworm (oldstable)
          - "1.75" # centos stream 8
          - "1.85" # debian trixie (stable)
          - stable
          - nightly
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust-version }}
      - name: install lld
        run: sudo apt-get install -y lld
      - run: make debug
      - run: make release

  rustdoc:
    name: cargo doc
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo doc --document-private-items --workspace --all-features
      - name: upload docs
        uses: actions/upload-artifact@v7
        with:
          name: rustdoc
          path: target/doc

  nextest-archive:
    strategy:
      fail-fast: false
      matrix:
        run-as:
          - unpriv
          - root
    name: cargo nextest archive (${{ matrix.run-as }})
    runs-on: ubuntu-latest
    env:
      FEATURES: >-
        capi
        _test_race
        ${{ matrix.run-as == 'root' && '_test_as_root' || '' }}
    steps:
      - uses: actions/checkout@v6
      # Nightly rust is required for llvm-cov --doc.
      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: llvm-tools
      - uses: taiki-e/install-action@cargo-llvm-cov
      - uses: taiki-e/install-action@nextest
      - name: cargo nextest archive
        run: >-
          cargo llvm-cov \
            nextest-archive \
              --workspace \
              -F "${{ env.FEATURES }}" \
              --archive-file nextest-pathrs-${{ matrix.run-as }}.tar.zst
      - name: upload nextest archive
        uses: actions/upload-artifact@v7
        with:
          name: nextest-archive-${{ matrix.run-as }}
          path: nextest-pathrs-${{ matrix.run-as }}.tar.zst
          retention-days: 7 # no need to waste disk space

  doctest:
    name: cargo test --doc
    runs-on: ubuntu-latest
    env:
      CARGO_NIGHTLY: cargo
    steps:
      - uses: actions/checkout@v6
      # Nightly rust is required for llvm-cov --doc.
      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: llvm-tools
      - uses: taiki-e/install-action@cargo-llvm-cov
      - run: make test-rust-doctest
      - name: upload rust coverage (artifact)
        uses: actions/upload-artifact@v7
        with:
          name: profraw-${{ github.job }}-${{ strategy.job-index }}
          path: "target/llvm-cov-target/*.profraw"
          retention-days: 7 # no need to waste disk space

      # TODO: Upload to CodeCov. Unfortunately, "cargo test --doc" does not
      # generate a binary that llvm-cov can use to generate coverage reports
      # from.

  compute-test-partitions:
    name: compute test partitions
    runs-on: ubuntu-latest
    outputs:
      tests: ${{ steps.test-partitions.outputs.data }}
    steps:
      - uses: actions/checkout@v6
      - name: compute test partitions
        id: test-partitions
        run: |-
          # Compute the default test set then convert each object to a string
          # so that we can double-fromJSON it (once as a list for
          # test.strategy, and once again for test.name and the actual test).
          partitions="$(./hack/ci-compute-test-partition.jq <<<"null")"
          jq -CS <<<"$partitions" # for debugging
          echo "data=$(jq -ScM 'map("\(.)")' <<<"$partitions")" >>"$GITHUB_OUTPUT"

  nextest:
    needs:
      - compute-test-partitions
      - nextest-archive
    strategy:
      fail-fast: false
      matrix:
        tests: ${{ fromJSON(needs.compute-test-partitions.outputs.tests) }}
        run-as:
          - unpriv
          - root
        enosys:
          - ""
          - openat2
          - statx
        exclude:
          # The statx tests are quite slow with statx disabled, and there is no
          # real benefit to including them since the fallback code is tested
          # elsewhere and our race tests don't try even to attack fdinfo.
          - enosys: statx
            tests: >-
              {"name":"race","pattern":"test(#tests::test_race*)"}

    env:
      NEXTEST_PATTERN_SPEC: ${{ fromJSON(matrix.tests).pattern }}
    name: >-
      cargo nextest
      ${{
          format('({0}, {1}{2})',
            fromJSON(matrix.tests).name,
            matrix.run-as,
            matrix.enosys && format(', {0}=enosys', matrix.enosys) || '',
          )
      }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      # Nightly rust is required for llvm-cov --doc.
      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: llvm-tools
      - uses: taiki-e/install-action@cargo-llvm-cov
      - uses: taiki-e/install-action@nextest
      - name: install llvm-tools wrappers
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-binutils

      - name: pull nextest archive
        uses: actions/download-artifact@v8
        with:
          name: nextest-archive-${{ matrix.run-as }}
          path: .

      - name: rust unit tests (${{ matrix.run-as }})
        run: >-
          ./hack/rust-tests.sh \
            --cargo=cargo \
            ${{ matrix.run-as == 'root' && '--sudo' || '' }} \
            --enosys="${{ matrix.enosys }}" \
            --archive-file="nextest-pathrs-${{ matrix.run-as }}.tar.zst" \
            "${{ env.NEXTEST_PATTERN_SPEC }}"

      - name: upload rust coverage (artifact)
        uses: actions/upload-artifact@v7
        with:
          name: profraw-${{ github.job }}-${{ strategy.job-index }}
          path: "target/llvm-cov-target/*.profraw"
          retention-days: 7 # no need to waste disk space

      # FIXME: llvm-cov appears to have some kind of bug with
      # --nextest-archive-file as they do not strip the "target" prefix from
      # the nextest archive. As a workaround, we just extract it ourselves.
      - name: extract nextest archive
        run: >-
          tar xv -f nextest-pathrs-${{ matrix.run-as }}.tar.zst -C target/llvm-cov-target/ --strip-components=1
      # Upload to CodeCov.
      - name: generate codecov-friendly coverage
        id: codecov-coverage
        run: |-
          CODECOV_FILE="$(mktemp coverage-codecov.lcov.txt.XXXXXX)"
          cargo llvm-cov report --lcov --output-path="$CODECOV_FILE"
          echo "file=$CODECOV_FILE" >>"$GITHUB_OUTPUT"
      - name: upload rust coverage (codecov)
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          slug: cyphar/libpathrs
          files: ${{ steps.codecov-coverage.outputs.file }}

  # Smoke-test for our %check section in the libpathrs RPM.
  # <https://github.com/cyphar/libpathrs/issues/299>
  # TODO: I guess we should run this as root too...
  cargo-test:
    name: cargo test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - name: cargo test
        run: cargo test --features capi

  coverage:
    needs:
      - doctest
      - nextest
    name: compute coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      # Nightly rust is required for llvm-cov --doc.
      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: llvm-tools
      - uses: taiki-e/install-action@cargo-llvm-cov
      - name: install llvm-tools wrappers
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-binutils

      - name: pull rust coverage
        id: rust-coverage
        uses: actions/download-artifact@v8
        with:
          pattern: "profraw-*"
          path: profraw
      - name: merge coverage
        run: |-
          mkdir -p target/llvm-cov-target
          profraw_list="$(mktemp --tmpdir libpathrs-profraw.XXXXXXXX)"
          find "${{ steps.rust-coverage.outputs.download-path }}" -name '*.profraw' -type f >"$profraw_list"
          rust-profdata merge --sparse -f "$profraw_list" -o ./target/llvm-cov-target/libpathrs-combined.profraw
      - name: upload merged rust coverage
        uses: actions/upload-artifact@v7
        with:
          name: libpathrs-combined-profraw
          path: target/llvm-cov-target/libpathrs-combined.profraw
          retention-days: 7 # no need to waste disk space

      # FIXME: We just pull one version of the archive and use it for
      # generating coverage profiles, but this really is not correct because
      # the "root" and "unpriv" binaries are different and so the coverage data
      # is a little off. See <https://github.com/cyphar/libpathrs/issues/282>.
      - name: pull nextest archive
        uses: actions/download-artifact@v8
        with:
          name: nextest-archive-root
          path: .

      # FIXME: llvm-cov appears to have some kind of bug with
      # --nextest-archive-file as they do not strip the "target" prefix from
      # the nextest archive. As a workaround, we just extract it ourselves.
      - name: extract nextest archive
        run: >-
          tar xv -f nextest-pathrs-root.tar.zst -C target/llvm-cov-target/ --strip-components=1

      - name: calculate coverage
        run: cargo llvm-cov report
      - name: generate coverage html
        run: cargo llvm-cov report --html
      - name: upload coverage html
        uses: actions/upload-artifact@v7
        with:
          name: coverage-report
          path: target/llvm-cov/html

  examples:
    name: smoke-test examples
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo build --examples
      - run: make -C examples smoke-test-rust

  size:
    permissions:
      contents: read
      statuses: write
    strategy:
      fail-fast: false
      matrix:
        libtype: [ "cdylib", "staticlib" ]
    name: check ${{ matrix.libtype }} size
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - run: make release
      - name: compute ${{ matrix.libtype }} file name
        run: |-
          case "${{ matrix.libtype }}" in
            cdylib)
              libfile=libpathrs.so ;;
            staticlib)
              libfile=libpathrs.a ;;
            *)
              exit 1 ;;
          esac
          echo "LIB_FILENAME=$libfile" >>"$GITHUB_ENV"
      - name: strip ${{ matrix.libtype }}
        run: |-
          cp target/release/$LIB_FILENAME{,.nostrip}
          strip target/release/$LIB_FILENAME
      - name: compute ${{ matrix.libtype }} binary size
        run: |-
          LIB_SIZE="$(stat -c "%s" "target/release/$LIB_FILENAME" | numfmt --to=si --suffix=B)"
          LIB_NOSTRIP_SIZE="$(stat -c "%s" "target/release/$LIB_FILENAME.nostrip" | numfmt --to=si --suffix=B)"
          cat >&2 <<-EOF
          === binary sizes ===
          $LIB_FILENAME Size: $LIB_SIZE
          Unstripped: $LIB_NOSTRIP_SIZE
          EOF
          echo "LIB_SIZE=$LIB_SIZE" >>"$GITHUB_ENV"
          echo "LIB_NOSTRIP_SIZE=$LIB_NOSTRIP_SIZE" >>"$GITHUB_ENV"
      # At the moment, we can only attach the commit status for push operations
      # because pull requests don't get the right permissions in the default
      # GITHUB_TOKEN. It's not really clear to me how we should work around
      # this (secrets like access tokens are not provided for PRs from forked
      # repos) -- we probably need to switch to status checks?
      - if: github.event_name == 'push'
        name: update commit status
        uses: octokit/request-action@v2.x
        with:
          route: POST /repos/{owner_repo}/statuses/{sha}
          owner_repo: ${{ github.repository }}
          sha: ${{ github.sha }}
          state: success
          description: ${{ env.LIB_FILENAME }} (${{ matrix.libtype }}) is ${{ env.LIB_SIZE }} (${{ env.LIB_NOSTRIP_SIZE }} unstripped)
          context: rust-ci / ${{ matrix.libtype }} size
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  rust-complete:
    needs:
      - codespell
      - check
      - check-msrv
      - check-cross
      - fmt
      - clippy
      - check-lint-nohack
      - validate-cbindgen
      - validate-elf-symbols
      - validate-keyring
      - validate-dist-release
      - build
      - rustdoc
      - doctest
      - nextest
      - cargo-test
      - coverage
      - examples
      - size
    runs-on: ubuntu-latest
    steps:
      - run: echo "Rust CI jobs completed successfully."

  release-crate:
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    needs:
      - rust-complete
    runs-on: ubuntu-latest
    environment:
      name: release-crate
      url: "https://crates.io/crates/pathrs"
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}