]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Further changes for library compatibility
[builder.git] / source / copy.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <errno.h>
9 #include <sys/stat.h>
10 #include <msp/fs/dir.h>
11 #include <msp/fs/stat.h>
12 #include <msp/fs/utils.h>
13 #include <msp/io/file.h>
14 #include <msp/io/print.h>
15 #include "builder.h"
16 #include "copy.h"
17 #include "package.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 Copy::Copy(Builder &b, const Package &pkg, const FS::Path &s, const FS::Path &d):
23         InternalAction(b),
24         src(s),
25         dest(d)
26 {
27         announce(pkg.get_name(), "COPY", dest.str());
28         if(builder.get_verbose()>=2)
29                 IO::print("%s -> %s\n", s, d);
30
31         if(!builder.get_dry_run())
32                 worker = new Worker(*this);
33 }
34
35
36 Copy::Worker::Worker(Copy &c):
37         copy(c)
38 {
39         launch();
40 }
41
42 void Copy::Worker::main()
43 {
44         FS::mkpath(FS::dirname(copy.dest), 0755);
45
46         // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
47         if(FS::exists(copy.dest))
48         {
49                 try
50                 {
51                         unlink(copy.dest);
52                 }
53                 catch(const exception &e)
54                 {
55                         IO::print(IO::cerr, "%s\n", e.what());
56                         done = error = true;
57                         return;
58                 }
59         }
60
61         try
62         {
63                 IO::File in(copy.src.str());
64                 IO::File out(copy.dest.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                 done = error = true;
78                 return;
79         }
80
81         // Preserve file permissions
82         struct stat st;
83         if(stat(copy.src.str().c_str(), &st)==0)
84                 chmod(copy.dest.str().c_str(), st.st_mode&0777);
85
86         done = true;
87 }