Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct an in-memory virtual file system and then write this structure to disk

I'm looking for a way to create a virtual file system in Python for creating directories and files, before writing these directories and files to disk.

Using PyFilesystem I can construct a memory filesystem using the following:

>>> import fs
>>> dir = fs.open_fs('mem://')
>>> dir.makedirs('fruit')
SubFS(MemoryFS(), '/fruit')
>>> dir.makedirs('vegetables')
SubFS(MemoryFS(), '/vegetables')
>>> with dir.open('fruit/apple.txt', 'w') as apple: apple.write('braeburn')
... 
8
>>> dir.tree()
├── fruit
│   └── apple.txt
└── vegetables

Ideally, I want to be able to do something like:

dir.write_to_disk('<base path>')

To write this structure to disk, where <base path> is the parent directory in which this structure will be created.

As far as I can tell, PyFilesystem has no way of achieving this. Is there anything else I could use instead or would I have to implement this myself?

like image 324
James Vickery Avatar asked Jul 24 '18 22:07

James Vickery


People also ask

Which file system is used as a virtual system to maintain?

Linux File Systems Linux supports multiple file systems through the use of a virtual file system (VFS).

What is the first step in building FFS in Unix system?

For files, FFS does two things. First, it makes sure (in the general case) to allocate the data blocks of a file in the same group as its inode, thus preventing long seeks between inode and data (as in the old file system).

How file system is represented in the kernel?

The kernel keeps track of files using in-core inodes ("index nodes"), usually derived by the low-level filesystem from on-disk inodes. A file may have several names, and there is a layer of dentries ("directory entries") that represent pathnames, speeding up the lookup operation.


1 Answers

You can use fs.copy.copy_fs() to copy from one filesystem to another, or fs.move.move_fs() to move the filesystem altogether.

Given that PyFilesystem also abstracts around the underlying system filesystem - OSFS - in fact, it's the default protocol, all you need is to copy your in-memory filesystem (MemoryFS) to it and, in effect, you'll have it written to the disk:

import fs
import fs.copy

mem_fs = fs.open_fs('mem://')
mem_fs.makedirs('fruit')
mem_fs.makedirs('vegetables')
with mem_fs.open('fruit/apple.txt', 'w') as apple:
    apple.write('braeburn')

# write to the CWD for testing...
with fs.open_fs(".") as os_fs:  # use a custom path if you want, i.e. osfs://<base_path>
    fs.copy.copy_fs(mem_fs, os_fs)
like image 89
zwer Avatar answered Sep 18 '22 14:09

zwer