]> git.tdb.fi Git - builder.git/blobdiff - source/copy.cpp
Big rewrite for a more tool-centric approach
[builder.git] / source / copy.cpp
index 8ea7f3537ab32d8e84aa2a3dd2731cf32ed1df1f..b5ac403c4d4eb72e42567122bb656d576287bb5a 100644 (file)
@@ -7,55 +7,64 @@
 #include <msp/io/print.h>
 #include "builder.h"
 #include "copy.h"
-#include "package.h"
+#include "install.h"
 
 using namespace std;
 using namespace Msp;
 
-Copy::Copy(Builder &b, const Package &pkg, const FS::Path &s, const FS::Path &d, const FS::Path &l):
-       InternalAction(b),
-       src(s),
-       dest(d),
-       link(l)
+Copy::Copy(Builder &b):
+       Tool(b, "CP")
+{ }
+
+Target *Copy::create_target(const list<Target *> &sources, const string &arg) const
 {
-       announce(pkg.get_name(), "COPY", dest.str());
-       if(builder.get_verbose()>=2)
-               IO::print("%s -> %s\n", s, d);
+       FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
+       const SourcePackage &pkg = dynamic_cast<const SourcePackage &>(*file_tgt.get_package());
+       Install *inst = new Install(builder, pkg, file_tgt, arg);
+       inst->set_tool(*this);
+       return inst;
+}
 
-       if(!builder.get_dry_run())
-               worker = new Worker(*this);
+Task *Copy::run(const Target &target) const
+{
+       const Install &install = dynamic_cast<const Install &>(target);
+       Worker *worker = new Worker(install);
+       return new InternalTask(worker);
 }
 
 
-Copy::Worker::Worker(Copy &c):
-       copy(c)
+Copy::Worker::Worker(const Install &t):
+       target(t)
 {
        launch();
 }
 
 void Copy::Worker::main()
 {
-       FS::mkpath(FS::dirname(copy.dest), 0755);
+       const FileTarget &source = target.get_source();
+       const FS::Path &src_path = source.get_path();
+       const FS::Path &dst_path = target.get_path();
+       FS::mkpath(FS::dirname(dst_path), 0755);
 
        // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
-       if(FS::exists(copy.dest))
+       if(FS::exists(dst_path))
        {
                try
                {
-                       unlink(copy.dest);
+                       unlink(dst_path);
                }
                catch(const exception &e)
                {
                        IO::print(IO::cerr, "%s\n", e.what());
-                       done = error = true;
+                       status = Task::ERROR;
                        return;
                }
        }
 
        try
        {
-               IO::File in(copy.src.str());
-               IO::File out(copy.dest.str(), IO::M_WRITE);
+               IO::File in(src_path.str());
+               IO::File out(dst_path.str(), IO::M_WRITE);
 
                // Actual transfer loop
                char buf[16384];
@@ -68,20 +77,21 @@ void Copy::Worker::main()
        catch(const exception &e)
        {
                IO::print(IO::cerr, "%s\n", e.what());
-               done = error = true;
+               status = Task::ERROR;
                return;
        }
 
        // Preserve file permissions
        struct stat st;
-       if(stat(copy.src.str().c_str(), &st)==0)
-               chmod(copy.dest.str().c_str(), st.st_mode&0777);
+       if(stat(src_path.str().c_str(), &st)==0)
+               chmod(dst_path.str().c_str(), st.st_mode&0777);
 
-       if(!copy.link.empty())
+       const FS::Path &link = target.get_symlink();
+       if(!link.empty())
        {
-               FS::Path relpath = FS::relative(copy.dest, FS::dirname(copy.link));
-               symlink(relpath.str().c_str(), copy.link.str().c_str());
+               FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
+               symlink(relpath.str().c_str(), link.str().c_str());
        }
 
-       done = true;
+       status = Task::SUCCESS;
 }