Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import with_statement
  4.  
  5. import os
  6. import sys
  7. import errno
  8.  
  9. from fuse import FUSE, FuseOSError, Operations
  10.  
  11.  
  12. class Passthrough(Operations):
  13. def __init__(self, root):
  14. self.root = root
  15.  
  16. # Helpers
  17. # =======
  18.  
  19. def _full_path(self, partial):
  20. if partial.startswith("/"):
  21. partial = partial[1:]
  22. path = os.path.join(self.root, partial)
  23. return path
  24.  
  25. # Filesystem methods
  26. # ==================
  27.  
  28. def access(self, path, mode):
  29. full_path = self._full_path(path)
  30. if not os.access(full_path, mode):
  31. raise FuseOSError(errno.EACCES)
  32.  
  33. def chmod(self, path, mode):
  34. full_path = self._full_path(path)
  35. return os.chmod(full_path, mode)
  36.  
  37. def chown(self, path, uid, gid):
  38. full_path = self._full_path(path)
  39. return os.chown(full_path, uid, gid)
  40.  
  41. def getattr(self, path, fh=None):
  42. full_path = self._full_path(path)
  43. st = os.lstat(full_path)
  44. return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
  45. 'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
  46.  
  47. def readdir(self, path, fh):
  48. full_path = self._full_path(path)
  49.  
  50. dirents = ['.', '..']
  51. if os.path.isdir(full_path):
  52. dirents.extend(os.listdir(full_path))
  53. for r in dirents:
  54. yield r
  55.  
  56. def readlink(self, path):
  57. pathname = os.readlink(self._full_path(path))
  58. if pathname.startswith("/"):
  59. # Path name is absolute, sanitize it.
  60. return os.path.relpath(pathname, self.root)
  61. else:
  62. return pathname
  63.  
  64. def mknod(self, path, mode, dev):
  65. return os.mknod(self._full_path(path), mode, dev)
  66.  
  67. def rmdir(self, path):
  68. full_path = self._full_path(path)
  69. return os.rmdir(full_path)
  70.  
  71. def mkdir(self, path, mode):
  72. return os.mkdir(self._full_path(path), mode)
  73.  
  74. def statfs(self, path):
  75. full_path = self._full_path(path)
  76. stv = os.statvfs(full_path)
  77. return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
  78. 'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
  79. 'f_frsize', 'f_namemax'))
  80.  
  81. def unlink(self, path):
  82. return os.unlink(self._full_path(path))
  83.  
  84. def symlink(self, name, target):
  85. return os.symlink(name, self._full_path(target))
  86.  
  87. def rename(self, old, new):
  88. return os.rename(self._full_path(old), self._full_path(new))
  89.  
  90. def link(self, target, name):
  91. return os.link(self._full_path(target), self._full_path(name))
  92.  
  93. def utimens(self, path, times=None):
  94. return os.utime(self._full_path(path), times)
  95.  
  96. # File methods
  97. # ============
  98.  
  99. def open(self, path, flags):
  100. full_path = self._full_path(path)
  101. return os.open(full_path, flags)
  102.  
  103. def create(self, path, mode, fi=None):
  104. full_path = self._full_path(path)
  105. return os.open(full_path, os.O_WRONLY | os.O_CREAT, mode)
  106.  
  107. def read(self, path, length, offset, fh):
  108. os.lseek(fh, offset, os.SEEK_SET)
  109. return os.read(fh, length)
  110.  
  111. def write(self, path, buf, offset, fh):
  112. os.lseek(fh, offset, os.SEEK_SET)
  113. return os.write(fh, buf)
  114.  
  115. def truncate(self, path, length, fh=None):
  116. full_path = self._full_path(path)
  117. with open(full_path, 'r+') as f:
  118. f.truncate(length)
  119.  
  120. def flush(self, path, fh):
  121. return os.fsync(fh)
  122.  
  123. def release(self, path, fh):
  124. return os.close(fh)
  125.  
  126. def fsync(self, path, fdatasync, fh):
  127. return self.flush(path, fh)
  128.  
  129.  
  130. def main(mountpoint, root):
  131. FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True)
  132.  
  133. if __name__ == '__main__':
  134. main(sys.argv[2], sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement