/** * @deprecated use {@link #list(String, String, boolean)} instead */ @Deprecated public @Nonnull String[] list(String glob) throws IOException { return list(glob.replace('\\', '/'), null, true).toArray(MemoryReductionUtil.EMPTY_STRING_ARRAY); }
/** * Runs ant GLOB against the current {@link FilePath} and returns matching * paths. * @param baseRef String like "../../../" that cancels the 'rest' portion. Can be "./" */ private static List<List<Path>> patternScan(VirtualFile baseDir, String pattern, String baseRef) throws IOException { Collection<String> files = baseDir.list(pattern, null, /* TODO what is the user expectation? */true); if (!files.isEmpty()) { List<List<Path>> r = new ArrayList<List<Path>>(files.size()); for (String match : files) { List<Path> file = buildPathList(baseDir, baseDir.child(match), baseRef); r.add(file); } return r; } return null; }
private static void collectFiles(VirtualFile d, Collection<String> names, String prefix) throws IOException { for (VirtualFile child : d.list()) { if (child.isFile()) { names.add(prefix + child.getName()); } else if (child.isDirectory()) { collectFiles(child, names, prefix + child.getName() + "/"); } } } }
private static int addArtifacts(@Nonnull VirtualFile dir, @Nonnull String path, @Nonnull String pathHref, @Nonnull SerializableArtifactList r, @CheckForNull SerializableArtifact parent, int upTo) throws IOException { VirtualFile[] kids = dir.list(); Arrays.sort(kids);
zos.setEncoding(System.getProperty("file.encoding")); // TODO JENKINS-20663 make this overridable via query parameter for (String n : dir.list(glob.isEmpty() ? "**" : glob, null, /* TODO what is the user expectation? */true)) { String relativePath; if (glob.length() == 0) {
rsp.setContentType("text/plain;charset=UTF-8"); try (OutputStream os = rsp.getOutputStream()) { for (VirtualFile kid : baseFile.list()) { os.write(kid.getName().getBytes("UTF-8")); if (kid.isDirectory()) {
/** * Runs ant GLOB against the current {@link FilePath} and returns matching * paths. * @param baseRef String like "../../../" that cancels the 'rest' portion. Can be "./" */ private static List<List<Path>> patternScan(VirtualFile baseDir, String pattern, String baseRef) throws IOException { String[] files = baseDir.list(pattern); if (files.length > 0) { List<List<Path>> r = new ArrayList<List<Path>>(files.length); for (String match : files) { List<Path> file = buildPathList(baseDir, baseDir.child(match), baseRef); r.add(file); } return r; } return null; }
private static Map<String, String> copy(FilePath targetDir, VirtualFile srcDir, String expandedFilter, String expandedExcludes, boolean fingerprint, TaskListener listener, boolean flatten) throws IOException, InterruptedException { targetDir.mkdirs(); // Create target if needed Collection<String> list = srcDir.list(expandedFilter.replace('\\', '/'), expandedExcludes != null ? expandedExcludes.replace('\\', '/') : null, false); Map<String, String> fingerprints = new HashMap<>(); for (String entry : list) { String digest = copyOne(srcDir.child(entry), new FilePath(targetDir, flatten ? entry.replaceFirst(".+/", "") : entry), fingerprint, listener); fingerprints.put(entry, digest); } return fingerprints; }
private static void zip(OutputStream outputStream, VirtualFile dir, String glob) throws IOException { try (ZipOutputStream zos = new ZipOutputStream(outputStream)) { zos.setEncoding(System.getProperty("file.encoding")); // TODO JENKINS-20663 make this overridable via query parameter for (String n : dir.list(glob.length() == 0 ? "**" : glob)) { String relativePath; if (glob.length() == 0) { // JENKINS-19947: traditional behavior is to prepend the directory name relativePath = dir.getName() + '/' + n; } else { relativePath = n; } // In ZIP archives "All slashes MUST be forward slashes" (http://pkware.com/documents/casestudies/APPNOTE.TXT) // TODO On Linux file names can contain backslashes which should not treated as file separators. // Unfortunately, only the file separator char of the master is known (File.separatorChar) // but not the file separator char of the (maybe remote) "dir". ZipEntry e = new ZipEntry(relativePath.replace('\\', '/')); VirtualFile f = dir.child(n); e.setTime(f.lastModified()); zos.putNextEntry(e); try (InputStream in = f.open()) { IOUtils.copy(in, zos); } zos.closeEntry(); } } }
@Override public void copy(Run<?,?> original, Run<?,?> copy, TaskListener listener) throws IOException, InterruptedException { // TODO ArtifactManager should define an optimized operation to copy from another, or VirtualFile should define copyRecursive VirtualFile srcroot = original.getArtifactManager().root(); FilePath dstDir = createTmpDir(); try { Map<String,String> files = new HashMap<>(); for (String path : srcroot.list("**/*")) { files.put(path, path); InputStream in = srcroot.child(path).open(); try { dstDir.child(path).copyFrom(in); } finally { IOUtils.closeQuietly(in); } } if (!files.isEmpty()) { listener.getLogger().println("Copying " + files.size() + " artifact(s) from " + original.getDisplayName()); copy.getArtifactManager().archive(dstDir, new LocalLauncher(listener), new BuildListenerAdapter(listener), files); } } finally { dstDir.deleteRecursive(); } StashManager.copyAll(original, copy); }
private int addArtifacts(@Nonnull VirtualFile dir, @Nonnull String path, @Nonnull String pathHref, @Nonnull ArtifactList r, @Nonnull Artifact parent, int upTo) throws IOException { VirtualFile[] kids = dir.list(); Arrays.sort(kids);
private boolean matchesFile(VirtualFile file, VirtualFile root) throws IOException { if (file.isDirectory()) { for (VirtualFile child : file.list()) { if (matchesFile(child, root)) { return true; } } } else if (nameMatcher.matches(fullName(file, root))) { if (contentMatcher.matches(file.open())) { return true; } } return false; }
@Override protected List<FilePath> run() throws Exception { // where to copy artifacts from? Run<?, ?> r = getContext().get(Run.class); // TODO consider an option to override this (but in what format?) TaskListener listener = getContext().get(TaskListener.class); ArtifactManager am = r.getArtifactManager(); List<FilePath> files = new ArrayList<>(); for (Entry<String, String> e : mapping.entrySet()) { FilePath dst = new FilePath(getContext().get(FilePath.class), e.getValue()); String src = e.getKey(); String[] all = am.root().list(src); if (all.length == 0) { throw new AbortException("no artifacts to unarchive in " + src); } else if (all.length == 1 && all[0].equals(src)) { // the source is a file if (dst.isDirectory()) dst = dst.child(getFileName(all[0])); files.add(copy(am.root().child(all[0]), dst, listener)); } else { // copy into a directory for (String path : all) { files.add(copy(am.root().child(path), dst.child(path), listener)); } } } return files; }
@Override protected boolean matchesSafely(Run item) { ArtifactManager manager = item.getArtifactManager(); try { VirtualFile root = manager.root(); if (!root.exists()) { return false; } for (VirtualFile file : root.list()) { if (matchesFile(file, root)) { return true; } } return false; } catch (IOException e) { throw new RuntimeException("Failed to examine archived files!", e); } }
rsp.setContentType("text/plain;charset=UTF-8"); try (OutputStream os = rsp.getOutputStream()) { for (VirtualFile kid : baseFile.list()) { os.write(kid.getName().getBytes("UTF-8")); if (kid.isDirectory()) {