Skip to content

Normalize filename before storing #1027

Closed

Description

@tewe

Playback position is saved in ~/.mpv/watch_later/* where * is the MD5 of the filename as it was passed to mpv. This is a problem if you launch mpv in different ways, that may use different representations of the same unicode filename.

For instance, dragging a japanese file from the Finder into a Terminal window on the Mac uses a different normal form than copypasting the filename, so playback will not resume.

The solution is converting all filenames to the same normal form (no matter which) before calculating the hash.

Activity

  1. tewe commented on Aug 21, 2014

    @tewe
    Author

    I'm only talking about unicode, not relative paths. I don't see why the problem would be restricted to the Mac, but don't have other platforms to test on.

    If you didn't like the Finder example, think about storing paths in a database. You're almost guaranteed to get a different unicode representation out than what you put in.

  2. ghost added on Aug 21, 2014
  3. tewe commented on Aug 21, 2014

    @tewe
    Author

    It's the standard. As soon as you do any modifications to a filename you have to deal with it. I wouldn't be surprised if loading external subtitles had that bug as well, and aren't found despite the names looking identical.

    Luckily the fix is probably two lines, but might add a dependency on something other than iconv. Also this stuff is a bitch to test across platforms.

  4. ghost closed this as completedon Aug 21, 2014
  5. tewe commented on Aug 21, 2014

    @tewe
    Author

    POSIX only specifies paths as byte strings. On Linux you can configure per user how unicode names are stored. But I'm sure you know this, since you already know that the bug is in OS X.

  6. tewe commented on Aug 21, 2014

    @tewe
    Author

    Again, representations are part of the unicode standard. It's completely legal to use different ones. A user might even type different ones into the shell. You have to deal with that, or only support ASCII.

  7. qmega commented on Aug 21, 2014

    @qmega
    Contributor

    FWIW, I don't have any problems with Japanese filenames in terminal vs. file manager under KDE on Linux. Starting from either and resuming from the other works fine.

    If I explicitly convert one to decomposed form and pass that to mpv, it can't even find the file. Which kind of makes sense, because under POSIX with byte-string paths I could theoretically have two different files with different normalizations of the same unicode string.

    Also, it doesn't really make sense to allow any "K" form, as that for example converts "/" (completely legal in a filename) to "/", which would then point to a completely different path. That introduces ambiguity even if unicode is handled at the filesystem level.

  8. tewe commented on Aug 21, 2014

    @tewe
    Author

    I could theoretically have two different files with different normalizations of the same unicode string.

    That looks like a terrible idea. You could never type a filename, because you don't know where the existing file comes from, and if its form matches what your input method produces. But if it has to be supported...

  9. qmega commented on Aug 21, 2014

    @qmega
    Contributor

    Yes, of course it's a terrible idea; I'd never actually do it. But POSIX allows it, so POSIX-compatible filesystem APIs have to treat those strings as different. That particular case isn't "supported" on purpose, it just happens to be possible. A more realistic example would be a filename that isn't valid UTF-8 at all. I guess Apple's filesystems do require proper Unicode filenames, which would allow them to do conversion safely, but in doing that, they break POSIX.

  10. Pacoup commented on Aug 22, 2014

    @Pacoup

    @wm4 So, out curiosity, are you saying even if someone were to go out of their way and implement this code you wouldn't accept it?

  11. qmega commented on Feb 4, 2015

    @qmega
    Contributor

    Just had an idea: Why not do the conversion before calling mpv, so mpv doesn't have to care? Try adding something like this to your path before mpv:

    #!/usr/bin/env python
    
    MPV = '/path/to/real/mpv'
    
    from os import execv
    from sys import argv
    from unicodedata import normalize
    
    try:
        unicode
    except NameError: # Python 3
        unicode = lambda x, y: x # will already be unicode
    
    execv(MPV, [MPV] + [normalize('NFD', unicode(arg, 'utf-8')) for arg in argv[1:]])

    That's assuming Finder uses NFD and Terminal uses NFC. If it's the other way around, change the first argument to normalize().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions