]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
24be942532b83ab52a0dcfdeb8446ad29d708edf
[builder.git] / source / copy.cpp
1 #include <unistd.h>
2 #include <sys/stat.h>
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>
8 #include "builder.h"
9 #include "copy.h"
10 #include "installedfile.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Copy::Copy(Builder &b):
16         Tool(b, "CP")
17 { }
18
19 Target *Copy::create_target(const list<Target *> &sources, const string &arg)
20 {
21         FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
22         InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg);
23         inst->set_tool(*this);
24         return inst;
25 }
26
27 Task *Copy::run(const Target &target) const
28 {
29         const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
30         Worker *worker = new Worker(install);
31         return new InternalTask(worker);
32 }
33
34
35 Copy::Worker::Worker(const InstalledFile &t):
36         target(t)
37 { }
38
39 void Copy::Worker::main()
40 {
41         const FileTarget &source = target.get_source();
42         const FS::Path &src_path = source.get_path();
43         const FS::Path &dst_path = target.get_path();
44         FS::mkpath(FS::dirname(dst_path), 0755);
45
46         // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
47         if(FS::exists(dst_path))
48         {
49                 try
50                 {
51                         unlink(dst_path);
52                 }
53                 catch(const exception &e)
54                 {
55                         IO::print(IO::cerr, "%s\n", e.what());
56                         status = Task::ERROR;
57                         return;
58                 }
59         }
60
61         try
62         {
63                 IO::File in(src_path.str());
64                 IO::File out(dst_path.str(), IO::M_WRITE);
65
66                 // Actual transfer loop
67                 char buf[16384];
68                 while(!in.eof())
69                 {
70                         unsigned len = in.read(buf, sizeof(buf));
71                         out.write(buf, len);
72                 }
73         }
74         catch(const exception &e)
75         {
76                 IO::print(IO::cerr, "%s\n", e.what());
77                 status = Task::ERROR;
78                 return;
79         }
80
81         // Preserve file permissions
82         struct stat st;
83         if(stat(src_path.str().c_str(), &st)==0)
84                 chmod(dst_path.str().c_str(), st.st_mode&0777);
85
86         const FS::Path &link = target.get_symlink();
87         if(!link.empty())
88         {
89                 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
90                 if(FS::exists(link))
91                         FS::unlink(link);
92                 symlink(relpath.str().c_str(), link.str().c_str());
93         }
94
95         status = Task::SUCCESS;
96 }