mbox series

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

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

Message

Aman Karmani May 24, 2022, 1:58 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

v3

 * removed length check in path_is_extended()
 * added path_is_device_path() check in add_extended_prefix()
 * add_extended_prefix(): clarified doc and add checks
 * clarified string allocation length calculation
 * replaced 260 with MAX_PATH
 * removed redundant checks after normalization

v4

 * rebased. no changes

v5

 * resolved the ugly struct duplication
 * compatible with _USE_32BIT_TIME_T

v6

 * wchar_filename.h: added links to .NET source code
 * wchar_filename.h: free allocations on error
 * os_support.hs: use clean and safe way to redirect stat() calls

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   |  87 +++++++++++++-----
 libavutil/file_open.c      |   2 +-
 libavutil/wchar_filename.h | 180 +++++++++++++++++++++++++++++++++++++
 3 files changed, 244 insertions(+), 25 deletions(-)


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

Range-diff vs v5:

 1:  13118dc1fa ! 1:  960aa795ff avutil/wchar_filename,file_open: Support long file names on Windows
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +/**
      + * Checks for extended path prefixes for which normalization needs to be skipped.
      + * see .NET6: PathInternal.IsExtended()
     ++ * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L165
      + */
      +static inline int path_is_extended(const wchar_t *path)
      +{
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +
      +/**
      + * Checks for a device path prefix.
     -+ * see .NET6: PathInternal.IsDevicePath()
     ++ * see .NET6: PathInternal.IsDevice()
     ++ * we don't check forward slashes and extended paths (as already done)
     ++ * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L132
      + */
      +static inline int path_is_device_path(const wchar_t *path)
      +{
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +/**
      + * Performs path normalization by calling GetFullPathNameW().
      + * see .NET6: PathHelper.GetFullPathName()
     ++ * https://github.com/dotnet/runtime/blob/2a99e18eedabcf1add064c099da59d9301ce45e0/src/libraries/System.Private.CoreLib/src/System/IO/PathHelper.Windows.cs#L70
      + */
      +static inline int get_full_path_name(wchar_t **ppath_w)
      +{
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +
      +    num_chars = GetFullPathNameW(*ppath_w, num_chars, temp_w, NULL);
      +    if (num_chars <= 0) {
     ++        av_free(temp_w);
      +        errno = EINVAL;
      +        return -1;
      +    }
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      + * 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().
     ++ * see .NET6: PathHelper.Normalize()
     ++ * https://github.com/dotnet/runtime/blob/2a99e18eedabcf1add064c099da59d9301ce45e0/src/libraries/System.Private.CoreLib/src/System/IO/PathHelper.Windows.cs#L25
      + */
      +static inline int path_normalize(wchar_t **ppath_w)
      +{
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      + * This function expects that the path has been normalized before by
      + * calling path_normalize() and it doesn't check whether the path is
      + * actually long (> MAX_PATH).
     -+ * see .NET6: PathInternal.EnsureExtendedPrefix() *
     ++ * see .NET6: PathInternal.EnsureExtendedPrefix()
     ++ * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L107
      + */
      +static inline int add_extended_prefix(wchar_t **ppath_w)
      +{
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      + * Longs paths (>= MAX_PATH) are prefixed with the extended path or extended
      + * UNC path prefix.
      + * see .NET6: Path.GetFullPath() and Path.GetFullPathInternal()
     ++ * https://github.com/dotnet/runtime/blob/2a99e18eedabcf1add064c099da59d9301ce45e0/src/libraries/System.Private.CoreLib/src/System/IO/Path.Windows.cs#L126
      + */
      +static inline int get_extended_win32_path(const char *path, wchar_t **ppath_w)
      +{
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +        return 0;
      +    }
      +
     -+    if ((ret = path_normalize(ppath_w)) < 0)
     ++    if ((ret = path_normalize(ppath_w)) < 0) {
     ++        av_freep(ppath_w);
      +        return ret;
     ++    }
      +
     -+    // see .NET6: PathInternal.EnsureExtendedPrefixIfNeeded()
     ++    /* see .NET6: PathInternal.EnsureExtendedPrefixIfNeeded()
     ++     * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L92
     ++     */
      +    len = wcslen(*ppath_w);
      +    if (len >= MAX_PATH) {
     -+        if ((ret = add_extended_prefix(ppath_w)) < 0)
     ++        if ((ret = add_extended_prefix(ppath_w)) < 0) {
     ++            av_freep(ppath_w);
      +            return ret;
     ++        }
      +    }
      +
      +    return 0;
 2:  5313aeec0e ! 2:  6f8d400db7 avformat/os_support: Support long file names on Windows
     @@ libavformat/os_support.h
       #   undef stat
       #  endif
      -#  define stat _stati64
     ++
      +#  define stat win32_stat
      +
      +    struct win32_stat
      +    {
     -+        struct _stati64;
     ++        _dev_t         st_dev;     /* ID of device containing file */
     ++        _ino_t         st_ino;     /* inode number */
     ++        unsigned short st_mode;    /* protection */
     ++        short          st_nlink;   /* number of hard links */
     ++        short          st_uid;     /* user ID of owner */
     ++        short          st_gid;     /* group ID of owner */
     ++        _dev_t         st_rdev;    /* device ID (if special file) */
     ++        long           st_size;    /* total size, in bytes */
     ++        time_t         st_atime;   /* time of last access */
     ++        time_t         st_mtime;   /* time of last modification */
     ++        time_t         st_ctime;   /* time of last status change */
      +    };
      +
       #  ifdef fstat
     @@ libavformat/os_support.h: static inline int win32_##name(const char *filename_ut
               return -1;                                        \
           if (!filename_w)                                      \
               goto fallback;                                    \
     -@@ libavformat/os_support.h: static inline int win32_##name(const char *filename_utf8, partype par) \
     -     wchar_t *filename_w;                                  \
     -     int ret;                                              \
     -                                                           \
     +@@ libavformat/os_support.h: DEF_FS_FUNCTION(unlink, _wunlink, _unlink)
     + DEF_FS_FUNCTION(mkdir,  _wmkdir,  _mkdir)
     + DEF_FS_FUNCTION(rmdir,  _wrmdir , _rmdir)
     + 
     +-#define DEF_FS_FUNCTION2(name, wfunc, afunc, partype)     \
     +-static inline int win32_##name(const char *filename_utf8, partype par) \
     +-{                                                         \
     +-    wchar_t *filename_w;                                  \
     +-    int ret;                                              \
     +-                                                          \
      -    if (utf8towchar(filename_utf8, &filename_w))          \
     -+    if (get_extended_win32_path(filename_utf8, &filename_w)) \
     -         return -1;                                        \
     -     if (!filename_w)                                      \
     -         goto fallback;                                    \
     -@@ libavformat/os_support.h: static inline int win32_rename(const char *src_utf8, const char *dest_utf8)
     +-        return -1;                                        \
     +-    if (!filename_w)                                      \
     +-        goto fallback;                                    \
     +-                                                          \
     +-    ret = wfunc(filename_w, par);                         \
     +-    av_free(filename_w);                                  \
     +-    return ret;                                           \
     +-                                                          \
     +-fallback:                                                 \
     +-    /* filename may be be in CP_ACP */                    \
     +-    return afunc(filename_utf8, par);                     \
     ++static inline int win32_access(const char *filename_utf8, int par)
     ++{
     ++    wchar_t *filename_w;
     ++    int ret;
     ++    if (get_extended_win32_path(filename_utf8, &filename_w))
     ++        return -1;
     ++    if (!filename_w)
     ++        goto fallback;
     ++    ret = _waccess(filename_w, par);
     ++    av_free(filename_w);
     ++    return ret;
     ++fallback:
     ++    return _access(filename_utf8, par);
     + }
     + 
     +-DEF_FS_FUNCTION2(access, _waccess, _access, int)
     +-DEF_FS_FUNCTION2(stat, _wstati64, _stati64, struct stat*)
     ++static inline int win32_stat(const char *filename_utf8, struct stat *par)
     ++{
     ++    wchar_t *filename_w;
     ++    int ret;
     ++    struct _stati64 winstat = { 0 };
     ++
     ++    if (get_extended_win32_path(filename_utf8, &filename_w))
     ++        return -1;
     ++
     ++    if (filename_w) {
     ++        ret = _wstat64(filename_w, &winstat);
     ++        av_free(filename_w);
     ++    } else
     ++        ret = _stat64(filename_utf8, &winstat);
     ++
     ++    par->st_dev   = winstat.st_dev;
     ++    par->st_ino   = winstat.st_ino;
     ++    par->st_mode  = winstat.st_mode;
     ++    par->st_nlink = winstat.st_nlink;
     ++    par->st_uid   = winstat.st_uid;
     ++    par->st_gid   = winstat.st_gid;
     ++    par->st_rdev  = winstat.st_rdev;
     ++    par->st_size  = winstat.st_size;
     ++    par->st_atime = winstat.st_atime;
     ++    par->st_mtime = winstat.st_mtime;
     ++    par->st_ctime = winstat.st_ctime;
     ++
     ++    return ret;
     ++}
     + 
     + static inline int win32_rename(const char *src_utf8, const char *dest_utf8)
     + {
           wchar_t *src_w, *dest_w;
           int ret;