From 203067a2742ad2cbb986369b216b1f1ecfb96458 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 29 Sep 2015 13:06:08 +0200 Subject: Automatically cleanup package archive, directory on failure --- bpkg/fetch.cxx | 7 +++++++ bpkg/pkg-fetch.cxx | 4 ++++ bpkg/pkg-unpack.cxx | 18 +++++++----------- bpkg/utility | 5 +++++ bpkg/utility.cxx | 1 - 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/bpkg/fetch.cxx b/bpkg/fetch.cxx index 3ddd2e9..ebff730 100644 --- a/bpkg/fetch.cxx +++ b/bpkg/fetch.cxx @@ -495,6 +495,8 @@ namespace bpkg fail << "file " << r << " already exists"; string url (to_url (host, port, f)); + + auto_rm arm (r); process pr (start (o, url, r)); if (!pr.wait ()) @@ -506,6 +508,7 @@ namespace bpkg info << "re-run with -v for more information"; } + arm.cancel (); return r; } @@ -576,6 +579,8 @@ namespace bpkg if (!ifs.is_open ()) fail << "unable to open " << f << " in read mode"; + auto_rm arm (r); + ofstream ofs (r.string (), ios::binary); if (!ofs.is_open ()) fail << "unable to open " << r << " in write mode"; @@ -589,6 +594,8 @@ namespace bpkg // ifs.close (); ofs.close (); + + arm.cancel (); } catch (const iostream::failure&) { diff --git a/bpkg/pkg-fetch.cxx b/bpkg/pkg-fetch.cxx index 36b80a7..9da632a 100644 --- a/bpkg/pkg-fetch.cxx +++ b/bpkg/pkg-fetch.cxx @@ -35,6 +35,7 @@ namespace bpkg session s; path a; + auto_rm arm; bool purge; if (o.existing ()) @@ -96,6 +97,7 @@ namespace bpkg << "from " << pl->repository->name; a = fetch_archive (o, pl->repository->location, pl->location, c); + arm = auto_rm (a); purge = true; } @@ -138,7 +140,9 @@ namespace bpkg }); db.persist (p); + t.commit (); + arm.cancel (); if (verb) text << "fetched " << p->name << " " << p->version; diff --git a/bpkg/pkg-unpack.cxx b/bpkg/pkg-unpack.cxx index ba98b0b..84dd5ae 100644 --- a/bpkg/pkg-unpack.cxx +++ b/bpkg/pkg-unpack.cxx @@ -115,6 +115,11 @@ namespace bpkg if (exists (d)) fail << "package directory " << d << " already exists"; + // What should we do if tar or something after it fails? Cleaning + // up the package directory sounds like the right thing to do. + // + auto_rm_r arm (d); + const char* args[] { "tar", "-C", c.string ().c_str (), // -C/--directory -- change to directory. @@ -125,17 +130,6 @@ namespace bpkg if (verb >= 2) print_process (args); - // What should we do if tar or something after it fails? Cleaning - // up the package directory sounds like the right thing to do. - // - auto dg ( - make_exception_guard ( - [&d]() - { - if (exists (d)) - rm_r (d); - })); - try { process pr (args); @@ -165,6 +159,8 @@ namespace bpkg db.update (p); t.commit (); + arm.cancel (); + return p; } diff --git a/bpkg/utility b/bpkg/utility index 72e02ba..02d447c 100644 --- a/bpkg/utility +++ b/bpkg/utility @@ -9,6 +9,8 @@ #include // move() #include // uncaught_exception () +#include + #include namespace bpkg @@ -38,6 +40,9 @@ namespace bpkg void rm_r (const dir_path&, bool dir = true); + using auto_rm = butl::auto_rmfile; + using auto_rm_r = butl::auto_rmdir; + // Process. // // The process command line is printed for verbosity >= 2 (essential diff --git a/bpkg/utility.cxx b/bpkg/utility.cxx index db47476..9b194df 100644 --- a/bpkg/utility.cxx +++ b/bpkg/utility.cxx @@ -7,7 +7,6 @@ #include #include -#include #include #include -- cgit v1.1