X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcopy.cpp;h=31e62100d9751d991a0bf59529dd7699973e8ac5;hb=d1eb133ab529cdae131be7b150209f03189248f3;hp=23cd6f1af358cebb11e9e526a7ede7ef1af4ef0b;hpb=0d80cabf649b931b26e7055385156c75a7385d35;p=builder.git diff --git a/source/copy.cpp b/source/copy.cpp index 23cd6f1..31e6210 100644 --- a/source/copy.cpp +++ b/source/copy.cpp @@ -1,48 +1,77 @@ -#include -#include +#ifndef _WIN32 +#include +#include +#endif +#include +#include +#include +#include +#include +#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) +Target *Copy::create_target(const vector &sources, const string &arg) { - announce(pkg.get_name(), "INST", dest[-1]); + FileTarget &file_tgt = dynamic_cast(*sources.front()); + InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg); + inst->set_tool(*this); + return inst; } -int Copy::check() +Task *Copy::run(const Target &target) const { - if(worker.get_done()) - { - signal_done.emit(); - worker.join(); - return 0; - } - return -1; + const InstalledFile &install = dynamic_cast(target); + InternalTask *task = new InternalTask([&install]{ return _run(install); }); + task->add_file(install.get_path()); + task->set_unlink(); + return task; } -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; }