mbox series

[FFmpeg-devel,v2,0/2] Support long file names on Windows

Message ID pull.28.v2.ffstaging.FFmpeg.1652653071.ffmpegagent@gmail.com
Headers show
Series Support long file names on Windows | expand

Message

softworkz May 15, 2022, 10:17 p.m. UTC
This patchset adds support for long file and directory paths on Windows. The
implementation follows the same logic that .NET is using internally, with
the only exception that it doesn't expand short path components in 8.3
format. .NET does this as the same function is also used for other purposes,
but in our case, that's not required. Short (8.3) paths are working as well
with the extended path prefix, even when longer than 260.

Successfully tested:

 * Regular paths wth drive letter
 * Regular UNC paths
 * Long paths wth drive letter
 * Long paths wth drive letter and forward slashes
 * Long UNC paths
 * Prefixed paths wth drive letter
 * Prefixed UNC paths

I have kept the individual functions separate on purpose, to make it easy to
compare with the .NET impl. (compilers should inlinie those anyway)

v2

 * wchar_filename: Improve comments and function documentation
 * os_support: adjust defines to use win32_stat

softworkz (2):
  avutil/wchar_filename,file_open: Support long file names on Windows
  avformat/os_support: Support long file names on Windows

 libavformat/os_support.h   |  26 +++++--
 libavutil/file_open.c      |   2 +-
 libavutil/wchar_filename.h | 156 +++++++++++++++++++++++++++++++++++++
 3 files changed, 178 insertions(+), 6 deletions(-)


base-commit: d2d8b9b972ba2df6b2a2ebe29f5307cbb7a69c33
Published-As: https://github.com/ffstaging/FFmpeg/releases/tag/pr-ffstaging-28%2Fsoftworkz%2Fsubmit_long_filenames-v2
Fetch-It-Via: git fetch https://github.com/ffstaging/FFmpeg pr-ffstaging-28/softworkz/submit_long_filenames-v2
Pull-Request: https://github.com/ffstaging/FFmpeg/pull/28

