You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/08/10 22:11:11 UTC

(commons-compress) 05/10: CpioArchiveEntry now throws ArchiveException instead of IllegalArgumentException.

This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit fdc2aba7869091cd0ee03af173311c6604cbbd07
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Aug 10 17:58:11 2026 -0400

    CpioArchiveEntry now throws ArchiveException instead of
    IllegalArgumentException.
---
 src/changes/changes.xml                            |  1 +
 .../compress/archivers/cpio/CpioArchiveEntry.java  | 51 ++++++++++++----------
 .../archivers/cpio/CpioArchiveEntryTest.java       |  6 +--
 3 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e1485cf61..cccdaf14e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -132,6 +132,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" issue="COMPRESS-711" dev="pkarwasz" due-to="Piotr P. Karwasz">Fix checksum calculation in CpioArchiveInputStream when reading with a non-zero offset.</action>
       <action type="fix" issue="COMPRESS-718" dev="ggregory" due-to="Anay Wadhera, Gary Gregory">CpioArchiveEntry does not allow files over 4GB in OLD_ASCII format.</action>
       <action type="fix" dev="ggregory" due-to="Christopher Linke, Gary Gregory">Throw ArchiveException instead of EOFException when CPIO name size less than or equal to 0 #771.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">CpioArchiveEntry now throws ArchiveException instead of IllegalArgumentException.</action>
       <!-- FIX gzip -->
       <action type="fix" dev="ggregory" due-to="Gary Gregory">GzipParameters.setOperatingSystem(int) now throws CompressorException on illegal input.</action>
       <action type="fix" issue="COMPRESS-705" dev="ggregory" due-to="Mario Fredenhagen, Gary Gregory">GZip IOException: Extra subfield length exceeds remaining bytes in extra field; use new option GzipCompressorInputStream.Builder.setIgnoreExtraField(boolean).</action>
diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
index aede81125..00b10f9f5 100644
--- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
@@ -211,8 +211,9 @@ public class CpioArchiveEntry implements CpioConstants, ArchiveEntry {
      *
      * @param inputFile The file to gather information from.
      * @param entryName The name of this entry.
+     * @throws ArchiveException Thrown if the file type cannot be determined.
      */
-    public CpioArchiveEntry(final File inputFile, final String entryName) {
+    public CpioArchiveEntry(final File inputFile, final String entryName) throws ArchiveException {
         this(FORMAT_NEW, inputFile, entryName);
     }
 
@@ -235,15 +236,16 @@ public CpioArchiveEntry(final Path inputPath, final String entryName, final Link
      * @param format The cpio format for this entry.
      *               <p>
      *               Possible format values are:
-     *
+     *               </p>
      *               <pre>
      * CpioConstants.FORMAT_NEW
      * CpioConstants.FORMAT_NEW_CRC
      * CpioConstants.FORMAT_OLD_BINARY
      * CpioConstants.FORMAT_OLD_ASCII
      *               </pre>
+     * @throws ArchiveException Thrown if the format is not recognized.
      */
-    public CpioArchiveEntry(final short format) {
+    public CpioArchiveEntry(final short format) throws ArchiveException {
         switch (format) {
         case FORMAT_NEW:
             this.headerSize = 110;
@@ -262,7 +264,7 @@ public CpioArchiveEntry(final short format) {
             this.alignmentBoundary = 2;
             break;
         default:
-            throw new IllegalArgumentException("Unknown header type " + format);
+            throw new ArchiveException("Unknown header type " + format);
         }
         this.fileFormat = format;
     }
@@ -275,24 +277,24 @@ public CpioArchiveEntry(final short format) {
      * @param entryName The name of this entry.
      *                  <p>
      *                  Possible format values are:
-     *
+     *                  </p>
      *                  <pre>
      * CpioConstants.FORMAT_NEW
      * CpioConstants.FORMAT_NEW_CRC
      * CpioConstants.FORMAT_OLD_BINARY
      * CpioConstants.FORMAT_OLD_ASCII
      *                  </pre>
-     *
+     * @throws ArchiveException Thrown if the file type cannot be determined.
      * @since 1.1
      */
-    public CpioArchiveEntry(final short format, final File inputFile, final String entryName) {
+    public CpioArchiveEntry(final short format, final File inputFile, final String entryName) throws ArchiveException {
         this(format, entryName, inputFile.isFile() ? inputFile.length() : 0);
         if (inputFile.isDirectory()) {
             setMode(C_ISDIR);
         } else if (inputFile.isFile()) {
             setMode(C_ISREG);
         } else {
-            throw new IllegalArgumentException("Cannot determine type of file " + inputFile.getName());
+            throw new ArchiveException("Cannot determine type of file " + inputFile.getName());
         }
         // TODO set other fields as needed
         setTimeMillis(inputFile.lastModified());
@@ -306,16 +308,16 @@ public CpioArchiveEntry(final short format, final File inputFile, final String e
      * @param entryName The name of this entry.
      *                  <p>
      *                  Possible format values are:
-     *
+     *                  </p>
      *                  <pre>
      * CpioConstants.FORMAT_NEW
      * CpioConstants.FORMAT_NEW_CRC
      * CpioConstants.FORMAT_OLD_BINARY
      * CpioConstants.FORMAT_OLD_ASCII
      *                  </pre>
-     *
      * @param options   options indicating how symbolic links are handled.
      * @throws IOException if an I/O error occurs.
+     * @throws ArchiveException Thrown if the file type cannot be determined.
      * @since 1.21
      */
     public CpioArchiveEntry(final short format, final Path inputPath, final String entryName, final LinkOption... options) throws IOException {
@@ -325,7 +327,7 @@ public CpioArchiveEntry(final short format, final Path inputPath, final String e
         } else if (Files.isRegularFile(inputPath, options)) {
             setMode(C_ISREG);
         } else {
-            throw new IllegalArgumentException("Cannot determine type of file " + inputPath);
+            throw new ArchiveException("Cannot determine type of file " + inputPath);
         }
         // TODO set other fields as needed
         setTime(Files.getLastModifiedTime(inputPath, options));
@@ -338,17 +340,17 @@ public CpioArchiveEntry(final short format, final Path inputPath, final String e
      * @param name   The name of this entry.
      *               <p>
      *               Possible format values are:
-     *
+     *               </p>
      *               <pre>
      * CpioConstants.FORMAT_NEW
      * CpioConstants.FORMAT_NEW_CRC
      * CpioConstants.FORMAT_OLD_BINARY
      * CpioConstants.FORMAT_OLD_ASCII
      *               </pre>
-     *
+     * @throws ArchiveException Thrown if the format is not recognized.
      * @since 1.1
      */
-    public CpioArchiveEntry(final short format, final String name) {
+    public CpioArchiveEntry(final short format, final String name) throws ArchiveException {
         this(format);
         this.name = name;
     }
@@ -361,7 +363,7 @@ public CpioArchiveEntry(final short format, final String name) {
      * @param size   The size of this entry
      *               <p>
      *               Possible format values are:
-     *
+     *               </p>
      *               <pre>
      * CpioConstants.FORMAT_NEW
      * CpioConstants.FORMAT_NEW_CRC
@@ -369,9 +371,10 @@ public CpioArchiveEntry(final short format, final String name) {
      * CpioConstants.FORMAT_OLD_ASCII
      *               </pre>
      *
+     * @throws ArchiveException Thrown if the size is negative, exceeds the maximum size for the format, or the format is not recognized..
      * @since 1.1
      */
-    public CpioArchiveEntry(final short format, final String name, final long size) {
+    public CpioArchiveEntry(final short format, final String name, final long size) throws ArchiveException {
         this(format, name);
         setSize(size);
     }
@@ -380,8 +383,9 @@ public CpioArchiveEntry(final short format, final String name, final long size)
      * Creates a CpioArchiveEntry with a specified name. The format of this entry will be the new format.
      *
      * @param name The name of this entry.
+     * @throws ArchiveException Thrown if the {@link CpioConstants#FORMAT_NEW} is not recognized.
      */
-    public CpioArchiveEntry(final String name) {
+    public CpioArchiveEntry(final String name) throws ArchiveException {
         this(FORMAT_NEW, name);
     }
 
@@ -390,8 +394,9 @@ public CpioArchiveEntry(final String name) {
      *
      * @param name The name of this entry.
      * @param size The size of this entry.
+     * @throws ArchiveException Thrown if the size is negative or exceeds the maximum size for the format.
      */
-    public CpioArchiveEntry(final String name, final long size) {
+    public CpioArchiveEntry(final String name, final long size) throws ArchiveException {
         this(name);
         setSize(size);
     }
@@ -836,8 +841,9 @@ public void setInode(final long inode) {
      * Sets the mode of this entry (for example directory, regular file).
      *
      * @param mode The mode to set.
+     * @throws ArchiveException Thrown if the mode is not recognized.
      */
-    public void setMode(final long mode) {
+    public void setMode(final long mode) throws ArchiveException {
         final long maskedMode = mode & S_IFMT;
         switch ((int) maskedMode) {
         case C_ISDIR:
@@ -850,7 +856,7 @@ public void setMode(final long mode) {
         case C_ISNWK:
             break;
         default:
-            throw new IllegalArgumentException("Unknown mode. Full: " + Long.toHexString(mode) + " Masked: " + Long.toHexString(maskedMode));
+            throw new ArchiveException("Unknown mode. Full: " + Long.toHexString(mode) + " Masked: " + Long.toHexString(maskedMode));
         }
         this.mode = mode;
     }
@@ -910,12 +916,13 @@ public void setRemoteDeviceMin(final long rmin) {
      * Sets the file size.
      *
      * @param size The file size to set.
+     * @throws ArchiveException Thrown if the size is negative or exceeds the maximum size for the format.
      */
-    public void setSize(final long size) {
+    public void setSize(final long size) throws ArchiveException {
         // When using the OLD_ASCII format, files can be up to 8GiB in size.
         final long maxSize = this.fileFormat == FORMAT_OLD_ASCII ? 0x1FFFFFFFFL : 0xFFFFFFFFL;
         if (size < 0 || size > maxSize) {
-            throw new IllegalArgumentException("Invalid entry size <" + size + ">");
+            throw new ArchiveException("Invalid entry size <" + size + ">");
         }
         this.fileSize = size;
     }
diff --git a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntryTest.java b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntryTest.java
index c5311598c..2bbb86870 100644
--- a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntryTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntryTest.java
@@ -29,16 +29,16 @@
 class CpioArchiveEntryTest {
 
     @Test
-    void testCpioEntrySizeOldAsciiFormatOver4GiB() {
+    void testCpioEntrySizeOldAsciiFormatOver4GiB() throws ArchiveException {
         final CpioArchiveEntry entry = new CpioArchiveEntry(CpioConstants.FORMAT_OLD_ASCII);
         entry.setSize(0x1FFFFFFFFL);
     }
 
     @ParameterizedTest
     @ValueSource(shorts = {CpioConstants.FORMAT_NEW, CpioConstants.FORMAT_NEW_CRC, CpioConstants.FORMAT_OLD_BINARY})
-    void testCpioEntrySizeUnder4GiBNotOldAsciiFormat(final short format) {
+    void testCpioEntrySizeUnder4GiBNotOldAsciiFormat(final short format) throws ArchiveException {
         final CpioArchiveEntry entry = new CpioArchiveEntry(format);
-        assertThrows(IllegalArgumentException.class, () -> entry.setSize(0x1FFFFFFFFL));
+        assertThrows(ArchiveException.class, () -> entry.setSize(0x1FFFFFFFFL));
     }
 
     @Test