]>
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 | 19 | |
453b82a3 | 20 | #include <string> |
561ab0db | 21 | #include <sys/stat.h> |
246bbb61 | 22 | #include <sys/time.h> |
453b82a3 | 23 | |
d77559ac | 24 | #include <apti18n.h> |
561ab0db AL |
25 | /*}}}*/ |
26 | ||
93bf083d AL |
27 | class CopyMethod : public pkgAcqMethod |
28 | { | |
be4401bf | 29 | virtual bool Fetch(FetchItem *Itm); |
93bf083d AL |
30 | |
31 | public: | |
32 | ||
33 | CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {}; | |
34 | }; | |
35 | ||
36 | // CopyMethod::Fetch - Fetch a file /*{{{*/ | |
561ab0db AL |
37 | // --------------------------------------------------------------------- |
38 | /* */ | |
be4401bf | 39 | bool CopyMethod::Fetch(FetchItem *Itm) |
561ab0db | 40 | { |
be4401bf | 41 | URI Get = Itm->Uri; |
8f3ba4e8 | 42 | std::string File = Get.Path; |
93bf083d | 43 | |
91cb4c6b AL |
44 | // Stat the file and send a start message |
45 | struct stat Buf; | |
46 | if (stat(File.c_str(),&Buf) != 0) | |
dc738e7a | 47 | return _error->Errno("stat",_("Failed to stat")); |
91cb4c6b AL |
48 | |
49 | // Forumulate a result and send a start message | |
50 | FetchResult Res; | |
51 | Res.Size = Buf.st_size; | |
52 | Res.Filename = Itm->DestFile; | |
53 | Res.LastModified = Buf.st_mtime; | |
54 | Res.IMSHit = false; | |
55 | URIStart(Res); | |
56 | ||
93bf083d AL |
57 | // See if the file exists |
58 | FileFd From(File,FileFd::ReadOnly); | |
22041bd2 | 59 | FileFd To(Itm->DestFile,FileFd::WriteAtomic); |
93bf083d AL |
60 | To.EraseOnFailure(); |
61 | if (_error->PendingError() == true) | |
91cb4c6b AL |
62 | { |
63 | To.OpFail(); | |
93bf083d | 64 | return false; |
91cb4c6b | 65 | } |
561ab0db | 66 | |
93bf083d AL |
67 | // Copy the file |
68 | if (CopyFile(From,To) == false) | |
91cb4c6b AL |
69 | { |
70 | To.OpFail(); | |
93bf083d | 71 | return false; |
91cb4c6b | 72 | } |
561ab0db | 73 | |
93bf083d | 74 | From.Close(); |
246bbb61 | 75 | To.Close(); |
9ce3cfc9 | 76 | |
93bf083d | 77 | // Transfer the modification times |
246bbb61 | 78 | struct timeval times[2]; |
9ce3cfc9 DK |
79 | times[0].tv_sec = Buf.st_atime; |
80 | times[1].tv_sec = Buf.st_mtime; | |
246bbb61 DK |
81 | times[0].tv_usec = times[1].tv_usec = 0; |
82 | if (utimes(Res.Filename.c_str(), times) != 0) | |
83 | return _error->Errno("utimes",_("Failed to set modification time")); | |
9ce3cfc9 | 84 | |
95f45727 MV |
85 | Hashes Hash; |
86 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
109eb151 | 87 | Hash.AddFD(Fd); |
95f45727 | 88 | Res.TakeHashes(Hash); |
ac3dee0e | 89 | |
93bf083d AL |
90 | URIDone(Res); |
91 | return true; | |
92 | } | |
93 | /*}}}*/ | |
94 | ||
95 | int main() | |
96 | { | |
b25423f6 MZ |
97 | setlocale(LC_ALL, ""); |
98 | ||
93bf083d AL |
99 | CopyMethod Mth; |
100 | return Mth.Run(); | |
561ab0db | 101 | } |