diff options
author | Max Kellermann <max@duempel.org> | 2014-07-30 20:58:14 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-07-30 22:17:01 +0200 |
commit | 9fb351a139a56fc7b1ece549894f8fc31fa887cd (patch) | |
tree | 7c9ce42a1262af587f1b9fede2b24ff39cef93fc /src/db/plugins | |
parent | 0d0ccacdf3e154b1cc27a0840fce80ac13f9641c (diff) | |
download | mpd-9fb351a139a56fc7b1ece549894f8fc31fa887cd.tar.gz mpd-9fb351a139a56fc7b1ece549894f8fc31fa887cd.tar.xz mpd-9fb351a139a56fc7b1ece549894f8fc31fa887cd.zip |
*Save, *State: use the OutputStream API instead of FILE*
Diffstat (limited to '')
-rw-r--r-- | src/db/plugins/simple/DatabaseSave.cxx | 17 | ||||
-rw-r--r-- | src/db/plugins/simple/DatabaseSave.hxx | 5 | ||||
-rw-r--r-- | src/db/plugins/simple/DirectorySave.cxx | 23 | ||||
-rw-r--r-- | src/db/plugins/simple/DirectorySave.hxx | 5 | ||||
-rw-r--r-- | src/db/plugins/simple/SimpleDatabasePlugin.cxx | 20 |
5 files changed, 33 insertions, 37 deletions
diff --git a/src/db/plugins/simple/DatabaseSave.cxx b/src/db/plugins/simple/DatabaseSave.cxx index 62034a0b8..e10d79d60 100644 --- a/src/db/plugins/simple/DatabaseSave.cxx +++ b/src/db/plugins/simple/DatabaseSave.cxx @@ -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 diff --git a/src/db/plugins/simple/DatabaseSave.hxx b/src/db/plugins/simple/DatabaseSave.hxx index 3bd3377ae..bb7f57115 100644 --- a/src/db/plugins/simple/DatabaseSave.hxx +++ b/src/db/plugins/simple/DatabaseSave.hxx @@ -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); diff --git a/src/db/plugins/simple/DirectorySave.cxx b/src/db/plugins/simple/DirectorySave.cxx index 9d3ebbac2..7e7cbc3ba 100644 --- a/src/db/plugins/simple/DirectorySave.cxx +++ b/src/db/plugins/simple/DirectorySave.cxx @@ -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 diff --git a/src/db/plugins/simple/DirectorySave.hxx b/src/db/plugins/simple/DirectorySave.hxx index 07e9e158b..f464f9946 100644 --- a/src/db/plugins/simple/DirectorySave.hxx +++ b/src/db/plugins/simple/DirectorySave.hxx @@ -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); diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.cxx b/src/db/plugins/simple/SimpleDatabasePlugin.cxx index 97d3e52df..fe712b90b 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.cxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.cxx @@ -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); - return false; - } + db_save_internal(bos, *root); - fclose(fp); + if (!bos.Flush(error) || !fos.Commit(error)) + return false; struct stat st; if (StatFile(path, st)) |