Table of Contents

Class UnixUtils

Namespace
NanoByte.Common.Native
Assembly
NanoByte.Common.dll

Provides helper methods for Unix-specific features of the Mono library.

public static class UnixUtils
Inheritance
UnixUtils

Remarks

Make sure to check IsUnix before calling any methods in this class to avoid exceptions.

Properties

CpuType

The CPU type as reported by the "uname" system call (after applying some normalization).

public static string CpuType { get; }

Property Value

string

HasGui

true if there is an X Server running or the current operating system is MacOS X.

public static bool HasGui { get; }

Property Value

bool

IsFreeBSD

true if the current operating system is FreeBSD.

public static bool IsFreeBSD { get; }

Property Value

bool

IsLinux

true if the current operating system is Linux.

public static bool IsLinux { get; }

Property Value

bool

IsMacOSX

true if the current operating system is MacOS X.

public static bool IsMacOSX { get; }

Property Value

bool

IsUnix

true if the current operating system is a Unixoid system (e.g. Linux or MacOS X).

public static bool IsUnix { get; }

Property Value

bool

OSName

The operating system name as reported by the "uname" system call.

public static string OSName { get; }

Property Value

string

Methods

Creates a new Unix hard link between two files.

public static void CreateHardlink(string sourcePath, string targetPath)

Parameters

sourcePath string

The path of the link to create.

targetPath string

The absolute path of the existing file to point to.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

Creates a new Unix symbolic link to a file or directory.

public static void CreateSymlink(string sourcePath, string targetPath)

Parameters

sourcePath string

The path of the link to create.

targetPath string

The path of the existing file or directory to point to (relative to sourcePath).

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

GetFileSystem(string)

Determines the file system type a file or directory is stored on.

public static string? GetFileSystem(string path)

Parameters

path string

The path of the file.

Returns

string

The name of the file system in fstab format (e.g. ext3 or ntfs-3g); null if unable to determine.

Remarks

Only works on Linux, not on other Unixes (e.g. MacOS X).

Exceptions

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

GetInode(string)

Returns the Inode ID of a file.

public static long GetInode(string path)

Parameters

path string

The path of the file.

Returns

long

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

GetXattr(string, string)

Gets an extended file attribute.

public static byte[]? GetXattr(string path, string name)

Parameters

path string

The path of the file to read the attribute from.

name string

The name of the attribute to read.

Returns

byte[]

The contents of the attribute as a byte array; null if there was a problem reading the file.

IsExecutable(string)

Checks whether a file is marked as Unix-executable.

public static bool IsExecutable(string path)

Parameters

path string

The file to check for executable rights.

Returns

bool

true if path points to an executable; false otherwise.

Remarks

Will return false for non-existing files.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IsRegularFile(string)

Checks whether a file is a regular file (i.e. not a device file, symbolic link, etc.).

public static bool IsRegularFile(string path)

Parameters

path string

Returns

bool

true if path points to a regular file; false otherwise.

Remarks

Will return false for non-existing files.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

Checks whether a file is a Unix symbolic link.

public static bool IsSymlink(string path)

Parameters

path string

The path of the file to check.

Returns

bool

true if path points to a symbolic link; false otherwise.

Will return false for non-existing files.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

Checks whether a file is a Unix symbolic link.

public static bool IsSymlink(string path, out string target)

Parameters

path string

The path of the file to check.

target string

Returns the target the symbolic link points to if it exists.

Returns

bool

true if path points to a symbolic link; false otherwise.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

MakeReadOnly(string)

Removes write permissions for everyone on a filesystem object (file or directory).

public static void MakeReadOnly(string path)

Parameters

path string

The filesystem object (file or directory) to make read-only.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

MakeWritable(string)

Sets write permissions for the owner on a filesystem object (file or directory).

public static void MakeWritable(string path)

Parameters

path string

The filesystem object (file or directory) to make writable by the owner.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

Rename(string, string)

Renames a file. Atomically replaces the destination if present.

public static void Rename(string source, string destination)

Parameters

source string

The path of the file to rename.

destination string

The new path of the file. Must reside on the same file system as source.

Exceptions

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

SetExecutable(string, bool)

Marks a file as Unix-executable or not Unix-executable.

public static void SetExecutable(string path, bool executable)

Parameters

path string

The file to mark as executable or not executable.

executable bool

true to mark the file as executable, true to mark it as not executable.

Exceptions

InvalidOperationException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).

SetXattr(string, string, byte[])

Sets an extended file attribute.

public static void SetXattr(string path, string name, byte[] data)

Parameters

path string

The path of the file to set the attribute for.

name string

The name of the attribute to set.

data byte[]

The data to write to the attribute.

Exceptions

IOException

The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).