diff options
author | Cameron Katri <me@cameronkatri.com> | 2022-07-06 21:43:17 -0400 |
---|---|---|
committer | Cameron Katri <me@cameronkatri.com> | 2022-07-06 21:43:17 -0400 |
commit | e956c1d890c664ccd80293d07a6e8f52a5949c2f (patch) | |
tree | e97684995ac86ef1bce97622c6e04611dacccf2d | |
parent | 672c7c058ee732471bca000744877e4f8db37c1c (diff) | |
download | ldid-e956c1d890c664ccd80293d07a6e8f52a5949c2f.tar.gz ldid-e956c1d890c664ccd80293d07a6e8f52a5949c2f.tar.zst ldid-e956c1d890c664ccd80293d07a6e8f52a5949c2f.zip |
Don't leave dangling temporary files
-rw-r--r-- | ldid.cpp | 9 |
1 files changed, 9 insertions, 0 deletions
@@ -128,6 +128,7 @@ __attribute__((packed)) std::string password; +std::vector<std::string> cleanup; template <typename Type_> struct Iterator_ { @@ -2036,6 +2037,7 @@ static std::string Temporary(std::filebuf &file, const Split &split) { std::string temp(split.dir + ".ldid." + split.base); mkdir_p(split.dir); _assert_(file.open(temp.c_str(), std::ios::out | std::ios::trunc | std::ios::binary) == &file, "open(): %s", temp.c_str()); + cleanup.push_back(temp); return temp; } @@ -2049,6 +2051,7 @@ static void Commit(const std::string &path, const std::string &temp) { } _syscall(rename(temp.c_str(), path.c_str())); + cleanup.erase(std::remove(cleanup.begin(), cleanup.end(), temp), cleanup.end()); } #endif // LDID_NOTOOLS @@ -3124,8 +3127,14 @@ static void usage(const char *argv0) { fprintf(stderr, "More information: 'man ldid'\n"); } +void cleanupfunc(void) { + for (const auto &temp : cleanup) + remove(temp.c_str()); +} + #ifndef LDID_NOTOOLS int main(int argc, char *argv[]) { + std::atexit(cleanupfunc); OpenSSL_add_all_algorithms(); # if OPENSSL_VERSION_MAJOR >= 3 OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy"); |