Standardize state_file handling routines.

This way it's easier to manage and extend.

git-svn-id: https://svn.musicpd.org/mpd/trunk@4494 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Eric Wong 2006-07-30 23:32:47 +00:00
parent 381d7232a0
commit 12aec5738b
9 changed files with 262 additions and 233 deletions

View File

@ -64,6 +64,7 @@ mpd_headers = \
sig_handlers.h \ sig_handlers.h \
sllist.h \ sllist.h \
song.h \ song.h \
state_file.h \
stats.h \ stats.h \
tag.h \ tag.h \
tagTracker.h \ tagTracker.h \
@ -111,6 +112,7 @@ mpd_SOURCES = \
signal_check.c \ signal_check.c \
sllist.c \ sllist.c \
song.c \ song.c \
state_file.c \
stats.c \ stats.c \
tag.c \ tag.c \
tagTracker.c \ tagTracker.c \

View File

@ -25,6 +25,7 @@
#include "playerData.h" #include "playerData.h"
#include "utils.h" #include "utils.h"
#include "playlist.h" #include "playlist.h"
#include "state_file.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -462,32 +463,19 @@ void printAudioDevices(int fd)
} }
} }
void saveAudioDevicesState(void) void saveAudioDevicesState(FILE *fp)
{ {
char *stateFile;
FILE *fp;
int i; int i;
if (!(stateFile = getStateFile()))
return;
while (!(fp = fopen(stateFile, "a")) && errno == EINTR) ;
if (!fp) {
ERROR("problems opening state file \"%s\" for "
"writing: %s\n", stateFile, strerror(errno));
return;
}
assert(audioOutputArraySize != 0); assert(audioOutputArraySize != 0);
for (i = 0; i < audioOutputArraySize; i++) { for (i = 0; i < audioOutputArraySize; i++) {
fprintf(fp, AUDIO_DEVICE_STATE "%d:%s\n", fprintf(fp, AUDIO_DEVICE_STATE "%d:%s\n",
(int)pdAudioDevicesEnabled[i], (int)pdAudioDevicesEnabled[i],
audioOutputArray[i]->name); audioOutputArray[i]->name);
} }
while (fclose(fp) && errno == EINTR) ;
} }
static void parse_audio_device_state(FILE * fp) void readAudioDevicesState(FILE *fp)
{ {
char buffer[AUDIO_BUFFER_SIZE]; char buffer[AUDIO_BUFFER_SIZE];
int i; int i;
@ -521,29 +509,3 @@ static void parse_audio_device_state(FILE * fp)
} }
} }
void readAudioDevicesState(void)
{
char *stateFile;
FILE *fp;
struct stat st;
if (!(stateFile = getStateFile()))
return;
if (stat(stateFile, &st) < 0) {
DEBUG("failed to stat state file\n");
return;
}
if (!S_ISREG(st.st_mode)) {
ERROR("state file \"%s\" is not a regular file\n", stateFile);
exit(EXIT_FAILURE);
}
fp = fopen(stateFile, "r");
if (!fp) {
ERROR("problems opening state file \"%s\" for "
"reading: %s\n", stateFile, strerror(errno));
exit(EXIT_FAILURE);
}
parse_audio_device_state(fp);
fclose(fp);
}

View File

@ -75,9 +75,9 @@ int disableAudioDevice(int fd, int device);
void printAudioDevices(int fd); void printAudioDevices(int fd);
void readAudioDevicesState(); void readAudioDevicesState(FILE *fp);
void saveAudioDevicesState(); void saveAudioDevicesState(FILE *fp);
void loadAudioDrivers(); void loadAudioDrivers();
#endif #endif

View File

