IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4KB

  1. #
  2. # Copyright (C) Stanislaw Adaszewski, 2020
  3. # License: GNU General Public License v3.0
  4. # URL: https://github.com/sadaszewski/focker
  5. # URL: https://adared.ch/focker
  6. #
  7. import random
  8. from .zfs import zfs_exists
  9. import os
  10. import fcntl
  11. import hashlib
  12. def filehash(fname):
  13. h = hashlib.sha256()
  14. with open(fname, 'rb') as f:
  15. while True:
  16. data = f.read(1024*1024*4)
  17. if not data:
  18. break
  19. h.update(data)
  20. res = h.hexdigest()
  21. return res
  22. def random_sha256_hexdigest():
  23. for _ in range(10**6):
  24. res = bytes([ random.randint(0, 255) for _ in range(32) ]).hex()
  25. if not res[:7].isnumeric():
  26. return res
  27. raise ValueError('Couldn\'t find random SHA256 hash with non-numeric 7-character prefix in 10^6 trials o_O')
  28. def find_prefix(head, tail):
  29. for pre in range(7, len(tail)):
  30. name = head + tail[:pre]
  31. if not zfs_exists(name):
  32. break
  33. return name
  34. def focker_lock():
  35. os.makedirs('/var/lock', exist_ok=True)
  36. if focker_lock.fd is None:
  37. focker_lock.fd = open('/var/lock/focker.lock', 'a+')
  38. print('Waiting for /var/lock/focker.lock ...')
  39. fcntl.flock(focker_lock.fd, fcntl.LOCK_EX)
  40. print('Lock acquired.')
  41. focker_lock.fd = None
  42. def focker_unlock():
  43. if focker_lock.fd is None:
  44. return
  45. fcntl.flock(focker_lock.fd, fcntl.LOCK_UN)
  46. print('Lock released')