Skip to content

Instantly share code, notes, and snippets.

@kofemann
Last active December 17, 2015 10:59
Show Gist options
  • Save kofemann/5598825 to your computer and use it in GitHub Desktop.
Save kofemann/5598825 to your computer and use it in GitHub Desktop.
dCache NFS file handle decoder
#!/usr/bin/env python
import sys
import struct
import binascii
def hexToByte(h):
bytes = []
h = ''.join( h.split(" ") )
for i in range(0, len(h), 2):
bytes.append( chr( int (h[i:i+2], 16 ) ) )
return ''.join( bytes )
fh = sys.argv[1].upper()
version = struct.unpack('b', hexToByte(fh[0:2]) )[0]
magic = fh[2:8]
generation = struct.unpack('>i', hexToByte(fh[8:16]))[0]
index = struct.unpack('>i', hexToByte(fh[16:24]))[0]
t = struct.unpack('b', hexToByte(fh[24:26]))[0]
l = struct.unpack('b', hexToByte(fh[26:28]) )[0]
handle = hexToByte(fh[28:])
INODE_TYPES = {
0: "INODE",
1: "TAG",
2: "TAGS",
3: "ID",
4: "PATHOF",
5: "PARENT",
6: "NAMWOF",
7: "PGET",
8: "PSET",
9: "CONST",
}
print 'version :', version
print 'magic :', magic
print 'generation:', generation
print 'index :', index
print 'type :', t
print 'opaque len:', l
print 'handle :', fh[28:]
print "fsid :", struct.unpack('b', hexToByte(fh[28:30]) )[0]
inodeType = struct.unpack('b', hexToByte(fh[30:32]) )[0]
print "inode type:", INODE_TYPES[inodeType]
idLen = struct.unpack('b', hexToByte(fh[32:34]) )[0]
print "id len :", idLen
print "id :", fh[34:34+idLen*2]
argsLen = struct.unpack('b', hexToByte(fh[34+idLen*2:36+idLen*2]) )[0]
print "args :", binascii.a2b_hex(fh[36+idLen*2:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment