-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# libpathrs: safe path resolution on Linux | ||
# Copyright (C) 2019-2024 Aleksa Sarai <cyphar@cyphar.com> | ||
# Copyright (C) 2019-2024 SUSE LLC | ||
# | ||
# This program is free software: you can redistribute it and/or modify it under | ||
# the terms of the GNU Lesser General Public License as published by the Free | ||
# Software Foundation, either version 3 of the License, or (at your option) any | ||
# later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
# PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License along | ||
# with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
CARGO ?= cargo | ||
|
||
EXAMPLE_NAME := $(basename $(shell basename $$PWD)) | ||
|
||
.PHONY: build | ||
build: | ||
$(CARGO) build --example $(EXAMPLE_NAME) | ||
|
||
.PHONY: smoke-test | ||
smoke-test: build | ||
../../target/debug/examples/$(EXAMPLE_NAME) . ../../main.rs &>/dev/null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* libpathrs: safe path resolution on Linux | ||
* Copyright (C) 2019-2024 Aleksa Sarai <cyphar@cyphar.com> | ||
* Copyright (C) 2019-2024 SUSE LLC | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along | ||
* with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/* | ||
* File: examples/rust-cat/main.rs | ||
* | ||
* An example program which opens a file inside a root and outputs its contents | ||
* using libpathrs. | ||
*/ | ||
|
||
use pathrs::{flags::OpenFlags, Root}; | ||
|
||
use std::io::{prelude::*, BufReader}; | ||
|
||
use anyhow::{Context, Error}; | ||
use clap::{Arg, Command}; | ||
|
||
fn main() -> Result<(), Error> { | ||
let m = Command::new("cat") | ||
.author(clap::crate_authors!("\n")) | ||
.version(clap::crate_version!()) | ||
.arg(Arg::new("root").value_name("ROOT")) | ||
.arg(Arg::new("unsafe-path").value_name("PATH")) | ||
.about("") | ||
.get_matches(); | ||
|
||
let root_path = m | ||
.get_one::<String>("root") | ||
.context("required root argument not provided")?; | ||
let unsafe_path = m | ||
.get_one::<String>("unsafe-path") | ||
.context("required unsafe-path argument not provided")?; | ||
|
||
let root = Root::open(root_path).context("open root failed")?; | ||
let handle = root | ||
.resolve(unsafe_path) | ||
.context("resolve unsafe path in root")?; | ||
|
||
let file = handle | ||
.reopen(OpenFlags::O_RDONLY) | ||
.context("reopen path with O_RDONLY")?; | ||
|
||
let reader = BufReader::new(file); | ||
for line in reader.lines() { | ||
println!("{}", line.context("read lines")?); | ||
} | ||
Ok(()) | ||
} |