]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Don't start tasks automatically upon creation
[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) const
20 {
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);
25         return inst;
26 }
27
28 Task *Copy::run(const Target &target) const
29 {
30         const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
31         Worker *worker = new Worker(install);
32         return new InternalTask(worker);
33 }
34
35
36 Copy::Worker::Worker(const InstalledFile &t):
37         target(t)
38 { }
39
40 void Copy::Worker::main()
41 {
42         const FileTarget &source = target.get_source();
43         const FS::Path &src_path = source.get_path();
44         const FS::Path &dst_path = target.get_path();
45         FS::mkpath(FS::dirname(dst_path), 0755);
46
47         // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
48         if(FS::exists(dst_path))
49         {
50                 try
51                 {
52                         unlink(dst_path);
53                 }
54                 catch(const exception &e)
55                 {
56                         IO::print(IO::cerr, "%s\n", e.what());
57                         status = Task::ERROR;
58                         return;
59                 }
60         }
61
62         try
63         {
64                 IO::File in(src_path.str());
65                 IO::File out(dst_path.str(), IO::M_WRITE);
66
67                 // Actual transfer loop
68                 char buf[16384];
69                 while(!in.eof())
70                 {
71                         unsigned len = in.read(buf, sizeof(buf));
72                         out.write(buf, len);
73                 }
74         }
75         catch(const exception &e)
76         {
77                 IO::print(IO::cerr, "%s\n", e.what());
78                 status = Task::ERROR;
79                 return;
80         }
81
82         // Preserve file permissions
83         struct stat st;
84         if(stat(src_path.str().c_str(), &st)==0)
85                 chmod(dst_path.str().c_str(), st.st_mode&0777);
86
87         const FS::Path &link = target.get_symlink();
88         if(!link.empty())
89         {
90                 FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
91                 symlink(relpath.str().c_str(), link.str().c_str());
92         }
93
94         status = Task::SUCCESS;
95 }