StateFile.cxx 3.07 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21 22 23
#include "StateFile.hxx"
#include "OutputState.hxx"
#include "PlaylistState.hxx"
24
#include "TextFile.hxx"
25
#include "Partition.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "Volume.hxx"
27
#include "event/Loop.hxx"
28
#include "fs/FileSystem.hxx"
29 30
#include "util/Domain.hxx"
#include "Log.hxx"
31

32
#include <string.h>
33

34
static constexpr Domain state_file_domain("state_file");
35

36 37 38 39
StateFile::StateFile(Path &&_path,
		     Partition &_partition, EventLoop &_loop)
	:TimeoutMonitor(_loop),
	 path(std::move(_path)), path_utf8(path.ToUTF8()),
40
	 partition(_partition),
41 42
	 prev_volume_version(0), prev_output_version(0),
	 prev_playlist_version(0)
43
{
44
}
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void
StateFile::RememberVersions()
{
	prev_volume_version = sw_volume_state_get_hash();
	prev_output_version = audio_output_state_get_version();
	prev_playlist_version = playlist_state_get_hash(&partition.playlist,
							&partition.pc);
}

bool
StateFile::IsModified() const
{
	return prev_volume_version != sw_volume_state_get_hash() ||
		prev_output_version != audio_output_state_get_version() ||
		prev_playlist_version != playlist_state_get_hash(&partition.playlist,
								 &partition.pc);
}

64 65 66
void
StateFile::Write()
{
67 68
	FormatDebug(state_file_domain,
		    "Saving state file %s", path_utf8.c_str());
69

70
	FILE *fp = FOpen(path, FOpenMode::WriteText);
71 72 73
	if (gcc_unlikely(!fp)) {
		FormatErrno(state_file_domain, "failed to create %s",
			    path_utf8.c_str());
74 75 76
		return;
	}

77
	save_sw_volume_state(fp);
78
	audio_output_state_save(fp);
79
	playlist_state_save(fp, &partition.playlist, &partition.pc);
80

81
	fclose(fp);
82

83
	RememberVersions();
84 85
}

86 87
void
StateFile::Read()
88
{
89
	bool success;
90

91
	FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
92

93
	TextFile file(path);
94
	if (file.HasFailed()) {
95 96
		FormatErrno(state_file_domain, "failed to open %s",
			    path_utf8.c_str());
97
		return;
98
	}
99

100
	const char *line;
101
	while ((line = file.ReadLine()) != NULL) {
102
		success = read_sw_volume_state(line) ||
103
			audio_output_state_read(line) ||
104 105
			playlist_state_restore(line, file, &partition.playlist,
					       &partition.pc);
106
		if (!success)
107 108 109
			FormatError(state_file_domain,
				    "Unrecognized line in state file: %s",
				    line);
110
	}
111

112
	RememberVersions();
113
}
114

115 116
void
StateFile::CheckModified()
117
{
118 119
	if (!IsActive() && IsModified())
		ScheduleSeconds(2 * 60);
120 121
}

122
void
123
StateFile::OnTimeout()
124
{
125
	Write();
126
}