Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
DVR: Remember all filenames (for deletion or other future operations)…
…, fixes#1672
  • Loading branch information
perexg committed May 20, 2015
1 parent 0e5ec2a commit 3a36e86
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 85 deletions.
28 changes: 27 additions & 1 deletion src/config.c
Expand Up @@ -1185,6 +1185,31 @@ config_migrate_v17 ( void )
}
}

static void
config_migrate_v18 ( void )
{
htsmsg_t *c, *e, *l, *m;
htsmsg_field_t *f;
const char *filename;

if ((c = hts_settings_load("dvr/log")) != NULL) {
HTSMSG_FOREACH(f, c) {
if (!(e = htsmsg_field_get_map(f))) continue;
if ((filename = htsmsg_get_str(e, "filename")) == NULL)
continue;
if ((l = htsmsg_get_list(e, "files")) != NULL)
continue;
l = htsmsg_create_list();
m = htsmsg_create_map();
htsmsg_add_str(m, "filename", filename);
htsmsg_add_msg(l, NULL, m);
htsmsg_delete_field(e, "filename");
htsmsg_add_msg(e, "files", l);
hts_settings_save(e, "dvr/log/%s", f->hmf_name);
}
}
}

/*
* Perform backup
*/
Expand Down Expand Up @@ -1297,7 +1322,8 @@ static const config_migrate_t config_migrate_table[] = {
config_migrate_v14,
config_migrate_v15,
config_migrate_v16,
config_migrate_v17
config_migrate_v17,
config_migrate_v18,
};

/*
Expand Down
5 changes: 3 additions & 2 deletions src/dvr/dvr.h
Expand Up @@ -157,8 +157,7 @@ typedef struct dvr_entry {
char *de_owner;
char *de_creator;
char *de_comment;
char *de_filename; /* Initially null if no filename has been
generated yet */
htsmsg_t *de_files; /* List of all used files */
char *de_directory; /* Can be set for autorec entries, will override any
directory setting from the configuration */
lang_str_t *de_title; /* Title in UTF-8 (from EPG) */
Expand Down Expand Up @@ -480,6 +479,8 @@ dvr_entry_t *dvr_entry_find_by_event_fuzzy(epg_broadcast_t *e);

dvr_entry_t *dvr_entry_find_by_episode(epg_broadcast_t *e);

const char *dvr_get_filename(dvr_entry_t *de);

int64_t dvr_get_filesize(dvr_entry_t *de);

