]>
Commit | Line | Data |
---|---|---|
561ab0db AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
93bf083d | 3 | // $Id: copy.cc,v 1.4 1998/10/30 07:53:51 jgg 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 /*{{{*/ | |
12 | #include <apt-pkg/fileutl.h> | |
93bf083d | 13 | #include <apt-pkg/acquire-method.h> |
561ab0db AL |
14 | #include <apt-pkg/error.h> |
15 | ||
16 | #include <sys/stat.h> | |
92173b19 | 17 | #include <utime.h> |
561ab0db | 18 | #include <unistd.h> |
561ab0db AL |
19 | /*}}}*/ |
20 | ||
93bf083d AL |
21 | class CopyMethod : public pkgAcqMethod |
22 | { | |
23 | virtual bool Fetch(string Message,URI Get); | |
24 | ||
25 | public: | |
26 | ||
27 | CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {}; | |
28 | }; | |
29 | ||
30 | // CopyMethod::Fetch - Fetch a file /*{{{*/ | |
561ab0db AL |
31 | // --------------------------------------------------------------------- |
32 | /* */ | |
93bf083d | 33 | bool CopyMethod::Fetch(string Message,URI Get) |
561ab0db | 34 | { |
93bf083d AL |
35 | string File = Get.Path; |
36 | ||
37 | // See if the file exists | |
38 | FileFd From(File,FileFd::ReadOnly); | |
39 | FileFd To(DestFile,FileFd::WriteEmpty); | |
40 | To.EraseOnFailure(); | |
41 | if (_error->PendingError() == true) | |
42 | return false; | |
561ab0db | 43 | |
93bf083d AL |
44 | // Copy the file |
45 | if (CopyFile(From,To) == false) | |
46 | return false; | |
561ab0db | 47 | |
93bf083d AL |
48 | From.Close(); |
49 | To.Close(); | |
561ab0db | 50 | |
93bf083d AL |
51 | // Transfer the modification times |
52 | struct stat Buf; | |
53 | if (stat(File.c_str(),&Buf) != 0) | |
561ab0db | 54 | { |
93bf083d AL |
55 | To.OpFail(); |
56 | return _error->Errno("stat","Failed to stat"); | |
561ab0db AL |
57 | } |
58 | ||
93bf083d AL |
59 | struct utimbuf TimeBuf; |
60 | TimeBuf.actime = Buf.st_atime; | |
61 | TimeBuf.modtime = Buf.st_mtime; | |
62 | if (utime(DestFile.c_str(),&TimeBuf) != 0) | |
63 | { | |
64 | To.OpFail(); | |
65 | return _error->Errno("utime","Failed to set modification time"); | |
66 | } | |
67 | ||
68 | // Forumulate a result | |
69 | FetchResult Res; | |
70 | Res.Size = Buf.st_size; | |
71 | Res.Filename = DestFile; | |
72 | Res.LastModified = Buf.st_mtime; | |
73 | Res.IMSHit = false; | |
74 | ||
75 | URIDone(Res); | |
76 | return true; | |
77 | } | |
78 | /*}}}*/ | |
79 | ||
80 | int main() | |
81 | { | |
82 | CopyMethod Mth; | |
83 | return Mth.Run(); | |
561ab0db | 84 | } |