def doAccept(self, args, msg, addr, nolocal=0): # ACCEPT <farm name> <nfrep> <lpath> <addr> <port> <info> #print("doAccept(%s, %s, %s, %s)" % (args, msg, addr, nolocal)) if not cellmgr_global.VFSSrvIF.Reconciled: return None if nolocal and self.clientIsLocal(addr): return None args = msg.split(None, 6)[2:] if len(args) < 5: return None #print 'doAccept: %s' % args nfrep = int(args[0]) lp = args[1] sock_addr = (args[2], int(args[3])) info = VFSFileInfo(lp, args[4]) if not cellmgr_global.DataServer.canReceive(lp): #print 'DataServer can not receive' return None txn, attrc = cellmgr_global.CellStorage.receiveFile(lp, info) if txn == None: #print 'Storage can not receive' return None txn.NFRep = nfrep delay = txn.PSA.spaceUsage() * 1.0 + float(50 - attrc) / 100.0 if not self.clientIsLocal(addr) and not nolocal: delay = delay + 0.5 delay = max(0.0, delay) cellmgr_global.DataServer.recvSocket(txn, sock_addr, delay) return None
def getFileInfo(self, lpath): ipath = self.fullInfoPath(lpath) dpath = self.fullDataPath(lpath) try: st = os.stat(dpath) except: self.delFile(lpath) return None if stat.S_ISDIR(st[stat.ST_MODE]): try: os.rmdir(dpath) except: pass try: os.rmdir(ipath) except: pass return None try: f = open(ipath, 'r') except: #self.delFile(lpath) return None else: str = f.read() f.close() info = VFSFileInfo(lpath, str) return info
def readItem(self, lpath): t = None info = None #self.debug('readItem(%s)...' % (lpath,)) try: fpath = self.fullPath(lpath) st = os.stat(fpath) if stat.S_ISDIR(st[stat.ST_MODE]): t = 'd' except: # it's a file t = 'f' #self.debug('readItem: stat(%s): %s %s' % (fpath, sys.exc_type, sys.exc_value)) pass #self.debug('readItem: type for <%s> is %s' % (lpath, t)) if t == 'f': try: db = tdb.open(self.dirIndexPath(self.parentPath(lpath))) str = db[self.fileName(lpath)] except: #self.debug('readItem: db key for %s not found' % (lpath,)) return None, None info = VFSFileInfo(lpath, str) db.close() elif t == 'd': #print 'readItem: opening %s' % self.dirIndexPath(lpath) try: db = tdb.open(self.dirIndexPath(lpath)) str = db[self.__DirInfoKey] except: return None, None info = VFSDirInfo(lpath, str) db.close() return t, info
def readItem(self, lpath): typ, info = self.DB.getItem(lpath) if typ == 'd': return 'd', VFSDirInfo(lpath, info) if typ == 'f': return 'f', VFSFileInfo(lpath, info) else: return None
def glob(self, dirp, ptrn='*'): dirp = VFSCanonicPath(dirp) for name, typ, info in self.DB.listItems(dirp): if name[0] != '.' and fnmatch.fnmatch(name, ptrn): if typ == 'f': info = VFSFileInfo(dirp + '/' + name, info) else: info = VFSDirInfo(dirp + '/' + name, info) yield name, typ, info
def doMkFile(self, cmd, args, msg): # MF <lpath> <mult> <info> if not self.Username: return 'ERR Say HELLO first' if len(args) < 3: return 'ERR Protocol syntax error: <%s>' % msg lpath = VFSCanonicPath(args[0]) mult = int(args[1]) dirinfo = vfssrv_global.G_VFSDB.getDirInfo( vfssrv_global.G_VFSDB.parentPath(lpath)) if dirinfo == None: return 'ERR Parent directory does not exist' if not self.USrv.isAdmin(self.Username): if dirinfo.Username == self.Username: if dirinfo.Prot[1] != 'w': return 'PERM Permission denied' else: if dirinfo.Prot[3] != 'w': return 'PERM Permission denied' info = vfssrv_global.G_VFSDB.getFileInfo(lpath) if info != None: sts, reason = self._delfile(info) if not sts: return 'ERR Can not remove existing file: %s' % reason info = VFSFileInfo(lpath, msg.split(None, 3)[3]) info.Username = self.Username # do not trust them info.CTime = int(time.time()) if vfssrv_global.G_QuotaMgr.wouldExceedQuota(info.Username, info.sizeMB(), mult): return 'ERR Quota exceeded' vfssrv_global.G_QuotaMgr.makeReservation(info.Username, '%s %s' % (lpath, info.CTime), info.sizeMB(), mult) for k, v in list(dirinfo.Attrs.items()): if k[0] == '_': info.Attrs[k] = v sts, reason = vfssrv_global.G_VFSDB.mkfile(info) if sts: self.log('Created file %s %s' % (lpath, info.CTime)) return 'OK %s' % info.serialize() else: return 'ERR %s' % reason
def listDir(self, dir, ptrn='*'): # always returns list of relative paths dir = VFSCanonicPath(dir) lst1 = [] db = tdb.open(self.dirIndexPath(dir)) for fn in self.glob1(dir, ptrn): lpath = dir + '/' + fn typ = self.getType(lpath) info = None if typ == 'f': info = VFSFileInfo(lpath, db[fn]) elif typ == 'd': info = self.getDirInfo(lpath) # remove dir from fn lst1.append((fn, typ, info)) #print 'listDir(%s, %s): lst1(%d): %s' % (dir, ptrn, len(lst1), lst1[:5]) db.close() return lst1
def glob2(self, dirp, ptrn='*'): #print 'glob2: opening %s' % dirp db = tdb.open(self.dirIndexPath(dirp)) files = list( filter(lambda fn, pt=ptrn, s=self: fn != s.__DirInfoKey and fnmatch .fnmatch(fn, pt), list(db.keys()))) lst = [] for fn in files: info = VFSFileInfo(dirp + '/' + fn, db[fn]) lst.append((fn, 'f', info)) db.close() fullp = self.fullPath(dirp) for fn in glob.glob1(fullp, '*'): if not fn or fn[0] == '.': continue st = os.stat(fullp + '/' + fn) if stat.S_ISDIR(st[stat.ST_MODE]): lp = dirp + '/' + fn info = self.getDirInfo(lp) if info != None: lst.append((fn, 'd', info)) return lst