]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Inline simple constructors
[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
14 using namespace std;
15 using namespace Msp;
16
17 Target *Copy::create_target(const vector<Target *> &sources, const string &arg)
18 {
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);
22         return inst;
23 }
24
25 Task *Copy::run(const Target &target) const
26 {
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());
31         task->set_unlink();
32         return task;
33 }
34
35
36 void Copy::Worker::main()
37 {
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();
41
42         try
43         {
44                 IO::File in(src_path.str());
45                 IO::File out(dst_path.str(), IO::M_WRITE);
46
47                 // Actual transfer loop
48                 char buf[16384];
49                 while(!in.eof())
50                 {
51                         unsigned len = in.read(buf, sizeof(buf));
52                         out.write(buf, len);
53                 }
54         }
55         catch(const exception &e)
56         {
57                 IO::print(IO::cerr, "%s\n", e.what());
58                 status = Task::ERROR;
59                 return;
60         }
61
62 #ifndef _WIN32
63         // Preserve file permissions
64         struct stat st;
65         if(stat(src_path.str().c_str(), &st)==0)
66                 chmod(dst_path.str().c_str(), st.st_mode&0777);
67
68         const FS::Path &link = target.get_symlink();
69         if(!link.empty())
70         {
71                 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
72                 if(FS::exists(link))
73                         FS::unlink(link);
74                 symlink(relpath.str().c_str(), link.str().c_str());
75         }
76 #endif
77
78         status = Task::SUCCESS;
79 }