@ -35,6 +35,7 @@
#include "inputPlugin.h" #include "inputPlugin.h"
#include "audioOutput.h" #include "audioOutput.h"
#include "inputStream.h" #include "inputStream.h"
#include "state_file.h"
#include "tag.h" #include "tag.h"
#include "tagTracker.h" #include "tagTracker.h"
#include "dbUtils.h" #include "dbUtils.h"
@ -346,7 +347,6 @@ static void startMainProcess(void)
if (pid > 0) { if (pid > 0) {
initInputStream(); initInputStream();
initReplayGainState(); initReplayGainState();
readAudioDevicesState();
/* free stuff we don't need */ /* free stuff we don't need */
freeAllListenSockets(); freeAllListenSockets();
@ -581,7 +581,7 @@ int main(int argc, char *argv[])
my_usleep(1); my_usleep(1);
openVolumeDevice(); openVolumeDevice();
readPlaylistState(); read_state_file();
while (COMMAND_RETURN_KILL != doIOForInterfaces()) { while (COMMAND_RETURN_KILL != doIOForInterfaces()) {
if (COMMAND_RETURN_KILL == handlePendingSignals()) if (COMMAND_RETURN_KILL == handlePendingSignals())
@ -591,8 +591,7 @@ int main(int argc, char *argv[])
readDirectoryDBIfUpdateIsFinished(); readDirectoryDBIfUpdateIsFinished();
} }
savePlaylistState(); write_state_file();
saveAudioDevicesState();
freeAllInterfaces(); freeAllInterfaces();
closeAllListenSockets(); closeAllListenSockets();

View File

@ -27,6 +27,7 @@
#include "path.h" #include "path.h"
#include "utils.h" #include "utils.h"
#include "sig_handlers.h" #include "sig_handlers.h"
#include "state_file.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -92,16 +93,6 @@ static void swapOrder(int a, int b);
static int playPlaylistOrderNumber(int fd, int orderNum); static int playPlaylistOrderNumber(int fd, int orderNum);
static void randomizeOrder(int start, int end); static void randomizeOrder(int start, int end);
char *getStateFile(void)
{
ConfigParam *param = parseConfigFilePath(CONF_STATE_FILE, 0);
if (!param)
return NULL;
return param->value;
}
static void incrPlaylistVersion(void) static void incrPlaylistVersion(void)
{ {
static unsigned long max = ((mpd_uint32) 1 << 31) - 1; static unsigned long max = ((mpd_uint32) 1 << 31) - 1;
@ -256,208 +247,141 @@ int showPlaylist(int fd)
return 0; return 0;
} }
void savePlaylistState(void) void savePlaylistState(FILE *fp)
{ {
char *stateFile = getStateFile(); fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE);
switch (playlist_state) {
if (stateFile) { case PLAYLIST_STATE_PLAY:
FILE *fp; switch (getPlayerState()) {
case PLAYER_STATE_PAUSE:
while (!(fp = fopen(stateFile, "w")) && errno == EINTR) ; fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PAUSE);
if (!fp) {
ERROR("problems opening state file \"%s\" for "
"writing: %s\n", stateFile, strerror(errno));
return;
}
fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE);
switch (playlist_state) {
case PLAYLIST_STATE_PLAY:
switch (getPlayerState()) {
case PLAYER_STATE_PAUSE:
fprintf(fp, "%s\n",
PLAYLIST_STATE_FILE_STATE_PAUSE);
break;
default:
fprintf(fp, "%s\n",
PLAYLIST_STATE_FILE_STATE_PLAY);
}
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CURRENT,
playlist.order[playlist.current]);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_TIME,
getPlayerElapsedTime());
break; break;
default: default:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_STOP); fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PLAY);
break;
} }
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_RANDOM, fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CURRENT,
playlist.random); playlist.order[playlist.current]);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_REPEAT, fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_TIME,
playlist.repeat); getPlayerElapsedTime());
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE, break;
(int)(getPlayerCrossFade())); default:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN); fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_STOP);
fflush(fp); break;
showPlaylist(fileno(fp));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END);
while (fclose(fp) && errno == EINTR) ;
} }
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_RANDOM, playlist.random);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_REPEAT, playlist.repeat);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE,
(int)(getPlayerCrossFade()));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN);
fflush(fp);
showPlaylist(fileno(fp));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END);
} }
void loadPlaylistFromStateFile(FILE *fp, char *buffer, int state, int current, static void loadPlaylistFromStateFile(FILE *fp, char *buffer,
int time) int state, int current, int time)
{ {
char *temp; char *temp;
int song; int song;
char *stateFile = getStateFile();
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
ERROR("error parsing state file \"%s\"\n", stateFile); state_file_fatal();
exit(EXIT_FAILURE);
}
while (strcmp(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) { while (strcmp(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
song = atoi(strtok(buffer, ":")); song = atoi(strtok(buffer, ":"));
if (!(temp = strtok(NULL, ""))) { if (!(temp = strtok(NULL, "")))
ERROR("error parsing state file \"%s\"\n", stateFile); state_file_fatal();
exit(EXIT_FAILURE);
}
if (!addToPlaylist(STDERR_FILENO, temp, 0) && current == song) { if (!addToPlaylist(STDERR_FILENO, temp, 0) && current == song) {
if (state != PLAYER_STATE_STOP) { if (state != PLAYER_STATE_STOP) {
playPlaylist(STDERR_FILENO, playPlaylist(STDERR_FILENO,
playlist.length - 1, 0); playlist.length - 1, 0);
} }
if (state == PLAYER_STATE_PAUSE) { if (state == PLAYER_STATE_PAUSE) {
playerPause(STDERR_FILENO); playerPause(STDERR_FILENO);
} }
if (state != PLAYER_STATE_STOP) { if (state != PLAYER_STATE_STOP) {
seekSongInPlaylist(STDERR_FILENO, seekSongInPlaylist(STDERR_FILENO,
playlist.length - 1, playlist.length - 1, time);
time);
} }
} }
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
ERROR("error parsing state file \"%s\"\n", stateFile); state_file_fatal();
exit(EXIT_FAILURE);
}
} }
} }
void readPlaylistState(void) void readPlaylistState(FILE *fp)
{ {
char *stateFile = getStateFile(); int current = -1;
int time = 0;
int state = PLAYER_STATE_STOP;
char buffer[PLAYLIST_BUFFER_SIZE];
if (stateFile) { while (myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
FILE *fp; if (strncmp(buffer, PLAYLIST_STATE_FILE_STATE,
struct stat st; strlen(PLAYLIST_STATE_FILE_STATE)) == 0) {
int current = -1; if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
int time = 0; PLAYLIST_STATE_FILE_STATE_PLAY) == 0) {
int state = PLAYER_STATE_STOP; state = PLAYER_STATE_PLAY;
char buffer[PLAYLIST_BUFFER_SIZE];
if (stat(stateFile, &st) < 0) {
DEBUG("failed to stat state file\n");
return;
}
if (!S_ISREG(st.st_mode)) {
ERROR("state file \"%s\" is not a regular "
"file\n", stateFile);
exit(EXIT_FAILURE);
}
fp = fopen(stateFile, "r");
if (!fp) {
ERROR("problems opening state file \"%s\" for "
"reading: %s\n", stateFile, strerror(errno));
exit(EXIT_FAILURE);
}
while (myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
if (strncmp(buffer, PLAYLIST_STATE_FILE_STATE,
strlen(PLAYLIST_STATE_FILE_STATE)) == 0) {
if (strcmp(&(buffer
[strlen
(PLAYLIST_STATE_FILE_STATE)]),
PLAYLIST_STATE_FILE_STATE_PLAY) ==
0) {
state = PLAYER_STATE_PLAY;
} else if (strcmp(&(buffer
[strlen
(PLAYLIST_STATE_FILE_STATE)]),
PLAYLIST_STATE_FILE_STATE_PAUSE)
== 0) {
state = PLAYER_STATE_PAUSE;
}
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_TIME,
strlen(PLAYLIST_STATE_FILE_TIME)) ==
0) {
time =
atoi(&
(buffer
[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else } else
if (strncmp if (strcmp
(buffer, PLAYLIST_STATE_FILE_REPEAT, (&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
strlen(PLAYLIST_STATE_FILE_REPEAT)) == 0) { PLAYLIST_STATE_FILE_STATE_PAUSE)
if (strcmp == 0) {
(& state = PLAYER_STATE_PAUSE;
(buffer
[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) {
setPlaylistRepeatStatus(STDERR_FILENO,
1);
} else
setPlaylistRepeatStatus(STDERR_FILENO,
0);
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_CROSSFADE,
strlen(PLAYLIST_STATE_FILE_CROSSFADE)) == 0) {
setPlayerCrossFade(atoi
(&
(buffer
[strlen
(PLAYLIST_STATE_FILE_CROSSFADE)])));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_RANDOM,
strlen(PLAYLIST_STATE_FILE_RANDOM)) == 0) {
if (strcmp
(&
(buffer
[strlen(PLAYLIST_STATE_FILE_RANDOM)]),
"1") == 0) {
setPlaylistRandomStatus(STDERR_FILENO,
1);
} else
setPlaylistRandomStatus(STDERR_FILENO,
0);
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_CURRENT,
strlen(PLAYLIST_STATE_FILE_CURRENT))
== 0) {
if (strlen(buffer) ==
strlen(PLAYLIST_STATE_FILE_CURRENT)) {
ERROR("error parsing state "
"file \"%s\"\n", stateFile);
exit(EXIT_FAILURE);
}
current = atoi(&(buffer
[strlen
(PLAYLIST_STATE_FILE_CURRENT)]));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_PLAYLIST_BEGIN,
strlen(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)
) == 0) {
if (state == PLAYER_STATE_STOP)
current = -1;
loadPlaylistFromStateFile(fp, buffer, state,
current, time);
} }
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_TIME,
strlen(PLAYLIST_STATE_FILE_TIME)) == 0) {
time =
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_REPEAT,
strlen(PLAYLIST_STATE_FILE_REPEAT)) == 0) {
if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) {
setPlaylistRepeatStatus(STDERR_FILENO, 1);
} else
setPlaylistRepeatStatus(STDERR_FILENO, 0);
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_CROSSFADE,
strlen(PLAYLIST_STATE_FILE_CROSSFADE)) == 0) {
setPlayerCrossFade(atoi
(&
(buffer
[strlen
(PLAYLIST_STATE_FILE_CROSSFADE)])));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_RANDOM,
strlen(PLAYLIST_STATE_FILE_RANDOM)) == 0) {
if (strcmp
(&
(buffer
[strlen(PLAYLIST_STATE_FILE_RANDOM)]),
"1") == 0) {
setPlaylistRandomStatus(STDERR_FILENO, 1);
} else
setPlaylistRandomStatus(STDERR_FILENO, 0);
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_CURRENT,
strlen(PLAYLIST_STATE_FILE_CURRENT))
== 0) {
if (strlen(buffer) ==
strlen(PLAYLIST_STATE_FILE_CURRENT))
state_file_fatal();
current = atoi(&(buffer
[strlen
(PLAYLIST_STATE_FILE_CURRENT)]));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_PLAYLIST_BEGIN,
strlen(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)
) == 0) {
if (state == PLAYER_STATE_STOP)
current = -1;
loadPlaylistFromStateFile(fp, buffer, state,
current, time);
} }
fclose(fp);
} }
} }

