5 #include <msp/builder/builder.h>
6 #include <msp/builder/installedfile.h>
7 #include <msp/fs/dir.h>
8 #include <msp/fs/stat.h>
9 #include <msp/fs/utils.h>
10 #include <msp/io/file.h>
11 #include <msp/io/print.h>
17 Copy::Copy(Builder &b):
20 set_run_internal(_run);
23 Target *Copy::create_target(const vector<Target *> &sources, const string &arg)
25 FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
26 InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg);
27 inst->set_tool(*this);
31 bool Copy::_run(const InstalledFile &install)
33 const FileTarget &source = install.get_source();
34 const FS::Path &src_path = source.get_path();
35 const FS::Path &dst_path = install.get_path();
39 IO::File in(src_path.str());
40 IO::File out(dst_path.str(), IO::M_WRITE);
42 // Actual transfer loop
46 unsigned len = in.read(buf, sizeof(buf));
50 catch(const exception &e)
52 IO::print(IO::cerr, "%s\n", e.what());
57 // Preserve file permissions
59 if(stat(src_path.str().c_str(), &st)==0)
60 chmod(dst_path.str().c_str(), st.st_mode&0777);
62 const FS::Path &link = install.get_symlink();
65 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
68 symlink(relpath.str().c_str(), link.str().c_str());