5 #include <msp/fs/dir.h>
6 #include <msp/fs/stat.h>
7 #include <msp/fs/utils.h>
8 #include <msp/io/file.h>
9 #include <msp/io/print.h>
12 #include "installedfile.h"
13 #include "internaltask.h"
18 Target *Copy::create_target(const vector<Target *> &sources, const string &arg)
20 FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
21 InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg);
22 inst->set_tool(*this);
26 Task *Copy::run(const Target &target) const
28 const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
29 InternalTask *task = new InternalTask([&install]{ return _run(install); });
30 task->add_file(install.get_path());
35 bool Copy::_run(const InstalledFile &install)
37 const FileTarget &source = install.get_source();
38 const FS::Path &src_path = source.get_path();
39 const FS::Path &dst_path = install.get_path();
43 IO::File in(src_path.str());
44 IO::File out(dst_path.str(), IO::M_WRITE);
46 // Actual transfer loop
50 unsigned len = in.read(buf, sizeof(buf));
54 catch(const exception &e)
56 IO::print(IO::cerr, "%s\n", e.what());
61 // Preserve file permissions
63 if(stat(src_path.str().c_str(), &st)==0)
64 chmod(dst_path.str().c_str(), st.st_mode&0777);
66 const FS::Path &link = install.get_symlink();
69 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
72 symlink(relpath.str().c_str(), link.str().c_str());