Skip to content

Commit

Permalink
Implement pl_env_vars option for library/playlist/cache relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin authored and flyingmutant committed Jul 15, 2023
1 parent 6167caa commit 3334ca7
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Doc/cmus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,12 @@ pause_on_output_change (false)

Supported output plugins: pulse.

pl_env_vars
Comma separated list of environment variables to substitute when saving
library/playlist files. The paths must be absolute to take effect.

Note: This option will not take full effect until cmus has been restarted.

pl_sort () [`Sort Keys`]
Sort keys for the playlist view (3). Empty value disables sorting and
enables manually moving tracks.
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ cmus-y := \
comment.o convert.lo cue.o cue_utils.o debug.o discid.o editable.o expr.o \
filters.o format_print.o gbuf.o glob.o help.o history.o http.o id3.o input.o \
job.o keys.o keyval.o lib.o load_dir.o locking.o mergesort.o misc.o options.o \
output.o pcm.o player.o play_queue.o pl.o rbtree.o read_wrapper.o search_mode.o \
search.o server.o spawn.o tabexp_file.o tabexp.o track_info.o track.o tree.o \
uchar.o u_collate.o ui_curses.o window.o worker.o xstrjoin.o
output.o pcm.o player.o play_queue.o pl.o pl_env.o rbtree.o read_wrapper.o \
search_mode.o search.o server.o spawn.o tabexp_file.o tabexp.o track_info.o \
track.o tree.o uchar.o u_collate.o ui_curses.o window.o worker.o xstrjoin.o

cmus-$(CONFIG_MPRIS) += mpris.o

Expand Down
30 changes: 27 additions & 3 deletions cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "xstrjoin.h"
#include "gbuf.h"
#include "options.h"
#include "pl_env.h"

