public static boolean isSubdirectoryOf(File parent, File subdirectory) throws IOException { File parentFile = parent.getCanonicalFile(); File current = subdirectory.getCanonicalFile(); while (current != null) { if (current.equals(parentFile)) { return true; } current = current.getParentFile(); } return false; }
public static boolean isValidZipEntryName(String entryName) { try { File currentPath = new File(".").getCanonicalFile(); File canonical = new File(currentPath, entryName).getCanonicalFile(); if (isInSubDirectoryInternal(currentPath, canonical)) { return true; } LOG.error("Path traversal attack detected, invalid name: {}", entryName); return false; } catch (Exception e) { LOG.error("Path traversal attack detected, invalid name: {}", entryName); return false; } }
public URL find(String classname) { char sep = File.separatorChar; String filename = directory + sep + classname.replace('.', sep) + ".class"; File f = new File(filename); if (f.exists()) try { return f.getCanonicalFile().toURI().toURL(); } catch (MalformedURLException e) {} catch (IOException e) {} return null; }
public static boolean isSymlink(File file) throws IOException { if (file == null) throw new NullPointerException("File must not be null"); File canon; if (file.getParent() == null) { canon = file; } else { File canonDir = file.getParentFile().getCanonicalFile(); canon = new File(canonDir, file.getName()); } return !canon.getCanonicalFile().equals(canon.getAbsoluteFile()); }
if(!fe.isAbsolute() && pe.charAt(0)!='/' && pe.charAt(0)!='\\') { fe=new File(rootPath,fe.getPath()); } try { fe=fe.getCanonicalFile(); } catch(IOException thr) { log.diagln("Skipping non-existent classpath element '"+fe+"' ("+thr+")."); fe=new File(fe,bn); addJars(fe.getParentFile(),bn); else if(!fe.exists()) { // s/never be due getCanonicalFile() above log.diagln("Could not find classpath element '"+fe+"'"); else if(fe.isDirectory()) { addURL(createUrl(fe)); else if(fe.getName().toLowerCase().endsWith(".zip") || fe.getName().toLowerCase().endsWith(".jar")) { addURL(createUrl(fe)); if(!dir.exists()) { log.diagln("Could not find directory for Class Path element '"+dir+File.separator+nam+".jar'"); return; URL url=fe.toURI().toURL(); log.diagln("Added URL: '"+url.toString()+"'"); if(classPath.length()>0) { classPath+=File.pathSeparator; }
private File doCreateTempDir(String prefix, String suffix) throws IOException { prefix = prefix == null ? "" : prefix; suffix = suffix == null ? ".tmp" : suffix; do { int count = ourRandom.nextInt(); final File f = new File(myCurrentTempDir, prefix + count + suffix); if (!f.exists() && f.mkdirs()) { return f.getCanonicalFile(); } } while (true); }
JarClassPath(String pathname) throws NotFoundException { try { jarfile = new JarFile(pathname); jarfileURL = new File(pathname).getCanonicalFile() .toURI().toURL().toString(); return; } catch (IOException e) {} throw new NotFoundException(pathname); }
public static File getRelativeFile(File baseFile, File fileToRelativize) throws IOException { if (baseFile.isFile()) { baseFile = baseFile.getParentFile(); } return new File(getRelativeFileInternal(baseFile.getCanonicalFile(), fileToRelativize.getCanonicalFile())); }
String pid = new File("/proc/self").getCanonicalFile().getName();
if (location.isDirectory()) { try { if (altName != null) { File f = new File(location, altName); if (f.isFile() && f.canRead()) return f.toURI().toURL(); File f = new File(location, name); if (f.isFile() && f.canRead()) return f.toURI().toURL(); return null; } catch (MalformedURLException e) { absoluteFile = location.getCanonicalFile(); } catch (Exception e) { absoluteFile = location.getAbsoluteFile(); String absoluteUri = absoluteFile.toURI().toString();
final boolean file1Exists = file1.exists(); if (file1Exists != file2.exists()) { return false; if (file1.isDirectory() || file2.isDirectory()) { if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
public void setRootPath(String rootPath) { if (rootPath == null) throw new NullPointerException(); try { root = new File(rootPath).getCanonicalFile(); if (!root.isDirectory()){ throw new IllegalArgumentException("Given root path \"" + root + "\" is not a directory"); } } catch (IOException ex) { throw new AssetLoadException("Root path is invalid", ex); } }
/** * Canonical file resolution on windows does not resolve links. * Real paths on windows can be resolved only using {@link Path#toRealPath(LinkOption...)}, but file should exist in that case. * We will try to do as much as possible and will try to use {@link Path#toRealPath(LinkOption...)} when file exist and will fallback to only * use {@link File#getCanonicalFile()} if file does not exist. * see JDK-8003887 for details * @param file - file to resolve canonical representation * @return canonical file representation. */ public static File getCanonicalFile( File file ) { try { File fileToResolve = file.exists() ? file.toPath().toRealPath().toFile() : file; return fileToResolve.getCanonicalFile(); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }
String name = f.getName(); if (f.isDirectory()) { File deref = f.getCanonicalFile(); if (currentPath.add(deref)) { scanDirectory(deref, classloader, packagePrefix + name + "/", currentPath);
/** * Creates any necessary but nonexistent parent directories of the specified file. Note that if * this operation fails it may have succeeded in creating some (but not all) of the necessary * parent directories. * * @throws IOException if an I/O error occurs, or if any necessary but nonexistent parent * directories of the specified file could not be created. * @since 4.0 */ public static void createParentDirs(File file) throws IOException { checkNotNull(file); File parent = file.getCanonicalFile().getParentFile(); if (parent == null) { /* * The given directory is a filesystem root. All zero of its ancestors exist. This doesn't * mean that the root itself exists -- consider x:\ on a Windows machine without such a drive * -- or even that the caller can create it, but this method makes no such guarantees even for * non-root files. */ return; } parent.mkdirs(); if (!parent.isDirectory()) { throw new IOException("Unable to create parent directories of " + file); } }
/** Get a relative URI for {@code file} from {@code output} folder. */ static String createRelativeUri(File file, File output) { if (file == null) { return null; } try { file = file.getCanonicalFile(); output = output.getCanonicalFile(); if (file.equals(output)) { throw new IllegalArgumentException("File path and output folder are the same."); } StringBuilder builder = new StringBuilder(); while (!file.equals(output)) { if (builder.length() > 0) { builder.insert(0, "/"); } builder.insert(0, file.getName()); file = file.getParentFile().getCanonicalFile(); } return builder.toString(); } catch (IOException e) { throw new RuntimeException(e); } }
public static boolean isSymlink(File file) throws IOException { if (file == null) throw new NullPointerException("File must not be null"); File canon; if (file.getParent() == null) { canon = file; } else { File canonDir = file.getParentFile().getCanonicalFile(); canon = new File(canonDir, file.getName()); } return !canon.getCanonicalFile().equals(canon.getAbsoluteFile()); }
@Override public URL find(String classname) { char sep = File.separatorChar; String filename = directory + sep + classname.replace('.', sep) + ".class"; File f = new File(filename); if (f.exists()) try { return f.getCanonicalFile().toURI().toURL(); } catch (MalformedURLException e) {} catch (IOException e) {} return null; }
private Path[] toPath(final List<File> watchDir) throws IOException { Set<File> files = new LinkedHashSet<>(); files.add(new File(System.getProperty("user.dir"))); if (watchDir != null) { files.addAll(watchDir); } List<Path> paths = new ArrayList<>(); for (File file : files) { if (file.exists()) { paths.add(file.getCanonicalFile().toPath()); } } return paths.toArray(new Path[paths.size()]); }
public URL resourceNameToURL(String resource) throws Exception { try { // Try to format as a URL? return new URL(resource); } catch (MalformedURLException e) { // try to locate a file if (resource.startsWith("./")) resource = resource.substring(2); File file = new File(resource).getCanonicalFile(); return file.toURI().toURL(); } }