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