2006-07-31 01:32:47 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2006-07-31 01:32:47 +02:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../config.h"
|
|
|
|
#include "state_file.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "utils.h"
|
2006-07-31 01:32:54 +02:00
|
|
|
#include "volume.h"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2008-12-02 02:22:43 +01:00
|
|
|
#include <glib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2006-07-31 01:32:47 +02:00
|
|
|
|
|
|
|
static struct _sf_cb {
|
|
|
|
void (*reader)(FILE *);
|
|
|
|
void (*writer)(FILE *);
|
|
|
|
} sf_callbacks [] = {
|
2006-07-31 01:32:54 +02:00
|
|
|
{ read_sw_volume_state, save_sw_volume_state },
|
2006-07-31 01:32:47 +02:00
|
|
|
{ readAudioDevicesState, saveAudioDevicesState },
|
|
|
|
{ readPlaylistState, savePlaylistState },
|
|
|
|
};
|
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static const char *sfpath;
|
2006-07-31 01:32:47 +02:00
|
|
|
|
|
|
|
static void get_state_file_path(void)
|
|
|
|
{
|
|
|
|
ConfigParam *param;
|
|
|
|
if (sfpath)
|
|
|
|
return;
|
|
|
|
param = parseConfigFilePath(CONF_STATE_FILE, 0);
|
|
|
|
if (param)
|
|
|
|
sfpath = (const char *)param->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_state_file(void)
|
|
|
|
{
|
2008-03-26 11:38:48 +01:00
|
|
|
unsigned int i;
|
2006-07-31 01:32:47 +02:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (!sfpath)
|
|
|
|
return;
|
2008-09-23 22:38:36 +02:00
|
|
|
fp = fopen(sfpath, "w");
|
2008-12-02 02:22:43 +01:00
|
|
|
if (G_UNLIKELY(!fp)) {
|
2006-07-31 01:32:47 +02:00
|
|
|
ERROR("problems opening state file \"%s\" for writing: %s\n",
|
|
|
|
sfpath, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(sf_callbacks); i++)
|
|
|
|
sf_callbacks[i].writer(fp);
|
|
|
|
|
|
|
|
while(fclose(fp) && errno == EINTR) /* nothing */;
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_state_file(void)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2008-03-26 11:38:48 +01:00
|
|
|
unsigned int i;
|
2006-07-31 01:32:47 +02:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
get_state_file_path();
|
|
|
|
if (!sfpath)
|
|
|
|
return;
|
|
|
|
if (stat(sfpath, &st) < 0) {
|
|
|
|
DEBUG("failed to stat state file: %s\n", sfpath);
|
|
|
|
return;
|
|
|
|
}
|
2007-05-26 20:15:54 +02:00
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
FATAL("state file \"%s\" is not a regular file\n", sfpath);
|
2006-07-31 01:32:47 +02:00
|
|
|
|
|
|
|
while (!(fp = fopen(sfpath, "r")) && errno == EINTR);
|
2008-12-02 02:22:43 +01:00
|
|
|
if (G_UNLIKELY(!fp)) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems opening state file \"%s\" for reading: %s\n",
|
2006-07-31 01:32:47 +02:00
|
|
|
sfpath, strerror(errno));
|
|
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(sf_callbacks); i++) {
|
|
|
|
sf_callbacks[i].reader(fp);
|
|
|
|
rewind(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(fclose(fp) && errno == EINTR) /* nothing */;
|
|
|
|
}
|
|
|
|
|
2008-12-02 02:42:19 +01:00
|
|
|
void G_GNUC_NORETURN state_file_fatal(void)
|
2006-07-31 01:32:47 +02:00
|
|
|
{
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("error parsing state file \"%s\"\n", sfpath);
|
2006-07-31 01:32:47 +02:00
|
|
|
}
|
|
|
|
|