def compare(self, filename, fix_local_path, fix_length, sha1, fix_time): """Check a fixity entry against the current state of the data in the filesystem""" try: local_path = fix_local_path if not os.path.exists(fix_local_path): if self.resolvers is not None: # use resolvers['binpid2path'] to try to find file (bin_lid, format) = re.split(r'\.',filename) hit = resolvers['binpid2path'].resolve(pid=bin_lid,format=format) if hit is not None: local_path = hit.value print 'WARNING on %s: file has been moved to %s' % (fix_local_path, local_path) else: raise FixityError('file is missing') file_stat = os.stat(local_path) file_length = file_stat.st_size file_time = file_stat.st_mtime time_delta = file_time - fix_time fix_date = iso8601(time.gmtime(fix_time)) file_date = iso8601(time.gmtime(file_time)) if fix_length != file_length: raise FixityError('file was %d bytes at fix time of %s, but is %d bytes as of %s' % (fix_length, fix_date, file_length, file_date)) if local_path == fix_local_path and time_delta > self.time_threshold: checksum = sha1_file(local_path) if checksum != sha1: raise FixityError('file modified at %s, after fix date of %s' % (file_date, fix_date)) else: raise FixityError('file touched at %s, after fix date of %s, but checksums match' % (file_date, fix_date)) except KeyboardInterrupt: raise except FixityError as e: print 'FAILED on %s: %s' % (local_path,e)
def compute_fixity(local_path): """Compute fixity for a given file""" filename = os.path.basename(local_path) length = os.stat(local_path).st_size fix_time = int(time.time()) sha1 = sha1_file(local_path) return filename, length, sha1, fix_time
def compute_fixity(self,fast=False): """compute fixity, overwriting existing fixity""" """requires that local_path is correct""" self.fix_time = datetime.now() self.length = os.stat(self.local_path).st_size self.filename = os.path.basename(self.local_path) # skip checksumming, because it's slow if fast: self.sha1 = CHECKSUM_PLACEHOLDER else: self.sha1 = sha1_file(self.local_path)
def fix(self): """compute fixity based on pathname""" now = secs2utcdatetime() stat = os.stat(self.pathname) self.length = stat.st_size self.fix_time = now self.create_time = secs2utcdatetime(stat.st_ctime) self.mod_time = secs2utcdatetime(stat.st_mtime) if self.checksum_type=='md5': self.checksum = md5_file(self.pathname) elif self.checksum_type=='sha1': self.checksum = sha1_file(self.pathname)
def check_fixity(self,fast=False): status = { FILE_EXISTS: False, FILE_LENGTH: False, FILE_CHECKSUM: False } if os.path.exists(self.local_path): status[FILE_EXISTS] = True if fast: sha1 = CHECKSUM_PLACEHOLDER else: sha1 = sha1_file(self.local_path) status[FILE_CHECKSUM] = self.sha1==sha1 status[FILE_LENGTH] = self.length==os.stat(self.local_path).st_size return status