All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.lib.io.Windows Maven / Gradle / Ivy

Go to download

A main program (executable JAR) that will listen to port 29998. At first, it can only answer that it is an Envoy (a limited agent). The only function it supports is installing a -runpath. It will then create a framework + agent and transfer the connection to the just installed agent who will then install the bundles. This JAR is a main command for JPM called bndremote. In JPM, it will start up with debug enabled. This JAR does some highly complicated class loading wizardy to ensure that it does not enforce any constraints on the -runpath.

The newest version!
package aQute.lib.io;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import aQute.lib.io.IO.OS;

class Windows implements OS {
	final static Pattern WINDOWS_BAD_FILE_NAME_P = Pattern.compile(
		"(?:(:?.*[\u0000-\u001F<>:\"|/\\\\?*].*)|\\.\\.|CON|PRN|AUX|NUL|COM\\d|COM¹|COM²|COM³|LPT\\d|LPT¹|LPT²|LPT³)(?:\\.\\w+)?",
		Pattern.CASE_INSENSITIVE);
	final static Pattern	DRIVE_P					= Pattern.compile("/?(?[a-z]:)", Pattern.CASE_INSENSITIVE);

	@Override
	public File getBasedFile(File base, String subPath) throws IOException {
		String use;
		Matcher matcher = WINDOWS_BAD_FILE_NAME_P.matcher(subPath);
		if (matcher.find()) {

			try {
				Path normalizedPath = Path.of(subPath)
					.normalize();

				if (normalizedPath.startsWith(IO.DOTDOT)) {
					throw new IOException("io.sub.up invalid path, will escape the designated directory. path='"
						+ subPath + "', base='" + base + "', normalized='" + normalizedPath + "'");
				}
				for (int i = 0; i < normalizedPath.getNameCount(); i++) {
					String segment = normalizedPath.getName(i)
						.toString();
					if (matcher.reset(segment)
						.matches()) {
						throw new IOException("io.win.sub.invalid pathcontains reserved names on windows. path='"
							+ subPath + "', base='" + base + "', pattern='" + WINDOWS_BAD_FILE_NAME_P + "'");
					}
				}
				use = normalizedPath.toString();
			} catch (java.nio.file.InvalidPathException e) {
				throw new IOException("io.win.sub.invalid pathcontains reserved names on windows. path='" + subPath
					+ "', base='" + base + "': '" + e.getMessage() + "'");
			}
		} else
			use = subPath;
		return new File(base, use);
	}

	@Override
	public String getenv(String string) {
		return IO.hc.getenv(string);
	}

	@Override
	public String toSafeFileName(String string) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < string.length(); i++) {
			char c = string.charAt(i);
			if (c < ' ')
				continue;

			sb.append(switch (c) {
				case '<', '>', '"', '/', '\\', '|', '*', ':', '?' -> '%';
				default -> c;
			});
		}
		if (sb.length() == 0 || WINDOWS_BAD_FILE_NAME_P.matcher(sb)
			.matches())
			sb.append("_");

		return sb.toString();
	}

	@Override
	public File getFile(File base, String file) {
		file = file.replace('\\', '/');
		Matcher m = DRIVE_P.matcher(file);
		if (m.lookingAt()) {
			base = new File(m.group("drive"));
		}
		return Other.getFile0(base, file);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy