Repository: GoogleContainerTools/jib
File: jib-core/src/main/java/com/google/cloud/tools/jib/tar/TarStreamBuilder.java (Java)

Code before:

  /**
   * Adds a blob to the archive. Note that this should be used with raw bytes and not file contents;
   * for adding files to the archive, use {@link #addTarArchiveEntry}.
   *
   * @param contents the bytes to add to the tarball
   * @param name the name of the entry (i.e. filename)
   */
  public void addByteEntry(byte[] contents, String name) {
    TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(contents.length);
    archiveMap.put(entry, Blobs.from(outputStream -> outputStream.write(contents)));
  }

  /**
   * Adds a blob to the archive. Note that this should be used with non-file {@link Blob}s; for
   * adding files to the archive, use {@link #addTarArchiveEntry}.
   *
   * @param blob the {@link Blob} to add to the tarball
   * @param size the size (in bytes) of {@code blob}
   * @param name the name of the entry (i.e. filename)
   */
  public void addBlobEntry(Blob blob, long size, String name) {
    TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(size);
    entry.setModTime(DEFAULT_MODIFICATION_TIME.getEpochSecond());
    archiveMap.put(entry, blob);
  }
}


Review comment (refactor):
I realized that this isn't enough. The `addByteEntry()` above also needs to set the timestamp. Both `addBlobEntry()` and `addByteEntry()` are used in `ImageTarball` to create a tar image stream.

However, we have layers of abstraction in our codebase with a hierarchy, so it doesn't feel right to make `TarStreamBuilder` to get into the business of deciding the timestamp of tar entries. This is a general class to build a tar stream. What I think we should do is to change the signature of the method to `addBlobEntry(Blob blob, long size, String name, Instant modTime)`, and have `ImageTarball` pass in the time.

Code after:
  /**
   * Adds a blob to the archive. Note that this should be used with raw bytes and not file contents;
   * for adding files to the archive, use {@link #addTarArchiveEntry}.
   *
   * @param contents the bytes to add to the tarball
   * @param name the name of the entry (i.e. filename)
   * @param modificationTime the modification time of the entry
   */
  public void addByteEntry(byte[] contents, String name, Instant modificationTime) {
    TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(contents.length);
    entry.setModTime(modificationTime.toEpochMilli());
    archiveMap.put(entry, Blobs.from(outputStream -> outputStream.write(contents)));
  }

  /**
   * Adds a blob to the archive. Note that this should be used with non-file {@link Blob}s; for
   * adding files to the archive, use {@link #addTarArchiveEntry}.
   *
   * @param blob the {@link Blob} to add to the tarball
   * @param size the size (in bytes) of {@code blob}
   * @param name the name of the entry (i.e. filename)
   * @param modificationTime the modification time of the entry
   */
  public void addBlobEntry(Blob blob, long size, String name, Instant modificationTime) {
    TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(size);
    entry.setModTime(modificationTime.toEpochMilli());
    archiveMap.put(entry, blob);
  }
}