]> git.saurik.com Git - apt.git/blob - methods/file.cc
ensure valid or remove destination file in file method
[apt.git] / methods / file.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
5
6 File URI method for APT
7
8 This simply checks that the file specified exists, if so the relevant
9 information is returned. If a .gz filename is specified then the file
10 name with .gz removed will also be checked and information about it
11 will be returned in Alt-*
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #include <config.h>
17
18 #include <apt-pkg/acquire-method.h>
19 #include <apt-pkg/aptconfiguration.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/hashes.h>
22 #include <apt-pkg/fileutl.h>
23 #include <apt-pkg/strutl.h>
24
25 #include <string>
26 #include <sys/stat.h>
27
28 #include <apti18n.h>
29 /*}}}*/
30
31 class FileMethod : public pkgAcqMethod
32 {
33 virtual bool Fetch(FetchItem *Itm);
34
35 public:
36
37 FileMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig | LocalOnly) {};
38 };
39
40 // FileMethod::Fetch - Fetch a file /*{{{*/
41 // ---------------------------------------------------------------------
42 /* */
43 bool FileMethod::Fetch(FetchItem *Itm)
44 {
45 URI Get = Itm->Uri;
46 std::string File = Get.Path;
47 FetchResult Res;
48 if (Get.Host.empty() == false)
49 return _error->Error(_("Invalid URI, local URIS must not start with //"));
50
51 struct stat Buf;
52 // deal with destination files which might linger around
53 if (lstat(Itm->DestFile.c_str(), &Buf) == 0)
54 {
55 if ((Buf.st_mode & S_IFREG) != 0)
56 {
57 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
58 {
59 HashStringList const hsl = Itm->ExpectedHashes;
60 if (Itm->ExpectedHashes.VerifyFile(File))
61 Res.IMSHit = true;
62 }
63 }
64 }
65 if (Res.IMSHit != true)
66 unlink(Itm->DestFile.c_str());
67
68 // See if the file exists
69 if (stat(File.c_str(),&Buf) == 0)
70 {
71 Res.Size = Buf.st_size;
72 Res.Filename = File;
73 Res.LastModified = Buf.st_mtime;
74 Res.IMSHit = false;
75 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
76 {
77 unsigned long long const filesize = Itm->ExpectedHashes.FileSize();
78 if (filesize != 0 && filesize == Res.Size)
79 Res.IMSHit = true;
80 }
81 }
82
83 // See if the uncompressed file exists and reuse it
84 FetchResult AltRes;
85 AltRes.Filename.clear();
86 std::vector<std::string> extensions = APT::Configuration::getCompressorExtensions();
87 for (std::vector<std::string>::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
88 {
89 if (APT::String::Endswith(File, *ext) == true)
90 {
91 std::string const unfile = File.substr(0, File.length() - ext->length() - 1);
92 if (stat(unfile.c_str(),&Buf) == 0)
93 {
94 AltRes.Size = Buf.st_size;
95 AltRes.Filename = unfile;
96 AltRes.LastModified = Buf.st_mtime;
97 AltRes.IMSHit = false;
98 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
99 AltRes.IMSHit = true;
100 break;
101 }
102 // no break here as we could have situations similar to '.gz' vs '.tar.gz' here
103 }
104 }
105
106 if (Res.Filename.empty() == false)
107 {
108 Hashes Hash(Itm->ExpectedHashes);
109 FileFd Fd(Res.Filename, FileFd::ReadOnly);
110 Hash.AddFD(Fd);
111 Res.TakeHashes(Hash);
112 }
113
114 if (AltRes.Filename.empty() == false)
115 URIDone(Res,&AltRes);
116 else if (Res.Filename.empty() == false)
117 URIDone(Res);
118 else
119 return _error->Error(_("File not found"));
120
121 return true;
122 }
123 /*}}}*/
124
125 int main()
126 {
127 setlocale(LC_ALL, "");
128
129 FileMethod Mth;
130 return Mth.Run();
131 }