def open(self, filesystem, flags): if filesystem.cache.isInCache(self) == False: if self.isOnPersistence(filesystem) == False: print self.getKeyName(), " Is Not on persistence" filesystem.cache.addToCache(self) else: self.readFrom(filesystem) #File in cache, open it up self.iolock.acquire() self.openref = self.openref+1 self.iolock.release() file = os.fdopen(filesystem.cache.openInCache(self,flags),tools.flag2mode(flags)) print "open: ",self.name,'flags',flags,'file',file return file
def open(self, filesystem, flags): if filesystem.cache.isInCache(self) == False: if self.isOnPersistence(filesystem) == False: print self.getKeyName(), " Is Not on persistence" filesystem.cache.addToCache(self) else: self.readFrom(filesystem) #File in cache, open it up self.iolock.acquire() self.openref = self.openref + 1 self.iolock.release() file = os.fdopen(filesystem.cache.openInCache(self, flags), tools.flag2mode(flags)) print "open: ", self.name, 'flags', flags, 'file', file return file
def shadowInCache(self, node): #Make a shadow copy that dissappears on close #Must be called with the nodes iolock held keyname = node.getKeyName() filename = self.dir + "/" + keyname origfile = os.fdopen(os.open(filename,os.O_RDWR),flag2mode(os.O_RDWR)) shadowfile = tempfile.TemporaryFile("w+b") shutil.copyfileobj(origfile,shadowfile) shadowfile.seek(0) origfile.seek(0) sum = hashlib.sha256() end = False while end == False: buf = origfile.read(4096) if (len(buf) == 0): end = True else: sum.update(buf) origfile.close() node.sha_sum = base64.urlsafe_b64encode(sum.digest()) return shadowfile
def addToCache(self, node, flags=(os.O_RDWR|os.O_CREAT)): keyname = node.getKeyName() fd = os.fdopen(os.open(self.dir + "/" + keyname, flags),flag2mode(flags)) fd.close() self.lru_list.append(keyname) return 0