Skip to content

Commit 98e477a

Browse files
committed
screenshot: fix %f corner case
URLs can end with trailing slashes (/) which in turn results in GNU basename[1] returning the empty string. Catch that case and fallback on the raw value of mpctx->filename. Subsequent path sanitation takes care of translating invalid path component chars. Issue reproduction steps: ``` mpv \ --screenshot-dir=$HOME/mpv-shots \ --screenshot-template='%f/%P' \ https://example.org/video/ ``` This would result in %f expanding to '' and thus render screenshot-template an absolute path which, for some reason, would in turn take precedence of screenshot-dir and hence result in a non-writeable path, i.e. `/timestamp.ext`. With this new approach the resulting path looks like this: `/home/user/mpv-shots/http:__example.org_video_/timestamp.ext` Not particularly pretty but less ambiguous and more consistent over a diverse range of input paths. [1] #14635 (comment)
1 parent 28ef6b7 commit 98e477a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

player/screenshot.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,17 @@ static char *create_fname(struct MPContext *mpctx, char *template,
175175
}
176176
case 'f':
177177
case 'F': {
178-
char *video_file = NULL;
179-
if (mpctx->filename)
178+
char *video_file = mpctx->filename;
179+
if (!video_file) {
180+
video_file = "NO_FILE";
181+
} else {
180182
video_file = mp_basename(mpctx->filename);
181183

182-
if (!video_file)
183-
video_file = "NO_FILE";
184+
/* This should only matter for rare URLs with a trailing /
185+
* because GNU basename then returns ''. */
186+
if (!video_file[0])
187+
video_file = mpctx->filename; // use the raw value instead
188+
}
184189

185190
char *name = video_file;
186191
if (fmt == 'F')
@@ -300,9 +305,6 @@ static char *gen_fname(struct mp_cmd_ctx *cmd, const char *file_ext)
300305
void *t = fname;
301306
dir = mp_get_user_path(t, ctx->mpctx->global, dir);
302307
fname = mp_path_join(NULL, dir, fname);
303-
304-
mp_mkdirp(dir);
305-
306308
talloc_free(t);
307309
}
308310

0 commit comments

Comments
 (0)