*Save, *State: use the OutputStream API instead of FILE*
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
#include "db/DatabaseError.hxx"
|
||||
#include "Directory.hxx"
|
||||
#include "DirectorySave.hxx"
|
||||
#include "fs/output/BufferedOutputStream.hxx"
|
||||
#include "fs/TextFile.hxx"
|
||||
#include "tag/Tag.hxx"
|
||||
#include "tag/TagSettings.h"
|
||||
@@ -49,20 +50,20 @@ static constexpr unsigned DB_FORMAT = 2;
|
||||
static constexpr unsigned OLDEST_DB_FORMAT = 1;
|
||||
|
||||
void
|
||||
db_save_internal(FILE *fp, const Directory &music_root)
|
||||
db_save_internal(BufferedOutputStream &os, const Directory &music_root)
|
||||
{
|
||||
fprintf(fp, "%s\n", DIRECTORY_INFO_BEGIN);
|
||||
fprintf(fp, DB_FORMAT_PREFIX "%u\n", DB_FORMAT);
|
||||
fprintf(fp, "%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
|
||||
fprintf(fp, "%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset());
|
||||
os.Format("%s\n", DIRECTORY_INFO_BEGIN);
|
||||
os.Format(DB_FORMAT_PREFIX "%u\n", DB_FORMAT);
|
||||
os.Format("%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
|
||||
os.Format("%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset());
|
||||
|
||||
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
|
||||
if (!ignore_tag_items[i])
|
||||
fprintf(fp, DB_TAG_PREFIX "%s\n", tag_item_names[i]);
|
||||
os.Format(DB_TAG_PREFIX "%s\n", tag_item_names[i]);
|
||||
|
||||
fprintf(fp, "%s\n", DIRECTORY_INFO_END);
|
||||
os.Format("%s\n", DIRECTORY_INFO_END);
|
||||
|
||||
directory_save(fp, music_root);
|
||||
directory_save(os, music_root);
|
||||
}
|
||||
|
||||
bool
|
||||
|
@@ -20,14 +20,13 @@
|
||||
#ifndef MPD_DATABASE_SAVE_HXX
|
||||
#define MPD_DATABASE_SAVE_HXX
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct Directory;
|
||||
class BufferedOutputStream;
|
||||
class TextFile;
|
||||
class Error;
|
||||
|
||||
void
|
||||
db_save_internal(FILE *file, const Directory &root);
|
||||
db_save_internal(BufferedOutputStream &os, const Directory &root);
|
||||
|
||||
bool
|
||||
db_load_internal(TextFile &file, Directory &root, Error &error);
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include "DetachedSong.hxx"
|
||||
#include "PlaylistDatabase.hxx"
|
||||
#include "fs/TextFile.hxx"
|
||||
#include "fs/output/BufferedOutputStream.hxx"
|
||||
#include "util/StringUtil.hxx"
|
||||
#include "util/NumberParser.hxx"
|
||||
#include "util/Error.hxx"
|
||||
@@ -70,37 +71,37 @@ ParseTypeString(const char *type)
|
||||
}
|
||||
|
||||
void
|
||||
directory_save(FILE *fp, const Directory &directory)
|
||||
directory_save(BufferedOutputStream &os, const Directory &directory)
|
||||
{
|
||||
if (!directory.IsRoot()) {
|
||||
const char *type = DeviceToTypeString(directory.device);
|
||||
if (type != nullptr)
|
||||
fprintf(fp, DIRECTORY_TYPE "%s\n", type);
|
||||
os.Format(DIRECTORY_TYPE "%s\n", type);
|
||||
|
||||
if (directory.mtime != 0)
|
||||
fprintf(fp, DIRECTORY_MTIME "%lu\n",
|
||||
(unsigned long)directory.mtime);
|
||||
os.Format(DIRECTORY_MTIME "%lu\n",
|
||||
(unsigned long)directory.mtime);
|
||||
|
||||
fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
|
||||
os.Format("%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
|
||||
}
|
||||
|
||||
for (const auto &child : directory.children) {
|
||||
fprintf(fp, DIRECTORY_DIR "%s\n", child.GetName());
|
||||
os.Format(DIRECTORY_DIR "%s\n", child.GetName());
|
||||
|
||||
if (!child.IsMount())
|
||||
directory_save(fp, child);
|
||||
directory_save(os, child);
|
||||
|
||||
if (ferror(fp))
|
||||
if (!os.Check())
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto &song : directory.songs)
|
||||
song_save(fp, song);
|
||||
song_save(os, song);
|
||||
|
||||
playlist_vector_save(fp, directory.playlists);
|
||||
playlist_vector_save(os, directory.playlists);
|
||||
|
||||
if (!directory.IsRoot())
|
||||
fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath());
|
||||
os.Format(DIRECTORY_END "%s\n", directory.GetPath());
|
||||
}
|
||||
|
||||
static bool
|
||||
|
@@ -20,14 +20,13 @@
|
||||
#ifndef MPD_DIRECTORY_SAVE_HXX
|
||||
#define MPD_DIRECTORY_SAVE_HXX
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct Directory;
|
||||
class TextFile;
|
||||
class BufferedOutputStream;
|
||||
class Error;
|
||||
|
||||
void
|
||||
directory_save(FILE *fp, const Directory &directory);
|
||||
directory_save(BufferedOutputStream &os, const Directory &directory);
|
||||
|
||||
bool
|
||||
directory_load(TextFile &file, Directory &directory, Error &error);
|
||||
|
@@ -32,6 +32,8 @@
|
||||
#include "db/DatabaseLock.hxx"
|
||||
#include "db/DatabaseError.hxx"
|
||||
#include "fs/TextFile.hxx"
|
||||
#include "fs/output/BufferedOutputStream.hxx"
|
||||
#include "fs/output/FileOutputStream.hxx"
|
||||
#include "config/ConfigData.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
#include "util/CharUtil.hxx"
|
||||
@@ -366,22 +368,16 @@ SimpleDatabase::Save(Error &error)
|
||||
|
||||
LogDebug(simple_db_domain, "writing DB");
|
||||
|
||||
FILE *fp = FOpen(path, FOpenMode::WriteText);
|
||||
if (!fp) {
|
||||
error.FormatErrno("unable to write to db file \"%s\"",
|
||||
path_utf8.c_str());
|
||||
FileOutputStream fos(path, error);
|
||||
if (!fos.IsDefined())
|
||||
return false;
|
||||
}
|
||||
|
||||
db_save_internal(fp, *root);
|
||||
BufferedOutputStream bos(fos);
|
||||
|
||||
if (ferror(fp)) {
|
||||
error.SetErrno("Failed to write to database file");
|
||||
fclose(fp);
|
||||
db_save_internal(bos, *root);
|
||||
|
||||
if (!bos.Flush(error) || !fos.Commit(error))
|
||||
return false;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
struct stat st;
|
||||
if (StatFile(path, st))
|
||||
|
Reference in New Issue
Block a user