def run(self, source, dest_dir): """Installs a file at |source| into |dest_dir| in process. Args: source: Path to the file to be installed. dest_dir: Path to the directory which |source| will be installed into. Returns: True on success, otherwise False. """ dest = os.path.join(dest_dir, os.path.basename(source)) if not self._is_install_allowed(source, dest): return False # To emulate the `install` command, remove the dest file in # advance. try: os.unlink(dest) except OSError as e: # Removing a non-existing entry should be handled as a # regular case. if e.errno != errno.ENOENT: raise try: copyfile(source, dest) _set_attributes(self._parsed_options, dest) if self._copy_xattr: movefile._copyxattr(source, dest, exclude=self._xattr_exclude) except Exception: logging.exception( 'Failed to copy file: ' '_parsed_options=%r, source=%r, dest_dir=%r', self._parsed_options, source, dest_dir) return False return True
def recompose_mem(self, xpdata, break_hardlinks=True): """ Update the xpak segment. @param xpdata: A new xpak segment to be written, like that returned from the xpak_mem() function. @param break_hardlinks: If hardlinks exist, create a copy in order to break them. This makes it safe to use hardlinks to create cheap snapshots of the repository, which is useful for solving race conditions on binhosts as described here: https://crbug.com/185031 Default is True. """ self.scan() # Don't care about condition... We'll rewrite the data anyway. if break_hardlinks and self.filestat and self.filestat.st_nlink > 1: tmp_fname = "%s.%d" % (self.file, os.getpid()) copyfile(self.file, tmp_fname) try: portage.util.apply_stat_permissions(self.file, self.filestat) except portage.exception.OperationNotPermitted: pass os.rename(tmp_fname, self.file) myfile = open(_unicode_encode(self.file, encoding=_encodings['fs'], errors='strict'), 'ab+') if not myfile: raise IOError myfile.seek(-self.xpaksize, 2) # 0,2 or -0,2 just mean EOF. myfile.truncate() myfile.write(xpdata + encodeint(len(xpdata)) + b'STOP') myfile.flush() myfile.close() return 1
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 _run(self): src_path = _unicode_encode(self.src_path, encoding=_encodings['fs'], errors='strict') dest_path = _unicode_encode(self.dest_path, encoding=_encodings['fs'], errors='strict') copyfile(src_path, dest_path) apply_stat_permissions(dest_path, _os.stat(src_path))
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 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 run(self, source, dest_dir): """Installs a file at |source| into |dest_dir| in process. Args: source: Path to the file to be installed. dest_dir: Path to the directory which |source| will be installed into. Returns: True on success, otherwise False. """ dest = os.path.join(dest_dir, os.path.basename(source)) # Raise an exception if stat(source) fails, intentionally. sstat = os.stat(source) if not self._is_install_allowed(source, sstat, dest): return False # To emulate the `install` command, remove the dest file in # advance. try: os.unlink(dest) except OSError as e: # Removing a non-existing entry should be handled as a # regular case. if e.errno != errno.ENOENT: raise try: copyfile(source, dest) _set_attributes(self._parsed_options, dest) if self._copy_xattr: movefile._copyxattr( source, dest, exclude=self._xattr_exclude) if self._parsed_options.preserve_timestamps: _set_timestamps(sstat, dest) except Exception: logging.exception( 'Failed to copy file: ' '_parsed_options=%r, source=%r, dest_dir=%r', self._parsed_options, source, dest_dir) return False return True
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)