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