NanoByte.Common
Static Public Member Functions | List of all members
NanoByte.Common.Storage.FileUtils Class Reference

Provides filesystem-related helper methods. More...

Static Public Member Functions

static string PathCombine (params string[] paths)
 Combines an array of strings into a path. More...
 
static ? string UnifySlashes (string? value)
 Replaces Unix-style directory slashes with Path.DirectorySeparatorChar. More...
 
static bool IsBreakoutPath ([Localizable(false)] string path)
 Determines whether a path might escape its parent directory (by being absolute or using ..). More...
 
static string RelativeTo (this FileSystemInfo target, FileSystemInfo baseRef)
 Returns a relative path pointing to target from baseRef using Unix/Uri-style directory separators. More...
 
static bool ExistsCaseSensitive ([Localizable(false)] string path)
 Like File.Exists but case-sensitive, even on Windows. More...
 
static void Touch ([Localizable(false)] string path)
 Sets the "last modified" timestamp for a file to now. Creates a new empty file if it does not exist yet. More...
 
static long ToUnixTime (this DateTime time)
 Converts a DateTime into the number of seconds since the Unix epoch (1970-1-1). More...
 
static DateTime FromUnixTime (long time)
 Converts a number of seconds since the Unix epoch (1970-1-1) into a DateTime. More...
 
static int DetermineTimeAccuracy ([Localizable(false)] string path)
 Determines the accuracy with which the filesystem underlying a specific directory can store file-changed times. More...
 
static string GetTempFile ([Localizable(false)] string prefix)
 Creates a uniquely named, empty temporary file on disk and returns the full path of that file. More...
 
static string GetTempDirectory ([Localizable(false)] string prefix)
 Creates a uniquely named, empty temporary directory on disk and returns the full path of that directory. More...
 
static void Replace ([Localizable(false)] string sourcePath, [Localizable(false)] string destinationPath)
 Replaces one file with another. Rolls back in case of problems. If the destination file does not exist yet, this acts like a simple rename. More...
 
static ? string ReadFirstLine (this FileInfo file, Encoding encoding)
 Reads the first line of text from a file. More...
 
static void Walk (this DirectoryInfo directory, Action< DirectoryInfo >? dirAction=null, Action< FileInfo >? fileAction=null, bool followDirSymlinks=false)
 Walks a directory structure recursively and performs an action for every directory and file encountered. More...
 
static DirectoryInfo WalkThroughPrefix (this DirectoryInfo directory)
 Skips through any directories that only contain a single subdirectory and no files. More...
 
static string[] GetFilesRecursive (string path)
 Returns the full paths of all files in a directory and its subdirectories. More...
 
static void ResetAcl (this DirectoryInfo directoryInfo)
 Removes any custom ACLs a user may have set, restores ACL inheritance and sets the Administrators group as the owner. More...
 
static void CanonicalizeAcl (this ObjectSecurity objectSecurity)
 Fixes ACLs that are not canonical (not ordered correctly). More...
 
static void EnableWriteProtection ([Localizable(false)] string path)
 Uses the best means the current platform provides to prevent further write access to a directory (read-only attribute, ACLs, Unix octals, etc.). More...
 
static void DisableWriteProtection ([Localizable(false)] string path)
 Removes whatever means the current platform provides to prevent write access to a directory (read-only attribute, ACLs, Unix octals, etc.). More...
 
static void CreateSymlink ([Localizable(false)] string sourcePath, [Localizable(false)] string targetPath)
 Creates a new symbolic link to a file or directory. More...
 
static void CreateHardlink ([Localizable(false)] string sourcePath, [Localizable(false)] string targetPath)
 Creates a new hard link between two files. More...
 
static bool AreHardlinked ([Localizable(false)] string path1, [Localizable(false)] string path2)
 Determines whether two files are hardlinked. More...
 
static bool IsRegularFile ([Localizable(false)] string path)
 Checks whether a file is a regular file (i.e. not a device file, symbolic link, etc.). More...
 
