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