]>
Commit | Line | Data |
---|---|---|
24231681 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz Exp $ |
24231681 AL |
4 | /* ###################################################################### |
5 | ||
6 | File URI method for APT | |
7 | ||
1e3f4083 | 8 | This simply checks that the file specified exists, if so the relevant |
24231681 AL |
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 /*{{{*/ | |
ea542140 DK |
16 | #include <config.h> |
17 | ||
93bf083d | 18 | #include <apt-pkg/acquire-method.h> |
a09f6eb8 | 19 | #include <apt-pkg/aptconfiguration.h> |
93bf083d | 20 | #include <apt-pkg/error.h> |
13e8426f MV |
21 | #include <apt-pkg/hashes.h> |
22 | #include <apt-pkg/fileutl.h> | |
472ff00e | 23 | #include <apt-pkg/strutl.h> |
24231681 | 24 | |
453b82a3 | 25 | #include <string> |
24231681 | 26 | #include <sys/stat.h> |
453b82a3 | 27 | |
d77559ac | 28 | #include <apti18n.h> |
24231681 AL |
29 | /*}}}*/ |
30 | ||
93bf083d | 31 | class FileMethod : public pkgAcqMethod |
24231681 | 32 | { |
3b302846 | 33 | virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; |
0940230d DK |
34 | virtual bool Configuration(std::string Message) APT_OVERRIDE; |
35 | ||
93bf083d | 36 | public: |
0940230d | 37 | |
a09f6eb8 | 38 | FileMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig | LocalOnly) {}; |
93bf083d | 39 | }; |
0940230d DK |
40 | bool FileMethod::Configuration(std::string Message) |
41 | { | |
42 | if (pkgAcqMethod::Configuration(Message) == false) | |
43 | return false; | |
44 | ||
45 | DropPrivsOrDie(); | |
46 | ||
47 | return true; | |
48 | } | |
9391a747 | 49 | |
93bf083d AL |
50 | // FileMethod::Fetch - Fetch a file /*{{{*/ |
51 | // --------------------------------------------------------------------- | |
52 | /* */ | |
be4401bf | 53 | bool FileMethod::Fetch(FetchItem *Itm) |
9391a747 | 54 | { |
be4401bf | 55 | URI Get = Itm->Uri; |
8f3ba4e8 | 56 | std::string File = Get.Path; |
93bf083d | 57 | FetchResult Res; |
7f25bdff | 58 | if (Get.Host.empty() == false) |
dc738e7a | 59 | return _error->Error(_("Invalid URI, local URIS must not start with //")); |
7f25bdff | 60 | |
93bf083d | 61 | struct stat Buf; |
9f697f69 DK |
62 | // deal with destination files which might linger around |
63 | if (lstat(Itm->DestFile.c_str(), &Buf) == 0) | |
64 | { | |
65 | if ((Buf.st_mode & S_IFREG) != 0) | |
66 | { | |
67 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) | |
68 | { | |
69 | HashStringList const hsl = Itm->ExpectedHashes; | |
70 | if (Itm->ExpectedHashes.VerifyFile(File)) | |
ff86d7df DK |
71 | { |
72 | Res.Filename = Itm->DestFile; | |
9f697f69 | 73 | Res.IMSHit = true; |
ff86d7df | 74 | } |
9f697f69 DK |
75 | } |
76 | } | |
77 | } | |
78 | if (Res.IMSHit != true) | |
79 | unlink(Itm->DestFile.c_str()); | |
80 | ||
81 | // See if the file exists | |
93bf083d | 82 | if (stat(File.c_str(),&Buf) == 0) |
24231681 | 83 | { |
93bf083d AL |
84 | Res.Size = Buf.st_size; |
85 | Res.Filename = File; | |
86 | Res.LastModified = Buf.st_mtime; | |
87 | Res.IMSHit = false; | |
c5ccf175 | 88 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) |
448c38bd DK |
89 | { |
90 | unsigned long long const filesize = Itm->ExpectedHashes.FileSize(); | |
91 | if (filesize != 0 && filesize == Res.Size) | |
92 | Res.IMSHit = true; | |
93 | } | |
ff86d7df DK |
94 | |
95 | Hashes Hash(Itm->ExpectedHashes); | |
96 | FileFd Fd(File, FileFd::ReadOnly); | |
97 | Hash.AddFD(Fd); | |
98 | Res.TakeHashes(Hash); | |
93bf083d | 99 | } |
ff86d7df DK |
100 | if (Res.IMSHit == false) |
101 | URIStart(Res); | |
a09f6eb8 DK |
102 | |
103 | // See if the uncompressed file exists and reuse it | |
9f697f69 DK |
104 | FetchResult AltRes; |
105 | AltRes.Filename.clear(); | |
a09f6eb8 DK |
106 | std::vector<std::string> extensions = APT::Configuration::getCompressorExtensions(); |
107 | for (std::vector<std::string>::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext) | |
93bf083d | 108 | { |
a09f6eb8 | 109 | if (APT::String::Endswith(File, *ext) == true) |
24231681 | 110 | { |
a09f6eb8 DK |
111 | std::string const unfile = File.substr(0, File.length() - ext->length() - 1); |
112 | if (stat(unfile.c_str(),&Buf) == 0) | |
113 | { | |
a09f6eb8 DK |
114 | AltRes.Size = Buf.st_size; |
115 | AltRes.Filename = unfile; | |
116 | AltRes.LastModified = Buf.st_mtime; | |
117 | AltRes.IMSHit = false; | |
118 | if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) | |
119 | AltRes.IMSHit = true; | |
9f697f69 | 120 | break; |
a09f6eb8 DK |
121 | } |
122 | // no break here as we could have situations similar to '.gz' vs '.tar.gz' here | |
123 | } | |
24231681 | 124 | } |
a09f6eb8 | 125 | |
9f697f69 DK |
126 | if (AltRes.Filename.empty() == false) |
127 | URIDone(Res,&AltRes); | |
128 | else if (Res.Filename.empty() == false) | |
129 | URIDone(Res); | |
130 | else | |
dc738e7a | 131 | return _error->Error(_("File not found")); |
13e8426f | 132 | |
93bf083d AL |
133 | return true; |
134 | } | |
135 | /*}}}*/ | |
136 | ||
137 | int main() | |
138 | { | |
b25423f6 MZ |
139 | setlocale(LC_ALL, ""); |
140 | ||
93bf083d AL |
141 | FileMethod Mth; |
142 | return Mth.Run(); | |
9391a747 | 143 | } |