-
Notifications
You must be signed in to change notification settings - Fork 3.2k
path_utils: strip trailing path separators #17015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
path_utils: strip trailing path separators #17015
Conversation
|
|
Thanks for clearing that up(*). I think I have found the culprit in https://github.com/mpv-player/mpv/blob/28ef6b780c227507e248890b18397da21027d935/misc/path_utils.c#L56C1-L56C28. Interestingly enough there is mp_path_strip_trailing_separator in the same file but it is not called in mp_basename, but it should. But then the compiler complains about discarding the const qualifier of path. So, is it a hard requirement for path to be a const? And if so, what if I then call mp_path_strip_trailing_separator((char *)path)? It does fix both issues I mentioned above, the filename property now reflects the basename of said URLs and %f returns the same result.
|
|
You can verify the implementation in Also see #14635 (comment) |
|
|
|
Thanks again. But it gets stranger because turning that mp_basename call into a noop, still results in the trailing '/' being stripped, which did not happen before. Anyway, I think the last commit should round this out by adding a call to |
e73cfd1 to
629550b
Compare
Path may well end with a '/', e.g. URLs. So strip them away since otherwise mp_basename finds the wrong part of the given path. In case of the 'filename' property this simply results in it being set to the whole URL. But in case of the %f/%F screenshot-template specifiers it may result in a broken screenshot path, if %f occurs before the first '/'.
629550b to
5afdc08
Compare
|
I think this should be it. I've updated the PR message to reflect my findings. Correct me if I am wrong, seeing that in case of no slashes in |
It may be rare but multiple trailing slashes are perfectly valid. Keep stripping until all are gone. Previously this was a one shot operation.
This is intended according to the documentation. |
|
BTW, doesn't this overwrite what happens above in case mpv runs on Windows and path is not a URL? |
Yes, it may be, as is mine. But setting |
This is not what is happening. When %f expands to an empty string, |
Sorry for the misleading message. What actually happens is that mp_basename finds the first (last) occurence of '/' with P.S.: I've updated the PR message. Sorry again for the confusion, I simply missed that part on my last edit. |
Sorry I missed this. Arguably those trailing slashes are redundant. And running Maybe some guidance on how to solve this? I've tried working with a |
|
Also, about manipulating |
|
Ok, so the lockup happens because P.S.: As I suspected |
Are you sure yt-dlp doesn't return them for a reason? e.g. is there an extra redirect if you reload that playlist entry?
mpv implements the GNU version of
That only changes the pointer. |
Pretty sure. Also
Sorry, I missed that before. Now I understand and stand corrected.
Thanks for pointing that out, but I think it's in direct conflict with the policy (GNU basename). I must look elsewhere then for finding the cause of the problem.
Ah, now I see. Thanks for explaining! |
Ok. In theory direct media URLs could end with /. I don't know if these actually exist, but if it doesn't cause issues, trailing slashes would have to be stripped in Though I was not shooting down changing |
|
@guidocella but it does cause issues. Those URLs do exist, e.g. this one which triggered some bug as described in the PR message. So My updated expectation is that a And I can't really understand where it goes wrong. |
|
I meant if stripping the trailing slash doesn't cause issues with any direct media URL redirecting or not playing. #17015 (comment) already explained what is happening. |
But that is not what the documentation suggests. The decision, if Could this maybe solved by stating that all |
|
Can't you just also use the fallback if the filename is empty? i.e. |
I did think of that, but I figured there may be a reason why it is not done that way already. My guess was that there is a subtle difference in the property getter returning |
|
It's not done that way because nobody thought about it. It was added in a8e6de9 for the case of running without any file. |
That commit describes a very different scenario, which only strengthens my belief in the distinction. After all |
|
But it irks me that, if |
|
Oh, one more thing, which I forgot to carry over in my edits to the PR message: |
|
How about falling back to the full content of the filename property, in case Any takers? |
|
You can just update the PR to do that. |
|
|
When the path is a URL, use the whole path as basename. The dirname will be "." in this case. This simplifies various checks throughout the codebase. In particular, it avoids returning an empty string for URLs ending with /. The filename property will return full URLs, which is desirable because the domain and path segments before the last are useful information. This fixes half of mpv-player#10975. The empty string check in mp_property_filename() can be removed because it only happened with the basename of URLs ending with /. mp_property_filename() will not be called with directories with trailing slashes because mpv expands them immediately when they are the current playlist entry; I tested it by observing filename. Also mpv-player#16896 will prevent directories with trailing slashes anyway. This also fixes the issue described in mpv-player#17015 of %f in --screenshot-template being evaluated to an empty string for URLs ending with /, which can inadvertently make it use an absolute path. Alternative to mpv-player#16932 and mpv-player#17021.
When the path is a URL, use the whole path as basename. The dirname will be "." in this case. This simplifies various checks throughout the codebase. In particular, it avoids returning an empty string for URLs ending with /. The filename property will return full URLs, which is desirable because the domain and path segments before the last are useful information. This fixes half of mpv-player#10975. This also fixes the issue described in mpv-player#17015 of %f in --screenshot-template being evaluated to an empty string for URLs ending with /, which can inadvertently make it use an absolute path. Alternative to mpv-player#16932 and mpv-player#17021.
Previous behavior results in a '\0' (nul char) being returned if 'filename' is a URL that ends with a '/', which in turn would lead to 'screenshot-dir' being ignored or overwritten, which then results screenshot-path effecitively starting with '/' in case of nesting screenshot dirs per file, e.g.:
This happens with ytdl-hook on some sites, in case it matters. Since %f expands to '' and somehow mpv also forgets about screenshot-dir in the process, the effective path template now is '/%P', which is most certainly not writable for ordinary users, on *nix at least.
This strips trailing path separators in mp_basename before doing the reverse search for the first '/' which otherwise returns the very last index of the string + 1 which in turn points to the terminating '\0'.
Read this before you submit this pull request:
https://github.com/mpv-player/mpv/blob/master/DOCS/contribute.md
I have read the aforementioned document.