summaryrefslogtreecommitdiff
path: root/src/writer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/writer.c')
-rw-r--r--src/writer.c96
1 files changed, 17 insertions, 79 deletions
diff --git a/src/writer.c b/src/writer.c
index f89adfb..c767c5b 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -28,8 +28,6 @@
#include <gst/gst.h>
#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
#include <errno.h>
#include <string.h>
@@ -39,23 +37,7 @@
#include "datatypes.h"
#include "log.h"
-
-static int compute_filname(struct tm* time, const char* type, file_t* file, const char* format, const char* dir, mode_t mode)
-{
- log_printf(INFO, "%s time: %02d:%02d:%02d on %d.%d.%d", type, time->tm_hour, time->tm_min, time->tm_sec, time->tm_mday, time->tm_mon+1, time->tm_year+1900);
-
- char name[256];
- strftime(name, sizeof(name), format, time);
- asprintf(&(file->path_), "%s/%s", dir, name);
- if(!file->path_)
- return -2;
-
- log_printf(INFO, "%s filename is: %s(.?)", type, file->path_);
- file->fd_ = -1;
- file->mode_ = mode;
-
- return 0;
-}
+#include "file_list.h"
static int init_time_boundaries(writer_t* writer)
{
@@ -68,8 +50,8 @@ static int init_time_boundaries(writer_t* writer)
struct tm bd_time;
localtime_r(&(now.tv_sec), &bd_time);
- int ret = compute_filname(&bd_time, "current", &(writer->current_), writer->name_format_, writer->output_dir_, writer->mode_);
- if(ret) return ret;
+ writer->current_ = file_list_add(&(writer->files_), &bd_time, "current", writer->name_format_, writer->output_dir_, writer->mode_);
+ if(writer->current_ == NULL) return -2;
bd_time.tm_sec = 0;
bd_time.tm_min = 0;
@@ -80,7 +62,10 @@ static int init_time_boundaries(writer_t* writer)
struct timespec b = { T, 0 };
writer->next_boundary_ = b;
- return compute_filname(&bd_time, "next", &(writer->next_), writer->name_format_, writer->output_dir_, writer->mode_);
+ writer->next_ = file_list_add(&(writer->files_), &bd_time, "next", writer->name_format_, writer->output_dir_, writer->mode_);
+ if(writer->next_ == NULL) return -2;
+
+ return 0;
}
int writer_init(writer_t* writer, GMainLoop *loop, const char* name_format, mode_t mode, const char* output_dir, int interval, int offset)
@@ -104,51 +89,12 @@ int writer_init(writer_t* writer, GMainLoop *loop, const char* name_format, mode
writer->output_dir_ = output_dir;
writer->interval_ = interval * GST_MSECOND;
writer->offset_ = offset * GST_MSECOND;
- writer->current_.path_ = NULL;
- writer->next_.path_ = NULL;
writer->clock_id_ = NULL;
writer->thread_ = NULL;
- return init_time_boundaries(writer);
-}
-
-static int open_file(file_t* file)
-{
- if(file->fd_ > 0) // file already open!
- return -1;
-
- char* orig_path = file->path_;
- int cnt = 0;
- do {
- file->fd_ = open(file->path_, O_WRONLY | O_CREAT | O_EXCL, file->mode_);
- if(file->fd_ < 0) {
- if(errno != EEXIST) {
- // TODO: thread safe strerror
- log_printf(ERROR, "can't open file '%s': %s", file->path_, strerror(errno));
- if(orig_path != file->path_)
- free(orig_path);
- return -1;
- }
- cnt++;
- char* tmp;
- asprintf(&tmp, "%s.%d", orig_path, cnt);
- if(!tmp) {
- if(orig_path != file->path_)
- free(orig_path);
- return -2;
- }
-
- if(file->path_ != orig_path)
- free(file->path_);
- file->path_ = tmp;
- }
- fchmod(file->fd_, file->mode_);
- }
- while(file->fd_ < 0);
-
- if(orig_path != file->path_)
- free(orig_path);
+ int ret = file_list_init(&(writer->files_));
+ if(ret) return ret;
- return 0;
+ return init_time_boundaries(writer);
}
static int check_boundaries(writer_t* writer)
@@ -164,28 +110,24 @@ static int check_boundaries(writer_t* writer)
localtime_r(&(now.tv_sec), &now_bd);
log_printf(DEBUG, "boundary reached! it's now: %02d:%02d:%02d.%06d on %d.%d.%d", now_bd.tm_hour, now_bd.tm_min, now_bd.tm_sec, now.tv_nsec/1000, now_bd.tm_mday, now_bd.tm_mon+1, now_bd.tm_year+1900);
- int ret = open_file(&(writer->next_));
+ int ret = open_file(writer->next_);
// if(ret) return ret; // TODO: stop writer on open_file error ???
// - add new file
// - remove old file
- writer->old_ = writer->current_;
+ int old_fd = writer->current_->fd_;
writer->current_ = writer->next_;
writer->next_boundary_.tv_sec += 3600;
struct tm bd_time;
localtime_r(&(writer->next_boundary_.tv_sec), &bd_time);
- ret = compute_filname(&bd_time, "next", &(writer->next_), writer->name_format_, writer->output_dir_, writer->mode_);
- if(ret) return ret;
-
+ writer->next_ = file_list_add(&(writer->files_), &bd_time, "next", writer->name_format_, writer->output_dir_, writer->mode_);
+ if(writer->next_ == NULL) return -2;
// - call post processing script from fd_removed callback
- if(writer->old_.fd_ > 0)
- close(writer->old_.fd_);
- if(writer->old_.path_)
- free(writer->old_.path_);
+ file_list_remove(&(writer->files_), old_fd);
}
return 0;
@@ -220,7 +162,7 @@ int writer_start(writer_t* writer)
if(!writer)
return -1;
- int ret = open_file(&(writer->current_));
+ int ret = open_file(writer->current_);
if(ret)
return ret;
@@ -245,11 +187,7 @@ void writer_stop(writer_t* writer)
if(!writer)
return;
- if(writer->current_.path_) free(writer->current_.path_);
- if(writer->current_.fd_ >= 0) close(writer->current_.fd_);
- if(writer->next_.path_) free(writer->next_.path_);
- if(writer->next_.fd_ >= 0) close(writer->next_.fd_);
-
+ file_list_clear(&(writer->files_));
if(writer->clock_id_) {
gst_clock_id_unschedule(writer->clock_id_);
}