]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind: enable integer flags to libcephfs open
authorJohn Spray <john.spray@redhat.com>
Mon, 7 Mar 2016 13:06:41 +0000 (13:06 +0000)
committerRamana Raja <rraja@redhat.com>
Mon, 18 Jul 2016 10:06:40 +0000 (15:36 +0530)
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 <john.spray@redhat.com>
src/pybind/cephfs/cephfs.pyx
src/test/pybind/test_cephfs.py

index 7abf967c4eb06b5d9d3f90b28d18d041af9fc73d..b2232d329f6fb26bf6d666a80367d3cf683df757 100644 (file)
@@ -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
index 25e7bf0269764201c7799e9c4ce7e7e1d7df3901..607e9de6853c5494ff71cb9b67d23d9e27fee04c 100644 (file)
@@ -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')