]> git.tdb.fi Git - builder.git/blobdiff - source/copy.cpp
Redesign how tools are run
[builder.git] / source / copy.cpp
index 23cd6f1af358cebb11e9e526a7ede7ef1af4ef0b..8f6b81d46b8764e82b5a9e8ba7755888e34e8576 100644 (file)
@@ -1,48 +1,74 @@
-#include <fstream>
-#include <msp/path/utils.h>
+#ifndef _WIN32
+#include <unistd.h>
+#include <sys/stat.h>
+#endif
+#include <msp/fs/dir.h>
+#include <msp/fs/stat.h>
+#include <msp/fs/utils.h>
+#include <msp/io/file.h>
+#include <msp/io/print.h>
+#include "builder.h"
 #include "copy.h"
-#include "package.h"
+#include "installedfile.h"
+#include "internaltask.h"
 
 using namespace std;
 using namespace Msp;
 
-Copy::Copy(Builder &b, const Package &pkg, const Path::Path &s, const Path::Path &d):
-       Action(b),
-       src(s),
-       dest(d),
-       worker(*this)
+Copy::Copy(Builder &b):
+       Tool(b, "CP")
 {
-       announce(pkg.get_name(), "INST", dest[-1]);
+       set_run_internal(_run);
 }
 
-int Copy::check()
+Target *Copy::create_target(const vector<Target *> &sources, const string &arg)
 {
-       if(worker.get_done())
-       {
-               signal_done.emit();
-               worker.join();
-               return 0;
-       }
-       return -1;
+       FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
+       InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg);
+       inst->set_tool(*this);
+       return inst;
 }
 
-void Copy::Worker::main()
+bool Copy::_run(const InstalledFile &install)
 {
-       Path::mkpath(copy.src.subpath(0, copy.src.size()-1), 0755);
-       unlink(copy.dest.str().c_str());
-       ifstream in(copy.src.str().c_str());
-       ofstream out(copy.dest.str().c_str());
+       const FileTarget &source = install.get_source();
+       const FS::Path &src_path = source.get_path();
+       const FS::Path &dst_path = install.get_path();
+
+       try
+       {
+               IO::File in(src_path.str());
+               IO::File out(dst_path.str(), IO::M_WRITE);
 
-       char buf[16384];
-       while(!in.eof())
+               // Actual transfer loop
+               char buf[16384];
+               while(!in.eof())
+               {
+                       unsigned len = in.read(buf, sizeof(buf));
+                       out.write(buf, len);
+               }
+       }
+       catch(const exception &e)
        {
-               in.read(buf, sizeof(buf));
-               out.write(buf, in.gcount());
+               IO::print(IO::cerr, "%s\n", e.what());
+               return false;
        }
 
+#ifndef _WIN32
+       // Preserve file permissions
        struct stat st;
-       Path::stat(copy.src, st);
-       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);
+
+       const FS::Path &link = install.get_symlink();
+       if(!link.empty())
+       {
+               FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
+               if(FS::exists(link))
+                       FS::unlink(link);
+               symlink(relpath.str().c_str(), link.str().c_str());
+       }
+#endif
 
-       done=true;
+       return true;
 }