-
CSR
-
Resolution: Approved
-
P5
-
None
-
behavioral
-
minimal
-
New option is added, but behavior is not changed if not specified.
-
add/remove/modify command line option
-
JDK
Summary
Add an option to the jar
tool to not overwriting existing file on extraction.
Problem
The JDK ships with the jar
tool which can be used to extract the contents of a jar file. In its current form, the jar
tool extracts the contents to the target directory, and always overwrite existing files. There are situations where user may want to prevent overwriting, in particular, if a malformed jar file crafted to append a jar file entry with the same name to overwrite existing file, prevent overwriting could be used as a defensive mechanism.
Solution
An optional -k
or --keep-old-files
option will be allowed to be used with the -x
main operation. The -k
option will prevent jar extraction to overwrite the existing file.
Specification
Optional -k
option is added to the -x main operation of the jar tool. With -k option, if an entry with the same name already exist on the target directory, then the existing file will not be overwritten. Otherwise, the file is replaced with the extracted copy, which is current behavior.
This command extracts the test.jar
file to the current working directory, create the directory if necessary. This is current operation mode of jar.
$ jar xvf test.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
inflated: testfile1
inflated: testfile2
This command extracts the test.jar
file again to the current working directory, and keep current existing files.
$ jar xvkf test.jar
created: META-INF/
skipped: META-INF/MANIFEST.MF exists
skipped: testfile1 exists
skipped: testfile2 exists
Same as above, but if we remove the testfile1 and testfile2 from the current working directory, the command output will be
$ jar xvkf test.jar
created: META-INF/
skipped: META-INF/MANIFEST.MF exists
inflated: testfile1
inflated: testfile2
Now if we remove META-INF folder, and use the gnu style options,
$ jar -x --keep-old-files -v -f test.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
skipped: testfile1 exists
skipped: testfile2 exists
jar --help
has been updated to update the -x
option description and introduce -k, --keep-old-files
option in the help text:
-x, --extract Extract named (or all) files from the archive.
If a file with the same name appears more than once in
the archive, each copy will be extracted, with later copies
overwriting (replacing) earlier copies unless -k is specified.
...
Operation modifiers valid only in extract mode:
-k, --keep-old-files Do not overwrite existing files.
If a file with the same name exists in the target
directory, skip the extraction of that file.
As a result, if a file appears more than once in an
archive, later copies will not overwrite earlier copies.
Also note that some file system can be case insensitive.
man page updates for jar
tool follows:
diff --git a/closed/src/jdk.jartool/share/man/jar.md b/closed/src/jdk.jartool/share/man/jar.md
index 8c20e1f920..3e0d17ced4 100644
--- a/closed/src/jdk.jartool/share/man/jar.md
+++ b/closed/src/jdk.jartool/share/man/jar.md
@@ -1,5 +1,5 @@
---
-# Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
#
title: 'JAR(1) JDK @@VERSION_SHORT@@ | JDK Commands'
@@ -80,6 +80,9 @@ argument is the first argument specified on the command line.
`-x` or `--extract`
: Extracts the named (or all) files from the archive.
+ If a file with the same name appears more than once in
+ the archive, each copy will be extracted, with later copies
+ overwriting (replacing) earlier copies unless -k is specified.
`-d` or `--describe-module`
: Prints the module descriptor or automatic module name.
@@ -159,6 +162,16 @@ or `--create`) the update (`-u` or `--update` ) and the generate-index (`-i` or
format, to use for the timestamp of the entries,
e.g. "2022-02-12T12:30:00-05:00".
+## Operation Modifiers Valid Only in Extract Mode
+
+`-k` or `--keep-old-files`
+: Do not overwrite existing files.
+ If a Jar file entry with the same name exists in the target directory, the
+ existing file will not be overwritten.
+ As a result, if a file appears more than once in an archive, later copies will not overwrite
+ earlier copies.
+ Also note that some file system can be case insensitive.
+
## Other Options
The following options are recognized by the `jar` command and not used with
- csr of
-
JDK-8335912 Add an operation mode to the jar command when extracting to not overwriting existing files
- Resolved