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