create_diskfs.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # This file is part of asmc, a bootstrapping OS with minimal seed
  4. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  5. # https://gitlab.com/giomasce/asmc
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. import sys
  17. import os
  18. import struct
  19. import shutil
  20. def main():
  21. args = sys.stdin.read().split(None)
  22. assert len(args) % 2 == 0
  23. file_num = (len(args)) // 2
  24. table_len = 0
  25. files_len = 0
  26. names = []
  27. sizes = []
  28. starts = []
  29. for i in range(file_num):
  30. name = args[2*i].encode('ascii')
  31. data_filename = args[2*i+1]
  32. data_size = os.stat(data_filename).st_size
  33. names.append(name)
  34. sizes.append(data_size)
  35. starts.append(files_len)
  36. table_len += 9 + len(name)
  37. files_len += data_size
  38. sys.stdout.buffer.write(b'DISKFS ')
  39. # 8 bytes for DISKFS and one another for the final null
  40. table_len += 9
  41. for i in range(file_num):
  42. sys.stdout.buffer.write(names[i])
  43. sys.stdout.buffer.write(b'\0')
  44. sys.stdout.buffer.write(struct.pack('!II', starts[i] + table_len, sizes[i]))
  45. sys.stdout.buffer.write(b'\0')
  46. for i in range(file_num):
  47. with open(args[2*i+1], 'rb') as fin:
  48. shutil.copyfileobj(fin, sys.stdout.buffer)
  49. if __name__ == '__main__':
  50. main()