]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Remove redundant task setup from Copy
[builder.git] / source / copy.cpp
1 #ifndef _WIN32
2 #include <unistd.h>
3 #include <sys/stat.h>
4 #endif
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>
10 #include "builder.h"
11 #include "copy.h"
12 #include "installedfile.h"
13 #include "internaltask.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 Target *Copy::create_target(const vector<Target *> &sources, const string &arg)
19 {
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);
23         return inst;
24 }
25
26 Task *Copy::run(const Target &target) const
27 {
28         const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
29         return new InternalTask([&install]{ return _run(install); });
30 }
31
32 bool Copy::_run(const InstalledFile &install)
33 {
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();
37
38         try
39         {
40                 IO::File in(src_path.str());
41                 IO::File out(dst_path.str(), IO::M_WRITE);
42
43                 // Actual transfer loop
44                 char buf[16384];
45                 while(!in.eof())
46                 {
47                         unsigned len = in.read(buf, sizeof(buf));
48                         out.write(buf, len);
49                 }
50         }
51         catch(const exception &e)
52         {
53                 IO::print(IO::cerr, "%s\n", e.what());
54                 return false;
55         }
56
57 #ifndef _WIN32
58         // Preserve file permissions
59         struct stat st;
60         if(stat(src_path.str().c_str(), &st)==0)
61                 chmod(dst_path.str().c_str(), st.st_mode&0777);
62
63         const FS::Path &link = install.get_symlink();
64         if(!link.empty())
65         {
66                 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
67                 if(FS::exists(link))
68                         FS::unlink(link);
69                 symlink(relpath.str().c_str(), link.str().c_str());
70         }
71 #endif
72
73         return true;
74 }