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 return new InternalTask([&install]{ return _run(install); });
32 bool Copy::_run(const InstalledFile &install)
34 const FileTarget &source = install.get_source();
35 const FS::Path &src_path = source.get_path();
36 const FS::Path &dst_path = install.get_path();
40 IO::File in(src_path.str());
41 IO::File out(dst_path.str(), IO::M_WRITE);
43 // Actual transfer loop
47 unsigned len = in.read(buf, sizeof(buf));
51 catch(const exception &e)
53 IO::print(IO::cerr, "%s\n", e.what());
58 // Preserve file permissions
60 if(stat(src_path.str().c_str(), &st)==0)
61 chmod(dst_path.str().c_str(), st.st_mode&0777);
63 const FS::Path &link = install.get_symlink();
66 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
69 symlink(relpath.str().c_str(), link.str().c_str());