static bool IsSymlink ([Localizable(false)] string path)
 Checks whether a file is a symbolic link. More...
 
static bool IsSymlink ([Localizable(false)] string path, out string? target)
 Checks whether a file is a symbolic link. More...
 
static bool IsSymlink (this FileSystemInfo item, out string? target)
 Checks whether a file is a Unix symbolic link. More...
 
static bool IsExecutable ([Localizable(false)] string path)
 Checks whether a file is marked as Unix-executable. More...
 
static void SetExecutable ([Localizable(false)] string path, bool executable)
 Marks a file as Unix-executable or not Unix-executable. Only works on Unixoid systems! More...
 
static bool IsUnixFS ([Localizable(false)] string path)
 Checks whether a directory is located on a filesystem with support for Unixoid features such as executable bits. More...
 
static ? byte[] ReadExtendedMetadata ([Localizable(false)] string path, [Localizable(false)] string name)
 Reads metadata from an NTFS Alternate Data Stream (Windows) or extended file attribute (Unixoid). More...
 
static void WriteExtendedMetadata ([Localizable(false)] string path, [Localizable(false)] string name, byte[] data)
 Writes metadata to an NTFS Alternate Data Stream (Windows) or extended file attribute (Unixoid). More...
 

Detailed Description

Provides filesystem-related helper methods.

Member Function Documentation

◆ AreHardlinked()

static bool NanoByte.Common.Storage.FileUtils.AreHardlinked ( [Localizable(false)] string  path1,
[Localizable(false)] string  path2 
)
inlinestatic

Determines whether two files are hardlinked.

Parameters
path1The path of the first file.
path2The path of the second file.
Exceptions
IOExceptionCreating the files failed.
UnauthorizedAccessExceptionYou have insufficient rights to check the files.

◆ CanonicalizeAcl()

static void NanoByte.Common.Storage.FileUtils.CanonicalizeAcl ( this ObjectSecurity  objectSecurity)
inlinestatic

Fixes ACLs that are not canonical (not ordered correctly).

◆ CreateHardlink()

static void NanoByte.Common.Storage.FileUtils.CreateHardlink ( [Localizable(false)] string  sourcePath,
[Localizable(false)] string  targetPath 
)
inlinestatic

Creates a new hard link between two files.

Parameters
sourcePathThe path of the link to create.
targetPathThe absolute path of the existing file to point to.
Exceptions
IOExceptionCreating the hard link failed.
UnauthorizedAccessExceptionYou have insufficient rights to create the hard link.
PlatformNotSupportedExceptionThis method is called on a system with no hard link support.

◆ CreateSymlink()

static void NanoByte.Common.Storage.FileUtils.CreateSymlink ( [Localizable(false)] string  sourcePath,
[Localizable(false)] string  targetPath 
)
inlinestatic

Creates a new symbolic link to a file or directory.

Parameters
sourcePathThe path of the link to create.
targetPathThe path of the existing file or directory to point to (relative to sourcePath ).
Exceptions
IOExceptionCreating the symbolic link failed.
UnauthorizedAccessExceptionYou have insufficient rights to create the symbolic link.
PlatformNotSupportedExceptionThis method is called on a system with no symbolic link support.

◆ DetermineTimeAccuracy()

static int NanoByte.Common.Storage.FileUtils.DetermineTimeAccuracy ( [Localizable(false)] string  path)
inlinestatic

Determines the accuracy with which the filesystem underlying a specific directory can store file-changed times.

Parameters
pathThe path of the directory to check.
Returns
The accuracy in number of seconds. (i.e. 0 = perfect, 1 = may be off by up to one second)
Exceptions
DirectoryNotFoundExceptionThe specified directory doesn't exist.
IOExceptionWriting to the directory fails.
UnauthorizedAccessExceptionYou have insufficient rights to write to the directory.

◆ DisableWriteProtection()

static void NanoByte.Common.Storage.FileUtils.DisableWriteProtection ( [Localizable(false)] string  path)
inlinestatic