dvr_entry_t *dvr_entry_cancel(dvr_entry_t *de);
Expand Down
8 changes: 5 additions & 3 deletions src/dvr/dvr_cutpoints.c
Expand Up @@ -210,11 +210,13 @@ dvr_get_cutpoint_list (dvr_entry_t *de)
{
int i;
char *path, *sptr;
const char *filename;
dvr_cutpoint_list_t *cuts;

/* Check this is a valid recording */
assert(de != NULL);
if (de->de_filename == NULL)
filename = dvr_get_filename(de);
if (filename == NULL)
return NULL;

/* Allocate list space */
Expand All @@ -225,8 +227,8 @@ dvr_get_cutpoint_list (dvr_entry_t *de)

/* Get base filename */
// TODO: harcoded 3 for max extension
path = alloca(strlen(de->de_filename) + 3);
strcpy(path, de->de_filename);
path = alloca(strlen(filename) + 3);
strcpy(path, filename);
sptr = strrchr(path, '.');
if (!sptr) {
free(cuts);
Expand Down
76 changes: 60 additions & 16 deletions src/dvr/dvr_db.c
Expand Up @@ -347,7 +347,7 @@ dvr_entry_set_timer(dvr_entry_t *de)

if(now >= stop || de->de_dont_reschedule) {

if(de->de_filename == NULL)
if(htsmsg_is_empty(de->de_files))
dvr_entry_assign_sched_state(de, DVR_MISSED_TIME);
else
_dvr_entry_completed(de);
Expand Down Expand Up @@ -441,6 +441,7 @@ dvr_entry_create(const char *uuid, htsmsg_t *conf)
{
dvr_entry_t *de, *de2;
int64_t start, stop;
htsmsg_t *m;
const char *s;

if (conf) {
Expand Down Expand Up @@ -469,12 +470,17 @@ dvr_entry_create(const char *uuid, htsmsg_t *conf)

idnode_load(&de->de_id, conf);

/* special case, becaous PO_NOSAVE, load ignores it */
/* filenames */
m = htsmsg_get_list(conf, "files");
if (m)
de->de_files = htsmsg_copy(m);

/* special case, because PO_NOSAVE, load ignores it */
if (de->de_title == NULL &&
(s = htsmsg_get_str(conf, "disp_title")) != NULL)
dvr_entry_class_disp_title_set(de, s);

/* special case, becaous PO_NOSAVE, load ignores it */
/* special case, because PO_NOSAVE, load ignores it */
if (de->de_subtitle == NULL &&
(s = htsmsg_get_str(conf, "disp_subtitle")) != NULL)
dvr_entry_class_disp_subtitle_set(de, s);
Expand Down Expand Up @@ -805,7 +811,7 @@ dvr_entry_dec_ref(dvr_entry_t *de)
if(de->de_config != NULL)
LIST_REMOVE(de, de_config_link);

free(de->de_filename);
htsmsg_destroy(de->de_files);
free(de->de_owner);
free(de->de_creator);
free(de->de_comment);
Expand Down Expand Up @@ -879,6 +885,8 @@ dvr_entry_save(dvr_entry_t *de)
lock_assert(&global_lock);

idnode_save(&de->de_id, m);
if (de->de_files)
htsmsg_add_msg(m, "files", htsmsg_copy(de->de_files));
hts_settings_save(m, "dvr/log/%s", idnode_uuid_as_str(&de->de_id));
htsmsg_destroy(m);
}
Expand Down Expand Up @@ -1116,7 +1124,7 @@ dvr_stop_recording(dvr_entry_t *de, int stopcode, int saveconf)
{
if (de->de_rec_state == DVR_RS_PENDING ||
de->de_rec_state == DVR_RS_WAIT_PROGRAM_START ||
de->de_filename == NULL)
htsmsg_is_empty(de->de_files))
dvr_entry_assign_sched_state(de, DVR_MISSED_TIME);
else
_dvr_entry_completed(de);
Expand Down Expand Up @@ -1424,6 +1432,16 @@ dvr_entry_class_config_name_rend(void *o)
return NULL;
}

static const void *
dvr_entry_class_filename_get(void *o)
{
static const char *ret;
dvr_entry_t *de = (dvr_entry_t *)o;
const char *s = dvr_get_filename(de);
ret = s ?: "";
return &ret;
}

static int
dvr_entry_class_channel_set(void *o, const void *v)
{
Expand Down Expand Up @@ -2068,8 +2086,8 @@ const idclass_t dvr_entry_class = {
.type = PT_STR,
.id = "filename",
.name = "Filename",
.off = offsetof(dvr_entry_t, de_filename),
.opts = PO_RDONLY,
.get = dvr_entry_class_filename_get,
.opts = PO_RDONLY | PO_NOSAVE,
},
{
.type = PT_STR,
Expand Down Expand Up @@ -2214,25 +2232,43 @@ dvr_destroy_by_channel(channel_t *ch, int delconf)
}
}

/**
*
*/
const char *
dvr_get_filename(dvr_entry_t *de)
{
htsmsg_field_t *f;
htsmsg_t *m;
const char *s;
if ((f = htsmsg_field_last(de->de_files)) != NULL &&
(m = htsmsg_field_get_map(f)) != NULL &&
(s = htsmsg_get_str(m, "filename")) != NULL)
return s;
else
return NULL;
}

/**
*
*/
int64_t
dvr_get_filesize(dvr_entry_t *de)
{
const char *filename;
struct stat st;

if(de->de_filename == NULL)
filename = dvr_get_filename(de);

if(filename == NULL)
return -1;

if(stat(de->de_filename, &st) != 0)
if(stat(filename, &st) != 0)
return -1;

return st.st_size;
}



/**
*
*/
Expand Down Expand Up @@ -2264,8 +2300,11 @@ void
dvr_entry_delete(dvr_entry_t *de)
{
dvr_config_t *cfg = de->de_config;
htsmsg_t *m;
htsmsg_field_t *f;
time_t t;
struct tm tm;
const char *filename;
char tbuf[64], *rdir;
int r;

Expand All @@ -2281,18 +2320,23 @@ dvr_entry_delete(dvr_entry_t *de)
de->de_creator ?: "",
dvr_entry_get_retention(de));

if(de->de_filename != NULL) {
if(!htsmsg_is_empty(de->de_files)) {
#if ENABLE_INOTIFY
dvr_inotify_del(de);
#endif
rdir = NULL;
if(cfg->dvr_title_dir || cfg->dvr_channel_dir || cfg->dvr_dir_per_day || de->de_directory)
rdir = cfg->dvr_storage;

r = deferred_unlink(de->de_filename, rdir);
if(r && r != -ENOENT)
tvhlog(LOG_WARNING, "dvr", "Unable to remove file '%s' from disk -- %s",
de->de_filename, strerror(-errno));
HTSMSG_FOREACH(f, de->de_files) {
m = htsmsg_field_get_map(f);
if (m == NULL) continue;
filename = htsmsg_get_str(m, "filename");
r = deferred_unlink(filename, rdir);
if(r && r != -ENOENT)
tvhlog(LOG_WARNING, "dvr", "Unable to remove file '%s' from disk -- %s",
filename, strerror(-errno));
}
}
dvr_entry_destroy(de, 1);
}
Expand Down
63 changes: 34 additions & 29 deletions src/dvr/dvr_inotify.c
Expand Up @@ -92,15 +92,16 @@ void dvr_inotify_done ( void )
void dvr_inotify_add ( dvr_entry_t *de )
{
dvr_inotify_entry_t *e;
const char *filename = dvr_get_filename(de);
char *path;

if (_inot_fd < 0)
return;

if (!de->de_filename || de->de_filename[0] == '\0')
if (filename == NULL)
return;

path = strdup(de->de_filename);
path = strdup(filename);

SKEL_ALLOC(dvr_inotify_entry_skel);
dvr_inotify_entry_skel->path = dirname(path);
Expand Down Expand Up @@ -164,25 +165,6 @@ _dvr_inotify_find
return e;
}

/*
* Find DVR entry
*/
static dvr_entry_t *
_dvr_inotify_find2
( dvr_inotify_entry_t *die, const char *name )
{
dvr_entry_t *de = NULL;
char path[512];

snprintf(path, sizeof(path), "%s/%s", die->path, name);

LIST_FOREACH(de, &die->entries, de_inotify_link)
if (de->de_filename && !strcmp(path, de->de_filename))
break;

return de;
}

/*
* File moved
*/
Expand All @@ -192,20 +174,43 @@ _dvr_inotify_moved
{
dvr_inotify_entry_t *die;
dvr_entry_t *de;
char path[PATH_MAX];
const char *filename;
htsmsg_t *m = NULL;
htsmsg_field_t *f = NULL;

if (!(die = _dvr_inotify_find(fd)))
return;

if (!(de = _dvr_inotify_find2(die, from)))
snprintf(path, sizeof(path), "%s/%s", die->path, from);

LIST_FOREACH(de, &die->entries, de_inotify_link) {
if (de->de_files == NULL)
continue;
HTSMSG_FOREACH(f, de->de_files)
if ((m = htsmsg_field_get_list(f)) != NULL) {
filename = htsmsg_get_str(m, "filename");
if (filename && !strcmp(path, filename))
break;
}
if (f)
break;
}

if (!de)
return;

if (to) {
char path[512];
snprintf(path, sizeof(path), "%s/%s", die->path, to);
tvh_str_update(&de->de_filename, path);
dvr_entry_save(de);
} else
dvr_inotify_del(de);
if (f && m) {
if (to) {
snprintf(path, sizeof(path), "%s/%s", die->path, to);
htsmsg_set_str(m, "filename", path);
dvr_entry_save(de);
} else {
htsmsg_field_destroy(de->de_files, f);
if (htsmsg_is_empty(de->de_files))
dvr_inotify_del(de);
}
}

htsp_dvr_entry_update(de);
idnode_notify_changed(&de->de_id);
Expand Down

0 comments on commit 3a36e86

Please sign in to comment.