X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcopy.cpp;h=53118de754963dc04095c11be4107a49c0ca9ab2;hb=8a98d59b2b4954f51eb3f649d3675af976154de6;hp=23cd6f1af358cebb11e9e526a7ede7ef1af4ef0b;hpb=0d80cabf649b931b26e7055385156c75a7385d35;p=builder.git diff --git a/source/copy.cpp b/source/copy.cpp index 23cd6f1..53118de 100644 --- a/source/copy.cpp +++ b/source/copy.cpp @@ -1,48 +1,87 @@ -#include -#include +#ifndef _WIN32 +#include +#include +#endif +#include +#include +#include +#include +#include +#include "builder.h" #include "copy.h" -#include "package.h" +#include "installedfile.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") +{ } + +Target *Copy::create_target(const list &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); + Worker *worker = new Worker(install); + InternalTask *task = new InternalTask(worker); + task->add_file(install.get_path()); + task->set_unlink(); + return task; } + +Copy::Worker::Worker(const InstalledFile &t): + target(t) +{ } + void Copy::Worker::main() { - 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 = target.get_source(); + const FS::Path &src_path = source.get_path(); + const FS::Path &dst_path = target.get_path(); - char buf[16384]; - while(!in.eof()) + try { - in.read(buf, sizeof(buf)); - out.write(buf, in.gcount()); + IO::File in(src_path.str()); + IO::File out(dst_path.str(), IO::M_WRITE); + + // Actual transfer loop + char buf[16384]; + while(!in.eof()) + { + unsigned len = in.read(buf, sizeof(buf)); + out.write(buf, len); + } + } + catch(const exception &e) + { + IO::print(IO::cerr, "%s\n", e.what()); + status = Task::ERROR; + return; } +#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 = target.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; + status = Task::SUCCESS; }