From e956c1d890c664ccd80293d07a6e8f52a5949c2f Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Wed, 6 Jul 2022 21:43:17 -0400 Subject: Don't leave dangling temporary files --- ldid.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ldid.cpp b/ldid.cpp index c2e89cc..7b4c9db 100644 --- a/ldid.cpp +++ b/ldid.cpp @@ -128,6 +128,7 @@ __attribute__((packed)) std::string password; +std::vector cleanup; template 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"); -- cgit v1.2.3-56-ge451