X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/24231681aa99e84c8a6aae5e54b7e207e19d6087..124e6916b7b02984803ff8217e8163947aae2882:/methods/file.cc diff --git a/methods/file.cc b/methods/file.cc index fc42c2968..36f3c39b9 100644 --- a/methods/file.cc +++ b/methods/file.cc @@ -1,11 +1,11 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: file.cc,v 1.3 1998/10/23 00:50:02 jgg Exp $ +// $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz Exp $ /* ###################################################################### File URI method for APT - This simply checks that the file specified exists, if so the relevent + This simply checks that the file specified exists, if so the relevant information is returned. If a .gz filename is specified then the file name with .gz removed will also be checked and information about it will be returned in Alt-* @@ -13,132 +13,124 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include + +#include +#include +#include +#include #include -#include +#include +#include "aptmethod.h" +#include #include -#include -#include + +#include /*}}}*/ -// Fail - Generate a failure message /*{{{*/ +class FileMethod : public aptMethod +{ + virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; + + public: + FileMethod() : aptMethod("file", "1.0", SingleInstance | SendConfig | LocalOnly) {}; +}; + +// FileMethod::Fetch - Fetch a file /*{{{*/ // --------------------------------------------------------------------- /* */ -void Fail(string URI) +bool FileMethod::Fetch(FetchItem *Itm) { - printf("400 URI Failure\n" - "URI: %s\n" - "Message: File does not exist\n\n",URI.c_str()); -} - /*}}}*/ + URI Get = Itm->Uri; + std::string File = Get.Path; + FetchResult Res; + if (Get.Host.empty() == false) + return _error->Error(_("Invalid URI, local URIS must not start with //")); -int main() -{ - setlinebuf(stdout); - SetNonBlock(STDIN_FILENO,true); - - printf("100 Capabilities\n" - "Version: 1.0\n" - "Pipeline: true\n\n"); - - vector Messages; - while (1) + struct stat Buf; + // deal with destination files which might linger around + if (lstat(Itm->DestFile.c_str(), &Buf) == 0) { - if (WaitFd(STDIN_FILENO) == false || - ReadMessages(STDIN_FILENO,Messages) == false) - return 0; - - while (Messages.empty() == false) + if ((Buf.st_mode & S_IFREG) != 0) { - string Message = Messages.front(); - Messages.erase(Messages.begin()); - - // Fetch the message number - char *End; - int Number = strtol(Message.c_str(),&End,10); - if (End == Message.c_str()) - { - cerr << "Malformed message!" << endl; - return 100; - } - - // We only understand 600 URI Fetch messages - if (Number != 600) - continue; - - // Grab the URI bit - string URI = LookupTag(Message,"URI"); - - // Grab the filename - string::size_type Pos = URI.find(':'); - if (Pos == string::npos) + if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) { - Fail(URI); - continue; - } - string File = string(URI,Pos+1); - - // Grab the modification time - time_t LastMod; - string LTime = LookupTag(Message,"Last-Modified"); - if (LTime.empty() == false && StrToTime(LTime,LastMod) == false) - LTime = string(); - - // Start the reply message - string Result = "201 URI Done"; - Result += "\nURI: " + URI; - - // See if the file exists - struct stat Buf; - bool Ok = false; - if (stat(File.c_str(),&Buf) == 0) - { - char S[300]; - sprintf(S,"\nSize: %ld",Buf.st_size); - - Result += "\nFilename: " + File; - Result += S; - Result += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime); - if (LTime.empty() == false && LastMod == Buf.st_mtime) - Result += "\nIMS-Hit: true"; - - Ok = true; - } - - // See if we can compute a file without a .gz exentsion - Pos = File.rfind(".gz"); - if (Pos + 3 == File.length()) - { - File = string(File,0,Pos); - if (stat(File.c_str(),&Buf) == 0) + HashStringList const hsl = Itm->ExpectedHashes; + if (Itm->ExpectedHashes.VerifyFile(File)) { - char S[300]; - sprintf(S,"\nAlt-Size: %ld",Buf.st_size); - - Result += "\nAlt-Filename: " + File; - Result += S; - Result += "\nAlt-Last-Modified: " + TimeRFC1123(Buf.st_mtime); - if (LTime.empty() == false && LastMod == Buf.st_mtime) - Result += "\nAlt-IMS-Hit: true"; - - Ok = true; + Res.Filename = Itm->DestFile; + Res.IMSHit = true; } } - - // Did we find something? - if (Ok == false) + } + } + if (Res.IMSHit != true) + RemoveFile("file", Itm->DestFile); + + int olderrno = 0; + // See if the file exists + if (stat(File.c_str(),&Buf) == 0) + { + Res.Size = Buf.st_size; + Res.Filename = File; + Res.LastModified = Buf.st_mtime; + Res.IMSHit = false; + if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) + { + unsigned long long const filesize = Itm->ExpectedHashes.FileSize(); + if (filesize != 0 && filesize == Res.Size) + Res.IMSHit = true; + } + + CalculateHashes(Itm, Res); + } + else + olderrno = errno; + if (Res.IMSHit == false) + URIStart(Res); + + // See if the uncompressed file exists and reuse it + FetchResult AltRes; + AltRes.Filename.clear(); + std::vector extensions = APT::Configuration::getCompressorExtensions(); + for (std::vector::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext) + { + if (APT::String::Endswith(File, *ext) == true) + { + std::string const unfile = File.substr(0, File.length() - ext->length()); + if (stat(unfile.c_str(),&Buf) == 0) { - Fail(URI); - continue; + AltRes.Size = Buf.st_size; + AltRes.Filename = unfile; + AltRes.LastModified = Buf.st_mtime; + AltRes.IMSHit = false; + if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0) + AltRes.IMSHit = true; + break; } - Result += "\n\n"; - - // Send the message - if (write(STDOUT_FILENO,Result.begin(),Result.length()) != - (signed)Result.length()) - return 100; - } + // no break here as we could have situations similar to '.gz' vs '.tar.gz' here + } } - - return 0; + + if (AltRes.Filename.empty() == false) + URIDone(Res,&AltRes); + else if (Res.Filename.empty() == false) + URIDone(Res); + else + { + errno = olderrno; + return _error->Errno(File.c_str(), _("File not found")); + } + + return true; +} + /*}}}*/ + +int main() +{ + setlocale(LC_ALL, ""); + + FileMethod Mth; + return Mth.Run(); }