Removes whatever means the current platform provides to prevent write access to a directory (read-only attribute, ACLs, Unix octals, etc.).

May do nothing if the platform doesn't provide any known protection mechanisms.

Parameters
pathThe directory to unprotect.
Exceptions
IOExceptionThere was a problem removing the write protection.
UnauthorizedAccessExceptionYou have insufficient rights to remove the write protection.

◆ EnableWriteProtection()

static void NanoByte.Common.Storage.FileUtils.EnableWriteProtection ( [Localizable(false)] string  path)
inlinestatic

Uses the best means the current platform provides to prevent further write access to a directory (read-only attribute, ACLs, Unix octals, etc.).

May do nothing if the platform doesn't provide any known protection mechanisms.

Parameters
pathThe directory to protect.
Exceptions
IOExceptionThere was a problem applying the write protection.
UnauthorizedAccessExceptionYou have insufficient rights to apply the write protection.

◆ ExistsCaseSensitive()

static bool NanoByte.Common.Storage.FileUtils.ExistsCaseSensitive ( [Localizable(false)] string  path)
inlinestatic

Like File.Exists but case-sensitive, even on Windows.

◆ FromUnixTime()

static DateTime NanoByte.Common.Storage.FileUtils.FromUnixTime ( long  time)
inlinestatic

Converts a number of seconds since the Unix epoch (1970-1-1) into a DateTime.

◆ GetFilesRecursive()

static string [] NanoByte.Common.Storage.FileUtils.GetFilesRecursive ( string  path)
inlinestatic

Returns the full paths of all files in a directory and its subdirectories.

Parameters
pathThe path of the directory to search for files.

◆ GetTempDirectory()

static string NanoByte.Common.Storage.FileUtils.GetTempDirectory ( [Localizable(false)] string  prefix)
inlinestatic

Creates a uniquely named, empty temporary directory on disk and returns the full path of that directory.

Parameters
prefixA short string the filename should start with.
Returns
The full path of the newly created temporary directory.
Exceptions
IOExceptionA problem occurred while creating a directory in Path.GetTempPath.
UnauthorizedAccessExceptionCreating a directory in Path.GetTempPath is not permitted.

Use this method, because Path.GetTempFileName exhibits buggy behaviour in some Mono versions.

◆ GetTempFile()

static string NanoByte.Common.Storage.FileUtils.GetTempFile ( [Localizable(false)] string  prefix)
inlinestatic

Creates a uniquely named, empty temporary file on disk and returns the full path of that file.

Parameters
prefixA short string the filename should start with.
Returns
The full path of the newly created temporary file.
Exceptions
IOExceptionA problem occurred while creating a file in Path.GetTempPath.
UnauthorizedAccessExceptionCreating a file in Path.GetTempPath is not permitted.

Use this method, because Path.GetTempFileName exhibits buggy behaviour in some Mono versions.

◆ IsBreakoutPath()

static bool NanoByte.Common.Storage.FileUtils.IsBreakoutPath ( [Localizable(false)] string  path)
inlinestatic

Determines whether a path might escape its parent directory (by being absolute or using ..).

◆ IsExecutable()

static bool NanoByte.Common.Storage.FileUtils.IsExecutable ( [Localizable(false)] string  path)
inlinestatic

Checks whether a file is marked as Unix-executable.

Returns
true if path points to an executable; false otherwise.

Will return false for non-existing files. Will always return false on non-Unixoid systems.

Exceptions
UnauthorizedAccessExceptionYou have insufficient rights to query the file's properties.

◆ IsRegularFile()

static bool NanoByte.Common.Storage.FileUtils.IsRegularFile ( [Localizable(false)] string  path)
inlinestatic

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

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

Will return false for non-existing files.

Exceptions
UnauthorizedAccessExceptionYou have insufficient rights to query the file's properties.

◆ IsSymlink() [1/3]

