-
-
Notifications
You must be signed in to change notification settings - Fork 2
Pna/special files support #2537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fe8468d
18f25b3
59dfb61
67fa1b7
b184843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -74,3 +74,64 @@ pub(crate) fn file_create(path: impl AsRef<Path>, overwrite: bool) -> io::Result | |||||
| fs::File::create_new(path) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Decodes a raw device number (rdev) into major and minor device numbers. | ||||||
| /// | ||||||
| /// On Linux, the rdev field uses a more complex encoding for devices with | ||||||
| /// large numbers, but for most common devices the simple encoding suffices. | ||||||
| #[cfg(unix)] | ||||||
| #[inline] | ||||||
| pub(crate) fn decode_rdev(rdev: u64) -> (u32, u32) { | ||||||
| // Use libc's major/minor macros for platform-correct decoding | ||||||
| let major = libc::major(rdev) as u32; | ||||||
| let minor = libc::minor(rdev) as u32; | ||||||
| (major, minor) | ||||||
| } | ||||||
|
|
||||||
| /// Encodes major and minor device numbers into a raw device number (rdev). | ||||||
| #[cfg(unix)] | ||||||
| #[inline] | ||||||
| pub(crate) fn encode_rdev(major: u32, minor: u32) -> u64 { | ||||||
| libc::makedev(major, minor) | ||||||
| } | ||||||
|
|
||||||
| /// Creates a block device node at the specified path. | ||||||
| /// | ||||||
| /// Requires root privileges on most systems. | ||||||
| #[cfg(unix)] | ||||||
| pub(crate) fn mknod_block( | ||||||
| path: impl AsRef<Path>, | ||||||
| major: u32, | ||||||
| minor: u32, | ||||||
| mode: u32, | ||||||
| ) -> io::Result<()> { | ||||||
| use nix::sys::stat::{self, SFlag}; | ||||||
| let dev = encode_rdev(major, minor); | ||||||
| let mode = stat::Mode::from_bits_truncate(mode); | ||||||
| stat::mknod(path.as_ref(), SFlag::S_IFBLK, mode, dev).map_err(io::Error::other) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| /// Creates a character device node at the specified path. | ||||||
| /// | ||||||
| /// Requires root privileges on most systems. | ||||||
| #[cfg(unix)] | ||||||
| pub(crate) fn mknod_char( | ||||||
| path: impl AsRef<Path>, | ||||||
| major: u32, | ||||||
| minor: u32, | ||||||
| mode: u32, | ||||||
| ) -> io::Result<()> { | ||||||
| use nix::sys::stat::{self, SFlag}; | ||||||
| let dev = encode_rdev(major, minor); | ||||||
| let mode = stat::Mode::from_bits_truncate(mode); | ||||||
| stat::mknod(path.as_ref(), SFlag::S_IFCHR, mode, dev).map_err(io::Error::other) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| /// Creates a FIFO (named pipe) at the specified path. | ||||||
| #[cfg(unix)] | ||||||
| pub(crate) fn mkfifo(path: impl AsRef<Path>, mode: u32) -> io::Result<()> { | ||||||
| use nix::sys::stat::Mode; | ||||||
| use nix::unistd; | ||||||
| let mode = Mode::from_bits_truncate(mode); | ||||||
| unistd::mkfifo(path.as_ref(), mode).map_err(io::Error::other) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling
DataKind::BlockDeviceandDataKind::CharDeviceis nearly identical, leading to significant code duplication. This can be refactored into a helper function to improve maintainability. The helper function could take a boolean flag to distinguish between block and character devices and encapsulate the common logic for reading device numbers, checking permissions, and creating the device node.