Skip to content

Commit

Permalink
8324635: (zipfs) Regression in Files.setPosixFilePermissions called o…
Browse files Browse the repository at this point in the history
…n existing MSDOS entries

Reviewed-by: lancea
  • Loading branch information
Eirik Bjørsnøs committed Feb 2, 2024
1 parent 7476e29 commit a18b03b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ void setPermissions(byte[] path, Set<PosixFilePermission> perms) throws IOExcept
}
if (perms == null) {
e.posixPerms = -1;
} else if (e.posixPerms == -1) {
e.posixPerms = ZipUtils.permsToFlags(perms);
} else {
e.posixPerms = ZipUtils.permsToFlags(perms) |
(e.posixPerms & 0xFE00); // Preserve unrelated bits
Expand Down
51 changes: 49 additions & 2 deletions test/jdk/jdk/nio/zipfs/TestPosix.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

/**
* @test
* @bug 8213031 8273935
* @bug 8213031 8273935 8324635
* @summary Test POSIX ZIP file operations.
* @modules jdk.zipfs
* jdk.jartool
Expand Down Expand Up @@ -719,7 +719,7 @@ public void testJarFile() throws IOException {
}

/**
* Verify that calling Files.setPosixPermissions with the current
* Verify that calling Files.setPosixFilePermissions with the current
* permission set does not change the 'external file attributes' field.
*
* @throws IOException if an unexpected IOException occurs
Expand All @@ -733,6 +733,53 @@ public void setPermissionsShouldPreserveRemainingBits() throws IOException {
});
}

/**
* Verify that calling Files.setPosixFilePermissions on an MS-DOS entry
* results in only the expected permission bits being set
*
* @throws IOException if an unexpected IOException occurs
*/
@Test
public void setPermissionsShouldConvertToUnix() throws IOException {
// The default environment creates MS-DOS entries, with zero 'external file attributes'
createEmptyZipFile(ZIP_FILE, ENV_DEFAULT);
try (FileSystem fs = FileSystems.newFileSystem(ZIP_FILE, ENV_DEFAULT)) {
Path path = fs.getPath("hello.txt");
Files.createFile(path);
}
// The CEN header is now as follows:
//
// 004A CENTRAL HEADER #1 02014B50
// 004E Created Zip Spec 14 '2.0'
// 004F Created OS 00 'MS-DOS'
// 0050 Extract Zip Spec 14 '2.0'
// 0051 Extract OS 00 'MS-DOS'
// [...]
// 0070 Ext File Attributes 00000000

// Sanity check that all 'external file attributes' bits are all zero
verifyExternalFileAttribute(Files.readAllBytes(ZIP_FILE), "0");

// Convert to a UNIX entry by calling Files.setPosixFilePermissions
try (FileSystem fs = FileSystems.newFileSystem(ZIP_FILE, ENV_POSIX)) {
Path path = fs.getPath("hello.txt");
Files.setPosixFilePermissions(path, EnumSet.of(OWNER_READ));
}

// The CEN header should now be as follows:
//
// 004A CENTRAL HEADER #1 02014B50
// 004E Created Zip Spec 14 '2.0'
// 004F Created OS 03 'Unix'
// 0050 Extract Zip Spec 14 '2.0'
// 0051 Extract OS 00 'MS-DOS'
// [...]
// 0070 Ext File Attributes 01000000

// The first of the nine trailing permission bits should be set
verifyExternalFileAttribute(Files.readAllBytes(ZIP_FILE), "100000000");
}

/**
* Verify that a non-POSIX operation such as Files.setLastModifiedTime
* does not change the 'external file attributes' field.
Expand Down

0 comments on commit a18b03b

Please sign in to comment.