platform_base.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. API_VERSION = 3
  3. fdatasync = getattr(os, 'fdatasync', os.fsync)
  4. def acl_get(path, item, st, numeric_owner=False):
  5. """
  6. Saves ACL Entries
  7. If `numeric_owner` is True the user/group field is not preserved only uid/gid
  8. """
  9. def acl_set(path, item, numeric_owner=False):
  10. """
  11. Restore ACL Entries
  12. If `numeric_owner` is True the stored uid/gid is used instead
  13. of the user/group names
  14. """
  15. try:
  16. from os import lchflags
  17. def set_flags(path, bsd_flags, fd=None):
  18. lchflags(path, bsd_flags)
  19. except ImportError:
  20. def set_flags(path, bsd_flags, fd=None):
  21. pass
  22. def get_flags(path, st):
  23. """Return BSD-style file flags for path or stat without following symlinks."""
  24. return getattr(st, 'st_flags', 0)
  25. def sync_dir(path):
  26. fd = os.open(path, os.O_RDONLY)
  27. try:
  28. os.fsync(fd)
  29. finally:
  30. os.close(fd)
  31. class SyncFile:
  32. """
  33. A file class that is supposed to enable write ordering (one way or another) and data durability after close().
  34. The degree to which either is possible varies with operating system, file system and hardware.
  35. This fallback implements a naive and slow way of doing this. On some operating systems it can't actually
  36. guarantee any of the above, since fsync() doesn't guarantee it. Furthermore it may not be possible at all
  37. to satisfy the above guarantees on some hardware or operating systems. In these cases we hope that the thorough
  38. checksumming implemented catches any corrupted data due to misordered, delayed or partial writes.
  39. Note that POSIX doesn't specify *anything* about power failures (or similar failures). A system that
  40. routinely loses files or corrupts file on power loss is POSIX compliant.
  41. TODO: Use F_FULLSYNC on OSX.
  42. TODO: A Windows implementation should use CreateFile with FILE_FLAG_WRITE_THROUGH.
  43. """
  44. def __init__(self, path):
  45. self.fd = open(path, 'wb')
  46. self.fileno = self.fd.fileno()
  47. def __enter__(self):
  48. return self
  49. def __exit__(self, exc_type, exc_val, exc_tb):
  50. self.close()
  51. def write(self, data):
  52. self.fd.write(data)
  53. def sync(self):
  54. """
  55. Synchronize file contents. Everything written prior to sync() must become durable before anything written
  56. after sync().
  57. """
  58. self.fd.flush()
  59. fdatasync(self.fileno)
  60. if hasattr(os, 'posix_fadvise'):
  61. os.posix_fadvise(self.fileno, 0, 0, os.POSIX_FADV_DONTNEED)
  62. def close(self):
  63. """sync() and close."""
  64. self.sync()
  65. self.fd.close()
  66. sync_dir(os.path.dirname(self.fd.name))
  67. def swidth(s):
  68. """terminal output width of string <s>
  69. For western scripts, this is just len(s), but for cjk glyphs, 2 cells are used.
  70. """
  71. return len(s)