/* */ #include "SessionSerializer.h" #include #include #include #include #include "RequestGroupMan.h" #include "a2functional.h" #include "File.h" #include "A2STR.h" #include "download_helper.h" #include "Option.h" #include "DownloadResult.h" #include "FileEntry.h" #include "prefs.h" #include "util.h" #include "array_fun.h" #include "BufferedFile.h" #include "OptionParser.h" #include "OptionHandler.h" #include "SHA1IOFile.h" #if HAVE_ZLIB # include "GZipFile.h" #endif namespace aria2 { SessionSerializer::SessionSerializer(RequestGroupMan* requestGroupMan) : rgman_{requestGroupMan}, saveError_{true}, saveInProgress_{true}, saveWaiting_{true} { } bool SessionSerializer::save(const std::string& filename) const { std::string tempFilename = filename; tempFilename += "__temp"; { std::unique_ptr fp; #if HAVE_ZLIB if (util::endsWith(filename, ".gz")) { fp = make_unique(tempFilename.c_str(), IOFile::WRITE); } else #endif { fp = make_unique(tempFilename.c_str(), IOFile::WRITE); } if (!*fp) { return false; } if (!save(*fp) || fp->close() == EOF) { return false; } } return File(tempFilename).renameTo(filename); } namespace { // Write 1 line of option name/value pair. This function returns true // if it succeeds, or false. bool writeOptionLine(IOFile& fp, PrefPtr pref, const std::string& val) { size_t prefLen = strlen(pref->k); return fp.write(" ", 1) == 1 && fp.write(pref->k, prefLen) == prefLen && fp.write("=", 1) == 1 && fp.write(val.c_str(), val.size()) == val.size() && fp.write("\n", 1) == 1; } } // namespace namespace { bool writeOption(IOFile& fp, const std::shared_ptr