Updated package version
[lms] / lightmediascanner / src / plugins / dummy / dummy.c
1 /**
2  * Copyright (C) 2007 by INdT
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * Dummy plugin demonstrating the basic lightmediascanner_plugin API,
25  * it just write paths to /tmp/dummy.log.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define _XOPEN_SOURCE 600
33 #include <lightmediascanner_plugin.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41
42 static const char _name[] = "dummy";
43
44 struct plugin {
45     struct lms_plugin plugin;
46     int fd;
47 };
48
49 static void *
50 _match(struct plugin *p, const char *path, int len, int base)
51 {
52     return (void*)1;
53 }
54
55 static int
56 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
57 {
58     write(plugin->fd, finfo->path, finfo->path_len);
59     write(plugin->fd, "\n", 1);
60     return 0;
61 }
62
63 static int
64 _close(struct plugin *plugin)
65 {
66     free(plugin);
67     return 0;
68 }
69
70 static int
71 _setup(struct plugin *plugin,  struct lms_context *ctxt)
72 {
73     return 0;
74 }
75
76 static int
77 _start(struct plugin *plugin, struct lms_context *ctxt)
78 {
79     char logfile[PATH_MAX];
80
81     snprintf(logfile, sizeof(logfile), "/tmp/dummy-%d.log", getuid());
82     plugin->fd = open(logfile, O_WRONLY | O_CREAT, 0600);
83     if (plugin->fd < 0) {
84         perror("open");
85         return -1;
86     }
87
88     return 0;
89 }
90
91 static int
92 _finish(struct plugin *plugin, struct lms_context *ctxt)
93 {
94     if (close(plugin->fd) != 0)
95         perror("close");
96
97     plugin->fd = 0;
98
99     return 0;
100 }
101
102 API struct lms_plugin *
103 lms_plugin_open(void)
104 {
105     struct plugin *plugin;
106
107     plugin = malloc(sizeof(*plugin));
108     plugin->plugin.name = _name;
109     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
110     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
111     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
112     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
113     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
114     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
115
116     return (struct lms_plugin *)plugin;
117 }