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 Copy::Copy(Builder &b):
21 Target *Copy::create_target(const list<Target *> &sources, const string &arg)
23 FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
24 InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg);
25 inst->set_tool(*this);
29 Task *Copy::run(const Target &target) const
31 const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
32 Worker *worker = new Worker(install);
33 InternalTask *task = new InternalTask(worker);
34 task->add_file(install.get_path());
40 Copy::Worker::Worker(const InstalledFile &t):
44 void Copy::Worker::main()
46 const FileTarget &source = target.get_source();
47 const FS::Path &src_path = source.get_path();
48 const FS::Path &dst_path = target.get_path();
52 IO::File in(src_path.str());
53 IO::File out(dst_path.str(), IO::M_WRITE);
55 // Actual transfer loop
59 unsigned len = in.read(buf, sizeof(buf));
63 catch(const exception &e)
65 IO::print(IO::cerr, "%s\n", e.what());
71 // Preserve file permissions
73 if(stat(src_path.str().c_str(), &st)==0)
74 chmod(dst_path.str().c_str(), st.st_mode&0777);
76 const FS::Path &link = target.get_symlink();
79 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
82 symlink(relpath.str().c_str(), link.str().c_str());
86 status = Task::SUCCESS;