#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -124,10 +125,18 @@ static int valid_cache_entry(const struct cache_entry *e, unsigned int avail)
static struct track_info *cache_entry_to_ti(struct cache_entry *e)
{
const char *strings = e->strings;
struct track_info *ti = track_info_new(strings);
struct track_info *ti;
struct keyval *kv;
int str_size = e->size - sizeof(*e);
int pos, i, count;
char *proc_filename;

if (pl_env_var(strings, NULL) && (proc_filename = pl_env_expand(strings))) {
ti = track_info_new(proc_filename);
free(proc_filename);
} else {
ti = track_info_new(strings);
}

ti->duration = e->duration;
ti->bitrate = e->bitrate;
Expand Down Expand Up @@ -318,6 +327,7 @@ static void flush_buffer(int fd, struct gbuf *buf)

static void write_ti(int fd, struct gbuf *buf, struct track_info *ti, unsigned int *offsetp)
{
char *proc_filename = pl_env_reduce(ti->filename);
const struct keyval *kv = ti->comments;
unsigned int offset = *offsetp;
unsigned int pad;
Expand All @@ -334,7 +344,7 @@ static void write_ti(int fd, struct gbuf *buf, struct track_info *ti, unsigned i
e.mtime = ti->mtime;
e.play_count = ti->play_count;
e.bpm = ti->bpm;
len[count] = strlen(ti->filename) + 1;
len[count] = strlen(proc_filename) + 1;
e.size += len[count++];
len[count] = (ti->codec ? strlen(ti->codec) : 0) + 1;
e.size += len[count++];
Expand All @@ -359,7 +369,7 @@ static void write_ti(int fd, struct gbuf *buf, struct track_info *ti, unsigned i
if (pad)
gbuf_set(buf, 0, pad);
gbuf_add_bytes(buf, &e, sizeof(e));
gbuf_add_bytes(buf, ti->filename, len[count++]);
gbuf_add_bytes(buf, proc_filename, len[count++]);
gbuf_add_bytes(buf, ti->codec ? ti->codec : "", len[count++]);
gbuf_add_bytes(buf, ti->codec_profile ? ti->codec_profile : "", len[count++]);
for (i = 0; kv[i].key; i++) {
Expand All @@ -369,6 +379,8 @@ static void write_ti(int fd, struct gbuf *buf, struct track_info *ti, unsigned i

free(len);
*offsetp = offset + pad + e.size;

free(proc_filename);
}

int cache_close(void)
Expand Down Expand Up @@ -437,6 +449,18 @@ struct track_info *cache_get_ti(const char *filename, int force)
struct track_info *ti;
int reload = 0;

if (pl_env_var(filename, NULL)) {
struct growing_keyvals c = {NULL, 0, 0};
keyvals_terminate(&c);

ti = track_info_new(filename);
ti->duration = 0;
track_info_set_comments(ti, c.keyvals);

track_info_ref(ti);
return ti;
}

ti = lookup_cache_entry(filename, hash);
if (ti) {
if ((!skip_track_info && ti->duration == 0 && !is_http_url(filename)) || force){
Expand Down
7 changes: 6 additions & 1 deletion cmus.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "gbuf.h"
#include "discid.h"
#include "locking.h"
#include "pl_env.h"

#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -231,16 +232,20 @@ static int save_ext_playlist_cb(void *data, struct track_info *ti)

static int save_playlist_cb(void *data, struct track_info *ti)
{
char *proc_filename = pl_env_reduce(ti->filename);
int fd = *(int *)data;
const char nl = '\n';
int rc;

rc = write_all(fd, ti->filename, strlen(ti->filename));
rc = write_all(fd, proc_filename, strlen(proc_filename));
free(proc_filename);
if (rc == -1)
return -1;

rc = write_all(fd, &nl, 1);
if (rc == -1)
return -1;

return 0;
}

Expand Down
5 changes: 4 additions & 1 deletion job.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "xstrjoin.h"
#include "ui_curses.h"
#include "cue_utils.h"
#include "pl_env.h"

#include <string.h>
#include <unistd.h>
Expand Down Expand Up @@ -351,7 +352,9 @@ static int handle_line(void *data, const char *line)
if (is_http_url(line) || is_cue_url(line)) {
add_url(line);
} else {
char *absolute = path_absolute_cwd(line, data);
char *absolute = pl_env_var(line, NULL)
? pl_env_expand(line)
: path_absolute_cwd(line, data);
add_file(absolute, 0);
free(absolute);
}
Expand Down
53 changes: 53 additions & 0 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ char *window_title_alt_format = NULL;
char *id3_default_charset = NULL;
char *icecast_default_charset = NULL;
char *lib_add_filter = NULL;
char **pl_env_vars;

static void buf_int(char *buf, int val, size_t size)
{
Expand Down Expand Up @@ -268,6 +269,7 @@ static const struct {
{ "pl_sort", "" },
{ "id3_default_charset", "ISO-8859-1" },
{ "icecast_default_charset", "ISO-8859-1" },
{ "pl_env_vars", "" },
{ NULL, NULL }
};

Expand Down Expand Up @@ -507,6 +509,56 @@ static void set_tree_width_max(void *data, const char *buf)
update_size();
}

/* get_pl_env_vars converts the pl_env_vars array into a comma-separated list */
static void get_pl_env_vars(void *data, char *buf, size_t size)
{
if (!pl_env_vars) {
strscpy(buf, "", size);
return;
}
char *p = buf;
size_t r = size - 1;
for (char **x = pl_env_vars; *x; x++) {
if (x != pl_env_vars) {
if (!--r)
return; /* overflow */
*p++ = ',';
}
size_t l = strlen(*x);
if (!(r -= l))
return; /* overflow */
strcpy(p, *x);
p += l;
}
*p = '\0';
}

/* set_pl_env_vars splits the pl_env_vars config into the pl_env_vars array */
static void set_pl_env_vars(void *data, const char *buf)
{
if (pl_env_vars) {
free(*pl_env_vars);
free(pl_env_vars);
}
if (!*buf) {
pl_env_vars = NULL;
}
size_t n = 1;
for (const char *x = buf; *x; x++) {
if (*x == ',') {
n++;
}
}
char **a = pl_env_vars = xnew(char*, n+1);
for (char *x = *a++ = xstrdup(buf); *x; x++) {
if (*x == ',') {
*a++ = x+1;
*x = '\0';
}
}
*a = NULL;
}

/* }}} */

/* callbacks for toggle options {{{ */
Expand Down Expand Up @@ -1516,6 +1568,7 @@ static const struct {
DN(tree_width_percent)
DN(tree_width_max)
DT(pause_on_output_change)
DN(pl_env_vars)
{ NULL, NULL, NULL, NULL, 0 }
};

Expand Down
3 changes: 3 additions & 0 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ extern char *clipped_text_internal;
extern char *id3_default_charset;
extern char *icecast_default_charset;

/* comma-separated list of env vars to substitute in saved library/cache paths */
extern char **pl_env_vars;

/* build option list */
void options_add(void);

Expand Down
Loading

0 comments on commit 3334ca7

Please sign in to comment.