]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Adapt to changes in msppath
[builder.git] / source / copy.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <errno.h>
9 #include <fstream>
10 #include <iostream>
11 #include <msp/path/utils.h>
12 #include "builder.h"
13 #include "copy.h"
14 #include "package.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 Copy::Copy(Builder &b, const Package &pkg, const Path &s, const Path &d):
20         InternalAction(b),
21         src(s),
22         dest(d)
23 {
24         announce(pkg.get_name(), "COPY", dest[-1]);
25         if(builder.get_verbose()>=2)
26                 cout<<s<<" -> "<<d<<'\n';
27
28         if(!builder.get_dry_run())
29                 worker=new Worker(*this);
30 }
31
32
33 Copy::Worker::Worker(Copy &c):
34         copy(c)
35 {
36         launch();
37 }
38
39 void Copy::Worker::main()
40 {
41         mkpath(copy.dest.subpath(0, copy.dest.size()-1), 0755);
42
43         try
44         {
45                 // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
46                 unlink(copy.dest);
47         }
48         catch(const SystemError &e)
49         {
50                 if(e.get_error_code()!=ENOENT)
51                 {
52                         cerr<<e.what()<<'\n';
53                         done=error=true;
54                         return;
55                 }
56         }
57
58         ifstream in(copy.src.str().c_str());
59         if(!in)
60         {
61                 cerr<<"Can't open "<<copy.src<<" for reading\n";
62                 done=error=true;
63                 return;
64         }
65
66         ofstream out(copy.dest.str().c_str());
67         if(!out)
68         {
69                 cerr<<"Can't open "<<copy.dest<<" for writing\n";
70                 done=error=true;
71                 return;
72         }
73
74         // Actual transfer loop
75         char buf[16384];
76         while(!in.eof())
77         {
78                 in.read(buf, sizeof(buf));
79                 out.write(buf, in.gcount());
80         }
81
82         // Preserve file permissions
83         struct stat st=stat(copy.src);
84         chmod(copy.dest.str().c_str(), st.st_mode&0777);
85
86         done=true;
87 }