diff options
Diffstat (limited to 'methods')
-rw-r--r-- | methods/aptmethod.h | 37 | ||||
-rw-r--r-- | methods/copy.cc | 26 | ||||
-rw-r--r-- | methods/file.cc | 29 |
3 files changed, 54 insertions, 38 deletions
diff --git a/methods/aptmethod.h b/methods/aptmethod.h new file mode 100644 index 000000000..61d7b78f1 --- /dev/null +++ b/methods/aptmethod.h @@ -0,0 +1,37 @@ +#ifndef APT_APTMETHOD_H +#define APT_APTMETHOD_H + +#include <apt-pkg/acquire-method.h> + +#include <string> + +class aptMethod : public pkgAcqMethod +{ + char const * const Binary; + public: + virtual bool Configuration(std::string Message) APT_OVERRIDE; + + bool CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const; + + aptMethod(char const * const Binary, char const * const Ver, unsigned long const Flags) : pkgAcqMethod(Ver, Flags), Binary(Binary) {}; +}; +bool aptMethod::Configuration(std::string Message) +{ + if (pkgAcqMethod::Configuration(Message) == false) + return false; + + DropPrivsOrDie(); + + return true; +} +bool aptMethod::CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const +{ + Hashes Hash(Itm->ExpectedHashes); + FileFd Fd; + if (Fd.Open(Res.Filename, FileFd::ReadOnly) == false || Hash.AddFD(Fd) == false) + return false; + Res.TakeHashes(Hash); + return true; +} + +#endif diff --git a/methods/copy.cc b/methods/copy.cc index 373ad3604..e515b2def 100644 --- a/methods/copy.cc +++ b/methods/copy.cc @@ -17,6 +17,7 @@ #include <apt-pkg/error.h> #include <apt-pkg/hashes.h> #include <apt-pkg/configuration.h> +#include "aptmethod.h" #include <string> #include <sys/stat.h> @@ -25,23 +26,14 @@ #include <apti18n.h> /*}}}*/ -class CopyMethod : public pkgAcqMethod +class CopyMethod : public aptMethod { virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; - void CalculateHashes(FetchItem const * const Itm, FetchResult &Res); - + public: - - CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {}; -}; -void CopyMethod::CalculateHashes(FetchItem const * const Itm, FetchResult &Res) -{ - Hashes Hash(Itm->ExpectedHashes); - FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd); - Res.TakeHashes(Hash); -} + CopyMethod() : aptMethod("copy", "1.0",SingleInstance | SendConfig) {}; +}; // CopyMethod::Fetch - Fetch a file /*{{{*/ // --------------------------------------------------------------------- @@ -76,12 +68,7 @@ bool CopyMethod::Fetch(FetchItem *Itm) FileFd From(File,FileFd::ReadOnly); FileFd To(Itm->DestFile,FileFd::WriteAtomic); To.EraseOnFailure(); - if (_error->PendingError() == true) - { - To.OpFail(); - return false; - } - + // Copy the file if (CopyFile(From,To) == false) { @@ -101,7 +88,6 @@ bool CopyMethod::Fetch(FetchItem *Itm) return _error->Errno("utimes",_("Failed to set modification time")); CalculateHashes(Itm, Res); - URIDone(Res); return true; } diff --git a/methods/file.cc b/methods/file.cc index 8a087c36d..4e3410078 100644 --- a/methods/file.cc +++ b/methods/file.cc @@ -21,6 +21,7 @@ #include <apt-pkg/hashes.h> #include <apt-pkg/fileutl.h> #include <apt-pkg/strutl.h> +#include "aptmethod.h" #include <string> #include <sys/stat.h> @@ -28,24 +29,13 @@ #include <apti18n.h> /*}}}*/ -class FileMethod : public pkgAcqMethod +class FileMethod : public aptMethod { virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; - virtual bool Configuration(std::string Message) APT_OVERRIDE; public: - - FileMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig | LocalOnly) {}; + FileMethod() : aptMethod("file", "1.0", SingleInstance | SendConfig | LocalOnly) {}; }; -bool FileMethod::Configuration(std::string Message) -{ - if (pkgAcqMethod::Configuration(Message) == false) - return false; - - DropPrivsOrDie(); - - return true; -} // FileMethod::Fetch - Fetch a file /*{{{*/ // --------------------------------------------------------------------- @@ -78,6 +68,7 @@ bool FileMethod::Fetch(FetchItem *Itm) if (Res.IMSHit != true) RemoveFile("file", Itm->DestFile); + int olderrno = 0; // See if the file exists if (stat(File.c_str(),&Buf) == 0) { @@ -92,11 +83,10 @@ bool FileMethod::Fetch(FetchItem *Itm) Res.IMSHit = true; } - Hashes Hash(Itm->ExpectedHashes); - FileFd Fd(File, FileFd::ReadOnly); - Hash.AddFD(Fd); - Res.TakeHashes(Hash); + CalculateHashes(Itm, Res); } + else + olderrno = errno; if (Res.IMSHit == false) URIStart(Res); @@ -128,7 +118,10 @@ bool FileMethod::Fetch(FetchItem *Itm) else if (Res.Filename.empty() == false) URIDone(Res); else - return _error->Error(_("File not found")); + { + errno = olderrno; + return _error->Errno(File.c_str(), _("File not found")); + } return true; } |