java.lang.Object
net.i2p.router.startup.WorkingDir

public class WorkingDirextends Object
Provides the working directory for i2p.

For the location, first try the system property i2p.dir.config Next try $HOME/.i2p on linux or %LOCALAPPDATA%\I2P on Windows.

If the dir exists, return it. Otherwise, attempt to create it, and copy files from the base directory. To successfully copy, the base install dir must be the system property i2p.dir.base or else must be in $CWD.

If I2P was run from the install directory in the past, and migrateOldData = true, copy the necessary data files (except i2psnark/) over to the new working directory.

Otherwise, just copy over a limited number of files over.

Do not ever copy or move the old i2psnark/ directory, as if the old and new locations are on different file systems, this could be quite slow.

Modify some files while copying, see methods below.

After migration, the router will run using the new directory. The wrapper, however, must be stopped and restarted from the new script - until then, it will continue to write to wrapper.log* in the old directory.

  • Field Details

    • PROP_BASE_DIR

      private static final String PROP_BASE_DIR
      Config key of the base directory.
      See Also:
    • PROP_WORKING_DIR

      private static final String PROP_WORKING_DIR
      Config key of the working directory.
      See Also:
    • WORKING_DIR_DEFAULT_WINDOWS

      private static final String WORKING_DIR_DEFAULT_WINDOWS
      Default working directory on Windows.
      See Also:
    • WORKING_DIR_DEFAULT_MAC

      private static final String WORKING_DIR_DEFAULT_MAC
      Default working directory on Mac.
      See Also:
    • WORKING_DIR_DEFAULT

      private static final String WORKING_DIR_DEFAULT
      Default working directory on other systems.
      See Also:
    • WORKING_DIR_DEFAULT_DAEMON

      private static final String WORKING_DIR_DEFAULT_DAEMON
      Default working directory in daemon mode.
      See Also:
    • PROP_WRAPPER_LOG

      private static final String PROP_WRAPPER_LOG
      We do a couple of things differently if this is the username.
      See Also:
    • DEFAULT_WRAPPER_LOG

      private static final String DEFAULT_WRAPPER_LOG
      Default wrapper log file name.
      See Also:
    • EEPSITE_TIMESTAMP

      private static final long EEPSITE_TIMESTAMP
      Feb 16 2006.
      See Also:
    • MIGRATE_BASE

      private static final String MIGRATE_BASE
      Files and directories from the base install to copy over. None of these should be included in i2pupdate.zip

      The user should not delete these in the old location, leave them as templates for new users.

      See Also:
  • Constructor Details

    • WorkingDir

      private WorkingDir()
      Not called, the class is only a collection of static methods.
  • Method Details

    • getWorkingDir

      public static String getWorkingDir(Properties envProps, boolean migrateOldConfig)
      Only call this once on router invocation. Caller should store the return value for future reference.

      This also redirects stdout and stderr to a wrapper.log file if there is no wrapper present, unless system property I2P_DISABLE_OUTPUT_OVERRIDE is set.

      Parameters:
      envProps - the launcher's environment properties, or null to use the system properties
      migrateOldConfig - whether to copy all data over from an existing install
      Returns:
      the working dir
    • isSetup

      private static boolean isSetup(File dir)
      Tests if dir has been set up as an I2P working directory.
      Returns false if a directory is empty, or contains nothing that is usually migrated from the base install. This allows to pre-install plugins before the first router start.
      Parameters:
      dir - the directory to check for files migrated from the base install
      Returns:
      true if already set up
    • setupSystemOut

      private static void setupSystemOut(String dir)
      Redirect stdout and stderr to a wrapper.log file if there is no wrapper, unless system property I2P_DISABLE_OUTPUT_OVERRIDE is set.

      If there is no -Dwrapper.log=/path/to/wrapper.log on the java command line to specify a log file, check for existence of wrapper.log in CWD, for backward compatibility in old installations (don't move it). Otherwise, use (system temp dir)/wrapper.log. Create if it doesn't exist, and append to it if it does. Put the location in the environment as an absolute path, so logs.jsp can find it.

      Parameters:
      dir - if null, use Java temp dir; System property wrapper.logfile overrides
    • migrate

      private static boolean migrate(String list, File olddir, File todir)
      Moves the listed files to the new directory.
      Parameters:
      list - the file names to move
      olddir - the directory to move from
      todir - the directory to move to
      Returns:
      true if the files moved
    • migrateClientsConfig

      private static boolean migrateClientsConfig(File olddir, File todir)
      Copy over the clients.config file with modifications.
      Parameters:
      olddir - the directory holding the unmodified copy
      todir - the directory to write the modified copy to
      Returns:
      true if copied, or if the file was not there to begin with; false on failure
    • migrateJettyXml

      static boolean migrateJettyXml(File olddir, File todir, String filename, String oldString, String newString)
      Copy over the jetty.xml file with modifications It was already copied over once in migrate(String, File, File), throw that out and do it again with modifications.
      Parameters:
      olddir - the directory holding the unmodified copy
      todir - the directory to write the modified copy to
      filename - the name of the file in both directories
      oldString - the text to search for in each line
      newString - the replacement for oldString
      Returns:
      true if the copy succeeded, false if the file could not be read or written
    • migrateFileXML

      static void migrateFileXML(File oldFile, File newFile, String oldString, String newString, String oldString2, String newString2) throws IOException
      Copy over a XML file with modifications. Will overwrite any existing newFile.
      Parameters:
      oldFile - the source XML file to read
      newFile - the destination file, overwritten if it already exists
      oldString - the literal text to search for on each line
      newString - the text that replaces it
      oldString2 - a second literal text to search for, or null to skip
      newString2 - the text that replaces it, ignored when oldString2 is null
      Throws:
      IOException - if the source cannot be read or the destination cannot be written
    • copy

      private static boolean copy(File src, File targetDir)
      Recursive copy a file or dir to a dir.
      Parameters:
      src - file or directory, need not exist
      targetDir - the directory to copy to, will be created if it doesn't exist
      Returns:
      true for success OR if src does not exist
    • copyFile

      static boolean copyFile(File src, File dst)
      Copy a file with secure permissions.
      Parameters:
      src - not a directory, must exist
      dst - not a directory, will be overwritten if existing, will be mode 600 (best effort; on non-POSIX filesystems such as Windows only the read-only attribute is affected)
      Returns:
      true if it was copied successfully
    • touchRecursive

      private static void touchRecursive(File target, long time)
      Recursive touch all files in a dir to a given time.
      Parameters:
      target - the directory or file to touch, must exist
      time - the modification time in milliseconds since the epoch