StateFile.hxx 2.1 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 21
#ifndef MPD_STATE_FILE_HXX
#define MPD_STATE_FILE_HXX
22

23
#include "StateFileConfig.hxx"
24
#include "event/TimerEvent.hxx"
25
#include "fs/AllocatedPath.hxx"
26
#include "util/Compiler.h"
27 28

#include <string>
29
#include <chrono>
30

31
struct Partition;
32 33
class OutputStream;
class BufferedOutputStream;
34

35
class StateFile final {
36 37
	const StateFileConfig config;

38
	const std::string path_utf8;
39

40
	TimerEvent timer_event;
41

42 43 44 45 46 47
	Partition &partition;

	/**
	 * These version numbers determine whether we need to save the state
	 * file.  If nothing has changed, we won't let the hard drive spin up.
	 */
48 49
	unsigned prev_volume_version = 0, prev_output_version = 0,
		prev_playlist_version = 0;
50

51 52 53 54
#ifdef ENABLE_DATABASE
	unsigned prev_storage_version = 0;
#endif

55
public:
56
	StateFile(StateFileConfig &&_config,
57
		  Partition &partition, EventLoop &loop);
58

59 60
	void Read();
	void Write();
61

62 63 64 65 66
	/**
	 * Schedules a write if MPD's state was modified.
	 */
	void CheckModified();

67
private:
68
	void Write(OutputStream &os);
69 70
	void Write(BufferedOutputStream &os);

71 72 73
	/**
	 * Save the current state versions for use with IsModified().
	 */
74
	void RememberVersions() noexcept;
75 76 77 78 79 80

	/**
	 * Check if MPD's state was modified since the last
	 * RememberVersions() call.
	 */
	gcc_pure
81
	bool IsModified() const noexcept;
82

83 84
	/* callback for #timer_event */
	void OnTimeout();
85
};
86 87

#endif /* STATE_FILE_H */