]>
Commit | Line | Data |
---|---|---|
561ab0db AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $ |
561ab0db AL |
4 | /* ###################################################################### |
5 | ||
6 | Copy URI - This method takes a uri like a file: uri and copies it | |
92173b19 | 7 | to the destination file. |
561ab0db AL |
8 | |
9 | ##################################################################### */ | |
10 | /*}}}*/ | |
11 | // Include Files /*{{{*/ | |
ea542140 DK |
12 | #include <config.h> |
13 | ||
561ab0db | 14 | #include <apt-pkg/fileutl.h> |
472ff00e | 15 | #include <apt-pkg/strutl.h> |
93bf083d | 16 | #include <apt-pkg/acquire-method.h> |
561ab0db | 17 | #include <apt-pkg/error.h> |
95f45727 | 18 | #include <apt-pkg/hashes.h> |
561ab0db AL |
19 | |
20 | #include <sys/stat.h> | |
21 | #include <unistd.h> | |
d77559ac | 22 | #include <apti18n.h> |
561ab0db AL |
23 | /*}}}*/ |
24 | ||
93bf083d AL |
25 | class CopyMethod : public pkgAcqMethod |
26 | { | |
be4401bf | 27 | virtual bool Fetch(FetchItem *Itm); |
93bf083d AL |
28 | |
29 | public: | |
30 | ||
31 | CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {}; | |
32 | }; | |
33 | ||
34 | // CopyMethod::Fetch - Fetch a file /*{{{*/ | |
561ab0db AL |
35 | // --------------------------------------------------------------------- |
36 | /* */ | |
be4401bf | 37 | bool CopyMethod::Fetch(FetchItem *Itm) |
561ab0db | 38 | { |
be4401bf | 39 | URI Get = Itm->Uri; |
8f3ba4e8 | 40 | std::string File = Get.Path; |
93bf083d | 41 | |
91cb4c6b AL |
42 | // Stat the file and send a start message |
43 | struct stat Buf; | |
44 | if (stat(File.c_str(),&Buf) != 0) | |
dc738e7a | 45 | return _error->Errno("stat",_("Failed to stat")); |
91cb4c6b AL |
46 | |
47 | // Forumulate a result and send a start message | |
48 | FetchResult Res; | |
49 | Res.Size = Buf.st_size; | |
50 | Res.Filename = Itm->DestFile; | |
51 | Res.LastModified = Buf.st_mtime; | |
52 | Res.IMSHit = false; | |
53 | URIStart(Res); | |
54 | ||
93bf083d AL |
55 | // See if the file exists |
56 | FileFd From(File,FileFd::ReadOnly); | |
22041bd2 | 57 | FileFd To(Itm->DestFile,FileFd::WriteAtomic); |
93bf083d AL |
58 | To.EraseOnFailure(); |
59 | if (_error->PendingError() == true) | |
91cb4c6b AL |
60 | { |
61 | To.OpFail(); | |
93bf083d | 62 | return false; |
91cb4c6b | 63 | } |
561ab0db | 64 | |
93bf083d AL |
65 | // Copy the file |
66 | if (CopyFile(From,To) == false) | |
91cb4c6b AL |
67 | { |
68 | To.OpFail(); | |
93bf083d | 69 | return false; |
91cb4c6b | 70 | } |
561ab0db | 71 | |
93bf083d | 72 | From.Close(); |
9ce3cfc9 | 73 | |
93bf083d | 74 | // Transfer the modification times |
9ce3cfc9 DK |
75 | struct timespec times[2]; |
76 | times[0].tv_sec = Buf.st_atime; | |
77 | times[1].tv_sec = Buf.st_mtime; | |
78 | times[0].tv_nsec = times[1].tv_nsec = 0; | |
79 | if (futimens(To.Fd(), times) != 0) | |
93bf083d AL |
80 | { |
81 | To.OpFail(); | |
9ce3cfc9 | 82 | return _error->Errno("futimens",_("Failed to set modification time")); |
93bf083d | 83 | } |
9ce3cfc9 DK |
84 | To.Close(); |
85 | ||
95f45727 MV |
86 | Hashes Hash; |
87 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
109eb151 | 88 | Hash.AddFD(Fd); |
95f45727 | 89 | Res.TakeHashes(Hash); |
ac3dee0e | 90 | |
93bf083d AL |
91 | URIDone(Res); |
92 | return true; | |
93 | } | |
94 | /*}}}*/ | |
95 | ||
96 | int main() | |
97 | { | |
b25423f6 MZ |
98 | setlocale(LC_ALL, ""); |
99 | ||
93bf083d AL |
100 | CopyMethod Mth; |
101 | return Mth.Run(); | |
561ab0db | 102 | } |