]> git.saurik.com Git - apt.git/blob - methods/copy.cc
Resync
[apt.git] / methods / copy.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: copy.cc,v 1.8 2004/01/07 20:39:38 mdz Exp $
4 /* ######################################################################
5
6 Copy URI - This method takes a uri like a file: uri and copies it
7 to the destination file.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <apt-pkg/fileutl.h>
13 #include <apt-pkg/acquire-method.h>
14 #include <apt-pkg/error.h>
15
16 #include <sys/stat.h>
17 #include <utime.h>
18 #include <unistd.h>
19 #include <apti18n.h>
20 /*}}}*/
21
22 class CopyMethod : public pkgAcqMethod
23 {
24 virtual bool Fetch(FetchItem *Itm);
25
26 public:
27
28 CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {};
29 };
30
31 // CopyMethod::Fetch - Fetch a file /*{{{*/
32 // ---------------------------------------------------------------------
33 /* */
34 bool CopyMethod::Fetch(FetchItem *Itm)
35 {
36 URI Get = Itm->Uri;
37 string File = Get.Path;
38
39 // Stat the file and send a start message
40 struct stat Buf;
41 if (stat(File.c_str(),&Buf) != 0)
42 return _error->Errno("stat",_("Failed to stat"));
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
52 // See if the file exists
53 FileFd From(File,FileFd::ReadOnly);
54 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
55 To.EraseOnFailure();
56 if (_error->PendingError() == true)
57 {
58 To.OpFail();
59 return false;
60 }
61
62 // Copy the file
63 if (CopyFile(From,To) == false)
64 {
65 To.OpFail();
66 return false;
67 }
68
69 From.Close();
70 To.Close();
71
72 // Transfer the modification times
73 struct utimbuf TimeBuf;
74 TimeBuf.actime = Buf.st_atime;
75 TimeBuf.modtime = Buf.st_mtime;
76 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
77 {
78 To.OpFail();
79 return _error->Errno("utime",_("Failed to set modification time"));
80 }
81
82 URIDone(Res);
83 return true;
84 }
85 /*}}}*/
86
87 int main()
88 {
89 CopyMethod Mth;
90 return Mth.Run();
91 }