From: John Spray Date: Mon, 7 Mar 2016 13:06:41 +0000 (+0000) Subject: pybind: enable integer flags to libcephfs open X-Git-Tag: ses5-milestone5~353^2~13 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5678584f4176d07301acd7f57acc4efd7fb20e43;p=ceph.git 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 --- diff --git a/src/pybind/cephfs/cephfs.pyx b/src/pybind/cephfs/cephfs.pyx index 7abf967c4eb..b2232d329f6 100644 --- a/src/pybind/cephfs/cephfs.pyx +++ b/src/pybind/cephfs/cephfs.pyx @@ -608,25 +608,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 25e7bf02697..607e9de6853 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 @@ -132,6 +133,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')