]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Copy URI - This method takes a uri like a file: uri and copies it | |
7 | to the destination file. | |
8 | ||
9 | ##################################################################### */ | |
10 | /*}}}*/ | |
11 | // Include Files /*{{{*/ | |
12 | #include <config.h> | |
13 | ||
14 | #include <apt-pkg/fileutl.h> | |
15 | #include <apt-pkg/strutl.h> | |
16 | #include <apt-pkg/error.h> | |
17 | #include <apt-pkg/hashes.h> | |
18 | #include <apt-pkg/configuration.h> | |
19 | #include "aptmethod.h" | |
20 | ||
21 | #include <string> | |
22 | #include <sys/stat.h> | |
23 | #include <sys/time.h> | |
24 | ||
25 | #include <apti18n.h> | |
26 | /*}}}*/ | |
27 | ||
28 | class CopyMethod : public aptMethod | |
29 | { | |
30 | virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; | |
31 | ||
32 | public: | |
33 | ||
34 | CopyMethod() : aptMethod("copy", "1.0",SingleInstance | SendConfig) {}; | |
35 | }; | |
36 | ||
37 | // CopyMethod::Fetch - Fetch a file /*{{{*/ | |
38 | // --------------------------------------------------------------------- | |
39 | /* */ | |
40 | bool CopyMethod::Fetch(FetchItem *Itm) | |
41 | { | |
42 | // this ensures that relative paths work in copy | |
43 | std::string const File = Itm->Uri.substr(Itm->Uri.find(':')+1); | |
44 | ||
45 | // Stat the file and send a start message | |
46 | struct stat Buf; | |
47 | if (stat(File.c_str(),&Buf) != 0) | |
48 | return _error->Errno("stat",_("Failed to stat")); | |
49 | ||
50 | // Forumulate a result and send a start message | |
51 | FetchResult Res; | |
52 | Res.Size = Buf.st_size; | |
53 | Res.Filename = Itm->DestFile; | |
54 | Res.LastModified = Buf.st_mtime; | |
55 | Res.IMSHit = false; | |
56 | URIStart(Res); | |
57 | ||
58 | // just calc the hashes if the source and destination are identical | |
59 | if (File == Itm->DestFile || Itm->DestFile == "/dev/null") | |
60 | { | |
61 | CalculateHashes(Itm, Res); | |
62 | URIDone(Res); | |
63 | return true; | |
64 | } | |
65 | ||
66 | // See if the file exists | |
67 | FileFd From(File,FileFd::ReadOnly); | |
68 | FileFd To(Itm->DestFile,FileFd::WriteAtomic); | |
69 | To.EraseOnFailure(); | |
70 | ||
71 | // Copy the file | |
72 | if (CopyFile(From,To) == false) | |
73 | { | |
74 | To.OpFail(); | |
75 | return false; | |
76 | } | |
77 | ||
78 | From.Close(); | |
79 | To.Close(); | |
80 | ||
81 | // Transfer the modification times | |
82 | struct timeval times[2]; | |
83 | times[0].tv_sec = Buf.st_atime; | |
84 | times[1].tv_sec = Buf.st_mtime; | |
85 | times[0].tv_usec = times[1].tv_usec = 0; | |
86 | if (utimes(Res.Filename.c_str(), times) != 0) | |
87 | return _error->Errno("utimes",_("Failed to set modification time")); | |
88 | ||
89 | CalculateHashes(Itm, Res); | |
90 | URIDone(Res); | |
91 | return true; | |
92 | } | |
93 | /*}}}*/ | |
94 | ||
95 | int main() | |
96 | { | |
97 | return CopyMethod().Run(); | |
98 | } |