summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2007-09-26 21:06:54 +0000
committerBob Ippolito <bob@redivi.com>2007-09-26 21:06:54 +0000
commitd1b5ebb26b7e7974096e61ef98429ec5067636d0 (patch)
treeee95cafdbe61a7a616cac7e8fc5d90efc2449daa
parentc45b708511e5d89dd8bde8f6d47d3ca325d83cd7 (diff)
downloadxattr-d1b5ebb26b7e7974096e61ef98429ec5067636d0.tar.gz
xattr tool update
-rwxr-xr-xLib/xattr/tool.py62
1 files changed, 50 insertions, 12 deletions
diff --git a/Lib/xattr/tool.py b/Lib/xattr/tool.py
index b7eee1d..3b1121e 100755
--- a/Lib/xattr/tool.py
+++ b/Lib/xattr/tool.py
@@ -29,6 +29,7 @@ import sys
import os
import getopt
import xattr
+import zlib
def usage(e=None):
if e:
@@ -36,9 +37,9 @@ def usage(e=None):
print ""
name = os.path.basename(sys.argv[0])
- print "usage: %s [-l] file [file ...]" % (name,)
- print " %s -p [-l] attr_name file [file ...]" % (name,)
- print " %s -w attr_name attr_value file [file ...]" % (name,)
+ print "usage: %s [-lz] file [file ...]" % (name,)
+ print " %s -p [-lz] attr_name file [file ...]" % (name,)
+ print " %s -w [-z] attr_name attr_value file [file ...]" % (name,)
print " %s -d attr_name file [file ...]" % (name,)
print ""
print "The first form lists the names of all xattrs on the given file(s)."
@@ -49,15 +50,30 @@ def usage(e=None):
print "options:"
print " -h: print this help"
print " -l: print long format (attr_name: attr_value)"
+ print " -z: compress or decompress (if compressed) attribute value in zip format"
if e:
sys.exit(64)
else:
sys.exit(0)
+class NullsInString(Exception):
+ """Nulls in string."""
+
+_FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
+
+def _dump(src, length=16):
+ result=[]
+ for i in xrange(0, len(src), length):
+ s = src[i:i+length]
+ hexa = ' '.join(["%02X"%ord(x) for x in s])
+ printable = s.translate(_FILTER)
+ result.append("%04X %-*s %s\n" % (i, length*3, hexa, printable))
+ return ''.join(result)
+
def main():
try:
- (optargs, args) = getopt.getopt(sys.argv[1:], "hlpwd", ["help"])
+ (optargs, args) = getopt.getopt(sys.argv[1:], "hlpwdz", ["help"])
except getopt.GetoptError, e:
usage(e)
@@ -66,6 +82,8 @@ def main():
read = False
write = False
delete = False
+ compress = lambda x: x
+ decompress = compress
status = 0
for opt, arg in optargs:
@@ -75,10 +93,19 @@ def main():
long_format = True
elif opt == "-p":
read = True
+ if write or delete:
+ usage("-p not allowed with -w or -d")
elif opt == "-w":
write = True
+ if read or delete:
+ usage("-w not allowed with -p or -d")
elif opt == "-d":
delete = True
+ if read or write:
+ usage("-d not allowed with -p or -w")
+ elif opt == "-z":
+ compress = zlib.compress
+ decompress = zlib.decompress
if write or delete:
if long_format:
@@ -115,7 +142,7 @@ def main():
if write:
try:
- attrs[attr_name] = attr_value
+ attrs[attr_name] = compress(attr_value)
except (IOError, OSError), e:
onError(e)
continue
@@ -147,17 +174,28 @@ def main():
for attr_name in attr_names:
try:
- if long_format:
- print "".join((file_prefix, "%s: " % (attr_name,), attrs[attr_name]))
- else:
- if read:
- print "".join((file_prefix, attrs[attr_name]))
- else:
- print "".join((file_prefix, attr_name))
+ try:
+ attr_value = decompress(attrs[attr_name])
+ except zlib.error:
+ attr_value = attrs[attr_name]
except KeyError:
onError("%sNo such xattr: %s" % (file_prefix, attr_name))
continue
+ if long_format:
+ try:
+ if attr_value.find('\0') >= 0:
+ raise NullsInString;
+ print "".join((file_prefix, "%s: " % (attr_name,), attr_value))
+ except (UnicodeDecodeError, NullsInString):
+ print "".join((file_prefix, "%s:" % (attr_name,)))
+ print _dump(attr_value)
+ else:
+ if read:
+ print "".join((file_prefix, attr_value))
+ else:
+ print "".join((file_prefix, attr_name))
+
sys.exit(status)
if __name__ == "__main__":