public static synchronized Path extractResourceToDirectory()

in jvmti-access/src/main/java/co/elastic/otel/ResourceExtractionUtil.java [62:125]


  public static synchronized Path extractResourceToDirectory(
      String resource, String prefix, String suffix, Path directory) {
    try (InputStream resourceStream =
        ResourceExtractionUtil.class.getResourceAsStream("/" + resource)) {
      if (resourceStream == null) {
        throw new IllegalStateException(resource + " not found");
      }
      UserPrincipal currentUserPrincipal = getCurrentUserPrincipal();
      // we have to include current user name as multiple copies of the same agent could be attached
      // to multiple JVMs, each running under a different user. Hashing makes the name
      // path-friendly.
      String userHash = hash(currentUserPrincipal.getName());
      // to guard against re-using previous versions
      String resourceHash = hash(ResourceExtractionUtil.class.getResourceAsStream("/" + resource));

      Path tempFile =
          directory.resolve(
              prefix
                  + "-"
                  + userHash.substring(0, 32)
                  + "-"
                  + resourceHash.substring(0, 32)
                  + suffix);
      try {
        FileAttribute<?>[] attr;
        if (tempFile.getFileSystem().supportedFileAttributeViews().contains("posix")) {
          attr =
              new FileAttribute[] {
                PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_WRITE, OWNER_READ))
              };
        } else {
          attr = new FileAttribute[0];
        }
        try (FileChannel channel =
            FileChannel.open(tempFile, EnumSet.of(CREATE_NEW, WRITE), attr)) {
          // make other JVM instances wait until fully written
          try (FileLock writeLock = channel.lock()) {
            channel.transferFrom(Channels.newChannel(resourceStream), 0, Long.MAX_VALUE);
          }
        }
      } catch (FileAlreadyExistsException e) {
        try (FileChannel channel = FileChannel.open(tempFile, READ, NOFOLLOW_LINKS)) {
          // wait until other JVM instances have fully written the file
          // multiple JVMs can read the file at the same time
          try (FileLock readLock = channel.lock(0, Long.MAX_VALUE, true)) {
            if (!hash(Files.newInputStream(tempFile)).equals(resourceHash)) {
              throw new IllegalStateException(
                  "Invalid checksum of " + tempFile + ". Please delete this file.");
            } else if (!Files.getOwner(tempFile).equals(currentUserPrincipal)) {
              throw new IllegalStateException(
                  "File "
                      + tempFile
                      + " is not owned by '"
                      + currentUserPrincipal.getName()
                      + "'. Please delete this file.");
            }
          }
        }
      }
      return tempFile.toAbsolutePath();
    } catch (NoSuchAlgorithmException | IOException e) {
      throw new IllegalStateException(e);
    }
  }