def storeLink(self, fileName): '''Pricate specialization function Links are stored much like directories. Their type is link ''' assert os.path.islink(fileName) md = self.getMetadata(fileName) md['type'] = u'link' md['target'] = os.readlink(fileName) digest = sha1(fileName) metadataFileName = os.path.join(self.storagePath, 'tree', digest[:2], u'%s.json' % digest) metadata.appendMeta(metadataFileName, md)
def storeDirectory(self, fileName): '''Private specialization function Directories are stored by using the path to the file as input for the hash function. All the hashed files are placed in a special directory called "tree" because they only describe parts of the file system tree. ''' assert os.path.isdir(fileName) md = self.getMetadata(fileName) md['type'] = u'directory' digest = sha1(fileName) metadataFileName = os.path.join(self.storagePath, 'tree', digest[:2], u'%s.json' % digest) metadata.appendMeta(metadataFileName, md)
def storeFile(self, fileName): '''Private specialization function''' i = file(fileName) chunkSums = [] completeDigest = hashlib.sha1() while True: read = i.read(self.CHUNK_SIZE) h = hashlib.sha1() h.update(read) completeDigest.update(read) digest = unicode(h.hexdigest()) chunkSums.append(digest) #Store chunk chunkFileName = os.path.join(self.storagePath, 'data', digest[:2], digest) #Only write if not existing if not os.path.exists(chunkFileName): outputDir = os.path.dirname(chunkFileName) if not os.path.exists(outputDir): os.makedirs(outputDir) file(chunkFileName, 'w').write(read) del outputDir else: #If the file does exist, do some sanity checks if not os.path.getsize(chunkFileName) == len(read): raise Exception('Chunk with name %s already exists but it does not have the same size as the chunk I just read (with the same name) %i on disk, %i read' % (chunkFileName, os.path.getsize(chunkFileName), len(read))) #detect EOF if len(read) < self.CHUNK_SIZE: break del read md = self.getMetadata(fileName) md['type'] = u'file' md['chunks'] = chunkSums md['digest'] = unicode(completeDigest.hexdigest()) metadataFileName = os.path.join(self.storagePath, 'meta', digest[:2], u'%s.json' % digest) metadata.appendMeta(metadataFileName, md)