[rtems-tools commit] rtemstoolkit/rtl-file: Remove a file on close if requested

Chris Johns chrisj at rtems.org
Sat Apr 14 04:17:53 UTC 2018


Module:    rtems-tools
Branch:    master
Commit:    82c8788b111a4392a56414f8f421ba26ded5d0fd
Changeset: http://git.rtems.org/rtems-tools/commit/?id=82c8788b111a4392a56414f8f421ba26ded5d0fd

Author:    Chris Johns <chrisj at rtems.org>
Date:      Sat Apr 14 14:15:07 2018 +1000

rtemstoolkit/rtl-file: Remove a file on close if requested

Close #3395

---

 linkers/rtems-ld.cpp           |  6 +++---
 linkers/rtems-ra.cpp           |  6 +++---
 rtemstoolkit/rld-files.cpp     | 26 +++++++++++++++++++++++---
 rtemstoolkit/rld-files.h       |  9 +++++++++
 rtemstoolkit/rld-outputter.cpp | 17 ++++++++++-------
 rtemstoolkit/rld-outputter.h   | 14 +++++++-------
 6 files changed, 55 insertions(+), 23 deletions(-)

diff --git a/linkers/rtems-ld.cpp b/linkers/rtems-ld.cpp
index 92258df..eafbbbf 100644
--- a/linkers/rtems-ld.cpp
+++ b/linkers/rtems-ld.cpp
@@ -504,9 +504,9 @@ main (int argc, char* argv[])
                                            dependents, cache);
         else if (output_type == "rap")
         {
-          rld::outputter::application (output, entry, exit,
-                                       dependents, cache, symbols,
-                                       one_file);
+          rld::outputter::rap_application (output, entry, exit,
+                                           dependents, cache, symbols,
+                                           one_file);
           if (!outra.empty ())
           {
             rld::path::paths ra_libs;
diff --git a/linkers/rtems-ra.cpp b/linkers/rtems-ra.cpp
index efd4cf9..04c1868 100644
--- a/linkers/rtems-ra.cpp
+++ b/linkers/rtems-ra.cpp
@@ -375,9 +375,9 @@ main (int argc, char* argv[])
 
             /* Todo: include absolute name for rap_name */
 
-            rld::outputter::application (rap_name, entry, exit,
-                                         dependents, *cache, symbols,
-                                         true);
+            rld::outputter::rap_application (rap_name, entry, exit,
+                                             dependents, *cache, symbols,
+                                             true);
           }
 
           dependents.clear ();
diff --git a/rtemstoolkit/rld-files.cpp b/rtemstoolkit/rld-files.cpp
index c783f54..fdf12a6 100644
--- a/rtemstoolkit/rld-files.cpp
+++ b/rtemstoolkit/rld-files.cpp
@@ -229,7 +229,8 @@ namespace rld
         references_ (0),
         fd_ (-1),
         symbol_refs (0),
-        writable (false)
+        writable (false),
+        remove (false)
     {
     }
 
@@ -238,7 +239,8 @@ namespace rld
         references_ (0),
         fd_ (-1),
         symbol_refs (0),
-        writable (false)
+        writable (false),
+        remove (false)
     {
     }
 
@@ -246,7 +248,8 @@ namespace rld
       : references_ (0),
         fd_ (-1),
         symbol_refs (0),
-        writable (false)
+        writable (false),
+        remove (false)
     {
     }
 
@@ -255,7 +258,16 @@ namespace rld
       if (references_)
 	std::cerr << "rtl:file:image: references when destructing";
       if (fd_ >= 0)
+      {
         ::close (fd_);
+        if (writable && remove)
+        {
+          if (rld::verbose () >= RLD_VERBOSE_INFO)
+            std::cout << "image::close: removing " << name ().full ()
+                        << std::endl;
+          ::unlink (name_.path ().c_str ());
+        }
+      }
     }
 
     void
@@ -313,6 +325,14 @@ namespace rld
         {
           ::close (fd_);
           fd_ = -1;
+          if (writable && remove)
+          {
+            if (rld::verbose () >= RLD_VERBOSE_INFO)
+              std::cout << "image::close: removing " << name ().full ()
+                        << std::endl;
+            ::unlink (name_.path ().c_str ());
+            remove = false;
+          }
         }
       }
     }
diff --git a/rtemstoolkit/rld-files.h b/rtemstoolkit/rld-files.h
index 09e9aaf..f19b732 100644
--- a/rtemstoolkit/rld-files.h
+++ b/rtemstoolkit/rld-files.h
@@ -388,6 +388,14 @@ namespace rld
         return writable;
       }
 
+      /**
+       * Remove a writable file on close. This flag can be set when an error
+       * happens while writing a file.
+       */
+      void remove_on_close () {
+        remove = true;
+      }
+
     private:
 
       file      name_;       //< The name of the file.
@@ -396,6 +404,7 @@ namespace rld
       elf::file elf_;        //< The libelf reference.
       int       symbol_refs; //< The number of symbols references made.
       bool      writable;    //< The image is writable.
+      bool      remove;      //< Remove the image on close if writable.
     };
 
     /**
diff --git a/rtemstoolkit/rld-outputter.cpp b/rtemstoolkit/rld-outputter.cpp
index 600aedc..9343180 100644
--- a/rtemstoolkit/rld-outputter.cpp
+++ b/rtemstoolkit/rld-outputter.cpp
@@ -332,6 +332,7 @@ namespace rld
       catch (...)
       {
         out.close ();
+        ::unlink (name.c_str ());
         throw;
       }
 
@@ -410,6 +411,7 @@ namespace rld
       catch (...)
       {
         delete [] buffer;
+        app.remove_on_close ();
         app.close ();
         throw;
       }
@@ -427,13 +429,13 @@ namespace rld
     }
 
     void
-    application (const std::string&        name,
-                 const std::string&        entry,
-                 const std::string&        exit,
-                 const files::object_list& dependents,
-                 const files::cache&       cache,
-                 const symbols::table&     symbols,
-                 bool                      one_file)
+    rap_application (const std::string&        name,
+                     const std::string&        entry,
+                     const std::string&        exit,
+                     const files::object_list& dependents,
+                     const files::cache&       cache,
+                     const symbols::table&     symbols,
+                     bool                      one_file)
     {
       if (rld::verbose () >= RLD_VERBOSE_INFO)
         std::cout << "outputter:application: " << name << std::endl;
@@ -458,6 +460,7 @@ namespace rld
       }
       catch (...)
       {
+        app.remove_on_close ();
         app.close ();
         throw;
       }
diff --git a/rtemstoolkit/rld-outputter.h b/rtemstoolkit/rld-outputter.h
index 7fe52b2..985ae55 100644
--- a/rtemstoolkit/rld-outputter.h
+++ b/rtemstoolkit/rld-outputter.h
@@ -111,13 +111,13 @@ namespace rld
      *              the user requested.
      * @param symbols The symbol table used to resolve the application.
      */
-    void application (const std::string&        name,
-                      const std::string&        entry,
-                      const std::string&        exit,
-                      const files::object_list& dependents,
-                      const files::cache&       cache,
-                      const symbols::table&     symbols,
-                      bool                      one_file);
+    void rap_application (const std::string&        name,
+                          const std::string&        entry,
+                          const std::string&        exit,
+                          const files::object_list& dependents,
+                          const files::cache&       cache,
+                          const symbols::table&     symbols,
+                          bool                      one_file);
 
   }
 }



More information about the vc mailing list