Skip to content

Commit b31da50

Browse files
authored
Merge pull request #40 from bgilbert/workflow
workflows: test on multiple Rust versions; add lint checks; limit permissions to reading repo contents
2 parents 23a2b0d + da88c0a commit b31da50

File tree

3 files changed

+89
-28
lines changed

3 files changed

+89
-28
lines changed

.github/workflows/rust.yml

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,83 @@ on:
66
pull_request:
77
branches: [ main ]
88

9+
permissions:
10+
contents: read
11+
912
env:
1013
CARGO_TERM_COLOR: always
14+
# Minimum supported Rust version (MSRV)
15+
ACTIONS_MSRV_TOOLCHAIN: 1.49.0
16+
# Pinned toolchain for linting
17+
ACTIONS_LINTS_TOOLCHAIN: 1.53.0
1118

1219
jobs:
13-
build:
20+
tests-stable:
21+
name: "Tests, stable toolchain"
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v2
26+
- name: Install toolchain
27+
uses: actions-rs/toolchain@v1
28+
with:
29+
toolchain: stable
30+
default: true
31+
- name: cargo build
32+
run: cargo build
33+
- name: cargo test
34+
run: cargo test
35+
tests-msrv:
36+
name: "Tests, minimum supported toolchain"
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: Checkout repository
40+
uses: actions/checkout@v2
41+
- name: Install toolchain
42+
uses: actions-rs/toolchain@v1
43+
with:
44+
toolchain: ${{ env['ACTIONS_MSRV_TOOLCHAIN'] }}
45+
default: true
46+
- name: cargo build
47+
run: cargo build
48+
- name: cargo test
49+
run: cargo test
50+
lints:
51+
name: "Lints, pinned toolchain"
52+
runs-on: ubuntu-latest
53+
steps:
54+
- name: Checkout repository
55+
uses: actions/checkout@v2
56+
- name: Install toolchain
57+
uses: actions-rs/toolchain@v1
58+
with:
59+
toolchain: ${{ env['ACTIONS_LINTS_TOOLCHAIN'] }}
60+
default: true
61+
components: rustfmt, clippy
62+
- name: cargo fmt (check)
63+
run: cargo fmt -- --check -l
64+
- name: cargo clippy (warnings)
65+
run: cargo clippy -- -D warnings
66+
- name: cargo build
67+
run: cargo build
68+
tests-other-channels:
69+
name: "Tests, unstable toolchain"
1470
runs-on: ubuntu-latest
71+
continue-on-error: true
72+
strategy:
73+
matrix:
74+
channel:
75+
- beta
76+
- nightly
1577
steps:
16-
- uses: actions/checkout@v2
17-
- name: Build
18-
run: cargo build --verbose
19-
- name: Run tests
20-
run: cargo test --verbose
78+
- name: Checkout repository
79+
uses: actions/checkout@v2
80+
- name: Install toolchain
81+
uses: actions-rs/toolchain@v1
82+
with:
83+
toolchain: ${{ matrix.channel }}
84+
default: true
85+
- name: cargo build
86+
run: cargo build
87+
- name: cargo test
88+
run: cargo test

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ documentation = "http://docs.rs/openat-ext"
1616
[dependencies]
1717
openat = "0.1.15"
1818
libc = "^0.2.94"
19-
nix = ">= 0.18, < 0.23"
19+
nix = ">= 0.22, < 0.23"
2020
rand = "0.8.3"
2121

2222
[dev-dependencies]

src/lib.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
const TEMPFILE_ATTEMPTS: u32 = 100;
2020

21-
use libc;
22-
use nix;
23-
use openat;
2421
use rand::Rng;
2522
use std::ffi::OsStr;
2623
use std::fs::File;
@@ -140,7 +137,7 @@ pub trait OpenatDirExt {
140137

141138
/// Create a `FileWriter` which provides a `std::io::BufWriter` and then atomically creates
142139
/// the file at the destination, renaming it over an existing one if necessary.
143-
fn new_file_writer<'a>(&'a self, mode: libc::mode_t) -> io::Result<FileWriter>;
140+
fn new_file_writer(&self, mode: libc::mode_t) -> io::Result<FileWriter>;
144141

145142
/// Atomically create or replace the destination file, calling the provided
146143
/// function to generate the contents. Note that the contents of the
@@ -220,7 +217,7 @@ impl OpenatDirExt for openat::Dir {
220217
}
221218

