def write(self, path, data, offset, fh):
     context = fuse_get_context()
     if fh in self.files:
         active_file = self.files[fh]
         if active_file.changed == False:
             active_file.changed = True 
             active_file.create_cpath()
             if not os.path.exists(os.path.dirname(active_file.cpath)):
                 os.makedirs(os.path.dirname(active_file.cpath))
             try:
                 shutil.copy2(path, active_file.cpath)
                 logger.debug("write copy on change from " + path + " to "
                              + active_file.to_string())
                 
             except (IOError, os.error) as why:
                 logger.error("write exception " + str(why))
             if active_file.mode !=0:
                 active_file.cfh = os.open(active_file.cpath,
                                           active_file.flags, 
                                           active_file.mode)
             else:
                 active_file.cfh = os.open(active_file.cpath, 
                                           active_file.flags)
         with self.rwlock:
             logger.debug("write to cpath " + active_file.to_string())
             os.lseek(active_file.cfh, offset, 0)
             return os.write(active_file.cfh, data)
     else:
         logger.error("write error EBADF fh:" + fh)
         return -EBADF 
    def write(self, path, data, offset, fh):
        context = fuse_get_context()
        if fh in self.files:
            active_file = self.files[fh]
            if active_file.changed == False:
                active_file.changed = True
                active_file.create_cpath()
                if not os.path.exists(os.path.dirname(active_file.cpath)):
                    os.makedirs(os.path.dirname(active_file.cpath))
                try:
                    shutil.copy2(path, active_file.cpath)
                    logger.debug("write copy on change from " + path + " to " +
                                 active_file.to_string())

                except (IOError, os.error) as why:
                    logger.error("write exception " + str(why))
                if active_file.mode != 0:
                    active_file.cfh = os.open(active_file.cpath,
                                              active_file.flags,
                                              active_file.mode)
                else:
                    active_file.cfh = os.open(active_file.cpath,
                                              active_file.flags)
            with self.rwlock:
                logger.debug("write to cpath " + active_file.to_string())
                os.lseek(active_file.cfh, offset, 0)
                return os.write(active_file.cfh, data)
        else:
            logger.error("write error EBADF fh:" + fh)
            return -EBADF
 def open(self, path, flags):
     print "OPEN is called with path: " + path
     context = fuse_get_context()
     fh = os.open(path, flags)
     if not fh in self.files:
         print "OPEN is called fh is not in dictionary"
         active_file = ActiveFile(path, context, fh, False)
         active_file.flags = flags
         logger.debug("open new file " + active_file.to_string())
         self.files.update({fh: active_file})
     return fh
 def open(self, path, flags):
     print "OPEN is called with path: " + path
     context = fuse_get_context()
     fh = os.open(path, flags)
     if not fh in self.files:
         print "OPEN is called fh is not in dictionary"
         active_file = ActiveFile(path, context, fh, False)
         active_file.flags = flags
         logger.debug("open new file " + active_file.to_string())
         self.files.update({fh: active_file})
     return fh
 def create(self, path, mode):
     print "CREATE is called with path: " + path
     context = fuse_get_context()
     try:
         fh = os.open(path, os.O_WRONLY | os.O_CREAT, mode)
     except:
         return -EBADF            
     if not fh in self.files:
         active_file = ActiveFile(path, context, fh, True)
         active_file.mode = mode
         active_file.flags = os.O_WRONLY | os.O_CREAT
         self.files.update({fh: active_file})
     logger.debug("create "+ self.files[fh].to_string())
     return fh
 def create(self, path, mode):
     print "CREATE is called with path: " + path
     context = fuse_get_context()
     try:
         fh = os.open(path, os.O_WRONLY | os.O_CREAT, mode)
     except:
         return -EBADF
     if not fh in self.files:
         active_file = ActiveFile(path, context, fh, True)
         active_file.mode = mode
         active_file.flags = os.O_WRONLY | os.O_CREAT
         self.files.update({fh: active_file})
     logger.debug("create " + self.files[fh].to_string())
     return fh
 def rename(self, old, new):
     if not new.startswith(self.root):
         new = self.root + new
     logger.debug("rename: " + old + " " + new)  
     uid, guid, pid  = fuse_get_context()
     return os.rename(old, new)
 def readdir(self, path, fh):
     uid, guid, pid  = fuse_get_context()
     return ['.', '..'] + os.listdir(path)
 def fsync(self, path, datasync, fh):
     logger.debug("fsync "+ self.files[fh].to_string())
     print "fsync: "+ self.files[fh].to_string()
     context = fuse_get_context()
     return self.handle_flush_sync(fh, context)
 def flush(self, path, fh):
     logger.debug("flush "+ self.files[fh].to_string())
     print "FLUSH is called. Path: " + path
     context = fuse_get_context()
     return self.handle_flush_sync(fh, context)
 def rename(self, old, new):
     if not new.startswith(self.root):
         new = self.root + new
     logger.debug("rename: " + old + " " + new)
     uid, guid, pid = fuse_get_context()
     return os.rename(old, new)
 def readdir(self, path, fh):
     uid, guid, pid = fuse_get_context()
     return ['.', '..'] + os.listdir(path)
 def fsync(self, path, datasync, fh):
     logger.debug("fsync " + self.files[fh].to_string())
     print "fsync: " + self.files[fh].to_string()
     context = fuse_get_context()
     return self.handle_flush_sync(fh, context)
 def flush(self, path, fh):
     logger.debug("flush " + self.files[fh].to_string())
     print "FLUSH is called. Path: " + path
     context = fuse_get_context()
     return self.handle_flush_sync(fh, context)