Guest User

Untitled

a guest
Mar 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. # --*-- encoding: utf-8 -*-
  2. import argparse
  3. import os
  4. import struct
  5.  
  6. class HostEntry(object):
  7.  
  8. def __init__(self, logical_offset, physical_offset, length, pid):
  9. self.logical_offset = logical_offset
  10. self.physical_offset = physical_offset
  11. self.length = length
  12. self.pid = pid
  13.  
  14. def __str__(self):
  15. delimter = ', '
  16. s = ''
  17. s += 'logical_offset: ' + str(self.logical_offset) + delimter
  18. s += 'physical_offset: ' + str(self.physical_offset) + delimter
  19. s += 'length: ' + str(self.length) + delimter
  20. s += 'pid: ' + str(self.pid) + '\n'
  21. return s
  22.  
  23. def __repr__(self):
  24. return str(self)
  25.  
  26.  
  27. def parse(fn):
  28. ret = []
  29. ENTRY_SIZE = 48
  30. LOGICAL = 0
  31. PHYSICAL = 8
  32. LENGTH = 16
  33. PID = 40
  34. with open(fn, 'rb') as inp:
  35. bs = inp.read()
  36. for i in range(len(bs)/48):
  37. offset = i * ENTRY_SIZE
  38. logical_offset = struct.unpack("<LL", bs[offset+LOGICAL:offset+LOGICAL+8])[0]
  39. physical_offset = struct.unpack("<LL", bs[offset+PHYSICAL:offset+PHYSICAL+8])[0]
  40. length = struct.unpack("<LL", bs[offset+LENGTH:offset+LENGTH+8])[0]
  41. pid = struct.unpack("<L", bs[offset+PID:offset+PID+4])[0]
  42. ret.append(HostEntry(logical_offset, physical_offset, length, pid))
  43.  
  44. # return sorted(ret, key=lambda x: x.pid)
  45. return sorted(ret, key=lambda x: x.logical_offset)
  46.  
  47.  
  48. def main(dirpath):
  49. idxs = []
  50. for dirpath, dirnames, filenames in os.walk(dirpath):
  51. for filename in filenames:
  52. if 'index' in filename:
  53. idxs.extend(parse(dirpath+'/'+filename))
  54. break
  55.  
  56. print idxs
  57.  
  58.  
  59.  
  60. if __name__ == '__main__':
  61. parser = argparse.ArgumentParser(description="Parse the index file of PLFS container")
  62. parser.add_argument('-i', type=str, default='/home/deng/fs/backend/poc/hostdir.26', help='输入文件夹')
  63. args = parser.parse_args()
  64. dirpath = args.i
  65. main(dirpath)
Add Comment
Please, Sign In to add comment