Skip to content

Commit 9e9319e

Browse files
committed
misc/path_utils: make mp_basename() return full URLs
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.
1 parent bbafb74 commit 9e9319e

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

misc/path_utils.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@
4141

4242
char *mp_basename(const char *path)
4343
{
44+
if (mp_is_url(bstr0(path)))
45+
return (char *)path;
46+
4447
char *s;
4548

4649
#if HAVE_DOS_PATHS
47-
if (!mp_is_url(bstr0(path))) {
48-
s = strrchr(path, '\\');
49-
if (s)
50-
path = s + 1;
51-
s = strrchr(path, ':');
52-
if (s)
53-
path = s + 1;
54-
}
50+
s = strrchr(path, '\\');
51+
if (s)
52+
path = s + 1;
53+
s = strrchr(path, ':');
54+
if (s)
55+
path = s + 1;
5556
#endif
5657
s = strrchr(path, '/');
5758
return s ? s + 1 : (char *)path;

player/command.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,6 @@ static int mp_property_filename(void *ctx, struct m_property *prop,
527527
if (mp_is_url(bstr0(filename)))
528528
mp_url_unescape_inplace(filename);
529529
char *f = (char *)mp_basename(filename);
530-
if (!f[0])
531-
f = filename;
532530
if (action == M_PROPERTY_KEY_ACTION) {
533531
struct m_property_action_arg *ka = arg;
534532
if (strcmp(ka->key, "no-ext") == 0) {
@@ -3508,12 +3506,9 @@ static int mp_property_playlist(void *ctx, struct m_property *prop,
35083506
const char *reset = pl->current == e ? get_style_reset(mpctx) : "";
35093507
char *p = e->title;
35103508
if (!p || mpctx->opts->playlist_entry_name > 0) {
3511-
p = e->filename;
3512-
if (!mp_is_url(bstr0(p))) {
3513-
char *s = mp_basename(e->filename);
3514-
if (s[0])
3515-
p = s;
3516-
}
3509+
p = mp_basename(e->filename);
3510+
if (!p[0])
3511+
p = e->filename;
35173512
}
35183513
if (!e->title || p == e->title || mpctx->opts->playlist_entry_name == 1) {
35193514
res = talloc_asprintf_append(res, "%s%s\n", p, reset);

player/configfiles.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ static char *mp_get_playback_resume_config_filename(struct MPContext *mpctx,
215215
char *res = NULL;
216216
void *tmp = talloc_new(NULL);
217217
const char *path = NULL;
218-
if (mp_is_url(bstr0(fname))) {
219-
path = fname;
220-
} else if (opts->ignore_path_in_watch_later_config) {
218+
if (opts->ignore_path_in_watch_later_config) {
221219
path = mp_basename(fname);
222220
} else {
223221
path = mp_normalize_path(tmp, fname);
@@ -255,7 +253,7 @@ static bool needs_config_quoting(const char *s)
255253

256254
static void write_filename(struct MPContext *mpctx, FILE *file, char *filename)
257255
{
258-
if (mpctx->opts->ignore_path_in_watch_later_config && !mp_is_url(bstr0(filename)))
256+
if (mpctx->opts->ignore_path_in_watch_later_config)
259257
filename = mp_basename(filename);
260258

261259
if (mpctx->opts->write_filename_in_watch_later_config) {
@@ -287,7 +285,7 @@ static void write_redirect(struct MPContext *mpctx, char *path)
287285

288286
static void write_redirects_for_parent_dirs(struct MPContext *mpctx, char *path)
289287
{
290-
if (mp_is_url(bstr0(path)) || mpctx->opts->ignore_path_in_watch_later_config)
288+
if (mpctx->opts->ignore_path_in_watch_later_config)
291289
return;
292290

293291
// Write redirect entries for the file's parent directories to allow
@@ -412,7 +410,7 @@ void mp_delete_watch_later_conf(struct MPContext *mpctx, const char *file)
412410
unlink(fname);
413411
talloc_free(fname);
414412

415-
if (mp_is_url(bstr0(path)) || mpctx->opts->ignore_path_in_watch_later_config)
413+
if (mpctx->opts->ignore_path_in_watch_later_config)
416414
goto exit;
417415

418416
bstr dir = mp_dirname(path);

player/lua/select.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ mp.add_key_binding(nil, "select-playlist", function ()
4949
for i, entry in ipairs(mp.get_property_native("playlist")) do
5050
playlist[i] = entry.title
5151
if not playlist[i] or show ~= "title" then
52-
playlist[i] = entry.filename
53-
if not playlist[i]:find("://") then
54-
playlist[i] = select(2, utils.split_path(
55-
playlist[i]:gsub(trailing_slash_pattern, "")))
56-
end
52+
playlist[i] = select(2, utils.split_path(
53+
entry.filename:gsub(trailing_slash_pattern, "")))
5754
end
5855
if entry.title and show == "both" then
5956
playlist[i] = string.format("%s (%s)", entry.title, playlist[i])

0 commit comments

Comments
 (0)