xref: /third_party/rust/crates/rustix/tests/fs/file.rs (revision b8a62b91)
1#[cfg(not(target_os = "redox"))]
2#[test]
3fn test_file() {
4    #[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
5    rustix::fs::accessat(
6        rustix::fs::cwd(),
7        "Cargo.toml",
8        rustix::fs::Access::READ_OK,
9        rustix::fs::AtFlags::empty(),
10    )
11    .unwrap();
12
13    assert_eq!(
14        rustix::fs::openat(
15            rustix::fs::cwd(),
16            "Cagro.motl",
17            rustix::fs::OFlags::RDONLY,
18            rustix::fs::Mode::empty(),
19        )
20        .unwrap_err(),
21        rustix::io::Errno::NOENT
22    );
23
24    let file = rustix::fs::openat(
25        rustix::fs::cwd(),
26        "Cargo.toml",
27        rustix::fs::OFlags::RDONLY,
28        rustix::fs::Mode::empty(),
29    )
30    .unwrap();
31
32    assert_eq!(
33        rustix::fs::openat(
34            &file,
35            "Cargo.toml",
36            rustix::fs::OFlags::RDONLY,
37            rustix::fs::Mode::empty(),
38        )
39        .unwrap_err(),
40        rustix::io::Errno::NOTDIR
41    );
42
43    #[cfg(not(any(
44        target_os = "dragonfly",
45        target_os = "haiku",
46        target_os = "illumos",
47        target_os = "ios",
48        target_os = "macos",
49        target_os = "netbsd",
50        target_os = "openbsd",
51        target_os = "redox",
52        target_os = "solaris",
53    )))]
54    rustix::fs::fadvise(&file, 0, 10, rustix::fs::Advice::Normal).unwrap();
55
56    assert_eq!(
57        rustix::io::fcntl_getfd(&file).unwrap(),
58        rustix::io::FdFlags::empty()
59    );
60    assert_eq!(
61        rustix::fs::fcntl_getfl(&file).unwrap(),
62        rustix::fs::OFlags::empty()
63    );
64
65    let stat = rustix::fs::fstat(&file).unwrap();
66    assert!(stat.st_size > 0);
67    assert!(stat.st_blocks > 0);
68
69    #[cfg(not(any(
70        target_os = "haiku",
71        target_os = "illumos",
72        target_os = "netbsd",
73        target_os = "redox",
74        target_os = "solaris",
75        target_os = "wasi",
76    )))]
77    {
78        let statfs = rustix::fs::fstatfs(&file).unwrap();
79        assert!(statfs.f_blocks > 0);
80    }
81
82    #[cfg(not(any(
83        target_os = "haiku",
84        target_os = "illumos",
85        target_os = "redox",
86        target_os = "solaris",
87        target_os = "wasi",
88    )))]
89    {
90        let statvfs = rustix::fs::fstatvfs(&file).unwrap();
91        assert!(statvfs.f_frsize > 0);
92    }
93
94    #[cfg(all(feature = "fs", feature = "net"))]
95    assert_eq!(rustix::io::is_read_write(&file).unwrap(), (true, false));
96
97    assert_ne!(rustix::io::ioctl_fionread(&file).unwrap(), 0);
98}
99