View File

@ -34,9 +34,9 @@ void initPlaylist();
void finishPlaylist(); void finishPlaylist();
void readPlaylistState(); void readPlaylistState(FILE *);
void savePlaylistState(); void savePlaylistState(FILE *);
int clearPlaylist(int fd); int clearPlaylist(int fd);
@ -116,6 +116,4 @@ int playlistChangesPosId(int fd, mpd_uint32 version);
int PlaylistInfo(int fd, char *utf8file, int detail); int PlaylistInfo(int fd, char *utf8file, int detail);
char *getStateFile();
#endif #endif

112
src/state_file.c Normal file
View File

@ -0,0 +1,112 @@
/* the Music Player Daemon (MPD)
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
* 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 "gcc.h"
#include "log.h"
#include "audio.h"
#include "playlist.h"
#include "utils.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
static struct _sf_cb {
void (*reader)(FILE *);
void (*writer)(FILE *);
} sf_callbacks [] = {
{ readAudioDevicesState, saveAudioDevicesState },
{ readPlaylistState, savePlaylistState },
};
static const char *sfpath = NULL;
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)
{
int i;
FILE *fp;
if (!sfpath)
return;
while (!(fp = fopen(sfpath, "w")) && errno == EINTR);
if (mpd_unlikely(!fp)) {
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;
int i;
FILE *fp;
get_state_file_path();
if (!sfpath)
return;
if (stat(sfpath, &st) < 0) {
DEBUG("failed to stat state file: %s\n", sfpath);
return;
}
if (!S_ISREG(st.st_mode)) {
ERROR("state file \"%s\" is not a regular file\n", sfpath);
exit(EXIT_FAILURE);
}
while (!(fp = fopen(sfpath, "r")) && errno == EINTR);
if (mpd_unlikely(!fp)) {
ERROR("problems opening state file \"%s\" for reading: %s\n",
sfpath, strerror(errno));
exit(EXIT_FAILURE);
}
for (i = 0; i < ARRAY_SIZE(sf_callbacks); i++) {
sf_callbacks[i].reader(fp);
rewind(fp);
}
while(fclose(fp) && errno == EINTR) /* nothing */;
}
void mpd_noreturn state_file_fatal(void)
{
ERROR("error parsing state file \"%s\"\n", sfpath);
exit(EXIT_FAILURE);
}

30
src/state_file.h Normal file
View File

@ -0,0 +1,30 @@
/* the Music Player Daemon (MPD)
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
* 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
*/
#ifndef STATE_FILE_H
#define STATE_FILE_H
#include "gcc.h"
#include <stdio.h>
void write_state_file(void);
void read_state_file(void);
void mpd_noreturn state_file_fatal(void);
#endif /* STATE_FILE_H */

View File

@ -29,6 +29,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
char *myFgets(char *buffer, int bufferSize, FILE * fp); char *myFgets(char *buffer, int bufferSize, FILE * fp);
char *strDupToUpper(char *str); char *strDupToUpper(char *str);