Skip to content

Commit

Permalink
xattr tool update
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Sep 26, 2007
1 parent c45b708 commit d1b5ebb
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions Lib/xattr/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
import os
import getopt
import xattr
import zlib

def usage(e=None):
if e:
print e
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)."
Expand All @@ -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)

Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -115,7 +142,7 @@ def onError(e):

if write:
try:
attrs[attr_name] = attr_value
attrs[attr_name] = compress(attr_value)
except (IOError, OSError), e:
onError(e)
continue
Expand Down Expand Up @@ -147,17 +174,28 @@ def onError(e):

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__":
Expand Down

0 comments on commit d1b5ebb

Please sign in to comment.