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