Record Class ServerFilePaths

java.lang.Object
java.lang.Record
eu.mrapik.minecraftservermcp.fs.ServerFilePaths
Record Components:
serverRoot - directory containing server.jar (or the parent of the world folder); typically getServer().getWorldContainer().getParentFile()

public record ServerFilePaths(File serverRoot) extends Record
Canonical Minecraft server root directory and helpers to resolve user-supplied relative paths safely.

resolveChecked(String) rejects path traversal: after normalizing, the resolved path must remain under the canonical server root (using File.getCanonicalFile() for the root and Path.normalize() for the combined path). A null return means the request path escapes the jail or is otherwise unusable.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ServerFilePaths(File serverRoot)
    Creates an instance of a ServerFilePaths record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    relativeToRoot(Path absolute)
    Computes a forward-slash path relative to the server root for policy checks and messages.
    resolveChecked(String relativePath)
    Resolves a path relative to the server root and ensures it stays inside that directory.
    Returns the value of the serverRoot record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ServerFilePaths

      public ServerFilePaths(File serverRoot)
      Creates an instance of a ServerFilePaths record class.
      Parameters:
      serverRoot - the value for the serverRoot record component
  • Method Details

    • resolveChecked

      public Path resolveChecked(String relativePath) throws IOException
      Resolves a path relative to the server root and ensures it stays inside that directory.

      Leading slashes are stripped so /foo and foo behave consistently. Backslashes are normalized to forward slashes before resolution. Symbolic links are not specially handled beyond what Path.normalize() provides; the final check uses startsWith(root) on normalized paths.

      Parameters:
      relativePath - user input relative to server root
      Returns:
      absolute normalized path under the server root, or null if outside the root
      Throws:
      IOException - if canonical path resolution fails
    • relativeToRoot

      public String relativeToRoot(Path absolute) throws IOException
      Computes a forward-slash path relative to the server root for policy checks and messages.
      Parameters:
      absolute - path that must already be under the canonical server root
      Returns:
      relative path using / separators
      Throws:
      IOException - if canonical root resolution fails
      IllegalArgumentException - if absolute is not under the server root
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • serverRoot

      public File serverRoot()
      Returns the value of the serverRoot record component.
      Returns:
      the value of the serverRoot record component