Search in sources :

Example 11 with VirtualChannel

use of hudson.remoting.VirtualChannel in project hudson-2.x by hudson.

the class FilePath method unzipFrom.

/**
     * Reads the given InputStream as a zip file and extracts it into this directory.
     *
     * @param _in
     *      The stream will be closed by this method after it's fully read.
     * @since 1.283
     * @see #unzip(FilePath)
     */
public void unzipFrom(InputStream _in) throws IOException, InterruptedException {
    final InputStream in = new RemoteInputStream(_in);
    act(new FileCallable<Void>() {

        public Void invoke(File dir, VirtualChannel channel) throws IOException {
            unzip(dir, in);
            return null;
        }

        private static final long serialVersionUID = 1L;
    });
}
Also used : VirtualChannel(hudson.remoting.VirtualChannel) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) RemoteInputStream(hudson.remoting.RemoteInputStream) ObjectInputStream(java.io.ObjectInputStream) ZipInputStream(java.util.zip.ZipInputStream) TarInputStream(hudson.org.apache.tools.tar.TarInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RemoteInputStream(hudson.remoting.RemoteInputStream) IOException(java.io.IOException) File(java.io.File)

Example 12 with VirtualChannel

use of hudson.remoting.VirtualChannel in project hudson-2.x by hudson.

the class FilePath method validateAntFileMask.

/**
     * Validates the ant file mask (like "foo/bar/*.txt, zot/*.jar")
     * against this directory, and try to point out the problem.
     *
     * <p>
     * This is useful in conjunction with {@link FormValidation}.
     *
     * @return
     *      null if no error was found. Otherwise returns a human readable error message.
     * @since 1.90
     * @see #validateFileMask(FilePath, String)
     */
public String validateAntFileMask(final String fileMasks) throws IOException, InterruptedException {
    return act(new FileCallable<String>() {

        public String invoke(File dir, VirtualChannel channel) throws IOException {
            if (fileMasks.startsWith("~"))
                return Messages.FilePath_TildaDoesntWork();
            StringTokenizer tokens = new StringTokenizer(fileMasks, ",");
            while (tokens.hasMoreTokens()) {
                final String fileMask = tokens.nextToken().trim();
                if (hasMatch(dir, fileMask))
                    // no error on this portion
                    continue;
                // so see if we can match by using ' ' as the separator
                if (fileMask.contains(" ")) {
                    boolean matched = true;
                    for (String token : Util.tokenize(fileMask)) matched &= hasMatch(dir, token);
                    if (matched)
                        return Messages.FilePath_validateAntFileMask_whitespaceSeprator();
                }
                // a common mistake is to assume the wrong base dir, and there are two variations
                // to this: (1) the user gave us aa/bb/cc/dd where cc/dd was correct
                // and (2) the user gave us cc/dd where aa/bb/cc/dd was correct.
                {
                    // check the (1) above first
                    String f = fileMask;
                    while (true) {
                        int idx = findSeparator(f);
                        if (idx == -1)
                            break;
                        f = f.substring(idx + 1);
                        if (hasMatch(dir, f))
                            return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask, f);
                    }
                }
                {
                    // check the (2) above next as this is more expensive.
                    // Try prepending "**/" to see if that results in a match
                    FileSet fs = Util.createFileSet(dir, "**/" + fileMask);
                    DirectoryScanner ds = fs.getDirectoryScanner(new Project());
                    if (ds.getIncludedFilesCount() != 0) {
                        // try shorter name first so that the suggestion results in least amount of changes
                        String[] names = ds.getIncludedFiles();
                        Arrays.sort(names, SHORTER_STRING_FIRST);
                        for (String f : names) {
                            // now we want to decompose f to the leading portion that matched "**"
                            // and the trailing portion that matched the file mask, so that
                            // we can suggest the user error.
                            //
                            // this is not a very efficient/clever way to do it, but it's relatively simple
                            String prefix = "";
                            while (true) {
                                int idx = findSeparator(f);
                                if (idx == -1)
                                    break;
                                prefix += f.substring(0, idx) + '/';
                                f = f.substring(idx + 1);
                                if (hasMatch(dir, prefix + fileMask))
                                    return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask, prefix + fileMask);
                            }
                        }
                    }
                }
                {
                    // finally, see if we can identify any sub portion that's valid. Otherwise bail out
                    String previous = null;
                    String pattern = fileMask;
                    while (true) {
                        if (hasMatch(dir, pattern)) {
                            // found a match
                            if (previous == null)
                                return Messages.FilePath_validateAntFileMask_portionMatchAndSuggest(fileMask, pattern);
                            else
                                return Messages.FilePath_validateAntFileMask_portionMatchButPreviousNotMatchAndSuggest(fileMask, pattern, previous);
                        }
                        int idx = findSeparator(pattern);
                        if (idx < 0) {
                            // no more path component left to go back
                            if (pattern.equals(fileMask))
                                return Messages.FilePath_validateAntFileMask_doesntMatchAnything(fileMask);
                            else
                                return Messages.FilePath_validateAntFileMask_doesntMatchAnythingAndSuggest(fileMask, pattern);
                        }
                        // cut off the trailing component and try again
                        previous = pattern;
                        pattern = pattern.substring(0, idx);
                    }
                }
            }
            // no error
            return null;
        }

        private boolean hasMatch(File dir, String pattern) {
            FileSet fs = Util.createFileSet(dir, pattern);
            DirectoryScanner ds = fs.getDirectoryScanner(new Project());
            return ds.getIncludedFilesCount() != 0 || ds.getIncludedDirsCount() != 0;
        }

        /**
             * Finds the position of the first path separator.
             */
        private int findSeparator(String pattern) {
            int idx1 = pattern.indexOf('\\');
            int idx2 = pattern.indexOf('/');
            if (idx1 == -1)
                return idx2;
            if (idx2 == -1)
                return idx1;
            return Math.min(idx1, idx2);
        }
    });
}
Also used : Project(org.apache.tools.ant.Project) AbstractProject(hudson.model.AbstractProject) StringTokenizer(java.util.StringTokenizer) FileSet(org.apache.tools.ant.types.FileSet) VirtualChannel(hudson.remoting.VirtualChannel) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) IOException(java.io.IOException) File(java.io.File)

Aggregations

VirtualChannel (hudson.remoting.VirtualChannel)12 IOException (java.io.IOException)9 File (java.io.File)7 IOException2 (hudson.util.IOException2)3 FileInputStream (java.io.FileInputStream)3 TarInputStream (hudson.org.apache.tools.tar.TarInputStream)2 Channel (hudson.remoting.Channel)2 RemoteInputStream (hudson.remoting.RemoteInputStream)2 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ZipInputStream (java.util.zip.ZipInputStream)2 CountingInputStream (org.apache.commons.io.input.CountingInputStream)2 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2 FileSet (org.apache.tools.ant.types.FileSet)2 FilePath (hudson.FilePath)1 AbstractBuild (hudson.model.AbstractBuild)1 AbstractProject (hudson.model.AbstractProject)1 Computer (hudson.model.Computer)1