]> git.saurik.com Git - apt.git/blame - methods/copy.cc
Lots and lots of i18n updates.
[apt.git] / methods / copy.cc
CommitLineData
561ab0db
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
dc738e7a 3// $Id: copy.cc,v 1.7 2003/02/10 07:34:41 doogie 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 /*{{{*/
dc738e7a 12#include <apti18n.h>
561ab0db 13#include <apt-pkg/fileutl.h>
93bf083d 14#include <apt-pkg/acquire-method.h>
561ab0db
AL
15#include <apt-pkg/error.h>
16
17#include <sys/stat.h>
92173b19 18#include <utime.h>
561ab0db 19#include <unistd.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{
89 CopyMethod Mth;
90 return Mth.Run();
561ab0db 91}