X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcopy.cpp;h=24be942532b83ab52a0dcfdeb8446ad29d708edf;hb=df88e22a258f169b9505acb0cb8d0ba7e66af7b6;hp=2c35ef30e1b1f3467421170df7f0f41525ffa318;hpb=43bd25ffcb0b4f7882773f4676b209a99cb73c04;p=builder.git diff --git a/source/copy.cpp b/source/copy.cpp index 2c35ef3..24be942 100644 --- a/source/copy.cpp +++ b/source/copy.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -7,54 +7,61 @@ #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 FS::Path &s, const FS::Path &d): - InternalAction(b), - src(s), - dest(d) -{ - announce(pkg.get_name(), "COPY", dest.str()); - if(builder.get_verbose()>=2) - IO::print("%s -> %s\n", s, d); +Copy::Copy(Builder &b): + Tool(b, "CP") +{ } - if(!builder.get_dry_run()) - worker = new Worker(*this); +Target *Copy::create_target(const list &sources, const string &arg) +{ + 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; } - -Copy::Worker::Worker(Copy &c): - copy(c) +Task *Copy::run(const Target &target) const { - launch(); + const InstalledFile &install = dynamic_cast(target); + Worker *worker = new Worker(install); + return new InternalTask(worker); } + +Copy::Worker::Worker(const InstalledFile &t): + target(t) +{ } + 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]; @@ -67,14 +74,23 @@ 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); + + 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()); + } - done = true; + status = Task::SUCCESS; }