]>
Commit | Line | Data |
---|---|---|
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/aptconfiguration.h> | |
19 | #include <apt-pkg/error.h> | |
20 | #include <apt-pkg/hashes.h> | |
21 | #include <apt-pkg/fileutl.h> | |
22 | #include <apt-pkg/strutl.h> | |
23 | #include "aptmethod.h" | |
24 | ||
25 | #include <string> | |
26 | #include <sys/stat.h> | |
27 | ||
28 | #include <apti18n.h> | |
29 | /*}}}*/ | |
30 | ||
31 | class FileMethod : public aptMethod | |
32 | { | |
33 | virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; | |
34 | ||
35 | public: | |
36 | FileMethod() : aptMethod("file", "1.0", SingleInstance | SendConfig | LocalOnly) {}; | |
37 | }; | |
38 | ||
39 | // FileMethod::Fetch - Fetch a file /*{{{*/ | |
40 | // --------------------------------------------------------------------- | |
41 | /* */ | |
42 | bool FileMethod::Fetch(FetchItem *Itm) | |
43 | { | |
44 | URI Get = Itm->Uri; | |
45 | std::string File = Get.Path; | |
46 | FetchResult Res; | |
47 | if (Get.Host.empty() == false) | |
48 | return _error->Error(_("Invalid URI, local URIS must not start with //")); | |
49 | ||
50 | struct stat Buf; | |
51 | // deal with destination files which might linger around | |
52 | if (lstat(Itm->DestFile.c_str(), &Buf) == 0) | |
53 | { | |
54 | if ((Buf.st_mode & S_IFREG) != 0) | |
55 | { | |
56 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) | |
57 | { | |
58 | HashStringList const hsl = Itm->ExpectedHashes; | |
59 | if (Itm->ExpectedHashes.VerifyFile(File)) | |
60 | { | |
61 | Res.Filename = Itm->DestFile; | |
62 | Res.IMSHit = true; | |
63 | } | |
64 | } | |
65 | } | |
66 | } | |
67 | if (Res.IMSHit != true) | |
68 | RemoveFile("file", Itm->DestFile); | |
69 | ||
70 | int olderrno = 0; | |
71 | // See if the file exists | |
72 | if (stat(File.c_str(),&Buf) == 0) | |
73 | { | |
74 | Res.Size = Buf.st_size; | |
75 | Res.Filename = File; | |
76 | Res.LastModified = Buf.st_mtime; | |
77 | Res.IMSHit = false; | |
78 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) | |
79 | { | |
80 | unsigned long long const filesize = Itm->ExpectedHashes.FileSize(); | |
81 | if (filesize != 0 && filesize == Res.Size) | |
82 | Res.IMSHit = true; | |
83 | } | |
84 | ||
85 | CalculateHashes(Itm, Res); | |
86 | } | |
87 | else | |
88 | olderrno = errno; | |
89 | if (Res.IMSHit == false) | |
90 | URIStart(Res); | |
91 | ||
92 | // See if the uncompressed file exists and reuse it | |
93 | FetchResult AltRes; | |
94 | AltRes.Filename.clear(); | |
95 | std::vector<std::string> extensions = APT::Configuration::getCompressorExtensions(); | |
96 | for (std::vector<std::string>::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext) | |
97 | { | |
98 | if (APT::String::Endswith(File, *ext) == true) | |
99 | { | |
100 | std::string const unfile = File.substr(0, File.length() - ext->length()); | |
101 | if (stat(unfile.c_str(),&Buf) == 0) | |
102 | { | |
103 | AltRes.Size = Buf.st_size; | |
104 | AltRes.Filename = unfile; | |
105 | AltRes.LastModified = Buf.st_mtime; | |
106 | AltRes.IMSHit = false; | |
107 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) | |
108 | AltRes.IMSHit = true; | |
109 | break; | |
110 | } | |
111 | // no break here as we could have situations similar to '.gz' vs '.tar.gz' here | |
112 | } | |
113 | } | |
114 | ||
115 | if (AltRes.Filename.empty() == false) | |
116 | URIDone(Res,&AltRes); | |
117 | else if (Res.Filename.empty() == false) | |
118 | URIDone(Res); | |
119 | else | |
120 | { | |
121 | errno = olderrno; | |
122 | return _error->Errno(File.c_str(), _("File not found")); | |
123 | } | |
124 | ||
125 | return true; | |
126 | } | |
127 | /*}}}*/ | |
128 | ||
129 | int main() | |
130 | { | |
131 | return FileMethod().Run(); | |
132 | } |