]> git-server-git.apps.pok.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>
Tue, 2 Aug 2016 10:57:57 +0000 (16:27 +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>
(cherry picked from commit 5678584f4176d07301acd7f57acc4efd7fb20e43)

src/pybind/cephfs/cephfs.pyx
src/test/pybind/test_cephfs.py

index d3b581b815774fcd09c80dc907253de46dfaae33..4810ba40dcf5fdf0667f56d648229b8b0eace0a2 100644 (file)
@@ -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
index baea91420840d4969d4f65bc8d8de322d5148567..6267b5f34d0f71c8aa0ccbb46e5e09da83b8e01d 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
 
@@ -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')