Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions contrib/bindings/python/pathrs/_pathrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
INTERNAL_ERROR,
# CFFI helpers.
_cstr,
_pystr,
_cbuffer,
)
from ._libpathrs_cffi import lib as libpathrs_so
Expand All @@ -51,11 +52,26 @@
# Core api.
"Root",
"Handle",
"library_version",
# Error api (re-export).
"PathrsError",
]


def library_version() -> str:
"""
Return the version of the underlying libpathrs C library that this Python
binding is linked against.

The returned string follows SemVer 2.0.0 (with an optional
"+<build-metadata>" suffix for pre-release builds, e.g. "0.2.4+dev").

This is distinct from pathrs.__version__ which contains the version of the
Python pathrs package itself.
"""
return _pystr(libpathrs_so.pathrs_version())

Check failure on line 72 in contrib/bindings/python/pathrs/_pathrs.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Module has no attribute "pathrs_version" [attr-defined] Raw Output: /home/runner/work/libpathrs/libpathrs/contrib/bindings/python/pathrs/_pathrs.py:72:19: error: Module has no attribute "pathrs_version" [attr-defined]


class Handle(WrappedFd):
"A handle to a filesystem object, usually resolved using Root.resolve()."

Expand Down
1 change: 1 addition & 0 deletions examples/c/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int open_in_root(const char *root_path, const char *unsafe_path)
}

void usage(void) {
printf("cat (libpathrs %s)\n", pathrs_version());
printf("usage: cat <root> <unsafe-path>\n");
exit(1);
}
Expand Down
1 change: 1 addition & 0 deletions examples/c/cat_multithread.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void *worker(void *_arg) {
}

void usage(void) {
printf("cat (libpathrs %s)\n", pathrs_version());
printf("usage: cat <root> <unsafe-path>\n");
exit(1);
}
Expand Down
9 changes: 7 additions & 2 deletions examples/go/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ import (
"cyphar.com/go-pathrs"
)

func usage() {
fmt.Fprintf(os.Stderr, "cat (libpathrs %s)\n", pathrs.Version())
fmt.Fprintln(os.Stderr, "usage: cat <root> <unsafe-path>")
os.Exit(1)
}

func Main(args ...string) error {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "usage: cat <root> <unsafe-path>")
os.Exit(1)
usage()
}

rootPath, unsafePath := args[0], args[1]
Expand Down
7 changes: 7 additions & 0 deletions examples/python/sysctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import sys

sys.path.append(os.path.dirname(__file__) + "/../contrib/bindings/python")
import pathrs
from pathrs import procfs
from pathrs.procfs import ProcfsHandle

Expand Down Expand Up @@ -65,6 +66,12 @@ def main(*args):
prog="sysctl.py",
description="A minimal implementation of sysctl(8) but using the libpathrs procfs API.",
)
parser.add_argument(
"-V",
"--version",
action="version",
version=f"sysctl.py (libpathrs {pathrs.library_version()})",
)
parser.add_argument(
"-n",
"--values",
Expand Down
5 changes: 5 additions & 0 deletions go-pathrs/internal/libpathrs/libpathrs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func fetchError(errID C.int) error {
return err
}

// Version wraps pathrs_version.
func Version() string {
return C.GoString(C.pathrs_version())
}

// OpenRoot wraps pathrs_open_root.
func OpenRoot(path string) (uintptr, error) {
cPath := C.CString(path)
Expand Down
29 changes: 29 additions & 0 deletions go-pathrs/version_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build linux

// 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/.
*/

package pathrs

import (
"cyphar.com/go-pathrs/internal/libpathrs"
)

// Version returns the version string of the underlying libpathrs C library
// that go-pathrs is linked against.
//
// The returned string follows [SemVer 2.0.0] (with an optional
// "+<build-metadata>" suffix for pre-release builds, e.g. "0.2.4+dev").
//
// [SemVer 2.0.0]: https://semver.org/spec/v2.0.0.html
func Version() string {
return libpathrs.Version()
}
16 changes: 16 additions & 0 deletions include/pathrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ typedef struct __CBINDGEN_ALIGNED(8) {
*/
#define __PATHRS_MAX_ERR_VALUE -4096

/**
* Get the libpathrs version as a static, NUL-terminated string.
*
* The returned string follows [SemVer 2.0.0][semver] (with an optional
* `+<build-metadata>` suffix for pre-release builds, e.g. `0.2.4+dev`).
*
* # Return Value
*
* This function returns a pointer to a static, NUL-terminated string
* containing the libpathrs version. The returned pointer must not be freed,
* and remains valid for the lifetime of the program.
*
* [semver]: https://semver.org/spec/v2.0.0.html
*/
const char *pathrs_version(void);

/**
* Open a root handle.
*
Expand Down
20 changes: 20 additions & 0 deletions src/capi/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@

use libc::{c_char, c_int, c_uint, dev_t, size_t};

/// Get the libpathrs version as a static, NUL-terminated string.
///
/// The returned string follows [SemVer 2.0.0][semver] (with an optional
/// `+<build-metadata>` suffix for pre-release builds, e.g. `0.2.4+dev`).
///
/// # Return Value
///
/// This function returns a pointer to a static, NUL-terminated string
/// containing the libpathrs version. The returned pointer must not be freed,
/// and remains valid for the lifetime of the program.
///
/// [semver]: https://semver.org/spec/v2.0.0.html
#[no_mangle]
pub extern "C" fn pathrs_version() -> *const c_char {
concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr() as *const c_char
}

Check warning on line 67 in src/capi/core.rs

View check run for this annotation

Codecov / codecov/patch

src/capi/core.rs#L65-L67

Added lines #L65 - L67 were not covered by tests
utils::symver! {
fn pathrs_version <- (pathrs_version, version = "LIBPATHRS_0.2", default);
}

/// Open a root handle.
///
/// The provided path must be an existing directory.
Expand Down
Loading