Peter Girnus

View Original

Rust Metadata Extensions (std::os::windows::fs::MetadataExt) Trait for the Windows OS

The Rust std::os::windows::fs::MetadataExt trait provides operating system extensions for the Microsoft Windows platform. These Metadata extensions for Windows allow programmers to view platform-specific metadata such as permissions, size, modification times, and other useful information about a file provided by the Windows operating system. The MetadataExt trait is a part of the Windows-specific extensions to primitives in the Rust standar library (std::fs) module. 

Data exposed by this trait include those of the following Windows BY_HANDLE_FILE_INFORMATION structure.

See this content in the original post

In this blog post we will cover some of the functions associated with this trait that allows programmers to supercharge their development work on the Windows operating system. To begin we need to import the Rust standard library (std::fs) as well as the Rust Metadata Extensions for Windows (std::os::windows::fs::MetadataExt) trait.

If you’d like to access actual code for these examples you can find examples in this blog posts GitHub repo.

Importing the Rust Standard Library (std::fs)

We need to import the Rust standard library (std::fs) in order to gain access to the metadata() function which will return a Metadata structure. This is the structure we will interact the most with while exploring the Metadata Extension trait for Windows.

See this content in the original post

Importing fs::MetadataExt for Windows 

We can import the Metadata Extensions trait for Microsoft Windows with the following command.

We can import the Metadata Extensions trait for Microsoft Windows with the following command.

See this content in the original post

Setting an Example File Variable

In order to demonstrate the Metadata Extensions trait through example we need to have a simple local file, for example a basic hello world file. In my case I will create a constant variable that points to hello.txt on my filesystem

See this content in the original post

Defining a Variable to Hold Our Metadata Struct Object

After we have a local file variable, let's pass this variable to the std::fs::metadata() function. This function will store a file Metadata result object. The std::fs::metadata() function will query the operating system and give us Metadata information about a file. We can then use the MetadataExt trait for Windows on the stored Metadata structure.

See this content in the original post

Rust File Metadata Extensions: Windows OS Functions & Trait Implementations Code Examples

In the following examples, we will explore the Windows platform MetadataExt trait functions and see what types of information we can gather from it. The std::os::windows::fs::MetadataExt trait gives the programmer access to the fields of the BY_HANDLE_FILE_INFORMATION structure.

Getting Windows Platform-Specific BY_HANDLE_FILE_INFORMATION Fields

Rust Windows MetadataExt: How to get the file attributes field?

To get the dwFileAttributes field of a file, we can use the Windows-specific extension file_attributes() function, which returns a u32 value.

See this content in the original post

Rust Windows MetadataExt: How to get the creation time field of a file?

To get the ftCreationTime field of a file, we can use the Windows-specific extension creation_time() function, which returns a u64 value denoting the creation time.

See this content in the original post

Rust Windows MetadataExt: How to get a file's last access time field?

To get the ftLastAccessTime field, we can use the last_access_time() method, which returns a u64 value denoting the last access time.

See this content in the original post

Rust Windows MetadataExt: How to get a file's last write time field?

To get the ftLastWriteTime field, we can use the last_write_time() method, which returns a u64 value denoting the last write time.

See this content in the original post

Rust Windows MetadataExt: How to get the file size field of a file?

To get the nFileSize{High, Low} fields, we can use the file_size() method, which returns a u64 value denoting the file size.

See this content in the original post

Rust Windows MetadataExt: How to get the volume's serial number field containing a file?

To get the dwVolumeSerialNumber field, we can use the volume_serial_number() method, which returns an Option<u32> type containing a u32 value.

See this content in the original post

Rust Windows MetadataExt: How to get the number of links field to a file?

To get the nNumberOfLinks field, we can use the number_of_links() method, which returns an Option<u32> type containing a u32 value.

See this content in the original post

Rust Windows MetadataExt: How to get the file index identifiers field?

To get the nFileIndex{Low, High} fields, we can use the file_index() method, which returns an Option<u64> type containing a u64 value.

See this content in the original post

Windows MetadataExt Rust trait: BY_HANDLE_FILE_INFORMATION Full Code

Here is a full code example of the various MetadataExt trait implementation functions available to the Windows operating system. The Data exposed by this trait include those of the following Windows BY_HANDLE_FILE_INFORMATION structure.

See this content in the original post

Rust Metadata Extensions (std::os::windows::fs::MetadataExt) for the Windows OS Conclusion

In conclusion the Metadata Extensions (std::os::windows::fs::MetadataExt) trait for the Windows operating system provides programmers with a plethora of tools to gather information about a files metadata. From gaining information on access times to information about permissions the MetadataExt trait gives developers the tools they need to interact with files on the filesystem and build rich interactive applications.

If you’d like to access actual code for these examples you can find examples in this blog posts GitHub repo.

See this social icon list in the original post

If you have any questions on implementing this trait in Windows feel free to reach out with any questions you might have!