Skip to content

Implement conversions from std::fs::Metadata* types #413

@multimeric

Description

@multimeric

For example: impl<T> From<T> for FileAttr where T: std::fs::MetadataExt.

Currently, this is a pretty trivial mapping:

fn metadata_convert(m: Metadata) -> FileAttr {
    FileAttr {
        ino: m.ino(),
        size: m.size(),
        blocks: m.blocks(),
        atime: from_unix_secs_signed(m.atime()),
        mtime: from_unix_secs_signed(m.mtime()),
        ctime: from_unix_secs_signed(m.ctime()),
        kind: match m.file_type() {
            ft if ft.is_file() => FileType::RegularFile,
            ft if ft.is_dir() => FileType::Directory,
            ft if ft.is_symlink() => FileType::Symlink,
            ft if ft.is_block_device() => FileType::BlockDevice,
            ft if ft.is_char_device() => FileType::CharDevice,
            ft if ft.is_fifo() => FileType::NamedPipe,
            ft if ft.is_socket() => FileType::Socket,
            _ => return Err("Unknown file type".into()),
        },
        perm: (m.mode() & 0o7777) as u16,
        nlink: m.nlink().try_into()?,
        uid: m.uid(),
        gid: m.gid(),
        rdev: m.rdev().try_into()?,
        blksize: m.blksize().try_into()?,
        // MacOS only, which we don't yet support
        crtime: UNIX_EPOCH,
        flags: 0,
    }
}

However, we can't implement is using From/Into since we don't own the trait. Moving it upstream would be ideal. I'm happy to provide my implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions