apt-key del: Ignore case when checking if a keyid exists in a keyring.
[ntk/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 <config.h>
13
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/acquire-method.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/hashes.h>
19 #include <apt-pkg/configuration.h>
20
21 #include <string>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24
25 #include <apti18n.h>
26 /*}}}*/
27
28 class CopyMethod : public pkgAcqMethod
29 {
30 virtual bool Fetch(FetchItem *Itm);
31 void CalculateHashes(FetchResult &Res);
32
33 public:
34
35 CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
36 };
37
38 void CopyMethod::CalculateHashes(FetchResult &Res)
39 {
40 Hashes Hash;
41 FileFd::CompressMode CompressMode = FileFd::None;
42 if (_config->FindB("Acquire::GzipIndexes", false) == true)
43 CompressMode = FileFd::Extension;
44
45 FileFd Fd(Res.Filename, FileFd::ReadOnly, CompressMode);
46 Hash.AddFD(Fd);
47 Res.TakeHashes(Hash);
48 }
49
50 // CopyMethod::Fetch - Fetch a file /*{{{*/
51 // ---------------------------------------------------------------------
52 /* */
53 bool CopyMethod::Fetch(FetchItem *Itm)
54 {
55 // this ensures that relative paths work in copy
56 std::string File = Itm->Uri.substr(Itm->Uri.find(':')+1);
57
58 // Stat the file and send a start message
59 struct stat Buf;
60 if (stat(File.c_str(),&Buf) != 0)
61 return _error->Errno("stat",_("Failed to stat"));
62
63 // Forumulate a result and send a start message
64 FetchResult Res;
65 Res.Size = Buf.st_size;
66 Res.Filename = Itm->DestFile;
67 Res.LastModified = Buf.st_mtime;
68 Res.IMSHit = false;
69 URIStart(Res);
70
71 // just calc the hashes if the source and destination are identical
72 if (File == Itm->DestFile)
73 {
74 CalculateHashes(Res);
75 URIDone(Res);
76 return true;
77 }
78
79 // See if the file exists
80 FileFd From(File,FileFd::ReadOnly);
81 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
82 To.EraseOnFailure();
83 if (_error->PendingError() == true)
84 {
85 To.OpFail();
86 return false;
87 }
88
89 // Copy the file
90 if (CopyFile(From,To) == false)
91 {
92 To.OpFail();
93 return false;
94 }
95
96 From.Close();
97 To.Close();
98
99 // Transfer the modification times
100 struct timeval times[2];
101 times[0].tv_sec = Buf.st_atime;
102 times[1].tv_sec = Buf.st_mtime;
103 times[0].tv_usec = times[1].tv_usec = 0;
104 if (utimes(Res.Filename.c_str(), times) != 0)
105 return _error->Errno("utimes",_("Failed to set modification time"));
106
107 CalculateHashes(Res);
108
109 URIDone(Res);
110 return true;
111 }
112 /*}}}*/
113
114 int main()
115 {
116 setlocale(LC_ALL, "");
117
118 CopyMethod Mth;
119 return Mth.Run();
120 }