Changeset 19637 in josm


Ignore:
Timestamp:
2026-10-06T08:57:52+02:00 (5 days ago)
Author:
stoecker
Message:

detect the UTF-8 filename issue in non-UTF contexts, patch by wangi, fix #24940

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/native/linux/latest/usr/bin/josm-latest

    r19430 r19637  
    5858    # shellcheck source=/etc/default/josm-latest
    5959    . "/etc/default/${JOSM_VERSION}"
     60fi
     61
     62# Java decodes file names with the character set of the locale, and -Dsun.jnu.encoding
     63# cannot override it. If the locale is C/POSIX, or cannot be set at all - typically a
     64# UTF-8 LANG whose locale is not installed, as in a Flatpak sandbox - file names are
     65# decoded as ASCII and opening a folder that holds a non-ASCII name fails with
     66# InvalidPathException. Fall back to C.UTF-8 then, which is built into glibc 2.35 and
     67# later. This does not change the language of JOSM: when the locale cannot be set, Java
     68# already falls back to English. LC_ALL is needed, as setlocale() fails as a whole if
     69# any category names a locale that is not installed.
     70if { [ -n "$(locale 2>&1 >/dev/null)" ] || [ "$(locale charmap 2>/dev/null)" = "ANSI_X3.4-1968" ]; } \
     71        && [ -z "$(LC_ALL=C.UTF-8 locale 2>&1 >/dev/null)" ]; then
     72    export LC_ALL=C.UTF-8
    6073fi
    6174
  • trunk/native/linux/tested/usr/bin/josm

    r19430 r19637  
    5858    # shellcheck source=/etc/default/josm
    5959    . "/etc/default/${JOSM_VERSION}"
     60fi
     61
     62# Java decodes file names with the character set of the locale, and -Dsun.jnu.encoding
     63# cannot override it. If the locale is C/POSIX, or cannot be set at all - typically a
     64# UTF-8 LANG whose locale is not installed, as in a Flatpak sandbox - file names are
     65# decoded as ASCII and opening a folder that holds a non-ASCII name fails with
     66# InvalidPathException. Fall back to C.UTF-8 then, which is built into glibc 2.35 and
     67# later. This does not change the language of JOSM: when the locale cannot be set, Java
     68# already falls back to English. LC_ALL is needed, as setlocale() fails as a whole if
     69# any category names a locale that is not installed.
     70if { [ -n "$(locale 2>&1 >/dev/null)" ] || [ "$(locale charmap 2>/dev/null)" = "ANSI_X3.4-1968" ]; } \
     71        && [ -z "$(LC_ALL=C.UTF-8 locale 2>&1 >/dev/null)" ]; then
     72    export LC_ALL=C.UTF-8
    6073fi
    6174
  • trunk/src/org/openstreetmap/josm/gui/bugreport/BugReportDialog.java

    r17392 r19637  
    3232import org.openstreetmap.josm.tools.ImageProvider;
    3333import org.openstreetmap.josm.tools.InputMapUtils;
     34import org.openstreetmap.josm.tools.Utils;
    3435import org.openstreetmap.josm.tools.bugreport.BugReport;
    3536import org.openstreetmap.josm.tools.bugreport.BugReportQueue.SuppressionMode;
    … …  
    4647public class BugReportDialog extends JDialog {
    4748    private static final int MAX_MESSAGE_SIZE = 500;
     49    /** whether the user has been told about file names that cannot be represented, which is done once per session */
     50    private static volatile boolean unmappableFileNameExplained;
    4851    // This is explicitly not an ExtendedDialog - we still want to be able to display bug reports if there are problems with preferences/..
    4952    private final JPanel content = new JPanel(new GridBagLayout());
    … …  
    229232                    );
    230233            return SuppressionMode.NONE;
     234        } else if (e.isUnmappableFileName()) {
     235            // Not a bug in JOSM, but a locale which cannot represent the name of a file, see #14596. Explain how to
     236            // fix it rather than asking for a bug report, and only once: browsing other folders throws it again.
     237            if (!unmappableFileNameExplained) {
     238                unmappableFileNameExplained = true;
     239                GuiHelper.runInEDTAndWait(() -> JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
     240                        tr("<html>JOSM cannot handle the name of a file, because it is running with the character set {0} "
     241                                + "for file names, which cannot represent it.<br>"
     242                                + "This happens when JOSM is started without a UTF-8 locale, for example with {1}, "
     243                                + "or with a locale which is not installed.<br><br>"
     244                                + "Please start JOSM with the environment variable {2}.</html>",
     245                                Utils.getSystemProperty("sun.jnu.encoding"), "<tt>LANG=C</tt>", "<tt>LC_ALL=C.UTF-8</tt>"),
     246                        tr("Unsupported file name"),
     247                        JOptionPane.WARNING_MESSAGE));
     248            }
     249            return SuppressionMode.NONE;
    231250        } else {
    232251            PluginDownloadTask downloadTask = PluginHandler.updateOrdisablePluginAfterException(e);
  • trunk/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java

    r19535 r19637  
    55import java.io.Serializable;
    66import java.lang.reflect.InvocationTargetException;
     7import java.nio.file.InvalidPathException;
    78import java.util.ArrayList;
    89import java.util.Arrays;
    … …  
    2223import org.openstreetmap.josm.tools.Logging;
    2324import org.openstreetmap.josm.tools.StreamUtils;
     25import org.openstreetmap.josm.tools.Utils;
    2426
    2527/**
    … …  
    279281
    280282    /**
     283     * Check if this is caused by the name of a file which cannot be represented in the character set Java uses for
     284     * file names. Java takes that character set from the locale, so this happens when JOSM runs in the C/POSIX locale,
     285     * or with a locale which is not installed: file names are then ASCII only, and opening a folder which holds a
     286     * non-ASCII name fails. See #14596.
     287     * @return <code>true</code> if it is.
     288     * @since 19637
     289     */
     290    public boolean isUnmappableFileName() {
     291        return !"UTF-8".equalsIgnoreCase(Utils.getSystemProperty("sun.jnu.encoding"))
     292                && StreamUtils.toStream(CauseTraceIterator::new).anyMatch(t -> t instanceof InvalidPathException
     293                        && String.valueOf(((InvalidPathException) t).getReason()).contains("unmappable characters"));
     294    }
     295
     296    /**
    281297     * Iterates over the causes for this exception. Ignores cycles and aborts iteration then.
    282298     * @author Michal Zangl
  • trunk/test/unit/org/openstreetmap/josm/tools/bugreport/ReportedExceptionTest.java

    r17275 r19637  
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertFalse;
     6import static org.junit.jupiter.api.Assertions.assertTrue;
    57
     8import java.nio.file.InvalidPathException;
    69import java.util.Arrays;
    710
    … …  
    6265
    6366    /**
     67     * Tests {@link ReportedException#isUnmappableFileName()}: a file name which the character set used for file names
     68     * cannot represent is recognised, also as the cause of another exception, but only if that character set is not
     69     * UTF-8 - otherwise the locale is not to blame. See #14596.
     70     */
     71    @Test
     72    void testIsUnmappableFileName() {
     73        // the exception sun.nio.fs.UnixPath throws for "Capture d'écran.png" when the locale is C or not installed
     74        InvalidPathException unmappable = new InvalidPathException("/home/user/Capture d'\u00e9cran.png",
     75                "Malformed input or input contains unmappable characters");
     76        String jnuEncoding = System.getProperty("sun.jnu.encoding");
     77        try {
     78            System.setProperty("sun.jnu.encoding", "ANSI_X3.4-1968");
     79            assertTrue(new ReportedException(unmappable).isUnmappableFileName());
     80            assertTrue(new ReportedException(new IllegalStateException(unmappable)).isUnmappableFileName());
     81            assertFalse(new ReportedException(new InvalidPathException("a\0b", "Nul character not allowed")).isUnmappableFileName());
     82            assertFalse(new ReportedException(new IllegalStateException()).isUnmappableFileName());
     83
     84            System.setProperty("sun.jnu.encoding", "UTF-8");
     85            assertFalse(new ReportedException(unmappable).isUnmappableFileName());
     86        } finally {
     87            System.setProperty("sun.jnu.encoding", jnuEncoding);
     88        }
     89    }
     90
     91    /**
    6492     * Tests that {@link ReportedException#isSame(ReportedException)} works as expected.
    6593     */
Note: See TracChangeset for help on using the changeset viewer.