Rebex File Server
SFTP, SCP and SSH server library for .NET
Download 30-day free trial Buy from $349More .NET libraries
-
Rebex SFTP
.NET SFTP client
-
Rebex FTP
.NET FTP client
-
Rebex Total Pack
All Rebex libraries together
Back to feature list...
Virtual file systems
On this page:
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.
Rebex.FileSystem
assembly.
For more information, see Rebex.IO API.
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:
LocalFileSystemProvider
- local (physical) file system providerMemoryFileSystemProvider
- memory-based file system provider (RAM disk)MountCapableFileSystemProvider
- mount-capable file system provider
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.
// 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:
// 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:
// 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:
// 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);
Custom file system providers can be used without the File Server library, which is very useful for testing and development. See Rebex.IO API for more information.
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
:
// 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
:
// 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 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:
// 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:
// 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(); } };
Rebex.IO API
Rebex.IO
API makes it possible to perform operations on file and directories in a virtual file systems using
a simple API that resembles .NET's System.IO
.
The following classes are available:
These make it simple to test
custom file system providers,
or to use file system providers in a stand-alone mode, without Rebex.FileServer
assembly.
// settings for a virtual drive var settings = new FileSystemProviderSettings(); settings.EnableRegistrationAsDrive = true; settings.DriveName = "ramdisk"; // create an instance of a virtual file system provider // based on the settings above var virtualFS = new MemoryFileSystemProvider(settings); // create a subdirectory in the root of the virtual filesystem VDirectory.CreateDirectory("ramdisk:/dir1");
Virtual file system paths start with the virtual drive name, which can be assigned when initializing the provider. Once registered, they are available throughout the whole application. The following example shows how to copy a file to a subdirectory:
// create a file info object for an existing file var fileInfo = new VFileInfo("ramdisk:/file.txt"); // copy the file to a subdirectory fileInfo.CopyTo("ramdisk:/dir1/file.txt");
Creating a file and writing text or other data is just as simple as with System.IO
API:
// create a text file using (StreamWriter writer = VFile.CreateText("ramdisk:/file.txt")) { // write data to the file writer.WriteLine("Hi there!"); }
Another way of creating a file and modyfing its attributes:
// create a file info object for a non-existent file var fileInfo = new VFileInfo("ramdisk:/file.txt"); // create a text file using (StreamWriter writer = fileInfo.CreateText()) { // write data to the file writer.WriteLine("Hi there!"); } // change creation date of the file fileInfo.CreationTime = DateTime.Now.AddDays(-1);
Note: By default, all file system provider instances are registered as virtual drives and are available
for access in the whole application. If this is not desirable, set FileSystemProviderSettings
's
EnableRegistrationAsDrive
property to false
when creating the provider instance.
Back to feature list...