[pypy-commit] cffi default: add demo/btrfs-snap.py

RonnyPfannschmidt noreply at buildbot.pypy.org
Tue Jun 26 11:39:39 CEST 2012


Author: Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
Branch: 
Changeset: r524:14bb2dd80cf5
Date: 2012-06-26 11:39 +0200
http://bitbucket.org/cffi/cffi/changeset/14bb2dd80cf5/

Log:	add demo/btrfs-snap.py

	this is a example for ioctrl structure usage

diff --git a/demo/btrfs-snap.py b/demo/btrfs-snap.py
new file mode 100644
--- /dev/null
+++ b/demo/btrfs-snap.py
@@ -0,0 +1,52 @@
+"""
+btrfs-snap.py: source target newname
+
+creates a exactly named snapshots and bails out if they exist
+"""
+
+import argparse
+import fcntl
+import os
+import sys
+
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.cdef("""
+    #define BTRFS_IOC_SNAP_CREATE_V2 ...
+    // needed for some fields
+    typedef unsigned long long __u64;
+    struct btrfs_ioctl_vol_args_v2 {
+        int64_t fd;
+        __u64 transid;
+        __u64 flags;
+        __u64 unused[4];
+        char name[]; ...;
+    };
+""")
+
+v = ffi.verify("#include <btrfs/ioctl.h>")
+
+
+
+parser = argparse.ArgumentParser(usage=__doc__.strip())
+parser.add_argument('source', help='source subvolume')
+parser.add_argument('target', help='target directory')
+parser.add_argument('newname', help='name of the new snapshot')
+opts = parser.parse_args()
+
+source = os.open(opts.source, os.O_DIRECTORY)
+target = os.open(opts.target, os.O_DIRECTORY)
+
+
+args = ffi.new('struct btrfs_ioctl_vol_args_v2')
+args.name = opts.newname
+args.fd = source
+args_buffer = ffi.buffer(args)
+try:
+    fcntl.ioctl(target, v.BTRFS_IOC_SNAP_CREATE_V2, args_buffer)
+except IOError as e:
+    print e
+    sys.exit(1)
+


More information about the pypy-commit mailing list