Search in sources :

Example 6 with ContainerFileProxy

use of org.eclipse.linuxtools.internal.docker.core.ContainerFileProxy in project linuxtools by eclipse.

the class CopyFromContainerCommandHandler method performCopyFromContainer.

private void performCopyFromContainer(final IDockerConnection connection, final IDockerContainer container, final String target, final List<ContainerFileProxy> files) {
    final Job copyFromContainerJob = new Job(CommandMessages.getFormattedString(COPY_FROM_CONTAINER_JOB_TITLE, container.name())) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(CommandMessages.getString(COPY_FROM_CONTAINER_JOB_TASK), files.size());
            try (Closeable token = ((DockerConnection) connection).getOperationToken()) {
                for (ContainerFileProxy proxy : files) {
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return Status.CANCEL_STATUS;
                    }
                    try {
                        monitor.setTaskName(CommandMessages.getFormattedString(COPY_FROM_CONTAINER_JOB_SUBTASK, proxy.getFullPath()));
                        monitor.worked(1);
                        InputStream in = ((DockerConnection) connection).copyContainer(token, container.id(), proxy.getLink());
                        /*
							 * The input stream from copyContainer might be
							 * incomplete or non-blocking so we should wrap it
							 * in a stream that is guaranteed to block until
							 * data is available.
							 */
                        TarArchiveInputStream k = new TarArchiveInputStream(new BlockingInputStream(in));
                        TarArchiveEntry te = null;
                        while ((te = k.getNextTarEntry()) != null) {
                            long size = te.getSize();
                            IPath path = new Path(target);
                            path = path.append(te.getName());
                            File f = new File(path.toOSString());
                            if (te.isDirectory()) {
                                f.mkdir();
                                continue;
                            } else {
                                f.createNewFile();
                            }
                            FileOutputStream os = new FileOutputStream(f);
                            int bufferSize = ((int) size > 4096 ? 4096 : (int) size);
                            byte[] barray = new byte[bufferSize];
                            int result = -1;
                            while ((result = k.read(barray, 0, bufferSize)) > -1) {
                                if (monitor.isCanceled()) {
                                    monitor.done();
                                    k.close();
                                    os.close();
                                    return Status.CANCEL_STATUS;
                                }
                                os.write(barray, 0, result);
                            }
                            os.close();
                        }
                        k.close();
                    } catch (final DockerException e) {
                        Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), CommandMessages.getFormattedString(ERROR_COPYING_FROM_CONTAINER, proxy.getLink(), container.name()), e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
                    }
                }
            } catch (InterruptedException e) {
            // do nothing
            } catch (IOException e) {
                Activator.log(e);
            } catch (DockerException e1) {
                Activator.log(e1);
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    copyFromContainerJob.schedule();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IPath(org.eclipse.core.runtime.IPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) Closeable(java.io.Closeable) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ContainerFileProxy(org.eclipse.linuxtools.internal.docker.core.ContainerFileProxy) FileOutputStream(java.io.FileOutputStream) Job(org.eclipse.core.runtime.jobs.Job) File(java.io.File)

Aggregations

ContainerFileProxy (org.eclipse.linuxtools.internal.docker.core.ContainerFileProxy)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)2 IDockerContainerInfo (org.eclipse.linuxtools.docker.core.IDockerContainerInfo)2 ContainerFileSystemProvider (org.eclipse.linuxtools.internal.docker.ui.ContainerFileSystemProvider)2 MinimizedFileSystemElement (org.eclipse.linuxtools.internal.docker.ui.MinimizedFileSystemElement)2 PopulateContainerFilesOperation (org.eclipse.linuxtools.internal.docker.ui.PopulateContainerFilesOperation)2 Closeable (java.io.Closeable)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Iterator (java.util.Iterator)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 Job (org.eclipse.core.runtime.jobs.Job)1