Advertisement
Guest User

Ping Fuse

a guest
Apr 1st, 2012
1,970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.64 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os, sys, stat, errno, posix, logging, time, fuse
  4. import ping, ping_reporter, ping_filesystem
  5. from time import time
  6.  
  7. fuse.fuse_python_api = (0,2)
  8.  
  9. log = ping_reporter.setup_log('PingFuse')
  10.  
  11. class PingFuse(fuse.Fuse):
  12.     def __init__(self, server):
  13.         self.FS = ping_filesystem.PingFS(server)
  14.         #ping.drop_privileges()
  15.         fuse.Fuse.__init__(self)
  16.         log.notice('ping::fuse: initialized (%d-byte blocks)'%self.FS.disk.block_size())
  17.  
  18.     def fsinit(self):
  19.         self.reporter = ping_reporter.PingReporter(log,'',90)
  20.         self.reporter.start()
  21.  
  22.     def getattr(self, path):
  23.         """
  24.         - st_mode (protection bits)
  25.         - st_ino (inode number)
  26.         - st_dev (device)
  27.         - st_nlink (number of hard links)
  28.         - st_uid (user ID of owner)
  29.         - st_gid (group ID of owner)
  30.         - st_size (size of file, in bytes)
  31.         - st_atime (time of most recent access)
  32.         - st_mtime (time of most recent content modification)
  33.         - st_ctime (platform dependent; time of most recent metadata change on Unix,
  34.                     or the time of creation on Windows).
  35.         """
  36.  
  37.         log.info('getattr: %s' % path)
  38.  
  39.         pFile = self.FS.get(path)
  40.         if not pFile: return -errno.ENOENT
  41.  
  42.         st = fuse.Stat()
  43.         st.st_mode  = pFile.type | pFile.mode
  44.         st.st_ino   = pFile.inode
  45.         st.st_nlink = pFile.links()
  46.         st.st_uid   = pFile.uid
  47.         st.st_gid   = pFile.gid
  48.         st.st_size  = pFile.size()
  49.         #st.st_atime = time()
  50.         #st.st_mtime = time()
  51.         #st.st_ctime = time()
  52.         #st.st_dev = 2050L
  53.         return st
  54.  
  55.     def readdir(self, path, offset):
  56.         log.info('readdir: %s'%path)
  57.         pDir = self.FS.get(path)
  58.         if not pDir: return -errno.ENOENT
  59.         if pDir.type != stat.S_IFDIR:
  60.             return [fuse.Direntry(pDir.name)]
  61.  
  62.         files = [fuse.Direntry('.'),fuse.Direntry('..')]
  63.         for e in pDir.entries:
  64.             files.append(fuse.Direntry(e.name))
  65.         return files
  66.  
  67.     def mkdir(self, path, mode):
  68.         log.info('mkdir: %s mode=%04o'%(path,mode))
  69.         if path == '/' or path == '': return -errno.EACCESS
  70.         if self.FS.get(path): return -errno.EEXIST
  71.         rPath,rName = path.rsplit('/',1)
  72.         pDir = self.FS.get(rPath)
  73.         if not pDir: return -errno.ENOENT
  74.  
  75.         nDir = ping_filesystem.PingDirectory(rName)
  76.         self.FS.add(nDir) # acquire inode
  77.         pDir.add_node(nDir) # add dirent
  78.         self.FS.update(pDir) # save
  79.         return 0
  80.  
  81.     def open(self, path, flags):
  82.         log.info('open: %s flags=%x'%(path,flags))
  83.         pFile = self.FS.get(path)
  84.         if not pFile: return -errno.ENOENT
  85.         return 0
  86.  
  87.     def read(self, path, length, offset):
  88.         log.info('read: %s region=%d,%d'%(path,offset,length))
  89.         pFile = self.FS.get(path)
  90.         if not pFile: return -errno.ENOENT
  91.         if offset > len(pFile.data): return -errno.EINVAL
  92.         if pFile.type == stat.S_IFDIR: return -errno.EISDIR
  93.         return pFile.data[offset:offset+length]
  94.  
  95.     def chmod(self, path, mode):
  96.         log.info('chmod: %s mode=%04o'%(path,mode))
  97.         pFile = self.FS.get(path)
  98.         if not pFile: return -errno.ENOENT
  99.         pFile.mode = mode
  100.         self.FS.update(pFile)
  101.         return 0
  102.  
  103.     def chown(self, path, uid, gid):
  104.         log.info('chown: %s uid=%d gid=%d)'%(path,uid,gid))
  105.         pFile = self.FS.get(path)
  106.         if not pFile: return -errno.ENOENT
  107.         pFile.uid = uid
  108.         pFile.gid = gid
  109.         self.FS.update(pFile)
  110.         return 0
  111.  
  112.     def rmdir(self, path):
  113.         log.info('rmdir: %s'%path)
  114.         pFile = self.FS.get(path)
  115.         if not pFile: return -errno.ENOENT
  116.         if pFile.type != stat.S_IFDIR: return -errno.ENOTDIR
  117.         if self.FS.unlink(path,pFile): return 0
  118.         return -errno.EINVAL
  119.  
  120.     def unlink(self, path):
  121.         log.info('unlink: %s'%path)
  122.         pFile = self.FS.get(path)
  123.         if not pFile: return -errno.ENOENT
  124.         if pFile.type != stat.S_IFREG: return -errno.ENOTDIR
  125.         if self.FS.unlink(path,pFile): return 0
  126.         return -errno.EINVAL
  127.  
  128.     def write(self, path, buf, offset):
  129.         log.info('write: %s region=%d,%d'%(path,offset,offset+len(buf)))
  130.         pFile = self.FS.get(path)
  131.         if not pFile: return -errno.ENOENT
  132.         pDir = self.FS.get_parent(path,pFile)
  133.         if not pDir: raise Exception('write failed to find parent after filding child!')
  134.         if offset:
  135.             fill = '\0' * min(0,offset - len(pFile.data)) # technically don't need to bound negatives
  136.             offset = pFile.data[:offset] + fill
  137.         else: offset = ''
  138.  
  139.         pFile.data = offset + buf
  140.         if not self.FS.update(pFile,pDir):
  141.             return -errno.EINVAL
  142.         return len(buf)
  143.  
  144.     def truncate(self, path, size):
  145.         log.info('truncate: %s size=%d'%(path, size))
  146.         pFile = self.FS.get(path)
  147.         if not pFile: return -errno.ENOENT
  148.         if size > len(pFile.data): return -errno.EINVAL
  149.         if pFile.type != stat.S_IFREG: return -errno.EINVAL
  150.         pFile.data = pFile.data[:size]
  151.         self.FS.update(pFile)
  152.         return 0
  153.  
  154.     def mknod(self, path, mode, dev):
  155.         log.info('mknod: %s mode=%04o dev=%d)'%(path,mode,dev))
  156.         if not mode & stat.S_IFREG: return -errno.ENOSYS
  157.         pFile = self.FS.get(path)
  158.         if pFile: return -errno.EEXIST
  159.         pFile = self.FS.create(path)
  160.         if not pFile: return -errno.EINVAL
  161.         pFile.mode = mode & 0777
  162.         self.FS.update(pFile)
  163.         return 0
  164.  
  165.     def rename(self, old_path, new_path):
  166.         log.info('rename: %s -> %s'%(old_path,new_path))
  167.  
  168.         (oDir,oFile) = self.FS.get_both(old_path)
  169.         (nDir,nFile) = self.FS.get_both(new_path)
  170.         new_name = new_path.rsplit('/',1)[1]
  171.  
  172.         if not oFile: return -errno.ENOENT
  173.         if not oDir or not nDir: return -errno.ENOENT
  174.         if nFile: return -errno.EEXIST
  175.  
  176.         oDir.del_node(oFile.name,oFile)
  177.         oFile.name = new_name
  178.         nDir.add_node(oFile)
  179.  
  180.         # better to be in both than neither
  181.         self.FS.update(nDir)
  182.         self.FS.update(oFile)
  183.         self.FS.update(oDir)
  184.         return 0
  185.  
  186.     def link(self, targetPath, linkPath):
  187.         log.info('link: %s <- %s)'%(targetPath, linkPath))
  188.         return -errno.ENOSYS
  189.  
  190.     def readlink(self, path):
  191.         log.info('readlink: %s'%path)
  192.         return -errno.ENOSYS
  193.  
  194.     def symlink(self, targetPath, linkPath):
  195.         log.info('symlink: %s <- %s'%(targetPath, linkPath))
  196.         return -errno.ENOSYS
  197.  
  198.     def release(self, path, flags):
  199.         log.info('release: %s flags=%x'%(path,flags))
  200.         return -errno.ENOSYS
  201.  
  202.     def statf(self):
  203.         log.info('statfs')
  204.         return -errno.ENOSYS
  205.  
  206.     def utime(self, path, times):
  207.         log.info('utime: %s times=%s'%(path,times))
  208.         return -errno.ENOSYS
  209.  
  210.     def fsync(self, path, isFsyncFile):
  211.         log.info('fsync: %s fsyncFile? %s'%(path,isFsyncFile))
  212.         return -errno.ENOSYS
  213.  
  214.  
  215.  
  216. if __name__ == "__main__":
  217.     import ping_disk
  218.     ping_reporter.enableAllLogs(logging.INFO,logging.TRACE)
  219.     #ping_reporter.start_log(log,logging.NOTICE)
  220.     #ping_reporter.start_log(ping_filesystem.log,logging.DEBUG)
  221.     #ping_reporter.start_log(ping_disk.log,logging.DEBUG)
  222.     server = ping.select_server(log)
  223.     if len(sys.argv) < 2:
  224.         print 'usage: %s <mountpoint>' % sys.argv[0]
  225.         sys.exit(1)
  226.     sys.argv.append('-f')
  227.     fs = PingFuse(server)
  228.     fs.parse(errex=1)
  229.  
  230.     fs.flags = 0
  231.     #fs.multithreaded = 1
  232.     ping_filesystem.init_fs(fs.FS)
  233.     #ping_filesystem.test_fs(fs.FS)
  234.  
  235.  
  236.     if 0:
  237.         log.info('file system testing begun')
  238.         fs.mknod('/test_file2',stat.S_IFREG | 0777,0)
  239.         fs.mknod('/test_file1',stat.S_IFREG | 0777,0)
  240.         wData = 'A'*fs.FS.disk.region_size()*fs.FS.disk.block_size()
  241.  
  242.         fs.write('/test_file1',wData,0)
  243.         log.info('completed writing testfile1: %d bytes'%len(wData))
  244.         rData = fs.read('/test_file1',len(wData),0)
  245.         if rData == wData:             log.info( 'read /testfile1 verified successfully')
  246.         elif len(wData) != len(rData): log.error('read failed (%d of %d bytes)'%(len(rData),len(wData)))
  247.         else:                          log.error('read failed (bytes corrupted)')
  248.  
  249.         fs.write('/test_file1',wData,len(wData))
  250.         wData = wData + wData
  251.         log.info('completed writing testfile1: %d bytes'%len(wData))
  252.         rData = fs.read('/test_file1',len(wData),0)
  253.         if rData == wData:             log.info( 'read /testfile1 verified successfully')
  254.         elif len(wData) != len(rData): log.error('read failed (%d of %d bytes)'%(len(rData),len(wData)))
  255.         else:                          log.error('read failed (bytes corrupted)')
  256.  
  257.         bData = 'B'*fs.FS.disk.region_size()*fs.FS.disk.block_size()*3
  258.         fs.write('/test_file2',bData,0)
  259.         log.info('completed writing testfile2: %d bytes'%len(bData))
  260.         rData = fs.read('/test_file2',len(bData),0)
  261.         if rData == bData:             log.info( 'read /testfile2 verified successfully')
  262.         elif len(bData) != len(rData): log.error('read failed (%d of %d bytes)'%(len(rData),len(bData)))
  263.         else:                          log.error('read failed (bytes corrupted)')
  264.  
  265.         rData = fs.read('/test_file1',len(wData),0)
  266.         if rData == wData:             log.info( 'read /testfile1 verified successfully')
  267.         elif len(wData) != len(rData): log.error('read failed (%d of %d bytes)'%(len(rData),len(wData)))
  268.         else:                          log.error('read failed (bytes corrupted)')
  269.  
  270.     log.info('file system testing complete')
  271.     log.info('file system up and running')
  272.     try:
  273.         fs.main()
  274.     except KeyboardInterrupt:
  275.         log.info('fs stopping')
  276.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement