You are to simulate accessing a virtual file system. First create three separate
ID: 3763771 • Letter: Y
Question
You are to simulate accessing a virtual file system. First create three separate "drives" A, B, and C. Each is to have it's own tree structure with directories and common files. Restrict the number of files under each directory to no more than five. Each common file is to have it's own i-node which need contain only filename, extension, and one line of UNIQUE data (in lieu of a pointer to the data on "disk"). For simplicity, do not make the depth of any directory tree exceed four levels. On top of these file systems build a VFS that makes the three separate systems appear as one big system. All user accesses should be via the VFS through the v-nodes that are provided. The only functions that the VFS is to provide is add a new file, delete a file, and "print"(show the one line of data) a file. You must demonstrate all three operations and show the user's view of the file structure, as well as, the actual file system structure before and after each operation. As before, all commands are to be entered via a GUI except that a data file may be used to build the initial system (instead of entering the pieces one file at a time).Explanation / Answer
You'll need the most recent version of PyFilesystem from SVN to run this code.
We're going to create a virtual filesystem for a fictitious application that requires per-application and per-user resources, as well as a location for cache and log files. I'll also demonstrate how to mount files located on a web server. Here's the code:
That's, all there is to it; two lines of code (one if you don't count the import). Obviously there is quite a bit going on under the hood here, which I'll explain below, but lets see what this code gives you…
The app_fs object is an interface to a single filesystem that contains all the file locations our application will use. For example, the path /user/app.ini references a per-user file, whereas /resources/logo.png references a per application file. The actual physical location of the data is irrelevant because as far as your application is concerned the paths never change. This abstraction is useful because the real path for such files varies according to the platform the code is running on; Windows, Mac and Linux all have different conventions, and if you put your files in the wrong place, your app will likely break on one platform or another.
Here's how a per-user configuration file might be opened:
The files in our virtual filesystem don't even have to reside on the local filesystem. For instance, /live/ may actually reference a location on the web, where the version of the current release and a short ‘message of the day’ is stored.
Here's how the version number and MOTD might be read:
You'll notice that even though the actual data is retrieved over HTTP (the files are located here and here), the code would be no different if the files were stored locally.
So how is all this behaviour created from a single line of code? The line fsopendir("mount://fs.ini", create_dir=True) opens a MountFS from the information contained within an INI file (create_dir=True will create specified directories if they don't exist). Here's an example of an INI file that could be used during development:
The INI file is used to construct a MountFS, where the keys in the [fs] section are the top level directory names and the values are the real locations of the files. In above example, /user/ maps on to a directory called user relative to the current directory – but it could be changed to an absolute path or to a location on a server (e.g. FTP, SFTP, HTTP, DAV), or even to a directory within a zip file.
You can change the section to use in a mount opener by specifying it after a # symbol, i.e. mount://fs.ini#mysection
There are a few changes to this INI file we will need to make when our application is ready for release. User data, site data, logs and cache all have canonical locations that are derived from the name of the application (and the author on Windows). PyFilesystem contains handy openers for these special locations. For example, appuser://examplesoft:myapp detects the appropriate per-user data location for an application called “myapp” developed by “examplesoft”. Ditto for the other per-application directories. e.g.:
The /live/ path is different in that it needs to point to a web server:
Of course, you don't need to use the canonical locations. For instance, let's say you want to store all your static resources in a zip file. No problem:
Or you want to keep your user data on a SFTP (Secure FTP) server:
Perhaps you don't want to preserve the cache across sessions, for security reasons. The temp opener creates files in a temp directory and deletes them on close:
Although, if you are really paranoid you can store the cache files in memory without ever writing them to disk:
Setting /user/ to mem:// is a useful way of simulating a fresh install when debugging.
I hope that covers why you might need – or at least want – a virtual file system in your application. I've glossed over some the details and other features of PyFilesystem. If you would like more information, see my previous posts, check out the documentation or join the PyFilesystem discussion group.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.