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"
17 Target *Copy::create_target(const vector<Target *> &sources, const string &arg)
19 FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
20 InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg);
21 inst->set_tool(*this);
25 Task *Copy::run(const Target &target) const
27 const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
28 Worker *worker = new Worker(install);
29 InternalTask *task = new InternalTask(worker);
30 task->add_file(install.get_path());
36 void Copy::Worker::main()
38 const FileTarget &source = target.get_source();
39 const FS::Path &src_path = source.get_path();
40 const FS::Path &dst_path = target.get_path();
44 IO::File in(src_path.str());
45 IO::File out(dst_path.str(), IO::M_WRITE);
47 // Actual transfer loop
51 unsigned len = in.read(buf, sizeof(buf));
55 catch(const exception &e)
57 IO::print(IO::cerr, "%s\n", e.what());
63 // Preserve file permissions
65 if(stat(src_path.str().c_str(), &st)==0)
66 chmod(dst_path.str().c_str(), st.st_mode&0777);
68 const FS::Path &link = target.get_symlink();
71 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
74 symlink(relpath.str().c_str(), link.str().c_str());
78 status = Task::SUCCESS;