protected synchronized void installAndConfigureFiles() throws IOException, InterruptedException {
    onionProxyContext.installFiles();

    if (!setExecutable(onionProxyContext.getTorExecutableFile())) {
      throw new RuntimeException("could not make Tor executable.");
    }

    // We need to edit the config file to specify exactly where the cookie/geoip files should be
    // stored, on
    // Android this is always a fixed location relative to the configFiles which is why this extra
    // step
    // wasn't needed in Briar's Android code. But in Windows it ends up in the user's
    // AppData/Roaming. Rather
    // than track it down we just tell Tor where to put it.
    PrintWriter printWriter = null;
    try {
      printWriter =
          new PrintWriter(
              new BufferedWriter(new FileWriter(onionProxyContext.getTorrcFile(), true)));
      printWriter.println("CookieAuthFile " + onionProxyContext.getCookieFile().getAbsolutePath());
      // For some reason the GeoIP's location can only be given as a file name, not a path and it
      // has
      // to be in the data directory so we need to set both
      printWriter.println(
          "DataDirectory " + onionProxyContext.getWorkingDirectory().getAbsolutePath());
      printWriter.println("GeoIPFile " + onionProxyContext.getGeoIpFile().getName());
      printWriter.println("GeoIPv6File " + onionProxyContext.getGeoIpv6File().getName());
    } finally {
      if (printWriter != null) {
        printWriter.close();
      }
    }
  }
  /**
   * Installs all necessary files and starts the Tor OP in offline mode (e.g.
   * networkEnabled(false)). This would only be used if you wanted to start the Tor OP so that the
   * install and related is all done but aren't ready to actually connect it to the network.
   *
   * @return True if all files installed and Tor OP successfully started
   * @throws java.io.IOException - IO Exceptions
   * @throws java.lang.InterruptedException - If we are, well, interrupted
   */
  public synchronized boolean installAndStartTorOp() throws IOException, InterruptedException {
    // The Tor OP will die if it looses the connection to its socket so if there is no controlSocket
    // defined
    // then Tor is dead. This assumes, of course, that takeOwnership works and we can't end up with
    // Zombies.
    if (controlConnection != null) {
      LOG.info("Tor is already running");
      return true;
    }

    // The code below is why this method is synchronized, we don't want two instances of it running
    // at once
    // as the result would be a mess of screwed up files and connections.
    LOG.info("Tor is not running");

    installAndConfigureFiles();

    LOG.info("Starting Tor");
    File cookieFile = onionProxyContext.getCookieFile();
    if (cookieFile.getParentFile().exists() == false
        && cookieFile.getParentFile().mkdirs() == false) {
      throw new RuntimeException("Could not create cookieFile parent directory");
    }

    // The original code from Briar watches individual files, not a directory and Android's file
    // observer
    // won't work on files that don't exist. Rather than take 5 seconds to rewrite Briar's code I
    // instead
    // just make sure the file exists
    if (cookieFile.exists() == false && cookieFile.createNewFile() == false) {
      throw new RuntimeException("Could not create cookieFile");
    }

    File workingDirectory = onionProxyContext.getWorkingDirectory();
    // Watch for the auth cookie file being created/updated
    WriteObserver cookieObserver = onionProxyContext.generateWriteObserver(cookieFile);
    // Start a new Tor process
    String torPath = onionProxyContext.getTorExecutableFile().getAbsolutePath();
    String configPath = onionProxyContext.getTorrcFile().getAbsolutePath();
    String pid = onionProxyContext.getProcessId();
    String[] cmd = {torPath, "-f", configPath, OWNER, pid};
    String[] env = onionProxyContext.getEnvironmentArgsForExec();
    ProcessBuilder processBuilder = new ProcessBuilder(cmd);
    onionProxyContext.setEnvironmentArgsAndWorkingDirectoryForStart(processBuilder);
    Process torProcess = null;
    try {
      //            torProcess = Runtime.getRuntime().exec(cmd, env, workingDirectory);
      torProcess = processBuilder.start();
      CountDownLatch controlPortCountDownLatch = new CountDownLatch(1);
      eatStream(torProcess.getInputStream(), false, controlPortCountDownLatch);
      eatStream(torProcess.getErrorStream(), true, null);

      // On platforms other than Windows we run as a daemon and so we need to wait for the process
      // to detach
      // or exit. In the case of Windows the equivalent is running as a service and unfortunately
      // that requires
      // managing the service, such as turning it off or uninstalling it when it's time to move on.
      // Any number
      // of errors can prevent us from doing the cleanup and so we would leave the process running
      // around. Rather
      // than do that on Windows we just let the process run on the exec and hence don't look for an
      // exit code.
      // This does create a condition where the process has exited due to a problem but we should
      // hopefully
      // detect that when we try to use the control connection.
      if (OsData.getOsType() != OsData.OsType.Windows) {
        int exit = torProcess.waitFor();
        torProcess = null;
        if (exit != 0) {
          LOG.warn("Tor exited with value " + exit);
          return false;
        }
      }

      // Wait for the auth cookie file to be created/updated
      if (!cookieObserver.poll(COOKIE_TIMEOUT, MILLISECONDS)) {
        LOG.warn("Auth cookie not created");
        FileUtilities.listFilesToLog(workingDirectory);
        return false;
      }

      // Now we should be able to connect to the new process
      controlPortCountDownLatch.await();
      controlSocket = new Socket("127.0.0.1", control_port);

      // Open a control connection and authenticate using the cookie file
      TorControlConnection controlConnection = new TorControlConnection(controlSocket);
      controlConnection.authenticate(FileUtilities.read(cookieFile));
      // Tell Tor to exit when the control connection is closed
      controlConnection.takeOwnership();
      controlConnection.resetConf(Collections.singletonList(OWNER));
      // Register to receive events from the Tor process
      controlConnection.setEventHandler(new OnionProxyManagerEventHandler());
      controlConnection.setEvents(Arrays.asList(EVENTS));

      // We only set the class property once the connection is in a known good state
      this.controlConnection = controlConnection;
      return true;
    } catch (SecurityException e) {
      LOG.warn(e.toString(), e);
      return false;
    } catch (InterruptedException e) {
      LOG.warn("Interrupted while starting Tor", e);
      Thread.currentThread().interrupt();
      return false;
    } finally {
      if (controlConnection == null && torProcess != null) {
        // It's possible that something 'bad' could happen after we executed exec but before we
        // takeOwnership()
        // in which case the Tor OP will hang out as a zombie until this process is killed. This is
        // problematic
        // when we want to do things like
        torProcess.destroy();
      }
    }
  }