def testCopyFileSparse(self): tempdir = tempfile.mkdtemp() try: src_path = os.path.join(tempdir, "src") dest_path = os.path.join(tempdir, "dest") content = b"foo" # Use seek to create some sparse blocks. Don't make these # files too big, in case the filesystem doesn't support # sparse files. with open(src_path, "wb") as f: f.write(content) f.seek(2 ** 17, 1) f.write(content) f.seek(2 ** 18, 1) f.write(content) # Test that sparse blocks are handled correctly at # the end of the file (involves seek and truncate). f.seek(2 ** 17, 1) copyfile(src_path, dest_path) self.assertEqual(perform_md5(src_path), perform_md5(dest_path)) # This last part of the test is expected to fail when sparse # copy is not implemented, so set the todo flag in order # to tolerate failures. self.todo = True # If sparse blocks were preserved, then both files should # consume the same number of blocks. self.assertEqual(os.stat(src_path).st_blocks, os.stat(dest_path).st_blocks) finally: shutil.rmtree(tempdir)
def _verify_obj(self, files, cfile, real_cfile, errs): """Verify the MD5 sum and/or mtime and return any errors.""" obj_errs = errs[:] if self.check_sums: md5sum = files[cfile][2] try: cur_checksum = checksum.perform_md5(real_cfile, calc_prelink=1) except IOError: err = "Insufficient permissions to read %(cfile)s" obj_errs.append(err % locals()) return obj_errs if cur_checksum != md5sum: err = "%(cfile)s has incorrect MD5sum" obj_errs.append(err % locals()) return obj_errs if self.check_timestamps: mtime = int(files[cfile][1]) st_mtime = int(os.lstat(real_cfile).st_mtime) if st_mtime != mtime: err = ("%(cfile)s has wrong mtime (is %(st_mtime)d, should be " "%(mtime)d)") obj_errs.append(err % locals()) return obj_errs return obj_errs
def _verify_obj(self, files, cfile, errs): """Verify the MD5 sum and/or mtime and return any errors.""" obj_errs = errs[:] if self.check_sums: md5sum = files[cfile][2] try: cur_checksum = checksum.perform_md5(cfile, calc_prelink=1) except IOError: err = "Insufficient permissions to read %(cfile)s" obj_errs.append(err % locals()) return obj_errs if cur_checksum != md5sum: err = "%(cfile)s has incorrect MD5sum" obj_errs.append(err % locals()) return obj_errs if self.check_timestamps: mtime = int(files[cfile][1]) st_mtime = int(os.lstat(cfile).st_mtime) if st_mtime != mtime: err = ( "%(cfile)s has wrong mtime (is %(st_mtime)d, should be " "%(mtime)d)" ) obj_errs.append(err % locals()) return obj_errs return obj_errs
def testCopyFile(self): tempdir = tempfile.mkdtemp() try: src_path = os.path.join(tempdir, "src") dest_path = os.path.join(tempdir, "dest") content = b"foo" with open(src_path, "wb") as f: f.write(content) copyfile(src_path, dest_path) self.assertEqual(perform_md5(src_path), perform_md5(dest_path)) finally: shutil.rmtree(tempdir)
def testCopyFile(self): tempdir = tempfile.mkdtemp() try: src_path = os.path.join(tempdir, 'src') dest_path = os.path.join(tempdir, 'dest') content = b'foo' with open(src_path, 'wb') as f: f.write(content) copyfile(src_path, dest_path) self.assertEqual(perform_md5(src_path), perform_md5(dest_path)) finally: shutil.rmtree(tempdir)
def _checksum_failure_temp_file(settings, distdir, basename): """ First try to find a duplicate temp file with the same checksum and return that filename if available. Otherwise, use mkstemp to create a new unique filename._checksum_failure_.$RANDOM, rename the given file, and return the new filename. In any case, filename will be renamed or removed before this function returns a temp filename. """ filename = os.path.join(distdir, basename) if basename.endswith(_download_suffix): normal_basename = basename[:-len(_download_suffix)] else: normal_basename = basename size = os.stat(filename).st_size checksum = None tempfile_re = re.compile(re.escape(normal_basename) + r'\._checksum_failure_\..*') for temp_filename in os.listdir(distdir): if not tempfile_re.match(temp_filename): continue temp_filename = os.path.join(distdir, temp_filename) try: if size != os.stat(temp_filename).st_size: continue except OSError: continue try: temp_checksum = perform_md5(temp_filename) except FileNotFound: # Apparently the temp file disappeared. Let it go. continue if checksum is None: checksum = perform_md5(filename) if checksum == temp_checksum: os.unlink(filename) return temp_filename fd, temp_filename = \ tempfile.mkstemp("", normal_basename + "._checksum_failure_.", distdir) os.close(fd) _movefile(filename, temp_filename, mysettings=settings) return temp_filename
def testCopyFileSparse(self): tempdir = tempfile.mkdtemp() try: src_path = os.path.join(tempdir, 'src') dest_path = os.path.join(tempdir, 'dest') content = b'foo' # Use seek to create some sparse blocks. Don't make these # files too big, in case the filesystem doesn't support # sparse files. with open(src_path, 'wb') as f: f.write(content) f.seek(2**17, 1) f.write(content) f.seek(2**18, 1) f.write(content) # Test that sparse blocks are handled correctly at # the end of the file (involves seek and truncate). f.seek(2**17, 1) copyfile(src_path, dest_path) self.assertEqual(perform_md5(src_path), perform_md5(dest_path)) # This last part of the test is expected to fail when sparse # copy is not implemented, so set the todo flag in order # to tolerate failures. self.todo = True # If sparse blocks were preserved, then both files should # consume the same number of blocks. self.assertEqual( os.stat(src_path).st_blocks, os.stat(dest_path).st_blocks) finally: shutil.rmtree(tempdir)