Class OsSupport

java.lang.Object
org.ek9lang.core.OsSupport
All Implemented Interfaces:
Serializable

public final class OsSupport extends Object implements Serializable
Operating System support and generic stuff for directories and files.
See Also:
  • Constructor Details

    • OsSupport

      public OsSupport()
    • OsSupport

      public OsSupport(boolean testStubMode)
      Used as and when you want to use a stub for testing. You don't always want to create or access the actual users home directory or current working directory.
  • Method Details

    • setTestSandboxId

      public static void setTestSandboxId(String sandboxId)
      Set the stub-directory sandbox id for the current thread. Test-infrastructure only (auto-registered StubDirectoryIsolationExtension); has no effect unless a later OsSupport(true) on this thread creates a stub directory.
    • clearTestSandboxId

      public static void clearTestSandboxId()
      Clear the stub-directory sandbox id for the current thread. Test-infrastructure only.
    • numberOfProcessors

      public static int numberOfProcessors()
    • isInStubMode

      public boolean isInStubMode()
      Is this configured to be in stub mode.
    • getPid

      public long getPid()
      Provides the current process id of this running application.
    • makeSubDirectoryIfNotExists

      public String makeSubDirectoryIfNotExists(File directory, String subDirectory)
    • makeDirectoryIfNotExists

      public void makeDirectoryIfNotExists(File directory)
      Create directory if it does not exist or exception if failed.

      Deliberately NOT synchronized. It used to be, and had to be: exists() followed by mkdirs() is a time-of-check-to-time-of-use race, and two threads creating the same parent could have one see mkdirs() return false and throw. The monitor closed that race by serialising every caller.

      That is a global monitor with a filesystem syscall inside its critical section, which mattered when the JVM back end called it once per construct - 51,956 times on a 200K-SLOC compile - from jvm.OutputFileAccess.createOutputFile. That call is gone: naming an output no longer creates a directory, and only FileConstructOutputSink creates one, once per file it actually writes. CODE_GENERATION_AGGREGATES is now parallel over constructs, so a monitor here WOULD have every worker queueing on it - which is precisely why it is not one.

      Files.createDirectories(Path, FileAttribute...) removes the race rather than guarding it: it is idempotent and, unlike File.mkdirs(), does not report failure when the directory already exists - so there is no check to be invalidated between test and use, and no need to exclude other threads.

    • getTempDirectory

      public String getTempDirectory()
      Get name of temporary directory.
    • getUsersHomeDirectory

      public String getUsersHomeDirectory()
      Get the users home directory.
    • getCurrentWorkingDirectory

      public String getCurrentWorkingDirectory()
      Get current working directory for this running process.
    • getNumberOfProcessors

      public int getNumberOfProcessors()
      How many CPU's/Core reported.
    • getFileNameWithoutPath

      public String getFileNameWithoutPath(String fileNameWithPath)
      Extract just the final part of a file name.
    • isFileReadable

      public boolean isFileReadable(String fileName)
    • isFileReadable

      public boolean isFileReadable(File file)
    • getFileContent

      public Optional<String> getFileContent(File file)
      Load up a file into a String. Option empty if not possible to load.
    • isDirectoryReadable

      public boolean isDirectoryReadable(String directoryName)
    • isDirectoryReadable

      public boolean isDirectoryReadable(File directory)
    • isDirectoryWritable

      public boolean isDirectoryWritable(String directoryName)
    • isDirectoryWritable

      public boolean isDirectoryWritable(File directory)
    • getFilesFromDirectories

      public List<File> getFilesFromDirectories(Collection<File> inDirectories, String fileSuffix)
      Get List of all the files in a set of directories with a particular suffix.
    • getDirectoriesInDirectory

      public List<File> getDirectoriesInDirectory(File inDirectory, String excludeStartingWith)
      Get files in a particular directory, but not if they start with a prefix.
    • getFilesRecursivelyFrom

      public List<File> getFilesRecursivelyFrom(File inDirectory, Glob searchCondition)
      Search down a directory structure matching a 'Glob".
    • getFilesRecursivelyFrom

      public List<File> getFilesRecursivelyFrom(File inDirectory)
      Get all files down a directory structure.
    • getFilesFromDirectory

      public Collection<File> getFilesFromDirectory(File inDirectory, String fileSuffix)
      Get all files in a directory with a specific suffix.
    • getAllSubdirectories

      public Collection<File> getAllSubdirectories(String directoryRoot)
      Get all subdirectories from a root directory.