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