&& inBase && !root.child((_base.length() > 0 ? _base + "/" : "") + pathElement).exists()) if(!ALLOW_SYMLINK_ESCAPE && (root.supportIsDescendant() && !root.isDescendant(base))){ LOGGER.log(Level.WARNING, "Trying to access a file outside of the directory, target: "+ base); rsp.sendError(HttpServletResponse.SC_FORBIDDEN, "Trying to access a file outside of the directory, target: " + base); VirtualFile baseFile = base.isEmpty() ? root : root.child(base); if(baseFile.isDirectory()) { if(zip) { rsp.setContentType("application/zip"); 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()) { os.write('/'); glob = baseFile.run(new BuildChildPaths(baseFile, req.getLocale())); baseFile = baseFile.child(indexFileName); if(!baseFile.exists()) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; InputStream fingerprintInput = baseFile.open(); try {
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() + "/"); } } } }
/** * Builds the path list and href recursively top-down. */ private static void buildPathList(VirtualFile baseDir, VirtualFile filePath, List<Path> pathList, StringBuilder href) throws IOException { VirtualFile parent = filePath.getParent(); if (!baseDir.equals(parent)) { buildPathList(baseDir, parent, pathList, href); } href.append(Util.rawEncode(filePath.getName())); if (filePath.isDirectory()) { href.append("/"); } Path path = new Path(href.toString(), filePath.getName(), filePath.isDirectory(), filePath.length(), filePath.canRead(), filePath.lastModified()); pathList.add(path); }
/** * 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; }
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) { relativePath = dir.getName() + '/' + n; } else { relativePath = n; String targetFile = dir.toString().substring(root.toString().length()) + n; if (!ALLOW_SYMLINK_ESCAPE && root.supportIsDescendant() && !root.isDescendant(targetFile)) { LOGGER.log(Level.INFO, "Trying to access a file outside of the directory: " + root + ", illicit target: " + targetFile); } else { VirtualFile f = dir.child(n); e.setTime(f.lastModified()); zos.putNextEntry(e); try (InputStream in = f.open()) { IOUtils.copy(in, zos);
&& inBase && !root.child((_base.length() > 0 ? _base + "/" : "") + pathElement).exists()) VirtualFile baseFile = root.child(base); if(baseFile.isDirectory()) { if(zip) { rsp.setContentType("application/zip"); 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()) { os.write('/'); glob = baseFile.run(new BuildChildPaths(baseFile, req.getLocale())); baseFile = baseFile.child(indexFileName); if(!baseFile.exists()) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; InputStream fingerprintInput = baseFile.open(); try { rsp.forward(Jenkins.getInstance().getFingerprint(Util.getDigestOf(fingerprintInput)), "/", req); long lastModified = baseFile.lastModified(); long length = baseFile.length();
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); }
List<List<Path>> r = new ArrayList<List<Path>>(); VirtualFile[] files = cur.list(); Arrays.sort(files,new FileComparator(locale)); Path p = new Path(Util.rawEncode(f.getName()), f.getName(), f.isDirectory(), f.length(), f.canRead(), f.lastModified()); if(!f.isDirectory()) { r.add(Collections.singletonList(p)); } else { String relPath = Util.rawEncode(f.getName()); while(true) { for (VirtualFile vf : f.list()) { String name = vf.getName(); if (!name.startsWith(".") && !name.equals("CVS") && !name.equals(".svn")) { sub.add(vf); if (sub.size() !=1 || !sub.get(0).isDirectory()) break; f = sub.get(0); relPath += '/'+Util.rawEncode(f.getName()); l.add(new Path(relPath,f.getName(),true, f.length(), f.canRead(), f.lastModified()));
@Override public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException { rsp.setContentType("application/octet-stream"); Util.copyStreamAndClose(parent.parent.getArtifactManager().root().child(artifactPath()).open(), rsp.getCompressedOutputStream(req)); } };
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; }
private static String copyOne(VirtualFile s, FilePath d, boolean fingerprint, TaskListener listener) throws IOException, InterruptedException { String link = s.readLink(); if (link != null) { d.getParent().mkdirs(); URL u = s.toExternalURL(); byte[] digest; if (u != null) { if (fingerprint) { MessageDigest md5 = md5(); try (InputStream is = s.open(); OutputStream os = d.write()) { IOUtils.copy(is, new DigestOutputStream(os, md5)); try (InputStream is = s.open()) { d.copyFrom(is); d.touch(s.lastModified()); } catch (IOException x) { LOGGER.warning(x.getMessage()); int mode = s.mode(); if (mode != -1) { d.chmod(mode);
VirtualFile appRoot = VirtualFile.open(Play.applicationPath); Play.javaPath.add(0, appRoot.child("test/sub/packages/data.yml"));
private FilePath copy(VirtualFile src, FilePath dst, TaskListener listener) throws IOException, InterruptedException { URL u = src.toExternalURL(); if (u != null) { new RobustHTTPClient().copyFromRemotely(dst, u, listener); } else { try (InputStream in = src.open()) { dst.copyFrom(in); } } return dst; }
/** * {@link} read VirtualFile * * @param f file to read * @return read string * @throws IOException */ public static String readFileToString(VirtualFile f) throws IOException, InterruptedException { InputStream in = f.open(); try { return IOUtils.toString(in); } finally { in.close(); } }
private String fullName(VirtualFile file, VirtualFile root) throws IOException { StringBuilder name = new StringBuilder(file.getName()); VirtualFile parent = file.getParent(); while (parent != null && parent.isDirectory() && !parent.equals(root)) { if(!StringUtils.isEmpty(parent.getName())) { name.insert(0, parent.getName() + "/"); } parent = parent.getParent(); } return name.toString(); }
/** * @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); }
@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); } }
private int dirRank(VirtualFile f) { try { if(f.isDirectory()) return 0; else return 1; } catch (IOException ex) { return 0; } } }