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