Skip to content

UnixStorageInfo Struct

Naveen Dharmathunga edited this page Jan 19, 2026 · 1 revision

UnixStorageInfo Struct

Represents detailed information about a file or directory on a Unix-based file system.

Namespace: Rheo.Storage.Information

Assembly: Rheo.Storage.dll

Definition

public struct UnixStorageInfo : IStorageInfoStruct

Implements

  • IStorageInfoStruct

Properties

Property Type Description
Attributes FileAttributes Gets or sets the file attributes.
Size UInt64 Gets or sets the size in bytes.
CreationTime DateTime Gets or sets the creation time.
LastWriteTime DateTime Gets or sets the last write time.
LastAccessTime DateTime Gets or sets the last access time.
OwnerId UInt32 Gets or sets the owner identifier (UID on Unix).
GroupId UInt32 Gets or sets the group identifier (GID on Unix).
Mode UInt32 Gets or sets the file mode (permissions and type).
SymlinkTarget String Gets or sets the target path if the file is a symbolic link.

Remarks

This structure provides file metadata commonly retrieved from Unix file system queries, including attributes, size, timestamps, ownership, permissions, and symbolic link information. Some fields may have platform-specific interpretations; for example, OwnerId and GroupId correspond to user and group IDs on Unix systems, while Mode reflects Unix file permissions and type. Not all fields may be meaningful or populated on non-Unix platforms.


Property Details

Attributes

Gets or sets the file attributes.

public FileAttributes Attributes { get; set; }

Property Value

FileAttributes

The file attributes flags.


Size

Gets or sets the size in bytes.

public ulong Size { get; set; }

Property Value

UInt64

The file size in bytes.


CreationTime

Gets or sets the creation time.

public DateTime CreationTime { get; set; }

Property Value

DateTime

The creation timestamp.

Remarks

On many Unix systems, creation time may not be available and this may reflect the change time (ctime) instead.


LastWriteTime

Gets or sets the last write time.

public DateTime LastWriteTime { get; set; }

Property Value

DateTime

The last modification timestamp (mtime).


LastAccessTime

Gets or sets the last access time.

public DateTime LastAccessTime { get; set; }

Property Value

DateTime

The last access timestamp (atime).


OwnerId

Gets or sets the owner identifier.

public uint OwnerId { get; set; }

Property Value

UInt32

The user ID (UID) on Unix systems, or a partial SID on Windows.

Remarks

On Unix systems, this corresponds to the st_uid field from the stat structure.


GroupId

Gets or sets the group identifier.

public uint GroupId { get; set; }

Property Value

UInt32

The group ID (GID) on Unix systems.

Remarks

On Unix systems, this corresponds to the st_gid field from the stat structure.


Mode

Gets or sets the file mode (permissions and type).

public uint Mode { get; set; }

Property Value

UInt32

The file mode value.

Remarks

On Unix systems, this corresponds to the st_mode field and includes both file type and permission bits. Common values include:

  • File permissions (0777, 0644, etc.)
  • File type bits (S_IFREG, S_IFDIR, S_IFLNK, etc.)

SymlinkTarget

Gets or sets the target path if the file is a symbolic link.

public string? SymlinkTarget { get; set; }

Property Value

String

The target path, or null if not a symbolic link.


Examples

Accessing Unix-Specific Information

The following example demonstrates accessing Unix-specific storage details:

using Rheo.Storage.Information;

if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
    var fileInfo = new FileInformation("/home/user/document.txt");
    
    if (fileInfo.TryGetUnixStorageInfo(out var unixInfo))
    {
        Console.WriteLine($"Owner UID: {unixInfo.OwnerId}");
        Console.WriteLine($"Group GID: {unixInfo.GroupId}");
        Console.WriteLine($"Mode: {Convert.ToString(unixInfo.Mode, 8)}");
        
        if (unixInfo.SymlinkTarget != null)
        {
            Console.WriteLine($"Symlink Target: {unixInfo.SymlinkTarget}");
        }
    }
}

Checking File Permissions

The following example shows how to check Unix file permissions:

using Rheo.Storage.Information;

const uint S_IRUSR = 0x100; // Owner read
const uint S_IWUSR = 0x080; // Owner write
const uint S_IXUSR = 0x040; // Owner execute

var fileInfo = new FileInformation("/usr/bin/tool");

if (fileInfo.TryGetUnixStorageInfo(out var unixInfo))
{
    bool ownerCanRead = (unixInfo.Mode & S_IRUSR) != 0;
    bool ownerCanWrite = (unixInfo.Mode & S_IWUSR) != 0;
    bool ownerCanExecute = (unixInfo.Mode & S_IXUSR) != 0;
    
    Console.WriteLine($"Owner Permissions:");
    Console.WriteLine($"  Read: {ownerCanRead}");
    Console.WriteLine($"  Write: {ownerCanWrite}");
    Console.WriteLine($"  Execute: {ownerCanExecute}");
}

Working with Symbolic Links

The following example demonstrates symbolic link handling:

using Rheo.Storage.Information;

var fileInfo = new FileInformation("/home/user/link");

if (fileInfo.TryGetUnixStorageInfo(out var unixInfo))
{
    if (unixInfo.SymlinkTarget != null)
    {
        Console.WriteLine($"Symbolic link points to: {unixInfo.SymlinkTarget}");
        
        // Follow the link
        if (File.Exists(unixInfo.SymlinkTarget))
        {
            var targetInfo = new FileInformation(unixInfo.SymlinkTarget);
            Console.WriteLine($"Target size: {targetInfo.FormattedSize}");
        }
    }
}

Formatting Permissions

The following example shows how to format Unix permissions as a string:

using Rheo.Storage.Information;

string FormatPermissions(uint mode)
{
    var perms = new char[9];
    
    // Owner permissions
    perms[0] = (mode & 0x100) != 0 ? 'r' : '-';
    perms[1] = (mode & 0x080) != 0 ? 'w' : '-';
    perms[2] = (mode & 0x040) != 0 ? 'x' : '-';
    
    // Group permissions
    perms[3] = (mode & 0x020) != 0 ? 'r' : '-';
    perms[4] = (mode & 0x010) != 0 ? 'w' : '-';
    perms[5] = (mode & 0x008) != 0 ? 'x' : '-';
    
    // Other permissions
    perms[6] = (mode & 0x004) != 0 ? 'r' : '-';
    perms[7] = (mode & 0x002) != 0 ? 'w' : '-';
    perms[8] = (mode & 0x001) != 0 ? 'x' : '-';
    
    return new string(perms);
}

var fileInfo = new FileInformation("/etc/passwd");

if (fileInfo.TryGetUnixStorageInfo(out var unixInfo))
{
    Console.WriteLine($"Permissions: {FormatPermissions(unixInfo.Mode)}");
}

Thread Safety

This struct is not thread-safe. Instances should not be modified from multiple threads simultaneously.

See Also

Clone this wiki locally