Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implemented deferred_unlink() for DVR
  • Loading branch information
perexg committed Apr 24, 2015
1 parent c4d8b7a commit 2b00b88
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/dvr/dvr_db.c
Expand Up @@ -2210,6 +2210,7 @@ dvr_entry_delete(dvr_entry_t *de)
time_t t;
struct tm tm;
char tbuf[64];
int r;

t = dvr_entry_get_start_time(de);
localtime_r(&t, &tm);
Expand All @@ -2227,9 +2228,10 @@ dvr_entry_delete(dvr_entry_t *de)
#if ENABLE_INOTIFY
dvr_inotify_del(de);
#endif
if(unlink(de->de_filename) && errno != ENOENT)
r = deferred_unlink(de->de_filename);
if(r && r != -ENOENT)
tvhlog(LOG_WARNING, "dvr", "Unable to remove file '%s' from disk -- %s",
de->de_filename, strerror(errno));
de->de_filename, strerror(-errno));

/* Also delete directories, if they were created for the recording and if they are empty */

Expand Down
20 changes: 20 additions & 0 deletions src/main.c
Expand Up @@ -305,6 +305,20 @@ gtimer_disarm(gtimer_t *gti)
}
}

/**
*
*/
tasklet_t *
tasklet_arm_alloc(tsk_callback_t *callback, void *opaque)
{
tasklet_t *tsk = calloc(1, sizeof(*tsk));
if (tsk) {
tsk->tsk_allocated = 1;
tasklet_arm(tsk, callback, opaque);
}
return tsk;
}

/**
*
*/
Expand Down Expand Up @@ -339,6 +353,8 @@ tasklet_disarm(tasklet_t *tsk)
TAILQ_REMOVE(&tasklets, tsk, tsk_link);
tsk->tsk_callback(tsk->tsk_opaque, 1);
tsk->tsk_callback = NULL;
if (tsk->tsk_allocated)
free(tsk);
}

pthread_mutex_unlock(&tasklet_lock);
Expand All @@ -355,6 +371,8 @@ tasklet_flush()
TAILQ_REMOVE(&tasklets, tsk, tsk_link);
tsk->tsk_callback(tsk->tsk_opaque, 1);
tsk->tsk_callback = NULL;
if (tsk->tsk_allocated)
free(tsk);
}

pthread_mutex_unlock(&tasklet_lock);
Expand Down Expand Up @@ -538,6 +556,7 @@ main(int argc, char **argv)
pthread_mutex_init(&atomic_lock, NULL);
pthread_cond_init(&gtimer_cond, NULL);
pthread_cond_init(&tasklet_cond, NULL);
TAILQ_INIT(&tasklets);

/* Defaults */
tvheadend_webui_port = 9981;
Expand Down Expand Up @@ -1058,6 +1077,7 @@ main(int argc, char **argv)
tvhftrace("main", spawn_done);

tvhtrace("main", "tasklet enter");
pthread_cond_signal(&tasklet_cond);
pthread_join(tasklet_tid, NULL);
tvhtrace("main", "tasklet thread end");
tasklet_flush();
Expand Down
4 changes: 4 additions & 0 deletions src/tvheadend.h
Expand Up @@ -184,8 +184,10 @@ typedef struct tasklet {
TAILQ_ENTRY(tasklet) tsk_link;
tsk_callback_t *tsk_callback;
void *tsk_opaque;
int tsk_allocated;
} tasklet_t;

tasklet_t *tasklet_arm_alloc(tsk_callback_t *callback, void *opaque);
void tasklet_arm(tasklet_t *tsk, tsk_callback_t *callback, void *opaque);
void tasklet_disarm(tasklet_t *gti);

Expand Down Expand Up @@ -736,6 +738,8 @@ char *url_encode(char *str);

int mpegts_word_count(const uint8_t *tsb, int len, uint32_t mask);

int deferred_unlink(const char *filename);

static inline int32_t deltaI32(int32_t a, int32_t b) { return (a > b) ? (a - b) : (b - a); }
static inline uint32_t deltaU32(uint32_t a, uint32_t b) { return (a > b) ? (a - b) : (b - a); }

Expand Down
31 changes: 31 additions & 0 deletions src/utils.c
Expand Up @@ -638,3 +638,34 @@ mpegts_word_count ( const uint8_t *tsb, int len, uint32_t mask )

return r;
}

static void
deferred_unlink_cb(void *s, int dearmed)
{
if (unlink((const char *)s))
tvherror("main", "unable to remove file '%s'", (const char *)s);
free(s);
}

int
deferred_unlink(const char *filename)
{
char *s;
size_t l;
int r;

l = strlen(filename);
s = malloc(l + 9);
if (s == NULL)
return -ENOMEM;
strcpy(s, filename);
strcpy(s + l, ".removing");
r = rename(filename, s);
if (r) {
r = -errno;
free(s);
return r;
}
tasklet_arm_alloc(deferred_unlink_cb, s);
return 0;
}

0 comments on commit 2b00b88

Please sign in to comment.