Range-diff vs v1:

 1:  26c579e4ee ! 1:  b66dbdf40c avutil/wchar_filename,file_open: Support long file names on Windows
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
           return 0;
       }
      +
     ++/**
     ++ * Checks for extended path prefixes for which normalization needs to be skipped.
     ++ * see .NET6: PathInternal.IsExtended()
     ++ */
      +static inline int path_is_extended(const wchar_t *path)
      +{
     -+    // see .NET6: PathInternal.IsExtended()
      +    size_t len = wcslen(path);
      +    if (len >= 4  && path[0] == L'\\' && (path[1] == L'\\' || path[1] == L'?') && path[2] == L'?' && path[3] == L'\\')
      +        return 1;
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +    return 0;
      +}
      +
     ++/**
     ++ * Performs path normalization by calling GetFullPathNameW().
     ++ * see .NET6: PathHelper.GetFullPathName()
     ++ */
      +static inline int get_full_path_name(wchar_t **ppath_w)
      +{
      +    int num_chars;
      +    wchar_t *temp_w;
      +
     -+    // see .NET6: PathHelper.GetFullPathName()
      +    num_chars = GetFullPathNameW(*ppath_w, 0, NULL, NULL);
      +    if (num_chars <= 0) {
      +        errno = EINVAL;
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +    return 0;
      +}
      +
     ++/**
     ++ * Normalizes a Windows file or folder path.
     ++ * Expansion of short paths (with 8.3 path components) is currently omitted
     ++ * as it is not required for accessing long paths.
     ++ * see .NET6: PathHelper.Normalize().
     ++ */
      +static inline int path_normalize(wchar_t **ppath_w)
      +{
      +    int ret;
      +
     -+    // see .NET6: PathHelper.Normalize()
      +    if ((ret = get_full_path_name(ppath_w)) < 0)
      +        return ret;
      +
      +    /* What .NET does at this point is to call PathHelper.TryExpandShortFileName()
     -+       in case the path contains a '~' character.
     -+       We don't need to do this as we don't need to normalize the file name
     -+       for presentation, and the extended path prefix works with 8.3 path
     -+       components as well */
     ++     * in case the path contains a '~' character.
     ++     * We don't need to do this as we don't need to normalize the file name
     ++     * for presentation, and the extended path prefix works with 8.3 path
     ++     * components as well
     ++     */
      +    return 0;
      +}
      +
     ++/**
     ++ * Adds an extended path or UNC prefix to longs paths or paths ending
     ++ * with a space or a dot. (' ' or '.').
     ++ * This function expects that the path has been normalized before by
     ++ * calling path_normalize().
     ++ * see .NET6: PathInternal.EnsureExtendedPrefix() *
     ++ */
      +static inline int add_extended_prefix(wchar_t **ppath_w)
      +{
      +    const wchar_t *unc_prefix           = L"\\\\?\\UNC\\";
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +    if (len < 2)
      +        return 0;
      +
     -+    // see .NET6: PathInternal.EnsureExtendedPrefix()
     ++    /* We're skipping the check IsPartiallyQualified() because
     ++     * we know we have called GetFullPathNameW() already, also
     ++     * we don't check IsDevice() because device paths are not
     ++     * allowed to be long paths and we're calling this only
     ++     * for long paths.
     ++     */
      +    if (path_w[0] == L'\\' && path_w[1] == L'\\') {
     ++        // The length of unc_prefix is 6 plus 1 for terminating zeros
      +        temp_w = (wchar_t *)av_calloc(len + 6 + 1, sizeof(wchar_t));
      +        if (!temp_w) {
      +            errno = ENOMEM;
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +        wcscpy(temp_w, unc_prefix);
      +        wcscat(temp_w, path_w + 2);
      +    } else {
     ++        // The length of extended_path_prefix is 4 plus 1 for terminating zeros
      +        temp_w = (wchar_t *)av_calloc(len + 4 + 1, sizeof(wchar_t));
      +        if (!temp_w) {
      +            errno = ENOMEM;
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +    return 0;
      +}
      +
     ++/**
     ++ * Converts a file or folder path to wchar_t for use with Windows file
     ++ * APIs. Paths with extended path prefix (either '\\?\' or \??\') are
     ++ * left unchanged.
     ++ * All other paths are normalized and converted to absolute paths.
     ++ * Longs paths (>= 260) are prefixed with the extended path or extended
     ++ * UNC path prefix.
     ++ * see .NET6: Path.GetFullPath() and Path.GetFullPathInternal()
     ++ */
      +static inline int get_extended_win32_path(const char *path, wchar_t **ppath_w)
      +{
      +    int ret;
      +    size_t len;
      +
     -+    // see .NET6: Path.GetFullPath() and Path.GetFullPathInternal()
      +    if ((ret = utf8towchar(path, ppath_w)) < 0)
      +        return ret;
      +
      +    if (path_is_extended(*ppath_w)) {
     -+        /* \\?\ paths are considered normalized by definition. Windows doesn't normalize \\?\
     -+           paths and neither should we. Even if we wanted to, GetFullPathName does not work
     -+           properly with device paths. If one wants to pass a \\?\ path through normalization
     -+           one can chop off the prefix, pass it to GetFullPath and add it again. */
     ++        /* Paths prefixed with '\\?\' or \??\' are considered normalized by definition.
     ++         * Windows doesn't normalize those paths and neither should we.
     ++         */
      +        return 0;
      +    }
      +
 2:  acd81c61c3 ! 2:  8ecbafe2b7 avformat/os_support: Support long file names on Windows
     @@ Commit message
          Signed-off-by: softworkz <softworkz@hotmail.com>
      
       ## libavformat/os_support.h ##
     +@@
     + #  ifdef stat
     + #   undef stat
     + #  endif
     +-#  define stat _stati64
     ++#  define stat win32_stat
     ++
     ++    struct win32_stat
     ++    {
     ++        _dev_t         st_dev;
     ++        _ino_t         st_ino;
     ++        unsigned short st_mode;
     ++        short          st_nlink;
     ++        short          st_uid;
     ++        short          st_gid;
     ++        _dev_t         st_rdev;
     ++        __int64        st_size;
     ++        __time64_t     st_atime;
     ++        __time64_t     st_mtime;
     ++        __time64_t     st_ctime;
     ++    };
     ++
     + #  ifdef fstat
     + #   undef fstat
     + #  endif
      @@ libavformat/os_support.h: static inline int win32_##name(const char *filename_utf8) \
           wchar_t *filename_w;                                  \
           int ret;                                              \