]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Replace per-file copyright notices with a single file
[builder.git] / source / copy.cpp
1 #include <errno.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 "package.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Copy::Copy(Builder &b, const Package &pkg, const FS::Path &s, const FS::Path &d):
16         InternalAction(b),
17         src(s),
18         dest(d)
19 {
20         announce(pkg.get_name(), "COPY", dest.str());
21         if(builder.get_verbose()>=2)
22                 IO::print("%s -> %s\n", s, d);
23
24         if(!builder.get_dry_run())
25                 worker = new Worker(*this);
26 }
27
28
29 Copy::Worker::Worker(Copy &c):
30         copy(c)
31 {
32         launch();
33 }
34
35 void Copy::Worker::main()
36 {
37         FS::mkpath(FS::dirname(copy.dest), 0755);
38
39         // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
40         if(FS::exists(copy.dest))
41         {
42                 try
43                 {
44                         unlink(copy.dest);
45                 }
46                 catch(const exception &e)
47                 {
48                         IO::print(IO::cerr, "%s\n", e.what());
49                         done = error = true;
50                         return;
51                 }
52         }
53
54         try
55         {
56                 IO::File in(copy.src.str());
57                 IO::File out(copy.dest.str(), IO::M_WRITE);
58
59                 // Actual transfer loop
60                 char buf[16384];
61                 while(!in.eof())
62                 {
63                         unsigned len = in.read(buf, sizeof(buf));
64                         out.write(buf, len);
65                 }
66         }
67         catch(const exception &e)
68         {
69                 IO::print(IO::cerr, "%s\n", e.what());
70                 done = error = true;
71                 return;
72         }
73
74         // Preserve file permissions
75         struct stat st;
76         if(stat(copy.src.str().c_str(), &st)==0)
77                 chmod(copy.dest.str().c_str(), st.st_mode&0777);
78
79         done = true;
80 }