diff options
author | Eric Harney <eharney@redhat.com> | 2019-02-20 14:37:36 -0500 |
---|---|---|
committer | Eric Harney <eharney@redhat.com> | 2019-02-22 16:41:12 -0500 |
commit | 2e292ddeb4148e12b8972fa0a25c03ab4c5e61a8 (patch) | |
tree | 9435d2be32afdef9d25ab1ea20210b75ab9443de /cinder/privsep | |
parent | a62c9dfdd41ab0be8bedd99ca39b82701d73ef4f (diff) | |
download | cinder-2e292ddeb4148e12b8972fa0a25c03ab4c5e61a8.tar.gz |
Use native python truncate for privsep
Call truncate directly rather than shelling
out to truncate.
Change-Id: Ib9b1fcd268785c314a2fb8f4a9ee3fac57d68703
Diffstat (limited to 'cinder/privsep')
-rw-r--r-- | cinder/privsep/fs.py | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/cinder/privsep/fs.py b/cinder/privsep/fs.py index f880a6d8c..e96fbab37 100644 --- a/cinder/privsep/fs.py +++ b/cinder/privsep/fs.py @@ -20,9 +20,39 @@ Helpers for filesystem related routines. from oslo_concurrency import processutils +from oslo_log import log as logging import cinder.privsep +LOG = logging.getLogger(__name__) + +mult_table = {'K': 1024} +mult_table['M'] = mult_table['K'] * 1024 +mult_table['G'] = mult_table['M'] * 1024 +mult_table['T'] = mult_table['G'] * 1024 +mult_table['P'] = mult_table['T'] * 1024 +mult_table['E'] = mult_table['P'] * 1024 + + +def _convert_sizestr(sizestr): + try: + ret = int(sizestr) + return ret + except ValueError: + pass + + error = ValueError(sizestr + " is not a valid sizestr") + + unit = sizestr[-1:].upper() + if unit in mult_table: + try: + ret = int(sizestr[:-1]) * mult_table[unit] + except ValueError: + raise error + return ret + + raise error + @cinder.privsep.sys_admin_pctxt.entrypoint def umount(mountpoint): @@ -30,5 +60,18 @@ def umount(mountpoint): @cinder.privsep.sys_admin_pctxt.entrypoint -def truncate(size, path): - processutils.execute('truncate', '-s', size, path) +def _truncate(size, path): + # On Python 3.6, os.truncate() can accept a path arg. + # For now, do it this way. + with open(path, 'a+b') as f: + f.truncate(size) + + +def truncate(sizestr, path): + # TODO(eharney): change calling code to use bytes instead size strings + size = _convert_sizestr(sizestr) + + LOG.debug('truncating file %(path)s to %(size)s bytes', {'path': path, + 'size': size}) + + _truncate(size, path) |