]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Add Id tag to all files
[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::Path &s, const Path::Path &d):
20         Action(b),
21         src(s),
22         dest(d),
23         worker(0)
24 {
25         announce(pkg.get_name(), "COPY", dest[-1]);
26         if(builder.get_verbose()>=2)
27                 cout<<s<<" -> "<<d<<'\n';
28         
29         if(!builder.get_dry_run())
30                 worker=new Worker(*this);
31 }
32
33 int Copy::check()
34 {
35         if(!worker)  // True for dry run
36         {
37                 signal_done.emit();
38                 return 0;
39         }
40         
41         if(worker->get_done())
42         {
43                 signal_done.emit();
44                 worker->join();
45                 return worker->get_error()?1:0;
46         }
47         
48         return -1;
49 }
50
51 Copy::~Copy()
52 {
53         delete worker;
54 }
55
56 void Copy::Worker::main()
57 {
58         Path::mkpath(copy.dest.subpath(0, copy.dest.size()-1), 0755);
59         
60         // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
61         if(unlink(copy.dest.str().c_str())<0 && errno!=ENOENT)
62         {
63                 int err=errno;
64                 cerr<<"Can't unlink "<<copy.dest<<": "<<strerror(err)<<'\n';
65                 done=error=true;
66                 return;
67         }
68
69         ifstream in(copy.src.str().c_str());
70         if(!in)
71         {
72                 cerr<<"Can't open "<<copy.src<<" for reading\n";
73                 done=error=true;
74                 return;
75         }
76
77         ofstream out(copy.dest.str().c_str());
78         if(!out)
79         {
80                 cerr<<"Can't open "<<copy.dest<<" for writing\n";
81                 done=error=true;
82                 return;
83         }
84
85         // Actual transfer loop
86         char buf[16384];
87         while(!in.eof())
88         {
89                 in.read(buf, sizeof(buf));
90                 out.write(buf, in.gcount());
91         }
92
93         // Preserve file permissions
94         struct stat st;
95         Path::stat(copy.src, st);
96         chmod(copy.dest.str().c_str(), st.st_mode&0777);
97
98         done=true;
99 }