static bool NanoByte.Common.Storage.FileUtils.IsSymlink ( [Localizable(false)] string  path)
inlinestatic

Checks whether a file is a symbolic link.

Parameters
pathThe path of the file to check.
Returns
true if path points to a symbolic link; false otherwise.
Exceptions
IOExceptionThere was an IO problem reading the file.
UnauthorizedAccessExceptionRead access to the file was denied.

◆ IsSymlink() [2/3]

static bool NanoByte.Common.Storage.FileUtils.IsSymlink ( [Localizable(false)] string  path,
out string?  target 
)
inlinestatic

Checks whether a file is a symbolic link.

Parameters
pathThe path of the file to check.
targetReturns the target the symbolic link points to if it exists.
Returns
true if path points to a symbolic link; false otherwise.
Exceptions
IOExceptionThere was an IO problem reading the file.
UnauthorizedAccessExceptionRead access to the file was denied.

◆ IsSymlink() [3/3]

static bool NanoByte.Common.Storage.FileUtils.IsSymlink ( this FileSystemInfo  item,
out string?  target 
)
inlinestatic

Checks whether a file is a Unix symbolic link.

Parameters
itemThe file to check.
targetReturns the target the symbolic link points to if it exists.
Returns
true if item points to a symbolic link; false otherwise.
Exceptions
IOExceptionThere was an IO problem reading the file.
UnauthorizedAccessExceptionRead access to the file was denied.

◆ IsUnixFS()

static bool NanoByte.Common.Storage.FileUtils.IsUnixFS ( [Localizable(false)] string  path)
inlinestatic

Checks whether a directory is located on a filesystem with support for Unixoid features such as executable bits.

Returns
true if path points to directory on a Unixoid filesystem; false otherwise.

Will always return false on non-Unixoid systems. Only requires read access on Linux to determine file system. Requires write access on other Unixes (e.g. MacOS X).

Exceptions
DirectoryNotFoundExceptionThe specified directory doesn't exist.
IOExceptionChecking the directory failed.
UnauthorizedAccessExceptionYou have insufficient right to stat to the directory.

◆ PathCombine()

static string NanoByte.Common.Storage.FileUtils.PathCombine ( params string[]  paths)
inlinestatic

Combines an array of strings into a path.

Backport of the n-parameter version of Path.Combine(string,string) introduced in .NET 4.0.

◆ ReadExtendedMetadata()

static ? byte [] NanoByte.Common.Storage.FileUtils.ReadExtendedMetadata ( [Localizable(false)] string  path,
[Localizable(false)] string  name 
)
inlinestatic

Reads metadata from an NTFS Alternate Data Stream (Windows) or extended file attribute (Unixoid).

Parameters
pathThe path of the file the Alternate Data Stream is associated with.
nameThe name of the metadata stream.
Returns
The contents of the metadata stream; null if the file exists but the stream specified by name does not.
Exceptions
FileNotFoundExceptionThe file specified by path does not exist.
IOExceptionThere was a problem reading the metadata stream.
PlatformNotSupportedExceptionThe current operating system provides no method for storing extended metadata.

◆ ReadFirstLine()

static ? string NanoByte.Common.Storage.FileUtils.ReadFirstLine ( this FileInfo  file,
Encoding  encoding 
)
inlinestatic

Reads the first line of text from a file.

Parameters
fileThe file to read from.
encodingThe text encoding to use for reading.
Returns
The first line of text in the file; null if decoding does not work on the contents.
Exceptions
IOExceptionA problem occurred while reading the file.
UnauthorizedAccessExceptionRead access to the file is not permitted.

◆ RelativeTo()

static string NanoByte.Common.Storage.FileUtils.RelativeTo ( this FileSystemInfo  target,
FileSystemInfo  baseRef 
)
inlinestatic

Returns a relative path pointing to target from baseRef using Unix/Uri-style directory separators.

◆ Replace()

static void NanoByte.Common.Storage.FileUtils.Replace ( [Localizable(false)] string  sourcePath,
[Localizable(false)] string  destinationPath 
)
inlinestatic

