diff options
Diffstat (limited to 'src/StateFile.cxx')
-rw-r--r-- | src/StateFile.cxx | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/src/StateFile.cxx b/src/StateFile.cxx index 75cef2c99..e0f0cedb1 100644 --- a/src/StateFile.cxx +++ b/src/StateFile.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * Copyright (C) 2003-2014 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -19,12 +19,15 @@ #include "config.h" #include "StateFile.hxx" -#include "OutputState.hxx" -#include "PlaylistState.hxx" -#include "TextFile.hxx" +#include "output/OutputState.hxx" +#include "queue/PlaylistState.hxx" +#include "fs/io/TextFile.hxx" +#include "fs/io/FileOutputStream.hxx" +#include "fs/io/BufferedOutputStream.hxx" #include "Partition.hxx" -#include "Volume.hxx" -#include "event/Loop.hxx" +#include "Instance.hxx" +#include "mixer/Volume.hxx" +#include "SongLoader.hxx" #include "fs/FileSystem.hxx" #include "util/Domain.hxx" #include "Log.hxx" @@ -61,25 +64,35 @@ StateFile::IsModified() const partition.pc); } +inline void +StateFile::Write(BufferedOutputStream &os) +{ + save_sw_volume_state(os); + audio_output_state_save(os, partition.outputs); + playlist_state_save(os, partition.playlist, partition.pc); +} + +inline bool +StateFile::Write(OutputStream &os, Error &error) +{ + BufferedOutputStream bos(os); + Write(bos); + return bos.Flush(error); +} + void StateFile::Write() { FormatDebug(state_file_domain, "Saving state file %s", path_utf8.c_str()); - FILE *fp = FOpen(path, FOpenMode::WriteText); - if (gcc_unlikely(!fp)) { - FormatErrno(state_file_domain, "failed to create %s", - path_utf8.c_str()); + Error error; + FileOutputStream fos(path, error); + if (!fos.IsDefined() || !Write(fos, error) || !fos.Commit(error)) { + LogError(error); return; } - save_sw_volume_state(fp); - audio_output_state_save(fp); - playlist_state_save(fp, partition.playlist, partition.pc); - - fclose(fp); - RememberVersions(); } @@ -90,18 +103,26 @@ StateFile::Read() FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str()); - TextFile file(path); + Error error; + TextFile file(path, error); if (file.HasFailed()) { - FormatErrno(state_file_domain, "failed to open %s", - path_utf8.c_str()); + LogError(error); return; } +#ifdef ENABLE_DATABASE + const SongLoader song_loader(partition.instance.database, + partition.instance.storage); +#else + const SongLoader song_loader(nullptr, nullptr); +#endif + const char *line; - while ((line = file.ReadLine()) != NULL) { - success = read_sw_volume_state(line) || - audio_output_state_read(line) || - playlist_state_restore(line, file, partition.playlist, + while ((line = file.ReadLine()) != nullptr) { + success = read_sw_volume_state(line, partition.outputs) || + audio_output_state_read(line, partition.outputs) || + playlist_state_restore(line, file, song_loader, + partition.playlist, partition.pc); if (!success) FormatError(state_file_domain, |