Exemplo n.º 1
0
 private static void mkdirHard(File f) {
   if (!f.mkdir()) {
     stderr.println("Cannot create directory: " + f);
     System.exit(1);
   }
 }
Exemplo n.º 2
0
  /** Return an unused directory in the execution pool directory. Set virtualExecDir */
  public static String createVirtualExecDir() {
    if (useStandardExecPoolDirStrategy) {
      // Assume we are in the run directory, so set the standard paths
      execPoolDir = new File(SysInfoUtils.getcwd(), "state/execs").toString();
      actualExecPoolDir =
          new File(SysInfoUtils.getcwd(), "state/hosts/" + SysInfoUtils.getShortHostName())
              .toString();
      if (!new File(actualExecPoolDir).isDirectory()) actualExecPoolDir = null;
    }
    if (!StrUtils.isEmpty(execPoolDir) && !new File(execPoolDir).isDirectory())
      throw Exceptions.bad("Execution pool directory '" + execPoolDir + "' doesn't exist");
    if (!StrUtils.isEmpty(actualExecPoolDir) && !new File(actualExecPoolDir).isDirectory())
      throw Exceptions.bad(
          "Actual execution pool directory '" + actualExecPoolDir + "' doesn't exist");

    if (!StrUtils.isEmpty(execDir)) { // Use specified execDir
      boolean exists = new File(execDir).isDirectory();
      if (exists && !overwriteExecDir)
        throw Exceptions.bad("Directory %s already exists and overwrite flag is false", execDir);
      if (!exists) mkdirHard(new File(execDir));
      else {
        // This part looks at actualExecPoolDir
        // This case is overwriting an existing execution directory, which
        // happens when we are executing a thunk.  We have to be careful here
        // because the actual symlinked directory that was created when thunking
        // might be using a different actualPoolDir.  If this happens, we need
        // to move the actual thunked symlinked directory into the actual
        // execution pool directory requested.  In fact, we always do this for simplicity.
        String oldActualExecDir = Utils.systemGetStringOutputEasy("readlink " + execDir);
        if (oldActualExecDir == null) { // Not symlink
          if (!StrUtils.isEmpty(actualExecPoolDir))
            throw Exceptions.bad(
                "The old execution directory was not created with actualExecPoolDir but now we want an actualExecPoolDir");
          // Do nothing, just use the directory as is
        } else { // Symlink
          oldActualExecDir = oldActualExecDir.trim();
          if (StrUtils.isEmpty(actualExecPoolDir))
            throw Exceptions.bad(
                "The old execution directory was created with actualExecPoolDir but now we don't want an actualExecPoolDir");
          // Note that now the execution numbers might not correspond between the
          // actual and virtual execution pool directories.
          File newActualExecDir = null;
          for (int i = 0; ; i++) {
            newActualExecDir = new File(actualExecPoolDir, i + "a.exec");
            if (!newActualExecDir.exists()) break;
          }
          // Move the old directory to the new directory
          Utils.systemHard(String.format("mv %s %s", oldActualExecDir, newActualExecDir));
          // Update the symlink (execDir -> newActualExecDir)
          Utils.systemHard(
              String.format("ln -sf %s %s", newActualExecDir.getAbsolutePath(), execDir));
        }
      }
      return virtualExecDir = execDir;
    }

    // execDir hasn't been specified, so we need to pick one from a pool directory
    // execPoolDir must exist; actualExecPoolDir is optional

    // Get a list of files that already exists
    Set<String> files = new HashSet<String>();
    for (String f : new File(execPoolDir).list()) files.add(f);

    // Go through and pick out a file that doesn't exist
    int numFailures = 0;
    for (int i = 0; numFailures < 3; i++) {
      // Either the virtual file (a link) or the actual file
      File f = new File(execPoolDir, i + ".exec");
      // Actual file
      File g =
          StrUtils.isEmpty(actualExecPoolDir) ? null : new File(actualExecPoolDir, i + ".exec");

      if (!files.contains(i + ".exec") && (g == null || !g.exists())) {
        if (g == null || g.equals(f)) {
          mkdirHard(f);
          return virtualExecDir = f.toString();
        }
        // Create symlink before mkdir to try to reserve the name and avoid race conditions
        if (Utils.createSymLink(g.getAbsolutePath(), f.getAbsolutePath())) {
          mkdirHard(g);
          return virtualExecDir = f.toString();
        }

        // Probably because someone else already linked to it
        // in the race condition: so try again
        stderr.println("Cannot create symlink from " + f + " to " + g);
        numFailures++;
      }
    }
    throw Exceptions.bad("Failed many times to create execution directory");
  }
Exemplo n.º 3
0
 public static void linkFileFromExec(String file, String realFileName) {
   if (StrUtils.isEmpty(realFileName) || StrUtils.isEmpty(file)) return;
   File f = new File(realFileName);
   Utils.createSymLink(getFile(file), f.getAbsolutePath());
 }