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-codec.git
commit f4c34673afb6de753b5fc83c10f958b03e84a1db Author: Gary Gregory <[email protected]> AuthorDate: Sat Nov 21 11:12:11 2026 -0500 Clarify GitIdentifiers file modes and extend regression coverage Document owner-execute handling, the non-POSIX fallback, and differences from Git's indexed modes. Clarify that TreeIdBuilder uses explicit modes. Add portable regression tests that distinguish execution access from POSIX owner-execute permissions without changing host permissions. --- .../commons/codec/digest/GitIdentifiers.java | 24 +- .../codec/digest/GitIdentifiersFileSystemTest.java | 349 +++++++++++++++++++++ .../commons/codec/digest/GitIdentifiersTest.java | 6 +- 3 files changed, 368 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java b/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java index f323f75d..8a3c800a 100644 --- a/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java +++ b/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java @@ -54,9 +54,8 @@ import java.util.function.Supplier; * </p> * * <p> - * The executable bit of a file is recorded only when the file system exposes POSIX permissions. File systems without them, - * such as the default Windows file system, record every non-directory, non-symbolic-link entry as a regular file, matching - * Git's behavior with its default {@code core.fileMode=false}. + * {@link #treeId(MessageDigest, Path)} derives file modes from file system attributes without consulting Git's index or configuration. See that method for + * details. {@link TreeIdBuilder} uses the file modes supplied by the caller, independently of the file system. * </p> * * <p> @@ -224,11 +223,8 @@ public class GitIdentifiers { SYMBOLIC_LINK(new byte[] { '1', '2', '0', '0', '0', '0' }); private static FileMode get(final Path path) throws IOException { - // Read one attribute set, preferring POSIX where the file system provides it. The basic view always exists and - // classifies links and directories; only POSIX permissions carry the owner-execute bit that Git records. Git - // records the executable bit only where the file system exposes POSIX permissions; elsewhere, for example with - // Git for Windows and its default core.fileMode=false, regular files are stored as 100644 even though Windows - // reports every readable file as executable. + // Prefer POSIX attributes to read the owner-execute bit Git records. Files.isExecutable tests the current process's access instead. + // Without POSIX permissions, use the basic attributes and default files to REGULAR. No Git index or configuration is consulted. BasicFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); if (view == null) { view = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); @@ -525,6 +521,18 @@ public class GitIdentifiers { * When the hash algorithm is SHA-1, the identifier is identical to Git tree identifier and SWHID directory identifier. * </p> * + * <p> + * Symbolic links and directories are classified without following links. Other entries use mode {@code 100755} only when POSIX permissions are available + * and {@link PosixFilePermission#OWNER_EXECUTE} is set; otherwise they use mode {@code 100644}. Execution access granted or denied to the current process, + * for example through an access control list, does not determine the recorded mode. + * </p> + * + * <p> + * Git's index and configuration are not consulted. On file systems without POSIX permissions, the regular-file fallback matches Git's treatment of newly + * added files with {@code core.fileMode=false}. Git can preserve executable modes already stored in its index, so this method may produce a different + * identifier from Git for an existing checkout. Use {@link TreeIdBuilder} to supply modes explicitly when the file system does not retain them. + * </p> + * * @param messageDigest The MessageDigest to use (for example SHA-1). * @param data Path to the directory to digest. * @return A generalized Git tree identifier. diff --git a/src/test/java/org/apache/commons/codec/digest/GitIdentifiersFileSystemTest.java b/src/test/java/org/apache/commons/codec/digest/GitIdentifiersFileSystemTest.java new file mode 100644 index 00000000..2b6eaf11 --- /dev/null +++ b/src/test/java/org/apache/commons/codec/digest/GitIdentifiersFileSystemTest.java @@ -0,0 +1,349 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.codec.digest; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Proxy; +import java.net.URI; +import java.nio.channels.SeekableByteChannel; +import java.nio.file.AccessDeniedException; +import java.nio.file.AccessMode; +import java.nio.file.CopyOption; +import java.nio.file.DirectoryStream; +import java.nio.file.FileStore; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.Paths; +import java.nio.file.WatchService; +import java.nio.file.attribute.BasicFileAttributeView; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.FileAttributeView; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.PosixFileAttributes; +import java.nio.file.attribute.PosixFilePermissions; +import java.nio.file.attribute.UserPrincipalLookupService; +import java.nio.file.spi.FileSystemProvider; +import java.security.MessageDigest; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * Tests file modes independently of the host file system's permissions and execution access. + */ +class GitIdentifiersFileSystemTest { + + /** + * A single-file tree exposing only the operations needed to compute its identifier. Proxies implement the small subset of the Path and attribute + * interfaces used by the tests; unsupported operations fail explicitly. No files or permissions on the host are changed. + */ + private static final class TestFileSystem extends FileSystem { + + private final TestFileSystemProvider provider; + private boolean open = true; + + TestFileSystem(final String permissions, final boolean executable) { + provider = new TestFileSystemProvider(this, permissions, executable); + } + + @Override + public void close() { + open = false; + } + + @Override + public Iterable<FileStore> getFileStores() { + return Collections.emptyList(); + } + + @Override + public Path getPath(final String first, final String... more) { + final Path name = Paths.get(first, more); + return (Path) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Path.class }, (proxy, method, args) -> { + switch (method.getName()) { + case "getFileSystem": + return this; + case "getFileName": + return name.getFileName(); + case "toString": + return name.toString(); + default: + throw new UnsupportedOperationException(method.getName()); + } + }); + } + + @Override + public PathMatcher getPathMatcher(final String syntaxAndPattern) { + throw new UnsupportedOperationException(); + } + + @Override + public Iterable<Path> getRootDirectories() { + return Collections.singleton(getPath("root")); + } + + @Override + public String getSeparator() { + return "/"; + } + + @Override + public UserPrincipalLookupService getUserPrincipalLookupService() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isOpen() { + return open; + } + + @Override + public boolean isReadOnly() { + return true; + } + + @Override + public WatchService newWatchService() { + throw new UnsupportedOperationException(); + } + + @Override + public FileSystemProvider provider() { + return provider; + } + + @Override + public Set<String> supportedFileAttributeViews() { + return provider.permissions == null ? Collections.singleton("basic") : new HashSet<>(Arrays.asList("basic", "posix")); + } + } + + private static final class TestFileSystemProvider extends FileSystemProvider { + + private final TestFileSystem fileSystem; + private final String permissions; + private final boolean executable; + private final BasicFileAttributes attributes; + + TestFileSystemProvider(final TestFileSystem fileSystem, final String permissions, final boolean executable) { + this.fileSystem = fileSystem; + this.permissions = permissions; + this.executable = executable; + final Class<?> attributeType = permissions == null ? BasicFileAttributes.class : PosixFileAttributes.class; + attributes = (BasicFileAttributes) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { attributeType }, + (proxy, method, args) -> { + switch (method.getName()) { + case "isDirectory": + case "isSymbolicLink": + case "isOther": + return false; + case "isRegularFile": + return true; + case "permissions": + return PosixFilePermissions.fromString(permissions); + case "size": + return (long) CONTENT.length; + default: + throw new UnsupportedOperationException(method.getName()); + } + }); + } + + @Override + public void checkAccess(final Path path, final AccessMode... modes) throws IOException { + for (final AccessMode mode : modes) { + if (mode == AccessMode.WRITE || mode == AccessMode.EXECUTE && !executable) { + throw new AccessDeniedException(path.toString()); + } + } + } + + @Override + public void copy(final Path source, final Path target, final CopyOption... options) { + throw new UnsupportedOperationException(); + } + + @Override + public void createDirectory(final Path dir, final FileAttribute<?>... attrs) { + throw new UnsupportedOperationException(); + } + + @Override + public void delete(final Path path) { + throw new UnsupportedOperationException(); + } + + @Override + public <V extends FileAttributeView> V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) { + if (type != BasicFileAttributeView.class && (type != PosixFileAttributeView.class || permissions == null)) { + return null; + } + return type.cast(Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { type }, (proxy, method, args) -> { + if ("readAttributes".equals(method.getName())) { + return attributes; + } + throw new UnsupportedOperationException(method.getName()); + })); + } + + @Override + public FileStore getFileStore(final Path path) { + throw new UnsupportedOperationException(); + } + + @Override + public FileSystem getFileSystem(final URI uri) { + return fileSystem; + } + + @Override + public Path getPath(final URI uri) { + return fileSystem.getPath(uri.getPath()); + } + + @Override + public String getScheme() { + return "git-identifiers-test"; + } + + @Override + public boolean isHidden(final Path path) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isSameFile(final Path path, final Path path2) { + throw new UnsupportedOperationException(); + } + + @Override + public void move(final Path source, final Path target, final CopyOption... options) { + throw new UnsupportedOperationException(); + } + + @Override + public SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) { + throw new UnsupportedOperationException(); + } + + @Override + public DirectoryStream<Path> newDirectoryStream(final Path dir, final DirectoryStream.Filter<? super Path> filter) throws IOException { + final Path file = fileSystem.getPath("file.txt"); + final Set<Path> entries = filter.accept(file) ? Collections.singleton(file) : Collections.emptySet(); + return new DirectoryStream<Path>() { + @Override + public void close() { + // No resources to release. + } + + @Override + public Iterator<Path> iterator() { + return entries.iterator(); + } + }; + } + + @Override + public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream newInputStream(final Path path, final OpenOption... options) { + return new ByteArrayInputStream(CONTENT); + } + + @Override + public <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) { + return type.cast(attributes); + } + + @Override + public Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options) { + throw new UnsupportedOperationException(); + } + + @Override + public void setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) { + throw new UnsupportedOperationException(); + } + } + + private static final byte[] CONTENT = { 'x' }; + + private static void assertTreeId(final FileSystem fileSystem, final GitIdentifiers.FileMode mode) throws IOException { + final MessageDigest md = DigestUtils.getSha1Digest(); + final GitIdentifiers.TreeIdBuilder builder = GitIdentifiers.treeIdBuilder(md); + builder.addFile(mode, "file.txt", CONTENT); + assertArrayEquals(builder.get(), GitIdentifiers.treeId(md, fileSystem.getPath("root"))); + } + + /** + * Execution access without POSIX attributes must not turn an ordinary file into an executable Git entry. + */ + @ParameterizedTest + @ValueSource(booleans = { false, true }) + void testTreeIdWithoutPosixPermissions(final boolean executable) throws IOException { + try (TestFileSystem fileSystem = new TestFileSystem(null, executable)) { + final Path file = fileSystem.getPath("file.txt"); + assertNull(Files.getFileAttributeView(file, PosixFileAttributeView.class)); + assertEquals(executable, Files.isExecutable(file)); + assertTreeId(fileSystem, GitIdentifiers.FileMode.REGULAR); + } + } + + /** + * Only the owner-execute bit determines the Git mode, even when group permissions or an ACL give the current process different execution access. + */ + @ParameterizedTest + @CsvSource({ + "rw-------, false, REGULAR", + "rw-------, true, REGULAR", + "rw-r-xr--, true, REGULAR", + "rw-r--r-x, true, REGULAR", + "rwx------, false, EXECUTABLE", + "rwx------, true, EXECUTABLE" + }) + void testTreeIdWithPosixPermissions(final String permissions, final boolean executable, final GitIdentifiers.FileMode mode) throws IOException { + try (TestFileSystem fileSystem = new TestFileSystem(permissions, executable)) { + final Path file = fileSystem.getPath("file.txt"); + assertEquals(PosixFilePermissions.fromString(permissions), Files.getPosixFilePermissions(file)); + assertEquals(executable, Files.isExecutable(file)); + assertTreeId(fileSystem, mode); + } + } +} diff --git a/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java b/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java index de30da06..17c98ba2 100644 --- a/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java +++ b/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java @@ -396,11 +396,11 @@ class GitIdentifiersTest { } /** - * A file must be recorded with mode {@code 100644} unless the file system reports a POSIX executable bit for it. + * A newly created file without the POSIX owner-execute bit is recorded with mode {@code 100644}. * * <p> - * {@link Files#isExecutable(Path)} alone is not enough: Windows reports every readable file as executable, which would encode ordinary files as - * {@code 100755}. Git on Windows defaults to {@code core.fileMode=false} and therefore stores regular files as {@code 100644}. + * On Windows, ordinary files can report execution access through {@link Files#isExecutable(Path)} without exposing POSIX permissions. Such files still + * use mode {@code 100644}, matching newly added files in Git with {@code core.fileMode=false}. * </p> */ @Test
