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