3 #include <msp/fs/dir.h>
4 #include <msp/fs/stat.h>
5 #include <msp/fs/utils.h>
6 #include <msp/io/file.h>
7 #include <msp/io/print.h>
10 #include "installedfile.h"
15 Copy::Copy(Builder &b):
19 Target *Copy::create_target(const list<Target *> &sources, const string &arg) const
21 FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
22 const SourcePackage &pkg = dynamic_cast<const SourcePackage &>(*file_tgt.get_package());
23 InstalledFile *inst = new InstalledFile(builder, pkg, file_tgt, arg);
24 inst->set_tool(*this);
28 Task *Copy::run(const Target &target) const
30 const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
31 Worker *worker = new Worker(install);
32 return new InternalTask(worker);
36 Copy::Worker::Worker(const InstalledFile &t):
42 void Copy::Worker::main()
44 const FileTarget &source = target.get_source();
45 const FS::Path &src_path = source.get_path();
46 const FS::Path &dst_path = target.get_path();
47 FS::mkpath(FS::dirname(dst_path), 0755);
49 // Remove old file. Not doing this would cause Bad Stuff when installing libraries.
50 if(FS::exists(dst_path))
56 catch(const exception &e)
58 IO::print(IO::cerr, "%s\n", e.what());
66 IO::File in(src_path.str());
67 IO::File out(dst_path.str(), IO::M_WRITE);
69 // Actual transfer loop
73 unsigned len = in.read(buf, sizeof(buf));
77 catch(const exception &e)
79 IO::print(IO::cerr, "%s\n", e.what());
84 // Preserve file permissions
86 if(stat(src_path.str().c_str(), &st)==0)
87 chmod(dst_path.str().c_str(), st.st_mode&0777);
89 const FS::Path &link = target.get_symlink();
92 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
93 symlink(relpath.str().c_str(), link.str().c_str());
96 status = Task::SUCCESS;