Show / Hide Table of Contents

Class UnixUtils

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

Inheritance
Object
UnixUtils
Namespace: NanoByte.Common.Native
Assembly: NanoByte.Common.dll
Syntax
public static class UnixUtils : Object
Remarks

This class has a dependency on Mono.Posix. 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).

Declaration
public static string CpuType { get; }
Property Value
Type Description
String

HasGui

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

Declaration
public static bool HasGui { get; }
Property Value
Type Description
Boolean

IsFreeBSD

true if the current operating system is FreeBSD.

Declaration
public static bool IsFreeBSD { get; }
Property Value
Type Description
Boolean

IsLinux

true if the current operating system is Linux.

Declaration
public static bool IsLinux { get; }
Property Value
Type Description
Boolean

IsMacOSX

true if the current operating system is MacOS X.

Declaration
public static bool IsMacOSX { get; }
Property Value
Type Description
Boolean

IsUnix

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

Declaration
public static bool IsUnix { get; }
Property Value
Type Description
Boolean

OSName

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

Declaration
public static string OSName { get; }
Property Value
Type Description
String

Methods

CreateHardlink(String, String)

Creates a new Unix hard link between two files.

Declaration
public static void CreateHardlink(string sourcePath, string targetPath)
Parameters
Type Name Description
String sourcePath

The path of the link to create.

String targetPath

The absolute path of the existing file to point to.

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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

CreateSymlink(String, String)

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

Declaration
public static void CreateSymlink(string sourcePath, string targetPath)
Parameters
Type Name Description
String sourcePath

The path of the link to create.

String targetPath

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

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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.

Declaration
public static string GetFileSystem(string path)
Parameters
Type Name Description
String path

The path of the file.

Returns
Type Description
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
Type Condition
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.

Declaration
public static long GetInode(string path)
Parameters
Type Name Description
String path

The path of the file.

Returns
Type Description
Int64
Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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

GetXattr(String, String)

Gets an extended file attribute.

Declaration
public static byte[] GetXattr(string path, string name)
Parameters
Type Name Description
String path

The path of the file to read the attribute from.

String name

The name of the attribute to read.

Returns
Type Description
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.

Declaration
public static bool IsExecutable(string path)
Parameters
Type Name Description
String path

The file to check for executable rights.

Returns
Type Description
Boolean

true if path points to an executable; false otherwise.

Remarks

Will return false for non-existing files.

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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.).

Declaration
public static bool IsRegularFile(string path)
Parameters
Type Name Description
String path
Returns
Type Description
Boolean

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

Remarks

Will return false for non-existing files.

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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

IsSymlink(String)

Checks whether a file is a Unix symbolic link.

Declaration
public static bool IsSymlink(string path)
Parameters
Type Name Description
String path

The path of the file to check.

Returns
Type Description
Boolean

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

Remarks

Will return false for non-existing files.

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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

IsSymlink(String, out String)

Checks whether a file is a Unix symbolic link.

Declaration
public static bool IsSymlink(string path, out string target)
Parameters
Type Name Description
String path

The path of the file to check.

String target

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

Returns
Type Description
Boolean

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

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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).

Declaration
public static void MakeReadOnly(string path)
Parameters
Type Name Description
String path

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

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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).

Declaration
public static void MakeWritable(string path)
Parameters
Type Name Description
String path

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

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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.

Declaration
public static void Rename(string source, string destination)
Parameters
Type Name Description
String source

The path of the file to rename.

String destination

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

Exceptions
Type Condition
Mono.Unix.UnixIOException

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

SetExecutable(String, Boolean)

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

Declaration
public static void SetExecutable(string path, bool executable)
Parameters
Type Name Description
String path

The file to mark as executable or not executable.

Boolean executable

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

Exceptions
Type Condition
InvalidOperationException

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

Mono.Unix.UnixIOException

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

SetXattr(String, String, Byte[])

Sets an extended file attribute.

Declaration
public static void SetXattr(string path, string name, byte[] data)
Parameters
Type Name Description
String path

The path of the file to set the attribute for.

String name

The name of the attribute to set.

Byte[] data

The data to write to the attribute.

Exceptions
Type Condition
Mono.Unix.UnixIOException

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

In This Article
Back to top Copyright Bastian Eicher