github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/lib/python/fusepy/low-level/llfuse_example.py (about)

     1  #!/usr/bin/env python
     2  '''
     3  $Id: llfuse_example.py 46 2010-01-29 17:10:10Z nikratio $
     4  
     5  Copyright (c) 2010, Nikolaus Rath <Nikolaus@rath.org>
     6  All rights reserved.
     7  
     8  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     9  
    10      * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11      * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12      * Neither the name of the main author nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13  
    14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15  ''' 
    16  
    17  from __future__ import division, print_function, absolute_import
    18  
    19  import llfuse
    20  import errno
    21  import stat
    22  import sys
    23  
    24  class Operations(llfuse.Operations):
    25      '''A very simple example filesystem'''
    26      
    27      def __init__(self):      
    28          super(Operations, self).__init__()
    29          self.entries = [
    30                     # name, attr
    31                     (b'.', { 'st_ino': 1,
    32                             'st_mode': stat.S_IFDIR | 0755,
    33                             'st_nlink': 2}),
    34                    (b'..', { 'st_ino': 1,
    35                             'st_mode': stat.S_IFDIR | 0755,
    36                             'st_nlink': 2}),
    37                    (b'file1', { 'st_ino': 2, 'st_nlink': 1,
    38                                'st_mode': stat.S_IFREG | 0644 }),
    39                    (b'file2', { 'st_ino': 3, 'st_nlink': 1,
    40                                'st_mode': stat.S_IFREG | 0644 }) ]
    41          
    42          self.contents = { # Inode: Contents
    43                           2: b'Hello, World\n',
    44                           3: b'Some more file contents\n'
    45          }
    46          
    47          self.by_inode = dict()
    48          self.by_name = dict()
    49          
    50          for entry in self.entries:
    51              (name, attr) = entry        
    52              if attr['st_ino'] in self.contents: 
    53                  attr['st_size'] = len(self.contents[attr['st_ino']])
    54   
    55                  
    56              self.by_inode[attr['st_ino']] = attr
    57              self.by_name[name] = attr
    58  
    59                             
    60          
    61      def lookup(self, parent_inode, name):
    62          try:
    63              attr = self.by_name[name].copy()
    64          except KeyError:
    65              raise llfuse.FUSEError(errno.ENOENT)
    66          
    67          attr['attr_timeout'] = 1
    68          attr['entry_timeout'] = 1
    69          attr['generation'] = 1
    70          
    71          return attr
    72   
    73      
    74      def getattr(self, inode):
    75          attr = self.by_inode[inode].copy()
    76          attr['attr_timeout'] = 1
    77          return attr
    78      
    79      def readdir(self, fh, off):    
    80          for entry in self.entries:
    81              if off > 0:
    82                  off -= 1
    83                  continue
    84              
    85              yield entry
    86      
    87          
    88      def read(self, fh, off, size):
    89          return self.contents[fh][off:off+size]  
    90      
    91      def open(self, inode, flags):
    92          if inode in self.contents:
    93              return inode
    94          else:
    95              raise RuntimeError('Attempted to open() a directory')
    96      
    97      def opendir(self, inode):
    98          return inode
    99   
   100      def access(self, inode, mode, ctx, get_sup_gids):
   101          return True
   102  
   103  
   104  
   105  if __name__ == '__main__':
   106      
   107      if len(sys.argv) != 2:
   108          raise SystemExit('Usage: %s <mountpoint>' % sys.argv[0])
   109      
   110      mountpoint = sys.argv[1]
   111      operations = Operations()
   112      
   113      llfuse.init(operations, mountpoint, [ b"nonempty", b'fsname=llfuses_xmp' ])
   114      llfuse.main()
   115