Replaces one file with another. Rolls back in case of problems. If the destination file does not exist yet, this acts like a simple rename.

Parameters
sourcePathThe path of source directory.
destinationPathThe path of the target directory. Must reside on the same filesystem as sourcePath .
Exceptions
ArgumentExceptionsourcePath and destinationPath are equal.
IOExceptionThe file could not be replaced.
UnauthorizedAccessExceptionThe read or write access to one of the files was denied.

◆ ResetAcl()

static void NanoByte.Common.Storage.FileUtils.ResetAcl ( this DirectoryInfo  directoryInfo)
inlinestatic

Removes any custom ACLs a user may have set, restores ACL inheritance and sets the Administrators group as the owner.

◆ SetExecutable()

static void NanoByte.Common.Storage.FileUtils.SetExecutable ( [Localizable(false)] string  path,
bool  executable 
)
inlinestatic

Marks a file as Unix-executable or not Unix-executable. Only works on Unixoid systems!

Parameters
pathThe file to mark as executable or not executable.
executabletrue to mark the file as executable, true to mark it as not executable.
Exceptions
FileNotFoundExceptionpath points to a file that does not exist or cannot be accessed.
UnauthorizedAccessExceptionYou have insufficient rights to change the file's properties.
PlatformNotSupportedExceptionThis method is called on a non-Unixoid system.

◆ Touch()

static void NanoByte.Common.Storage.FileUtils.Touch ( [Localizable(false)] string  path)
inlinestatic

Sets the "last modified" timestamp for a file to now. Creates a new empty file if it does not exist yet.

Exceptions
IOExceptionCreating the file or updating its timestamp failed.
UnauthorizedAccessExceptionYou have insufficient rights to create the file or update its timestamp.

◆ ToUnixTime()

static long NanoByte.Common.Storage.FileUtils.ToUnixTime ( this DateTime  time)
inlinestatic

Converts a DateTime into the number of seconds since the Unix epoch (1970-1-1).

◆ UnifySlashes()

static ? string NanoByte.Common.Storage.FileUtils.UnifySlashes ( string?  value)
static

Replaces Unix-style directory slashes with Path.DirectorySeparatorChar.

◆ Walk()

static void NanoByte.Common.Storage.FileUtils.Walk ( this DirectoryInfo  directory,
Action< DirectoryInfo >?  dirAction = null,
Action< FileInfo >?  fileAction = null,
bool  followDirSymlinks = false 
)
inlinestatic

Walks a directory structure recursively and performs an action for every directory and file encountered.

Parameters
directoryThe directory to walk.
dirActionThe action to perform for every found directory (including the starting directory ); can be null.
fileActionThe action to perform for every found file; can be null.
followDirSymlinksIf true recurse into directory symlinks; if false only execute dirAction for directory symlinks but do not recurse.

◆ WalkThroughPrefix()

static DirectoryInfo NanoByte.Common.Storage.FileUtils.WalkThroughPrefix ( this DirectoryInfo  directory)
inlinestatic

Skips through any directories that only contain a single subdirectory and no files.

Ignores files that start with a dot.

◆ WriteExtendedMetadata()

static void NanoByte.Common.Storage.FileUtils.WriteExtendedMetadata ( [Localizable(false)] string  path,
[Localizable(false)] string  name,
byte[]  data 
)
inlinestatic

Writes metadata to an NTFS Alternate Data Stream (Windows) or extended file attribute (Unixoid).

Parameters
pathThe path of the file to associate the metadata with.
nameThe name of the metadata stream.
dataThe data to write to the metadata stream.
Exceptions
FileNotFoundExceptionThe file specified by path does not exist.
IOExceptionThere was a problem writing the metadata stream.
UnauthorizedAccessExceptionYou have insufficient rights to write the metadata.
PlatformNotSupportedExceptionThe current operating system provides no method for storing extended metadata.

The documentation for this class was generated from the following file: