2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-02 19:52:57 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-09 15:23:37 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-09 15:23:37 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "DirectorySave.hxx"
|
2013-01-02 22:52:08 +01:00
|
|
|
#include "Directory.hxx"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "SongSave.hxx"
|
|
|
|
#include "PlaylistDatabase.hxx"
|
2013-01-03 10:12:41 +01:00
|
|
|
#include "TextFile.hxx"
|
2013-11-28 18:48:35 +01:00
|
|
|
#include "util/StringUtil.hxx"
|
2013-10-21 09:48:31 +02:00
|
|
|
#include "util/NumberParser.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2008-10-09 15:23:37 +02:00
|
|
|
|
2013-10-02 08:11:58 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2012-02-13 19:24:17 +01:00
|
|
|
#define DIRECTORY_DIR "directory: "
|
2009-02-28 14:02:00 +01:00
|
|
|
#define DIRECTORY_MTIME "mtime: "
|
2009-02-27 19:30:16 +01:00
|
|
|
#define DIRECTORY_BEGIN "begin: "
|
|
|
|
#define DIRECTORY_END "end: "
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain directory_domain("directory");
|
2009-03-02 15:42:42 +01:00
|
|
|
|
2009-11-07 16:20:07 +01:00
|
|
|
void
|
2013-10-19 18:48:38 +02:00
|
|
|
directory_save(FILE *fp, const Directory &directory)
|
2008-10-09 15:23:37 +02:00
|
|
|
{
|
2013-10-19 18:48:38 +02:00
|
|
|
if (!directory.IsRoot()) {
|
2009-02-28 14:02:00 +01:00
|
|
|
fprintf(fp, DIRECTORY_MTIME "%lu\n",
|
2013-10-19 18:48:38 +02:00
|
|
|
(unsigned long)directory.mtime);
|
2009-02-28 14:02:00 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
|
2008-10-09 15:23:37 +02:00
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *cur;
|
2012-01-24 18:20:58 +01:00
|
|
|
directory_for_each_child(cur, directory) {
|
2013-10-21 10:04:59 +02:00
|
|
|
fprintf(fp, DIRECTORY_DIR "%s\n", cur->GetName());
|
2009-11-07 16:20:07 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
directory_save(fp, *cur);
|
2009-11-07 16:20:07 +01:00
|
|
|
|
|
|
|
if (ferror(fp))
|
|
|
|
return;
|
2008-10-09 15:23:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song;
|
2012-01-24 21:33:09 +01:00
|
|
|
directory_for_each_song(song, directory)
|
2013-10-19 18:48:38 +02:00
|
|
|
song_save(fp, *song);
|
2008-10-09 15:23:37 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
playlist_vector_save(fp, directory.playlists);
|
2010-07-21 08:58:15 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
if (!directory.IsRoot())
|
|
|
|
fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath());
|
2008-10-09 15:23:37 +02:00
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
static Directory *
|
2013-10-19 18:48:38 +02:00
|
|
|
directory_load_subdir(TextFile &file, Directory &parent, const char *name,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2009-11-01 15:34:12 +01:00
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
if (parent.FindChild(name) != nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(directory_domain,
|
|
|
|
"Duplicate subdirectory '%s'", name);
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-11-01 15:34:13 +01:00
|
|
|
}
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
Directory *directory = parent.CreateChild(name);
|
2009-11-01 15:34:14 +01:00
|
|
|
|
2013-01-03 10:16:05 +01:00
|
|
|
const char *line = file.ReadLine();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (line == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(directory_domain, "Unexpected end of file");
|
2013-01-02 23:06:10 +01:00
|
|
|
directory->Delete();
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-11-01 15:34:12 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 18:48:35 +01:00
|
|
|
if (StringStartsWith(line, DIRECTORY_MTIME)) {
|
2009-11-01 15:34:14 +01:00
|
|
|
directory->mtime =
|
2013-10-21 09:48:31 +02:00
|
|
|
ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
|
2009-11-01 15:34:12 +01:00
|
|
|
|
2013-01-03 10:16:05 +01:00
|
|
|
line = file.ReadLine();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (line == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(directory_domain, "Unexpected end of file");
|
2013-01-02 23:06:10 +01:00
|
|
|
directory->Delete();
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-11-01 15:34:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-28 18:48:35 +01:00
|
|
|
if (!StringStartsWith(line, DIRECTORY_BEGIN)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(directory_domain, "Malformed line: %s", line);
|
2013-01-02 23:06:10 +01:00
|
|
|
directory->Delete();
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-11-01 15:34:12 +01:00
|
|
|
}
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
success = directory_load(file, *directory, error);
|
2009-11-01 15:34:13 +01:00
|
|
|
if (!success) {
|
2013-01-02 23:06:10 +01:00
|
|
|
directory->Delete();
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-11-01 15:34:13 +01:00
|
|
|
}
|
2009-11-01 15:34:12 +01:00
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2009-03-02 15:42:42 +01:00
|
|
|
bool
|
2013-10-19 18:48:38 +02:00
|
|
|
directory_load(TextFile &file, Directory &directory, Error &error)
|
2008-10-09 15:23:37 +02:00
|
|
|
{
|
2009-11-01 15:37:16 +01:00
|
|
|
const char *line;
|
2008-10-09 15:23:37 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
while ((line = file.ReadLine()) != nullptr &&
|
2013-11-28 18:48:35 +01:00
|
|
|
!StringStartsWith(line, DIRECTORY_END)) {
|
|
|
|
if (StringStartsWith(line, DIRECTORY_DIR)) {
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *subdir =
|
2013-01-03 10:16:05 +01:00
|
|
|
directory_load_subdir(file, directory,
|
2009-11-01 15:37:16 +01:00
|
|
|
line + sizeof(DIRECTORY_DIR) - 1,
|
2013-01-03 10:16:05 +01:00
|
|
|
error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (subdir == nullptr)
|
2009-03-02 15:42:42 +01:00
|
|
|
return false;
|
2013-11-28 18:48:35 +01:00
|
|
|
} else if (StringStartsWith(line, SONG_BEGIN)) {
|
2009-11-01 17:51:29 +01:00
|
|
|
const char *name = line + sizeof(SONG_BEGIN) - 1;
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song;
|
2009-11-01 17:51:29 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
if (directory.FindSong(name) != nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(directory_domain,
|
|
|
|
"Duplicate song '%s'", name);
|
2013-01-02 19:52:57 +01:00
|
|
|
return false;
|
2009-11-01 17:51:29 +01:00
|
|
|
}
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
song = song_load(file, &directory, name, error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (song == nullptr)
|
2009-07-05 08:29:52 +02:00
|
|
|
return false;
|
2009-11-01 17:51:29 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
directory.AddSong(song);
|
2013-11-28 18:48:35 +01:00
|
|
|
} else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) {
|
2010-07-25 12:01:29 +02:00
|
|
|
/* duplicate the name, because
|
|
|
|
playlist_metadata_load() will overwrite the
|
|
|
|
buffer */
|
|
|
|
char *name = g_strdup(line + sizeof(PLAYLIST_META_BEGIN) - 1);
|
2010-07-21 08:58:15 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
if (!playlist_metadata_load(file, directory.playlists,
|
2013-01-03 10:16:05 +01:00
|
|
|
name, error)) {
|
2010-07-25 12:01:29 +02:00
|
|
|
g_free(name);
|
2010-07-21 08:58:15 +02:00
|
|
|
return false;
|
2010-07-25 12:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free(name);
|
2008-10-09 15:23:37 +02:00
|
|
|
} else {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(directory_domain,
|
|
|
|
"Malformed line: %s", line);
|
2009-03-02 15:42:42 +01:00
|
|
|
return false;
|
2008-10-09 15:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-02 15:42:42 +01:00
|
|
|
|
|
|
|
return true;
|
2008-10-09 15:23:37 +02:00
|
|
|
}
|