X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/92173b19af439ac49d842902a3a57105d396d7d8..efcbb0f0c0ec92d6b61c6349a5ad69952939272e:/methods/copy.cc diff --git a/methods/copy.cc b/methods/copy.cc index c1cc26a69..b78053d36 100644 --- a/methods/copy.cc +++ b/methods/copy.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: copy.cc,v 1.2 1998/10/25 07:07:29 jgg Exp $ +// $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $ /* ###################################################################### Copy URI - This method takes a uri like a file: uri and copies it @@ -9,130 +9,115 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include + #include -#include +#include +#include #include +#include +#include +#include #include -#include -#include -#include +#include + +#include /*}}}*/ -// Fail - Generate a failure message /*{{{*/ -// --------------------------------------------------------------------- -/* */ -void Fail(string URI) +class CopyMethod : public pkgAcqMethod { - string Err = "Undetermined Error"; - if (_error->empty() == false) - _error->PopMessage(Err); + virtual bool Fetch(FetchItem *Itm); + void CalculateHashes(FetchResult &Res); - printf("400 URI Failure\n" - "URI: %s\n" - "Message: %s\n\n",URI.c_str(),Err.c_str()); - _error->Discard(); + public: + + CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {}; +}; + +void CopyMethod::CalculateHashes(FetchResult &Res) +{ + // For gzip indexes we need to look inside the gzip for the hash + // We can not use the extension here as its not used in partial + // on a IMS hit + FileFd::OpenMode OpenMode = FileFd::ReadOnly; + if (_config->FindB("Acquire::GzipIndexes", false) == true) + OpenMode = FileFd::ReadOnlyGzip; + + Hashes Hash; + FileFd Fd(Res.Filename, OpenMode); + Hash.AddFD(Fd); + Res.TakeHashes(Hash); } - /*}}}*/ -int main() +// CopyMethod::Fetch - Fetch a file /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool CopyMethod::Fetch(FetchItem *Itm) { - setlinebuf(stdout); - SetNonBlock(STDIN_FILENO,true); + // this ensures that relative paths work in copy + std::string File = Itm->Uri.substr(Itm->Uri.find(':')+1); + + // Stat the file and send a start message + struct stat Buf; + if (stat(File.c_str(),&Buf) != 0) + return _error->Errno("stat",_("Failed to stat")); + + // Forumulate a result and send a start message + FetchResult Res; + Res.Size = Buf.st_size; + Res.Filename = Itm->DestFile; + Res.LastModified = Buf.st_mtime; + Res.IMSHit = false; + URIStart(Res); - printf("100 Capabilities\n" - "Version: 1.0\n" - "Pipeline: true\n\n"); - - vector Messages; - while (1) + // just calc the hashes if the source and destination are identical + if (File == Itm->DestFile) { - if (WaitFd(STDIN_FILENO) == false || - ReadMessages(STDIN_FILENO,Messages) == false) - return 0; - - while (Messages.empty() == false) - { - 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"); - string Target = LookupTag(Message,"Filename"); - - // Grab the filename - string::size_type Pos = URI.find(':'); - if (Pos == string::npos) - { - _error->Error("Invalid message"); - Fail(URI); - continue; - } - string File = string(URI,Pos+1); - - // Start the reply message - string Result = "201 URI Done"; - Result += "\nURI: " + URI; - Result += "\nFileName: " + Target; - - // See if the file exists - FileFd From(File,FileFd::ReadOnly); - FileFd To(Target,FileFd::WriteEmpty); - To.EraseOnFailure(); - if (_error->PendingError() == true) - { - Fail(URI); - continue; - } - - // Copy the file - if (CopyFile(From,To) == false) - { - Fail(URI); - continue; - } - - From.Close(); - To.Close(); - - // Transfer the modification times - struct stat Buf; - if (stat(File.c_str(),&Buf) != 0) - { - _error->Errno("stat","Failed to stat"); - Fail(URI); - continue; - } - struct utimbuf TimeBuf; - TimeBuf.actime = Buf.st_atime; - TimeBuf.modtime = Buf.st_mtime; - if (utime(Target.c_str(),&TimeBuf) != 0) - { - _error->Errno("utime","Failed to set modification time"); - Fail(URI); - continue; - } - - // Send the message - Result += "\n\n"; - if (write(STDOUT_FILENO,Result.begin(),Result.length()) != - (signed)Result.length()) - return 100; - } + CalculateHashes(Res); + URIDone(Res); + return true; + } + + // See if the file exists + FileFd From(File,FileFd::ReadOnly); + FileFd To(Itm->DestFile,FileFd::WriteAtomic); + To.EraseOnFailure(); + if (_error->PendingError() == true) + { + To.OpFail(); + return false; } - return 0; + // Copy the file + if (CopyFile(From,To) == false) + { + To.OpFail(); + return false; + } + + From.Close(); + To.Close(); + + // Transfer the modification times + struct timeval times[2]; + times[0].tv_sec = Buf.st_atime; + times[1].tv_sec = Buf.st_mtime; + times[0].tv_usec = times[1].tv_usec = 0; + if (utimes(Res.Filename.c_str(), times) != 0) + return _error->Errno("utimes",_("Failed to set modification time")); + + CalculateHashes(Res); + + URIDone(Res); + return true; +} + /*}}}*/ + +int main() +{ + setlocale(LC_ALL, ""); + + CopyMethod Mth; + return Mth.Run(); }