1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
extern crate libc as upstream_libc;

use crate::libc::{
    c_int, c_uint, c_void, FSCONFIG_CMD_CREATE, FSCONFIG_CMD_RECONFIGURE,
    FSCONFIG_SET_BINARY, FSCONFIG_SET_FD, FSCONFIG_SET_FLAG, FSCONFIG_SET_PATH,
    FSCONFIG_SET_PATH_EMPTY, FSCONFIG_SET_STRING,
};
use bitflags::bitflags;
use nix::{errno::Errno, unistd::close, Error, NixPath, Result};
use std::{
    convert::TryFrom,
    ffi::CStr,
    mem,
    os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
    ptr,
};

// copied from nix
macro_rules! libc_bitflags {
    (
        $(#[$outer:meta])*
        pub struct $BitFlags:ident: $T:ty {
            $(
                $(#[$inner:ident $($args:tt)*])*
                $Flag:ident $(as $cast:ty)*;
            )+
        }
    ) => {
        bitflags! {
            $(#[$outer])*
            pub struct $BitFlags: $T {
                $(
                    $(#[$inner $($args)*])*
                    const $Flag = libc::$Flag $(as $cast)*;
                )+
            }
        }
    };
}

mod libc {
    #![allow(non_upper_case_globals)]

    pub use upstream_libc::*;

    // https://github.com/rust-lang/libc/pull/1759
    pub const SYS_open_tree: c_long = 428;
    pub const SYS_move_mount: c_long = 429;
    pub const SYS_fsopen: c_long = 430;
    pub const SYS_fsconfig: c_long = 431;
    pub const SYS_fsmount: c_long = 432;
    pub const SYS_fspick: c_long = 433;

    // https://github.com/rust-lang/libc/pull/????
    pub const OPEN_TREE_CLONE: c_uint = 1;
    pub const OPEN_TREE_CLOEXEC: c_uint = O_CLOEXEC as c_uint;

    pub const MOVE_MOUNT_F_SYMLINKS: c_uint = 0x00000001;
    pub const MOVE_MOUNT_F_AUTOMOUNTS: c_uint = 0x00000002;
    pub const MOVE_MOUNT_F_EMPTY_PATH: c_uint = 0x00000004;
    pub const MOVE_MOUNT_T_SYMLINKS: c_uint = 0x00000010;
    pub const MOVE_MOUNT_T_AUTOMOUNTS: c_uint = 0x00000020;
    pub const MOVE_MOUNT_T_EMPTY_PATH: c_uint = 0x00000040;
    #[allow(dead_code)]
    pub const MOVE_MOUNT__MASK: c_uint = 0x00000077;

    pub const FSOPEN_CLOEXEC: c_uint = 0x00000001;

    pub const FSPICK_CLOEXEC: c_uint = 0x00000001;
    pub const FSPICK_SYMLINK_NOFOLLOW: c_uint = 0x00000002;
    pub const FSPICK_NO_AUTOMOUNT: c_uint = 0x00000004;
    pub const FSPICK_EMPTY_PATH: c_uint = 0x00000008;

    pub const FSCONFIG_SET_FLAG: c_uint = 0;
    pub const FSCONFIG_SET_STRING: c_uint = 1;
    pub const FSCONFIG_SET_BINARY: c_uint = 2;
    pub const FSCONFIG_SET_PATH: c_uint = 3;
    pub const FSCONFIG_SET_PATH_EMPTY: c_uint = 4;
    pub const FSCONFIG_SET_FD: c_uint = 5;
    pub const FSCONFIG_CMD_CREATE: c_uint = 6;
    pub const FSCONFIG_CMD_RECONFIGURE: c_uint = 7;

    pub const FSMOUNT_CLOEXEC: c_uint = 0x00000001;

    pub const MOUNT_ATTR_RDONLY: c_uint = 0x00000001;
    pub const MOUNT_ATTR_NOSUID: c_uint = 0x00000002;
    pub const MOUNT_ATTR_NODEV: c_uint = 0x00000004;
    pub const MOUNT_ATTR_NOEXEC: c_uint = 0x00000008;
    #[allow(dead_code)]
    pub const MOUNT_ATTR__ATIME: c_uint = 0x00000070;
    pub const MOUNT_ATTR_RELATIME: c_uint = 0x00000000;
    pub const MOUNT_ATTR_NOATIME: c_uint = 0x00000010;
    pub const MOUNT_ATTR_STRICTATIME: c_uint = 0x00000020;
    pub const MOUNT_ATTR_NODIRATIME: c_uint = 0x00000080;

    pub const AT_RECURSIVE: c_uint = 0x8000;
}

mod sys {
    use crate::libc::{
        c_char, c_int, c_uint, c_void, syscall, SYS_fsconfig, SYS_fsmount, SYS_fsopen,
        SYS_fspick, SYS_move_mount, SYS_open_tree,
    };

    pub unsafe fn open_tree(dfd: c_int, filename: *const c_char, flags: c_uint) -> c_int {
        syscall(
            SYS_open_tree,
            dfd as usize,
            filename as usize,
            flags as usize,
        ) as c_int
    }

    pub unsafe fn move_mount(
        from_dfd: c_int,
        from_pathname: *const c_char,
        to_dfd: c_int,
        to_pathname: *const c_char,
        flags: c_uint,
    ) -> c_int {
        syscall(
            SYS_move_mount,
            from_dfd as usize,
            from_pathname as usize,
            to_dfd as usize,
            to_pathname as usize,
            flags as usize,
        ) as c_int
    }

    pub unsafe fn fsopen(fs_name: *const c_char, flags: c_uint) -> c_int {
        syscall(SYS_fsopen, fs_name as usize, flags as usize) as c_int
    }

    pub unsafe fn fsconfig(
        fd: c_int,
        cmd: c_uint,
        key: *const c_char,
        value: *const c_void,
        aux: c_int,
    ) -> c_int {
        syscall(
            SYS_fsconfig,
            fd as usize,
            cmd as usize,
            key as usize,
            value as usize,
            aux as usize,
        ) as c_int
    }

    pub unsafe fn fsmount(fs_fd: c_int, flags: c_uint, attr_flags: c_uint) -> c_int {
        syscall(
            SYS_fsmount,
            fs_fd as usize,
            flags as usize,
            attr_flags as usize,
        ) as c_int
    }

    pub unsafe fn fspick(dfd: c_int, path: *const c_char, flags: c_uint) -> c_int {
        syscall(SYS_fspick, dfd as usize, path as usize, flags as usize) as c_int
    }
}

libc_bitflags! {
    pub struct OpenTreeFlags: c_uint {
        AT_EMPTY_PATH as c_uint;
        AT_NO_AUTOMOUNT as c_uint;
        AT_RECURSIVE;
        AT_SYMLINK_NOFOLLOW as c_uint;
        OPEN_TREE_CLONE;
        OPEN_TREE_CLOEXEC;
    }
}

libc_bitflags! {
    pub struct MoveMountFlags: c_uint {
        MOVE_MOUNT_F_SYMLINKS;
        MOVE_MOUNT_F_AUTOMOUNTS;
        MOVE_MOUNT_F_EMPTY_PATH;
        MOVE_MOUNT_T_SYMLINKS;
        MOVE_MOUNT_T_AUTOMOUNTS;
        MOVE_MOUNT_T_EMPTY_PATH;
    }
}

libc_bitflags! {
    pub struct FsopenFlags: c_uint {
        FSOPEN_CLOEXEC;
    }
}

libc_bitflags! {
    pub struct FsmountFlags: c_uint {
        FSMOUNT_CLOEXEC;
    }
}

libc_bitflags! {
    pub struct MountAttrFlags: c_uint {
        MOUNT_ATTR_RDONLY;
        MOUNT_ATTR_NOSUID;
        MOUNT_ATTR_NODEV;
        MOUNT_ATTR_NOEXEC;
        MOUNT_ATTR_RELATIME;
        MOUNT_ATTR_NOATIME;
        MOUNT_ATTR_STRICTATIME;
        MOUNT_ATTR_NODIRATIME;
    }
}

libc_bitflags! {
    pub struct FspickFlags: c_uint {
        FSPICK_CLOEXEC;
        FSPICK_SYMLINK_NOFOLLOW;
        FSPICK_NO_AUTOMOUNT;
        FSPICK_EMPTY_PATH;
    }
}

pub fn open_tree<P>(dfd: RawFd, filename: &P, flags: OpenTreeFlags) -> Result<RawFd>
where
    P: ?Sized + NixPath,
{
    let res = filename.with_nix_path(|filename| unsafe {
        sys::open_tree(dfd, filename.as_ptr(), flags.bits())
    })?;
    Errno::result(res)
}

pub fn move_mount<P, Q>(
    from_dfd: RawFd,
    from_pathname: &P,
    to_dfd: RawFd,
    to_pathname: &Q,
    flags: MoveMountFlags,
) -> Result<()>
where
    P: ?Sized + NixPath,
    Q: ?Sized + NixPath,
{
    let res = from_pathname.with_nix_path(|from_pathname| {
        to_pathname.with_nix_path(|to_pathname| unsafe {
            sys::move_mount(
                from_dfd,
                from_pathname.as_ptr(),
                to_dfd,
                to_pathname.as_ptr(),
                flags.bits(),
            )
        })
    })??;
    Errno::result(res).map(drop)
}

pub fn fsopen(fs_name: &CStr, flags: FsopenFlags) -> Result<RawFd> {
    let res = unsafe { sys::fsopen(fs_name.as_ptr(), flags.bits()) };
    Errno::result(res)
}

pub fn fsconfig_set_flag(fs_fd: RawFd, key: &CStr) -> Result<()> {
    let res =
        unsafe { sys::fsconfig(fs_fd, FSCONFIG_SET_FLAG, key.as_ptr(), ptr::null(), 0) };
    Errno::result(res).map(drop)
}

pub fn fsconfig_set_string(fs_fd: RawFd, key: &CStr, value: &CStr) -> Result<()> {
    let res = unsafe {
        sys::fsconfig(
            fs_fd,
            FSCONFIG_SET_STRING,
            key.as_ptr(),
            value.as_ptr() as *const c_void,
            0,
        )
    };
    Errno::result(res).map(drop)
}

pub fn fsconfig_set_binary(fs_fd: RawFd, key: &CStr, value: &[u8]) -> Result<()> {
    let len = match c_int::try_from(value.len()) {
        Ok(len) => len,
        Err(_) => return Err(Error::Sys(Errno::EINVAL)),
    };
    let res = unsafe {
        sys::fsconfig(
            fs_fd,
            FSCONFIG_SET_BINARY,
            key.as_ptr(),
            value.as_ptr() as *const c_void,
            len,
        )
    };
    Errno::result(res).map(drop)
}

fn _fsconfig_set_path<P>(
    fs_fd: RawFd,
    cmd: c_uint,
    key: &CStr,
    dfd: RawFd,
    path: &P,
) -> Result<()>
where
    P: ?Sized + NixPath,
{
    let res = path.with_nix_path(|path| unsafe {
        sys::fsconfig(
            fs_fd,
            cmd,
            key.as_ptr(),
            path.as_ptr() as *const c_void,
            dfd,
        )
    })?;
    Errno::result(res).map(drop)
}

pub fn fsconfig_set_path<P>(fs_fd: RawFd, key: &CStr, dfd: RawFd, path: &P) -> Result<()>
where
    P: ?Sized + NixPath,
{
    _fsconfig_set_path(fs_fd, FSCONFIG_SET_PATH, key, dfd, path)
}

pub fn fsconfig_set_path_empty<P>(
    fs_fd: RawFd,
    key: &CStr,
    dfd: RawFd,
    path: &P,
) -> Result<()>
where
    P: ?Sized + NixPath,
{
    _fsconfig_set_path(fs_fd, FSCONFIG_SET_PATH_EMPTY, key, dfd, path)
}

pub fn fsconfig_set_fd(fs_fd: RawFd, key: &CStr, fd: RawFd) -> Result<()> {
    let res =
        unsafe { sys::fsconfig(fs_fd, FSCONFIG_SET_FD, key.as_ptr(), ptr::null(), fd) };
    Errno::result(res).map(drop)
}

fn _fsconfig_cmd(fs_fd: RawFd, cmd: c_uint) -> Result<()> {
    let res = unsafe { sys::fsconfig(fs_fd, cmd, ptr::null(), ptr::null(), 0) };
    Errno::result(res).map(drop)
}

pub fn fsconfig_cmd_create(fs_fd: RawFd) -> Result<()> {
    _fsconfig_cmd(fs_fd, FSCONFIG_CMD_CREATE)
}

pub fn fsconfig_cmd_reconfigure(fs_fd: RawFd) -> Result<()> {
    _fsconfig_cmd(fs_fd, FSCONFIG_CMD_RECONFIGURE)
}

pub fn fsmount(
    fs_fd: RawFd,
    flags: FsmountFlags,
    attr_flags: MountAttrFlags,
) -> Result<RawFd> {
    let res = unsafe { sys::fsmount(fs_fd, flags.bits(), attr_flags.bits()) };
    Errno::result(res)
}

pub fn fspick<P>(dfd: RawFd, path: &P, flags: FspickFlags) -> Result<RawFd>
where
    P: ?Sized + NixPath,
{
    let res = path
        .with_nix_path(|path| unsafe { sys::fspick(dfd, path.as_ptr(), flags.bits()) })?;
    Errno::result(res)
}

pub struct Mount {
    mnt_fd: RawFd,
}

impl Mount {
    pub fn move_mount<P>(
        &self,
        to_dfd: RawFd,
        to_path: &P,
        mut flags: MoveMountFlags,
    ) -> Result<()>
    where
        P: ?Sized + NixPath,
    {
        flags |= MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH;
        move_mount(
            self.mnt_fd,
            CStr::from_bytes_with_nul(b"\0").unwrap(),
            to_dfd,
            to_path,
            flags,
        )
    }

    pub fn open<P>(dfd: RawFd, path: &P, flags: OpenTreeFlags) -> Result<Self>
    where
        P: ?Sized + NixPath,
    {
        Ok(Mount {
            mnt_fd: open_tree(dfd, path, flags)?,
        })
    }
}

impl Drop for Mount {
    fn drop(&mut self) {
        let _ = close(self.mnt_fd);
    }
}

impl AsRawFd for Mount {
    fn as_raw_fd(&self) -> RawFd {
        self.mnt_fd
    }
}

impl IntoRawFd for Mount {
    fn into_raw_fd(self) -> RawFd {
        let mnt_fd = self.mnt_fd;
        mem::forget(self);
        mnt_fd
    }
}

impl FromRawFd for Mount {
    unsafe fn from_raw_fd(mnt_fd: RawFd) -> Self {
        Mount { mnt_fd }
    }
}

pub struct Fs {
    fs_fd: RawFd,
}

impl Fs {
    pub fn open(fs_name: &CStr, flags: FsopenFlags) -> Result<Self> {
        Ok(Fs {
            fs_fd: fsopen(fs_name, flags)?,
        })
    }

    pub fn pick<P>(dfd: RawFd, path: &P, flags: FspickFlags) -> Result<Self>
    where
        P: ?Sized + NixPath,
    {
        Ok(Fs {
            fs_fd: fspick(dfd, path, flags)?,
        })
    }

    pub fn set_flag(&self, key: &CStr) -> Result<()> {
        fsconfig_set_flag(self.fs_fd, key)
    }

    pub fn set_string(&self, key: &CStr, value: &CStr) -> Result<()> {
        fsconfig_set_string(self.fs_fd, key, value)
    }

    pub fn set_binary(&self, key: &CStr, value: &[u8]) -> Result<()> {
        fsconfig_set_binary(self.fs_fd, key, value)
    }

    pub fn set_path<P>(&self, key: &CStr, dfd: RawFd, path: &P) -> Result<()>
    where
        P: ?Sized + NixPath,
    {
        fsconfig_set_path(self.fs_fd, key, dfd, path)
    }

    pub fn set_path_empty<P>(&self, key: &CStr, dfd: RawFd, path: &P) -> Result<()>
    where
        P: ?Sized + NixPath,
    {
        fsconfig_set_path_empty(self.fs_fd, key, dfd, path)
    }

    pub fn set_path_fd(&self, key: &CStr, fd: RawFd) -> Result<()> {
        fsconfig_set_fd(self.fs_fd, key, fd)
    }

    pub fn create(&self) -> Result<()> {
        fsconfig_cmd_create(self.fs_fd)
    }

    pub fn reconfigure(&self) -> Result<()> {
        fsconfig_cmd_reconfigure(self.fs_fd)
    }

    pub fn mount(
        &self,
        flags: FsmountFlags,
        attr_flags: MountAttrFlags,
    ) -> Result<Mount> {
        Ok(Mount {
            mnt_fd: fsmount(self.fs_fd, flags, attr_flags)?,
        })
    }
}

impl Drop for Fs {
    fn drop(&mut self) {
        let _ = close(self.fs_fd);
    }
}

impl AsRawFd for Fs {
    fn as_raw_fd(&self) -> RawFd {
        self.fs_fd
    }
}

impl IntoRawFd for Fs {
    fn into_raw_fd(self) -> RawFd {
        let fs_fd = self.fs_fd;
        mem::forget(self);
        fs_fd
    }
}

impl FromRawFd for Fs {
    unsafe fn from_raw_fd(fs_fd: RawFd) -> Self {
        Fs { fs_fd }
    }
}

#[cfg(test)]
mod test {
    use crate::*;
    use nix::{
        fcntl::{openat, OFlag},
        libc::AT_FDCWD,
        mount::{mount, umount2, MntFlags, MsFlags},
        sys::stat::Mode,
        unistd::close,
        NixPath,
    };
    use std::{
        ffi::CStr,
        fs,
        os::unix::io::{AsRawFd, RawFd},
        path::Path,
    };
    use tempfile::{tempdir, tempdir_in};

    fn cstr(s: &str) -> &CStr {
        CStr::from_bytes_with_nul(s.as_bytes()).unwrap()
    }

    fn tmpfs() -> Mount {
        let fs = Fs::open(cstr("tmpfs\0"), FsopenFlags::empty()).unwrap();
        fs.create().unwrap();
        fs.mount(FsmountFlags::empty(), MountAttrFlags::empty())
            .unwrap()
    }

    fn with_mount_point<'a, P: Into<Option<&'a Path>>, T, F: FnOnce(&Path) -> T>(
        base: P,
        f: F,
    ) -> T {
        struct Umount<'a>(&'a Path);

        impl<'a> Drop for Umount<'a> {
            fn drop(&mut self) {
                let _ = umount2(self.0, MntFlags::MNT_DETACH);
            }
        }

        let dir = match base.into() {
            Some(base) => tempdir_in(base),
            _ => tempdir(),
        }
        .unwrap();
        let _umount = Umount(dir.path());
        f(dir.path())
    }

    fn with_private_mount<T, F: FnOnce(&Path) -> T>(f: F) -> T {
        with_mount_point(None, |path| {
            let fs = tmpfs();
            fs.move_mount(AT_FDCWD, path, MoveMountFlags::empty())
                .unwrap();
            mount::<Path, _, Path, Path>(None, path, None, MsFlags::MS_PRIVATE, None)
                .unwrap();
            f(path)
        })
    }

    fn create_file<P: ?Sized + NixPath>(dfd: RawFd, p: &P) {
        let _ = close(
            openat(dfd, p, OFlag::O_CREAT | OFlag::O_RDONLY, Mode::empty()).unwrap(),
        );
    }

    #[test]
    fn move_mount1() {
        with_private_mount(|path| {
            with_mount_point(path, |p1| {
                with_mount_point(path, |p2| {
                    const FILE: &str = "test";

                    let mnt = tmpfs();
                    create_file(mnt.as_raw_fd(), FILE);

                    // move to p1
                    mnt.move_mount(AT_FDCWD, p1, MoveMountFlags::empty())
                        .unwrap();
                    assert!(p1.join(FILE).exists());

                    // move from p1 to p2
                    mnt.move_mount(AT_FDCWD, p2, MoveMountFlags::empty())
                        .unwrap();
                    assert!(!p1.join(FILE).exists());
                    assert!(p2.join(FILE).exists());

                    // open tree at p2 and move to p1
                    let mnt = Mount::open(AT_FDCWD, p2, OpenTreeFlags::empty()).unwrap();
                    mnt.move_mount(AT_FDCWD, p1, MoveMountFlags::empty())
                        .unwrap();
                    assert!(p1.join(FILE).exists());
                    assert!(!p2.join(FILE).exists());

                    // move from p2 to p1
                    move_mount(AT_FDCWD, p1, AT_FDCWD, p2, MoveMountFlags::empty())
                        .unwrap();
                    assert!(!p1.join(FILE).exists());
                    assert!(p2.join(FILE).exists());

                    // clone tree at p2 and attach to p1
                    let mnt = Mount::open(AT_FDCWD, p2, OpenTreeFlags::OPEN_TREE_CLONE)
                        .unwrap();
                    mnt.move_mount(AT_FDCWD, p1, MoveMountFlags::empty())
                        .unwrap();
                    assert!(p1.join(FILE).exists());
                    assert!(p2.join(FILE).exists());
                });
            });
        });
    }

    #[test]
    fn fspick1() {
        with_mount_point(None, |p1| {
            let mnt = tmpfs();
            mnt.move_mount(AT_FDCWD, p1, MoveMountFlags::empty())
                .unwrap();

            fs::create_dir(p1.join("a")).unwrap();

            // make read-only
            let fs = Fs::pick(AT_FDCWD, p1, FspickFlags::empty()).unwrap();
            fs.set_flag(CStr::from_bytes_with_nul(b"ro\0").unwrap())
                .unwrap();
            fs.reconfigure().unwrap();

            assert!(fs::create_dir(p1.join("b")).is_err());
        });
    }

    #[test]
    fn fsmount1() {
        let fs = Fs::open(cstr("proc\0"), FsopenFlags::empty()).unwrap();
        fs.create().unwrap();
        let mnt = fs
            .mount(FsmountFlags::empty(), MountAttrFlags::empty())
            .unwrap();

        // open /proc/cpuinfo
        let _ = close(
            openat(mnt.as_raw_fd(), "cpuinfo", OFlag::O_RDONLY, Mode::empty()).unwrap(),
        );
    }
}