mbox series

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

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

Message

Aman Karmani May 16, 2022, 9:23 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

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 | 166 +++++++++++++++++++++++++++++++++++++
 3 files changed, 188 insertions(+), 6 deletions(-)


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

Range-diff vs v2:

 1:  b66dbdf40c ! 1:  ce70f7b021 avutil/wchar_filename,file_open: Support long file names on Windows
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      + */
      +static inline int path_is_extended(const wchar_t *path)
      +{
     -+    size_t len = wcslen(path);
     -+    if (len >= 4  && path[0] == L'\\' && (path[1] == L'\\' || path[1] == L'?') && path[2] == L'?' && path[3] == L'\\')
     ++    if (path[0] == L'\\' && (path[1] == L'\\' || path[1] == L'?') && path[2] == L'?' && path[3] == L'\\')
     ++        return 1;
     ++
     ++    return 0;
     ++}
     ++
     ++/**
     ++ * Checks for a device path prefix.
     ++ * see .NET6: PathInternal.IsDevicePath()
     ++ */
     ++static inline int path_is_device_path(const wchar_t *path)
     ++{
     ++    if (path[0] == L'\\' && path[1] == L'\\' && path[2] == L'.' && path[3] == L'\\')
      +        return 1;
      +
      +    return 0;
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      + * 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().
     ++ * calling path_normalize() and it doesn't check whether the path is
     ++ * actually long (> MAX_PATH).
      + * see .NET6: PathInternal.EnsureExtendedPrefix() *
      + */
      +static inline int add_extended_prefix(wchar_t **ppath_w)
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +    const size_t len                    = wcslen(path_w);
      +    wchar_t *temp_w;
      +
     -+    if (len < 2)
     ++    /* We're skipping the check IsPartiallyQualified() because
     ++     * we expect to have called GetFullPathNameW() already. */
     ++    if (len < 2 || path_is_extended(*ppath_w) || path_is_device_path(*ppath_w)) {
      +        return 0;
     ++    }
      +
     -+    /* 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));
     ++        /* unc_prefix length is 8 plus 1 for terminating zeros,
     ++         * we subtract 2 for the leading '\\' of the original path */
     ++        temp_w = (wchar_t *)av_calloc(len - 2 + 8 + 1, sizeof(wchar_t));
      +        if (!temp_w) {
      +            errno = ENOMEM;
      +            return -1;
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      + * 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
     ++ * Longs paths (>= MAX_PATH) are prefixed with the extended path or extended
      + * UNC path prefix.
      + * see .NET6: Path.GetFullPath() and Path.GetFullPathInternal()
      + */
     @@ libavutil/wchar_filename.h: static inline int utf8towchar(const char *filename_u
      +
      +    // see .NET6: PathInternal.EnsureExtendedPrefixIfNeeded()
      +    len = wcslen(*ppath_w);
     -+    if (len >= 260 || (*ppath_w)[len - 1] == L' ' || (*ppath_w)[len - 1] == L'.') {
     ++    if (len >= MAX_PATH) {
      +        if ((ret = add_extended_prefix(ppath_w)) < 0)
      +            return ret;
      +    }
 2:  8ecbafe2b7 = 2:  a5268800a4 avformat/os_support: Support long file names on Windows