From e14dc25e1fca7fd74971cbda74d1fcadff5be3b4 Mon Sep 17 00:00:00 2001 From: John Spray Date: Mon, 7 Mar 2016 13:06:41 +0000 Subject: [PATCH] pybind: enable integer flags to libcephfs open The 'rw+' style flags are handy and convenient, but they don't capture all possibilities. Change to optionally accept an integer here for advance users who want to specify arbitrary combinations of flags. Signed-off-by: John Spray (cherry picked from commit 5678584f4176d07301acd7f57acc4efd7fb20e43) --- src/pybind/cephfs/cephfs.pyx | 35 +++++++++++++++++++--------------- src/test/pybind/test_cephfs.py | 5 +++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/pybind/cephfs/cephfs.pyx b/src/pybind/cephfs/cephfs.pyx index d3b581b81577..4810ba40dcf5 100644 --- a/src/pybind/cephfs/cephfs.pyx +++ b/src/pybind/cephfs/cephfs.pyx @@ -599,25 +599,30 @@ cdef class LibCephFS(object): def open(self, path, flags, mode=0): self.require_state("mounted") - path = cstr(path, 'path') - flags = cstr(flags, 'flags') + if not isinstance(mode, int): raise TypeError('mode must be an int') - cephfs_flags = 0 - if flags == '': - cephfs_flags = os.O_RDONLY + if isinstance(flags, basestring): + flags = cstr(flags, 'flags') + cephfs_flags = 0 + if flags == '': + cephfs_flags = os.O_RDONLY + else: + for c in flags: + if c == 'r': + cephfs_flags |= os.O_RDONLY + elif c == 'w': + cephfs_flags |= os.O_WRONLY | os.O_TRUNC | os.O_CREAT + elif c == '+': + cephfs_flags |= os.O_RDWR + else: + raise OperationNotSupported( + "open flags doesn't support %s" % c) + elif isinstance(flags, int): + cephfs_flags = flags else: - for c in flags: - if c == 'r': - cephfs_flags |= os.O_RDONLY - elif c == 'w': - cephfs_flags |= os.O_WRONLY | os.O_TRUNC | os.O_CREAT - elif c == '+': - cephfs_flags |= os.O_RDWR - else: - raise OperationNotSupported( - "open flags doesn't support %s" % c) + raise TypeError("flags must be a string or an integer") cdef: char* _path = path diff --git a/src/test/pybind/test_cephfs.py b/src/test/pybind/test_cephfs.py index baea91420840..6267b5f34d0f 100644 --- a/src/test/pybind/test_cephfs.py +++ b/src/test/pybind/test_cephfs.py @@ -2,6 +2,7 @@ from nose.tools import assert_raises, assert_equal, with_setup import cephfs as libcephfs import fcntl +import os cephfs = None @@ -121,6 +122,10 @@ def test_open(): cephfs.write(fd, "zxcv", 4) assert_equal(cephfs.read(fd, 4, 8), "zxcv") cephfs.close(fd) + fd = cephfs.open('file-1', os.O_RDWR, 0755) + cephfs.write(fd, "asdf", 0) + assert_equal(cephfs.read(fd, 0, 4), "asdf") + cephfs.close(fd) assert_raises(libcephfs.OperationNotSupported, cephfs.open, 'file-1', 'a') cephfs.unlink('file-1') -- 2.47.3