安全 FFI 封装容器

Rust 为通过 外部函数接口 (FFI) 调用函数提供了出色的支持。我们将使用它为 libc 函数构建一个安全封装容器,用于从 C 代码中读取目录中的文件名称。

建议您参考以下手册页面:

您还需要浏览“std::ffi”模块。在下方,您会发现完成这个练习所需的多种字符串类型:

类型编码使用
“str”“String”UTF-8用 Rust 进行文本处理
“CStr”“CString”以空字符结尾与 C 函数通信
“OsStr”“OsString”特定于操作系统与操作系统通信

您将在以下所有类型之间进行转换:

  • &str 转换为 CString:您需要为尾随 \0 字符分配空格,
  • CString 转换为 \*const i8 :您需要一个指针来调用 C 函数,
  • \*const i8 转换为 &CStr :您需要一些能够找到尾随 \0 字符的内容,
  • &CStr to &[u8]: a slice of bytes is the universal interface for “some unknown data”,
  • &\[u8\] 转换为 &OsStr&OsStr 是向 OsString 迈进的一步,请使用OsStrExt来创建它,
  • 将“&OsStr”转换为“OsString”:您需要克隆“&OsStr”中的数据,以便能够返回它并再次调用“readdir”。

秘典 中也有一个关于 FFI 的非常实用的章节。

将以下代码复制到 https://play.rust-lang.org/,并填入缺少的函数和方法:

  1. // TODO: remove this when you're done with your implementation.
  2. #![allow(unused_imports, unused_variables, dead_code)]
  3. mod ffi {
  4. use std::os::raw::{c_char, c_int};
  5. #[cfg(not(target_os = "macos"))]
  6. use std::os::raw::{c_long, c_uchar, c_ulong, c_ushort};
  7. // Opaque type. See https://doc.rust-lang.org/nomicon/ffi.html.
  8. #[repr(C)]
  9. pub struct DIR {
  10. _data: [u8; 0],
  11. _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
  12. }
  13. // Layout according to the Linux man page for readdir(3), where ino_t and
  14. // off_t are resolved according to the definitions in
  15. // /usr/include/x86_64-linux-gnu/{sys/types.h, bits/typesizes.h}.
  16. #[cfg(not(target_os = "macos"))]
  17. #[repr(C)]
  18. pub struct dirent {
  19. pub d_ino: c_ulong,
  20. pub d_off: c_long,
  21. pub d_reclen: c_ushort,
  22. pub d_type: c_uchar,
  23. pub d_name: [c_char; 256],
  24. }
  25. // Layout according to the macOS man page for dir(5).
  26. #[cfg(all(target_os = "macos"))]
  27. #[repr(C)]
  28. pub struct dirent {
  29. pub d_fileno: u64,
  30. pub d_seekoff: u64,
  31. pub d_reclen: u16,
  32. pub d_namlen: u16,
  33. pub d_type: u8,
  34. pub d_name: [c_char; 1024],
  35. }
  36. extern "C" {
  37. pub fn opendir(s: *const c_char) -> *mut DIR;
  38. #[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
  39. pub fn readdir(s: *mut DIR) -> *const dirent;
  40. // See https://github.com/rust-lang/libc/issues/414 and the section on
  41. // _DARWIN_FEATURE_64_BIT_INODE in the macOS man page for stat(2).
  42. //
  43. // "Platforms that existed before these updates were available" refers
  44. // to macOS (as opposed to iOS / wearOS / etc.) on Intel and PowerPC.
  45. #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
  46. #[link_name = "readdir$INODE64"]
  47. pub fn readdir(s: *mut DIR) -> *const dirent;
  48. pub fn closedir(s: *mut DIR) -> c_int;
  49. }
  50. }
  51. use std::ffi::{CStr, CString, OsStr, OsString};
  52. use std::os::unix::ffi::OsStrExt;
  53. #[derive(Debug)]
  54. struct DirectoryIterator {
  55. path: CString,
  56. dir: *mut ffi::DIR,
  57. }
  58. impl DirectoryIterator {
  59. fn new(path: &str) -> Result<DirectoryIterator, String> {
  60. // Call opendir and return a Ok value if that worked,
  61. // otherwise return Err with a message.
  62. unimplemented!()
  63. }
  64. }
  65. impl Iterator for DirectoryIterator {
  66. type Item = OsString;
  67. fn next(&mut self) -> Option<OsString> {
  68. // Keep calling readdir until we get a NULL pointer back.
  69. unimplemented!()
  70. }
  71. }
  72. impl Drop for DirectoryIterator {
  73. fn drop(&mut self) {
  74. // Call closedir as needed.
  75. unimplemented!()
  76. }
  77. }
  78. fn main() -> Result<(), String> {
  79. let iter = DirectoryIterator::new(".")?;
  80. println!("files: {:#?}", iter.collect::<Vec<_>>());
  81. Ok(())
  82. }