Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration that has "\" and "/" as separator #73

Open
Katharsas opened this issue Sep 8, 2018 · 0 comments
Open

Allow configuration that has "\" and "/" as separator #73

Katharsas opened this issue Sep 8, 2018 · 0 comments
Labels
P4 type=enhancement Make an existing feature better

Comments

@Katharsas
Copy link

Katharsas commented Sep 8, 2018

I want to create a virtual FileSystem similar to Configuration.unix() that is able to handle both / and \ as separators, like the default Java filesystem.

I regularly read relative file paths from default FileSystem or configuration and need to convert them to relative JimFs paths. Those paths happily mix / and \ if the program is executed on windows, and i do not want to manually replace the separators at every occation since actually JimFs has this functionality built in, but I don't see a way to use it.

A PathType for this would be nice. Or the possibility to configure this somehow. The only way I see to do this is to create my own PathType adapter class that forwards all methods to UnixPathType but has a different constructor that specified additional separators.

Edit:
Implementing adapter is somewhat cumbersome because toUriPath(...) and parseUriPath(...) are protected, so i cannot call them unless i put my adapter into the same package as UnixPathType, but thats only nitpicking.

Edit2:
I need to implement parsePath(String path) as well so it uses the custom Splitter. This solution is not great.

code

package com.google.common.jimfs;

import static com.google.common.jimfs.Feature.FILE_CHANNEL;
import static com.google.common.jimfs.Feature.LINKS;
import static com.google.common.jimfs.Feature.SECURE_DIRECTORY_STREAM;
import static com.google.common.jimfs.Feature.SYMBOLIC_LINKS;

import java.nio.file.InvalidPathException;

/**
 * Very similar to normal Configuration.unix(), but is able to parse windows separators in
 * addition to unix separator.
 * 
 */
public class UnixCompatible {
	
	public static Configuration config() {
		return Configuration.builder(new CompatiblePathType())
	            .setRoots("/")
	            .setWorkingDirectory("/work")
	            .setAttributeViews("basic")
	            .setSupportedFeatures(LINKS, SYMBOLIC_LINKS, SECURE_DIRECTORY_STREAM, FILE_CHANNEL)
	            .build();
	}
	
	static class CompatiblePathType extends PathType {

		final PathType unix;
		
		public CompatiblePathType() {
			super(false, '/', '\\');
			unix = PathType.unix();
		}
		
		/** Copied from UnixPathType, custom splitter */
		@Override
		public ParseResult parsePath(String path) {
			if (path.isEmpty()) {
				return emptyPath();
			}

			checkValid(path);

			final String root = path.startsWith("/") ? "/" : null;
			return new ParseResult(root, splitter().split(path));
		}
		
		/** Copied from UnixPathType */
		private static void checkValid(String path) {
			final int nulIndex = path.indexOf('\0');
			if (nulIndex != -1) {
				throw new InvalidPathException(path, "nul character not allowed", nulIndex);
			}
		}

		@Override
		public String toString(String root, Iterable<String> names) {
			return unix.toString(root, names);
		}

		@Override
		public String toString() {
			return unix.toString();
		}

		@Override
		protected String toUriPath(String root, Iterable<String> names, boolean directory) {
			return unix.toUriPath(root, names, directory);
		}

		@Override
		protected ParseResult parseUriPath(String uriPath) {
			return unix.parseUriPath(uriPath);
		}
	}
}

@cpovirk cpovirk added type=enhancement Make an existing feature better P4 labels Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P4 type=enhancement Make an existing feature better
Projects
None yet
Development

No branches or pull requests

2 participants