222219
fn remove_file_optional<P: openat::AsPath>(&self, p: P) -> io::Result<bool> {
223-
Ok(impl_remove_file_optional(self, p)?)
220+
impl_remove_file_optional(self, p)
224221
}
225222

226223
fn remove_dir_optional<P: openat::AsPath>(&self, p: P) -> io::Result<bool> {
@@ -414,8 +411,8 @@ impl OpenatDirExt for openat::Dir {
414411
Ok(())
415412
}
416413

417-
fn new_file_writer<'a>(&'a self, mode: libc::mode_t) -> io::Result<FileWriter> {
418-
let (tmpf, name) = if let Some(tmpf) = self.new_unnamed_file(mode).ok() {
414+
fn new_file_writer(&self, mode: libc::mode_t) -> io::Result<FileWriter> {
415+
let (tmpf, name) = if let Ok(tmpf) = self.new_unnamed_file(mode) {
419416
(tmpf, None)
420417
} else {
421418
// FIXME allow this to be configurable
@@ -433,10 +430,7 @@ fn impl_read_to_string(mut f: File) -> io::Result<String> {
433430
}
434431

435432
fn map_nix_error(e: nix::Error) -> io::Error {
436-
match e.as_errno() {
437-
Some(os_err) => io::Error::from_raw_os_error(os_err as i32),
438-
_ => io::Error::new(io::ErrorKind::Other, e),
439-
}
433+
io::Error::from_raw_os_error(e as i32)
440434
}
441435

442436
fn copy_regfile_inner(
@@ -501,7 +495,7 @@ pub(crate) fn tempfile_in(
501495
match d.new_file(tmpname.as_str(), mode) {
502496
Ok(f) => return Ok((f, tmpname)),
503497
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
504-
Err(e) => Err(e)?,
498+
Err(e) => return Err(e),
505499
}
506500
}
507501
Err(io::Error::new(
@@ -518,7 +512,7 @@ pub(crate) fn tempfile_in(
518512
/// optimal case where all components exist above.
519513
pub(crate) fn impl_ensure_dir_all(d: &openat::Dir, p: &Path, mode: libc::mode_t) -> io::Result<()> {
520514
if let Some(parent) = p.parent() {
521-
if parent.as_os_str().len() > 0 {
515+
if !parent.as_os_str().is_empty() {
522516
impl_ensure_dir_all(d, parent, mode)?;
523517
}
524518
}
@@ -734,22 +728,22 @@ impl FileExt for File {
734728
let copy_result =
735729
copy_file_range(self.as_raw_fd(), None, to.as_raw_fd(), None, bytes_to_copy);
736730
if let Err(ref copy_err) = copy_result {
737-
match copy_err.as_errno() {
738-
Some(Errno::ENOSYS) | Some(Errno::EPERM) => {
731+
match copy_err {
732+
Errno::ENOSYS | Errno::EPERM => {
739733
HAS_COPY_FILE_RANGE.store(false, Ordering::Relaxed);
740734
}
741735
_ => {}
742736
}
743737
}
744738
copy_result
745739
} else {
746-
Err(nix::Error::from_errno(Errno::ENOSYS))
740+
Err(Errno::ENOSYS)
747741
};
748742
match copy_result {
749743
Ok(ret) => written += ret as u64,
750744
Err(err) => {
751-
match err.as_errno() {
752-
Some(os_err)
745+
match err {
746+
os_err
753747
if os_err == Errno::ENOSYS
754748
|| os_err == Errno::EXDEV
755749
|| os_err == Errno::EINVAL
@@ -763,8 +757,7 @@ impl FileExt for File {
763757
assert_eq!(written, 0);
764758
return fallback_file_copy(self, to);
765759
}
766-
Some(os_err) => return Err(io::Error::from_raw_os_error(os_err as i32)),
767-
_ => return Err(io::Error::new(io::ErrorKind::Other, err)),
760+
os_err => return Err(map_nix_error(os_err)),
768761
}
769762
}
770763
}
@@ -785,7 +778,7 @@ impl FileExt for File {
785778
fn pread_exact(&self, buf: &mut [u8], start_pos: usize) -> io::Result<()> {
786779
use nix::sys::uio::pread;
787780

788-
if buf.len() == 0 {
781+
if buf.is_empty() {
789782
return Err(io::Error::new(
790783
io::ErrorKind::InvalidInput,
791784
"zero-sized buffer in input",

0 commit comments

Comments
 (0)