1
/* the Music Player Daemon (MPD)
2
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3
* This project's homepage is: http://www.musicpd.org
2
* Copyright (C) 2003-2009 The Music Player Daemon Project
3
* http://www.musicpd.org
5
5
* This program is free software; you can redistribute it and/or modify
6
6
* it under the terms of the GNU General Public License as published by
11
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
13
* GNU General Public License for more details.
14
* You should have received a copy of the GNU 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
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#include "../config.h"
20
20
#include "state_file.h"
21
#include "output_state.h"
24
22
#include "playlist.h"
26
23
#include "volume.h"
29
27
#include <string.h>
31
#define G_LOG_DOMAIN "state_file"
32
33
static struct _sf_cb {
33
34
void (*reader)(FILE *);
38
39
{ readPlaylistState, savePlaylistState },
41
static const char *sfpath;
43
static void get_state_file_path(void)
48
param = parseConfigFilePath(CONF_STATE_FILE, 0);
50
sfpath = (const char *)param->value;
53
void write_state_file(void)
42
static char *state_file_path;
44
/** the GLib source id for the save timer */
45
static guint save_state_source_id;
48
state_file_write(void)
53
if (state_file_path == NULL)
60
fp = fopen(sfpath, "w");
56
fp = fopen(state_file_path, "w");
61
57
if (G_UNLIKELY(!fp)) {
62
ERROR("problems opening state file \"%s\" for writing: %s\n",
63
sfpath, strerror(errno));
58
g_warning("failed to create %s: %s",
59
state_file_path, strerror(errno));
67
for (i = 0; i < ARRAY_SIZE(sf_callbacks); i++)
63
for (i = 0; i < G_N_ELEMENTS(sf_callbacks); i++)
68
64
sf_callbacks[i].writer(fp);
70
66
while(fclose(fp) && errno == EINTR) /* nothing */;
73
void read_state_file(void)
79
get_state_file_path();
82
if (stat(sfpath, &st) < 0) {
83
DEBUG("failed to stat state file: %s\n", sfpath);
86
if (!S_ISREG(st.st_mode))
87
FATAL("state file \"%s\" is not a regular file\n", sfpath);
89
while (!(fp = fopen(sfpath, "r")) && errno == EINTR);
75
assert(state_file_path != NULL);
77
g_debug("Saving state file");
79
fp = fopen(state_file_path, "r");
90
80
if (G_UNLIKELY(!fp)) {
91
FATAL("problems opening state file \"%s\" for reading: %s\n",
92
sfpath, strerror(errno));
81
g_warning("failed to open %s: %s",
82
state_file_path, strerror(errno));
94
for (i = 0; i < ARRAY_SIZE(sf_callbacks); i++) {
85
for (i = 0; i < G_N_ELEMENTS(sf_callbacks); i++) {
95
86
sf_callbacks[i].reader(fp);
99
90
while(fclose(fp) && errno == EINTR) /* nothing */;
102
void G_GNUC_NORETURN state_file_fatal(void)
104
FATAL("error parsing state file \"%s\"\n", sfpath);
94
* This function is called every 5 minutes by the GLib main loop, and
95
* saves the state file.
98
timer_save_state_file(G_GNUC_UNUSED gpointer data)
105
state_file_init(const char *path)
107
assert(state_file_path == NULL);
112
state_file_path = g_strdup(path);
115
save_state_source_id = g_timeout_add(5 * 60 * 1000,
116
timer_save_state_file, NULL);
120
state_file_finish(void)
122
if (save_state_source_id != 0)
123
g_source_remove(save_state_source_id);
125
if (state_file_path != NULL)
128
g_free(state_file_path);