def unlink(self, path): log.debug("path %s" % path) # os.unlink("." + path) hashpath=HashPath("Salt") dir, full = hashpath.path(path) os.unlink(full) self.unlink_empty_dirs(dir)
def truncate(self, path, len): log.debug("path %s len %d" % (path, len )) hashpath=HashPath("Salt") dir, full = hashpath.path(path) # f = open(full, "a") f = open("." + path, "a") f.truncate(len) f.close()
def getattr(self, path): log.debug("path %s" % path) try: if path=='/': lstat = os.lstat('.') else: hashpath=HashPath("Salt") dir, full = hashpath.path(path) lstat = os.lstat(full) except Exception as ex: log.debug("not found %s, exception=%s" % (path,ex) ) raise lstat = os.lstat("." + path) log.debug("lstat %s" % lstat) return lstat
class HashTestSha1(unittest2.TestCase): def __init__(self,test_name): super(HashTestSha1, self).__init__(test_name) def setUp(self): self.hashpath = HashPath("Salt",separator="\n") self.hash_of_Test = hashlib.sha1('Salt\nTest\nmain\n0').hexdigest() def test_hashpath_split(self): self.assertEqual(self.hash_of_Test,''.join(HashPath.__split__(self.hash_of_Test, self.hashpath.split_num))) def test_hashpath_and_count_parts(self): path= self.hashpath.path('Test',"main") self.assertEqual(len(path), len(self.hash_of_Test)+9) self.assertEqual(self.hash_of_Test, string.replace(path, '/', '', 9))
class HashTestMd5(unittest2.TestCase): def __init__(self,test_name): super(HashTestMd5, self).__init__(test_name) def setUp(self): self.hashpath = HashPath("Salt",separator="#", algorithm=hashlib.md5) self.hash_of_Test = hashlib.md5('Salt#Test#main#0').hexdigest() def test_hashpath_split(self): self.assertEqual(self.hash_of_Test,''.join(HashPath.__split__(self.hash_of_Test, self.hashpath.split_num))) def test_hashpath_and_count_parts(self): path= self.hashpath.path('Test',"main") self.assertEqual(len(path), len(self.hash_of_Test)+7) self.assertEqual(self.hash_of_Test, string.replace(path, '/', '', 7))
def test_write_to_mount_and_read_from_root(self): basename="testfile" mount_filename="%s/%s" % (self.mount.mountdir,basename) mount_file=file(mount_filename,"w+") value=random.random() mount_file.write("%f\n" % value) mount_file.close() self.assertTrue(os.path.exists(mount_filename)) hashpath=HashPath("Salt") dir, full = hashpath.path("/"+basename,block=0) root_filename="%s/%s" % (self.mount.rootdir,full) self.assertTrue(cmp(root_filename,mount_filename)) os.system("find test/rootdir test/mountdir -type f -ls") os.system("ls -lFt test") os.remove(mount_filename) self.assertFalse(os.path.exists(root_filename))
class XmpFile(object): def __init__(self, path, flags, *mode): log.debug("file path %s flags %s mode %s " % ( path, flags, mode)) # self.file = os.fdopen(os.open("." + path, flags, *mode), # flag2mode(flags)) # self.fd = self.file.fileno() # log.debug("file %s fd %d" % (self.file, self.fd)) self.hashpath = HashPath("Salt") dir, full = self.hashpath.path(path) log.debug("dir=%s full= %s" %(dir,full)) self.makedirs(dir) self.file = os.fdopen(os.open(full, flags, *mode), flag2mode(flags)) self.fd = self.file.fileno() self.path = path self.flags = flags self.direct_io = 0 self.keep_cache = 0 def read(self, length, offset): log.debug("file=%s length %d offset %d mod %d" % (self.path, length, offset, offset % self.hashpath.block_size)) dir, full = self.hashpath.path(self.path, block=offset) log.debug("hashpath %s/%s" % (dir,full)) # log.debug("hashpath+%s/%s" % self.hashpath.path(self.path, block=offset + self.hashpath.block_size)) hash_file=open(full) return hash_file.read(length) self.file.seek(offset) return self.file.read(length) def makedirs(self,dir): log.debug("dir=%s" % dir ) try: os.makedirs(dir) except OSError as exception: log.debug("%s" % exception) if exception.errno != EEXIST or not os.path.isdir(dir): raise def write(self, buf, offset): log.debug("file %s length of buf %d offset %d mod %d" % (self.path, len(buf), offset, offset % self.hashpath.block_size)) dir, full = self.hashpath.path(self.path, block=offset) log.debug("hashpath %s %s" % (dir,full)) try: self.makedirs(dir) log.debug("dir created") hash_file=open(full,"w") hash_file.write(buf) hash_file.close() except IOError as ex: log.error("could not write data" % ex.message) # self.file.seek(offset) # self.file.write(buf) return len(buf) def release(self, flags): log.debug("file %s flags %d\n" % (self.path, flags)) self.file.close() def _fflush(self): log.debug("file %s" % self.path) if 'w' in self.file.mode or 'a' in self.file.mode: self.file.flush() def fsync(self, isfsyncfile): log.debug("file %s isfsyncfile %d" % (self.path, isfsyncfile)) self._fflush() if isfsyncfile and hasattr(os, 'fdatasync'): os.fdatasync(self.fd) else: os.fsync(self.fd) def flush(self): log.debug("file %s" % self.path) self._fflush() # cf. xmp_flush() in fusexmp_fh.c os.close(os.dup(self.fd)) def fgetattr(self): log.debug("file %s" % self.path) return os.fstat(self.fd) def ftruncate(self, len): log.debug("file %s len %d" % ( self.path, len)) self.file.truncate(len) def lock(self, cmd, owner, **kw): # The code here is much rather just a demonstration of the locking # API than something which actually was seen to be useful. # Advisory file locking is pretty messy in Unix, and the Python # interface to this doesn't make it better. # We can't do fcntl(2)/F_GETLK from Python in a platfrom independent # way. The following implementation *might* work under Linux. # # if cmd == fcntl.F_GETLK: # import struct # # lockdata = struct.pack('hhQQi', kw['l_type'], os.SEEK_SET, # kw['l_start'], kw['l_len'], kw['l_pid']) # ld2 = fcntl.fcntl(self.fd, fcntl.F_GETLK, lockdata) # flockfields = ('l_type', 'l_whence', 'l_start', 'l_len', 'l_pid') # uld2 = struct.unpack('hhQQi', ld2) # res = {} # for i in xrange(len(uld2)): # res[flockfields[i]] = uld2[i] # # return fuse.Flock(**res) # Convert fcntl-ish lock parameters to Python's weird # lockf(3)/flock(2) medley locking API... op = {fcntl.F_UNLCK: fcntl.LOCK_UN, fcntl.F_RDLCK: fcntl.LOCK_SH, fcntl.F_WRLCK: fcntl.LOCK_EX}[kw['l_type']] if cmd == fcntl.F_GETLK: return -EOPNOTSUPP elif cmd == fcntl.F_SETLK: if op != fcntl.LOCK_UN: op |= fcntl.LOCK_NB elif cmd == fcntl.F_SETLKW: pass else: return -EINVAL fcntl.lockf(self.fd, op, kw['l_start'], kw['l_len'])