_ctime_functions.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. def load_ctime_functions():
  3. if os.name == "nt":
  4. import win32_setctime
  5. def get_ctime_windows(filepath):
  6. return os.stat(filepath).st_ctime
  7. def set_ctime_windows(filepath, timestamp):
  8. if not win32_setctime.SUPPORTED:
  9. return
  10. try:
  11. win32_setctime.setctime(filepath, timestamp)
  12. except (OSError, ValueError):
  13. pass
  14. return get_ctime_windows, set_ctime_windows
  15. if hasattr(os.stat_result, "st_birthtime"):
  16. def get_ctime_macos(filepath):
  17. return os.stat(filepath).st_birthtime
  18. def set_ctime_macos(filepath, timestamp):
  19. pass
  20. return get_ctime_macos, set_ctime_macos
  21. if hasattr(os, "getxattr") and hasattr(os, "setxattr"):
  22. def get_ctime_linux(filepath):
  23. try:
  24. return float(os.getxattr(filepath, b"user.loguru_crtime"))
  25. except OSError:
  26. return os.stat(filepath).st_mtime
  27. def set_ctime_linux(filepath, timestamp):
  28. try:
  29. os.setxattr(filepath, b"user.loguru_crtime", str(timestamp).encode("ascii"))
  30. except OSError:
  31. pass
  32. return get_ctime_linux, set_ctime_linux
  33. def get_ctime_fallback(filepath):
  34. return os.stat(filepath).st_mtime
  35. def set_ctime_fallback(filepath, timestamp):
  36. pass
  37. return get_ctime_fallback, set_ctime_fallback
  38. get_ctime, set_ctime = load_ctime_functions()