aboutsummaryrefslogtreecommitdiffstats
path: root/src/StateFile.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'src/StateFile.cxx')
-rw-r--r--src/StateFile.cxx72
1 files changed, 47 insertions, 25 deletions
diff --git a/src/StateFile.cxx b/src/StateFile.cxx
index 75cef2c99..7e9e35cc3 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"
@@ -33,10 +36,11 @@
static constexpr Domain state_file_domain("state_file");
-StateFile::StateFile(AllocatedPath &&_path,
+StateFile::StateFile(AllocatedPath &&_path, unsigned _interval,
Partition &_partition, EventLoop &_loop)
:TimeoutMonitor(_loop),
path(std::move(_path)), path_utf8(path.ToUTF8()),
+ interval(_interval),
partition(_partition),
prev_volume_version(0), prev_output_version(0),
prev_playlist_version(0)
@@ -61,25 +65,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 +104,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,
@@ -116,7 +138,7 @@ void
StateFile::CheckModified()
{
if (!IsActive() && IsModified())
- ScheduleSeconds(2 * 60);
+ ScheduleSeconds(interval);
}
void