]> git.cameronkatri.com Git - ldid.git/commitdiff
Don't leave dangling temporary files
authorCameron Katri <me@cameronkatri.com>
Thu, 7 Jul 2022 01:43:17 +0000 (21:43 -0400)
committerCameron Katri <me@cameronkatri.com>
Thu, 7 Jul 2022 01:43:17 +0000 (21:43 -0400)
ldid.cpp

index c2e89cc5d29419b4bc949525cf7168efd60a080b..7b4c9db8c41f8ef960653cf76451a9f29cb1221d 100644 (file)
--- a/ldid.cpp
+++ b/ldid.cpp
     __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");