Guest User

Untitled

a guest
Jun 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. #To run
  2. #python dl.py k /mnt/k
  3. #
  4.  
  5. #!/usr/bin/env python
  6.  
  7. from __future__ import with_statement
  8.  
  9. import os
  10. import sys
  11. import errno
  12.  
  13. from fuse import FUSE, FuseOSError, Operations
  14.  
  15.  
  16. class Passthrough(Operations):
  17. mask=""
  18. def __init__(self, root):
  19. self.root = root
  20.  
  21. # Helpers
  22. # =======
  23.  
  24. def _full_path(self, partial):
  25. self.mask=""
  26. if partial.startswith("/"):
  27. partial = partial[1:]
  28. if partial.find(':')>0:
  29. self.mask=partial.split(':')[1]
  30. partial=partial.split(':')[0]
  31. else:
  32. self.mask=None
  33. path = os.path.join(self.root, partial)
  34.  
  35.  
  36. return path
  37.  
  38. # Filesystem methods
  39. # ==================
  40.  
  41. def access(self, path, mode):
  42. full_path = self._full_path(path)
  43.  
  44. if not os.access(full_path, mode):
  45. raise FuseOSError(errno.EACCES)
  46.  
  47. def chmod(self, path, mode):
  48. full_path = self._full_path(path)
  49. return os.chmod(full_path, mode)
  50.  
  51. def chown(self, path, uid, gid):
  52. full_path = self._full_path(path)
  53. return os.chown(full_path, uid, gid)
  54.  
  55. def getattr(self, path, fh=None):
  56. full_path = self._full_path(path)
  57. st = os.lstat(full_path)
  58. return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
  59. 'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
  60.  
  61. def readdir(self, path, fh):
  62. full_path = self._full_path(path)
  63.  
  64. dirents = ['.', '..']
  65. if os.path.isdir(full_path):
  66. dirents.extend(os.listdir(full_path))
  67. for r in dirents:
  68. yield r
  69.  
  70. def readlink(self, path):
  71. pathname = os.readlink(self._full_path(path))
  72. if pathname.startswith("/"):
  73. # Path name is absolute, sanitize it.
  74. return os.path.relpath(pathname, self.root)
  75. else:
  76. return pathname
  77.  
  78. def mknod(self, path, mode, dev):
  79. return os.mknod(self._full_path(path), mode, dev)
  80.  
  81. def rmdir(self, path):
  82. full_path = self._full_path(path)
  83. return os.rmdir(full_path)
  84.  
  85. def mkdir(self, path, mode):
  86. return os.mkdir(self._full_path(path), mode)
  87.  
  88. def statfs(self, path):
  89. full_path = self._full_path(path)
  90. stv = os.statvfs(full_path)
  91. return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
  92. 'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
  93. 'f_frsize', 'f_namemax'))
  94.  
  95. def unlink(self, path):
  96. return os.unlink(self._full_path(path))
  97.  
  98. def symlink(self, name, target):
  99. return os.symlink(target, self._full_path(name))
  100.  
  101. def rename(self, old, new):
  102. return os.rename(self._full_path(old), self._full_path(new))
  103.  
  104. def link(self, target, name):
  105. return os.link(self._full_path(name), self._full_path(target))
  106.  
  107. def utimens(self, path, times=None):
  108. return os.utime(self._full_path(path), times)
  109.  
  110. # File methods
  111. # ============
  112.  
  113. def open(self, path, flags):
  114. #print("open")
  115. full_path = self._full_path(path)
  116. return os.open(full_path, flags)
  117.  
  118. def create(self, path, mode, fi=None):
  119. full_path = self._full_path(path)
  120. return os.open(full_path, os.O_WRONLY | os.O_CREAT, mode)
  121.  
  122. def read(self, path, length, offset, fh):
  123. #print("read")
  124. os.lseek(fh, offset, os.SEEK_SET)
  125. data=os.read(fh, length)
  126. #print(self.mask)
  127. m=self.mask
  128. if m is None:
  129. return data
  130. else:
  131. m=m.split(',')
  132. if len(m)>=1:
  133. lines=data.split('\n')
  134. data_=""
  135. for l in lines:
  136. d=l.split(',')
  137. #print ("line:"+l+str(len(m))+" "+str(len(d)))
  138. l_=['']*len(m)
  139. i=0
  140. for m_ in m:
  141. i_=int(m_)
  142. if i_< len(d):
  143. l_[i]=d[i_]
  144.  
  145. i=i+1
  146. d_=','.join(l_)
  147. data_=data_+d_+"\n"
  148.  
  149. return data_
  150.  
  151. #This is for only demo purposes, we need
  152. def write(self, path, buf, offset, fh):
  153. os.lseek(fh, offset, os.SEEK_SET)
  154. return os.write(fh, buf)
  155.  
  156. def truncate(self, path, length, fh=None):
  157. full_path = self._full_path(path)
  158. with open(full_path, 'r+') as f:
  159. f.truncate(length)
  160.  
  161. def flush(self, path, fh):
  162. return os.fsync(fh)
  163.  
  164. def release(self, path, fh):
  165. return os.close(fh)
  166.  
  167. def fsync(self, path, fdatasync, fh):
  168. return self.flush(path, fh)
  169.  
  170.  
  171. def main(mountpoint, root):
  172. FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True)
  173.  
  174. if __name__ == '__main__':
  175. main(sys.argv[2], sys.argv[1])
Add Comment
Please, Sign In to add comment