More .NET components

Virtual file systems

File system providers #

Rebex File Server supports custom file system providers. This makes it possible to present any kind of file system to users connected to the server's SFTP, SCP or virtual shell subsystems.

All file system providers inherit from Rebex.IO.FileSystem.FileSystemProvider class. Custom file system providers inherit from Rebex.IO.FileSystem.ReadOnlyFileSystemProvider or Rebex.IO.FileSystem.ReadWriteFileSystemProvider class.

Built-in providers #

Rebex File Server comes with several ready-to-use file system providers that can either be used as-is, modified using the notifier functionality, or used within another provider (such as the built-in mount-capable provider). The following built-in providers are available:

Tip: To see all these providers in action, check out our virtual file systems sample.

Local file system provider #

Rebex.IO.FileSystem.LocalFileSystemProvider is a file system provider that provides access to a specific directory of the local (physical) file system.

CSharp

// create an instance of the local file system provider that provides the content
// of the specified directory
var localFS = new LocalFileSystemProvider(@"c:\inetpub\sftproot", FileSystemType.ReadWrite);

// create a server user
var user02 = new FileServerUser("user02", "password");
server.Users.Add(user02);

// use the local file system provider as this user's virtual file system
user02.SetFileSystem(localFS);

Memory-based file system #

Rebex.IO.FileSystem.MemoryFileSystemProvider is a file system provider that stores all data in memory. This means that its content is deleted when the provider instance is discarded. It essentially works like a RAM-disk.

The following sample code creates a user who only has access to a memory-based file system, which is initially empty:

CSharp

// create an instance of the memory file system provider
var memoryFS = new MemoryFileSystemProvider();

// create a server user
var user01 = new FileServerUser("user01", "password");
server.Users.Add(user01);

// use the memory file system provider as this user's virtual file system
user01.SetFileSystem(memoryFS);

Note: Files uploaded to the memory-based file system remain there even after the user disconnects, and can be accessed again later. However, they will disappear when the server application ends or when the instance of MemoryFileSystemProvider is disposed.

Mount-capable file system #

Rebex.IO.FileSystem.MountCapableFileSystemProvider is a file system provider that makes it possible to create composite file systems. This is done by assigning distinct providers to serve different parts of the composite file system, in a way that resembles mount/umount Unix commands:

CSharp

// create an instance of mount-capable file system provider
var virtualFS = new MountCapableFileSystemProvider();

// create a read-write instance of the local file system provider
var homeFS = new LocalFileSystemProvider(@"c:\data\users\user03", FileSystemType.ReadWrite);
virtualFS.Mount("/home/user03", homeFS);

// create a read-only instance of the local file system provider
var publicFS = new LocalFileSystemProvider(@"c:\data\public", FileSystemType.ReadOnly);
virtualFS.Mount("/public", publicFS);

// create an instance of the memory file system provider
var tempFS = new MemoryFileSystemProvider();
virtualFS.Mount("/temp", tempFS);

// create a server user
var user03 = new FileServerUser("user03", "password");
server.Users.Add(user03);

// use the mount-capable file system provider as this user's virtual file system
user03.SetFileSystem(virtualFS);

Custom file system providers #

Rebex.IO.FileSystem.ReadOnlyFileSystemProvider and Rebex.IO.FileSystem.ReadWriteFileSystemProvider classes make it possible to implement a custom file system that stores data wherever it needs. It could be a database, a cloud, or something entirely different.

Using a custom file system provider is just as simple as using one of the built-in providers:

CSharp

// create an instance of the local file system provider that provides the content
// of the specified directory
var customFS = new CustomFileSystemProvider(customConfig);

// create a server user
var user04 = new FileServerUser("user04", "password");
server.Users.Add(user04);

// use the custom file system provider as this user's virtual file system
user04.SetFileSystem(customFS);

Tip: Custom file system providers can be mounted into MountCapableFileSystemProvider as well.

Our virtual file systems sample includes sample source code for RegistryFileSystemProvider (which presents the Windows registry as a file system) and DriveFileSystemProvider (a lightweight local file system provider based on System.IO).

File system notifiers (filters) #

Rebex File Server file system API also makes it possible to easily modify behavior of existing providers. All providers support the GetFileSystemNotifier extension method that returns an instance of Rebex.IO.FileSystem.Notifications.FileSystemNotifier - a notifier class that provides a rich set of events associated with file system operations.

A typical order of these hook events is:

  • Preview event (such as CreatePreview) (a pre-hook event).
  • Surrogate event (such as CreateSurrogate) (a pre-hook event).
  • Completed event (such as CreateCompleted) (a post-hook event).

To get started, create an instance of any FileSystemProvider:

CSharp

// create an instance of the local file system provider that provides the content
// of the specified directory
var localFS = new LocalFileSystemProvider(@"c:\inetpub\sftproot", FileSystemType.ReadWrite);

// create a server user
var user02 = new FileServerUser("user02", "password");
server.Users.Add(user02);

// use the local file system provider as this user's virtual file system
user02.SetFileSystem(localFS);

Then, get an instance of FileSystemNotifier:

CSharp

// get an instance of localFS's notifier
FileSystemNotifier notifier = localFS.GetFileSystemNotifier();

Note: To be able to see and use the extension method, make sure to add using be able to see and use the extension method, make sure to add using Rebex.IO.FileSystem.Notifications; to your code.

Finally, supply the events you need. For example, to prevent creation of certain files or directories, register the 'Create' preview event:

CSharp

// register 'Create' preview event
notifier.CreatePreview += (sender, args) =>
{
    // is it a file with an extension other than '.zip'?
    if (args.Node.IsFile && !args.Node.Extension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
    {
        // cancel creation of these files
        args.CancelOperation();
    }
};

Hook events are powerful. The 'GetContent' surrogate event can even be used to generate file contents on-demand:

CSharp

// register 'GetContent' surrogate event
notifier.GetContentSurrogate += (sender, args) =>
{
    // is it a file called 'orders.txt'?
    if (args.Node.IsFile && args.Node.Name == "orders.txt")
    {
        // generate content for these file on-the-fly

        // retrieve parent directory path
        string path = args.Node.Parent.Path.ToString();

        // create a stream representing the desired file content by calling a custom method
        Stream content = GetOrdersAsStream(path);

        // present the content (in read-only mode) instead of the real file content
        args.ResultContent = NodeContent.CreateReadOnlyContent(content);
        args.MarkAsHandled();
    }
};