diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2015-09-29 13:06:08 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2015-09-29 13:06:08 +0200 |
commit | 203067a2742ad2cbb986369b216b1f1ecfb96458 (patch) | |
tree | b3b755ca08aba82a8b780b462dce877afe9d0bea | |
parent | f5adc6c0ee7965abcad4cc73d0f36d1ed3cba3cc (diff) |
Automatically cleanup package archive, directory on failure
-rw-r--r-- | bpkg/fetch.cxx | 7 | ||||
-rw-r--r-- | bpkg/pkg-fetch.cxx | 4 | ||||
-rw-r--r-- | bpkg/pkg-unpack.cxx | 18 | ||||
-rw-r--r-- | bpkg/utility | 5 | ||||
-rw-r--r-- | 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 <utility> // move() #include <exception> // uncaught_exception () +#include <butl/filesystem> + #include <bpkg/types> 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 <system_error> #include <butl/process> -#include <butl/filesystem> #include <bpkg/types> #include <bpkg/diagnostics> |