Permalink
Browse files

Make find_new more library friendly

  • Loading branch information...
1 parent e5ea23a commit 8bf4582d1e575dd8cc1bc42ec3d7c34bce61c72c @g2p committed Sep 27, 2012
Showing with 14 additions and 11 deletions.
  1. +3 −2 btrfs-find-new
  2. +11 −9 btrfs.py
View
@@ -12,6 +12,7 @@ but modified to include directories as well.
import argparse
import os
+import sys
from btrfs import find_new
@@ -24,6 +25,6 @@ opts = parser.parse_args()
volume_fd = os.open(opts.volume, os.O_DIRECTORY)
-find_new(volume_fd, opts.generation)
-
+# May raise FindError, let Python print it
+find_new(volume_fd, opts.generation, sys.stdout)
View
@@ -4,7 +4,6 @@
import fcntl
-import sys # XXX
from os import getcwd # XXX
@@ -236,7 +235,11 @@ def name_of_dir_item(item):
return ffi.string(ffi.cast('char*', item + 1), namelen)
-def find_new(volume_fd, min_generation):
+class FindError(Exception):
+ pass
+
+
+def find_new(volume_fd, min_generation, results_file):
args = ffi.new('struct btrfs_ioctl_search_args *')
args_buffer = ffi.buffer(args)
sk = args.key
@@ -259,8 +262,7 @@ def find_new(volume_fd, min_generation):
fcntl.ioctl(
volume_fd, v.BTRFS_IOC_TREE_SEARCH, args_buffer)
except IOError as e:
- sys.stderr.write('Exception %s\n' % (e,))
- sys.exit(1)
+ raise FindError(e)
if sk.nr_items == 0:
break
@@ -281,7 +283,7 @@ def find_new(volume_fd, min_generation):
item)
# XXX How do we name a hardlinked file?
#name = ino_resolve(volume_fd, sh.objectid)
- sys.stderr.write(
+ results_file.write(
'item type %d ino %d len %d gen0 %d gen1 %d\n' % (
sh.type, sh.objectid, sh.len, sh.transid, found_gen))
if found_gen < min_generation:
@@ -291,7 +293,7 @@ def find_new(volume_fd, min_generation):
'struct btrfs_inode_item *', sh + 1)
found_gen = v.btrfs_stack_inode_generation(item)
#name = ino_resolve(volume_fd, sh.objectid)
- sys.stderr.write(
+ results_file.write(
'item type %d ino %d len %d gen0 %d gen1 %d\n' % (
sh.type, sh.objectid, sh.len, sh.transid, found_gen))
if found_gen < min_generation:
@@ -300,21 +302,21 @@ def find_new(volume_fd, min_generation):
ref = ffi.cast(
'struct btrfs_inode_ref *', sh + 1)
name = name_of_inode_ref(ref)
- sys.stderr.write(
+ results_file.write(
'item type %d ino %d len %d gen0 %d name %s\n' % (
sh.type, sh.objectid, sh.len, sh.transid, name))
elif (sh.type == v.BTRFS_DIR_ITEM_KEY
or sh.type == v.BTRFS_DIR_INDEX_KEY):
item = ffi.cast(
'struct btrfs_dir_item *', sh + 1)
name = name_of_dir_item(item)
- sys.stderr.write(
+ results_file.write(
'item type %d dir ino %d len %d'
' gen0 %d gen1 %d type1 %d name %s\n' % (
sh.type, sh.objectid, sh.len,
sh.transid, item.transid, item.type, name))
else:
- sys.stderr.write(
+ results_file.write(
'item type %d oid %d len %d gen0 %d\n' % (
sh.type, sh.objectid, sh.len, sh.transid))
sk.min_objectid = sh.objectid

0 comments on commit 8bf4582

Please sign in to comment.