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

java.nio.file.InvalidPathException when using the LocalFileSystem in Windows #84

Closed
bytefish opened this issue Jun 27, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@bytefish
Copy link

I am working with Windows 10 and JDK 14. When using the LocalFileSystem in the application I run into the following exception when parsing a Windows File path passed from the CLI to LocalFileSystem:

java.nio.file.InvalidPathException: Illegal char <:> at index 2: 

It happens when Path path = Paths.get(uri.getPath()); on Windows is called. I saw several similar bugs in the internet, and some fixed this by resolving the path like this: Paths.get(new File(uri).getAbsolutePath()), but I am unsure if it also works for Mac OS and Linux`.

That's why I am opening an issue instead of directly making a PR.

The LocalFileSystem then looks like this:

import com.baremaps.util.vfs.FileSystem;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class LocalFileSystem extends FileSystem {

    @Override
    public boolean accept(URI uri) {
        return uri.getHost() == null && uri.getPath() != null;
    }

    @Override
    public InputStream read(URI uri) throws IOException {
        Path path = getPathFromUri(uri);

        return Files.newInputStream(path);
    }

    @Override
    public byte[] readByteArray(URI uri) throws IOException {
        Path path = getPathFromUri(uri);

        return Files.readAllBytes(path);
    }

    @Override
    public OutputStream write(URI uri) throws IOException {
        Path path = getPathFromUri(uri);

        if (!Files.exists(path.getParent())) {
            Files.createDirectories(path.getParent());
        }
        return Files.newOutputStream(path);
    }

    @Override
    public void writeByteArray(URI uri, byte[] bytes) throws IOException {

        Path path = getPathFromUri(uri);

        if (!Files.exists(path.getParent())) {
            Files.createDirectories(path.getParent());
        }
        Files.write(path, bytes);
    }

    @Override
    public void delete(URI uri) throws IOException {
        Path path = getPathFromUri(uri);

        Files.deleteIfExists(path);
    }

    private Path getPathFromUri(URI uri) {
        return Paths.get(new File(uri).getAbsolutePath());
    }
}
@bchapuis
Copy link
Member

Thanks a lot for reporting this issue, I have not tested Baremaps on Windows yet.

Regarding the issue, the fix does not seem to work on linux and osx: the getAbsolutePath() call throws an Exception. For now, we may simply differentiate the environments until we find a solution that works on all operating systems. For instance, the following getPathFromUri method works on my side as well:

 private Path getPathFromUri(URI uri) {
    if (System.getProperty("os.name").toLowerCase().contains("win")) {
      return Paths.get(new File(uri).getAbsolutePath());
    } else {
      return Paths.get(uri.getPath());
    }
  }

We could then add the different build target to .github/workflows/maven.yml in order to ensure that the tests are executed on every operating systems.

  build-ubuntu:
    name: Build Project
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Build with Maven
        run: mvn -B package --file pom.xml
  build-macos:
    name: Build Project
    runs-on: macos-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Build with Maven
        run: mvn -B package --file pom.xml
  build-windows:
    name: Build Project
    runs-on: windows-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Build with Maven
        run: mvn -B package --file pom.xml

What do you think? If this approach suits you do not hesitate to create a PR with your changes.

@bchapuis bchapuis added the bug Something isn't working label Jun 27, 2020
@bytefish
Copy link
Author

I am lacking the time for further experiments with baremaps on Windows. @bchapuis Thanks for your support and valuable feedback. Once I have more time I will revisit it.

I am closing this issue.

@bchapuis
Copy link
Member

No problem, thank a lot your feedbacks, it really helps. If you don't mind, I will keep the issue open as not been fixed yet. Hopefully, I will eventually find time to improve support on windows.

@bchapuis bchapuis reopened this Jul 20, 2020
@veikkoeeva
Copy link

veikkoeeva commented Nov 27, 2020

I can at least try to run the latest and see what happens if someone has a second to instruct me. :) I downloaded https://github.com/baremaps/baremaps/releases/tag/v0.3.2 and decompressed it to C:\baremaps. I have the latest PostgreSQL with PostGIS and latest Java installed.

What should I do get it to run and generate vector tiles (e.g. generate vector tiles for the globe)? I see there is one file that has contents as

#!/bin/sh
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
java -cp "$DIR/../lib/*" com.baremaps.cli.Baremaps "$@"

but I'm not sure what should that be in Windows. Maybe @bytefish got this far (tagging just in case). :)

@bytefish
Copy link
Author

@veikkoeeva I am not much of a help here, sorry. I cloned the project and experimented with the CLI implementation to find out how to fix the error and probably make a PR. So I didn't use the CLI. @bchapuis can better help here.

@bchapuis
Copy link
Member

bchapuis commented Nov 28, 2020

@veikkoeeva The current issue is solved so I created another one to track the creation of the bat script (#140). Also, notice that the project is now built on windows with github actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants