Skip to content

Commit d03d46c

Browse files
committed
various: expand paths for several file/directory related options
There's several path-related options that do not handle common shortcuts (like ~). Fix this by using mp_get_user_path where appropriate which expands the path so users get more intuitive behavior. Fixes mpv-player#15598.
1 parent d87113f commit d03d46c

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

Diff for: audio/out/ao_pcm.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mpv_talloc.h"
2929

3030
#include "options/m_option.h"
31+
#include "options/path.h"
3132
#include "audio/format.h"
3233
#include "ao.h"
3334
#include "internal.h"
@@ -109,7 +110,7 @@ static int init(struct ao *ao)
109110
{
110111
struct priv *priv = ao->priv;
111112

112-
char *outputfilename = priv->outputfilename;
113+
char *outputfilename = mp_get_user_path(priv, ao->global, priv->outputfilename);
113114
if (!outputfilename) {
114115
outputfilename = talloc_strdup(priv, priv->waveheader ? "audiodump.wav"
115116
: "audiodump.pcm");

Diff for: common/encode_lavc.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "options/m_config.h"
3232
#include "options/m_option.h"
3333
#include "options/options.h"
34+
#include "options/path.h"
3435
#include "osdep/timer.h"
3536
#include "video/out/vo.h"
3637
#include "mpv_talloc.h"
@@ -135,7 +136,9 @@ struct encode_lavc_context *encode_lavc_init(struct mpv_global *global)
135136

136137
p->muxer->oformat = ctx->oformat;
137138

138-
p->muxer->url = av_strdup(filename);
139+
char *path = mp_get_user_path(ctx, global, filename);
140+
p->muxer->url = av_strdup(path);
141+
talloc_free(path);
139142
MP_HANDLE_OOM(p->muxer->url);
140143

141144
return ctx;

Diff for: common/playlist.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -403,17 +403,16 @@ struct playlist *playlist_parse_file(const char *file, struct mp_cancel *cancel,
403403
struct mp_log *log = mp_log_new(NULL, global->log, "!playlist_parser");
404404
mp_verbose(log, "Parsing playlist file %s...\n", file);
405405

406+
char *path = mp_get_user_path(NULL, global, file);
406407
struct demuxer_params p = {
407408
.force_format = "playlist",
408409
.stream_flags = STREAM_ORIGIN_DIRECT,
409410
};
410-
struct demuxer *d = demux_open_url(file, &p, cancel, global);
411-
if (!d) {
412-
talloc_free(log);
413-
return NULL;
414-
}
415-
411+
struct demuxer *d = demux_open_url(path, &p, cancel, global);
416412
struct playlist *ret = NULL;
413+
if (!d)
414+
goto done;
415+
417416
if (d && d->playlist) {
418417
ret = talloc_zero(NULL, struct playlist);
419418
playlist_populate_playlist_path(d->playlist, file);
@@ -434,7 +433,9 @@ struct playlist *playlist_parse_file(const char *file, struct mp_cancel *cancel,
434433
if (ret && !ret->num_entries)
435434
mp_warn(log, "Warning: empty playlist\n");
436435

436+
done:
437437
talloc_free(log);
438+
talloc_free(path);
438439
return ret;
439440
}
440441

Diff for: options/path.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void mp_init_paths(struct mpv_global *global, struct MPOpts *opts)
115115
if (!opts->load_config)
116116
force_configdir = "";
117117

118-
global->configdir = talloc_strdup(global, force_configdir);
118+
global->configdir = mp_get_user_path(opts, global, force_configdir);
119119
}
120120

121121
char *mp_find_user_file(void *talloc_ctx, struct mpv_global *global,

Diff for: player/loadfile.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,9 @@ int mp_add_external_file(struct MPContext *mpctx, char *filename,
852852

853853
mp_core_unlock(mpctx);
854854

855+
char *path = mp_get_user_path(NULL, mpctx->global, filename);
855856
struct demuxer *demuxer =
856-
demux_open_url(filename, &params, cancel, mpctx->global);
857+
demux_open_url(path, &params, cancel, mpctx->global);
857858
if (demuxer)
858859
enable_demux_thread(mpctx, demuxer);
859860

@@ -907,11 +908,13 @@ int mp_add_external_file(struct MPContext *mpctx, char *filename,
907908
}
908909

909910
mp_cancel_set_parent(demuxer->cancel, mpctx->playback_abort);
911+
talloc_free(path);
910912

911913
return first_num;
912914

913915
err_out:
914916
demux_cancel_and_free(demuxer);
917+
talloc_free(path);
915918
if (!mp_cancel_test(cancel))
916919
MP_ERR(mpctx, "Can not open external file %s.\n", disp_filename);
917920
return -1;
@@ -1057,7 +1060,7 @@ static void load_chapters(struct MPContext *mpctx)
10571060
bool free_src = false;
10581061
char *chapter_file = mpctx->opts->chapter_file;
10591062
if (chapter_file && chapter_file[0]) {
1060-
chapter_file = talloc_strdup(NULL, chapter_file);
1063+
chapter_file = mp_get_user_path(NULL, mpctx->global, chapter_file);
10611064
mp_core_unlock(mpctx);
10621065
struct demuxer_params p = {.stream_flags = STREAM_ORIGIN_DIRECT};
10631066
struct demuxer *demux = demux_open_url(chapter_file, &p,

Diff for: player/misc.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "options/options.h"
3232
#include "options/m_property.h"
3333
#include "options/m_config.h"
34+
#include "options/path.h"
3435
#include "common/common.h"
3536
#include "common/encode.h"
3637
#include "common/playlist.h"
@@ -277,7 +278,8 @@ int stream_dump(struct MPContext *mpctx, const char *source_filename)
277278

278279
int64_t size = stream_get_size(stream);
279280

280-
FILE *dest = fopen(opts->stream_dump, "wb");
281+
char *filename = mp_get_user_path(NULL, mpctx->global, opts->stream_dump);
282+
FILE *dest = fopen(filename, "wb");
281283
if (!dest) {
282284
MP_ERR(mpctx, "Error opening dump file: %s\n", mp_strerror(errno));
283285
goto done;
@@ -305,6 +307,7 @@ int stream_dump(struct MPContext *mpctx, const char *source_filename)
305307
ok &= fclose(dest) == 0;
306308
done:
307309
free_stream(stream);
310+
talloc_free(filename);
308311
return ok ? 0 : -1;
309312
}
310313

Diff for: video/out/vo_image.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ struct priv {
6666
struct vo_image_opts *opts;
6767

6868
struct mp_image *current;
69+
char *dir;
6970
int frame;
7071
};
7172

7273
static bool checked_mkdir(struct vo *vo, const char *buf)
7374
{
74-
MP_INFO(vo, "Creating output directory '%s'...\n", buf);
75-
if (mkdir(buf, 0755) < 0) {
75+
struct priv *p = vo->priv;
76+
p->dir = mp_get_user_path(vo, vo->global, buf);
77+
MP_INFO(vo, "Creating output directory '%s'...\n", p->dir);
78+
if (mkdir(p->dir, 0755) < 0) {
7679
char *errstr = mp_strerror(errno);
7780
if (errno == EEXIST) {
7881
struct stat stat_p;
@@ -117,8 +120,8 @@ static void flip_page(struct vo *vo)
117120
char *filename = talloc_asprintf(t, "%08d.%s", p->frame,
118121
image_writer_file_ext(p->opts->opts));
119122

120-
if (p->opts->outdir && strlen(p->opts->outdir))
121-
filename = mp_path_join(t, p->opts->outdir, filename);
123+
if (p->dir && strlen(p->dir))
124+
filename = mp_path_join(t, p->dir, filename);
122125

123126
MP_INFO(vo, "Saving %s\n", filename);
124127
write_image(p->current, p->opts->opts, filename, vo->global, vo->log, true);

0 commit comments

Comments
 (0)